image.c 25 KB

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