Annotation of openbsd/fw_update/fw_install.sh, Revision 1.69
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.50 afresh1 18: set -o errexit -o pipefail -o nounset
19:
20: CFILE=SHA256.sig
21: DESTDIR=${DESTDIR:-}
22: FWPATTERNS="${DESTDIR}/usr/share/misc/firmware_patterns"
23:
24: VNAME=${VNAME:-$(sysctl -n kern.osrelease)}
25: VERSION=${VERSION:-"${VNAME%.*}${VNAME#*.}"}
26:
27: HTTP_FWDIR="$VNAME"
1.52 afresh1 28: VTYPE=$( sed -n "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p" \
29: /var/run/dmesg.boot | sed '$!d' )
1.50 afresh1 30: [[ $VTYPE == -!(stable) ]] && HTTP_FWDIR=snapshots
1.22 afresh1 31:
1.50 afresh1 32: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
33: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.8 afresh1 34:
1.69 ! afresh1 35: LOCALSRC=
! 36:
1.8 afresh1 37: tmpdir() {
38: local _i=1 _dir
39:
1.50 afresh1 40: # If we're not in the installer,
41: # we have mktemp and a more hostile environment
42: if [ -x /usr/bin/mktemp ]; then
43: _dir=$( mktemp -d "${1}-XXXXXXXXX" )
1.44 afresh1 44: else
1.50 afresh1 45: until _dir="${1}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
1.44 afresh1 46: ((++_i < 10000)) || return 1
47: done
48: fi
1.50 afresh1 49:
1.8 afresh1 50: echo "$_dir"
51: }
1.6 afresh1 52:
1.55 afresh1 53: realpath () {
54: if [ -x /usr/bin/realpath ]; then
55: /usr/bin/realpath "$1"
56: elif [ "$1" = "${1%/*}" ]; then
57: echo "${PWD}/$1"
58: else
59: echo "$( cd "${1%/*}" && pwd )/${1##*/}"
60: fi
61: }
62:
1.50 afresh1 63: fetch() {
1.69 ! afresh1 64: local _src="${FWURL}/${1##*/}" _dst=$1 _user=_file _exit
1.6 afresh1 65:
1.50 afresh1 66: # If we're not in the installer, we have su(1)
67: # and doas(1) is unlikely to be configured.
1.53 afresh1 68: if [ -x /usr/bin/su ]; then
1.50 afresh1 69: /usr/bin/su -s /bin/ksh "$_user" -c \
1.69 ! afresh1 70: "/usr/bin/ftp -D 'Get/Verify' -Vm -o- '$_src'" > "$_dst"
1.50 afresh1 71: _exit="$?"
1.42 afresh1 72: else
1.50 afresh1 73: /usr/bin/doas -u "$_user" \
1.69 ! afresh1 74: /usr/bin/ftp -D 'Get/Verify' -Vm -o- "$_src" > "$_dst"
1.50 afresh1 75: _exit="$?"
1.42 afresh1 76: fi
1.6 afresh1 77:
1.50 afresh1 78: if [ "$_exit" -ne 0 ]; then
1.69 ! afresh1 79: rm -f "$_dst"
! 80: echo "Cannot fetch $_src" >&2
1.50 afresh1 81: return 1
82: fi
1.6 afresh1 83: }
84:
1.50 afresh1 85: verify() {
86: # On the installer we don't get sha256 -C, so fake it.
1.69 ! afresh1 87: if ! fgrep -qx "SHA256 (${1##*/}) = $( /bin/sha256 -qb "$1" )" "$CFILE"; then
! 88: echo "Checksum test for ${1##*/} failed." >&2
1.50 afresh1 89: return 1
90: fi
1.6 afresh1 91: }
92:
1.50 afresh1 93: devices_needing_firmware() {
94: local _d _m _grep _dmesgtail _last=''
1.1 afresh1 95:
1.50 afresh1 96: # When we're not in the installer, the dmesg.boot can
97: # contain multiple boots, so only look in the last one
98: _dmesgtail=$( sed -n 'H;/^OpenBSD/h;${g;p;}' /var/run/dmesg.boot )
99:
100: grep -v '^[[:space:]]*#' "$FWPATTERNS" |
101: while read -r _d _m; do
102: _grep="grep"
103: [ "$_last" = "$_d" ] && continue
104: [ "$_m" ] || _m="^${_d}[0-9][0-9]* at "
105: [ "$_m" = "${_m#^}" ] && _grep="fgrep"
106:
107: echo "$_dmesgtail" | $_grep -q "$_m" || continue
108: echo "$_d"
109: _last="$_d"
110: done
111: }
1.14 afresh1 112:
1.50 afresh1 113: firmware_filename() {
1.56 afresh1 114: local _f
115: _f="$( sed -n "s/.*(\($1-firmware-.*\.tgz\)).*/\1/p" "$CFILE" | sed '$!d' )"
116: ! [ "$_f" ] && echo "Unable to find firmware for $1" >&2 && return 1
117: echo "$_f"
1.50 afresh1 118: }
1.7 afresh1 119:
1.57 afresh1 120: firmware_devicename() {
121: local _d="${1##*/}"
122: _d="${_d%-firmware-*}"
123: echo "$_d"
124: }
125:
1.50 afresh1 126: installed_firmware() {
127: for fw in "${DESTDIR}/var/db/pkg/$1-firmware"*; do
128: [ -e "$fw" ] || continue
129: echo "${fw##*/}"
130: done
131: }
1.1 afresh1 132:
1.50 afresh1 133: add_firmware () {
1.58 afresh1 134: local _f="${1##*/}" _pkgdir="${DESTDIR}/var/db/pkg"
1.50 afresh1 135: ftp -D "Install" -Vmo- "file:${1}" |
136: tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
1.69 ! afresh1 137: -s ",^firmware,${DESTDIR}/etc/firmware," \
! 138: -C / -zxphf - "+*" "firmware/*"
1.1 afresh1 139:
1.50 afresh1 140: # TODO: Should we mark these so real fw_update can -Drepair?
141: ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
142: /^@comment pkgpath/ -1a
143: @option manual-installation
144: @option firmware
145: @comment install-script
146: .
147: w
148: EOL
149: }
1.22 afresh1 150:
1.50 afresh1 151: delete_firmware() {
1.51 afresh1 152: local _cwd _pkg="$1" _pkgdir="${DESTDIR}/var/db/pkg"
1.22 afresh1 153:
1.50 afresh1 154: # TODO: Check hash for files before deleting
155: echo "Uninstalling $_pkg"
1.51 afresh1 156: _cwd="${_pkgdir}/$_pkg"
1.50 afresh1 157:
1.51 afresh1 158: set -A _remove -- "${_cwd}/+CONTENTS" "${_cwd}"
1.50 afresh1 159:
160: while read -r c g; do
161: case $c in
1.51 afresh1 162: @cwd) _cwd="${DESTDIR}$g"
1.50 afresh1 163: ;;
164: @*) continue
165: ;;
1.52 afresh1 166: *) set -A _remove -- "$_cwd/$c" "${_remove[@]}"
1.50 afresh1 167: ;;
168: esac
169: done < "${_pkgdir}/${_pkg}/+CONTENTS"
170:
171: # We specifically rm -f here because not removing files/dirs
172: # is probably not worth failing over.
173: for _r in "${_remove[@]}" ; do
174: if [ -d "$_r" ]; then
175: # Try hard not to actually remove recursively
176: # without rmdir on the install media.
177: [ "$_r/*" = "$( echo "$_r"/* )" ] && rm -rf "$_r"
178: else
179: rm -f "$_r"
1.22 afresh1 180: fi
181: done
1.50 afresh1 182: }
1.1 afresh1 183:
1.59 afresh1 184: usage() {
1.66 afresh1 185: echo "usage: fw_install [-d dir | -L dir] [driver | file [...]]"
1.59 afresh1 186: exit 2
187: }
188:
1.68 afresh1 189: INSTALL=true
190: DOWNLOAD=true
191:
192: while getopts dL name
1.59 afresh1 193: do
194: case "$name" in
1.68 afresh1 195: # "download only" means local dir and don't install
1.69 ! afresh1 196: d) LOCALSRC=. INSTALL=false ;;
! 197: L) LOCALSRC=. ;;
1.59 afresh1 198: ?) usage 2 ;;
199: esac
200: done
201: shift $((OPTIND - 1))
202:
1.68 afresh1 203: # If we're installing from a local dir
204: # we don't want to download anything
1.69 ! afresh1 205: [ "$LOCALSRC" ] && "$INSTALL" && DOWNLOAD=false
! 206: [ "$LOCALSRC" ] || LOCALSRC="$( tmpdir "${DESTDIR}/tmp/fw_install" )"
1.64 afresh1 207:
1.58 afresh1 208: set -A devices -- "$@"
209:
210: [ "${devices[*]:-}" ] ||
211: set -A devices -- $( devices_needing_firmware )
1.1 afresh1 212:
1.65 afresh1 213: if [ ! "${devices[*]:-}" ]; then
1.50 afresh1 214: echo "No devices found which need firmware files to be downloaded."
215: exit
216: fi
217:
1.58 afresh1 218: # Have to find the full path to firmware files
219: # so we can cd and still find them later.
220: i=0
221: while (( i < "${#devices[@]}" )); do
222: f="${devices[$i]}"
223: d=$( firmware_devicename "$f" )
224: if [ -e "$f" ]; then
1.68 afresh1 225: if "$DOWNLOAD"; then
1.60 afresh1 226: echo "Cannot download local file $f" >&2
227: exit 2
228: fi
1.58 afresh1 229: devices[$i]="$d:$( realpath "$f" )"
230: fi
231: i=$((i + 1))
232: done
233:
1.68 afresh1 234: if "$DOWNLOAD" && ! [[ -e "$CFILE" ]]; then
1.64 afresh1 235: fetch "$CFILE"
236: ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
237: echo "Signature check of SHA256.sig failed" >&2 && exit 1
238: fi
1.50 afresh1 239:
240: for d in "${devices[@]}"; do
1.58 afresh1 241: f="${d##*:}"
242: if [ "$f" = "$d" ]; then
243: f=$( firmware_filename "$d" || true )
244: [ "$f" ] || continue
1.69 ! afresh1 245: f="$LOCALSRC/$f"
1.58 afresh1 246: else
1.62 afresh1 247: d="${d%:*}"
1.58 afresh1 248: fi
249:
1.50 afresh1 250: set -A installed -- $( installed_firmware "$d" )
251:
1.68 afresh1 252: if "$INSTALL" && [ "${installed[*]:-}" ]; then
1.65 afresh1 253: for i in "${installed[@]}"; do
1.58 afresh1 254: if [ "${f##*/}" = "$i.tgz" ]; then
1.50 afresh1 255: echo "$i already installed"
1.11 afresh1 256: continue 2
1.1 afresh1 257: fi
258: done
1.50 afresh1 259: fi
1.1 afresh1 260:
1.58 afresh1 261: if [ ! -e "$f" ]; then
1.68 afresh1 262: "$INSTALL" && ! "$DOWNLOAD" &&
1.69 ! afresh1 263: echo "Cannot install ${f##*/}, not found" >&2 && continue
1.58 afresh1 264: fetch "$f" || continue
265: verify "$f" || continue
1.68 afresh1 266: elif $DOWNLOAD; then
1.60 afresh1 267: echo "Already have $f"
1.61 afresh1 268: verify "$f" || continue
1.58 afresh1 269: fi
1.60 afresh1 270:
1.68 afresh1 271: "$INSTALL" || continue
1.11 afresh1 272:
1.65 afresh1 273: if [ "${installed[*]:-}" ]; then
1.50 afresh1 274: for i in "${installed[@]}"; do
275: delete_firmware "$i"
276: done
277: fi
1.11 afresh1 278:
1.50 afresh1 279: add_firmware "$f"
280: done
1.1 afresh1 281:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>