blob: f41509d308ac6fcd535f54c3da92bb4c9f200250 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
Patrick Williams2390b1b2022-11-03 13:47:49 -05007inherit kernel-uboot kernel-artifact-names uboot-config
Patrick Williams96e4b4e2025-02-03 15:49:15 -05008require conf/image-fitimage.conf
Patrick Williams92b42cb2022-09-03 06:53:57 -05009
10def get_fit_replacement_type(d):
11 kerneltypes = d.getVar('KERNEL_IMAGETYPES') or ""
12 replacementtype = ""
13 if 'fitImage' in kerneltypes.split():
14 uarch = d.getVar("UBOOT_ARCH")
15 if uarch == "arm64":
16 replacementtype = "Image"
17 elif uarch == "riscv":
18 replacementtype = "Image"
19 elif uarch == "mips":
20 replacementtype = "vmlinuz.bin"
21 elif uarch == "x86":
22 replacementtype = "bzImage"
23 elif uarch == "microblaze":
24 replacementtype = "linux.bin"
25 else:
26 replacementtype = "zImage"
27 return replacementtype
28
29KERNEL_IMAGETYPE_REPLACEMENT ?= "${@get_fit_replacement_type(d)}"
30DEPENDS:append = " ${@'u-boot-tools-native dtc-native' if 'fitImage' in (d.getVar('KERNEL_IMAGETYPES') or '').split() else ''}"
31
32python __anonymous () {
Patrick Williams84603582024-12-14 08:00:57 -050033 # Override KERNEL_IMAGETYPE_FOR_MAKE variable, which is internal
34 # to kernel.bbclass . We have to override it, since we pack zImage
35 # (at least for now) into the fitImage .
36 typeformake = d.getVar("KERNEL_IMAGETYPE_FOR_MAKE") or ""
37 if 'fitImage' in typeformake.split():
38 d.setVar('KERNEL_IMAGETYPE_FOR_MAKE', typeformake.replace('fitImage', d.getVar('KERNEL_IMAGETYPE_REPLACEMENT')))
Patrick Williams92b42cb2022-09-03 06:53:57 -050039
Patrick Williams84603582024-12-14 08:00:57 -050040 image = d.getVar('INITRAMFS_IMAGE')
Patrick Williams43a6b7c2025-02-13 15:13:32 -050041 if image and not bb.utils.to_boolean(d.getVar('INITRAMFS_IMAGE_BUNDLE')):
42 if d.getVar('INITRAMFS_MULTICONFIG'):
43 mc = d.getVar('BB_CURRENT_MC')
44 d.appendVarFlag('do_assemble_fitimage_initramfs', 'mcdepends', ' mc:' + mc + ':${INITRAMFS_MULTICONFIG}:${INITRAMFS_IMAGE}:do_image_complete')
45 else:
46 d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' ${INITRAMFS_IMAGE}:do_image_complete')
Patrick Williams92b42cb2022-09-03 06:53:57 -050047
Patrick Williams84603582024-12-14 08:00:57 -050048 #check if there are any dtb providers
49 providerdtb = d.getVar("PREFERRED_PROVIDER_virtual/dtb")
50 if providerdtb:
51 d.appendVarFlag('do_assemble_fitimage', 'depends', ' virtual/dtb:do_populate_sysroot')
52 d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' virtual/dtb:do_populate_sysroot')
53 d.setVar('EXTERNAL_KERNEL_DEVICETREE', "${RECIPE_SYSROOT}/boot/devicetree")
Patrick Williams92b42cb2022-09-03 06:53:57 -050054}
55
Patrick Williams92b42cb2022-09-03 06:53:57 -050056#
57# Emit the fitImage ITS header
58#
59# $1 ... .its filename
60fitimage_emit_fit_header() {
61 cat << EOF >> $1
62/dts-v1/;
63
64/ {
65 description = "${FIT_DESC}";
Andrew Geissler028142b2023-05-05 11:29:21 -050066 #address-cells = <${FIT_ADDRESS_CELLS}>;
Patrick Williams92b42cb2022-09-03 06:53:57 -050067EOF
68}
69
70#
71# Emit the fitImage section bits
72#
73# $1 ... .its filename
74# $2 ... Section bit type: imagestart - image section start
75# confstart - configuration section start
76# sectend - section end
77# fitend - fitimage end
78#
79fitimage_emit_section_maint() {
80 case $2 in
81 imagestart)
82 cat << EOF >> $1
83
84 images {
85EOF
86 ;;
87 confstart)
88 cat << EOF >> $1
89
90 configurations {
91EOF
92 ;;
93 sectend)
94 cat << EOF >> $1
95 };
96EOF
97 ;;
98 fitend)
99 cat << EOF >> $1
100};
101EOF
102 ;;
103 esac
104}
105
106#
107# Emit the fitImage ITS kernel section
108#
109# $1 ... .its filename
110# $2 ... Image counter
111# $3 ... Path to kernel image
112# $4 ... Compression type
113fitimage_emit_section_kernel() {
114
115 kernel_csum="${FIT_HASH_ALG}"
116 kernel_sign_algo="${FIT_SIGN_ALG}"
117 kernel_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
118
119 ENTRYPOINT="${UBOOT_ENTRYPOINT}"
120 if [ -n "${UBOOT_ENTRYSYMBOL}" ]; then
121 ENTRYPOINT=`${HOST_PREFIX}nm vmlinux | \
122 awk '$3=="${UBOOT_ENTRYSYMBOL}" {print "0x"$1;exit}'`
123 fi
124
125 cat << EOF >> $1
126 kernel-$2 {
127 description = "Linux kernel";
128 data = /incbin/("$3");
129 type = "${UBOOT_MKIMAGE_KERNEL_TYPE}";
130 arch = "${UBOOT_ARCH}";
131 os = "linux";
132 compression = "$4";
133 load = <${UBOOT_LOADADDRESS}>;
134 entry = <$ENTRYPOINT>;
135 hash-1 {
136 algo = "$kernel_csum";
137 };
138 };
139EOF
140
141 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$kernel_sign_keyname" ] ; then
142 sed -i '$ d' $1
143 cat << EOF >> $1
144 signature-1 {
145 algo = "$kernel_csum,$kernel_sign_algo";
146 key-name-hint = "$kernel_sign_keyname";
147 };
148 };
149EOF
150 fi
151}
152
153#
154# Emit the fitImage ITS DTB section
155#
156# $1 ... .its filename
157# $2 ... Image counter
158# $3 ... Path to DTB image
159fitimage_emit_section_dtb() {
160
161 dtb_csum="${FIT_HASH_ALG}"
162 dtb_sign_algo="${FIT_SIGN_ALG}"
163 dtb_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
164
165 dtb_loadline=""
166 dtb_ext=${DTB##*.}
167 if [ "${dtb_ext}" = "dtbo" ]; then
168 if [ -n "${UBOOT_DTBO_LOADADDRESS}" ]; then
169 dtb_loadline="load = <${UBOOT_DTBO_LOADADDRESS}>;"
170 fi
171 elif [ -n "${UBOOT_DTB_LOADADDRESS}" ]; then
172 dtb_loadline="load = <${UBOOT_DTB_LOADADDRESS}>;"
173 fi
174 cat << EOF >> $1
175 fdt-$2 {
176 description = "Flattened Device Tree blob";
177 data = /incbin/("$3");
178 type = "flat_dt";
179 arch = "${UBOOT_ARCH}";
180 compression = "none";
181 $dtb_loadline
182 hash-1 {
183 algo = "$dtb_csum";
184 };
185 };
186EOF
187
188 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$dtb_sign_keyname" ] ; then
189 sed -i '$ d' $1
190 cat << EOF >> $1
191 signature-1 {
192 algo = "$dtb_csum,$dtb_sign_algo";
193 key-name-hint = "$dtb_sign_keyname";
194 };
195 };
196EOF
197 fi
198}
199
200#
201# Emit the fitImage ITS u-boot script section
202#
203# $1 ... .its filename
204# $2 ... Image counter
205# $3 ... Path to boot script image
206fitimage_emit_section_boot_script() {
207
208 bootscr_csum="${FIT_HASH_ALG}"
209 bootscr_sign_algo="${FIT_SIGN_ALG}"
210 bootscr_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
211
212 cat << EOF >> $1
213 bootscr-$2 {
214 description = "U-boot script";
215 data = /incbin/("$3");
216 type = "script";
217 arch = "${UBOOT_ARCH}";
218 compression = "none";
219 hash-1 {
220 algo = "$bootscr_csum";
221 };
222 };
223EOF
224
225 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$bootscr_sign_keyname" ] ; then
226 sed -i '$ d' $1
227 cat << EOF >> $1
228 signature-1 {
229 algo = "$bootscr_csum,$bootscr_sign_algo";
230 key-name-hint = "$bootscr_sign_keyname";
231 };
232 };
233EOF
234 fi
235}
236
237#
238# Emit the fitImage ITS setup section
239#
240# $1 ... .its filename
241# $2 ... Image counter
242# $3 ... Path to setup image
243fitimage_emit_section_setup() {
244
245 setup_csum="${FIT_HASH_ALG}"
246
247 cat << EOF >> $1
248 setup-$2 {
249 description = "Linux setup.bin";
250 data = /incbin/("$3");
251 type = "x86_setup";
252 arch = "${UBOOT_ARCH}";
253 os = "linux";
254 compression = "none";
255 load = <0x00090000>;
256 entry = <0x00090000>;
257 hash-1 {
258 algo = "$setup_csum";
259 };
260 };
261EOF
262}
263
264#
265# Emit the fitImage ITS ramdisk section
266#
267# $1 ... .its filename
268# $2 ... Image counter
269# $3 ... Path to ramdisk image
270fitimage_emit_section_ramdisk() {
271
272 ramdisk_csum="${FIT_HASH_ALG}"
273 ramdisk_sign_algo="${FIT_SIGN_ALG}"
274 ramdisk_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
275 ramdisk_loadline=""
276 ramdisk_entryline=""
277
278 if [ -n "${UBOOT_RD_LOADADDRESS}" ]; then
279 ramdisk_loadline="load = <${UBOOT_RD_LOADADDRESS}>;"
280 fi
281 if [ -n "${UBOOT_RD_ENTRYPOINT}" ]; then
282 ramdisk_entryline="entry = <${UBOOT_RD_ENTRYPOINT}>;"
283 fi
284
285 cat << EOF >> $1
286 ramdisk-$2 {
287 description = "${INITRAMFS_IMAGE}";
288 data = /incbin/("$3");
289 type = "ramdisk";
290 arch = "${UBOOT_ARCH}";
291 os = "linux";
292 compression = "none";
293 $ramdisk_loadline
294 $ramdisk_entryline
295 hash-1 {
296 algo = "$ramdisk_csum";
297 };
298 };
299EOF
300
301 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$ramdisk_sign_keyname" ] ; then
302 sed -i '$ d' $1
303 cat << EOF >> $1
304 signature-1 {
305 algo = "$ramdisk_csum,$ramdisk_sign_algo";
306 key-name-hint = "$ramdisk_sign_keyname";
307 };
308 };
309EOF
310 fi
311}
312
313#
Patrick Williams2390b1b2022-11-03 13:47:49 -0500314# echoes symlink destination if it points below directory
315#
316# $1 ... file that's a potential symlink
317# $2 ... expected parent directory
318symlink_points_below() {
319 file="$2/$1"
320 dir=$2
321
322 if ! [ -L "$file" ]; then
323 return
324 fi
325
326 realpath="$(realpath --relative-to=$dir $file)"
327 if [ -z "${realpath%%../*}" ]; then
328 return
329 fi
330
331 echo "$realpath"
332}
333
334#
Patrick Williams92b42cb2022-09-03 06:53:57 -0500335# Emit the fitImage ITS configuration section
336#
337# $1 ... .its filename
338# $2 ... Linux kernel ID
339# $3 ... DTB image name
340# $4 ... ramdisk ID
341# $5 ... u-boot script ID
342# $6 ... config ID
343# $7 ... default flag
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500344# $8 ... default DTB image name
Patrick Williams92b42cb2022-09-03 06:53:57 -0500345fitimage_emit_section_config() {
346
347 conf_csum="${FIT_HASH_ALG}"
348 conf_sign_algo="${FIT_SIGN_ALG}"
349 conf_padding_algo="${FIT_PAD_ALG}"
350 if [ "${UBOOT_SIGN_ENABLE}" = "1" ] ; then
351 conf_sign_keyname="${UBOOT_SIGN_KEYNAME}"
352 fi
353
354 its_file="$1"
355 kernel_id="$2"
356 dtb_image="$3"
357 ramdisk_id="$4"
358 bootscr_id="$5"
359 config_id="$6"
360 default_flag="$7"
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500361 default_dtb_image="$8"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500362
363 # Test if we have any DTBs at all
364 sep=""
365 conf_desc=""
366 conf_node="${FIT_CONF_PREFIX}"
367 kernel_line=""
368 fdt_line=""
369 ramdisk_line=""
370 bootscr_line=""
371 setup_line=""
372 default_line=""
Patrick Williams03514f12024-04-05 07:04:11 -0500373 compatible_line=""
Patrick Williams92b42cb2022-09-03 06:53:57 -0500374
Patrick Williams2390b1b2022-11-03 13:47:49 -0500375 dtb_image_sect=$(symlink_points_below $dtb_image "${EXTERNAL_KERNEL_DEVICETREE}")
376 if [ -z "$dtb_image_sect" ]; then
377 dtb_image_sect=$dtb_image
378 fi
379
Patrick Williams2a254922023-08-11 09:48:11 -0500380 dtb_path="${EXTERNAL_KERNEL_DEVICETREE}/${dtb_image_sect}"
Patrick Williams84603582024-12-14 08:00:57 -0500381 if [ -f "$dtb_path" ] || [ -L "$dtb_path" ]; then
Patrick Williams03514f12024-04-05 07:04:11 -0500382 compat=$(fdtget -t s "$dtb_path" / compatible | sed 's/ /", "/g')
383 if [ -n "$compat" ]; then
384 compatible_line="compatible = \"$compat\";"
385 fi
386 fi
Patrick Williams2a254922023-08-11 09:48:11 -0500387
Patrick Williams2390b1b2022-11-03 13:47:49 -0500388 dtb_image=$(echo $dtb_image | tr '/' '_')
389 dtb_image_sect=$(echo "${dtb_image_sect}" | tr '/' '_')
390
Patrick Williams92b42cb2022-09-03 06:53:57 -0500391 # conf node name is selected based on dtb ID if it is present,
392 # otherwise its selected based on kernel ID
393 if [ -n "$dtb_image" ]; then
394 conf_node=$conf_node$dtb_image
395 else
396 conf_node=$conf_node$kernel_id
397 fi
398
399 if [ -n "$kernel_id" ]; then
400 conf_desc="Linux kernel"
401 sep=", "
402 kernel_line="kernel = \"kernel-$kernel_id\";"
403 fi
404
405 if [ -n "$dtb_image" ]; then
406 conf_desc="$conf_desc${sep}FDT blob"
407 sep=", "
Patrick Williams2390b1b2022-11-03 13:47:49 -0500408 fdt_line="fdt = \"fdt-$dtb_image_sect\";"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500409 fi
410
411 if [ -n "$ramdisk_id" ]; then
412 conf_desc="$conf_desc${sep}ramdisk"
413 sep=", "
414 ramdisk_line="ramdisk = \"ramdisk-$ramdisk_id\";"
415 fi
416
417 if [ -n "$bootscr_id" ]; then
418 conf_desc="$conf_desc${sep}u-boot script"
419 sep=", "
420 bootscr_line="bootscr = \"bootscr-$bootscr_id\";"
421 fi
422
423 if [ -n "$config_id" ]; then
424 conf_desc="$conf_desc${sep}setup"
425 setup_line="setup = \"setup-$config_id\";"
426 fi
427
428 if [ "$default_flag" = "1" ]; then
429 # default node is selected based on dtb ID if it is present,
430 # otherwise its selected based on kernel ID
431 if [ -n "$dtb_image" ]; then
Patrick Williams84603582024-12-14 08:00:57 -0500432 # Select default node as user specified dtb when
433 # multiple dtb exists.
434 if [ -n "$default_dtb_image" ]; then
435 default_line="default = \"${FIT_CONF_PREFIX}$default_dtb_image\";"
436 else
437 default_line="default = \"${FIT_CONF_PREFIX}$dtb_image\";"
438 fi
Patrick Williams92b42cb2022-09-03 06:53:57 -0500439 else
440 default_line="default = \"${FIT_CONF_PREFIX}$kernel_id\";"
441 fi
442 fi
443
444 cat << EOF >> $its_file
445 $default_line
446 $conf_node {
447 description = "$default_flag $conf_desc";
Patrick Williams2a254922023-08-11 09:48:11 -0500448 $compatible_line
Patrick Williams92b42cb2022-09-03 06:53:57 -0500449 $kernel_line
450 $fdt_line
451 $ramdisk_line
452 $bootscr_line
453 $setup_line
454 hash-1 {
455 algo = "$conf_csum";
456 };
457EOF
458
459 if [ -n "$conf_sign_keyname" ] ; then
460
461 sign_line="sign-images = "
462 sep=""
463
464 if [ -n "$kernel_id" ]; then
465 sign_line="$sign_line${sep}\"kernel\""
466 sep=", "
467 fi
468
469 if [ -n "$dtb_image" ]; then
470 sign_line="$sign_line${sep}\"fdt\""
471 sep=", "
472 fi
473
474 if [ -n "$ramdisk_id" ]; then
475 sign_line="$sign_line${sep}\"ramdisk\""
476 sep=", "
477 fi
478
479 if [ -n "$bootscr_id" ]; then
480 sign_line="$sign_line${sep}\"bootscr\""
481 sep=", "
482 fi
483
484 if [ -n "$config_id" ]; then
485 sign_line="$sign_line${sep}\"setup\""
486 fi
487
488 sign_line="$sign_line;"
489
490 cat << EOF >> $its_file
491 signature-1 {
492 algo = "$conf_csum,$conf_sign_algo";
493 key-name-hint = "$conf_sign_keyname";
494 padding = "$conf_padding_algo";
495 $sign_line
496 };
497EOF
498 fi
499
500 cat << EOF >> $its_file
501 };
502EOF
503}
504
505#
506# Assemble fitImage
507#
508# $1 ... .its filename
509# $2 ... fitImage name
510# $3 ... include ramdisk
511fitimage_assemble() {
512 kernelcount=1
513 dtbcount=""
514 DTBS=""
515 ramdiskcount=$3
516 setupcount=""
517 bootscr_id=""
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500518 default_dtb_image=""
519 rm -f $1 arch/${ARCH}/boot/$2
Patrick Williams92b42cb2022-09-03 06:53:57 -0500520
521 if [ -n "${UBOOT_SIGN_IMG_KEYNAME}" -a "${UBOOT_SIGN_KEYNAME}" = "${UBOOT_SIGN_IMG_KEYNAME}" ]; then
522 bbfatal "Keys used to sign images and configuration nodes must be different."
523 fi
524
525 fitimage_emit_fit_header $1
526
527 #
528 # Step 1: Prepare a kernel image section.
529 #
530 fitimage_emit_section_maint $1 imagestart
531
532 uboot_prep_kimage
533 fitimage_emit_section_kernel $1 $kernelcount linux.bin "$linux_comp"
534
535 #
536 # Step 2: Prepare a DTB image section
537 #
538
539 if [ -n "${KERNEL_DEVICETREE}" ]; then
540 dtbcount=1
541 for DTB in ${KERNEL_DEVICETREE}; do
542 if echo $DTB | grep -q '/dts/'; then
543 bbwarn "$DTB contains the full path to the the dts file, but only the dtb name should be used."
544 DTB=`basename $DTB | sed 's,\.dts$,.dtb,g'`
545 fi
546
547 # Skip ${DTB} if it's also provided in ${EXTERNAL_KERNEL_DEVICETREE}
548 if [ -n "${EXTERNAL_KERNEL_DEVICETREE}" ] && [ -s ${EXTERNAL_KERNEL_DEVICETREE}/${DTB} ]; then
549 continue
550 fi
551
Patrick Williams2390b1b2022-11-03 13:47:49 -0500552 DTB_PATH="${KERNEL_OUTPUT_DIR}/dts/$DTB"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500553 if [ ! -e "$DTB_PATH" ]; then
Patrick Williams2390b1b2022-11-03 13:47:49 -0500554 DTB_PATH="${KERNEL_OUTPUT_DIR}/$DTB"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500555 fi
556
Patrick Williams84603582024-12-14 08:00:57 -0500557 # Strip off the path component from the filename
Andrew Geissler220dafd2023-10-04 10:18:08 -0500558 if "${@'false' if oe.types.boolean(d.getVar('KERNEL_DTBVENDORED')) else 'true'}"; then
Patrick Williams84603582024-12-14 08:00:57 -0500559 DTB=`basename $DTB`
Andrew Geissler220dafd2023-10-04 10:18:08 -0500560 fi
561
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500562 # Set the default dtb image if it exists in the devicetree.
Patrick Williams84603582024-12-14 08:00:57 -0500563 if [ "${FIT_CONF_DEFAULT_DTB}" = "$DTB" ];then
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500564 default_dtb_image=$(echo "$DTB" | tr '/' '_')
565 fi
566
567 DTB=$(echo "$DTB" | tr '/' '_')
568
Patrick Williams92b42cb2022-09-03 06:53:57 -0500569 # Skip DTB if we've picked it up previously
570 echo "$DTBS" | tr ' ' '\n' | grep -xq "$DTB" && continue
571
572 DTBS="$DTBS $DTB"
Patrick Williams2390b1b2022-11-03 13:47:49 -0500573 DTB=$(echo $DTB | tr '/' '_')
Patrick Williams92b42cb2022-09-03 06:53:57 -0500574 fitimage_emit_section_dtb $1 $DTB $DTB_PATH
575 done
576 fi
577
578 if [ -n "${EXTERNAL_KERNEL_DEVICETREE}" ]; then
579 dtbcount=1
Andrew Geissler517393d2023-01-13 08:55:19 -0600580 for DTB in $(find "${EXTERNAL_KERNEL_DEVICETREE}" -name '*.dtb' -printf '%P\n' | sort) \
581 $(find "${EXTERNAL_KERNEL_DEVICETREE}" -name '*.dtbo' -printf '%P\n' | sort); do
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500582 # Set the default dtb image if it exists in the devicetree.
583 if [ ${FIT_CONF_DEFAULT_DTB} = $DTB ];then
584 default_dtb_image=$(echo "$DTB" | tr '/' '_')
585 fi
586
587 DTB=$(echo "$DTB" | tr '/' '_')
588
Andrew Geissler517393d2023-01-13 08:55:19 -0600589 # Skip DTB/DTBO if we've picked it up previously
Patrick Williams92b42cb2022-09-03 06:53:57 -0500590 echo "$DTBS" | tr ' ' '\n' | grep -xq "$DTB" && continue
591
592 DTBS="$DTBS $DTB"
Patrick Williams2390b1b2022-11-03 13:47:49 -0500593
594 # Also skip if a symlink. We'll later have each config section point at it
595 [ $(symlink_points_below $DTB "${EXTERNAL_KERNEL_DEVICETREE}") ] && continue
596
597 DTB=$(echo $DTB | tr '/' '_')
Patrick Williams92b42cb2022-09-03 06:53:57 -0500598 fitimage_emit_section_dtb $1 $DTB "${EXTERNAL_KERNEL_DEVICETREE}/$DTB"
599 done
600 fi
601
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500602 if [ -n "${FIT_CONF_DEFAULT_DTB}" ] && [ -z $default_dtb_image ]; then
603 bbwarn "${FIT_CONF_DEFAULT_DTB} is not available in the list of device trees."
604 fi
605
Patrick Williams92b42cb2022-09-03 06:53:57 -0500606 #
607 # Step 3: Prepare a u-boot script section
608 #
609
Patrick Williams43a6b7c2025-02-13 15:13:32 -0500610 if [ -n "${FIT_UBOOT_ENV}" ]; then
611 cp ${UNPACKDIR}/${FIT_UBOOT_ENV} ${B}
612 bootscr_id="${FIT_UBOOT_ENV}"
613 fitimage_emit_section_boot_script $1 "$bootscr_id" ${FIT_UBOOT_ENV}
Patrick Williams92b42cb2022-09-03 06:53:57 -0500614 fi
615
616 #
617 # Step 4: Prepare a setup section. (For x86)
618 #
Patrick Williams2390b1b2022-11-03 13:47:49 -0500619 if [ -e ${KERNEL_OUTPUT_DIR}/setup.bin ]; then
Patrick Williams92b42cb2022-09-03 06:53:57 -0500620 setupcount=1
Patrick Williams2390b1b2022-11-03 13:47:49 -0500621 fitimage_emit_section_setup $1 $setupcount ${KERNEL_OUTPUT_DIR}/setup.bin
Patrick Williams92b42cb2022-09-03 06:53:57 -0500622 fi
623
624 #
625 # Step 5: Prepare a ramdisk section.
626 #
627 if [ "x${ramdiskcount}" = "x1" ] && [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ]; then
628 # Find and use the first initramfs image archive type we find
629 found=
630 for img in ${FIT_SUPPORTED_INITRAMFS_FSTYPES}; do
Patrick Williams43a6b7c2025-02-13 15:13:32 -0500631 initramfs_path="${INITRAMFS_DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500632 if [ -e "$initramfs_path" ]; then
633 bbnote "Found initramfs image: $initramfs_path"
634 found=true
635 fitimage_emit_section_ramdisk $1 "$ramdiskcount" "$initramfs_path"
636 break
637 else
638 bbnote "Did not find initramfs image: $initramfs_path"
639 fi
640 done
641
642 if [ -z "$found" ]; then
643 bbfatal "Could not find a valid initramfs type for ${INITRAMFS_IMAGE_NAME}, the supported types are: ${FIT_SUPPORTED_INITRAMFS_FSTYPES}"
644 fi
645 fi
646
647 fitimage_emit_section_maint $1 sectend
648
649 # Force the first Kernel and DTB in the default config
650 kernelcount=1
651 if [ -n "$dtbcount" ]; then
652 dtbcount=1
653 fi
654
655 #
656 # Step 6: Prepare a configurations section
657 #
658 fitimage_emit_section_maint $1 confstart
659
660 # kernel-fitimage.bbclass currently only supports a single kernel (no less or
661 # more) to be added to the FIT image along with 0 or more device trees and
662 # 0 or 1 ramdisk.
Patrick Williams84603582024-12-14 08:00:57 -0500663 # It is also possible to include an initramfs bundle (kernel and rootfs in one binary)
664 # When the initramfs bundle is used ramdisk is disabled.
Patrick Williams92b42cb2022-09-03 06:53:57 -0500665 # If a device tree is to be part of the FIT image, then select
666 # the default configuration to be used is based on the dtbcount. If there is
667 # no dtb present than select the default configuation to be based on
668 # the kernelcount.
669 if [ -n "$DTBS" ]; then
670 i=1
671 for DTB in ${DTBS}; do
672 dtb_ext=${DTB##*.}
673 if [ "$dtb_ext" = "dtbo" ]; then
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500674 fitimage_emit_section_config $1 "" "$DTB" "" "$bootscr_id" "" "`expr $i = $dtbcount`" "$default_dtb_image"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500675 else
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500676 fitimage_emit_section_config $1 $kernelcount "$DTB" "$ramdiskcount" "$bootscr_id" "$setupcount" "`expr $i = $dtbcount`" "$default_dtb_image"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500677 fi
678 i=`expr $i + 1`
679 done
680 else
681 defaultconfigcount=1
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500682 fitimage_emit_section_config $1 $kernelcount "" "$ramdiskcount" "$bootscr_id" "$setupcount" $defaultconfigcount "$default_dtb_image"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500683 fi
684
685 fitimage_emit_section_maint $1 sectend
686
687 fitimage_emit_section_maint $1 fitend
688
689 #
690 # Step 7: Assemble the image
691 #
692 ${UBOOT_MKIMAGE} \
693 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
694 -f $1 \
Patrick Williams2390b1b2022-11-03 13:47:49 -0500695 ${KERNEL_OUTPUT_DIR}/$2
Patrick Williams92b42cb2022-09-03 06:53:57 -0500696
697 #
Patrick Williams2390b1b2022-11-03 13:47:49 -0500698 # Step 8: Sign the image
Patrick Williams92b42cb2022-09-03 06:53:57 -0500699 #
700 if [ "x${UBOOT_SIGN_ENABLE}" = "x1" ] ; then
Patrick Williams92b42cb2022-09-03 06:53:57 -0500701 ${UBOOT_MKIMAGE_SIGN} \
702 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
703 -F -k "${UBOOT_SIGN_KEYDIR}" \
Patrick Williams2390b1b2022-11-03 13:47:49 -0500704 -r ${KERNEL_OUTPUT_DIR}/$2 \
Patrick Williams92b42cb2022-09-03 06:53:57 -0500705 ${UBOOT_MKIMAGE_SIGN_ARGS}
706 fi
707}
708
709do_assemble_fitimage() {
710 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage"; then
711 cd ${B}
Patrick Williams2390b1b2022-11-03 13:47:49 -0500712 fitimage_assemble fit-image.its fitImage-none ""
713 if [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ]; then
714 ln -sf fitImage-none ${B}/${KERNEL_OUTPUT_DIR}/fitImage
715 fi
Patrick Williams92b42cb2022-09-03 06:53:57 -0500716 fi
717}
718
719addtask assemble_fitimage before do_install after do_compile
720
Patrick Williams2390b1b2022-11-03 13:47:49 -0500721SYSROOT_DIRS:append = " /sysroot-only"
722do_install:append() {
723 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage" && \
724 [ "${UBOOT_SIGN_ENABLE}" = "1" ]; then
725 install -D ${B}/${KERNEL_OUTPUT_DIR}/fitImage-none ${D}/sysroot-only/fitImage
726 fi
727}
728
Patrick Williams92b42cb2022-09-03 06:53:57 -0500729do_assemble_fitimage_initramfs() {
730 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage" && \
731 test -n "${INITRAMFS_IMAGE}" ; then
732 cd ${B}
733 if [ "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then
Patrick Williams2390b1b2022-11-03 13:47:49 -0500734 fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage-bundle ""
735 ln -sf fitImage-bundle ${B}/${KERNEL_OUTPUT_DIR}/fitImage
Patrick Williams92b42cb2022-09-03 06:53:57 -0500736 else
737 fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage-${INITRAMFS_IMAGE} 1
738 fi
739 fi
740}
741
742addtask assemble_fitimage_initramfs before do_deploy after do_bundle_initramfs
743
744do_kernel_generate_rsa_keys() {
745 if [ "${UBOOT_SIGN_ENABLE}" = "0" ] && [ "${FIT_GENERATE_KEYS}" = "1" ]; then
746 bbwarn "FIT_GENERATE_KEYS is set to 1 even though UBOOT_SIGN_ENABLE is set to 0. The keys will not be generated as they won't be used."
747 fi
748
749 if [ "${UBOOT_SIGN_ENABLE}" = "1" ] && [ "${FIT_GENERATE_KEYS}" = "1" ]; then
750
751 # Generate keys to sign configuration nodes, only if they don't already exist
752 if [ ! -f "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".key ] || \
753 [ ! -f "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".crt ]; then
754
755 # make directory if it does not already exist
756 mkdir -p "${UBOOT_SIGN_KEYDIR}"
757
758 bbnote "Generating RSA private key for signing fitImage"
759 openssl genrsa ${FIT_KEY_GENRSA_ARGS} -out \
760 "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".key \
761 "${FIT_SIGN_NUMBITS}"
762
763 bbnote "Generating certificate for signing fitImage"
764 openssl req ${FIT_KEY_REQ_ARGS} "${FIT_KEY_SIGN_PKCS}" \
765 -key "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".key \
766 -out "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".crt
767 fi
768
769 # Generate keys to sign image nodes, only if they don't already exist
770 if [ ! -f "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_IMG_KEYNAME}".key ] || \
771 [ ! -f "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_IMG_KEYNAME}".crt ]; then
772
773 # make directory if it does not already exist
774 mkdir -p "${UBOOT_SIGN_KEYDIR}"
775
776 bbnote "Generating RSA private key for signing fitImage"
777 openssl genrsa ${FIT_KEY_GENRSA_ARGS} -out \
778 "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_IMG_KEYNAME}".key \
779 "${FIT_SIGN_NUMBITS}"
780
781 bbnote "Generating certificate for signing fitImage"
782 openssl req ${FIT_KEY_REQ_ARGS} "${FIT_KEY_SIGN_PKCS}" \
783 -key "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_IMG_KEYNAME}".key \
784 -out "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_IMG_KEYNAME}".crt
785 fi
786 fi
787}
788
789addtask kernel_generate_rsa_keys before do_assemble_fitimage after do_compile
790
791kernel_do_deploy[vardepsexclude] = "DATETIME"
792kernel_do_deploy:append() {
793 # Update deploy directory
794 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage"; then
795
796 if [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ]; then
797 bbnote "Copying fit-image.its source file..."
798 install -m 0644 ${B}/fit-image.its "$deployDir/fitImage-its-${KERNEL_FIT_NAME}.its"
799 if [ -n "${KERNEL_FIT_LINK_NAME}" ] ; then
800 ln -snf fitImage-its-${KERNEL_FIT_NAME}.its "$deployDir/fitImage-its-${KERNEL_FIT_LINK_NAME}"
801 fi
802
803 bbnote "Copying linux.bin file..."
804 install -m 0644 ${B}/linux.bin $deployDir/fitImage-linux.bin-${KERNEL_FIT_NAME}${KERNEL_FIT_BIN_EXT}
805 if [ -n "${KERNEL_FIT_LINK_NAME}" ] ; then
806 ln -snf fitImage-linux.bin-${KERNEL_FIT_NAME}${KERNEL_FIT_BIN_EXT} "$deployDir/fitImage-linux.bin-${KERNEL_FIT_LINK_NAME}"
807 fi
808 fi
809
810 if [ -n "${INITRAMFS_IMAGE}" ]; then
811 bbnote "Copying fit-image-${INITRAMFS_IMAGE}.its source file..."
812 install -m 0644 ${B}/fit-image-${INITRAMFS_IMAGE}.its "$deployDir/fitImage-its-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.its"
813 if [ -n "${KERNEL_FIT_LINK_NAME}" ] ; then
814 ln -snf fitImage-its-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.its "$deployDir/fitImage-its-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_LINK_NAME}"
815 fi
816
817 if [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ]; then
818 bbnote "Copying fitImage-${INITRAMFS_IMAGE} file..."
Patrick Williams2390b1b2022-11-03 13:47:49 -0500819 install -m 0644 ${B}/${KERNEL_OUTPUT_DIR}/fitImage-${INITRAMFS_IMAGE} "$deployDir/fitImage-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}${KERNEL_FIT_BIN_EXT}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500820 if [ -n "${KERNEL_FIT_LINK_NAME}" ] ; then
821 ln -snf fitImage-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}${KERNEL_FIT_BIN_EXT} "$deployDir/fitImage-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_LINK_NAME}"
822 fi
823 fi
824 fi
825 fi
Patrick Williams92b42cb2022-09-03 06:53:57 -0500826}