image.c 43 KB

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