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

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

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