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

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() {
1.60      afresh1   190:        echo "Usage: fw_install [ -d dir ] [ driver | file [ ... ] ]"
1.59      afresh1   191:        exit 2
                    192: }
                    193:
1.60      afresh1   194: DOWNLOADDIR=
                    195: while getopts d: name
1.59      afresh1   196: do
                    197:        case "$name" in
1.60      afresh1   198:        d) DOWNLOADDIR=$OPTARG ;;
1.59      afresh1   199:        ?) usage 2 ;;
                    200:        esac
                    201: done
                    202: shift $((OPTIND - 1))
                    203:
1.58      afresh1   204: set -A devices -- "$@"
                    205:
                    206: [ "${devices[*]:-}" ] ||
                    207:     set -A devices -- $( devices_needing_firmware )
1.1       afresh1   208:
1.50      afresh1   209: if [ ! "${devices:-}" ]; then
                    210:        echo "No devices found which need firmware files to be downloaded."
                    211:        exit
                    212: fi
                    213:
1.58      afresh1   214: # Have to find the full path to firmware files
                    215: # so we can cd and still find them later.
                    216: i=0
                    217: while (( i < "${#devices[@]}" )); do
                    218:        f="${devices[$i]}"
                    219:        d=$( firmware_devicename "$f" )
                    220:        [ "$f" = "$d" ] && f="$( echo "$f"-firmware-*.tgz | sed '$!d' )"
                    221:        if [ -e "$f" ]; then
1.60      afresh1   222:                if [ "${DOWNLOADDIR:-}" ]; then
                    223:                        echo "Cannot download local file $f" >&2
                    224:                        exit 2
                    225:                fi
1.58      afresh1   226:                devices[$i]="$d:$( realpath "$f" )"
                    227:        fi
                    228:        i=$((i + 1))
                    229: done
                    230:
1.60      afresh1   231: if [ "$DOWNLOADDIR" ]; then
                    232:        if ! cd "$DOWNLOADDIR"; then
                    233:                echo "Unable to use $DOWNLOADDIR, make sure it is a directory"
                    234:                exit 2
                    235:        fi
                    236: else
                    237:        TMPDIR=$( tmpdir "${DESTDIR}/tmp/fw_install" )
                    238:        cd "$TMPDIR"
                    239: fi
1.50      afresh1   240:
                    241: # To unpriv we need to let the unpriv user into this dir
                    242: chmod go+x .
                    243:
                    244: fetch "$CFILE"
1.54      afresh1   245: ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
1.50      afresh1   246:     echo "Signature check of SHA256.sig failed" >&2 && exit 1
                    247:
                    248: for d in "${devices[@]}"; do
1.58      afresh1   249:        f="${d##*:}"
                    250:        if [ "$f" = "$d" ]; then
                    251:                f=$( firmware_filename "$d" || true )
                    252:                [ "$f" ] || continue
                    253:        else
1.62    ! afresh1   254:                d="${d%:*}"
1.58      afresh1   255:        fi
                    256:
1.50      afresh1   257:        set -A installed -- $( installed_firmware "$d" )
                    258:
1.60      afresh1   259:        if [ ! "${DOWNLOADDIR:-}" ] && [ "${installed:-}" ]; then
1.50      afresh1   260:                for i in "${installed[@]:-}"; do
1.58      afresh1   261:                        if [ "${f##*/}" = "$i.tgz" ]; then
1.50      afresh1   262:                                echo "$i already installed"
1.11      afresh1   263:                                continue 2
1.1       afresh1   264:                        fi
                    265:                done
1.50      afresh1   266:        fi
1.1       afresh1   267:
1.58      afresh1   268:        if [ ! -e "$f" ]; then
                    269:                fetch  "$f" || continue
                    270:                verify "$f" || continue
1.60      afresh1   271:        elif [ "${DOWNLOADDIR:-}" ]; then
                    272:                echo "Already have $f"
1.61      afresh1   273:                verify "$f" || continue
1.58      afresh1   274:        fi
1.60      afresh1   275:
                    276:        [ "${DOWNLOADDIR:-}" ] && continue
1.11      afresh1   277:
1.50      afresh1   278:        if [ "${installed:-}" ]; then
                    279:                for i in "${installed[@]}"; do
                    280:                        delete_firmware "$i"
                    281:                done
                    282:        fi
1.11      afresh1   283:
1.50      afresh1   284:        add_firmware "$f"
                    285: done
1.1       afresh1   286:

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