[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.64

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:
                     35: tmpdir() {
                     36:        local _i=1 _dir
                     37:
1.50      afresh1    38:        # If we're not in the installer,
                     39:        # we have mktemp and a more hostile environment
                     40:        if [ -x /usr/bin/mktemp ]; then
                     41:                _dir=$( mktemp -d "${1}-XXXXXXXXX" )
1.44      afresh1    42:        else
1.50      afresh1    43:                until _dir="${1}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
1.44      afresh1    44:                    ((++_i < 10000)) || return 1
                     45:                done
                     46:        fi
1.50      afresh1    47:
1.8       afresh1    48:        echo "$_dir"
                     49: }
1.6       afresh1    50:
1.55      afresh1    51: realpath () {
                     52:        if [ -x /usr/bin/realpath ]; then
                     53:                /usr/bin/realpath "$1"
                     54:        elif [ "$1" = "${1%/*}" ]; then
                     55:                echo "${PWD}/$1"
                     56:        else
                     57:                echo "$( cd "${1%/*}" && pwd )/${1##*/}"
                     58:        fi
                     59: }
                     60:
1.50      afresh1    61: fetch() {
                     62:        local _file=$1 _user=_file _exit
1.6       afresh1    63:
1.50      afresh1    64:        # If we're not in the installer, we have su(1)
                     65:        # and doas(1) is unlikely to be configured.
1.53      afresh1    66:        if [ -x /usr/bin/su ]; then
1.50      afresh1    67:                /usr/bin/su -s /bin/ksh "$_user" -c \
                     68:                    "/usr/bin/ftp -D 'Get/Verify' -Vm \
1.63      afresh1    69:                        -o- '${FWURL}/${_file}'" > "$_file"
1.50      afresh1    70:                _exit="$?"
1.42      afresh1    71:        else
1.50      afresh1    72:                /usr/bin/doas -u "$_user" \
                     73:                    ftp -D 'Get/Verify' -Vm \
1.63      afresh1    74:                        -o- "${FWURL}/${_file}" > "$_file"
1.50      afresh1    75:                _exit="$?"
1.42      afresh1    76:        fi
1.6       afresh1    77:
1.50      afresh1    78:        if [ "$_exit" -ne 0 ]; then
                     79:                rm -f "$_file"
                     80:                echo "Cannot fetch $_file" >&2
                     81:                return 1
                     82:        fi
1.6       afresh1    83: }
                     84:
1.50      afresh1    85: verify() {
                     86:        # On the installer we don't get sha256 -C, so fake it.
                     87:        if ! fgrep -qx "SHA256 ($1) = $( /bin/sha256 -qb "$1" )" "$CFILE"; then
                     88:                echo "Checksum test for $1 failed." >&2
                     89:                return 1
                     90:        fi
1.6       afresh1    91: }
                     92:
1.50      afresh1    93: devices_needing_firmware() {
                     94:        local _d _m _grep _dmesgtail _last=''
1.1       afresh1    95:
1.50      afresh1    96:        # When we're not in the installer, the dmesg.boot can
                     97:        # contain multiple boots, so only look in the last one
                     98:        _dmesgtail=$( sed -n 'H;/^OpenBSD/h;${g;p;}' /var/run/dmesg.boot )
                     99:
                    100:        grep -v '^[[:space:]]*#' "$FWPATTERNS" |
                    101:            while read -r _d _m; do
                    102:                _grep="grep"
                    103:                [ "$_last" = "$_d" ] && continue
                    104:                [ "$_m" ] || _m="^${_d}[0-9][0-9]* at "
                    105:                [ "$_m" = "${_m#^}" ] && _grep="fgrep"
                    106:
                    107:                echo "$_dmesgtail" | $_grep -q "$_m" || continue
                    108:                echo "$_d"
                    109:                _last="$_d"
                    110:        done
                    111: }
1.14      afresh1   112:
1.50      afresh1   113: firmware_filename() {
1.56      afresh1   114:        local _f
                    115:        _f="$( sed -n "s/.*(\($1-firmware-.*\.tgz\)).*/\1/p" "$CFILE" | sed '$!d' )"
                    116:        ! [ "$_f" ] && echo "Unable to find firmware for $1" >&2 && return 1
                    117:        echo "$_f"
1.50      afresh1   118: }
1.7       afresh1   119:
1.57      afresh1   120: firmware_devicename() {
                    121:        local _d="${1##*/}"
                    122:        _d="${_d%-firmware-*}"
                    123:        echo "$_d"
                    124: }
                    125:
1.50      afresh1   126: installed_firmware() {
                    127:        for fw in "${DESTDIR}/var/db/pkg/$1-firmware"*; do
                    128:                [ -e "$fw" ] || continue
                    129:                echo "${fw##*/}"
                    130:        done
                    131: }
1.1       afresh1   132:
1.50      afresh1   133: add_firmware () {
1.58      afresh1   134:        local _f="${1##*/}" _pkgdir="${DESTDIR}/var/db/pkg"
1.50      afresh1   135:        ftp -D "Install" -Vmo- "file:${1}" |
                    136:                tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
                    137:                -s ",^firmware,${DESTDIR}/etc/firmware," \
                    138:                -C / -zxphf - "+*" "firmware/*"
1.1       afresh1   139:
1.50      afresh1   140:        # TODO: Should we mark these so real fw_update can -Drepair?
                    141:        ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
                    142: /^@comment pkgpath/ -1a
                    143: @option manual-installation
                    144: @option firmware
                    145: @comment install-script
                    146: .
                    147: w
                    148: EOL
                    149: }
1.22      afresh1   150:
1.50      afresh1   151: delete_firmware() {
1.51      afresh1   152:        local _cwd _pkg="$1" _pkgdir="${DESTDIR}/var/db/pkg"
1.22      afresh1   153:
1.50      afresh1   154:        # TODO: Check hash for files before deleting
                    155:        echo "Uninstalling $_pkg"
1.51      afresh1   156:        _cwd="${_pkgdir}/$_pkg"
1.50      afresh1   157:
1.51      afresh1   158:        set -A _remove -- "${_cwd}/+CONTENTS" "${_cwd}"
1.50      afresh1   159:
                    160:        while read -r c g; do
                    161:                case $c in
1.51      afresh1   162:                @cwd) _cwd="${DESTDIR}$g"
1.50      afresh1   163:                  ;;
                    164:                @*) continue
                    165:                  ;;
