summaryrefslogtreecommitdiffstats
path: root/meta/classes-recipe/kernel-fitimage.bbclass
diff options
context:
space:
mode:
authorSean Anderson <sean.anderson@seco.com>2022-10-21 19:37:26 -0400
committerRichard Purdie <richard.purdie@linuxfoundation.org>2022-10-26 12:26:17 +0100
commit5e12dc911d0c541f43aa6d0c046fb87e8b7c1f7e (patch)
treeb1961bf2a6308ad0a6263a05ab1a9a1910fef641 /meta/classes-recipe/kernel-fitimage.bbclass
parent60c1a170f1f8c11bc8f42026debf121433b39115 (diff)
downloadopenembedded-core-contrib-5e12dc911d0c541f43aa6d0c046fb87e8b7c1f7e.tar.gz
u-boot: Rework signing to remove interdependencies
The U-Boot signing code is a bit of a mess. The problem is that mkimage determines the public keys to embed into a device tree based on an image that it is signing. This results in all sorts of contortions: U-Boot has to be available to the kernel recipe so that it can have the correct public keys embedded. Then, the signed U-Boot has to be made available to U-Boot's do_deploy. This same dance is then repeated for SPL. To complicate matters, signing for U-Boot and U-Boot SPL is optional, so the whole process must be seamlessly integrated with a non-signed build. The complexity and interdependency of this process makes it difficult to extend. For example, it is not possible to install a signed U-Boot binary into the root filesystem. This is first because u-boot:do_install must run before linux:do_assemble_fitimage, which must run before u-boot:do_deploy. But aside from infrastructure issues, installing a signed U-Boot also can't happen, because the kernel image might have an embedded initramfs (containing the signed U-Boot). However, all of this complexity is accidental. It is not necessary to embed the public keys into U-Boot and sign the kernel in one fell swoop. Instead, we can sign the kernel, stage it, and sign the staged kernel again to embed the public keys into U-Boot [1]. This twice-signed kernel serves only to provide the correct parameters to mkimage, and does not have to be installed or deployed. By cutting the dependency of linux:do_assemble_fitimage on u-boot:do_install, we can drastically simplify the build process, making it much more extensible. The process of doing this conversion is a bit involved, since the U-Boot and Linux recipes are so intertwined at the moment. The most major change is that uboot-sign is no longer inherited by kernel-fitimage. Similarly, all U-Boot-related tasks have been removed from kernel-fitimage. We add a new step to the install task to stage the kernel in /sysroot-only. The logic to disable assemble_fitimage has been removed. We always assemble it, even if the final fitImage will use a bundled initramfs, because U-Boot will need it. On the U-Boot side, much of the churn stems from multiple config support. Previously, we took a fairly ad-hoc approach to UBOOT_CONFIG and UBOOT_MACHINE, introducing for loops wherever we needed to deal with them. However, I have chosen to use a much more structured approach. Each task which needs to use the build directory uses the following pseudocode: do_mytask() { if ${UBOOT_CONFIG}; then for config, type in zip(${UBOOT_CONFIG}, ${UBOOT_MACHINE}); do cd ${config} mytask_helper ${type} done else cd ${B} mytask_helper "" fi } By explicitly placing the work in mytask_helper, we make it easier to ensure that everything is covered, and we also allow bbappends files to more easily extend the task (as otherwise they would need to reimplement the loop themselves). [1] It doesn't particularly matter what we sign. Any FIT will do, but I chose the kernel's because we already went to the trouble of setting it up with the correct hashes and signatures. In the future, we could create a "dummy" image and sign that instead, but it would probably have to happen in the kernel recipe anyway (so we have access to the appropriate variables). Signed-off-by: Sean Anderson <sean.anderson@seco.com> Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Diffstat (limited to 'meta/classes-recipe/kernel-fitimage.bbclass')
-rw-r--r--meta/classes-recipe/kernel-fitimage.bbclass68
1 files changed, 16 insertions, 52 deletions
diff --git a/meta/classes-recipe/kernel-fitimage.bbclass b/meta/classes-recipe/kernel-fitimage.bbclass
index e4a130a0f2..befdf2568c 100644
--- a/meta/classes-recipe/kernel-fitimage.bbclass
+++ b/meta/classes-recipe/kernel-fitimage.bbclass
@@ -4,7 +4,7 @@
# SPDX-License-Identifier: MIT
#
-inherit kernel-uboot kernel-artifact-names uboot-sign
+inherit kernel-uboot kernel-artifact-names uboot-config
def get_fit_replacement_type(d):
kerneltypes = d.getVar('KERNEL_IMAGETYPES') or ""
@@ -50,15 +50,6 @@ python __anonymous () {
d.appendVarFlag('do_assemble_fitimage', 'depends', ' virtual/dtb:do_populate_sysroot')
d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' virtual/dtb:do_populate_sysroot')
d.setVar('EXTERNAL_KERNEL_DEVICETREE', "${RECIPE_SYSROOT}/boot/devicetree")
-
- # Verified boot will sign the fitImage and append the public key to
- # U-Boot dtb. We ensure the U-Boot dtb is deployed before assembling
- # the fitImage:
- if d.getVar('UBOOT_SIGN_ENABLE') == "1" and d.getVar('UBOOT_DTB_BINARY'):
- uboot_pn = d.getVar('PREFERRED_PROVIDER_u-boot') or 'u-boot'
- d.appendVarFlag('do_assemble_fitimage', 'depends', ' %s:do_populate_sysroot' % uboot_pn)
- if d.getVar('INITRAMFS_IMAGE_BUNDLE') == "1":
- d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' %s:do_populate_sysroot' % uboot_pn)
}
@@ -678,20 +669,12 @@ fitimage_assemble() {
${KERNEL_OUTPUT_DIR}/$2
#
- # Step 8: Sign the image and add public key to U-Boot dtb
+ # Step 8: Sign the image
#
if [ "x${UBOOT_SIGN_ENABLE}" = "x1" ] ; then
- add_key_to_u_boot=""
- if [ -n "${UBOOT_DTB_BINARY}" ]; then
- # The u-boot.dtb is a symlink to UBOOT_DTB_IMAGE, so we need copy
- # both of them, and don't dereference the symlink.
- cp -P ${STAGING_DATADIR}/u-boot*.dtb ${B}
- add_key_to_u_boot="-K ${B}/${UBOOT_DTB_BINARY}"
- fi
${UBOOT_MKIMAGE_SIGN} \
${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
-F -k "${UBOOT_SIGN_KEYDIR}" \
- $add_key_to_u_boot \
-r ${KERNEL_OUTPUT_DIR}/$2 \
${UBOOT_MKIMAGE_SIGN_ARGS}
fi
@@ -700,18 +683,30 @@ fitimage_assemble() {
do_assemble_fitimage() {
if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage"; then
cd ${B}
- fitimage_assemble fit-image.its fitImage ""
+ fitimage_assemble fit-image.its fitImage-none ""
+ if [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ]; then
+ ln -sf fitImage-none ${B}/${KERNEL_OUTPUT_DIR}/fitImage
+ fi
fi
}
addtask assemble_fitimage before do_install after do_compile
+SYSROOT_DIRS:append = " /sysroot-only"
+do_install:append() {
+ if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage" && \
+ [ "${UBOOT_SIGN_ENABLE}" = "1" ]; then
+ install -D ${B}/${KERNEL_OUTPUT_DIR}/fitImage-none ${D}/sysroot-only/fitImage
+ fi
+}
+
do_assemble_fitimage_initramfs() {
if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage" && \
test -n "${INITRAMFS_IMAGE}" ; then
cd ${B}
if [ "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then
- fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage ""
+ fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage-bundle ""
+ ln -sf fitImage-bundle ${B}/${KERNEL_OUTPUT_DIR}/fitImage
else
fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage-${INITRAMFS_IMAGE} 1
fi
@@ -802,35 +797,4 @@ kernel_do_deploy:append() {
fi
fi
fi
- if [ "${UBOOT_SIGN_ENABLE}" = "1" -o "${UBOOT_FITIMAGE_ENABLE}" = "1" ] && \
- [ -n "${UBOOT_DTB_BINARY}" ] ; then
- # UBOOT_DTB_IMAGE is a realfile, but we can't use
- # ${UBOOT_DTB_IMAGE} since it contains ${PV} which is aimed
- # for u-boot, but we are in kernel env now.
- install -m 0644 ${B}/u-boot-${MACHINE}*.dtb "$deployDir/"
- fi
- if [ "${UBOOT_FITIMAGE_ENABLE}" = "1" -a -n "${UBOOT_BINARY}" -a -n "${SPL_DTB_BINARY}" ] ; then
- # If we're also creating and/or signing the uboot fit, now we need to
- # deploy it, it's its file, as well as u-boot-spl.dtb
- install -m 0644 ${B}/u-boot-spl-${MACHINE}*.dtb "$deployDir/"
- bbnote "Copying u-boot-fitImage file..."
- install -m 0644 ${B}/u-boot-fitImage-* "$deployDir/"
- bbnote "Copying u-boot-its file..."
- install -m 0644 ${B}/u-boot-its-* "$deployDir/"
- fi
-}
-
-# The function below performs the following in case of initramfs bundles:
-# - Removes do_assemble_fitimage. FIT generation is done through
-# do_assemble_fitimage_initramfs. do_assemble_fitimage is not needed
-# and should not be part of the tasks to be executed.
-# - Since do_kernel_generate_rsa_keys is inserted by default
-# between do_compile and do_assemble_fitimage, this is
-# not suitable in case of initramfs bundles. do_kernel_generate_rsa_keys
-# should be between do_bundle_initramfs and do_assemble_fitimage_initramfs.
-python () {
- if d.getVar('INITRAMFS_IMAGE_BUNDLE') == "1":
- bb.build.deltask('do_assemble_fitimage', d)
- bb.build.deltask('kernel_generate_rsa_keys', d)
- bb.build.addtask('kernel_generate_rsa_keys', 'do_assemble_fitimage_initramfs', 'do_bundle_initramfs', d)
}