image.c 42 KB

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