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

1.1       afresh1     1: #!/bin/ksh
                      2: set -e
                      3:
                      4: scan_dmesg() {
                      5:        # no bsort for now
                      6:        sed -n "$1" /var/run/dmesg.boot
                      7: }
                      8:
                      9: installed_firmware() {
                     10:        for fw in ${PKGDIR}/$1-firmware*; do
                     11:                [ -e "$fw" ] || continue
                     12:                echo ${fw##*/}
                     13:        done
                     14: }
                     15:
1.8     ! afresh1    16: # tmpdir, do_as, unpriv, and unpriv2 are from install.sub
        !            17:
        !            18: # Create a temporary directory based on the supplied directory name prefix.
        !            19: tmpdir() {
        !            20:        local _i=1 _dir
        !            21:
        !            22:        until _dir="${1?}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
        !            23:                ((++_i < 10000)) || return 1
        !            24:        done
        !            25:        echo "$_dir"
        !            26: }
1.6       afresh1    27:
                     28: # Run a command ($2+) as unprivileged user ($1).
                     29: # Take extra care that after "cmd" no "user" processes exist.
                     30: #
                     31: # Optionally:
                     32: #      - create "file" and chown it to "user"
                     33: #      - after "cmd", chown "file" back to root
                     34: #
                     35: # Usage: do_as user [-f file] cmd
                     36: do_as() {
                     37:        (( $# >= 2 )) || return
                     38:
                     39:        local _file _rc _user=$1
                     40:        shift
                     41:
                     42:        if [[ $1 == -f ]]; then
                     43:                _file=$2
                     44:                shift 2
                     45:        fi
                     46:
                     47:        if [[ -n $_file ]]; then
                     48:                >$_file
                     49:                chown "$_user" "$_file"
                     50:        fi
                     51:
                     52:        doas -u "$_user" "$@"
                     53:        _rc=$?
                     54:
                     55:        while doas -u "$_user" kill -9 -1 2>/dev/null; do
                     56:                echo "Processes still running for user $_user after: $@"
                     57:                sleep 1
                     58:        done
                     59:
                     60:        [[ -n $_file ]] && chown root "$_file"
                     61:
                     62:        return $_rc
                     63: }
                     64:
                     65: unpriv() {
                     66:        do_as _sndio "$@"
                     67: }
                     68:
                     69: unpriv2() {
                     70:        do_as _file "$@"
                     71: }
                     72:
1.8     ! afresh1    73: _issue=
        !            74: fail() {
        !            75:        echo $_issue >&2
        !            76:        exit 1
        !            77: }
        !            78:
1.7       afresh1    79: VNAME=$(sysctl -n kern.osrelease)
                     80: VERSION="${VNAME%.*}${VNAME#*.}"
                     81: FWDIR="$VNAME"
1.1       afresh1    82:
1.7       afresh1    83: HTTP_FWDIR=$FWDIR
                     84: set -- $(scan_dmesg "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p")
                     85: [[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots
                     86:
                     87: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
                     88: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.1       afresh1    89: PKGDIR=${DESTDIR}/var/db/pkg
                     90: PATTERNS="file:${0%/*}/firmware_patterns"
                     91:
                     92: drivers=$(
                     93:        last=''
                     94:        ftp -D "Detecting" -Vmo- $PATTERNS |
                     95:        while read d m; do
1.5       afresh1    96:                grep=grep
1.1       afresh1    97:                [ "$last" = "$d" ] && continue
                     98:                [ "$m" ] || m="^$d[0-9][0-9]* at "
1.5       afresh1    99:                [ "$m" = "${m#^}" ] && grep=fgrep
1.3       afresh1   100:                $grep -q "$m" /var/run/dmesg.boot || continue
1.1       afresh1   101:                echo $d
                    102:                last=$d
                    103:        done
                    104: )
                    105:
                    106: if [ -z "$drivers" ]; then
                    107:        echo "No devices found which need firmware files to be downloaded." >&2
                    108:        exit 0
                    109: fi
                    110:
1.8     ! afresh1   111: _src=$FWURL
        !           112: if _tmpsrc=$( tmpdir "${DESTDIR}/tmp/fw_update" ); then
        !           113:        (
        !           114:        >$_tmpsrc/t &&
        !           115:        $_unpriv cat $_tmpsrc/t
        !           116:        ) >/dev/null 2>&1 ||
        !           117:            rm -r $_tmpsrc
1.1       afresh1   118: fi
                    119:
1.8     ! afresh1   120: [[ ! -d $_tmpsrc ]] &&
        !           121:        _issue="Cannot create prefetch area" && fail
        !           122:
        !           123: cd "$_tmpsrc"
        !           124:
        !           125: _t=Get
        !           126: _cfile="$_tmpsrc/SHA256"
        !           127: _srclocal=false
        !           128:
        !           129: ! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" &&
        !           130:     _issue="Cannot fetch SHA256.sig" && fail
        !           131:
        !           132: # Verify signature file with public keys.
        !           133: ! unpriv -f "$_cfile" \
        !           134:     signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" &&
        !           135:     _issue="Signature check of SHA256.sig failed" && fail
1.1       afresh1   136:
                    137: for d in $drivers; do
1.8     ! afresh1   138:        _f=$( sed -n "s/.*(\($d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" )
        !           139:        installed=$( installed_firmware "$d" )
1.1       afresh1   140:
                    141:        for i in $installed; do
1.8     ! afresh1   142:                if [ "$_f" = "$i.tgz" ]; then
1.1       afresh1   143:                        echo "Firmware for $d already installed ($installed)"
                    144:                        continue 2
                    145:                fi
                    146:        done
                    147:
1.8     ! afresh1   148:        rm -f /tmp/h /tmp/fail
1.1       afresh1   149:
1.8     ! afresh1   150:        _t=Get/Verify
        !           151:        # Fetch firmware file and create a checksum by piping through
        !           152:        # sha256. Create a flag file in case ftp failed. Firmware
        !           153:        # from net is written to the prefetch area.
        !           154:        ( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) |
        !           155:        ( $_srclocal && unpriv2 sha256 -b >/tmp/h ||
        !           156:            unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" )
        !           157:
        !           158:        # Handle failed transfer.
        !           159:        if [[ -f /tmp/fail ]]; then
        !           160:                rm -f "$_tmpsrc/$_f"
        !           161:                _issue="Fetching of $_f failed!"
        !           162:                fail
        !           163:        fi
        !           164:
        !           165:        # Verify firmware by comparing its checksum with SHA256.
        !           166:        if fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then
        !           167:                #_unver=$(rmel $_f $_unver)
        !           168:                true
        !           169:        else
        !           170:                [[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc"
        !           171:                _issue="Checksum test for $_f failed."
        !           172:                fail
        !           173:        fi
1.1       afresh1   174:
                    175:        # TODO: Check hash for files before deleting
                    176:        if [ "$installed" ] && [ -e "${PKGDIR}/$installed/+CONTENTS" ]; then
                    177:                echo "Uninstalling $installed"
                    178:                cwd=${PKGDIR}/$installed
                    179:
                    180:                remove="${cwd}/+CONTENTS ${cwd}"
                    181:
                    182:                while read c g; do
                    183:                        case $c in
                    184:                        @cwd) cwd=$g
                    185:                          ;;
                    186:                        @*) continue
                    187:                          ;;
                    188:                        *)  remove="$cwd/$c $remove"
                    189:                          ;;
                    190:                        esac
                    191:                done < "${PKGDIR}/$installed/+CONTENTS"
                    192:
                    193:                for r in $remove ; do
                    194:                        if [ -d "$r" ]; then
                    195:                                # Try hard not to actually remove recursively
                    196:                                # without rmdir on the install media.
                    197:                                [ "$r/*" = $( echo "$r"/* ) ] && rm -rf "$r"
                    198:                        else
                    199:                                rm -f "$r"
                    200:                        fi
                    201:                done
                    202:        fi
                    203:
                    204:        # TODO: Add some details about the install to +CONTENTS like pkg_add
                    205:        # TODO: Or, maybe we save the firmware someplace and make pkg_add reinstall
1.8     ! afresh1   206:        echo "Installing $_f"
        !           207:        tar -zxphf "$_f" -C /etc "firmware/*"
1.1       afresh1   208:        mkdir -p ${PKGDIR}/${firmware%.tgz}/
1.8     ! afresh1   209:        tar -zxphf "$_f" -C "${PKGDIR}/${firmware%.tgz}" "+*"
1.4       afresh1   210:        ed -s "${PKGDIR}/${firmware%.tgz}/+CONTENTS" <<EOL
1.1       afresh1   211: /^@comment pkgpath/ -1a
                    212: @option manual-installation
                    213: @option firmware
                    214: @comment install-script
                    215: .
                    216: w
                    217: EOL
                    218: done
                    219:

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