Annotation of openbsd/fw_update/fw_install.sh, Revision 1.14
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.14 ! afresh1 83: # TODO: We need the firmware for the system we just installed
! 84: # not the one we booted from. For example:
! 85: # * booting from a snapshot bsd.rd that thinks it is the 7.0 release
! 86: # will install the firmware from the 7.0 directory instead of
! 87: # from the snapshots dir.
! 88: # If they're using sysupgrade, then the installer kernel will be correct.
! 89: # If we're doing this in the installer we can check what they picked
! 90: # for downloading sets and use that value.
! 91: # Otherwise, the fw_update after first boot will fix it up for us.
! 92:
1.7 afresh1 93: HTTP_FWDIR=$FWDIR
94: set -- $(scan_dmesg "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p")
95: [[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots
96:
97: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
98: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.1 afresh1 99: PKGDIR=${DESTDIR}/var/db/pkg
100: PATTERNS="file:${0%/*}/firmware_patterns"
101:
1.11 afresh1 102: fw_update() {
103: local _tmpsrc _f _remove _r
1.12 afresh1 104: local _src=$FWURL _t=Get _cfile="/tmp/SHA256" _srclocal=false
1.11 afresh1 105: local _drivers=$(
106: last=''
107: ftp -D "Detecting" -Vmo- $PATTERNS |
108: while read d m; do
109: grep=grep
110: [ "$last" = "$d" ] && continue
111: [ "$m" ] || m="^$d[0-9][0-9]* at "
112: [ "$m" = "${m#^}" ] && grep=fgrep
113: $grep -q "$m" /var/run/dmesg.boot || continue
114: echo $d
115: last=$d
116: done
117: )
118:
119: if [ -z "$_drivers" ]; then
120: echo "No devices found which need firmware files to be downloaded." >&2
121: return
122: fi
1.1 afresh1 123:
1.11 afresh1 124: if _tmpsrc=$( tmpdir "${DESTDIR}/tmp/fw_update" ); then
125: (
126: >$_tmpsrc/t &&
127: $_unpriv cat $_tmpsrc/t
128: ) >/dev/null 2>&1 ||
129: rm -r $_tmpsrc
130: fi
1.1 afresh1 131:
1.11 afresh1 132: [[ ! -d $_tmpsrc ]] &&
133: _issue="Cannot create prefetch area" && fail
1.1 afresh1 134:
1.11 afresh1 135: ! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" &&
136: _issue="Cannot fetch SHA256.sig" && fail
1.1 afresh1 137:
1.11 afresh1 138: # Verify signature file with public keys.
139: ! unpriv -f "$_cfile" \
140: signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" &&
141: _issue="Signature check of SHA256.sig failed" && fail
142:
143: for d in $_drivers; do
144: _f=$( sed -n "s/.*(\($d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" )
145: installed=$( installed_firmware "$d" )
146:
147: for i in $installed; do
148: if [ "$_f" = "$i.tgz" ]; then
149: echo "Firmware for $d already installed ($installed)"
150: continue 2
1.1 afresh1 151: fi
152: done
153:
1.11 afresh1 154: rm -f /tmp/h /tmp/fail
155:
156: _t=Get/Verify
157: # Fetch firmware file and create a checksum by piping through
158: # sha256. Create a flag file in case ftp failed. Firmware
159: # from net is written to the prefetch area.
160: ( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) |
161: ( $_srclocal && unpriv2 sha256 -b >/tmp/h ||
162: unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" )
163:
164: # Handle failed transfer.
165: if [[ -f /tmp/fail ]]; then
166: rm -f "$_tmpsrc/$_f"
167: _issue="Fetching of $_f failed!"
168: fail
169: fi
170:
171: # Verify firmware by comparing its checksum with SHA256.
172: if fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then
173: #_unver=$(rmel $_f $_unver)
174: true
175: else
176: [[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc"
177: _issue="Checksum test for $_f failed."
178: fail
179: fi
180:
181: # TODO: Check hash for files before deleting
182: if [ "$installed" ] && [ -e "${PKGDIR}/$installed/+CONTENTS" ]; then
183: echo "Uninstalling $installed"
184: cwd=${PKGDIR}/$installed
185:
186: set -A _remove -- "${cwd}/+CONTENTS" "${cwd}"
187:
188: while read c g; do
189: case $c in
1.13 afresh1 190: @cwd) cwd="${DESTDIR}/$g"
1.11 afresh1 191: ;;
192: @*) continue
193: ;;
194: *) set -A _remove -- "$cwd/$c" "${_remove[@]}"
195: ;;
196: esac
197: done < "${PKGDIR}/$installed/+CONTENTS"
198:
199: for _r in "${_remove[@]}" ; do
200: if [ -d "$_r" ]; then
201: # Try hard not to actually remove recursively
202: # without rmdir on the install media.
203: [ "$_r/*" = $( echo "$_r"/* ) ] && rm -rf "$_r"
204: else
205: rm -f "$_r"
206: fi
207: done
208: fi
209:
210: # TODO: Add some details about the install to +CONTENTS like pkg_add
211: # TODO: Or, maybe we save the firmware someplace and make pkg_add reinstall
212: echo "Installing $_f"
1.13 afresh1 213: tar -zxphf "$_tmpsrc/$_f" -C "${DESTDIR}/etc" "firmware/*"
1.11 afresh1 214: mkdir -p ${PKGDIR}/${_f%.tgz}/
1.12 afresh1 215: tar -zxphf "$_tmpsrc/$_f" -C "${PKGDIR}/${_f%.tgz}" "+*"
1.11 afresh1 216: ed -s "${PKGDIR}/${_f%.tgz}/+CONTENTS" <<EOL
1.1 afresh1 217: /^@comment pkgpath/ -1a
218: @option manual-installation
219: @option firmware
220: @comment install-script
221: .
222: w
223: EOL
1.11 afresh1 224: done
225: }
1.1 afresh1 226:
1.11 afresh1 227: fw_update
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>