floppy.subr revision 247280
1238104Sdesif [ ! "$_MEDIA_FLOPPY_SUBR" ]; then _MEDIA_FLOPPY_SUBR=1
2238104Sdes#
3238104Sdes# Copyright (c) 2012-2013 Devin Teske
4238104Sdes# All Rights Reserved.
5238104Sdes#
6238104Sdes# Redistribution and use in source and binary forms, with or without
7238104Sdes# modification, are permitted provided that the following conditions
8238104Sdes# are met:
9238104Sdes# 1. Redistributions of source code must retain the above copyright
10238104Sdes#    notice, this list of conditions and the following disclaimer.
11238104Sdes# 2. Redistributions in binary form must reproduce the above copyright
12238104Sdes#    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 (INLUDING, 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# $FreeBSD: head/usr.sbin/bsdconfig/share/media/floppy.subr 247280 2013-02-25 19:55:32Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/floppy.subr
34f_include $BSDCFG_SHARE/struct.subr
35f_include $BSDCFG_SHARE/device.subr
36f_include $BSDCFG_SHARE/dialog.subr
37f_include $BSDCFG_SHARE/variable.subr
38f_include $BSDCFG_SHARE/media/common.subr
39
40BSDCFG_LIBE="/usr/libexec/bsdconfig"
41f_include_lang $BSDCFG_LIBE/include/messages.subr
42
43############################################################ GLOBALS
44
45FLOPPY_MOUNTED=
46FLOPPY_DISTWANTED=
47
48############################################################ FUNCTIONS
49
50# f_media_set_floppy
51#
52# Return success if we both found and set the media type to be a floppy.
53#
54f_media_set_floppy()
55{
56	f_media_close
57
58	local devs ndevs
59	f_device_find "" $DEVICE_TYPE_FLOPPY devs
60	ndevs=$( set -- $devs; echo $# )
61
62	if [ ${ndevs:=0} -eq 0 ]; then
63		f_interactive && f_dialog_msgbox "$msg_no_floppy_devices_found"
64		return $FAILURE
65	elif [ $ndevs -gt 1 ]; then
66		local title="$msg_choose_a_floppy_drive"
67		local prompt="$msg_please_select_a_floppy_drive"
68		local hline=""
69
70		local dev retval
71		dev=$( f_device_menu \
72			"$title" "$prompt" "$hline" $DEVICE_TYPE_FLOPPY \
73			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
74		retval=$?
75		[ "$dev" ] || return $FAILURE
76
77		f_device_find "$dev" $DEVICE_TYPE_FLOPPY devs
78		[ "$devs" ] || return $FAILURE
79		dev="${devs%%[$IFS]*}"
80
81		f_struct_copy device_$dev device_media
82		[ $retval -eq $SUCCESS ] || return $FAILURE
83	else
84		f_struct_copy device_$devs device_media
85	fi
86
87	f_struct device_media &&
88		device_media unset private
89
90	f_struct device_media || return $FAILURE
91}
92
93# f_media_init_floppy $device
94#
95# Initializes the Floppy media device. Returns success if able to mount the
96# Floppy disk device using either mount_msdosfs(8) or mount(8) (tried in that
97# order).
98#
99f_media_init_floppy()
100{
101	local dev="$1" devname err
102
103	device_$dev get devname devname || return $FAILURE
104	f_dprintf "Init floppy called for %s distribution. devname=[%s]" \
105	          "${FLOPPY_DISTWANTED:-some}" "$devname"
106
107	if [ "$FLOPPY_MOUNTED" ]; then
108		f_dprintf "Floppy device already mounted."
109		return $SUCCESS
110	fi
111
112	local mp
113	device_$dev get private mp
114	if [ ! -e "${mp:=$MOUNTPOINT}" ] && ! f_quietly mkdir -p "$mp"; then
115		f_show_msg "$msg_unable_to_make_directory_mountpoint" \
116		           "$mp" "$devname"
117		return $FAILURE
118	fi
119
120	if f_interactive; then
121		local desc
122		device_$dev get desc desc
123		if [ "$FLOPPY_DISTWANTED" ]; then
124			f_show_msg "$msg_please_insert_floppy_in_drive" "$desc"
125		else
126			f_show_msg "$msg_please_insert_floppy_containing" \
127			           "$FLOPPY_DISTWANTED" "$desc"
128		fi
129	fi
130
131	if ! {
132	    f_quietly mount_msdosfs -o ro -m 0777 -u 0 -g 0 "$devname" "$mp" ||
133	    err=$( mount -o ro "$devname" "$mp" 2>&1 )
134	}; then
135		err="${err#mount: }"; err="${err#*: }"
136		local name
137		device_$dev get name name
138		f_show_msg "$msg_error_mounting_floppy_device" \
139		           "$name" "$devname" "$mp" "$err"
140		return $FAILURE
141	fi
142	FLOPPY_MOUNTED=1
143	FLOPPY_DISTWANTED=
144	return $SUCCESS
145}
146
147# f_media_get_floppy $device $file [$probe_only]
148#
149# Returns data from $file on a mounted Floppy disk device. Similar to cat(1).
150# If $probe_only is present and non-null, limits retries to zero.
151#
152f_media_get_floppy()
153{
154	local dev="$1" file="$2" probe_only="$3"
155
156	f_dprintf "f_media_get_floppy: dev=[%s] file=[%s] probe_only=%s" \
157	          "$dev" "$file" "$probe_only"
158
159	#
160	# floppies don't use f_media_generic_get() because it's too expensive
161	# to speculatively open files on a floppy disk.  Make user get it
162	# right or give up with floppies.
163	#
164	local mp
165	device_$dev get private mp
166	local fp="${mp:=$MOUNTPOINT}/$file"
167	if ! [ -f "$fp" -a -r "$fp" ]; then
168		local nretries=4
169		[ "$probe_only" ] && return $FAILURE
170		while ! [ -f "$fp" -a -r "$fp" ]; do
171			if [ $nretries -eq 0 ]; then
172				f_show_msg "$msg_failed_to_get_floppy_file" \
173				           "$fp"
174				return $FAILURE
175			fi
176			FLOPPY_DISTWANTED="$fp"
177			f_media_shutdown_floppy "$dev"
178			f_media_init_floppy "$dev" || return $FAILURE
179			nretries=$(( $nretries - 1 ))
180		done
181	fi
182	cat "$fp"
183}
184
185# f_media_shutdown_floppy $device
186#
187# Shuts down the Floppy disk device using umount(8). Return status should be
188# ignored.
189#
190f_media_shutdown_floppy()
191{
192	local dev="$1" err mp
193
194	[ "$FLOPPY_MOUNTED" ] || return
195
196	device_$dev get private mp
197	if ! err=$( umount -f "${mp:=$MOUNTPOINT}" 2>&1 ); then
198		err="${err#umount: }"; err="${err#*:}"
199		f_dprintf "Umount of floppy on %s failed: %s" "$mp" "$err"
200	else
201		FLOPPY_MOUNTED=
202		if f_interactive && [ "$_systemState" != "fixit" ]; then
203			local desc
204			device_$dev get desc desc
205			f_show_msg "$msg_you_may_remove_the_floppy" "$desc"
206		fi
207	fi
208}
209
210############################################################ MAIN
211
212f_dprintf "%s: Successfully loaded." media/floppy.subr
213
214fi # ! $_MEDIA_FLOPPY_SUBR
215