image.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2006
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #ifndef USE_HOSTCC
  26. #include <common.h>
  27. #include <watchdog.h>
  28. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  29. #include <status_led.h>
  30. #endif
  31. #ifdef CONFIG_HAS_DATAFLASH
  32. #include <dataflash.h>
  33. #endif
  34. #ifdef CONFIG_LOGBUFFER
  35. #include <logbuff.h>
  36. #endif
  37. #include <rtc.h>
  38. #include <environment.h>
  39. #include <image.h>
  40. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  41. #include <libfdt.h>
  42. #include <fdt_support.h>
  43. #endif
  44. #include <u-boot/md5.h>
  45. #include <sha1.h>
  46. #ifdef CONFIG_CMD_BDI
  47. extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  48. #endif
  49. DECLARE_GLOBAL_DATA_PTR;
  50. static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
  51. int verify);
  52. #else
  53. #include "mkimage.h"
  54. #include <u-boot/md5.h>
  55. #include <time.h>
  56. #include <image.h>
  57. #endif /* !USE_HOSTCC*/
  58. #include <u-boot/crc.h>
  59. static const table_entry_t uimage_arch[] = {
  60. { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
  61. { IH_ARCH_ALPHA, "alpha", "Alpha", },
  62. { IH_ARCH_ARM, "arm", "ARM", },
  63. { IH_ARCH_I386, "x86", "Intel x86", },
  64. { IH_ARCH_IA64, "ia64", "IA64", },
  65. { IH_ARCH_M68K, "m68k", "M68K", },
  66. { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
  67. { IH_ARCH_MIPS, "mips", "MIPS", },
  68. { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
  69. { IH_ARCH_NIOS2, "nios2", "NIOS II", },
  70. { IH_ARCH_PPC, "powerpc", "PowerPC", },
  71. { IH_ARCH_PPC, "ppc", "PowerPC", },
  72. { IH_ARCH_S390, "s390", "IBM S390", },
  73. { IH_ARCH_SH, "sh", "SuperH", },
  74. { IH_ARCH_SPARC, "sparc", "SPARC", },
  75. { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
  76. { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
  77. { IH_ARCH_AVR32, "avr32", "AVR32", },
  78. { IH_ARCH_NDS32, "nds32", "NDS32", },
  79. { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
  80. { -1, "", "", },
  81. };
  82. static const table_entry_t uimage_os[] = {
  83. { IH_OS_INVALID, NULL, "Invalid OS", },
  84. { IH_OS_LINUX, "linux", "Linux", },
  85. #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
  86. { IH_OS_LYNXOS, "lynxos", "LynxOS", },
  87. #endif
  88. { IH_OS_NETBSD, "netbsd", "NetBSD", },
  89. { IH_OS_OSE, "ose", "Enea OSE", },
  90. { IH_OS_PLAN9, "plan9", "Plan 9", },
  91. { IH_OS_RTEMS, "rtems", "RTEMS", },
  92. { IH_OS_U_BOOT, "u-boot", "U-Boot", },
  93. #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
  94. { IH_OS_QNX, "qnx", "QNX", },
  95. { IH_OS_VXWORKS, "vxworks", "VxWorks", },
  96. #endif
  97. #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
  98. { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
  99. #endif
  100. #ifdef USE_HOSTCC
  101. { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
  102. { IH_OS_DELL, "dell", "Dell", },
  103. { IH_OS_ESIX, "esix", "Esix", },
  104. { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
  105. { IH_OS_IRIX, "irix", "Irix", },
  106. { IH_OS_NCR, "ncr", "NCR", },
  107. { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
  108. { IH_OS_PSOS, "psos", "pSOS", },
  109. { IH_OS_SCO, "sco", "SCO", },
  110. { IH_OS_SOLARIS, "solaris", "Solaris", },
  111. { IH_OS_SVR4, "svr4", "SVR4", },
  112. #endif
  113. { -1, "", "", },
  114. };
  115. static const table_entry_t uimage_type[] = {
  116. { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
  117. { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
  118. { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
  119. { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
  120. { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
  121. { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
  122. { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
  123. { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
  124. { IH_TYPE_INVALID, NULL, "Invalid Image", },
  125. { IH_TYPE_MULTI, "multi", "Multi-File Image", },
  126. { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
  127. { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
  128. { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
  129. { IH_TYPE_SCRIPT, "script", "Script", },
  130. { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
  131. { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
  132. { -1, "", "", },
  133. };
  134. static const table_entry_t uimage_comp[] = {
  135. { IH_COMP_NONE, "none", "uncompressed", },
  136. { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
  137. { IH_COMP_GZIP, "gzip", "gzip compressed", },
  138. { IH_COMP_LZMA, "lzma", "lzma compressed", },
  139. { IH_COMP_LZO, "lzo", "lzo compressed", },
  140. { -1, "", "", },
  141. };
  142. /*****************************************************************************/
  143. /* Legacy format routines */
  144. /*****************************************************************************/
  145. int image_check_hcrc(const image_header_t *hdr)
  146. {
  147. ulong hcrc;
  148. ulong len = image_get_header_size();
  149. image_header_t header;
  150. /* Copy header so we can blank CRC field for re-calculation */
  151. memmove(&header, (char *)hdr, image_get_header_size());
  152. image_set_hcrc(&header, 0);
  153. hcrc = crc32(0, (unsigned char *)&header, len);
  154. return (hcrc == image_get_hcrc(hdr));
  155. }
  156. int image_check_dcrc(const image_header_t *hdr)
  157. {
  158. ulong data = image_get_data(hdr);
  159. ulong len = image_get_data_size(hdr);
  160. ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
  161. return (dcrc == image_get_dcrc(hdr));
  162. }
  163. /**
  164. * image_multi_count - get component (sub-image) count
  165. * @hdr: pointer to the header of the multi component image
  166. *
  167. * image_multi_count() returns number of components in a multi
  168. * component image.
  169. *
  170. * Note: no checking of the image type is done, caller must pass
  171. * a valid multi component image.
  172. *
  173. * returns:
  174. * number of components
  175. */
  176. ulong image_multi_count(const image_header_t *hdr)
  177. {
  178. ulong i, count = 0;
  179. uint32_t *size;
  180. /* get start of the image payload, which in case of multi
  181. * component images that points to a table of component sizes */
  182. size = (uint32_t *)image_get_data(hdr);
  183. /* count non empty slots */
  184. for (i = 0; size[i]; ++i)
  185. count++;
  186. return count;
  187. }
  188. /**
  189. * image_multi_getimg - get component data address and size
  190. * @hdr: pointer to the header of the multi component image
  191. * @idx: index of the requested component
  192. * @data: pointer to a ulong variable, will hold component data address
  193. * @len: pointer to a ulong variable, will hold component size
  194. *
  195. * image_multi_getimg() returns size and data address for the requested
  196. * component in a multi component image.
  197. *
  198. * Note: no checking of the image type is done, caller must pass
  199. * a valid multi component image.
  200. *
  201. * returns:
  202. * data address and size of the component, if idx is valid
  203. * 0 in data and len, if idx is out of range
  204. */
  205. void image_multi_getimg(const image_header_t *hdr, ulong idx,
  206. ulong *data, ulong *len)
  207. {
  208. int i;
  209. uint32_t *size;
  210. ulong offset, count, img_data;
  211. /* get number of component */
  212. count = image_multi_count(hdr);
  213. /* get start of the image payload, which in case of multi
  214. * component images that points to a table of component sizes */
  215. size = (uint32_t *)image_get_data(hdr);
  216. /* get address of the proper component data start, which means
  217. * skipping sizes table (add 1 for last, null entry) */
  218. img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
  219. if (idx < count) {
  220. *len = uimage_to_cpu(size[idx]);
  221. offset = 0;
  222. /* go over all indices preceding requested component idx */
  223. for (i = 0; i < idx; i++) {
  224. /* add up i-th component size, rounding up to 4 bytes */
  225. offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
  226. }
  227. /* calculate idx-th component data address */
  228. *data = img_data + offset;
  229. } else {
  230. *len = 0;
  231. *data = 0;
  232. }
  233. }
  234. static void image_print_type(const image_header_t *hdr)
  235. {
  236. const char *os, *arch, *type, *comp;
  237. os = genimg_get_os_name(image_get_os(hdr));
  238. arch = genimg_get_arch_name(image_get_arch(hdr));
  239. type = genimg_get_type_name(image_get_type(hdr));
  240. comp = genimg_get_comp_name(image_get_comp(hdr));
  241. printf("%s %s %s (%s)\n", arch, os, type, comp);
  242. }
  243. /**
  244. * image_print_contents - prints out the contents of the legacy format image
  245. * @ptr: pointer to the legacy format image header
  246. * @p: pointer to prefix string
  247. *
  248. * image_print_contents() formats a multi line legacy image contents description.
  249. * The routine prints out all header fields followed by the size/offset data
  250. * for MULTI/SCRIPT images.
  251. *
  252. * returns:
  253. * no returned results
  254. */
  255. void image_print_contents(const void *ptr)
  256. {
  257. const image_header_t *hdr = (const image_header_t *)ptr;
  258. const char *p;
  259. #ifdef USE_HOSTCC
  260. p = "";
  261. #else
  262. p = " ";
  263. #endif
  264. printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
  265. if (IMAGE_ENABLE_TIMESTAMP) {
  266. printf("%sCreated: ", p);
  267. genimg_print_time((time_t)image_get_time(hdr));
  268. }
  269. printf("%sImage Type: ", p);
  270. image_print_type(hdr);
  271. printf("%sData Size: ", p);
  272. genimg_print_size(image_get_data_size(hdr));
  273. printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
  274. printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
  275. if (image_check_type(hdr, IH_TYPE_MULTI) ||
  276. image_check_type(hdr, IH_TYPE_SCRIPT)) {
  277. int i;
  278. ulong data, len;
  279. ulong count = image_multi_count(hdr);
  280. printf("%sContents:\n", p);
  281. for (i = 0; i < count; i++) {
  282. image_multi_getimg(hdr, i, &data, &len);
  283. printf("%s Image %d: ", p, i);
  284. genimg_print_size(len);
  285. if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
  286. /*
  287. * the user may need to know offsets
  288. * if planning to do something with
  289. * multiple files
  290. */
  291. printf("%s Offset = 0x%08lx\n", p, data);
  292. }
  293. }
  294. }
  295. }
  296. #ifndef USE_HOSTCC
  297. /**
  298. * image_get_ramdisk - get and verify ramdisk image
  299. * @rd_addr: ramdisk image start address
  300. * @arch: expected ramdisk architecture
  301. * @verify: checksum verification flag
  302. *
  303. * image_get_ramdisk() returns a pointer to the verified ramdisk image
  304. * header. Routine receives image start address and expected architecture
  305. * flag. Verification done covers data and header integrity and os/type/arch
  306. * fields checking.
  307. *
  308. * If dataflash support is enabled routine checks for dataflash addresses
  309. * and handles required dataflash reads.
  310. *
  311. * returns:
  312. * pointer to a ramdisk image header, if image was found and valid
  313. * otherwise, return NULL
  314. */
  315. static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
  316. int verify)
  317. {
  318. const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
  319. if (!image_check_magic(rd_hdr)) {
  320. puts("Bad Magic Number\n");
  321. bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
  322. return NULL;
  323. }
  324. if (!image_check_hcrc(rd_hdr)) {
  325. puts("Bad Header Checksum\n");
  326. bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
  327. return NULL;
  328. }
  329. bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
  330. image_print_contents(rd_hdr);
  331. if (verify) {
  332. puts(" Verifying Checksum ... ");
  333. if (!image_check_dcrc(rd_hdr)) {
  334. puts("Bad Data CRC\n");
  335. bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
  336. return NULL;
  337. }
  338. puts("OK\n");
  339. }
  340. bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
  341. if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
  342. !image_check_arch(rd_hdr, arch) ||
  343. !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
  344. printf("No Linux %s Ramdisk Image\n",
  345. genimg_get_arch_name(arch));
  346. bootstage_error(BOOTSTAGE_ID_RAMDISK);
  347. return NULL;
  348. }
  349. return rd_hdr;
  350. }
  351. #endif /* !USE_HOSTCC */
  352. /*****************************************************************************/
  353. /* Shared dual-format routines */
  354. /*****************************************************************************/
  355. #ifndef USE_HOSTCC
  356. ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
  357. ulong save_addr; /* Default Save Address */
  358. ulong save_size; /* Default Save Size (in bytes) */
  359. static int on_loadaddr(const char *name, const char *value, enum env_op op,
  360. int flags)
  361. {
  362. switch (op) {
  363. case env_op_create:
  364. case env_op_overwrite:
  365. load_addr = simple_strtoul(value, NULL, 16);
  366. break;
  367. default:
  368. break;
  369. }
  370. return 0;
  371. }
  372. U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
  373. ulong getenv_bootm_low(void)
  374. {
  375. char *s = getenv("bootm_low");
  376. if (s) {
  377. ulong tmp = simple_strtoul(s, NULL, 16);
  378. return tmp;
  379. }
  380. #if defined(CONFIG_SYS_SDRAM_BASE)
  381. return CONFIG_SYS_SDRAM_BASE;
  382. #elif defined(CONFIG_ARM)
  383. return gd->bd->bi_dram[0].start;
  384. #else
  385. return 0;
  386. #endif
  387. }
  388. phys_size_t getenv_bootm_size(void)
  389. {
  390. phys_size_t tmp;
  391. char *s = getenv("bootm_size");
  392. if (s) {
  393. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  394. return tmp;
  395. }
  396. s = getenv("bootm_low");
  397. if (s)
  398. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  399. else
  400. tmp = 0;
  401. #if defined(CONFIG_ARM)
  402. return gd->bd->bi_dram[0].size - tmp;
  403. #else
  404. return gd->bd->bi_memsize - tmp;
  405. #endif
  406. }
  407. phys_size_t getenv_bootm_mapsize(void)
  408. {
  409. phys_size_t tmp;
  410. char *s = getenv("bootm_mapsize");
  411. if (s) {
  412. tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
  413. return tmp;
  414. }
  415. #if defined(CONFIG_SYS_BOOTMAPSZ)
  416. return CONFIG_SYS_BOOTMAPSZ;
  417. #else
  418. return getenv_bootm_size();
  419. #endif
  420. }
  421. void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  422. {
  423. if (to == from)
  424. return;
  425. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  426. while (len > 0) {
  427. size_t tail = (len > chunksz) ? chunksz : len;
  428. WATCHDOG_RESET();
  429. memmove(to, from, tail);
  430. to += tail;
  431. from += tail;
  432. len -= tail;
  433. }
  434. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  435. memmove(to, from, len);
  436. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  437. }
  438. #endif /* !USE_HOSTCC */
  439. void genimg_print_size(uint32_t size)
  440. {
  441. #ifndef USE_HOSTCC
  442. printf("%d Bytes = ", size);
  443. print_size(size, "\n");
  444. #else
  445. printf("%d Bytes = %.2f kB = %.2f MB\n",
  446. size, (double)size / 1.024e3,
  447. (double)size / 1.048576e6);
  448. #endif
  449. }
  450. #if IMAGE_ENABLE_TIMESTAMP
  451. void genimg_print_time(time_t timestamp)
  452. {
  453. #ifndef USE_HOSTCC
  454. struct rtc_time tm;
  455. to_tm(timestamp, &tm);
  456. printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
  457. tm.tm_year, tm.tm_mon, tm.tm_mday,
  458. tm.tm_hour, tm.tm_min, tm.tm_sec);
  459. #else
  460. printf("%s", ctime(&timestamp));
  461. #endif
  462. }
  463. #endif
  464. /**
  465. * get_table_entry_name - translate entry id to long name
  466. * @table: pointer to a translation table for entries of a specific type
  467. * @msg: message to be returned when translation fails
  468. * @id: entry id to be translated
  469. *
  470. * get_table_entry_name() will go over translation table trying to find
  471. * entry that matches given id. If matching entry is found, its long
  472. * name is returned to the caller.
  473. *
  474. * returns:
  475. * long entry name if translation succeeds
  476. * msg otherwise
  477. */
  478. char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
  479. {
  480. for (; table->id >= 0; ++table) {
  481. if (table->id == id)
  482. #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
  483. return table->lname;
  484. #else
  485. return table->lname + gd->reloc_off;
  486. #endif
  487. }
  488. return (msg);
  489. }
  490. const char *genimg_get_os_name(uint8_t os)
  491. {
  492. return (get_table_entry_name(uimage_os, "Unknown OS", os));
  493. }
  494. const char *genimg_get_arch_name(uint8_t arch)
  495. {
  496. return (get_table_entry_name(uimage_arch, "Unknown Architecture",
  497. arch));
  498. }
  499. const char *genimg_get_type_name(uint8_t type)
  500. {
  501. return (get_table_entry_name(uimage_type, "Unknown Image", type));
  502. }
  503. const char *genimg_get_comp_name(uint8_t comp)
  504. {
  505. return (get_table_entry_name(uimage_comp, "Unknown Compression",
  506. comp));
  507. }
  508. /**
  509. * get_table_entry_id - translate short entry name to id
  510. * @table: pointer to a translation table for entries of a specific type
  511. * @table_name: to be used in case of error
  512. * @name: entry short name to be translated
  513. *
  514. * get_table_entry_id() will go over translation table trying to find
  515. * entry that matches given short name. If matching entry is found,
  516. * its id returned to the caller.
  517. *
  518. * returns:
  519. * entry id if translation succeeds
  520. * -1 otherwise
  521. */
  522. int get_table_entry_id(const table_entry_t *table,
  523. const char *table_name, const char *name)
  524. {
  525. const table_entry_t *t;
  526. #ifdef USE_HOSTCC
  527. int first = 1;
  528. for (t = table; t->id >= 0; ++t) {
  529. if (t->sname && strcasecmp(t->sname, name) == 0)
  530. return(t->id);
  531. }
  532. fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
  533. for (t = table; t->id >= 0; ++t) {
  534. if (t->sname == NULL)
  535. continue;
  536. fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
  537. first = 0;
  538. }
  539. fprintf(stderr, "\n");
  540. #else
  541. for (t = table; t->id >= 0; ++t) {
  542. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  543. if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
  544. #else
  545. if (t->sname && strcmp(t->sname, name) == 0)
  546. #endif
  547. return (t->id);
  548. }
  549. debug("Invalid %s Type: %s\n", table_name, name);
  550. #endif /* USE_HOSTCC */
  551. return (-1);
  552. }
  553. int genimg_get_os_id(const char *name)
  554. {
  555. return (get_table_entry_id(uimage_os, "OS", name));
  556. }
  557. int genimg_get_arch_id(const char *name)
  558. {
  559. return (get_table_entry_id(uimage_arch, "CPU", name));
  560. }
  561. int genimg_get_type_id(const char *name)
  562. {
  563. return (get_table_entry_id(uimage_type, "Image", name));
  564. }
  565. int genimg_get_comp_id(const char *name)
  566. {
  567. return (get_table_entry_id(uimage_comp, "Compression", name));
  568. }
  569. #ifndef USE_HOSTCC
  570. /**
  571. * genimg_get_format - get image format type
  572. * @img_addr: image start address
  573. *
  574. * genimg_get_format() checks whether provided address points to a valid
  575. * legacy or FIT image.
  576. *
  577. * New uImage format and FDT blob are based on a libfdt. FDT blob
  578. * may be passed directly or embedded in a FIT image. In both situations
  579. * genimg_get_format() must be able to dectect libfdt header.
  580. *
  581. * returns:
  582. * image format type or IMAGE_FORMAT_INVALID if no image is present
  583. */
  584. int genimg_get_format(void *img_addr)
  585. {
  586. ulong format = IMAGE_FORMAT_INVALID;
  587. const image_header_t *hdr;
  588. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  589. char *fit_hdr;
  590. #endif
  591. hdr = (const image_header_t *)img_addr;
  592. if (image_check_magic(hdr))
  593. format = IMAGE_FORMAT_LEGACY;
  594. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  595. else {
  596. fit_hdr = (char *)img_addr;
  597. if (fdt_check_header(fit_hdr) == 0)
  598. format = IMAGE_FORMAT_FIT;
  599. }
  600. #endif
  601. return format;
  602. }
  603. /**
  604. * genimg_get_image - get image from special storage (if necessary)
  605. * @img_addr: image start address
  606. *
  607. * genimg_get_image() checks if provided image start adddress is located
  608. * in a dataflash storage. If so, image is moved to a system RAM memory.
  609. *
  610. * returns:
  611. * image start address after possible relocation from special storage
  612. */
  613. ulong genimg_get_image(ulong img_addr)
  614. {
  615. ulong ram_addr = img_addr;
  616. #ifdef CONFIG_HAS_DATAFLASH
  617. ulong h_size, d_size;
  618. if (addr_dataflash(img_addr)) {
  619. /* ger RAM address */
  620. ram_addr = CONFIG_SYS_LOAD_ADDR;
  621. /* get header size */
  622. h_size = image_get_header_size();
  623. #if defined(CONFIG_FIT)
  624. if (sizeof(struct fdt_header) > h_size)
  625. h_size = sizeof(struct fdt_header);
  626. #endif
  627. /* read in header */
  628. debug(" Reading image header from dataflash address "
  629. "%08lx to RAM address %08lx\n", img_addr, ram_addr);
  630. read_dataflash(img_addr, h_size, (char *)ram_addr);
  631. /* get data size */
  632. switch (genimg_get_format((void *)ram_addr)) {
  633. case IMAGE_FORMAT_LEGACY:
  634. d_size = image_get_data_size(
  635. (const image_header_t *)ram_addr);
  636. debug(" Legacy format image found at 0x%08lx, "
  637. "size 0x%08lx\n",
  638. ram_addr, d_size);
  639. break;
  640. #if defined(CONFIG_FIT)
  641. case IMAGE_FORMAT_FIT:
  642. d_size = fit_get_size((const void *)ram_addr) - h_size;
  643. debug(" FIT/FDT format image found at 0x%08lx, "
  644. "size 0x%08lx\n",
  645. ram_addr, d_size);
  646. break;
  647. #endif
  648. default:
  649. printf(" No valid image found at 0x%08lx\n",
  650. img_addr);
  651. return ram_addr;
  652. }
  653. /* read in image data */
  654. debug(" Reading image remaining data from dataflash address "
  655. "%08lx to RAM address %08lx\n", img_addr + h_size,
  656. ram_addr + h_size);
  657. read_dataflash(img_addr + h_size, d_size,
  658. (char *)(ram_addr + h_size));
  659. }
  660. #endif /* CONFIG_HAS_DATAFLASH */
  661. return ram_addr;
  662. }
  663. /**
  664. * fit_has_config - check if there is a valid FIT configuration
  665. * @images: pointer to the bootm command headers structure
  666. *
  667. * fit_has_config() checks if there is a FIT configuration in use
  668. * (if FTI support is present).
  669. *
  670. * returns:
  671. * 0, no FIT support or no configuration found
  672. * 1, configuration found
  673. */
  674. int genimg_has_config(bootm_headers_t *images)
  675. {
  676. #if defined(CONFIG_FIT)
  677. if (images->fit_uname_cfg)
  678. return 1;
  679. #endif
  680. return 0;
  681. }
  682. /**
  683. * boot_get_ramdisk - main ramdisk handling routine
  684. * @argc: command argument count
  685. * @argv: command argument list
  686. * @images: pointer to the bootm images structure
  687. * @arch: expected ramdisk architecture
  688. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  689. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  690. *
  691. * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
  692. * Curently supported are the following ramdisk sources:
  693. * - multicomponent kernel/ramdisk image,
  694. * - commandline provided address of decicated ramdisk image.
  695. *
  696. * returns:
  697. * 0, if ramdisk image was found and valid, or skiped
  698. * rd_start and rd_end are set to ramdisk start/end addresses if
  699. * ramdisk image is found and valid
  700. *
  701. * 1, if ramdisk image is found but corrupted, or invalid
  702. * rd_start and rd_end are set to 0 if no ramdisk exists
  703. */
  704. int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
  705. uint8_t arch, ulong *rd_start, ulong *rd_end)
  706. {
  707. ulong rd_addr, rd_load;
  708. ulong rd_data, rd_len;
  709. const image_header_t *rd_hdr;
  710. #ifdef CONFIG_SUPPORT_RAW_INITRD
  711. char *end;
  712. #endif
  713. #if defined(CONFIG_FIT)
  714. void *fit_hdr;
  715. const char *fit_uname_config = NULL;
  716. const char *fit_uname_ramdisk = NULL;
  717. ulong default_addr;
  718. int rd_noffset;
  719. int cfg_noffset;
  720. const void *data;
  721. size_t size;
  722. #endif
  723. *rd_start = 0;
  724. *rd_end = 0;
  725. /*
  726. * Look for a '-' which indicates to ignore the
  727. * ramdisk argument
  728. */
  729. if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
  730. debug("## Skipping init Ramdisk\n");
  731. rd_len = rd_data = 0;
  732. } else if (argc >= 3 || genimg_has_config(images)) {
  733. #if defined(CONFIG_FIT)
  734. if (argc >= 3) {
  735. /*
  736. * If the init ramdisk comes from the FIT image and
  737. * the FIT image address is omitted in the command
  738. * line argument, try to use os FIT image address or
  739. * default load address.
  740. */
  741. if (images->fit_uname_os)
  742. default_addr = (ulong)images->fit_hdr_os;
  743. else
  744. default_addr = load_addr;
  745. if (fit_parse_conf(argv[2], default_addr,
  746. &rd_addr, &fit_uname_config)) {
  747. debug("* ramdisk: config '%s' from image at "
  748. "0x%08lx\n",
  749. fit_uname_config, rd_addr);
  750. } else if (fit_parse_subimage(argv[2], default_addr,
  751. &rd_addr, &fit_uname_ramdisk)) {
  752. debug("* ramdisk: subimage '%s' from image at "
  753. "0x%08lx\n",
  754. fit_uname_ramdisk, rd_addr);
  755. } else
  756. #endif
  757. {
  758. rd_addr = simple_strtoul(argv[2], NULL, 16);
  759. debug("* ramdisk: cmdline image address = "
  760. "0x%08lx\n",
  761. rd_addr);
  762. }
  763. #if defined(CONFIG_FIT)
  764. } else {
  765. /* use FIT configuration provided in first bootm
  766. * command argument
  767. */
  768. rd_addr = (ulong)images->fit_hdr_os;
  769. fit_uname_config = images->fit_uname_cfg;
  770. debug("* ramdisk: using config '%s' from image "
  771. "at 0x%08lx\n",
  772. fit_uname_config, rd_addr);
  773. /*
  774. * Check whether configuration has ramdisk defined,
  775. * if not, don't try to use it, quit silently.
  776. */
  777. fit_hdr = (void *)rd_addr;
  778. cfg_noffset = fit_conf_get_node(fit_hdr,
  779. fit_uname_config);
  780. if (cfg_noffset < 0) {
  781. debug("* ramdisk: no such config\n");
  782. return 1;
  783. }
  784. rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
  785. cfg_noffset);
  786. if (rd_noffset < 0) {
  787. debug("* ramdisk: no ramdisk in config\n");
  788. return 0;
  789. }
  790. }
  791. #endif
  792. /* copy from dataflash if needed */
  793. rd_addr = genimg_get_image(rd_addr);
  794. /*
  795. * Check if there is an initrd image at the
  796. * address provided in the second bootm argument
  797. * check image type, for FIT images get FIT node.
  798. */
  799. switch (genimg_get_format((void *)rd_addr)) {
  800. case IMAGE_FORMAT_LEGACY:
  801. printf("## Loading init Ramdisk from Legacy "
  802. "Image at %08lx ...\n", rd_addr);
  803. bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
  804. rd_hdr = image_get_ramdisk(rd_addr, arch,
  805. images->verify);
  806. if (rd_hdr == NULL)
  807. return 1;
  808. rd_data = image_get_data(rd_hdr);
  809. rd_len = image_get_data_size(rd_hdr);
  810. rd_load = image_get_load(rd_hdr);
  811. break;
  812. #if defined(CONFIG_FIT)
  813. case IMAGE_FORMAT_FIT:
  814. fit_hdr = (void *)rd_addr;
  815. printf("## Loading init Ramdisk from FIT "
  816. "Image at %08lx ...\n", rd_addr);
  817. bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
  818. if (!fit_check_format(fit_hdr)) {
  819. puts("Bad FIT ramdisk image format!\n");
  820. bootstage_error(
  821. BOOTSTAGE_ID_FIT_RD_FORMAT);
  822. return 1;
  823. }
  824. bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
  825. if (!fit_uname_ramdisk) {
  826. /*
  827. * no ramdisk image node unit name, try to get config
  828. * node first. If config unit node name is NULL
  829. * fit_conf_get_node() will try to find default config node
  830. */
  831. bootstage_mark(
  832. BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
  833. cfg_noffset = fit_conf_get_node(fit_hdr,
  834. fit_uname_config);
  835. if (cfg_noffset < 0) {
  836. puts("Could not find configuration "
  837. "node\n");
  838. bootstage_error(
  839. BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
  840. return 1;
  841. }
  842. fit_uname_config = fdt_get_name(fit_hdr,
  843. cfg_noffset, NULL);
  844. printf(" Using '%s' configuration\n",
  845. fit_uname_config);
  846. rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
  847. cfg_noffset);
  848. fit_uname_ramdisk = fit_get_name(fit_hdr,
  849. rd_noffset, NULL);
  850. } else {
  851. /* get ramdisk component image node offset */
  852. bootstage_mark(
  853. BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
  854. rd_noffset = fit_image_get_node(fit_hdr,
  855. fit_uname_ramdisk);
  856. }
  857. if (rd_noffset < 0) {
  858. puts("Could not find subimage node\n");
  859. bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
  860. return 1;
  861. }
  862. printf(" Trying '%s' ramdisk subimage\n",
  863. fit_uname_ramdisk);
  864. bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
  865. if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
  866. images->verify))
  867. return 1;
  868. /* get ramdisk image data address and length */
  869. if (fit_image_get_data(fit_hdr, rd_noffset, &data,
  870. &size)) {
  871. puts("Could not find ramdisk subimage data!\n");
  872. bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
  873. return 1;
  874. }
  875. bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
  876. rd_data = (ulong)data;
  877. rd_len = size;
  878. if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
  879. puts("Can't get ramdisk subimage load "
  880. "address!\n");
  881. bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
  882. return 1;
  883. }
  884. bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
  885. images->fit_hdr_rd = fit_hdr;
  886. images->fit_uname_rd = fit_uname_ramdisk;
  887. images->fit_noffset_rd = rd_noffset;
  888. break;
  889. #endif
  890. default:
  891. #ifdef CONFIG_SUPPORT_RAW_INITRD
  892. if (argc >= 3 && (end = strchr(argv[2], ':'))) {
  893. rd_len = simple_strtoul(++end, NULL, 16);
  894. rd_data = rd_addr;
  895. } else
  896. #endif
  897. {
  898. puts("Wrong Ramdisk Image Format\n");
  899. rd_data = rd_len = rd_load = 0;
  900. return 1;
  901. }
  902. }
  903. } else if (images->legacy_hdr_valid &&
  904. image_check_type(&images->legacy_hdr_os_copy,
  905. IH_TYPE_MULTI)) {
  906. /*
  907. * Now check if we have a legacy mult-component image,
  908. * get second entry data start address and len.
  909. */
  910. bootstage_mark(BOOTSTAGE_ID_RAMDISK);
  911. printf("## Loading init Ramdisk from multi component "
  912. "Legacy Image at %08lx ...\n",
  913. (ulong)images->legacy_hdr_os);
  914. image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
  915. } else {
  916. /*
  917. * no initrd image
  918. */
  919. bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
  920. rd_len = rd_data = 0;
  921. }
  922. if (!rd_data) {
  923. debug("## No init Ramdisk\n");
  924. } else {
  925. *rd_start = rd_data;
  926. *rd_end = rd_data + rd_len;
  927. }
  928. debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  929. *rd_start, *rd_end);
  930. return 0;
  931. }
  932. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  933. /**
  934. * boot_ramdisk_high - relocate init ramdisk
  935. * @lmb: pointer to lmb handle, will be used for memory mgmt
  936. * @rd_data: ramdisk data start address
  937. * @rd_len: ramdisk data length
  938. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  939. * start address (after possible relocation)
  940. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  941. * end address (after possible relocation)
  942. *
  943. * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
  944. * variable and if requested ramdisk data is moved to a specified location.
  945. *
  946. * Initrd_start and initrd_end are set to final (after relocation) ramdisk
  947. * start/end addresses if ramdisk image start and len were provided,
  948. * otherwise set initrd_start and initrd_end set to zeros.
  949. *
  950. * returns:
  951. * 0 - success
  952. * -1 - failure
  953. */
  954. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  955. ulong *initrd_start, ulong *initrd_end)
  956. {
  957. char *s;
  958. ulong initrd_high;
  959. int initrd_copy_to_ram = 1;
  960. if ((s = getenv("initrd_high")) != NULL) {
  961. /* a value of "no" or a similar string will act like 0,
  962. * turning the "load high" feature off. This is intentional.
  963. */
  964. initrd_high = simple_strtoul(s, NULL, 16);
  965. if (initrd_high == ~0)
  966. initrd_copy_to_ram = 0;
  967. } else {
  968. /* not set, no restrictions to load high */
  969. initrd_high = ~0;
  970. }
  971. #ifdef CONFIG_LOGBUFFER
  972. /* Prevent initrd from overwriting logbuffer */
  973. lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
  974. #endif
  975. debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  976. initrd_high, initrd_copy_to_ram);
  977. if (rd_data) {
  978. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  979. debug(" in-place initrd\n");
  980. *initrd_start = rd_data;
  981. *initrd_end = rd_data + rd_len;
  982. lmb_reserve(lmb, rd_data, rd_len);
  983. } else {
  984. if (initrd_high)
  985. *initrd_start = (ulong)lmb_alloc_base(lmb,
  986. rd_len, 0x1000, initrd_high);
  987. else
  988. *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
  989. 0x1000);
  990. if (*initrd_start == 0) {
  991. puts("ramdisk - allocation error\n");
  992. goto error;
  993. }
  994. bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
  995. *initrd_end = *initrd_start + rd_len;
  996. printf(" Loading Ramdisk to %08lx, end %08lx ... ",
  997. *initrd_start, *initrd_end);
  998. memmove_wd((void *)*initrd_start,
  999. (void *)rd_data, rd_len, CHUNKSZ);
  1000. #ifdef CONFIG_MP
  1001. /*
  1002. * Ensure the image is flushed to memory to handle
  1003. * AMP boot scenarios in which we might not be
  1004. * HW cache coherent
  1005. */
  1006. flush_cache((unsigned long)*initrd_start, rd_len);
  1007. #endif
  1008. puts("OK\n");
  1009. }
  1010. } else {
  1011. *initrd_start = 0;
  1012. *initrd_end = 0;
  1013. }
  1014. debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  1015. *initrd_start, *initrd_end);
  1016. return 0;
  1017. error:
  1018. return -1;
  1019. }
  1020. #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
  1021. #ifdef CONFIG_OF_LIBFDT
  1022. static void fdt_error(const char *msg)
  1023. {
  1024. puts("ERROR: ");
  1025. puts(msg);
  1026. puts(" - must RESET the board to recover.\n");
  1027. }
  1028. static const image_header_t *image_get_fdt(ulong fdt_addr)
  1029. {
  1030. const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
  1031. image_print_contents(fdt_hdr);
  1032. puts(" Verifying Checksum ... ");
  1033. if (!image_check_hcrc(fdt_hdr)) {
  1034. fdt_error("fdt header checksum invalid");
  1035. return NULL;
  1036. }
  1037. if (!image_check_dcrc(fdt_hdr)) {
  1038. fdt_error("fdt checksum invalid");
  1039. return NULL;
  1040. }
  1041. puts("OK\n");
  1042. if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
  1043. fdt_error("uImage is not a fdt");
  1044. return NULL;
  1045. }
  1046. if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
  1047. fdt_error("uImage is compressed");
  1048. return NULL;
  1049. }
  1050. if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
  1051. fdt_error("uImage data is not a fdt");
  1052. return NULL;
  1053. }
  1054. return fdt_hdr;
  1055. }
  1056. /**
  1057. * fit_check_fdt - verify FIT format FDT subimage
  1058. * @fit_hdr: pointer to the FIT header
  1059. * fdt_noffset: FDT subimage node offset within FIT image
  1060. * @verify: data CRC verification flag
  1061. *
  1062. * fit_check_fdt() verifies integrity of the FDT subimage and from
  1063. * specified FIT image.
  1064. *
  1065. * returns:
  1066. * 1, on success
  1067. * 0, on failure
  1068. */
  1069. #if defined(CONFIG_FIT)
  1070. static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
  1071. {
  1072. fit_image_print(fit, fdt_noffset, " ");
  1073. if (verify) {
  1074. puts(" Verifying Hash Integrity ... ");
  1075. if (!fit_image_verify(fit, fdt_noffset)) {
  1076. fdt_error("Bad Data Hash");
  1077. return 0;
  1078. }
  1079. puts("OK\n");
  1080. }
  1081. if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
  1082. fdt_error("Not a FDT image");
  1083. return 0;
  1084. }
  1085. if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
  1086. fdt_error("FDT image is compressed");
  1087. return 0;
  1088. }
  1089. return 1;
  1090. }
  1091. #endif /* CONFIG_FIT */
  1092. #ifndef CONFIG_SYS_FDT_PAD
  1093. #define CONFIG_SYS_FDT_PAD 0x3000
  1094. #endif
  1095. #if defined(CONFIG_OF_LIBFDT)
  1096. /**
  1097. * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
  1098. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1099. * @fdt_blob: pointer to fdt blob base address
  1100. *
  1101. * Adds the memreserve regions in the dtb to the lmb block. Adding the
  1102. * memreserve regions prevents u-boot from using them to store the initrd
  1103. * or the fdt blob.
  1104. */
  1105. void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
  1106. {
  1107. uint64_t addr, size;
  1108. int i, total;
  1109. if (fdt_check_header(fdt_blob) != 0)
  1110. return;
  1111. total = fdt_num_mem_rsv(fdt_blob);
  1112. for (i = 0; i < total; i++) {
  1113. if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
  1114. continue;
  1115. printf(" reserving fdt memory region: addr=%llx size=%llx\n",
  1116. (unsigned long long)addr, (unsigned long long)size);
  1117. lmb_reserve(lmb, addr, size);
  1118. }
  1119. }
  1120. /**
  1121. * boot_relocate_fdt - relocate flat device tree
  1122. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1123. * @of_flat_tree: pointer to a char* variable, will hold fdt start address
  1124. * @of_size: pointer to a ulong variable, will hold fdt length
  1125. *
  1126. * boot_relocate_fdt() allocates a region of memory within the bootmap and
  1127. * relocates the of_flat_tree into that region, even if the fdt is already in
  1128. * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
  1129. * bytes.
  1130. *
  1131. * of_flat_tree and of_size are set to final (after relocation) values
  1132. *
  1133. * returns:
  1134. * 0 - success
  1135. * 1 - failure
  1136. */
  1137. int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
  1138. {
  1139. void *fdt_blob = *of_flat_tree;
  1140. void *of_start = NULL;
  1141. char *fdt_high;
  1142. ulong of_len = 0;
  1143. int err;
  1144. int disable_relocation = 0;
  1145. /* nothing to do */
  1146. if (*of_size == 0)
  1147. return 0;
  1148. if (fdt_check_header(fdt_blob) != 0) {
  1149. fdt_error("image is not a fdt");
  1150. goto error;
  1151. }
  1152. /* position on a 4K boundary before the alloc_current */
  1153. /* Pad the FDT by a specified amount */
  1154. of_len = *of_size + CONFIG_SYS_FDT_PAD;
  1155. /* If fdt_high is set use it to select the relocation address */
  1156. fdt_high = getenv("fdt_high");
  1157. if (fdt_high) {
  1158. void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
  1159. if (((ulong) desired_addr) == ~0UL) {
  1160. /* All ones means use fdt in place */
  1161. of_start = fdt_blob;
  1162. lmb_reserve(lmb, (ulong)of_start, of_len);
  1163. disable_relocation = 1;
  1164. } else if (desired_addr) {
  1165. of_start =
  1166. (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
  1167. (ulong)desired_addr);
  1168. if (of_start == NULL) {
  1169. puts("Failed using fdt_high value for Device Tree");
  1170. goto error;
  1171. }
  1172. } else {
  1173. of_start =
  1174. (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
  1175. }
  1176. } else {
  1177. of_start =
  1178. (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
  1179. getenv_bootm_mapsize()
  1180. + getenv_bootm_low());
  1181. }
  1182. if (of_start == NULL) {
  1183. puts("device tree - allocation error\n");
  1184. goto error;
  1185. }
  1186. if (disable_relocation) {
  1187. /* We assume there is space after the existing fdt to use for padding */
  1188. fdt_set_totalsize(of_start, of_len);
  1189. printf(" Using Device Tree in place at %p, end %p\n",
  1190. of_start, of_start + of_len - 1);
  1191. } else {
  1192. debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
  1193. fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
  1194. printf(" Loading Device Tree to %p, end %p ... ",
  1195. of_start, of_start + of_len - 1);
  1196. err = fdt_open_into(fdt_blob, of_start, of_len);
  1197. if (err != 0) {
  1198. fdt_error("fdt move failed");
  1199. goto error;
  1200. }
  1201. puts("OK\n");
  1202. }
  1203. *of_flat_tree = of_start;
  1204. *of_size = of_len;
  1205. set_working_fdt_addr(*of_flat_tree);
  1206. return 0;
  1207. error:
  1208. return 1;
  1209. }
  1210. #endif /* CONFIG_OF_LIBFDT */
  1211. /**
  1212. * boot_get_fdt - main fdt handling routine
  1213. * @argc: command argument count
  1214. * @argv: command argument list
  1215. * @images: pointer to the bootm images structure
  1216. * @of_flat_tree: pointer to a char* variable, will hold fdt start address
  1217. * @of_size: pointer to a ulong variable, will hold fdt length
  1218. *
  1219. * boot_get_fdt() is responsible for finding a valid flat device tree image.
  1220. * Curently supported are the following ramdisk sources:
  1221. * - multicomponent kernel/ramdisk image,
  1222. * - commandline provided address of decicated ramdisk image.
  1223. *
  1224. * returns:
  1225. * 0, if fdt image was found and valid, or skipped
  1226. * of_flat_tree and of_size are set to fdt start address and length if
  1227. * fdt image is found and valid
  1228. *
  1229. * 1, if fdt image is found but corrupted
  1230. * of_flat_tree and of_size are set to 0 if no fdt exists
  1231. */
  1232. int boot_get_fdt(int flag, int argc, char * const argv[],
  1233. bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
  1234. {
  1235. const image_header_t *fdt_hdr;
  1236. ulong fdt_addr;
  1237. char *fdt_blob = NULL;
  1238. ulong image_start, image_data, image_end;
  1239. ulong load_start, load_end;
  1240. #if defined(CONFIG_FIT)
  1241. void *fit_hdr;
  1242. const char *fit_uname_config = NULL;
  1243. const char *fit_uname_fdt = NULL;
  1244. ulong default_addr;
  1245. int cfg_noffset;
  1246. int fdt_noffset;
  1247. const void *data;
  1248. size_t size;
  1249. #endif
  1250. *of_flat_tree = NULL;
  1251. *of_size = 0;
  1252. if (argc > 3 || genimg_has_config(images)) {
  1253. #if defined(CONFIG_FIT)
  1254. if (argc > 3) {
  1255. /*
  1256. * If the FDT blob comes from the FIT image and the
  1257. * FIT image address is omitted in the command line
  1258. * argument, try to use ramdisk or os FIT image
  1259. * address or default load address.
  1260. */
  1261. if (images->fit_uname_rd)
  1262. default_addr = (ulong)images->fit_hdr_rd;
  1263. else if (images->fit_uname_os)
  1264. default_addr = (ulong)images->fit_hdr_os;
  1265. else
  1266. default_addr = load_addr;
  1267. if (fit_parse_conf(argv[3], default_addr,
  1268. &fdt_addr, &fit_uname_config)) {
  1269. debug("* fdt: config '%s' from image at "
  1270. "0x%08lx\n",
  1271. fit_uname_config, fdt_addr);
  1272. } else if (fit_parse_subimage(argv[3], default_addr,
  1273. &fdt_addr, &fit_uname_fdt)) {
  1274. debug("* fdt: subimage '%s' from image at "
  1275. "0x%08lx\n",
  1276. fit_uname_fdt, fdt_addr);
  1277. } else
  1278. #endif
  1279. {
  1280. fdt_addr = simple_strtoul(argv[3], NULL, 16);
  1281. debug("* fdt: cmdline image address = "
  1282. "0x%08lx\n",
  1283. fdt_addr);
  1284. }
  1285. #if defined(CONFIG_FIT)
  1286. } else {
  1287. /* use FIT configuration provided in first bootm
  1288. * command argument
  1289. */
  1290. fdt_addr = (ulong)images->fit_hdr_os;
  1291. fit_uname_config = images->fit_uname_cfg;
  1292. debug("* fdt: using config '%s' from image "
  1293. "at 0x%08lx\n",
  1294. fit_uname_config, fdt_addr);
  1295. /*
  1296. * Check whether configuration has FDT blob defined,
  1297. * if not quit silently.
  1298. */
  1299. fit_hdr = (void *)fdt_addr;
  1300. cfg_noffset = fit_conf_get_node(fit_hdr,
  1301. fit_uname_config);
  1302. if (cfg_noffset < 0) {
  1303. debug("* fdt: no such config\n");
  1304. return 0;
  1305. }
  1306. fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
  1307. cfg_noffset);
  1308. if (fdt_noffset < 0) {
  1309. debug("* fdt: no fdt in config\n");
  1310. return 0;
  1311. }
  1312. }
  1313. #endif
  1314. debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
  1315. fdt_addr);
  1316. /* copy from dataflash if needed */
  1317. fdt_addr = genimg_get_image(fdt_addr);
  1318. /*
  1319. * Check if there is an FDT image at the
  1320. * address provided in the second bootm argument
  1321. * check image type, for FIT images get a FIT node.
  1322. */
  1323. switch (genimg_get_format((void *)fdt_addr)) {
  1324. case IMAGE_FORMAT_LEGACY:
  1325. /* verify fdt_addr points to a valid image header */
  1326. printf("## Flattened Device Tree from Legacy Image "
  1327. "at %08lx\n",
  1328. fdt_addr);
  1329. fdt_hdr = image_get_fdt(fdt_addr);
  1330. if (!fdt_hdr)
  1331. goto error;
  1332. /*
  1333. * move image data to the load address,
  1334. * make sure we don't overwrite initial image
  1335. */
  1336. image_start = (ulong)fdt_hdr;
  1337. image_data = (ulong)image_get_data(fdt_hdr);
  1338. image_end = image_get_image_end(fdt_hdr);
  1339. load_start = image_get_load(fdt_hdr);
  1340. load_end = load_start + image_get_data_size(fdt_hdr);
  1341. if (load_start == image_start ||
  1342. load_start == image_data) {
  1343. fdt_blob = (char *)image_data;
  1344. break;
  1345. }
  1346. if ((load_start < image_end) && (load_end > image_start)) {
  1347. fdt_error("fdt overwritten");
  1348. goto error;
  1349. }
  1350. debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
  1351. image_data, load_start);
  1352. memmove((void *)load_start,
  1353. (void *)image_data,
  1354. image_get_data_size(fdt_hdr));
  1355. fdt_blob = (char *)load_start;
  1356. break;
  1357. case IMAGE_FORMAT_FIT:
  1358. /*
  1359. * This case will catch both: new uImage format
  1360. * (libfdt based) and raw FDT blob (also libfdt
  1361. * based).
  1362. */
  1363. #if defined(CONFIG_FIT)
  1364. /* check FDT blob vs FIT blob */
  1365. if (fit_check_format((const void *)fdt_addr)) {
  1366. /*
  1367. * FIT image
  1368. */
  1369. fit_hdr = (void *)fdt_addr;
  1370. printf("## Flattened Device Tree from FIT "
  1371. "Image at %08lx\n",
  1372. fdt_addr);
  1373. if (!fit_uname_fdt) {
  1374. /*
  1375. * no FDT blob image node unit name,
  1376. * try to get config node first. If
  1377. * config unit node name is NULL
  1378. * fit_conf_get_node() will try to
  1379. * find default config node
  1380. */
  1381. cfg_noffset = fit_conf_get_node(fit_hdr,
  1382. fit_uname_config);
  1383. if (cfg_noffset < 0) {
  1384. fdt_error("Could not find "
  1385. "configuration "
  1386. "node\n");
  1387. goto error;
  1388. }
  1389. fit_uname_config = fdt_get_name(fit_hdr,
  1390. cfg_noffset, NULL);
  1391. printf(" Using '%s' configuration\n",
  1392. fit_uname_config);
  1393. fdt_noffset = fit_conf_get_fdt_node(
  1394. fit_hdr,
  1395. cfg_noffset);
  1396. fit_uname_fdt = fit_get_name(fit_hdr,
  1397. fdt_noffset, NULL);
  1398. } else {
  1399. /* get FDT component image node offset */
  1400. fdt_noffset = fit_image_get_node(
  1401. fit_hdr,
  1402. fit_uname_fdt);
  1403. }
  1404. if (fdt_noffset < 0) {
  1405. fdt_error("Could not find subimage "
  1406. "node\n");
  1407. goto error;
  1408. }
  1409. printf(" Trying '%s' FDT blob subimage\n",
  1410. fit_uname_fdt);
  1411. if (!fit_check_fdt(fit_hdr, fdt_noffset,
  1412. images->verify))
  1413. goto error;
  1414. /* get ramdisk image data address and length */
  1415. if (fit_image_get_data(fit_hdr, fdt_noffset,
  1416. &data, &size)) {
  1417. fdt_error("Could not find FDT "
  1418. "subimage data");
  1419. goto error;
  1420. }
  1421. /* verift that image data is a proper FDT blob */
  1422. if (fdt_check_header((char *)data) != 0) {
  1423. fdt_error("Subimage data is not a FTD");
  1424. goto error;
  1425. }
  1426. /*
  1427. * move image data to the load address,
  1428. * make sure we don't overwrite initial image
  1429. */
  1430. image_start = (ulong)fit_hdr;
  1431. image_end = fit_get_end(fit_hdr);
  1432. if (fit_image_get_load(fit_hdr, fdt_noffset,
  1433. &load_start) == 0) {
  1434. load_end = load_start + size;
  1435. if ((load_start < image_end) &&
  1436. (load_end > image_start)) {
  1437. fdt_error("FDT overwritten");
  1438. goto error;
  1439. }
  1440. printf(" Loading FDT from 0x%08lx "
  1441. "to 0x%08lx\n",
  1442. (ulong)data,
  1443. load_start);
  1444. memmove((void *)load_start,
  1445. (void *)data, size);
  1446. fdt_blob = (char *)load_start;
  1447. } else {
  1448. fdt_blob = (char *)data;
  1449. }
  1450. images->fit_hdr_fdt = fit_hdr;
  1451. images->fit_uname_fdt = fit_uname_fdt;
  1452. images->fit_noffset_fdt = fdt_noffset;
  1453. break;
  1454. } else
  1455. #endif
  1456. {
  1457. /*
  1458. * FDT blob
  1459. */
  1460. fdt_blob = (char *)fdt_addr;
  1461. debug("* fdt: raw FDT blob\n");
  1462. printf("## Flattened Device Tree blob at "
  1463. "%08lx\n", (long)fdt_blob);
  1464. }
  1465. break;
  1466. default:
  1467. puts("ERROR: Did not find a cmdline Flattened Device "
  1468. "Tree\n");
  1469. goto error;
  1470. }
  1471. printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
  1472. } else if (images->legacy_hdr_valid &&
  1473. image_check_type(&images->legacy_hdr_os_copy,
  1474. IH_TYPE_MULTI)) {
  1475. ulong fdt_data, fdt_len;
  1476. /*
  1477. * Now check if we have a legacy multi-component image,
  1478. * get second entry data start address and len.
  1479. */
  1480. printf("## Flattened Device Tree from multi "
  1481. "component Image at %08lX\n",
  1482. (ulong)images->legacy_hdr_os);
  1483. image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
  1484. &fdt_len);
  1485. if (fdt_len) {
  1486. fdt_blob = (char *)fdt_data;
  1487. printf(" Booting using the fdt at 0x%p\n", fdt_blob);
  1488. if (fdt_check_header(fdt_blob) != 0) {
  1489. fdt_error("image is not a fdt");
  1490. goto error;
  1491. }
  1492. if (fdt_totalsize(fdt_blob) != fdt_len) {
  1493. fdt_error("fdt size != image size");
  1494. goto error;
  1495. }
  1496. } else {
  1497. debug("## No Flattened Device Tree\n");
  1498. return 0;
  1499. }
  1500. } else {
  1501. debug("## No Flattened Device Tree\n");
  1502. return 0;
  1503. }
  1504. *of_flat_tree = fdt_blob;
  1505. *of_size = fdt_totalsize(fdt_blob);
  1506. debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
  1507. (ulong)*of_flat_tree, *of_size);
  1508. return 0;
  1509. error:
  1510. *of_flat_tree = NULL;
  1511. *of_size = 0;
  1512. return 1;
  1513. }
  1514. #endif /* CONFIG_OF_LIBFDT */
  1515. #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
  1516. /**
  1517. * boot_get_cmdline - allocate and initialize kernel cmdline
  1518. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1519. * @cmd_start: pointer to a ulong variable, will hold cmdline start
  1520. * @cmd_end: pointer to a ulong variable, will hold cmdline end
  1521. *
  1522. * boot_get_cmdline() allocates space for kernel command line below
  1523. * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
  1524. * variable is present its contents is copied to allocated kernel
  1525. * command line.
  1526. *
  1527. * returns:
  1528. * 0 - success
  1529. * -1 - failure
  1530. */
  1531. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
  1532. {
  1533. char *cmdline;
  1534. char *s;
  1535. cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
  1536. getenv_bootm_mapsize() + getenv_bootm_low());
  1537. if (cmdline == NULL)
  1538. return -1;
  1539. if ((s = getenv("bootargs")) == NULL)
  1540. s = "";
  1541. strcpy(cmdline, s);
  1542. *cmd_start = (ulong) & cmdline[0];
  1543. *cmd_end = *cmd_start + strlen(cmdline);
  1544. debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  1545. return 0;
  1546. }
  1547. #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
  1548. #ifdef CONFIG_SYS_BOOT_GET_KBD
  1549. /**
  1550. * boot_get_kbd - allocate and initialize kernel copy of board info
  1551. * @lmb: pointer to lmb handle, will be used for memory mgmt
  1552. * @kbd: double pointer to board info data
  1553. *
  1554. * boot_get_kbd() allocates space for kernel copy of board info data below
  1555. * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
  1556. * with the current u-boot board info data.
  1557. *
  1558. * returns:
  1559. * 0 - success
  1560. * -1 - failure
  1561. */
  1562. int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
  1563. {
  1564. *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
  1565. getenv_bootm_mapsize() + getenv_bootm_low());
  1566. if (*kbd == NULL)
  1567. return -1;
  1568. **kbd = *(gd->bd);
  1569. debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  1570. #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  1571. do_bdinfo(NULL, 0, 0, NULL);
  1572. #endif
  1573. return 0;
  1574. }
  1575. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  1576. #endif /* !USE_HOSTCC */