17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  *  GRUB  --  GRand Unified Bootloader
37c478bd9Sstevel@tonic-gate  *  Copyright (C) 2000,2001,2002  Free Software Foundation, Inc.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  *  This program is free software; you can redistribute it and/or modify
67c478bd9Sstevel@tonic-gate  *  it under the terms of the GNU General Public License as published by
77c478bd9Sstevel@tonic-gate  *  the Free Software Foundation; either version 2 of the License, or
87c478bd9Sstevel@tonic-gate  *  (at your option) any later version.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  *  This program is distributed in the hope that it will be useful,
117c478bd9Sstevel@tonic-gate  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
127c478bd9Sstevel@tonic-gate  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137c478bd9Sstevel@tonic-gate  *  GNU General Public License for more details.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  *  You should have received a copy of the GNU General Public License
167c478bd9Sstevel@tonic-gate  *  along with this program; if not, write to the Free Software
177c478bd9Sstevel@tonic-gate  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate /* Based on "src/misc.c" in etherboot-5.0.5.  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include "grub.h"
237c478bd9Sstevel@tonic-gate #include "timer.h"
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include "nic.h"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /**************************************************************************
287c478bd9Sstevel@tonic-gate RANDOM - compute a random number between 0 and 2147483647L or 2147483562?
297c478bd9Sstevel@tonic-gate **************************************************************************/
random(void)307c478bd9Sstevel@tonic-gate int32_t random(void)
317c478bd9Sstevel@tonic-gate {
327c478bd9Sstevel@tonic-gate 	static int32_t seed = 0;
337c478bd9Sstevel@tonic-gate 	int32_t q;
347c478bd9Sstevel@tonic-gate 	if (!seed) /* Initialize linear congruential generator */
357c478bd9Sstevel@tonic-gate 		seed = currticks() + *(int32_t *)&arptable[ARP_CLIENT].node
367c478bd9Sstevel@tonic-gate 		       + ((int16_t *)arptable[ARP_CLIENT].node)[2];
377c478bd9Sstevel@tonic-gate 	/* simplified version of the LCG given in Bruce Schneier's
387c478bd9Sstevel@tonic-gate 	   "Applied Cryptography" */
397c478bd9Sstevel@tonic-gate 	q = seed/53668;
407c478bd9Sstevel@tonic-gate 	if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
417c478bd9Sstevel@tonic-gate 	return seed;
427c478bd9Sstevel@tonic-gate }
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /**************************************************************************
457c478bd9Sstevel@tonic-gate POLL INTERRUPTIONS
467c478bd9Sstevel@tonic-gate **************************************************************************/
poll_interruptions(void)477c478bd9Sstevel@tonic-gate void poll_interruptions(void)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	if (checkkey() != -1 && ASCII_CHAR(getkey()) == K_INTR) {
507c478bd9Sstevel@tonic-gate 		user_abort++;
517c478bd9Sstevel@tonic-gate 	}
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /**************************************************************************
557c478bd9Sstevel@tonic-gate SLEEP
567c478bd9Sstevel@tonic-gate **************************************************************************/
sleep(int secs)577c478bd9Sstevel@tonic-gate void sleep(int secs)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	unsigned long tmo;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
627c478bd9Sstevel@tonic-gate 		poll_interruptions();
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /**************************************************************************
677c478bd9Sstevel@tonic-gate INTERRUPTIBLE SLEEP
687c478bd9Sstevel@tonic-gate **************************************************************************/
interruptible_sleep(int secs)697c478bd9Sstevel@tonic-gate void interruptible_sleep(int secs)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	printf("<sleep>\n");
727c478bd9Sstevel@tonic-gate 	return sleep(secs);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /**************************************************************************
767c478bd9Sstevel@tonic-gate TWIDDLE
777c478bd9Sstevel@tonic-gate **************************************************************************/
twiddle(void)787c478bd9Sstevel@tonic-gate void twiddle(void)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate #ifdef BAR_PROGRESS
817c478bd9Sstevel@tonic-gate 	static int count=0;
827c478bd9Sstevel@tonic-gate 	static const char tiddles[]="-\\|/";
837c478bd9Sstevel@tonic-gate 	static unsigned long lastticks = 0;
847c478bd9Sstevel@tonic-gate 	unsigned long ticks;
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate #ifdef FREEBSD_PXEEMU
877c478bd9Sstevel@tonic-gate 	extern char pxeemu_nbp_active;
887c478bd9Sstevel@tonic-gate 	if(pxeemu_nbp_active != 0)
897c478bd9Sstevel@tonic-gate 		return;
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate #ifdef	BAR_PROGRESS
927c478bd9Sstevel@tonic-gate 	/* Limit the maximum rate at which characters are printed */
937c478bd9Sstevel@tonic-gate 	ticks = currticks();
947c478bd9Sstevel@tonic-gate 	if ((lastticks + (TICKS_PER_SEC/18)) > ticks)
957c478bd9Sstevel@tonic-gate 		return;
967c478bd9Sstevel@tonic-gate 	lastticks = ticks;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	putchar(tiddles[(count++)&3]);
997c478bd9Sstevel@tonic-gate 	putchar('\b');
1007c478bd9Sstevel@tonic-gate #else
1017c478bd9Sstevel@tonic-gate 	//putchar('.');
1027c478bd9Sstevel@tonic-gate #endif	/* BAR_PROGRESS */
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* Because Etherboot uses its own formats for the printf family,
1077c478bd9Sstevel@tonic-gate    define separate definitions from GRUB.  */
1087c478bd9Sstevel@tonic-gate /**************************************************************************
1097c478bd9Sstevel@tonic-gate PRINTF and friends
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	Formats:
1127c478bd9Sstevel@tonic-gate 		%[#]x	- 4 bytes long (8 hex digits, lower case)
1137c478bd9Sstevel@tonic-gate 		%[#]X	- 4 bytes long (8 hex digits, upper case)
1147c478bd9Sstevel@tonic-gate 		%[#]hx	- 2 bytes int (4 hex digits, lower case)
1157c478bd9Sstevel@tonic-gate 		%[#]hX	- 2 bytes int (4 hex digits, upper case)
1167c478bd9Sstevel@tonic-gate 		%[#]hhx	- 1 byte int (2 hex digits, lower case)
1177c478bd9Sstevel@tonic-gate 		%[#]hhX	- 1 byte int (2 hex digits, upper case)
1187c478bd9Sstevel@tonic-gate 			- optional # prefixes 0x or 0X
1197c478bd9Sstevel@tonic-gate 		%d	- decimal int
1207c478bd9Sstevel@tonic-gate 		%c	- char
1217c478bd9Sstevel@tonic-gate 		%s	- string
1227c478bd9Sstevel@tonic-gate 		%@	- Internet address in ddd.ddd.ddd.ddd notation
1237c478bd9Sstevel@tonic-gate 		%!	- Ethernet address in xx:xx:xx:xx:xx:xx notation
1247c478bd9Sstevel@tonic-gate 	Note: width specification not supported
1257c478bd9Sstevel@tonic-gate **************************************************************************/
1267c478bd9Sstevel@tonic-gate static int
etherboot_vsprintf(char * buf,const char * fmt,const int * dp)1277c478bd9Sstevel@tonic-gate etherboot_vsprintf (char *buf, const char *fmt, const int *dp)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate   char *p, *s;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate   s = buf;
1327c478bd9Sstevel@tonic-gate   for ( ; *fmt != '\0'; ++fmt)
1337c478bd9Sstevel@tonic-gate     {
1347c478bd9Sstevel@tonic-gate       if (*fmt != '%')
1357c478bd9Sstevel@tonic-gate 	{
1367c478bd9Sstevel@tonic-gate 	  buf ? *s++ = *fmt : grub_putchar (*fmt);
1377c478bd9Sstevel@tonic-gate 	  continue;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate       if (*++fmt == 's')
1417c478bd9Sstevel@tonic-gate 	{
1427c478bd9Sstevel@tonic-gate 	  for (p = (char *) *dp++; *p != '\0'; p++)
1437c478bd9Sstevel@tonic-gate 	    buf ? *s++ = *p : grub_putchar (*p);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate       else
1467c478bd9Sstevel@tonic-gate 	{
1477c478bd9Sstevel@tonic-gate 	  /* Length of item is bounded */
1487c478bd9Sstevel@tonic-gate 	  char tmp[20], *q = tmp;
1497c478bd9Sstevel@tonic-gate 	  int alt = 0;
1507c478bd9Sstevel@tonic-gate 	  int shift = 28;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	  if (*fmt == '#')
1537c478bd9Sstevel@tonic-gate 	    {
1547c478bd9Sstevel@tonic-gate 	      alt = 1;
1557c478bd9Sstevel@tonic-gate 	      fmt++;
1567c478bd9Sstevel@tonic-gate 	    }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	  if (*fmt == 'h')
1597c478bd9Sstevel@tonic-gate 	    {
1607c478bd9Sstevel@tonic-gate 	      shift = 12;
1617c478bd9Sstevel@tonic-gate 	      fmt++;
1627c478bd9Sstevel@tonic-gate 	    }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	  if (*fmt == 'h')
1657c478bd9Sstevel@tonic-gate 	    {
1667c478bd9Sstevel@tonic-gate 	      shift = 4;
1677c478bd9Sstevel@tonic-gate 	      fmt++;
1687c478bd9Sstevel@tonic-gate 	    }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	  /*
1717c478bd9Sstevel@tonic-gate 	   * Before each format q points to tmp buffer
1727c478bd9Sstevel@tonic-gate 	   * After each format q points past end of item
1737c478bd9Sstevel@tonic-gate 	   */
1747c478bd9Sstevel@tonic-gate 	  if ((*fmt | 0x20) == 'x')
1757c478bd9Sstevel@tonic-gate 	    {
1767c478bd9Sstevel@tonic-gate 	      /* With x86 gcc, sizeof(long) == sizeof(int) */
1777c478bd9Sstevel@tonic-gate 	      const long *lp = (const long *) dp;
1787c478bd9Sstevel@tonic-gate 	      long h = *lp++;
1797c478bd9Sstevel@tonic-gate 	      int ncase = (*fmt & 0x20);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	      dp = (const int *) lp;
1827c478bd9Sstevel@tonic-gate 	      if (alt)
1837c478bd9Sstevel@tonic-gate 		{
1847c478bd9Sstevel@tonic-gate 		  *q++ = '0';
1857c478bd9Sstevel@tonic-gate 		  *q++ = 'X' | ncase;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 	      for (; shift >= 0; shift -= 4)
1887c478bd9Sstevel@tonic-gate 		*q++ = "0123456789ABCDEF"[(h >> shift) & 0xF] | ncase;
1897c478bd9Sstevel@tonic-gate 	    }
1907c478bd9Sstevel@tonic-gate 	  else if (*fmt == 'd')
1917c478bd9Sstevel@tonic-gate 	    {
1927c478bd9Sstevel@tonic-gate 	      int i = *dp++;
1937c478bd9Sstevel@tonic-gate 	      char *r;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	      if (i < 0)
1967c478bd9Sstevel@tonic-gate 		{
1977c478bd9Sstevel@tonic-gate 		  *q++ = '-';
1987c478bd9Sstevel@tonic-gate 		  i = -i;
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	      p = q;		/* save beginning of digits */
2027c478bd9Sstevel@tonic-gate 	      do
2037c478bd9Sstevel@tonic-gate 		{
2047c478bd9Sstevel@tonic-gate 		  *q++ = '0' + (i % 10);
2057c478bd9Sstevel@tonic-gate 		  i /= 10;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	      while (i);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	      /* reverse digits, stop in middle */
2107c478bd9Sstevel@tonic-gate 	      r = q;		/* don't alter q */
2117c478bd9Sstevel@tonic-gate 	      while (--r > p)
2127c478bd9Sstevel@tonic-gate 		{
2137c478bd9Sstevel@tonic-gate 		  i = *r;
2147c478bd9Sstevel@tonic-gate 		  *r = *p;
2157c478bd9Sstevel@tonic-gate 		  *p++ = i;
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 	    }
2187c478bd9Sstevel@tonic-gate 	  else if (*fmt == '@')
2197c478bd9Sstevel@tonic-gate 	    {
2207c478bd9Sstevel@tonic-gate 	      unsigned char *r;
2217c478bd9Sstevel@tonic-gate 	      union
2227c478bd9Sstevel@tonic-gate 	      {
2237c478bd9Sstevel@tonic-gate 		long		l;
2247c478bd9Sstevel@tonic-gate 		unsigned char	c[4];
2257c478bd9Sstevel@tonic-gate 	      }
2267c478bd9Sstevel@tonic-gate 	      u;
2277c478bd9Sstevel@tonic-gate 	      const long *lp = (const long *) dp;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	      u.l = *lp++;
2307c478bd9Sstevel@tonic-gate 	      dp = (const int *) lp;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	      for (r = &u.c[0]; r < &u.c[4]; ++r)
2337c478bd9Sstevel@tonic-gate 		q += etherboot_sprintf (q, "%d.", *r);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	      --q;
2367c478bd9Sstevel@tonic-gate 	    }
2377c478bd9Sstevel@tonic-gate 	  else if (*fmt == '!')
2387c478bd9Sstevel@tonic-gate 	    {
2397c478bd9Sstevel@tonic-gate 	      char *r;
2407c478bd9Sstevel@tonic-gate 	      p = (char *) *dp++;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	      for (r = p + ETH_ALEN; p < r; ++p)
2437c478bd9Sstevel@tonic-gate 		q += etherboot_sprintf (q, "%hhX:", *p);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	      --q;
2467c478bd9Sstevel@tonic-gate 	    }
2477c478bd9Sstevel@tonic-gate 	  else if (*fmt == 'c')
2487c478bd9Sstevel@tonic-gate 	    *q++ = *dp++;
2497c478bd9Sstevel@tonic-gate 	  else
2507c478bd9Sstevel@tonic-gate 	    *q++ = *fmt;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	  /* now output the saved string */
2537c478bd9Sstevel@tonic-gate 	  for (p = tmp; p < q; ++p)
2547c478bd9Sstevel@tonic-gate 	    buf ? *s++ = *p : grub_putchar (*p);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate     }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate   if (buf)
2597c478bd9Sstevel@tonic-gate     *s = '\0';
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate   return (s - buf);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate int
etherboot_sprintf(char * buf,const char * fmt,...)2657c478bd9Sstevel@tonic-gate etherboot_sprintf (char *buf, const char *fmt, ...)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate   return etherboot_vsprintf (buf, fmt, ((const int *) &fmt) + 1);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate void
etherboot_printf(const char * fmt,...)2717c478bd9Sstevel@tonic-gate etherboot_printf (const char *fmt, ...)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate   (void) etherboot_vsprintf (0, fmt, ((const int *) &fmt) + 1);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate int
inet_aton(char * p,in_addr * addr)2777c478bd9Sstevel@tonic-gate inet_aton (char *p, in_addr *addr)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate   unsigned long ip = 0;
2807c478bd9Sstevel@tonic-gate   int val;
2817c478bd9Sstevel@tonic-gate   int i;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate   for (i = 0; i < 4; i++)
2847c478bd9Sstevel@tonic-gate     {
2857c478bd9Sstevel@tonic-gate       val = getdec (&p);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate       if (val < 0 || val > 255)
2887c478bd9Sstevel@tonic-gate 	return 0;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate       if (i != 3 && *p++ != '.')
2917c478bd9Sstevel@tonic-gate 	return 0;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate       ip = (ip << 8) | val;
2947c478bd9Sstevel@tonic-gate     }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate   addr->s_addr = htonl (ip);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate   return 1;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate int
getdec(char ** ptr)3027c478bd9Sstevel@tonic-gate getdec (char **ptr)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate   char *p = *ptr;
3057c478bd9Sstevel@tonic-gate   int ret = 0;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate   if (*p < '0' || *p > '9')
3087c478bd9Sstevel@tonic-gate     return -1;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate   while (*p >= '0' && *p <= '9')
3117c478bd9Sstevel@tonic-gate     {
3127c478bd9Sstevel@tonic-gate       ret = ret * 10 + (*p - '0');
3137c478bd9Sstevel@tonic-gate       p++;
3147c478bd9Sstevel@tonic-gate     }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate   *ptr = p;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate   return ret;
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 
322