xref: /freebsd/usr.sbin/bsdconfig/share/media/common.subr (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1if [ ! "$_MEDIA_COMMON_SUBR" ]; then _MEDIA_COMMON_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# $FreeBSD$
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/common.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/media/any.subr
36f_include $BSDCFG_SHARE/struct.subr
37
38############################################################ GLOBALS
39
40#
41# Where to mount media
42#
43MOUNTPOINT=/dist
44
45#
46# Media probe values to use for `f_media_get_TYPE media $file $PROBE' or
47# `f_device_get device_media $file $PROBE' (where $PROBE is one of the below
48# values).
49#
50PROBE_EXIST=1
51PROBE_SIZE=2
52
53############################################################ FUNCTIONS
54
55# f_media_open
56#
57# Returms success if able to initialize the media device.
58#
59f_media_open()
60{
61	f_dprintf "f_media_open: Verifying and initiliazing media device"
62	{ # Verify and initialize device media if-defined
63		f_struct device_media &&
64		f_media_verify &&
65		f_device_init device_media
66	} || return $FAILURE
67}
68
69# f_media_close
70#
71# Shuts down the media device, see f_device_shutdown() from device.subr for
72# more details.
73#
74f_media_close()
75{
76	f_dprintf "f_media_close: Shutting down media device"
77	f_struct device_media &&
78		f_device_shutdown device_media
79	f_struct_free device_media
80}
81
82# f_media_verify
83#
84# Returns success if the media device is available, and if not, prompts the
85# user to select a media type. See f_media_get_type() from media/any.subr for
86# more details.
87#
88f_media_verify()
89{
90	f_dprintf "f_media_verify: Verifying media device"
91	f_struct device_media || f_media_get_type
92}
93
94# f_media_generic_get $base $file [$probe_type]
95#
96# A generic open which follows a well-known "path" of places to look. If
97# $probe_type is present and non-NULL, returns success if $file exists. If
98# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
99# standard-out.
100#
101f_media_generic_get()
102{
103	local funcname=f_media_generic_get
104	local base="$1" file="$2" probe_type="$3"
105
106	local fname=f_media_generic_get
107	f_dprintf "%s: base=[%s] files=[%s] probe_type=%s" \
108	          $fname "$base" "$file" "$probe_type"
109
110	local rel path
111	f_getvar $VAR_RELNAME rel
112	for path in \
113		"$base/$file" \
114		"$base/FreeBSD/$file" \
115		"$base/releases/$file" \
116		"$base/$rel/$file" \
117	; do
118		if [ -f "$path" -a -r "$path" ]; then
119			f_dprintf "%s: file exists path=[%s]" $fname "$path"
120			if [ "$probe_type" = "$PROBE_SIZE" ]; then
121				local size
122				f_eval_catch -dk size $funcname stat \
123					'stat -f %%z "%s"' "$path" || size=-1
124				f_isinteger "$size" || size=-1
125				echo $size
126			fi
127			[ "$probe_type" ] && return $SUCCESS
128			cat "$path"
129			return $?
130		fi
131	done
132
133	path="$base/releases/$rel/$file" # Final path to try
134	if [ -f "$path" -a -r "$path" ]; then
135		f_dprintf "%s: file exists path=[%s]" $fname "$path"
136		if [ "$probe_type" = "$PROBE_SIZE" ]; then
137			local size
138			f_eval_catch -dk size $funcname stat \
139				'stat -f %%z "%s"' "$path" || size=-1
140			f_isinteger "$size" || size=-1
141			echo $size
142		fi
143		[ "$probe_type" ] && return $SUCCESS
144	elif [ "$probe_type" ]; then
145		[ "$probe_type" = "$PROBE_SIZE" ] && echo "-1"
146		return $FAILURE
147	fi
148	cat "$base/releases/$rel/$file" # Final path to try
149}
150
151############################################################ MAIN
152
153f_dprintf "%s: Successfully loaded." media/common.subr
154
155fi # ! $_MEDIA_COMMON_SUBR
156