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