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