image.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2006
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #ifndef USE_HOSTCC
  10. #include <common.h>
  11. #include <watchdog.h>
  12. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  13. #include <status_led.h>
  14. #endif
  15. #ifdef CONFIG_HAS_DATAFLASH
  16. #include <dataflash.h>
  17. #endif
  18. #ifdef CONFIG_LOGBUFFER
  19. #include <logbuff.h>
  20. #endif
  21. #include <rtc.h>
  22. #include <environment.h>
  23. #include <image.h>
  24. #include <mapmem.h>
  25. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  26. #include <libfdt.h>
  27. #include <fdt_support.h>
  28. #endif
  29. #include <u-boot/md5.h>
  30. #include <u-boot/sha1.h>
  31. #include <asm/errno.h>
  32. #include <asm/io.h>
  33. #ifdef CONFIG_CMD_BDI
  34. extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  35. #endif
  36. DECLARE_GLOBAL_DATA_PTR;
  37. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  38. static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
  39. int verify);
  40. #endif
  41. #else
  42. #include "mkimage.h"
  43. #include <u-boot/md5.h>
  44. #include <time.h>
  45. #include <image.h>
  46. #ifndef __maybe_unused
  47. # define __maybe_unused /* unimplemented */
  48. #endif
  49. #endif /* !USE_HOSTCC*/
  50. #include <u-boot/crc.h>
  51. #ifndef CONFIG_SYS_BARGSIZE
  52. #define CONFIG_SYS_BARGSIZE 512
  53. #endif
  54. static const table_entry_t uimage_arch[] = {
  55. { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
  56. { IH_ARCH_ALPHA, "alpha", "Alpha", },
  57. { IH_ARCH_ARM, "arm", "ARM", },
  58. { IH_ARCH_I386, "x86", "Intel x86", },
  59. { IH_ARCH_IA64, "ia64", "IA64", },
  60. { IH_ARCH_M68K, "m68k", "M68K", },
  61. { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
  62. { IH_ARCH_MIPS, "mips", "MIPS", },
  63. { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
  64. { IH_ARCH_NIOS2, "nios2", "NIOS II", },
  65. { IH_ARCH_PPC, "powerpc", "PowerPC", },
  66. { IH_ARCH_PPC, "ppc", "PowerPC", },
  67. { IH_ARCH_S390, "s390", "IBM S390", },
  68. { IH_ARCH_SH, "sh", "SuperH", },
  69. { IH_ARCH_SPARC, "sparc", "SPARC", },
  70. { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
  71. { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
  72. { IH_ARCH_AVR32, "avr32", "AVR32", },
  73. { IH_ARCH_NDS32, "nds32", "NDS32", },
  74. { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
  75. { IH_ARCH_SANDBOX, "sandbox", "Sandbox", },
  76. { IH_ARCH_ARM64, "arm64", "AArch64", },
  77. { IH_ARCH_ARC, "arc", "ARC", },
  78. { IH_ARCH_X86_64, "x86_64", "AMD x86_64", },
  79. { -1, "", "", },
  80. };
  81. static const table_entry_t uimage_os[] = {
  82. { IH_OS_INVALID, NULL, "Invalid OS", },
  83. { IH_OS_LINUX, "linux", "Linux", },
  84. #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
  85. { IH_OS_LYNXOS, "lynxos", "LynxOS", },
  86. #endif
  87. { IH_OS_NETBSD, "netbsd", "NetBSD", },
  88. { IH_OS_OSE, "ose", "Enea OSE", },
  89. { IH_OS_PLAN9, "plan9", "Plan 9", },
  90. { IH_OS_RTEMS, "rtems", "RTEMS", },
  91. { IH_OS_U_BOOT, "u-boot", "U-Boot", },
  92. { IH_OS_VXWORKS, "vxworks", "VxWorks", },
  93. #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
  94. { IH_OS_QNX, "qnx", "QNX", },
  95. #endif
  96. #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
  97. { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
  98. #endif
  99. #ifdef USE_HOSTCC
  100. { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
  101. { IH_OS_DELL, "dell", "Dell", },
  102. { IH_OS_ESIX, "esix", "Esix", },
  103. { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
  104. { IH_OS_IRIX, "irix", "Irix", },
  105. { IH_OS_NCR, "ncr", "NCR", },
  106. { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
  107. { IH_OS_PSOS, "psos", "pSOS", },
  108. { IH_OS_SCO, "sco", "SCO", },
  109. { IH_OS_SOLARIS, "solaris", "Solaris", },
  110. { IH_OS_SVR4, "svr4", "SVR4", },
  111. #endif
  112. #if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
  113. { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
  114. #endif
  115. { -1, "", "", },
  116. };
  117. static const table_entry_t uimage_type[] = {
  118. { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
  119. { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
  120. { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
  121. { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
  122. { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
  123. { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
  124. { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
  125. { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
  126. { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
  127. { IH_TYPE_INVALID, NULL, "Invalid Image", },
  128. { IH_TYPE_MULTI, "multi", "Multi-File Image", },
  129. { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
  130. { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
  131. { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
  132. { IH_TYPE_SCRIPT, "script", "Script", },
  133. { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SOCFPGA preloader",},
  134. { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
  135. { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
  136. { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
  137. { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
  138. { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
  139. { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
  140. { -1, "", "", },
  141. };
  142. static const table_entry_t uimage_comp[] = {
  143. { IH_COMP_NONE, "none", "uncompressed", },
  144. { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
  145. { IH_COMP_GZIP, "gzip", "gzip compressed", },
  146. { IH_COMP_LZMA, "lzma", "lzma compressed", },
  147. { IH_COMP_LZO, "lzo", "lzo compressed", },
  148. { -1, "", "", },
  149. };
  150. /*****************************************************************************/
  151. /* Legacy format routines */
  152. /*****************************************************************************/
  153. int image_check_hcrc(const image_header_t *hdr)
  154. {
  155. ulong hcrc;
  156. ulong len = image_get_header_size();
  157. image_header_t header;
  158. /* Copy header so we can blank CRC field for re-calculation */
  159. memmove(&header, (char *)hdr, image_get_header_size());
  160. image_set_hcrc(&header, 0);
  161. hcrc = crc32(0, (unsigned char *)&header, len);
  162. return (hcrc == image_get_hcrc(hdr));
  163. }
  164. int image_check_dcrc(const image_header_t *hdr)
  165. {
  166. ulong data = image_get_data(hdr);
  167. ulong len = image_get_data_size(hdr);
  168. ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
  169. return (dcrc == image_get_dcrc(hdr));
  170. }
  171. /**
  172. * image_multi_count - get component (sub-image) count
  173. * @hdr: pointer to the header of the multi component image
  174. *
  175. * image_multi_count() returns number of components in a multi
  176. * component image.
  177. *
  178. * Note: no checking of the image type is done, caller must pass
  179. * a valid multi component image.
  180. *
  181. * returns:
  182. * number of components
  183. */
  184. ulong image_multi_count(const image_header_t *hdr)
  185. {
  186. ulong i, count = 0;
  187. uint32_t *size;
  188. /* get start of the image payload, which in case of multi
  189. * component images that points to a table of component sizes */
  190. size = (uint32_t *)image_get_data(hdr);
  191. /* count non empty slots */
  192. for (i = 0; size[i]; ++i)
  193. count++;
  194. return count;
  195. }
  196. /**
  197. * image_multi_getimg - get component data address and size
  198. * @hdr: pointer to the header of the multi component image
  199. * @idx: index of the requested component
  200. * @data: pointer to a ulong variable, will hold component data address
  201. * @len: pointer to a ulong variable, will hold component size
  202. *
  203. * image_multi_getimg() returns size and data address for the requested
  204. * component in a multi component image.
  205. *
  206. * Note: no checking of the image type is done, caller must pass
  207. * a valid multi component image.
  208. *
  209. * returns:
  210. * data address and size of the component, if idx is valid
  211. * 0 in data and len, if idx is out of range
  212. */
  213. void image_multi_getimg(const image_header_t *hdr, ulong idx,
  214. ulong *data, ulong *len)
  215. {
  216. int i;
  217. uint32_t *size;
  218. ulong offset, count, img_data;
  219. /* get number of component */
  220. count = image_multi_count(hdr);
  221. /* get start of the image payload, which in case of multi
  222. * component images that points to a table of component sizes */
  223. size = (uint32_t *)image_get_data(hdr);
  224. /* get address of the proper component data start, which means
  225. * skipping sizes table (add 1 for last, null entry) */
  226. img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
  227. if (idx < count) {
  228. *len = uimage_to_cpu(size[idx]);
  229. offset = 0;
  230. /* go over all indices preceding requested component idx */
  231. for (i = 0; i < idx; i++) {
  232. /* add up i-th component size, rounding up to 4 bytes */
  233. offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
  234. }
  235. /* calculate idx-th component data address */
  236. *data = img_data + offset;
  237. } else {
  238. *len = 0;
  239. *data = 0;
  240. }
  241. }
  242. static void image_print_type(const image_header_t *hdr)
  243. {
  244. const char __maybe_unused *os, *arch, *type, *comp;
  245. os = genimg_get_os_name(image_get_os(hdr));
  246. arch = genimg_get_arch_name(image_get_arch(hdr));
  247. type = genimg_get_type_name(image_get_type(hdr));
  248. comp = genimg_get_comp_name(image_get_comp(hdr));
  249. printf("%s %s %s (%s)\n", arch, os, type, comp);
  250. }
  251. /**
  252. * image_print_contents - prints out the contents of the legacy format image
  253. * @ptr: pointer to the legacy format image header
  254. * @p: pointer to prefix string
  255. *
  256. * image_print_contents() formats a multi line legacy image contents description.
  257. * The routine prints out all header fields followed by the size/offset data
  258. * for MULTI/SCRIPT images.
  259. *
  260. * returns:
  261. * no returned results
  262. */
  263. void image_print_contents(const void *ptr)
  264. {
  265. const image_header_t *hdr = (const image_header_t *)ptr;
  266. const char __maybe_unused *p;
  267. p = IMAGE_INDENT_STRING;
  268. printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
  269. if (IMAGE_ENABLE_TIMESTAMP) {
  270. printf("%sCreated: ", p);
  271. genimg_print_time((time_t)image_get_time(hdr));
  272. }
  273. printf("%sImage Type: ", p);
  274. image_print_type(hdr);
  275. printf("%sData Size: ", p);
  276. genimg_print_size(image_get_data_size(hdr));
  277. printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
  278. printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
  279. if (image_check_type(hdr, IH_TYPE_MULTI) ||
  280. image_check_type(hdr, IH_TYPE_SCRIPT)) {
  281. int i;
  282. ulong data, len;
  283. ulong count = image_multi_count(hdr);
  284. printf("%sContents:\n", p);
  285. for (i = 0; i < count; i++) {
  286. image_multi_getimg(hdr, i, &data, &len);
  287. printf("%s Image %d: ", p, i);
  288. genimg_print_size(len);
  289. if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
  290. /*
  291. * the user may need to know offsets
  292. * if planning to do something with
  293. * multiple files
  294. */
  295. printf("%s Offset = 0x%08lx\n", p, data);
  296. }
  297. }
  298. }
  299. }
  300. #ifndef USE_HOSTCC
  301. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  302. /**
  303. * image_get_ramdisk - get and verify ramdisk image
  304. * @rd_addr: ramdisk image start address
  305. * @arch: expected ramdisk architecture
  306. * @verify: checksum verification flag
  307. *
  308. * image_get_ramdisk() returns a pointer to the verified ramdisk image
  309. * header. Routine receives image start address and expected architecture
  310. * flag. Verification done covers data and header integrity and os/type/arch
  311. * fields checking.
  312. *
  313. * If dataflash support is enabled routine checks for dataflash addresses
  314. * and handles required dataflash reads.
  315. *
  316. * returns:
  317. * pointer to a ramdisk image header, if image was found and valid
  318. * otherwise, return NULL
  319. */
  320. static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
  321. int verify)
  322. {
  323. const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
  324. if (!image_check_magic(rd_hdr)) {
  325. puts("Bad Magic Number\n");
  326. bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
  327. return NULL;
  328. }
  329. if (!image_check_hcrc(rd_hdr)) {
  330. puts("Bad Header Checksum\n");
  331. bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
  332. return NULL;
  333. }
  334. bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
  335. image_print_contents(rd_hdr);
  336. if (verify) {
  337. puts(" Verifying Checksum ... ");
  338. if (!image_check_dcrc(rd_hdr)) {
  339. puts("Bad Data CRC\n");
  340. bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
  341. return NULL;
  342. }
  343. puts("OK\n");
  344. }
  345. bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
  346. if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
  347. !image_check_arch(rd_hdr, arch) ||
  348. !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
  349. printf("No Linux %s Ramdisk Image\n",
  350. genimg_get_arch_name(arch));
  351. bootstage_error(BOOTSTAGE_ID_RAMDISK);
  352. return NULL;
  353. }
  354. return rd_hdr;
  355. }
  356. #endif
  357. #endif /* !USE_HOSTCC */
  358. /*****************************************************************************/
  359. /* Shared dual-format routines */
  360. /*****************************************************************************/
  361. #ifndef USE_HOSTCC
  362. ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
  363. ulong save_addr; /* Default Save Address */
  364. ulong save_size; /* Default Save Size (in bytes) */
  365. static int on_loadaddr(const char *name, const char *value, enum env_op op,
  366. int flags)
  367. {
  368. switch (op) {
  369. case env_op_create:
  370. case env_op_overwrite:
  371. load_addr = simple_strtoul(value, NULL, 16);
  372. break;
  373. default:
  374. break;
  375. }
  376. return 0;
  377. }
  378. U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
  379. ulong getenv_bootm_low(void)
  380. {
  381. char *s = getenv("bootm_low");
  382. if (s) {
  383. ulong tmp = simple_strtoul(s, NULL, 16);
  384. return tmp;
  385. }
  386. #if defined(CONFIG_SYS_SDRAM_BASE)
  387. return CONFIG_SYS_SDRAM_BASE;
  388. #elif defined(CONFIG_ARM)
  389. return gd->bd->bi_dram[0].start;
  390. #else
  391. return 0;
  392. #endif
  393. }
  394. phys_size_t getenv_bootm_size(void)
  395. {
  396. phys_size_t tmp;
  397. char *s = getenv("bootm_size");
  398. if (s) {
  399. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  400. return tmp;
  401. }
  402. s = getenv("bootm_low");
  403. if (s)
  404. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  405. else
  406. tmp = 0;
  407. #if defined(CONFIG_ARM) && defined(CONFIG_NR_DRAM_BANKS)
  408. return gd->bd->bi_dram[0].size - tmp;
  409. #else
  410. return gd->bd->bi_memsize - tmp;
  411. #endif
  412. }
  413. phys_size_t getenv_bootm_mapsize(void)
  414. {
  415. phys_size_t tmp;
  416. char *s = getenv("bootm_mapsize");
  417. if (s) {
  418. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  419. return tmp;
  420. }
  421. #if defined(CONFIG_SYS_BOOTMAPSZ)
  422. return CONFIG_SYS_BOOTMAPSZ;
  423. #else
  424. return getenv_bootm_size();
  425. #endif
  426. }
  427. void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  428. {
  429. if (to == from)
  430. return;
  431. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  432. if (to > from) {
  433. from += len;
  434. to += len;
  435. }
  436. while (len > 0) {
  437. size_t tail = (len > chunksz) ? chunksz : len;
  438. WATCHDOG_RESET();
  439. if (to > from) {
  440. to -= tail;
  441. from -= tail;
  442. }
  443. memmove(to, from, tail);
  444. if (to < from) {
  445. to += tail;
  446. from += tail;
  447. }
  448. len -= tail;
  449. }
  450. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  451. memmove(to, from, len);
  452. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  453. }
  454. #endif /* !USE_HOSTCC */
  455. void genimg_print_size(uint32_t size)
  456. {
  457. #ifndef USE_HOSTCC
  458. printf("%d Bytes = ", size);
  459. print_size(size, "\n");
  460. #else
  461. printf("%d Bytes = %.2f kB = %.2f MB\n",
  462. size, (double)size / 1.024e3,
  463. (double)size / 1.048576e6);
  464. #endif
  465. }
  466. #if IMAGE_ENABLE_TIMESTAMP
  467. void genimg_print_time(time_t timestamp)
  468. {
  469. #ifndef USE_HOSTCC
  470. struct rtc_time tm;
  471. rtc_to_tm(timestamp, &tm);
  472. printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
  473. tm.tm_year, tm.tm_mon, tm.tm_mday,
  474. tm.tm_hour, tm.tm_min, tm.tm_sec);
  475. #else
  476. printf("%s", ctime(&timestamp));
  477. #endif
  478. }
  479. #endif
  480. const table_entry_t *get_table_entry(const table_entry_t *table, int id)
  481. {
  482. for (; table->id >= 0; ++table) {
  483. if (table->id == id)
  484. return table;
  485. }
  486. return NULL;
  487. }
  488. /**
  489. * get_table_entry_name - translate entry id to long name
  490. * @table: pointer to a translation table for entries of a specific type
  491. * @msg: message to be returned when translation fails
  492. * @id: entry id to be translated
  493. *
  494. * get_table_entry_name() will go over translation table trying to find
  495. * entry that matches given id. If matching entry is found, its long
  496. * name is returned to the caller.
  497. *
  498. * returns:
  499. * long entry name if translation succeeds
  500. * msg otherwise
  501. */
  502. char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
  503. {
  504. table = get_table_entry(table, id);
  505. if (!table)
  506. return msg;
  507. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  508. return table->lname;
  509. #else
  510. return table->lname + gd->reloc_off;
  511. #endif
  512. }
  513. const char *genimg_get_os_name(uint8_t os)
  514. {
  515. return (get_table_entry_name(uimage_os, "Unknown OS", os));
  516. }
  517. const char *genimg_get_arch_name(uint8_t arch)
  518. {
  519. return (get_table_entry_name(uimage_arch, "Unknown Architecture",
  520. arch));
  521. }
  522. const char *genimg_get_type_name(uint8_t type)
  523. {
  524. return (get_table_entry_name(uimage_type, "Unknown Image", type));
  525. }
  526. const char *genimg_get_type_short_name(uint8_t type)
  527. {
  528. const table_entry_t *table;
  529. table = get_table_entry(uimage_type, type);
  530. if (!table)
  531. return "unknown";
  532. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  533. return table->sname;
  534. #else
  535. return table->sname + gd->reloc_off;
  536. #endif
  537. }
  538. const char *genimg_get_comp_name(uint8_t comp)
  539. {
  540. return (get_table_entry_name(uimage_comp, "Unknown Compression",
  541. comp));
  542. }
  543. /**
  544. * get_table_entry_id - translate short entry name to id
  545. * @table: pointer to a translation table for entries of a specific type
  546. * @table_name: to be used in case of error
  547. * @name: entry short name to be translated
  548. *
  549. * get_table_entry_id() will go over translation table trying to find
  550. * entry that matches given short name. If matching entry is found,
  551. * its id returned to the caller.
  552. *
  553. * returns:
  554. * entry id if translation succeeds
  555. * -1 otherwise
  556. */
  557. int get_table_entry_id(const table_entry_t *table,
  558. const char *table_name, const char *name)
  559. {
  560. const table_entry_t *t;
  561. for (t = table; t->id >= 0; ++t) {
  562. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  563. if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
  564. #else
  565. if (t->sname && strcasecmp(t->sname, name) == 0)
  566. #endif
  567. return (t->id);
  568. }
  569. debug("Invalid %s Type: %s\n", table_name, name);
  570. return -1;
  571. }
  572. int genimg_get_os_id(const char *name)
  573. {
  574. return (get_table_entry_id(uimage_os, "OS", name));
  575. }
  576. int genimg_get_arch_id(const char *name)
  577. {
  578. return (get_table_entry_id(uimage_arch, "CPU", name));
  579. }
  580. int genimg_get_type_id(const char *name)
  581. {
  582. return (get_table_entry_id(uimage_type, "Image", name));
  583. }
  584. int genimg_get_comp_id(const char *name)
  585. {
  586. return (get_table_entry_id(uimage_comp, "Compression", name));
  587. }
  588. #ifndef USE_HOSTCC
  589. /**
  590. * genimg_get_kernel_addr_fit - get the real kernel address and return 2
  591. * FIT strings
  592. * @img_addr: a string might contain real image address
  593. * @fit_uname_config: double pointer to a char, will hold pointer to a
  594. * configuration unit name
  595. * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
  596. * name
  597. *
  598. * genimg_get_kernel_addr_fit get the real kernel start address from a string
  599. * which is normally the first argv of bootm/bootz
  600. *
  601. * returns:
  602. * kernel start address
  603. */
  604. ulong genimg_get_kernel_addr_fit(char * const img_addr,
  605. const char **fit_uname_config,
  606. const char **fit_uname_kernel)
  607. {
  608. ulong kernel_addr;
  609. /* find out kernel image address */
  610. if (!img_addr) {
  611. kernel_addr = load_addr;
  612. debug("* kernel: default image load address = 0x%08lx\n",
  613. load_addr);
  614. #if defined(CONFIG_FIT)
  615. } else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
  616. fit_uname_config)) {
  617. debug("* kernel: config '%s' from image at 0x%08lx\n",
  618. *fit_uname_config, kernel_addr);
  619. } else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
  620. fit_uname_kernel)) {
  621. debug("* kernel: subimage '%s' from image at 0x%08lx\n",
  622. *fit_uname_kernel, kernel_addr);
  623. #endif
  624. } else {
  625. kernel_addr = simple_strtoul(img_addr, NULL, 16);
  626. debug("* kernel: cmdline image address = 0x%08lx\n",
  627. kernel_addr);
  628. }
  629. return kernel_addr;
  630. }
  631. /**
  632. * genimg_get_kernel_addr() is the simple version of
  633. * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
  634. */
  635. ulong genimg_get_kernel_addr(char * const img_addr)
  636. {
  637. const char *fit_uname_config = NULL;
  638. const char *fit_uname_kernel = NULL;
  639. return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
  640. &fit_uname_kernel);
  641. }
  642. /**
  643. * genimg_get_format - get image format type
  644. * @img_addr: image start address
  645. *
  646. * genimg_get_format() checks whether provided address points to a valid
  647. * legacy or FIT image.
  648. *
  649. * New uImage format and FDT blob are based on a libfdt. FDT blob
  650. * may be passed directly or embedded in a FIT image. In both situations
  651. * genimg_get_format() must be able to dectect libfdt header.
  652. *
  653. * returns:
  654. * image format type or IMAGE_FORMAT_INVALID if no image is present
  655. */
  656. int genimg_get_format(const void *img_addr)
  657. {
  658. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  659. const image_header_t *hdr;
  660. hdr = (const image_header_t *)img_addr;
  661. if (image_check_magic(hdr))
  662. return IMAGE_FORMAT_LEGACY;
  663. #endif
  664. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  665. if (fdt_check_header(img_addr) == 0)
  666. return IMAGE_FORMAT_FIT;
  667. #endif
  668. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  669. if (android_image_check_header(img_addr) == 0)
  670. return IMAGE_FORMAT_ANDROID;
  671. #endif
  672. return IMAGE_FORMAT_INVALID;
  673. }
  674. /**
  675. * genimg_get_image - get image from special storage (if necessary)
  676. * @img_addr: image start address
  677. *
  678. * genimg_get_image() checks if provided image start address is located
  679. * in a dataflash storage. If so, image is moved to a system RAM memory.
  680. *
  681. * returns:
  682. * image start address after possible relocation from special storage
  683. */
  684. ulong genimg_get_image(ulong img_addr)
  685. {
  686. ulong ram_addr = img_addr;
  687. #ifdef CONFIG_HAS_DATAFLASH
  688. ulong h_size, d_size;
  689. if (addr_dataflash(img_addr)) {
  690. void *buf;
  691. /* ger RAM address */
  692. ram_addr = CONFIG_SYS_LOAD_ADDR;
  693. /* get header size */
  694. h_size = image_get_header_size();
  695. #if defined(CONFIG_FIT)
  696. if (sizeof(struct fdt_header) > h_size)
  697. h_size = sizeof(struct fdt_header);
  698. #endif
  699. /* read in header */
  700. debug(" Reading image header from dataflash address "
  701. "%08lx to RAM address %08lx\n", img_addr, ram_addr);
  702. buf = map_sysmem(ram_addr, 0);
  703. read_dataflash(img_addr, h_size, buf);
  704. /* get data size */
  705. switch (genimg_get_format(buf)) {
  706. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  707. case IMAGE_FORMAT_LEGACY:
  708. d_size = image_get_data_size(buf);
  709. debug(" Legacy format image found at 0x%08lx, "
  710. "size 0x%08lx\n",
  711. ram_addr, d_size);
  712. break;
  713. #endif
  714. #if defined(CONFIG_FIT)
  715. case IMAGE_FORMAT_FIT:
  716. d_size = fit_get_size(buf) - h_size;
  717. debug(" FIT/FDT format image found at 0x%08lx, "
  718. "size 0x%08lx\n",
  719. ram_addr, d_size);
  720. break;
  721. #endif
  722. default:
  723. printf(" No valid image found at 0x%08lx\n",
  724. img_addr);
  725. return ram_addr;
  726. }
  727. /* read in image data */
  728. debug(" Reading image remaining data from dataflash address "
  729. "%08lx to RAM address %08lx\n", img_addr + h_size,
  730. ram_addr + h_size);
  731. read_dataflash(img_addr + h_size, d_size,
  732. (char *)(buf + h_size));
  733. }
  734. #endif /* CONFIG_HAS_DATAFLASH */
  735. return ram_addr;
  736. }
  737. /**
  738. * fit_has_config - check if there is a valid FIT configuration
  739. * @images: pointer to the bootm command headers structure
  740. *
  741. * fit_has_config() checks if there is a FIT configuration in use
  742. * (if FTI support is present).
  743. *
  744. * returns:
  745. * 0, no FIT support or no configuration found
  746. * 1, configuration found
  747. */
  748. int genimg_has_config(bootm_headers_t *images)
  749. {
  750. #if defined(CONFIG_FIT)
  751. if (images->fit_uname_cfg)
  752. return 1;
  753. #endif
  754. return 0;
  755. }
  756. /**
  757. * boot_get_ramdisk - main ramdisk handling routine
  758. * @argc: command argument count
  759. * @argv: command argument list
  760. * @images: pointer to the bootm images structure
  761. * @arch: expected ramdisk architecture
  762. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  763. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  764. *
  765. * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
  766. * Curently supported are the following ramdisk sources:
  767. * - multicomponent kernel/ramdisk image,
  768. * - commandline provided address of decicated ramdisk image.
  769. *
  770. * returns:
  771. * 0, if ramdisk image was found and valid, or skiped
  772. * rd_start and rd_end are set to ramdisk start/end addresses if
  773. * ramdisk image is found and valid
  774. *
  775. * 1, if ramdisk image is found but corrupted, or invalid
  776. * rd_start and rd_end are set to 0 if no ramdisk exists
  777. */
  778. int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
  779. uint8_t arch, ulong *rd_start, ulong *rd_end)
  780. {
  781. ulong rd_addr, rd_load;
  782. ulong rd_data, rd_len;
  783. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  784. const image_header_t *rd_hdr;
  785. #endif
  786. void *buf;
  787. #ifdef CONFIG_SUPPORT_RAW_INITRD
  788. char *end;
  789. #endif
  790. #if defined(CONFIG_FIT)
  791. const char *fit_uname_config = images->fit_uname_cfg;
  792. const char *fit_uname_ramdisk = NULL;
  793. ulong default_addr;
  794. int rd_noffset;
  795. #endif
  796. const char *select = NULL;
  797. *rd_start = 0;
  798. *rd_end = 0;
  799. if (argc >= 2)
  800. select = argv[1];
  801. /*
  802. * Look for a '-' which indicates to ignore the
  803. * ramdisk argument
  804. */
  805. if (select && strcmp(select, "-") == 0) {
  806. debug("## Skipping init Ramdisk\n");
  807. rd_len = rd_data = 0;
  808. } else if (select || genimg_has_config(images)) {
  809. #if defined(CONFIG_FIT)
  810. if (select) {
  811. /*
  812. * If the init ramdisk comes from the FIT image and
  813. * the FIT image address is omitted in the command
  814. * line argument, try to use os FIT image address or
  815. * default load address.
  816. */
  817. if (images->fit_uname_os)
  818. default_addr = (ulong)images->fit_hdr_os;
  819. else
  820. default_addr = load_addr;
  821. if (fit_parse_conf(select, default_addr,
  822. &rd_addr, &fit_uname_config)) {
  823. debug("* ramdisk: config '%s' from image at "
  824. "0x%08lx\n",
  825. fit_uname_config, rd_addr);
  826. } else if (fit_parse_subimage(select, default_addr,
  827. &rd_addr, &fit_uname_ramdisk)) {
  828. debug("* ramdisk: subimage '%s' from image at "
  829. "0x%08lx\n",
  830. fit_uname_ramdisk, rd_addr);
  831. } else
  832. #endif
  833. {
  834. rd_addr = simple_strtoul(select, NULL, 16);
  835. debug("* ramdisk: cmdline image address = "
  836. "0x%08lx\n",
  837. rd_addr);
  838. }
  839. #if defined(CONFIG_FIT)
  840. } else {
  841. /* use FIT configuration provided in first bootm
  842. * command argument. If the property is not defined,
  843. * quit silently.
  844. */
  845. rd_addr = map_to_sysmem(images->fit_hdr_os);
  846. rd_noffset = fit_get_node_from_config(images,
  847. FIT_RAMDISK_PROP, rd_addr);
  848. if (rd_noffset == -ENOLINK)
  849. return 0;
  850. else if (rd_noffset < 0)
  851. return 1;
  852. }
  853. #endif
  854. /* copy from dataflash if needed */
  855. rd_addr = genimg_get_image(rd_addr);
  856. /*
  857. * Check if there is an initrd image at the
  858. * address provided in the second bootm argument
  859. * check image type, for FIT images get FIT node.
  860. */
  861. buf = map_sysmem(rd_addr, 0);
  862. switch (genimg_get_format(buf)) {
  863. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  864. case IMAGE_FORMAT_LEGACY:
  865. printf("## Loading init Ramdisk from Legacy "
  866. "Image at %08lx ...\n", rd_addr);
  867. bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
  868. rd_hdr = image_get_ramdisk(rd_addr, arch,
  869. images->verify);
  870. if (rd_hdr == NULL)
  871. return 1;
  872. rd_data = image_get_data(rd_hdr);
  873. rd_len = image_get_data_size(rd_hdr);
  874. rd_load = image_get_load(rd_hdr);
  875. break;
  876. #endif
  877. #if defined(CONFIG_FIT)
  878. case IMAGE_FORMAT_FIT:
  879. rd_noffset = fit_image_load(images,
  880. rd_addr, &fit_uname_ramdisk,
  881. &fit_uname_config, arch,
  882. IH_TYPE_RAMDISK,
  883. BOOTSTAGE_ID_FIT_RD_START,
  884. FIT_LOAD_OPTIONAL_NON_ZERO,
  885. &rd_data, &rd_len);
  886. if (rd_noffset < 0)
  887. return 1;
  888. images->fit_hdr_rd = map_sysmem(rd_addr, 0);
  889. images->fit_uname_rd = fit_uname_ramdisk;
  890. images->fit_noffset_rd = rd_noffset;
  891. break;
  892. #endif
  893. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  894. case IMAGE_FORMAT_ANDROID:
  895. android_image_get_ramdisk((void *)images->os.start,
  896. &rd_data, &rd_len);
  897. break;
  898. #endif
  899. default:
  900. #ifdef CONFIG_SUPPORT_RAW_INITRD
  901. end = NULL;
  902. if (select)
  903. end = strchr(select, ':');
  904. if (end) {
  905. rd_len = simple_strtoul(++end, NULL, 16);
  906. rd_data = rd_addr;
  907. } else
  908. #endif
  909. {
  910. puts("Wrong Ramdisk Image Format\n");
  911. rd_data = rd_len = rd_load = 0;
  912. return 1;
  913. }
  914. }
  915. } else if (images->legacy_hdr_valid &&
  916. image_check_type(&images->legacy_hdr_os_copy,
  917. IH_TYPE_MULTI)) {
  918. /*
  919. * Now check if we have a legacy mult-component image,
  920. * get second entry data start address and len.
  921. */
  922. bootstage_mark(BOOTSTAGE_ID_RAMDISK);
  923. printf("## Loading init Ramdisk from multi component "
  924. "Legacy Image at %08lx ...\n",
  925. (ulong)images->legacy_hdr_os);
  926. image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
  927. } else {
  928. /*
  929. * no initrd image
  930. */
  931. bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
  932. rd_len = rd_data = 0;
  933. }
  934. if (!rd_data) {
  935. debug("## No init Ramdisk\n");
  936. } else {
  937. *rd_start = rd_data;
  938. *rd_end = rd_data + rd_len;
  939. }
  940. debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  941. *rd_start, *rd_end);
  942. return 0;
  943. }
  944. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  945. /**
  946. * boot_ramdisk_high - relocate init ramdisk
  947. * @lmb: pointer to lmb handle, will be used for memory mgmt
  948. * @rd_data: ramdisk data start address
  949. * @rd_len: ramdisk data length
  950. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  951. * start address (after possible relocation)
  952. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  953. * end address (after possible relocation)
  954. *
  955. * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
  956. * variable and if requested ramdisk data is moved to a specified location.
  957. *
  958. * Initrd_start and initrd_end are set to final (after relocation) ramdisk
  959. * start/end addresses if ramdisk image start and len were provided,
  960. * otherwise set initrd_start and initrd_end set to zeros.
  961. *
  962. * returns:
  963. * 0 - success
  964. * -1 - failure
  965. */
  966. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  967. ulong *initrd_start, ulong *initrd_end)
  968. {
  969. char *s;
  970. ulong initrd_high;
  971. int initrd_copy_to_ram = 1;
  972. if ((s = getenv("initrd_high")) != NULL) {
  973. /* a value of "no" or a similar string will act like 0,
  974. * turning the "load high" feature off. This is intentional.
  975. */
  976. initrd_high = simple_strtoul(s, NULL, 16);
  977. if (initrd_high == ~0)
  978. initrd_copy_to_ram = 0;
  979. } else {
  980. /* not set, no restrictions to load high */
  981. initrd_high = ~0;
  982. }
  983. #ifdef CONFIG_LOGBUFFER
  984. /* Prevent initrd from overwriting logbuffer */
  985. lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
  986. #endif
  987. debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  988. initrd_high, initrd_copy_to_ram);
  989. if (rd_data) {
  990. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  991. debug(" in-place initrd\n");
  992. *initrd_start = rd_data;
  993. *initrd_end = rd_data + rd_len;
  994. lmb_reserve(lmb, rd_data, rd_len);
  995. } else {
  996. if (initrd_high)
  997. *initrd_start = (ulong)lmb_alloc_base(lmb,
  998. rd_len, 0x1000, initrd_high);
  999. else
  1000. *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
  1001. 0x1000);
  1002. if (*initrd_start == 0) {
  1003. puts("ramdisk - allocation error\n");
  1004. goto error;
  1005. }
  1006. bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
  1007. *initrd_end = *initrd_start + rd_len;
  1008. printf(" Loading Ramdisk to %08lx, end %08lx ... ",
  1009. *initrd_start, *initrd_end);
  1010. memmove_wd((void *)*initrd_start,
  1011. (void *)rd_data, rd_len, CHUNKSZ);
  1012. #ifdef CONFIG_MP
  1013. /*
  1014. * Ensure the image is flushed to memory to handle
  1015. * AMP boot scenarios in which we might not be
  1016. * HW cache coherent
  1017. */
  1018. flush_cache((unsigned long)*initrd_start, rd_len);
  1019. #endif
  1020. puts("OK\n");
  1021. }
  1022. } else {
  1023. *initrd_start = 0;
  1024. *initrd_end = 0;
  1025. }
  1026. debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  1027. *initrd_start, *initrd_end);
  1028. return 0;
  1029. error:
  1030. return -1;
  1031. }
  1032. #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
  1033. int boot_get_setup(bootm_headers_t *images, uint8_t arch,
  1034. ulong *setup_start, ulong *setup_len)
  1035. {
  1036. #if defined(CONFIG_FIT)
  1037. return boot_get_setup_fit(images, arch, setup_start, setup_len);
  1038. #else
  1039. return -ENOENT;
  1040. #endif
  1041. }
  1042. #if defined(CONFIG_FIT)
  1043. int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
  1044. uint8_t arch, const ulong *ld_start, ulong * const ld_len)
  1045. {
  1046. /*
  1047. * These variables are used to hold the current image location
  1048. * in system memory.
  1049. */
  1050. ulong tmp_img_addr;
  1051. /*
  1052. * These two variables are requirements for fit_image_load, but
  1053. * their values are not used
  1054. */
  1055. ulong img_data, img_len;
  1056. void *buf;
  1057. int loadables_index;
  1058. int conf_noffset;
  1059. int fit_img_result;
  1060. char *uname;
  1061. /* Check to see if the images struct has a FIT configuration */
  1062. if (!genimg_has_config(images)) {
  1063. debug("## FIT configuration was not specified\n");
  1064. return 0;
  1065. }
  1066. /*
  1067. * Obtain the os FIT header from the images struct
  1068. * copy from dataflash if needed
  1069. */
  1070. tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
  1071. tmp_img_addr = genimg_get_image(tmp_img_addr);
  1072. buf = map_sysmem(tmp_img_addr, 0);
  1073. /*
  1074. * Check image type. For FIT images get FIT node
  1075. * and attempt to locate a generic binary.
  1076. */
  1077. switch (genimg_get_format(buf)) {
  1078. case IMAGE_FORMAT_FIT:
  1079. conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
  1080. for (loadables_index = 0;
  1081. fdt_get_string_index(buf, conf_noffset,
  1082. FIT_LOADABLE_PROP,
  1083. loadables_index,
  1084. (const char **)&uname) == 0;
  1085. loadables_index++)
  1086. {
  1087. fit_img_result = fit_image_load(images,
  1088. tmp_img_addr,
  1089. (const char **)&uname,
  1090. &(images->fit_uname_cfg), arch,
  1091. IH_TYPE_LOADABLE,
  1092. BOOTSTAGE_ID_FIT_LOADABLE_START,
  1093. FIT_LOAD_OPTIONAL_NON_ZERO,
  1094. &img_data, &img_len);
  1095. if (fit_img_result < 0) {
  1096. /* Something went wrong! */
  1097. return fit_img_result;
  1098. }
  1099. }
  1100. break;
  1101. default:
  1102. printf("The given image format is not supported (corrupt?)\n");
  1103. return 1;
  1104. }
  1105. return 0;
  1106. }
  1107. #endif
  1108. #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
  1109. /**
  1110. * boot_get_cmdline - allocate and initialize kernel cmdline
  1111. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1112. * @cmd_start: pointer to a ulong variable, will hold cmdline start
  1113. * @cmd_end: pointer to a ulong variable, will hold cmdline end
  1114. *
  1115. * boot_get_cmdline() allocates space for kernel command line below
  1116. * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
  1117. * variable is present its contents is copied to allocated kernel
  1118. * command line.
  1119. *
  1120. * returns:
  1121. * 0 - success
  1122. * -1 - failure
  1123. */
  1124. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
  1125. {
  1126. char *cmdline;
  1127. char *s;
  1128. cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
  1129. getenv_bootm_mapsize() + getenv_bootm_low());
  1130. if (cmdline == NULL)
  1131. return -1;
  1132. if ((s = getenv("bootargs")) == NULL)
  1133. s = "";
  1134. strcpy(cmdline, s);
  1135. *cmd_start = (ulong) & cmdline[0];
  1136. *cmd_end = *cmd_start + strlen(cmdline);
  1137. debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  1138. return 0;
  1139. }
  1140. #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
  1141. #ifdef CONFIG_SYS_BOOT_GET_KBD
  1142. /**
  1143. * boot_get_kbd - allocate and initialize kernel copy of board info
  1144. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1145. * @kbd: double pointer to board info data
  1146. *
  1147. * boot_get_kbd() allocates space for kernel copy of board info data below
  1148. * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
  1149. * with the current u-boot board info data.
  1150. *
  1151. * returns:
  1152. * 0 - success
  1153. * -1 - failure
  1154. */
  1155. int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
  1156. {
  1157. *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
  1158. getenv_bootm_mapsize() + getenv_bootm_low());
  1159. if (*kbd == NULL)
  1160. return -1;
  1161. **kbd = *(gd->bd);
  1162. debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  1163. #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  1164. do_bdinfo(NULL, 0, 0, NULL);
  1165. #endif
  1166. return 0;
  1167. }
  1168. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  1169. #ifdef CONFIG_LMB
  1170. int image_setup_linux(bootm_headers_t *images)
  1171. {
  1172. ulong of_size = images->ft_len;
  1173. char **of_flat_tree = &images->ft_addr;
  1174. ulong *initrd_start = &images->initrd_start;
  1175. ulong *initrd_end = &images->initrd_end;
  1176. struct lmb *lmb = &images->lmb;
  1177. ulong rd_len;
  1178. int ret;
  1179. if (IMAGE_ENABLE_OF_LIBFDT)
  1180. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  1181. if (IMAGE_BOOT_GET_CMDLINE) {
  1182. ret = boot_get_cmdline(lmb, &images->cmdline_start,
  1183. &images->cmdline_end);
  1184. if (ret) {
  1185. puts("ERROR with allocation of cmdline\n");
  1186. return ret;
  1187. }
  1188. }
  1189. if (IMAGE_ENABLE_RAMDISK_HIGH) {
  1190. rd_len = images->rd_end - images->rd_start;
  1191. ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
  1192. initrd_start, initrd_end);
  1193. if (ret)
  1194. return ret;
  1195. }
  1196. if (IMAGE_ENABLE_OF_LIBFDT) {
  1197. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  1198. if (ret)
  1199. return ret;
  1200. }
  1201. if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
  1202. ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
  1203. if (ret)
  1204. return ret;
  1205. }
  1206. return 0;
  1207. }
  1208. #endif /* CONFIG_LMB */
  1209. #endif /* !USE_HOSTCC */