image.c 44 KB

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