bootm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. #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 = getenv("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(getenv("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 *)addr;
  215. printf("\n## Checking Image at %08lx ...\n", addr);
  216. switch (genimg_get_format(hdr)) {
  217. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  218. case IMAGE_FORMAT_LEGACY:
  219. puts(" Legacy image found\n");
  220. if (!image_check_magic(hdr)) {
  221. puts(" Bad Magic Number\n");
  222. return 1;
  223. }
  224. if (!image_check_hcrc(hdr)) {
  225. puts(" Bad Header Checksum\n");
  226. return 1;
  227. }
  228. image_print_contents(hdr);
  229. puts(" Verifying Checksum ... ");
  230. if (!image_check_dcrc(hdr)) {
  231. puts(" Bad Data CRC\n");
  232. return 1;
  233. }
  234. puts("OK\n");
  235. return 0;
  236. #endif
  237. #if defined(CONFIG_ANDROID_BOOT_IMAGE)
  238. case IMAGE_FORMAT_ANDROID:
  239. puts(" Android image found\n");
  240. android_print_contents(hdr);
  241. return 0;
  242. #endif
  243. #if defined(CONFIG_FIT)
  244. case IMAGE_FORMAT_FIT:
  245. puts(" FIT image found\n");
  246. if (!fit_check_format(hdr)) {
  247. puts("Bad FIT image format!\n");
  248. return 1;
  249. }
  250. fit_print_contents(hdr);
  251. if (!fit_all_image_verify(hdr)) {
  252. puts("Bad hash in FIT image!\n");
  253. return 1;
  254. }
  255. return 0;
  256. #endif
  257. default:
  258. puts("Unknown image format!\n");
  259. break;
  260. }
  261. return 1;
  262. }
  263. U_BOOT_CMD(
  264. iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
  265. "print header information for application image",
  266. "addr [addr ...]\n"
  267. " - print header information for application image starting at\n"
  268. " address 'addr' in memory; this includes verification of the\n"
  269. " image contents (magic number, header and payload checksums)"
  270. );
  271. #endif
  272. /*******************************************************************/
  273. /* imls - list all images found in flash */
  274. /*******************************************************************/
  275. #if defined(CONFIG_CMD_IMLS)
  276. static int do_imls_nor(void)
  277. {
  278. flash_info_t *info;
  279. int i, j;
  280. void *hdr;
  281. for (i = 0, info = &flash_info[0];
  282. i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  283. if (info->flash_id == FLASH_UNKNOWN)
  284. goto next_bank;
  285. for (j = 0; j < info->sector_count; ++j) {
  286. hdr = (void *)info->start[j];
  287. if (!hdr)
  288. goto next_sector;
  289. switch (genimg_get_format(hdr)) {
  290. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  291. case IMAGE_FORMAT_LEGACY:
  292. if (!image_check_hcrc(hdr))
  293. goto next_sector;
  294. printf("Legacy Image at %08lX:\n", (ulong)hdr);
  295. image_print_contents(hdr);
  296. puts(" Verifying Checksum ... ");
  297. if (!image_check_dcrc(hdr)) {
  298. puts("Bad Data CRC\n");
  299. } else {
  300. puts("OK\n");
  301. }
  302. break;
  303. #endif
  304. #if defined(CONFIG_FIT)
  305. case IMAGE_FORMAT_FIT:
  306. if (!fit_check_format(hdr))
  307. goto next_sector;
  308. printf("FIT Image at %08lX:\n", (ulong)hdr);
  309. fit_print_contents(hdr);
  310. break;
  311. #endif
  312. default:
  313. goto next_sector;
  314. }
  315. next_sector: ;
  316. }
  317. next_bank: ;
  318. }
  319. return 0;
  320. }
  321. #endif
  322. #if defined(CONFIG_CMD_IMLS_NAND)
  323. static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
  324. loff_t off, size_t len)
  325. {
  326. void *imgdata;
  327. int ret;
  328. imgdata = malloc(len);
  329. if (!imgdata) {
  330. printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
  331. nand_dev, off);
  332. printf(" Low memory(cannot allocate memory for image)\n");
  333. return -ENOMEM;
  334. }
  335. ret = nand_read_skip_bad(mtd, off, &len, imgdata);
  336. if (ret < 0 && ret != -EUCLEAN) {
  337. free(imgdata);
  338. return ret;
  339. }
  340. if (!image_check_hcrc(imgdata)) {
  341. free(imgdata);
  342. return 0;
  343. }
  344. printf("Legacy Image at NAND device %d offset %08llX:\n",
  345. nand_dev, off);
  346. image_print_contents(imgdata);
  347. puts(" Verifying Checksum ... ");
  348. if (!image_check_dcrc(imgdata))
  349. puts("Bad Data CRC\n");
  350. else
  351. puts("OK\n");
  352. free(imgdata);
  353. return 0;
  354. }
  355. static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
  356. size_t len)
  357. {
  358. void *imgdata;
  359. int ret;
  360. imgdata = malloc(len);
  361. if (!imgdata) {
  362. printf("May be a FIT Image at NAND device %d offset %08llX:\n",
  363. nand_dev, off);
  364. printf(" Low memory(cannot allocate memory for image)\n");
  365. return -ENOMEM;
  366. }
  367. ret = nand_read_skip_bad(mtd, off, &len, imgdata);
  368. if (ret < 0 && ret != -EUCLEAN) {
  369. free(imgdata);
  370. return ret;
  371. }
  372. if (!fit_check_format(imgdata)) {
  373. free(imgdata);
  374. return 0;
  375. }
  376. printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
  377. fit_print_contents(imgdata);
  378. free(imgdata);
  379. return 0;
  380. }
  381. static int do_imls_nand(void)
  382. {
  383. struct mtd_info *mtd;
  384. int nand_dev = nand_curr_device;
  385. size_t len;
  386. loff_t off;
  387. u32 buffer[16];
  388. if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  389. puts("\nNo NAND devices available\n");
  390. return -ENODEV;
  391. }
  392. printf("\n");
  393. for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
  394. mtd = nand_info[nand_dev];
  395. if (!mtd->name || !mtd->size)
  396. continue;
  397. for (off = 0; off < mtd->size; off += mtd->erasesize) {
  398. const image_header_t *header;
  399. int ret;
  400. if (nand_block_isbad(mtd, off))
  401. continue;
  402. len = sizeof(buffer);
  403. ret = nand_read(mtd, off, &len, (u8 *)buffer);
  404. if (ret < 0 && ret != -EUCLEAN) {
  405. printf("NAND read error %d at offset %08llX\n",
  406. ret, off);
  407. continue;
  408. }
  409. switch (genimg_get_format(buffer)) {
  410. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  411. case IMAGE_FORMAT_LEGACY:
  412. header = (const image_header_t *)buffer;
  413. len = image_get_image_size(header);
  414. nand_imls_legacyimage(mtd, nand_dev, off, len);
  415. break;
  416. #endif
  417. #if defined(CONFIG_FIT)
  418. case IMAGE_FORMAT_FIT:
  419. len = fit_get_size(buffer);
  420. nand_imls_fitimage(mtd, nand_dev, off, len);
  421. break;
  422. #endif
  423. }
  424. }
  425. }
  426. return 0;
  427. }
  428. #endif
  429. #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
  430. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  431. {
  432. int ret_nor = 0, ret_nand = 0;
  433. #if defined(CONFIG_CMD_IMLS)
  434. ret_nor = do_imls_nor();
  435. #endif
  436. #if defined(CONFIG_CMD_IMLS_NAND)
  437. ret_nand = do_imls_nand();
  438. #endif
  439. if (ret_nor)
  440. return ret_nor;
  441. if (ret_nand)
  442. return ret_nand;
  443. return (0);
  444. }
  445. U_BOOT_CMD(
  446. imls, 1, 1, do_imls,
  447. "list all images found in flash",
  448. "\n"
  449. " - Prints information about all images found at sector/block\n"
  450. " boundaries in nor/nand flash."
  451. );
  452. #endif