bootm.c 13 KB

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