image.c 36 KB

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