Commit 4d73a26bba413bb0040283f37d2dd5928dc80b0d

Authored by Ji Luo
1 parent 8cb9cd7e43

MA-16594-1 Set dtb load offset in bootloader

We use the 'second_addr' in boot image header to store the
fdt load address which is set by '--second_offset' parameter,
but actually we don't pass any 'second' image to it. Now the
mkbootimg.py will check the 'second' image size and set the
'second_addr' as 0x0 if no 'second' image is specified.

Since we don't store dtbs in the boot.img (except recovery dtb
for legacy platforms), so set the dtb load offset in bootloader.
The dtb load address will be 'kernel_addr + FDT_OFFSET_TO_KERNEL'
(48MB memory reserved).

Test: Boots on imx8mq.

Change-Id: Ie597731992107ec2343e5ab6db0cab0e123f009c
Signed-off-by: Ji Luo <ji.luo@nxp.com>
(cherry picked from commit efb9b0c9322f531c15ceedccd2fa9c6c170003a2)

Showing 1 changed file with 15 additions and 14 deletions Inline Diff

drivers/fastboot/fb_fsl/fb_fsl_boot.c
1 // SPDX-License-Identifier: GPL-2.0+ 1 // SPDX-License-Identifier: GPL-2.0+
2 /* 2 /*
3 * Copyright 2019 NXP 3 * Copyright 2019 NXP
4 */ 4 */
5 5
6 #include <asm/mach-imx/sys_proto.h> 6 #include <asm/mach-imx/sys_proto.h>
7 #include <fb_fsl.h> 7 #include <fb_fsl.h>
8 #include <fastboot.h> 8 #include <fastboot.h>
9 #include <mmc.h> 9 #include <mmc.h>
10 #include <android_image.h> 10 #include <android_image.h>
11 #include <asm/bootm.h> 11 #include <asm/bootm.h>
12 #include <nand.h> 12 #include <nand.h>
13 #include <part.h> 13 #include <part.h>
14 #include <sparse_format.h> 14 #include <sparse_format.h>
15 #include <image-sparse.h> 15 #include <image-sparse.h>
16 #include <image.h> 16 #include <image.h>
17 #include <asm/mach-imx/boot_mode.h> 17 #include <asm/mach-imx/boot_mode.h>
18 #include <asm/arch/sys_proto.h> 18 #include <asm/arch/sys_proto.h>
19 #include <asm/setup.h> 19 #include <asm/setup.h>
20 #include <env.h> 20 #include <env.h>
21 #include "../lib/avb/fsl/utils.h" 21 #include "../lib/avb/fsl/utils.h"
22 22
23 #ifdef CONFIG_AVB_SUPPORT 23 #ifdef CONFIG_AVB_SUPPORT
24 #include <dt_table.h> 24 #include <dt_table.h>
25 #include <fsl_avb.h> 25 #include <fsl_avb.h>
26 #endif 26 #endif
27 27
28 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 28 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
29 #include <asm-generic/gpio.h> 29 #include <asm-generic/gpio.h>
30 #include <asm/mach-imx/gpio.h> 30 #include <asm/mach-imx/gpio.h>
31 #include "../lib/avb/fsl/fsl_avbkey.h" 31 #include "../lib/avb/fsl/fsl_avbkey.h"
32 #include "../arch/arm/include/asm/mach-imx/hab.h" 32 #include "../arch/arm/include/asm/mach-imx/hab.h"
33 #endif 33 #endif
34 34
35 #if defined(CONFIG_FASTBOOT_LOCK) 35 #if defined(CONFIG_FASTBOOT_LOCK)
36 #include "fastboot_lock_unlock.h" 36 #include "fastboot_lock_unlock.h"
37 #endif 37 #endif
38 38
39 #ifdef CONFIG_IMX_TRUSTY_OS 39 #ifdef CONFIG_IMX_TRUSTY_OS
40 #include "u-boot/sha256.h" 40 #include "u-boot/sha256.h"
41 #include <trusty/libtipc.h> 41 #include <trusty/libtipc.h>
42 #endif 42 #endif
43 43
44 #include "fb_fsl_common.h" 44 #include "fb_fsl_common.h"
45 45
46 #if defined (CONFIG_ARCH_IMX8) || defined (CONFIG_ARCH_IMX8M) 46 #if defined (CONFIG_ARCH_IMX8) || defined (CONFIG_ARCH_IMX8M)
47 #define DST_DECOMPRESS_LEN 1024*1024*32 47 #define DST_DECOMPRESS_LEN 1024*1024*32
48 #endif 48 #endif
49 49
50 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 50 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
51 #define FDT_PART_NAME "oem_bootloader" 51 #define FDT_PART_NAME "oem_bootloader"
52 #else 52 #else
53 #define FDT_PART_NAME "dtbo" 53 #define FDT_PART_NAME "dtbo"
54 #endif 54 #endif
55 55
56 /* Offset (in u32's) of start and end fields in the zImage header. */ 56 /* Offset (in u32's) of start and end fields in the zImage header. */
57 #define ZIMAGE_START_ADDR 10 57 #define ZIMAGE_START_ADDR 10
58 #define ZIMAGE_END_ADDR 11 58 #define ZIMAGE_END_ADDR 11
59 59
60 #define FDT_OFFSET_TO_KERNEL 0x3000000 /* device tree blob offset to the kernel image */
61
60 /* Boot metric variables */ 62 /* Boot metric variables */
61 boot_metric metrics = { 63 boot_metric metrics = {
62 .bll_1 = 0, 64 .bll_1 = 0,
63 .ble_1 = 0, 65 .ble_1 = 0,
64 .kl = 0, 66 .kl = 0,
65 .kd = 0, 67 .kd = 0,
66 .avb = 0, 68 .avb = 0,
67 .odt = 0, 69 .odt = 0,
68 .sw = 0 70 .sw = 0
69 }; 71 };
70 72
71 int read_from_partition_multi(const char* partition, 73 int read_from_partition_multi(const char* partition,
72 int64_t offset, size_t num_bytes, void* buffer, size_t* out_num_read) 74 int64_t offset, size_t num_bytes, void* buffer, size_t* out_num_read)
73 { 75 {
74 struct fastboot_ptentry *pte; 76 struct fastboot_ptentry *pte;
75 unsigned char *bdata; 77 unsigned char *bdata;
76 unsigned char *out_buf = (unsigned char *)buffer; 78 unsigned char *out_buf = (unsigned char *)buffer;
77 unsigned char *dst, *dst64 = NULL; 79 unsigned char *dst, *dst64 = NULL;
78 unsigned long blksz; 80 unsigned long blksz;
79 unsigned long s, cnt; 81 unsigned long s, cnt;
80 size_t num_read = 0; 82 size_t num_read = 0;
81 lbaint_t part_start, part_end, bs, be, bm, blk_num; 83 lbaint_t part_start, part_end, bs, be, bm, blk_num;
82 margin_pos_t margin; 84 margin_pos_t margin;
83 struct blk_desc *fs_dev_desc = NULL; 85 struct blk_desc *fs_dev_desc = NULL;
84 int dev_no; 86 int dev_no;
85 int ret; 87 int ret;
86 88
87 assert(buffer != NULL && out_num_read != NULL); 89 assert(buffer != NULL && out_num_read != NULL);
88 90
89 dev_no = mmc_get_env_dev(); 91 dev_no = mmc_get_env_dev();
90 if ((fs_dev_desc = blk_get_dev("mmc", dev_no)) == NULL) { 92 if ((fs_dev_desc = blk_get_dev("mmc", dev_no)) == NULL) {
91 printf("mmc device not found\n"); 93 printf("mmc device not found\n");
92 return -1; 94 return -1;
93 } 95 }
94 96
95 pte = fastboot_flash_find_ptn(partition); 97 pte = fastboot_flash_find_ptn(partition);
96 if (!pte) { 98 if (!pte) {
97 printf("no %s partition\n", partition); 99 printf("no %s partition\n", partition);
98 fastboot_flash_dump_ptn(); 100 fastboot_flash_dump_ptn();
99 return -1; 101 return -1;
100 } 102 }
101 103
102 blksz = fs_dev_desc->blksz; 104 blksz = fs_dev_desc->blksz;
103 part_start = pte->start; 105 part_start = pte->start;
104 part_end = pte->start + pte->length - 1; 106 part_end = pte->start + pte->length - 1;
105 107
106 if (get_margin_pos((uint64_t)part_start, (uint64_t)part_end, blksz, 108 if (get_margin_pos((uint64_t)part_start, (uint64_t)part_end, blksz,
107 &margin, offset, num_bytes, true)) 109 &margin, offset, num_bytes, true))
108 return -1; 110 return -1;
109 111
110 bs = (lbaint_t)margin.blk_start; 112 bs = (lbaint_t)margin.blk_start;
111 be = (lbaint_t)margin.blk_end; 113 be = (lbaint_t)margin.blk_end;
112 s = margin.start; 114 s = margin.start;
113 bm = margin.multi; 115 bm = margin.multi;
114 116
115 /* alloc a blksz mem */ 117 /* alloc a blksz mem */
116 bdata = (unsigned char *)memalign(ALIGN_BYTES, blksz); 118 bdata = (unsigned char *)memalign(ALIGN_BYTES, blksz);
117 if (bdata == NULL) { 119 if (bdata == NULL) {
118 printf("Failed to allocate memory!\n"); 120 printf("Failed to allocate memory!\n");
119 return -1; 121 return -1;
120 } 122 }
121 123
122 /* support multi blk read */ 124 /* support multi blk read */
123 while (bs <= be) { 125 while (bs <= be) {
124 if (!s && bm > 1) { 126 if (!s && bm > 1) {
125 dst = out_buf; 127 dst = out_buf;
126 dst64 = PTR_ALIGN(out_buf, 64); /* for mmc blk read alignment */ 128 dst64 = PTR_ALIGN(out_buf, 64); /* for mmc blk read alignment */
127 if (dst64 != dst) { 129 if (dst64 != dst) {
128 dst = dst64; 130 dst = dst64;
129 bm--; 131 bm--;
130 } 132 }
131 blk_num = bm; 133 blk_num = bm;
132 cnt = bm * blksz; 134 cnt = bm * blksz;
133 bm = 0; /* no more multi blk */ 135 bm = 0; /* no more multi blk */
134 } else { 136 } else {
135 blk_num = 1; 137 blk_num = 1;
136 cnt = blksz - s; 138 cnt = blksz - s;
137 if (num_read + cnt > num_bytes) 139 if (num_read + cnt > num_bytes)
138 cnt = num_bytes - num_read; 140 cnt = num_bytes - num_read;
139 dst = bdata; 141 dst = bdata;
140 } 142 }
141 if (blk_dread(fs_dev_desc, bs, blk_num, dst) != blk_num) { 143 if (blk_dread(fs_dev_desc, bs, blk_num, dst) != blk_num) {
142 ret = -1; 144 ret = -1;
143 goto fail; 145 goto fail;
144 } 146 }
145 147
146 if (dst == bdata) 148 if (dst == bdata)
147 memcpy(out_buf, bdata + s, cnt); 149 memcpy(out_buf, bdata + s, cnt);
148 else if (dst == dst64) 150 else if (dst == dst64)
149 memcpy(out_buf, dst, cnt); /* internal copy */ 151 memcpy(out_buf, dst, cnt); /* internal copy */
150 152
151 s = 0; 153 s = 0;
152 bs += blk_num; 154 bs += blk_num;
153 num_read += cnt; 155 num_read += cnt;
154 out_buf += cnt; 156 out_buf += cnt;
155 } 157 }
156 *out_num_read = num_read; 158 *out_num_read = num_read;
157 ret = 0; 159 ret = 0;
158 160
159 fail: 161 fail:
160 free(bdata); 162 free(bdata);
161 return ret; 163 return ret;
162 } 164 }
163 165
164 166
165 #if defined(CONFIG_FASTBOOT_LOCK) 167 #if defined(CONFIG_FASTBOOT_LOCK)
166 int do_lock_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { 168 int do_lock_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) {
167 FbLockState status = fastboot_get_lock_stat(); 169 FbLockState status = fastboot_get_lock_stat();
168 if (status != FASTBOOT_LOCK_ERROR) { 170 if (status != FASTBOOT_LOCK_ERROR) {
169 if (status == FASTBOOT_LOCK) 171 if (status == FASTBOOT_LOCK)
170 printf("fastboot lock status: locked.\n"); 172 printf("fastboot lock status: locked.\n");
171 else 173 else
172 printf("fastboot lock status: unlocked.\n"); 174 printf("fastboot lock status: unlocked.\n");
173 } else 175 } else
174 printf("fastboot lock status error!\n"); 176 printf("fastboot lock status error!\n");
175 177
176 display_lock(status, -1); 178 display_lock(status, -1);
177 179
178 return 0; 180 return 0;
179 181
180 } 182 }
181 183
182 U_BOOT_CMD( 184 U_BOOT_CMD(
183 lock_status, 2, 1, do_lock_status, 185 lock_status, 2, 1, do_lock_status,
184 "lock_status", 186 "lock_status",
185 "lock_status"); 187 "lock_status");
186 #endif 188 #endif
187 189
188 #if defined(CONFIG_FLASH_MCUFIRMWARE_SUPPORT) && defined(CONFIG_ARCH_IMX8M) 190 #if defined(CONFIG_FLASH_MCUFIRMWARE_SUPPORT) && defined(CONFIG_ARCH_IMX8M)
189 static int do_bootmcu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 191 static int do_bootmcu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
190 { 192 {
191 int ret; 193 int ret;
192 size_t out_num_read; 194 size_t out_num_read;
193 void *mcu_base_addr = (void *)MCU_BOOTROM_BASE_ADDR; 195 void *mcu_base_addr = (void *)MCU_BOOTROM_BASE_ADDR;
194 char command[32]; 196 char command[32];
195 197
196 ret = read_from_partition_multi(FASTBOOT_MCU_FIRMWARE_PARTITION, 198 ret = read_from_partition_multi(FASTBOOT_MCU_FIRMWARE_PARTITION,
197 0, ANDROID_MCU_FIRMWARE_SIZE, (void *)mcu_base_addr, &out_num_read); 199 0, ANDROID_MCU_FIRMWARE_SIZE, (void *)mcu_base_addr, &out_num_read);
198 if ((ret != 0) || (out_num_read != ANDROID_MCU_FIRMWARE_SIZE)) { 200 if ((ret != 0) || (out_num_read != ANDROID_MCU_FIRMWARE_SIZE)) {
199 printf("Read MCU images failed!\n"); 201 printf("Read MCU images failed!\n");
200 return 1; 202 return 1;
201 } else { 203 } else {
202 printf("run command: 'bootaux 0x%x'\n",(unsigned int)(ulong)mcu_base_addr); 204 printf("run command: 'bootaux 0x%x'\n",(unsigned int)(ulong)mcu_base_addr);
203 205
204 sprintf(command, "bootaux 0x%x", (unsigned int)(ulong)mcu_base_addr); 206 sprintf(command, "bootaux 0x%x", (unsigned int)(ulong)mcu_base_addr);
205 ret = run_command(command, 0); 207 ret = run_command(command, 0);
206 if (ret) { 208 if (ret) {
207 printf("run 'bootaux' command failed!\n"); 209 printf("run 'bootaux' command failed!\n");
208 return 1; 210 return 1;
209 } 211 }
210 } 212 }
211 return 0; 213 return 0;
212 } 214 }
213 215
214 U_BOOT_CMD( 216 U_BOOT_CMD(
215 bootmcu, 1, 0, do_bootmcu, 217 bootmcu, 1, 0, do_bootmcu,
216 "boot mcu images\n", 218 "boot mcu images\n",
217 "boot mcu images from 'mcu_os' partition, only support images run from TCM" 219 "boot mcu images from 'mcu_os' partition, only support images run from TCM"
218 ); 220 );
219 #endif 221 #endif
220 222
221 #if defined CONFIG_SYSTEM_RAMDISK_SUPPORT && defined CONFIG_ANDROID_AUTO_SUPPORT 223 #if defined CONFIG_SYSTEM_RAMDISK_SUPPORT && defined CONFIG_ANDROID_AUTO_SUPPORT
222 /* Setup booargs for taking the system parition as ramdisk */ 224 /* Setup booargs for taking the system parition as ramdisk */
223 static void fastboot_setup_system_boot_args(const char *slot, bool append_root) 225 static void fastboot_setup_system_boot_args(const char *slot, bool append_root)
224 { 226 {
225 const char *system_part_name = NULL; 227 const char *system_part_name = NULL;
226 #ifdef CONFIG_ANDROID_AB_SUPPORT 228 #ifdef CONFIG_ANDROID_AB_SUPPORT
227 if(slot == NULL) 229 if(slot == NULL)
228 return; 230 return;
229 if(!strncmp(slot, "_a", strlen("_a")) || !strncmp(slot, "boot_a", strlen("boot_a"))) { 231 if(!strncmp(slot, "_a", strlen("_a")) || !strncmp(slot, "boot_a", strlen("boot_a"))) {
230 system_part_name = FASTBOOT_PARTITION_SYSTEM_A; 232 system_part_name = FASTBOOT_PARTITION_SYSTEM_A;
231 } 233 }
232 else if(!strncmp(slot, "_b", strlen("_b")) || !strncmp(slot, "boot_b", strlen("boot_b"))) { 234 else if(!strncmp(slot, "_b", strlen("_b")) || !strncmp(slot, "boot_b", strlen("boot_b"))) {
233 system_part_name = FASTBOOT_PARTITION_SYSTEM_B; 235 system_part_name = FASTBOOT_PARTITION_SYSTEM_B;
234 } else { 236 } else {
235 printf("slot invalid!\n"); 237 printf("slot invalid!\n");
236 return; 238 return;
237 } 239 }
238 #else 240 #else
239 system_part_name = FASTBOOT_PARTITION_SYSTEM; 241 system_part_name = FASTBOOT_PARTITION_SYSTEM;
240 #endif 242 #endif
241 243
242 struct fastboot_ptentry *ptentry = fastboot_flash_find_ptn(system_part_name); 244 struct fastboot_ptentry *ptentry = fastboot_flash_find_ptn(system_part_name);
243 if(ptentry != NULL) { 245 if(ptentry != NULL) {
244 char bootargs_3rd[ANDR_BOOT_ARGS_SIZE] = {'\0'}; 246 char bootargs_3rd[ANDR_BOOT_ARGS_SIZE] = {'\0'};
245 if (append_root) { 247 if (append_root) {
246 u32 dev_no = mmc_map_to_kernel_blk(mmc_get_env_dev()); 248 u32 dev_no = mmc_map_to_kernel_blk(mmc_get_env_dev());
247 sprintf(bootargs_3rd, "root=/dev/mmcblk%dp%d ", 249 sprintf(bootargs_3rd, "root=/dev/mmcblk%dp%d ",
248 dev_no, 250 dev_no,
249 ptentry->partition_index); 251 ptentry->partition_index);
250 } 252 }
251 strcat(bootargs_3rd, "rootwait"); 253 strcat(bootargs_3rd, "rootwait");
252 254
253 env_set("bootargs_3rd", bootargs_3rd); 255 env_set("bootargs_3rd", bootargs_3rd);
254 } else { 256 } else {
255 printf("Can't find partition: %s\n", system_part_name); 257 printf("Can't find partition: %s\n", system_part_name);
256 fastboot_flash_dump_ptn(); 258 fastboot_flash_dump_ptn();
257 } 259 }
258 } 260 }
259 #endif 261 #endif
260 262
261 #ifdef CONFIG_CMD_BOOTA 263 #ifdef CONFIG_CMD_BOOTA
262 264
263 /* Section for Android bootimage format support 265 /* Section for Android bootimage format support
264 * Refer: 266 * Refer:
265 * http://android.git.kernel.org/?p=platform/system/core.git;a=blob; 267 * http://android.git.kernel.org/?p=platform/system/core.git;a=blob;
266 * f=mkbootimg/bootimg.h 268 * f=mkbootimg/bootimg.h
267 */ 269 */
268 270
269 void 271 void
270 bootimg_print_image_hdr(struct andr_img_hdr *hdr) 272 bootimg_print_image_hdr(struct andr_img_hdr *hdr)
271 { 273 {
272 #ifdef DEBUG 274 #ifdef DEBUG
273 int i; 275 int i;
274 printf(" Image magic: %s\n", hdr->magic); 276 printf(" Image magic: %s\n", hdr->magic);
275 277
276 printf(" kernel_size: 0x%x\n", hdr->kernel_size); 278 printf(" kernel_size: 0x%x\n", hdr->kernel_size);
277 printf(" kernel_addr: 0x%x\n", hdr->kernel_addr); 279 printf(" kernel_addr: 0x%x\n", hdr->kernel_addr);
278 280
279 printf(" rdisk_size: 0x%x\n", hdr->ramdisk_size); 281 printf(" rdisk_size: 0x%x\n", hdr->ramdisk_size);
280 printf(" rdisk_addr: 0x%x\n", hdr->ramdisk_addr); 282 printf(" rdisk_addr: 0x%x\n", hdr->ramdisk_addr);
281 283
282 printf(" second_size: 0x%x\n", hdr->second_size); 284 printf(" second_size: 0x%x\n", hdr->second_size);
283 printf(" second_addr: 0x%x\n", hdr->second_addr); 285 printf(" second_addr: 0x%x\n", hdr->second_addr);
284 286
285 printf(" tags_addr: 0x%x\n", hdr->tags_addr); 287 printf(" tags_addr: 0x%x\n", hdr->tags_addr);
286 printf(" page_size: 0x%x\n", hdr->page_size); 288 printf(" page_size: 0x%x\n", hdr->page_size);
287 289
288 printf(" name: %s\n", hdr->name); 290 printf(" name: %s\n", hdr->name);
289 printf(" cmdline: %s\n", hdr->cmdline); 291 printf(" cmdline: %s\n", hdr->cmdline);
290 292
291 for (i = 0; i < 8; i++) 293 for (i = 0; i < 8; i++)
292 printf(" id[%d]: 0x%x\n", i, hdr->id[i]); 294 printf(" id[%d]: 0x%x\n", i, hdr->id[i]);
293 #endif 295 #endif
294 } 296 }
295 297
296 #if !defined(CONFIG_AVB_SUPPORT) || !defined(CONFIG_MMC) 298 #if !defined(CONFIG_AVB_SUPPORT) || !defined(CONFIG_MMC)
297 static struct andr_img_hdr boothdr __aligned(ARCH_DMA_MINALIGN); 299 static struct andr_img_hdr boothdr __aligned(ARCH_DMA_MINALIGN);
298 #endif 300 #endif
299 301
300 #ifdef CONFIG_IMX_TRUSTY_OS 302 #ifdef CONFIG_IMX_TRUSTY_OS
301 #if defined(CONFIG_DUAL_BOOTLOADER) && defined(CONFIG_AVB_ATX) 303 #if defined(CONFIG_DUAL_BOOTLOADER) && defined(CONFIG_AVB_ATX)
302 static int sha256_concatenation(uint8_t *hash_buf, uint8_t *vbh, uint8_t *image_hash) 304 static int sha256_concatenation(uint8_t *hash_buf, uint8_t *vbh, uint8_t *image_hash)
303 { 305 {
304 if ((hash_buf == NULL) || (vbh == NULL) || (image_hash == NULL)) { 306 if ((hash_buf == NULL) || (vbh == NULL) || (image_hash == NULL)) {
305 printf("sha256_concatenation: null buffer found!\n"); 307 printf("sha256_concatenation: null buffer found!\n");
306 return -1; 308 return -1;
307 } 309 }
308 310
309 memcpy(hash_buf, vbh, AVB_SHA256_DIGEST_SIZE); 311 memcpy(hash_buf, vbh, AVB_SHA256_DIGEST_SIZE);
310 memcpy(hash_buf + AVB_SHA256_DIGEST_SIZE, 312 memcpy(hash_buf + AVB_SHA256_DIGEST_SIZE,
311 image_hash, AVB_SHA256_DIGEST_SIZE); 313 image_hash, AVB_SHA256_DIGEST_SIZE);
312 sha256_csum_wd((unsigned char *)hash_buf, 2 * AVB_SHA256_DIGEST_SIZE, 314 sha256_csum_wd((unsigned char *)hash_buf, 2 * AVB_SHA256_DIGEST_SIZE,
313 (unsigned char *)vbh, CHUNKSZ_SHA256); 315 (unsigned char *)vbh, CHUNKSZ_SHA256);
314 316
315 return 0; 317 return 0;
316 } 318 }
317 319
318 /* Since we use fit format to organize the atf, tee, u-boot and u-boot dtb, 320 /* Since we use fit format to organize the atf, tee, u-boot and u-boot dtb,
319 * so calculate the hash of fit is enough. 321 * so calculate the hash of fit is enough.
320 */ 322 */
321 static int vbh_bootloader(uint8_t *image_hash) 323 static int vbh_bootloader(uint8_t *image_hash)
322 { 324 {
323 char* slot_suffixes[2] = {"_a", "_b"}; 325 char* slot_suffixes[2] = {"_a", "_b"};
324 char partition_name[20]; 326 char partition_name[20];
325 AvbABData ab_data; 327 AvbABData ab_data;
326 uint8_t *image_buf = NULL; 328 uint8_t *image_buf = NULL;
327 uint32_t image_size; 329 uint32_t image_size;
328 size_t image_num_read; 330 size_t image_num_read;
329 int target_slot; 331 int target_slot;
330 int ret = 0; 332 int ret = 0;
331 333
332 /* Load A/B metadata and decide which slot we are going to load */ 334 /* Load A/B metadata and decide which slot we are going to load */
333 if (fsl_avb_ab_ops.read_ab_metadata(&fsl_avb_ab_ops, &ab_data) != 335 if (fsl_avb_ab_ops.read_ab_metadata(&fsl_avb_ab_ops, &ab_data) !=
334 AVB_IO_RESULT_OK) { 336 AVB_IO_RESULT_OK) {
335 ret = -1; 337 ret = -1;
336 goto fail ; 338 goto fail ;
337 } 339 }
338 target_slot = get_curr_slot(&ab_data); 340 target_slot = get_curr_slot(&ab_data);
339 sprintf(partition_name, "bootloader%s", slot_suffixes[target_slot]); 341 sprintf(partition_name, "bootloader%s", slot_suffixes[target_slot]);
340 342
341 /* Read image header to find the image size */ 343 /* Read image header to find the image size */
342 image_buf = (uint8_t *)malloc(MMC_SATA_BLOCK_SIZE); 344 image_buf = (uint8_t *)malloc(MMC_SATA_BLOCK_SIZE);
343 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, partition_name, 345 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, partition_name,
344 0, MMC_SATA_BLOCK_SIZE, 346 0, MMC_SATA_BLOCK_SIZE,
345 image_buf, &image_num_read)) { 347 image_buf, &image_num_read)) {
346 printf("bootloader image load error!\n"); 348 printf("bootloader image load error!\n");
347 ret = -1; 349 ret = -1;
348 goto fail; 350 goto fail;
349 } 351 }
350 image_size = fdt_totalsize((struct image_header *)image_buf); 352 image_size = fdt_totalsize((struct image_header *)image_buf);
351 image_size = (image_size + 3) & ~3; 353 image_size = (image_size + 3) & ~3;
352 free(image_buf); 354 free(image_buf);
353 355
354 /* Load full fit image */ 356 /* Load full fit image */
355 image_buf = (uint8_t *)malloc(image_size); 357 image_buf = (uint8_t *)malloc(image_size);
356 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, partition_name, 358 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, partition_name,
357 0, image_size, 359 0, image_size,
358 image_buf, &image_num_read)) { 360 image_buf, &image_num_read)) {
359 printf("bootloader image load error!\n"); 361 printf("bootloader image load error!\n");
360 ret = -1; 362 ret = -1;
361 goto fail; 363 goto fail;
362 } 364 }
363 /* Calculate hash */ 365 /* Calculate hash */
364 sha256_csum_wd((unsigned char *)image_buf, image_size, 366 sha256_csum_wd((unsigned char *)image_buf, image_size,
365 (unsigned char *)image_hash, CHUNKSZ_SHA256); 367 (unsigned char *)image_hash, CHUNKSZ_SHA256);
366 368
367 fail: 369 fail:
368 if (image_buf != NULL) 370 if (image_buf != NULL)
369 free(image_buf); 371 free(image_buf);
370 return ret; 372 return ret;
371 } 373 }
372 374
373 int vbh_calculate(uint8_t *vbh, AvbSlotVerifyData *avb_out_data) 375 int vbh_calculate(uint8_t *vbh, AvbSlotVerifyData *avb_out_data)
374 { 376 {
375 uint8_t image_hash[AVB_SHA256_DIGEST_SIZE]; 377 uint8_t image_hash[AVB_SHA256_DIGEST_SIZE];
376 uint8_t hash_buf[2 * AVB_SHA256_DIGEST_SIZE]; 378 uint8_t hash_buf[2 * AVB_SHA256_DIGEST_SIZE];
377 uint8_t* image_buf = NULL; 379 uint8_t* image_buf = NULL;
378 uint32_t image_size; 380 uint32_t image_size;
379 size_t image_num_read; 381 size_t image_num_read;
380 int ret = 0; 382 int ret = 0;
381 383
382 if (vbh == NULL) 384 if (vbh == NULL)
383 return -1; 385 return -1;
384 386
385 /* Initial VBH (VBH0) should be 32 bytes 0 */ 387 /* Initial VBH (VBH0) should be 32 bytes 0 */
386 memset(vbh, 0, AVB_SHA256_DIGEST_SIZE); 388 memset(vbh, 0, AVB_SHA256_DIGEST_SIZE);
387 /* Load and calculate the sha256 hash of spl.bin */ 389 /* Load and calculate the sha256 hash of spl.bin */
388 image_size = (ANDROID_SPL_SIZE + MMC_SATA_BLOCK_SIZE -1) / 390 image_size = (ANDROID_SPL_SIZE + MMC_SATA_BLOCK_SIZE -1) /
389 MMC_SATA_BLOCK_SIZE; 391 MMC_SATA_BLOCK_SIZE;
390 image_buf = (uint8_t *)malloc(image_size); 392 image_buf = (uint8_t *)malloc(image_size);
391 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops, 393 if (fsl_avb_ops.read_from_partition(&fsl_avb_ops,
392 FASTBOOT_PARTITION_BOOTLOADER, 394 FASTBOOT_PARTITION_BOOTLOADER,
393 0, image_size, 395 0, image_size,
394 image_buf, &image_num_read)) { 396 image_buf, &image_num_read)) {
395 printf("spl image load error!\n"); 397 printf("spl image load error!\n");
396 ret = -1; 398 ret = -1;
397 goto fail; 399 goto fail;
398 } 400 }
399 sha256_csum_wd((unsigned char *)image_buf, image_size, 401 sha256_csum_wd((unsigned char *)image_buf, image_size,
400 (unsigned char *)image_hash, CHUNKSZ_SHA256); 402 (unsigned char *)image_hash, CHUNKSZ_SHA256);
401 /* Calculate VBH1 */ 403 /* Calculate VBH1 */
402 if (sha256_concatenation(hash_buf, vbh, image_hash)) { 404 if (sha256_concatenation(hash_buf, vbh, image_hash)) {
403 ret = -1; 405 ret = -1;
404 goto fail; 406 goto fail;
405 } 407 }
406 free(image_buf); 408 free(image_buf);
407 409
408 /* Load and calculate hash of bootloader.img */ 410 /* Load and calculate hash of bootloader.img */
409 if (vbh_bootloader(image_hash)) { 411 if (vbh_bootloader(image_hash)) {
410 ret = -1; 412 ret = -1;
411 goto fail; 413 goto fail;
412 } 414 }
413 415
414 /* Calculate VBH2 */ 416 /* Calculate VBH2 */
415 if (sha256_concatenation(hash_buf, vbh, image_hash)) { 417 if (sha256_concatenation(hash_buf, vbh, image_hash)) {
416 ret = -1; 418 ret = -1;
417 goto fail; 419 goto fail;
418 } 420 }
419 421
420 /* Calculate the hash of vbmeta.img */ 422 /* Calculate the hash of vbmeta.img */
421 avb_slot_verify_data_calculate_vbmeta_digest(avb_out_data, 423 avb_slot_verify_data_calculate_vbmeta_digest(avb_out_data,
422 AVB_DIGEST_TYPE_SHA256, 424 AVB_DIGEST_TYPE_SHA256,
423 image_hash); 425 image_hash);
424 /* Calculate VBH3 */ 426 /* Calculate VBH3 */
425 if (sha256_concatenation(hash_buf, vbh, image_hash)) { 427 if (sha256_concatenation(hash_buf, vbh, image_hash)) {
426 ret = -1; 428 ret = -1;
427 goto fail; 429 goto fail;
428 } 430 }
429 431
430 fail: 432 fail:
431 if (image_buf != NULL) 433 if (image_buf != NULL)
432 free(image_buf); 434 free(image_buf);
433 return ret; 435 return ret;
434 } 436 }
435 #endif /* CONFIG_DUAL_BOOTLOADER && CONFIG_AVB_ATX */ 437 #endif /* CONFIG_DUAL_BOOTLOADER && CONFIG_AVB_ATX */
436 438
437 int trusty_setbootparameter(struct andr_img_hdr *hdr, AvbABFlowResult avb_result, 439 int trusty_setbootparameter(struct andr_img_hdr *hdr, AvbABFlowResult avb_result,
438 AvbSlotVerifyData *avb_out_data) { 440 AvbSlotVerifyData *avb_out_data) {
439 #if defined(CONFIG_DUAL_BOOTLOADER) && defined(CONFIG_AVB_ATX) 441 #if defined(CONFIG_DUAL_BOOTLOADER) && defined(CONFIG_AVB_ATX)
440 uint8_t vbh[AVB_SHA256_DIGEST_SIZE]; 442 uint8_t vbh[AVB_SHA256_DIGEST_SIZE];
441 #endif 443 #endif
442 int ret = 0; 444 int ret = 0;
443 u32 os_ver = hdr->os_version >> 11; 445 u32 os_ver = hdr->os_version >> 11;
444 u32 os_ver_km = (((os_ver >> 14) & 0x7F) * 100 + ((os_ver >> 7) & 0x7F)) * 100 446 u32 os_ver_km = (((os_ver >> 14) & 0x7F) * 100 + ((os_ver >> 7) & 0x7F)) * 100
445 + (os_ver & 0x7F); 447 + (os_ver & 0x7F);
446 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 448 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
447 u32 os_lvl_km = ((os_lvl >> 4) + 2000) * 100 + (os_lvl & 0x0F); 449 u32 os_lvl_km = ((os_lvl >> 4) + 2000) * 100 + (os_lvl & 0x0F);
448 keymaster_verified_boot_t vbstatus; 450 keymaster_verified_boot_t vbstatus;
449 FbLockState lock_status = fastboot_get_lock_stat(); 451 FbLockState lock_status = fastboot_get_lock_stat();
450 452
451 uint8_t boot_key_hash[AVB_SHA256_DIGEST_SIZE]; 453 uint8_t boot_key_hash[AVB_SHA256_DIGEST_SIZE];
452 #ifdef CONFIG_AVB_ATX 454 #ifdef CONFIG_AVB_ATX
453 if (fsl_read_permanent_attributes_hash(&fsl_avb_atx_ops, boot_key_hash)) { 455 if (fsl_read_permanent_attributes_hash(&fsl_avb_atx_ops, boot_key_hash)) {
454 printf("ERROR - failed to read permanent attributes hash for keymaster\n"); 456 printf("ERROR - failed to read permanent attributes hash for keymaster\n");
455 memset(boot_key_hash, 0, AVB_SHA256_DIGEST_SIZE); 457 memset(boot_key_hash, 0, AVB_SHA256_DIGEST_SIZE);
456 } 458 }
457 #else 459 #else
458 uint8_t public_key_buf[AVB_MAX_BUFFER_LENGTH]; 460 uint8_t public_key_buf[AVB_MAX_BUFFER_LENGTH];
459 if (trusty_read_vbmeta_public_key(public_key_buf, 461 if (trusty_read_vbmeta_public_key(public_key_buf,
460 AVB_MAX_BUFFER_LENGTH) != 0) { 462 AVB_MAX_BUFFER_LENGTH) != 0) {
461 printf("ERROR - failed to read public key for keymaster\n"); 463 printf("ERROR - failed to read public key for keymaster\n");
462 memset(boot_key_hash, 0, AVB_SHA256_DIGEST_SIZE); 464 memset(boot_key_hash, 0, AVB_SHA256_DIGEST_SIZE);
463 } else 465 } else
464 sha256_csum_wd((unsigned char *)public_key_buf, AVB_SHA256_DIGEST_SIZE, 466 sha256_csum_wd((unsigned char *)public_key_buf, AVB_SHA256_DIGEST_SIZE,
465 (unsigned char *)boot_key_hash, CHUNKSZ_SHA256); 467 (unsigned char *)boot_key_hash, CHUNKSZ_SHA256);
466 #endif 468 #endif
467 469
468 bool lock = (lock_status == FASTBOOT_LOCK)? true: false; 470 bool lock = (lock_status == FASTBOOT_LOCK)? true: false;
469 if (avb_result == AVB_AB_FLOW_RESULT_OK) 471 if (avb_result == AVB_AB_FLOW_RESULT_OK)
470 vbstatus = KM_VERIFIED_BOOT_VERIFIED; 472 vbstatus = KM_VERIFIED_BOOT_VERIFIED;
471 else 473 else
472 vbstatus = KM_VERIFIED_BOOT_FAILED; 474 vbstatus = KM_VERIFIED_BOOT_FAILED;
473 475
474 /* Calculate VBH */ 476 /* Calculate VBH */
475 #if defined(CONFIG_DUAL_BOOTLOADER) && defined(CONFIG_AVB_ATX) 477 #if defined(CONFIG_DUAL_BOOTLOADER) && defined(CONFIG_AVB_ATX)
476 if (vbh_calculate(vbh, avb_out_data)) { 478 if (vbh_calculate(vbh, avb_out_data)) {
477 ret = -1; 479 ret = -1;
478 goto fail; 480 goto fail;
479 } 481 }
480 482
481 trusty_set_boot_params(os_ver_km, os_lvl_km, vbstatus, lock, 483 trusty_set_boot_params(os_ver_km, os_lvl_km, vbstatus, lock,
482 boot_key_hash, AVB_SHA256_DIGEST_SIZE, 484 boot_key_hash, AVB_SHA256_DIGEST_SIZE,
483 vbh, AVB_SHA256_DIGEST_SIZE); 485 vbh, AVB_SHA256_DIGEST_SIZE);
484 #else 486 #else
485 trusty_set_boot_params(os_ver_km, os_lvl_km, vbstatus, lock, 487 trusty_set_boot_params(os_ver_km, os_lvl_km, vbstatus, lock,
486 boot_key_hash, AVB_SHA256_DIGEST_SIZE, 488 boot_key_hash, AVB_SHA256_DIGEST_SIZE,
487 NULL, 0); 489 NULL, 0);
488 #endif 490 #endif
489 491
490 fail: 492 fail:
491 return ret; 493 return ret;
492 } 494 }
493 #endif 495 #endif
494 496
495 #if defined(CONFIG_AVB_SUPPORT) && defined(CONFIG_MMC) 497 #if defined(CONFIG_AVB_SUPPORT) && defined(CONFIG_MMC)
496 /* we can use avb to verify Trusty if we want */ 498 /* we can use avb to verify Trusty if we want */
497 const char *requested_partitions_boot[] = {"boot", FDT_PART_NAME, NULL}; 499 const char *requested_partitions_boot[] = {"boot", FDT_PART_NAME, NULL};
498 const char *requested_partitions_recovery[] = {"recovery", FDT_PART_NAME, NULL}; 500 const char *requested_partitions_recovery[] = {"recovery", FDT_PART_NAME, NULL};
499 501
500 static bool is_load_fdt_from_part(void) 502 static bool is_load_fdt_from_part(void)
501 { 503 {
502 bool ptn_find; 504 bool ptn_find;
503 505
504 #if defined(CONFIG_ANDROID_THINGS_SUPPORT) 506 #if defined(CONFIG_ANDROID_THINGS_SUPPORT)
505 ptn_find = fastboot_flash_find_ptn("oem_bootloader_a") && 507 ptn_find = fastboot_flash_find_ptn("oem_bootloader_a") &&
506 fastboot_flash_find_ptn("oem_bootloader_b"); 508 fastboot_flash_find_ptn("oem_bootloader_b");
507 #elif defined(CONFIG_ANDROID_AB_SUPPORT) 509 #elif defined(CONFIG_ANDROID_AB_SUPPORT)
508 ptn_find = fastboot_flash_find_ptn("dtbo_a") && 510 ptn_find = fastboot_flash_find_ptn("dtbo_a") &&
509 fastboot_flash_find_ptn("dtbo_b"); 511 fastboot_flash_find_ptn("dtbo_b");
510 #else 512 #else
511 ptn_find = fastboot_flash_find_ptn("dtbo"); 513 ptn_find = fastboot_flash_find_ptn("dtbo");
512 #endif 514 #endif
513 515
514 if (ptn_find) 516 if (ptn_find)
515 return true; 517 return true;
516 else 518 else
517 return false; 519 return false;
518 } 520 }
519 521
520 static int find_partition_data_by_name(char* part_name, 522 static int find_partition_data_by_name(char* part_name,
521 AvbSlotVerifyData* avb_out_data, AvbPartitionData** avb_loadpart) 523 AvbSlotVerifyData* avb_out_data, AvbPartitionData** avb_loadpart)
522 { 524 {
523 int num = 0; 525 int num = 0;
524 AvbPartitionData* loadpart = NULL; 526 AvbPartitionData* loadpart = NULL;
525 527
526 for (num = 0; num < avb_out_data->num_loaded_partitions; num++) { 528 for (num = 0; num < avb_out_data->num_loaded_partitions; num++) {
527 loadpart = &(avb_out_data->loaded_partitions[num]); 529 loadpart = &(avb_out_data->loaded_partitions[num]);
528 if (!(strncmp(loadpart->partition_name, 530 if (!(strncmp(loadpart->partition_name,
529 part_name, strlen(part_name)))) { 531 part_name, strlen(part_name)))) {
530 *avb_loadpart = loadpart; 532 *avb_loadpart = loadpart;
531 break; 533 break;
532 } 534 }
533 } 535 }
534 if (num == avb_out_data->num_loaded_partitions) { 536 if (num == avb_out_data->num_loaded_partitions) {
535 printf("Error! Can't find %s partition from avb partition data!\n", 537 printf("Error! Can't find %s partition from avb partition data!\n",
536 part_name); 538 part_name);
537 return -1; 539 return -1;
538 } 540 }
539 else 541 else
540 return 0; 542 return 0;
541 } 543 }
542 544
543 int do_boota(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { 545 int do_boota(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) {
544 546
545 ulong addr = 0; 547 ulong addr = 0;
546 struct andr_img_hdr *hdr = NULL; 548 struct andr_img_hdr *hdr = NULL;
547 void *boot_buf = NULL; 549 void *boot_buf = NULL;
548 ulong image_size; 550 ulong image_size;
549 u32 avb_metric; 551 u32 avb_metric;
550 bool check_image_arm64 = false; 552 bool check_image_arm64 = false;
551 bool is_recovery_mode = false; 553 bool is_recovery_mode = false;
552 554
553 AvbABFlowResult avb_result; 555 AvbABFlowResult avb_result;
554 AvbSlotVerifyData *avb_out_data = NULL; 556 AvbSlotVerifyData *avb_out_data = NULL;
555 AvbPartitionData *avb_loadpart = NULL; 557 AvbPartitionData *avb_loadpart = NULL;
556 558
557 /* get bootmode, default to boot "boot" */ 559 /* get bootmode, default to boot "boot" */
558 if (argc > 1) { 560 if (argc > 1) {
559 is_recovery_mode = 561 is_recovery_mode =
560 (strncmp(argv[1], "recovery", sizeof("recovery")) != 0) ? false: true; 562 (strncmp(argv[1], "recovery", sizeof("recovery")) != 0) ? false: true;
561 if (is_recovery_mode) 563 if (is_recovery_mode)
562 printf("Will boot from recovery!\n"); 564 printf("Will boot from recovery!\n");
563 } 565 }
564 566
565 /* check lock state */ 567 /* check lock state */
566 FbLockState lock_status = fastboot_get_lock_stat(); 568 FbLockState lock_status = fastboot_get_lock_stat();
567 if (lock_status == FASTBOOT_LOCK_ERROR) { 569 if (lock_status == FASTBOOT_LOCK_ERROR) {
568 #ifdef CONFIG_AVB_ATX 570 #ifdef CONFIG_AVB_ATX
569 printf("In boota get fastboot lock status error, enter fastboot mode.\n"); 571 printf("In boota get fastboot lock status error, enter fastboot mode.\n");
570 goto fail; 572 goto fail;
571 #else 573 #else
572 printf("In boota get fastboot lock status error. Set lock status\n"); 574 printf("In boota get fastboot lock status error. Set lock status\n");
573 fastboot_set_lock_stat(FASTBOOT_LOCK); 575 fastboot_set_lock_stat(FASTBOOT_LOCK);
574 lock_status = FASTBOOT_LOCK; 576 lock_status = FASTBOOT_LOCK;
575 #endif 577 #endif
576 } 578 }
577 bool allow_fail = (lock_status == FASTBOOT_UNLOCK ? true : false); 579 bool allow_fail = (lock_status == FASTBOOT_UNLOCK ? true : false);
578 avb_metric = get_timer(0); 580 avb_metric = get_timer(0);
579 /* we don't need to verify fdt partition if we don't have it. */ 581 /* we don't need to verify fdt partition if we don't have it. */
580 if (!is_load_fdt_from_part()) { 582 if (!is_load_fdt_from_part()) {
581 requested_partitions_boot[1] = NULL; 583 requested_partitions_boot[1] = NULL;
582 requested_partitions_recovery[1] = NULL; 584 requested_partitions_recovery[1] = NULL;
583 } 585 }
584 #ifndef CONFIG_SYSTEM_RAMDISK_SUPPORT 586 #ifndef CONFIG_SYSTEM_RAMDISK_SUPPORT
585 else if (is_recovery_mode){ 587 else if (is_recovery_mode){
586 requested_partitions_recovery[1] = NULL; 588 requested_partitions_recovery[1] = NULL;
587 } 589 }
588 #endif 590 #endif
589 591
590 /* if in lock state, do avb verify */ 592 /* if in lock state, do avb verify */
591 #ifndef CONFIG_DUAL_BOOTLOADER 593 #ifndef CONFIG_DUAL_BOOTLOADER
592 /* For imx6 on Android, we don't have a/b slot and we want to verify 594 /* For imx6 on Android, we don't have a/b slot and we want to verify
593 * boot/recovery with AVB. For imx8 and Android Things we don't have 595 * boot/recovery with AVB. For imx8 and Android Things we don't have
594 * recovery and support a/b slot for boot */ 596 * recovery and support a/b slot for boot */
595 #ifdef CONFIG_ANDROID_AB_SUPPORT 597 #ifdef CONFIG_ANDROID_AB_SUPPORT
596 /* we can use avb to verify Trusty if we want */ 598 /* we can use avb to verify Trusty if we want */
597 avb_result = avb_ab_flow_fast(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail, 599 avb_result = avb_ab_flow_fast(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail,
598 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, &avb_out_data); 600 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, &avb_out_data);
599 #else 601 #else
600 #ifndef CONFIG_SYSTEM_RAMDISK_SUPPORT 602 #ifndef CONFIG_SYSTEM_RAMDISK_SUPPORT
601 if (!is_recovery_mode) { 603 if (!is_recovery_mode) {
602 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail, 604 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail,
603 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data); 605 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data);
604 } else { 606 } else {
605 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_recovery, allow_fail, 607 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_recovery, allow_fail,
606 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data); 608 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data);
607 } 609 }
608 #else /* CONFIG_SYSTEM_RAMDISK_SUPPORT defined */ 610 #else /* CONFIG_SYSTEM_RAMDISK_SUPPORT defined */
609 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail, 611 avb_result = avb_single_flow(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail,
610 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data); 612 AVB_HASHTREE_ERROR_MODE_RESTART, &avb_out_data);
611 #endif /*CONFIG_SYSTEM_RAMDISK_SUPPORT*/ 613 #endif /*CONFIG_SYSTEM_RAMDISK_SUPPORT*/
612 #endif 614 #endif
613 #else /* !CONFIG_DUAL_BOOTLOADER */ 615 #else /* !CONFIG_DUAL_BOOTLOADER */
614 /* We will only verify single one slot which has been selected in SPL */ 616 /* We will only verify single one slot which has been selected in SPL */
615 avb_result = avb_flow_dual_uboot(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail, 617 avb_result = avb_flow_dual_uboot(&fsl_avb_ab_ops, requested_partitions_boot, allow_fail,
616 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, &avb_out_data); 618 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, &avb_out_data);
617 619
618 /* Reboot if current slot is not bootable. */ 620 /* Reboot if current slot is not bootable. */
619 if (avb_result == AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS) { 621 if (avb_result == AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS) {
620 printf("boota: slot verify fail!\n"); 622 printf("boota: slot verify fail!\n");
621 do_reset(NULL, 0, 0, NULL); 623 do_reset(NULL, 0, 0, NULL);
622 } 624 }
623 #endif /* !CONFIG_DUAL_BOOTLOADER */ 625 #endif /* !CONFIG_DUAL_BOOTLOADER */
624 626
625 /* get the duration of avb */ 627 /* get the duration of avb */
626 metrics.avb = get_timer(avb_metric); 628 metrics.avb = get_timer(avb_metric);
627 629
628 if ((avb_result == AVB_AB_FLOW_RESULT_OK) || 630 if ((avb_result == AVB_AB_FLOW_RESULT_OK) ||
629 (avb_result == AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR)) { 631 (avb_result == AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR)) {
630 assert(avb_out_data != NULL); 632 assert(avb_out_data != NULL);
631 /* We may have more than one partition loaded by AVB, find the boot 633 /* We may have more than one partition loaded by AVB, find the boot
632 * partition first. 634 * partition first.
633 */ 635 */
634 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT 636 #ifdef CONFIG_SYSTEM_RAMDISK_SUPPORT
635 if (find_partition_data_by_name("boot", avb_out_data, &avb_loadpart)) 637 if (find_partition_data_by_name("boot", avb_out_data, &avb_loadpart))
636 goto fail; 638 goto fail;
637 #else 639 #else
638 if (!is_recovery_mode) { 640 if (!is_recovery_mode) {
639 if (find_partition_data_by_name("boot", avb_out_data, &avb_loadpart)) 641 if (find_partition_data_by_name("boot", avb_out_data, &avb_loadpart))
640 goto fail; 642 goto fail;
641 } else { 643 } else {
642 if (find_partition_data_by_name("recovery", avb_out_data, &avb_loadpart)) 644 if (find_partition_data_by_name("recovery", avb_out_data, &avb_loadpart))
643 goto fail; 645 goto fail;
644 } 646 }
645 #endif 647 #endif
646 assert(avb_loadpart != NULL); 648 assert(avb_loadpart != NULL);
647 /* we should use avb_part_data->data as boot image */ 649 /* we should use avb_part_data->data as boot image */
648 /* boot image is already read by avb */ 650 /* boot image is already read by avb */
649 hdr = (struct andr_img_hdr *)avb_loadpart->data; 651 hdr = (struct andr_img_hdr *)avb_loadpart->data;
650 if (android_image_check_header(hdr)) { 652 if (android_image_check_header(hdr)) {
651 printf("boota: bad boot image magic\n"); 653 printf("boota: bad boot image magic\n");
652 goto fail; 654 goto fail;
653 } 655 }
654 if (avb_result == AVB_AB_FLOW_RESULT_OK) 656 if (avb_result == AVB_AB_FLOW_RESULT_OK)
655 printf(" verify OK, boot '%s%s'\n", 657 printf(" verify OK, boot '%s%s'\n",
656 avb_loadpart->partition_name, avb_out_data->ab_suffix); 658 avb_loadpart->partition_name, avb_out_data->ab_suffix);
657 else { 659 else {
658 printf(" verify FAIL, state: UNLOCK\n"); 660 printf(" verify FAIL, state: UNLOCK\n");
659 printf(" boot '%s%s' still\n", 661 printf(" boot '%s%s' still\n",
660 avb_loadpart->partition_name, avb_out_data->ab_suffix); 662 avb_loadpart->partition_name, avb_out_data->ab_suffix);
661 } 663 }
662 char bootargs_sec[ANDR_BOOT_EXTRA_ARGS_SIZE]; 664 char bootargs_sec[ANDR_BOOT_EXTRA_ARGS_SIZE];
663 if (lock_status == FASTBOOT_LOCK) { 665 if (lock_status == FASTBOOT_LOCK) {
664 snprintf(bootargs_sec, sizeof(bootargs_sec), 666 snprintf(bootargs_sec, sizeof(bootargs_sec),
665 "androidboot.verifiedbootstate=green androidboot.flash.locked=1 androidboot.slot_suffix=%s ", 667 "androidboot.verifiedbootstate=green androidboot.flash.locked=1 androidboot.slot_suffix=%s ",
666 avb_out_data->ab_suffix); 668 avb_out_data->ab_suffix);
667 } else { 669 } else {
668 snprintf(bootargs_sec, sizeof(bootargs_sec), 670 snprintf(bootargs_sec, sizeof(bootargs_sec),
669 "androidboot.verifiedbootstate=orange androidboot.flash.locked=0 androidboot.slot_suffix=%s ", 671 "androidboot.verifiedbootstate=orange androidboot.flash.locked=0 androidboot.slot_suffix=%s ",
670 avb_out_data->ab_suffix); 672 avb_out_data->ab_suffix);
671 } 673 }
672 #ifdef CONFIG_ANDROID_AUTO_SUPPORT 674 #ifdef CONFIG_ANDROID_AUTO_SUPPORT
673 strcat(bootargs_sec, avb_out_data->cmdline); 675 strcat(bootargs_sec, avb_out_data->cmdline);
674 #else 676 #else
675 strcat(bootargs_sec, strstr(avb_out_data->cmdline, "androidboot")); 677 strcat(bootargs_sec, strstr(avb_out_data->cmdline, "androidboot"));
676 /* for standard android, recovery ramdisk will be used anyway, to 678 /* for standard android, recovery ramdisk will be used anyway, to
677 * boot up Android, "androidboot.force_normal_boot=1" is needed */ 679 * boot up Android, "androidboot.force_normal_boot=1" is needed */
678 if(!is_recovery_mode) { 680 if(!is_recovery_mode) {
679 strcat(bootargs_sec, " androidboot.force_normal_boot=1"); 681 strcat(bootargs_sec, " androidboot.force_normal_boot=1");
680 } 682 }
681 #endif 683 #endif
682 env_set("bootargs_sec", bootargs_sec); 684 env_set("bootargs_sec", bootargs_sec);
683 #if defined CONFIG_SYSTEM_RAMDISK_SUPPORT && defined CONFIG_ANDROID_AUTO_SUPPORT 685 #if defined CONFIG_SYSTEM_RAMDISK_SUPPORT && defined CONFIG_ANDROID_AUTO_SUPPORT
684 if(!is_recovery_mode) { 686 if(!is_recovery_mode) {
685 if(avb_out_data->cmdline != NULL && strstr(avb_out_data->cmdline, "root=")) 687 if(avb_out_data->cmdline != NULL && strstr(avb_out_data->cmdline, "root="))
686 fastboot_setup_system_boot_args(avb_out_data->ab_suffix, false); 688 fastboot_setup_system_boot_args(avb_out_data->ab_suffix, false);
687 else 689 else
688 fastboot_setup_system_boot_args(avb_out_data->ab_suffix, true); 690 fastboot_setup_system_boot_args(avb_out_data->ab_suffix, true);
689 } 691 }
690 #endif /* CONFIG_SYSTEM_RAMDISK_SUPPORT */ 692 #endif /* CONFIG_SYSTEM_RAMDISK_SUPPORT */
691 image_size = avb_loadpart->data_size; 693 image_size = avb_loadpart->data_size;
692 #if defined (CONFIG_ARCH_IMX8) || defined (CONFIG_ARCH_IMX8M) 694 #if defined (CONFIG_ARCH_IMX8) || defined (CONFIG_ARCH_IMX8M)
693 /* If we are using uncompressed kernel image, copy it directly to 695 /* If we are using uncompressed kernel image, copy it directly to
694 * hdr->kernel_addr, if we are using compressed lz4 kernel image, 696 * hdr->kernel_addr, if we are using compressed lz4 kernel image,
695 * we need to decompress the kernel image first. */ 697 * we need to decompress the kernel image first. */
696 if (image_arm64((void *)((ulong)hdr + hdr->page_size))) { 698 if (image_arm64((void *)((ulong)hdr + hdr->page_size))) {
697 memcpy((void *)(long)hdr->kernel_addr, 699 memcpy((void *)(long)hdr->kernel_addr,
698 (void *)((ulong)hdr + hdr->page_size), hdr->kernel_size); 700 (void *)((ulong)hdr + hdr->page_size), hdr->kernel_size);
699 } else { 701 } else {
700 #ifdef CONFIG_LZ4 702 #ifdef CONFIG_LZ4
701 size_t lz4_len = DST_DECOMPRESS_LEN; 703 size_t lz4_len = DST_DECOMPRESS_LEN;
702 if (ulz4fn((void *)((ulong)hdr + hdr->page_size), 704 if (ulz4fn((void *)((ulong)hdr + hdr->page_size),
703 hdr->kernel_size, (void *)(ulong)hdr->kernel_addr, &lz4_len) != 0) { 705 hdr->kernel_size, (void *)(ulong)hdr->kernel_addr, &lz4_len) != 0) {
704 printf("Decompress kernel fail!\n"); 706 printf("Decompress kernel fail!\n");
705 goto fail; 707 goto fail;
706 } 708 }
707 #else /* CONFIG_LZ4 */ 709 #else /* CONFIG_LZ4 */
708 printf("please enable CONFIG_LZ4 if we're using compressed lz4 kernel image!\n"); 710 printf("please enable CONFIG_LZ4 if we're using compressed lz4 kernel image!\n");
709 goto fail; 711 goto fail;
710 #endif /* CONFIG_LZ4 */ 712 #endif /* CONFIG_LZ4 */
711 } 713 }
712 #else /* CONFIG_ARCH_IMX8 || CONFIG_ARCH_IMX8M */ 714 #else /* CONFIG_ARCH_IMX8 || CONFIG_ARCH_IMX8M */
713 /* copy kernel image and boot header to hdr->kernel_addr - hdr->page_size */ 715 /* copy kernel image and boot header to hdr->kernel_addr - hdr->page_size */
714 memcpy((void *)(ulong)(hdr->kernel_addr - hdr->page_size), (void *)hdr, 716 memcpy((void *)(ulong)(hdr->kernel_addr - hdr->page_size), (void *)hdr,
715 hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size)); 717 hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size));
716 #endif /* CONFIG_ARCH_IMX8 || CONFIG_ARCH_IMX8M */ 718 #endif /* CONFIG_ARCH_IMX8 || CONFIG_ARCH_IMX8M */
717 } else { 719 } else {
718 /* Fall into fastboot mode if get unacceptable error from avb 720 /* Fall into fastboot mode if get unacceptable error from avb
719 * or verify fail in lock state. 721 * or verify fail in lock state.
720 */ 722 */
721 if (lock_status == FASTBOOT_LOCK) 723 if (lock_status == FASTBOOT_LOCK)
722 printf(" verify FAIL, state: LOCK\n"); 724 printf(" verify FAIL, state: LOCK\n");
723 725
724 goto fail; 726 goto fail;
725 } 727 }
726 728
727 flush_cache((ulong)image_load_addr, image_size); 729 flush_cache((ulong)image_load_addr, image_size);
728 check_image_arm64 = image_arm64((void *)(ulong)hdr->kernel_addr); 730 check_image_arm64 = image_arm64((void *)(ulong)hdr->kernel_addr);
729 #if !defined(CONFIG_SYSTEM_RAMDISK_SUPPORT) || !defined(CONFIG_ANDROID_AUTO_SUPPORT) 731 #if !defined(CONFIG_SYSTEM_RAMDISK_SUPPORT) || !defined(CONFIG_ANDROID_AUTO_SUPPORT)
730 memcpy((void *)(ulong)hdr->ramdisk_addr, (void *)(ulong)hdr + hdr->page_size 732 memcpy((void *)(ulong)hdr->ramdisk_addr, (void *)(ulong)hdr + hdr->page_size
731 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size); 733 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size);
732 #else 734 #else
733 if (is_recovery_mode) 735 if (is_recovery_mode)
734 memcpy((void *)(ulong)hdr->ramdisk_addr, (void *)(ulong)hdr + hdr->page_size 736 memcpy((void *)(ulong)hdr->ramdisk_addr, (void *)(ulong)hdr + hdr->page_size
735 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size); 737 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size);
736 #endif 738 #endif
737 739
738 #ifdef CONFIG_OF_LIBFDT 740 #ifdef CONFIG_OF_LIBFDT
739 /* load the dtb file */ 741 /* load the dtb file */
742 u32 fdt_addr = 0;
740 u32 fdt_size = 0; 743 u32 fdt_size = 0;
741 struct dt_table_header *dt_img = NULL; 744 struct dt_table_header *dt_img = NULL;
742 745
743 if (is_load_fdt_from_part()) { 746 if (is_load_fdt_from_part()) {
747 fdt_addr = (ulong)((ulong)(hdr->kernel_addr) + FDT_OFFSET_TO_KERNEL);
744 #ifdef CONFIG_ANDROID_THINGS_SUPPORT 748 #ifdef CONFIG_ANDROID_THINGS_SUPPORT
745 if (find_partition_data_by_name("oem_bootloader", 749 if (find_partition_data_by_name("oem_bootloader",
746 avb_out_data, &avb_loadpart)) { 750 avb_out_data, &avb_loadpart)) {
747 goto fail; 751 goto fail;
748 } else 752 } else
749 dt_img = (struct dt_table_header *)avb_loadpart->data; 753 dt_img = (struct dt_table_header *)avb_loadpart->data;
750 #elif defined(CONFIG_SYSTEM_RAMDISK_SUPPORT) /* It means boot.img(recovery) do not include dtb, it need load dtb from partition */ 754 #elif defined(CONFIG_SYSTEM_RAMDISK_SUPPORT) /* It means boot.img(recovery) do not include dtb, it need load dtb from partition */
751 if (find_partition_data_by_name("dtbo", 755 if (find_partition_data_by_name("dtbo",
752 avb_out_data, &avb_loadpart)) { 756 avb_out_data, &avb_loadpart)) {
753 goto fail; 757 goto fail;
754 } else 758 } else
755 dt_img = (struct dt_table_header *)avb_loadpart->data; 759 dt_img = (struct dt_table_header *)avb_loadpart->data;
756 #else /* recovery.img include dts while boot.img use dtbo */ 760 #else /* recovery.img include dts while boot.img use dtbo */
757 if (is_recovery_mode) { 761 if (is_recovery_mode) {
758 if (hdr->header_version != 1) { 762 if (hdr->header_version != 1) {
759 printf("boota: boot image header version error!\n"); 763 printf("boota: boot image header version error!\n");
760 goto fail; 764 goto fail;
761 } 765 }
762 766
763 dt_img = (struct dt_table_header *)((void *)(ulong)hdr + 767 dt_img = (struct dt_table_header *)((void *)(ulong)hdr +
764 hdr->page_size + 768 hdr->page_size +
765 ALIGN(hdr->kernel_size, hdr->page_size) + 769 ALIGN(hdr->kernel_size, hdr->page_size) +
766 ALIGN(hdr->ramdisk_size, hdr->page_size) + 770 ALIGN(hdr->ramdisk_size, hdr->page_size) +
767 ALIGN(hdr->second_size, hdr->page_size)); 771 ALIGN(hdr->second_size, hdr->page_size));
768 } else if (find_partition_data_by_name("dtbo", 772 } else if (find_partition_data_by_name("dtbo",
769 avb_out_data, &avb_loadpart)) { 773 avb_out_data, &avb_loadpart)) {
770 goto fail; 774 goto fail;
771 } else 775 } else
772 dt_img = (struct dt_table_header *)avb_loadpart->data; 776 dt_img = (struct dt_table_header *)avb_loadpart->data;
773 #endif 777 #endif
774 778
775 if (be32_to_cpu(dt_img->magic) != DT_TABLE_MAGIC) { 779 if (be32_to_cpu(dt_img->magic) != DT_TABLE_MAGIC) {
776 printf("boota: bad dt table magic %08x\n", 780 printf("boota: bad dt table magic %08x\n",
777 be32_to_cpu(dt_img->magic)); 781 be32_to_cpu(dt_img->magic));
778 goto fail; 782 goto fail;
779 } else if (!be32_to_cpu(dt_img->dt_entry_count)) { 783 } else if (!be32_to_cpu(dt_img->dt_entry_count)) {
780 printf("boota: no dt entries\n"); 784 printf("boota: no dt entries\n");
781 goto fail; 785 goto fail;
782 } 786 }
783 787
784 struct dt_table_entry *dt_entry; 788 struct dt_table_entry *dt_entry;
785 dt_entry = (struct dt_table_entry *)((ulong)dt_img + 789 dt_entry = (struct dt_table_entry *)((ulong)dt_img +
786 be32_to_cpu(dt_img->dt_entries_offset)); 790 be32_to_cpu(dt_img->dt_entries_offset));
787 fdt_size = be32_to_cpu(dt_entry->dt_size); 791 fdt_size = be32_to_cpu(dt_entry->dt_size);
788 memcpy((void *)(ulong)hdr->second_addr, (void *)((ulong)dt_img + 792 memcpy((void *)fdt_addr, (void *)((ulong)dt_img +
789 be32_to_cpu(dt_entry->dt_offset)), fdt_size); 793 be32_to_cpu(dt_entry->dt_offset)), fdt_size);
790 } else { 794 } else {
791 if (hdr->second_size && hdr->second_addr) { 795 fdt_addr = (ulong)(hdr->second_addr);
792 memcpy((void *)(ulong)hdr->second_addr, 796 fdt_size = (ulong)(hdr->second_size);
797 if (fdt_size && fdt_addr) {
798 memcpy((void *)(ulong)fdt_addr,
793 (void *)(ulong)hdr + hdr->page_size 799 (void *)(ulong)hdr + hdr->page_size
794 + ALIGN(hdr->kernel_size, hdr->page_size) 800 + ALIGN(hdr->kernel_size, hdr->page_size)
795 + ALIGN(hdr->ramdisk_size, hdr->page_size), 801 + ALIGN(hdr->ramdisk_size, hdr->page_size),
796 hdr->second_size); 802 fdt_size);
797 } 803 }
798 } 804 }
799 #endif /*CONFIG_OF_LIBFDT*/ 805 #endif /*CONFIG_OF_LIBFDT*/
800 806
801 if (check_image_arm64) { 807 if (check_image_arm64) {
802 android_image_get_kernel(hdr, 0, NULL, NULL); 808 android_image_get_kernel(hdr, 0, NULL, NULL);
803 addr = hdr->kernel_addr; 809 addr = hdr->kernel_addr;
804 } else { 810 } else {
805 addr = (ulong)(hdr->kernel_addr - hdr->page_size); 811 addr = (ulong)(hdr->kernel_addr - hdr->page_size);
806 } 812 }
807 printf("kernel @ %08x (%d)\n", hdr->kernel_addr, hdr->kernel_size); 813 printf("kernel @ %08x (%d)\n", hdr->kernel_addr, hdr->kernel_size);
808 printf("ramdisk @ %08x (%d)\n", hdr->ramdisk_addr, hdr->ramdisk_size); 814 printf("ramdisk @ %08x (%d)\n", hdr->ramdisk_addr, hdr->ramdisk_size);
809 #ifdef CONFIG_OF_LIBFDT 815 #ifdef CONFIG_OF_LIBFDT
810 if (is_load_fdt_from_part()) { 816 if (fdt_size)
811 if (fdt_size) 817 printf("fdt @ %08x (%d)\n", fdt_addr, fdt_size);
812 printf("fdt @ %08x (%d)\n", hdr->second_addr, fdt_size);
813 } else {
814 if (hdr->second_size)
815 printf("fdt @ %08x (%d)\n", hdr->second_addr, hdr->second_size);
816 }
817 #endif /*CONFIG_OF_LIBFDT*/ 818 #endif /*CONFIG_OF_LIBFDT*/
818 819
819 char boot_addr_start[12]; 820 char boot_addr_start[12];
820 char ramdisk_addr[25]; 821 char ramdisk_addr[25];
821 char fdt_addr[12]; 822 char fdt_addr_start[12];
822 823
823 char *boot_args[] = { NULL, boot_addr_start, ramdisk_addr, fdt_addr}; 824 char *boot_args[] = { NULL, boot_addr_start, ramdisk_addr, fdt_addr_start};
824 if (check_image_arm64) 825 if (check_image_arm64)
825 boot_args[0] = "booti"; 826 boot_args[0] = "booti";
826 else 827 else
827 boot_args[0] = "bootm"; 828 boot_args[0] = "bootm";
828 829
829 sprintf(boot_addr_start, "0x%lx", addr); 830 sprintf(boot_addr_start, "0x%lx", addr);
830 sprintf(ramdisk_addr, "0x%x:0x%x", hdr->ramdisk_addr, hdr->ramdisk_size); 831 sprintf(ramdisk_addr, "0x%x:0x%x", hdr->ramdisk_addr, hdr->ramdisk_size);
831 sprintf(fdt_addr, "0x%x", hdr->second_addr); 832 sprintf(fdt_addr_start, "0x%x", fdt_addr);
832 833
833 /* when CONFIG_SYSTEM_RAMDISK_SUPPORT is enabled and it's for Android Auto, if it's not recovery mode 834 /* when CONFIG_SYSTEM_RAMDISK_SUPPORT is enabled and it's for Android Auto, if it's not recovery mode
834 * do not pass ramdisk addr*/ 835 * do not pass ramdisk addr*/
835 #if defined(CONFIG_SYSTEM_RAMDISK_SUPPORT) && defined(CONFIG_ANDROID_AUTO_SUPPORT) 836 #if defined(CONFIG_SYSTEM_RAMDISK_SUPPORT) && defined(CONFIG_ANDROID_AUTO_SUPPORT)
836 if (!is_recovery_mode) 837 if (!is_recovery_mode)
837 boot_args[2] = NULL; 838 boot_args[2] = NULL;
838 #endif 839 #endif
839 840
840 #ifdef CONFIG_IMX_TRUSTY_OS 841 #ifdef CONFIG_IMX_TRUSTY_OS
841 /* Trusty keymaster needs some parameters before it work */ 842 /* Trusty keymaster needs some parameters before it work */
842 if (trusty_setbootparameter(hdr, avb_result, avb_out_data)) 843 if (trusty_setbootparameter(hdr, avb_result, avb_out_data))
843 goto fail; 844 goto fail;
844 /* lock the boot status and rollback_idx preventing Linux modify it */ 845 /* lock the boot status and rollback_idx preventing Linux modify it */
845 trusty_lock_boot_state(); 846 trusty_lock_boot_state();
846 /* lock the boot state so linux can't use some hwcrypto commands. */ 847 /* lock the boot state so linux can't use some hwcrypto commands. */
847 hwcrypto_lock_boot_state(); 848 hwcrypto_lock_boot_state();
848 /* put ql-tipc to release resource for Linux */ 849 /* put ql-tipc to release resource for Linux */
849 trusty_ipc_shutdown(); 850 trusty_ipc_shutdown();
850 #endif 851 #endif
851 852
852 if (avb_out_data != NULL) 853 if (avb_out_data != NULL)
853 avb_slot_verify_data_free(avb_out_data); 854 avb_slot_verify_data_free(avb_out_data);
854 if (boot_buf != NULL) 855 if (boot_buf != NULL)
855 free(boot_buf); 856 free(boot_buf);
856 857
857 if (check_image_arm64) { 858 if (check_image_arm64) {
858 #ifdef CONFIG_CMD_BOOTI 859 #ifdef CONFIG_CMD_BOOTI
859 do_booti(NULL, 0, 4, boot_args); 860 do_booti(NULL, 0, 4, boot_args);
860 #else 861 #else
861 debug("please enable CONFIG_CMD_BOOTI when kernel are Image"); 862 debug("please enable CONFIG_CMD_BOOTI when kernel are Image");
862 #endif 863 #endif
863 } else { 864 } else {
864 do_bootm(NULL, 0, 4, boot_args); 865 do_bootm(NULL, 0, 4, boot_args);
865 } 866 }
866 867
867 /* This only happens if image is somehow faulty so we start over */ 868 /* This only happens if image is somehow faulty so we start over */
868 do_reset(NULL, 0, 0, NULL); 869 do_reset(NULL, 0, 0, NULL);
869 870
870 return 1; 871 return 1;
871 872
872 fail: 873 fail:
873 /* avb has no recovery */ 874 /* avb has no recovery */
874 if (avb_out_data != NULL) 875 if (avb_out_data != NULL)
875 avb_slot_verify_data_free(avb_out_data); 876 avb_slot_verify_data_free(avb_out_data);
876 877
877 return run_command("fastboot 0", 0); 878 return run_command("fastboot 0", 0);
878 } 879 }
879 880
880 U_BOOT_CMD( 881 U_BOOT_CMD(
881 boota, 2, 1, do_boota, 882 boota, 2, 1, do_boota,
882 "boota - boot android bootimg \n", 883 "boota - boot android bootimg \n",
883 "boot from current mmc with avb verify\n" 884 "boot from current mmc with avb verify\n"
884 ); 885 );
885 886
886 #else /* CONFIG_AVB_SUPPORT */ 887 #else /* CONFIG_AVB_SUPPORT */
887 /* boota <addr> [ mmc0 | mmc1 [ <partition> ] ] */ 888 /* boota <addr> [ mmc0 | mmc1 [ <partition> ] ] */
888 int do_boota(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 889 int do_boota(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
889 { 890 {
890 ulong addr = 0; 891 ulong addr = 0;
891 char *ptn = "boot"; 892 char *ptn = "boot";
892 int mmcc = -1; 893 int mmcc = -1;
893 struct andr_img_hdr *hdr = &boothdr; 894 struct andr_img_hdr *hdr = &boothdr;
894 ulong image_size; 895 ulong image_size;
895 bool check_image_arm64 = false; 896 bool check_image_arm64 = false;
896 int i = 0; 897 int i = 0;
897 898
898 for (i = 0; i < argc; i++) 899 for (i = 0; i < argc; i++)
899 printf("%s ", argv[i]); 900 printf("%s ", argv[i]);
900 printf("\n"); 901 printf("\n");
901 902
902 if (argc < 2) 903 if (argc < 2)
903 return -1; 904 return -1;
904 905
905 mmcc = simple_strtoul(argv[1]+3, NULL, 10); 906 mmcc = simple_strtoul(argv[1]+3, NULL, 10);
906 907
907 if (argc > 2) 908 if (argc > 2)
908 ptn = argv[2]; 909 ptn = argv[2];
909 910
910 if (mmcc != -1) { 911 if (mmcc != -1) {
911 #ifdef CONFIG_MMC 912 #ifdef CONFIG_MMC
912 struct fastboot_ptentry *pte; 913 struct fastboot_ptentry *pte;
913 struct mmc *mmc; 914 struct mmc *mmc;
914 disk_partition_t info; 915 disk_partition_t info;
915 struct blk_desc *dev_desc = NULL; 916 struct blk_desc *dev_desc = NULL;
916 unsigned bootimg_sectors; 917 unsigned bootimg_sectors;
917 918
918 memset((void *)&info, 0 , sizeof(disk_partition_t)); 919 memset((void *)&info, 0 , sizeof(disk_partition_t));
919 /* i.MX use MBR as partition table, so this will have 920 /* i.MX use MBR as partition table, so this will have
920 to find the start block and length for the 921 to find the start block and length for the
921 partition name and register the fastboot pte we 922 partition name and register the fastboot pte we
922 define the partition number of each partition in 923 define the partition number of each partition in
923 config file 924 config file
924 */ 925 */
925 mmc = find_mmc_device(mmcc); 926 mmc = find_mmc_device(mmcc);
926 if (!mmc) { 927 if (!mmc) {
927 printf("boota: cannot find '%d' mmc device\n", mmcc); 928 printf("boota: cannot find '%d' mmc device\n", mmcc);
928 goto fail; 929 goto fail;
929 } 930 }
930 dev_desc = blk_get_dev("mmc", mmcc); 931 dev_desc = blk_get_dev("mmc", mmcc);
931 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { 932 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
932 printf("** Block device MMC %d not supported\n", mmcc); 933 printf("** Block device MMC %d not supported\n", mmcc);
933 goto fail; 934 goto fail;
934 } 935 }
935 936
936 /* below was i.MX mmc operation code */ 937 /* below was i.MX mmc operation code */
937 if (mmc_init(mmc)) { 938 if (mmc_init(mmc)) {
938 printf("mmc%d init failed\n", mmcc); 939 printf("mmc%d init failed\n", mmcc);
939 goto fail; 940 goto fail;
940 } 941 }
941 942
942 pte = fastboot_flash_find_ptn(ptn); 943 pte = fastboot_flash_find_ptn(ptn);
943 if (!pte) { 944 if (!pte) {
944 printf("boota: cannot find '%s' partition\n", ptn); 945 printf("boota: cannot find '%s' partition\n", ptn);
945 fastboot_flash_dump_ptn(); 946 fastboot_flash_dump_ptn();
946 goto fail; 947 goto fail;
947 } 948 }
948 949
949 if (blk_dread(dev_desc, pte->start, 950 if (blk_dread(dev_desc, pte->start,
950 1, (void *)hdr) < 0) { 951 1, (void *)hdr) < 0) {
951 printf("boota: mmc failed to read bootimg header\n"); 952 printf("boota: mmc failed to read bootimg header\n");
952 goto fail; 953 goto fail;
953 } 954 }
954 955
955 if (android_image_check_header(hdr)) { 956 if (android_image_check_header(hdr)) {
956 printf("boota: bad boot image magic\n"); 957 printf("boota: bad boot image magic\n");
957 goto fail; 958 goto fail;
958 } 959 }
959 960
960 image_size = android_image_get_end(hdr) - (ulong)hdr; 961 image_size = android_image_get_end(hdr) - (ulong)hdr;
961 bootimg_sectors = image_size/512; 962 bootimg_sectors = image_size/512;
962 963
963 if (blk_dread(dev_desc, pte->start, 964 if (blk_dread(dev_desc, pte->start,
964 bootimg_sectors, 965 bootimg_sectors,
965 (void *)(hdr->kernel_addr - hdr->page_size)) < 0) { 966 (void *)(hdr->kernel_addr - hdr->page_size)) < 0) {
966 printf("boota: mmc failed to read bootimage\n"); 967 printf("boota: mmc failed to read bootimage\n");
967 goto fail; 968 goto fail;
968 } 969 }
969 check_image_arm64 = image_arm64((void *)hdr->kernel_addr); 970 check_image_arm64 = image_arm64((void *)hdr->kernel_addr);
970 #if defined(CONFIG_FASTBOOT_LOCK) 971 #if defined(CONFIG_FASTBOOT_LOCK)
971 int verifyresult = -1; 972 int verifyresult = -1;
972 #endif 973 #endif
973 974
974 #if defined(CONFIG_FASTBOOT_LOCK) 975 #if defined(CONFIG_FASTBOOT_LOCK)
975 int lock_status = fastboot_get_lock_stat(); 976 int lock_status = fastboot_get_lock_stat();
976 if (lock_status == FASTBOOT_LOCK_ERROR) { 977 if (lock_status == FASTBOOT_LOCK_ERROR) {
977 printf("In boota get fastboot lock status error. Set lock status\n"); 978 printf("In boota get fastboot lock status error. Set lock status\n");
978 fastboot_set_lock_stat(FASTBOOT_LOCK); 979 fastboot_set_lock_stat(FASTBOOT_LOCK);
979 } 980 }
980 display_lock(fastboot_get_lock_stat(), verifyresult); 981 display_lock(fastboot_get_lock_stat(), verifyresult);
981 #endif 982 #endif
982 /* load the ramdisk file */ 983 /* load the ramdisk file */
983 memcpy((void *)hdr->ramdisk_addr, (void *)hdr->kernel_addr 984 memcpy((void *)hdr->ramdisk_addr, (void *)hdr->kernel_addr
984 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size); 985 + ALIGN(hdr->kernel_size, hdr->page_size), hdr->ramdisk_size);
985 986
986 #ifdef CONFIG_OF_LIBFDT 987 #ifdef CONFIG_OF_LIBFDT
987 u32 fdt_size = 0; 988 u32 fdt_size = 0;
988 /* load the dtb file */ 989 /* load the dtb file */
989 if (hdr->second_addr) { 990 if (hdr->second_addr) {
990 u32 zimage_size = ((u32 *)hdrload->kernel_addr)[ZIMAGE_END_ADDR] 991 u32 zimage_size = ((u32 *)hdrload->kernel_addr)[ZIMAGE_END_ADDR]
991 - ((u32 *)hdrload->kernel_addr)[ZIMAGE_START_ADDR]; 992 - ((u32 *)hdrload->kernel_addr)[ZIMAGE_START_ADDR];
992 fdt_size = hdrload->kernel_size - zimage_size; 993 fdt_size = hdrload->kernel_size - zimage_size;
993 memcpy((void *)(ulong)hdrload->second_addr, 994 memcpy((void *)(ulong)hdrload->second_addr,
994 (void*)(ulong)hdrload->kernel_addr + zimage_size, fdt_size); 995 (void*)(ulong)hdrload->kernel_addr + zimage_size, fdt_size);
995 } 996 }
996 #endif /*CONFIG_OF_LIBFDT*/ 997 #endif /*CONFIG_OF_LIBFDT*/
997 998
998 #else /*! CONFIG_MMC*/ 999 #else /*! CONFIG_MMC*/
999 return -1; 1000 return -1;
1000 #endif /*! CONFIG_MMC*/ 1001 #endif /*! CONFIG_MMC*/
1001 } else { 1002 } else {
1002 printf("boota: parameters is invalid. only support mmcX device\n"); 1003 printf("boota: parameters is invalid. only support mmcX device\n");
1003 return -1; 1004 return -1;
1004 } 1005 }
1005 1006
1006 printf("kernel @ %08x (%d)\n", hdr->kernel_addr, hdr->kernel_size); 1007 printf("kernel @ %08x (%d)\n", hdr->kernel_addr, hdr->kernel_size);
1007 printf("ramdisk @ %08x (%d)\n", hdr->ramdisk_addr, hdr->ramdisk_size); 1008 printf("ramdisk @ %08x (%d)\n", hdr->ramdisk_addr, hdr->ramdisk_size);
1008 #ifdef CONFIG_OF_LIBFDT 1009 #ifdef CONFIG_OF_LIBFDT
1009 if (fdt_size) 1010 if (fdt_size)
1010 printf("fdt @ %08x (%d)\n", hdr->second_addr, fdt_size); 1011 printf("fdt @ %08x (%d)\n", hdr->second_addr, fdt_size);
1011 #endif /*CONFIG_OF_LIBFDT*/ 1012 #endif /*CONFIG_OF_LIBFDT*/
1012 1013
1013 1014
1014 char boot_addr_start[12]; 1015 char boot_addr_start[12];
1015 char ramdisk_addr[25]; 1016 char ramdisk_addr[25];
1016 char fdt_addr[12]; 1017 char fdt_addr[12];
1017 char *boot_args[] = { NULL, boot_addr_start, ramdisk_addr, fdt_addr}; 1018 char *boot_args[] = { NULL, boot_addr_start, ramdisk_addr, fdt_addr};
1018 if (check_image_arm64 ) { 1019 if (check_image_arm64 ) {
1019 addr = hdr->kernel_addr; 1020 addr = hdr->kernel_addr;
1020 boot_args[0] = "booti"; 1021 boot_args[0] = "booti";
1021 } else { 1022 } else {
1022 addr = hdr->kernel_addr - hdr->page_size; 1023 addr = hdr->kernel_addr - hdr->page_size;
1023 boot_args[0] = "bootm"; 1024 boot_args[0] = "bootm";
1024 } 1025 }
1025 1026
1026 sprintf(boot_addr_start, "0x%lx", addr); 1027 sprintf(boot_addr_start, "0x%lx", addr);
1027 sprintf(ramdisk_addr, "0x%x:0x%x", hdr->ramdisk_addr, hdr->ramdisk_size); 1028 sprintf(ramdisk_addr, "0x%x:0x%x", hdr->ramdisk_addr, hdr->ramdisk_size);
1028 sprintf(fdt_addr, "0x%x", hdr->second_addr); 1029 sprintf(fdt_addr, "0x%x", hdr->second_addr);
1029 if (check_image_arm64) { 1030 if (check_image_arm64) {
1030 android_image_get_kernel(hdr, 0, NULL, NULL); 1031 android_image_get_kernel(hdr, 0, NULL, NULL);
1031 #ifdef CONFIG_CMD_BOOTI 1032 #ifdef CONFIG_CMD_BOOTI
1032 do_booti(NULL, 0, 4, boot_args); 1033 do_booti(NULL, 0, 4, boot_args);
1033 #else 1034 #else
1034 debug("please enable CONFIG_CMD_BOOTI when kernel are Image"); 1035 debug("please enable CONFIG_CMD_BOOTI when kernel are Image");
1035 #endif 1036 #endif
1036 } else { 1037 } else {
1037 do_bootm(NULL, 0, 4, boot_args); 1038 do_bootm(NULL, 0, 4, boot_args);
1038 } 1039 }
1039 /* This only happens if image is somehow faulty so we start over */ 1040 /* This only happens if image is somehow faulty so we start over */
1040 do_reset(NULL, 0, 0, NULL); 1041 do_reset(NULL, 0, 0, NULL);
1041 1042
1042 return 1; 1043 return 1;
1043 1044
1044 fail: 1045 fail:
1045 #if defined(CONFIG_FSL_FASTBOOT) 1046 #if defined(CONFIG_FSL_FASTBOOT)
1046 return run_command("fastboot 0", 0); 1047 return run_command("fastboot 0", 0);
1047 #else /*! CONFIG_FSL_FASTBOOT*/ 1048 #else /*! CONFIG_FSL_FASTBOOT*/
1048 return -1; 1049 return -1;
1049 #endif /*! CONFIG_FSL_FASTBOOT*/ 1050 #endif /*! CONFIG_FSL_FASTBOOT*/
1050 } 1051 }
1051 1052
1052 U_BOOT_CMD( 1053 U_BOOT_CMD(
1053 boota, 3, 1, do_boota, 1054 boota, 3, 1, do_boota,
1054 "boota - boot android bootimg from memory\n", 1055 "boota - boot android bootimg from memory\n",
1055 "[<addr> | mmc0 | mmc1 | mmc2 | mmcX] [<partition>]\n " 1056 "[<addr> | mmc0 | mmc1 | mmc2 | mmcX] [<partition>]\n "
1056 "- boot application image stored in memory or mmc\n" 1057 "- boot application image stored in memory or mmc\n"
1057 "\t'addr' should be the address of boot image " 1058 "\t'addr' should be the address of boot image "
1058 "which is zImage+ramdisk.img\n" 1059 "which is zImage+ramdisk.img\n"
1059 "\t'mmcX' is the mmc device you store your boot.img, " 1060 "\t'mmcX' is the mmc device you store your boot.img, "
1060 "which will read the boot.img from 1M offset('/boot' partition)\n" 1061 "which will read the boot.img from 1M offset('/boot' partition)\n"
1061 "\t 'partition' (optional) is the partition id of your device, " 1062 "\t 'partition' (optional) is the partition id of your device, "