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

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

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