Commit 45d3f14c63b593858145a14f55288efb319fe6c9

Authored by faqiang.zhu
1 parent 896d7aac49

MA-15339-1 load ramdisk in boot image to do first stage mount

With Android10 code, to build GSI image for devices launching with
Android10, the target should be "aosp_$arch-user". Google releases GSI
images is so built in user mode.

To do CTS-on-GSI test, a debug ramdisk containing .prop file to enable
adb root permission and GSI keys to verify the GSI image is needed, this
ramdisk is in boot image. so ramdisk in boot image need to be loaded by
uboot even in non-recovery mode.

To save boot time, only standard Android use ramdisk to boot up Android,
Android Auto keeps the original way: kernel be responsible for verify
and mount system partition. Let the customers to decide whether to use
recovery ramdisk to boot the system. and under this condition, user-debug
Android Auto GSI image need to be used for VTS-on-GSI test.

when use ramdisk to bootup Android, info provided by "dm=" bootarg is
not used by kernel to setup dm-verity, so it is removed from the
bootargs. The 4.19 kernel used together with this uboot does not handle
"skip_initramfs", so it's also removed.

Change-Id: Ia8b8fa8b85a44acda2670b46504038a009ce01a8
Signed-off-by: faqiang.zhu <faqiang.zhu@nxp.com>

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