=================================================================== RCS file: /cvs/openbsd/fw_update/fw_install.sh,v retrieving revision 1.95 retrieving revision 1.121 diff -u -r1.95 -r1.121 --- openbsd/fw_update/fw_install.sh 2021/12/21 02:16:47 1.95 +++ openbsd/fw_update/fw_install.sh 2021/12/24 02:27:46 1.121 @@ -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.121 2021/12/24 02:27:46 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 -N '${0##/}' -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 -N "${0##/}" -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 -N "${0##/}" -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,59 +241,139 @@ } usage() { - echo "usage: ${0##*/} [-D | -L] [driver | file ...]" + echo "usage: ${0##*/} [-d | -D | -L] [-av] [-p path] [driver | file ...]" exit 2 } +ALL=false OPT_D= OPT_L= -while getopts DL name +while getopts :adDLp:v name do case "$name" in + a) ALL=true ;; + d) DELETE=true ;; D) OPT_D=true ;; L) OPT_L=true ;; - ?) usage 2 ;; + p) LOCALSRC="$OPTARG" ;; + v) VERBOSE=true ;; + :) + echo "${0##*/}: option requires an argument -- -$OPTARG" >&2 + usage 2 + ;; + ?) + echo "${0##*/}: unknown option -- -$OPTARG" >&2 + usage 2 + ;; esac done shift $((OPTIND - 1)) [ "$OPT_D" ] && [ "$OPT_L" ] && usage 1 +if [ "$LOCALSRC" ]; then + if [[ $LOCALSRC = @(ftp|http?(s))://* ]]; then + FWURL="${LOCALSRC}" + LOCALSRC= + else + # If we've not specifically asked to download to this path + # then we are installing from the local dir and no downloading. + [ "$OPT_D" ] || DOWNLOAD=false + LOCALSRC="${LOCALSRC:#file:}" + + ! [ -d "$LOCALSRC" ] && + echo "The path must be a URL or an existing directory" >&2 && + exit 2 + fi +fi + if [ "$OPT_D" ]; then # "Download only" means local dir and don't install INSTALL=false - LOCALSRC=. + LOCALSRC="${LOCALSRC:-.}" elif [ "$OPT_L" ]; then # "Local" means don't download, install from local dir DOWNLOAD=false - LOCALSRC=. -else - LOCALSRC="$( tmpdir "${DESTDIR}/tmp/fw_install" )" + LOCALSRC="${LOCALSRC:-.}" +elif [ ! "$LOCALSRC" ]; then + LOCALSRC="$( tmpdir "${DESTDIR}/tmp/${0##*/}" )" fi CFILE="$LOCALSRC/$CFILE" -if "$INSTALL" && [ -x /usr/bin/id ] && [ $(/usr/bin/id -u) != 0 ]; then +if [ -x /usr/bin/id ] && [ "$(/usr/bin/id -u)" != 0 ]; then echo "need root privileges" >&2 exit 1 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 if "$DOWNLOAD"; then + set +o noclobber # we want to get the latest CFILE fetch "$CFILE" + set -o noclobber ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" && echo "Signature check of SHA256.sig failed" >&2 && exit 1 +elif [ ! -e "$CFILE" ]; then + # TODO: We shouldn't need a CFILE if all arguments are files. + echo "${0##*/}: $CFILE: No such file or directory" >&2 + exit 2 fi +added='' +updated='' +kept='' for f in "${devices[@]}"; do d="$( firmware_devicename "$f" )" @@ -276,12 +386,13 @@ 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" + kept="$kept,$d" continue 2 fi done @@ -289,13 +400,15 @@ if [ -e "$f" ]; then if "$DOWNLOAD"; then - echo "Verify existing ${f##*/}" + "$VERBOSE" && echo "Verify existing ${f##*/}" verify "$f" || continue + "$INSTALL" || kept="$kept,$d" # else assume it was verified when downloaded fi elif "$DOWNLOAD"; then fetch "$f" || continue verify "$f" || continue + "$INSTALL" || added="$added,$d" elif "$INSTALL"; then echo "Cannot install ${f##*/}, not found" >&2 continue @@ -303,12 +416,28 @@ "$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:#,}" +kept="${kept:#,}" +if "$INSTALL"; then + echo "${0##*/}: added ${added:-none}; updated ${updated:-none}; kept ${kept:-none}" +else + echo "${0##*/}: downloaded ${added:-none}; kept ${kept:-none}" +fi