Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | # This file is part of U-Boot verified boot support and is intended to be |
| 2 | # inherited from u-boot recipe and from kernel-fitimage.bbclass. |
| 3 | # |
| 4 | # The signature procedure requires the user to generate an RSA key and |
| 5 | # certificate in a directory and to define the following variable: |
| 6 | # |
| 7 | # UBOOT_SIGN_KEYDIR = "/keys/directory" |
| 8 | # UBOOT_SIGN_KEYNAME = "dev" # keys name in keydir (eg. "dev.crt", "dev.key") |
| 9 | # UBOOT_MKIMAGE_DTCOPTS = "-I dts -O dtb -p 2000" |
| 10 | # UBOOT_SIGN_ENABLE = "1" |
| 11 | # |
| 12 | # As verified boot depends on fitImage generation, following is also required: |
| 13 | # |
| 14 | # KERNEL_CLASSES ?= " kernel-fitimage " |
| 15 | # KERNEL_IMAGETYPE ?= "fitImage" |
| 16 | # |
| 17 | # The signature support is limited to the use of CONFIG_OF_SEPARATE in U-Boot. |
| 18 | # |
| 19 | # The tasks sequence is set as below, using DEPLOY_IMAGE_DIR as common place to |
| 20 | # treat the device tree blob: |
| 21 | # |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 22 | # * u-boot:do_install:append |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 23 | # Install UBOOT_DTB_BINARY to datadir, so that kernel can use it for |
| 24 | # signing, and kernel will deploy UBOOT_DTB_BINARY after signs it. |
| 25 | # |
| 26 | # * virtual/kernel:do_assemble_fitimage |
| 27 | # Sign the image |
| 28 | # |
| 29 | # * u-boot:do_deploy[postfuncs] |
| 30 | # Deploy files like UBOOT_DTB_IMAGE, UBOOT_DTB_SYMLINK and others. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 31 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 32 | # For more details on signature process, please refer to U-Boot documentation. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 33 | |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 34 | # We need some variables from u-boot-config |
| 35 | inherit uboot-config |
| 36 | |
| 37 | # Enable use of a U-Boot fitImage |
| 38 | UBOOT_FITIMAGE_ENABLE ?= "0" |
| 39 | |
| 40 | # Signature activation - these require their respective fitImages |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 41 | UBOOT_SIGN_ENABLE ?= "0" |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 42 | SPL_SIGN_ENABLE ?= "0" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 43 | |
| 44 | # Default value for deployment filenames. |
| 45 | UBOOT_DTB_IMAGE ?= "u-boot-${MACHINE}-${PV}-${PR}.dtb" |
| 46 | UBOOT_DTB_BINARY ?= "u-boot.dtb" |
| 47 | UBOOT_DTB_SYMLINK ?= "u-boot-${MACHINE}.dtb" |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 48 | UBOOT_NODTB_IMAGE ?= "u-boot-nodtb-${MACHINE}-${PV}-${PR}.bin" |
| 49 | UBOOT_NODTB_BINARY ?= "u-boot-nodtb.bin" |
| 50 | UBOOT_NODTB_SYMLINK ?= "u-boot-nodtb-${MACHINE}.bin" |
| 51 | UBOOT_ITS_IMAGE ?= "u-boot-its-${MACHINE}-${PV}-${PR}" |
| 52 | UBOOT_ITS ?= "u-boot.its" |
| 53 | UBOOT_ITS_SYMLINK ?= "u-boot-its-${MACHINE}" |
| 54 | UBOOT_FITIMAGE_IMAGE ?= "u-boot-fitImage-${MACHINE}-${PV}-${PR}" |
| 55 | UBOOT_FITIMAGE_BINARY ?= "u-boot-fitImage" |
| 56 | UBOOT_FITIMAGE_SYMLINK ?= "u-boot-fitImage-${MACHINE}" |
| 57 | SPL_DIR ?= "spl" |
| 58 | SPL_DTB_IMAGE ?= "u-boot-spl-${MACHINE}-${PV}-${PR}.dtb" |
| 59 | SPL_DTB_BINARY ?= "u-boot-spl.dtb" |
| 60 | SPL_DTB_SYMLINK ?= "u-boot-spl-${MACHINE}.dtb" |
| 61 | SPL_NODTB_IMAGE ?= "u-boot-spl-nodtb-${MACHINE}-${PV}-${PR}.bin" |
| 62 | SPL_NODTB_BINARY ?= "u-boot-spl-nodtb.bin" |
| 63 | SPL_NODTB_SYMLINK ?= "u-boot-spl-nodtb-${MACHINE}.bin" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 64 | |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 65 | # U-Boot fitImage description |
| 66 | UBOOT_FIT_DESC ?= "U-Boot fitImage for ${DISTRO_NAME}/${PV}/${MACHINE}" |
| 67 | |
| 68 | # Kernel / U-Boot fitImage Hash Algo |
| 69 | FIT_HASH_ALG ?= "sha256" |
| 70 | UBOOT_FIT_HASH_ALG ?= "sha256" |
| 71 | |
| 72 | # Kernel / U-Boot fitImage Signature Algo |
| 73 | FIT_SIGN_ALG ?= "rsa2048" |
| 74 | UBOOT_FIT_SIGN_ALG ?= "rsa2048" |
| 75 | |
| 76 | # Generate keys for signing Kernel / U-Boot fitImage |
| 77 | FIT_GENERATE_KEYS ?= "0" |
| 78 | UBOOT_FIT_GENERATE_KEYS ?= "0" |
| 79 | |
| 80 | # Size of private keys in number of bits |
| 81 | FIT_SIGN_NUMBITS ?= "2048" |
| 82 | UBOOT_FIT_SIGN_NUMBITS ?= "2048" |
| 83 | |
| 84 | # args to openssl genrsa (Default is just the public exponent) |
| 85 | FIT_KEY_GENRSA_ARGS ?= "-F4" |
| 86 | UBOOT_FIT_KEY_GENRSA_ARGS ?= "-F4" |
| 87 | |
| 88 | # args to openssl req (Default is -batch for non interactive mode and |
| 89 | # -new for new certificate) |
| 90 | FIT_KEY_REQ_ARGS ?= "-batch -new" |
| 91 | UBOOT_FIT_KEY_REQ_ARGS ?= "-batch -new" |
| 92 | |
| 93 | # Standard format for public key certificate |
| 94 | FIT_KEY_SIGN_PKCS ?= "-x509" |
| 95 | UBOOT_FIT_KEY_SIGN_PKCS ?= "-x509" |
| 96 | |
| 97 | # Functions on this bbclass can apply to either U-boot or Kernel, |
| 98 | # depending on the scenario |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 99 | UBOOT_PN = "${@d.getVar('PREFERRED_PROVIDER_u-boot') or 'u-boot'}" |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 100 | KERNEL_PN = "${@d.getVar('PREFERRED_PROVIDER_virtual/kernel')}" |
| 101 | |
| 102 | # We need u-boot-tools-native if we're creating a U-Boot fitImage |
| 103 | python() { |
| 104 | if d.getVar('UBOOT_FITIMAGE_ENABLE') == '1': |
| 105 | depends = d.getVar("DEPENDS") |
| 106 | depends = "%s u-boot-tools-native dtc-native" % depends |
| 107 | d.setVar("DEPENDS", depends) |
| 108 | } |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 109 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 110 | concat_dtb_helper() { |
| 111 | if [ -e "${UBOOT_DTB_BINARY}" ]; then |
| 112 | ln -sf ${UBOOT_DTB_IMAGE} ${DEPLOYDIR}/${UBOOT_DTB_BINARY} |
| 113 | ln -sf ${UBOOT_DTB_IMAGE} ${DEPLOYDIR}/${UBOOT_DTB_SYMLINK} |
| 114 | fi |
| 115 | |
| 116 | if [ -f "${UBOOT_NODTB_BINARY}" ]; then |
| 117 | install ${UBOOT_NODTB_BINARY} ${DEPLOYDIR}/${UBOOT_NODTB_IMAGE} |
| 118 | ln -sf ${UBOOT_NODTB_IMAGE} ${DEPLOYDIR}/${UBOOT_NODTB_SYMLINK} |
| 119 | ln -sf ${UBOOT_NODTB_IMAGE} ${DEPLOYDIR}/${UBOOT_NODTB_BINARY} |
| 120 | fi |
| 121 | |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 122 | # If we're not using a signed u-boot fit, concatenate SPL w/o DTB & U-Boot DTB |
| 123 | # with public key (otherwise it will be deployed by the equivalent |
| 124 | # concat_spl_dtb_helper function - cf. kernel-fitimage.bbclass for more details) |
| 125 | if [ "${SPL_SIGN_ENABLE}" != "1" ] ; then |
| 126 | deployed_uboot_dtb_binary='${DEPLOY_DIR_IMAGE}/${UBOOT_DTB_IMAGE}' |
| 127 | if [ "x${UBOOT_SUFFIX}" = "ximg" -o "x${UBOOT_SUFFIX}" = "xrom" ] && \ |
| 128 | [ -e "$deployed_uboot_dtb_binary" ]; then |
| 129 | oe_runmake EXT_DTB=$deployed_uboot_dtb_binary |
| 130 | install ${UBOOT_BINARY} ${DEPLOYDIR}/${UBOOT_IMAGE} |
| 131 | elif [ -e "${DEPLOYDIR}/${UBOOT_NODTB_IMAGE}" -a -e "$deployed_uboot_dtb_binary" ]; then |
| 132 | cd ${DEPLOYDIR} |
| 133 | cat ${UBOOT_NODTB_IMAGE} $deployed_uboot_dtb_binary | tee ${B}/${CONFIG_B_PATH}/${UBOOT_BINARY} > ${UBOOT_IMAGE} |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 134 | |
| 135 | if [ -n "${UBOOT_CONFIG}" ] |
| 136 | then |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 137 | i=0 |
| 138 | j=0 |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 139 | for config in ${UBOOT_MACHINE}; do |
| 140 | i=$(expr $i + 1); |
| 141 | for type in ${UBOOT_CONFIG}; do |
| 142 | j=$(expr $j + 1); |
| 143 | if [ $j -eq $i ] |
| 144 | then |
| 145 | cp ${UBOOT_IMAGE} ${B}/${CONFIG_B_PATH}/u-boot-$type.${UBOOT_SUFFIX} |
| 146 | fi |
| 147 | done |
| 148 | done |
| 149 | fi |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 150 | else |
| 151 | bbwarn "Failure while adding public key to u-boot binary. Verified boot won't be available." |
| 152 | fi |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 153 | fi |
| 154 | } |
| 155 | |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 156 | concat_spl_dtb_helper() { |
| 157 | |
| 158 | # We only deploy symlinks to the u-boot-spl.dtb,as the KERNEL_PN will |
| 159 | # be responsible for deploying the real file |
| 160 | if [ -e "${SPL_DIR}/${SPL_DTB_BINARY}" ] ; then |
| 161 | ln -sf ${SPL_DTB_IMAGE} ${DEPLOYDIR}/${SPL_DTB_SYMLINK} |
| 162 | ln -sf ${SPL_DTB_IMAGE} ${DEPLOYDIR}/${SPL_DTB_BINARY} |
| 163 | fi |
| 164 | |
| 165 | # Concatenate the SPL nodtb binary and u-boot.dtb |
| 166 | deployed_spl_dtb_binary='${DEPLOY_DIR_IMAGE}/${SPL_DTB_IMAGE}' |
| 167 | if [ -e "${DEPLOYDIR}/${SPL_NODTB_IMAGE}" -a -e "$deployed_spl_dtb_binary" ] ; then |
| 168 | cd ${DEPLOYDIR} |
| 169 | cat ${SPL_NODTB_IMAGE} $deployed_spl_dtb_binary | tee ${B}/${CONFIG_B_PATH}/${SPL_BINARY} > ${SPL_IMAGE} |
| 170 | else |
| 171 | bbwarn "Failure while adding public key to spl binary. Verified U-Boot boot won't be available." |
| 172 | fi |
| 173 | } |
| 174 | |
| 175 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 176 | concat_dtb() { |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 177 | if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${PN}" = "${UBOOT_PN}" -a -n "${UBOOT_DTB_BINARY}" ]; then |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 178 | mkdir -p ${DEPLOYDIR} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 179 | if [ -n "${UBOOT_CONFIG}" ]; then |
| 180 | for config in ${UBOOT_MACHINE}; do |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 181 | CONFIG_B_PATH="$config" |
| 182 | cd ${B}/$config |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 183 | concat_dtb_helper |
| 184 | done |
| 185 | else |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 186 | CONFIG_B_PATH="" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 187 | cd ${B} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 188 | concat_dtb_helper |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 189 | fi |
| 190 | fi |
| 191 | } |
| 192 | |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 193 | concat_spl_dtb() { |
| 194 | if [ "${SPL_SIGN_ENABLE}" = "1" -a "${PN}" = "${UBOOT_PN}" -a -n "${SPL_DTB_BINARY}" ]; then |
| 195 | mkdir -p ${DEPLOYDIR} |
| 196 | if [ -n "${UBOOT_CONFIG}" ]; then |
| 197 | for config in ${UBOOT_MACHINE}; do |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 198 | CONFIG_B_PATH="$config" |
| 199 | cd ${B}/$config |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 200 | concat_spl_dtb_helper |
| 201 | done |
| 202 | else |
| 203 | CONFIG_B_PATH="" |
| 204 | cd ${B} |
| 205 | concat_spl_dtb_helper |
| 206 | fi |
| 207 | fi |
| 208 | } |
| 209 | |
| 210 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 211 | # Install UBOOT_DTB_BINARY to datadir, so that kernel can use it for |
| 212 | # signing, and kernel will deploy UBOOT_DTB_BINARY after signs it. |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 213 | install_helper() { |
| 214 | if [ -f "${UBOOT_DTB_BINARY}" ]; then |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 215 | # UBOOT_DTB_BINARY is a symlink to UBOOT_DTB_IMAGE, so we |
| 216 | # need both of them. |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 217 | install -Dm 0644 ${UBOOT_DTB_BINARY} ${D}${datadir}/${UBOOT_DTB_IMAGE} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 218 | ln -sf ${UBOOT_DTB_IMAGE} ${D}${datadir}/${UBOOT_DTB_BINARY} |
| 219 | else |
| 220 | bbwarn "${UBOOT_DTB_BINARY} not found" |
| 221 | fi |
| 222 | } |
| 223 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 224 | # Install SPL dtb and u-boot nodtb to datadir, |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 225 | install_spl_helper() { |
| 226 | if [ -f "${SPL_DIR}/${SPL_DTB_BINARY}" ]; then |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 227 | install -Dm 0644 ${SPL_DIR}/${SPL_DTB_BINARY} ${D}${datadir}/${SPL_DTB_IMAGE} |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 228 | ln -sf ${SPL_DTB_IMAGE} ${D}${datadir}/${SPL_DTB_BINARY} |
| 229 | else |
| 230 | bbwarn "${SPL_DTB_BINARY} not found" |
| 231 | fi |
| 232 | if [ -f "${UBOOT_NODTB_BINARY}" ] ; then |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 233 | install -Dm 0644 ${UBOOT_NODTB_BINARY} ${D}${datadir}/${UBOOT_NODTB_IMAGE} |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 234 | ln -sf ${UBOOT_NODTB_IMAGE} ${D}${datadir}/${UBOOT_NODTB_BINARY} |
| 235 | else |
| 236 | bbwarn "${UBOOT_NODTB_BINARY} not found" |
| 237 | fi |
| 238 | |
| 239 | # We need to install a 'stub' u-boot-fitimage + its to datadir, |
| 240 | # so that the KERNEL_PN can use the correct filename when |
| 241 | # assembling and deploying them |
| 242 | touch ${D}/${datadir}/${UBOOT_FITIMAGE_IMAGE} |
| 243 | touch ${D}/${datadir}/${UBOOT_ITS_IMAGE} |
| 244 | } |
| 245 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 246 | do_install:append() { |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 247 | if [ "${PN}" = "${UBOOT_PN}" ]; then |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 248 | if [ -n "${UBOOT_CONFIG}" ]; then |
| 249 | for config in ${UBOOT_MACHINE}; do |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 250 | cd ${B}/$config |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 251 | if [ "${UBOOT_SIGN_ENABLE}" = "1" -o "${UBOOT_FITIMAGE_ENABLE}" = "1" ] && \ |
| 252 | [ -n "${UBOOT_DTB_BINARY}" ]; then |
| 253 | install_helper |
| 254 | fi |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 255 | if [ "${UBOOT_FITIMAGE_ENABLE}" = "1" -a -n "${SPL_DTB_BINARY}" ]; then |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 256 | install_spl_helper |
| 257 | fi |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 258 | done |
| 259 | else |
| 260 | cd ${B} |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 261 | if [ "${UBOOT_SIGN_ENABLE}" = "1" -o "${UBOOT_FITIMAGE_ENABLE}" = "1" ] && \ |
| 262 | [ -n "${UBOOT_DTB_BINARY}" ]; then |
| 263 | install_helper |
| 264 | fi |
| 265 | if [ "${UBOOT_FITIMAGE_ENABLE}" = "1" -a -n "${SPL_DTB_BINARY}" ]; then |
| 266 | install_spl_helper |
| 267 | fi |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 268 | fi |
| 269 | fi |
| 270 | } |
| 271 | |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 272 | do_uboot_generate_rsa_keys() { |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 273 | if [ "${SPL_SIGN_ENABLE}" = "0" ] && [ "${UBOOT_FIT_GENERATE_KEYS}" = "1" ]; then |
| 274 | bbwarn "UBOOT_FIT_GENERATE_KEYS is set to 1 eventhough SPL_SIGN_ENABLE is set to 0. The keys will not be generated as they won't be used." |
| 275 | fi |
| 276 | |
| 277 | if [ "${SPL_SIGN_ENABLE}" = "1" ] && [ "${UBOOT_FIT_GENERATE_KEYS}" = "1" ]; then |
| 278 | |
| 279 | # Generate keys only if they don't already exist |
| 280 | if [ ! -f "${SPL_SIGN_KEYDIR}/${SPL_SIGN_KEYNAME}".key ] || \ |
| 281 | [ ! -f "${SPL_SIGN_KEYDIR}/${SPL_SIGN_KEYNAME}".crt ]; then |
| 282 | |
| 283 | # make directory if it does not already exist |
| 284 | mkdir -p "${SPL_SIGN_KEYDIR}" |
| 285 | |
| 286 | echo "Generating RSA private key for signing U-Boot fitImage" |
| 287 | openssl genrsa ${UBOOT_FIT_KEY_GENRSA_ARGS} -out \ |
| 288 | "${SPL_SIGN_KEYDIR}/${SPL_SIGN_KEYNAME}".key \ |
| 289 | "${UBOOT_FIT_SIGN_NUMBITS}" |
| 290 | |
| 291 | echo "Generating certificate for signing U-Boot fitImage" |
| 292 | openssl req ${FIT_KEY_REQ_ARGS} "${UBOOT_FIT_KEY_SIGN_PKCS}" \ |
| 293 | -key "${SPL_SIGN_KEYDIR}/${SPL_SIGN_KEYNAME}".key \ |
| 294 | -out "${SPL_SIGN_KEYDIR}/${SPL_SIGN_KEYNAME}".crt |
| 295 | fi |
| 296 | fi |
| 297 | |
| 298 | } |
| 299 | |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 300 | addtask uboot_generate_rsa_keys before do_uboot_assemble_fitimage after do_compile |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 301 | |
| 302 | # Create a ITS file for the U-boot FIT, for use when |
| 303 | # we want to sign it so that the SPL can verify it |
| 304 | uboot_fitimage_assemble() { |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 305 | uboot_its="$1" |
| 306 | uboot_nodtb_bin="$2" |
| 307 | uboot_dtb="$3" |
| 308 | uboot_bin="$4" |
| 309 | spl_dtb="$5" |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 310 | uboot_csum="${UBOOT_FIT_HASH_ALG}" |
| 311 | uboot_sign_algo="${UBOOT_FIT_SIGN_ALG}" |
| 312 | uboot_sign_keyname="${SPL_SIGN_KEYNAME}" |
| 313 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 314 | rm -f $uboot_its $uboot_bin |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 315 | |
| 316 | # First we create the ITS script |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 317 | cat << EOF >> $uboot_its |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 318 | /dts-v1/; |
| 319 | |
| 320 | / { |
| 321 | description = "${UBOOT_FIT_DESC}"; |
| 322 | #address-cells = <1>; |
| 323 | |
| 324 | images { |
| 325 | uboot { |
| 326 | description = "U-Boot image"; |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 327 | data = /incbin/("$uboot_nodtb_bin"); |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 328 | type = "standalone"; |
| 329 | os = "u-boot"; |
| 330 | arch = "${UBOOT_ARCH}"; |
| 331 | compression = "none"; |
| 332 | load = <${UBOOT_LOADADDRESS}>; |
| 333 | entry = <${UBOOT_ENTRYPOINT}>; |
| 334 | EOF |
| 335 | |
| 336 | if [ "${SPL_SIGN_ENABLE}" = "1" ] ; then |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 337 | cat << EOF >> $uboot_its |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 338 | signature { |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 339 | algo = "$uboot_csum,$uboot_sign_algo"; |
| 340 | key-name-hint = "$uboot_sign_keyname"; |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 341 | }; |
| 342 | EOF |
| 343 | fi |
| 344 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 345 | cat << EOF >> $uboot_its |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 346 | }; |
| 347 | fdt { |
| 348 | description = "U-Boot FDT"; |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 349 | data = /incbin/("$uboot_dtb"); |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 350 | type = "flat_dt"; |
| 351 | arch = "${UBOOT_ARCH}"; |
| 352 | compression = "none"; |
| 353 | EOF |
| 354 | |
| 355 | if [ "${SPL_SIGN_ENABLE}" = "1" ] ; then |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 356 | cat << EOF >> $uboot_its |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 357 | signature { |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 358 | algo = "$uboot_csum,$uboot_sign_algo"; |
| 359 | key-name-hint = "$uboot_sign_keyname"; |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 360 | }; |
| 361 | EOF |
| 362 | fi |
| 363 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 364 | cat << EOF >> $uboot_its |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 365 | }; |
| 366 | }; |
| 367 | |
| 368 | configurations { |
| 369 | default = "conf"; |
| 370 | conf { |
| 371 | description = "Boot with signed U-Boot FIT"; |
| 372 | loadables = "uboot"; |
| 373 | fdt = "fdt"; |
| 374 | }; |
| 375 | }; |
| 376 | }; |
| 377 | EOF |
| 378 | |
| 379 | # |
| 380 | # Assemble the U-boot FIT image |
| 381 | # |
| 382 | ${UBOOT_MKIMAGE} \ |
| 383 | ${@'-D "${SPL_MKIMAGE_DTCOPTS}"' if len('${SPL_MKIMAGE_DTCOPTS}') else ''} \ |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 384 | -f $uboot_its \ |
| 385 | $uboot_bin |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 386 | |
| 387 | if [ "${SPL_SIGN_ENABLE}" = "1" ] ; then |
| 388 | # |
| 389 | # Sign the U-boot FIT image and add public key to SPL dtb |
| 390 | # |
| 391 | ${UBOOT_MKIMAGE_SIGN} \ |
| 392 | ${@'-D "${SPL_MKIMAGE_DTCOPTS}"' if len('${SPL_MKIMAGE_DTCOPTS}') else ''} \ |
| 393 | -F -k "${SPL_SIGN_KEYDIR}" \ |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 394 | -K "$spl_dtb" \ |
| 395 | -r $uboot_bin \ |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 396 | ${SPL_MKIMAGE_SIGN_ARGS} |
| 397 | fi |
| 398 | |
| 399 | } |
| 400 | |
| 401 | do_uboot_assemble_fitimage() { |
| 402 | # This function runs in KERNEL_PN context. The reason for that is that we need to |
| 403 | # support the scenario where UBOOT_SIGN_ENABLE is placing the Kernel fitImage's |
| 404 | # pubkey in the u-boot.dtb file, so that we can use it when building the U-Boot |
| 405 | # fitImage itself. |
| 406 | if [ "${UBOOT_FITIMAGE_ENABLE}" = "1" ] && \ |
| 407 | [ -n "${SPL_DTB_BINARY}" -a "${PN}" = "${KERNEL_PN}" ] ; then |
| 408 | if [ "${UBOOT_SIGN_ENABLE}" != "1" ]; then |
| 409 | # If we're not signing the Kernel fitImage, that means |
| 410 | # we need to copy the u-boot.dtb from staging ourselves |
| 411 | cp -P ${STAGING_DATADIR}/u-boot*.dtb ${B} |
| 412 | fi |
| 413 | # As we are in the kernel context, we need to copy u-boot-spl.dtb from staging first. |
| 414 | # Unfortunately, need to glob on top of ${SPL_DTB_BINARY} since _IMAGE and _SYMLINK |
| 415 | # will contain U-boot's PV |
| 416 | # Similarly, we need to get the filename for the 'stub' u-boot-fitimage + its in |
| 417 | # staging so that we can use it for creating the image with the correct filename |
| 418 | # in the KERNEL_PN context. |
| 419 | # As for the u-boot.dtb (with fitimage's pubkey), it should come from the dependent |
| 420 | # do_assemble_fitimage task |
| 421 | cp -P ${STAGING_DATADIR}/u-boot-spl*.dtb ${B} |
| 422 | cp -P ${STAGING_DATADIR}/u-boot-nodtb*.bin ${B} |
| 423 | rm -rf ${B}/u-boot-fitImage-* ${B}/u-boot-its-* |
| 424 | kernel_uboot_fitimage_name=`basename ${STAGING_DATADIR}/u-boot-fitImage-*` |
| 425 | kernel_uboot_its_name=`basename ${STAGING_DATADIR}/u-boot-its-*` |
| 426 | cd ${B} |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 427 | uboot_fitimage_assemble $kernel_uboot_its_name ${UBOOT_NODTB_BINARY} \ |
| 428 | ${UBOOT_DTB_BINARY} $kernel_uboot_fitimage_name \ |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 429 | ${SPL_DTB_BINARY} |
| 430 | fi |
| 431 | } |
| 432 | |
| 433 | addtask uboot_assemble_fitimage before do_deploy after do_compile |
| 434 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 435 | do_deploy:prepend:pn-${UBOOT_PN}() { |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 436 | if [ "${UBOOT_SIGN_ENABLE}" = "1" -a -n "${UBOOT_DTB_BINARY}" ] ; then |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 437 | concat_dtb |
| 438 | fi |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 439 | |
| 440 | if [ "${UBOOT_FITIMAGE_ENABLE}" = "1" ] ; then |
| 441 | # Deploy the u-boot-nodtb binary and symlinks... |
| 442 | if [ -f "${SPL_DIR}/${SPL_NODTB_BINARY}" ] ; then |
| 443 | echo "Copying u-boot-nodtb binary..." |
| 444 | install -m 0644 ${SPL_DIR}/${SPL_NODTB_BINARY} ${DEPLOYDIR}/${SPL_NODTB_IMAGE} |
| 445 | ln -sf ${SPL_NODTB_IMAGE} ${DEPLOYDIR}/${SPL_NODTB_SYMLINK} |
| 446 | ln -sf ${SPL_NODTB_IMAGE} ${DEPLOYDIR}/${SPL_NODTB_BINARY} |
| 447 | fi |
| 448 | |
| 449 | |
| 450 | # We only deploy the symlinks to the uboot-fitImage and uboot-its |
| 451 | # images, as the KERNEL_PN will take care of deploying the real file |
| 452 | ln -sf ${UBOOT_FITIMAGE_IMAGE} ${DEPLOYDIR}/${UBOOT_FITIMAGE_BINARY} |
| 453 | ln -sf ${UBOOT_FITIMAGE_IMAGE} ${DEPLOYDIR}/${UBOOT_FITIMAGE_SYMLINK} |
| 454 | ln -sf ${UBOOT_ITS_IMAGE} ${DEPLOYDIR}/${UBOOT_ITS} |
| 455 | ln -sf ${UBOOT_ITS_IMAGE} ${DEPLOYDIR}/${UBOOT_ITS_SYMLINK} |
| 456 | fi |
| 457 | |
| 458 | if [ "${SPL_SIGN_ENABLE}" = "1" -a -n "${SPL_DTB_BINARY}" ] ; then |
| 459 | concat_spl_dtb |
| 460 | fi |
| 461 | |
| 462 | |
| 463 | } |
| 464 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 465 | do_deploy:append:pn-${UBOOT_PN}() { |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 466 | # If we're creating a u-boot fitImage, point u-boot.bin |
| 467 | # symlink since it might get used by image recipes |
| 468 | if [ "${UBOOT_FITIMAGE_ENABLE}" = "1" ] ; then |
| 469 | ln -sf ${UBOOT_FITIMAGE_IMAGE} ${DEPLOYDIR}/${UBOOT_BINARY} |
| 470 | ln -sf ${UBOOT_FITIMAGE_IMAGE} ${DEPLOYDIR}/${UBOOT_SYMLINK} |
| 471 | fi |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 472 | } |
| 473 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 474 | python () { |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 475 | if ( (d.getVar('UBOOT_SIGN_ENABLE') == '1' |
| 476 | or d.getVar('UBOOT_FITIMAGE_ENABLE') == '1') |
| 477 | and d.getVar('PN') == d.getVar('UBOOT_PN') |
| 478 | and d.getVar('UBOOT_DTB_BINARY')): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 479 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 480 | # Make "bitbake u-boot -cdeploy" deploys the signed u-boot.dtb |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 481 | # and/or the U-Boot fitImage |
| 482 | d.appendVarFlag('do_deploy', 'depends', ' %s:do_deploy' % d.getVar('KERNEL_PN')) |
| 483 | |
| 484 | if d.getVar('UBOOT_FITIMAGE_ENABLE') == '1' and d.getVar('PN') == d.getVar('KERNEL_PN'): |
| 485 | # As the U-Boot fitImage is created by the KERNEL_PN, we need |
| 486 | # to make sure that the u-boot-spl.dtb and u-boot-spl-nodtb.bin |
| 487 | # files are in the staging dir for it's use |
| 488 | d.appendVarFlag('do_uboot_assemble_fitimage', 'depends', ' %s:do_populate_sysroot' % d.getVar('UBOOT_PN')) |
| 489 | |
| 490 | # If the Kernel fitImage is being signed, we need to |
| 491 | # create the U-Boot fitImage after it |
| 492 | if d.getVar('UBOOT_SIGN_ENABLE') == '1': |
| 493 | d.appendVarFlag('do_uboot_assemble_fitimage', 'depends', ' %s:do_assemble_fitimage' % d.getVar('KERNEL_PN')) |
| 494 | d.appendVarFlag('do_uboot_assemble_fitimage', 'depends', ' %s:do_assemble_fitimage_initramfs' % d.getVar('KERNEL_PN')) |
| 495 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 496 | } |