image.c 40 KB

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