image.c 31 KB

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