git: 34632ed1a495 - main - arm: Introduce MK_KERNEL_BIN to control generation of kernel.bin

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 27 Oct 2023 03:18:59 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=34632ed1a49512f52968736abca66c0420bb576a

commit 34632ed1a49512f52968736abca66c0420bb576a
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2023-10-27 03:10:36 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-10-27 03:10:36 +0000

    arm: Introduce MK_KERNEL_BIN to control generation of kernel.bin
    
    It's sometimes desirable to generate kernel.bin and install it. While
    the mainstream has moved on to UEFI booting on arm, some specialized
    gear can't support it. For that gear, we unconditionally generate
    kernel.bin. Add a knob so that WITH_KERNEL_BIN or WITHOUT_KERNEL_BIN
    control its generation and installation. config files should add
    'makeoptions WITH_KERNEL_BIN=t' to enable it. Since its use is
    specialized, it is off by default now since the arm world has largely
    moved on to UEFI.
    
    It only affects arm and arm64 (since those are the only two that support
    it).
    
    Sponsored by:           Netflix
    Reviewed by:            mmel
    Differential Revision:  https://reviews.freebsd.org/D39013
---
 sys/conf/Makefile.arm               |  5 -----
 sys/conf/Makefile.arm64             |  6 ------
 sys/conf/kern.opts.mk               | 19 ++++++++++++-------
 sys/conf/kern.post.mk               |  9 +++++++++
 tools/build/options/WITH_KERNEL_BIN |  8 ++++++++
 5 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/sys/conf/Makefile.arm b/sys/conf/Makefile.arm
index af6ce7136636..d3047406e31c 100644
--- a/sys/conf/Makefile.arm
+++ b/sys/conf/Makefile.arm
@@ -69,9 +69,6 @@ SYSTEM_LD= \
 # Generate the .bin (no elf headers) kernel as an extra build output.
 # We must relink to generate the .bin kernel, because without headers the
 # location of everything changes.  We also strip the ARM marker symbols.
-KERNEL_EXTRA+= ${KERNEL_KO}.bin
-KERNEL_EXTRA_INSTALL+= ${KERNEL_KO}.bin
-
 ${KERNEL_KO}.bin: ${SYSTEM_DEP} vers.o
 	@echo "linking ${.TARGET}"
 	@${SYSTEM_LD_BASECMD} \
@@ -100,8 +97,6 @@ genassym.o: bus_if.h device_if.h
 
 %CLEAN
 
-CLEAN+=	${KERNEL_KO}.bin
-
 %RULES
 	
 .include "$S/conf/kern.post.mk"
diff --git a/sys/conf/Makefile.arm64 b/sys/conf/Makefile.arm64
index 77c36fa6ce6c..435326cadd9d 100644
--- a/sys/conf/Makefile.arm64
+++ b/sys/conf/Makefile.arm64
@@ -56,11 +56,6 @@ SYSTEM_LD= \
 	    --strip-symbol='$$[adtx]*' \
 	    ${.TARGET}
 
-# Generate the .bin (booti images) kernel as an extra build output.
-# The targets and rules to generate these appear near the end of the file.
-KERNEL_EXTRA+= ${KERNEL_KO}.bin
-KERNEL_EXTRA_INSTALL+= ${KERNEL_KO}.bin
-
 .if !empty(DDB_ENABLED) || !empty(DTRACE_ENABLED) || !empty(HWPMC_ENABLED)
 CFLAGS += -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
 .endif
@@ -76,7 +71,6 @@ CFLAGS += -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
 %FILES.m
 
 %CLEAN
-CLEAN+=	${KERNEL_KO}.bin
 
 %RULES
 
diff --git a/sys/conf/kern.opts.mk b/sys/conf/kern.opts.mk
index cc6f8a1d8755..d9d96a133250 100644
--- a/sys/conf/kern.opts.mk
+++ b/sys/conf/kern.opts.mk
@@ -56,18 +56,18 @@ __DEFAULT_YES_OPTIONS = \
 
 __DEFAULT_NO_OPTIONS = \
     BHYVE_SNAPSHOT \
+    KERNEL_BIN \
     KERNEL_RETPOLINE \
     RATELIMIT \
     REPRODUCIBLE_BUILD \
     VERIEXEC
 
-# Some options are totally broken on some architectures. We disable
-# them. If you need to enable them on an experimental basis, you
-# must change this code.
-# Note: These only apply to the list of modules we build by default
-# and sometimes what is in the opt_*.h files by default.
-# Kernel config files are unaffected, though some targets can be
-# affected by KERNEL_SYMBOLS, FORMAT_EXTENSIONS, CTF and SSP.
+# Some options are totally broken on some architectures. We disable them. If you
+# need to enable them on an experimental basis, you must change this code.
+# Note: These only apply to the list of modules we build by default and
+# sometimes what is in the opt_*.h files by default.  Kernel config files are
+# unaffected, though some targets can be affected by KERNEL_BIN, KERNEL_SYMBOLS,
+# FORMAT_EXTENSIONS, CTF and SSP.
 
 # Broken on 32-bit arm, kernel module compile errors
 .if ${MACHINE_CPUARCH} == "arm"
@@ -84,6 +84,11 @@ BROKEN_OPTIONS+= KERNEL_RETPOLINE
 BROKEN_OPTIONS+=EFI
 .endif
 
+# We only generate kernel.bin on arm and arm64, otherwise they break the build.
+.if ${MACHINE} != "arm" && ${MACHINE} != "arm64"
+BROKEN_OPTIONS+=KERNEL_BIN
+.endif
+
 .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
 __DEFAULT_NO_OPTIONS += FDT
 .else
diff --git a/sys/conf/kern.post.mk b/sys/conf/kern.post.mk
index bea29507a736..5258cea9441c 100644
--- a/sys/conf/kern.post.mk
+++ b/sys/conf/kern.post.mk
@@ -466,4 +466,13 @@ embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE} $S/dev/md/embedfs.S
 .endif
 .endif
 
+# Generate the .bin (booti images) kernel as an extra build output.
+# The targets and rules to generate these appear in Makefile.$MACHINE
+# if the platform supports it.
+.if ${MK_KERNEL_BIN} != "no"
+KERNEL_EXTRA+= ${KERNEL_KO}.bin
+KERNEL_EXTRA_INSTALL+= ${KERNEL_KO}.bin
+CLEAN+=	${KERNEL_KO}.bin
+.endif
+
 .include "kern.mk"
diff --git a/tools/build/options/WITH_KERNEL_BIN b/tools/build/options/WITH_KERNEL_BIN
new file mode 100644
index 000000000000..b5e008dbd6f0
--- /dev/null
+++ b/tools/build/options/WITH_KERNEL_BIN
@@ -0,0 +1,8 @@
+Generate and install kernel.bin from kernel as part of the normal build and
+install processes for the kernel. Available only on arm and arm64.
+
+Usually this will be added to the kernel config file with:
+
+makeoptions	WITH_KERNEL_BIN=1
+
+though it can also be used on the command line.