1.52      afresh1   166:                *) set -A _remove -- "$_cwd/$c" "${_remove[@]}"
1.50      afresh1   167:                  ;;
                    168:                esac
                    169:        done < "${_pkgdir}/${_pkg}/+CONTENTS"
                    170:
                    171:        # We specifically rm -f here because not removing files/dirs
                    172:        # is probably not worth failing over.
                    173:        for _r in "${_remove[@]}" ; do
                    174:                if [ -d "$_r" ]; then
                    175:                        # Try hard not to actually remove recursively
                    176:                        # without rmdir on the install media.
                    177:                        [ "$_r/*" = "$( echo "$_r"/* )" ] && rm -rf "$_r"
                    178:                else
                    179:                        rm -f "$_r"
1.22      afresh1   180:                fi
                    181:        done
1.50      afresh1   182: }
1.1       afresh1   183:
1.59      afresh1   184: usage() {
1.64    ! afresh1   185:        echo "Usage: fw_install [ -d dir | -L dir ] [ driver | file [ ... ] ]"
1.59      afresh1   186:        exit 2
                    187: }
                    188:
1.60      afresh1   189: DOWNLOADDIR=
1.64    ! afresh1   190: LOCALDIR=
        !           191: while getopts d:L: name
1.59      afresh1   192: do
                    193:        case "$name" in
1.60      afresh1   194:        d) DOWNLOADDIR=$OPTARG ;;
1.64    ! afresh1   195:        L) LOCALDIR=$OPTARG    ;;
1.59      afresh1   196:        ?) usage 2 ;;
                    197:        esac
                    198: done
                    199: shift $((OPTIND - 1))
                    200:
1.64    ! afresh1   201: if [[ -n "${DOWNLOADDIR:-}" && -n "${LOCALDIR:-}" ]]; then
        !           202:        echo "Cannot use -d and -L" >&2
        !           203:        usage 2
        !           204: fi
        !           205:
