Annotation of openbsd/fw_update/fw_install.sh, Revision 1.43
1.1 afresh1 1: #!/bin/ksh
1.36 afresh1 2: # $OpenBSD$
1.1 afresh1 3: set -e
4:
1.38 afresh1 5: # Copyright (c) 2021 Andrew Hewus Fresh <afresh1@openbsd.org>
6: #
7: # Permission to use, copy, modify, and distribute this software for any
8: # purpose with or without fee is hereby granted, provided that the above
9: # copyright notice and this permission notice appear in all copies.
10: #
11: # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18:
1.24 afresh1 19: # Fake up some things from install.sub that we don't need to actually do
1.22 afresh1 20: prefetcharea_fs_list() {
1.27 afresh1 21: echo "${DESTDIR}/tmp"
1.22 afresh1 22: }
23:
1.8 afresh1 24: # tmpdir, do_as, unpriv, and unpriv2 are from install.sub
1.43 ! afresh1 25: # modified to use su(1) when not in the installer.
1.8 afresh1 26:
27: # Create a temporary directory based on the supplied directory name prefix.
28: tmpdir() {
29: local _i=1 _dir
30:
31: until _dir="${1?}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
32: ((++_i < 10000)) || return 1
33: done
34: echo "$_dir"
35: }
1.6 afresh1 36:
37: # Run a command ($2+) as unprivileged user ($1).
38: # Take extra care that after "cmd" no "user" processes exist.
39: #
40: # Optionally:
41: # - create "file" and chown it to "user"
42: # - after "cmd", chown "file" back to root
43: #
44: # Usage: do_as user [-f file] cmd
45: do_as() {
46: (( $# >= 2 )) || return
47:
48: local _file _rc _user=$1
49: shift
50:
51: if [[ $1 == -f ]]; then
52: _file=$2
53: shift 2
54: fi
55:
56: if [[ -n $_file ]]; then
57: >$_file
58: chown "$_user" "$_file"
59: fi
60:
1.42 afresh1 61: if [[ -x /usr/bin/su ]]; then
62: /usr/bin/su -s /bin/ksh "$_user" -c "$*"
63: else
64: doas -u "$_user" "$@"
65: fi
1.6 afresh1 66: _rc=$?
67:
68: while doas -u "$_user" kill -9 -1 2>/dev/null; do
69: echo "Processes still running for user $_user after: $@"
70: sleep 1
71: done
72:
73: [[ -n $_file ]] && chown root "$_file"
74:
75: return $_rc
76: }
77:
78: unpriv() {
79: do_as _sndio "$@"
80: }
81:
82: unpriv2() {
83: do_as _file "$@"
84: }
85:
1.33 afresh1 86: VNAME=${VNAME:-$(sysctl -n kern.osrelease)}
1.34 afresh1 87: VERSION=${VERSION:-"${VNAME%.*}${VNAME#*.}"}
1.33 afresh1 88: FWDIR=${FWDIR:-$VNAME}
1.1 afresh1 89:
1.14 afresh1 90: # TODO: We need the firmware for the system we just installed
91: # not the one we booted from. For example:
92: # * booting from a snapshot bsd.rd that thinks it is the 7.0 release
93: # will install the firmware from the 7.0 directory instead of
94: # from the snapshots dir.
95: # If they're using sysupgrade, then the installer kernel will be correct.
96: # If we're doing this in the installer we can check what they picked
97: # for downloading sets and use that value.
98: # Otherwise, the fw_update after first boot will fix it up for us.
99:
1.7 afresh1 100: HTTP_FWDIR=$FWDIR
1.30 afresh1 101: set -- sed -n "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p" /var/run/dmesg.boot
1.7 afresh1 102: [[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots
103:
104: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
105: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.37 afresh1 106: FWPATTERNS="${DESTDIR}/usr/share/misc/firmware_patterns"
1.1 afresh1 107:
1.20 afresh1 108: # TODO: support srclocal installation of firmware somehow
1.35 afresh1 109: fw_install() {
1.24 afresh1 110: local _src=$1 _tmpfs_list _tmpfs _tmpsrc \
1.27 afresh1 111: _t=Get _cfile="/tmp/SHA256" _pkgdir=${DESTDIR}/var/db/pkg \
1.25 afresh1 112: _f _r _remove _i _installed
113: local _srclocal=false _unpriv=unpriv
1.26 afresh1 114:
1.15 afresh1 115: local _d _drivers=$(
1.11 afresh1 116: last=''
1.15 afresh1 117: while read _d _m; do
1.11 afresh1 118: grep=grep
1.15 afresh1 119: [ "$last" = "$_d" ] && continue
120: [ "$_m" ] || _m="^$_d[0-9][0-9]* at "
121: [ "$_m" = "${_m#^}" ] && grep=fgrep
122: $grep -q "$_m" /var/run/dmesg.boot || continue
123: echo $_d
124: last=$_d
1.37 afresh1 125: done < $FWPATTERNS
1.11 afresh1 126: )
127:
128: if [ -z "$_drivers" ]; then
1.22 afresh1 129: echo "No devices found which need firmware files to be downloaded."
1.11 afresh1 130: return
131: fi
1.1 afresh1 132:
1.22 afresh1 133: ! _tmpfs_list=$(prefetcharea_fs_list) &&
134: echo "Cannot determine prefetch area" >&2 && return
135:
136: for _tmpfs in $_tmpfs_list; do
137: # Try to clean up from previous runs, assuming
138: # the _tmpfs selection yields the same mount
139: # point.
140: for _tmpsrc in $_tmpfs/firmware.+([0-9]).+([0-9]); do
141: [[ -d $_tmpsrc ]] && rm -r $_tmpsrc
142: done
143:
144: # Create a download directory for the firmware and
145: # check that the _sndio user can read files from
146: # it. Otherwise cleanup and skip the filesystem.
147: if _tmpsrc=$(tmpdir "$_tmpfs/firmware"); then
148: (
149: >$_tmpsrc/t &&
150: $_unpriv cat $_tmpsrc/t
151: ) >/dev/null 2>&1 && break ||
152: rm -r $_tmpsrc
153: fi
154: done
1.1 afresh1 155:
1.11 afresh1 156: [[ ! -d $_tmpsrc ]] &&
1.22 afresh1 157: echo "Cannot create prefetch area" >&2 && return 1
1.1 afresh1 158:
1.16 afresh1 159: # Cleanup from previous runs.
160: rm -f $_cfile $_cfile.sig
161:
1.21 afresh1 162: _t=Get/Verify
1.17 afresh1 163:
1.11 afresh1 164: ! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" &&
1.22 afresh1 165: echo "Cannot fetch SHA256.sig" >&2 && return 1
1.1 afresh1 166:
1.11 afresh1 167: # Verify signature file with public keys.
1.24 afresh1 168: ! $_unpriv -f "$_cfile" \
1.11 afresh1 169: signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" &&
1.22 afresh1 170: echo "Signature check of SHA256.sig failed" >&2 && return 1
1.11 afresh1 171:
1.15 afresh1 172: for _d in $_drivers; do
1.41 afresh1 173: _f=$( sed -n "s/.*(\($_d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" | sed '$!d' )
1.19 afresh1 174: _installed=$(
1.22 afresh1 175: for fw in "${_pkgdir}/$_d-firmware"*; do
1.19 afresh1 176: [ -e "$fw" ] || continue
177: echo ${fw##*/}
178: done
1.21 afresh1 179: )
1.15 afresh1 180:
181: for _i in $_installed; do
182: if [ "$_f" = "$_i.tgz" ]; then
1.24 afresh1 183: echo "$_i already installed"
1.11 afresh1 184: continue 2
1.1 afresh1 185: fi
186: done
187:
1.11 afresh1 188: rm -f /tmp/h /tmp/fail
189:
190: # Fetch firmware file and create a checksum by piping through
191: # sha256. Create a flag file in case ftp failed. Firmware
192: # from net is written to the prefetch area.
193: ( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) |
194: ( $_srclocal && unpriv2 sha256 -b >/tmp/h ||
195: unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" )
196:
197: # Handle failed transfer.
198: if [[ -f /tmp/fail ]]; then
199: rm -f "$_tmpsrc/$_f"
1.22 afresh1 200: echo "Fetching of $_f failed!" >&2
201: continue
1.11 afresh1 202: fi
203:
204: # Verify firmware by comparing its checksum with SHA256.
1.22 afresh1 205: if ! fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then
1.11 afresh1 206: [[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc"
1.22 afresh1 207: echo "Checksum test for $_f failed." >&2
208: continue
1.11 afresh1 209: fi
210:
211: # TODO: Check hash for files before deleting
1.22 afresh1 212: if [ "$_installed" ] && [ -e "${_pkgdir}/$_installed/+CONTENTS" ]; then
1.18 afresh1 213: echo "Uninstalling $_installed"
1.22 afresh1 214: cwd=${_pkgdir}/$_installed
1.11 afresh1 215:
216: set -A _remove -- "${cwd}/+CONTENTS" "${cwd}"
217:
218: while read c g; do
219: case $c in
1.27 afresh1 220: @cwd) cwd="${DESTDIR}/$g"
1.11 afresh1 221: ;;
222: @*) continue
223: ;;
224: *) set -A _remove -- "$cwd/$c" "${_remove[@]}"
225: ;;
226: esac
1.22 afresh1 227: done < "${_pkgdir}/$_installed/+CONTENTS"
1.11 afresh1 228:
1.21 afresh1 229: # We specifically rm -f here because not removing files/dirs
230: # is probably not worth failing over.
1.11 afresh1 231: for _r in "${_remove[@]}" ; do
232: if [ -d "$_r" ]; then
233: # Try hard not to actually remove recursively
234: # without rmdir on the install media.
235: [ "$_r/*" = $( echo "$_r"/* ) ] && rm -rf "$_r"
236: else
237: rm -f "$_r"
238: fi
239: done
240: fi
241:
1.21 afresh1 242: # TODO: Should we mark these so real fw_update can -Drepair?
1.25 afresh1 243: ftp -D "Install" -Vmo- "file:$_tmpsrc/$_f" |
244: tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
1.28 afresh1 245: -s ",^firmware,${DESTDIR}/etc/firmware," \
1.25 afresh1 246: -C / -zxphf - "+*" "firmware/*"
1.23 afresh1 247:
1.22 afresh1 248: ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
1.1 afresh1 249: /^@comment pkgpath/ -1a
250: @option manual-installation
251: @option firmware
252: @comment install-script
253: .
254: w
255: EOL
1.11 afresh1 256: done
257: }
1.1 afresh1 258:
1.35 afresh1 259: fw_install "$FWURL"
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>