[BACK]Return to fw_install.sh CVS log [TXT][DIR] Up to [local] / openbsd / fw_update

Annotation of openbsd/fw_update/fw_install.sh, Revision 1.71

1.1       afresh1     1: #!/bin/ksh
1.36      afresh1     2: #      $OpenBSD$
1.50      afresh1     3: #
1.38      afresh1     4: # Copyright (c) 2021 Andrew Hewus Fresh <afresh1@openbsd.org>
                      5: #
                      6: # Permission to use, copy, modify, and distribute this software for any
                      7: # purpose with or without fee is hereby granted, provided that the above
                      8: # copyright notice and this permission notice appear in all copies.
                      9: #
                     10: # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11: # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12: # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13: # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15: # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16: # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:
1.50      afresh1    18: set -o errexit -o pipefail -o nounset
                     19:
                     20: CFILE=SHA256.sig
                     21: DESTDIR=${DESTDIR:-}
                     22: FWPATTERNS="${DESTDIR}/usr/share/misc/firmware_patterns"
                     23:
                     24: VNAME=${VNAME:-$(sysctl -n kern.osrelease)}
                     25: VERSION=${VERSION:-"${VNAME%.*}${VNAME#*.}"}
                     26:
                     27: HTTP_FWDIR="$VNAME"
1.52      afresh1    28: VTYPE=$( sed -n "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p" \
                     29:     /var/run/dmesg.boot | sed '$!d' )
1.50      afresh1    30: [[ $VTYPE == -!(stable) ]] && HTTP_FWDIR=snapshots
1.22      afresh1    31:
1.50      afresh1    32: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
                     33: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.8       afresh1    34:
1.69      afresh1    35: LOCALSRC=
                     36:
1.8       afresh1    37: tmpdir() {
                     38:        local _i=1 _dir
                     39:
1.50      afresh1    40:        # If we're not in the installer,
                     41:        # we have mktemp and a more hostile environment
                     42:        if [ -x /usr/bin/mktemp ]; then
                     43:                _dir=$( mktemp -d "${1}-XXXXXXXXX" )
1.44      afresh1    44:        else
1.50      afresh1    45:                until _dir="${1}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
1.44      afresh1    46:                    ((++_i < 10000)) || return 1
                     47:                done
                     48:        fi
1.50      afresh1    49:
1.8       afresh1    50:        echo "$_dir"
                     51: }
1.6       afresh1    52:
1.50      afresh1    53: fetch() {
1.69      afresh1    54:        local _src="${FWURL}/${1##*/}" _dst=$1 _user=_file _exit
1.6       afresh1    55:
1.50      afresh1    56:        # If we're not in the installer, we have su(1)
                     57:        # and doas(1) is unlikely to be configured.
1.53      afresh1    58:        if [ -x /usr/bin/su ]; then
1.50      afresh1    59:                /usr/bin/su -s /bin/ksh "$_user" -c \
1.69      afresh1    60:                    "/usr/bin/ftp -D 'Get/Verify' -Vm -o- '$_src'" > "$_dst"
1.50      afresh1    61:                _exit="$?"
1.42      afresh1    62:        else
1.50      afresh1    63:                /usr/bin/doas -u "$_user" \
1.69      afresh1    64:                    /usr/bin/ftp -D 'Get/Verify' -Vm -o- "$_src" > "$_dst"
1.50      afresh1    65:                _exit="$?"
1.42      afresh1    66:        fi
1.6       afresh1    67:
1.50      afresh1    68:        if [ "$_exit" -ne 0 ]; then
1.69      afresh1    69:                rm -f "$_dst"
                     70:                echo "Cannot fetch $_src" >&2
1.50      afresh1    71:                return 1
                     72:        fi
1.6       afresh1    73: }
                     74:
1.50      afresh1    75: verify() {
                     76:        # On the installer we don't get sha256 -C, so fake it.
1.69      afresh1    77:        if ! fgrep -qx "SHA256 (${1##*/}) = $( /bin/sha256 -qb "$1" )" "$CFILE"; then
                     78:                echo "Checksum test for ${1##*/} failed." >&2
1.50      afresh1    79:                return 1
                     80:        fi
1.6       afresh1    81: }
                     82:
