1 if [ ! "$_MEDIA_USB_SUBR" ]; then _MEDIA_USB_SUBR=1
3 # Copyright (c) 2012-2013 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." media/usb.subr
34 f_include $BSDCFG_SHARE/device.subr
35 f_include $BSDCFG_SHARE/dialog.subr
36 f_include $BSDCFG_SHARE/media/common.subr
37 f_include $BSDCFG_SHARE/struct.subr
38 f_include $BSDCFG_SHARE/variable.subr
40 BSDCFG_LIBE="/usr/libexec/bsdconfig"
41 f_include_lang $BSDCFG_LIBE/include/messages.subr
43 ############################################################ GLOBALS
47 ############################################################ FUNCTIONS
51 # Attempt to use USB as the media type. Return success if we both found and set
52 # the media type to be a USB drive.
59 f_device_find "" $DEVICE_TYPE_USB devs
60 ndevs=$( set -- $devs; echo $# )
62 if [ ${ndevs:=0} -eq 0 ]; then
63 f_show_msg "$msg_no_usb_devices_found"
65 elif [ $ndevs -gt 1 ]; then
66 local title="$msg_choose_a_usb_drive"
67 local prompt="$msg_please_select_a_usb_drive"
71 dev=$( f_device_menu \
72 "$title" "$prompt" "$hline" $DEVICE_TYPE_USB \
73 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
75 [ "$dev" ] || return $FAILURE
77 f_device_find "$dev" $DEVICE_TYPE_USB devs
78 [ "$devs" ] || return $FAILURE
79 dev="${devs%%[$IFS]*}"
81 f_struct_copy device_$dev device_media
82 [ $retval -eq $SUCCESS ] || return $FAILURE
84 f_struct_copy device_$devs device_media
87 f_struct device_media &&
88 device_media unset private
90 if f_interactive; then
92 f_struct device_media get name name
93 f_show_msg "$msg_using_usb_device" "$name"
96 f_struct device_media || return $FAILURE
99 # f_media_init_usb $device
101 # Initializes the USB media device. Returns success if able to mount the USB
102 # disk device using mount(8).
106 local dev="$1" devname err
108 device_$dev get devname devname || return $FAILURE
109 f_dprintf "Init routine called for USB device. devname=[%s]" \
112 if [ "$USB_MOUNTED" ]; then
113 f_dprintf "USB device already mounted."
117 if [ ! -e "$MOUNTPOINT" ] &&
118 ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 )
120 f_dialog_msgbox "$err"
124 if err=$( mount "$devname" "$MOUNTPOINT" 2>&1 ); then
129 err="${err#mount: }"; err="${err#$devname: }"
130 f_show_msg "$msg_error_mounting_usb_drive" \
131 "$devname" "$MOUNTPOINT" "$err"
135 # f_media_get_usb $device $file [$probe_type]
137 # Returns data from $file on a mounted USB disk device. Similar to cat(1). If
138 # $probe_type is present and non-NULL, returns success if $file exists. If
139 # $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
144 local dev="$1" file="$2" probe_type="$3"
146 f_dprintf "f_media_get_usb: dev=[%s] file=[%s] probe_type=%s" \
147 "$dev" "$file" "$probe_type"
149 f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
152 # f_media_shutdown_usb $device
154 # Shuts down the USB disk device using umount(8). Return status should be
157 f_media_shutdown_usb()
161 [ "$USB_MOUNTED" ] || return $FAILURE
163 if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then
164 err="${err#umount: }"; err="${err#*: }"
165 f_show_msg "$msg_could_not_unmount_the_ufs_partition" \
172 ############################################################ MAIN
174 f_dprintf "%s: Successfully loaded." media/usb.subr
176 fi # ! $_MEDIA_USB_SUBR