image.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. hdr = (const image_header_t *)img_addr;
  576. if (image_check_magic(hdr))
  577. format = IMAGE_FORMAT_LEGACY;
  578. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  579. else {
  580. if (fdt_check_header(img_addr) == 0)
  581. format = IMAGE_FORMAT_FIT;
  582. }
  583. #endif
  584. return format;
  585. }
  586. /**
  587. * genimg_get_image - get image from special storage (if necessary)
  588. * @img_addr: image start address
  589. *
  590. * genimg_get_image() checks if provided image start adddress is located
  591. * in a dataflash storage. If so, image is moved to a system RAM memory.
  592. *
  593. * returns:
  594. * image start address after possible relocation from special storage
  595. */
  596. ulong genimg_get_image(ulong img_addr)
  597. {
  598. ulong ram_addr = img_addr;
  599. #ifdef CONFIG_HAS_DATAFLASH
  600. ulong h_size, d_size;
  601. if (addr_dataflash(img_addr)) {
  602. void *buf;
  603. /* ger RAM address */
  604. ram_addr = CONFIG_SYS_LOAD_ADDR;
  605. /* get header size */
  606. h_size = image_get_header_size();
  607. #if defined(CONFIG_FIT)
  608. if (sizeof(struct fdt_header) > h_size)
  609. h_size = sizeof(struct fdt_header);
  610. #endif
  611. /* read in header */
  612. debug(" Reading image header from dataflash address "
  613. "%08lx to RAM address %08lx\n", img_addr, ram_addr);
  614. buf = map_sysmem(ram_addr, 0);
  615. read_dataflash(img_addr, h_size, buf);
  616. /* get data size */
  617. switch (genimg_get_format(buf)) {
  618. case IMAGE_FORMAT_LEGACY:
  619. d_size = image_get_data_size(buf);
  620. debug(" Legacy format image found at 0x%08lx, "
  621. "size 0x%08lx\n",
  622. ram_addr, d_size);
  623. break;
  624. #if defined(CONFIG_FIT)
  625. case IMAGE_FORMAT_FIT:
  626. d_size = fit_get_size(buf) - h_size;
  627. debug(" FIT/FDT format image found at 0x%08lx, "
  628. "size 0x%08lx\n",
  629. ram_addr, d_size);
  630. break;
  631. #endif
  632. default:
  633. printf(" No valid image found at 0x%08lx\n",
  634. img_addr);
  635. return ram_addr;
  636. }
  637. /* read in image data */
  638. debug(" Reading image remaining data from dataflash address "
  639. "%08lx to RAM address %08lx\n", img_addr + h_size,
  640. ram_addr + h_size);
  641. read_dataflash(img_addr + h_size, d_size,
  642. (char *)(buf + h_size));
  643. }
  644. #endif /* CONFIG_HAS_DATAFLASH */
  645. return ram_addr;
  646. }
  647. /**
  648. * fit_has_config - check if there is a valid FIT configuration
  649. * @images: pointer to the bootm command headers structure
  650. *
  651. * fit_has_config() checks if there is a FIT configuration in use
  652. * (if FTI support is present).
  653. *
  654. * returns:
  655. * 0, no FIT support or no configuration found
  656. * 1, configuration found
  657. */
  658. int genimg_has_config(bootm_headers_t *images)
  659. {
  660. #if defined(CONFIG_FIT)
  661. if (images->fit_uname_cfg)
  662. return 1;
  663. #endif
  664. return 0;
  665. }
  666. /**
  667. * boot_get_ramdisk - main ramdisk handling routine
  668. * @argc: command argument count
  669. * @argv: command argument list
  670. * @images: pointer to the bootm images structure
  671. * @arch: expected ramdisk architecture
  672. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  673. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  674. *
  675. * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
  676. * Curently supported are the following ramdisk sources:
  677. * - multicomponent kernel/ramdisk image,
  678. * - commandline provided address of decicated ramdisk image.
  679. *
  680. * returns:
  681. * 0, if ramdisk image was found and valid, or skiped
  682. * rd_start and rd_end are set to ramdisk start/end addresses if
  683. * ramdisk image is found and valid
  684. *
  685. * 1, if ramdisk image is found but corrupted, or invalid
  686. * rd_start and rd_end are set to 0 if no ramdisk exists
  687. */
  688. int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
  689. uint8_t arch, ulong *rd_start, ulong *rd_end)
  690. {
  691. ulong rd_addr, rd_load;
  692. ulong rd_data, rd_len;
  693. const image_header_t *rd_hdr;
  694. void *buf;
  695. #ifdef CONFIG_SUPPORT_RAW_INITRD
  696. char *end;
  697. #endif
  698. #if defined(CONFIG_FIT)
  699. const char *fit_uname_config = images->fit_uname_cfg;
  700. const char *fit_uname_ramdisk = NULL;
  701. ulong default_addr;
  702. int rd_noffset;
  703. #endif
  704. const char *select = NULL;
  705. *rd_start = 0;
  706. *rd_end = 0;
  707. if (argc >= 2)
  708. select = argv[1];
  709. /*
  710. * Look for a '-' which indicates to ignore the
  711. * ramdisk argument
  712. */
  713. if (select && strcmp(select, "-") == 0) {
  714. debug("## Skipping init Ramdisk\n");
  715. rd_len = rd_data = 0;
  716. } else if (select || genimg_has_config(images)) {
  717. #if defined(CONFIG_FIT)
  718. if (select) {
  719. /*
  720. * If the init ramdisk comes from the FIT image and
  721. * the FIT image address is omitted in the command
  722. * line argument, try to use os FIT image address or
  723. * default load address.
  724. */
  725. if (images->fit_uname_os)
  726. default_addr = (ulong)images->fit_hdr_os;
  727. else
  728. default_addr = load_addr;
  729. if (fit_parse_conf(select, default_addr,
  730. &rd_addr, &fit_uname_config)) {
  731. debug("* ramdisk: config '%s' from image at "
  732. "0x%08lx\n",
  733. fit_uname_config, rd_addr);
  734. } else if (fit_parse_subimage(select, default_addr,
  735. &rd_addr, &fit_uname_ramdisk)) {
  736. debug("* ramdisk: subimage '%s' from image at "
  737. "0x%08lx\n",
  738. fit_uname_ramdisk, rd_addr);
  739. } else
  740. #endif
  741. {
  742. rd_addr = simple_strtoul(select, NULL, 16);
  743. debug("* ramdisk: cmdline image address = "
  744. "0x%08lx\n",
  745. rd_addr);
  746. }
  747. #if defined(CONFIG_FIT)
  748. } else {
  749. /* use FIT configuration provided in first bootm
  750. * command argument. If the property is not defined,
  751. * quit silently.
  752. */
  753. rd_addr = map_to_sysmem(images->fit_hdr_os);
  754. rd_noffset = fit_get_node_from_config(images,
  755. FIT_RAMDISK_PROP, rd_addr);
  756. if (rd_noffset == -ENOLINK)
  757. return 0;
  758. else if (rd_noffset < 0)
  759. return 1;
  760. }
  761. #endif
  762. /* copy from dataflash if needed */
  763. rd_addr = genimg_get_image(rd_addr);
  764. /*
  765. * Check if there is an initrd image at the
  766. * address provided in the second bootm argument
  767. * check image type, for FIT images get FIT node.
  768. */
  769. buf = map_sysmem(rd_addr, 0);
  770. switch (genimg_get_format(buf)) {
  771. case IMAGE_FORMAT_LEGACY:
  772. printf("## Loading init Ramdisk from Legacy "
  773. "Image at %08lx ...\n", rd_addr);
  774. bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
  775. rd_hdr = image_get_ramdisk(rd_addr, arch,
  776. images->verify);
  777. if (rd_hdr == NULL)
  778. return 1;
  779. rd_data = image_get_data(rd_hdr);
  780. rd_len = image_get_data_size(rd_hdr);
  781. rd_load = image_get_load(rd_hdr);
  782. break;
  783. #if defined(CONFIG_FIT)
  784. case IMAGE_FORMAT_FIT:
  785. rd_noffset = fit_image_load(images, FIT_RAMDISK_PROP,
  786. rd_addr, &fit_uname_ramdisk,
  787. &fit_uname_config, arch,
  788. IH_TYPE_RAMDISK,
  789. BOOTSTAGE_ID_FIT_RD_START,
  790. FIT_LOAD_IGNORED, &rd_data, &rd_len);
  791. if (rd_noffset < 0)
  792. return 1;
  793. images->fit_hdr_rd = map_sysmem(rd_addr, 0);
  794. images->fit_uname_rd = fit_uname_ramdisk;
  795. images->fit_noffset_rd = rd_noffset;
  796. break;
  797. #endif
  798. default:
  799. #ifdef CONFIG_SUPPORT_RAW_INITRD
  800. end = NULL;
  801. if (select)
  802. end = strchr(select, ':');
  803. if (end) {
  804. rd_len = simple_strtoul(++end, NULL, 16);
  805. rd_data = rd_addr;
  806. } else
  807. #endif
  808. {
  809. puts("Wrong Ramdisk Image Format\n");
  810. rd_data = rd_len = rd_load = 0;
  811. return 1;
  812. }
  813. }
  814. } else if (images->legacy_hdr_valid &&
  815. image_check_type(&images->legacy_hdr_os_copy,
  816. IH_TYPE_MULTI)) {
  817. /*
  818. * Now check if we have a legacy mult-component image,
  819. * get second entry data start address and len.
  820. */
  821. bootstage_mark(BOOTSTAGE_ID_RAMDISK);
  822. printf("## Loading init Ramdisk from multi component "
  823. "Legacy Image at %08lx ...\n",
  824. (ulong)images->legacy_hdr_os);
  825. image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
  826. } else {
  827. /*
  828. * no initrd image
  829. */
  830. bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
  831. rd_len = rd_data = 0;
  832. }
  833. if (!rd_data) {
  834. debug("## No init Ramdisk\n");
  835. } else {
  836. *rd_start = rd_data;
  837. *rd_end = rd_data + rd_len;
  838. }
  839. debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  840. *rd_start, *rd_end);
  841. return 0;
  842. }
  843. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  844. /**
  845. * boot_ramdisk_high - relocate init ramdisk
  846. * @lmb: pointer to lmb handle, will be used for memory mgmt
  847. * @rd_data: ramdisk data start address
  848. * @rd_len: ramdisk data length
  849. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  850. * start address (after possible relocation)
  851. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  852. * end address (after possible relocation)
  853. *
  854. * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
  855. * variable and if requested ramdisk data is moved to a specified location.
  856. *
  857. * Initrd_start and initrd_end are set to final (after relocation) ramdisk
  858. * start/end addresses if ramdisk image start and len were provided,
  859. * otherwise set initrd_start and initrd_end set to zeros.
  860. *
  861. * returns:
  862. * 0 - success
  863. * -1 - failure
  864. */
  865. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  866. ulong *initrd_start, ulong *initrd_end)
  867. {
  868. char *s;
  869. ulong initrd_high;
  870. int initrd_copy_to_ram = 1;
  871. if ((s = getenv("initrd_high")) != NULL) {
  872. /* a value of "no" or a similar string will act like 0,
  873. * turning the "load high" feature off. This is intentional.
  874. */
  875. initrd_high = simple_strtoul(s, NULL, 16);
  876. if (initrd_high == ~0)
  877. initrd_copy_to_ram = 0;
  878. } else {
  879. /* not set, no restrictions to load high */
  880. initrd_high = ~0;
  881. }
  882. #ifdef CONFIG_LOGBUFFER
  883. /* Prevent initrd from overwriting logbuffer */
  884. lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
  885. #endif
  886. debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  887. initrd_high, initrd_copy_to_ram);
  888. if (rd_data) {
  889. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  890. debug(" in-place initrd\n");
  891. *initrd_start = rd_data;
  892. *initrd_end = rd_data + rd_len;
  893. lmb_reserve(lmb, rd_data, rd_len);
  894. } else {
  895. if (initrd_high)
  896. *initrd_start = (ulong)lmb_alloc_base(lmb,
  897. rd_len, 0x1000, initrd_high);
  898. else
  899. *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
  900. 0x1000);
  901. if (*initrd_start == 0) {
  902. puts("ramdisk - allocation error\n");
  903. goto error;
  904. }
  905. bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
  906. *initrd_end = *initrd_start + rd_len;
  907. printf(" Loading Ramdisk to %08lx, end %08lx ... ",
  908. *initrd_start, *initrd_end);
  909. memmove_wd((void *)*initrd_start,
  910. (void *)rd_data, rd_len, CHUNKSZ);
  911. #ifdef CONFIG_MP
  912. /*
  913. * Ensure the image is flushed to memory to handle
  914. * AMP boot scenarios in which we might not be
  915. * HW cache coherent
  916. */
  917. flush_cache((unsigned long)*initrd_start, rd_len);
  918. #endif
  919. puts("OK\n");
  920. }
  921. } else {
  922. *initrd_start = 0;
  923. *initrd_end = 0;
  924. }
  925. debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  926. *initrd_start, *initrd_end);
  927. return 0;
  928. error:
  929. return -1;
  930. }
  931. #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
  932. #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
  933. /**
  934. * boot_get_cmdline - allocate and initialize kernel cmdline
  935. * @lmb: pointer to lmb handle, will be used for memory mgmt
  936. * @cmd_start: pointer to a ulong variable, will hold cmdline start
  937. * @cmd_end: pointer to a ulong variable, will hold cmdline end
  938. *
  939. * boot_get_cmdline() allocates space for kernel command line below
  940. * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
  941. * variable is present its contents is copied to allocated kernel
  942. * command line.
  943. *
  944. * returns:
  945. * 0 - success
  946. * -1 - failure
  947. */
  948. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
  949. {
  950. char *cmdline;
  951. char *s;
  952. cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
  953. getenv_bootm_mapsize() + getenv_bootm_low());
  954. if (cmdline == NULL)
  955. return -1;
  956. if ((s = getenv("bootargs")) == NULL)
  957. s = "";
  958. strcpy(cmdline, s);
  959. *cmd_start = (ulong) & cmdline[0];
  960. *cmd_end = *cmd_start + strlen(cmdline);
  961. debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  962. return 0;
  963. }
  964. #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
  965. #ifdef CONFIG_SYS_BOOT_GET_KBD
  966. /**
  967. * boot_get_kbd - allocate and initialize kernel copy of board info
  968. * @lmb: pointer to lmb handle, will be used for memory mgmt
  969. * @kbd: double pointer to board info data
  970. *
  971. * boot_get_kbd() allocates space for kernel copy of board info data below
  972. * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
  973. * with the current u-boot board info data.
  974. *
  975. * returns:
  976. * 0 - success
  977. * -1 - failure
  978. */
  979. int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
  980. {
  981. *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
  982. getenv_bootm_mapsize() + getenv_bootm_low());
  983. if (*kbd == NULL)
  984. return -1;
  985. **kbd = *(gd->bd);
  986. debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  987. #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  988. do_bdinfo(NULL, 0, 0, NULL);
  989. #endif
  990. return 0;
  991. }
  992. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  993. #ifdef CONFIG_LMB
  994. int image_setup_linux(bootm_headers_t *images)
  995. {
  996. ulong of_size = images->ft_len;
  997. char **of_flat_tree = &images->ft_addr;
  998. ulong *initrd_start = &images->initrd_start;
  999. ulong *initrd_end = &images->initrd_end;
  1000. struct lmb *lmb = &images->lmb;
  1001. ulong rd_len;
  1002. int ret;
  1003. if (IMAGE_ENABLE_OF_LIBFDT)
  1004. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  1005. if (IMAGE_BOOT_GET_CMDLINE) {
  1006. ret = boot_get_cmdline(lmb, &images->cmdline_start,
  1007. &images->cmdline_end);
  1008. if (ret) {
  1009. puts("ERROR with allocation of cmdline\n");
  1010. return ret;
  1011. }
  1012. }
  1013. if (IMAGE_ENABLE_RAMDISK_HIGH) {
  1014. rd_len = images->rd_end - images->rd_start;
  1015. ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
  1016. initrd_start, initrd_end);
  1017. if (ret)
  1018. return ret;
  1019. }
  1020. if (IMAGE_ENABLE_OF_LIBFDT) {
  1021. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  1022. if (ret)
  1023. return ret;
  1024. }
  1025. if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
  1026. ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
  1027. if (ret)
  1028. return ret;
  1029. }
  1030. return 0;
  1031. }
  1032. #endif /* CONFIG_LMB */
  1033. #endif /* !USE_HOSTCC */