1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 */
6
7 /*
8 * Boot support
9 */
10 #include <common.h>
11 #include <bootm.h>
12 #include <command.h>
13 #include <env.h>
14 #include <errno.h>
15 #include <image.h>
16 #include <malloc.h>
17 #include <nand.h>
18 #include <asm/byteorder.h>
19 #include <linux/ctype.h>
20 #include <linux/err.h>
21 #include <u-boot/zlib.h>
22 #include <mapmem.h>
23 #include "hi3516cv500_vo.h"
24 #if defined(CONFIG_OHOS_SEC_BOOT_SUPPORT)
25 #include "../product/hisec/sec_boot.h"
26 #endif
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 #if defined(CONFIG_CMD_IMI)
31 static int image_info(unsigned long addr);
32 #endif
33
34 #if defined(CONFIG_CMD_IMLS)
35 #include <flash.h>
36 #include <mtd/cfi_flash.h>
37 extern flash_info_t flash_info[]; /* info for FLASH chips */
38 #endif
39
40 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
41 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
42 #endif
43
44 /* we overload the cmd field with our state machine info instead of a
45 * function pointer */
46 static cmd_tbl_t cmd_bootm_sub[] = {
47 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
48 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
49 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
50 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
51 #endif
52 #ifdef CONFIG_OF_LIBFDT
53 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
54 #endif
55 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
56 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
57 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
58 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
59 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
60 };
61
do_bootm_subcommand(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])62 static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
63 char * const argv[])
64 {
65 int ret = 0;
66 long state;
67 cmd_tbl_t *c;
68
69 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
70 argc--; argv++;
71
72 if (c) {
73 state = (long)c->cmd;
74 if (state == BOOTM_STATE_START)
75 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
76 } else {
77 /* Unrecognized command */
78 return CMD_RET_USAGE;
79 }
80
81 if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
82 images.state >= state) {
83 printf("Trying to execute a command out of order\n");
84 return CMD_RET_USAGE;
85 }
86
87 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
88
89 return ret;
90 }
91
92 /*******************************************************************/
93 /* bootm - boot application image from image in memory */
94 /*******************************************************************/
95
96 #define MISC_LOAD_LOGO_ADDR 0X85000000
97 #define MISC_RGB_MAX_LEN 0xFEC
98 #define lOGO_SIZE 480 * 960 * 3
do_bootm(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])99 int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
100 {
101 if (strcmp(env_get("flag"), "1") == 0) {
102 extern int start_vo(unsigned int dev, unsigned int type, unsigned int sync);
103 extern int stop_vo(unsigned int dev);
104 extern int mipi_tx_display(unsigned int vosync);
105 extern int start_gx(unsigned int layer, unsigned addr, unsigned int strd,
106 unsigned int x, unsigned int y, unsigned int w, unsigned int h);
107 extern int stop_gx(unsigned int layer);
108 extern int set_vobg(unsigned int dev, unsigned int rgb);
109
110 const char cmd[100] = {0};
111
112 sprintf(cmd, "mmc read 0x0 %x %s %x", MISC_LOAD_LOGO_ADDR, env_get("misc_addr"), MISC_RGB_MAX_LEN);
113 run_command(cmd, 0);
114 set_vobg(0, 0xFFFFFF);
115 /* mipi 16384 */
116 start_vo(0, 16384, VO_OUTPUT_480x960_60);
117 mipi_tx_display(VO_OUTPUT_480x960_60);
118 start_gx(0, MISC_LOAD_LOGO_ADDR, 1440, 0, 0, 480, 960);
119 }
120 #if defined(CONFIG_OHOS_SEC_BOOT_SUPPORT)
121 if (CONFIG_OHOS_SEC_BOOT_ENABLE) {
122 int ret = check_security_boot(CONFIG_OHOS_X509_BIN_START_ADDR);
123 if (ret) {
124 printf("## Check security boot falied, sig_addr:%#X\n", CONFIG_OHOS_X509_BIN_START_ADDR);
125 while (1);
126 }
127 }
128 #endif
129
130 #ifdef CONFIG_ARM64_SUPPORT_LOAD_FIP
131 extern int is_fip(char *buf);
132 extern int load_fip(char *buf);
133 extern int load_fip_amp(char *buf);
134 extern long long kernel_load_addr;
135 if (2 == argc) {
136 char *buf = (char *)(uintptr_t)simple_strtoul(argv[1], NULL, 16);
137 /* Modify this configuration according to the system framework */
138 kernel_load_addr = CONFIG_KERNEL_LOAD_ADDR;
139 if (is_fip(buf)) {
140 return load_fip(buf);
141 }
142 }
143 #endif
144
145 #ifdef CONFIG_NEEDS_MANUAL_RELOC
146 static int relocated = 0;
147
148 if (!relocated) {
149 int i;
150
151 /* relocate names of sub-command table */
152 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
153 cmd_bootm_sub[i].name += gd->reloc_off;
154
155 relocated = 1;
156 }
157 #endif
158
159 /* determine if we have a sub command */
160 argc--; argv++;
161 if (argc > 0) {
162 char *endp;
163
164 simple_strtoul(argv[0], &endp, 16);
165 /* endp pointing to NULL means that argv[0] was just a
166 * valid number, pass it along to the normal bootm processing
167 *
168 * If endp is ':' or '#' assume a FIT identifier so pass
169 * along for normal processing.
170 *
171 * Right now we assume the first arg should never be '-'
172 */
173 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
174 return do_bootm_subcommand(cmdtp, flag, argc, argv);
175 }
176
177 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
178 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
179 BOOTM_STATE_LOADOS |
180 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
181 BOOTM_STATE_RAMDISK |
182 #endif
183 #if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
184 BOOTM_STATE_OS_CMDLINE |
185 #endif
186 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
187 BOOTM_STATE_OS_GO, &images, 1);
188 }
189
bootm_maybe_autostart(cmd_tbl_t * cmdtp,const char * cmd)190 int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
191 {
192 const char *ep = env_get("autostart");
193
194 if (ep && !strcmp(ep, "yes")) {
195 char *local_args[2];
196 local_args[0] = (char *)cmd;
197 local_args[1] = NULL;
198 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
199 return do_bootm(cmdtp, 0, 1, local_args);
200 }
201
202 return 0;
203 }
204
205 #ifdef CONFIG_SYS_LONGHELP
206 static char bootm_help_text[] =
207 "[addr [arg ...]]\n - boot application image stored in memory\n"
208 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
209 "\t'arg' can be the address of an initrd image\n"
210 #if defined(CONFIG_OF_LIBFDT)
211 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
212 "\ta third argument is required which is the address of the\n"
213 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
214 "\tuse a '-' for the second argument. If you do not pass a third\n"
215 "\ta bd_info struct will be passed instead\n"
216 #endif
217 #if defined(CONFIG_FIT)
218 "\t\nFor the new multi component uImage format (FIT) addresses\n"
219 "\tmust be extended to include component or configuration unit name:\n"
220 "\taddr:<subimg_uname> - direct component image specification\n"
221 "\taddr#<conf_uname> - configuration specification\n"
222 "\tUse iminfo command to get the list of existing component\n"
223 "\timages and configurations.\n"
224 #endif
225 "\nSub-commands to do part of the bootm sequence. The sub-commands "
226 "must be\n"
227 "issued in the order below (it's ok to not issue all sub-commands):\n"
228 "\tstart [addr [arg ...]]\n"
229 "\tloados - load OS image\n"
230 #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
231 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
232 #endif
233 #if defined(CONFIG_OF_LIBFDT)
234 "\tfdt - relocate flat device tree\n"
235 #endif
236 "\tcmdline - OS specific command line processing/setup\n"
237 "\tbdt - OS specific bd_t processing\n"
238 "\tprep - OS specific prep before relocation or go\n"
239 #if defined(CONFIG_TRACE)
240 "\tfake - OS specific fake start without go\n"
241 #endif
242 "\tgo - start OS";
243 #endif
244
245 U_BOOT_CMD(
246 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
247 "boot application image from memory", bootm_help_text
248 );
249
250 /*******************************************************************/
251 /* bootd - boot default image */
252 /*******************************************************************/
253 #if defined(CONFIG_CMD_BOOTD)
do_bootd(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])254 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
255 {
256 return run_command(env_get("bootcmd"), flag);
257 }
258
259 U_BOOT_CMD(
260 boot, 1, 1, do_bootd,
261 "boot default, i.e., run 'bootcmd'",
262 ""
263 );
264
265 /* keep old command name "bootd" for backward compatibility */
266 U_BOOT_CMD(
267 bootd, 1, 1, do_bootd,
268 "boot default, i.e., run 'bootcmd'",
269 ""
270 );
271
272 #endif
273
274
275 /*******************************************************************/
276 /* iminfo - print header info for a requested image */
277 /*******************************************************************/
278 #if defined(CONFIG_CMD_IMI)
do_iminfo(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])279 static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
280 {
281 int arg;
282 ulong addr;
283 int rcode = 0;
284
285 if (argc < 2) {
286 return image_info(load_addr);
287 }
288
289 for (arg = 1; arg < argc; ++arg) {
290 addr = simple_strtoul(argv[arg], NULL, 16);
291 if (image_info(addr) != 0)
292 rcode = 1;
293 }
294 return rcode;
295 }
296
image_info(ulong addr)297 static int image_info(ulong addr)
298 {
299 void *hdr = (void *)map_sysmem(addr, 0);
300
301 printf("\n## Checking Image at %08lx ...\n", addr);
302
303 switch (genimg_get_format(hdr)) {
304 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
305 case IMAGE_FORMAT_LEGACY:
306 puts(" Legacy image found\n");
307 if (!image_check_magic(hdr)) {
308 puts(" Bad Magic Number\n");
309 unmap_sysmem(hdr);
310 return 1;
311 }
312
313 if (!image_check_hcrc(hdr)) {
314 puts(" Bad Header Checksum\n");
315 unmap_sysmem(hdr);
316 return 1;
317 }
318
319 image_print_contents(hdr);
320
321 puts(" Verifying Checksum ... ");
322 if (!image_check_dcrc(hdr)) {
323 puts(" Bad Data CRC\n");
324 unmap_sysmem(hdr);
325 return 1;
326 }
327 puts("OK\n");
328 unmap_sysmem(hdr);
329 return 0;
330 #endif
331 #if defined(CONFIG_ANDROID_BOOT_IMAGE)
332 case IMAGE_FORMAT_ANDROID:
333 puts(" Android image found\n");
334 android_print_contents(hdr);
335 unmap_sysmem(hdr);
336 return 0;
337 #endif
338 #if defined(CONFIG_FIT)
339 case IMAGE_FORMAT_FIT:
340 puts(" FIT image found\n");
341
342 if (fit_check_format(hdr, IMAGE_SIZE_INVAL)) {
343 puts("Bad FIT image format!\n");
344 unmap_sysmem(hdr);
345 return 1;
346 }
347
348 fit_print_contents(hdr);
349
350 if (!fit_all_image_verify(hdr)) {
351 puts("Bad hash in FIT image!\n");
352 unmap_sysmem(hdr);
353 return 1;
354 }
355
356 unmap_sysmem(hdr);
357 return 0;
358 #endif
359 default:
360 puts("Unknown image format!\n");
361 break;
362 }
363
364 unmap_sysmem(hdr);
365 return 1;
366 }
367
368 U_BOOT_CMD(
369 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
370 "print header information for application image",
371 "addr [addr ...]\n"
372 " - print header information for application image starting at\n"
373 " address 'addr' in memory; this includes verification of the\n"
374 " image contents (magic number, header and payload checksums)"
375 );
376 #endif
377
378
379 /*******************************************************************/
380 /* imls - list all images found in flash */
381 /*******************************************************************/
382 #if defined(CONFIG_CMD_IMLS)
do_imls_nor(void)383 static int do_imls_nor(void)
384 {
385 flash_info_t *info;
386 int i, j;
387 void *hdr;
388
389 for (i = 0, info = &flash_info[0];
390 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
391
392 if (info->flash_id == FLASH_UNKNOWN)
393 goto next_bank;
394 for (j = 0; j < info->sector_count; ++j) {
395
396 hdr = (void *)info->start[j];
397 if (!hdr)
398 goto next_sector;
399
400 switch (genimg_get_format(hdr)) {
401 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
402 case IMAGE_FORMAT_LEGACY:
403 if (!image_check_hcrc(hdr))
404 goto next_sector;
405
406 printf("Legacy Image at %08lX:\n", (ulong)hdr);
407 image_print_contents(hdr);
408
409 puts(" Verifying Checksum ... ");
410 if (!image_check_dcrc(hdr)) {
411 puts("Bad Data CRC\n");
412 } else {
413 puts("OK\n");
414 }
415 break;
416 #endif
417 #if defined(CONFIG_FIT)
418 case IMAGE_FORMAT_FIT:
419 if (fit_check_format(hdr, IMAGE_SIZE_INVAL))
420 goto next_sector;
421
422 printf("FIT Image at %08lX:\n", (ulong)hdr);
423 fit_print_contents(hdr);
424 break;
425 #endif
426 default:
427 goto next_sector;
428 }
429
430 next_sector: ;
431 }
432 next_bank: ;
433 }
434 return 0;
435 }
436 #endif
437
438 #if defined(CONFIG_CMD_IMLS_NAND)
nand_imls_legacyimage(struct mtd_info * mtd,int nand_dev,loff_t off,size_t len)439 static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
440 loff_t off, size_t len)
441 {
442 void *imgdata;
443 int ret;
444
445 imgdata = malloc(len);
446 if (!imgdata) {
447 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
448 nand_dev, off);
449 printf(" Low memory(cannot allocate memory for image)\n");
450 return -ENOMEM;
451 }
452
453 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
454 if (ret < 0 && ret != -EUCLEAN) {
455 free(imgdata);
456 return ret;
457 }
458
459 if (!image_check_hcrc(imgdata)) {
460 free(imgdata);
461 return 0;
462 }
463
464 printf("Legacy Image at NAND device %d offset %08llX:\n",
465 nand_dev, off);
466 image_print_contents(imgdata);
467
468 puts(" Verifying Checksum ... ");
469 if (!image_check_dcrc(imgdata))
470 puts("Bad Data CRC\n");
471 else
472 puts("OK\n");
473
474 free(imgdata);
475
476 return 0;
477 }
478
nand_imls_fitimage(struct mtd_info * mtd,int nand_dev,loff_t off,size_t len)479 static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
480 size_t len)
481 {
482 void *imgdata;
483 int ret;
484
485 imgdata = malloc(len);
486 if (!imgdata) {
487 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
488 nand_dev, off);
489 printf(" Low memory(cannot allocate memory for image)\n");
490 return -ENOMEM;
491 }
492
493 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
494 if (ret < 0 && ret != -EUCLEAN) {
495 free(imgdata);
496 return ret;
497 }
498
499 if (fit_check_format(imgdata, IMAGE_SIZE_INVAL)) {
500 free(imgdata);
501 return 0;
502 }
503
504 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
505
506 fit_print_contents(imgdata);
507 free(imgdata);
508
509 return 0;
510 }
511
do_imls_nand(void)512 static int do_imls_nand(void)
513 {
514 struct mtd_info *mtd;
515 int nand_dev = nand_curr_device;
516 size_t len;
517 loff_t off;
518 u32 buffer[16];
519
520 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
521 puts("\nNo NAND devices available\n");
522 return -ENODEV;
523 }
524
525 printf("\n");
526
527 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
528 mtd = get_nand_dev_by_index(nand_dev);
529 if (!mtd->name || !mtd->size)
530 continue;
531
532 for (off = 0; off < mtd->size; off += mtd->erasesize) {
533 const image_header_t *header;
534 int ret;
535
536 if (nand_block_isbad(mtd, off))
537 continue;
538
539 len = sizeof(buffer);
540
541 ret = nand_read(mtd, off, &len, (u8 *)buffer);
542 if (ret < 0 && ret != -EUCLEAN) {
543 printf("NAND read error %d at offset %08llX\n",
544 ret, off);
545 continue;
546 }
547
548 switch (genimg_get_format(buffer)) {
549 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
550 case IMAGE_FORMAT_LEGACY:
551 header = (const image_header_t *)buffer;
552
553 len = image_get_image_size(header);
554 nand_imls_legacyimage(mtd, nand_dev, off, len);
555 break;
556 #endif
557 #if defined(CONFIG_FIT)
558 case IMAGE_FORMAT_FIT:
559 len = fit_get_size(buffer);
560 nand_imls_fitimage(mtd, nand_dev, off, len);
561 break;
562 #endif
563 }
564 }
565 }
566
567 return 0;
568 }
569 #endif
570
571 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
do_imls(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])572 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
573 {
574 int ret_nor = 0, ret_nand = 0;
575
576 #if defined(CONFIG_CMD_IMLS)
577 ret_nor = do_imls_nor();
578 #endif
579
580 #if defined(CONFIG_CMD_IMLS_NAND)
581 ret_nand = do_imls_nand();
582 #endif
583
584 if (ret_nor)
585 return ret_nor;
586
587 if (ret_nand)
588 return ret_nand;
589
590 return (0);
591 }
592
593 U_BOOT_CMD(
594 imls, 1, 1, do_imls,
595 "list all images found in flash",
596 "\n"
597 " - Prints information about all images found at sector/block\n"
598 " boundaries in nor/nand flash."
599 );
600 #endif
601