opnsense-deps/rc.d/20-kernel-update
Jeremy Karst ccebd54083 board package: build per series, harden kernel install for U-Boot
- Makefile: read the FreeBSD release from tools' config/<series>/build.conf
  and derive the compiler target and package ABI from it, version the
  package <release>_<commit count>, take libxml2 from our own packages
  set instead of pkg.FreeBSD.org, clone the NXP sources from the Forgejo
  mirrors, and drop the image target (release/build.sh replaces it)
- +MANIFEST: ABI from the series, new maintainer and www
- GATEWAY.conf: annotate mono-gateway as coming from the OPNsense
  repository; fail the image build if the package is missing
- mk-image.sh: remove the amd64 qemu binary setup_xtools leaves behind
- 20-kernel-update: drop the 26.1.3 guard; check room on the FAT
  partition, copy and verify the new kernel before replacing the old
  one, keep kernel.old, restore it on failure, retry at next shutdown
- 01-mono-modules: log modules that fail to load instead of hiding it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 18:56:54 -05:00

117 lines
3.4 KiB
Bash

#!/bin/sh
#
# rc.syshook "stop" script: install a freshly updated kernel for U-Boot.
#
# U-Boot loads kernel.img and the device tree straight from the FAT boot
# partition (/boot/msdos), not from /boot/kernel. After opnsense-update has
# installed a kernel set, this moves the new kernel.bin and device tree onto
# the FAT partition before the reboot.
#
# kernel.img must stay bootable throughout: nothing on the FAT partition is
# touched unless there is room for the new files, the new kernel is copied
# and verified before the old one is replaced, the old one is kept as
# kernel.old, and it is restored if a step fails. If the update cannot be
# done, /boot/kernel/kernel.bin is left in place so the next shutdown retries.
#
# GW_ROOT prefixes every path (tests only).
#
# Copyright 2026 Mono Technologies Inc.
# SPDX-License-Identifier: BSD-2-Clause
ROOT=${GW_ROOT:-}
BOOT=${ROOT}/boot/msdos
NEW_KERNEL=${ROOT}/boot/kernel/kernel.bin
NEW_DTB=${ROOT}/boot/dtb/freescale/mono-gateway-dk.dtb
MARGIN=1048576
log()
{
logger -t kernel-update -- "$*" 2>/dev/null
echo "kernel-update: $*" >&2
}
size()
{
if [ -f "${1}" ]; then wc -c < "${1}" | tr -d ' '; else echo 0; fi
}
free_bytes()
{
df -Pk "${BOOT}" | awk 'NR == 2 { print $4 * 1024 }'
}
# install_file NEW TARGET: replace TARGET with NEW, keeping TARGET.old.
# The old file is only renamed once the new copy is complete and verified.
# Sets REPLACED=1 if TARGET changed, 0 if it already was identical.
install_file()
{
_new=${1}
_target=${2}
_base=${_target%.*}
REPLACED=0
if [ -f "${_target}" ] && cmp -s "${_new}" "${_target}"; then
return 0
fi
# the previous backup makes room first; kernel.img itself is untouched
rm -f "${_base}.new" "${_base}.old"
if ! cp "${_new}" "${_base}.new" || ! sync || ! cmp -s "${_new}" "${_base}.new"; then
rm -f "${_base}.new"
log "copying ${_new} to ${_base}.new failed"
return 1
fi
if [ -f "${_target}" ]; then
if ! mv "${_target}" "${_base}.old"; then
rm -f "${_base}.new"
log "cannot rotate ${_target}"
return 1
fi
fi
if ! mv "${_base}.new" "${_target}"; then
mv "${_base}.old" "${_target}" 2>/dev/null
rm -f "${_base}.new"
log "cannot install ${_target}, restored the previous one"
return 1
fi
sync
REPLACED=1
return 0
}
[ -f "${NEW_KERNEL}" ] || exit 0
if [ ! -f "${BOOT}/kernel.img" ]; then
log "no ${BOOT}/kernel.img: FAT boot partition not mounted? new kernel not installed"
exit 1
fi
# Room needed for the new copies; stale .old/.new files are given back first.
DTB_TARGET=${BOOT}/dtb/mono-gateway-dk.dtb
NEED=$(( $(size "${NEW_KERNEL}") + $(size "${NEW_DTB}") + MARGIN ))
HAVE=$(( $(free_bytes) + $(size "${BOOT}/kernel.old") + $(size "${BOOT}/kernel.new") \
+ $(size "${BOOT}/dtb/mono-gateway-dk.old") + $(size "${BOOT}/dtb/mono-gateway-dk.new") ))
if [ "${HAVE}" -lt "${NEED}" ]; then
log "only ${HAVE} bytes free on ${BOOT}, ${NEED} needed: new kernel not installed"
exit 1
fi
if ! install_file "${NEW_KERNEL}" "${BOOT}/kernel.img"; then
exit 1
fi
KERNEL_REPLACED=${REPLACED}
if [ -f "${NEW_DTB}" ]; then
mkdir -p "${BOOT}/dtb"
if ! install_file "${NEW_DTB}" "${DTB_TARGET}"; then
# keep kernel and device tree from the same build
if [ "${KERNEL_REPLACED}" = 1 ]; then
mv "${BOOT}/kernel.old" "${BOOT}/kernel.img" && sync
fi
log "device tree update failed, kept the previous kernel"
exit 1
fi
fi
rm -f "${NEW_KERNEL}"
sync
log "installed new kernel and device tree on ${BOOT} (previous kept as kernel.old)"
exit 0