image.c 46 KB

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