bootm.c 13 KB

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