image.c 41 KB

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