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