1.50      afresh1    83: devices_needing_firmware() {
                     84:        local _d _m _grep _dmesgtail _last=''
1.1       afresh1    85:
1.50      afresh1    86:        # When we're not in the installer, the dmesg.boot can
                     87:        # contain multiple boots, so only look in the last one
                     88:        _dmesgtail=$( sed -n 'H;/^OpenBSD/h;${g;p;}' /var/run/dmesg.boot )
                     89:
                     90:        grep -v '^[[:space:]]*#' "$FWPATTERNS" |
                     91:            while read -r _d _m; do
                     92:                _grep="grep"
                     93:                [ "$_last" = "$_d" ] && continue
                     94:                [ "$_m" ] || _m="^${_d}[0-9][0-9]* at "
                     95:                [ "$_m" = "${_m#^}" ] && _grep="fgrep"
                     96:
                     97:                echo "$_dmesgtail" | $_grep -q "$_m" || continue
                     98:                echo "$_d"
                     99:                _last="$_d"
                    100:        done
                    101: }
1.14      afresh1   102:
1.50      afresh1   103: firmware_filename() {
1.56      afresh1   104:        local _f
                    105:        _f="$( sed -n "s/.*(\($1-firmware-.*\.tgz\)).*/\1/p" "$CFILE" | sed '$!d' )"
                    106:        ! [ "$_f" ] && echo "Unable to find firmware for $1" >&2 && return 1
                    107:        echo "$_f"
1.50      afresh1   108: }
1.7       afresh1   109:
1.57      afresh1   110: firmware_devicename() {
                    111:        local _d="${1##*/}"
                    112:        _d="${_d%-firmware-*}"
                    113:        echo "$_d"
                    114: }
                    115:
1.50      afresh1   116: installed_firmware() {
                    117:        for fw in "${DESTDIR}/var/db/pkg/$1-firmware"*; do
                    118:                [ -e "$fw" ] || continue
                    119:                echo "${fw##*/}"
                    120:        done
                    121: }
1.1       afresh1   122:
1.50      afresh1   123: add_firmware () {
1.58      afresh1   124:        local _f="${1##*/}" _pkgdir="${DESTDIR}/var/db/pkg"
1.50      afresh1   125:        ftp -D "Install" -Vmo- "file:${1}" |
                    126:                tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
1.69      afresh1   127:                    -s ",^firmware,${DESTDIR}/etc/firmware," \
                    128:                    -C / -zxphf - "+*" "firmware/*"
1.1       afresh1   129:
1.50      afresh1   130:        # TODO: Should we mark these so real fw_update can -Drepair?
                    131:        ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
                    132: /^@comment pkgpath/ -1a
                    133: @option manual-installation
                    134: @option firmware
                    135: @comment install-script
                    136: .
                    137: w
                    138: EOL
                    139: }
1.22      afresh1   140:
1.50      afresh1   141: delete_firmware() {
1.51      afresh1   142:        local _cwd _pkg="$1" _pkgdir="${DESTDIR}/var/db/pkg"
1.22      afresh1   143:
1.50      afresh1   144:        # TODO: Check hash for files before deleting
                    145:        echo "Uninstalling $_pkg"
1.51      afresh1   146:        _cwd="${_pkgdir}/$_pkg"
1.50      afresh1   147:
1.51      afresh1   148:        set -A _remove -- "${_cwd}/+CONTENTS" "${_cwd}"
1.50      afresh1   149:
                    150:        while read -r c g; do
                    151:                case $c in
1.51      afresh1   152:                @cwd) _cwd="${DESTDIR}$g"
1.50      afresh1   153:                  ;;
                    154:                @*) continue
                    155:                  ;;
1.52      afresh1   156:                *) set -A _remove -- "$_cwd/$c" "${_remove[@]}"
1.50      afresh1   157:                  ;;
                    158:                esac
                    159:        done < "${_pkgdir}/${_pkg}/+CONTENTS"
                    160:
                    161:        # We specifically rm -f here because not removing files/dirs
                    162:        # is probably not worth failing over.
                    163:        for _r in "${_remove[@]}" ; do
                    164:                if [ -d "$_r" ]; then
                    165:                        # Try hard not to actually remove recursively
                    166:                        # without rmdir on the install media.
                    167:                        [ "$_r/*" = "$( echo "$_r"/* )" ] && rm -rf "$_r"
                    168:                else
                    169:                        rm -f "$_r"
