image.c 40 KB

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