image.c 44 KB

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