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