image.c 42 KB

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