image.c 40 KB

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