blob: 9fa302a5c816efd214e416bd7002772c155023ce [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001inherit kernel-uboot kernel-artifact-names uboot-sign
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
3python __anonymous () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -05004 kerneltypes = d.getVar('KERNEL_IMAGETYPES') or ""
He Zhefe76b1e2016-05-25 04:47:16 -04005 if 'fitImage' in kerneltypes.split():
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006 depends = d.getVar("DEPENDS")
Brad Bishop19323692019-04-05 15:28:33 -04007 depends = "%s u-boot-tools-native dtc-native" % depends
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008 d.setVar("DEPENDS", depends)
9
Brad Bishopd7bf8c12018-02-25 22:55:05 -050010 uarch = d.getVar("UBOOT_ARCH")
11 if uarch == "arm64":
12 replacementtype = "Image"
Brad Bishopc342db32019-05-15 21:57:59 -040013 elif uarch == "riscv":
14 replacementtype = "Image"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015 elif uarch == "mips":
Brad Bishop6e60e8b2018-02-01 10:27:11 -050016 replacementtype = "vmlinuz.bin"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050017 elif uarch == "x86":
Patrick Williamsc0f7c042017-02-23 20:41:17 -060018 replacementtype = "bzImage"
Brad Bishop316dfdd2018-06-25 12:45:53 -040019 elif uarch == "microblaze":
20 replacementtype = "linux.bin"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060021 else:
22 replacementtype = "zImage"
23
Brad Bishop19323692019-04-05 15:28:33 -040024 # Override KERNEL_IMAGETYPE_FOR_MAKE variable, which is internal
25 # to kernel.bbclass . We have to override it, since we pack zImage
26 # (at least for now) into the fitImage .
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027 typeformake = d.getVar("KERNEL_IMAGETYPE_FOR_MAKE") or ""
He Zhefe76b1e2016-05-25 04:47:16 -040028 if 'fitImage' in typeformake.split():
Patrick Williamsc0f7c042017-02-23 20:41:17 -060029 d.setVar('KERNEL_IMAGETYPE_FOR_MAKE', typeformake.replace('fitImage', replacementtype))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031 image = d.getVar('INITRAMFS_IMAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032 if image:
George McCollister185c8ae2016-05-26 08:55:16 -050033 d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' ${INITRAMFS_IMAGE}:do_image_complete')
34
Brad Bishop19323692019-04-05 15:28:33 -040035 #check if there are any dtb providers
36 providerdtb = d.getVar("PREFERRED_PROVIDER_virtual/dtb")
37 if providerdtb:
38 d.appendVarFlag('do_assemble_fitimage', 'depends', ' virtual/dtb:do_populate_sysroot')
39 d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' virtual/dtb:do_populate_sysroot')
40 d.setVar('EXTERNAL_KERNEL_DEVICETREE', "${RECIPE_SYSROOT}/boot/devicetree")
41
Patrick Williamsc0f7c042017-02-23 20:41:17 -060042 # Verified boot will sign the fitImage and append the public key to
Brad Bishop6e60e8b2018-02-01 10:27:11 -050043 # U-Boot dtb. We ensure the U-Boot dtb is deployed before assembling
Patrick Williamsc0f7c042017-02-23 20:41:17 -060044 # the fitImage:
Brad Bishop15ae2502019-06-18 21:44:24 -040045 if d.getVar('UBOOT_SIGN_ENABLE') == "1" and d.getVar('UBOOT_DTB_BINARY'):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 uboot_pn = d.getVar('PREFERRED_PROVIDER_u-boot') or 'u-boot'
Brad Bishop19323692019-04-05 15:28:33 -040047 d.appendVarFlag('do_assemble_fitimage', 'depends', ' %s:do_populate_sysroot' % uboot_pn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048}
49
Yannick Gicqueld5813b42016-04-27 16:20:55 +020050# Options for the device tree compiler passed to mkimage '-D' feature:
51UBOOT_MKIMAGE_DTCOPTS ??= ""
52
Brad Bishopf3fd2882019-06-21 08:06:37 -040053# fitImage Hash Algo
54FIT_HASH_ALG ?= "sha256"
55
Brad Bishop64c979e2019-11-04 13:55:29 -050056# fitImage Signature Algo
57FIT_SIGN_ALG ?= "rsa2048"
58
Andrew Geisslerf0343792020-11-18 10:42:21 -060059# Generate keys for signing fitImage
60FIT_GENERATE_KEYS ?= "0"
61
62# Size of private key in number of bits
63FIT_SIGN_NUMBITS ?= "2048"
64
65# args to openssl genrsa (Default is just the public exponent)
66FIT_KEY_GENRSA_ARGS ?= "-F4"
67
68# args to openssl req (Default is -batch for non interactive mode and
69# -new for new certificate)
70FIT_KEY_REQ_ARGS ?= "-batch -new"
71
72# Standard format for public key certificate
73FIT_KEY_SIGN_PKCS ?= "-x509"
74
Andrew Geisslerc723b722021-01-08 16:14:09 -060075# Description string
76FIT_DESC ?= "U-Boot fitImage for ${DISTRO_NAME}/${PV}/${MACHINE}"
77
78# Sign individual images as well
79FIT_SIGN_INDIVIDUAL ?= "0"
80
81# mkimage command
82UBOOT_MKIMAGE ?= "uboot-mkimage"
83UBOOT_MKIMAGE_SIGN ?= "${UBOOT_MKIMAGE}"
84
85# Arguments passed to mkimage for signing
86UBOOT_MKIMAGE_SIGN_ARGS ?= ""
87
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088#
89# Emit the fitImage ITS header
90#
George McCollister185c8ae2016-05-26 08:55:16 -050091# $1 ... .its filename
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092fitimage_emit_fit_header() {
George McCollister185c8ae2016-05-26 08:55:16 -050093 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094/dts-v1/;
95
96/ {
Andrew Geisslerc723b722021-01-08 16:14:09 -060097 description = "${FIT_DESC}";
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 #address-cells = <1>;
99EOF
100}
101
102#
103# Emit the fitImage section bits
104#
George McCollister185c8ae2016-05-26 08:55:16 -0500105# $1 ... .its filename
106# $2 ... Section bit type: imagestart - image section start
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107# confstart - configuration section start
108# sectend - section end
109# fitend - fitimage end
110#
111fitimage_emit_section_maint() {
George McCollister185c8ae2016-05-26 08:55:16 -0500112 case $2 in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 imagestart)
George McCollister185c8ae2016-05-26 08:55:16 -0500114 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115
116 images {
117EOF
118 ;;
119 confstart)
George McCollister185c8ae2016-05-26 08:55:16 -0500120 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121
122 configurations {
123EOF
124 ;;
125 sectend)
George McCollister185c8ae2016-05-26 08:55:16 -0500126 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127 };
128EOF
129 ;;
130 fitend)
George McCollister185c8ae2016-05-26 08:55:16 -0500131 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132};
133EOF
134 ;;
135 esac
136}
137
138#
139# Emit the fitImage ITS kernel section
140#
George McCollister185c8ae2016-05-26 08:55:16 -0500141# $1 ... .its filename
142# $2 ... Image counter
143# $3 ... Path to kernel image
144# $4 ... Compression type
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145fitimage_emit_section_kernel() {
146
Brad Bishopf3fd2882019-06-21 08:06:37 -0400147 kernel_csum="${FIT_HASH_ALG}"
Andrew Geisslerc723b722021-01-08 16:14:09 -0600148 kernel_sign_algo="${FIT_SIGN_ALG}"
149 kernel_sign_keyname="${UBOOT_SIGN_KEYNAME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150
Brad Bishop316dfdd2018-06-25 12:45:53 -0400151 ENTRYPOINT="${UBOOT_ENTRYPOINT}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500152 if [ -n "${UBOOT_ENTRYSYMBOL}" ]; then
153 ENTRYPOINT=`${HOST_PREFIX}nm vmlinux | \
154 awk '$3=="${UBOOT_ENTRYSYMBOL}" {print "0x"$1;exit}'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155 fi
156
George McCollister185c8ae2016-05-26 08:55:16 -0500157 cat << EOF >> ${1}
158 kernel@${2} {
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159 description = "Linux kernel";
George McCollister185c8ae2016-05-26 08:55:16 -0500160 data = /incbin/("${3}");
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161 type = "kernel";
162 arch = "${UBOOT_ARCH}";
163 os = "linux";
George McCollister185c8ae2016-05-26 08:55:16 -0500164 compression = "${4}";
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 load = <${UBOOT_LOADADDRESS}>;
166 entry = <${ENTRYPOINT}>;
167 hash@1 {
168 algo = "${kernel_csum}";
169 };
170 };
171EOF
Andrew Geisslerc723b722021-01-08 16:14:09 -0600172
173 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "${kernel_sign_keyname}" ] ; then
174 sed -i '$ d' ${1}
175 cat << EOF >> ${1}
176 signature@1 {
177 algo = "${kernel_csum},${kernel_sign_algo}";
178 key-name-hint = "${kernel_sign_keyname}";
179 };
180 };
181EOF
182 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183}
184
185#
186# Emit the fitImage ITS DTB section
187#
George McCollister185c8ae2016-05-26 08:55:16 -0500188# $1 ... .its filename
189# $2 ... Image counter
190# $3 ... Path to DTB image
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191fitimage_emit_section_dtb() {
192
Brad Bishopf3fd2882019-06-21 08:06:37 -0400193 dtb_csum="${FIT_HASH_ALG}"
Andrew Geisslerc723b722021-01-08 16:14:09 -0600194 dtb_sign_algo="${FIT_SIGN_ALG}"
195 dtb_sign_keyname="${UBOOT_SIGN_KEYNAME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800197 dtb_loadline=""
198 dtb_ext=${DTB##*.}
199 if [ "${dtb_ext}" = "dtbo" ]; then
200 if [ -n "${UBOOT_DTBO_LOADADDRESS}" ]; then
201 dtb_loadline="load = <${UBOOT_DTBO_LOADADDRESS}>;"
202 fi
203 elif [ -n "${UBOOT_DTB_LOADADDRESS}" ]; then
204 dtb_loadline="load = <${UBOOT_DTB_LOADADDRESS}>;"
205 fi
George McCollister185c8ae2016-05-26 08:55:16 -0500206 cat << EOF >> ${1}
207 fdt@${2} {
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 description = "Flattened Device Tree blob";
George McCollister185c8ae2016-05-26 08:55:16 -0500209 data = /incbin/("${3}");
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500210 type = "flat_dt";
211 arch = "${UBOOT_ARCH}";
212 compression = "none";
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800213 ${dtb_loadline}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500214 hash@1 {
215 algo = "${dtb_csum}";
216 };
217 };
218EOF
Andrew Geisslerc723b722021-01-08 16:14:09 -0600219
220 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "${dtb_sign_keyname}" ] ; then
221 sed -i '$ d' ${1}
222 cat << EOF >> ${1}
223 signature@1 {
224 algo = "${dtb_csum},${dtb_sign_algo}";
225 key-name-hint = "${dtb_sign_keyname}";
226 };
227 };
228EOF
229 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230}
231
232#
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600233# Emit the fitImage ITS setup section
234#
235# $1 ... .its filename
236# $2 ... Image counter
237# $3 ... Path to setup image
238fitimage_emit_section_setup() {
239
Brad Bishopf3fd2882019-06-21 08:06:37 -0400240 setup_csum="${FIT_HASH_ALG}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600241
242 cat << EOF >> ${1}
243 setup@${2} {
244 description = "Linux setup.bin";
245 data = /incbin/("${3}");
246 type = "x86_setup";
247 arch = "${UBOOT_ARCH}";
248 os = "linux";
249 compression = "none";
250 load = <0x00090000>;
251 entry = <0x00090000>;
252 hash@1 {
253 algo = "${setup_csum}";
254 };
255 };
256EOF
257}
258
259#
George McCollister185c8ae2016-05-26 08:55:16 -0500260# Emit the fitImage ITS ramdisk section
261#
262# $1 ... .its filename
263# $2 ... Image counter
264# $3 ... Path to ramdisk image
265fitimage_emit_section_ramdisk() {
266
Brad Bishopf3fd2882019-06-21 08:06:37 -0400267 ramdisk_csum="${FIT_HASH_ALG}"
Andrew Geisslerc723b722021-01-08 16:14:09 -0600268 ramdisk_sign_algo="${FIT_SIGN_ALG}"
269 ramdisk_sign_keyname="${UBOOT_SIGN_KEYNAME}"
Nathan Rossib4a4dc02016-10-21 22:07:27 +1000270 ramdisk_loadline=""
271 ramdisk_entryline=""
272
273 if [ -n "${UBOOT_RD_LOADADDRESS}" ]; then
274 ramdisk_loadline="load = <${UBOOT_RD_LOADADDRESS}>;"
275 fi
276 if [ -n "${UBOOT_RD_ENTRYPOINT}" ]; then
277 ramdisk_entryline="entry = <${UBOOT_RD_ENTRYPOINT}>;"
278 fi
George McCollister185c8ae2016-05-26 08:55:16 -0500279
280 cat << EOF >> ${1}
281 ramdisk@${2} {
Rick Altherrbc1b8802017-01-20 11:28:53 -0800282 description = "${INITRAMFS_IMAGE}";
George McCollister185c8ae2016-05-26 08:55:16 -0500283 data = /incbin/("${3}");
284 type = "ramdisk";
285 arch = "${UBOOT_ARCH}";
286 os = "linux";
Brad Bishop00e122a2019-10-05 11:10:57 -0400287 compression = "none";
Nathan Rossib4a4dc02016-10-21 22:07:27 +1000288 ${ramdisk_loadline}
289 ${ramdisk_entryline}
George McCollister185c8ae2016-05-26 08:55:16 -0500290 hash@1 {
291 algo = "${ramdisk_csum}";
292 };
293 };
294EOF
Andrew Geisslerc723b722021-01-08 16:14:09 -0600295
296 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "${ramdisk_sign_keyname}" ] ; then
297 sed -i '$ d' ${1}
298 cat << EOF >> ${1}
299 signature@1 {
300 algo = "${ramdisk_csum},${ramdisk_sign_algo}";
301 key-name-hint = "${ramdisk_sign_keyname}";
302 };
303 };
304EOF
305 fi
George McCollister185c8ae2016-05-26 08:55:16 -0500306}
307
308#
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500309# Emit the fitImage ITS configuration section
310#
George McCollister185c8ae2016-05-26 08:55:16 -0500311# $1 ... .its filename
312# $2 ... Linux kernel ID
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500313# $3 ... DTB image name
George McCollister185c8ae2016-05-26 08:55:16 -0500314# $4 ... ramdisk ID
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600315# $5 ... config ID
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500316# $6 ... default flag
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500317fitimage_emit_section_config() {
318
Brad Bishopf3fd2882019-06-21 08:06:37 -0400319 conf_csum="${FIT_HASH_ALG}"
Brad Bishop64c979e2019-11-04 13:55:29 -0500320 conf_sign_algo="${FIT_SIGN_ALG}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600321 if [ -n "${UBOOT_SIGN_ENABLE}" ] ; then
322 conf_sign_keyname="${UBOOT_SIGN_KEYNAME}"
323 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500324
Andrew Geisslerc723b722021-01-08 16:14:09 -0600325 its_file="${1}"
326 kernel_id="${2}"
327 dtb_image="${3}"
328 ramdisk_id="${4}"
329 config_id="${5}"
330 default_flag="${6}"
331
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500332 # Test if we have any DTBs at all
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800333 sep=""
334 conf_desc=""
Andrew Geissler635e0e42020-08-21 15:58:33 -0500335 conf_node="conf@"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800336 kernel_line=""
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600337 fdt_line=""
338 ramdisk_line=""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500339 setup_line=""
340 default_line=""
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600341
Andrew Geissler635e0e42020-08-21 15:58:33 -0500342 # conf node name is selected based on dtb ID if it is present,
343 # otherwise its selected based on kernel ID
Andrew Geisslerc723b722021-01-08 16:14:09 -0600344 if [ -n "${dtb_image}" ]; then
345 conf_node=$conf_node${dtb_image}
Andrew Geissler635e0e42020-08-21 15:58:33 -0500346 else
Andrew Geisslerc723b722021-01-08 16:14:09 -0600347 conf_node=$conf_node${kernel_id}
Andrew Geissler635e0e42020-08-21 15:58:33 -0500348 fi
349
Andrew Geisslerc723b722021-01-08 16:14:09 -0600350 if [ -n "${kernel_id}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800351 conf_desc="Linux kernel"
352 sep=", "
Andrew Geisslerc723b722021-01-08 16:14:09 -0600353 kernel_line="kernel = \"kernel@${kernel_id}\";"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800354 fi
355
Andrew Geisslerc723b722021-01-08 16:14:09 -0600356 if [ -n "${dtb_image}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800357 conf_desc="${conf_desc}${sep}FDT blob"
358 sep=", "
Andrew Geisslerc723b722021-01-08 16:14:09 -0600359 fdt_line="fdt = \"fdt@${dtb_image}\";"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600360 fi
361
Andrew Geisslerc723b722021-01-08 16:14:09 -0600362 if [ -n "${ramdisk_id}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800363 conf_desc="${conf_desc}${sep}ramdisk"
364 sep=", "
Andrew Geisslerc723b722021-01-08 16:14:09 -0600365 ramdisk_line="ramdisk = \"ramdisk@${ramdisk_id}\";"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500366 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600367
Andrew Geisslerc723b722021-01-08 16:14:09 -0600368 if [ -n "${config_id}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800369 conf_desc="${conf_desc}${sep}setup"
Andrew Geisslerc723b722021-01-08 16:14:09 -0600370 setup_line="setup = \"setup@${config_id}\";"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600371 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500372
Andrew Geisslerc723b722021-01-08 16:14:09 -0600373 if [ "${default_flag}" = "1" ]; then
Andrew Geissler635e0e42020-08-21 15:58:33 -0500374 # default node is selected based on dtb ID if it is present,
375 # otherwise its selected based on kernel ID
Andrew Geisslerc723b722021-01-08 16:14:09 -0600376 if [ -n "${dtb_image}" ]; then
377 default_line="default = \"conf@${dtb_image}\";"
Andrew Geissler635e0e42020-08-21 15:58:33 -0500378 else
Andrew Geisslerc723b722021-01-08 16:14:09 -0600379 default_line="default = \"conf@${kernel_id}\";"
Andrew Geissler635e0e42020-08-21 15:58:33 -0500380 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500381 fi
382
Andrew Geisslerc723b722021-01-08 16:14:09 -0600383 cat << EOF >> ${its_file}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500384 ${default_line}
Andrew Geissler635e0e42020-08-21 15:58:33 -0500385 $conf_node {
Andrew Geisslerc723b722021-01-08 16:14:09 -0600386 description = "${default_flag} ${conf_desc}";
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500387 ${kernel_line}
388 ${fdt_line}
George McCollister185c8ae2016-05-26 08:55:16 -0500389 ${ramdisk_line}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600390 ${setup_line}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500391 hash@1 {
392 algo = "${conf_csum}";
393 };
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600394EOF
395
396 if [ ! -z "${conf_sign_keyname}" ] ; then
397
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800398 sign_line="sign-images = "
399 sep=""
400
Andrew Geisslerc723b722021-01-08 16:14:09 -0600401 if [ -n "${kernel_id}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800402 sign_line="${sign_line}${sep}\"kernel\""
403 sep=", "
404 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600405
Andrew Geisslerc723b722021-01-08 16:14:09 -0600406 if [ -n "${dtb_image}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800407 sign_line="${sign_line}${sep}\"fdt\""
408 sep=", "
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600409 fi
410
Andrew Geisslerc723b722021-01-08 16:14:09 -0600411 if [ -n "${ramdisk_id}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800412 sign_line="${sign_line}${sep}\"ramdisk\""
413 sep=", "
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600414 fi
415
Andrew Geisslerc723b722021-01-08 16:14:09 -0600416 if [ -n "${config_id}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800417 sign_line="${sign_line}${sep}\"setup\""
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600418 fi
419
420 sign_line="${sign_line};"
421
Andrew Geisslerc723b722021-01-08 16:14:09 -0600422 cat << EOF >> ${its_file}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600423 signature@1 {
Brad Bishop64c979e2019-11-04 13:55:29 -0500424 algo = "${conf_csum},${conf_sign_algo}";
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600425 key-name-hint = "${conf_sign_keyname}";
426 ${sign_line}
427 };
428EOF
429 fi
430
Andrew Geisslerc723b722021-01-08 16:14:09 -0600431 cat << EOF >> ${its_file}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500432 };
433EOF
434}
435
George McCollister185c8ae2016-05-26 08:55:16 -0500436#
437# Assemble fitImage
438#
439# $1 ... .its filename
440# $2 ... fitImage name
441# $3 ... include ramdisk
442fitimage_assemble() {
443 kernelcount=1
444 dtbcount=""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500445 DTBS=""
George McCollister185c8ae2016-05-26 08:55:16 -0500446 ramdiskcount=${3}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600447 setupcount=""
George McCollister185c8ae2016-05-26 08:55:16 -0500448 rm -f ${1} arch/${ARCH}/boot/${2}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500449
George McCollister185c8ae2016-05-26 08:55:16 -0500450 fitimage_emit_fit_header ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500451
George McCollister185c8ae2016-05-26 08:55:16 -0500452 #
453 # Step 1: Prepare a kernel image section.
454 #
455 fitimage_emit_section_maint ${1} imagestart
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500456
George McCollister185c8ae2016-05-26 08:55:16 -0500457 uboot_prep_kimage
458 fitimage_emit_section_kernel ${1} "${kernelcount}" linux.bin "${linux_comp}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500459
George McCollister185c8ae2016-05-26 08:55:16 -0500460 #
461 # Step 2: Prepare a DTB image section
462 #
Brad Bishop19323692019-04-05 15:28:33 -0400463
464 if [ -z "${EXTERNAL_KERNEL_DEVICETREE}" ] && [ -n "${KERNEL_DEVICETREE}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500465 dtbcount=1
George McCollister185c8ae2016-05-26 08:55:16 -0500466 for DTB in ${KERNEL_DEVICETREE}; do
467 if echo ${DTB} | grep -q '/dts/'; then
468 bbwarn "${DTB} contains the full path to the the dts file, but only the dtb name should be used."
469 DTB=`basename ${DTB} | sed 's,\.dts$,.dtb,g'`
470 fi
471 DTB_PATH="arch/${ARCH}/boot/dts/${DTB}"
472 if [ ! -e "${DTB_PATH}" ]; then
473 DTB_PATH="arch/${ARCH}/boot/${DTB}"
474 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500475
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500476 DTB=$(echo "${DTB}" | tr '/' '_')
477 DTBS="${DTBS} ${DTB}"
478 fitimage_emit_section_dtb ${1} ${DTB} ${DTB_PATH}
George McCollister185c8ae2016-05-26 08:55:16 -0500479 done
480 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500481
Brad Bishop19323692019-04-05 15:28:33 -0400482 if [ -n "${EXTERNAL_KERNEL_DEVICETREE}" ]; then
483 dtbcount=1
Andrew Geissler82c905d2020-04-13 13:39:40 -0500484 for DTB in $(find "${EXTERNAL_KERNEL_DEVICETREE}" \( -name '*.dtb' -o -name '*.dtbo' \) -printf '%P\n' | sort); do
Brad Bishop19323692019-04-05 15:28:33 -0400485 DTB=$(echo "${DTB}" | tr '/' '_')
486 DTBS="${DTBS} ${DTB}"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500487 fitimage_emit_section_dtb ${1} ${DTB} "${EXTERNAL_KERNEL_DEVICETREE}/${DTB}"
Brad Bishop19323692019-04-05 15:28:33 -0400488 done
489 fi
490
George McCollister185c8ae2016-05-26 08:55:16 -0500491 #
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600492 # Step 3: Prepare a setup section. (For x86)
493 #
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500494 if [ -e arch/${ARCH}/boot/setup.bin ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600495 setupcount=1
496 fitimage_emit_section_setup ${1} "${setupcount}" arch/${ARCH}/boot/setup.bin
497 fi
498
499 #
500 # Step 4: Prepare a ramdisk section.
George McCollister185c8ae2016-05-26 08:55:16 -0500501 #
502 if [ "x${ramdiskcount}" = "x1" ] ; then
Rick Altherrbc1b8802017-01-20 11:28:53 -0800503 # Find and use the first initramfs image archive type we find
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800504 for img in cpio.lz4 cpio.lzo cpio.lzma cpio.xz cpio.gz ext2.gz cpio; do
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500505 initramfs_path="${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.${img}"
Rick Altherrbc1b8802017-01-20 11:28:53 -0800506 echo "Using $initramfs_path"
507 if [ -e "${initramfs_path}" ]; then
508 fitimage_emit_section_ramdisk ${1} "${ramdiskcount}" "${initramfs_path}"
509 break
510 fi
511 done
George McCollister185c8ae2016-05-26 08:55:16 -0500512 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500513
George McCollister185c8ae2016-05-26 08:55:16 -0500514 fitimage_emit_section_maint ${1} sectend
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500515
George McCollister185c8ae2016-05-26 08:55:16 -0500516 # Force the first Kernel and DTB in the default config
517 kernelcount=1
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500518 if [ -n "${dtbcount}" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600519 dtbcount=1
520 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500521
George McCollister185c8ae2016-05-26 08:55:16 -0500522 #
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600523 # Step 5: Prepare a configurations section
George McCollister185c8ae2016-05-26 08:55:16 -0500524 #
525 fitimage_emit_section_maint ${1} confstart
526
Andrew Geissler635e0e42020-08-21 15:58:33 -0500527 # kernel-fitimage.bbclass currently only supports a single kernel (no less or
528 # more) to be added to the FIT image along with 0 or more device trees and
529 # 0 or 1 ramdisk.
530 # If a device tree is to be part of the FIT image, then select
531 # the default configuration to be used is based on the dtbcount. If there is
532 # no dtb present than select the default configuation to be based on
533 # the kernelcount.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500534 if [ -n "${DTBS}" ]; then
535 i=1
536 for DTB in ${DTBS}; do
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800537 dtb_ext=${DTB##*.}
538 if [ "${dtb_ext}" = "dtbo" ]; then
539 fitimage_emit_section_config ${1} "" "${DTB}" "" "" "`expr ${i} = ${dtbcount}`"
540 else
541 fitimage_emit_section_config ${1} "${kernelcount}" "${DTB}" "${ramdiskcount}" "${setupcount}" "`expr ${i} = ${dtbcount}`"
542 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500543 i=`expr ${i} + 1`
544 done
Andrew Geissler635e0e42020-08-21 15:58:33 -0500545 else
546 defaultconfigcount=1
547 fitimage_emit_section_config ${1} "${kernelcount}" "" "${ramdiskcount}" "${setupcount}" "${defaultconfigcount}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500548 fi
George McCollister185c8ae2016-05-26 08:55:16 -0500549
550 fitimage_emit_section_maint ${1} sectend
551
552 fitimage_emit_section_maint ${1} fitend
553
554 #
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600555 # Step 6: Assemble the image
George McCollister185c8ae2016-05-26 08:55:16 -0500556 #
Andrew Geisslerc723b722021-01-08 16:14:09 -0600557 ${UBOOT_MKIMAGE} \
George McCollister185c8ae2016-05-26 08:55:16 -0500558 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
559 -f ${1} \
560 arch/${ARCH}/boot/${2}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600561
562 #
563 # Step 7: Sign the image and add public key to U-Boot dtb
564 #
565 if [ "x${UBOOT_SIGN_ENABLE}" = "x1" ] ; then
Brad Bishop19323692019-04-05 15:28:33 -0400566 add_key_to_u_boot=""
567 if [ -n "${UBOOT_DTB_BINARY}" ]; then
568 # The u-boot.dtb is a symlink to UBOOT_DTB_IMAGE, so we need copy
569 # both of them, and don't dereference the symlink.
570 cp -P ${STAGING_DATADIR}/u-boot*.dtb ${B}
571 add_key_to_u_boot="-K ${B}/${UBOOT_DTB_BINARY}"
572 fi
Andrew Geisslerc723b722021-01-08 16:14:09 -0600573 ${UBOOT_MKIMAGE_SIGN} \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600574 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
575 -F -k "${UBOOT_SIGN_KEYDIR}" \
Brad Bishop19323692019-04-05 15:28:33 -0400576 $add_key_to_u_boot \
Andrew Geisslerc723b722021-01-08 16:14:09 -0600577 -r arch/${ARCH}/boot/${2} \
578 ${UBOOT_MKIMAGE_SIGN_ARGS}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600579 fi
George McCollister185c8ae2016-05-26 08:55:16 -0500580}
581
582do_assemble_fitimage() {
583 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage"; then
584 cd ${B}
585 fitimage_assemble fit-image.its fitImage
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500586 fi
587}
588
589addtask assemble_fitimage before do_install after do_compile
590
George McCollister185c8ae2016-05-26 08:55:16 -0500591do_assemble_fitimage_initramfs() {
592 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage" && \
593 test -n "${INITRAMFS_IMAGE}" ; then
594 cd ${B}
595 fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage-${INITRAMFS_IMAGE} 1
596 fi
597}
598
Brad Bishop19323692019-04-05 15:28:33 -0400599addtask assemble_fitimage_initramfs before do_deploy after do_bundle_initramfs
George McCollister185c8ae2016-05-26 08:55:16 -0500600
Andrew Geisslerf0343792020-11-18 10:42:21 -0600601do_generate_rsa_keys() {
602 if [ "${UBOOT_SIGN_ENABLE}" = "0" ] && [ "${FIT_GENERATE_KEYS}" = "1" ]; then
603 bbwarn "FIT_GENERATE_KEYS is set to 1 eventhough UBOOT_SIGN_ENABLE is set to 0. The keys will not be generated as they won't be used."
604 fi
605
606 if [ "${UBOOT_SIGN_ENABLE}" = "1" ] && [ "${FIT_GENERATE_KEYS}" = "1" ]; then
607
608 # Generate keys only if they don't already exist
609 if [ ! -f "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".key ] || \
610 [ ! -f "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".crt]; then
611
612 # make directory if it does not already exist
613 mkdir -p "${UBOOT_SIGN_KEYDIR}"
614
615 echo "Generating RSA private key for signing fitImage"
616 openssl genrsa ${FIT_KEY_GENRSA_ARGS} -out \
617 "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".key \
618 "${FIT_SIGN_NUMBITS}"
619
620 echo "Generating certificate for signing fitImage"
621 openssl req ${FIT_KEY_REQ_ARGS} "${FIT_KEY_SIGN_PKCS}" \
622 -key "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".key \
623 -out "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".crt
624 fi
625 fi
626}
627
628addtask generate_rsa_keys before do_assemble_fitimage after do_compile
George McCollister185c8ae2016-05-26 08:55:16 -0500629
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500630kernel_do_deploy[vardepsexclude] = "DATETIME"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500631kernel_do_deploy_append() {
632 # Update deploy directory
He Zhefe76b1e2016-05-25 04:47:16 -0400633 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500634 echo "Copying fit-image.its source file..."
Brad Bishop64c979e2019-11-04 13:55:29 -0500635 install -m 0644 ${B}/fit-image.its "$deployDir/fitImage-its-${KERNEL_FIT_NAME}.its"
636 ln -snf fitImage-its-${KERNEL_FIT_NAME}.its "$deployDir/fitImage-its-${KERNEL_FIT_LINK_NAME}"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800637
638 echo "Copying linux.bin file..."
Brad Bishop64c979e2019-11-04 13:55:29 -0500639 install -m 0644 ${B}/linux.bin $deployDir/fitImage-linux.bin-${KERNEL_FIT_NAME}.bin
640 ln -snf fitImage-linux.bin-${KERNEL_FIT_NAME}.bin "$deployDir/fitImage-linux.bin-${KERNEL_FIT_LINK_NAME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500641
George McCollister185c8ae2016-05-26 08:55:16 -0500642 if [ -n "${INITRAMFS_IMAGE}" ]; then
643 echo "Copying fit-image-${INITRAMFS_IMAGE}.its source file..."
Brad Bishop64c979e2019-11-04 13:55:29 -0500644 install -m 0644 ${B}/fit-image-${INITRAMFS_IMAGE}.its "$deployDir/fitImage-its-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.its"
645 ln -snf fitImage-its-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.its "$deployDir/fitImage-its-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_LINK_NAME}"
George McCollister185c8ae2016-05-26 08:55:16 -0500646
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800647 echo "Copying fitImage-${INITRAMFS_IMAGE} file..."
Brad Bishop64c979e2019-11-04 13:55:29 -0500648 install -m 0644 ${B}/arch/${ARCH}/boot/fitImage-${INITRAMFS_IMAGE} "$deployDir/fitImage-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.bin"
649 ln -snf fitImage-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.bin "$deployDir/fitImage-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_LINK_NAME}"
George McCollister185c8ae2016-05-26 08:55:16 -0500650 fi
Brad Bishop19323692019-04-05 15:28:33 -0400651 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a -n "${UBOOT_DTB_BINARY}" ] ; then
652 # UBOOT_DTB_IMAGE is a realfile, but we can't use
653 # ${UBOOT_DTB_IMAGE} since it contains ${PV} which is aimed
654 # for u-boot, but we are in kernel env now.
Brad Bishop64c979e2019-11-04 13:55:29 -0500655 install -m 0644 ${B}/u-boot-${MACHINE}*.dtb "$deployDir/"
Brad Bishop19323692019-04-05 15:28:33 -0400656 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500657 fi
658}