Annotation of openbsd/fw_update/fw_install.sh, Revision 1.30
1.1 afresh1 1: #!/bin/ksh
2: set -e
3:
1.24 afresh1 4: # Fake up some things from install.sub that we don't need to actually do
1.22 afresh1 5: prefetcharea_fs_list() {
1.27 afresh1 6: echo "${DESTDIR}/tmp"
1.22 afresh1 7: }
8: reset_watchdog() {
9: }
10:
1.8 afresh1 11: # tmpdir, do_as, unpriv, and unpriv2 are from install.sub
12:
13: # Create a temporary directory based on the supplied directory name prefix.
14: tmpdir() {
15: local _i=1 _dir
16:
17: until _dir="${1?}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
18: ((++_i < 10000)) || return 1
19: done
20: echo "$_dir"
21: }
1.6 afresh1 22:
23: # Run a command ($2+) as unprivileged user ($1).
24: # Take extra care that after "cmd" no "user" processes exist.
25: #
26: # Optionally:
27: # - create "file" and chown it to "user"
28: # - after "cmd", chown "file" back to root
29: #
30: # Usage: do_as user [-f file] cmd
31: do_as() {
32: (( $# >= 2 )) || return
33:
34: local _file _rc _user=$1
35: shift
36:
37: if [[ $1 == -f ]]; then
38: _file=$2
39: shift 2
40: fi
41:
42: if [[ -n $_file ]]; then
43: >$_file
44: chown "$_user" "$_file"
45: fi
46:
47: doas -u "$_user" "$@"
48: _rc=$?
49:
50: while doas -u "$_user" kill -9 -1 2>/dev/null; do
51: echo "Processes still running for user $_user after: $@"
52: sleep 1
53: done
54:
55: [[ -n $_file ]] && chown root "$_file"
56:
57: return $_rc
58: }
59:
60: unpriv() {
61: do_as _sndio "$@"
62: }
63:
64: unpriv2() {
65: do_as _file "$@"
66: }
67:
1.20 afresh1 68: # "fail" needs to be replaced with the "ask_yn" loop like in the installer.
1.8 afresh1 69: _issue=
70: fail() {
71: echo $_issue >&2
72: exit 1
73: }
74:
1.7 afresh1 75: VNAME=$(sysctl -n kern.osrelease)
76: VERSION="${VNAME%.*}${VNAME#*.}"
77: FWDIR="$VNAME"
1.29 afresh1 78: MODE=${MODE:-install}
1.1 afresh1 79:
1.14 afresh1 80: # TODO: We need the firmware for the system we just installed
81: # not the one we booted from. For example:
82: # * booting from a snapshot bsd.rd that thinks it is the 7.0 release
83: # will install the firmware from the 7.0 directory instead of
84: # from the snapshots dir.
85: # If they're using sysupgrade, then the installer kernel will be correct.
86: # If we're doing this in the installer we can check what they picked
87: # for downloading sets and use that value.
88: # Otherwise, the fw_update after first boot will fix it up for us.
89:
1.7 afresh1 90: HTTP_FWDIR=$FWDIR
1.30 ! afresh1 91: set -- sed -n "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p" /var/run/dmesg.boot
1.7 afresh1 92: [[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots
93:
94: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
95: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.20 afresh1 96: FWPATTERNS="file:${0%/*}/firmware_patterns"
1.1 afresh1 97:
1.20 afresh1 98: # TODO: support srclocal installation of firmware somehow
1.11 afresh1 99: fw_update() {
1.24 afresh1 100: local _src=$1 _tmpfs_list _tmpfs _tmpsrc \
1.27 afresh1 101: _t=Get _cfile="/tmp/SHA256" _pkgdir=${DESTDIR}/var/db/pkg \
1.25 afresh1 102: _f _r _remove _i _installed
103: local _srclocal=false _unpriv=unpriv
1.26 afresh1 104:
105: echo "Let's $MODE firmware!"
1.15 afresh1 106: local _d _drivers=$(
1.11 afresh1 107: last=''
1.24 afresh1 108: $_unpriv ftp -D "Detecting" -Vmo- $FWPATTERNS |
1.15 afresh1 109: while read _d _m; do
1.11 afresh1 110: grep=grep
1.15 afresh1 111: [ "$last" = "$_d" ] && continue
112: [ "$_m" ] || _m="^$_d[0-9][0-9]* at "
113: [ "$_m" = "${_m#^}" ] && grep=fgrep
114: $grep -q "$_m" /var/run/dmesg.boot || continue
115: echo $_d
116: last=$_d
1.11 afresh1 117: done
118: )
119:
120: if [ -z "$_drivers" ]; then
1.22 afresh1 121: echo "No devices found which need firmware files to be downloaded."
1.11 afresh1 122: return
123: fi
1.1 afresh1 124:
1.22 afresh1 125: ! _tmpfs_list=$(prefetcharea_fs_list) &&
126: echo "Cannot determine prefetch area" >&2 && return
127:
128: for _tmpfs in $_tmpfs_list; do
129: # Try to clean up from previous runs, assuming
130: # the _tmpfs selection yields the same mount
131: # point.
132: for _tmpsrc in $_tmpfs/firmware.+([0-9]).+([0-9]); do
133: [[ -d $_tmpsrc ]] && rm -r $_tmpsrc
134: done
135:
136: # Create a download directory for the firmware and
137: # check that the _sndio user can read files from
138: # it. Otherwise cleanup and skip the filesystem.
139: if _tmpsrc=$(tmpdir "$_tmpfs/firmware"); then
140: (
141: >$_tmpsrc/t &&
142: $_unpriv cat $_tmpsrc/t
143: ) >/dev/null 2>&1 && break ||
144: rm -r $_tmpsrc
145: fi
146: done
1.1 afresh1 147:
1.11 afresh1 148: [[ ! -d $_tmpsrc ]] &&
1.22 afresh1 149: echo "Cannot create prefetch area" >&2 && return 1
1.1 afresh1 150:
1.16 afresh1 151: # Cleanup from previous runs.
152: rm -f $_cfile $_cfile.sig
153:
1.21 afresh1 154: _t=Get/Verify
1.17 afresh1 155:
1.11 afresh1 156: ! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" &&
1.22 afresh1 157: echo "Cannot fetch SHA256.sig" >&2 && return 1
1.1 afresh1 158:
1.11 afresh1 159: # Verify signature file with public keys.
1.24 afresh1 160: ! $_unpriv -f "$_cfile" \
1.11 afresh1 161: signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" &&
1.22 afresh1 162: echo "Signature check of SHA256.sig failed" >&2 && return 1
1.11 afresh1 163:
1.15 afresh1 164: for _d in $_drivers; do
1.22 afresh1 165: $UU && reset_watchdog
1.15 afresh1 166: _f=$( sed -n "s/.*(\($_d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" )
1.19 afresh1 167: _installed=$(
1.22 afresh1 168: for fw in "${_pkgdir}/$_d-firmware"*; do
1.19 afresh1 169: [ -e "$fw" ] || continue
170: echo ${fw##*/}
171: done
1.21 afresh1 172: )
1.15 afresh1 173:
174: for _i in $_installed; do
175: if [ "$_f" = "$_i.tgz" ]; then
1.24 afresh1 176: echo "$_i already installed"
1.11 afresh1 177: continue 2
1.1 afresh1 178: fi
179: done
180:
1.11 afresh1 181: rm -f /tmp/h /tmp/fail
182:
183: # Fetch firmware file and create a checksum by piping through
184: # sha256. Create a flag file in case ftp failed. Firmware
185: # from net is written to the prefetch area.
186: ( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) |
187: ( $_srclocal && unpriv2 sha256 -b >/tmp/h ||
188: unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" )
189:
190: # Handle failed transfer.
191: if [[ -f /tmp/fail ]]; then
192: rm -f "$_tmpsrc/$_f"
1.22 afresh1 193: echo "Fetching of $_f failed!" >&2
194: continue
1.11 afresh1 195: fi
196:
197: # Verify firmware by comparing its checksum with SHA256.
1.22 afresh1 198: if ! fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then
1.11 afresh1 199: [[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc"
1.22 afresh1 200: echo "Checksum test for $_f failed." >&2
201: continue
1.11 afresh1 202: fi
203:
204: # TODO: Check hash for files before deleting
1.22 afresh1 205: if [ "$_installed" ] && [ -e "${_pkgdir}/$_installed/+CONTENTS" ]; then
1.18 afresh1 206: echo "Uninstalling $_installed"
1.22 afresh1 207: cwd=${_pkgdir}/$_installed
1.11 afresh1 208:
209: set -A _remove -- "${cwd}/+CONTENTS" "${cwd}"
210:
211: while read c g; do
212: case $c in
1.27 afresh1 213: @cwd) cwd="${DESTDIR}/$g"
1.11 afresh1 214: ;;
215: @*) continue
216: ;;
217: *) set -A _remove -- "$cwd/$c" "${_remove[@]}"
218: ;;
219: esac
1.22 afresh1 220: done < "${_pkgdir}/$_installed/+CONTENTS"
1.11 afresh1 221:
1.21 afresh1 222: # We specifically rm -f here because not removing files/dirs
223: # is probably not worth failing over.
1.11 afresh1 224: for _r in "${_remove[@]}" ; do
225: if [ -d "$_r" ]; then
226: # Try hard not to actually remove recursively
227: # without rmdir on the install media.
228: [ "$_r/*" = $( echo "$_r"/* ) ] && rm -rf "$_r"
229: else
230: rm -f "$_r"
231: fi
232: done
233: fi
234:
1.21 afresh1 235: # TODO: Should we mark these so real fw_update can -Drepair?
1.25 afresh1 236: ftp -D "Install" -Vmo- "file:$_tmpsrc/$_f" |
237: tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
1.28 afresh1 238: -s ",^firmware,${DESTDIR}/etc/firmware," \
1.25 afresh1 239: -C / -zxphf - "+*" "firmware/*"
1.23 afresh1 240:
1.22 afresh1 241: ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
1.1 afresh1 242: /^@comment pkgpath/ -1a
243: @option manual-installation
244: @option firmware
245: @comment install-script
246: .
247: w
248: EOL
1.11 afresh1 249: done
250: }
1.1 afresh1 251:
1.24 afresh1 252: fw_update "$FWURL"
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>