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

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

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