image.c 36 KB

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