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