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

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.95      afresh1    18: set -o errexit -o pipefail -o nounset -o noclobber -o noglob
1.85      afresh1    19: set +o monitor
1.77      afresh1    20: export PATH=/usr/bin:/bin:/usr/sbin:/sbin
1.50      afresh1    21:
                     22: CFILE=SHA256.sig
                     23: DESTDIR=${DESTDIR:-}
                     24: FWPATTERNS="${DESTDIR}/usr/share/misc/firmware_patterns"
                     25:
                     26: VNAME=${VNAME:-$(sysctl -n kern.osrelease)}
                     27: VERSION=${VERSION:-"${VNAME%.*}${VNAME#*.}"}
                     28:
                     29: HTTP_FWDIR="$VNAME"
1.52      afresh1    30: VTYPE=$( sed -n "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p" \
                     31:     /var/run/dmesg.boot | sed '$!d' )
1.50      afresh1    32: [[ $VTYPE == -!(stable) ]] && HTTP_FWDIR=snapshots
1.22      afresh1    33:
1.50      afresh1    34: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
                     35: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.8       afresh1    36:
1.97      afresh1    37: VERBOSE=false
1.111     afresh1    38: DELETE=false
1.87      afresh1    39: DOWNLOAD=true
                     40: INSTALL=true
1.69      afresh1    41: LOCALSRC=
                     42:
1.126     afresh1    43: unset FTPPID
1.129   ! afresh1    44: unset FWPKGTMP
1.126     afresh1    45: REMOVE_LOCALSRC=false
                     46: cleanup() {
1.127     afresh1    47:        set +o errexit # ignore errors from killing ftp
1.128     afresh1    48:        [ "${FTPPID:-}" ] && kill -TERM -"$FTPPID" 2>/dev/null
1.129   ! afresh1    49:        [ "${FWPKGTMP:-}" ] && rm -rf "$FWPKGTMP"
1.126     afresh1    50:        "$REMOVE_LOCALSRC" && rm -rf "$LOCALSRC"
                     51: }
                     52: trap cleanup EXIT
                     53:
1.8       afresh1    54: tmpdir() {
                     55:        local _i=1 _dir
                     56:
1.50      afresh1    57:        # If we're not in the installer,
1.79      afresh1    58:        # we have mktemp and a more hostile environment.
1.50      afresh1    59:        if [ -x /usr/bin/mktemp ]; then
                     60:                _dir=$( mktemp -d "${1}-XXXXXXXXX" )
1.44      afresh1    61:        else
1.50      afresh1    62:                until _dir="${1}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
1.44      afresh1    63:                    ((++_i < 10000)) || return 1
                     64:                done
                     65:        fi
1.50      afresh1    66:
1.8       afresh1    67:        echo "$_dir"
                     68: }
