image.c 41 KB

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