image.c 36 KB

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