image.c 36 KB

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