image.c 39 KB

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