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:28:40 +0100
commitd6858c9f45d25cfec6defec17f62139593ae87f3 (patch)
treeac07edf4808e2d172481e13e861f148e8ccb6cb3 /meta/classes-recipe/kernel-fitimage.bbclass
parent930dc57fc8dcbf9ca8a3a431a8c4c02901f63182 (diff)
downloadpoky-d6858c9f45d25cfec6defec17f62139593ae87f3.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). (From OE-Core rev: 5e12dc911d0c541f43aa6d0c046fb87e8b7c1f7e) Signed-off-by: Sean Anderson <sean.anderson@seco.com> Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
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 @@
4# SPDX-License-Identifier: MIT 4# SPDX-License-Identifier: MIT
5# 5#
6 6
7inherit kernel-uboot kernel-artifact-names uboot-sign 7inherit kernel-uboot kernel-artifact-names uboot-config
8 8
9def get_fit_replacement_type(d): 9def get_fit_replacement_type(d):
10 kerneltypes = d.getVar('KERNEL_IMAGETYPES') or "" 10 kerneltypes = d.getVar('KERNEL_IMAGETYPES') or ""
@@ -50,15 +50,6 @@ python __anonymous () {
50 d.appendVarFlag('do_assemble_fitimage', 'depends', ' virtual/dtb:do_populate_sysroot') 50 d.appendVarFlag('do_assemble_fitimage', 'depends', ' virtual/dtb:do_populate_sysroot')
51 d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' virtual/dtb:do_populate_sysroot') 51 d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' virtual/dtb:do_populate_sysroot')
52 d.setVar('EXTERNAL_KERNEL_DEVICETREE', "${RECIPE_SYSROOT}/boot/devicetree") 52 d.setVar('EXTERNAL_KERNEL_DEVICETREE', "${RECIPE_SYSROOT}/boot/devicetree")
53
54 # Verified boot will sign the fitImage and append the public key to
55 # U-Boot dtb. We ensure the U-Boot dtb is deployed before assembling
56 # the fitImage:
57 if d.getVar('UBOOT_SIGN_ENABLE') == "1" and d.getVar('UBOOT_DTB_BINARY'):
58 uboot_pn = d.getVar('PREFERRED_PROVIDER_u-boot') or 'u-boot'
59 d.appendVarFlag('do_assemble_fitimage', 'depends', ' %s:do_populate_sysroot' % uboot_pn)
60 if d.getVar('INITRAMFS_IMAGE_BUNDLE') == "1":
61 d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' %s:do_populate_sysroot' % uboot_pn)
62} 53}
63 54
64 55
@@ -678,20 +669,12 @@ fitimage_assemble() {
678 ${KERNEL_OUTPUT_DIR}/$2 669 ${KERNEL_OUTPUT_DIR}/$2
679 670
680 # 671 #
681 # Step 8: Sign the image and add public key to U-Boot dtb 672 # Step 8: Sign the image
682 # 673 #
683 if [ "x${UBOOT_SIGN_ENABLE}" = "x1" ] ; then 674 if [ "x${UBOOT_SIGN_ENABLE}" = "x1" ] ; then
684 add_key_to_u_boot=""
685 if [ -n "${UBOOT_DTB_BINARY}" ]; then
686 # The u-boot.dtb is a symlink to UBOOT_DTB_IMAGE, so we need copy
687 # both of them, and don't dereference the symlink.
688 cp -P ${STAGING_DATADIR}/u-boot*.dtb ${B}
689 add_key_to_u_boot="-K ${B}/${UBOOT_DTB_BINARY}"
690 fi
691 ${UBOOT_MKIMAGE_SIGN} \ 675 ${UBOOT_MKIMAGE_SIGN} \
692 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \ 676 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
693 -F -k "${UBOOT_SIGN_KEYDIR}" \ 677 -F -k "${UBOOT_SIGN_KEYDIR}" \
694 $add_key_to_u_boot \
695 -r ${KERNEL_OUTPUT_DIR}/$2 \ 678 -r ${KERNEL_OUTPUT_DIR}/$2 \
696 ${UBOOT_MKIMAGE_SIGN_ARGS} 679 ${UBOOT_MKIMAGE_SIGN_ARGS}
697 fi 680 fi
@@ -700,18 +683,30 @@ fitimage_assemble() {
700do_assemble_fitimage() { 683do_assemble_fitimage() {
701 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage"; then 684 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage"; then
702 cd ${B} 685 cd ${B}
703 fitimage_assemble fit-image.its fitImage "" 686 fitimage_assemble fit-image.its fitImage-none ""
687 if [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ]; then
688 ln -sf fitImage-none ${B}/${KERNEL_OUTPUT_DIR}/fitImage
689 fi
704 fi 690 fi
705} 691}
706 692
707addtask assemble_fitimage before do_install after do_compile 693addtask assemble_fitimage before do_install after do_compile
708 694
695SYSROOT_DIRS:append = " /sysroot-only"
696do_install:append() {
697 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage" && \
698 [ "${UBOOT_SIGN_ENABLE}" = "1" ]; then
699 install -D ${B}/${KERNEL_OUTPUT_DIR}/fitImage-none ${D}/sysroot-only/fitImage
700 fi
701}
702
709do_assemble_fitimage_initramfs() { 703do_assemble_fitimage_initramfs() {
710 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage" && \ 704 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage" && \
711 test -n "${INITRAMFS_IMAGE}" ; then 705 test -n "${INITRAMFS_IMAGE}" ; then
712 cd ${B} 706 cd ${B}
713 if [ "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then 707 if [ "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then
714 fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage "" 708 fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage-bundle ""
709 ln -sf fitImage-bundle ${B}/${KERNEL_OUTPUT_DIR}/fitImage
715 else 710 else
716 fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage-${INITRAMFS_IMAGE} 1 711 fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage-${INITRAMFS_IMAGE} 1
717 fi 712 fi
@@ -802,35 +797,4 @@ kernel_do_deploy:append() {
802 fi 797 fi
803 fi 798 fi
804 fi 799 fi
805 if [ "${UBOOT_SIGN_ENABLE}" = "1" -o "${UBOOT_FITIMAGE_ENABLE}" = "1" ] && \
806 [ -n "${UBOOT_DTB_BINARY}" ] ; then
807 # UBOOT_DTB_IMAGE is a realfile, but we can't use
808 # ${UBOOT_DTB_IMAGE} since it contains ${PV} which is aimed
809 # for u-boot, but we are in kernel env now.
810 install -m 0644 ${B}/u-boot-${MACHINE}*.dtb "$deployDir/"
811 fi
812 if [ "${UBOOT_FITIMAGE_ENABLE}" = "1" -a -n "${UBOOT_BINARY}" -a -n "${SPL_DTB_BINARY}" ] ; then
813 # If we're also creating and/or signing the uboot fit, now we need to
814 # deploy it, it's its file, as well as u-boot-spl.dtb
815 install -m 0644 ${B}/u-boot-spl-${MACHINE}*.dtb "$deployDir/"
816 bbnote "Copying u-boot-fitImage file..."
817 install -m 0644 ${B}/u-boot-fitImage-* "$deployDir/"
818 bbnote "Copying u-boot-its file..."
819 install -m 0644 ${B}/u-boot-its-* "$deployDir/"
820 fi
821}
822
823# The function below performs the following in case of initramfs bundles:
824# - Removes do_assemble_fitimage. FIT generation is done through
825# do_assemble_fitimage_initramfs. do_assemble_fitimage is not needed
826# and should not be part of the tasks to be executed.
827# - Since do_kernel_generate_rsa_keys is inserted by default
828# between do_compile and do_assemble_fitimage, this is
829# not suitable in case of initramfs bundles. do_kernel_generate_rsa_keys
830# should be between do_bundle_initramfs and do_assemble_fitimage_initramfs.
831python () {
832 if d.getVar('INITRAMFS_IMAGE_BUNDLE') == "1":
833 bb.build.deltask('do_assemble_fitimage', d)
834 bb.build.deltask('kernel_generate_rsa_keys', d)
835 bb.build.addtask('kernel_generate_rsa_keys', 'do_assemble_fitimage_initramfs', 'do_bundle_initramfs', d)
836} 800}