image.c 43 KB

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