xref: /freebsd-13-stable/usr.sbin/bsdconfig/share/media/floppy.subr (revision 023fc80ee38a117fa65b2ccb2abf8bdc7dbd6fd9)
1if [ ! "$_MEDIA_FLOPPY_SUBR" ]; then _MEDIA_FLOPPY_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/floppy.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
44FLOPPY_MOUNTED=
45FLOPPY_DISTWANTED=
46
47############################################################ FUNCTIONS
48
49# f_media_set_floppy
50#
51# Return success if we both found and set the media type to be a floppy.
52#
53f_media_set_floppy()
54{
55	f_media_close
56
57	local devs ndevs
58	f_device_find "" $DEVICE_TYPE_FLOPPY devs
59	f_count ndevs $devs
60
61	if [ ${ndevs:=0} -eq 0 ]; then
62		f_interactive && f_show_msg "$msg_no_floppy_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_floppy_drive"
69		local prompt="$msg_please_select_a_floppy_drive"
70		local hline=
71
72		dev=$( f_device_menu \
73			"$title" "$prompt" "$hline" $DEVICE_TYPE_FLOPPY \
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	f_struct device_media || return $FAILURE
84}
85
86# f_media_init_floppy $device
87#
88# Initializes the Floppy media device. Returns success if able to mount the
89# Floppy disk device using either mount_msdosfs(8) or mount(8) (tried in that
90# order).
91#
92f_media_init_floppy()
93{
94	local funcname=f_media_init_floppy
95	local dev="$1" devname err
96
97	$dev get devname devname || return $FAILURE
98	f_dprintf "Init floppy called for %s distribution. devname=[%s]" \
99	          "${FLOPPY_DISTWANTED:-some}" "$devname"
100
101	if [ "$FLOPPY_MOUNTED" ]; then
102		f_dprintf "Floppy device already mounted."
103		return $SUCCESS
104	fi
105
106	local mp
107	$dev get private mp
108	if [ ! -e "${mp:=$MOUNTPOINT}" ] && ! f_quietly mkdir -p "$mp"; then
109		f_show_msg "$msg_unable_to_make_directory_mountpoint" \
110		           "$mp" "$devname"
111		return $FAILURE
112	fi
113
114	if f_interactive; then
115		local desc
116		$dev get desc desc
117		if [ "$FLOPPY_DISTWANTED" ]; then
118			f_show_msg "$msg_please_insert_floppy_in_drive" "$desc"
119		else
120			f_show_msg "$msg_please_insert_floppy_containing" \
121			           "$FLOPPY_DISTWANTED" "$desc"
122		fi
123	fi
124
125	if ! {
126	    f_eval_catch -dk err $funcname mount_msdosfs \
127		'mount_msdosfs -o ro -m 0777 -u 0 -g 0 "%s" "%s"' \
128		"$devname" "$mp" ||
129	    f_eval_catch -dk err $funcname mount \
130		'mount -o ro "%s" "%s"' "$devname" "$mp"
131	}; then
132		err="${err#mount: }"; err="${err#*: }"
133		local name
134		$dev get name name
135		f_show_msg "$msg_error_mounting_floppy_device" \
136		           "$name" "$devname" "$mp" "$err"
137		return $FAILURE
138	fi
139	FLOPPY_MOUNTED=1
140	FLOPPY_DISTWANTED=
141	return $SUCCESS
142}
143
144# f_media_get_floppy $device $file [$probe_type]
145#
146# Returns data from $file on a mounted Floppy disk device. Similar to cat(1).
147# If $probe_type is present and non-NULL, limits retries to zero and returns
148# success if $file exists. If $probe_type is equal to $PROBE_SIZE, prints the
149# size of $file in bytes to standard-out.
150#
151f_media_get_floppy()
152{
153	local funcname=f_media_get_floppy
154	local dev="$1" file="$2" probe_type="$3"
155	local name
156
157	$dev get name name
158	f_dprintf "f_media_get_floppy: dev=[%s] file=[%s] probe_type=%s" \
159	          "$name" "$file" "$probe_type"
160
161	#
162	# floppies don't use f_media_generic_get() because it's too expensive
163	# to speculatively open files on a floppy disk.  Make user get it
164	# right or give up with floppies.
165	#
166	local mp
167	$dev get private mp
168	local fp="${mp:=$MOUNTPOINT}/$file"
169	if ! [ -f "$fp" -a -r "$fp" ]; then
170		local nretries=4
171		[ "$probe_type" = "$PROBE_SIZE" ] && echo "-1"
172		[ "$probe_type" ] && return $FAILURE
173		while ! [ -f "$fp" -a -r "$fp" ]; do
174			if [ $nretries -eq 0 ]; then
175				f_show_msg "$msg_failed_to_get_floppy_file" \
176				           "$fp"
177				[ "$probe_type" = "$PROBE_SIZE" ] && echo "-1"
178				return $FAILURE
179			fi
180			FLOPPY_DISTWANTED="$fp"
181			f_media_shutdown_floppy "$dev"
182			f_media_init_floppy "$dev" || return $FAILURE
183			nretries=$(( $nretries - 1 ))
184		done
185	fi
186	#
187	# If we reach here, $file exists
188	#
189	if [ "$probe_type" = "$PROBE_SIZE" ]; then
190		local size
191		f_eval_catch -dk size $funcname stat 'stat -f %%z "%s"' "$fp"
192		f_isinteger "$size" || size=-1
193		echo "$size"
194	fi
195	[ "$probe_type" ] && return $SUCCESS
196	cat "$fp"
197}
198
199# f_media_shutdown_floppy $device
200#
201# Shuts down the Floppy disk device using umount(8). Return status should be
202# ignored.
203#
204f_media_shutdown_floppy()
205{
206	local funcname=f_media_shutdown_floppy
207	local dev="$1" err mp
208
209	[ "$FLOPPY_MOUNTED" ] || return $FAILURE
210
211	$dev get private mp
212	if f_eval_catch -d $funcname umount \
213		'umount -f "%s"' "${mp:=$MOUNTPOINT}"
214	then
215		FLOPPY_MOUNTED=
216		if f_interactive && [ "$_systemState" != "fixit" ]; then
217			local desc
218			$dev get desc desc
219			f_show_msg "$msg_you_may_remove_the_floppy" "$desc"
220		fi
221	fi
222}
223
224############################################################ MAIN
225
226f_dprintf "%s: Successfully loaded." media/floppy.subr
227
228fi # ! $_MEDIA_FLOPPY_SUBR
229