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

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

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