image.c 45 KB

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