Annotation of openbsd/fw_update/fw_install.sh, Revision 1.12
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
1.12 ! afresh1 94: local _src=$FWURL _t=Get _cfile="/tmp/SHA256" _srclocal=false
1.11 afresh1 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: ! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" &&
126: _issue="Cannot fetch SHA256.sig" && fail
1.1 afresh1 127:
1.11 afresh1 128: # Verify signature file with public keys.
129: ! unpriv -f "$_cfile" \
130: signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" &&
131: _issue="Signature check of SHA256.sig failed" && fail
132:
133: for d in $_drivers; do
134: _f=$( sed -n "s/.*(\($d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" )
135: installed=$( installed_firmware "$d" )
136:
137: for i in $installed; do
138: if [ "$_f" = "$i.tgz" ]; then
139: echo "Firmware for $d already installed ($installed)"
140: continue 2
1.1 afresh1 141: fi
142: done
143:
1.11 afresh1 144: rm -f /tmp/h /tmp/fail
145:
146: _t=Get/Verify
147: # Fetch firmware file and create a checksum by piping through
148: # sha256. Create a flag file in case ftp failed. Firmware
149: # from net is written to the prefetch area.
150: ( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) |
151: ( $_srclocal && unpriv2 sha256 -b >/tmp/h ||
152: unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" )
153:
154: # Handle failed transfer.
155: if [[ -f /tmp/fail ]]; then
156: rm -f "$_tmpsrc/$_f"
157: _issue="Fetching of $_f failed!"
158: fail
159: fi
160:
161: # Verify firmware by comparing its checksum with SHA256.
162: if fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then
163: #_unver=$(rmel $_f $_unver)
164: true
165: else
166: [[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc"
167: _issue="Checksum test for $_f failed."
168: fail
169: fi
170:
171: # TODO: Check hash for files before deleting
172: if [ "$installed" ] && [ -e "${PKGDIR}/$installed/+CONTENTS" ]; then
173: echo "Uninstalling $installed"
174: cwd=${PKGDIR}/$installed
175:
176: set -A _remove -- "${cwd}/+CONTENTS" "${cwd}"
177:
178: while read c g; do
179: case $c in
180: @cwd) cwd=$g
181: ;;
182: @*) continue
183: ;;
184: *) set -A _remove -- "$cwd/$c" "${_remove[@]}"
185: ;;
186: esac
187: done < "${PKGDIR}/$installed/+CONTENTS"
188:
189: for _r in "${_remove[@]}" ; do
190: if [ -d "$_r" ]; then
191: # Try hard not to actually remove recursively
192: # without rmdir on the install media.
193: [ "$_r/*" = $( echo "$_r"/* ) ] && rm -rf "$_r"
194: else
195: rm -f "$_r"
196: fi
197: done
198: fi
199:
200: # TODO: Add some details about the install to +CONTENTS like pkg_add
201: # TODO: Or, maybe we save the firmware someplace and make pkg_add reinstall
202: echo "Installing $_f"
1.12 ! afresh1 203: tar -zxphf "$_tmpsrc/$_f" -C /etc "firmware/*"
1.11 afresh1 204: mkdir -p ${PKGDIR}/${_f%.tgz}/
1.12 ! afresh1 205: tar -zxphf "$_tmpsrc/$_f" -C "${PKGDIR}/${_f%.tgz}" "+*"
1.11 afresh1 206: ed -s "${PKGDIR}/${_f%.tgz}/+CONTENTS" <<EOL
1.1 afresh1 207: /^@comment pkgpath/ -1a
208: @option manual-installation
209: @option firmware
210: @comment install-script
211: .
212: w
213: EOL
1.11 afresh1 214: done
215: }
1.1 afresh1 216:
1.11 afresh1 217: fw_update
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>