1.6       afresh1    69:
1.50      afresh1    70: fetch() {
1.126     afresh1    71:        local _src="${FWURL}/${1##*/}" _dst=$1 _user=_file _exit _error=''
1.6       afresh1    72:
1.79      afresh1    73:        # If we're not in the installer,
                     74:        # we have su(1) and doas(1) is unlikely to be configured.
1.85      afresh1    75:        set -o monitor # make sure ftp gets its own process group
                     76:        (
1.101     afresh1    77:        flags=-VM
                     78:        "$VERBOSE" && flags=-vm
1.53      afresh1    79:        if [ -x /usr/bin/su ]; then
1.85      afresh1    80:                exec /usr/bin/su -s /bin/ksh "$_user" -c \
1.119     afresh1    81:                    "/usr/bin/ftp -N '${0##/}' -D 'Get/Verify' $flags -o- '$_src'" > "$_dst"
1.42      afresh1    82:        else
1.85      afresh1    83:                exec /usr/bin/doas -u "$_user" \
1.119     afresh1    84:                    /usr/bin/ftp -N "${0##/}" -D 'Get/Verify' $flags -o- "$_src" > "$_dst"
1.42      afresh1    85:        fi
1.126     afresh1    86:        ) & FTPPID=$!
1.85      afresh1    87:        set +o monitor
                     88:
                     89:        SECONDS=0
                     90:        _last=0
1.126     afresh1    91:        while kill -0 -"$FTPPID" 2>/dev/null; do
1.85      afresh1    92:                if [[ $SECONDS -gt 12 ]]; then
                     93:                        set -- $( ls -ln "$_dst" 2>/dev/null )
                     94:                        if [[ $_last -ne $5 ]]; then
                     95:                                _last=$5
                     96:                                SECONDS=0
                     97:                                sleep 1
                     98:                        else
1.126     afresh1    99:                                kill -INT -"$FTPPID"
1.92      afresh1   100:                                _error=" (timed out)"
1.85      afresh1   101:                        fi
                    102:                else
                    103:                        sleep 1
                    104:                fi
                    105:        done
                    106:
                    107:        set +o errexit
1.126     afresh1   108:        wait "$FTPPID"
1.85      afresh1   109:        _exit=$?
                    110:        set -o errexit
                    111:
1.126     afresh1   112:        unset FTPPID
1.6       afresh1   113:
1.50      afresh1   114:        if [ "$_exit" -ne 0 ]; then
1.69      afresh1   115:                rm -f "$_dst"
1.92      afresh1   116:                echo "Cannot fetch $_src$_error" >&2
1.50      afresh1   117:                return 1
                    118:        fi
1.6       afresh1   119: }
                    120:
1.50      afresh1   121: verify() {
                    122:        # On the installer we don't get sha256 -C, so fake it.
1.69      afresh1   123:        if ! fgrep -qx "SHA256 (${1##*/}) = $( /bin/sha256 -qb "$1" )" "$CFILE"; then
                    124:                echo "Checksum test for ${1##*/} failed." >&2
1.50      afresh1   125:                return 1
                    126:        fi
1.6       afresh1   127: }
                    128:
1.124     afresh1   129: firmware_in_dmesg() {
1.90      afresh1   130:        local _d _m _line _dmesgtail _last='' _nl=$( echo )
1.1       afresh1   131:
1.50      afresh1   132:        # When we're not in the installer, the dmesg.boot can
                    133:        # contain multiple boots, so only look in the last one
1.89      afresh1   134:        _dmesgtail="$( echo ; sed -n 'H;/^OpenBSD/h;${g;p;}' /var/run/dmesg.boot |
                    135:            grep -e "^[a-z][a-z]*[0-9]" -e " not configured " )"
1.50      afresh1   136:
1.89      afresh1   137:        grep -v '^[[:space:]]*#' "$FWPATTERNS" |
                    138:            while read -r _d _m; do
                    139:                [ "$_d" = "$_last" ] && continue
1.90      afresh1   140:                [ "$_m" ]             || _m="${_nl}${_d}[0-9] at "
                    141:                [ "$_m" = "${_m#^}" ] || _m="${_nl}${_m#^}"
1.83      afresh1   142:
1.89      afresh1   143:                if [[ $_dmesgtail = *$_m* ]]; then
                    144:                        echo "$_d"
                    145:                        _last="$_d"
                    146:                fi
                    147:            done
1.50      afresh1   148: }
1.14      afresh1   149:
1.50      afresh1   150: firmware_filename() {
1.56      afresh1   151:        local _f
                    152:        _f="$( sed -n "s/.*(\($1-firmware-.*\.tgz\)).*/\1/p" "$CFILE" | sed '$!d' )"
                    153:        ! [ "$_f" ] && echo "Unable to find firmware for $1" >&2 && return 1
                    154:        echo "$_f"
1.50      afresh1   155: }
1.7       afresh1   156:
1.57      afresh1   157: firmware_devicename() {
                    158:        local _d="${1##*/}"
                    159:        _d="${_d%-firmware-*}"
                    160:        echo "$_d"
                    161: }
                    162:
