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