image.c 41 KB

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