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