image.c 40 KB

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