17323adacSDevin Teskeif [ ! "$_MEDIA_USB_SUBR" ]; then _MEDIA_USB_SUBR=1
27323adacSDevin Teske#
37323adacSDevin Teske# Copyright (c) 2012-2013 Devin Teske
4f8ea072aSDevin Teske# All rights reserved.
57323adacSDevin Teske#
67323adacSDevin Teske# Redistribution and use in source and binary forms, with or without
77323adacSDevin Teske# modification, are permitted provided that the following conditions
87323adacSDevin Teske# are met:
97323adacSDevin Teske# 1. Redistributions of source code must retain the above copyright
107323adacSDevin Teske#    notice, this list of conditions and the following disclaimer.
117323adacSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
127323adacSDevin Teske#    notice, this list of conditions and the following disclaimer in the
137323adacSDevin Teske#    documentation and/or other materials provided with the distribution.
147323adacSDevin Teske#
157323adacSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
177323adacSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
187323adacSDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
197323adacSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
217323adacSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
227323adacSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
237323adacSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247323adacSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257323adacSDevin Teske# SUCH DAMAGE.
267323adacSDevin Teske#
277323adacSDevin Teske#
287323adacSDevin Teske############################################################ INCLUDES
297323adacSDevin Teske
307323adacSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
317323adacSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
327323adacSDevin Teskef_dprintf "%s: loading includes..." media/usb.subr
337323adacSDevin Teskef_include $BSDCFG_SHARE/device.subr
347323adacSDevin Teskef_include $BSDCFG_SHARE/dialog.subr
357323adacSDevin Teskef_include $BSDCFG_SHARE/media/common.subr
361de60ff0SDevin Teskef_include $BSDCFG_SHARE/struct.subr
371de60ff0SDevin Teskef_include $BSDCFG_SHARE/variable.subr
387323adacSDevin Teske
397323adacSDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig"
407323adacSDevin Teskef_include_lang $BSDCFG_LIBE/include/messages.subr
417323adacSDevin Teske
427323adacSDevin Teske############################################################ GLOBALS
437323adacSDevin Teske
447323adacSDevin TeskeUSB_MOUNTED=
457323adacSDevin Teske
467323adacSDevin Teske############################################################ FUNCTIONS
477323adacSDevin Teske
487323adacSDevin Teske# f_media_set_usb
497323adacSDevin Teske#
507323adacSDevin Teske# Attempt to use USB as the media type. Return success if we both found and set
517323adacSDevin Teske# the media type to be a USB drive.
527323adacSDevin Teske#
537323adacSDevin Teskef_media_set_usb()
547323adacSDevin Teske{
557323adacSDevin Teske	f_media_close
567323adacSDevin Teske
577323adacSDevin Teske	local devs ndevs
587323adacSDevin Teske	f_device_find "" $DEVICE_TYPE_USB devs
59d4ae33f0SDevin Teske	f_count ndevs $devs
607323adacSDevin Teske
617323adacSDevin Teske	if [ ${ndevs:=0} -eq 0 ]; then
627079fc4eSDevin Teske		f_show_msg "$msg_no_usb_devices_found"
637323adacSDevin Teske		return $FAILURE
649ecd54f2SDevin Teske	elif [ $ndevs -eq 1 ]; then
659ecd54f2SDevin Teske		f_struct_copy $devs device_media
669ecd54f2SDevin Teske	else
679ecd54f2SDevin Teske		local dev
687323adacSDevin Teske		local title="$msg_choose_a_usb_drive"
697323adacSDevin Teske		local prompt="$msg_please_select_a_usb_drive"
709ecd54f2SDevin Teske		local hline=
717323adacSDevin Teske
727323adacSDevin Teske		dev=$( f_device_menu \
737323adacSDevin Teske			"$title" "$prompt" "$hline" $DEVICE_TYPE_USB \
749ecd54f2SDevin Teske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) ||
759ecd54f2SDevin Teske			return $FAILURE
767323adacSDevin Teske
779ecd54f2SDevin Teske		f_struct_copy "$dev" device_media
787323adacSDevin Teske	fi
797323adacSDevin Teske
807323adacSDevin Teske	f_struct device_media &&
817323adacSDevin Teske		device_media unset private
827323adacSDevin Teske
837323adacSDevin Teske	if f_interactive; then
847323adacSDevin Teske		local name
857323adacSDevin Teske		f_struct device_media get name name
867323adacSDevin Teske		f_show_msg "$msg_using_usb_device" "$name"
877323adacSDevin Teske	fi
887323adacSDevin Teske
897323adacSDevin Teske	f_struct device_media || return $FAILURE
907323adacSDevin Teske}
917323adacSDevin Teske
927323adacSDevin Teske# f_media_init_usb $device
937323adacSDevin Teske#
947323adacSDevin Teske# Initializes the USB media device. Returns success if able to mount the USB
957323adacSDevin Teske# disk device using mount(8).
967323adacSDevin Teske#
977323adacSDevin Teskef_media_init_usb()
987323adacSDevin Teske{
99d4ae33f0SDevin Teske	local funcname=f_media_init_usb
1007323adacSDevin Teske	local dev="$1" devname err
1017323adacSDevin Teske
1029ecd54f2SDevin Teske	$dev get devname devname || return $FAILURE
1037323adacSDevin Teske	f_dprintf "Init routine called for USB device. devname=[%s]" \
1047323adacSDevin Teske	          "$devname"
1057323adacSDevin Teske
1067323adacSDevin Teske	if [ "$USB_MOUNTED" ]; then
1077323adacSDevin Teske		f_dprintf "USB device already mounted."
1087323adacSDevin Teske		return $SUCCESS
1097323adacSDevin Teske	fi
1107323adacSDevin Teske
111d4ae33f0SDevin Teske	if [ ! -e "$MOUNTPOINT" ]; then
112d4ae33f0SDevin Teske		f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" ||
1137323adacSDevin Teske			return $FAILURE
1147323adacSDevin Teske	fi
1157323adacSDevin Teske
116d4ae33f0SDevin Teske	if f_eval_catch -dk err $funcname mount \
117d4ae33f0SDevin Teske		'mount "%s" "%s"' "$devname" "$MOUNTPOINT"
118d4ae33f0SDevin Teske	then
1197323adacSDevin Teske		USB_MOUNTED=1
1207323adacSDevin Teske		return $SUCCESS
1217323adacSDevin Teske	fi
1227323adacSDevin Teske
1237323adacSDevin Teske	err="${err#mount: }"; err="${err#$devname: }"
1247323adacSDevin Teske	f_show_msg "$msg_error_mounting_usb_drive" \
1257323adacSDevin Teske	           "$devname" "$MOUNTPOINT" "$err"
1267323adacSDevin Teske	return $FAILURE
1277323adacSDevin Teske}
1287323adacSDevin Teske
129dde7be41SDevin Teske# f_media_get_usb $device $file [$probe_type]
1307323adacSDevin Teske#
131dde7be41SDevin Teske# Returns data from $file on a mounted USB disk device. Similar to cat(1). If
132dde7be41SDevin Teske# $probe_type is present and non-NULL, returns success if $file exists. If
133dde7be41SDevin Teske# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
134dde7be41SDevin Teske# standard-out.
1357323adacSDevin Teske#
1367323adacSDevin Teskef_media_get_usb()
1377323adacSDevin Teske{
138dde7be41SDevin Teske	local dev="$1" file="$2" probe_type="$3"
1399ecd54f2SDevin Teske	local name
1407323adacSDevin Teske
1419ecd54f2SDevin Teske	$dev get name name
142dde7be41SDevin Teske	f_dprintf "f_media_get_usb: dev=[%s] file=[%s] probe_type=%s" \
1439ecd54f2SDevin Teske	          "$name" "$file" "$probe_type"
1447323adacSDevin Teske
145dde7be41SDevin Teske	f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
1467323adacSDevin Teske}
1477323adacSDevin Teske
1487323adacSDevin Teske# f_media_shutdown_usb $device
1497323adacSDevin Teske#
1507323adacSDevin Teske# Shuts down the USB disk device using umount(8). Return status should be
1517323adacSDevin Teske# ignored.
1527323adacSDevin Teske#
1537323adacSDevin Teskef_media_shutdown_usb()
1547323adacSDevin Teske{
155d4ae33f0SDevin Teske	local funcname=f_media_shutdown_usb
1567323adacSDevin Teske	local dev="$1" err
1577323adacSDevin Teske
158f677a9e2SDevin Teske	[ "$USB_MOUNTED" ] || return $FAILURE
1597323adacSDevin Teske
160d4ae33f0SDevin Teske	if ! f_eval_catch -dk err $funcname umount \
161d4ae33f0SDevin Teske		'umount -f "%s"' "$MOUNTPOINT"
162d4ae33f0SDevin Teske	then
1637323adacSDevin Teske		err="${err#umount: }"; err="${err#*: }"
1647323adacSDevin Teske		f_show_msg "$msg_could_not_unmount_the_ufs_partition" \
1657323adacSDevin Teske		           "$MOUNTPOINT" "$err"
1667323adacSDevin Teske	else
1677323adacSDevin Teske		USB_MOUNTED=
1687323adacSDevin Teske	fi
1697323adacSDevin Teske}
1707323adacSDevin Teske
1717323adacSDevin Teske############################################################ MAIN
1727323adacSDevin Teske
1737323adacSDevin Teskef_dprintf "%s: Successfully loaded." media/usb.subr
1747323adacSDevin Teske
1757323adacSDevin Teskefi # ! $_MEDIA_USB_SUBR
176