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

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:        >"$_file"
                     65:        chown "$_user" "$_file"
1.6       afresh1    66:
1.50      afresh1    67:        # If we're not in the installer, we have su(1)
                     68:        # and doas(1) is unlikely to be configured.
1.53      afresh1    69:        if [ -x /usr/bin/su ]; then
1.50      afresh1    70:                /usr/bin/su -s /bin/ksh "$_user" -c \
                     71:                    "/usr/bin/ftp -D 'Get/Verify' -Vm \
                     72:                        -o '$_file' '${FWURL}/${_file}'"
                     73:                _exit="$?"
1.42      afresh1    74:        else
1.50      afresh1    75:                /usr/bin/doas -u "$_user" \
                     76:                    ftp -D 'Get/Verify' -Vm \
                     77:                        -o "$_file" "${FWURL}/${_file}"
                     78:                _exit="$?"
1.42      afresh1    79:        fi
1.6       afresh1    80:
1.50      afresh1    81:        if [ "$_exit" -ne 0 ]; then
                     82:                rm -f "$_file"
                     83:                echo "Cannot fetch $_file" >&2
                     84:                return 1
                     85:        fi
1.6       afresh1    86:
1.50      afresh1    87:        chown root "$_file"
1.6       afresh1    88: }
                     89:
1.50      afresh1    90: verify() {
                     91:        # On the installer we don't get sha256 -C, so fake it.
                     92:        if ! fgrep -qx "SHA256 ($1) = $( /bin/sha256 -qb "$1" )" "$CFILE"; then
                     93:                echo "Checksum test for $1 failed." >&2
                     94:                return 1
                     95:        fi
1.6       afresh1    96: }
                     97:
1.50      afresh1    98: devices_needing_firmware() {
                     99:        local _d _m _grep _dmesgtail _last=''
1.1       afresh1   100:
1.50      afresh1   101:        # When we're not in the installer, the dmesg.boot can
                    102:        # contain multiple boots, so only look in the last one
                    103:        _dmesgtail=$( sed -n 'H;/^OpenBSD/h;${g;p;}' /var/run/dmesg.boot )
                    104:
                    105:        grep -v '^[[:space:]]*#' "$FWPATTERNS" |
                    106:            while read -r _d _m; do
                    107:                _grep="grep"
                    108:                [ "$_last" = "$_d" ] && continue
                    109:                [ "$_m" ] || _m="^${_d}[0-9][0-9]* at "
                    110:                [ "$_m" = "${_m#^}" ] && _grep="fgrep"
                    111:
                    112:                echo "$_dmesgtail" | $_grep -q "$_m" || continue
                    113:                echo "$_d"
                    114:                _last="$_d"
                    115:        done
                    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.58      afresh1   139:        local _f="${1##*/}" _pkgdir="${DESTDIR}/var/db/pkg"
1.50      afresh1   140:        ftp -D "Install" -Vmo- "file:${1}" |
                    141:                tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
                    142:                -s ",^firmware,${DESTDIR}/etc/firmware," \
                    143:                -C / -zxphf - "+*" "firmware/*"
1.1       afresh1   144:
1.50      afresh1   145:        # TODO: Should we mark these so real fw_update can -Drepair?
                    146:        ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
                    147: /^@comment pkgpath/ -1a
                    148: @option manual-installation
                    149: @option firmware
                    150: @comment install-script
                    151: .
                    152: w
                    153: EOL
                    154: }
1.22      afresh1   155:
1.50      afresh1   156: delete_firmware() {
1.51      afresh1   157:        local _cwd _pkg="$1" _pkgdir="${DESTDIR}/var/db/pkg"
1.22      afresh1   158:
1.50      afresh1   159:        # TODO: Check hash for files before deleting
                    160:        echo "Uninstalling $_pkg"
1.51      afresh1   161:        _cwd="${_pkgdir}/$_pkg"
1.50      afresh1   162:
1.51      afresh1   163:        set -A _remove -- "${_cwd}/+CONTENTS" "${_cwd}"
1.50      afresh1   164:
                    165:        while read -r c g; do
                    166:                case $c in
1.51      afresh1   167:                @cwd) _cwd="${DESTDIR}$g"
1.50      afresh1   168:                  ;;
                    169:                @*) continue
                    170:                  ;;
