image.c 31 KB

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