bootm.c 13 KB

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