bootm.c 13 KB

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