image.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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. { -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. * If dataflash support is enabled routine checks for dataflash addresses
  343. * and handles required dataflash reads.
  344. *
  345. * returns:
  346. * pointer to a ramdisk image header, if image was found and valid
  347. * otherwise, return NULL
  348. */
  349. static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
  350. int verify)
  351. {
  352. const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
  353. if (!image_check_magic(rd_hdr)) {
  354. puts("Bad Magic Number\n");
  355. bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
  356. return NULL;
  357. }
  358. if (!image_check_hcrc(rd_hdr)) {
  359. puts("Bad Header Checksum\n");
  360. bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
  361. return NULL;
  362. }
  363. bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
  364. image_print_contents(rd_hdr);
  365. if (verify) {
  366. puts(" Verifying Checksum ... ");
  367. if (!image_check_dcrc(rd_hdr)) {
  368. puts("Bad Data CRC\n");
  369. bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
  370. return NULL;
  371. }
  372. puts("OK\n");
  373. }
  374. bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
  375. if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
  376. !image_check_arch(rd_hdr, arch) ||
  377. !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
  378. printf("No Linux %s Ramdisk Image\n",
  379. genimg_get_arch_name(arch));
  380. bootstage_error(BOOTSTAGE_ID_RAMDISK);
  381. return NULL;
  382. }
  383. return rd_hdr;
  384. }
  385. #endif
  386. #endif /* !USE_HOSTCC */
  387. /*****************************************************************************/
  388. /* Shared dual-format routines */
  389. /*****************************************************************************/
  390. #ifndef USE_HOSTCC
  391. ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
  392. ulong save_addr; /* Default Save Address */
  393. ulong save_size; /* Default Save Size (in bytes) */
  394. static int on_loadaddr(const char *name, const char *value, enum env_op op,
  395. int flags)
  396. {
  397. switch (op) {
  398. case env_op_create:
  399. case env_op_overwrite:
  400. load_addr = simple_strtoul(value, NULL, 16);
  401. break;
  402. default:
  403. break;
  404. }
  405. return 0;
  406. }
  407. U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
  408. ulong env_get_bootm_low(void)
  409. {
  410. char *s = env_get("bootm_low");
  411. if (s) {
  412. ulong tmp = simple_strtoul(s, NULL, 16);
  413. return tmp;
  414. }
  415. #if defined(CONFIG_SYS_SDRAM_BASE)
  416. return CONFIG_SYS_SDRAM_BASE;
  417. #elif defined(CONFIG_ARM)
  418. return gd->bd->bi_dram[0].start;
  419. #else
  420. return 0;
  421. #endif
  422. }
  423. phys_size_t env_get_bootm_size(void)
  424. {
  425. phys_size_t tmp, size;
  426. phys_addr_t start;
  427. char *s = env_get("bootm_size");
  428. if (s) {
  429. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  430. return tmp;
  431. }
  432. #if defined(CONFIG_ARM) && defined(CONFIG_NR_DRAM_BANKS)
  433. start = gd->bd->bi_dram[0].start;
  434. size = gd->bd->bi_dram[0].size;
  435. #else
  436. start = gd->bd->bi_memstart;
  437. size = gd->bd->bi_memsize;
  438. #endif
  439. s = env_get("bootm_low");
  440. if (s)
  441. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  442. else
  443. tmp = start;
  444. return size - (tmp - start);
  445. }
  446. phys_size_t env_get_bootm_mapsize(void)
  447. {
  448. phys_size_t tmp;
  449. char *s = env_get("bootm_mapsize");
  450. if (s) {
  451. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  452. return tmp;
  453. }
  454. #if defined(CONFIG_SYS_BOOTMAPSZ)
  455. return CONFIG_SYS_BOOTMAPSZ;
  456. #else
  457. return env_get_bootm_size();
  458. #endif
  459. }
  460. void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  461. {
  462. if (to == from)
  463. return;
  464. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  465. if (to > from) {
  466. from += len;
  467. to += len;
  468. }
  469. while (len > 0) {
  470. size_t tail = (len > chunksz) ? chunksz : len;
  471. WATCHDOG_RESET();
  472. if (to > from) {
  473. to -= tail;
  474. from -= tail;
  475. }
  476. memmove(to, from, tail);
  477. if (to < from) {
  478. to += tail;
  479. from += tail;
  480. }
  481. len -= tail;
  482. }
  483. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  484. memmove(to, from, len);
  485. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  486. }
  487. #endif /* !USE_HOSTCC */
  488. void genimg_print_size(uint32_t size)
  489. {
  490. #ifndef USE_HOSTCC
  491. printf("%d Bytes = ", size);
  492. print_size(size, "\n");
  493. #else
  494. printf("%d Bytes = %.2f KiB = %.2f MiB\n",
  495. size, (double)size / 1.024e3,
  496. (double)size / 1.048576e6);
  497. #endif
  498. }
  499. #if IMAGE_ENABLE_TIMESTAMP
  500. void genimg_print_time(time_t timestamp)
  501. {
  502. #ifndef USE_HOSTCC
  503. struct rtc_time tm;
  504. rtc_to_tm(timestamp, &tm);
  505. printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
  506. tm.tm_year, tm.tm_mon, tm.tm_mday,
  507. tm.tm_hour, tm.tm_min, tm.tm_sec);
  508. #else
  509. printf("%s", ctime(&timestamp));
  510. #endif
  511. }
  512. #endif
  513. const table_entry_t *get_table_entry(const table_entry_t *table, int id)
  514. {
  515. for (; table->id >= 0; ++table) {
  516. if (table->id == id)
  517. return table;
  518. }
  519. return NULL;
  520. }
  521. static const char *unknown_msg(enum ih_category category)
  522. {
  523. static const char unknown_str[] = "Unknown ";
  524. static char msg[30];
  525. strcpy(msg, unknown_str);
  526. strncat(msg, table_info[category].desc,
  527. sizeof(msg) - sizeof(unknown_str));
  528. return msg;
  529. }
  530. /**
  531. * get_cat_table_entry_name - translate entry id to long name
  532. * @category: category to look up (enum ih_category)
  533. * @id: entry id to be translated
  534. *
  535. * This will scan the translation table trying to find the entry that matches
  536. * the given id.
  537. *
  538. * @retur long entry name if translation succeeds; error string on failure
  539. */
  540. const char *genimg_get_cat_name(enum ih_category category, uint id)
  541. {
  542. const table_entry_t *entry;
  543. entry = get_table_entry(table_info[category].table, id);
  544. if (!entry)
  545. return unknown_msg(category);
  546. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  547. return entry->lname;
  548. #else
  549. return entry->lname + gd->reloc_off;
  550. #endif
  551. }
  552. /**
  553. * get_cat_table_entry_short_name - translate entry id to short name
  554. * @category: category to look up (enum ih_category)
  555. * @id: entry id to be translated
  556. *
  557. * This will scan the translation table trying to find the entry that matches
  558. * the given id.
  559. *
  560. * @retur short entry name if translation succeeds; error string on failure
  561. */
  562. const char *genimg_get_cat_short_name(enum ih_category category, uint id)
  563. {
  564. const table_entry_t *entry;
  565. entry = get_table_entry(table_info[category].table, id);
  566. if (!entry)
  567. return unknown_msg(category);
  568. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  569. return entry->sname;
  570. #else
  571. return entry->sname + gd->reloc_off;
  572. #endif
  573. }
  574. int genimg_get_cat_count(enum ih_category category)
  575. {
  576. return table_info[category].count;
  577. }
  578. const char *genimg_get_cat_desc(enum ih_category category)
  579. {
  580. return table_info[category].desc;
  581. }
  582. /**
  583. * get_table_entry_name - translate entry id to long name
  584. * @table: pointer to a translation table for entries of a specific type
  585. * @msg: message to be returned when translation fails
  586. * @id: entry id to be translated
  587. *
  588. * get_table_entry_name() will go over translation table trying to find
  589. * entry that matches given id. If matching entry is found, its long
  590. * name is returned to the caller.
  591. *
  592. * returns:
  593. * long entry name if translation succeeds
  594. * msg otherwise
  595. */
  596. char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
  597. {
  598. table = get_table_entry(table, id);
  599. if (!table)
  600. return msg;
  601. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  602. return table->lname;
  603. #else
  604. return table->lname + gd->reloc_off;
  605. #endif
  606. }
  607. const char *genimg_get_os_name(uint8_t os)
  608. {
  609. return (get_table_entry_name(uimage_os, "Unknown OS", os));
  610. }
  611. const char *genimg_get_arch_name(uint8_t arch)
  612. {
  613. return (get_table_entry_name(uimage_arch, "Unknown Architecture",
  614. arch));
  615. }
  616. const char *genimg_get_type_name(uint8_t type)
  617. {
  618. return (get_table_entry_name(uimage_type, "Unknown Image", type));
  619. }
  620. static const char *genimg_get_short_name(const table_entry_t *table, int val)
  621. {
  622. table = get_table_entry(table, val);
  623. if (!table)
  624. return "unknown";
  625. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  626. return table->sname;
  627. #else
  628. return table->sname + gd->reloc_off;
  629. #endif
  630. }
  631. const char *genimg_get_type_short_name(uint8_t type)
  632. {
  633. return genimg_get_short_name(uimage_type, type);
  634. }
  635. const char *genimg_get_comp_name(uint8_t comp)
  636. {
  637. return (get_table_entry_name(uimage_comp, "Unknown Compression",
  638. comp));
  639. }
  640. const char *genimg_get_comp_short_name(uint8_t comp)
  641. {
  642. return genimg_get_short_name(uimage_comp, comp);
  643. }
  644. const char *genimg_get_os_short_name(uint8_t os)
  645. {
  646. return genimg_get_short_name(uimage_os, os);
  647. }
  648. const char *genimg_get_arch_short_name(uint8_t arch)
  649. {
  650. return genimg_get_short_name(uimage_arch, arch);
  651. }
  652. /**
  653. * get_table_entry_id - translate short entry name to id
  654. * @table: pointer to a translation table for entries of a specific type
  655. * @table_name: to be used in case of error
  656. * @name: entry short name to be translated
  657. *
  658. * get_table_entry_id() will go over translation table trying to find
  659. * entry that matches given short name. If matching entry is found,
  660. * its id returned to the caller.
  661. *
  662. * returns:
  663. * entry id if translation succeeds
  664. * -1 otherwise
  665. */
  666. int get_table_entry_id(const table_entry_t *table,
  667. const char *table_name, const char *name)
  668. {
  669. const table_entry_t *t;
  670. for (t = table; t->id >= 0; ++t) {
  671. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  672. if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
  673. #else
  674. if (t->sname && strcasecmp(t->sname, name) == 0)
  675. #endif
  676. return (t->id);
  677. }
  678. debug("Invalid %s Type: %s\n", table_name, name);
  679. return -1;
  680. }
  681. int genimg_get_os_id(const char *name)
  682. {
  683. return (get_table_entry_id(uimage_os, "OS", name));
  684. }
  685. int genimg_get_arch_id(const char *name)
  686. {
  687. return (get_table_entry_id(uimage_arch, "CPU", name));
  688. }
  689. int genimg_get_type_id(const char *name)
  690. {
  691. return (get_table_entry_id(uimage_type, "Image", name));
  692. }
  693. int genimg_get_comp_id(const char *name)
  694. {
  695. return (get_table_entry_id(uimage_comp, "Compression", name));
  696. }
  697. #ifndef USE_HOSTCC
  698. /**
  699. * genimg_get_kernel_addr_fit - get the real kernel address and return 2
  700. * FIT strings
  701. * @img_addr: a string might contain real image address
  702. * @fit_uname_config: double pointer to a char, will hold pointer to a
  703. * configuration unit name
  704. * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
  705. * name
  706. *
  707. * genimg_get_kernel_addr_fit get the real kernel start address from a string
  708. * which is normally the first argv of bootm/bootz
  709. *
  710. * returns:
  711. * kernel start address
  712. */
  713. ulong genimg_get_kernel_addr_fit(char * const img_addr,
  714. const char **fit_uname_config,
  715. const char **fit_uname_kernel)
  716. {
  717. ulong kernel_addr;
  718. /* find out kernel image address */
  719. if (!img_addr) {
  720. kernel_addr = load_addr;
  721. debug("* kernel: default image load address = 0x%08lx\n",
  722. load_addr);
  723. #if CONFIG_IS_ENABLED(FIT)
  724. } else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
  725. fit_uname_config)) {
  726. debug("* kernel: config '%s' from image at 0x%08lx\n",
  727. *fit_uname_config, kernel_addr);
  728. } else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
  729. fit_uname_kernel)) {
  730. debug("* kernel: subimage '%s' from image at 0x%08lx\n",
  731. *fit_uname_kernel, kernel_addr);
  732. #endif
  733. } else {
  734. kernel_addr = simple_strtoul(img_addr, NULL, 16);
  735. debug("* kernel: cmdline image address = 0x%08lx\n",
  736. kernel_addr);
  737. }
  738. return kernel_addr;
  739. }
  740. /**
  741. * genimg_get_kernel_addr() is the simple version of
  742. * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
  743. */
  744. ulong genimg_get_kernel_addr(char * const img_addr)
  745. {
  746. const char *fit_uname_config = NULL;
  747. const char *fit_uname_kernel = NULL;
  748. return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
  749. &fit_uname_kernel);
  750. }
  751. /**
  752. * genimg_get_format - get image format type
  753. * @img_addr: image start address
  754. *
  755. * genimg_get_format() checks whether provided address points to a valid
  756. * legacy or FIT image.
  757. *
  758. * New uImage format and FDT blob are based on a libfdt. FDT blob
  759. * may be passed directly or embedded in a FIT image. In both situations
  760. * genimg_get_format() must be able to dectect libfdt header.
  761. *
  762. * returns:
  763. * image format type or IMAGE_FORMAT_INVALID if no image is present
  764. */
  765. int genimg_get_format(const void *img_addr)
  766. {
  767. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  768. const image_header_t *hdr;
  769. hdr = (const image_header_t *)img_addr;
  770. if (image_check_magic(hdr))
  771. return IMAGE_FORMAT_LEGACY;
  772. #endif
  773. #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
  774. if (fdt_check_header(img_addr) == 0)
  775. return IMAGE_FORMAT_FIT;
  776. #endif
  777. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  778. if (android_image_check_header(img_addr) == 0)
  779. return IMAGE_FORMAT_ANDROID;
  780. #endif
  781. return IMAGE_FORMAT_INVALID;
  782. }
  783. /**
  784. * genimg_get_image - get image from special storage (if necessary)
  785. * @img_addr: image start address
  786. *
  787. * genimg_get_image() checks if provided image start address is located
  788. * in a dataflash storage. If so, image is moved to a system RAM memory.
  789. *
  790. * returns:
  791. * image start address after possible relocation from special storage
  792. */
  793. ulong genimg_get_image(ulong img_addr)
  794. {
  795. ulong ram_addr = img_addr;
  796. #ifdef CONFIG_HAS_DATAFLASH
  797. ulong h_size, d_size;
  798. if (addr_dataflash(img_addr)) {
  799. void *buf;
  800. /* ger RAM address */
  801. ram_addr = CONFIG_SYS_LOAD_ADDR;
  802. /* get header size */
  803. h_size = image_get_header_size();
  804. #if IMAGE_ENABLE_FIT
  805. if (sizeof(struct fdt_header) > h_size)
  806. h_size = sizeof(struct fdt_header);
  807. #endif
  808. /* read in header */
  809. debug(" Reading image header from dataflash address "
  810. "%08lx to RAM address %08lx\n", img_addr, ram_addr);
  811. buf = map_sysmem(ram_addr, 0);
  812. read_dataflash(img_addr, h_size, buf);
  813. /* get data size */
  814. switch (genimg_get_format(buf)) {
  815. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  816. case IMAGE_FORMAT_LEGACY:
  817. d_size = image_get_data_size(buf);
  818. debug(" Legacy format image found at 0x%08lx, "
  819. "size 0x%08lx\n",
  820. ram_addr, d_size);
  821. break;
  822. #endif
  823. #if IMAGE_ENABLE_FIT
  824. case IMAGE_FORMAT_FIT:
  825. d_size = fit_get_size(buf) - h_size;
  826. debug(" FIT/FDT format image found at 0x%08lx, "
  827. "size 0x%08lx\n",
  828. ram_addr, d_size);
  829. break;
  830. #endif
  831. default:
  832. printf(" No valid image found at 0x%08lx\n",
  833. img_addr);
  834. return ram_addr;
  835. }
  836. /* read in image data */
  837. debug(" Reading image remaining data from dataflash address "
  838. "%08lx to RAM address %08lx\n", img_addr + h_size,
  839. ram_addr + h_size);
  840. read_dataflash(img_addr + h_size, d_size,
  841. (char *)(buf + h_size));
  842. }
  843. #endif /* CONFIG_HAS_DATAFLASH */
  844. return ram_addr;
  845. }
  846. /**
  847. * fit_has_config - check if there is a valid FIT configuration
  848. * @images: pointer to the bootm command headers structure
  849. *
  850. * fit_has_config() checks if there is a FIT configuration in use
  851. * (if FTI support is present).
  852. *
  853. * returns:
  854. * 0, no FIT support or no configuration found
  855. * 1, configuration found
  856. */
  857. int genimg_has_config(bootm_headers_t *images)
  858. {
  859. #if IMAGE_ENABLE_FIT
  860. if (images->fit_uname_cfg)
  861. return 1;
  862. #endif
  863. return 0;
  864. }
  865. /**
  866. * boot_get_ramdisk - main ramdisk handling routine
  867. * @argc: command argument count
  868. * @argv: command argument list
  869. * @images: pointer to the bootm images structure
  870. * @arch: expected ramdisk architecture
  871. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  872. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  873. *
  874. * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
  875. * Curently supported are the following ramdisk sources:
  876. * - multicomponent kernel/ramdisk image,
  877. * - commandline provided address of decicated ramdisk image.
  878. *
  879. * returns:
  880. * 0, if ramdisk image was found and valid, or skiped
  881. * rd_start and rd_end are set to ramdisk start/end addresses if
  882. * ramdisk image is found and valid
  883. *
  884. * 1, if ramdisk image is found but corrupted, or invalid
  885. * rd_start and rd_end are set to 0 if no ramdisk exists
  886. */
  887. int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
  888. uint8_t arch, ulong *rd_start, ulong *rd_end)
  889. {
  890. ulong rd_addr, rd_load;
  891. ulong rd_data, rd_len;
  892. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  893. const image_header_t *rd_hdr;
  894. #endif
  895. void *buf;
  896. #ifdef CONFIG_SUPPORT_RAW_INITRD
  897. char *end;
  898. #endif
  899. #if IMAGE_ENABLE_FIT
  900. const char *fit_uname_config = images->fit_uname_cfg;
  901. const char *fit_uname_ramdisk = NULL;
  902. ulong default_addr;
  903. int rd_noffset;
  904. #endif
  905. const char *select = NULL;
  906. *rd_start = 0;
  907. *rd_end = 0;
  908. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  909. /*
  910. * Look for an Android boot image.
  911. */
  912. buf = map_sysmem(images->os.start, 0);
  913. if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
  914. select = argv[0];
  915. #endif
  916. if (argc >= 2)
  917. select = argv[1];
  918. /*
  919. * Look for a '-' which indicates to ignore the
  920. * ramdisk argument
  921. */
  922. if (select && strcmp(select, "-") == 0) {
  923. debug("## Skipping init Ramdisk\n");
  924. rd_len = rd_data = 0;
  925. } else if (select || genimg_has_config(images)) {
  926. #if IMAGE_ENABLE_FIT
  927. if (select) {
  928. /*
  929. * If the init ramdisk comes from the FIT image and
  930. * the FIT image address is omitted in the command
  931. * line argument, try to use os FIT image address or
  932. * default load address.
  933. */
  934. if (images->fit_uname_os)
  935. default_addr = (ulong)images->fit_hdr_os;
  936. else
  937. default_addr = load_addr;
  938. if (fit_parse_conf(select, default_addr,
  939. &rd_addr, &fit_uname_config)) {
  940. debug("* ramdisk: config '%s' from image at "
  941. "0x%08lx\n",
  942. fit_uname_config, rd_addr);
  943. } else if (fit_parse_subimage(select, default_addr,
  944. &rd_addr, &fit_uname_ramdisk)) {
  945. debug("* ramdisk: subimage '%s' from image at "
  946. "0x%08lx\n",
  947. fit_uname_ramdisk, rd_addr);
  948. } else
  949. #endif
  950. {
  951. rd_addr = simple_strtoul(select, NULL, 16);
  952. debug("* ramdisk: cmdline image address = "
  953. "0x%08lx\n",
  954. rd_addr);
  955. }
  956. #if IMAGE_ENABLE_FIT
  957. } else {
  958. /* use FIT configuration provided in first bootm
  959. * command argument. If the property is not defined,
  960. * quit silently.
  961. */
  962. rd_addr = map_to_sysmem(images->fit_hdr_os);
  963. rd_noffset = fit_get_node_from_config(images,
  964. FIT_RAMDISK_PROP, rd_addr);
  965. if (rd_noffset == -ENOENT)
  966. return 0;
  967. else if (rd_noffset < 0)
  968. return 1;
  969. }
  970. #endif
  971. /* copy from dataflash if needed */
  972. rd_addr = genimg_get_image(rd_addr);
  973. /*
  974. * Check if there is an initrd image at the
  975. * address provided in the second bootm argument
  976. * check image type, for FIT images get FIT node.
  977. */
  978. buf = map_sysmem(rd_addr, 0);
  979. switch (genimg_get_format(buf)) {
  980. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  981. case IMAGE_FORMAT_LEGACY:
  982. printf("## Loading init Ramdisk from Legacy "
  983. "Image at %08lx ...\n", rd_addr);
  984. bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
  985. rd_hdr = image_get_ramdisk(rd_addr, arch,
  986. images->verify);
  987. if (rd_hdr == NULL)
  988. return 1;
  989. rd_data = image_get_data(rd_hdr);
  990. rd_len = image_get_data_size(rd_hdr);
  991. rd_load = image_get_load(rd_hdr);
  992. break;
  993. #endif
  994. #if IMAGE_ENABLE_FIT
  995. case IMAGE_FORMAT_FIT:
  996. rd_noffset = fit_image_load(images,
  997. rd_addr, &fit_uname_ramdisk,
  998. &fit_uname_config, arch,
  999. IH_TYPE_RAMDISK,
  1000. BOOTSTAGE_ID_FIT_RD_START,
  1001. FIT_LOAD_OPTIONAL_NON_ZERO,
  1002. &rd_data, &rd_len);
  1003. if (rd_noffset < 0)
  1004. return 1;
  1005. images->fit_hdr_rd = map_sysmem(rd_addr, 0);
  1006. images->fit_uname_rd = fit_uname_ramdisk;
  1007. images->fit_noffset_rd = rd_noffset;
  1008. break;
  1009. #endif
  1010. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  1011. case IMAGE_FORMAT_ANDROID:
  1012. android_image_get_ramdisk((void *)images->os.start,
  1013. &rd_data, &rd_len);
  1014. break;
  1015. #endif
  1016. default:
  1017. #ifdef CONFIG_SUPPORT_RAW_INITRD
  1018. end = NULL;
  1019. if (select)
  1020. end = strchr(select, ':');
  1021. if (end) {
  1022. rd_len = simple_strtoul(++end, NULL, 16);
  1023. rd_data = rd_addr;
  1024. } else
  1025. #endif
  1026. {
  1027. puts("Wrong Ramdisk Image Format\n");
  1028. rd_data = rd_len = rd_load = 0;
  1029. return 1;
  1030. }
  1031. }
  1032. } else if (images->legacy_hdr_valid &&
  1033. image_check_type(&images->legacy_hdr_os_copy,
  1034. IH_TYPE_MULTI)) {
  1035. /*
  1036. * Now check if we have a legacy mult-component image,
  1037. * get second entry data start address and len.
  1038. */
  1039. bootstage_mark(BOOTSTAGE_ID_RAMDISK);
  1040. printf("## Loading init Ramdisk from multi component "
  1041. "Legacy Image at %08lx ...\n",
  1042. (ulong)images->legacy_hdr_os);
  1043. image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
  1044. } else {
  1045. /*
  1046. * no initrd image
  1047. */
  1048. bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
  1049. rd_len = rd_data = 0;
  1050. }
  1051. if (!rd_data) {
  1052. debug("## No init Ramdisk\n");
  1053. } else {
  1054. *rd_start = rd_data;
  1055. *rd_end = rd_data + rd_len;
  1056. }
  1057. debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  1058. *rd_start, *rd_end);
  1059. return 0;
  1060. }
  1061. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  1062. /**
  1063. * boot_ramdisk_high - relocate init ramdisk
  1064. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1065. * @rd_data: ramdisk data start address
  1066. * @rd_len: ramdisk data length
  1067. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  1068. * start address (after possible relocation)
  1069. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  1070. * end address (after possible relocation)
  1071. *
  1072. * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
  1073. * variable and if requested ramdisk data is moved to a specified location.
  1074. *
  1075. * Initrd_start and initrd_end are set to final (after relocation) ramdisk
  1076. * start/end addresses if ramdisk image start and len were provided,
  1077. * otherwise set initrd_start and initrd_end set to zeros.
  1078. *
  1079. * returns:
  1080. * 0 - success
  1081. * -1 - failure
  1082. */
  1083. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  1084. ulong *initrd_start, ulong *initrd_end)
  1085. {
  1086. char *s;
  1087. ulong initrd_high;
  1088. int initrd_copy_to_ram = 1;
  1089. s = env_get("initrd_high");
  1090. if (s) {
  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 = env_get_bootm_mapsize() + env_get_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 + env_get_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. env_get_bootm_mapsize() + env_get_bootm_low());
  1355. if (cmdline == NULL)
  1356. return -1;
  1357. s = env_get("bootargs");
  1358. if (!s)
  1359. s = "";
  1360. strcpy(cmdline, s);
  1361. *cmd_start = (ulong) & cmdline[0];
  1362. *cmd_end = *cmd_start + strlen(cmdline);
  1363. debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  1364. return 0;
  1365. }
  1366. #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
  1367. #ifdef CONFIG_SYS_BOOT_GET_KBD
  1368. /**
  1369. * boot_get_kbd - allocate and initialize kernel copy of board info
  1370. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1371. * @kbd: double pointer to board info data
  1372. *
  1373. * boot_get_kbd() allocates space for kernel copy of board info data below
  1374. * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
  1375. * with the current u-boot board info data.
  1376. *
  1377. * returns:
  1378. * 0 - success
  1379. * -1 - failure
  1380. */
  1381. int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
  1382. {
  1383. *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
  1384. env_get_bootm_mapsize() + env_get_bootm_low());
  1385. if (*kbd == NULL)
  1386. return -1;
  1387. **kbd = *(gd->bd);
  1388. debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  1389. #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  1390. do_bdinfo(NULL, 0, 0, NULL);
  1391. #endif
  1392. return 0;
  1393. }
  1394. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  1395. #ifdef CONFIG_LMB
  1396. int image_setup_linux(bootm_headers_t *images)
  1397. {
  1398. ulong of_size = images->ft_len;
  1399. char **of_flat_tree = &images->ft_addr;
  1400. struct lmb *lmb = &images->lmb;
  1401. int ret;
  1402. if (IMAGE_ENABLE_OF_LIBFDT)
  1403. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  1404. if (IMAGE_BOOT_GET_CMDLINE) {
  1405. ret = boot_get_cmdline(lmb, &images->cmdline_start,
  1406. &images->cmdline_end);
  1407. if (ret) {
  1408. puts("ERROR with allocation of cmdline\n");
  1409. return ret;
  1410. }
  1411. }
  1412. if (IMAGE_ENABLE_OF_LIBFDT) {
  1413. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  1414. if (ret)
  1415. return ret;
  1416. }
  1417. if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
  1418. ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
  1419. if (ret)
  1420. return ret;
  1421. }
  1422. return 0;
  1423. }
  1424. #endif /* CONFIG_LMB */
  1425. #endif /* !USE_HOSTCC */