image.c 46 KB

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