image.c 45 KB

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