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

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.50      afresh1    51: fetch() {
                     52:        local _file=$1 _user=_file _exit
1.6       afresh1    53:
1.50      afresh1    54:        >"$_file"
                     55:        chown "$_user" "$_file"
1.6       afresh1    56:
1.50      afresh1    57:        # If we're not in the installer, we have su(1)
                     58:        # and doas(1) is unlikely to be configured.
                     59:        if [ -x /usr/bin/sh ]; then
                     60:                /usr/bin/su -s /bin/ksh "$_user" -c \
                     61:                    "/usr/bin/ftp -D 'Get/Verify' -Vm \
                     62:                        -o '$_file' '${FWURL}/${_file}'"
                     63:                _exit="$?"
1.42      afresh1    64:        else
1.50      afresh1    65:                /usr/bin/doas -u "$_user" \
                     66:                    ftp -D 'Get/Verify' -Vm \
                     67:                        -o "$_file" "${FWURL}/${_file}"
                     68:                _exit="$?"
1.42      afresh1    69:        fi
1.6       afresh1    70:
1.50      afresh1    71:        if [ "$_exit" -ne 0 ]; then
                     72:                rm -f "$_file"
                     73:                echo "Cannot fetch $_file" >&2
                     74:                return 1
                     75:        fi
1.6       afresh1    76:
1.50      afresh1    77:        chown root "$_file"
1.6       afresh1    78: }
                     79:
1.50      afresh1    80: verify() {
                     81:        # On the installer we don't get sha256 -C, so fake it.
                     82:        if ! fgrep -qx "SHA256 ($1) = $( /bin/sha256 -qb "$1" )" "$CFILE"; then
                     83:                echo "Checksum test for $1 failed." >&2
                     84:                return 1
                     85:        fi
1.6       afresh1    86: }
                     87:
1.50      afresh1    88: devices_needing_firmware() {
                     89:        local _d _m _grep _dmesgtail _last=''
1.1       afresh1    90:
1.50      afresh1    91:        # When we're not in the installer, the dmesg.boot can
                     92:        # contain multiple boots, so only look in the last one
                     93:        _dmesgtail=$( sed -n 'H;/^OpenBSD/h;${g;p;}' /var/run/dmesg.boot )
                     94:
                     95:        grep -v '^[[:space:]]*#' "$FWPATTERNS" |
                     96:            while read -r _d _m; do
                     97:                _grep="grep"
                     98:                [ "$_last" = "$_d" ] && continue
                     99:                [ "$_m" ] || _m="^${_d}[0-9][0-9]* at "
                    100:                [ "$_m" = "${_m#^}" ] && _grep="fgrep"
                    101:
                    102:                echo "$_dmesgtail" | $_grep -q "$_m" || continue
                    103:                echo "$_d"
                    104:                _last="$_d"
                    105:        done
                    106: }
1.14      afresh1   107:
1.50      afresh1   108: firmware_filename() {
                    109:        sed -n "s/.*(\($1-firmware-.*\.tgz\)).*/\1/p" "$CFILE" | sed '$!d'
                    110: }
1.7       afresh1   111:
1.50      afresh1   112: installed_firmware() {
                    113:        for fw in "${DESTDIR}/var/db/pkg/$1-firmware"*; do
                    114:                [ -e "$fw" ] || continue
                    115:                echo "${fw##*/}"
                    116:        done
                    117: }
1.1       afresh1   118:
1.50      afresh1   119: add_firmware () {
                    120:        local _f="$1" _pkgdir="${DESTDIR}/var/db/pkg"
                    121:        ftp -D "Install" -Vmo- "file:${1}" |
                    122:                tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
                    123:                -s ",^firmware,${DESTDIR}/etc/firmware," \
                    124:                -C / -zxphf - "+*" "firmware/*"
1.1       afresh1   125:
1.50      afresh1   126:        # TODO: Should we mark these so real fw_update can -Drepair?
                    127:        ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
                    128: /^@comment pkgpath/ -1a
                    129: @option manual-installation
                    130: @option firmware
                    131: @comment install-script
                    132: .
                    133: w
                    134: EOL
                    135: }
1.22      afresh1   136:
1.50      afresh1   137: delete_firmware() {
1.51      afresh1   138:        local _cwd _pkg="$1" _pkgdir="${DESTDIR}/var/db/pkg"
1.22      afresh1   139:
1.50      afresh1   140:        # TODO: Check hash for files before deleting
                    141:        echo "Uninstalling $_pkg"
1.51      afresh1   142:        _cwd="${_pkgdir}/$_pkg"
1.50      afresh1   143:
1.51      afresh1   144:        set -A _remove -- "${_cwd}/+CONTENTS" "${_cwd}"
1.50      afresh1   145:
                    146:        while read -r c g; do
                    147:                case $c in
1.51      afresh1   148:                @cwd) _cwd="${DESTDIR}$g"
1.50      afresh1   149:                  ;;
                    150:                @*) continue
                    151:                  ;;
1.52    ! afresh1   152:                *) set -A _remove -- "$_cwd/$c" "${_remove[@]}"
1.50      afresh1   153:                  ;;
                    154:                esac
                    155:        done < "${_pkgdir}/${_pkg}/+CONTENTS"
                    156:
                    157:        # We specifically rm -f here because not removing files/dirs
                    158:        # is probably not worth failing over.
                    159:        for _r in "${_remove[@]}" ; do
                    160:                if [ -d "$_r" ]; then
                    161:                        # Try hard not to actually remove recursively
                    162:                        # without rmdir on the install media.
                    163:                        [ "$_r/*" = "$( echo "$_r"/* )" ] && rm -rf "$_r"
                    164:                else
                    165:                        rm -f "$_r"
1.22      afresh1   166:                fi
                    167:        done
1.50      afresh1   168: }
1.1       afresh1   169:
1.50      afresh1   170: set -A devices -- $( devices_needing_firmware )
1.1       afresh1   171:
1.50      afresh1   172: if [ ! "${devices:-}" ]; then
                    173:        echo "No devices found which need firmware files to be downloaded."
                    174:        exit
                    175: fi
                    176:
                    177: TMPDIR=$( tmpdir "${DESTDIR}/tmp/fw_install" )
                    178: cd "$TMPDIR"
                    179:
                    180: # To unpriv we need to let the unpriv user into this dir
                    181: chmod go+x .
                    182:
                    183: fetch "$CFILE"
                    184: ! signify -Vep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
                    185:     echo "Signature check of SHA256.sig failed" >&2 && exit 1
                    186:
                    187: for d in "${devices[@]}"; do
                    188:        f=$( firmware_filename "$d" )
                    189:        [ "$f" ] || continue
                    190:        set -A installed -- $( installed_firmware "$d" )
                    191:
                    192:        if [ "${installed:-}" ]; then
                    193:                for i in "${installed[@]:-}"; do
                    194:                        if [ "$f" = "$i.tgz" ]; then
                    195:                                echo "$i already installed"
1.11      afresh1   196:                                continue 2
1.1       afresh1   197:                        fi
                    198:                done
1.50      afresh1   199:        fi
1.1       afresh1   200:
1.50      afresh1   201:        fetch  "$f" || continue
                    202:        verify "$f" || continue
1.11      afresh1   203:
1.50      afresh1   204:        if [ "${installed:-}" ]; then
                    205:                for i in "${installed[@]}"; do
                    206:                        delete_firmware "$i"
                    207:                done
                    208:        fi
1.11      afresh1   209:
1.50      afresh1   210:        add_firmware "$f"
                    211: done
1.1       afresh1   212:

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