image.c 43 KB

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