1.52      afresh1   171:                *) set -A _remove -- "$_cwd/$c" "${_remove[@]}"
1.50      afresh1   172:                  ;;
                    173:                esac
                    174:        done < "${_pkgdir}/${_pkg}/+CONTENTS"
                    175:
                    176:        # We specifically rm -f here because not removing files/dirs
                    177:        # is probably not worth failing over.
                    178:        for _r in "${_remove[@]}" ; do
                    179:                if [ -d "$_r" ]; then
                    180:                        # Try hard not to actually remove recursively
                    181:                        # without rmdir on the install media.
                    182:                        [ "$_r/*" = "$( echo "$_r"/* )" ] && rm -rf "$_r"
                    183:                else
                    184:                        rm -f "$_r"
1.22      afresh1   185:                fi
                    186:        done
1.50      afresh1   187: }
1.1       afresh1   188:
1.59    ! afresh1   189: usage() {
        !           190:        echo "Usage: fw_install [ driver | file [ ... ] ]"
        !           191:        exit 2
        !           192: }
        !           193:
        !           194: while getopts '?' name
        !           195: do
        !           196:        case "$name" in
        !           197:        ?) usage 2 ;;
        !           198:        esac
        !           199: done
        !           200: shift $((OPTIND - 1))
        !           201:
1.58      afresh1   202: set -A devices -- "$@"
                    203:
                    204: [ "${devices[*]:-}" ] ||
                    205:     set -A devices -- $( devices_needing_firmware )
1.1       afresh1   206:
1.50      afresh1   207: if [ ! "${devices:-}" ]; then
                    208:        echo "No devices found which need firmware files to be downloaded."
                    209:        exit
                    210: fi
                    211:
1.58      afresh1   212: # Have to find the full path to firmware files
                    213: # so we can cd and still find them later.
                    214: i=0
                    215: while (( i < "${#devices[@]}" )); do
                    216:        f="${devices[$i]}"
                    217:        d=$( firmware_devicename "$f" )
                    218:        [ "$f" = "$d" ] && f="$( echo "$f"-firmware-*.tgz | sed '$!d' )"
                    219:        if [ -e "$f" ]; then
                    220:                devices[$i]="$d:$( realpath "$f" )"
                    221:        fi
                    222:        i=$((i + 1))
                    223: done
                    224:
1.50      afresh1   225: TMPDIR=$( tmpdir "${DESTDIR}/tmp/fw_install" )
                    226: cd "$TMPDIR"
                    227:
                    228: # To unpriv we need to let the unpriv user into this dir
                    229: chmod go+x .
                    230:
                    231: fetch "$CFILE"
1.54      afresh1   232: ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
1.50      afresh1   233:     echo "Signature check of SHA256.sig failed" >&2 && exit 1
                    234:
                    235: for d in "${devices[@]}"; do
1.58      afresh1   236:        f="${d##*:}"
                    237:        if [ "$f" = "$d" ]; then
                    238:                f=$( firmware_filename "$d" || true )
                    239:                [ "$f" ] || continue
                    240:        else
                    241:            d="${d%:*}"
                    242:        fi
                    243:
1.50      afresh1   244:        set -A installed -- $( installed_firmware "$d" )
                    245:
                    246:        if [ "${installed:-}" ]; then
                    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.58      afresh1   255:        if [ ! -e "$f" ]; then
                    256:                fetch  "$f" || continue
                    257:                verify "$f" || continue
                    258:        fi
1.11      afresh1   259:
1.50      afresh1   260:        if [ "${installed:-}" ]; then
                    261:                for i in "${installed[@]}"; do
                    262:                        delete_firmware "$i"
                    263:                done
                    264:        fi
1.11      afresh1   265:
1.50      afresh1   266:        add_firmware "$f"
                    267: done
1.1       afresh1   268:

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