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

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 () {
                    139:        local _f="$1" _pkgdir="${DESTDIR}/var/db/pkg"
                    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.50      afresh1   189: set -A devices -- $( devices_needing_firmware )
1.1       afresh1   190:
1.50      afresh1   191: if [ ! "${devices:-}" ]; then
                    192:        echo "No devices found which need firmware files to be downloaded."
                    193:        exit
                    194: fi
                    195:
                    196: TMPDIR=$( tmpdir "${DESTDIR}/tmp/fw_install" )
                    197: cd "$TMPDIR"
                    198:
                    199: # To unpriv we need to let the unpriv user into this dir
                    200: chmod go+x .
                    201:
                    202: fetch "$CFILE"
1.54      afresh1   203: ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
1.50      afresh1   204:     echo "Signature check of SHA256.sig failed" >&2 && exit 1
                    205:
                    206: for d in "${devices[@]}"; do
1.56      afresh1   207:        f=$( firmware_filename "$d" || true )
1.50      afresh1   208:        [ "$f" ] || continue
                    209:        set -A installed -- $( installed_firmware "$d" )
                    210:
                    211:        if [ "${installed:-}" ]; then
                    212:                for i in "${installed[@]:-}"; do
                    213:                        if [ "$f" = "$i.tgz" ]; then
                    214:                                echo "$i already installed"
1.11      afresh1   215:                                continue 2
1.1       afresh1   216:                        fi
                    217:                done
1.50      afresh1   218:        fi
1.1       afresh1   219:
1.50      afresh1   220:        fetch  "$f" || continue
                    221:        verify "$f" || continue
1.11      afresh1   222:
1.50      afresh1   223:        if [ "${installed:-}" ]; then
                    224:                for i in "${installed[@]}"; do
                    225:                        delete_firmware "$i"
                    226:                done
                    227:        fi
1.11      afresh1   228:
1.50      afresh1   229:        add_firmware "$f"
                    230: done
1.1       afresh1   231:

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