1.50      afresh1   163: installed_firmware() {
1.110     afresh1   164:        local _pre="$1" _match="$2" _post="$3" _firmware
1.123     afresh1   165:        set -sA _firmware -- $(
1.109     afresh1   166:            set +o noglob
                    167:            grep -Fxl '@option firmware' \
1.110     afresh1   168:                "${DESTDIR}/var/db/pkg/"$_pre"$_match"$_post"/+CONTENTS" \
1.109     afresh1   169:                2>/dev/null || true
                    170:            set -o noglob
                    171:        )
                    172:
                    173:        [ "${_firmware[*]:-}" ] || return 0
                    174:        for fw in "${_firmware[@]}"; do
                    175:                fw="${fw%/+CONTENTS}"
1.50      afresh1   176:                echo "${fw##*/}"
                    177:        done
                    178: }
1.1       afresh1   179:
1.124     afresh1   180: detect_firmware() {
                    181:        local _devices _last='' _d
                    182:
                    183:        set -sA _devices -- $(
                    184:            firmware_in_dmesg
                    185:            for _d in $( installed_firmware '*' '-firmware-' '*' ); do
                    186:                echo "$( firmware_devicename "$_d" )"
                    187:            done
                    188:        )
                    189:
                    190:        [ "${_devices[*]:-}" ] || return 0
                    191:        for _d in "${_devices[@]}"; do
                    192:                [[ $_last = $_d ]] && continue
                    193:                echo $_d
                    194:                _last="$_d"
                    195:        done
                    196: }
                    197:
1.50      afresh1   198: add_firmware () {
1.106     afresh1   199:        local _f="${1##*/}" _pkgname
1.129   ! afresh1   200:        FWPKGTMP="$( tmpdir "${DESTDIR}/var/db/pkg/.firmware" )"
1.101     afresh1   201:        local flags=-VM
                    202:        "$VERBOSE" && flags=-vm
1.119     afresh1   203:        ftp -N "${0##/}" -D "Install" "$flags" -o- "file:${1}" |
1.129   ! afresh1   204:                tar -s ",^\+,${FWPKGTMP}/+," \
1.69      afresh1   205:                    -s ",^firmware,${DESTDIR}/etc/firmware," \
                    206:                    -C / -zxphf - "+*" "firmware/*"
1.1       afresh1   207:
1.129   ! afresh1   208:        _pkgname="$( sed -n '/^@name /{s///p;q;}' "${FWPKGTMP}/+CONTENTS" )"
1.106     afresh1   209:        if [ ! "$_pkgname" ]; then
                    210:                echo "Failed to extract name from $1, partial install" 2>&1
1.129   ! afresh1   211:                rm -rf "$FWPKGTMP"
        !           212:                unset FWPKGTMP
1.106     afresh1   213:                return 1
                    214:        fi
                    215:
1.50      afresh1   216:        # TODO: Should we mark these so real fw_update can -Drepair?
1.129   ! afresh1   217:        ed -s "${FWPKGTMP}/+CONTENTS" <<EOL
1.50      afresh1   218: /^@comment pkgpath/ -1a
                    219: @option manual-installation
                    220: @option firmware
                    221: @comment install-script
                    222: .
                    223: w
                    224: EOL
1.106     afresh1   225:
1.129   ! afresh1   226:        chmod 755 "$FWPKGTMP"
        !           227:        mv "$FWPKGTMP" "${DESTDIR}/var/db/pkg/${_pkgname}"
        !           228:        unset FWPKGTMP
1.50      afresh1   229: }
1.22      afresh1   230:
1.50      afresh1   231: delete_firmware() {
1.51      afresh1   232:        local _cwd _pkg="$1" _pkgdir="${DESTDIR}/var/db/pkg"
1.22      afresh1   233:
1.50      afresh1   234:        # TODO: Check hash for files before deleting
1.101     afresh1   235:        "$VERBOSE" && echo "Uninstalling $_pkg"
1.51      afresh1   236:        _cwd="${_pkgdir}/$_pkg"
1.50      afresh1   237:
1.107     afresh1   238:        if [ ! -e "$_cwd/+CONTENTS" ] ||
                    239:            ! grep -Fxq '@option firmware' "$_cwd/+CONTENTS"; then
                    240:                echo "${0##*/}: $_pkg does not appear to be firmware" >&2
                    241:                return 2
                    242:        fi
                    243:
1.51      afresh1   244:        set -A _remove -- "${_cwd}/+CONTENTS" "${_cwd}"
1.50      afresh1   245:
                    246:        while read -r c g; do
                    247:                case $c in
1.51      afresh1   248:                @cwd) _cwd="${DESTDIR}$g"
1.50      afresh1   249:                  ;;
                    250:                @*) continue
                    251:                  ;;
