cmd_bootm.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Boot support
  25. */
  26. #include <common.h>
  27. #include <watchdog.h>
  28. #include <command.h>
  29. #include <cmd_boot.h>
  30. #include <image.h>
  31. #include <malloc.h>
  32. #include <zlib.h>
  33. #include <environment.h>
  34. #include <asm/byteorder.h>
  35. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  36. #include <rtc.h>
  37. #endif
  38. #ifdef CFG_HUSH_PARSER
  39. #include <hush.h>
  40. #endif
  41. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  42. # include <status_led.h>
  43. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  44. #else
  45. # define SHOW_BOOT_PROGRESS(arg)
  46. #endif
  47. #ifdef CFG_INIT_RAM_LOCK
  48. #include <asm/cache.h>
  49. #endif
  50. #ifdef CONFIG_LOGBUFFER
  51. #include <logbuff.h>
  52. #endif
  53. /*
  54. * Some systems (for example LWMON) have very short watchdog periods;
  55. * we must make sure to split long operations like memmove() or
  56. * crc32() into reasonable chunks.
  57. */
  58. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  59. # define CHUNKSZ (64 * 1024)
  60. #endif
  61. int gunzip (void *, int, unsigned char *, int *);
  62. static void *zalloc(void *, unsigned, unsigned);
  63. static void zfree(void *, void *, unsigned);
  64. #if (CONFIG_COMMANDS & CFG_CMD_IMI)
  65. static int image_info (unsigned long addr);
  66. #endif
  67. static void print_type (image_header_t *hdr);
  68. #ifdef __I386__
  69. image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
  70. #endif
  71. /*
  72. * Continue booting an OS image; caller already has:
  73. * - copied image header to global variable `header'
  74. * - checked header magic number, checksums (both header & image),
  75. * - verified image architecture (PPC) and type (KERNEL or MULTI),
  76. * - loaded (first part of) image to header load address,
  77. * - disabled interrupts.
  78. */
  79. typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
  80. int argc, char *argv[],
  81. ulong addr, /* of image to boot */
  82. ulong *len_ptr, /* multi-file image length table */
  83. int verify); /* getenv("verify")[0] != 'n' */
  84. #ifdef CONFIG_PPC
  85. static boot_os_Fcn do_bootm_linux;
  86. #else
  87. extern boot_os_Fcn do_bootm_linux;
  88. #endif
  89. static boot_os_Fcn do_bootm_netbsd;
  90. static boot_os_Fcn do_bootm_rtems;
  91. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  92. static boot_os_Fcn do_bootm_vxworks;
  93. static boot_os_Fcn do_bootm_qnxelf;
  94. int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
  95. int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
  96. #endif /* CFG_CMD_ELF */
  97. #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
  98. static boot_os_Fcn do_bootm_artos;
  99. #endif
  100. image_header_t header;
  101. ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
  102. int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  103. {
  104. ulong iflag;
  105. ulong addr;
  106. ulong data, len, checksum;
  107. ulong *len_ptr;
  108. int i, verify;
  109. char *name, *s;
  110. int (*appl)(cmd_tbl_t *, int, int, char *[]);
  111. image_header_t *hdr = &header;
  112. s = getenv ("verify");
  113. verify = (s && (*s == 'n')) ? 0 : 1;
  114. if (argc < 2) {
  115. addr = load_addr;
  116. } else {
  117. addr = simple_strtoul(argv[1], NULL, 16);
  118. }
  119. SHOW_BOOT_PROGRESS (1);
  120. printf ("## Booting image at %08lx ...\n", addr);
  121. /* Copy header so we can blank CRC field for re-calculation */
  122. memmove (&header, (char *)addr, sizeof(image_header_t));
  123. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  124. #ifdef __I386__ /* correct image format not implemented yet - fake it */
  125. if (fake_header(hdr, (void*)addr, -1) != NULL) {
  126. /* to compensate for the addition below */
  127. addr -= sizeof(image_header_t);
  128. /* turnof verify,
  129. * fake_header() does not fake the data crc
  130. */
  131. verify = 0;
  132. } else
  133. #endif /* __I386__ */
  134. {
  135. printf ("Bad Magic Number\n");
  136. SHOW_BOOT_PROGRESS (-1);
  137. return 1;
  138. }
  139. }
  140. SHOW_BOOT_PROGRESS (2);
  141. data = (ulong)&header;
  142. len = sizeof(image_header_t);
  143. checksum = ntohl(hdr->ih_hcrc);
  144. hdr->ih_hcrc = 0;
  145. if (crc32 (0, (char *)data, len) != checksum) {
  146. printf ("Bad Header Checksum\n");
  147. SHOW_BOOT_PROGRESS (-2);
  148. return 1;
  149. }
  150. SHOW_BOOT_PROGRESS (3);
  151. /* for multi-file images we need the data part, too */
  152. print_image_hdr (hdr);
  153. data = addr + sizeof(image_header_t);
  154. len = ntohl(hdr->ih_size);
  155. if (verify) {
  156. printf (" Verifying Checksum ... ");
  157. if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
  158. printf ("Bad Data CRC\n");
  159. SHOW_BOOT_PROGRESS (-3);
  160. return 1;
  161. }
  162. printf ("OK\n");
  163. }
  164. SHOW_BOOT_PROGRESS (4);
  165. len_ptr = (ulong *)data;
  166. #if defined(__PPC__)
  167. if (hdr->ih_arch != IH_CPU_PPC)
  168. #elif defined(__ARM__)
  169. if (hdr->ih_arch != IH_CPU_ARM)
  170. #elif defined(__I386__)
  171. if (hdr->ih_arch != IH_CPU_I386)
  172. #elif defined(__mips__)
  173. if (hdr->ih_arch != IH_CPU_MIPS)
  174. #else
  175. # error Unknown CPU type
  176. #endif
  177. {
  178. printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
  179. SHOW_BOOT_PROGRESS (-4);
  180. return 1;
  181. }
  182. SHOW_BOOT_PROGRESS (5);
  183. switch (hdr->ih_type) {
  184. case IH_TYPE_STANDALONE: name = "Standalone Application";
  185. break;
  186. case IH_TYPE_KERNEL: name = "Kernel Image";
  187. break;
  188. case IH_TYPE_MULTI: name = "Multi-File Image";
  189. len = ntohl(len_ptr[0]);
  190. /* OS kernel is always the first image */
  191. data += 8; /* kernel_len + terminator */
  192. for (i=1; len_ptr[i]; ++i)
  193. data += 4;
  194. break;
  195. default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
  196. SHOW_BOOT_PROGRESS (-5);
  197. return 1;
  198. }
  199. SHOW_BOOT_PROGRESS (6);
  200. /*
  201. * We have reached the point of no return: we are going to
  202. * overwrite all exception vector code, so we cannot easily
  203. * recover from any failures any more...
  204. */
  205. iflag = disable_interrupts();
  206. #ifdef CONFIG_AMIGAONEG3SE
  207. /*
  208. * We've possible left the caches enabled during
  209. * bios emulation, so turn them off again
  210. */
  211. icache_disable();
  212. invalidate_l1_instruction_cache();
  213. flush_data_cache();
  214. dcache_disable();
  215. #endif
  216. switch (hdr->ih_comp) {
  217. case IH_COMP_NONE:
  218. if(ntohl(hdr->ih_load) == addr) {
  219. printf (" XIP %s ... ", name);
  220. } else {
  221. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  222. size_t l = len;
  223. void *to = (void *)ntohl(hdr->ih_load);
  224. void *from = (void *)data;
  225. printf (" Loading %s ... ", name);
  226. while (l > 0) {
  227. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  228. WATCHDOG_RESET();
  229. memmove (to, from, tail);
  230. to += tail;
  231. from += tail;
  232. l -= tail;
  233. }
  234. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  235. memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
  236. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  237. }
  238. break;
  239. case IH_COMP_GZIP:
  240. printf (" Uncompressing %s ... ", name);
  241. if (gunzip ((void *)ntohl(hdr->ih_load), 0x400000,
  242. (uchar *)data, (int *)&len) != 0) {
  243. printf ("GUNZIP ERROR - must RESET board to recover\n");
  244. SHOW_BOOT_PROGRESS (-6);
  245. do_reset (cmdtp, flag, argc, argv);
  246. }
  247. break;
  248. default:
  249. if (iflag)
  250. enable_interrupts();
  251. printf ("Unimplemented compression type %d\n", hdr->ih_comp);
  252. SHOW_BOOT_PROGRESS (-7);
  253. return 1;
  254. }
  255. printf ("OK\n");
  256. SHOW_BOOT_PROGRESS (7);
  257. switch (hdr->ih_type) {
  258. case IH_TYPE_STANDALONE:
  259. if (iflag)
  260. enable_interrupts();
  261. /* load (and uncompress), but don't start if "autostart"
  262. * is set to "no"
  263. */
  264. if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0))
  265. return 0;
  266. appl = (int (*)(cmd_tbl_t *, int, int, char *[]))ntohl(hdr->ih_ep);
  267. (*appl)(cmdtp, flag, argc-1, &argv[1]);
  268. return 0;
  269. case IH_TYPE_KERNEL:
  270. case IH_TYPE_MULTI:
  271. /* handled below */
  272. break;
  273. default:
  274. if (iflag)
  275. enable_interrupts();
  276. printf ("Can't boot image type %d\n", hdr->ih_type);
  277. SHOW_BOOT_PROGRESS (-8);
  278. return 1;
  279. }
  280. SHOW_BOOT_PROGRESS (8);
  281. switch (hdr->ih_os) {
  282. default: /* handled by (original) Linux case */
  283. case IH_OS_LINUX:
  284. do_bootm_linux (cmdtp, flag, argc, argv,
  285. addr, len_ptr, verify);
  286. break;
  287. case IH_OS_NETBSD:
  288. do_bootm_netbsd (cmdtp, flag, argc, argv,
  289. addr, len_ptr, verify);
  290. break;
  291. case IH_OS_RTEMS:
  292. do_bootm_rtems (cmdtp, flag, argc, argv,
  293. addr, len_ptr, verify);
  294. break;
  295. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  296. case IH_OS_VXWORKS:
  297. do_bootm_vxworks (cmdtp, flag, argc, argv,
  298. addr, len_ptr, verify);
  299. break;
  300. case IH_OS_QNX:
  301. do_bootm_qnxelf (cmdtp, flag, argc, argv,
  302. addr, len_ptr, verify);
  303. break;
  304. #endif /* CFG_CMD_ELF */
  305. #ifdef CONFIG_ARTOS
  306. case IH_OS_ARTOS:
  307. do_bootm_artos (cmdtp, flag, argc, argv,
  308. addr, len_ptr, verify);
  309. break;
  310. #endif
  311. }
  312. SHOW_BOOT_PROGRESS (-9);
  313. #ifdef DEBUG
  314. printf ("\n## Control returned to monitor - resetting...\n");
  315. do_reset (cmdtp, flag, argc, argv);
  316. #endif
  317. return 1;
  318. }
  319. #ifdef CONFIG_PPC
  320. static void
  321. do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
  322. int argc, char *argv[],
  323. ulong addr,
  324. ulong *len_ptr,
  325. int verify)
  326. {
  327. DECLARE_GLOBAL_DATA_PTR;
  328. ulong sp;
  329. ulong len, checksum;
  330. ulong initrd_start, initrd_end;
  331. ulong cmd_start, cmd_end;
  332. ulong initrd_high;
  333. ulong data;
  334. int initrd_copy_to_ram = 1;
  335. char *cmdline;
  336. char *s;
  337. bd_t *kbd;
  338. void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
  339. image_header_t *hdr = &header;
  340. if ((s = getenv ("initrd_high")) != NULL) {
  341. /* a value of "no" or a similar string will act like 0,
  342. * turning the "load high" feature off. This is intentional.
  343. */
  344. initrd_high = simple_strtoul(s, NULL, 16);
  345. if (initrd_high == ~0)
  346. initrd_copy_to_ram = 0;
  347. } else { /* not set, no restrictions to load high */
  348. initrd_high = ~0;
  349. }
  350. #ifdef CONFIG_LOGBUFFER
  351. kbd=gd->bd;
  352. /* Prevent initrd from overwriting logbuffer */
  353. if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
  354. initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
  355. debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
  356. #endif
  357. /*
  358. * Booting a (Linux) kernel image
  359. *
  360. * Allocate space for command line and board info - the
  361. * address should be as high as possible within the reach of
  362. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  363. * memory, which means far enough below the current stack
  364. * pointer.
  365. */
  366. asm( "mr %0,1": "=r"(sp) : );
  367. debug ("## Current stack ends at 0x%08lX ", sp);
  368. sp -= 2048; /* just to be sure */
  369. if (sp > CFG_BOOTMAPSZ)
  370. sp = CFG_BOOTMAPSZ;
  371. sp &= ~0xF;
  372. debug ("=> set upper limit to 0x%08lX\n", sp);
  373. cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
  374. kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
  375. if ((s = getenv("bootargs")) == NULL)
  376. s = "";
  377. strcpy (cmdline, s);
  378. cmd_start = (ulong)&cmdline[0];
  379. cmd_end = cmd_start + strlen(cmdline);
  380. *kbd = *(gd->bd);
  381. #ifdef DEBUG
  382. printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
  383. do_bdinfo (NULL, 0, 0, NULL);
  384. #endif
  385. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  386. /* convert all clock information to MHz */
  387. kbd->bi_intfreq /= 1000000L;
  388. kbd->bi_busfreq /= 1000000L;
  389. #if defined(CONFIG_8260)
  390. kbd->bi_cpmfreq /= 1000000L;
  391. kbd->bi_brgfreq /= 1000000L;
  392. kbd->bi_sccfreq /= 1000000L;
  393. kbd->bi_vco /= 1000000L;
  394. #endif /* CONFIG_8260 */
  395. }
  396. kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))hdr->ih_ep;
  397. /*
  398. * Check if there is an initrd image
  399. */
  400. if (argc >= 3) {
  401. SHOW_BOOT_PROGRESS (9);
  402. addr = simple_strtoul(argv[2], NULL, 16);
  403. printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
  404. /* Copy header so we can blank CRC field for re-calculation */
  405. memmove (&header, (char *)addr, sizeof(image_header_t));
  406. if (hdr->ih_magic != IH_MAGIC) {
  407. printf ("Bad Magic Number\n");
  408. SHOW_BOOT_PROGRESS (-10);
  409. do_reset (cmdtp, flag, argc, argv);
  410. }
  411. data = (ulong)&header;
  412. len = sizeof(image_header_t);
  413. checksum = hdr->ih_hcrc;
  414. hdr->ih_hcrc = 0;
  415. if (crc32 (0, (char *)data, len) != checksum) {
  416. printf ("Bad Header Checksum\n");
  417. SHOW_BOOT_PROGRESS (-11);
  418. do_reset (cmdtp, flag, argc, argv);
  419. }
  420. SHOW_BOOT_PROGRESS (10);
  421. print_image_hdr (hdr);
  422. data = addr + sizeof(image_header_t);
  423. len = hdr->ih_size;
  424. if (verify) {
  425. ulong csum = 0;
  426. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  427. ulong cdata = data, edata = cdata + len;
  428. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  429. printf (" Verifying Checksum ... ");
  430. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  431. while (cdata < edata) {
  432. ulong chunk = edata - cdata;
  433. if (chunk > CHUNKSZ)
  434. chunk = CHUNKSZ;
  435. csum = crc32 (csum, (char *)cdata, chunk);
  436. cdata += chunk;
  437. WATCHDOG_RESET();
  438. }
  439. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  440. csum = crc32 (0, (char *)data, len);
  441. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  442. if (csum != hdr->ih_dcrc) {
  443. printf ("Bad Data CRC\n");
  444. SHOW_BOOT_PROGRESS (-12);
  445. do_reset (cmdtp, flag, argc, argv);
  446. }
  447. printf ("OK\n");
  448. }
  449. SHOW_BOOT_PROGRESS (11);
  450. if ((hdr->ih_os != IH_OS_LINUX) ||
  451. (hdr->ih_arch != IH_CPU_PPC) ||
  452. (hdr->ih_type != IH_TYPE_RAMDISK) ) {
  453. printf ("No Linux PPC Ramdisk Image\n");
  454. SHOW_BOOT_PROGRESS (-13);
  455. do_reset (cmdtp, flag, argc, argv);
  456. }
  457. /*
  458. * Now check if we have a multifile image
  459. */
  460. } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
  461. u_long tail = ntohl(len_ptr[0]) % 4;
  462. int i;
  463. SHOW_BOOT_PROGRESS (13);
  464. /* skip kernel length and terminator */
  465. data = (ulong)(&len_ptr[2]);
  466. /* skip any additional image length fields */
  467. for (i=1; len_ptr[i]; ++i)
  468. data += 4;
  469. /* add kernel length, and align */
  470. data += ntohl(len_ptr[0]);
  471. if (tail) {
  472. data += 4 - tail;
  473. }
  474. len = ntohl(len_ptr[1]);
  475. } else {
  476. /*
  477. * no initrd image
  478. */
  479. SHOW_BOOT_PROGRESS (14);
  480. len = data = 0;
  481. }
  482. if (!data) {
  483. debug ("No initrd\n");
  484. }
  485. if (data) {
  486. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  487. initrd_start = data;
  488. initrd_end = initrd_start + len;
  489. } else {
  490. initrd_start = (ulong)kbd - len;
  491. initrd_start &= ~(4096 - 1); /* align on page */
  492. if (initrd_high) {
  493. ulong nsp;
  494. /*
  495. * the inital ramdisk does not need to be within
  496. * CFG_BOOTMAPSZ as it is not accessed until after
  497. * the mm system is initialised.
  498. *
  499. * do the stack bottom calculation again and see if
  500. * the initrd will fit just below the monitor stack
  501. * bottom without overwriting the area allocated
  502. * above for command line args and board info.
  503. */
  504. asm( "mr %0,1": "=r"(nsp) : );
  505. nsp -= 2048; /* just to be sure */
  506. nsp &= ~0xF;
  507. if (nsp > initrd_high) /* limit as specified */
  508. nsp = initrd_high;
  509. nsp -= len;
  510. nsp &= ~(4096 - 1); /* align on page */
  511. if (nsp >= sp)
  512. initrd_start = nsp;
  513. }
  514. SHOW_BOOT_PROGRESS (12);
  515. debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  516. data, data + len - 1, len, len);
  517. initrd_end = initrd_start + len;
  518. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  519. initrd_start, initrd_end);
  520. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  521. {
  522. size_t l = len;
  523. void *to = (void *)initrd_start;
  524. void *from = (void *)data;
  525. while (l > 0) {
  526. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  527. WATCHDOG_RESET();
  528. memmove (to, from, tail);
  529. to += tail;
  530. from += tail;
  531. l -= tail;
  532. }
  533. }
  534. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  535. memmove ((void *)initrd_start, (void *)data, len);
  536. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  537. printf ("OK\n");
  538. }
  539. } else {
  540. initrd_start = 0;
  541. initrd_end = 0;
  542. }
  543. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  544. (ulong)kernel);
  545. SHOW_BOOT_PROGRESS (15);
  546. #ifdef CFG_INIT_RAM_LOCK
  547. unlock_ram_in_cache();
  548. #endif
  549. /*
  550. * Linux Kernel Parameters:
  551. * r3: ptr to board info data
  552. * r4: initrd_start or 0 if no initrd
  553. * r5: initrd_end - unused if r4 is 0
  554. * r6: Start of command line string
  555. * r7: End of command line string
  556. */
  557. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  558. }
  559. #endif /* CONFIG_PPC */
  560. static void
  561. do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
  562. int argc, char *argv[],
  563. ulong addr,
  564. ulong *len_ptr,
  565. int verify)
  566. {
  567. DECLARE_GLOBAL_DATA_PTR;
  568. image_header_t *hdr = &header;
  569. void (*loader)(bd_t *, image_header_t *, char *, char *);
  570. image_header_t *img_addr;
  571. char *consdev;
  572. char *cmdline;
  573. /*
  574. * Booting a (NetBSD) kernel image
  575. *
  576. * This process is pretty similar to a standalone application:
  577. * The (first part of an multi-) image must be a stage-2 loader,
  578. * which in turn is responsible for loading & invoking the actual
  579. * kernel. The only differences are the parameters being passed:
  580. * besides the board info strucure, the loader expects a command
  581. * line, the name of the console device, and (optionally) the
  582. * address of the original image header.
  583. */
  584. img_addr = 0;
  585. if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
  586. img_addr = (image_header_t *) addr;
  587. consdev = "";
  588. #if defined (CONFIG_8xx_CONS_SMC1)
  589. consdev = "smc1";
  590. #elif defined (CONFIG_8xx_CONS_SMC2)
  591. consdev = "smc2";
  592. #elif defined (CONFIG_8xx_CONS_SCC2)
  593. consdev = "scc2";
  594. #elif defined (CONFIG_8xx_CONS_SCC3)
  595. consdev = "scc3";
  596. #endif
  597. if (argc > 2) {
  598. ulong len;
  599. int i;
  600. for (i=2, len=0 ; i<argc ; i+=1)
  601. len += strlen (argv[i]) + 1;
  602. cmdline = malloc (len);
  603. for (i=2, len=0 ; i<argc ; i+=1) {
  604. if (i > 2)
  605. cmdline[len++] = ' ';
  606. strcpy (&cmdline[len], argv[i]);
  607. len += strlen (argv[i]);
  608. }
  609. } else if ((cmdline = getenv("bootargs")) == NULL) {
  610. cmdline = "";
  611. }
  612. loader = (void (*)(bd_t *, image_header_t *, char *, char *)) hdr->ih_ep;
  613. printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
  614. (ulong)loader);
  615. SHOW_BOOT_PROGRESS (15);
  616. /*
  617. * NetBSD Stage-2 Loader Parameters:
  618. * r3: ptr to board info data
  619. * r4: image address
  620. * r5: console device
  621. * r6: boot args string
  622. */
  623. (*loader) (gd->bd, img_addr, consdev, cmdline);
  624. }
  625. #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
  626. /* Function that returns a character from the environment */
  627. extern uchar (*env_get_char)(int);
  628. static void
  629. do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
  630. int argc, char *argv[],
  631. ulong addr,
  632. ulong *len_ptr,
  633. int verify)
  634. {
  635. DECLARE_GLOBAL_DATA_PTR;
  636. ulong top;
  637. char *s, *cmdline;
  638. char **fwenv, **ss;
  639. int i, j, nxt, len, envno, envsz;
  640. bd_t *kbd;
  641. void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
  642. image_header_t *hdr = &header;
  643. /*
  644. * Booting an ARTOS kernel image + application
  645. */
  646. /* this used to be the top of memory, but was wrong... */
  647. #ifdef CONFIG_PPC
  648. /* get stack pointer */
  649. asm volatile ("mr %0,1" : "=r"(top) );
  650. #endif
  651. debug ("## Current stack ends at 0x%08lX ", top);
  652. top -= 2048; /* just to be sure */
  653. if (top > CFG_BOOTMAPSZ)
  654. top = CFG_BOOTMAPSZ;
  655. top &= ~0xF;
  656. debug ("=> set upper limit to 0x%08lX\n", top);
  657. /* first check the artos specific boot args, then the linux args*/
  658. if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
  659. s = "";
  660. /* get length of cmdline, and place it */
  661. len = strlen(s);
  662. top = (top - (len + 1)) & ~0xF;
  663. cmdline = (char *)top;
  664. debug ("## cmdline at 0x%08lX ", top);
  665. strcpy(cmdline, s);
  666. /* copy bdinfo */
  667. top = (top - sizeof(bd_t)) & ~0xF;
  668. debug ("## bd at 0x%08lX ", top);
  669. kbd = (bd_t *)top;
  670. memcpy(kbd, gd->bd, sizeof(bd_t));
  671. /* first find number of env entries, and their size */
  672. envno = 0;
  673. envsz = 0;
  674. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  675. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  676. ;
  677. envno++;
  678. envsz += (nxt - i) + 1; /* plus trailing zero */
  679. }
  680. envno++; /* plus the terminating zero */
  681. debug ("## %u envvars total size %u ", envno, envsz);
  682. top = (top - sizeof(char **)*envno) & ~0xF;
  683. fwenv = (char **)top;
  684. debug ("## fwenv at 0x%08lX ", top);
  685. top = (top - envsz) & ~0xF;
  686. s = (char *)top;
  687. ss = fwenv;
  688. /* now copy them */
  689. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  690. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  691. ;
  692. *ss++ = s;
  693. for (j = i; j < nxt; ++j)
  694. *s++ = env_get_char(j);
  695. *s++ = '\0';
  696. }
  697. *ss++ = NULL; /* terminate */
  698. entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
  699. (*entry)(kbd, cmdline, fwenv, top);
  700. }
  701. #endif
  702. #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
  703. int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  704. {
  705. int rcode = 0;
  706. #ifndef CFG_HUSH_PARSER
  707. if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
  708. #else
  709. if (parse_string_outer(getenv("bootcmd"),
  710. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
  711. #endif
  712. return rcode;
  713. }
  714. #endif
  715. #if (CONFIG_COMMANDS & CFG_CMD_IMI)
  716. int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  717. {
  718. int arg;
  719. ulong addr;
  720. int rcode=0;
  721. if (argc < 2) {
  722. return image_info (load_addr);
  723. }
  724. for (arg=1; arg <argc; ++arg) {
  725. addr = simple_strtoul(argv[arg], NULL, 16);
  726. if (image_info (addr) != 0) rcode = 1;
  727. }
  728. return rcode;
  729. }
  730. static int image_info (ulong addr)
  731. {
  732. ulong data, len, checksum;
  733. image_header_t *hdr = &header;
  734. printf ("\n## Checking Image at %08lx ...\n", addr);
  735. /* Copy header so we can blank CRC field for re-calculation */
  736. memmove (&header, (char *)addr, sizeof(image_header_t));
  737. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  738. printf (" Bad Magic Number\n");
  739. return 1;
  740. }
  741. data = (ulong)&header;
  742. len = sizeof(image_header_t);
  743. checksum = ntohl(hdr->ih_hcrc);
  744. hdr->ih_hcrc = 0;
  745. if (crc32 (0, (char *)data, len) != checksum) {
  746. printf (" Bad Header Checksum\n");
  747. return 1;
  748. }
  749. /* for multi-file images we need the data part, too */
  750. print_image_hdr ((image_header_t *)addr);
  751. data = addr + sizeof(image_header_t);
  752. len = ntohl(hdr->ih_size);
  753. printf (" Verifying Checksum ... ");
  754. if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
  755. printf (" Bad Data CRC\n");
  756. return 1;
  757. }
  758. printf ("OK\n");
  759. return 0;
  760. }
  761. #endif /* CFG_CMD_IMI */
  762. void
  763. print_image_hdr (image_header_t *hdr)
  764. {
  765. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  766. time_t timestamp = (time_t)ntohl(hdr->ih_time);
  767. struct rtc_time tm;
  768. #endif
  769. printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
  770. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  771. to_tm (timestamp, &tm);
  772. printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  773. tm.tm_year, tm.tm_mon, tm.tm_mday,
  774. tm.tm_hour, tm.tm_min, tm.tm_sec);
  775. #endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
  776. printf (" Image Type: "); print_type(hdr); printf ("\n");
  777. printf (" Data Size: %d Bytes = ", ntohl(hdr->ih_size));
  778. print_size (ntohl(hdr->ih_size), "\n");
  779. printf (" Load Address: %08x\n", ntohl(hdr->ih_load));
  780. printf (" Entry Point: %08x\n", ntohl(hdr->ih_ep));
  781. if (hdr->ih_type == IH_TYPE_MULTI) {
  782. int i;
  783. ulong len;
  784. ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
  785. printf (" Contents:\n");
  786. for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
  787. printf (" Image %d: %8ld Bytes = ", i, len);
  788. print_size (len, "\n");
  789. }
  790. }
  791. }
  792. static void
  793. print_type (image_header_t *hdr)
  794. {
  795. char *os, *arch, *type, *comp;
  796. switch (hdr->ih_os) {
  797. case IH_OS_INVALID: os = "Invalid OS"; break;
  798. case IH_OS_NETBSD: os = "NetBSD"; break;
  799. case IH_OS_LINUX: os = "Linux"; break;
  800. case IH_OS_VXWORKS: os = "VxWorks"; break;
  801. case IH_OS_QNX: os = "QNX"; break;
  802. case IH_OS_U_BOOT: os = "U-Boot"; break;
  803. case IH_OS_RTEMS: os = "RTEMS"; break;
  804. #ifdef CONFIG_ARTOS
  805. case IH_OS_ARTOS: os = "ARTOS"; break;
  806. #endif
  807. default: os = "Unknown OS"; break;
  808. }
  809. switch (hdr->ih_arch) {
  810. case IH_CPU_INVALID: arch = "Invalid CPU"; break;
  811. case IH_CPU_ALPHA: arch = "Alpha"; break;
  812. case IH_CPU_ARM: arch = "ARM"; break;
  813. case IH_CPU_I386: arch = "Intel x86"; break;
  814. case IH_CPU_IA64: arch = "IA64"; break;
  815. case IH_CPU_MIPS: arch = "MIPS"; break;
  816. case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
  817. case IH_CPU_PPC: arch = "PowerPC"; break;
  818. case IH_CPU_S390: arch = "IBM S390"; break;
  819. case IH_CPU_SH: arch = "SuperH"; break;
  820. case IH_CPU_SPARC: arch = "SPARC"; break;
  821. case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
  822. default: arch = "Unknown Architecture"; break;
  823. }
  824. switch (hdr->ih_type) {
  825. case IH_TYPE_INVALID: type = "Invalid Image"; break;
  826. case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
  827. case IH_TYPE_KERNEL: type = "Kernel Image"; break;
  828. case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
  829. case IH_TYPE_MULTI: type = "Multi-File Image"; break;
  830. case IH_TYPE_FIRMWARE: type = "Firmware"; break;
  831. case IH_TYPE_SCRIPT: type = "Script"; break;
  832. default: type = "Unknown Image"; break;
  833. }
  834. switch (hdr->ih_comp) {
  835. case IH_COMP_NONE: comp = "uncompressed"; break;
  836. case IH_COMP_GZIP: comp = "gzip compressed"; break;
  837. case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
  838. default: comp = "unknown compression"; break;
  839. }
  840. printf ("%s %s %s (%s)", arch, os, type, comp);
  841. }
  842. #define ZALLOC_ALIGNMENT 16
  843. static void *zalloc(void *x, unsigned items, unsigned size)
  844. {
  845. void *p;
  846. size *= items;
  847. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  848. p = malloc (size);
  849. return (p);
  850. }
  851. static void zfree(void *x, void *addr, unsigned nb)
  852. {
  853. free (addr);
  854. }
  855. #define HEAD_CRC 2
  856. #define EXTRA_FIELD 4
  857. #define ORIG_NAME 8
  858. #define COMMENT 0x10
  859. #define RESERVED 0xe0
  860. #define DEFLATED 8
  861. int gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
  862. {
  863. z_stream s;
  864. int r, i, flags;
  865. /* skip header */
  866. i = 10;
  867. flags = src[3];
  868. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  869. printf ("Error: Bad gzipped data\n");
  870. return (-1);
  871. }
  872. if ((flags & EXTRA_FIELD) != 0)
  873. i = 12 + src[10] + (src[11] << 8);
  874. if ((flags & ORIG_NAME) != 0)
  875. while (src[i++] != 0)
  876. ;
  877. if ((flags & COMMENT) != 0)
  878. while (src[i++] != 0)
  879. ;
  880. if ((flags & HEAD_CRC) != 0)
  881. i += 2;
  882. if (i >= *lenp) {
  883. printf ("Error: gunzip out of data in header\n");
  884. return (-1);
  885. }
  886. s.zalloc = zalloc;
  887. s.zfree = zfree;
  888. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  889. s.outcb = (cb_func)WATCHDOG_RESET;
  890. #else
  891. s.outcb = Z_NULL;
  892. #endif /* CONFIG_HW_WATCHDOG */
  893. r = inflateInit2(&s, -MAX_WBITS);
  894. if (r != Z_OK) {
  895. printf ("Error: inflateInit2() returned %d\n", r);
  896. return (-1);
  897. }
  898. s.next_in = src + i;
  899. s.avail_in = *lenp - i;
  900. s.next_out = dst;
  901. s.avail_out = dstlen;
  902. r = inflate(&s, Z_FINISH);
  903. if (r != Z_OK && r != Z_STREAM_END) {
  904. printf ("Error: inflate() returned %d\n", r);
  905. return (-1);
  906. }
  907. *lenp = s.next_out - (unsigned char *) dst;
  908. inflateEnd(&s);
  909. return (0);
  910. }
  911. static void
  912. do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  913. ulong addr, ulong *len_ptr, int verify)
  914. {
  915. DECLARE_GLOBAL_DATA_PTR;
  916. image_header_t *hdr = &header;
  917. void (*entry_point)(bd_t *);
  918. entry_point = (void (*)(bd_t *)) hdr->ih_ep;
  919. printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
  920. (ulong)entry_point);
  921. SHOW_BOOT_PROGRESS (15);
  922. /*
  923. * RTEMS Parameters:
  924. * r3: ptr to board info data
  925. */
  926. (*entry_point ) ( gd->bd );
  927. }
  928. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  929. static void
  930. do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  931. ulong addr, ulong *len_ptr, int verify)
  932. {
  933. image_header_t *hdr = &header;
  934. char str[80];
  935. sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
  936. setenv("loadaddr", str);
  937. do_bootvx(cmdtp, 0, 0, NULL);
  938. }
  939. static void
  940. do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  941. ulong addr, ulong *len_ptr, int verify)
  942. {
  943. image_header_t *hdr = &header;
  944. char *local_args[2];
  945. char str[16];
  946. sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
  947. local_args[0] = argv[0];
  948. local_args[1] = str; /* and provide it via the arguments */
  949. do_bootelf(cmdtp, 0, 2, local_args);
  950. }
  951. #endif /* CFG_CMD_ELF */