version 1.6, 2021/10/14 02:39:43 |
version 1.11, 2021/10/17 03:07:48 |
|
|
done |
done |
} |
} |
|
|
# do_as, unpriv, and unpriv2 are from install.sub |
# tmpdir, do_as, unpriv, and unpriv2 are from install.sub |
|
|
|
# Create a temporary directory based on the supplied directory name prefix. |
|
tmpdir() { |
|
local _i=1 _dir |
|
|
|
until _dir="${1?}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do |
|
((++_i < 10000)) || return 1 |
|
done |
|
echo "$_dir" |
|
} |
|
|
# Run a command ($2+) as unprivileged user ($1). |
# Run a command ($2+) as unprivileged user ($1). |
# Take extra care that after "cmd" no "user" processes exist. |
# Take extra care that after "cmd" no "user" processes exist. |
# |
# |
|
|
do_as _file "$@" |
do_as _file "$@" |
} |
} |
|
|
set -A _KERNV -- $( scan_dmesg '/^OpenBSD \([1-9][0-9]*\.[0-9]\)\([^ ]*\) .*/ { s//\1 \2/p; q; }' ) |
_issue= |
VNAME=${_KERNV[0]} |
fail() { |
OSDIR=$VNAME |
echo $_issue >&2 |
if ((${#_KERNV[*]} > 1)) && [ "$_KERNV[1]" = "-current" -o "$_KERNV[1]" = "-beta" ]; then |
exit 1 |
OSDIR=snapshots |
} |
fi |
|
|
|
FWURL=http://firmware.openbsd.org/firmware/${OSDIR} |
VNAME=$(sysctl -n kern.osrelease) |
|
VERSION="${VNAME%.*}${VNAME#*.}" |
|
FWDIR="$VNAME" |
|
|
|
HTTP_FWDIR=$FWDIR |
|
set -- $(scan_dmesg "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p") |
|
[[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots |
|
|
|
FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR} |
|
FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub |
PKGDIR=${DESTDIR}/var/db/pkg |
PKGDIR=${DESTDIR}/var/db/pkg |
PATTERNS="file:${0%/*}/firmware_patterns" |
PATTERNS="file:${0%/*}/firmware_patterns" |
|
|
drivers=$( |
fw_update() { |
last='' |
local _tmpsrc _f _remove _r |
ftp -D "Detecting" -Vmo- $PATTERNS | |
local _src=$FWURL _t=Get _cfile="$_tmpsrc/SHA256" _srclocal=false |
while read d m; do |
local _drivers=$( |
grep=grep |
last='' |
[ "$last" = "$d" ] && continue |
ftp -D "Detecting" -Vmo- $PATTERNS | |
[ "$m" ] || m="^$d[0-9][0-9]* at " |
while read d m; do |
[ "$m" = "${m#^}" ] && grep=fgrep |
grep=grep |
$grep -q "$m" /var/run/dmesg.boot || continue |
[ "$last" = "$d" ] && continue |
echo $d |
[ "$m" ] || m="^$d[0-9][0-9]* at " |
last=$d |
[ "$m" = "${m#^}" ] && grep=fgrep |
done |
$grep -q "$m" /var/run/dmesg.boot || continue |
) |
echo $d |
|
last=$d |
|
done |
|
) |
|
|
if [ -z "$drivers" ]; then |
if [ -z "$_drivers" ]; then |
echo "No devices found which need firmware files to be downloaded." >&2 |
echo "No devices found which need firmware files to be downloaded." >&2 |
exit 0 |
return |
fi |
fi |
|
|
tmpdir=${DESTDIR}/tmp/fw_update |
if _tmpsrc=$( tmpdir "${DESTDIR}/tmp/fw_update" ); then |
[ -e "$tmpdir" ] && rm -rf "$tmpdir" |
( |
mkdir -p "$tmpdir" |
>$_tmpsrc/t && |
cd "$tmpdir" |
$_unpriv cat $_tmpsrc/t |
|
) >/dev/null 2>&1 || |
|
rm -r $_tmpsrc |
|
fi |
|
|
# TODO: Drop privs during fetch and verify |
[[ ! -d $_tmpsrc ]] && |
ftp -D Get -Vm "$FWURL/SHA256.sig" |
_issue="Cannot create prefetch area" && fail |
|
|
# Probably should bundle the firmware sigfile on the installer, |
cd "$_tmpsrc" |
# although we can just get it from the recently installed system. |
|
if [ "$DESTDIR" ]; then |
|
sigfile=$( sed -n '/^untrusted comment: verify with \(.*\)$/ { s//\1/p; q; }' SHA256.sig ) |
|
if [ ! -e "/etc/signify/$sigfile" ] \ |
|
&& [ -e "${DESTDIR}/etc/signify/$sigfile" ]; then |
|
cp -a "${DESTDIR}/etc/signify/$sigfile" "/etc/signify/$sigfile" |
|
fi |
|
fi |
|
|
|
signify -Ve -x SHA256.sig -m - < SHA256.sig > SHA256 |
! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" && |
|
_issue="Cannot fetch SHA256.sig" && fail |
|
|
for d in $drivers; do |
# Verify signature file with public keys. |
firmware=$( sed -n "s/.*(\($d-firmware-.*\.tgz\)).*/\1/p" SHA256 ) |
! unpriv -f "$_cfile" \ |
installed=$( installed_firmware $d ) |
signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" && |
|
_issue="Signature check of SHA256.sig failed" && fail |
|
|
for i in $installed; do |
for d in $_drivers; do |
if [ "$firmware" = "$i.tgz" ]; then |
_f=$( sed -n "s/.*(\($d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" ) |
echo "Firmware for $d already installed ($installed)" |
installed=$( installed_firmware "$d" ) |
continue 2 |
|
fi |
|
done |
|
|
|
mkdir $d |
for i in $installed; do |
|
if [ "$_f" = "$i.tgz" ]; then |
|
echo "Firmware for $d already installed ($installed)" |
|
continue 2 |
|
fi |
|
done |
|
|
# TODO: Drop privs during fetch and verify |
rm -f /tmp/h /tmp/fail |
ftp -D "Get/Verify" -Vmo- "$FWURL/$firmware" | sha256 -bph "$d/h" > "$firmware" |
|
fgrep -qx "SHA256 ($firmware) = $(<$d/h)" SHA256 |
|
|
|
# TODO: Check hash for files before deleting |
_t=Get/Verify |
if [ "$installed" ] && [ -e "${PKGDIR}/$installed/+CONTENTS" ]; then |
# Fetch firmware file and create a checksum by piping through |
echo "Uninstalling $installed" |
# sha256. Create a flag file in case ftp failed. Firmware |
cwd=${PKGDIR}/$installed |
# from net is written to the prefetch area. |
|
( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) | |
|
( $_srclocal && unpriv2 sha256 -b >/tmp/h || |
|
unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" ) |
|
|
remove="${cwd}/+CONTENTS ${cwd}" |
# Handle failed transfer. |
|
if [[ -f /tmp/fail ]]; then |
|
rm -f "$_tmpsrc/$_f" |
|
_issue="Fetching of $_f failed!" |
|
fail |
|
fi |
|
|
while read c g; do |
# Verify firmware by comparing its checksum with SHA256. |
case $c in |
if fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then |
@cwd) cwd=$g |
#_unver=$(rmel $_f $_unver) |
;; |
true |
@*) continue |
else |
;; |
[[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc" |
*) remove="$cwd/$c $remove" |
_issue="Checksum test for $_f failed." |
;; |
fail |
esac |
fi |
done < "${PKGDIR}/$installed/+CONTENTS" |
|
|
|
for r in $remove ; do |
# TODO: Check hash for files before deleting |
if [ -d "$r" ]; then |
if [ "$installed" ] && [ -e "${PKGDIR}/$installed/+CONTENTS" ]; then |
# Try hard not to actually remove recursively |
echo "Uninstalling $installed" |
# without rmdir on the install media. |
cwd=${PKGDIR}/$installed |
[ "$r/*" = $( echo "$r"/* ) ] && rm -rf "$r" |
|
else |
|
rm -f "$r" |
|
fi |
|
done |
|
fi |
|
|
|
# TODO: Add some details about the install to +CONTENTS like pkg_add |
set -A _remove -- "${cwd}/+CONTENTS" "${cwd}" |
# TODO: Or, maybe we save the firmware someplace and make pkg_add reinstall |
|
echo "Installing $firmware" |
while read c g; do |
tar -zxphf "$firmware" -C /etc "firmware/*" |
case $c in |
mkdir -p ${PKGDIR}/${firmware%.tgz}/ |
@cwd) cwd=$g |
tar -zxphf "$firmware" -C "${PKGDIR}/${firmware%.tgz}" "+*" |
;; |
ed -s "${PKGDIR}/${firmware%.tgz}/+CONTENTS" <<EOL |
@*) continue |
|
;; |
|
*) set -A _remove -- "$cwd/$c" "${_remove[@]}" |
|
;; |
|
esac |
|
done < "${PKGDIR}/$installed/+CONTENTS" |
|
|
|
for _r in "${_remove[@]}" ; do |
|
if [ -d "$_r" ]; then |
|
# Try hard not to actually remove recursively |
|
# without rmdir on the install media. |
|
[ "$_r/*" = $( echo "$_r"/* ) ] && rm -rf "$_r" |
|
else |
|
rm -f "$_r" |
|
fi |
|
done |
|
fi |
|
|
|
# TODO: Add some details about the install to +CONTENTS like pkg_add |
|
# TODO: Or, maybe we save the firmware someplace and make pkg_add reinstall |
|
echo "Installing $_f" |
|
tar -zxphf "$_f" -C /etc "firmware/*" |
|
mkdir -p ${PKGDIR}/${_f%.tgz}/ |
|
tar -zxphf "$_f" -C "${PKGDIR}/${_f%.tgz}" "+*" |
|
ed -s "${PKGDIR}/${_f%.tgz}/+CONTENTS" <<EOL |
/^@comment pkgpath/ -1a |
/^@comment pkgpath/ -1a |
@option manual-installation |
@option manual-installation |
@option firmware |
@option firmware |
|
|
. |
. |
w |
w |
EOL |
EOL |
done |
done |
|
} |
|
|
|
fw_update |