usb.subr revision 260678
1193323Sedif [ ! "$_MEDIA_USB_SUBR" ]; then _MEDIA_USB_SUBR=1 2193323Sed# 3193323Sed# Copyright (c) 2012-2013 Devin Teske 4193323Sed# All rights reserved. 5193323Sed# 6193323Sed# Redistribution and use in source and binary forms, with or without 7193323Sed# modification, are permitted provided that the following conditions 8193323Sed# are met: 9193323Sed# 1. Redistributions of source code must retain the above copyright 10193323Sed# notice, this list of conditions and the following disclaimer. 11193323Sed# 2. Redistributions in binary form must reproduce the above copyright 12193323Sed# notice, this list of conditions and the following disclaimer in the 13193323Sed# documentation and/or other materials provided with the distribution. 14193323Sed# 15252723Sdim# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16218893Sdim# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17218893Sdim# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18252723Sdim# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19252723Sdim# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20252723Sdim# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21198090Srdivacky# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22252723Sdim# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23193323Sed# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24195340Sed# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25193323Sed# SUCH DAMAGE. 26218893Sdim# 27198090Srdivacky# $FreeBSD: stable/10/usr.sbin/bsdconfig/share/media/usb.subr 260678 2014-01-15 07:49:17Z dteske $ 28193323Sed# 29193323Sed############################################################ INCLUDES 30193323Sed 31193323SedBSDCFG_SHARE="/usr/share/bsdconfig" 32218893Sdim. $BSDCFG_SHARE/common.subr || exit 1 33235633Sdimf_dprintf "%s: loading includes..." media/usb.subr 34193323Sedf_include $BSDCFG_SHARE/device.subr 35193323Sedf_include $BSDCFG_SHARE/dialog.subr 36193323Sedf_include $BSDCFG_SHARE/media/common.subr 37193323Sedf_include $BSDCFG_SHARE/struct.subr 38193323Sedf_include $BSDCFG_SHARE/variable.subr 39193323Sed 40193323SedBSDCFG_LIBE="/usr/libexec/bsdconfig" 41193323Sedf_include_lang $BSDCFG_LIBE/include/messages.subr 42193323Sed 43193323Sed############################################################ GLOBALS 44193323Sed 45193323SedUSB_MOUNTED= 46193323Sed 47193323Sed############################################################ FUNCTIONS 48193323Sed 49193323Sed# f_media_set_usb 50193323Sed# 51193323Sed# Attempt to use USB as the media type. Return success if we both found and set 52193323Sed# the media type to be a USB drive. 53193323Sed# 54193323Sedf_media_set_usb() 55193323Sed{ 56193323Sed f_media_close 57193323Sed 58193323Sed local devs ndevs 59193323Sed f_device_find "" $DEVICE_TYPE_USB devs 60193323Sed f_count ndevs $devs 61193323Sed 62193323Sed if [ ${ndevs:=0} -eq 0 ]; then 63193323Sed f_show_msg "$msg_no_usb_devices_found" 64252723Sdim return $FAILURE 65193323Sed elif [ $ndevs -gt 1 ]; then 66193323Sed local title="$msg_choose_a_usb_drive" 67193323Sed local prompt="$msg_please_select_a_usb_drive" 68193323Sed local hline="" 69193323Sed 70193323Sed local dev retval 71193323Sed dev=$( f_device_menu \ 72193323Sed "$title" "$prompt" "$hline" $DEVICE_TYPE_USB \ 73193323Sed 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) 74193323Sed retval=$? 75193323Sed [ "$dev" ] || return $FAILURE 76193323Sed 77193323Sed f_device_find "$dev" $DEVICE_TYPE_USB devs 78193323Sed [ "$devs" ] || return $FAILURE 79193323Sed dev="${devs%%[$IFS]*}" 80252723Sdim 81193323Sed f_struct_copy device_$dev device_media 82193323Sed [ $retval -eq $SUCCESS ] || return $FAILURE 83193323Sed else 84193323Sed f_struct_copy device_$devs device_media 85252723Sdim fi 86193323Sed 87193323Sed f_struct device_media && 88193323Sed device_media unset private 89193323Sed 90193323Sed if f_interactive; then 91193323Sed local name 92193323Sed f_struct device_media get name name 93193323Sed f_show_msg "$msg_using_usb_device" "$name" 94193323Sed fi 95193323Sed 96193323Sed f_struct device_media || return $FAILURE 97193323Sed} 98193323Sed 99252723Sdim# f_media_init_usb $device 100193323Sed# 101193323Sed# Initializes the USB media device. Returns success if able to mount the USB 102193323Sed# disk device using mount(8). 103193323Sed# 104252723Sdimf_media_init_usb() 105193323Sed{ 106193323Sed local funcname=f_media_init_usb 107193323Sed local dev="$1" devname err 108193323Sed 109193323Sed device_$dev get devname devname || return $FAILURE 110193323Sed f_dprintf "Init routine called for USB device. devname=[%s]" \ 111193323Sed "$devname" 112193323Sed 113193323Sed if [ "$USB_MOUNTED" ]; then 114193323Sed f_dprintf "USB device already mounted." 115193323Sed return $SUCCESS 116193323Sed fi 117193323Sed 118193323Sed if [ ! -e "$MOUNTPOINT" ]; then 119193323Sed f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" || 120193323Sed return $FAILURE 121193323Sed fi 122252723Sdim 123252723Sdim if f_eval_catch -dk err $funcname mount \ 124252723Sdim 'mount "%s" "%s"' "$devname" "$MOUNTPOINT" 125252723Sdim then 126193323Sed USB_MOUNTED=1 127193323Sed return $SUCCESS 128193323Sed fi 129193323Sed 130193323Sed err="${err#mount: }"; err="${err#$devname: }" 131193323Sed f_show_msg "$msg_error_mounting_usb_drive" \ 132193323Sed "$devname" "$MOUNTPOINT" "$err" 133193323Sed return $FAILURE 134193323Sed} 135193323Sed 136193323Sed# f_media_get_usb $device $file [$probe_type] 137193323Sed# 138193323Sed# Returns data from $file on a mounted USB disk device. Similar to cat(1). If 139193323Sed# $probe_type is present and non-NULL, returns success if $file exists. If 140252723Sdim# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to 141252723Sdim# standard-out. 142193323Sed# 143193323Sedf_media_get_usb() 144193323Sed{ 145193323Sed local dev="$1" file="$2" probe_type="$3" 146193323Sed 147193323Sed f_dprintf "f_media_get_usb: dev=[%s] file=[%s] probe_type=%s" \ 148193323Sed "$dev" "$file" "$probe_type" 149193323Sed 150193323Sed f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type" 151193323Sed} 152193323Sed 153193323Sed# f_media_shutdown_usb $device 154193323Sed# 155193323Sed# Shuts down the USB disk device using umount(8). Return status should be 156193323Sed# ignored. 157193323Sed# 158193323Sedf_media_shutdown_usb() 159193323Sed{ 160198090Srdivacky local funcname=f_media_shutdown_usb 161198090Srdivacky local dev="$1" err 162198090Srdivacky 163193323Sed [ "$USB_MOUNTED" ] || return $FAILURE 164193323Sed 165193323Sed if ! f_eval_catch -dk err $funcname umount \ 166193323Sed 'umount -f "%s"' "$MOUNTPOINT" 167193323Sed then 168193323Sed err="${err#umount: }"; err="${err#*: }" 169193323Sed f_show_msg "$msg_could_not_unmount_the_ufs_partition" \ 170193323Sed "$MOUNTPOINT" "$err" 171193323Sed else 172193323Sed USB_MOUNTED= 173193323Sed fi 174193323Sed} 175193323Sed 176193323Sed############################################################ MAIN 177193323Sed 178193323Sedf_dprintf "%s: Successfully loaded." media/usb.subr 179193323Sed 180193323Sedfi # ! $_MEDIA_USB_SUBR 181193323Sed