image.c 21 KB

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