image.c 26 KB

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