1.22      afresh1   170:                fi
                    171:        done
1.50      afresh1   172: }
1.1       afresh1   173:
1.59      afresh1   174: usage() {
1.66      afresh1   175:        echo "usage: fw_install [-d dir | -L dir] [driver | file [...]]"
1.59      afresh1   176:        exit 2
                    177: }
                    178:
1.68      afresh1   179: INSTALL=true
                    180: DOWNLOAD=true
                    181:
                    182: while getopts dL name
1.59      afresh1   183: do
                    184:        case "$name" in
1.68      afresh1   185:        # "download only" means local dir and don't install
1.69      afresh1   186:        d) LOCALSRC=. INSTALL=false ;;
                    187:        L) LOCALSRC=. ;;
1.59      afresh1   188:        ?) usage 2 ;;
                    189:        esac
                    190: done
                    191: shift $((OPTIND - 1))
                    192:
1.68      afresh1   193: # If we're installing from a local dir
                    194: # we don't want to download anything
1.69      afresh1   195: [ "$LOCALSRC" ] && "$INSTALL" && DOWNLOAD=false
                    196: [ "$LOCALSRC" ] || LOCALSRC="$( tmpdir "${DESTDIR}/tmp/fw_install" )"
1.64      afresh1   197:
1.58      afresh1   198: set -A devices -- "$@"
                    199:
                    200: [ "${devices[*]:-}" ] ||
                    201:     set -A devices -- $( devices_needing_firmware )
1.1       afresh1   202:
1.65      afresh1   203: if [ ! "${devices[*]:-}" ]; then
1.50      afresh1   204:        echo "No devices found which need firmware files to be downloaded."
                    205:        exit
                    206: fi
                    207:
1.71    ! afresh1   208: if "$DOWNLOAD"; then
1.64      afresh1   209:        fetch "$CFILE"
                    210:        ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
                    211:            echo "Signature check of SHA256.sig failed" >&2 && exit 1
                    212: fi
1.50      afresh1   213:
1.70      afresh1   214: for f in "${devices[@]}"; do
                    215:        d="$( firmware_devicename "$f" )"
                    216:
1.58      afresh1   217:        if [ "$f" = "$d" ]; then
                    218:                f=$( firmware_filename "$d" || true )
                    219:                [ "$f" ] || continue
1.69      afresh1   220:                f="$LOCALSRC/$f"
1.70      afresh1   221:        elif ! "$INSTALL" && ! grep -q "($f)" "$CFILE" ; then
                    222:                echo "Cannot download local file $f" >&2
                    223:                exit 2
1.58      afresh1   224:        fi
                    225:
1.50      afresh1   226:        set -A installed -- $( installed_firmware "$d" )
                    227:
1.68      afresh1   228:        if "$INSTALL" && [ "${installed[*]:-}" ]; then
1.65      afresh1   229:                for i in "${installed[@]}"; do
1.58      afresh1   230:                        if [ "${f##*/}" = "$i.tgz" ]; then
1.50      afresh1   231:                                echo "$i already installed"
1.11      afresh1   232:                                continue 2
1.1       afresh1   233:                        fi
                    234:                done
1.50      afresh1   235:        fi
1.1       afresh1   236:
1.58      afresh1   237:        if [ ! -e "$f" ]; then
1.68      afresh1   238:                "$INSTALL" && ! "$DOWNLOAD" &&
1.69      afresh1   239:                    echo "Cannot install ${f##*/}, not found" >&2 && continue
1.58      afresh1   240:                fetch  "$f" || continue
                    241:                verify "$f" || continue
1.68      afresh1   242:        elif $DOWNLOAD; then
1.60      afresh1   243:                echo "Already have $f"
1.61      afresh1   244:                verify "$f" || continue
1.58      afresh1   245:        fi
1.60      afresh1   246:
1.68      afresh1   247:        "$INSTALL" || continue
1.11      afresh1   248:
1.65      afresh1   249:        if [ "${installed[*]:-}" ]; then
1.50      afresh1   250:                for i in "${installed[@]}"; do
                    251:                        delete_firmware "$i"
                    252:                done
                    253:        fi
1.11      afresh1   254:
1.50      afresh1   255:        add_firmware "$f"
                    256: done
1.1       afresh1   257:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>