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

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.50      afresh1   125: installed_firmware() {
                    126:        for fw in "${DESTDIR}/var/db/pkg/$1-firmware"*; do
                    127:                [ -e "$fw" ] || continue
                    128:                echo "${fw##*/}"
                    129:        done
                    130: }
1.1       afresh1   131:
1.50      afresh1   132: add_firmware () {
                    133:        local _f="$1" _pkgdir="${DESTDIR}/var/db/pkg"
                    134:        ftp -D "Install" -Vmo- "file:${1}" |
                    135:                tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
                    136:                -s ",^firmware,${DESTDIR}/etc/firmware," \
                    137:                -C / -zxphf - "+*" "firmware/*"
1.1       afresh1   138:
1.50      afresh1   139:        # TODO: Should we mark these so real fw_update can -Drepair?
                    140:        ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
                    141: /^@comment pkgpath/ -1a
                    142: @option manual-installation
                    143: @option firmware
                    144: @comment install-script
                    145: .
                    146: w
                    147: EOL
                    148: }
1.22      afresh1   149:
1.50      afresh1   150: delete_firmware() {
1.51      afresh1   151:        local _cwd _pkg="$1" _pkgdir="${DESTDIR}/var/db/pkg"
1.22      afresh1   152:
1.50      afresh1   153:        # TODO: Check hash for files before deleting
                    154:        echo "Uninstalling $_pkg"
1.51      afresh1   155:        _cwd="${_pkgdir}/$_pkg"
1.50      afresh1   156:
1.51      afresh1   157:        set -A _remove -- "${_cwd}/+CONTENTS" "${_cwd}"
1.50      afresh1   158:
                    159:        while read -r c g; do
                    160:                case $c in
1.51      afresh1   161:                @cwd) _cwd="${DESTDIR}$g"
1.50      afresh1   162:                  ;;
                    163:                @*) continue
                    164:                  ;;
1.52      afresh1   165:                *) set -A _remove -- "$_cwd/$c" "${_remove[@]}"
1.50      afresh1   166:                  ;;
                    167:                esac
                    168:        done < "${_pkgdir}/${_pkg}/+CONTENTS"
                    169:
                    170:        # We specifically rm -f here because not removing files/dirs
                    171:        # is probably not worth failing over.
                    172:        for _r in "${_remove[@]}" ; do
                    173:                if [ -d "$_r" ]; then
                    174:                        # Try hard not to actually remove recursively
                    175:                        # without rmdir on the install media.
                    176:                        [ "$_r/*" = "$( echo "$_r"/* )" ] && rm -rf "$_r"
                    177:                else
                    178:                        rm -f "$_r"
1.22      afresh1   179:                fi
                    180:        done
1.50      afresh1   181: }
1.1       afresh1   182:
1.50      afresh1   183: set -A devices -- $( devices_needing_firmware )
1.1       afresh1   184:
1.50      afresh1   185: if [ ! "${devices:-}" ]; then
                    186:        echo "No devices found which need firmware files to be downloaded."
                    187:        exit
                    188: fi
                    189:
                    190: TMPDIR=$( tmpdir "${DESTDIR}/tmp/fw_install" )
                    191: cd "$TMPDIR"
                    192:
                    193: # To unpriv we need to let the unpriv user into this dir
                    194: chmod go+x .
                    195:
                    196: fetch "$CFILE"
1.54      afresh1   197: ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
1.50      afresh1   198:     echo "Signature check of SHA256.sig failed" >&2 && exit 1
                    199:
                    200: for d in "${devices[@]}"; do
1.56    ! afresh1   201:        f=$( firmware_filename "$d" || true )
1.50      afresh1   202:        [ "$f" ] || continue
                    203:        set -A installed -- $( installed_firmware "$d" )
                    204:
                    205:        if [ "${installed:-}" ]; then
                    206:                for i in "${installed[@]:-}"; do
                    207:                        if [ "$f" = "$i.tgz" ]; then
                    208:                                echo "$i already installed"
1.11      afresh1   209:                                continue 2
1.1       afresh1   210:                        fi
                    211:                done
1.50      afresh1   212:        fi
1.1       afresh1   213:
1.50      afresh1   214:        fetch  "$f" || continue
                    215:        verify "$f" || continue
1.11      afresh1   216:
1.50      afresh1   217:        if [ "${installed:-}" ]; then
                    218:                for i in "${installed[@]}"; do
                    219:                        delete_firmware "$i"
                    220:                done
                    221:        fi
1.11      afresh1   222:
1.50      afresh1   223:        add_firmware "$f"
                    224: done
1.1       afresh1   225:

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