image.c 26 KB

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