Merge branch 'master' of git://github.com/illumos/illumos-gate
[unleashed.git] / usr / src / grub / grub-0.97 / netboot / misc.c
blob339ef8e60269efd3b0954ddb88b1895001dd9271
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2000,2001,2002 Free Software Foundation, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* Based on "src/misc.c" in etherboot-5.0.5. */
22 #include "grub.h"
23 #include "timer.h"
25 #include "nic.h"
27 /**************************************************************************
28 RANDOM - compute a random number between 0 and 2147483647L or 2147483562?
29 **************************************************************************/
30 int32_t random(void)
32 static int32_t seed = 0;
33 int32_t q;
34 if (!seed) /* Initialize linear congruential generator */
35 seed = currticks() + *(int32_t *)&arptable[ARP_CLIENT].node
36 + ((int16_t *)arptable[ARP_CLIENT].node)[2];
37 /* simplified version of the LCG given in Bruce Schneier's
38 "Applied Cryptography" */
39 q = seed/53668;
40 if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
41 return seed;
44 /**************************************************************************
45 POLL INTERRUPTIONS
46 **************************************************************************/
47 void poll_interruptions(void)
49 if (checkkey() != -1 && ASCII_CHAR(getkey()) == K_INTR) {
50 user_abort++;
54 /**************************************************************************
55 SLEEP
56 **************************************************************************/
57 void sleep(int secs)
59 unsigned long tmo;
61 for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
62 poll_interruptions();
66 /**************************************************************************
67 INTERRUPTIBLE SLEEP
68 **************************************************************************/
69 void interruptible_sleep(int secs)
71 printf("<sleep>\n");
72 return sleep(secs);
75 /**************************************************************************
76 TWIDDLE
77 **************************************************************************/
78 void twiddle(void)
80 #ifdef BAR_PROGRESS
81 static int count=0;
82 static const char tiddles[]="-\\|/";
83 static unsigned long lastticks = 0;
84 unsigned long ticks;
85 #endif
86 #ifdef FREEBSD_PXEEMU
87 extern char pxeemu_nbp_active;
88 if(pxeemu_nbp_active != 0)
89 return;
90 #endif
91 #ifdef BAR_PROGRESS
92 /* Limit the maximum rate at which characters are printed */
93 ticks = currticks();
94 if ((lastticks + (TICKS_PER_SEC/18)) > ticks)
95 return;
96 lastticks = ticks;
98 putchar(tiddles[(count++)&3]);
99 putchar('\b');
100 #else
101 //putchar('.');
102 #endif /* BAR_PROGRESS */
106 /* Because Etherboot uses its own formats for the printf family,
107 define separate definitions from GRUB. */
108 /**************************************************************************
109 PRINTF and friends
111 Formats:
112 %[#]x - 4 bytes long (8 hex digits, lower case)
113 %[#]X - 4 bytes long (8 hex digits, upper case)
114 %[#]hx - 2 bytes int (4 hex digits, lower case)
115 %[#]hX - 2 bytes int (4 hex digits, upper case)
116 %[#]hhx - 1 byte int (2 hex digits, lower case)
117 %[#]hhX - 1 byte int (2 hex digits, upper case)
118 - optional # prefixes 0x or 0X
119 %d - decimal int
120 %c - char
121 %s - string
122 %@ - Internet address in ddd.ddd.ddd.ddd notation
123 %! - Ethernet address in xx:xx:xx:xx:xx:xx notation
124 Note: width specification not supported
125 **************************************************************************/
126 static int
127 etherboot_vsprintf (char *buf, const char *fmt, const int *dp)
129 char *p, *s;
131 s = buf;
132 for ( ; *fmt != '\0'; ++fmt)
134 if (*fmt != '%')
136 buf ? *s++ = *fmt : grub_putchar (*fmt);
137 continue;
140 if (*++fmt == 's')
142 for (p = (char *) *dp++; *p != '\0'; p++)
143 buf ? *s++ = *p : grub_putchar (*p);
145 else
147 /* Length of item is bounded */
148 char tmp[20], *q = tmp;
149 int alt = 0;
150 int shift = 28;
152 if (*fmt == '#')
154 alt = 1;
155 fmt++;
158 if (*fmt == 'h')
160 shift = 12;
161 fmt++;
164 if (*fmt == 'h')
166 shift = 4;
167 fmt++;
171 * Before each format q points to tmp buffer
172 * After each format q points past end of item
174 if ((*fmt | 0x20) == 'x')
176 /* With x86 gcc, sizeof(long) == sizeof(int) */
177 const long *lp = (const long *) dp;
178 long h = *lp++;
179 int ncase = (*fmt & 0x20);
181 dp = (const int *) lp;
182 if (alt)
184 *q++ = '0';
185 *q++ = 'X' | ncase;
187 for (; shift >= 0; shift -= 4)
188 *q++ = "0123456789ABCDEF"[(h >> shift) & 0xF] | ncase;
190 else if (*fmt == 'd')
192 int i = *dp++;
193 char *r;
195 if (i < 0)
197 *q++ = '-';
198 i = -i;
201 p = q; /* save beginning of digits */
204 *q++ = '0' + (i % 10);
205 i /= 10;
207 while (i);
209 /* reverse digits, stop in middle */
210 r = q; /* don't alter q */
211 while (--r > p)
213 i = *r;
214 *r = *p;
215 *p++ = i;
218 else if (*fmt == '@')
220 unsigned char *r;
221 union
223 long l;
224 unsigned char c[4];
227 const long *lp = (const long *) dp;
229 u.l = *lp++;
230 dp = (const int *) lp;
232 for (r = &u.c[0]; r < &u.c[4]; ++r)
233 q += etherboot_sprintf (q, "%d.", *r);
235 --q;
237 else if (*fmt == '!')
239 char *r;
240 p = (char *) *dp++;
242 for (r = p + ETH_ALEN; p < r; ++p)
243 q += etherboot_sprintf (q, "%hhX:", *p);
245 --q;
247 else if (*fmt == 'c')
248 *q++ = *dp++;
249 else
250 *q++ = *fmt;
252 /* now output the saved string */
253 for (p = tmp; p < q; ++p)
254 buf ? *s++ = *p : grub_putchar (*p);
258 if (buf)
259 *s = '\0';
261 return (s - buf);
265 etherboot_sprintf (char *buf, const char *fmt, ...)
267 return etherboot_vsprintf (buf, fmt, ((const int *) &fmt) + 1);
270 void
271 etherboot_printf (const char *fmt, ...)
273 (void) etherboot_vsprintf (0, fmt, ((const int *) &fmt) + 1);
277 inet_aton (char *p, in_addr *addr)
279 unsigned long ip = 0;
280 int val;
281 int i;
283 for (i = 0; i < 4; i++)
285 val = getdec (&p);
287 if (val < 0 || val > 255)
288 return 0;
290 if (i != 3 && *p++ != '.')
291 return 0;
293 ip = (ip << 8) | val;
296 addr->s_addr = htonl (ip);
298 return 1;
302 getdec (char **ptr)
304 char *p = *ptr;
305 int ret = 0;
307 if (*p < '0' || *p > '9')
308 return -1;
310 while (*p >= '0' && *p <= '9')
312 ret = ret * 10 + (*p - '0');
313 p++;
316 *ptr = p;
318 return ret;