blob: bb2f3c4ccc4d25fbf11064cc3b7e4bc7f6a9d145 [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001inherit kernel-uboot kernel-artifact-names uboot-sign
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
3python __anonymous () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -05004 kerneltypes = d.getVar('KERNEL_IMAGETYPES') or ""
He Zhefe76b1e2016-05-25 04:47:16 -04005 if 'fitImage' in kerneltypes.split():
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006 depends = d.getVar("DEPENDS")
Brad Bishop19323692019-04-05 15:28:33 -04007 depends = "%s u-boot-tools-native dtc-native" % depends
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008 d.setVar("DEPENDS", depends)
9
Brad Bishopd7bf8c12018-02-25 22:55:05 -050010 uarch = d.getVar("UBOOT_ARCH")
11 if uarch == "arm64":
12 replacementtype = "Image"
Brad Bishopc342db32019-05-15 21:57:59 -040013 elif uarch == "riscv":
14 replacementtype = "Image"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015 elif uarch == "mips":
Brad Bishop6e60e8b2018-02-01 10:27:11 -050016 replacementtype = "vmlinuz.bin"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050017 elif uarch == "x86":
Patrick Williamsc0f7c042017-02-23 20:41:17 -060018 replacementtype = "bzImage"
Brad Bishop316dfdd2018-06-25 12:45:53 -040019 elif uarch == "microblaze":
20 replacementtype = "linux.bin"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060021 else:
22 replacementtype = "zImage"
23
Brad Bishop19323692019-04-05 15:28:33 -040024 # Override KERNEL_IMAGETYPE_FOR_MAKE variable, which is internal
25 # to kernel.bbclass . We have to override it, since we pack zImage
26 # (at least for now) into the fitImage .
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027 typeformake = d.getVar("KERNEL_IMAGETYPE_FOR_MAKE") or ""
He Zhefe76b1e2016-05-25 04:47:16 -040028 if 'fitImage' in typeformake.split():
Patrick Williamsc0f7c042017-02-23 20:41:17 -060029 d.setVar('KERNEL_IMAGETYPE_FOR_MAKE', typeformake.replace('fitImage', replacementtype))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031 image = d.getVar('INITRAMFS_IMAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032 if image:
George McCollister185c8ae2016-05-26 08:55:16 -050033 d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' ${INITRAMFS_IMAGE}:do_image_complete')
34
Brad Bishop19323692019-04-05 15:28:33 -040035 #check if there are any dtb providers
36 providerdtb = d.getVar("PREFERRED_PROVIDER_virtual/dtb")
37 if providerdtb:
38 d.appendVarFlag('do_assemble_fitimage', 'depends', ' virtual/dtb:do_populate_sysroot')
39 d.appendVarFlag('do_assemble_fitimage_initramfs', 'depends', ' virtual/dtb:do_populate_sysroot')
40 d.setVar('EXTERNAL_KERNEL_DEVICETREE', "${RECIPE_SYSROOT}/boot/devicetree")
41
Patrick Williamsc0f7c042017-02-23 20:41:17 -060042 # Verified boot will sign the fitImage and append the public key to
Brad Bishop6e60e8b2018-02-01 10:27:11 -050043 # U-Boot dtb. We ensure the U-Boot dtb is deployed before assembling
Patrick Williamsc0f7c042017-02-23 20:41:17 -060044 # the fitImage:
Brad Bishop15ae2502019-06-18 21:44:24 -040045 if d.getVar('UBOOT_SIGN_ENABLE') == "1" and d.getVar('UBOOT_DTB_BINARY'):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 uboot_pn = d.getVar('PREFERRED_PROVIDER_u-boot') or 'u-boot'
Brad Bishop19323692019-04-05 15:28:33 -040047 d.appendVarFlag('do_assemble_fitimage', 'depends', ' %s:do_populate_sysroot' % uboot_pn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048}
49
Yannick Gicqueld5813b42016-04-27 16:20:55 +020050# Options for the device tree compiler passed to mkimage '-D' feature:
51UBOOT_MKIMAGE_DTCOPTS ??= ""
52
Brad Bishopf3fd2882019-06-21 08:06:37 -040053# fitImage Hash Algo
54FIT_HASH_ALG ?= "sha256"
55
Brad Bishop64c979e2019-11-04 13:55:29 -050056# fitImage Signature Algo
57FIT_SIGN_ALG ?= "rsa2048"
58
Andrew Geissleraf5e4ef2020-10-16 10:22:50 -050059# Generate keys for signing fitImage
60FIT_GENERATE_KEYS ?= "0"
61
62# Size of private key in number of bits
63FIT_SIGN_NUMBITS ?= "2048"
64
65# args to openssl genrsa (Default is just the public exponent)
66FIT_KEY_GENRSA_ARGS ?= "-F4"
67
68# args to openssl req (Default is -batch for non interactive mode and
69# -new for new certificate)
70FIT_KEY_REQ_ARGS ?= "-batch -new"
71
72# Standard format for public key certificate
73FIT_KEY_SIGN_PKCS ?= "-x509"
74
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075#
76# Emit the fitImage ITS header
77#
George McCollister185c8ae2016-05-26 08:55:16 -050078# $1 ... .its filename
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079fitimage_emit_fit_header() {
George McCollister185c8ae2016-05-26 08:55:16 -050080 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081/dts-v1/;
82
83/ {
84 description = "U-Boot fitImage for ${DISTRO_NAME}/${PV}/${MACHINE}";
85 #address-cells = <1>;
86EOF
87}
88
89#
90# Emit the fitImage section bits
91#
George McCollister185c8ae2016-05-26 08:55:16 -050092# $1 ... .its filename
93# $2 ... Section bit type: imagestart - image section start
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094# confstart - configuration section start
95# sectend - section end
96# fitend - fitimage end
97#
98fitimage_emit_section_maint() {
George McCollister185c8ae2016-05-26 08:55:16 -050099 case $2 in
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 imagestart)
George McCollister185c8ae2016-05-26 08:55:16 -0500101 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
103 images {
104EOF
105 ;;
106 confstart)
George McCollister185c8ae2016-05-26 08:55:16 -0500107 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108
109 configurations {
110EOF
111 ;;
112 sectend)
George McCollister185c8ae2016-05-26 08:55:16 -0500113 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 };
115EOF
116 ;;
117 fitend)
George McCollister185c8ae2016-05-26 08:55:16 -0500118 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119};
120EOF
121 ;;
122 esac
123}
124
125#
126# Emit the fitImage ITS kernel section
127#
George McCollister185c8ae2016-05-26 08:55:16 -0500128# $1 ... .its filename
129# $2 ... Image counter
130# $3 ... Path to kernel image
131# $4 ... Compression type
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132fitimage_emit_section_kernel() {
133
Brad Bishopf3fd2882019-06-21 08:06:37 -0400134 kernel_csum="${FIT_HASH_ALG}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135
Brad Bishop316dfdd2018-06-25 12:45:53 -0400136 ENTRYPOINT="${UBOOT_ENTRYPOINT}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 if [ -n "${UBOOT_ENTRYSYMBOL}" ]; then
138 ENTRYPOINT=`${HOST_PREFIX}nm vmlinux | \
139 awk '$3=="${UBOOT_ENTRYSYMBOL}" {print "0x"$1;exit}'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140 fi
141
George McCollister185c8ae2016-05-26 08:55:16 -0500142 cat << EOF >> ${1}
143 kernel@${2} {
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 description = "Linux kernel";
George McCollister185c8ae2016-05-26 08:55:16 -0500145 data = /incbin/("${3}");
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146 type = "kernel";
147 arch = "${UBOOT_ARCH}";
148 os = "linux";
George McCollister185c8ae2016-05-26 08:55:16 -0500149 compression = "${4}";
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150 load = <${UBOOT_LOADADDRESS}>;
151 entry = <${ENTRYPOINT}>;
152 hash@1 {
153 algo = "${kernel_csum}";
154 };
155 };
156EOF
157}
158
159#
160# Emit the fitImage ITS DTB section
161#
George McCollister185c8ae2016-05-26 08:55:16 -0500162# $1 ... .its filename
163# $2 ... Image counter
164# $3 ... Path to DTB image
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165fitimage_emit_section_dtb() {
166
Brad Bishopf3fd2882019-06-21 08:06:37 -0400167 dtb_csum="${FIT_HASH_ALG}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800169 dtb_loadline=""
170 dtb_ext=${DTB##*.}
171 if [ "${dtb_ext}" = "dtbo" ]; then
172 if [ -n "${UBOOT_DTBO_LOADADDRESS}" ]; then
173 dtb_loadline="load = <${UBOOT_DTBO_LOADADDRESS}>;"
174 fi
175 elif [ -n "${UBOOT_DTB_LOADADDRESS}" ]; then
176 dtb_loadline="load = <${UBOOT_DTB_LOADADDRESS}>;"
177 fi
George McCollister185c8ae2016-05-26 08:55:16 -0500178 cat << EOF >> ${1}
179 fdt@${2} {
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180 description = "Flattened Device Tree blob";
George McCollister185c8ae2016-05-26 08:55:16 -0500181 data = /incbin/("${3}");
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182 type = "flat_dt";
183 arch = "${UBOOT_ARCH}";
184 compression = "none";
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800185 ${dtb_loadline}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186 hash@1 {
187 algo = "${dtb_csum}";
188 };
189 };
190EOF
191}
192
193#
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600194# Emit the fitImage ITS setup section
195#
196# $1 ... .its filename
197# $2 ... Image counter
198# $3 ... Path to setup image
199fitimage_emit_section_setup() {
200
Brad Bishopf3fd2882019-06-21 08:06:37 -0400201 setup_csum="${FIT_HASH_ALG}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600202
203 cat << EOF >> ${1}
204 setup@${2} {
205 description = "Linux setup.bin";
206 data = /incbin/("${3}");
207 type = "x86_setup";
208 arch = "${UBOOT_ARCH}";
209 os = "linux";
210 compression = "none";
211 load = <0x00090000>;
212 entry = <0x00090000>;
213 hash@1 {
214 algo = "${setup_csum}";
215 };
216 };
217EOF
218}
219
220#
George McCollister185c8ae2016-05-26 08:55:16 -0500221# Emit the fitImage ITS ramdisk section
222#
223# $1 ... .its filename
224# $2 ... Image counter
225# $3 ... Path to ramdisk image
226fitimage_emit_section_ramdisk() {
227
Brad Bishopf3fd2882019-06-21 08:06:37 -0400228 ramdisk_csum="${FIT_HASH_ALG}"
Nathan Rossib4a4dc02016-10-21 22:07:27 +1000229 ramdisk_loadline=""
230 ramdisk_entryline=""
231
232 if [ -n "${UBOOT_RD_LOADADDRESS}" ]; then
233 ramdisk_loadline="load = <${UBOOT_RD_LOADADDRESS}>;"
234 fi
235 if [ -n "${UBOOT_RD_ENTRYPOINT}" ]; then
236 ramdisk_entryline="entry = <${UBOOT_RD_ENTRYPOINT}>;"
237 fi
George McCollister185c8ae2016-05-26 08:55:16 -0500238
239 cat << EOF >> ${1}
240 ramdisk@${2} {
Rick Altherrbc1b8802017-01-20 11:28:53 -0800241 description = "${INITRAMFS_IMAGE}";
George McCollister185c8ae2016-05-26 08:55:16 -0500242 data = /incbin/("${3}");
243 type = "ramdisk";
244 arch = "${UBOOT_ARCH}";
245 os = "linux";
Brad Bishop00e122a2019-10-05 11:10:57 -0400246 compression = "none";
Nathan Rossib4a4dc02016-10-21 22:07:27 +1000247 ${ramdisk_loadline}
248 ${ramdisk_entryline}
George McCollister185c8ae2016-05-26 08:55:16 -0500249 hash@1 {
250 algo = "${ramdisk_csum}";
251 };
252 };
253EOF
254}
255
256#
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257# Emit the fitImage ITS configuration section
258#
George McCollister185c8ae2016-05-26 08:55:16 -0500259# $1 ... .its filename
260# $2 ... Linux kernel ID
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261# $3 ... DTB image name
George McCollister185c8ae2016-05-26 08:55:16 -0500262# $4 ... ramdisk ID
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600263# $5 ... config ID
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500264# $6 ... default flag
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500265fitimage_emit_section_config() {
266
Brad Bishopf3fd2882019-06-21 08:06:37 -0400267 conf_csum="${FIT_HASH_ALG}"
Brad Bishop64c979e2019-11-04 13:55:29 -0500268 conf_sign_algo="${FIT_SIGN_ALG}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600269 if [ -n "${UBOOT_SIGN_ENABLE}" ] ; then
270 conf_sign_keyname="${UBOOT_SIGN_KEYNAME}"
271 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500272
273 # Test if we have any DTBs at all
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800274 sep=""
275 conf_desc=""
Andrew Geissler635e0e42020-08-21 15:58:33 -0500276 conf_node="conf@"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800277 kernel_line=""
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600278 fdt_line=""
279 ramdisk_line=""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500280 setup_line=""
281 default_line=""
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600282
Andrew Geissler635e0e42020-08-21 15:58:33 -0500283 # conf node name is selected based on dtb ID if it is present,
284 # otherwise its selected based on kernel ID
285 if [ -n "${3}" ]; then
286 conf_node=$conf_node${3}
287 else
288 conf_node=$conf_node${2}
289 fi
290
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800291 if [ -n "${2}" ]; then
292 conf_desc="Linux kernel"
293 sep=", "
294 kernel_line="kernel = \"kernel@${2}\";"
295 fi
296
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600297 if [ -n "${3}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800298 conf_desc="${conf_desc}${sep}FDT blob"
299 sep=", "
George McCollister185c8ae2016-05-26 08:55:16 -0500300 fdt_line="fdt = \"fdt@${3}\";"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600301 fi
302
303 if [ -n "${4}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800304 conf_desc="${conf_desc}${sep}ramdisk"
305 sep=", "
George McCollister185c8ae2016-05-26 08:55:16 -0500306 ramdisk_line="ramdisk = \"ramdisk@${4}\";"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500307 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600308
309 if [ -n "${5}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800310 conf_desc="${conf_desc}${sep}setup"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600311 setup_line="setup = \"setup@${5}\";"
312 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500313
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500314 if [ "${6}" = "1" ]; then
Andrew Geissler635e0e42020-08-21 15:58:33 -0500315 # default node is selected based on dtb ID if it is present,
316 # otherwise its selected based on kernel ID
317 if [ -n "${3}" ]; then
318 default_line="default = \"conf@${3}\";"
319 else
320 default_line="default = \"conf@${2}\";"
321 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500322 fi
323
George McCollister185c8ae2016-05-26 08:55:16 -0500324 cat << EOF >> ${1}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500325 ${default_line}
Andrew Geissler635e0e42020-08-21 15:58:33 -0500326 $conf_node {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500327 description = "${6} ${conf_desc}";
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500328 ${kernel_line}
329 ${fdt_line}
George McCollister185c8ae2016-05-26 08:55:16 -0500330 ${ramdisk_line}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600331 ${setup_line}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500332 hash@1 {
333 algo = "${conf_csum}";
334 };
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600335EOF
336
337 if [ ! -z "${conf_sign_keyname}" ] ; then
338
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800339 sign_line="sign-images = "
340 sep=""
341
342 if [ -n "${2}" ]; then
343 sign_line="${sign_line}${sep}\"kernel\""
344 sep=", "
345 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600346
347 if [ -n "${3}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800348 sign_line="${sign_line}${sep}\"fdt\""
349 sep=", "
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600350 fi
351
352 if [ -n "${4}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800353 sign_line="${sign_line}${sep}\"ramdisk\""
354 sep=", "
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600355 fi
356
357 if [ -n "${5}" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800358 sign_line="${sign_line}${sep}\"setup\""
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600359 fi
360
361 sign_line="${sign_line};"
362
363 cat << EOF >> ${1}
364 signature@1 {
Brad Bishop64c979e2019-11-04 13:55:29 -0500365 algo = "${conf_csum},${conf_sign_algo}";
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600366 key-name-hint = "${conf_sign_keyname}";
367 ${sign_line}
368 };
369EOF
370 fi
371
372 cat << EOF >> ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500373 };
374EOF
375}
376
George McCollister185c8ae2016-05-26 08:55:16 -0500377#
378# Assemble fitImage
379#
380# $1 ... .its filename
381# $2 ... fitImage name
382# $3 ... include ramdisk
383fitimage_assemble() {
384 kernelcount=1
385 dtbcount=""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500386 DTBS=""
George McCollister185c8ae2016-05-26 08:55:16 -0500387 ramdiskcount=${3}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600388 setupcount=""
George McCollister185c8ae2016-05-26 08:55:16 -0500389 rm -f ${1} arch/${ARCH}/boot/${2}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500390
George McCollister185c8ae2016-05-26 08:55:16 -0500391 fitimage_emit_fit_header ${1}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500392
George McCollister185c8ae2016-05-26 08:55:16 -0500393 #
394 # Step 1: Prepare a kernel image section.
395 #
396 fitimage_emit_section_maint ${1} imagestart
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500397
George McCollister185c8ae2016-05-26 08:55:16 -0500398 uboot_prep_kimage
399 fitimage_emit_section_kernel ${1} "${kernelcount}" linux.bin "${linux_comp}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500400
George McCollister185c8ae2016-05-26 08:55:16 -0500401 #
402 # Step 2: Prepare a DTB image section
403 #
Brad Bishop19323692019-04-05 15:28:33 -0400404
405 if [ -z "${EXTERNAL_KERNEL_DEVICETREE}" ] && [ -n "${KERNEL_DEVICETREE}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500406 dtbcount=1
George McCollister185c8ae2016-05-26 08:55:16 -0500407 for DTB in ${KERNEL_DEVICETREE}; do
408 if echo ${DTB} | grep -q '/dts/'; then
409 bbwarn "${DTB} contains the full path to the the dts file, but only the dtb name should be used."
410 DTB=`basename ${DTB} | sed 's,\.dts$,.dtb,g'`
411 fi
412 DTB_PATH="arch/${ARCH}/boot/dts/${DTB}"
413 if [ ! -e "${DTB_PATH}" ]; then
414 DTB_PATH="arch/${ARCH}/boot/${DTB}"
415 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500416
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500417 DTB=$(echo "${DTB}" | tr '/' '_')
418 DTBS="${DTBS} ${DTB}"
419 fitimage_emit_section_dtb ${1} ${DTB} ${DTB_PATH}
George McCollister185c8ae2016-05-26 08:55:16 -0500420 done
421 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500422
Brad Bishop19323692019-04-05 15:28:33 -0400423 if [ -n "${EXTERNAL_KERNEL_DEVICETREE}" ]; then
424 dtbcount=1
Andrew Geissler82c905d2020-04-13 13:39:40 -0500425 for DTB in $(find "${EXTERNAL_KERNEL_DEVICETREE}" \( -name '*.dtb' -o -name '*.dtbo' \) -printf '%P\n' | sort); do
Brad Bishop19323692019-04-05 15:28:33 -0400426 DTB=$(echo "${DTB}" | tr '/' '_')
427 DTBS="${DTBS} ${DTB}"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500428 fitimage_emit_section_dtb ${1} ${DTB} "${EXTERNAL_KERNEL_DEVICETREE}/${DTB}"
Brad Bishop19323692019-04-05 15:28:33 -0400429 done
430 fi
431
George McCollister185c8ae2016-05-26 08:55:16 -0500432 #
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600433 # Step 3: Prepare a setup section. (For x86)
434 #
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500435 if [ -e arch/${ARCH}/boot/setup.bin ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600436 setupcount=1
437 fitimage_emit_section_setup ${1} "${setupcount}" arch/${ARCH}/boot/setup.bin
438 fi
439
440 #
441 # Step 4: Prepare a ramdisk section.
George McCollister185c8ae2016-05-26 08:55:16 -0500442 #
443 if [ "x${ramdiskcount}" = "x1" ] ; then
Rick Altherrbc1b8802017-01-20 11:28:53 -0800444 # Find and use the first initramfs image archive type we find
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800445 for img in cpio.lz4 cpio.lzo cpio.lzma cpio.xz cpio.gz ext2.gz cpio; do
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500446 initramfs_path="${DEPLOY_DIR_IMAGE}/${INITRAMFS_IMAGE_NAME}.${img}"
Rick Altherrbc1b8802017-01-20 11:28:53 -0800447 echo "Using $initramfs_path"
448 if [ -e "${initramfs_path}" ]; then
449 fitimage_emit_section_ramdisk ${1} "${ramdiskcount}" "${initramfs_path}"
450 break
451 fi
452 done
George McCollister185c8ae2016-05-26 08:55:16 -0500453 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500454
George McCollister185c8ae2016-05-26 08:55:16 -0500455 fitimage_emit_section_maint ${1} sectend
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500456
George McCollister185c8ae2016-05-26 08:55:16 -0500457 # Force the first Kernel and DTB in the default config
458 kernelcount=1
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500459 if [ -n "${dtbcount}" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600460 dtbcount=1
461 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500462
George McCollister185c8ae2016-05-26 08:55:16 -0500463 #
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600464 # Step 5: Prepare a configurations section
George McCollister185c8ae2016-05-26 08:55:16 -0500465 #
466 fitimage_emit_section_maint ${1} confstart
467
Andrew Geissler635e0e42020-08-21 15:58:33 -0500468 # kernel-fitimage.bbclass currently only supports a single kernel (no less or
469 # more) to be added to the FIT image along with 0 or more device trees and
470 # 0 or 1 ramdisk.
471 # If a device tree is to be part of the FIT image, then select
472 # the default configuration to be used is based on the dtbcount. If there is
473 # no dtb present than select the default configuation to be based on
474 # the kernelcount.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500475 if [ -n "${DTBS}" ]; then
476 i=1
477 for DTB in ${DTBS}; do
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800478 dtb_ext=${DTB##*.}
479 if [ "${dtb_ext}" = "dtbo" ]; then
480 fitimage_emit_section_config ${1} "" "${DTB}" "" "" "`expr ${i} = ${dtbcount}`"
481 else
482 fitimage_emit_section_config ${1} "${kernelcount}" "${DTB}" "${ramdiskcount}" "${setupcount}" "`expr ${i} = ${dtbcount}`"
483 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500484 i=`expr ${i} + 1`
485 done
Andrew Geissler635e0e42020-08-21 15:58:33 -0500486 else
487 defaultconfigcount=1
488 fitimage_emit_section_config ${1} "${kernelcount}" "" "${ramdiskcount}" "${setupcount}" "${defaultconfigcount}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500489 fi
George McCollister185c8ae2016-05-26 08:55:16 -0500490
491 fitimage_emit_section_maint ${1} sectend
492
493 fitimage_emit_section_maint ${1} fitend
494
495 #
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600496 # Step 6: Assemble the image
George McCollister185c8ae2016-05-26 08:55:16 -0500497 #
498 uboot-mkimage \
499 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
500 -f ${1} \
501 arch/${ARCH}/boot/${2}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600502
503 #
504 # Step 7: Sign the image and add public key to U-Boot dtb
505 #
506 if [ "x${UBOOT_SIGN_ENABLE}" = "x1" ] ; then
Brad Bishop19323692019-04-05 15:28:33 -0400507 add_key_to_u_boot=""
508 if [ -n "${UBOOT_DTB_BINARY}" ]; then
509 # The u-boot.dtb is a symlink to UBOOT_DTB_IMAGE, so we need copy
510 # both of them, and don't dereference the symlink.
511 cp -P ${STAGING_DATADIR}/u-boot*.dtb ${B}
512 add_key_to_u_boot="-K ${B}/${UBOOT_DTB_BINARY}"
513 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600514 uboot-mkimage \
515 ${@'-D "${UBOOT_MKIMAGE_DTCOPTS}"' if len('${UBOOT_MKIMAGE_DTCOPTS}') else ''} \
516 -F -k "${UBOOT_SIGN_KEYDIR}" \
Brad Bishop19323692019-04-05 15:28:33 -0400517 $add_key_to_u_boot \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600518 -r arch/${ARCH}/boot/${2}
519 fi
George McCollister185c8ae2016-05-26 08:55:16 -0500520}
521
522do_assemble_fitimage() {
523 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage"; then
524 cd ${B}
525 fitimage_assemble fit-image.its fitImage
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500526 fi
527}
528
529addtask assemble_fitimage before do_install after do_compile
530
George McCollister185c8ae2016-05-26 08:55:16 -0500531do_assemble_fitimage_initramfs() {
532 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage" && \
533 test -n "${INITRAMFS_IMAGE}" ; then
534 cd ${B}
535 fitimage_assemble fit-image-${INITRAMFS_IMAGE}.its fitImage-${INITRAMFS_IMAGE} 1
536 fi
537}
538
Brad Bishop19323692019-04-05 15:28:33 -0400539addtask assemble_fitimage_initramfs before do_deploy after do_bundle_initramfs
George McCollister185c8ae2016-05-26 08:55:16 -0500540
Andrew Geissleraf5e4ef2020-10-16 10:22:50 -0500541do_generate_rsa_keys() {
542 if [ "${UBOOT_SIGN_ENABLE}" = "0" ] && [ "${FIT_GENERATE_KEYS}" = "1" ]; then
543 bbwarn "FIT_GENERATE_KEYS is set to 1 eventhough UBOOT_SIGN_ENABLE is set to 0. The keys will not be generated as they won't be used."
544 fi
545
546 if [ "${UBOOT_SIGN_ENABLE}" = "1" ] && [ "${FIT_GENERATE_KEYS}" = "1" ]; then
547
548 # Generate keys only if they don't already exist
549 if [ ! -f "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".key ] || \
550 [ ! -f "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".crt]; then
551
552 # make directory if it does not already exist
553 mkdir -p "${UBOOT_SIGN_KEYDIR}"
554
555 echo "Generating RSA private key for signing fitImage"
556 openssl genrsa ${FIT_KEY_GENRSA_ARGS} -out \
557 "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".key \
558 "${FIT_SIGN_NUMBITS}"
559
560 echo "Generating certificate for signing fitImage"
561 openssl req ${FIT_KEY_REQ_ARGS} "${FIT_KEY_SIGN_PKCS}" \
562 -key "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".key \
563 -out "${UBOOT_SIGN_KEYDIR}/${UBOOT_SIGN_KEYNAME}".crt
564 fi
565 fi
566}
567
568addtask generate_rsa_keys before do_assemble_fitimage after do_compile
George McCollister185c8ae2016-05-26 08:55:16 -0500569
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500570kernel_do_deploy[vardepsexclude] = "DATETIME"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500571kernel_do_deploy_append() {
572 # Update deploy directory
He Zhefe76b1e2016-05-25 04:47:16 -0400573 if echo ${KERNEL_IMAGETYPES} | grep -wq "fitImage"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500574 echo "Copying fit-image.its source file..."
Brad Bishop64c979e2019-11-04 13:55:29 -0500575 install -m 0644 ${B}/fit-image.its "$deployDir/fitImage-its-${KERNEL_FIT_NAME}.its"
576 ln -snf fitImage-its-${KERNEL_FIT_NAME}.its "$deployDir/fitImage-its-${KERNEL_FIT_LINK_NAME}"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800577
578 echo "Copying linux.bin file..."
Brad Bishop64c979e2019-11-04 13:55:29 -0500579 install -m 0644 ${B}/linux.bin $deployDir/fitImage-linux.bin-${KERNEL_FIT_NAME}.bin
580 ln -snf fitImage-linux.bin-${KERNEL_FIT_NAME}.bin "$deployDir/fitImage-linux.bin-${KERNEL_FIT_LINK_NAME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500581
George McCollister185c8ae2016-05-26 08:55:16 -0500582 if [ -n "${INITRAMFS_IMAGE}" ]; then
583 echo "Copying fit-image-${INITRAMFS_IMAGE}.its source file..."
Brad Bishop64c979e2019-11-04 13:55:29 -0500584 install -m 0644 ${B}/fit-image-${INITRAMFS_IMAGE}.its "$deployDir/fitImage-its-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.its"
585 ln -snf fitImage-its-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.its "$deployDir/fitImage-its-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_LINK_NAME}"
George McCollister185c8ae2016-05-26 08:55:16 -0500586
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800587 echo "Copying fitImage-${INITRAMFS_IMAGE} file..."
Brad Bishop64c979e2019-11-04 13:55:29 -0500588 install -m 0644 ${B}/arch/${ARCH}/boot/fitImage-${INITRAMFS_IMAGE} "$deployDir/fitImage-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.bin"
589 ln -snf fitImage-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_NAME}.bin "$deployDir/fitImage-${INITRAMFS_IMAGE_NAME}-${KERNEL_FIT_LINK_NAME}"
George McCollister185c8ae2016-05-26 08:55:16 -0500590 fi
Brad Bishop19323692019-04-05 15:28:33 -0400591 if [ "${UBOOT_SIGN_ENABLE}" = "1" -a -n "${UBOOT_DTB_BINARY}" ] ; then
592 # UBOOT_DTB_IMAGE is a realfile, but we can't use
593 # ${UBOOT_DTB_IMAGE} since it contains ${PV} which is aimed
594 # for u-boot, but we are in kernel env now.
Brad Bishop64c979e2019-11-04 13:55:29 -0500595 install -m 0644 ${B}/u-boot-${MACHINE}*.dtb "$deployDir/"
Brad Bishop19323692019-04-05 15:28:33 -0400596 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500597 fi
598}