blob: d92d7ab4c5d4322a6f14a040e4529e4667672cb8 [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 kernel_csum="sha256"
81
82 ENTRYPOINT=${UBOOT_ENTRYPOINT}
83 if test -n "${UBOOT_ENTRYSYMBOL}"; then
84 ENTRYPOINT=`${HOST_PREFIX}nm ${S}/vmlinux | \
85 awk '$3=="${UBOOT_ENTRYSYMBOL}" {print $1}'`
86 fi
87
88 cat << EOF >> fit-recovery-image.its
89 kernel@${1} {
90 description = "Linux kernel";
91 data = /incbin/("${2}");
92 type = "kernel";
93 arch = "${UBOOT_ARCH}";
94 os = "linux";
95 compression = "${3}";
96 load = <${UBOOT_LOADADDRESS}>;
97 entry = <${ENTRYPOINT}>;
98 hash@1 {
99 algo = "${kernel_csum}";
100 };
101 };
102EOF
103}
104
105#
106# Emit the fitImage ITS DTB section
107#
108# $1 ... Image counter
109# $2 ... Path to DTB image
110fit_recovery_image_emit_section_dtb() {
111
112 dtb_csum="sha256"
113
114 cat << EOF >> fit-recovery-image.its
115 fdt@${1} {
116 description = "Flattened Device Tree blob";
117 data = /incbin/("${2}");
118 type = "flat_dt";
119 arch = "${UBOOT_ARCH}";
120 compression = "none";
121 load = <${DTB_LOADADDRESS}>;
122 hash@1 {
123 algo = "${dtb_csum}";
124 };
125 };
126EOF
127}
128
129#
130# Emit the fitImage ITS configuration section
131#
132# $1 ... Linux kernel ID
133# $2 ... DTB image ID
134fit_recovery_image_emit_section_config() {
135
136 conf_csum="sha256,rsa2048"
137 conf_key_name="dev"
138
139 # Test if we have any DTBs at all
140 if [ -z "${2}" ] ; then
141 conf_desc="Boot Linux kernel"
142 fdt_line=""
143 else
144 conf_desc="Boot Linux kernel with FDT blob"
145 fdt_line="fdt = \"fdt@${2}\";"
146 fi
147 kernel_line="kernel = \"kernel@${1}\";"
148
149 cat << EOF >> fit-recovery-image.its
150 default = "conf@1";
151 conf@1 {
152 description = "${conf_desc}";
153 ${kernel_line}
154 ${fdt_line}
155 signature@1 {
156 algo = "${conf_csum}";
157 key-name-hint="${conf_key_name}";
158 sign-images="fdt","kernel";
159 };
160 };
161EOF
162}
163
164do_assemble_recovery_fitimage() {
165 cd ${B}
166 if test "x${KERNEL_IMAGETYPE}" = "xfitImage" && test -n "${RECOVERY_KERNEL_DEVICETREE}"; then
167 kernelcount=1
168 dtbcount=""
169 rm -f fit-recovery-image.its
170
171 fit_recovery_image_emit_fit_header
172
173 #
174 # Step 1: Prepare a kernel image section.
175 #
176 fit_recovery_image_emit_section_maint imagestart
177
178 uboot_prep_kimage
179 fit_recovery_image_emit_section_kernel "${kernelcount}" linux.bin "${linux_comp}"
180
181 #
182 # Step 2: Prepare a DTB image section
183 #
184 dtbcount=1
185 for DTB in ${RECOVERY_KERNEL_DEVICETREE}; do
186 if echo ${DTB} | grep -q '/dts/'; then
187 bbwarn "${DTB} contains the full path to the the dts file, but only the dtb name should be used."
188 DTB=`basename ${DTB} | sed 's,\.dts$,.dtb,g'`
189 fi
190
191 # go through device tree blob dir for 32/64 bits kernel
192 DTB_PATH="arch/${ARCH}/boot/dts/mediatek/${DTB}"
193 if [ ! -e "${DTB_PATH}" ]; then
194 DTB_PATH="arch/${ARCH}/boot/dts/${DTB}"
195 if [ ! -e "${DTB_PATH}" ]; then
196 DTB_PATH="arch/${ARCH}/boot/${DTB}"
197 fi
198 fi
199
200 fit_recovery_image_emit_section_dtb ${dtbcount} ${DTB_PATH}
201 dtbcount=`expr ${dtbcount} + 1`
202 done
203
204 fit_recovery_image_emit_section_maint sectend
205 # Force the first Kernel and DTB in the default config
206 kernelcount=1
207 dtbcount=1
208
209 #
210 # Step 3: Prepare a configurations section
211 #
212 fit_recovery_image_emit_section_maint confstart
213
214 fit_recovery_image_emit_section_config ${kernelcount} ${dtbcount}
215
216 fit_recovery_image_emit_section_maint sectend
217
218 fit_recovery_image_emit_section_maint fitend
219
220 #
221 # Step 4: Assemble the image
222 #
223 uboot-mkimage -f fit-recovery-image.its arch/${ARCH}/boot/fitRecoveryImage
224
225 if [ "${SECURE_BOOT_ENABLE}" = "yes" ]; then
226 mkdir -p ./mykeys
227 cp ${MTK_KEY_DIR}/${VERIFIED_KEY}.crt ./mykeys/dev.crt
228 cp ${MTK_KEY_DIR}/${VERIFIED_KEY}.pem ./mykeys/dev.key
229 uboot-mkimage -D "-I dts -O dtb -p 1024" -k ./mykeys -f fit-recovery-image.its -r arch/${ARCH}/boot/fitRecoveryImage
230 fi
231 fi
232}
233
234addtask assemble_recovery_fitimage before do_install after do_compile
235
236kernel_do_deploy_append() {
237 # Update deploy directory
238 if test "x${KERNEL_IMAGETYPE}" = "xfitImage" && test -n "${RECOVERY_KERNEL_DEVICETREE}"; then
239 cd ${B}
240 install -m 0644 fit-recovery-image.its ${DEPLOYDIR}/fit-recovery-image.its
241 install -m 0644 arch/${ARCH}/boot/fitRecoveryImage ${DEPLOYDIR}/recovery.img
242 fi
243}