image.c 31 KB

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