image.c 31 KB

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