=================================================================== RCS file: /cvs/openbsd/fw_update/fw_install.sh,v retrieving revision 1.95 retrieving revision 1.114 diff -u -r1.95 -r1.114 --- openbsd/fw_update/fw_install.sh 2021/12/21 02:16:47 1.95 +++ openbsd/fw_update/fw_install.sh 2021/12/23 21:41:29 1.114 @@ -1,5 +1,5 @@ #!/bin/ksh -# $OpenBSD: fw_install.sh,v 1.95 2021/12/21 02:16:47 afresh1 Exp $ +# $OpenBSD: fw_install.sh,v 1.114 2021/12/23 21:41:29 afresh1 Exp $ # # Copyright (c) 2021 Andrew Hewus Fresh # @@ -34,6 +34,8 @@ FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR} FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub +VERBOSE=false +DELETE=false DOWNLOAD=true INSTALL=true LOCALSRC= @@ -61,17 +63,19 @@ # we have su(1) and doas(1) is unlikely to be configured. set -o monitor # make sure ftp gets its own process group ( + flags=-VM + "$VERBOSE" && flags=-vm if [ -x /usr/bin/su ]; then exec /usr/bin/su -s /bin/ksh "$_user" -c \ - "/usr/bin/ftp -D 'Get/Verify' -Vm -o- '$_src'" > "$_dst" + "/usr/bin/ftp -D 'Get/Verify' $flags -o- '$_src'" > "$_dst" else exec /usr/bin/doas -u "$_user" \ - /usr/bin/ftp -D 'Get/Verify' -Vm -o- "$_src" > "$_dst" + /usr/bin/ftp -D 'Get/Verify' $flags -o- "$_src" > "$_dst" fi ) & _pid=$! set +o monitor - trap "kill -TERM '-$_pid'; exit 1" EXIT INT QUIT ABRT TERM + trap "kill -TERM '-$_pid' 2>/dev/null; exit 1" EXIT INT QUIT ABRT TERM SECONDS=0 _last=0 @@ -148,24 +152,41 @@ } installed_firmware() { - set +o noglob - for fw in "${DESTDIR}/var/db/pkg/$1-firmware"*; do - [ -e "$fw" ] || continue + local _pre="$1" _match="$2" _post="$3" _firmware + set -A _firmware -- $( + set +o noglob + grep -Fxl '@option firmware' \ + "${DESTDIR}/var/db/pkg/"$_pre"$_match"$_post"/+CONTENTS" \ + 2>/dev/null || true + set -o noglob + ) + + [ "${_firmware[*]:-}" ] || return 0 + for fw in "${_firmware[@]}"; do + fw="${fw%/+CONTENTS}" echo "${fw##*/}" done - set -o noglob } add_firmware () { - local _f="${1##*/}" - local _pkgdir="${DESTDIR}/var/db/pkg/${_f%.tgz}" - ftp -D "Install" -Vmo- "file:${1}" | - tar -s ",^\+,${_pkgdir}/+," \ + local _f="${1##*/}" _pkgname + local _tmpdir="$( tmpdir "${DESTDIR}/var/db/pkg/.firmware" )" + local flags=-VM + "$VERBOSE" && flags=-vm + ftp -D "Install" "$flags" -o- "file:${1}" | + tar -s ",^\+,${_tmpdir}/+," \ -s ",^firmware,${DESTDIR}/etc/firmware," \ -C / -zxphf - "+*" "firmware/*" + _pkgname="$( sed -n '/^@name /{s///p;q;}' "${_tmpdir}/+CONTENTS" )" + if [ ! "$_pkgname" ]; then + echo "Failed to extract name from $1, partial install" 2>&1 + rm -rf "$_tmpdir" + return 1 + fi + # TODO: Should we mark these so real fw_update can -Drepair? - ed -s "${_pkgdir}/+CONTENTS" <&2 + return 2 + fi + set -A _remove -- "${_cwd}/+CONTENTS" "${_cwd}" while read -r c g; do @@ -211,18 +241,22 @@ } usage() { - echo "usage: ${0##*/} [-D | -L] [driver | file ...]" + echo "usage: ${0##*/} [-d | -D | -L] [-av] [driver | file ...]" exit 2 } +ALL=false OPT_D= OPT_L= -while getopts DL name +while getopts :adDLv name do case "$name" in + a) ALL=true ;; + d) DELETE=true ;; D) OPT_D=true ;; L) OPT_L=true ;; - ?) usage 2 ;; + v) VERBOSE=true ;; + ?) echo "${0##*/}: unknown option -- -$OPTARG"; usage 2 ;; esac done shift $((OPTIND - 1)) @@ -238,22 +272,67 @@ DOWNLOAD=false LOCALSRC=. else - LOCALSRC="$( tmpdir "${DESTDIR}/tmp/fw_install" )" + LOCALSRC="$( tmpdir "${DESTDIR}/tmp/${0##*/}" )" fi CFILE="$LOCALSRC/$CFILE" -if "$INSTALL" && [ -x /usr/bin/id ] && [ $(/usr/bin/id -u) != 0 ]; then - echo "need root privileges" >&2 - exit 1 +if "$INSTALL" || "$DELETE"; then + if [ -x /usr/bin/id ] && [ "$(/usr/bin/id -u)" != 0 ]; then + echo "need root privileges" >&2 + exit 1 + fi fi set -A devices -- "$@" -if [ ! "${devices[*]:-}" ]; then - echo -n "Detecting firmware ..." +if "$DELETE"; then + [ "$OPT_D" ] || [ "$OPT_L" ] && usage 22 + + set -A installed + if [ "${devices[*]:-}" ]; then + "$ALL" && usage 22 + + set -A installed -- $( + for d in "${devices[@]}"; do + f="${d##*/}" # only care about the name + f="${f%.tgz}" # allow specifying the package name + [ "$( firmware_devicename "$f" )" = "$f" ] && f="$f-firmware" + + set -A i -- $( installed_firmware '' "$f-" '*' ) + + if [ "${i[*]:-}" ]; then + echo "${i[@]}" + else + echo "No firmware found for '$d'" >&2 + fi + done + ) + elif "$ALL"; then + set -A installed -- $( installed_firmware '*' '-firmware-' '*' ) + fi + + deleted='' + if [ "${installed:-}" ]; then + for fw in "${installed[@]}"; do + delete_firmware "$fw" || continue + deleted="$deleted,$( firmware_devicename "$fw" )" + done + fi + + deleted="${deleted:+${deleted#,}}" + echo "${0:##*/}: deleted ${deleted:-none}"; + + exit +fi + +if [ "${devices[*]:-}" ]; then + "$ALL" && usage 22 +else + "$VERBOSE" && echo -n "Detecting firmware ..." set -A devices -- $( devices_needing_firmware ) - [ "${devices[*]:-}" ] && echo " found." || echo " done." + "$VERBOSE" && + { [ "${devices[*]:-}" ] && echo " found." || echo " done." ; } fi [ "${devices[*]:-}" ] || exit @@ -264,6 +343,8 @@ echo "Signature check of SHA256.sig failed" >&2 && exit 1 fi +added='' +updated='' for f in "${devices[@]}"; do d="$( firmware_devicename "$f" )" @@ -276,12 +357,12 @@ exit 2 fi - set -A installed -- $( installed_firmware "$d" ) + set -A installed -- $( installed_firmware '' "$d-firmware-" '*' ) if "$INSTALL" && [ "${installed[*]:-}" ]; then for i in "${installed[@]}"; do if [ "${f##*/}" = "$i.tgz" ]; then - echo "$i already installed" + "$VERBOSE" && echo "$i already installed" continue 2 fi done @@ -289,7 +370,7 @@ if [ -e "$f" ]; then if "$DOWNLOAD"; then - echo "Verify existing ${f##*/}" + "$VERBOSE" && echo "Verify existing ${f##*/}" verify "$f" || continue # else assume it was verified when downloaded fi @@ -303,12 +384,23 @@ "$INSTALL" || continue + removed=false if [ "${installed[*]:-}" ]; then for i in "${installed[@]}"; do delete_firmware "$i" + removed=true done fi add_firmware "$f" + + if "$removed"; then + updated="$updated,$d" + else + added="$added,$d" + fi done +added="${added:#,}" +updated="${updated:#,}" +echo "${0##*/}: added ${added:-none}; updated ${updated:-none}"