image.c 42 KB

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