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

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

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