Annotation of openbsd/fw_update/fw_install.sh, Revision 1.131
1.1 afresh1 1: #!/bin/ksh
1.36 afresh1 2: # $OpenBSD$
1.50 afresh1 3: #
1.38 afresh1 4: # Copyright (c) 2021 Andrew Hewus Fresh <afresh1@openbsd.org>
5: #
6: # Permission to use, copy, modify, and distribute this software for any
7: # purpose with or without fee is hereby granted, provided that the above
8: # copyright notice and this permission notice appear in all copies.
9: #
10: # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17:
1.95 afresh1 18: set -o errexit -o pipefail -o nounset -o noclobber -o noglob
1.85 afresh1 19: set +o monitor
1.77 afresh1 20: export PATH=/usr/bin:/bin:/usr/sbin:/sbin
1.50 afresh1 21:
22: CFILE=SHA256.sig
23: DESTDIR=${DESTDIR:-}
24: FWPATTERNS="${DESTDIR}/usr/share/misc/firmware_patterns"
25:
26: VNAME=${VNAME:-$(sysctl -n kern.osrelease)}
27: VERSION=${VERSION:-"${VNAME%.*}${VNAME#*.}"}
28:
29: HTTP_FWDIR="$VNAME"
1.52 afresh1 30: VTYPE=$( sed -n "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p" \
31: /var/run/dmesg.boot | sed '$!d' )
1.50 afresh1 32: [[ $VTYPE == -!(stable) ]] && HTTP_FWDIR=snapshots
1.22 afresh1 33:
1.50 afresh1 34: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
35: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.8 afresh1 36:
1.131 ! afresh1 37: DRYRUN=false
1.97 afresh1 38: VERBOSE=false
1.111 afresh1 39: DELETE=false
1.87 afresh1 40: DOWNLOAD=true
41: INSTALL=true
1.69 afresh1 42: LOCALSRC=
43:
1.126 afresh1 44: unset FTPPID
1.129 afresh1 45: unset FWPKGTMP
1.126 afresh1 46: REMOVE_LOCALSRC=false
47: cleanup() {
1.127 afresh1 48: set +o errexit # ignore errors from killing ftp
1.128 afresh1 49: [ "${FTPPID:-}" ] && kill -TERM -"$FTPPID" 2>/dev/null
1.129 afresh1 50: [ "${FWPKGTMP:-}" ] && rm -rf "$FWPKGTMP"
1.126 afresh1 51: "$REMOVE_LOCALSRC" && rm -rf "$LOCALSRC"
52: }
53: trap cleanup EXIT
54:
1.8 afresh1 55: tmpdir() {
56: local _i=1 _dir
57:
1.50 afresh1 58: # If we're not in the installer,
1.79 afresh1 59: # we have mktemp and a more hostile environment.
1.50 afresh1 60: if [ -x /usr/bin/mktemp ]; then
61: _dir=$( mktemp -d "${1}-XXXXXXXXX" )
1.44 afresh1 62: else
1.50 afresh1 63: until _dir="${1}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
1.44 afresh1 64: ((++_i < 10000)) || return 1
65: done
66: fi
1.50 afresh1 67:
1.8 afresh1 68: echo "$_dir"
69: }
1.6 afresh1 70:
1.50 afresh1 71: fetch() {
1.126 afresh1 72: local _src="${FWURL}/${1##*/}" _dst=$1 _user=_file _exit _error=''
1.6 afresh1 73:
1.79 afresh1 74: # If we're not in the installer,
75: # we have su(1) and doas(1) is unlikely to be configured.
1.85 afresh1 76: set -o monitor # make sure ftp gets its own process group
77: (
1.101 afresh1 78: flags=-VM
79: "$VERBOSE" && flags=-vm
1.53 afresh1 80: if [ -x /usr/bin/su ]; then
1.85 afresh1 81: exec /usr/bin/su -s /bin/ksh "$_user" -c \
1.119 afresh1 82: "/usr/bin/ftp -N '${0##/}' -D 'Get/Verify' $flags -o- '$_src'" > "$_dst"
1.42 afresh1 83: else
1.85 afresh1 84: exec /usr/bin/doas -u "$_user" \
1.119 afresh1 85: /usr/bin/ftp -N "${0##/}" -D 'Get/Verify' $flags -o- "$_src" > "$_dst"
1.42 afresh1 86: fi
1.126 afresh1 87: ) & FTPPID=$!
1.85 afresh1 88: set +o monitor
89:
90: SECONDS=0
91: _last=0
1.126 afresh1 92: while kill -0 -"$FTPPID" 2>/dev/null; do
1.85 afresh1 93: if [[ $SECONDS -gt 12 ]]; then
94: set -- $( ls -ln "$_dst" 2>/dev/null )
95: if [[ $_last -ne $5 ]]; then
96: _last=$5
97: SECONDS=0
98: sleep 1
99: else
1.126 afresh1 100: kill -INT -"$FTPPID"
1.92 afresh1 101: _error=" (timed out)"
1.85 afresh1 102: fi
103: else
104: sleep 1
105: fi
106: done
107:
108: set +o errexit
1.126 afresh1 109: wait "$FTPPID"
1.85 afresh1 110: _exit=$?
111: set -o errexit
112:
1.126 afresh1 113: unset FTPPID
1.6 afresh1 114:
1.50 afresh1 115: if [ "$_exit" -ne 0 ]; then
1.69 afresh1 116: rm -f "$_dst"
1.92 afresh1 117: echo "Cannot fetch $_src$_error" >&2
1.50 afresh1 118: return 1
119: fi
1.6 afresh1 120: }
121:
1.50 afresh1 122: verify() {
123: # On the installer we don't get sha256 -C, so fake it.
1.69 afresh1 124: if ! fgrep -qx "SHA256 (${1##*/}) = $( /bin/sha256 -qb "$1" )" "$CFILE"; then
125: echo "Checksum test for ${1##*/} failed." >&2
1.50 afresh1 126: return 1
127: fi
1.6 afresh1 128: }
129:
1.124 afresh1 130: firmware_in_dmesg() {
1.90 afresh1 131: local _d _m _line _dmesgtail _last='' _nl=$( echo )
1.1 afresh1 132:
1.50 afresh1 133: # When we're not in the installer, the dmesg.boot can
134: # contain multiple boots, so only look in the last one
1.89 afresh1 135: _dmesgtail="$( echo ; sed -n 'H;/^OpenBSD/h;${g;p;}' /var/run/dmesg.boot |
136: grep -e "^[a-z][a-z]*[0-9]" -e " not configured " )"
1.50 afresh1 137:
1.89 afresh1 138: grep -v '^[[:space:]]*#' "$FWPATTERNS" |
139: while read -r _d _m; do
140: [ "$_d" = "$_last" ] && continue
1.90 afresh1 141: [ "$_m" ] || _m="${_nl}${_d}[0-9] at "
142: [ "$_m" = "${_m#^}" ] || _m="${_nl}${_m#^}"
1.83 afresh1 143:
1.89 afresh1 144: if [[ $_dmesgtail = *$_m* ]]; then
145: echo "$_d"
146: _last="$_d"
147: fi
148: done
1.50 afresh1 149: }
1.14 afresh1 150:
1.50 afresh1 151: firmware_filename() {
1.56 afresh1 152: local _f
153: _f="$( sed -n "s/.*(\($1-firmware-.*\.tgz\)).*/\1/p" "$CFILE" | sed '$!d' )"
154: ! [ "$_f" ] && echo "Unable to find firmware for $1" >&2 && return 1
155: echo "$_f"
1.50 afresh1 156: }
1.7 afresh1 157:
1.57 afresh1 158: firmware_devicename() {
159: local _d="${1##*/}"
160: _d="${_d%-firmware-*}"
161: echo "$_d"
162: }
163:
1.50 afresh1 164: installed_firmware() {
1.110 afresh1 165: local _pre="$1" _match="$2" _post="$3" _firmware
1.123 afresh1 166: set -sA _firmware -- $(
1.109 afresh1 167: set +o noglob
168: grep -Fxl '@option firmware' \
1.110 afresh1 169: "${DESTDIR}/var/db/pkg/"$_pre"$_match"$_post"/+CONTENTS" \
1.109 afresh1 170: 2>/dev/null || true
171: set -o noglob
172: )
173:
174: [ "${_firmware[*]:-}" ] || return 0
175: for fw in "${_firmware[@]}"; do
176: fw="${fw%/+CONTENTS}"
1.50 afresh1 177: echo "${fw##*/}"
178: done
179: }
1.1 afresh1 180:
1.124 afresh1 181: detect_firmware() {
182: local _devices _last='' _d
183:
184: set -sA _devices -- $(
185: firmware_in_dmesg
186: for _d in $( installed_firmware '*' '-firmware-' '*' ); do
187: echo "$( firmware_devicename "$_d" )"
188: done
189: )
190:
191: [ "${_devices[*]:-}" ] || return 0
192: for _d in "${_devices[@]}"; do
193: [[ $_last = $_d ]] && continue
194: echo $_d
195: _last="$_d"
196: done
197: }
198:
1.50 afresh1 199: add_firmware () {
1.106 afresh1 200: local _f="${1##*/}" _pkgname
1.129 afresh1 201: FWPKGTMP="$( tmpdir "${DESTDIR}/var/db/pkg/.firmware" )"
1.101 afresh1 202: local flags=-VM
203: "$VERBOSE" && flags=-vm
1.119 afresh1 204: ftp -N "${0##/}" -D "Install" "$flags" -o- "file:${1}" |
1.129 afresh1 205: tar -s ",^\+,${FWPKGTMP}/+," \
1.69 afresh1 206: -s ",^firmware,${DESTDIR}/etc/firmware," \
207: -C / -zxphf - "+*" "firmware/*"
1.1 afresh1 208:
1.129 afresh1 209: _pkgname="$( sed -n '/^@name /{s///p;q;}' "${FWPKGTMP}/+CONTENTS" )"
1.106 afresh1 210: if [ ! "$_pkgname" ]; then
211: echo "Failed to extract name from $1, partial install" 2>&1
1.129 afresh1 212: rm -rf "$FWPKGTMP"
213: unset FWPKGTMP
1.106 afresh1 214: return 1
215: fi
216:
1.50 afresh1 217: # TODO: Should we mark these so real fw_update can -Drepair?
1.129 afresh1 218: ed -s "${FWPKGTMP}/+CONTENTS" <<EOL
1.50 afresh1 219: /^@comment pkgpath/ -1a
220: @option manual-installation
221: @option firmware
222: @comment install-script
223: .
224: w
225: EOL
1.106 afresh1 226:
1.129 afresh1 227: chmod 755 "$FWPKGTMP"
228: mv "$FWPKGTMP" "${DESTDIR}/var/db/pkg/${_pkgname}"
229: unset FWPKGTMP
1.50 afresh1 230: }
1.22 afresh1 231:
1.50 afresh1 232: delete_firmware() {
1.51 afresh1 233: local _cwd _pkg="$1" _pkgdir="${DESTDIR}/var/db/pkg"
1.22 afresh1 234:
1.50 afresh1 235: # TODO: Check hash for files before deleting
1.101 afresh1 236: "$VERBOSE" && echo "Uninstalling $_pkg"
1.51 afresh1 237: _cwd="${_pkgdir}/$_pkg"
1.50 afresh1 238:
1.107 afresh1 239: if [ ! -e "$_cwd/+CONTENTS" ] ||
240: ! grep -Fxq '@option firmware' "$_cwd/+CONTENTS"; then
241: echo "${0##*/}: $_pkg does not appear to be firmware" >&2
242: return 2
243: fi
244:
1.51 afresh1 245: set -A _remove -- "${_cwd}/+CONTENTS" "${_cwd}"
1.50 afresh1 246:
247: while read -r c g; do
248: case $c in
1.51 afresh1 249: @cwd) _cwd="${DESTDIR}$g"
1.50 afresh1 250: ;;
251: @*) continue
252: ;;
1.52 afresh1 253: *) set -A _remove -- "$_cwd/$c" "${_remove[@]}"
1.50 afresh1 254: ;;
255: esac
256: done < "${_pkgdir}/${_pkg}/+CONTENTS"
257:
258: # We specifically rm -f here because not removing files/dirs
259: # is probably not worth failing over.
260: for _r in "${_remove[@]}" ; do
261: if [ -d "$_r" ]; then
262: # Try hard not to actually remove recursively
263: # without rmdir on the install media.
1.95 afresh1 264: set +o noglob
1.50 afresh1 265: [ "$_r/*" = "$( echo "$_r"/* )" ] && rm -rf "$_r"
1.95 afresh1 266: set -o noglob
1.50 afresh1 267: else
268: rm -f "$_r"
1.22 afresh1 269: fi
270: done
1.50 afresh1 271: }
1.1 afresh1 272:
1.59 afresh1 273: usage() {
1.122 afresh1 274: echo "usage: ${0##*/} [-d | -D] [-av] [-p path] [driver | file ...]"
1.59 afresh1 275: exit 2
276: }
277:
1.113 afresh1 278: ALL=false
1.87 afresh1 279: OPT_D=
1.131 ! afresh1 280: while getopts :adDnp:v name
1.59 afresh1 281: do
282: case "$name" in
1.113 afresh1 283: a) ALL=true ;;
1.111 afresh1 284: d) DELETE=true ;;
1.87 afresh1 285: D) OPT_D=true ;;
1.131 ! afresh1 286: n) DRYRUN=true ;;
1.121 afresh1 287: p) LOCALSRC="$OPTARG" ;;
1.97 afresh1 288: v) VERBOSE=true ;;
1.121 afresh1 289: :)
290: echo "${0##*/}: option requires an argument -- -$OPTARG" >&2
291: usage 2
292: ;;
293: ?)
294: echo "${0##*/}: unknown option -- -$OPTARG" >&2
295: usage 2
296: ;;
1.59 afresh1 297: esac
298: done
299: shift $((OPTIND - 1))
300:
1.121 afresh1 301: if [ "$LOCALSRC" ]; then
302: if [[ $LOCALSRC = @(ftp|http?(s))://* ]]; then
303: FWURL="${LOCALSRC}"
304: LOCALSRC=
305: else
306: LOCALSRC="${LOCALSRC:#file:}"
307: ! [ -d "$LOCALSRC" ] &&
308: echo "The path must be a URL or an existing directory" >&2 &&
309: exit 2
310: fi
311: fi
312:
1.122 afresh1 313: # "Download only" means local dir and don't install
1.87 afresh1 314: if [ "$OPT_D" ]; then
315: INSTALL=false
1.121 afresh1 316: LOCALSRC="${LOCALSRC:-.}"
1.130 afresh1 317: elif [ "$LOCALSRC" ]; then
318: DOWNLOAD=false
1.87 afresh1 319: fi
1.64 afresh1 320:
1.118 afresh1 321: if [ -x /usr/bin/id ] && [ "$(/usr/bin/id -u)" != 0 ]; then
322: echo "need root privileges" >&2
323: exit 1
1.94 afresh1 324: fi
325:
1.123 afresh1 326: set -sA devices -- "$@"
1.111 afresh1 327:
328: if "$DELETE"; then
1.122 afresh1 329: [ "$OPT_D" ] && usage 22
1.111 afresh1 330:
1.113 afresh1 331: set -A installed
332: if [ "${devices[*]:-}" ]; then
333: "$ALL" && usage 22
334:
335: set -A installed -- $(
336: for d in "${devices[@]}"; do
337: f="${d##*/}" # only care about the name
338: f="${f%.tgz}" # allow specifying the package name
339: [ "$( firmware_devicename "$f" )" = "$f" ] && f="$f-firmware"
340:
341: set -A i -- $( installed_firmware '' "$f-" '*' )
342:
343: if [ "${i[*]:-}" ]; then
344: echo "${i[@]}"
345: else
346: echo "No firmware found for '$d'" >&2
347: fi
348: done
349: )
350: elif "$ALL"; then
351: set -A installed -- $( installed_firmware '*' '-firmware-' '*' )
352: fi
1.111 afresh1 353:
354: deleted=''
1.113 afresh1 355: if [ "${installed:-}" ]; then
1.111 afresh1 356: for fw in "${installed[@]}"; do
1.131 ! afresh1 357: if "$DRYRUN"; then
! 358: echo "Delete $fw"
! 359: else
! 360: delete_firmware "$fw" || continue
! 361: fi
1.112 afresh1 362: deleted="$deleted,$( firmware_devicename "$fw" )"
1.111 afresh1 363: done
1.113 afresh1 364: fi
1.111 afresh1 365:
366: deleted="${deleted:+${deleted#,}}"
367: echo "${0:##*/}: deleted ${deleted:-none}";
368:
369: exit
370: fi
1.125 afresh1 371:
372: if [ ! "$LOCALSRC" ]; then
373: LOCALSRC="$( tmpdir "${DESTDIR}/tmp/${0##*/}" )"
1.126 afresh1 374: REMOVE_LOCALSRC=true
1.125 afresh1 375: fi
376:
377: CFILE="$LOCALSRC/$CFILE"
1.58 afresh1 378:
1.113 afresh1 379: if [ "${devices[*]:-}" ]; then
380: "$ALL" && usage 22
381: else
1.101 afresh1 382: "$VERBOSE" && echo -n "Detecting firmware ..."
1.124 afresh1 383: set -sA devices -- $( detect_firmware )
1.101 afresh1 384: "$VERBOSE" &&
385: { [ "${devices[*]:-}" ] && echo " found." || echo " done." ; }
1.50 afresh1 386: fi
1.91 afresh1 387:
388: [ "${devices[*]:-}" ] || exit
1.50 afresh1 389:
1.71 afresh1 390: if "$DOWNLOAD"; then
1.116 afresh1 391: set +o noclobber # we want to get the latest CFILE
1.64 afresh1 392: fetch "$CFILE"
1.116 afresh1 393: set -o noclobber
1.64 afresh1 394: ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
395: echo "Signature check of SHA256.sig failed" >&2 && exit 1
1.120 afresh1 396: elif [ ! -e "$CFILE" ]; then
397: # TODO: We shouldn't need a CFILE if all arguments are files.
398: echo "${0##*/}: $CFILE: No such file or directory" >&2
399: exit 2
1.64 afresh1 400: fi
1.50 afresh1 401:
1.103 afresh1 402: added=''
403: updated=''
1.115 afresh1 404: kept=''
1.70 afresh1 405: for f in "${devices[@]}"; do
406: d="$( firmware_devicename "$f" )"
407:
1.58 afresh1 408: if [ "$f" = "$d" ]; then
409: f=$( firmware_filename "$d" || true )
410: [ "$f" ] || continue
1.69 afresh1 411: f="$LOCALSRC/$f"
1.76 afresh1 412: elif ! "$INSTALL" && ! grep -Fq "($f)" "$CFILE" ; then
1.70 afresh1 413: echo "Cannot download local file $f" >&2
414: exit 2
1.58 afresh1 415: fi
416:
1.110 afresh1 417: set -A installed -- $( installed_firmware '' "$d-firmware-" '*' )
1.50 afresh1 418:
1.68 afresh1 419: if "$INSTALL" && [ "${installed[*]:-}" ]; then
1.65 afresh1 420: for i in "${installed[@]}"; do
1.58 afresh1 421: if [ "${f##*/}" = "$i.tgz" ]; then
1.131 ! afresh1 422: "$VERBOSE" && echo "Keep $i"
1.115 afresh1 423: kept="$kept,$d"
1.11 afresh1 424: continue 2
1.1 afresh1 425: fi
426: done
1.50 afresh1 427: fi
1.1 afresh1 428:
1.72 afresh1 429: if [ -e "$f" ]; then
430: if "$DOWNLOAD"; then
1.131 ! afresh1 431: if "$VERBOSE"; then
! 432: "$INSTALL" &&
! 433: echo "Verify ${f##*/}" ||
! 434: echo "Keep/Verify ${f##*/}"
! 435: echo "Verify ${f##*/}"
! 436: fi
! 437: "$DRYRUN" || verify "$f" || continue
! 438: "$INSTALL" || kept="$kept,$d"
1.72 afresh1 439: # else assume it was verified when downloaded
440: fi
1.75 afresh1 441: elif "$DOWNLOAD"; then
1.131 ! afresh1 442: if "$DRYRUN"; then
! 443: "$VERBOSE" && echo "Get/Verify ${f##*/}"
! 444: else
! 445: fetch "$f" || continue
! 446: verify "$f" || continue
! 447: fi
1.117 afresh1 448: "$INSTALL" || added="$added,$d"
1.75 afresh1 449: elif "$INSTALL"; then
1.72 afresh1 450: echo "Cannot install ${f##*/}, not found" >&2
451: continue
1.58 afresh1 452: fi
1.60 afresh1 453:
1.68 afresh1 454: "$INSTALL" || continue
1.11 afresh1 455:
1.103 afresh1 456: removed=false
1.65 afresh1 457: if [ "${installed[*]:-}" ]; then
1.50 afresh1 458: for i in "${installed[@]}"; do
1.131 ! afresh1 459: "$DRYRUN" || delete_firmware "$i"
1.103 afresh1 460: removed=true
1.50 afresh1 461: done
462: fi
1.11 afresh1 463:
1.131 ! afresh1 464: "$DRYRUN" || add_firmware "$f"
1.103 afresh1 465:
1.131 ! afresh1 466: f="${f##*/}"
! 467: f="${f%.tgz}"
1.103 afresh1 468: if "$removed"; then
1.131 ! afresh1 469: "$DRYRUN" && echo "Update $f"
1.114 afresh1 470: updated="$updated,$d"
1.103 afresh1 471: else
1.131 ! afresh1 472: "$DRYRUN" && echo "Install $f"
1.114 afresh1 473: added="$added,$d"
1.103 afresh1 474: fi
1.50 afresh1 475: done
1.1 afresh1 476:
1.114 afresh1 477: added="${added:#,}"
478: updated="${updated:#,}"
1.115 afresh1 479: kept="${kept:#,}"
1.117 afresh1 480: if "$INSTALL"; then
481: echo "${0##*/}: added ${added:-none}; updated ${updated:-none}; kept ${kept:-none}"
482: else
483: echo "${0##*/}: downloaded ${added:-none}; kept ${kept:-none}"
484: fi
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>