1.58      afresh1   206: set -A devices -- "$@"
                    207:
                    208: [ "${devices[*]:-}" ] ||
                    209:     set -A devices -- $( devices_needing_firmware )
1.1       afresh1   210:
1.50      afresh1   211: if [ ! "${devices:-}" ]; then
                    212:        echo "No devices found which need firmware files to be downloaded."
                    213:        exit
                    214: fi
                    215:
1.58      afresh1   216: # Have to find the full path to firmware files
                    217: # so we can cd and still find them later.
                    218: i=0
                    219: while (( i < "${#devices[@]}" )); do
                    220:        f="${devices[$i]}"
                    221:        d=$( firmware_devicename "$f" )
                    222:        [ "$f" = "$d" ] && f="$( echo "$f"-firmware-*.tgz | sed '$!d' )"
                    223:        if [ -e "$f" ]; then
1.60      afresh1   224:                if [ "${DOWNLOADDIR:-}" ]; then
                    225:                        echo "Cannot download local file $f" >&2
                    226:                        exit 2
                    227:                fi
1.58      afresh1   228:                devices[$i]="$d:$( realpath "$f" )"
                    229:        fi
                    230:        i=$((i + 1))
                    231: done
                    232:
1.60      afresh1   233: if [ "$DOWNLOADDIR" ]; then
                    234:        if ! cd "$DOWNLOADDIR"; then
                    235:                echo "Unable to use $DOWNLOADDIR, make sure it is a directory"
                    236:                exit 2
                    237:        fi
1.64    ! afresh1   238: elif [ "$LOCALDIR" ]; then
        !           239:        if ! cd "$LOCALDIR"; then
        !           240:                echo "Unable to use $LOCALDIR, make sure it is a directory"
        !           241:                exit 2
        !           242:        fi
1.60      afresh1   243: else
                    244:        TMPDIR=$( tmpdir "${DESTDIR}/tmp/fw_install" )
                    245:        cd "$TMPDIR"
                    246: fi
1.50      afresh1   247:
1.64    ! afresh1   248: if ! [[ -n "$LOCALDIR" && -e "$CFILE" ]]; then
        !           249:        fetch "$CFILE"
        !           250:        ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
        !           251:            echo "Signature check of SHA256.sig failed" >&2 && exit 1
        !           252: fi
1.50      afresh1   253:
                    254: for d in "${devices[@]}"; do
1.58      afresh1   255:        f="${d##*:}"
                    256:        if [ "$f" = "$d" ]; then
                    257:                f=$( firmware_filename "$d" || true )
                    258:                [ "$f" ] || continue
                    259:        else
1.62      afresh1   260:                d="${d%:*}"
1.58      afresh1   261:        fi
                    262:
1.50      afresh1   263:        set -A installed -- $( installed_firmware "$d" )
                    264:
1.60      afresh1   265:        if [ ! "${DOWNLOADDIR:-}" ] && [ "${installed:-}" ]; then
1.50      afresh1   266:                for i in "${installed[@]:-}"; do
1.58      afresh1   267:                        if [ "${f##*/}" = "$i.tgz" ]; then
1.50      afresh1   268:                                echo "$i already installed"
1.11      afresh1   269:                                continue 2
1.1       afresh1   270:                        fi
                    271:                done
1.50      afresh1   272:        fi
1.1       afresh1   273:
1.58      afresh1   274:        if [ ! -e "$f" ]; then
1.64    ! afresh1   275:                [ "$LOCALDIR" ] && echo "Cannot install $f, not found" >&2 && continue
1.58      afresh1   276:                fetch  "$f" || continue
                    277:                verify "$f" || continue
1.60      afresh1   278:        elif [ "${DOWNLOADDIR:-}" ]; then
                    279:                echo "Already have $f"
1.61      afresh1   280:                verify "$f" || continue
1.58      afresh1   281:        fi
1.60      afresh1   282:
                    283:        [ "${DOWNLOADDIR:-}" ] && continue
1.11      afresh1   284:
1.50      afresh1   285:        if [ "${installed:-}" ]; then
                    286:                for i in "${installed[@]}"; do
                    287:                        delete_firmware "$i"
                    288:                done
                    289:        fi
1.11      afresh1   290:
1.50      afresh1   291:        add_firmware "$f"
                    292: done
1.1       afresh1   293:

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