cmd_bootm.c 29 KB

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