Annotation of openbsd/fw_update/fw_install.sh, Revision 1.11
1.1 afresh1 1: #!/bin/ksh
2: set -e
3:
4: scan_dmesg() {
5: # no bsort for now
6: sed -n "$1" /var/run/dmesg.boot
7: }
8:
9: installed_firmware() {
10: for fw in ${PKGDIR}/$1-firmware*; do
11: [ -e "$fw" ] || continue
12: echo ${fw##*/}
13: done
14: }
15:
1.8 afresh1 16: # tmpdir, do_as, unpriv, and unpriv2 are from install.sub
17:
18: # Create a temporary directory based on the supplied directory name prefix.
19: tmpdir() {
20: local _i=1 _dir
21:
22: until _dir="${1?}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
23: ((++_i < 10000)) || return 1
24: done
25: echo "$_dir"
26: }
1.6 afresh1 27:
28: # Run a command ($2+) as unprivileged user ($1).
29: # Take extra care that after "cmd" no "user" processes exist.
30: #
31: # Optionally:
32: # - create "file" and chown it to "user"
33: # - after "cmd", chown "file" back to root
34: #
35: # Usage: do_as user [-f file] cmd
36: do_as() {
37: (( $# >= 2 )) || return
38:
39: local _file _rc _user=$1
40: shift
41:
42: if [[ $1 == -f ]]; then
43: _file=$2
44: shift 2
45: fi
46:
47: if [[ -n $_file ]]; then
48: >$_file
49: chown "$_user" "$_file"
50: fi
51:
52: doas -u "$_user" "$@"
53: _rc=$?
54:
55: while doas -u "$_user" kill -9 -1 2>/dev/null; do
56: echo "Processes still running for user $_user after: $@"
57: sleep 1
58: done
59:
60: [[ -n $_file ]] && chown root "$_file"
61:
62: return $_rc
63: }
64:
65: unpriv() {
66: do_as _sndio "$@"
67: }
68:
69: unpriv2() {
70: do_as _file "$@"
71: }
72:
1.8 afresh1 73: _issue=
74: fail() {
75: echo $_issue >&2
76: exit 1
77: }
78:
1.7 afresh1 79: VNAME=$(sysctl -n kern.osrelease)
80: VERSION="${VNAME%.*}${VNAME#*.}"
81: FWDIR="$VNAME"
1.1 afresh1 82:
1.7 afresh1 83: HTTP_FWDIR=$FWDIR
84: set -- $(scan_dmesg "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p")
85: [[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots
86:
87: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
88: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.1 afresh1 89: PKGDIR=${DESTDIR}/var/db/pkg
90: PATTERNS="file:${0%/*}/firmware_patterns"
91:
1.11 ! afresh1 92: fw_update() {
! 93: local _tmpsrc _f _remove _r
! 94: local _src=$FWURL _t=Get _cfile="$_tmpsrc/SHA256" _srclocal=false
! 95: local _drivers=$(
! 96: last=''
! 97: ftp -D "Detecting" -Vmo- $PATTERNS |
! 98: while read d m; do
! 99: grep=grep
! 100: [ "$last" = "$d" ] && continue
! 101: [ "$m" ] || m="^$d[0-9][0-9]* at "
! 102: [ "$m" = "${m#^}" ] && grep=fgrep
! 103: $grep -q "$m" /var/run/dmesg.boot || continue
! 104: echo $d
! 105: last=$d
! 106: done
! 107: )
! 108:
! 109: if [ -z "$_drivers" ]; then
! 110: echo "No devices found which need firmware files to be downloaded." >&2
! 111: return
! 112: fi
1.1 afresh1 113:
1.11 ! afresh1 114: if _tmpsrc=$( tmpdir "${DESTDIR}/tmp/fw_update" ); then
! 115: (
! 116: >$_tmpsrc/t &&
! 117: $_unpriv cat $_tmpsrc/t
! 118: ) >/dev/null 2>&1 ||
! 119: rm -r $_tmpsrc
! 120: fi
1.1 afresh1 121:
1.11 ! afresh1 122: [[ ! -d $_tmpsrc ]] &&
! 123: _issue="Cannot create prefetch area" && fail
1.1 afresh1 124:
1.11 ! afresh1 125: cd "$_tmpsrc"
1.8 afresh1 126:
1.11 ! afresh1 127: ! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" &&
! 128: _issue="Cannot fetch SHA256.sig" && fail
1.1 afresh1 129:
1.11 ! afresh1 130: # Verify signature file with public keys.
! 131: ! unpriv -f "$_cfile" \
! 132: signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" &&
! 133: _issue="Signature check of SHA256.sig failed" && fail
! 134:
! 135: for d in $_drivers; do
! 136: _f=$( sed -n "s/.*(\($d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" )
! 137: installed=$( installed_firmware "$d" )
! 138:
! 139: for i in $installed; do
! 140: if [ "$_f" = "$i.tgz" ]; then
! 141: echo "Firmware for $d already installed ($installed)"
! 142: continue 2
1.1 afresh1 143: fi
144: done
145:
1.11 ! afresh1 146: rm -f /tmp/h /tmp/fail
! 147:
! 148: _t=Get/Verify
! 149: # Fetch firmware file and create a checksum by piping through
! 150: # sha256. Create a flag file in case ftp failed. Firmware
! 151: # from net is written to the prefetch area.
! 152: ( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) |
! 153: ( $_srclocal && unpriv2 sha256 -b >/tmp/h ||
! 154: unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" )
! 155:
! 156: # Handle failed transfer.
! 157: if [[ -f /tmp/fail ]]; then
! 158: rm -f "$_tmpsrc/$_f"
! 159: _issue="Fetching of $_f failed!"
! 160: fail
! 161: fi
! 162:
! 163: # Verify firmware by comparing its checksum with SHA256.
! 164: if fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then
! 165: #_unver=$(rmel $_f $_unver)
! 166: true
! 167: else
! 168: [[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc"
! 169: _issue="Checksum test for $_f failed."
! 170: fail
! 171: fi
! 172:
! 173: # TODO: Check hash for files before deleting
! 174: if [ "$installed" ] && [ -e "${PKGDIR}/$installed/+CONTENTS" ]; then
! 175: echo "Uninstalling $installed"
! 176: cwd=${PKGDIR}/$installed
! 177:
! 178: set -A _remove -- "${cwd}/+CONTENTS" "${cwd}"
! 179:
! 180: while read c g; do
! 181: case $c in
! 182: @cwd) cwd=$g
! 183: ;;
! 184: @*) continue
! 185: ;;
! 186: *) set -A _remove -- "$cwd/$c" "${_remove[@]}"
! 187: ;;
! 188: esac
! 189: done < "${PKGDIR}/$installed/+CONTENTS"
! 190:
! 191: for _r in "${_remove[@]}" ; do
! 192: if [ -d "$_r" ]; then
! 193: # Try hard not to actually remove recursively
! 194: # without rmdir on the install media.
! 195: [ "$_r/*" = $( echo "$_r"/* ) ] && rm -rf "$_r"
! 196: else
! 197: rm -f "$_r"
! 198: fi
! 199: done
! 200: fi
! 201:
! 202: # TODO: Add some details about the install to +CONTENTS like pkg_add
! 203: # TODO: Or, maybe we save the firmware someplace and make pkg_add reinstall
! 204: echo "Installing $_f"
! 205: tar -zxphf "$_f" -C /etc "firmware/*"
! 206: mkdir -p ${PKGDIR}/${_f%.tgz}/
! 207: tar -zxphf "$_f" -C "${PKGDIR}/${_f%.tgz}" "+*"
! 208: ed -s "${PKGDIR}/${_f%.tgz}/+CONTENTS" <<EOL
1.1 afresh1 209: /^@comment pkgpath/ -1a
210: @option manual-installation
211: @option firmware
212: @comment install-script
213: .
214: w
215: EOL
1.11 ! afresh1 216: done
! 217: }
1.1 afresh1 218:
1.11 ! afresh1 219: fw_update
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>