blob: 95ea6359979fd89ebe49494403eeae11f122e3b4 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001inherit kernel-uboot-extension hsm-sign-env
2
3python __anonymous () {
4 kerneltype = d.getVar('KERNEL_IMAGETYPE', True)
5 if kerneltype == 'fitImage':
6 depends = d.getVar("DEPENDS", True)
7 depends = "%s u-boot-mkimage-native lz4-native dtc-native" % depends
8 d.setVar("DEPENDS", depends)
9
10 # Override KERNEL_IMAGETYPE_FOR_MAKE variable, which is internal
11 # to kernel.bbclass . We have to override it, since we pack zImage
12 # (at least for now) into the fitImage .
13 kernelarch = d.getVar('KERNEL_ARCH', True)
14 if kernelarch == 'arm':
15 d.setVar("KERNEL_IMAGETYPE_FOR_MAKE", "zImage")
16 elif kernelarch == 'arm64':
17 d.setVar("KERNEL_IMAGETYPE_FOR_MAKE", "Image")
18 else:
19 print("please set KERNEL_ARCH variable.")
20 sys.exit(1)
21 image = d.getVar('INITRAMFS_IMAGE', True)
22 if image:
23 d.appendVarFlag('do_assemble_fitimage', 'depends', ' ${INITRAMFS_IMAGE}:do_image_complete')
24}
25
26#
27# Emit the fitImage ITS header
28#
29fitimage_emit_fit_header() {
30 cat << EOF >> fit-image.its
31/dts-v1/;
32
33/ {
34 description = "U-Boot fitImage for ${DISTRO_NAME}/${PV}/${MACHINE}";
35 #address-cells = <1>;
36EOF
37}
38
39#
40# Emit the fitImage section bits
41#
42# $1 ... Section bit type: imagestart - image section start
43# confstart - configuration section start
44# sectend - section end
45# fitend - fitimage end
46#
47fitimage_emit_section_maint() {
48 case $1 in
49 imagestart)
50 cat << EOF >> fit-image.its
51
52 images {
53EOF
54 ;;
55 confstart)
56 cat << EOF >> fit-image.its
57
58 configurations {
59EOF
60 ;;
61 sectend)
62 cat << EOF >> fit-image.its
63 };
64EOF
65 ;;
66 fitend)
67 cat << EOF >> fit-image.its
68};
69EOF
70 ;;
71 esac
72}
73
74#
75# Emit the fitImage ITS kernel section
76#
77# $1 ... Image counter
78# $2 ... Path to kernel image
79# $3 ... Compression type
80fitimage_emit_section_kernel() {
81
82 if [ -n "${IMAGE_HASH_ALGO}" ] ; then
83 kernel_csum="${IMAGE_HASH_ALGO}"
84 else
85 kernel_csum="sha256"
86 fi
87
88 ENTRYPOINT=${UBOOT_ENTRYPOINT}
89 if [ -n "${UBOOT_ENTRYSYMBOL}" ] ; then
90 ENTRYPOINT=`${HOST_PREFIX}nm ${S}/vmlinux | \
91 awk '$3=="${UBOOT_ENTRYSYMBOL}" {print $1}'`
92 fi
93
94 cat << EOF >> fit-image.its
95 kernel@${1} {
96 description = "Linux kernel";
97 data = /incbin/("${2}");
98 type = "kernel";
99 arch = "${UBOOT_ARCH}";
100 os = "linux";
101 compression = "${3}";
102 load = <${UBOOT_LOADADDRESS}>;
103 entry = <${ENTRYPOINT}>;
104 hash@1 {
105 algo = "${kernel_csum}";
106 };
107 };
108EOF
109}
110
111#
112# Emit the fitImage ITS DTB section
113#
114# $1 ... Image counter
115# $2 ... Path to DTB image
116fitimage_emit_section_dtb() {
117
118 if [ -n "${IMAGE_HASH_ALGO}" ] ; then
119 dtb_csum="${IMAGE_HASH_ALGO}"
120 else
121 dtb_csum="sha256"
122 fi
123
124 cat << EOF >> fit-image.its
125 fdt@${1} {
126 description = "Flattened Device Tree blob";
127 data = /incbin/("${2}");
128 type = "flat_dt";
129 arch = "${UBOOT_ARCH}";
130 compression = "none";
131 load = <${DTB_LOADADDRESS}>;
132 hash@1 {
133 algo = "${dtb_csum}";
134 };
135 };
136EOF
137}
138
139#
140# Emit the fitImage ITS configuration section
141#
142# $1 ... Linux kernel ID
143# $2 ... DTB image ID
144fitimage_emit_section_config() {
145
146 if [ -n "${VB_HASH_ALGO}" -a -n "${VB_RSA_ALGO}" ] ; then
147 conf_csum="${VB_HASH_ALGO},${VB_RSA_ALGO}"
148 else
149 conf_csum="sha256,rsa2048"
150 fi
151 conf_key_name="dev"
152
153 # Test if we have any DTBs at all
154 if [ -z "${2}" ] ; then
155 conf_desc="Boot Linux kernel"
156 fdt_line=""
157 else
158 conf_desc="Boot Linux kernel with FDT blob"
159 fdt_line="fdt = \"fdt@${2}\";"
160 fi
161 kernel_line="kernel = \"kernel@${1}\";"
162
163 cat << EOF >> fit-image.its
164 default = "conf@1";
165 conf@1 {
166 description = "${conf_desc}";
167 ${kernel_line}
168 ${fdt_line}
169 signature@1 {
170 algo = "${conf_csum}";
171 key-name-hint="${conf_key_name}";
172 sign-images="fdt","kernel";
173 };
174 };
175EOF
176}
177
178do_assemble_fitimage() {
179 cd ${B}
180 if [ "x${KERNEL_IMAGETYPE}" = "xfitImage" ] ; then
181 kernelcount=1
182 dtbcount=""
183 rm -f fit-image.its
184
185 fitimage_emit_fit_header
186
187 #
188 # Step 1: Prepare a kernel image section.
189 #
190 fitimage_emit_section_maint imagestart
191
192 uboot_prep_kimage
193 fitimage_emit_section_kernel "${kernelcount}" linux.bin "${linux_comp}"
194
195 #
196 # Step 2: Prepare a DTB image section
197 #
198 if [ -n "${KERNEL_DEVICETREE}" ] ; then
199 dtbcount=1
200 for DTB in ${KERNEL_DEVICETREE}; do
201 if echo ${DTB} | grep -q '/dts/'; then
202 bbwarn "${DTB} contains the full path to the the dts file, but only the dtb name should be used."
203 DTB=`basename ${DTB} | sed 's,\.dts$,.dtb,g'`
204 fi
205
206 # go through device tree blob dir for 32/64 bits kernel
207 DTB_PATH="arch/${ARCH}/boot/dts/mediatek/${DTB}"
208 if [ ! -e "${DTB_PATH}" ]; then
209 DTB_PATH="arch/${ARCH}/boot/dts/${DTB}"
210 if [ ! -e "${DTB_PATH}" ]; then
211 DTB_PATH="arch/${ARCH}/boot/${DTB}"
212 fi
213 fi
214
215 fitimage_emit_section_dtb ${dtbcount} ${DTB_PATH}
216 dtbcount=`expr ${dtbcount} + 1`
217 done
218 fi
219
220 fitimage_emit_section_maint sectend
221
222 # Force the first Kernel and DTB in the default config
223 kernelcount=1
224 dtbcount=1
225
226 #
227 # Step 3: Prepare a configurations section
228 #
229 fitimage_emit_section_maint confstart
230
231 fitimage_emit_section_config ${kernelcount} ${dtbcount}
232
233 fitimage_emit_section_maint sectend
234
235 fitimage_emit_section_maint fitend
236
237 #
238 # Step 4: Assemble the image
239 #
240
241 ${HSM_ENV} HSM_KEY_NAME=${VERIFIED_KEY} uboot-mkimage -f fit-image.its arch/${ARCH}/boot/fitImage
242
243 if [ "${SECURE_BOOT_ENABLE}" = "yes" -a "${STANDALONE_SIGN_PREPARE}" != "yes" ]; then
244 mkdir -p ./mykeys
245 cp ${MTK_KEY_DIR}/${VERIFIED_KEY}.crt ./mykeys/dev.crt
246 cp ${MTK_KEY_DIR}/${VERIFIED_KEY}.pem ./mykeys/dev.key
247 ${HSM_ENV} HSM_KEY_NAME=${VERIFIED_KEY} uboot-mkimage -D "-I dts -O dtb -p 1024" -k ./mykeys -f fit-image.its -r arch/${ARCH}/boot/fitImage
248 fi
249 fi
250}
251
252addtask assemble_fitimage before do_install after do_compile_kernelmodules
253
254kernel_do_deploy[vardepsexclude] = "DATETIME"
255kernel_do_deploy_append() {
256 # Update deploy directory
257 if [ "x${KERNEL_IMAGETYPE}" = "xfitImage" ] ; then
258 cd ${B}
259 echo "Copying fit-image.its source file..."
260 its_base_name="${KERNEL_IMAGETYPE}-its-${PV}-${PR}-${MACHINE}-${DATETIME}"
261 its_symlink_name=${KERNEL_IMAGETYPE}-its-${MACHINE}
262 install -m 0644 fit-image.its ${DEPLOYDIR}/${its_base_name}.its
263 linux_bin_base_name="${KERNEL_IMAGETYPE}-linux.bin-${PV}-${PR}-${MACHINE}-${DATETIME}"
264 linux_bin_symlink_name=${KERNEL_IMAGETYPE}-linux.bin-${MACHINE}
265 install -m 0644 linux.bin ${DEPLOYDIR}/${linux_bin_base_name}.bin
266 if [ -n "${RECOVERY_KERNEL_DEVICETREE}" ] ; then
267 find arch/${ARCH}/boot/dts -name "${RECOVERY_KERNEL_DEVICETREE}" -exec install -m 0644 {} ${DEPLOYDIR}/${RECOVERY_KERNEL_DEVICETREE} \;
268 fi
269
270 cd ${DEPLOYDIR}
271 ln -sf ${its_base_name}.its ${its_symlink_name}.its
272 ln -sf ${linux_bin_base_name}.bin ${linux_bin_symlink_name}.bin
273 ln -nfs ${KERNEL_IMAGETYPE}-${KERNEL_IMAGE_NAME}.bin boot.img
274 fi
275}