image.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. #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, size;
  402. phys_addr_t start;
  403. char *s = getenv("bootm_size");
  404. if (s) {
  405. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  406. return tmp;
  407. }
  408. #if defined(CONFIG_ARM) && defined(CONFIG_NR_DRAM_BANKS)
  409. start = gd->bd->bi_dram[0].start;
  410. size = gd->bd->bi_dram[0].size;
  411. #else
  412. start = gd->bd->bi_memstart;
  413. size = gd->bd->bi_memsize;
  414. #endif
  415. s = getenv("bootm_low");
  416. if (s)
  417. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  418. else
  419. tmp = start;
  420. return size - (tmp - start);
  421. }
  422. phys_size_t getenv_bootm_mapsize(void)
  423. {
  424. phys_size_t tmp;
  425. char *s = getenv("bootm_mapsize");
  426. if (s) {
  427. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  428. return tmp;
  429. }
  430. #if defined(CONFIG_SYS_BOOTMAPSZ)
  431. return CONFIG_SYS_BOOTMAPSZ;
  432. #else
  433. return getenv_bootm_size();
  434. #endif
  435. }
  436. void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  437. {
  438. if (to == from)
  439. return;
  440. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  441. if (to > from) {
  442. from += len;
  443. to += len;
  444. }
  445. while (len > 0) {
  446. size_t tail = (len > chunksz) ? chunksz : len;
  447. WATCHDOG_RESET();
  448. if (to > from) {
  449. to -= tail;
  450. from -= tail;
  451. }
  452. memmove(to, from, tail);
  453. if (to < from) {
  454. to += tail;
  455. from += tail;
  456. }
  457. len -= tail;
  458. }
  459. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  460. memmove(to, from, len);
  461. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  462. }
  463. #endif /* !USE_HOSTCC */
  464. void genimg_print_size(uint32_t size)
  465. {
  466. #ifndef USE_HOSTCC
  467. printf("%d Bytes = ", size);
  468. print_size(size, "\n");
  469. #else
  470. printf("%d Bytes = %.2f kB = %.2f MB\n",
  471. size, (double)size / 1.024e3,
  472. (double)size / 1.048576e6);
  473. #endif
  474. }
  475. #if IMAGE_ENABLE_TIMESTAMP
  476. void genimg_print_time(time_t timestamp)
  477. {
  478. #ifndef USE_HOSTCC
  479. struct rtc_time tm;
  480. rtc_to_tm(timestamp, &tm);
  481. printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
  482. tm.tm_year, tm.tm_mon, tm.tm_mday,
  483. tm.tm_hour, tm.tm_min, tm.tm_sec);
  484. #else
  485. printf("%s", ctime(&timestamp));
  486. #endif
  487. }
  488. #endif
  489. const table_entry_t *get_table_entry(const table_entry_t *table, int id)
  490. {
  491. for (; table->id >= 0; ++table) {
  492. if (table->id == id)
  493. return table;
  494. }
  495. return NULL;
  496. }
  497. /**
  498. * get_table_entry_name - translate entry id to long name
  499. * @table: pointer to a translation table for entries of a specific type
  500. * @msg: message to be returned when translation fails
  501. * @id: entry id to be translated
  502. *
  503. * get_table_entry_name() will go over translation table trying to find
  504. * entry that matches given id. If matching entry is found, its long
  505. * name is returned to the caller.
  506. *
  507. * returns:
  508. * long entry name if translation succeeds
  509. * msg otherwise
  510. */
  511. char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
  512. {
  513. table = get_table_entry(table, id);
  514. if (!table)
  515. return msg;
  516. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  517. return table->lname;
  518. #else
  519. return table->lname + gd->reloc_off;
  520. #endif
  521. }
  522. const char *genimg_get_os_name(uint8_t os)
  523. {
  524. return (get_table_entry_name(uimage_os, "Unknown OS", os));
  525. }
  526. const char *genimg_get_arch_name(uint8_t arch)
  527. {
  528. return (get_table_entry_name(uimage_arch, "Unknown Architecture",
  529. arch));
  530. }
  531. const char *genimg_get_type_name(uint8_t type)
  532. {
  533. return (get_table_entry_name(uimage_type, "Unknown Image", type));
  534. }
  535. static const char *genimg_get_short_name(const table_entry_t *table, int val)
  536. {
  537. table = get_table_entry(table, val);
  538. if (!table)
  539. return "unknown";
  540. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  541. return table->sname;
  542. #else
  543. return table->sname + gd->reloc_off;
  544. #endif
  545. }
  546. const char *genimg_get_type_short_name(uint8_t type)
  547. {
  548. return genimg_get_short_name(uimage_type, type);
  549. }
  550. const char *genimg_get_comp_name(uint8_t comp)
  551. {
  552. return (get_table_entry_name(uimage_comp, "Unknown Compression",
  553. comp));
  554. }
  555. const char *genimg_get_comp_short_name(uint8_t comp)
  556. {
  557. return genimg_get_short_name(uimage_comp, comp);
  558. }
  559. const char *genimg_get_os_short_name(uint8_t os)
  560. {
  561. return genimg_get_short_name(uimage_os, os);
  562. }
  563. const char *genimg_get_arch_short_name(uint8_t arch)
  564. {
  565. return genimg_get_short_name(uimage_arch, arch);
  566. }
  567. /**
  568. * get_table_entry_id - translate short entry name to id
  569. * @table: pointer to a translation table for entries of a specific type
  570. * @table_name: to be used in case of error
  571. * @name: entry short name to be translated
  572. *
  573. * get_table_entry_id() will go over translation table trying to find
  574. * entry that matches given short name. If matching entry is found,
  575. * its id returned to the caller.
  576. *
  577. * returns:
  578. * entry id if translation succeeds
  579. * -1 otherwise
  580. */
  581. int get_table_entry_id(const table_entry_t *table,
  582. const char *table_name, const char *name)
  583. {
  584. const table_entry_t *t;
  585. for (t = table; t->id >= 0; ++t) {
  586. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  587. if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
  588. #else
  589. if (t->sname && strcasecmp(t->sname, name) == 0)
  590. #endif
  591. return (t->id);
  592. }
  593. debug("Invalid %s Type: %s\n", table_name, name);
  594. return -1;
  595. }
  596. int genimg_get_os_id(const char *name)
  597. {
  598. return (get_table_entry_id(uimage_os, "OS", name));
  599. }
  600. int genimg_get_arch_id(const char *name)
  601. {
  602. return (get_table_entry_id(uimage_arch, "CPU", name));
  603. }
  604. int genimg_get_type_id(const char *name)
  605. {
  606. return (get_table_entry_id(uimage_type, "Image", name));
  607. }
  608. int genimg_get_comp_id(const char *name)
  609. {
  610. return (get_table_entry_id(uimage_comp, "Compression", name));
  611. }
  612. #ifndef USE_HOSTCC
  613. /**
  614. * genimg_get_kernel_addr_fit - get the real kernel address and return 2
  615. * FIT strings
  616. * @img_addr: a string might contain real image address
  617. * @fit_uname_config: double pointer to a char, will hold pointer to a
  618. * configuration unit name
  619. * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
  620. * name
  621. *
  622. * genimg_get_kernel_addr_fit get the real kernel start address from a string
  623. * which is normally the first argv of bootm/bootz
  624. *
  625. * returns:
  626. * kernel start address
  627. */
  628. ulong genimg_get_kernel_addr_fit(char * const img_addr,
  629. const char **fit_uname_config,
  630. const char **fit_uname_kernel)
  631. {
  632. ulong kernel_addr;
  633. /* find out kernel image address */
  634. if (!img_addr) {
  635. kernel_addr = load_addr;
  636. debug("* kernel: default image load address = 0x%08lx\n",
  637. load_addr);
  638. #if CONFIG_IS_ENABLED(FIT)
  639. } else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
  640. fit_uname_config)) {
  641. debug("* kernel: config '%s' from image at 0x%08lx\n",
  642. *fit_uname_config, kernel_addr);
  643. } else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
  644. fit_uname_kernel)) {
  645. debug("* kernel: subimage '%s' from image at 0x%08lx\n",
  646. *fit_uname_kernel, kernel_addr);
  647. #endif
  648. } else {
  649. kernel_addr = simple_strtoul(img_addr, NULL, 16);
  650. debug("* kernel: cmdline image address = 0x%08lx\n",
  651. kernel_addr);
  652. }
  653. return kernel_addr;
  654. }
  655. /**
  656. * genimg_get_kernel_addr() is the simple version of
  657. * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
  658. */
  659. ulong genimg_get_kernel_addr(char * const img_addr)
  660. {
  661. const char *fit_uname_config = NULL;
  662. const char *fit_uname_kernel = NULL;
  663. return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
  664. &fit_uname_kernel);
  665. }
  666. /**
  667. * genimg_get_format - get image format type
  668. * @img_addr: image start address
  669. *
  670. * genimg_get_format() checks whether provided address points to a valid
  671. * legacy or FIT image.
  672. *
  673. * New uImage format and FDT blob are based on a libfdt. FDT blob
  674. * may be passed directly or embedded in a FIT image. In both situations
  675. * genimg_get_format() must be able to dectect libfdt header.
  676. *
  677. * returns:
  678. * image format type or IMAGE_FORMAT_INVALID if no image is present
  679. */
  680. int genimg_get_format(const void *img_addr)
  681. {
  682. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  683. const image_header_t *hdr;
  684. hdr = (const image_header_t *)img_addr;
  685. if (image_check_magic(hdr))
  686. return IMAGE_FORMAT_LEGACY;
  687. #endif
  688. #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
  689. if (fdt_check_header(img_addr) == 0)
  690. return IMAGE_FORMAT_FIT;
  691. #endif
  692. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  693. if (android_image_check_header(img_addr) == 0)
  694. return IMAGE_FORMAT_ANDROID;
  695. #endif
  696. return IMAGE_FORMAT_INVALID;
  697. }
  698. /**
  699. * genimg_get_image - get image from special storage (if necessary)
  700. * @img_addr: image start address
  701. *
  702. * genimg_get_image() checks if provided image start address is located
  703. * in a dataflash storage. If so, image is moved to a system RAM memory.
  704. *
  705. * returns:
  706. * image start address after possible relocation from special storage
  707. */
  708. ulong genimg_get_image(ulong img_addr)
  709. {
  710. ulong ram_addr = img_addr;
  711. #ifdef CONFIG_HAS_DATAFLASH
  712. ulong h_size, d_size;
  713. if (addr_dataflash(img_addr)) {
  714. void *buf;
  715. /* ger RAM address */
  716. ram_addr = CONFIG_SYS_LOAD_ADDR;
  717. /* get header size */
  718. h_size = image_get_header_size();
  719. #if IMAGE_ENABLE_FIT
  720. if (sizeof(struct fdt_header) > h_size)
  721. h_size = sizeof(struct fdt_header);
  722. #endif
  723. /* read in header */
  724. debug(" Reading image header from dataflash address "
  725. "%08lx to RAM address %08lx\n", img_addr, ram_addr);
  726. buf = map_sysmem(ram_addr, 0);
  727. read_dataflash(img_addr, h_size, buf);
  728. /* get data size */
  729. switch (genimg_get_format(buf)) {
  730. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  731. case IMAGE_FORMAT_LEGACY:
  732. d_size = image_get_data_size(buf);
  733. debug(" Legacy format image found at 0x%08lx, "
  734. "size 0x%08lx\n",
  735. ram_addr, d_size);
  736. break;
  737. #endif
  738. #if IMAGE_ENABLE_FIT
  739. case IMAGE_FORMAT_FIT:
  740. d_size = fit_get_size(buf) - h_size;
  741. debug(" FIT/FDT format image found at 0x%08lx, "
  742. "size 0x%08lx\n",
  743. ram_addr, d_size);
  744. break;
  745. #endif
  746. default:
  747. printf(" No valid image found at 0x%08lx\n",
  748. img_addr);
  749. return ram_addr;
  750. }
  751. /* read in image data */
  752. debug(" Reading image remaining data from dataflash address "
  753. "%08lx to RAM address %08lx\n", img_addr + h_size,
  754. ram_addr + h_size);
  755. read_dataflash(img_addr + h_size, d_size,
  756. (char *)(buf + h_size));
  757. }
  758. #endif /* CONFIG_HAS_DATAFLASH */
  759. return ram_addr;
  760. }
  761. /**
  762. * fit_has_config - check if there is a valid FIT configuration
  763. * @images: pointer to the bootm command headers structure
  764. *
  765. * fit_has_config() checks if there is a FIT configuration in use
  766. * (if FTI support is present).
  767. *
  768. * returns:
  769. * 0, no FIT support or no configuration found
  770. * 1, configuration found
  771. */
  772. int genimg_has_config(bootm_headers_t *images)
  773. {
  774. #if IMAGE_ENABLE_FIT
  775. if (images->fit_uname_cfg)
  776. return 1;
  777. #endif
  778. return 0;
  779. }
  780. /**
  781. * boot_get_ramdisk - main ramdisk handling routine
  782. * @argc: command argument count
  783. * @argv: command argument list
  784. * @images: pointer to the bootm images structure
  785. * @arch: expected ramdisk architecture
  786. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  787. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  788. *
  789. * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
  790. * Curently supported are the following ramdisk sources:
  791. * - multicomponent kernel/ramdisk image,
  792. * - commandline provided address of decicated ramdisk image.
  793. *
  794. * returns:
  795. * 0, if ramdisk image was found and valid, or skiped
  796. * rd_start and rd_end are set to ramdisk start/end addresses if
  797. * ramdisk image is found and valid
  798. *
  799. * 1, if ramdisk image is found but corrupted, or invalid
  800. * rd_start and rd_end are set to 0 if no ramdisk exists
  801. */
  802. int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
  803. uint8_t arch, ulong *rd_start, ulong *rd_end)
  804. {
  805. ulong rd_addr, rd_load;
  806. ulong rd_data, rd_len;
  807. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  808. const image_header_t *rd_hdr;
  809. #endif
  810. void *buf;
  811. #ifdef CONFIG_SUPPORT_RAW_INITRD
  812. char *end;
  813. #endif
  814. #if IMAGE_ENABLE_FIT
  815. const char *fit_uname_config = images->fit_uname_cfg;
  816. const char *fit_uname_ramdisk = NULL;
  817. ulong default_addr;
  818. int rd_noffset;
  819. #endif
  820. const char *select = NULL;
  821. *rd_start = 0;
  822. *rd_end = 0;
  823. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  824. /*
  825. * Look for an Android boot image.
  826. */
  827. buf = map_sysmem(images->os.start, 0);
  828. if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
  829. select = argv[0];
  830. #endif
  831. if (argc >= 2)
  832. select = argv[1];
  833. /*
  834. * Look for a '-' which indicates to ignore the
  835. * ramdisk argument
  836. */
  837. if (select && strcmp(select, "-") == 0) {
  838. debug("## Skipping init Ramdisk\n");
  839. rd_len = rd_data = 0;
  840. } else if (select || genimg_has_config(images)) {
  841. #if IMAGE_ENABLE_FIT
  842. if (select) {
  843. /*
  844. * If the init ramdisk comes from the FIT image and
  845. * the FIT image address is omitted in the command
  846. * line argument, try to use os FIT image address or
  847. * default load address.
  848. */
  849. if (images->fit_uname_os)
  850. default_addr = (ulong)images->fit_hdr_os;
  851. else
  852. default_addr = load_addr;
  853. if (fit_parse_conf(select, default_addr,
  854. &rd_addr, &fit_uname_config)) {
  855. debug("* ramdisk: config '%s' from image at "
  856. "0x%08lx\n",
  857. fit_uname_config, rd_addr);
  858. } else if (fit_parse_subimage(select, default_addr,
  859. &rd_addr, &fit_uname_ramdisk)) {
  860. debug("* ramdisk: subimage '%s' from image at "
  861. "0x%08lx\n",
  862. fit_uname_ramdisk, rd_addr);
  863. } else
  864. #endif
  865. {
  866. rd_addr = simple_strtoul(select, NULL, 16);
  867. debug("* ramdisk: cmdline image address = "
  868. "0x%08lx\n",
  869. rd_addr);
  870. }
  871. #if IMAGE_ENABLE_FIT
  872. } else {
  873. /* use FIT configuration provided in first bootm
  874. * command argument. If the property is not defined,
  875. * quit silently.
  876. */
  877. rd_addr = map_to_sysmem(images->fit_hdr_os);
  878. rd_noffset = fit_get_node_from_config(images,
  879. FIT_RAMDISK_PROP, rd_addr);
  880. if (rd_noffset == -ENOLINK)
  881. return 0;
  882. else if (rd_noffset < 0)
  883. return 1;
  884. }
  885. #endif
  886. /* copy from dataflash if needed */
  887. rd_addr = genimg_get_image(rd_addr);
  888. /*
  889. * Check if there is an initrd image at the
  890. * address provided in the second bootm argument
  891. * check image type, for FIT images get FIT node.
  892. */
  893. buf = map_sysmem(rd_addr, 0);
  894. switch (genimg_get_format(buf)) {
  895. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  896. case IMAGE_FORMAT_LEGACY:
  897. printf("## Loading init Ramdisk from Legacy "
  898. "Image at %08lx ...\n", rd_addr);
  899. bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
  900. rd_hdr = image_get_ramdisk(rd_addr, arch,
  901. images->verify);
  902. if (rd_hdr == NULL)
  903. return 1;
  904. rd_data = image_get_data(rd_hdr);
  905. rd_len = image_get_data_size(rd_hdr);
  906. rd_load = image_get_load(rd_hdr);
  907. break;
  908. #endif
  909. #if IMAGE_ENABLE_FIT
  910. case IMAGE_FORMAT_FIT:
  911. rd_noffset = fit_image_load(images,
  912. rd_addr, &fit_uname_ramdisk,
  913. &fit_uname_config, arch,
  914. IH_TYPE_RAMDISK,
  915. BOOTSTAGE_ID_FIT_RD_START,
  916. FIT_LOAD_OPTIONAL_NON_ZERO,
  917. &rd_data, &rd_len);
  918. if (rd_noffset < 0)
  919. return 1;
  920. images->fit_hdr_rd = map_sysmem(rd_addr, 0);
  921. images->fit_uname_rd = fit_uname_ramdisk;
  922. images->fit_noffset_rd = rd_noffset;
  923. break;
  924. #endif
  925. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  926. case IMAGE_FORMAT_ANDROID:
  927. android_image_get_ramdisk((void *)images->os.start,
  928. &rd_data, &rd_len);
  929. break;
  930. #endif
  931. default:
  932. #ifdef CONFIG_SUPPORT_RAW_INITRD
  933. end = NULL;
  934. if (select)
  935. end = strchr(select, ':');
  936. if (end) {
  937. rd_len = simple_strtoul(++end, NULL, 16);
  938. rd_data = rd_addr;
  939. } else
  940. #endif
  941. {
  942. puts("Wrong Ramdisk Image Format\n");
  943. rd_data = rd_len = rd_load = 0;
  944. return 1;
  945. }
  946. }
  947. } else if (images->legacy_hdr_valid &&
  948. image_check_type(&images->legacy_hdr_os_copy,
  949. IH_TYPE_MULTI)) {
  950. /*
  951. * Now check if we have a legacy mult-component image,
  952. * get second entry data start address and len.
  953. */
  954. bootstage_mark(BOOTSTAGE_ID_RAMDISK);
  955. printf("## Loading init Ramdisk from multi component "
  956. "Legacy Image at %08lx ...\n",
  957. (ulong)images->legacy_hdr_os);
  958. image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
  959. } else {
  960. /*
  961. * no initrd image
  962. */
  963. bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
  964. rd_len = rd_data = 0;
  965. }
  966. if (!rd_data) {
  967. debug("## No init Ramdisk\n");
  968. } else {
  969. *rd_start = rd_data;
  970. *rd_end = rd_data + rd_len;
  971. }
  972. debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  973. *rd_start, *rd_end);
  974. return 0;
  975. }
  976. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  977. /**
  978. * boot_ramdisk_high - relocate init ramdisk
  979. * @lmb: pointer to lmb handle, will be used for memory mgmt
  980. * @rd_data: ramdisk data start address
  981. * @rd_len: ramdisk data length
  982. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  983. * start address (after possible relocation)
  984. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  985. * end address (after possible relocation)
  986. *
  987. * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
  988. * variable and if requested ramdisk data is moved to a specified location.
  989. *
  990. * Initrd_start and initrd_end are set to final (after relocation) ramdisk
  991. * start/end addresses if ramdisk image start and len were provided,
  992. * otherwise set initrd_start and initrd_end set to zeros.
  993. *
  994. * returns:
  995. * 0 - success
  996. * -1 - failure
  997. */
  998. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  999. ulong *initrd_start, ulong *initrd_end)
  1000. {
  1001. char *s;
  1002. ulong initrd_high;
  1003. int initrd_copy_to_ram = 1;
  1004. if ((s = getenv("initrd_high")) != NULL) {
  1005. /* a value of "no" or a similar string will act like 0,
  1006. * turning the "load high" feature off. This is intentional.
  1007. */
  1008. initrd_high = simple_strtoul(s, NULL, 16);
  1009. if (initrd_high == ~0)
  1010. initrd_copy_to_ram = 0;
  1011. } else {
  1012. initrd_high = getenv_bootm_mapsize() + getenv_bootm_low();
  1013. }
  1014. #ifdef CONFIG_LOGBUFFER
  1015. /* Prevent initrd from overwriting logbuffer */
  1016. lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
  1017. #endif
  1018. debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  1019. initrd_high, initrd_copy_to_ram);
  1020. if (rd_data) {
  1021. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  1022. debug(" in-place initrd\n");
  1023. *initrd_start = rd_data;
  1024. *initrd_end = rd_data + rd_len;
  1025. lmb_reserve(lmb, rd_data, rd_len);
  1026. } else {
  1027. if (initrd_high)
  1028. *initrd_start = (ulong)lmb_alloc_base(lmb,
  1029. rd_len, 0x1000, initrd_high);
  1030. else
  1031. *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
  1032. 0x1000);
  1033. if (*initrd_start == 0) {
  1034. puts("ramdisk - allocation error\n");
  1035. goto error;
  1036. }
  1037. bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
  1038. *initrd_end = *initrd_start + rd_len;
  1039. printf(" Loading Ramdisk to %08lx, end %08lx ... ",
  1040. *initrd_start, *initrd_end);
  1041. memmove_wd((void *)*initrd_start,
  1042. (void *)rd_data, rd_len, CHUNKSZ);
  1043. #ifdef CONFIG_MP
  1044. /*
  1045. * Ensure the image is flushed to memory to handle
  1046. * AMP boot scenarios in which we might not be
  1047. * HW cache coherent
  1048. */
  1049. flush_cache((unsigned long)*initrd_start, rd_len);
  1050. #endif
  1051. puts("OK\n");
  1052. }
  1053. } else {
  1054. *initrd_start = 0;
  1055. *initrd_end = 0;
  1056. }
  1057. debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  1058. *initrd_start, *initrd_end);
  1059. return 0;
  1060. error:
  1061. return -1;
  1062. }
  1063. #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
  1064. int boot_get_setup(bootm_headers_t *images, uint8_t arch,
  1065. ulong *setup_start, ulong *setup_len)
  1066. {
  1067. #if IMAGE_ENABLE_FIT
  1068. return boot_get_setup_fit(images, arch, setup_start, setup_len);
  1069. #else
  1070. return -ENOENT;
  1071. #endif
  1072. }
  1073. #if IMAGE_ENABLE_FIT
  1074. int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
  1075. uint8_t arch, const ulong *ld_start, ulong * const ld_len)
  1076. {
  1077. /*
  1078. * These variables are used to hold the current image location
  1079. * in system memory.
  1080. */
  1081. ulong tmp_img_addr;
  1082. /*
  1083. * These two variables are requirements for fit_image_load, but
  1084. * their values are not used
  1085. */
  1086. ulong img_data, img_len;
  1087. void *buf;
  1088. int loadables_index;
  1089. int conf_noffset;
  1090. int fit_img_result;
  1091. char *uname;
  1092. /* Check to see if the images struct has a FIT configuration */
  1093. if (!genimg_has_config(images)) {
  1094. debug("## FIT configuration was not specified\n");
  1095. return 0;
  1096. }
  1097. /*
  1098. * Obtain the os FIT header from the images struct
  1099. * copy from dataflash if needed
  1100. */
  1101. tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
  1102. tmp_img_addr = genimg_get_image(tmp_img_addr);
  1103. buf = map_sysmem(tmp_img_addr, 0);
  1104. /*
  1105. * Check image type. For FIT images get FIT node
  1106. * and attempt to locate a generic binary.
  1107. */
  1108. switch (genimg_get_format(buf)) {
  1109. case IMAGE_FORMAT_FIT:
  1110. conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
  1111. for (loadables_index = 0;
  1112. fdt_get_string_index(buf, conf_noffset,
  1113. FIT_LOADABLE_PROP,
  1114. loadables_index,
  1115. (const char **)&uname) == 0;
  1116. loadables_index++)
  1117. {
  1118. fit_img_result = fit_image_load(images,
  1119. tmp_img_addr,
  1120. (const char **)&uname,
  1121. &(images->fit_uname_cfg), arch,
  1122. IH_TYPE_LOADABLE,
  1123. BOOTSTAGE_ID_FIT_LOADABLE_START,
  1124. FIT_LOAD_OPTIONAL_NON_ZERO,
  1125. &img_data, &img_len);
  1126. if (fit_img_result < 0) {
  1127. /* Something went wrong! */
  1128. return fit_img_result;
  1129. }
  1130. }
  1131. break;
  1132. default:
  1133. printf("The given image format is not supported (corrupt?)\n");
  1134. return 1;
  1135. }
  1136. return 0;
  1137. }
  1138. #endif
  1139. #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
  1140. /**
  1141. * boot_get_cmdline - allocate and initialize kernel cmdline
  1142. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1143. * @cmd_start: pointer to a ulong variable, will hold cmdline start
  1144. * @cmd_end: pointer to a ulong variable, will hold cmdline end
  1145. *
  1146. * boot_get_cmdline() allocates space for kernel command line below
  1147. * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-Boot environemnt
  1148. * variable is present its contents is copied to allocated kernel
  1149. * command line.
  1150. *
  1151. * returns:
  1152. * 0 - success
  1153. * -1 - failure
  1154. */
  1155. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
  1156. {
  1157. char *cmdline;
  1158. char *s;
  1159. cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
  1160. getenv_bootm_mapsize() + getenv_bootm_low());
  1161. if (cmdline == NULL)
  1162. return -1;
  1163. if ((s = getenv("bootargs")) == NULL)
  1164. s = "";
  1165. strcpy(cmdline, s);
  1166. *cmd_start = (ulong) & cmdline[0];
  1167. *cmd_end = *cmd_start + strlen(cmdline);
  1168. debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  1169. return 0;
  1170. }
  1171. #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
  1172. #ifdef CONFIG_SYS_BOOT_GET_KBD
  1173. /**
  1174. * boot_get_kbd - allocate and initialize kernel copy of board info
  1175. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1176. * @kbd: double pointer to board info data
  1177. *
  1178. * boot_get_kbd() allocates space for kernel copy of board info data below
  1179. * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
  1180. * with the current u-boot board info data.
  1181. *
  1182. * returns:
  1183. * 0 - success
  1184. * -1 - failure
  1185. */
  1186. int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
  1187. {
  1188. *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
  1189. getenv_bootm_mapsize() + getenv_bootm_low());
  1190. if (*kbd == NULL)
  1191. return -1;
  1192. **kbd = *(gd->bd);
  1193. debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  1194. #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  1195. do_bdinfo(NULL, 0, 0, NULL);
  1196. #endif
  1197. return 0;
  1198. }
  1199. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  1200. #ifdef CONFIG_LMB
  1201. int image_setup_linux(bootm_headers_t *images)
  1202. {
  1203. ulong of_size = images->ft_len;
  1204. char **of_flat_tree = &images->ft_addr;
  1205. ulong *initrd_start = &images->initrd_start;
  1206. ulong *initrd_end = &images->initrd_end;
  1207. struct lmb *lmb = &images->lmb;
  1208. ulong rd_len;
  1209. int ret;
  1210. if (IMAGE_ENABLE_OF_LIBFDT)
  1211. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  1212. if (IMAGE_BOOT_GET_CMDLINE) {
  1213. ret = boot_get_cmdline(lmb, &images->cmdline_start,
  1214. &images->cmdline_end);
  1215. if (ret) {
  1216. puts("ERROR with allocation of cmdline\n");
  1217. return ret;
  1218. }
  1219. }
  1220. if (IMAGE_ENABLE_RAMDISK_HIGH) {
  1221. rd_len = images->rd_end - images->rd_start;
  1222. ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
  1223. initrd_start, initrd_end);
  1224. if (ret)
  1225. return ret;
  1226. }
  1227. if (IMAGE_ENABLE_OF_LIBFDT) {
  1228. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  1229. if (ret)
  1230. return ret;
  1231. }
  1232. if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
  1233. ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
  1234. if (ret)
  1235. return ret;
  1236. }
  1237. return 0;
  1238. }
  1239. #endif /* CONFIG_LMB */
  1240. #endif /* !USE_HOSTCC */