bootm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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",
  126. image_load_addr);
  127. return do_bootm(cmdtp, 0, 1, local_args);
  128. }
  129. return 0;
  130. }
  131. #ifdef CONFIG_SYS_LONGHELP
  132. static char bootm_help_text[] =
  133. "[addr [arg ...]]\n - boot application image stored in memory\n"
  134. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  135. "\t'arg' can be the address of an initrd image\n"
  136. #if defined(CONFIG_OF_LIBFDT)
  137. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  138. "\ta third argument is required which is the address of the\n"
  139. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  140. "\tuse a '-' for the second argument. If you do not pass a third\n"
  141. "\ta bd_info struct will be passed instead\n"
  142. #endif
  143. #if defined(CONFIG_FIT)
  144. "\t\nFor the new multi component uImage format (FIT) addresses\n"
  145. "\tmust be extended to include component or configuration unit name:\n"
  146. "\taddr:<subimg_uname> - direct component image specification\n"
  147. "\taddr#<conf_uname> - configuration specification\n"
  148. "\tUse iminfo command to get the list of existing component\n"
  149. "\timages and configurations.\n"
  150. #endif
  151. "\nSub-commands to do part of the bootm sequence. The sub-commands "
  152. "must be\n"
  153. "issued in the order below (it's ok to not issue all sub-commands):\n"
  154. "\tstart [addr [arg ...]]\n"
  155. "\tloados - load OS image\n"
  156. #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
  157. "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
  158. #endif
  159. #if defined(CONFIG_OF_LIBFDT)
  160. "\tfdt - relocate flat device tree\n"
  161. #endif
  162. "\tcmdline - OS specific command line processing/setup\n"
  163. "\tbdt - OS specific bd_t processing\n"
  164. "\tprep - OS specific prep before relocation or go\n"
  165. #if defined(CONFIG_TRACE)
  166. "\tfake - OS specific fake start without go\n"
  167. #endif
  168. "\tgo - start OS";
  169. #endif
  170. U_BOOT_CMD(
  171. bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
  172. "boot application image from memory", bootm_help_text
  173. );
  174. /*******************************************************************/
  175. /* bootd - boot default image */
  176. /*******************************************************************/
  177. #if defined(CONFIG_CMD_BOOTD)
  178. int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  179. {
  180. return run_command(env_get("bootcmd"), flag);
  181. }
  182. U_BOOT_CMD(
  183. boot, 1, 1, do_bootd,
  184. "boot default, i.e., run 'bootcmd'",
  185. ""
  186. );
  187. /* keep old command name "bootd" for backward compatibility */
  188. U_BOOT_CMD(
  189. bootd, 1, 1, do_bootd,
  190. "boot default, i.e., run 'bootcmd'",
  191. ""
  192. );
  193. #endif
  194. /*******************************************************************/
  195. /* iminfo - print header info for a requested image */
  196. /*******************************************************************/
  197. #if defined(CONFIG_CMD_IMI)
  198. static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  199. {
  200. int arg;
  201. ulong addr;
  202. int rcode = 0;
  203. if (argc < 2) {
  204. return image_info(image_load_addr);
  205. }
  206. for (arg = 1; arg < argc; ++arg) {
  207. addr = simple_strtoul(argv[arg], NULL, 16);
  208. if (image_info(addr) != 0)
  209. rcode = 1;
  210. }
  211. return rcode;
  212. }
  213. static int image_info(ulong addr)
  214. {
  215. void *hdr = (void *)map_sysmem(addr, 0);
  216. printf("\n## Checking Image at %08lx ...\n", addr);
  217. switch (genimg_get_format(hdr)) {
  218. #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
  219. case IMAGE_FORMAT_LEGACY:
  220. puts(" Legacy image found\n");
  221. if (!image_check_magic(hdr)) {
  222. puts(" Bad Magic Number\n");
  223. unmap_sysmem(hdr);
  224. return 1;
  225. }
  226. if (!image_check_hcrc(hdr)) {
  227. puts(" Bad Header Checksum\n");
  228. unmap_sysmem(hdr);
  229. return 1;
  230. }
  231. image_print_contents(hdr);
  232. puts(" Verifying Checksum ... ");
  233. if (!image_check_dcrc(hdr)) {
  234. puts(" Bad Data CRC\n");
  235. unmap_sysmem(hdr);
  236. return 1;
  237. }
  238. puts("OK\n");
  239. unmap_sysmem(hdr);
  240. return 0;
  241. #endif
  242. #if defined(CONFIG_ANDROID_BOOT_IMAGE)
  243. case IMAGE_FORMAT_ANDROID:
  244. puts(" Android image found\n");
  245. android_print_contents(hdr);
  246. unmap_sysmem(hdr);
  247. return 0;
  248. #endif
  249. #if defined(CONFIG_FIT)
  250. case IMAGE_FORMAT_FIT:
  251. puts(" FIT image found\n");
  252. if (!fit_check_format(hdr)) {
  253. puts("Bad FIT image format!\n");
  254. unmap_sysmem(hdr);
  255. return 1;
  256. }
  257. fit_print_contents(hdr);
  258. if (!fit_all_image_verify(hdr)) {
  259. puts("Bad hash in FIT image!\n");
  260. unmap_sysmem(hdr);
  261. return 1;
  262. }
  263. unmap_sysmem(hdr);
  264. return 0;
  265. #endif
  266. default:
  267. puts("Unknown image format!\n");
  268. break;
  269. }
  270. unmap_sysmem(hdr);
  271. return 1;
  272. }
  273. U_BOOT_CMD(
  274. iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
  275. "print header information for application image",
  276. "addr [addr ...]\n"
  277. " - print header information for application image starting at\n"
  278. " address 'addr' in memory; this includes verification of the\n"
  279. " image contents (magic number, header and payload checksums)"
  280. );
  281. #endif
  282. /*******************************************************************/
  283. /* imls - list all images found in flash */
  284. /*******************************************************************/
  285. #if defined(CONFIG_CMD_IMLS)
  286. static int do_imls_nor(void)
  287. {
  288. flash_info_t *info;
  289. int i, j;
  290. void *hdr;
  291. for (i = 0, info = &flash_info[0];
  292. i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  293. if (info->flash_id == FLASH_UNKNOWN)
  294. goto next_bank;
  295. for (j = 0; j < info->sector_count; ++j) {
  296. hdr = (void *)info->start[j];
  297. if (!hdr)
  298. goto next_sector;
  299. switch (genimg_get_format(hdr)) {
  300. #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
  301. case IMAGE_FORMAT_LEGACY:
  302. if (!image_check_hcrc(hdr))
  303. goto next_sector;
  304. printf("Legacy Image at %08lX:\n", (ulong)hdr);
  305. image_print_contents(hdr);
  306. puts(" Verifying Checksum ... ");
  307. if (!image_check_dcrc(hdr)) {
  308. puts("Bad Data CRC\n");
  309. } else {
  310. puts("OK\n");
  311. }
  312. break;
  313. #endif
  314. #if defined(CONFIG_FIT)
  315. case IMAGE_FORMAT_FIT:
  316. if (!fit_check_format(hdr))
  317. goto next_sector;
  318. printf("FIT Image at %08lX:\n", (ulong)hdr);
  319. fit_print_contents(hdr);
  320. break;
  321. #endif
  322. default:
  323. goto next_sector;
  324. }
  325. next_sector: ;
  326. }
  327. next_bank: ;
  328. }
  329. return 0;
  330. }
  331. #endif
  332. #if defined(CONFIG_CMD_IMLS_NAND)
  333. static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
  334. loff_t off, size_t len)
  335. {
  336. void *imgdata;
  337. int ret;
  338. imgdata = malloc(len);
  339. if (!imgdata) {
  340. printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
  341. nand_dev, off);
  342. printf(" Low memory(cannot allocate memory for image)\n");
  343. return -ENOMEM;
  344. }
  345. ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
  346. if (ret < 0 && ret != -EUCLEAN) {
  347. free(imgdata);
  348. return ret;
  349. }
  350. if (!image_check_hcrc(imgdata)) {
  351. free(imgdata);
  352. return 0;
  353. }
  354. printf("Legacy Image at NAND device %d offset %08llX:\n",
  355. nand_dev, off);
  356. image_print_contents(imgdata);
  357. puts(" Verifying Checksum ... ");
  358. if (!image_check_dcrc(imgdata))
  359. puts("Bad Data CRC\n");
  360. else
  361. puts("OK\n");
  362. free(imgdata);
  363. return 0;
  364. }
  365. static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
  366. size_t len)
  367. {
  368. void *imgdata;
  369. int ret;
  370. imgdata = malloc(len);
  371. if (!imgdata) {
  372. printf("May be a FIT Image at NAND device %d offset %08llX:\n",
  373. nand_dev, off);
  374. printf(" Low memory(cannot allocate memory for image)\n");
  375. return -ENOMEM;
  376. }
  377. ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
  378. if (ret < 0 && ret != -EUCLEAN) {
  379. free(imgdata);
  380. return ret;
  381. }
  382. if (!fit_check_format(imgdata)) {
  383. free(imgdata);
  384. return 0;
  385. }
  386. printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
  387. fit_print_contents(imgdata);
  388. free(imgdata);
  389. return 0;
  390. }
  391. static int do_imls_nand(void)
  392. {
  393. struct mtd_info *mtd;
  394. int nand_dev = nand_curr_device;
  395. size_t len;
  396. loff_t off;
  397. u32 buffer[16];
  398. if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  399. puts("\nNo NAND devices available\n");
  400. return -ENODEV;
  401. }
  402. printf("\n");
  403. for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
  404. mtd = get_nand_dev_by_index(nand_dev);
  405. if (!mtd->name || !mtd->size)
  406. continue;
  407. for (off = 0; off < mtd->size; off += mtd->erasesize) {
  408. const image_header_t *header;
  409. int ret;
  410. if (nand_block_isbad(mtd, off))
  411. continue;
  412. len = sizeof(buffer);
  413. ret = nand_read(mtd, off, &len, (u8 *)buffer);
  414. if (ret < 0 && ret != -EUCLEAN) {
  415. printf("NAND read error %d at offset %08llX\n",
  416. ret, off);
  417. continue;
  418. }
  419. switch (genimg_get_format(buffer)) {
  420. #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
  421. case IMAGE_FORMAT_LEGACY:
  422. header = (const image_header_t *)buffer;
  423. len = image_get_image_size(header);
  424. nand_imls_legacyimage(mtd, nand_dev, off, len);
  425. break;
  426. #endif
  427. #if defined(CONFIG_FIT)
  428. case IMAGE_FORMAT_FIT:
  429. len = fit_get_size(buffer);
  430. nand_imls_fitimage(mtd, nand_dev, off, len);
  431. break;
  432. #endif
  433. }
  434. }
  435. }
  436. return 0;
  437. }
  438. #endif
  439. #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
  440. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  441. {
  442. int ret_nor = 0, ret_nand = 0;
  443. #if defined(CONFIG_CMD_IMLS)
  444. ret_nor = do_imls_nor();
  445. #endif
  446. #if defined(CONFIG_CMD_IMLS_NAND)
  447. ret_nand = do_imls_nand();
  448. #endif
  449. if (ret_nor)
  450. return ret_nor;
  451. if (ret_nand)
  452. return ret_nand;
  453. return (0);
  454. }
  455. U_BOOT_CMD(
  456. imls, 1, 1, do_imls,
  457. "list all images found in flash",
  458. "\n"
  459. " - Prints information about all images found at sector/block\n"
  460. " boundaries in nor/nand flash."
  461. );
  462. #endif