image.c 42 KB

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