image.c 40 KB

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