image.c 31 KB

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