image.c 43 KB

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