image.c 43 KB

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