xref: /freebsd/usr.sbin/bsdconfig/share/media/usb.subr (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1if [ ! "$_MEDIA_USB_SUBR" ]; then _MEDIA_USB_SUBR=1
2#
3# Copyright (c) 2012-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
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.
14#
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
25# SUCH DAMAGE.
26#
27#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32f_dprintf "%s: loading includes..." media/usb.subr
33f_include $BSDCFG_SHARE/device.subr
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/media/common.subr
36f_include $BSDCFG_SHARE/struct.subr
37f_include $BSDCFG_SHARE/variable.subr
38
39BSDCFG_LIBE="/usr/libexec/bsdconfig"
40f_include_lang $BSDCFG_LIBE/include/messages.subr
41
42############################################################ GLOBALS
43
44USB_MOUNTED=
45
46############################################################ FUNCTIONS
47
48# f_media_set_usb
49#
50# Attempt to use USB as the media type. Return success if we both found and set
51# the media type to be a USB drive.
52#
53f_media_set_usb()
54{
55	f_media_close
56
57	local devs ndevs
58	f_device_find "" $DEVICE_TYPE_USB devs
59	f_count ndevs $devs
60
61	if [ ${ndevs:=0} -eq 0 ]; then
62		f_show_msg "$msg_no_usb_devices_found"
63		return $FAILURE
64	elif [ $ndevs -eq 1 ]; then
65		f_struct_copy $devs device_media
66	else
67		local dev
68		local title="$msg_choose_a_usb_drive"
69		local prompt="$msg_please_select_a_usb_drive"
70		local hline=
71
72		dev=$( f_device_menu \
73			"$title" "$prompt" "$hline" $DEVICE_TYPE_USB \
74			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) ||
75			return $FAILURE
76
77		f_struct_copy "$dev" device_media
78	fi
79
80	f_struct device_media &&
81		device_media unset private
82
83	if f_interactive; then
84		local name
85		f_struct device_media get name name
86		f_show_msg "$msg_using_usb_device" "$name"
87	fi
88
89	f_struct device_media || return $FAILURE
90}
91
92# f_media_init_usb $device
93#
94# Initializes the USB media device. Returns success if able to mount the USB
95# disk device using mount(8).
96#
97f_media_init_usb()
98{
99	local funcname=f_media_init_usb
100	local dev="$1" devname err
101
102	$dev get devname devname || return $FAILURE
103	f_dprintf "Init routine called for USB device. devname=[%s]" \
104	          "$devname"
105
106	if [ "$USB_MOUNTED" ]; then
107		f_dprintf "USB device already mounted."
108		return $SUCCESS
109	fi
110
111	if [ ! -e "$MOUNTPOINT" ]; then
112		f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" ||
113			return $FAILURE
114	fi
115
116	if f_eval_catch -dk err $funcname mount \
117		'mount "%s" "%s"' "$devname" "$MOUNTPOINT"
118	then
119		USB_MOUNTED=1
120		return $SUCCESS
121	fi
122
123	err="${err#mount: }"; err="${err#$devname: }"
124	f_show_msg "$msg_error_mounting_usb_drive" \
125	           "$devname" "$MOUNTPOINT" "$err"
126	return $FAILURE
127}
128
129# f_media_get_usb $device $file [$probe_type]
130#
131# Returns data from $file on a mounted USB disk device. Similar to cat(1). If
132# $probe_type is present and non-NULL, returns success if $file exists. If
133# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
134# standard-out.
135#
136f_media_get_usb()
137{
138	local dev="$1" file="$2" probe_type="$3"
139	local name
140
141	$dev get name name
142	f_dprintf "f_media_get_usb: dev=[%s] file=[%s] probe_type=%s" \
143	          "$name" "$file" "$probe_type"
144
145	f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
146}
147
148# f_media_shutdown_usb $device
149#
150# Shuts down the USB disk device using umount(8). Return status should be
151# ignored.
152#
153f_media_shutdown_usb()
154{
155	local funcname=f_media_shutdown_usb
156	local dev="$1" err
157
158	[ "$USB_MOUNTED" ] || return $FAILURE
159
160	if ! f_eval_catch -dk err $funcname umount \
161		'umount -f "%s"' "$MOUNTPOINT"
162	then
163		err="${err#umount: }"; err="${err#*: }"
164		f_show_msg "$msg_could_not_unmount_the_ufs_partition" \
165		           "$MOUNTPOINT" "$err"
166	else
167		USB_MOUNTED=
168	fi
169}
170
171############################################################ MAIN
172
173f_dprintf "%s: Successfully loaded." media/usb.subr
174
175fi # ! $_MEDIA_USB_SUBR
176