image.c 43 KB

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