blob: 8be3043547fd14be0bd40e0d9b74f3a62ea85bf9 [file] [log] [blame]
Patrick Williamse479e922023-01-06 13:45:20 -06001inherit uboot-config
2
3CONVERSIONTYPES += "fitImage"
4
Patrick Williams1dc6b4a2023-08-04 10:41:33 -05005CONVERSION_CMD:fitImage = "run_assemble_fitimage ${IMAGE_NAME}.${type}"
6INITRAMFS_IMAGE="${IMAGE_NAME}.cpio.${INITRAMFS_CTYPE}"
Patrick Williamse479e922023-01-06 13:45:20 -06007KERNEL_OUTPUT_DIR="${DEPLOY_DIR_IMAGE}"
8
9do_image_cpio[depends] += "virtual/kernel:do_deploy"
10
11run_assemble_fitimage() {
12 export linux_comp="none"
13 fitimage_assemble $1.its $1.fitImage 1
14
15 # The fitimage_assemble puts the image into DEPLOY_DIR_NAME due to
16 # KERNEL_OUTPUT_DIR, but we really want it still in ${IMGDEPLOYDIR}, so
17 # move it.
18 mv ${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE}.fitImage .
19 # Delete the spurious linux.bin created by our stubbed uboot_prep_kimage.
20 rm linux.bin
21}
22
23UBOOT_MKIMAGE_KERNEL_TYPE ?= "kernel"
24uboot_prep_kimage() {
25 cp ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE} linux.bin
26}
27
28DEPENDS:append = " u-boot-tools-native dtc-native virtual/${TARGET_PREFIX}binutils"
29
30# Description string
31FIT_DESC ?= "Kernel fitImage for ${DISTRO_NAME}/${PV}/${MACHINE}"
32
33# Kernel fitImage Hash Algo
34FIT_HASH_ALG ?= "sha256"
35
36# Kernel fitImage Signature Algo
37FIT_SIGN_ALG ?= "rsa2048"
38
39# Kernel / U-Boot fitImage Padding Algo
40FIT_PAD_ALG ?= "pkcs-1.5"
41
42# Generate keys for signing Kernel fitImage
43FIT_GENERATE_KEYS ?= "0"
44
45# Size of private keys in number of bits
46FIT_SIGN_NUMBITS ?= "2048"
47
48# args to openssl genrsa (Default is just the public exponent)
49FIT_KEY_GENRSA_ARGS ?= "-F4"
50
51# args to openssl req (Default is -batch for non interactive mode and
52# -new for new certificate)
53FIT_KEY_REQ_ARGS ?= "-batch -new"
54
55# Standard format for public key certificate
56FIT_KEY_SIGN_PKCS ?= "-x509"
57
58# Sign individual images as well
59FIT_SIGN_INDIVIDUAL ?= "0"
60
61FIT_CONF_PREFIX ?= "conf-"
62FIT_CONF_PREFIX[doc] = "Prefix to use for FIT configuration node name"
63
64FIT_SUPPORTED_INITRAMFS_FSTYPES ?= "cpio.lz4 cpio.lzo cpio.lzma cpio.xz cpio.zst cpio.gz ext2.gz cpio"
65
66#
67# Emit the fitImage ITS header
68#
69# $1 ... .its filename
70fitimage_emit_fit_header() {
Patrick Williamsaf48f632023-03-20 10:13:55 -050071 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -060072/dts-v1/;
73
74/ {
75 description = "${FIT_DESC}";
76 #address-cells = <1>;
77EOF
78}
79
80#
81# Emit the fitImage section bits
82#
83# $1 ... .its filename
84# $2 ... Section bit type: imagestart - image section start
85# confstart - configuration section start
86# sectend - section end
87# fitend - fitimage end
88#
89fitimage_emit_section_maint() {
Patrick Williamsaf48f632023-03-20 10:13:55 -050090 case $2 in
91 imagestart)
92 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -060093
94 images {
95EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -050096 ;;
97 confstart)
98 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -060099
100 configurations {
101EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500102 ;;
103 sectend)
104 cat << EOF >> $1
105 };
Patrick Williamse479e922023-01-06 13:45:20 -0600106EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500107 ;;
108 fitend)
109 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600110};
111EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500112 ;;
113 esac
Patrick Williamse479e922023-01-06 13:45:20 -0600114}
115
116
117#
118# Emit the fitImage ITS kernel section
119#
120# $1 ... .its filename
121# $2 ... Image counter
122# $3 ... Path to kernel image
123# $4 ... Compression type
124fitimage_emit_section_kernel() {
125
Patrick Williamsaf48f632023-03-20 10:13:55 -0500126 kernel_csum="${FIT_HASH_ALG}"
127 kernel_sign_algo="${FIT_SIGN_ALG}"
128 kernel_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
Patrick Williamse479e922023-01-06 13:45:20 -0600129
Patrick Williamsaf48f632023-03-20 10:13:55 -0500130 ENTRYPOINT="${UBOOT_ENTRYPOINT}"
131 if [ -n "${UBOOT_ENTRYSYMBOL}" ]; then
132 ENTRYPOINT=`${HOST_PREFIX}nm vmlinux | \
133 awk '$3=="${UBOOT_ENTRYSYMBOL}" {print "0x"$1;exit}'`
134 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600135
Patrick Williamsaf48f632023-03-20 10:13:55 -0500136 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600137 kernel-$2 {
138 description = "Linux kernel";
139 data = /incbin/("$3");
140 type = "${UBOOT_MKIMAGE_KERNEL_TYPE}";
141 arch = "${UBOOT_ARCH}";
142 os = "linux";
143 compression = "$4";
144 load = <${UBOOT_LOADADDRESS}>;
145 entry = <$ENTRYPOINT>;
146 hash-1 {
147 algo = "$kernel_csum";
148 };
149 };
150EOF
151
Patrick Williamsaf48f632023-03-20 10:13:55 -0500152 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$kernel_sign_keyname" ] ; then
153 sed -i '$ d' $1
154 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600155 signature-1 {
156 algo = "$kernel_csum,$kernel_sign_algo";
157 key-name-hint = "$kernel_sign_keyname";
158 };
159 };
160EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500161 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600162}
163
164#
165# Emit the fitImage ITS DTB section
166#
167# $1 ... .its filename
168# $2 ... Image counter
169# $3 ... Path to DTB image
170fitimage_emit_section_dtb() {
171
Patrick Williamsaf48f632023-03-20 10:13:55 -0500172 dtb_csum="${FIT_HASH_ALG}"
173 dtb_sign_algo="${FIT_SIGN_ALG}"
174 dtb_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
Patrick Williamse479e922023-01-06 13:45:20 -0600175
Patrick Williamsaf48f632023-03-20 10:13:55 -0500176 dtb_loadline=""
177 dtb_ext=${DTB##*.}
178 if [ "${dtb_ext}" = "dtbo" ]; then
179 if [ -n "${UBOOT_DTBO_LOADADDRESS}" ]; then
180 dtb_loadline="load = <${UBOOT_DTBO_LOADADDRESS}>;"
181 fi
182 elif [ -n "${UBOOT_DTB_LOADADDRESS}" ]; then
183 dtb_loadline="load = <${UBOOT_DTB_LOADADDRESS}>;"
184 fi
185 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600186 fdt-$2 {
187 description = "Flattened Device Tree blob";
188 data = /incbin/("$3");
189 type = "flat_dt";
190 arch = "${UBOOT_ARCH}";
191 compression = "none";
192 $dtb_loadline
193 hash-1 {
194 algo = "$dtb_csum";
195 };
196 };
197EOF
198
Patrick Williamsaf48f632023-03-20 10:13:55 -0500199 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$dtb_sign_keyname" ] ; then
200 sed -i '$ d' $1
201 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600202 signature-1 {
203 algo = "$dtb_csum,$dtb_sign_algo";
204 key-name-hint = "$dtb_sign_keyname";
205 };
206 };
207EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500208 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600209}
210
211#
212# Emit the fitImage ITS u-boot script section
213#
214# $1 ... .its filename
215# $2 ... Image counter
216# $3 ... Path to boot script image
217fitimage_emit_section_boot_script() {
218
Patrick Williamsaf48f632023-03-20 10:13:55 -0500219 bootscr_csum="${FIT_HASH_ALG}"
220 bootscr_sign_algo="${FIT_SIGN_ALG}"
221 bootscr_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
Patrick Williamse479e922023-01-06 13:45:20 -0600222
223 cat << EOF >> $1
224 bootscr-$2 {
225 description = "U-boot script";
226 data = /incbin/("$3");
227 type = "script";
228 arch = "${UBOOT_ARCH}";
229 compression = "none";
230 hash-1 {
231 algo = "$bootscr_csum";
232 };
233 };
234EOF
235
Patrick Williamsaf48f632023-03-20 10:13:55 -0500236 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$bootscr_sign_keyname" ] ; then
237 sed -i '$ d' $1
238 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600239 signature-1 {
240 algo = "$bootscr_csum,$bootscr_sign_algo";
241 key-name-hint = "$bootscr_sign_keyname";
242 };
243 };
244EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500245 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600246}
247
248#
249# Emit the fitImage ITS setup section
250#
251# $1 ... .its filename
252# $2 ... Image counter
253# $3 ... Path to setup image
254fitimage_emit_section_setup() {
255
Patrick Williamsaf48f632023-03-20 10:13:55 -0500256 setup_csum="${FIT_HASH_ALG}"
Patrick Williamse479e922023-01-06 13:45:20 -0600257
Patrick Williamsaf48f632023-03-20 10:13:55 -0500258 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600259 setup-$2 {
260 description = "Linux setup.bin";
261 data = /incbin/("$3");
262 type = "x86_setup";
263 arch = "${UBOOT_ARCH}";
264 os = "linux";
265 compression = "none";
266 load = <0x00090000>;
267 entry = <0x00090000>;
268 hash-1 {
269 algo = "$setup_csum";
270 };
271 };
272EOF
273}
274
275#
276# Emit the fitImage ITS ramdisk section
277#
278# $1 ... .its filename
279# $2 ... Image counter
280# $3 ... Path to ramdisk image
281fitimage_emit_section_ramdisk() {
282
Patrick Williamsaf48f632023-03-20 10:13:55 -0500283 ramdisk_csum="${FIT_HASH_ALG}"
284 ramdisk_sign_algo="${FIT_SIGN_ALG}"
285 ramdisk_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
286 ramdisk_loadline=""
287 ramdisk_entryline=""
Patrick Williamse479e922023-01-06 13:45:20 -0600288
Patrick Williamsaf48f632023-03-20 10:13:55 -0500289 if [ -n "${UBOOT_RD_LOADADDRESS}" ]; then
290 ramdisk_loadline="load = <${UBOOT_RD_LOADADDRESS}>;"
291 fi
292 if [ -n "${UBOOT_RD_ENTRYPOINT}" ]; then
293 ramdisk_entryline="entry = <${UBOOT_RD_ENTRYPOINT}>;"
294 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600295
Patrick Williamsaf48f632023-03-20 10:13:55 -0500296 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600297 ramdisk-$2 {
298 description = "${INITRAMFS_IMAGE}";
299 data = /incbin/("$3");
300 type = "ramdisk";
301 arch = "${UBOOT_ARCH}";
302 os = "linux";
303 compression = "none";
304 $ramdisk_loadline
305 $ramdisk_entryline
306 hash-1 {
307 algo = "$ramdisk_csum";
308 };
309 };
310EOF
311
Patrick Williamsaf48f632023-03-20 10:13:55 -0500312 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$ramdisk_sign_keyname" ] ; then
313 sed -i '$ d' $1
314 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600315 signature-1 {
316 algo = "$ramdisk_csum,$ramdisk_sign_algo";
317 key-name-hint = "$ramdisk_sign_keyname";
318 };
319 };
320EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500321 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600322}
323
324#
325# echoes symlink destination if it points below directory
326#
327# $1 ... file that's a potential symlink
328# $2 ... expected parent directory
329symlink_points_below() {
Patrick Williamsaf48f632023-03-20 10:13:55 -0500330 file="$2/$1"
331 dir=$2
Patrick Williamse479e922023-01-06 13:45:20 -0600332
Patrick Williamsaf48f632023-03-20 10:13:55 -0500333 if ! [ -L "$file" ]; then
334 return
335 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600336
Patrick Williamsaf48f632023-03-20 10:13:55 -0500337 realpath="$(realpath --relative-to=$dir $file)"
338 if [ -z "${realpath%%../*}" ]; then
339 return
340 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600341
Patrick Williamsaf48f632023-03-20 10:13:55 -0500342 echo "$realpath"
Patrick Williamse479e922023-01-06 13:45:20 -0600343}
344
345#
346# Emit the fitImage ITS configuration section
347#
348# $1 ... .its filename
349# $2 ... Linux kernel ID
350# $3 ... DTB image name
351# $4 ... ramdisk ID
352# $5 ... u-boot script ID
353# $6 ... config ID
354# $7 ... default flag
355fitimage_emit_section_config() {
356
Patrick Williamsaf48f632023-03-20 10:13:55 -0500357 conf_csum="${FIT_HASH_ALG}"
358 conf_sign_algo="${FIT_SIGN_ALG}"
359 conf_padding_algo="${FIT_PAD_ALG}"
360 if [ "${UBOOT_SIGN_ENABLE}" = "1" ] ; then
361 conf_sign_keyname="${UBOOT_SIGN_KEYNAME}"
362 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600363
Patrick Williamsaf48f632023-03-20 10:13:55 -0500364 its_file="$1"
365 kernel_id="$2"
366 dtb_image="$3"
367 ramdisk_id="$4"
368 bootscr_id="$5"
369 config_id="$6"
370 default_flag="$7"
Patrick Williamse479e922023-01-06 13:45:20 -0600371
Patrick Williamsaf48f632023-03-20 10:13:55 -0500372 # Test if we have any DTBs at all
373 sep=""
374 conf_desc=""
375 conf_node="${FIT_CONF_PREFIX}"
376 kernel_line=""
377 fdt_line=""
378 ramdisk_line=""
379 bootscr_line=""
380 setup_line=""
381 default_line=""
Patrick Williamse479e922023-01-06 13:45:20 -0600382
Patrick Williamsaf48f632023-03-20 10:13:55 -0500383 dtb_image_sect=$(symlink_points_below $dtb_image "${EXTERNAL_KERNEL_DEVICETREE}")
384 if [ -z "$dtb_image_sect" ]; then
385 dtb_image_sect=$dtb_image
386 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600387
Patrick Williamsaf48f632023-03-20 10:13:55 -0500388 dtb_image=$(echo $dtb_image | tr '/' '_')
389 dtb_image_sect=$(echo "${dtb_image_sect}" | tr '/' '_')
Patrick Williamse479e922023-01-06 13:45:20 -0600390
Patrick Williamsaf48f632023-03-20 10:13:55 -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
Patrick Williamse479e922023-01-06 13:45:20 -0600398
Patrick Williamsaf48f632023-03-20 10:13:55 -0500399 if [ -n "$kernel_id" ]; then
400 conf_desc="Linux kernel"
401 sep=", "
402 kernel_line="kernel = \"kernel-$kernel_id\";"
403 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600404
Patrick Williamsaf48f632023-03-20 10:13:55 -0500405 if [ -n "$dtb_image" ]; then
406 conf_desc="$conf_desc${sep}FDT blob"
407 sep=", "
408 fdt_line="fdt = \"fdt-$dtb_image_sect\";"
409 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600410
Patrick Williamsaf48f632023-03-20 10:13:55 -0500411 if [ -n "$ramdisk_id" ]; then
412 conf_desc="$conf_desc${sep}ramdisk"
413 sep=", "
414 ramdisk_line="ramdisk = \"ramdisk-$ramdisk_id\";"
415 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600416
Patrick Williamsaf48f632023-03-20 10:13:55 -0500417 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
Patrick Williamse479e922023-01-06 13:45:20 -0600422
Patrick Williamsaf48f632023-03-20 10:13:55 -0500423 if [ -n "$config_id" ]; then
424 conf_desc="$conf_desc${sep}setup"
425 setup_line="setup = \"setup-$config_id\";"
426 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600427
Patrick Williamsaf48f632023-03-20 10:13:55 -0500428 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
432 default_line="default = \"${FIT_CONF_PREFIX}$dtb_image\";"
433 else
434 default_line="default = \"${FIT_CONF_PREFIX}$kernel_id\";"
435 fi
436 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600437
Patrick Williamsaf48f632023-03-20 10:13:55 -0500438 cat << EOF >> $its_file
Patrick Williamse479e922023-01-06 13:45:20 -0600439 $default_line
440 $conf_node {
441 description = "$default_flag $conf_desc";
442 $kernel_line
443 $fdt_line
444 $ramdisk_line
445 $bootscr_line
446 $setup_line
447 hash-1 {
448 algo = "$conf_csum";
449 };
450EOF
451
Patrick Williamsaf48f632023-03-20 10:13:55 -0500452 if [ -n "$conf_sign_keyname" ] ; then
Patrick Williamse479e922023-01-06 13:45:20 -0600453
Patrick Williamsaf48f632023-03-20 10:13:55 -0500454 sign_line="sign-images = "
455 sep=""
Patrick Williamse479e922023-01-06 13:45:20 -0600456
Patrick Williamsaf48f632023-03-20 10:13:55 -0500457 if [ -n "$kernel_id" ]; then
458 sign_line="$sign_line${sep}\"kernel\""
459 sep=", "
460 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600461
Patrick Williamsaf48f632023-03-20 10:13:55 -0500462 if [ -n "$dtb_image" ]; then
463 sign_line="$sign_line${sep}\"fdt\""
464 sep=", "
465 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600466
Patrick Williamsaf48f632023-03-20 10:13:55 -0500467 if [ -n "$ramdisk_id" ]; then
468 sign_line="$sign_line${sep}\"ramdisk\""
469 sep=", "
470 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600471
Patrick Williamsaf48f632023-03-20 10:13:55 -0500472 if [ -n "$bootscr_id" ]; then
473 sign_line="$sign_line${sep}\"bootscr\""
474 sep=", "
475 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600476
Patrick Williamsaf48f632023-03-20 10:13:55 -0500477 if [ -n "$config_id" ]; then
478 sign_line="$sign_line${sep}\"setup\""
479 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600480
Patrick Williamsaf48f632023-03-20 10:13:55 -0500481 sign_line="$sign_line;"
Patrick Williamse479e922023-01-06 13:45:20 -0600482
Patrick Williamsaf48f632023-03-20 10:13:55 -0500483 cat << EOF >> $its_file
Patrick Williamse479e922023-01-06 13:45:20 -0600484 signature-1 {
485 algo = "$conf_csum,$conf_sign_algo";
486 key-name-hint = "$conf_sign_keyname";
487 padding = "$conf_padding_algo";
488 $sign_line
489 };
490EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500491 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600492
Patrick Williamsaf48f632023-03-20 10:13:55 -0500493 cat << EOF >> $its_file
Patrick Williamse479e922023-01-06 13:45:20 -0600494 };
495EOF
496}
497
498#
499# Assemble fitImage
500#
501# $1 ... .its filename
502# $2 ... fitImage name
503# $3 ... include ramdisk
504fitimage_assemble() {
Patrick Williamsaf48f632023-03-20 10:13:55 -0500505 kernelcount=1
506 dtbcount=""
507 DTBS=""
508 ramdiskcount=$3
509 setupcount=""
510 bootscr_id=""
511 rm -f $1 ${KERNEL_OUTPUT_DIR}/$2
Patrick Williamse479e922023-01-06 13:45:20 -0600512
Patrick Williamsaf48f632023-03-20 10:13:55 -0500513 if [ -n "${UBOOT_SIGN_IMG_KEYNAME}" -a "${UBOOT_SIGN_KEYNAME}" = "${UBOOT_SIGN_IMG_KEYNAME}" ]; then
514 bbfatal "Keys used to sign images and configuration nodes must be different."
515 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600516
Patrick Williamsaf48f632023-03-20 10:13:55 -0500517 fitimage_emit_fit_header $1
Patrick Williamse479e922023-01-06 13:45:20 -0600518
Patrick Williamsaf48f632023-03-20 10:13:55 -0500519 #
520 # Step 1: Prepare a kernel image section.
521 #
522 fitimage_emit_section_maint $1 imagestart
Patrick Williamse479e922023-01-06 13:45:20 -0600523
Patrick Williamsaf48f632023-03-20 10:13:55 -0500524 uboot_prep_kimage
525 fitimage_emit_section_kernel $1 $kernelcount linux.bin "$linux_comp"
Patrick Williamse479e922023-01-06 13:45:20 -0600526
Patrick Williamsaf48f632023-03-20 10:13:55 -0500527 #
528 # Step 2: Prepare a DTB image section
529 #
Patrick Williamse479e922023-01-06 13:45:20 -0600530
Patrick Williamsaf48f632023-03-20 10:13:55 -0500531 if [ -n "${KERNEL_DEVICETREE}" ]; then
532 dtbcount=1
533 for DTB in ${KERNEL_DEVICETREE}; do
534 if echo $DTB | grep -q '/dts/'; then
535 bbwarn "$DTB contains the full path to the the dts file, but only the dtb name should be used."
536 DTB=`basename $DTB | sed 's,\.dts$,.dtb,g'`
537 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600538
Patrick Williamsaf48f632023-03-20 10:13:55 -0500539 # Skip ${DTB} if it's also provided in ${EXTERNAL_KERNEL_DEVICETREE}
540 if [ -n "${EXTERNAL_KERNEL_DEVICETREE}" ] && [ -s ${EXTERNAL_KERNEL_DEVICETREE}/${DTB} ]; then
541 continue
542 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600543
Andrew Geissler0cb5bb52023-09-14 08:37:27 -0400544 # For non-vendored DTBs, we need to strip off the vendor path.
545 if "${@'false' if oe.types.boolean(d.getVar('KERNEL_DTBVENDORED')) else 'true'}"; then
546 DTB=`basename $DTB`
547 fi
548
Patrick Williamsaf48f632023-03-20 10:13:55 -0500549 DTB_PATH="${KERNEL_OUTPUT_DIR}/dts/$DTB"
550 if [ ! -e "$DTB_PATH" ]; then
551 DTB_PATH="${KERNEL_OUTPUT_DIR}/$DTB"
552 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600553
Patrick Williamsaf48f632023-03-20 10:13:55 -0500554 # Skip DTB if we've picked it up previously
555 echo "$DTBS" | tr ' ' '\n' | grep -xq "$DTB" && continue
Patrick Williamse479e922023-01-06 13:45:20 -0600556
Patrick Williamsaf48f632023-03-20 10:13:55 -0500557 DTBS="$DTBS $DTB"
558 DTB=$(echo $DTB | tr '/' '_')
559 fitimage_emit_section_dtb $1 $DTB $DTB_PATH
560 done
561 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600562
Patrick Williamsaf48f632023-03-20 10:13:55 -0500563 if [ -n "${EXTERNAL_KERNEL_DEVICETREE}" ]; then
564 dtbcount=1
565 for DTB in $(find "${EXTERNAL_KERNEL_DEVICETREE}" \( -name '*.dtb' -o -name '*.dtbo' \) -printf '%P\n' | sort); do
566 # Skip DTB if we've picked it up previously
567 echo "$DTBS" | tr ' ' '\n' | grep -xq "$DTB" && continue
Patrick Williamse479e922023-01-06 13:45:20 -0600568
Patrick Williamsaf48f632023-03-20 10:13:55 -0500569 DTBS="$DTBS $DTB"
Patrick Williamse479e922023-01-06 13:45:20 -0600570
Patrick Williamsaf48f632023-03-20 10:13:55 -0500571 # Also skip if a symlink. We'll later have each config section point at it
572 [ $(symlink_points_below $DTB "${EXTERNAL_KERNEL_DEVICETREE}") ] && continue
Patrick Williamse479e922023-01-06 13:45:20 -0600573
Patrick Williamsaf48f632023-03-20 10:13:55 -0500574 DTB=$(echo $DTB | tr '/' '_')
575 fitimage_emit_section_dtb $1 $DTB "${EXTERNAL_KERNEL_DEVICETREE}/$DTB"
576 done
577 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600578
Patrick Williamsaf48f632023-03-20 10:13:55 -0500579 #
580 # Step 3: Prepare a u-boot script section
581 #
Patrick Williamse479e922023-01-06 13:45:20 -0600582
Patrick Williamsaf48f632023-03-20 10:13:55 -0500583 if [ -n "${UBOOT_ENV}" ] && [ -d "${STAGING_DIR_HOST}/boot" ]; then
584 if [ -e "${STAGING_DIR_HOST}/boot/${UBOOT_ENV_BINARY}" ]; then
585 cp ${STAGING_DIR_HOST}/boot/${UBOOT_ENV_BINARY} ${B}
586 bootscr_id="${UBOOT_ENV_BINARY}"
587 fitimage_emit_section_boot_script $1 "$bootscr_id" ${UBOOT_ENV_BINARY}
588 else
589 bbwarn "${STAGING_DIR_HOST}/boot/${UBOOT_ENV_BINARY} not found."
590 fi
591 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600592
Patrick Williamsaf48f632023-03-20 10:13:55 -0500593 #
594 # Step 4: Prepare a setup section. (For x86)
595 #
596 if [ -e ${KERNEL_OUTPUT_DIR}/setup.bin ]; then
597 setupcount=1
598 fitimage_emit_section_setup $1 $setupcount ${KERNEL_OUTPUT_DIR}/setup.bin
599 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600600
Patrick Williamsaf48f632023-03-20 10:13:55 -0500601 #
602 # Step 5: Prepare a ramdisk section.
603 #
604 if [ "x${ramdiskcount}" = "x1" ] && [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ]; then
605 # Find and use the first initramfs image archive type we find
606 found=
607 for img in ${FIT_SUPPORTED_INITRAMFS_FSTYPES}; do
608 initramfs_path="${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img"
609 if [ -e "$initramfs_path" ]; then
610 bbnote "Found initramfs image: $initramfs_path"
611 found=true
612 fitimage_emit_section_ramdisk $1 "$ramdiskcount" "$initramfs_path"
613 break
614 else
615 bbnote "Did not find initramfs image: $initramfs_path"
616 fi
617 done
Patrick Williamse479e922023-01-06 13:45:20 -0600618
619 if [ -z "$found" ] && [ -e ${INITRAMFS_IMAGE} ]; then
620 found=true
621 bbnote "Found initramfs image: ${INITRAMFS_IMAGE}"
622 fitimage_emit_section_ramdisk $1 "$ramdiskcount" "${INITRAMFS_IMAGE}"
623 fi
624
Patrick Williamsaf48f632023-03-20 10:13:55 -0500625 if [ -z "$found" ]; then
626 bbfatal "Could not find a valid initramfs type for ${INITRAMFS_IMAGE_NAME}, the supported types are: ${FIT_SUPPORTED_INITRAMFS_FSTYPES}"
627 fi
628 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600629
Patrick Williamsaf48f632023-03-20 10:13:55 -0500630 fitimage_emit_section_maint $1 sectend
Patrick Williamse479e922023-01-06 13:45:20 -0600631
Patrick Williamsaf48f632023-03-20 10:13:55 -0500632 # Force the first Kernel and DTB in the default config
633 kernelcount=1
634 if [ -n "$dtbcount" ]; then
635 dtbcount=1
636 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600637
Patrick Williamsaf48f632023-03-20 10:13:55 -0500638 #
639 # Step 6: Prepare a configurations section
640 #
641 fitimage_emit_section_maint $1 confstart
Patrick Williamse479e922023-01-06 13:45:20 -0600642
Patrick Williamsaf48f632023-03-20 10:13:55 -0500643 # kernel-fitimage.bbclass currently only supports a single kernel (no less or
644 # more) to be added to the FIT image along with 0 or more device trees and
645 # 0 or 1 ramdisk.
Patrick Williamse479e922023-01-06 13:45:20 -0600646 # It is also possible to include an initramfs bundle (kernel and rootfs in one binary)
647 # When the initramfs bundle is used ramdisk is disabled.
Patrick Williamsaf48f632023-03-20 10:13:55 -0500648 # If a device tree is to be part of the FIT image, then select
649 # the default configuration to be used is based on the dtbcount. If there is
650 # no dtb present than select the default configuation to be based on
651 # the kernelcount.
652 if [ -n "$DTBS" ]; then
653 i=1
654 for DTB in ${DTBS}; do
655 dtb_ext=${DTB##*.}
656 if [ "$dtb_ext" = "dtbo" ]; then
657 fitimage_emit_section_config $1 "" "$DTB" "" "$bootscr_id" "" "`expr $i = $dtbcount`"
658 else
659 fitimage_emit_section_config $1 $kernelcount "$DTB" "$ramdiskcount" "$bootscr_id" "$setupcount" "`expr $i = $dtbcount`"
660 fi
661 i=`expr $i + 1`
662 done
663 else
664 defaultconfigcount=1
665 fitimage_emit_section_config $1 $kernelcount "" "$ramdiskcount" "$bootscr_id" "$setupcount" $defaultconfigcount
666 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600667
Patrick Williamsaf48f632023-03-20 10:13:55 -0500668 fitimage_emit_section_maint $1 sectend
Patrick Williamse479e922023-01-06 13:45:20 -0600669
Patrick Williamsaf48f632023-03-20 10:13:55 -0500670 fitimage_emit_section_maint $1 fitend
Patrick Williamse479e922023-01-06 13:45:20 -0600671
Patrick Williamsaf48f632023-03-20 10:13:55 -0500672 #
673 # Step 7: Assemble the image
674 #
675 ${UBOOT_MKIMAGE} \
676 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
677 -f $1 \
678 ${KERNEL_OUTPUT_DIR}/$2
Patrick Williamse479e922023-01-06 13:45:20 -0600679
Patrick Williamsaf48f632023-03-20 10:13:55 -0500680 #
681 # Step 8: Sign the image
682 #
683 if [ "x${UBOOT_SIGN_ENABLE}" = "x1" ] ; then
684 ${UBOOT_MKIMAGE_SIGN} \
685 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
686 -F -k "${UBOOT_SIGN_KEYDIR}" \
687 -r ${KERNEL_OUTPUT_DIR}/$2 \
688 ${UBOOT_MKIMAGE_SIGN_ARGS}
689 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600690}