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