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

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

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