image-board.c 24 KB

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