image.c 44 KB

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