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