image.c 44 KB

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