bootm.c 13 KB

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