image.c 43 KB

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