image.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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. start = gd->ram_base;
  602. size = gd->ram_size;
  603. if (start + size > gd->ram_top)
  604. size = gd->ram_top - start;
  605. s = env_get("bootm_low");
  606. if (s)
  607. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  608. else
  609. tmp = start;
  610. return size - (tmp - start);
  611. }
  612. phys_size_t env_get_bootm_mapsize(void)
  613. {
  614. phys_size_t tmp;
  615. char *s = env_get("bootm_mapsize");
  616. if (s) {
  617. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  618. return tmp;
  619. }
  620. #if defined(CONFIG_SYS_BOOTMAPSZ)
  621. return CONFIG_SYS_BOOTMAPSZ;
  622. #else
  623. return env_get_bootm_size();
  624. #endif
  625. }
  626. void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  627. {
  628. if (to == from)
  629. return;
  630. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  631. if (to > from) {
  632. from += len;
  633. to += len;
  634. }
  635. while (len > 0) {
  636. size_t tail = (len > chunksz) ? chunksz : len;
  637. WATCHDOG_RESET();
  638. if (to > from) {
  639. to -= tail;
  640. from -= tail;
  641. }
  642. memmove(to, from, tail);
  643. if (to < from) {
  644. to += tail;
  645. from += tail;
  646. }
  647. len -= tail;
  648. }
  649. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  650. memmove(to, from, len);
  651. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  652. }
  653. #else /* USE_HOSTCC */
  654. void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  655. {
  656. memmove(to, from, len);
  657. }
  658. #endif /* !USE_HOSTCC */
  659. void genimg_print_size(uint32_t size)
  660. {
  661. #ifndef USE_HOSTCC
  662. printf("%d Bytes = ", size);
  663. print_size(size, "\n");
  664. #else
  665. printf("%d Bytes = %.2f KiB = %.2f MiB\n",
  666. size, (double)size / 1.024e3,
  667. (double)size / 1.048576e6);
  668. #endif
  669. }
  670. #if IMAGE_ENABLE_TIMESTAMP
  671. void genimg_print_time(time_t timestamp)
  672. {
  673. #ifndef USE_HOSTCC
  674. struct rtc_time tm;
  675. rtc_to_tm(timestamp, &tm);
  676. printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
  677. tm.tm_year, tm.tm_mon, tm.tm_mday,
  678. tm.tm_hour, tm.tm_min, tm.tm_sec);
  679. #else
  680. printf("%s", ctime(&timestamp));
  681. #endif
  682. }
  683. #endif
  684. const table_entry_t *get_table_entry(const table_entry_t *table, int id)
  685. {
  686. for (; table->id >= 0; ++table) {
  687. if (table->id == id)
  688. return table;
  689. }
  690. return NULL;
  691. }
  692. static const char *unknown_msg(enum ih_category category)
  693. {
  694. static const char unknown_str[] = "Unknown ";
  695. static char msg[30];
  696. strcpy(msg, unknown_str);
  697. strncat(msg, table_info[category].desc,
  698. sizeof(msg) - sizeof(unknown_str));
  699. return msg;
  700. }
  701. /**
  702. * get_cat_table_entry_name - translate entry id to long name
  703. * @category: category to look up (enum ih_category)
  704. * @id: entry id to be translated
  705. *
  706. * This will scan the translation table trying to find the entry that matches
  707. * the given id.
  708. *
  709. * @retur long entry name if translation succeeds; error string on failure
  710. */
  711. const char *genimg_get_cat_name(enum ih_category category, uint id)
  712. {
  713. const table_entry_t *entry;
  714. entry = get_table_entry(table_info[category].table, id);
  715. if (!entry)
  716. return unknown_msg(category);
  717. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  718. return entry->lname;
  719. #else
  720. return entry->lname + gd->reloc_off;
  721. #endif
  722. }
  723. /**
  724. * get_cat_table_entry_short_name - translate entry id to short name
  725. * @category: category to look up (enum ih_category)
  726. * @id: entry id to be translated
  727. *
  728. * This will scan the translation table trying to find the entry that matches
  729. * the given id.
  730. *
  731. * @retur short entry name if translation succeeds; error string on failure
  732. */
  733. const char *genimg_get_cat_short_name(enum ih_category category, uint id)
  734. {
  735. const table_entry_t *entry;
  736. entry = get_table_entry(table_info[category].table, id);
  737. if (!entry)
  738. return unknown_msg(category);
  739. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  740. return entry->sname;
  741. #else
  742. return entry->sname + gd->reloc_off;
  743. #endif
  744. }
  745. int genimg_get_cat_count(enum ih_category category)
  746. {
  747. return table_info[category].count;
  748. }
  749. const char *genimg_get_cat_desc(enum ih_category category)
  750. {
  751. return table_info[category].desc;
  752. }
  753. /**
  754. * get_table_entry_name - translate entry id to long name
  755. * @table: pointer to a translation table for entries of a specific type
  756. * @msg: message to be returned when translation fails
  757. * @id: entry id to be translated
  758. *
  759. * get_table_entry_name() will go over translation table trying to find
  760. * entry that matches given id. If matching entry is found, its long
  761. * name is returned to the caller.
  762. *
  763. * returns:
  764. * long entry name if translation succeeds
  765. * msg otherwise
  766. */
  767. char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
  768. {
  769. table = get_table_entry(table, id);
  770. if (!table)
  771. return msg;
  772. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  773. return table->lname;
  774. #else
  775. return table->lname + gd->reloc_off;
  776. #endif
  777. }
  778. const char *genimg_get_os_name(uint8_t os)
  779. {
  780. return (get_table_entry_name(uimage_os, "Unknown OS", os));
  781. }
  782. const char *genimg_get_arch_name(uint8_t arch)
  783. {
  784. return (get_table_entry_name(uimage_arch, "Unknown Architecture",
  785. arch));
  786. }
  787. const char *genimg_get_type_name(uint8_t type)
  788. {
  789. return (get_table_entry_name(uimage_type, "Unknown Image", type));
  790. }
  791. static const char *genimg_get_short_name(const table_entry_t *table, int val)
  792. {
  793. table = get_table_entry(table, val);
  794. if (!table)
  795. return "unknown";
  796. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  797. return table->sname;
  798. #else
  799. return table->sname + gd->reloc_off;
  800. #endif
  801. }
  802. const char *genimg_get_type_short_name(uint8_t type)
  803. {
  804. return genimg_get_short_name(uimage_type, type);
  805. }
  806. const char *genimg_get_comp_name(uint8_t comp)
  807. {
  808. return (get_table_entry_name(uimage_comp, "Unknown Compression",
  809. comp));
  810. }
  811. const char *genimg_get_comp_short_name(uint8_t comp)
  812. {
  813. return genimg_get_short_name(uimage_comp, comp);
  814. }
  815. const char *genimg_get_os_short_name(uint8_t os)
  816. {
  817. return genimg_get_short_name(uimage_os, os);
  818. }
  819. const char *genimg_get_arch_short_name(uint8_t arch)
  820. {
  821. return genimg_get_short_name(uimage_arch, arch);
  822. }
  823. /**
  824. * get_table_entry_id - translate short entry name to id
  825. * @table: pointer to a translation table for entries of a specific type
  826. * @table_name: to be used in case of error
  827. * @name: entry short name to be translated
  828. *
  829. * get_table_entry_id() will go over translation table trying to find
  830. * entry that matches given short name. If matching entry is found,
  831. * its id returned to the caller.
  832. *
  833. * returns:
  834. * entry id if translation succeeds
  835. * -1 otherwise
  836. */
  837. int get_table_entry_id(const table_entry_t *table,
  838. const char *table_name, const char *name)
  839. {
  840. const table_entry_t *t;
  841. for (t = table; t->id >= 0; ++t) {
  842. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  843. if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
  844. #else
  845. if (t->sname && strcasecmp(t->sname, name) == 0)
  846. #endif
  847. return (t->id);
  848. }
  849. debug("Invalid %s Type: %s\n", table_name, name);
  850. return -1;
  851. }
  852. int genimg_get_os_id(const char *name)
  853. {
  854. return (get_table_entry_id(uimage_os, "OS", name));
  855. }
  856. int genimg_get_arch_id(const char *name)
  857. {
  858. return (get_table_entry_id(uimage_arch, "CPU", name));
  859. }
  860. int genimg_get_type_id(const char *name)
  861. {
  862. return (get_table_entry_id(uimage_type, "Image", name));
  863. }
  864. int genimg_get_comp_id(const char *name)
  865. {
  866. return (get_table_entry_id(uimage_comp, "Compression", name));
  867. }
  868. #ifndef USE_HOSTCC
  869. /**
  870. * genimg_get_kernel_addr_fit - get the real kernel address and return 2
  871. * FIT strings
  872. * @img_addr: a string might contain real image address
  873. * @fit_uname_config: double pointer to a char, will hold pointer to a
  874. * configuration unit name
  875. * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
  876. * name
  877. *
  878. * genimg_get_kernel_addr_fit get the real kernel start address from a string
  879. * which is normally the first argv of bootm/bootz
  880. *
  881. * returns:
  882. * kernel start address
  883. */
  884. ulong genimg_get_kernel_addr_fit(char * const img_addr,
  885. const char **fit_uname_config,
  886. const char **fit_uname_kernel)
  887. {
  888. ulong kernel_addr;
  889. /* find out kernel image address */
  890. if (!img_addr) {
  891. kernel_addr = image_load_addr;
  892. debug("* kernel: default image load address = 0x%08lx\n",
  893. image_load_addr);
  894. #if CONFIG_IS_ENABLED(FIT)
  895. } else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
  896. fit_uname_config)) {
  897. debug("* kernel: config '%s' from image at 0x%08lx\n",
  898. *fit_uname_config, kernel_addr);
  899. } else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
  900. fit_uname_kernel)) {
  901. debug("* kernel: subimage '%s' from image at 0x%08lx\n",
  902. *fit_uname_kernel, kernel_addr);
  903. #endif
  904. } else {
  905. kernel_addr = simple_strtoul(img_addr, NULL, 16);
  906. debug("* kernel: cmdline image address = 0x%08lx\n",
  907. kernel_addr);
  908. }
  909. return kernel_addr;
  910. }
  911. /**
  912. * genimg_get_kernel_addr() is the simple version of
  913. * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
  914. */
  915. ulong genimg_get_kernel_addr(char * const img_addr)
  916. {
  917. const char *fit_uname_config = NULL;
  918. const char *fit_uname_kernel = NULL;
  919. return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
  920. &fit_uname_kernel);
  921. }
  922. /**
  923. * genimg_get_format - get image format type
  924. * @img_addr: image start address
  925. *
  926. * genimg_get_format() checks whether provided address points to a valid
  927. * legacy or FIT image.
  928. *
  929. * New uImage format and FDT blob are based on a libfdt. FDT blob
  930. * may be passed directly or embedded in a FIT image. In both situations
  931. * genimg_get_format() must be able to dectect libfdt header.
  932. *
  933. * returns:
  934. * image format type or IMAGE_FORMAT_INVALID if no image is present
  935. */
  936. int genimg_get_format(const void *img_addr)
  937. {
  938. #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
  939. const image_header_t *hdr;
  940. hdr = (const image_header_t *)img_addr;
  941. if (image_check_magic(hdr))
  942. return IMAGE_FORMAT_LEGACY;
  943. #endif
  944. #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
  945. if (fdt_check_header(img_addr) == 0)
  946. return IMAGE_FORMAT_FIT;
  947. #endif
  948. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  949. if (android_image_check_header(img_addr) == 0)
  950. return IMAGE_FORMAT_ANDROID;
  951. #endif
  952. return IMAGE_FORMAT_INVALID;
  953. }
  954. /**
  955. * fit_has_config - check if there is a valid FIT configuration
  956. * @images: pointer to the bootm command headers structure
  957. *
  958. * fit_has_config() checks if there is a FIT configuration in use
  959. * (if FTI support is present).
  960. *
  961. * returns:
  962. * 0, no FIT support or no configuration found
  963. * 1, configuration found
  964. */
  965. int genimg_has_config(bootm_headers_t *images)
  966. {
  967. #if IMAGE_ENABLE_FIT
  968. if (images->fit_uname_cfg)
  969. return 1;
  970. #endif
  971. return 0;
  972. }
  973. /**
  974. * boot_get_ramdisk - main ramdisk handling routine
  975. * @argc: command argument count
  976. * @argv: command argument list
  977. * @images: pointer to the bootm images structure
  978. * @arch: expected ramdisk architecture
  979. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  980. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  981. *
  982. * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
  983. * Curently supported are the following ramdisk sources:
  984. * - multicomponent kernel/ramdisk image,
  985. * - commandline provided address of decicated ramdisk image.
  986. *
  987. * returns:
  988. * 0, if ramdisk image was found and valid, or skiped
  989. * rd_start and rd_end are set to ramdisk start/end addresses if
  990. * ramdisk image is found and valid
  991. *
  992. * 1, if ramdisk image is found but corrupted, or invalid
  993. * rd_start and rd_end are set to 0 if no ramdisk exists
  994. */
  995. int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
  996. uint8_t arch, ulong *rd_start, ulong *rd_end)
  997. {
  998. ulong rd_addr, rd_load;
  999. ulong rd_data, rd_len;
  1000. #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
  1001. const image_header_t *rd_hdr;
  1002. #endif
  1003. void *buf;
  1004. #ifdef CONFIG_SUPPORT_RAW_INITRD
  1005. char *end;
  1006. #endif
  1007. #if IMAGE_ENABLE_FIT
  1008. const char *fit_uname_config = images->fit_uname_cfg;
  1009. const char *fit_uname_ramdisk = NULL;
  1010. ulong default_addr;
  1011. int rd_noffset;
  1012. #endif
  1013. const char *select = NULL;
  1014. *rd_start = 0;
  1015. *rd_end = 0;
  1016. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  1017. /*
  1018. * Look for an Android boot image.
  1019. */
  1020. buf = map_sysmem(images->os.start, 0);
  1021. if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
  1022. select = (argc == 0) ? env_get("loadaddr") : argv[0];
  1023. #endif
  1024. if (argc >= 2)
  1025. select = argv[1];
  1026. /*
  1027. * Look for a '-' which indicates to ignore the
  1028. * ramdisk argument
  1029. */
  1030. if (select && strcmp(select, "-") == 0) {
  1031. debug("## Skipping init Ramdisk\n");
  1032. rd_len = rd_data = 0;
  1033. } else if (select || genimg_has_config(images)) {
  1034. #if IMAGE_ENABLE_FIT
  1035. if (select) {
  1036. /*
  1037. * If the init ramdisk comes from the FIT image and
  1038. * the FIT image address is omitted in the command
  1039. * line argument, try to use os FIT image address or
  1040. * default load address.
  1041. */
  1042. if (images->fit_uname_os)
  1043. default_addr = (ulong)images->fit_hdr_os;
  1044. else
  1045. default_addr = image_load_addr;
  1046. if (fit_parse_conf(select, default_addr,
  1047. &rd_addr, &fit_uname_config)) {
  1048. debug("* ramdisk: config '%s' from image at "
  1049. "0x%08lx\n",
  1050. fit_uname_config, rd_addr);
  1051. } else if (fit_parse_subimage(select, default_addr,
  1052. &rd_addr, &fit_uname_ramdisk)) {
  1053. debug("* ramdisk: subimage '%s' from image at "
  1054. "0x%08lx\n",
  1055. fit_uname_ramdisk, rd_addr);
  1056. } else
  1057. #endif
  1058. {
  1059. rd_addr = simple_strtoul(select, NULL, 16);
  1060. debug("* ramdisk: cmdline image address = "
  1061. "0x%08lx\n",
  1062. rd_addr);
  1063. }
  1064. #if IMAGE_ENABLE_FIT
  1065. } else {
  1066. /* use FIT configuration provided in first bootm
  1067. * command argument. If the property is not defined,
  1068. * quit silently.
  1069. */
  1070. rd_addr = map_to_sysmem(images->fit_hdr_os);
  1071. rd_noffset = fit_get_node_from_config(images,
  1072. FIT_RAMDISK_PROP, rd_addr);
  1073. if (rd_noffset == -ENOENT)
  1074. return 0;
  1075. else if (rd_noffset < 0)
  1076. return 1;
  1077. }
  1078. #endif
  1079. /*
  1080. * Check if there is an initrd image at the
  1081. * address provided in the second bootm argument
  1082. * check image type, for FIT images get FIT node.
  1083. */
  1084. buf = map_sysmem(rd_addr, 0);
  1085. switch (genimg_get_format(buf)) {
  1086. #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
  1087. case IMAGE_FORMAT_LEGACY:
  1088. printf("## Loading init Ramdisk from Legacy "
  1089. "Image at %08lx ...\n", rd_addr);
  1090. bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
  1091. rd_hdr = image_get_ramdisk(rd_addr, arch,
  1092. images->verify);
  1093. if (rd_hdr == NULL)
  1094. return 1;
  1095. rd_data = image_get_data(rd_hdr);
  1096. rd_len = image_get_data_size(rd_hdr);
  1097. rd_load = image_get_load(rd_hdr);
  1098. break;
  1099. #endif
  1100. #if IMAGE_ENABLE_FIT
  1101. case IMAGE_FORMAT_FIT:
  1102. rd_noffset = fit_image_load(images,
  1103. rd_addr, &fit_uname_ramdisk,
  1104. &fit_uname_config, arch,
  1105. IH_TYPE_RAMDISK,
  1106. BOOTSTAGE_ID_FIT_RD_START,
  1107. FIT_LOAD_OPTIONAL_NON_ZERO,
  1108. &rd_data, &rd_len);
  1109. if (rd_noffset < 0)
  1110. return 1;
  1111. images->fit_hdr_rd = map_sysmem(rd_addr, 0);
  1112. images->fit_uname_rd = fit_uname_ramdisk;
  1113. images->fit_noffset_rd = rd_noffset;
  1114. break;
  1115. #endif
  1116. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  1117. case IMAGE_FORMAT_ANDROID:
  1118. android_image_get_ramdisk((void *)images->os.start,
  1119. &rd_data, &rd_len);
  1120. break;
  1121. #endif
  1122. default:
  1123. #ifdef CONFIG_SUPPORT_RAW_INITRD
  1124. end = NULL;
  1125. if (select)
  1126. end = strchr(select, ':');
  1127. if (end) {
  1128. rd_len = simple_strtoul(++end, NULL, 16);
  1129. rd_data = rd_addr;
  1130. } else
  1131. #endif
  1132. {
  1133. puts("Wrong Ramdisk Image Format\n");
  1134. rd_data = rd_len = rd_load = 0;
  1135. return 1;
  1136. }
  1137. }
  1138. } else if (images->legacy_hdr_valid &&
  1139. image_check_type(&images->legacy_hdr_os_copy,
  1140. IH_TYPE_MULTI)) {
  1141. /*
  1142. * Now check if we have a legacy mult-component image,
  1143. * get second entry data start address and len.
  1144. */
  1145. bootstage_mark(BOOTSTAGE_ID_RAMDISK);
  1146. printf("## Loading init Ramdisk from multi component "
  1147. "Legacy Image at %08lx ...\n",
  1148. (ulong)images->legacy_hdr_os);
  1149. image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
  1150. } else {
  1151. /*
  1152. * no initrd image
  1153. */
  1154. bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
  1155. rd_len = rd_data = 0;
  1156. }
  1157. if (!rd_data) {
  1158. debug("## No init Ramdisk\n");
  1159. } else {
  1160. *rd_start = rd_data;
  1161. *rd_end = rd_data + rd_len;
  1162. }
  1163. debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  1164. *rd_start, *rd_end);
  1165. return 0;
  1166. }
  1167. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  1168. /**
  1169. * boot_ramdisk_high - relocate init ramdisk
  1170. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1171. * @rd_data: ramdisk data start address
  1172. * @rd_len: ramdisk data length
  1173. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  1174. * start address (after possible relocation)
  1175. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  1176. * end address (after possible relocation)
  1177. *
  1178. * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
  1179. * variable and if requested ramdisk data is moved to a specified location.
  1180. *
  1181. * Initrd_start and initrd_end are set to final (after relocation) ramdisk
  1182. * start/end addresses if ramdisk image start and len were provided,
  1183. * otherwise set initrd_start and initrd_end set to zeros.
  1184. *
  1185. * returns:
  1186. * 0 - success
  1187. * -1 - failure
  1188. */
  1189. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  1190. ulong *initrd_start, ulong *initrd_end)
  1191. {
  1192. char *s;
  1193. ulong initrd_high;
  1194. int initrd_copy_to_ram = 1;
  1195. s = env_get("initrd_high");
  1196. if (s) {
  1197. /* a value of "no" or a similar string will act like 0,
  1198. * turning the "load high" feature off. This is intentional.
  1199. */
  1200. initrd_high = simple_strtoul(s, NULL, 16);
  1201. if (initrd_high == ~0)
  1202. initrd_copy_to_ram = 0;
  1203. } else {
  1204. initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
  1205. }
  1206. debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  1207. initrd_high, initrd_copy_to_ram);
  1208. if (rd_data) {
  1209. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  1210. debug(" in-place initrd\n");
  1211. *initrd_start = rd_data;
  1212. *initrd_end = rd_data + rd_len;
  1213. lmb_reserve(lmb, rd_data, rd_len);
  1214. } else {
  1215. if (initrd_high)
  1216. *initrd_start = (ulong)lmb_alloc_base(lmb,
  1217. rd_len, 0x1000, initrd_high);
  1218. else
  1219. *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
  1220. 0x1000);
  1221. if (*initrd_start == 0) {
  1222. puts("ramdisk - allocation error\n");
  1223. goto error;
  1224. }
  1225. bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
  1226. *initrd_end = *initrd_start + rd_len;
  1227. printf(" Loading Ramdisk to %08lx, end %08lx ... ",
  1228. *initrd_start, *initrd_end);
  1229. memmove_wd((void *)*initrd_start,
  1230. (void *)rd_data, rd_len, CHUNKSZ);
  1231. #ifdef CONFIG_MP
  1232. /*
  1233. * Ensure the image is flushed to memory to handle
  1234. * AMP boot scenarios in which we might not be
  1235. * HW cache coherent
  1236. */
  1237. flush_cache((unsigned long)*initrd_start,
  1238. ALIGN(rd_len, ARCH_DMA_MINALIGN));
  1239. #endif
  1240. puts("OK\n");
  1241. }
  1242. } else {
  1243. *initrd_start = 0;
  1244. *initrd_end = 0;
  1245. }
  1246. debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  1247. *initrd_start, *initrd_end);
  1248. return 0;
  1249. error:
  1250. return -1;
  1251. }
  1252. #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
  1253. int boot_get_setup(bootm_headers_t *images, uint8_t arch,
  1254. ulong *setup_start, ulong *setup_len)
  1255. {
  1256. #if IMAGE_ENABLE_FIT
  1257. return boot_get_setup_fit(images, arch, setup_start, setup_len);
  1258. #else
  1259. return -ENOENT;
  1260. #endif
  1261. }
  1262. #if IMAGE_ENABLE_FIT
  1263. #if defined(CONFIG_FPGA)
  1264. int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
  1265. uint8_t arch, const ulong *ld_start, ulong * const ld_len)
  1266. {
  1267. ulong tmp_img_addr, img_data, img_len;
  1268. void *buf;
  1269. int conf_noffset;
  1270. int fit_img_result;
  1271. const char *uname, *name;
  1272. int err;
  1273. int devnum = 0; /* TODO support multi fpga platforms */
  1274. /* Check to see if the images struct has a FIT configuration */
  1275. if (!genimg_has_config(images)) {
  1276. debug("## FIT configuration was not specified\n");
  1277. return 0;
  1278. }
  1279. /*
  1280. * Obtain the os FIT header from the images struct
  1281. */
  1282. tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
  1283. buf = map_sysmem(tmp_img_addr, 0);
  1284. /*
  1285. * Check image type. For FIT images get FIT node
  1286. * and attempt to locate a generic binary.
  1287. */
  1288. switch (genimg_get_format(buf)) {
  1289. case IMAGE_FORMAT_FIT:
  1290. conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
  1291. uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
  1292. NULL);
  1293. if (!uname) {
  1294. debug("## FPGA image is not specified\n");
  1295. return 0;
  1296. }
  1297. fit_img_result = fit_image_load(images,
  1298. tmp_img_addr,
  1299. (const char **)&uname,
  1300. &(images->fit_uname_cfg),
  1301. arch,
  1302. IH_TYPE_FPGA,
  1303. BOOTSTAGE_ID_FPGA_INIT,
  1304. FIT_LOAD_OPTIONAL_NON_ZERO,
  1305. &img_data, &img_len);
  1306. debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
  1307. uname, img_data, img_len);
  1308. if (fit_img_result < 0) {
  1309. /* Something went wrong! */
  1310. return fit_img_result;
  1311. }
  1312. if (!fpga_is_partial_data(devnum, img_len)) {
  1313. name = "full";
  1314. err = fpga_loadbitstream(devnum, (char *)img_data,
  1315. img_len, BIT_FULL);
  1316. if (err)
  1317. err = fpga_load(devnum, (const void *)img_data,
  1318. img_len, BIT_FULL);
  1319. } else {
  1320. name = "partial";
  1321. err = fpga_loadbitstream(devnum, (char *)img_data,
  1322. img_len, BIT_PARTIAL);
  1323. if (err)
  1324. err = fpga_load(devnum, (const void *)img_data,
  1325. img_len, BIT_PARTIAL);
  1326. }
  1327. if (err)
  1328. return err;
  1329. printf(" Programming %s bitstream... OK\n", name);
  1330. break;
  1331. default:
  1332. printf("The given image format is not supported (corrupt?)\n");
  1333. return 1;
  1334. }
  1335. return 0;
  1336. }
  1337. #endif
  1338. static void fit_loadable_process(uint8_t img_type,
  1339. ulong img_data,
  1340. ulong img_len)
  1341. {
  1342. int i;
  1343. const unsigned int count =
  1344. ll_entry_count(struct fit_loadable_tbl, fit_loadable);
  1345. struct fit_loadable_tbl *fit_loadable_handler =
  1346. ll_entry_start(struct fit_loadable_tbl, fit_loadable);
  1347. /* For each loadable handler */
  1348. for (i = 0; i < count; i++, fit_loadable_handler++)
  1349. /* matching this type */
  1350. if (fit_loadable_handler->type == img_type)
  1351. /* call that handler with this image data */
  1352. fit_loadable_handler->handler(img_data, img_len);
  1353. }
  1354. int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
  1355. uint8_t arch, const ulong *ld_start, ulong * const ld_len)
  1356. {
  1357. /*
  1358. * These variables are used to hold the current image location
  1359. * in system memory.
  1360. */
  1361. ulong tmp_img_addr;
  1362. /*
  1363. * These two variables are requirements for fit_image_load, but
  1364. * their values are not used
  1365. */
  1366. ulong img_data, img_len;
  1367. void *buf;
  1368. int loadables_index;
  1369. int conf_noffset;
  1370. int fit_img_result;
  1371. const char *uname;
  1372. uint8_t img_type;
  1373. /* Check to see if the images struct has a FIT configuration */
  1374. if (!genimg_has_config(images)) {
  1375. debug("## FIT configuration was not specified\n");
  1376. return 0;
  1377. }
  1378. /*
  1379. * Obtain the os FIT header from the images struct
  1380. */
  1381. tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
  1382. buf = map_sysmem(tmp_img_addr, 0);
  1383. /*
  1384. * Check image type. For FIT images get FIT node
  1385. * and attempt to locate a generic binary.
  1386. */
  1387. switch (genimg_get_format(buf)) {
  1388. case IMAGE_FORMAT_FIT:
  1389. conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
  1390. for (loadables_index = 0;
  1391. uname = fdt_stringlist_get(buf, conf_noffset,
  1392. FIT_LOADABLE_PROP, loadables_index,
  1393. NULL), uname;
  1394. loadables_index++)
  1395. {
  1396. fit_img_result = fit_image_load(images,
  1397. tmp_img_addr,
  1398. &uname,
  1399. &(images->fit_uname_cfg), arch,
  1400. IH_TYPE_LOADABLE,
  1401. BOOTSTAGE_ID_FIT_LOADABLE_START,
  1402. FIT_LOAD_OPTIONAL_NON_ZERO,
  1403. &img_data, &img_len);
  1404. if (fit_img_result < 0) {
  1405. /* Something went wrong! */
  1406. return fit_img_result;
  1407. }
  1408. fit_img_result = fit_image_get_node(buf, uname);
  1409. if (fit_img_result < 0) {
  1410. /* Something went wrong! */
  1411. return fit_img_result;
  1412. }
  1413. fit_img_result = fit_image_get_type(buf,
  1414. fit_img_result,
  1415. &img_type);
  1416. if (fit_img_result < 0) {
  1417. /* Something went wrong! */
  1418. return fit_img_result;
  1419. }
  1420. fit_loadable_process(img_type, img_data, img_len);
  1421. }
  1422. break;
  1423. default:
  1424. printf("The given image format is not supported (corrupt?)\n");
  1425. return 1;
  1426. }
  1427. return 0;
  1428. }
  1429. #endif
  1430. #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
  1431. /**
  1432. * boot_get_cmdline - allocate and initialize kernel cmdline
  1433. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1434. * @cmd_start: pointer to a ulong variable, will hold cmdline start
  1435. * @cmd_end: pointer to a ulong variable, will hold cmdline end
  1436. *
  1437. * boot_get_cmdline() allocates space for kernel command line below
  1438. * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
  1439. * variable is present its contents is copied to allocated kernel
  1440. * command line.
  1441. *
  1442. * returns:
  1443. * 0 - success
  1444. * -1 - failure
  1445. */
  1446. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
  1447. {
  1448. char *cmdline;
  1449. char *s;
  1450. cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
  1451. env_get_bootm_mapsize() + env_get_bootm_low());
  1452. if (cmdline == NULL)
  1453. return -1;
  1454. s = env_get("bootargs");
  1455. if (!s)
  1456. s = "";
  1457. strcpy(cmdline, s);
  1458. *cmd_start = (ulong) & cmdline[0];
  1459. *cmd_end = *cmd_start + strlen(cmdline);
  1460. debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  1461. return 0;
  1462. }
  1463. #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
  1464. #ifdef CONFIG_SYS_BOOT_GET_KBD
  1465. /**
  1466. * boot_get_kbd - allocate and initialize kernel copy of board info
  1467. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1468. * @kbd: double pointer to board info data
  1469. *
  1470. * boot_get_kbd() allocates space for kernel copy of board info data below
  1471. * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
  1472. * with the current u-boot board info data.
  1473. *
  1474. * returns:
  1475. * 0 - success
  1476. * -1 - failure
  1477. */
  1478. int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
  1479. {
  1480. *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
  1481. sizeof(struct bd_info),
  1482. 0xf,
  1483. env_get_bootm_mapsize() + env_get_bootm_low());
  1484. if (*kbd == NULL)
  1485. return -1;
  1486. **kbd = *(gd->bd);
  1487. debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  1488. #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  1489. do_bdinfo(NULL, 0, 0, NULL);
  1490. #endif
  1491. return 0;
  1492. }
  1493. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  1494. #ifdef CONFIG_LMB
  1495. int image_setup_linux(bootm_headers_t *images)
  1496. {
  1497. ulong of_size = images->ft_len;
  1498. char **of_flat_tree = &images->ft_addr;
  1499. struct lmb *lmb = &images->lmb;
  1500. int ret;
  1501. if (IMAGE_ENABLE_OF_LIBFDT)
  1502. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  1503. if (IMAGE_BOOT_GET_CMDLINE) {
  1504. ret = boot_get_cmdline(lmb, &images->cmdline_start,
  1505. &images->cmdline_end);
  1506. if (ret) {
  1507. puts("ERROR with allocation of cmdline\n");
  1508. return ret;
  1509. }
  1510. }
  1511. if (IMAGE_ENABLE_OF_LIBFDT) {
  1512. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  1513. if (ret)
  1514. return ret;
  1515. }
  1516. if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
  1517. ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
  1518. if (ret)
  1519. return ret;
  1520. }
  1521. return 0;
  1522. }
  1523. #endif /* CONFIG_LMB */
  1524. #endif /* !USE_HOSTCC */