1.52      afresh1   252:                *) set -A _remove -- "$_cwd/$c" "${_remove[@]}"
1.50      afresh1   253:                  ;;
                    254:                esac
                    255:        done < "${_pkgdir}/${_pkg}/+CONTENTS"
                    256:
                    257:        # We specifically rm -f here because not removing files/dirs
                    258:        # is probably not worth failing over.
                    259:        for _r in "${_remove[@]}" ; do
                    260:                if [ -d "$_r" ]; then
                    261:                        # Try hard not to actually remove recursively
                    262:                        # without rmdir on the install media.
1.95      afresh1   263:                        set +o noglob
1.50      afresh1   264:                        [ "$_r/*" = "$( echo "$_r"/* )" ] && rm -rf "$_r"
1.95      afresh1   265:                        set -o noglob
1.50      afresh1   266:                else
                    267:                        rm -f "$_r"
1.22      afresh1   268:                fi
                    269:        done
1.50      afresh1   270: }
1.1       afresh1   271:
1.59      afresh1   272: usage() {
1.122     afresh1   273:        echo "usage:  ${0##*/} [-d | -D] [-av] [-p path] [driver | file ...]"
1.59      afresh1   274:        exit 2
                    275: }
                    276:
1.113     afresh1   277: ALL=false
1.87      afresh1   278: OPT_D=
1.122     afresh1   279: while getopts :adDp:v name
1.59      afresh1   280: do
                    281:        case "$name" in
1.113     afresh1   282:        a) ALL=true ;;
1.111     afresh1   283:        d) DELETE=true ;;
1.87      afresh1   284:        D) OPT_D=true ;;
1.121     afresh1   285:        p) LOCALSRC="$OPTARG" ;;
1.97      afresh1   286:        v) VERBOSE=true ;;
1.121     afresh1   287:        :)
                    288:           echo "${0##*/}: option requires an argument -- -$OPTARG" >&2
                    289:           usage 2
                    290:           ;;
                    291:        ?)
                    292:           echo "${0##*/}: unknown option -- -$OPTARG" >&2
                    293:           usage 2
                    294:           ;;
1.59      afresh1   295:        esac
                    296: done
                    297: shift $((OPTIND - 1))
                    298:
