image.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. #define DEBUG
  26. #ifndef USE_HOSTCC
  27. #include <common.h>
  28. #include <watchdog.h>
  29. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  30. #include <status_led.h>
  31. #endif
  32. #ifdef CONFIG_HAS_DATAFLASH
  33. #include <dataflash.h>
  34. #endif
  35. #ifdef CONFIG_LOGBUFFER
  36. #include <logbuff.h>
  37. #endif
  38. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
  39. #include <rtc.h>
  40. #endif
  41. #if defined(CONFIG_FIT)
  42. #include <fdt.h>
  43. #include <libfdt.h>
  44. #include <fdt_support.h>
  45. #endif
  46. #ifdef CONFIG_CMD_BDI
  47. extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  48. #endif
  49. DECLARE_GLOBAL_DATA_PTR;
  50. static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
  51. int argc, char *argv[],
  52. ulong rd_addr, uint8_t arch, int verify);
  53. #else
  54. #include "mkimage.h"
  55. #endif /* USE_HOSTCC*/
  56. #include <image.h>
  57. unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
  58. int image_check_hcrc (image_header_t *hdr)
  59. {
  60. ulong hcrc;
  61. ulong len = image_get_header_size ();
  62. image_header_t header;
  63. /* Copy header so we can blank CRC field for re-calculation */
  64. memmove (&header, (char *)hdr, image_get_header_size ());
  65. image_set_hcrc (&header, 0);
  66. hcrc = crc32 (0, (unsigned char *)&header, len);
  67. return (hcrc == image_get_hcrc (hdr));
  68. }
  69. int image_check_dcrc (image_header_t *hdr)
  70. {
  71. ulong data = image_get_data (hdr);
  72. ulong len = image_get_data_size (hdr);
  73. ulong dcrc = crc32 (0, (unsigned char *)data, len);
  74. return (dcrc == image_get_dcrc (hdr));
  75. }
  76. #ifndef USE_HOSTCC
  77. int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
  78. {
  79. ulong dcrc = 0;
  80. ulong len = image_get_data_size (hdr);
  81. ulong data = image_get_data (hdr);
  82. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  83. ulong cdata = data;
  84. ulong edata = cdata + len;
  85. while (cdata < edata) {
  86. ulong chunk = edata - cdata;
  87. if (chunk > chunksz)
  88. chunk = chunksz;
  89. dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
  90. cdata += chunk;
  91. WATCHDOG_RESET ();
  92. }
  93. #else
  94. dcrc = crc32 (0, (unsigned char *)data, len);
  95. #endif
  96. return (dcrc == image_get_dcrc (hdr));
  97. }
  98. int getenv_verify (void)
  99. {
  100. char *s = getenv ("verify");
  101. return (s && (*s == 'n')) ? 0 : 1;
  102. }
  103. int getenv_autostart (void)
  104. {
  105. char *s = getenv ("autostart");
  106. return (s && (*s == 'n')) ? 0 : 1;
  107. }
  108. ulong getenv_bootm_low(void)
  109. {
  110. char *s = getenv ("bootm_low");
  111. if (s) {
  112. ulong tmp = simple_strtoul (s, NULL, 16);
  113. return tmp;
  114. }
  115. #ifdef CFG_SDRAM_BASE
  116. return CFG_SDRAM_BASE;
  117. #else
  118. return 0;
  119. #endif
  120. }
  121. ulong getenv_bootm_size(void)
  122. {
  123. char *s = getenv ("bootm_size");
  124. if (s) {
  125. ulong tmp = simple_strtoul (s, NULL, 16);
  126. return tmp;
  127. }
  128. return gd->bd->bi_memsize;
  129. }
  130. void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
  131. {
  132. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  133. while (len > 0) {
  134. size_t tail = (len > chunksz) ? chunksz : len;
  135. WATCHDOG_RESET ();
  136. memmove (to, from, tail);
  137. to += tail;
  138. from += tail;
  139. len -= tail;
  140. }
  141. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  142. memmove (to, from, len);
  143. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  144. }
  145. #endif /* USE_HOSTCC */
  146. /**
  147. * image_multi_count - get component (sub-image) count
  148. * @hdr: pointer to the header of the multi component image
  149. *
  150. * image_multi_count() returns number of components in a multi
  151. * component image.
  152. *
  153. * Note: no checking of the image type is done, caller must pass
  154. * a valid multi component image.
  155. *
  156. * returns:
  157. * number of components
  158. */
  159. ulong image_multi_count (image_header_t *hdr)
  160. {
  161. ulong i, count = 0;
  162. ulong *size;
  163. /* get start of the image payload, which in case of multi
  164. * component images that points to a table of component sizes */
  165. size = (ulong *)image_get_data (hdr);
  166. /* count non empty slots */
  167. for (i = 0; size[i]; ++i)
  168. count++;
  169. return count;
  170. }
  171. /**
  172. * image_multi_getimg - get component data address and size
  173. * @hdr: pointer to the header of the multi component image
  174. * @idx: index of the requested component
  175. * @data: pointer to a ulong variable, will hold component data address
  176. * @len: pointer to a ulong variable, will hold component size
  177. *
  178. * image_multi_getimg() returns size and data address for the requested
  179. * component in a multi component image.
  180. *
  181. * Note: no checking of the image type is done, caller must pass
  182. * a valid multi component image.
  183. *
  184. * returns:
  185. * data address and size of the component, if idx is valid
  186. * 0 in data and len, if idx is out of range
  187. */
  188. void image_multi_getimg (image_header_t *hdr, ulong idx,
  189. ulong *data, ulong *len)
  190. {
  191. int i;
  192. ulong *size;
  193. ulong offset, tail, count, img_data;
  194. /* get number of component */
  195. count = image_multi_count (hdr);
  196. /* get start of the image payload, which in case of multi
  197. * component images that points to a table of component sizes */
  198. size = (ulong *)image_get_data (hdr);
  199. /* get address of the proper component data start, which means
  200. * skipping sizes table (add 1 for last, null entry) */
  201. img_data = image_get_data (hdr) + (count + 1) * sizeof (ulong);
  202. if (idx < count) {
  203. *len = size[idx];
  204. offset = 0;
  205. tail = 0;
  206. /* go over all indices preceding requested component idx */
  207. for (i = 0; i < idx; i++) {
  208. /* add up i-th component size */
  209. offset += size[i];
  210. /* add up alignment for i-th component */
  211. tail += (4 - size[i] % 4);
  212. }
  213. /* calculate idx-th component data address */
  214. *data = img_data + offset + tail;
  215. } else {
  216. *len = 0;
  217. *data = 0;
  218. }
  219. }
  220. #ifndef USE_HOSTCC
  221. const char* image_get_os_name (uint8_t os)
  222. {
  223. const char *name;
  224. switch (os) {
  225. case IH_OS_INVALID: name = "Invalid OS"; break;
  226. case IH_OS_NETBSD: name = "NetBSD"; break;
  227. case IH_OS_LINUX: name = "Linux"; break;
  228. case IH_OS_VXWORKS: name = "VxWorks"; break;
  229. case IH_OS_QNX: name = "QNX"; break;
  230. case IH_OS_U_BOOT: name = "U-Boot"; break;
  231. case IH_OS_RTEMS: name = "RTEMS"; break;
  232. #ifdef CONFIG_ARTOS
  233. case IH_OS_ARTOS: name = "ARTOS"; break;
  234. #endif
  235. #ifdef CONFIG_LYNXKDI
  236. case IH_OS_LYNXOS: name = "LynxOS"; break;
  237. #endif
  238. default: name = "Unknown OS"; break;
  239. }
  240. return name;
  241. }
  242. const char* image_get_arch_name (uint8_t arch)
  243. {
  244. const char *name;
  245. switch (arch) {
  246. case IH_ARCH_INVALID: name = "Invalid Architecture"; break;
  247. case IH_ARCH_ALPHA: name = "Alpha"; break;
  248. case IH_ARCH_ARM: name = "ARM"; break;
  249. case IH_ARCH_AVR32: name = "AVR32"; break;
  250. case IH_ARCH_BLACKFIN: name = "Blackfin"; break;
  251. case IH_ARCH_I386: name = "Intel x86"; break;
  252. case IH_ARCH_IA64: name = "IA64"; break;
  253. case IH_ARCH_M68K: name = "M68K"; break;
  254. case IH_ARCH_MICROBLAZE:name = "Microblaze"; break;
  255. case IH_ARCH_MIPS64: name = "MIPS 64 Bit"; break;
  256. case IH_ARCH_MIPS: name = "MIPS"; break;
  257. case IH_ARCH_NIOS2: name = "Nios-II"; break;
  258. case IH_ARCH_NIOS: name = "Nios"; break;
  259. case IH_ARCH_PPC: name = "PowerPC"; break;
  260. case IH_ARCH_S390: name = "IBM S390"; break;
  261. case IH_ARCH_SH: name = "SuperH"; break;
  262. case IH_ARCH_SPARC64: name = "SPARC 64 Bit"; break;
  263. case IH_ARCH_SPARC: name = "SPARC"; break;
  264. default: name = "Unknown Architecture"; break;
  265. }
  266. return name;
  267. }
  268. const char* image_get_type_name (uint8_t type)
  269. {
  270. const char *name;
  271. switch (type) {
  272. case IH_TYPE_INVALID: name = "Invalid Image"; break;
  273. case IH_TYPE_STANDALONE:name = "Standalone Program"; break;
  274. case IH_TYPE_KERNEL: name = "Kernel Image"; break;
  275. case IH_TYPE_RAMDISK: name = "RAMDisk Image"; break;
  276. case IH_TYPE_MULTI: name = "Multi-File Image"; break;
  277. case IH_TYPE_FIRMWARE: name = "Firmware"; break;
  278. case IH_TYPE_SCRIPT: name = "Script"; break;
  279. case IH_TYPE_FLATDT: name = "Flat Device Tree"; break;
  280. default: name = "Unknown Image"; break;
  281. }
  282. return name;
  283. }
  284. const char* image_get_comp_name (uint8_t comp)
  285. {
  286. const char *name;
  287. switch (comp) {
  288. case IH_COMP_NONE: name = "uncompressed"; break;
  289. case IH_COMP_GZIP: name = "gzip compressed"; break;
  290. case IH_COMP_BZIP2: name = "bzip2 compressed"; break;
  291. default: name = "unknown compression"; break;
  292. }
  293. return name;
  294. }
  295. static void image_print_type (image_header_t *hdr)
  296. {
  297. const char *os, *arch, *type, *comp;
  298. os = image_get_os_name (image_get_os (hdr));
  299. arch = image_get_arch_name (image_get_arch (hdr));
  300. type = image_get_type_name (image_get_type (hdr));
  301. comp = image_get_comp_name (image_get_comp (hdr));
  302. printf ("%s %s %s (%s)", arch, os, type, comp);
  303. }
  304. void image_print_contents (image_header_t *hdr)
  305. {
  306. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
  307. time_t timestamp = (time_t)image_get_time (hdr);
  308. struct rtc_time tm;
  309. #endif
  310. printf (" Image Name: %.*s\n", IH_NMLEN, image_get_name (hdr));
  311. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
  312. to_tm (timestamp, &tm);
  313. printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  314. tm.tm_year, tm.tm_mon, tm.tm_mday,
  315. tm.tm_hour, tm.tm_min, tm.tm_sec);
  316. #endif
  317. puts (" Image Type: ");
  318. image_print_type (hdr);
  319. printf ("\n Data Size: %d Bytes = ", image_get_data_size (hdr));
  320. print_size (image_get_data_size (hdr), "\n");
  321. printf (" Load Address: %08x\n"
  322. " Entry Point: %08x\n",
  323. image_get_load (hdr), image_get_ep (hdr));
  324. if (image_check_type (hdr, IH_TYPE_MULTI)) {
  325. int i;
  326. ulong data, len;
  327. ulong count = image_multi_count (hdr);
  328. puts (" Contents:\n");
  329. for (i = 0; i < count; i++) {
  330. image_multi_getimg (hdr, i, &data, &len);
  331. printf (" Image %d: %8ld Bytes = ", i, len);
  332. print_size (len, "\n");
  333. }
  334. }
  335. }
  336. /**
  337. * gen_image_get_format - get image format type
  338. * @img_addr: image start address
  339. *
  340. * gen_image_get_format() checks whether provided address points to a valid
  341. * legacy or FIT image.
  342. *
  343. * New uImage format and FDT blob are based on a libfdt. FDT blob
  344. * may be passed directly or embedded in a FIT image. In both situations
  345. * gen_image_get_format() must be able to dectect libfdt header.
  346. *
  347. * returns:
  348. * image format type or IMAGE_FORMAT_INVALID if no image is present
  349. */
  350. int gen_image_get_format (void *img_addr)
  351. {
  352. ulong format = IMAGE_FORMAT_INVALID;
  353. image_header_t *hdr;
  354. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  355. char *fit_hdr;
  356. #endif
  357. hdr = (image_header_t *)img_addr;
  358. if (image_check_magic(hdr))
  359. format = IMAGE_FORMAT_LEGACY;
  360. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  361. else {
  362. fit_hdr = (char *)img_addr;
  363. if (fdt_check_header (fit_hdr) == 0)
  364. format = IMAGE_FORMAT_FIT;
  365. }
  366. #endif
  367. return format;
  368. }
  369. /**
  370. * gen_get_image - get image from special storage (if necessary)
  371. * @img_addr: image start address
  372. *
  373. * gen_get_image() checks if provided image start adddress is located
  374. * in a dataflash storage. If so, image is moved to a system RAM memory.
  375. *
  376. * returns:
  377. * image start address after possible relocation from special storage
  378. */
  379. ulong gen_get_image (ulong img_addr)
  380. {
  381. ulong ram_addr = img_addr;
  382. #ifdef CONFIG_HAS_DATAFLASH
  383. ulong h_size, d_size;
  384. if (addr_dataflash (img_addr)){
  385. /* ger RAM address */
  386. ram_addr = CFG_LOAD_ADDR;
  387. /* get header size */
  388. h_size = image_get_header_size ();
  389. #if defined(CONFIG_FIT)
  390. if (sizeof(struct fdt_header) > h_size)
  391. h_size = sizeof(struct fdt_header);
  392. #endif
  393. /* read in header */
  394. debug (" Reading image header from dataflash address "
  395. "%08lx to RAM address %08lx\n", img_addr, ram_addr);
  396. read_dataflash (img_addr, h_size, (char *)ram_addr);
  397. /* get data size */
  398. switch (gen_image_get_format ((void *)ram_addr)) {
  399. case IMAGE_FORMAT_LEGACY:
  400. d_size = image_get_data_size ((image_header_t *)ram_addr);
  401. debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
  402. ram_addr, d_size);
  403. break;
  404. #if defined(CONFIG_FIT)
  405. case IMAGE_FORMAT_FIT:
  406. d_size = fdt_totalsize((void *)ram_addr) - h_size;
  407. debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
  408. ram_addr, d_size);
  409. break;
  410. #endif
  411. default:
  412. printf (" No valid image found at 0x%08lx\n", img_addr);
  413. return ram_addr;
  414. }
  415. /* read in image data */
  416. debug (" Reading image remaining data from dataflash address "
  417. "%08lx to RAM address %08lx\n", img_addr + h_size,
  418. ram_addr + h_size);
  419. read_dataflash (img_addr + h_size, d_size,
  420. (char *)(ram_addr + h_size));
  421. }
  422. #endif /* CONFIG_HAS_DATAFLASH */
  423. return ram_addr;
  424. }
  425. /**
  426. * image_get_ramdisk - get and verify ramdisk image
  427. * @cmdtp: command table pointer
  428. * @flag: command flag
  429. * @argc: command argument count
  430. * @argv: command argument list
  431. * @rd_addr: ramdisk image start address
  432. * @arch: expected ramdisk architecture
  433. * @verify: checksum verification flag
  434. *
  435. * image_get_ramdisk() returns a pointer to the verified ramdisk image
  436. * header. Routine receives image start address and expected architecture
  437. * flag. Verification done covers data and header integrity and os/type/arch
  438. * fields checking.
  439. *
  440. * If dataflash support is enabled routine checks for dataflash addresses
  441. * and handles required dataflash reads.
  442. *
  443. * returns:
  444. * pointer to a ramdisk image header, if image was found and valid
  445. * otherwise, return NULL
  446. */
  447. static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
  448. int argc, char *argv[],
  449. ulong rd_addr, uint8_t arch, int verify)
  450. {
  451. image_header_t *rd_hdr;
  452. show_boot_progress (9);
  453. rd_hdr = (image_header_t *)rd_addr;
  454. if (!image_check_magic (rd_hdr)) {
  455. puts ("Bad Magic Number\n");
  456. show_boot_progress (-10);
  457. return NULL;
  458. }
  459. if (!image_check_hcrc (rd_hdr)) {
  460. puts ("Bad Header Checksum\n");
  461. show_boot_progress (-11);
  462. return NULL;
  463. }
  464. show_boot_progress (10);
  465. image_print_contents (rd_hdr);
  466. if (verify) {
  467. puts(" Verifying Checksum ... ");
  468. if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
  469. puts ("Bad Data CRC\n");
  470. show_boot_progress (-12);
  471. return NULL;
  472. }
  473. puts("OK\n");
  474. }
  475. show_boot_progress (11);
  476. if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
  477. !image_check_arch (rd_hdr, arch) ||
  478. !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
  479. printf ("No Linux %s Ramdisk Image\n",
  480. image_get_arch_name(arch));
  481. show_boot_progress (-13);
  482. return NULL;
  483. }
  484. return rd_hdr;
  485. }
  486. /**
  487. * get_ramdisk - main ramdisk handling routine
  488. * @cmdtp: command table pointer
  489. * @flag: command flag
  490. * @argc: command argument count
  491. * @argv: command argument list
  492. * @images: pointer to the bootm images structure
  493. * @arch: expected ramdisk architecture
  494. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  495. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  496. *
  497. * get_ramdisk() is responsible for finding a valid ramdisk image.
  498. * Curently supported are the following ramdisk sources:
  499. * - multicomponent kernel/ramdisk image,
  500. * - commandline provided address of decicated ramdisk image.
  501. *
  502. * returns:
  503. * rd_start and rd_end are set to ramdisk start/end addresses if
  504. * ramdisk image is found and valid
  505. * rd_start and rd_end are set to 0 if no ramdisk exists
  506. * return 1 if ramdisk image is found but corrupted
  507. */
  508. int get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  509. bootm_headers_t *images, uint8_t arch,
  510. ulong *rd_start, ulong *rd_end)
  511. {
  512. ulong rd_addr, rd_load;
  513. ulong rd_data, rd_len;
  514. image_header_t *rd_hdr;
  515. #if defined(CONFIG_FIT)
  516. void *fit_hdr;
  517. const char *fit_uname_config = NULL;
  518. const char *fit_uname_ramdisk = NULL;
  519. ulong default_addr;
  520. #endif
  521. /*
  522. * Look for a '-' which indicates to ignore the
  523. * ramdisk argument
  524. */
  525. if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
  526. debug ("## Skipping init Ramdisk\n");
  527. rd_len = rd_data = 0;
  528. } else if (argc >= 3) {
  529. #if defined(CONFIG_FIT)
  530. /*
  531. * If the init ramdisk comes from the FIT image and the FIT image
  532. * address is omitted in the command line argument, try to use
  533. * os FIT image address or default load address.
  534. */
  535. if (images->fit_uname_os)
  536. default_addr = (ulong)images->fit_hdr_os;
  537. else
  538. default_addr = load_addr;
  539. if (fit_parse_conf (argv[2], default_addr,
  540. &rd_addr, &fit_uname_config)) {
  541. debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
  542. fit_uname_config, rd_addr);
  543. } else if (fit_parse_subimage (argv[2], default_addr,
  544. &rd_addr, &fit_uname_ramdisk)) {
  545. debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
  546. fit_uname_ramdisk, rd_addr);
  547. } else
  548. #endif
  549. {
  550. rd_addr = simple_strtoul(argv[2], NULL, 16);
  551. debug ("* ramdisk: cmdline image address = 0x%08lx\n",
  552. rd_addr);
  553. }
  554. /* copy from dataflash if needed */
  555. printf ("## Loading init Ramdisk Image at %08lx ...\n",
  556. rd_addr);
  557. rd_addr = gen_get_image (rd_addr);
  558. /*
  559. * Check if there is an initrd image at the
  560. * address provided in the second bootm argument
  561. * check image type, for FIT images get FIT node.
  562. */
  563. switch (gen_image_get_format ((void *)rd_addr)) {
  564. case IMAGE_FORMAT_LEGACY:
  565. debug ("* ramdisk: legacy format image\n");
  566. rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
  567. rd_addr, arch, images->verify);
  568. if (rd_hdr == NULL) {
  569. *rd_start = 0;
  570. *rd_end = 0;
  571. return 1;
  572. }
  573. rd_data = image_get_data (rd_hdr);
  574. rd_len = image_get_data_size (rd_hdr);
  575. rd_load = image_get_load (rd_hdr);
  576. break;
  577. #if defined(CONFIG_FIT)
  578. case IMAGE_FORMAT_FIT:
  579. fit_hdr = (void *)rd_addr;
  580. debug ("* ramdisk: FIT format image\n");
  581. fit_unsupported_reset ("ramdisk");
  582. return 1;
  583. #endif
  584. default:
  585. printf ("Wrong Image Format for %s command\n",
  586. cmdtp->name);
  587. rd_data = rd_len = 0;
  588. }
  589. #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
  590. /*
  591. * We need to copy the ramdisk to SRAM to let Linux boot
  592. */
  593. if (rd_data) {
  594. memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
  595. rd_data = rd_load;
  596. }
  597. #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
  598. } else if (images->legacy_hdr_valid &&
  599. image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
  600. /*
  601. * Now check if we have a legacy mult-component image,
  602. * get second entry data start address and len.
  603. */
  604. show_boot_progress (13);
  605. printf ("## Loading init Ramdisk from multi component "
  606. "Image at %08lx ...\n",
  607. (ulong)images->legacy_hdr_os);
  608. image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
  609. } else {
  610. /*
  611. * no initrd image
  612. */
  613. show_boot_progress (14);
  614. rd_len = rd_data = 0;
  615. }
  616. if (!rd_data) {
  617. debug ("## No init Ramdisk\n");
  618. *rd_start = 0;
  619. *rd_end = 0;
  620. } else {
  621. *rd_start = rd_data;
  622. *rd_end = rd_data + rd_len;
  623. }
  624. debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  625. *rd_start, *rd_end);
  626. return 0;
  627. }
  628. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  629. /**
  630. * ramdisk_high - relocate init ramdisk
  631. * @lmb: pointer to lmb handle, will be used for memory mgmt
  632. * @rd_data: ramdisk data start address
  633. * @rd_len: ramdisk data length
  634. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  635. * start address (after possible relocation)
  636. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  637. * end address (after possible relocation)
  638. *
  639. * ramdisk_high() takes a relocation hint from "initrd_high" environement
  640. * variable and if requested ramdisk data is moved to a specified location.
  641. *
  642. * returns:
  643. * - initrd_start and initrd_end are set to final (after relocation) ramdisk
  644. * start/end addresses if ramdisk image start and len were provided
  645. * otherwise set initrd_start and initrd_end set to zeros
  646. * - returns:
  647. * 0 - success
  648. * -1 - failure
  649. */
  650. int ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
  651. ulong *initrd_start, ulong *initrd_end)
  652. {
  653. char *s;
  654. ulong initrd_high;
  655. int initrd_copy_to_ram = 1;
  656. if ((s = getenv ("initrd_high")) != NULL) {
  657. /* a value of "no" or a similar string will act like 0,
  658. * turning the "load high" feature off. This is intentional.
  659. */
  660. initrd_high = simple_strtoul (s, NULL, 16);
  661. if (initrd_high == ~0)
  662. initrd_copy_to_ram = 0;
  663. } else {
  664. /* not set, no restrictions to load high */
  665. initrd_high = ~0;
  666. }
  667. debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  668. initrd_high, initrd_copy_to_ram);
  669. if (rd_data) {
  670. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  671. debug (" in-place initrd\n");
  672. *initrd_start = rd_data;
  673. *initrd_end = rd_data + rd_len;
  674. lmb_reserve(lmb, rd_data, rd_len);
  675. } else {
  676. if (initrd_high)
  677. *initrd_start = lmb_alloc_base(lmb, rd_len, 0x1000, initrd_high);
  678. else
  679. *initrd_start = lmb_alloc(lmb, rd_len, 0x1000);
  680. if (*initrd_start == 0) {
  681. puts("ramdisk - allocation error\n");
  682. goto error;
  683. }
  684. show_boot_progress (12);
  685. *initrd_end = *initrd_start + rd_len;
  686. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  687. *initrd_start, *initrd_end);
  688. memmove_wd((void *)*initrd_start,
  689. (void *)rd_data, rd_len, CHUNKSZ);
  690. puts ("OK\n");
  691. }
  692. } else {
  693. *initrd_start = 0;
  694. *initrd_end = 0;
  695. }
  696. debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  697. *initrd_start, *initrd_end);
  698. return 0;
  699. error:
  700. return -1;
  701. }
  702. /**
  703. * get_boot_cmdline - allocate and initialize kernel cmdline
  704. * @lmb: pointer to lmb handle, will be used for memory mgmt
  705. * @cmd_start: pointer to a ulong variable, will hold cmdline start
  706. * @cmd_end: pointer to a ulong variable, will hold cmdline end
  707. * @bootmap_base: ulong variable, holds offset in physical memory to
  708. * base of bootmap
  709. *
  710. * get_boot_cmdline() allocates space for kernel command line below
  711. * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
  712. * variable is present its contents is copied to allocated kernel
  713. * command line.
  714. *
  715. * returns:
  716. * 0 - success
  717. * -1 - failure
  718. */
  719. int get_boot_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
  720. ulong bootmap_base)
  721. {
  722. char *cmdline;
  723. char *s;
  724. cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
  725. CFG_BOOTMAPSZ + bootmap_base);
  726. if (cmdline == NULL)
  727. return -1;
  728. if ((s = getenv("bootargs")) == NULL)
  729. s = "";
  730. strcpy(cmdline, s);
  731. *cmd_start = (ulong) & cmdline[0];
  732. *cmd_end = *cmd_start + strlen(cmdline);
  733. debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  734. return 0;
  735. }
  736. /**
  737. * get_boot_kbd - allocate and initialize kernel copy of board info
  738. * @lmb: pointer to lmb handle, will be used for memory mgmt
  739. * @kbd: double pointer to board info data
  740. * @bootmap_base: ulong variable, holds offset in physical memory to
  741. * base of bootmap
  742. *
  743. * get_boot_kbd() allocates space for kernel copy of board info data below
  744. * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
  745. * the current u-boot board info data.
  746. *
  747. * returns:
  748. * 0 - success
  749. * -1 - failure
  750. */
  751. int get_boot_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
  752. {
  753. *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
  754. CFG_BOOTMAPSZ + bootmap_base);
  755. if (*kbd == NULL)
  756. return -1;
  757. **kbd = *(gd->bd);
  758. debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  759. #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  760. do_bdinfo(NULL, 0, 0, NULL);
  761. #endif
  762. return 0;
  763. }
  764. #endif /* CONFIG_PPC || CONFIG_M68K */
  765. #if defined(CONFIG_FIT)
  766. /*****************************************************************************/
  767. /* New uImage format routines */
  768. /*****************************************************************************/
  769. static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
  770. ulong *addr, const char **name)
  771. {
  772. const char *sep;
  773. *addr = addr_curr;
  774. *name = NULL;
  775. sep = strchr (spec, sepc);
  776. if (sep) {
  777. if (sep - spec > 0)
  778. *addr = simple_strtoul (spec, NULL, 16);
  779. *name = sep + 1;
  780. return 1;
  781. }
  782. return 0;
  783. }
  784. /**
  785. * fit_parse_conf - parse FIT configuration spec
  786. * @spec: input string, containing configuration spec
  787. * @add_curr: current image address (to be used as a possible default)
  788. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  789. * configuration
  790. * @conf_name double pointer to a char, will hold pointer to a configuration
  791. * unit name
  792. *
  793. * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
  794. * where <addr> is a FIT image address that contains configuration
  795. * with a <conf> unit name.
  796. *
  797. * Address part is optional, and if omitted default add_curr will
  798. * be used instead.
  799. *
  800. * returns:
  801. * 1 if spec is a valid configuration string,
  802. * addr and conf_name are set accordingly
  803. * 0 otherwise
  804. */
  805. inline int fit_parse_conf (const char *spec, ulong addr_curr,
  806. ulong *addr, const char **conf_name)
  807. {
  808. return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
  809. }
  810. /**
  811. * fit_parse_subimage - parse FIT subimage spec
  812. * @spec: input string, containing subimage spec
  813. * @add_curr: current image address (to be used as a possible default)
  814. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  815. * subimage
  816. * @image_name: double pointer to a char, will hold pointer to a subimage name
  817. *
  818. * fit_parse_subimage() expects subimage spec in the for of
  819. * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
  820. * subimage with a <subimg> unit name.
  821. *
  822. * Address part is optional, and if omitted default add_curr will
  823. * be used instead.
  824. *
  825. * returns:
  826. * 1 if spec is a valid subimage string,
  827. * addr and image_name are set accordingly
  828. * 0 otherwise
  829. */
  830. inline int fit_parse_subimage (const char *spec, ulong addr_curr,
  831. ulong *addr, const char **image_name)
  832. {
  833. return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
  834. }
  835. #endif /* CONFIG_FIT */
  836. #endif /* USE_HOSTCC */