blob: d0c51831ae2ce3e9684dcb835b48f78743eeb9e9 [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
Patrick Williamsaf48f632023-03-20 10:13:55 -0500544 DTB_PATH="${KERNEL_OUTPUT_DIR}/dts/$DTB"
545 if [ ! -e "$DTB_PATH" ]; then
546 DTB_PATH="${KERNEL_OUTPUT_DIR}/$DTB"
547 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600548
Patrick Williamsaf48f632023-03-20 10:13:55 -0500549 # Skip DTB if we've picked it up previously
550 echo "$DTBS" | tr ' ' '\n' | grep -xq "$DTB" && continue
Patrick Williamse479e922023-01-06 13:45:20 -0600551
Patrick Williamsaf48f632023-03-20 10:13:55 -0500552 DTBS="$DTBS $DTB"
553 DTB=$(echo $DTB | tr '/' '_')
554 fitimage_emit_section_dtb $1 $DTB $DTB_PATH
555 done
556 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600557
Patrick Williamsaf48f632023-03-20 10:13:55 -0500558 if [ -n "${EXTERNAL_KERNEL_DEVICETREE}" ]; then
559 dtbcount=1
560 for DTB in $(find "${EXTERNAL_KERNEL_DEVICETREE}" \( -name '*.dtb' -o -name '*.dtbo' \) -printf '%P\n' | sort); do
561 # Skip DTB if we've picked it up previously
562 echo "$DTBS" | tr ' ' '\n' | grep -xq "$DTB" && continue
Patrick Williamse479e922023-01-06 13:45:20 -0600563
Patrick Williamsaf48f632023-03-20 10:13:55 -0500564 DTBS="$DTBS $DTB"
Patrick Williamse479e922023-01-06 13:45:20 -0600565
Patrick Williamsaf48f632023-03-20 10:13:55 -0500566 # Also skip if a symlink. We'll later have each config section point at it
567 [ $(symlink_points_below $DTB "${EXTERNAL_KERNEL_DEVICETREE}") ] && continue
Patrick Williamse479e922023-01-06 13:45:20 -0600568
Patrick Williamsaf48f632023-03-20 10:13:55 -0500569 DTB=$(echo $DTB | tr '/' '_')
570 fitimage_emit_section_dtb $1 $DTB "${EXTERNAL_KERNEL_DEVICETREE}/$DTB"
571 done
572 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600573
Patrick Williamsaf48f632023-03-20 10:13:55 -0500574 #
575 # Step 3: Prepare a u-boot script section
576 #
Patrick Williamse479e922023-01-06 13:45:20 -0600577
Patrick Williamsaf48f632023-03-20 10:13:55 -0500578 if [ -n "${UBOOT_ENV}" ] && [ -d "${STAGING_DIR_HOST}/boot" ]; then
579 if [ -e "${STAGING_DIR_HOST}/boot/${UBOOT_ENV_BINARY}" ]; then
580 cp ${STAGING_DIR_HOST}/boot/${UBOOT_ENV_BINARY} ${B}
581 bootscr_id="${UBOOT_ENV_BINARY}"
582 fitimage_emit_section_boot_script $1 "$bootscr_id" ${UBOOT_ENV_BINARY}
583 else
584 bbwarn "${STAGING_DIR_HOST}/boot/${UBOOT_ENV_BINARY} not found."
585 fi
586 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600587
Patrick Williamsaf48f632023-03-20 10:13:55 -0500588 #
589 # Step 4: Prepare a setup section. (For x86)
590 #
591 if [ -e ${KERNEL_OUTPUT_DIR}/setup.bin ]; then
592 setupcount=1
593 fitimage_emit_section_setup $1 $setupcount ${KERNEL_OUTPUT_DIR}/setup.bin
594 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600595
Patrick Williamsaf48f632023-03-20 10:13:55 -0500596 #
597 # Step 5: Prepare a ramdisk section.
598 #
599 if [ "x${ramdiskcount}" = "x1" ] && [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ]; then
600 # Find and use the first initramfs image archive type we find
601 found=
602 for img in ${FIT_SUPPORTED_INITRAMFS_FSTYPES}; do
603 initramfs_path="${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img"
604 if [ -e "$initramfs_path" ]; then
605 bbnote "Found initramfs image: $initramfs_path"
606 found=true
607 fitimage_emit_section_ramdisk $1 "$ramdiskcount" "$initramfs_path"
608 break
609 else
610 bbnote "Did not find initramfs image: $initramfs_path"
611 fi
612 done
Patrick Williamse479e922023-01-06 13:45:20 -0600613
614 if [ -z "$found" ] && [ -e ${INITRAMFS_IMAGE} ]; then
615 found=true
616 bbnote "Found initramfs image: ${INITRAMFS_IMAGE}"
617 fitimage_emit_section_ramdisk $1 "$ramdiskcount" "${INITRAMFS_IMAGE}"
618 fi
619
Patrick Williamsaf48f632023-03-20 10:13:55 -0500620 if [ -z "$found" ]; then
621 bbfatal "Could not find a valid initramfs type for ${INITRAMFS_IMAGE_NAME}, the supported types are: ${FIT_SUPPORTED_INITRAMFS_FSTYPES}"
622 fi
623 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600624
Patrick Williamsaf48f632023-03-20 10:13:55 -0500625 fitimage_emit_section_maint $1 sectend
Patrick Williamse479e922023-01-06 13:45:20 -0600626
Patrick Williamsaf48f632023-03-20 10:13:55 -0500627 # Force the first Kernel and DTB in the default config
628 kernelcount=1
629 if [ -n "$dtbcount" ]; then
630 dtbcount=1
631 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600632
Patrick Williamsaf48f632023-03-20 10:13:55 -0500633 #
634 # Step 6: Prepare a configurations section
635 #
636 fitimage_emit_section_maint $1 confstart
Patrick Williamse479e922023-01-06 13:45:20 -0600637
Patrick Williamsaf48f632023-03-20 10:13:55 -0500638 # kernel-fitimage.bbclass currently only supports a single kernel (no less or
639 # more) to be added to the FIT image along with 0 or more device trees and
640 # 0 or 1 ramdisk.
Patrick Williamse479e922023-01-06 13:45:20 -0600641 # It is also possible to include an initramfs bundle (kernel and rootfs in one binary)
642 # When the initramfs bundle is used ramdisk is disabled.
Patrick Williamsaf48f632023-03-20 10:13:55 -0500643 # If a device tree is to be part of the FIT image, then select
644 # the default configuration to be used is based on the dtbcount. If there is
645 # no dtb present than select the default configuation to be based on
646 # the kernelcount.
647 if [ -n "$DTBS" ]; then
648 i=1
649 for DTB in ${DTBS}; do
650 dtb_ext=${DTB##*.}
651 if [ "$dtb_ext" = "dtbo" ]; then
652 fitimage_emit_section_config $1 "" "$DTB" "" "$bootscr_id" "" "`expr $i = $dtbcount`"
653 else
654 fitimage_emit_section_config $1 $kernelcount "$DTB" "$ramdiskcount" "$bootscr_id" "$setupcount" "`expr $i = $dtbcount`"
655 fi
656 i=`expr $i + 1`
657 done
658 else
659 defaultconfigcount=1
660 fitimage_emit_section_config $1 $kernelcount "" "$ramdiskcount" "$bootscr_id" "$setupcount" $defaultconfigcount
661 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600662
Patrick Williamsaf48f632023-03-20 10:13:55 -0500663 fitimage_emit_section_maint $1 sectend
Patrick Williamse479e922023-01-06 13:45:20 -0600664
Patrick Williamsaf48f632023-03-20 10:13:55 -0500665 fitimage_emit_section_maint $1 fitend
Patrick Williamse479e922023-01-06 13:45:20 -0600666
Patrick Williamsaf48f632023-03-20 10:13:55 -0500667 #
668 # Step 7: Assemble the image
669 #
670 ${UBOOT_MKIMAGE} \
671 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
672 -f $1 \
673 ${KERNEL_OUTPUT_DIR}/$2
Patrick Williamse479e922023-01-06 13:45:20 -0600674
Patrick Williamsaf48f632023-03-20 10:13:55 -0500675 #
676 # Step 8: Sign the image
677 #
678 if [ "x${UBOOT_SIGN_ENABLE}" = "x1" ] ; then
679 ${UBOOT_MKIMAGE_SIGN} \
680 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
681 -F -k "${UBOOT_SIGN_KEYDIR}" \
682 -r ${KERNEL_OUTPUT_DIR}/$2 \
683 ${UBOOT_MKIMAGE_SIGN_ARGS}
684 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600685}