1.121     afresh1   299: if [ "$LOCALSRC" ]; then
                    300:        if [[ $LOCALSRC = @(ftp|http?(s))://* ]]; then
                    301:                FWURL="${LOCALSRC}"
                    302:                LOCALSRC=
                    303:        else
                    304:                LOCALSRC="${LOCALSRC:#file:}"
                    305:                ! [ -d "$LOCALSRC" ] &&
                    306:                    echo "The path must be a URL or an existing directory" >&2 &&
                    307:                    exit 2
                    308:        fi
                    309: fi
                    310:
1.122     afresh1   311: # "Download only" means local dir and don't install
1.87      afresh1   312: if [ "$OPT_D" ]; then
                    313:        INSTALL=false
1.121     afresh1   314:        LOCALSRC="${LOCALSRC:-.}"
1.87      afresh1   315: fi
1.64      afresh1   316:
1.118     afresh1   317: if [ -x /usr/bin/id ] && [ "$(/usr/bin/id -u)" != 0 ]; then
                    318:        echo "need root privileges" >&2
                    319:        exit 1
1.94      afresh1   320: fi
                    321:
1.123     afresh1   322: set -sA devices -- "$@"
1.111     afresh1   323:
                    324: if "$DELETE"; then
1.122     afresh1   325:        [ "$OPT_D" ] && usage 22
1.111     afresh1   326:
1.113     afresh1   327:        set -A installed
                    328:        if [ "${devices[*]:-}" ]; then
                    329:                "$ALL" && usage 22
                    330:
                    331:                set -A installed -- $(
                    332:                    for d in "${devices[@]}"; do
                    333:                        f="${d##*/}"  # only care about the name
                    334:                        f="${f%.tgz}" # allow specifying the package name
                    335:                        [ "$( firmware_devicename "$f" )" = "$f" ] && f="$f-firmware"
                    336:
                    337:                        set -A i -- $( installed_firmware '' "$f-" '*' )
                    338:
                    339:                        if [ "${i[*]:-}" ]; then
                    340:                                echo "${i[@]}"
                    341:                        else
                    342:                                echo "No firmware found for '$d'" >&2
                    343:                        fi
                    344:                    done
                    345:                )
                    346:        elif "$ALL"; then
                    347:                set -A installed -- $( installed_firmware '*' '-firmware-' '*' )
                    348:        fi
1.111     afresh1   349:
                    350:        deleted=''
1.113     afresh1   351:        if [ "${installed:-}" ]; then
1.111     afresh1   352:                for fw in "${installed[@]}"; do
                    353:                        delete_firmware "$fw" || continue
1.112     afresh1   354:                        deleted="$deleted,$( firmware_devicename "$fw" )"
1.111     afresh1   355:                done
1.113     afresh1   356:        fi
1.111     afresh1   357:
                    358:        deleted="${deleted:+${deleted#,}}"
                    359:        echo "${0:##*/}: deleted ${deleted:-none}";
                    360:
                    361:        exit
                    362: fi
1.125     afresh1   363:
                    364: if [ ! "$LOCALSRC" ]; then
                    365:     LOCALSRC="$( tmpdir "${DESTDIR}/tmp/${0##*/}" )"
1.126     afresh1   366:     REMOVE_LOCALSRC=true
1.125     afresh1   367: fi
                    368:
                    369: CFILE="$LOCALSRC/$CFILE"
1.58      afresh1   370:
1.113     afresh1   371: if [ "${devices[*]:-}" ]; then
                    372:        "$ALL" && usage 22
                    373: else
1.101     afresh1   374:        "$VERBOSE" && echo -n "Detecting firmware ..."
1.124     afresh1   375:        set -sA devices -- $( detect_firmware )
1.101     afresh1   376:        "$VERBOSE" &&
                    377:            { [ "${devices[*]:-}" ] && echo " found." || echo " done." ; }
1.50      afresh1   378: fi
1.91      afresh1   379:
                    380: [ "${devices[*]:-}" ] || exit
1.50      afresh1   381:
1.71      afresh1   382: if "$DOWNLOAD"; then
1.116     afresh1   383:        set +o noclobber # we want to get the latest CFILE
1.64      afresh1   384:        fetch "$CFILE"
1.116     afresh1   385:        set -o noclobber
1.64      afresh1   386:        ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
                    387:            echo "Signature check of SHA256.sig failed" >&2 && exit 1
1.120     afresh1   388: elif [ ! -e "$CFILE" ]; then
                    389:        # TODO: We shouldn't need a CFILE if all arguments are files.
                    390:        echo "${0##*/}: $CFILE: No such file or directory" >&2
                    391:        exit 2
1.64      afresh1   392: fi
1.50      afresh1   393:
1.103     afresh1   394: added=''
                    395: updated=''
1.115     afresh1   396: kept=''
1.70      afresh1   397: for f in "${devices[@]}"; do
                    398:        d="$( firmware_devicename "$f" )"
                    399:
1.58      afresh1   400:        if [ "$f" = "$d" ]; then
                    401:                f=$( firmware_filename "$d" || true )
                    402:                [ "$f" ] || continue
1.69      afresh1   403:                f="$LOCALSRC/$f"
1.76      afresh1   404:        elif ! "$INSTALL" && ! grep -Fq "($f)" "$CFILE" ; then
1.70      afresh1   405:                echo "Cannot download local file $f" >&2
                    406:                exit 2
1.58      afresh1   407:        fi
                    408:
1.110     afresh1   409:        set -A installed -- $( installed_firmware '' "$d-firmware-" '*' )
1.50      afresh1   410:
1.68      afresh1   411:        if "$INSTALL" && [ "${installed[*]:-}" ]; then
1.65      afresh1   412:                for i in "${installed[@]}"; do
1.58      afresh1   413:                        if [ "${f##*/}" = "$i.tgz" ]; then
1.101     afresh1   414:                                "$VERBOSE" && echo "$i already installed"
1.115     afresh1   415:                                kept="$kept,$d"
1.11      afresh1   416:                                continue 2
1.1       afresh1   417:                        fi
                    418:                done
1.50      afresh1   419:        fi
1.1       afresh1   420:
1.72      afresh1   421:        if [ -e "$f" ]; then
                    422:                if "$DOWNLOAD"; then
1.101     afresh1   423:                        "$VERBOSE" && echo "Verify existing ${f##*/}"
1.72      afresh1   424:                        verify "$f" || continue
1.117     afresh1   425:                        "$INSTALL"  || kept="$kept,$d"
1.72      afresh1   426:                # else assume it was verified when downloaded
                    427:                fi
1.75      afresh1   428:        elif "$DOWNLOAD"; then
                    429:                fetch  "$f" || continue
                    430:                verify "$f" || continue
1.117     afresh1   431:                "$INSTALL"  || added="$added,$d"
1.75      afresh1   432:        elif "$INSTALL"; then
1.72      afresh1   433:                echo "Cannot install ${f##*/}, not found" >&2
                    434:                continue
1.58      afresh1   435:        fi
1.60      afresh1   436:
1.68      afresh1   437:        "$INSTALL" || continue
1.11      afresh1   438:
1.103     afresh1   439:        removed=false
1.65      afresh1   440:        if [ "${installed[*]:-}" ]; then
1.50      afresh1   441:                for i in "${installed[@]}"; do
                    442:                        delete_firmware "$i"
1.103     afresh1   443:                        removed=true
1.50      afresh1   444:                done
                    445:        fi
1.11      afresh1   446:
1.50      afresh1   447:        add_firmware "$f"
1.103     afresh1   448:
                    449:        if "$removed"; then
1.114     afresh1   450:                updated="$updated,$d"
1.103     afresh1   451:        else
1.114     afresh1   452:                added="$added,$d"
1.103     afresh1   453:        fi
1.50      afresh1   454: done
1.1       afresh1   455:
1.114     afresh1   456: added="${added:#,}"
                    457: updated="${updated:#,}"
1.115     afresh1   458: kept="${kept:#,}"
1.117     afresh1   459: if "$INSTALL"; then
                    460:        echo  "${0##*/}: added ${added:-none}; updated ${updated:-none}; kept ${kept:-none}"
                    461: else
                    462:        echo  "${0##*/}: downloaded ${added:-none}; kept ${kept:-none}"
                    463: fi

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