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