image-board.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Image code used by boards (and not host tools)
  4. *
  5. * (C) Copyright 2008 Semihalf
  6. *
  7. * (C) Copyright 2000-2006
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. */
  10. #include <common.h>
  11. #include <bootstage.h>
  12. #include <cpu_func.h>
  13. #include <env.h>
  14. #include <fpga.h>
  15. #include <image.h>
  16. #include <mapmem.h>
  17. #include <rtc.h>
  18. #include <watchdog.h>
  19. #include <asm/cache.h>
  20. #include <asm/global_data.h>
  21. #ifndef CONFIG_SYS_BARGSIZE
  22. #define CONFIG_SYS_BARGSIZE 512
  23. #endif
  24. DECLARE_GLOBAL_DATA_PTR;
  25. /**
  26. * image_get_ramdisk - get and verify ramdisk image
  27. * @rd_addr: ramdisk image start address
  28. * @arch: expected ramdisk architecture
  29. * @verify: checksum verification flag
  30. *
  31. * image_get_ramdisk() returns a pointer to the verified ramdisk image
  32. * header. Routine receives image start address and expected architecture
  33. * flag. Verification done covers data and header integrity and os/type/arch
  34. * fields checking.
  35. *
  36. * returns:
  37. * pointer to a ramdisk image header, if image was found and valid
  38. * otherwise, return NULL
  39. */
  40. static const image_header_t *image_get_ramdisk(ulong rd_addr, u8 arch,
  41. int verify)
  42. {
  43. const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
  44. if (!image_check_magic(rd_hdr)) {
  45. puts("Bad Magic Number\n");
  46. bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
  47. return NULL;
  48. }
  49. if (!image_check_hcrc(rd_hdr)) {
  50. puts("Bad Header Checksum\n");
  51. bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
  52. return NULL;
  53. }
  54. bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
  55. image_print_contents(rd_hdr);
  56. if (verify) {
  57. puts(" Verifying Checksum ... ");
  58. if (!image_check_dcrc(rd_hdr)) {
  59. puts("Bad Data CRC\n");
  60. bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
  61. return NULL;
  62. }
  63. puts("OK\n");
  64. }
  65. bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
  66. if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
  67. !image_check_arch(rd_hdr, arch) ||
  68. !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
  69. printf("No Linux %s Ramdisk Image\n",
  70. genimg_get_arch_name(arch));
  71. bootstage_error(BOOTSTAGE_ID_RAMDISK);
  72. return NULL;
  73. }
  74. return rd_hdr;
  75. }
  76. /*****************************************************************************/
  77. /* Shared dual-format routines */
  78. /*****************************************************************************/
  79. ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
  80. ulong image_save_addr; /* Default Save Address */
  81. ulong image_save_size; /* Default Save Size (in bytes) */
  82. static int on_loadaddr(const char *name, const char *value, enum env_op op,
  83. int flags)
  84. {
  85. switch (op) {
  86. case env_op_create:
  87. case env_op_overwrite:
  88. image_load_addr = hextoul(value, NULL);
  89. break;
  90. default:
  91. break;
  92. }
  93. return 0;
  94. }
  95. U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
  96. ulong env_get_bootm_low(void)
  97. {
  98. char *s = env_get("bootm_low");
  99. if (s) {
  100. ulong tmp = hextoul(s, NULL);
  101. return tmp;
  102. }
  103. #if defined(CONFIG_SYS_SDRAM_BASE)
  104. return CONFIG_SYS_SDRAM_BASE;
  105. #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
  106. return gd->bd->bi_dram[0].start;
  107. #else
  108. return 0;
  109. #endif
  110. }
  111. phys_size_t env_get_bootm_size(void)
  112. {
  113. phys_size_t tmp, size;
  114. phys_addr_t start;
  115. char *s = env_get("bootm_size");
  116. if (s) {
  117. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  118. return tmp;
  119. }
  120. start = gd->ram_base;
  121. size = gd->ram_size;
  122. if (start + size > gd->ram_top)
  123. size = gd->ram_top - start;
  124. s = env_get("bootm_low");
  125. if (s)
  126. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  127. else
  128. tmp = start;
  129. return size - (tmp - start);
  130. }
  131. phys_size_t env_get_bootm_mapsize(void)
  132. {
  133. phys_size_t tmp;
  134. char *s = env_get("bootm_mapsize");
  135. if (s) {
  136. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  137. return tmp;
  138. }
  139. #if defined(CONFIG_SYS_BOOTMAPSZ)
  140. return CONFIG_SYS_BOOTMAPSZ;
  141. #else
  142. return env_get_bootm_size();
  143. #endif
  144. }
  145. void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  146. {
  147. if (to == from)
  148. return;
  149. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  150. if (to > from) {
  151. from += len;
  152. to += len;
  153. }
  154. while (len > 0) {
  155. size_t tail = (len > chunksz) ? chunksz : len;
  156. WATCHDOG_RESET();
  157. if (to > from) {
  158. to -= tail;
  159. from -= tail;
  160. }
  161. memmove(to, from, tail);
  162. if (to < from) {
  163. to += tail;
  164. from += tail;
  165. }
  166. len -= tail;
  167. }
  168. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  169. memmove(to, from, len);
  170. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  171. }
  172. /**
  173. * genimg_get_kernel_addr_fit - get the real kernel address and return 2
  174. * FIT strings
  175. * @img_addr: a string might contain real image address
  176. * @fit_uname_config: double pointer to a char, will hold pointer to a
  177. * configuration unit name
  178. * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
  179. * name
  180. *
  181. * genimg_get_kernel_addr_fit get the real kernel start address from a string
  182. * which is normally the first argv of bootm/bootz
  183. *
  184. * returns:
  185. * kernel start address
  186. */
  187. ulong genimg_get_kernel_addr_fit(char * const img_addr,
  188. const char **fit_uname_config,
  189. const char **fit_uname_kernel)
  190. {
  191. ulong kernel_addr;
  192. /* find out kernel image address */
  193. if (!img_addr) {
  194. kernel_addr = image_load_addr;
  195. debug("* kernel: default image load address = 0x%08lx\n",
  196. image_load_addr);
  197. } else if (CONFIG_IS_ENABLED(FIT) &&
  198. fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
  199. fit_uname_config)) {
  200. debug("* kernel: config '%s' from image at 0x%08lx\n",
  201. *fit_uname_config, kernel_addr);
  202. } else if (CONFIG_IS_ENABLED(FIT) &&
  203. fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
  204. fit_uname_kernel)) {
  205. debug("* kernel: subimage '%s' from image at 0x%08lx\n",
  206. *fit_uname_kernel, kernel_addr);
  207. } else {
  208. kernel_addr = hextoul(img_addr, NULL);
  209. debug("* kernel: cmdline image address = 0x%08lx\n",
  210. kernel_addr);
  211. }
  212. return kernel_addr;
  213. }
  214. /**
  215. * genimg_get_kernel_addr() is the simple version of
  216. * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
  217. */
  218. ulong genimg_get_kernel_addr(char * const img_addr)
  219. {
  220. const char *fit_uname_config = NULL;
  221. const char *fit_uname_kernel = NULL;
  222. return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
  223. &fit_uname_kernel);
  224. }
  225. /**
  226. * genimg_get_format - get image format type
  227. * @img_addr: image start address
  228. *
  229. * genimg_get_format() checks whether provided address points to a valid
  230. * legacy or FIT image.
  231. *
  232. * New uImage format and FDT blob are based on a libfdt. FDT blob
  233. * may be passed directly or embedded in a FIT image. In both situations
  234. * genimg_get_format() must be able to dectect libfdt header.
  235. *
  236. * returns:
  237. * image format type or IMAGE_FORMAT_INVALID if no image is present
  238. */
  239. int genimg_get_format(const void *img_addr)
  240. {
  241. if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
  242. const image_header_t *hdr;
  243. hdr = (const image_header_t *)img_addr;
  244. if (image_check_magic(hdr))
  245. return IMAGE_FORMAT_LEGACY;
  246. }
  247. if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
  248. if (!fdt_check_header(img_addr))
  249. return IMAGE_FORMAT_FIT;
  250. }
  251. if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
  252. !android_image_check_header(img_addr))
  253. return IMAGE_FORMAT_ANDROID;
  254. return IMAGE_FORMAT_INVALID;
  255. }
  256. /**
  257. * fit_has_config - check if there is a valid FIT configuration
  258. * @images: pointer to the bootm command headers structure
  259. *
  260. * fit_has_config() checks if there is a FIT configuration in use
  261. * (if FTI support is present).
  262. *
  263. * returns:
  264. * 0, no FIT support or no configuration found
  265. * 1, configuration found
  266. */
  267. int genimg_has_config(bootm_headers_t *images)
  268. {
  269. if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
  270. return 1;
  271. return 0;
  272. }
  273. /**
  274. * select_ramdisk() - Select and locate the ramdisk to use
  275. *
  276. * @images: pointer to the bootm images structure
  277. * @select: name of ramdisk to select, or NULL for any
  278. * @arch: expected ramdisk architecture
  279. * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
  280. * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
  281. * @return 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
  282. * other -ve value on other error
  283. */
  284. static int select_ramdisk(bootm_headers_t *images, const char *select, u8 arch,
  285. ulong *rd_datap, ulong *rd_lenp)
  286. {
  287. ulong rd_addr = 0;
  288. char *buf;
  289. const char *fit_uname_config = images->fit_uname_cfg;
  290. const char *fit_uname_ramdisk = NULL;
  291. bool processed;
  292. int rd_noffset;
  293. if (select) {
  294. ulong default_addr;
  295. bool done = true;
  296. if (CONFIG_IS_ENABLED(FIT)) {
  297. /*
  298. * If the init ramdisk comes from the FIT image and
  299. * the FIT image address is omitted in the command
  300. * line argument, try to use os FIT image address or
  301. * default load address.
  302. */
  303. if (images->fit_uname_os)
  304. default_addr = (ulong)images->fit_hdr_os;
  305. else
  306. default_addr = image_load_addr;
  307. if (fit_parse_conf(select, default_addr, &rd_addr,
  308. &fit_uname_config)) {
  309. debug("* ramdisk: config '%s' from image at 0x%08lx\n",
  310. fit_uname_config, rd_addr);
  311. } else if (fit_parse_subimage(select, default_addr,
  312. &rd_addr,
  313. &fit_uname_ramdisk)) {
  314. debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
  315. fit_uname_ramdisk, rd_addr);
  316. } else {
  317. done = false;
  318. }
  319. }
  320. if (!done) {
  321. rd_addr = hextoul(select, NULL);
  322. debug("* ramdisk: cmdline image address = 0x%08lx\n",
  323. rd_addr);
  324. }
  325. } else if (CONFIG_IS_ENABLED(FIT)) {
  326. /* use FIT configuration provided in first bootm
  327. * command argument. If the property is not defined,
  328. * quit silently (with -ENOPKG )
  329. */
  330. rd_addr = map_to_sysmem(images->fit_hdr_os);
  331. rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
  332. rd_addr);
  333. if (rd_noffset == -ENOENT)
  334. return -ENOPKG;
  335. else if (rd_noffset < 0)
  336. return rd_noffset;
  337. }
  338. /*
  339. * Check if there is an initrd image at the
  340. * address provided in the second bootm argument
  341. * check image type, for FIT images get FIT node.
  342. */
  343. buf = map_sysmem(rd_addr, 0);
  344. processed = false;
  345. switch (genimg_get_format(buf)) {
  346. case IMAGE_FORMAT_LEGACY:
  347. if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
  348. const image_header_t *rd_hdr;
  349. printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
  350. rd_addr);
  351. bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
  352. rd_hdr = image_get_ramdisk(rd_addr, arch, images->verify);
  353. if (!rd_hdr)
  354. return -ENOENT;
  355. *rd_datap = image_get_data(rd_hdr);
  356. *rd_lenp = image_get_data_size(rd_hdr);
  357. processed = true;
  358. }
  359. break;
  360. case IMAGE_FORMAT_FIT:
  361. if (CONFIG_IS_ENABLED(FIT)) {
  362. rd_noffset = fit_image_load(images, rd_addr,
  363. &fit_uname_ramdisk,
  364. &fit_uname_config, arch,
  365. IH_TYPE_RAMDISK,
  366. BOOTSTAGE_ID_FIT_RD_START,
  367. FIT_LOAD_OPTIONAL_NON_ZERO,
  368. rd_datap, rd_lenp);
  369. if (rd_noffset < 0)
  370. return rd_noffset;
  371. images->fit_hdr_rd = map_sysmem(rd_addr, 0);
  372. images->fit_uname_rd = fit_uname_ramdisk;
  373. images->fit_noffset_rd = rd_noffset;
  374. processed = true;
  375. }
  376. break;
  377. case IMAGE_FORMAT_ANDROID:
  378. if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
  379. android_image_get_ramdisk((void *)images->os.start,
  380. rd_datap, rd_lenp);
  381. processed = true;
  382. }
  383. break;
  384. }
  385. if (!processed) {
  386. if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
  387. char *end = NULL;
  388. if (select)
  389. end = strchr(select, ':');
  390. if (end) {
  391. *rd_lenp = hextoul(++end, NULL);
  392. *rd_datap = rd_addr;
  393. processed = true;
  394. }
  395. }
  396. if (!processed) {
  397. puts("Wrong Ramdisk Image Format\n");
  398. return -EINVAL;
  399. }
  400. }
  401. return 0;
  402. }
  403. /**
  404. * boot_get_ramdisk - main ramdisk handling routine
  405. * @argc: command argument count
  406. * @argv: command argument list
  407. * @images: pointer to the bootm images structure
  408. * @arch: expected ramdisk architecture
  409. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  410. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  411. *
  412. * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
  413. * Currently supported are the following ramdisk sources:
  414. * - multicomponent kernel/ramdisk image,
  415. * - commandline provided address of decicated ramdisk image.
  416. *
  417. * returns:
  418. * 0, if ramdisk image was found and valid, or skiped
  419. * rd_start and rd_end are set to ramdisk start/end addresses if
  420. * ramdisk image is found and valid
  421. *
  422. * 1, if ramdisk image is found but corrupted, or invalid
  423. * rd_start and rd_end are set to 0 if no ramdisk exists
  424. */
  425. int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
  426. u8 arch, ulong *rd_start, ulong *rd_end)
  427. {
  428. ulong rd_data, rd_len;
  429. const char *select = NULL;
  430. *rd_start = 0;
  431. *rd_end = 0;
  432. if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
  433. char *buf;
  434. /* Look for an Android boot image */
  435. buf = map_sysmem(images->os.start, 0);
  436. if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
  437. select = (argc == 0) ? env_get("loadaddr") : argv[0];
  438. }
  439. if (argc >= 2)
  440. select = argv[1];
  441. /*
  442. * Look for a '-' which indicates to ignore the
  443. * ramdisk argument
  444. */
  445. if (select && strcmp(select, "-") == 0) {
  446. debug("## Skipping init Ramdisk\n");
  447. rd_len = 0;
  448. rd_data = 0;
  449. } else if (select || genimg_has_config(images)) {
  450. int ret;
  451. ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
  452. if (ret == -ENOPKG)
  453. return 0;
  454. else if (ret)
  455. return ret;
  456. } else if (images->legacy_hdr_valid &&
  457. image_check_type(&images->legacy_hdr_os_copy,
  458. IH_TYPE_MULTI)) {
  459. /*
  460. * Now check if we have a legacy mult-component image,
  461. * get second entry data start address and len.
  462. */
  463. bootstage_mark(BOOTSTAGE_ID_RAMDISK);
  464. printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
  465. (ulong)images->legacy_hdr_os);
  466. image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
  467. } else {
  468. /*
  469. * no initrd image
  470. */
  471. bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
  472. rd_len = 0;
  473. rd_data = 0;
  474. }
  475. if (!rd_data) {
  476. debug("## No init Ramdisk\n");
  477. } else {
  478. *rd_start = rd_data;
  479. *rd_end = rd_data + rd_len;
  480. }
  481. debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  482. *rd_start, *rd_end);
  483. return 0;
  484. }
  485. /**
  486. * boot_ramdisk_high - relocate init ramdisk
  487. * @lmb: pointer to lmb handle, will be used for memory mgmt
  488. * @rd_data: ramdisk data start address
  489. * @rd_len: ramdisk data length
  490. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  491. * start address (after possible relocation)
  492. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  493. * end address (after possible relocation)
  494. *
  495. * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
  496. * variable and if requested ramdisk data is moved to a specified location.
  497. *
  498. * Initrd_start and initrd_end are set to final (after relocation) ramdisk
  499. * start/end addresses if ramdisk image start and len were provided,
  500. * otherwise set initrd_start and initrd_end set to zeros.
  501. *
  502. * returns:
  503. * 0 - success
  504. * -1 - failure
  505. */
  506. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  507. ulong *initrd_start, ulong *initrd_end)
  508. {
  509. char *s;
  510. ulong initrd_high;
  511. int initrd_copy_to_ram = 1;
  512. s = env_get("initrd_high");
  513. if (s) {
  514. /* a value of "no" or a similar string will act like 0,
  515. * turning the "load high" feature off. This is intentional.
  516. */
  517. initrd_high = hextoul(s, NULL);
  518. if (initrd_high == ~0)
  519. initrd_copy_to_ram = 0;
  520. } else {
  521. initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
  522. }
  523. debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  524. initrd_high, initrd_copy_to_ram);
  525. if (rd_data) {
  526. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  527. debug(" in-place initrd\n");
  528. *initrd_start = rd_data;
  529. *initrd_end = rd_data + rd_len;
  530. lmb_reserve(lmb, rd_data, rd_len);
  531. } else {
  532. if (initrd_high)
  533. *initrd_start = (ulong)lmb_alloc_base(lmb,
  534. rd_len, 0x1000, initrd_high);
  535. else
  536. *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
  537. 0x1000);
  538. if (*initrd_start == 0) {
  539. puts("ramdisk - allocation error\n");
  540. goto error;
  541. }
  542. bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
  543. *initrd_end = *initrd_start + rd_len;
  544. printf(" Loading Ramdisk to %08lx, end %08lx ... ",
  545. *initrd_start, *initrd_end);
  546. memmove_wd((void *)*initrd_start,
  547. (void *)rd_data, rd_len, CHUNKSZ);
  548. /*
  549. * Ensure the image is flushed to memory to handle
  550. * AMP boot scenarios in which we might not be
  551. * HW cache coherent
  552. */
  553. if (IS_ENABLED(CONFIG_MP)) {
  554. flush_cache((unsigned long)*initrd_start,
  555. ALIGN(rd_len, ARCH_DMA_MINALIGN));
  556. }
  557. puts("OK\n");
  558. }
  559. } else {
  560. *initrd_start = 0;
  561. *initrd_end = 0;
  562. }
  563. debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  564. *initrd_start, *initrd_end);
  565. return 0;
  566. error:
  567. return -1;
  568. }
  569. int boot_get_setup(bootm_headers_t *images, u8 arch,
  570. ulong *setup_start, ulong *setup_len)
  571. {
  572. if (!CONFIG_IS_ENABLED(FIT))
  573. return -ENOENT;
  574. return boot_get_setup_fit(images, arch, setup_start, setup_len);
  575. }
  576. int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
  577. u8 arch, const ulong *ld_start, ulong * const ld_len)
  578. {
  579. ulong tmp_img_addr, img_data, img_len;
  580. void *buf;
  581. int conf_noffset;
  582. int fit_img_result;
  583. const char *uname, *name;
  584. int err;
  585. int devnum = 0; /* TODO support multi fpga platforms */
  586. if (!IS_ENABLED(CONFIG_FPGA))
  587. return -ENOSYS;
  588. /* Check to see if the images struct has a FIT configuration */
  589. if (!genimg_has_config(images)) {
  590. debug("## FIT configuration was not specified\n");
  591. return 0;
  592. }
  593. /*
  594. * Obtain the os FIT header from the images struct
  595. */
  596. tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
  597. buf = map_sysmem(tmp_img_addr, 0);
  598. /*
  599. * Check image type. For FIT images get FIT node
  600. * and attempt to locate a generic binary.
  601. */
  602. switch (genimg_get_format(buf)) {
  603. case IMAGE_FORMAT_FIT:
  604. conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
  605. uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
  606. NULL);
  607. if (!uname) {
  608. debug("## FPGA image is not specified\n");
  609. return 0;
  610. }
  611. fit_img_result = fit_image_load(images,
  612. tmp_img_addr,
  613. (const char **)&uname,
  614. &images->fit_uname_cfg,
  615. arch,
  616. IH_TYPE_FPGA,
  617. BOOTSTAGE_ID_FPGA_INIT,
  618. FIT_LOAD_OPTIONAL_NON_ZERO,
  619. &img_data, &img_len);
  620. debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
  621. uname, img_data, img_len);
  622. if (fit_img_result < 0) {
  623. /* Something went wrong! */
  624. return fit_img_result;
  625. }
  626. if (!fpga_is_partial_data(devnum, img_len)) {
  627. name = "full";
  628. err = fpga_loadbitstream(devnum, (char *)img_data,
  629. img_len, BIT_FULL);
  630. if (err)
  631. err = fpga_load(devnum, (const void *)img_data,
  632. img_len, BIT_FULL);
  633. } else {
  634. name = "partial";
  635. err = fpga_loadbitstream(devnum, (char *)img_data,
  636. img_len, BIT_PARTIAL);
  637. if (err)
  638. err = fpga_load(devnum, (const void *)img_data,
  639. img_len, BIT_PARTIAL);
  640. }
  641. if (err)
  642. return err;
  643. printf(" Programming %s bitstream... OK\n", name);
  644. break;
  645. default:
  646. printf("The given image format is not supported (corrupt?)\n");
  647. return 1;
  648. }
  649. return 0;
  650. }
  651. static void fit_loadable_process(u8 img_type,
  652. ulong img_data,
  653. ulong img_len)
  654. {
  655. int i;
  656. const unsigned int count =
  657. ll_entry_count(struct fit_loadable_tbl, fit_loadable);
  658. struct fit_loadable_tbl *fit_loadable_handler =
  659. ll_entry_start(struct fit_loadable_tbl, fit_loadable);
  660. /* For each loadable handler */
  661. for (i = 0; i < count; i++, fit_loadable_handler++)
  662. /* matching this type */
  663. if (fit_loadable_handler->type == img_type)
  664. /* call that handler with this image data */
  665. fit_loadable_handler->handler(img_data, img_len);
  666. }
  667. int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
  668. u8 arch, const ulong *ld_start, ulong * const ld_len)
  669. {
  670. /*
  671. * These variables are used to hold the current image location
  672. * in system memory.
  673. */
  674. ulong tmp_img_addr;
  675. /*
  676. * These two variables are requirements for fit_image_load, but
  677. * their values are not used
  678. */
  679. ulong img_data, img_len;
  680. void *buf;
  681. int loadables_index;
  682. int conf_noffset;
  683. int fit_img_result;
  684. const char *uname;
  685. u8 img_type;
  686. /* Check to see if the images struct has a FIT configuration */
  687. if (!genimg_has_config(images)) {
  688. debug("## FIT configuration was not specified\n");
  689. return 0;
  690. }
  691. /*
  692. * Obtain the os FIT header from the images struct
  693. */
  694. tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
  695. buf = map_sysmem(tmp_img_addr, 0);
  696. /*
  697. * Check image type. For FIT images get FIT node
  698. * and attempt to locate a generic binary.
  699. */
  700. switch (genimg_get_format(buf)) {
  701. case IMAGE_FORMAT_FIT:
  702. conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
  703. for (loadables_index = 0;
  704. uname = fdt_stringlist_get(buf, conf_noffset,
  705. FIT_LOADABLE_PROP,
  706. loadables_index, NULL), uname;
  707. loadables_index++) {
  708. fit_img_result = fit_image_load(images, tmp_img_addr,
  709. &uname,
  710. &images->fit_uname_cfg,
  711. arch, IH_TYPE_LOADABLE,
  712. BOOTSTAGE_ID_FIT_LOADABLE_START,
  713. FIT_LOAD_OPTIONAL_NON_ZERO,
  714. &img_data, &img_len);
  715. if (fit_img_result < 0) {
  716. /* Something went wrong! */
  717. return fit_img_result;
  718. }
  719. fit_img_result = fit_image_get_node(buf, uname);
  720. if (fit_img_result < 0) {
  721. /* Something went wrong! */
  722. return fit_img_result;
  723. }
  724. fit_img_result = fit_image_get_type(buf,
  725. fit_img_result,
  726. &img_type);
  727. if (fit_img_result < 0) {
  728. /* Something went wrong! */
  729. return fit_img_result;
  730. }
  731. fit_loadable_process(img_type, img_data, img_len);
  732. }
  733. break;
  734. default:
  735. printf("The given image format is not supported (corrupt?)\n");
  736. return 1;
  737. }
  738. return 0;
  739. }
  740. /**
  741. * boot_get_cmdline - allocate and initialize kernel cmdline
  742. * @lmb: pointer to lmb handle, will be used for memory mgmt
  743. * @cmd_start: pointer to a ulong variable, will hold cmdline start
  744. * @cmd_end: pointer to a ulong variable, will hold cmdline end
  745. *
  746. * boot_get_cmdline() allocates space for kernel command line below
  747. * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
  748. * variable is present its contents is copied to allocated kernel
  749. * command line.
  750. *
  751. * returns:
  752. * 0 - success
  753. * -1 - failure
  754. */
  755. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
  756. {
  757. char *cmdline;
  758. char *s;
  759. cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
  760. env_get_bootm_mapsize() + env_get_bootm_low());
  761. if (!cmdline)
  762. return -1;
  763. s = env_get("bootargs");
  764. if (!s)
  765. s = "";
  766. strcpy(cmdline, s);
  767. *cmd_start = (ulong)cmdline;
  768. *cmd_end = *cmd_start + strlen(cmdline);
  769. debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  770. return 0;
  771. }
  772. /**
  773. * boot_get_kbd - allocate and initialize kernel copy of board info
  774. * @lmb: pointer to lmb handle, will be used for memory mgmt
  775. * @kbd: double pointer to board info data
  776. *
  777. * boot_get_kbd() allocates space for kernel copy of board info data below
  778. * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
  779. * with the current u-boot board info data.
  780. *
  781. * returns:
  782. * 0 - success
  783. * -1 - failure
  784. */
  785. int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
  786. {
  787. *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
  788. sizeof(struct bd_info),
  789. 0xf,
  790. env_get_bootm_mapsize() +
  791. env_get_bootm_low());
  792. if (!*kbd)
  793. return -1;
  794. **kbd = *gd->bd;
  795. debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  796. #if defined(DEBUG)
  797. if (IS_ENABLED(CONFIG_CMD_BDI))
  798. do_bdinfo(NULL, 0, 0, NULL);
  799. #endif
  800. return 0;
  801. }
  802. int image_setup_linux(bootm_headers_t *images)
  803. {
  804. ulong of_size = images->ft_len;
  805. char **of_flat_tree = &images->ft_addr;
  806. struct lmb *lmb = &images->lmb;
  807. int ret;
  808. if (CONFIG_IS_ENABLED(OF_LIBFDT))
  809. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  810. if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
  811. ret = boot_get_cmdline(lmb, &images->cmdline_start,
  812. &images->cmdline_end);
  813. if (ret) {
  814. puts("ERROR with allocation of cmdline\n");
  815. return ret;
  816. }
  817. }
  818. if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
  819. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  820. if (ret)
  821. return ret;
  822. }
  823. if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
  824. ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
  825. if (ret)
  826. return ret;
  827. }
  828. return 0;
  829. }
  830. void genimg_print_size(uint32_t size)
  831. {
  832. printf("%d Bytes = ", size);
  833. print_size(size, "\n");
  834. }
  835. void genimg_print_time(time_t timestamp)
  836. {
  837. struct rtc_time tm;
  838. rtc_to_tm(timestamp, &tm);
  839. printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
  840. tm.tm_year, tm.tm_mon, tm.tm_mday,
  841. tm.tm_hour, tm.tm_min, tm.tm_sec);
  842. }