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