version 1.8, 2021/10/16 19:31:13 |
version 1.37, 2021/12/01 02:04:18 |
|
|
#!/bin/ksh |
#!/bin/ksh |
|
# $OpenBSD$ |
set -e |
set -e |
|
|
scan_dmesg() { |
# Fake up some things from install.sub that we don't need to actually do |
# no bsort for now |
prefetcharea_fs_list() { |
sed -n "$1" /var/run/dmesg.boot |
echo "${DESTDIR}/tmp" |
} |
} |
|
|
installed_firmware() { |
|
for fw in ${PKGDIR}/$1-firmware*; do |
|
[ -e "$fw" ] || continue |
|
echo ${fw##*/} |
|
done |
|
} |
|
|
|
# tmpdir, 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. |
# Create a temporary directory based on the supplied directory name prefix. |
|
|
do_as _file "$@" |
do_as _file "$@" |
} |
} |
|
|
_issue= |
VNAME=${VNAME:-$(sysctl -n kern.osrelease)} |
fail() { |
VERSION=${VERSION:-"${VNAME%.*}${VNAME#*.}"} |
echo $_issue >&2 |
FWDIR=${FWDIR:-$VNAME} |
exit 1 |
MODE=${MODE:-install} |
} |
|
|
|
VNAME=$(sysctl -n kern.osrelease) |
# TODO: We need the firmware for the system we just installed |
VERSION="${VNAME%.*}${VNAME#*.}" |
# not the one we booted from. For example: |
FWDIR="$VNAME" |
# * booting from a snapshot bsd.rd that thinks it is the 7.0 release |
|
# will install the firmware from the 7.0 directory instead of |
|
# from the snapshots dir. |
|
# If they're using sysupgrade, then the installer kernel will be correct. |
|
# If we're doing this in the installer we can check what they picked |
|
# for downloading sets and use that value. |
|
# Otherwise, the fw_update after first boot will fix it up for us. |
|
|
HTTP_FWDIR=$FWDIR |
HTTP_FWDIR=$FWDIR |
set -- $(scan_dmesg "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p") |
set -- sed -n "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p" /var/run/dmesg.boot |
[[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots |
[[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots |
|
|
FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR} |
FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR} |
FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub |
FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub |
PKGDIR=${DESTDIR}/var/db/pkg |
FWPATTERNS="${DESTDIR}/usr/share/misc/firmware_patterns" |
PATTERNS="file:${0%/*}/firmware_patterns" |
|
|
|
drivers=$( |
# TODO: support srclocal installation of firmware somehow |
last='' |
fw_install() { |
ftp -D "Detecting" -Vmo- $PATTERNS | |
local _src=$1 _tmpfs_list _tmpfs _tmpsrc \ |
while read d m; do |
_t=Get _cfile="/tmp/SHA256" _pkgdir=${DESTDIR}/var/db/pkg \ |
grep=grep |
_f _r _remove _i _installed |
[ "$last" = "$d" ] && continue |
local _srclocal=false _unpriv=unpriv |
[ "$m" ] || m="^$d[0-9][0-9]* at " |
|
[ "$m" = "${m#^}" ] && grep=fgrep |
|
$grep -q "$m" /var/run/dmesg.boot || continue |
|
echo $d |
|
last=$d |
|
done |
|
) |
|
|
|
if [ -z "$drivers" ]; then |
echo "Let's $MODE firmware!" |
echo "No devices found which need firmware files to be downloaded." >&2 |
local _d _drivers=$( |
exit 0 |
last='' |
fi |
while read _d _m; do |
|
grep=grep |
|
[ "$last" = "$_d" ] && continue |
|
[ "$_m" ] || _m="^$_d[0-9][0-9]* at " |
|
[ "$_m" = "${_m#^}" ] && grep=fgrep |
|
$grep -q "$_m" /var/run/dmesg.boot || continue |
|
echo $_d |
|
last=$_d |
|
done < $FWPATTERNS |
|
) |
|
|
_src=$FWURL |
if [ -z "$_drivers" ]; then |
if _tmpsrc=$( tmpdir "${DESTDIR}/tmp/fw_update" ); then |
echo "No devices found which need firmware files to be downloaded." |
( |
return |
>$_tmpsrc/t && |
fi |
$_unpriv cat $_tmpsrc/t |
|
) >/dev/null 2>&1 || |
|
rm -r $_tmpsrc |
|
fi |
|
|
|
[[ ! -d $_tmpsrc ]] && |
! _tmpfs_list=$(prefetcharea_fs_list) && |
_issue="Cannot create prefetch area" && fail |
echo "Cannot determine prefetch area" >&2 && return |
|
|
cd "$_tmpsrc" |
for _tmpfs in $_tmpfs_list; do |
|
# Try to clean up from previous runs, assuming |
|
# the _tmpfs selection yields the same mount |
|
# point. |
|
for _tmpsrc in $_tmpfs/firmware.+([0-9]).+([0-9]); do |
|
[[ -d $_tmpsrc ]] && rm -r $_tmpsrc |
|
done |
|
|
_t=Get |
# Create a download directory for the firmware and |
_cfile="$_tmpsrc/SHA256" |
# check that the _sndio user can read files from |
_srclocal=false |
# it. Otherwise cleanup and skip the filesystem. |
|
if _tmpsrc=$(tmpdir "$_tmpfs/firmware"); then |
|
( |
|
>$_tmpsrc/t && |
|
$_unpriv cat $_tmpsrc/t |
|
) >/dev/null 2>&1 && break || |
|
rm -r $_tmpsrc |
|
fi |
|
done |
|
|
! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" && |
[[ ! -d $_tmpsrc ]] && |
_issue="Cannot fetch SHA256.sig" && fail |
echo "Cannot create prefetch area" >&2 && return 1 |
|
|
# Verify signature file with public keys. |
# Cleanup from previous runs. |
! unpriv -f "$_cfile" \ |
rm -f $_cfile $_cfile.sig |
signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" && |
|
_issue="Signature check of SHA256.sig failed" && fail |
|
|
|
for d in $drivers; do |
_t=Get/Verify |
_f=$( sed -n "s/.*(\($d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" ) |
|
installed=$( installed_firmware "$d" ) |
|
|
|
for i in $installed; do |
! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" && |
if [ "$_f" = "$i.tgz" ]; then |
echo "Cannot fetch SHA256.sig" >&2 && return 1 |
echo "Firmware for $d already installed ($installed)" |
|
continue 2 |
|
fi |
|
done |
|
|
|
rm -f /tmp/h /tmp/fail |
# Verify signature file with public keys. |
|
! $_unpriv -f "$_cfile" \ |
|
signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" && |
|
echo "Signature check of SHA256.sig failed" >&2 && return 1 |
|
|
_t=Get/Verify |
for _d in $_drivers; do |
# Fetch firmware file and create a checksum by piping through |
_f=$( sed -n "s/.*(\($_d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" ) |
# sha256. Create a flag file in case ftp failed. Firmware |
_installed=$( |
# from net is written to the prefetch area. |
for fw in "${_pkgdir}/$_d-firmware"*; do |
( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) | |
[ -e "$fw" ] || continue |
( $_srclocal && unpriv2 sha256 -b >/tmp/h || |
echo ${fw##*/} |
unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" ) |
done |
|
) |
|
|
# Handle failed transfer. |
for _i in $_installed; do |
if [[ -f /tmp/fail ]]; then |
if [ "$_f" = "$_i.tgz" ]; then |
rm -f "$_tmpsrc/$_f" |
echo "$_i already installed" |
_issue="Fetching of $_f failed!" |
continue 2 |
fail |
fi |
fi |
done |
|
|
# Verify firmware by comparing its checksum with SHA256. |
rm -f /tmp/h /tmp/fail |
if fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then |
|
#_unver=$(rmel $_f $_unver) |
|
true |
|
else |
|
[[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc" |
|
_issue="Checksum test for $_f failed." |
|
fail |
|
fi |
|
|
|
# TODO: Check hash for files before deleting |
# Fetch firmware file and create a checksum by piping through |
if [ "$installed" ] && [ -e "${PKGDIR}/$installed/+CONTENTS" ]; then |
# sha256. Create a flag file in case ftp failed. Firmware |
echo "Uninstalling $installed" |
# from net is written to the prefetch area. |
cwd=${PKGDIR}/$installed |
( $_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" |
|
echo "Fetching of $_f failed!" >&2 |
|
continue |
|
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 |
[[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc" |
;; |
echo "Checksum test for $_f failed." >&2 |
@*) continue |
continue |
;; |
fi |
*) remove="$cwd/$c $remove" |
|
;; |
|
esac |
|
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 $_f" |
while read c g; do |
tar -zxphf "$_f" -C /etc "firmware/*" |
case $c in |
mkdir -p ${PKGDIR}/${firmware%.tgz}/ |
@cwd) cwd="${DESTDIR}/$g" |
tar -zxphf "$_f" -C "${PKGDIR}/${firmware%.tgz}" "+*" |
;; |
ed -s "${PKGDIR}/${firmware%.tgz}/+CONTENTS" <<EOL |
@*) continue |
|
;; |
|
*) set -A _remove -- "$cwd/$c" "${_remove[@]}" |
|
;; |
|
esac |
|
done < "${_pkgdir}/$_installed/+CONTENTS" |
|
|
|
# We specifically rm -f here because not removing files/dirs |
|
# is probably not worth failing over. |
|
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: Should we mark these so real fw_update can -Drepair? |
|
ftp -D "Install" -Vmo- "file:$_tmpsrc/$_f" | |
|
tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \ |
|
-s ",^firmware,${DESTDIR}/etc/firmware," \ |
|
-C / -zxphf - "+*" "firmware/*" |
|
|
|
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_install "$FWURL" |