image.c 40 KB

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