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

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

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