From ccebd540836943f0b8c95aa47325bdadddb77616 Mon Sep 17 00:00:00 2001 From: Jeremy Karst Date: Tue, 17 Nov 2026 18:56:54 -0500 Subject: [PATCH] board package: build per series, harden kernel install for U-Boot - Makefile: read the FreeBSD release from tools' config//build.conf and derive the compiler target and package ABI from it, version the package _, 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 --- .gitignore | 3 + Makefile | 159 +++++++++++------------------------------- config/GATEWAY.conf | 9 ++- mk-image.sh | 6 ++ pkg/+MANIFEST | 6 +- rc.d/01-mono-modules | 17 +++-- rc.d/20-kernel-update | 123 +++++++++++++++++++++++++++++--- 7 files changed, 186 insertions(+), 137 deletions(-) diff --git a/.gitignore b/.gitignore index dfbb6f9..712d0e7 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ opt_*.h machine x86 i386 + +# Python bytecode from the tests +__pycache__/ diff --git a/Makefile b/Makefile index 99f90f3..9c7c053 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,14 @@ -# Top-level build Makefile for opnsense-deps +# Top-level build Makefile for opnsense-deps: the Mono Gateway board package # -# Build everything: -# make -C /usr/deps all +# Build the mono-gateway package (kernel modules, userspace, configs): +# make -C /usr/deps package OPS_SETTINGS=26.1 OPS_VERSION=26.1.11 # -# Clean all build artifacts: -# make -C /usr/deps clean -# -# Build full image (kernel, modules, userspace, package, image): -# make -C /usr/deps image +# This needs the GATEWAY kernel build tree and a sysroot with the base set +# and libxml2 in it; release/build.sh prepares both and builds the whole +# OPNsense release around this package (see RELEASE.md). # # Build subsets: -# make -C /usr/deps modules # kernel modules only +# make -C /usr/deps modules # kernel modules only # make -C /usr/deps userspace # userspace only # # Parallel build (modules build concurrently): @@ -27,22 +25,29 @@ VENDORDIR?= ${OPSDIR}/_vendor # Source and tooling paths (standard OPNsense layout) SRCDIR?= /usr/src TOOLSDIR?= /usr/tools -COREDIR?= /usr/core SYSROOT?= /usr/obj${SRCDIR}/arm64.aarch64/tmp KERNBUILDDIR?= /usr/obj${SRCDIR}/arm64.aarch64/sys/GATEWAY -# OPNsense build settings +# OPNsense series and release this package is built for. The FreeBSD +# release comes from the tools configuration of that series, so the +# compiler target and the package ABI always match the base system. OPS_SETTINGS?= 26.1 -OPS_BRANCH= stable/${OPS_SETTINGS} -IMAGESDIR?= /usr/local/opnsense/build/${OPS_SETTINGS}/aarch64/images -SETSDIR?= /usr/local/opnsense/build/${OPS_SETTINGS}/aarch64/sets +OPS_VERSION?= # required for "package", e.g. 26.1.11 +.if !defined(OSREL) +OSREL!= sed -n 's/^OS?*=[[:space:]]*//p' ${TOOLSDIR}/config/${OPS_SETTINGS}/build.conf 2>/dev/null || true +.endif +.if empty(OSREL) +.error Cannot read OS from ${TOOLSDIR}/config/${OPS_SETTINGS}/build.conf; set OSREL= +.endif +PKG_ABI= FreeBSD:${OSREL:R}:aarch64 +CROSS_TARGET= --target=aarch64-unknown-freebsd${OSREL} # Common args for all kernel module builds # Explicit --target ensures cross-compilation works from amd64; harmless on native aarch64 KMOD_ARGS= SYSDIR=${SRCDIR}/sys \ KERNBUILDDIR=${KERNBUILDDIR} \ MACHINE=arm64 MACHINE_ARCH=aarch64 \ - CC="cc --target=aarch64-unknown-freebsd14.3" LD=ld.lld + CC="cc ${CROSS_TARGET}" LD=ld.lld # Local build directory for userspace components BUILDDIR?= ${OPSDIR}/_build @@ -50,17 +55,12 @@ BUILDDIR?= ${OPSDIR}/_build # Output directory for all final artifacts DISTDIR?= ${OPSDIR}/dist -# libxml2 aarch64 package for cross-compilation sysroot (fmc links against it). -# pkg.FreeBSD.org blocks directory listings, so we fetch by exact filename. -# Update this when the FreeBSD ports tree bumps libxml2. -LIBXML2_PKG?= libxml2-2.15.2.pkg -LIBXML2_URL= https://pkg.FreeBSD.org/FreeBSD:14:aarch64/latest/All/${LIBXML2_PKG} - -# Package version: derived from git tags (e.g., tag "26.1.2" + 3 commits = "26.1.2_3") -# Override: make package PKG_VERSION=26.1.2.1 -_PKG_TAG!= git -C ${OPSDIR} describe --abbrev=0 --always HEAD 2>/dev/null || echo 0.0.0 -_PKG_REV!= git -C ${OPSDIR} rev-list --count ${_PKG_TAG}..HEAD 2>/dev/null || echo 0 -PKG_VERSION?= ${_PKG_TAG}${_PKG_REV:N0:S/^/_/} +# Package version: the OPNsense release plus the commit count of this +# repository, e.g. "26.1.11_74". It grows with every commit and every +# release, so it always upgrades the previous package (including the +# vendor's "26.1.5_N" versions). Override: make package PKG_VERSION=... +_PKG_REV!= git -C ${OPSDIR} rev-list --count HEAD 2>/dev/null || echo 0 +PKG_VERSION?= ${OPS_VERSION}_${_PKG_REV} PKG_STAGEDIR= ${BUILDDIR}/pkg-stage PKG_CONFDIR= ${OPSDIR}/config PKG_RCDDIR= ${OPSDIR}/rc.d @@ -90,10 +90,11 @@ KMOD_TMP= ${DRIVERS}/tmp431 KMOD_MWIFI= ${DRIVERS}/mwifiex KMOD_DW= ${DRIVERS}/dpaa_wifi -# NXP vendor repositories (pinned commits for reproducible builds) -MWIFIEX_REPO= https://github.com/nxp-imx/mwifiex.git +# NXP vendor repositories (pinned commits for reproducible builds); Forgejo +# pull mirrors of github.com/nxp-imx/{mwifiex,imx-firmware} +MWIFIEX_REPO?= https://code.karsttech.com/claude/mwifiex.git MWIFIEX_COMMIT= 84ca65c9ff935d7f2999af100a82531c22c65234 -IMX_FW_REPO= https://github.com/nxp-imx/imx-firmware.git +IMX_FW_REPO?= https://code.karsttech.com/claude/imx-firmware.git IMX_FW_COMMIT= 8c9b278016c97527b285f2fcbe53c2d428eb171d build-cdx: @@ -173,33 +174,33 @@ modules: build-cdx build-fci build-auto_bridge build-pf_notify \ fmlib: @mkdir -p ${BUILDDIR}/fmlib cd ${BUILDDIR}/fmlib && ${MAKE} -f ${FASTPATH}/fmlib/Makefile.cross \ - SYSROOT=${SYSROOT} KSRCDIR=${SRCDIR} all + TARGET=${CROSS_TARGET} SYSROOT=${SYSROOT} KSRCDIR=${SRCDIR} all fmc: fmlib @mkdir -p ${BUILDDIR}/fmc cd ${BUILDDIR}/fmc && ${MAKE} -f ${FASTPATH}/fmc/Makefile.cross \ - SYSROOT=${SYSROOT} KSRCDIR=${SRCDIR} FMLIB_LIB=${BUILDDIR}/fmlib all + TARGET=${CROSS_TARGET} SYSROOT=${SYSROOT} KSRCDIR=${SRCDIR} FMLIB_LIB=${BUILDDIR}/fmlib all dpa_app: fmc @mkdir -p ${BUILDDIR}/dpa_app cd ${BUILDDIR}/dpa_app && ${MAKE} -f ${FASTPATH}/dpa_app/Makefile.cross \ - SYSROOT=${SYSROOT} KSRCDIR=${SRCDIR} \ + TARGET=${CROSS_TARGET} SYSROOT=${SYSROOT} KSRCDIR=${SRCDIR} \ FMLIB_LIB=${BUILDDIR}/fmlib FMC_LIB=${BUILDDIR}/fmc all cmm: @mkdir -p ${BUILDDIR}/cmm cd ${BUILDDIR}/cmm && ${MAKE} -f ${FASTPATH}/cmm/Makefile.cross \ - SYSROOT=${SYSROOT} KSRCDIR=${SRCDIR} all + TARGET=${CROSS_TARGET} SYSROOT=${SYSROOT} KSRCDIR=${SRCDIR} all cmmctl: @mkdir -p ${BUILDDIR}/cmmctl cd ${BUILDDIR}/cmmctl && ${MAKE} -f ${FASTPATH}/cmmctl/Makefile.cross \ - SYSROOT=${SYSROOT} all + TARGET=${CROSS_TARGET} SYSROOT=${SYSROOT} all fand: @mkdir -p ${BUILDDIR}/fand cd ${BUILDDIR}/fand && ${MAKE} -f ${DRIVERS}/fand/Makefile \ - SYSROOT=${SYSROOT} all + TARGET=${CROSS_TARGET} SYSROOT=${SYSROOT} all userspace: fmlib fmc dpa_app cmm cmmctl fand @mkdir -p ${DISTDIR} @@ -214,6 +215,7 @@ userspace: fmlib fmc dpa_app cmm cmmctl fand # ============================================================ package: all + @if [ -z "${OPS_VERSION}" ]; then echo "set OPS_VERSION (e.g. 26.1.11)" >&2; exit 1; fi @rm -f ${DISTDIR}/mono-gateway-*.pkg @rm -rf ${PKG_STAGEDIR} @mkdir -p ${PKG_STAGEDIR}/boot/modules @@ -261,93 +263,14 @@ package: all # CMM configd action (restart on interface reassignment) install -m 644 ${PKG_CONFDIR}/actions_cmm.conf ${PKG_STAGEDIR}/usr/local/opnsense/service/conf/actions.d/ # Generate manifest with version - sed 's/%%VERSION%%/${PKG_VERSION}/' ${PKG_METADIR}/+MANIFEST \ - > ${PKG_STAGEDIR}/+MANIFEST + sed -e 's/%%VERSION%%/${PKG_VERSION}/' -e 's/%%ABI%%/${PKG_ABI}/' \ + ${PKG_METADIR}/+MANIFEST > ${PKG_STAGEDIR}/+MANIFEST # Build package (ABI override for cross-compilation on amd64 host) - ABI=FreeBSD:14:aarch64 pkg create \ + ABI=${PKG_ABI} pkg create \ -M ${PKG_STAGEDIR}/+MANIFEST -p ${PKG_METADIR}/plist \ -r ${PKG_STAGEDIR} -o ${DISTDIR}/ @echo "==> Package: ${DISTDIR}/mono-gateway-${PKG_VERSION}.pkg" -# ============================================================ -# Full image build (kernel, modules, userspace, package, image) -# ============================================================ - -_NCPU!= sysctl -n hw.ncpu 2>/dev/null || echo 4 - -image: - @echo "NOTE: run as root (sudo make image)" - @rm -rf ${DISTDIR}/* - @touch ${DISTDIR}/.keep - @echo "==> Step 1: Checking repositories" - @test -d ${SRCDIR}/.git || git clone https://github.com/we-are-mono/opnsense-src.git -b ${OPS_BRANCH} ${SRCDIR} - @test -d ${TOOLSDIR}/.git || git clone https://github.com/opnsense/tools.git ${TOOLSDIR} - @cp -n ${OPSDIR}/config/GATEWAY.conf ${TOOLSDIR}/device/ 2>/dev/null || true - # --- Apply opnsense-core patches --- - local PATCHDIR="${OPSDIR}/patches" - if [ -d "${PATCHDIR}" ]; then - for p in "${PATCHDIR}"/*.patch; do - [ -f "${p}" ] || continue - echo ">>> Applying patch: $(basename ${p})" - patch -d "${COREDIR}" -p1 -V none < "${p}" - done - git -C "${COREDIR}" add -f src/etc/pkg/ - git -C "${COREDIR}" commit -a -m "Mono Gateway customizations" - fi - cd ${SRCDIR} && git checkout ${OPS_BRANCH} && git pull || echo " git pull skipped (NFS or no remote)" - @echo "==> Step 2: Building kernel (clean)" - rm -f ${SETSDIR}/kernel-*-GATEWAY.txz - chflags -R noschg /usr/obj${SRCDIR}/arm64.aarch64 2>/dev/null || true - rm -rf /usr/obj${SRCDIR}/arm64.aarch64 - ${MAKE} -C ${TOOLSDIR} kernel \ - DEVICE=GATEWAY SETTINGS=${OPS_SETTINGS} \ - TOOLSDIR=${TOOLSDIR} SRCDIR=${SRCDIR} - @echo "==> Step 2a: Populating sysroot from base set" - @_base=$$(ls ${SETSDIR}/base-*-aarch64-GATEWAY.txz 2>/dev/null | head -1); \ - if [ -z "$$_base" ]; then \ - echo "ERROR: no base set found in ${SETSDIR}"; exit 1; \ - fi; \ - echo " extracting headers and libraries from $$(basename $$_base)"; \ - tar xf "$$_base" -C ${SYSROOT} --no-fflags \ - --include='./usr/include/*' \ - --include='./usr/lib/*.a' \ - --include='./usr/lib/*.so' \ - --include='./usr/lib/*.o' \ - --include='./lib/*.so*' - @echo "==> Step 2b: Installing libxml2 into sysroot" - @if [ ! -f ${SYSROOT}/usr/local/lib/libxml2.so ]; then \ - rm -f /tmp/libxml2.pkg; \ - fetch -qo /tmp/libxml2.pkg '${LIBXML2_URL}'; \ - rm -rf /tmp/libxml2-extract; \ - mkdir -p /tmp/libxml2-extract; \ - tar xf /tmp/libxml2.pkg -C /tmp/libxml2-extract; \ - mkdir -p ${SYSROOT}/usr/local/include ${SYSROOT}/usr/local/lib; \ - cp -r /tmp/libxml2-extract/usr/local/include/libxml2 \ - ${SYSROOT}/usr/local/include/; \ - cp -a /tmp/libxml2-extract/usr/local/lib/libxml2* \ - ${SYSROOT}/usr/local/lib/; \ - rm -rf /tmp/libxml2-extract /tmp/libxml2.pkg; \ - echo " libxml2 installed into sysroot"; \ - else \ - echo " libxml2 already in sysroot"; \ - fi - @echo "==> Step 3: Building modules, userspace, and package" - ${MAKE} -C ${OPSDIR} -j${_NCPU} package - @echo "==> Step 5: Assembling ZFS image" - rm -f ${IMAGESDIR}/OPNsense-*-GATEWAY.img \ - ${IMAGESDIR}/OPNsense-*-GATEWAY.img.gz - cp ${OPSDIR}/mk-image.sh ${TOOLSDIR}/build/arm.sh - cp ${OPSDIR}/tools/mkgpt.py ${TOOLSDIR}/build/mkgpt.py - ${MAKE} -C ${TOOLSDIR} arm-5G \ - DEVICE=GATEWAY SETTINGS=${OPS_SETTINGS} \ - TOOLSDIR=${TOOLSDIR} SRCDIR=${SRCDIR} - @echo "==> Step 6: Compressing and copying to dist/" - @mkdir -p ${DISTDIR} - gzip -k ${IMAGESDIR}/OPNsense-*-GATEWAY.img - cp ${IMAGESDIR}/OPNsense-*-GATEWAY.img.gz ${DISTDIR}/ - @echo "==> Image ready:" - @ls -lh ${DISTDIR}/OPNsense-*-GATEWAY.img.gz - # ============================================================ # Clean # ============================================================ @@ -373,7 +296,7 @@ clean: .ORDER: clean modules .ORDER: clean userspace -.PHONY: all modules userspace package image clean fetch-vendor \ +.PHONY: all modules userspace package clean fetch-vendor \ build-cdx build-fci build-auto_bridge build-pf_notify \ build-emc2302 build-ina2xx build-lp5812 build-sfpled build-pcf2131 \ build-caam build-tmp431 build-mwifiex build-dpaa_wifi \ diff --git a/config/GATEWAY.conf b/config/GATEWAY.conf index a27cbc8..38ee16c 100644 --- a/config/GATEWAY.conf +++ b/config/GATEWAY.conf @@ -63,16 +63,19 @@ arm_hook() # rc.d scripts) are delivered via the mono-gateway FreeBSD package. # Install using the host's pkg via chroot, same mechanism as # setup_packages() in common.sh. /dev must be mounted for pkg -c. + # The package is also published in the OPNsense repository, so it is + # annotated as coming from there and later updates follow that repo. local MONO_GW_PKG=$(ls ${MONO_GW_DEPS}/mono-gateway-*.pkg 2>/dev/null | head -1) if [ -n "${MONO_GW_PKG}" ]; then cp "${MONO_GW_PKG}" "${STAGEDIR}/tmp/" mount -t devfs devfs "${STAGEDIR}/dev" pkg -c "${STAGEDIR}" add "/tmp/$(basename ${MONO_GW_PKG})" - pkg -c "${STAGEDIR}" annotate -qyA mono-gateway repository MonoGateway + pkg -c "${STAGEDIR}" annotate -qyA mono-gateway repository OPNsense umount "${STAGEDIR}/dev" rm "${STAGEDIR}/tmp/$(basename ${MONO_GW_PKG})" else - echo ">>> WARNING: mono-gateway package not found in ${MONO_GW_DEPS}" - echo ">>> Run: make -C opnsense-deps -j24 package" + echo ">>> ERROR: mono-gateway package not found in ${MONO_GW_DEPS}" >&2 + echo ">>> Run: make -C /usr/deps package OPS_VERSION=" >&2 + return 1 fi } diff --git a/mk-image.sh b/mk-image.sh index 0ca7923..9c8bc9d 100644 --- a/mk-image.sh +++ b/mk-image.sh @@ -90,6 +90,12 @@ setup_extras ${STAGEDIR} ${SELF} setup_entropy ${STAGEDIR} setup_xbase ${STAGEDIR} +# setup_xtools copies the host's qemu emulator to /usr/local/bin but +# setup_xbase only removes it from /usr/bin: drop the amd64 binary here. +# (release/build.sh also moves the xtools set aside before building the +# image so no other amd64 toolchain files are overlaid onto the image.) +rm -f ${STAGEDIR}/usr/local/bin/qemu-*-static + # ZFS fstab: only the FAT boot partition cat > ${STAGEDIR}/etc/fstab << EOF # Device Mountpoint FStype Options Dump Pass# diff --git a/pkg/+MANIFEST b/pkg/+MANIFEST index d17e8ea..4e67eb5 100644 --- a/pkg/+MANIFEST +++ b/pkg/+MANIFEST @@ -11,9 +11,9 @@ hardware drivers (emc2302, ina2xx, lp5812, sfpled), FMan configuration tools (fmc, dpa_app), connection management (cmm, cmmctl), and fan control (fand). EOD -maintainer: "tomaz@mono.si" -www: "https://mono.si" -arch: "FreeBSD:14:aarch64" +maintainer: "jeremy@karsttech.com" +www: "https://code.karsttech.com/claude/opnsense-deps" +arch: "%%ABI%%" prefix: "/" categories: ["sysutils"] licenses: ["BSD2CLAUSE"] diff --git a/rc.d/01-mono-modules b/rc.d/01-mono-modules index 10a1097..f11cdb1 100644 --- a/rc.d/01-mono-modules +++ b/rc.d/01-mono-modules @@ -1,15 +1,24 @@ #!/bin/sh # OPNsense rc.syshook early script — load Mono Gateway kernel modules -# Installed to /usr/local/etc/rc.syshook.d/early/02-mono-modules +# Installed to /usr/local/etc/rc.syshook.d/early/01-mono-modules +# +# A module that does not load is logged and skipped, never fatal: during a +# major upgrade the kernel is replaced one reboot before the packages, so the +# previous release's modules legitimately fail to load for one boot. + +load() +{ + kldload -n "${1}" 2>/dev/null || logger -t mono-modules "cannot load ${1}" +} for mod in cdx auto_bridge pf_notify fci lp5812 sfpled emc2302 ina2xx pcf2131 tmp431 cryptodev caam; do - kldload ${mod} 2>/dev/null + load ${mod} done # WiFi card is optional — only load driver if 88W9098 is present if pciconf -l 2>/dev/null | grep -q 'vendor=0x1b4b.*device=0x2b43'; then - kldload dpaa_wifi 2>/dev/null - kldload mwifiex 2>/dev/null + load dpaa_wifi + load mwifiex fi # Status LED: dim white diff --git a/rc.d/20-kernel-update b/rc.d/20-kernel-update index 247d6cd..3743873 100644 --- a/rc.d/20-kernel-update +++ b/rc.d/20-kernel-update @@ -1,12 +1,117 @@ #!/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 -# move kernel and copy Gateway dtb to FAT partition after kernel update -if [ -f /boot/kernel/kernel.bin ]; then - INSTALLED_KERNEL=$(cat /usr/local/opnsense/version/kernel) - if [ "${INSTALLED_KERNEL}" != "26.1.3" ]; then - cp /boot/msdos/kernel.img /boot/msdos/kernel.old - mv /boot/kernel/kernel.bin /boot/msdos/kernel.img - cp /boot/msdos/dtb/mono-gateway-dk.dtb /boot/msdos/dtb/mono-gateway-dk.old - cp /boot/dtb/freescale/mono-gateway-dk.dtb /boot/msdos/dtb/ - fi +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