image.c 25 KB

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