Annotation of openbsd/fw_update/fw_install.sh, Revision 1.63
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:
35: tmpdir() {
36: local _i=1 _dir
37:
1.50 afresh1 38: # If we're not in the installer,
39: # we have mktemp and a more hostile environment
40: if [ -x /usr/bin/mktemp ]; then
41: _dir=$( mktemp -d "${1}-XXXXXXXXX" )
1.44 afresh1 42: else
1.50 afresh1 43: until _dir="${1}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
1.44 afresh1 44: ((++_i < 10000)) || return 1
45: done
46: fi
1.50 afresh1 47:
1.8 afresh1 48: echo "$_dir"
49: }
1.6 afresh1 50:
1.55 afresh1 51: realpath () {
52: if [ -x /usr/bin/realpath ]; then
53: /usr/bin/realpath "$1"
54: elif [ "$1" = "${1%/*}" ]; then
55: echo "${PWD}/$1"
56: else
57: echo "$( cd "${1%/*}" && pwd )/${1##*/}"
58: fi
59: }
60:
1.50 afresh1 61: fetch() {
62: local _file=$1 _user=_file _exit
1.6 afresh1 63:
1.50 afresh1 64: # If we're not in the installer, we have su(1)
65: # and doas(1) is unlikely to be configured.
1.53 afresh1 66: if [ -x /usr/bin/su ]; then
1.50 afresh1 67: /usr/bin/su -s /bin/ksh "$_user" -c \
68: "/usr/bin/ftp -D 'Get/Verify' -Vm \
1.63 ! afresh1 69: -o- '${FWURL}/${_file}'" > "$_file"
1.50 afresh1 70: _exit="$?"
1.42 afresh1 71: else
1.50 afresh1 72: /usr/bin/doas -u "$_user" \
73: ftp -D 'Get/Verify' -Vm \
1.63 ! afresh1 74: -o- "${FWURL}/${_file}" > "$_file"
1.50 afresh1 75: _exit="$?"
1.42 afresh1 76: fi
1.6 afresh1 77:
1.50 afresh1 78: if [ "$_exit" -ne 0 ]; then
79: rm -f "$_file"
80: echo "Cannot fetch $_file" >&2
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.
87: if ! fgrep -qx "SHA256 ($1) = $( /bin/sha256 -qb "$1" )" "$CFILE"; then
88: echo "Checksum test for $1 failed." >&2
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}/+," \
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.60 afresh1 185: echo "Usage: fw_install [ -d dir ] [ driver | file [ ... ] ]"
1.59 afresh1 186: exit 2
187: }
188:
1.60 afresh1 189: DOWNLOADDIR=
190: while getopts d: name
1.59 afresh1 191: do
192: case "$name" in
1.60 afresh1 193: d) DOWNLOADDIR=$OPTARG ;;
1.59 afresh1 194: ?) usage 2 ;;
195: esac
196: done
197: shift $((OPTIND - 1))
198:
1.58 afresh1 199: set -A devices -- "$@"
200:
201: [ "${devices[*]:-}" ] ||
202: set -A devices -- $( devices_needing_firmware )
1.1 afresh1 203:
1.50 afresh1 204: if [ ! "${devices:-}" ]; then
205: echo "No devices found which need firmware files to be downloaded."
206: exit
207: fi
208:
1.58 afresh1 209: # Have to find the full path to firmware files
210: # so we can cd and still find them later.
211: i=0
212: while (( i < "${#devices[@]}" )); do
213: f="${devices[$i]}"
214: d=$( firmware_devicename "$f" )
215: [ "$f" = "$d" ] && f="$( echo "$f"-firmware-*.tgz | sed '$!d' )"
216: if [ -e "$f" ]; then
1.60 afresh1 217: if [ "${DOWNLOADDIR:-}" ]; then
218: echo "Cannot download local file $f" >&2
219: exit 2
220: fi
1.58 afresh1 221: devices[$i]="$d:$( realpath "$f" )"
222: fi
223: i=$((i + 1))
224: done
225:
1.60 afresh1 226: if [ "$DOWNLOADDIR" ]; then
227: if ! cd "$DOWNLOADDIR"; then
228: echo "Unable to use $DOWNLOADDIR, make sure it is a directory"
229: exit 2
230: fi
231: else
232: TMPDIR=$( tmpdir "${DESTDIR}/tmp/fw_install" )
233: cd "$TMPDIR"
234: fi
1.50 afresh1 235:
236: fetch "$CFILE"
1.54 afresh1 237: ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
1.50 afresh1 238: echo "Signature check of SHA256.sig failed" >&2 && exit 1
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
245: else
1.62 afresh1 246: d="${d%:*}"
1.58 afresh1 247: fi
248:
1.50 afresh1 249: set -A installed -- $( installed_firmware "$d" )
250:
1.60 afresh1 251: if [ ! "${DOWNLOADDIR:-}" ] && [ "${installed:-}" ]; then
1.50 afresh1 252: for i in "${installed[@]:-}"; do
1.58 afresh1 253: if [ "${f##*/}" = "$i.tgz" ]; then
1.50 afresh1 254: echo "$i already installed"
1.11 afresh1 255: continue 2
1.1 afresh1 256: fi
257: done
1.50 afresh1 258: fi
1.1 afresh1 259:
1.58 afresh1 260: if [ ! -e "$f" ]; then
261: fetch "$f" || continue
262: verify "$f" || continue
1.60 afresh1 263: elif [ "${DOWNLOADDIR:-}" ]; then
264: echo "Already have $f"
1.61 afresh1 265: verify "$f" || continue
1.58 afresh1 266: fi
1.60 afresh1 267:
268: [ "${DOWNLOADDIR:-}" ] && continue
1.11 afresh1 269:
1.50 afresh1 270: if [ "${installed:-}" ]; then
271: for i in "${installed[@]}"; do
272: delete_firmware "$i"
273: done
274: fi
1.11 afresh1 275:
1.50 afresh1 276: add_firmware "$f"
277: done
1.1 afresh1 278:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>