blob: f37aa0271f0240d606d2c407e8bad03f893eb8e4 [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
Tim Lee650d9dc2024-01-30 13:45:33 +08009FIT_KERNEL_COMP_ALG ?= "none"
10FIT_KERNEL_COMP_ALG_EXTENSION ?= ""
11
Patrick Williamse479e922023-01-06 13:45:20 -060012do_image_cpio[depends] += "virtual/kernel:do_deploy"
13
14run_assemble_fitimage() {
15 export linux_comp="none"
16 fitimage_assemble $1.its $1.fitImage 1
17
18 # The fitimage_assemble puts the image into DEPLOY_DIR_NAME due to
19 # KERNEL_OUTPUT_DIR, but we really want it still in ${IMGDEPLOYDIR}, so
20 # move it.
21 mv ${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE}.fitImage .
22 # Delete the spurious linux.bin created by our stubbed uboot_prep_kimage.
23 rm linux.bin
24}
25
26UBOOT_MKIMAGE_KERNEL_TYPE ?= "kernel"
27uboot_prep_kimage() {
28 cp ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE} linux.bin
Tim Lee650d9dc2024-01-30 13:45:33 +080029
30 if [ "${linux_comp}" != "none" ] ; then
31 linux_suffix="${FIT_KERNEL_COMP_ALG_EXTENSION}"
32 if [ "${linux_comp}" = "gzip" ] ; then
33 gzip -9 linux.bin
34 elif [ "${linux_comp}" = "lzo" ] ; then
35 lzop -9 linux.bin
36 fi
37 mv -f "linux.bin${linux_suffix}" linux.bin
38 fi
Patrick Williamse479e922023-01-06 13:45:20 -060039}
40
41DEPENDS:append = " u-boot-tools-native dtc-native virtual/${TARGET_PREFIX}binutils"
42
43# Description string
44FIT_DESC ?= "Kernel fitImage for ${DISTRO_NAME}/${PV}/${MACHINE}"
45
46# Kernel fitImage Hash Algo
47FIT_HASH_ALG ?= "sha256"
48
49# Kernel fitImage Signature Algo
50FIT_SIGN_ALG ?= "rsa2048"
51
52# Kernel / U-Boot fitImage Padding Algo
53FIT_PAD_ALG ?= "pkcs-1.5"
54
55# Generate keys for signing Kernel fitImage
56FIT_GENERATE_KEYS ?= "0"
57
58# Size of private keys in number of bits
59FIT_SIGN_NUMBITS ?= "2048"
60
61# args to openssl genrsa (Default is just the public exponent)
62FIT_KEY_GENRSA_ARGS ?= "-F4"
63
64# args to openssl req (Default is -batch for non interactive mode and
65# -new for new certificate)
66FIT_KEY_REQ_ARGS ?= "-batch -new"
67
68# Standard format for public key certificate
69FIT_KEY_SIGN_PKCS ?= "-x509"
70
71# Sign individual images as well
72FIT_SIGN_INDIVIDUAL ?= "0"
73
74FIT_CONF_PREFIX ?= "conf-"
75FIT_CONF_PREFIX[doc] = "Prefix to use for FIT configuration node name"
76
77FIT_SUPPORTED_INITRAMFS_FSTYPES ?= "cpio.lz4 cpio.lzo cpio.lzma cpio.xz cpio.zst cpio.gz ext2.gz cpio"
78
79#
80# Emit the fitImage ITS header
81#
82# $1 ... .its filename
83fitimage_emit_fit_header() {
Patrick Williamsaf48f632023-03-20 10:13:55 -050084 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -060085/dts-v1/;
86
87/ {
88 description = "${FIT_DESC}";
89 #address-cells = <1>;
90EOF
91}
92
93#
94# Emit the fitImage section bits
95#
96# $1 ... .its filename
97# $2 ... Section bit type: imagestart - image section start
98# confstart - configuration section start
99# sectend - section end
100# fitend - fitimage end
101#
102fitimage_emit_section_maint() {
Patrick Williamsaf48f632023-03-20 10:13:55 -0500103 case $2 in
104 imagestart)
105 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600106
107 images {
108EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500109 ;;
110 confstart)
111 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600112
113 configurations {
114EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500115 ;;
116 sectend)
117 cat << EOF >> $1
118 };
Patrick Williamse479e922023-01-06 13:45:20 -0600119EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500120 ;;
121 fitend)
122 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600123};
124EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500125 ;;
126 esac
Patrick Williamse479e922023-01-06 13:45:20 -0600127}
128
129
130#
131# Emit the fitImage ITS kernel section
132#
133# $1 ... .its filename
134# $2 ... Image counter
135# $3 ... Path to kernel image
136# $4 ... Compression type
137fitimage_emit_section_kernel() {
138
Patrick Williamsaf48f632023-03-20 10:13:55 -0500139 kernel_csum="${FIT_HASH_ALG}"
140 kernel_sign_algo="${FIT_SIGN_ALG}"
141 kernel_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
Patrick Williamse479e922023-01-06 13:45:20 -0600142
Patrick Williamsaf48f632023-03-20 10:13:55 -0500143 ENTRYPOINT="${UBOOT_ENTRYPOINT}"
144 if [ -n "${UBOOT_ENTRYSYMBOL}" ]; then
145 ENTRYPOINT=`${HOST_PREFIX}nm vmlinux | \
146 awk '$3=="${UBOOT_ENTRYSYMBOL}" {print "0x"$1;exit}'`
147 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600148
Patrick Williamsaf48f632023-03-20 10:13:55 -0500149 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600150 kernel-$2 {
151 description = "Linux kernel";
152 data = /incbin/("$3");
153 type = "${UBOOT_MKIMAGE_KERNEL_TYPE}";
154 arch = "${UBOOT_ARCH}";
155 os = "linux";
156 compression = "$4";
157 load = <${UBOOT_LOADADDRESS}>;
158 entry = <$ENTRYPOINT>;
159 hash-1 {
160 algo = "$kernel_csum";
161 };
162 };
163EOF
164
Patrick Williamsaf48f632023-03-20 10:13:55 -0500165 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$kernel_sign_keyname" ] ; then
166 sed -i '$ d' $1
167 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600168 signature-1 {
169 algo = "$kernel_csum,$kernel_sign_algo";
170 key-name-hint = "$kernel_sign_keyname";
171 };
172 };
173EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500174 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600175}
176
177#
178# Emit the fitImage ITS DTB section
179#
180# $1 ... .its filename
181# $2 ... Image counter
182# $3 ... Path to DTB image
183fitimage_emit_section_dtb() {
184
Patrick Williamsaf48f632023-03-20 10:13:55 -0500185 dtb_csum="${FIT_HASH_ALG}"
186 dtb_sign_algo="${FIT_SIGN_ALG}"
187 dtb_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
Patrick Williamse479e922023-01-06 13:45:20 -0600188
Patrick Williamsaf48f632023-03-20 10:13:55 -0500189 dtb_loadline=""
190 dtb_ext=${DTB##*.}
191 if [ "${dtb_ext}" = "dtbo" ]; then
192 if [ -n "${UBOOT_DTBO_LOADADDRESS}" ]; then
193 dtb_loadline="load = <${UBOOT_DTBO_LOADADDRESS}>;"
194 fi
195 elif [ -n "${UBOOT_DTB_LOADADDRESS}" ]; then
196 dtb_loadline="load = <${UBOOT_DTB_LOADADDRESS}>;"
197 fi
198 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600199 fdt-$2 {
200 description = "Flattened Device Tree blob";
201 data = /incbin/("$3");
202 type = "flat_dt";
203 arch = "${UBOOT_ARCH}";
204 compression = "none";
205 $dtb_loadline
206 hash-1 {
207 algo = "$dtb_csum";
208 };
209 };
210EOF
211
Patrick Williamsaf48f632023-03-20 10:13:55 -0500212 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$dtb_sign_keyname" ] ; then
213 sed -i '$ d' $1
214 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600215 signature-1 {
216 algo = "$dtb_csum,$dtb_sign_algo";
217 key-name-hint = "$dtb_sign_keyname";
218 };
219 };
220EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500221 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600222}
223
224#
225# Emit the fitImage ITS u-boot script section
226#
227# $1 ... .its filename
228# $2 ... Image counter
229# $3 ... Path to boot script image
230fitimage_emit_section_boot_script() {
231
Patrick Williamsaf48f632023-03-20 10:13:55 -0500232 bootscr_csum="${FIT_HASH_ALG}"
233 bootscr_sign_algo="${FIT_SIGN_ALG}"
234 bootscr_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
Patrick Williamse479e922023-01-06 13:45:20 -0600235
236 cat << EOF >> $1
237 bootscr-$2 {
238 description = "U-boot script";
239 data = /incbin/("$3");
240 type = "script";
241 arch = "${UBOOT_ARCH}";
242 compression = "none";
243 hash-1 {
244 algo = "$bootscr_csum";
245 };
246 };
247EOF
248
Patrick Williamsaf48f632023-03-20 10:13:55 -0500249 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$bootscr_sign_keyname" ] ; then
250 sed -i '$ d' $1
251 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600252 signature-1 {
253 algo = "$bootscr_csum,$bootscr_sign_algo";
254 key-name-hint = "$bootscr_sign_keyname";
255 };
256 };
257EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500258 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600259}
260
261#
262# Emit the fitImage ITS setup section
263#
264# $1 ... .its filename
265# $2 ... Image counter
266# $3 ... Path to setup image
267fitimage_emit_section_setup() {
268
Patrick Williamsaf48f632023-03-20 10:13:55 -0500269 setup_csum="${FIT_HASH_ALG}"
Patrick Williamse479e922023-01-06 13:45:20 -0600270
Patrick Williamsaf48f632023-03-20 10:13:55 -0500271 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600272 setup-$2 {
273 description = "Linux setup.bin";
274 data = /incbin/("$3");
275 type = "x86_setup";
276 arch = "${UBOOT_ARCH}";
277 os = "linux";
278 compression = "none";
279 load = <0x00090000>;
280 entry = <0x00090000>;
281 hash-1 {
282 algo = "$setup_csum";
283 };
284 };
285EOF
286}
287
288#
289# Emit the fitImage ITS ramdisk section
290#
291# $1 ... .its filename
292# $2 ... Image counter
293# $3 ... Path to ramdisk image
294fitimage_emit_section_ramdisk() {
295
Patrick Williamsaf48f632023-03-20 10:13:55 -0500296 ramdisk_csum="${FIT_HASH_ALG}"
297 ramdisk_sign_algo="${FIT_SIGN_ALG}"
298 ramdisk_sign_keyname="${UBOOT_SIGN_IMG_KEYNAME}"
299 ramdisk_loadline=""
300 ramdisk_entryline=""
Patrick Williamse479e922023-01-06 13:45:20 -0600301
Patrick Williamsaf48f632023-03-20 10:13:55 -0500302 if [ -n "${UBOOT_RD_LOADADDRESS}" ]; then
303 ramdisk_loadline="load = <${UBOOT_RD_LOADADDRESS}>;"
304 fi
305 if [ -n "${UBOOT_RD_ENTRYPOINT}" ]; then
306 ramdisk_entryline="entry = <${UBOOT_RD_ENTRYPOINT}>;"
307 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600308
Patrick Williamsaf48f632023-03-20 10:13:55 -0500309 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600310 ramdisk-$2 {
311 description = "${INITRAMFS_IMAGE}";
312 data = /incbin/("$3");
313 type = "ramdisk";
314 arch = "${UBOOT_ARCH}";
315 os = "linux";
316 compression = "none";
317 $ramdisk_loadline
318 $ramdisk_entryline
319 hash-1 {
320 algo = "$ramdisk_csum";
321 };
322 };
323EOF
324
Patrick Williamsaf48f632023-03-20 10:13:55 -0500325 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a "${FIT_SIGN_INDIVIDUAL}" = "1" -a -n "$ramdisk_sign_keyname" ] ; then
326 sed -i '$ d' $1
327 cat << EOF >> $1
Patrick Williamse479e922023-01-06 13:45:20 -0600328 signature-1 {
329 algo = "$ramdisk_csum,$ramdisk_sign_algo";
330 key-name-hint = "$ramdisk_sign_keyname";
331 };
332 };
333EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500334 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600335}
336
337#
338# echoes symlink destination if it points below directory
339#
340# $1 ... file that's a potential symlink
341# $2 ... expected parent directory
342symlink_points_below() {
Patrick Williamsaf48f632023-03-20 10:13:55 -0500343 file="$2/$1"
344 dir=$2
Patrick Williamse479e922023-01-06 13:45:20 -0600345
Patrick Williamsaf48f632023-03-20 10:13:55 -0500346 if ! [ -L "$file" ]; then
347 return
348 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600349
Patrick Williamsaf48f632023-03-20 10:13:55 -0500350 realpath="$(realpath --relative-to=$dir $file)"
351 if [ -z "${realpath%%../*}" ]; then
352 return
353 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600354
Patrick Williamsaf48f632023-03-20 10:13:55 -0500355 echo "$realpath"
Patrick Williamse479e922023-01-06 13:45:20 -0600356}
357
358#
359# Emit the fitImage ITS configuration section
360#
361# $1 ... .its filename
362# $2 ... Linux kernel ID
363# $3 ... DTB image name
364# $4 ... ramdisk ID
365# $5 ... u-boot script ID
366# $6 ... config ID
367# $7 ... default flag
368fitimage_emit_section_config() {
369
Patrick Williamsaf48f632023-03-20 10:13:55 -0500370 conf_csum="${FIT_HASH_ALG}"
371 conf_sign_algo="${FIT_SIGN_ALG}"
372 conf_padding_algo="${FIT_PAD_ALG}"
373 if [ "${UBOOT_SIGN_ENABLE}" = "1" ] ; then
374 conf_sign_keyname="${UBOOT_SIGN_KEYNAME}"
375 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600376
Patrick Williamsaf48f632023-03-20 10:13:55 -0500377 its_file="$1"
378 kernel_id="$2"
379 dtb_image="$3"
380 ramdisk_id="$4"
381 bootscr_id="$5"
382 config_id="$6"
383 default_flag="$7"
Patrick Williamse479e922023-01-06 13:45:20 -0600384
Patrick Williamsaf48f632023-03-20 10:13:55 -0500385 # Test if we have any DTBs at all
386 sep=""
387 conf_desc=""
388 conf_node="${FIT_CONF_PREFIX}"
389 kernel_line=""
390 fdt_line=""
391 ramdisk_line=""
392 bootscr_line=""
393 setup_line=""
394 default_line=""
Patrick Williamse479e922023-01-06 13:45:20 -0600395
Patrick Williamsaf48f632023-03-20 10:13:55 -0500396 dtb_image_sect=$(symlink_points_below $dtb_image "${EXTERNAL_KERNEL_DEVICETREE}")
397 if [ -z "$dtb_image_sect" ]; then
398 dtb_image_sect=$dtb_image
399 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600400
Patrick Williamsaf48f632023-03-20 10:13:55 -0500401 dtb_image=$(echo $dtb_image | tr '/' '_')
402 dtb_image_sect=$(echo "${dtb_image_sect}" | tr '/' '_')
Patrick Williamse479e922023-01-06 13:45:20 -0600403
Patrick Williamsaf48f632023-03-20 10:13:55 -0500404 # conf node name is selected based on dtb ID if it is present,
405 # otherwise its selected based on kernel ID
406 if [ -n "$dtb_image" ]; then
407 conf_node=$conf_node$dtb_image
408 else
409 conf_node=$conf_node$kernel_id
410 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600411
Patrick Williamsaf48f632023-03-20 10:13:55 -0500412 if [ -n "$kernel_id" ]; then
413 conf_desc="Linux kernel"
414 sep=", "
415 kernel_line="kernel = \"kernel-$kernel_id\";"
416 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600417
Patrick Williamsaf48f632023-03-20 10:13:55 -0500418 if [ -n "$dtb_image" ]; then
419 conf_desc="$conf_desc${sep}FDT blob"
420 sep=", "
421 fdt_line="fdt = \"fdt-$dtb_image_sect\";"
422 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600423
Patrick Williamsaf48f632023-03-20 10:13:55 -0500424 if [ -n "$ramdisk_id" ]; then
425 conf_desc="$conf_desc${sep}ramdisk"
426 sep=", "
427 ramdisk_line="ramdisk = \"ramdisk-$ramdisk_id\";"
428 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600429
Patrick Williamsaf48f632023-03-20 10:13:55 -0500430 if [ -n "$bootscr_id" ]; then
431 conf_desc="$conf_desc${sep}u-boot script"
432 sep=", "
433 bootscr_line="bootscr = \"bootscr-$bootscr_id\";"
434 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600435
Patrick Williamsaf48f632023-03-20 10:13:55 -0500436 if [ -n "$config_id" ]; then
437 conf_desc="$conf_desc${sep}setup"
438 setup_line="setup = \"setup-$config_id\";"
439 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600440
Patrick Williamsaf48f632023-03-20 10:13:55 -0500441 if [ "$default_flag" = "1" ]; then
442 # default node is selected based on dtb ID if it is present,
443 # otherwise its selected based on kernel ID
444 if [ -n "$dtb_image" ]; then
445 default_line="default = \"${FIT_CONF_PREFIX}$dtb_image\";"
446 else
447 default_line="default = \"${FIT_CONF_PREFIX}$kernel_id\";"
448 fi
449 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600450
Patrick Williamsaf48f632023-03-20 10:13:55 -0500451 cat << EOF >> $its_file
Patrick Williamse479e922023-01-06 13:45:20 -0600452 $default_line
453 $conf_node {
454 description = "$default_flag $conf_desc";
455 $kernel_line
456 $fdt_line
457 $ramdisk_line
458 $bootscr_line
459 $setup_line
460 hash-1 {
461 algo = "$conf_csum";
462 };
463EOF
464
Patrick Williamsaf48f632023-03-20 10:13:55 -0500465 if [ -n "$conf_sign_keyname" ] ; then
Patrick Williamse479e922023-01-06 13:45:20 -0600466
Patrick Williamsaf48f632023-03-20 10:13:55 -0500467 sign_line="sign-images = "
468 sep=""
Patrick Williamse479e922023-01-06 13:45:20 -0600469
Patrick Williamsaf48f632023-03-20 10:13:55 -0500470 if [ -n "$kernel_id" ]; then
471 sign_line="$sign_line${sep}\"kernel\""
472 sep=", "
473 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600474
Patrick Williamsaf48f632023-03-20 10:13:55 -0500475 if [ -n "$dtb_image" ]; then
476 sign_line="$sign_line${sep}\"fdt\""
477 sep=", "
478 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600479
Patrick Williamsaf48f632023-03-20 10:13:55 -0500480 if [ -n "$ramdisk_id" ]; then
481 sign_line="$sign_line${sep}\"ramdisk\""
482 sep=", "
483 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600484
Patrick Williamsaf48f632023-03-20 10:13:55 -0500485 if [ -n "$bootscr_id" ]; then
486 sign_line="$sign_line${sep}\"bootscr\""
487 sep=", "
488 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600489
Patrick Williamsaf48f632023-03-20 10:13:55 -0500490 if [ -n "$config_id" ]; then
491 sign_line="$sign_line${sep}\"setup\""
492 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600493
Patrick Williamsaf48f632023-03-20 10:13:55 -0500494 sign_line="$sign_line;"
Patrick Williamse479e922023-01-06 13:45:20 -0600495
Patrick Williamsaf48f632023-03-20 10:13:55 -0500496 cat << EOF >> $its_file
Patrick Williamse479e922023-01-06 13:45:20 -0600497 signature-1 {
498 algo = "$conf_csum,$conf_sign_algo";
499 key-name-hint = "$conf_sign_keyname";
500 padding = "$conf_padding_algo";
501 $sign_line
502 };
503EOF
Patrick Williamsaf48f632023-03-20 10:13:55 -0500504 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600505
Patrick Williamsaf48f632023-03-20 10:13:55 -0500506 cat << EOF >> $its_file
Patrick Williamse479e922023-01-06 13:45:20 -0600507 };
508EOF
509}
510
511#
512# Assemble fitImage
513#
514# $1 ... .its filename
515# $2 ... fitImage name
516# $3 ... include ramdisk
517fitimage_assemble() {
Patrick Williamsaf48f632023-03-20 10:13:55 -0500518 kernelcount=1
519 dtbcount=""
520 DTBS=""
521 ramdiskcount=$3
522 setupcount=""
523 bootscr_id=""
524 rm -f $1 ${KERNEL_OUTPUT_DIR}/$2
Patrick Williamse479e922023-01-06 13:45:20 -0600525
Patrick Williamsaf48f632023-03-20 10:13:55 -0500526 if [ -n "${UBOOT_SIGN_IMG_KEYNAME}" -a "${UBOOT_SIGN_KEYNAME}" = "${UBOOT_SIGN_IMG_KEYNAME}" ]; then
527 bbfatal "Keys used to sign images and configuration nodes must be different."
528 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600529
Patrick Williamsaf48f632023-03-20 10:13:55 -0500530 fitimage_emit_fit_header $1
Patrick Williamse479e922023-01-06 13:45:20 -0600531
Patrick Williamsaf48f632023-03-20 10:13:55 -0500532 #
533 # Step 1: Prepare a kernel image section.
534 #
535 fitimage_emit_section_maint $1 imagestart
Patrick Williamse479e922023-01-06 13:45:20 -0600536
Patrick Williamsaf48f632023-03-20 10:13:55 -0500537 uboot_prep_kimage
538 fitimage_emit_section_kernel $1 $kernelcount linux.bin "$linux_comp"
Patrick Williamse479e922023-01-06 13:45:20 -0600539
Patrick Williamsaf48f632023-03-20 10:13:55 -0500540 #
541 # Step 2: Prepare a DTB image section
542 #
Patrick Williamse479e922023-01-06 13:45:20 -0600543
Patrick Williamsaf48f632023-03-20 10:13:55 -0500544 if [ -n "${KERNEL_DEVICETREE}" ]; then
545 dtbcount=1
546 for DTB in ${KERNEL_DEVICETREE}; do
547 if echo $DTB | grep -q '/dts/'; then
548 bbwarn "$DTB contains the full path to the the dts file, but only the dtb name should be used."
549 DTB=`basename $DTB | sed 's,\.dts$,.dtb,g'`
550 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600551
Patrick Williamsaf48f632023-03-20 10:13:55 -0500552 # Skip ${DTB} if it's also provided in ${EXTERNAL_KERNEL_DEVICETREE}
553 if [ -n "${EXTERNAL_KERNEL_DEVICETREE}" ] && [ -s ${EXTERNAL_KERNEL_DEVICETREE}/${DTB} ]; then
554 continue
555 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600556
Andrew Geissler0cb5bb52023-09-14 08:37:27 -0400557 # For non-vendored DTBs, we need to strip off the vendor path.
558 if "${@'false' if oe.types.boolean(d.getVar('KERNEL_DTBVENDORED')) else 'true'}"; then
559 DTB=`basename $DTB`
560 fi
561
Patrick Williamsaf48f632023-03-20 10:13:55 -0500562 DTB_PATH="${KERNEL_OUTPUT_DIR}/dts/$DTB"
563 if [ ! -e "$DTB_PATH" ]; then
564 DTB_PATH="${KERNEL_OUTPUT_DIR}/$DTB"
565 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600566
Patrick Williamsaf48f632023-03-20 10:13:55 -0500567 # Skip DTB if we've picked it up previously
568 echo "$DTBS" | tr ' ' '\n' | grep -xq "$DTB" && continue
Patrick Williamse479e922023-01-06 13:45:20 -0600569
Patrick Williamsaf48f632023-03-20 10:13:55 -0500570 DTBS="$DTBS $DTB"
571 DTB=$(echo $DTB | tr '/' '_')
572 fitimage_emit_section_dtb $1 $DTB $DTB_PATH
573 done
574 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600575
Patrick Williamsaf48f632023-03-20 10:13:55 -0500576 if [ -n "${EXTERNAL_KERNEL_DEVICETREE}" ]; then
577 dtbcount=1
578 for DTB in $(find "${EXTERNAL_KERNEL_DEVICETREE}" \( -name '*.dtb' -o -name '*.dtbo' \) -printf '%P\n' | sort); do
579 # Skip DTB if we've picked it up previously
580 echo "$DTBS" | tr ' ' '\n' | grep -xq "$DTB" && continue
Patrick Williamse479e922023-01-06 13:45:20 -0600581
Patrick Williamsaf48f632023-03-20 10:13:55 -0500582 DTBS="$DTBS $DTB"
Patrick Williamse479e922023-01-06 13:45:20 -0600583
Patrick Williamsaf48f632023-03-20 10:13:55 -0500584 # Also skip if a symlink. We'll later have each config section point at it
585 [ $(symlink_points_below $DTB "${EXTERNAL_KERNEL_DEVICETREE}") ] && continue
Patrick Williamse479e922023-01-06 13:45:20 -0600586
Patrick Williamsaf48f632023-03-20 10:13:55 -0500587 DTB=$(echo $DTB | tr '/' '_')
588 fitimage_emit_section_dtb $1 $DTB "${EXTERNAL_KERNEL_DEVICETREE}/$DTB"
589 done
590 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600591
Patrick Williamsaf48f632023-03-20 10:13:55 -0500592 #
593 # Step 3: Prepare a u-boot script section
594 #
Patrick Williamse479e922023-01-06 13:45:20 -0600595
Patrick Williamsaf48f632023-03-20 10:13:55 -0500596 if [ -n "${UBOOT_ENV}" ] && [ -d "${STAGING_DIR_HOST}/boot" ]; then
597 if [ -e "${STAGING_DIR_HOST}/boot/${UBOOT_ENV_BINARY}" ]; then
598 cp ${STAGING_DIR_HOST}/boot/${UBOOT_ENV_BINARY} ${B}
599 bootscr_id="${UBOOT_ENV_BINARY}"
600 fitimage_emit_section_boot_script $1 "$bootscr_id" ${UBOOT_ENV_BINARY}
601 else
602 bbwarn "${STAGING_DIR_HOST}/boot/${UBOOT_ENV_BINARY} not found."
603 fi
604 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600605
Patrick Williamsaf48f632023-03-20 10:13:55 -0500606 #
607 # Step 4: Prepare a setup section. (For x86)
608 #
609 if [ -e ${KERNEL_OUTPUT_DIR}/setup.bin ]; then
610 setupcount=1
611 fitimage_emit_section_setup $1 $setupcount ${KERNEL_OUTPUT_DIR}/setup.bin
612 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600613
Patrick Williamsaf48f632023-03-20 10:13:55 -0500614 #
615 # Step 5: Prepare a ramdisk section.
616 #
617 if [ "x${ramdiskcount}" = "x1" ] && [ "${INITRAMFS_IMAGE_BUNDLE}" != "1" ]; then
618 # Find and use the first initramfs image archive type we find
619 found=
620 for img in ${FIT_SUPPORTED_INITRAMFS_FSTYPES}; do
621 initramfs_path="${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.$img"
622 if [ -e "$initramfs_path" ]; then
623 bbnote "Found initramfs image: $initramfs_path"
624 found=true
625 fitimage_emit_section_ramdisk $1 "$ramdiskcount" "$initramfs_path"
626 break
627 else
628 bbnote "Did not find initramfs image: $initramfs_path"
629 fi
630 done
Patrick Williamse479e922023-01-06 13:45:20 -0600631
632 if [ -z "$found" ] && [ -e ${INITRAMFS_IMAGE} ]; then
633 found=true
634 bbnote "Found initramfs image: ${INITRAMFS_IMAGE}"
635 fitimage_emit_section_ramdisk $1 "$ramdiskcount" "${INITRAMFS_IMAGE}"
636 fi
637
Patrick Williamsaf48f632023-03-20 10:13:55 -0500638 if [ -z "$found" ]; then
639 bbfatal "Could not find a valid initramfs type for ${INITRAMFS_IMAGE_NAME}, the supported types are: ${FIT_SUPPORTED_INITRAMFS_FSTYPES}"
640 fi
641 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600642
Patrick Williamsaf48f632023-03-20 10:13:55 -0500643 fitimage_emit_section_maint $1 sectend
Patrick Williamse479e922023-01-06 13:45:20 -0600644
Patrick Williamsaf48f632023-03-20 10:13:55 -0500645 # Force the first Kernel and DTB in the default config
646 kernelcount=1
647 if [ -n "$dtbcount" ]; then
648 dtbcount=1
649 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600650
Patrick Williamsaf48f632023-03-20 10:13:55 -0500651 #
652 # Step 6: Prepare a configurations section
653 #
654 fitimage_emit_section_maint $1 confstart
Patrick Williamse479e922023-01-06 13:45:20 -0600655
Patrick Williamsaf48f632023-03-20 10:13:55 -0500656 # kernel-fitimage.bbclass currently only supports a single kernel (no less or
657 # more) to be added to the FIT image along with 0 or more device trees and
658 # 0 or 1 ramdisk.
Patrick Williamse479e922023-01-06 13:45:20 -0600659 # It is also possible to include an initramfs bundle (kernel and rootfs in one binary)
660 # When the initramfs bundle is used ramdisk is disabled.
Patrick Williamsaf48f632023-03-20 10:13:55 -0500661 # If a device tree is to be part of the FIT image, then select
662 # the default configuration to be used is based on the dtbcount. If there is
663 # no dtb present than select the default configuation to be based on
664 # the kernelcount.
665 if [ -n "$DTBS" ]; then
666 i=1
667 for DTB in ${DTBS}; do
668 dtb_ext=${DTB##*.}
669 if [ "$dtb_ext" = "dtbo" ]; then
670 fitimage_emit_section_config $1 "" "$DTB" "" "$bootscr_id" "" "`expr $i = $dtbcount`"
671 else
672 fitimage_emit_section_config $1 $kernelcount "$DTB" "$ramdiskcount" "$bootscr_id" "$setupcount" "`expr $i = $dtbcount`"
673 fi
674 i=`expr $i + 1`
675 done
676 else
677 defaultconfigcount=1
678 fitimage_emit_section_config $1 $kernelcount "" "$ramdiskcount" "$bootscr_id" "$setupcount" $defaultconfigcount
679 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600680
Patrick Williamsaf48f632023-03-20 10:13:55 -0500681 fitimage_emit_section_maint $1 sectend
Patrick Williamse479e922023-01-06 13:45:20 -0600682
Patrick Williamsaf48f632023-03-20 10:13:55 -0500683 fitimage_emit_section_maint $1 fitend
Patrick Williamse479e922023-01-06 13:45:20 -0600684
Patrick Williamsaf48f632023-03-20 10:13:55 -0500685 #
686 # Step 7: Assemble the image
687 #
688 ${UBOOT_MKIMAGE} \
689 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
690 -f $1 \
691 ${KERNEL_OUTPUT_DIR}/$2
Patrick Williamse479e922023-01-06 13:45:20 -0600692
Patrick Williamsaf48f632023-03-20 10:13:55 -0500693 #
694 # Step 8: Sign the image
695 #
696 if [ "x${UBOOT_SIGN_ENABLE}" = "x1" ] ; then
697 ${UBOOT_MKIMAGE_SIGN} \
698 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
699 -F -k "${UBOOT_SIGN_KEYDIR}" \
700 -r ${KERNEL_OUTPUT_DIR}/$2 \
701 ${UBOOT_MKIMAGE_SIGN_ARGS}
702 fi
Patrick Williamse479e922023-01-06 13:45:20 -0600703}