revert between 56095 -> 55830 in arch
[AROS.git] / arch / all-pc / boot / grub / netboot / misc.c
blob28614fdc4a5a6c1027aca3cf8cfbfd04e567a711
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 #define GRUB 1
23 #include <etherboot.h>
25 void
26 sleep (int secs)
28 unsigned long tmo = currticks () + secs;
30 while (currticks () < tmo)
34 void
35 twiddle (void)
37 static unsigned long lastticks = 0;
38 static int count = 0;
39 static const char tiddles[]="-\\|/";
40 unsigned long ticks;
42 if (debug)
44 if ((ticks = currticks ()) == lastticks)
45 return;
47 lastticks = ticks;
48 grub_putchar (tiddles[(count++) & 3]);
49 grub_putchar ('\b');
53 /* Because Etherboot uses its own formats for the printf family,
54 define separate definitions from GRUB. */
55 /**************************************************************************
56 PRINTF and friends
58 Formats:
59 %[#]x - 4 bytes long (8 hex digits, lower case)
60 %[#]X - 4 bytes long (8 hex digits, upper case)
61 %[#]hx - 2 bytes int (4 hex digits, lower case)
62 %[#]hX - 2 bytes int (4 hex digits, upper case)
63 %[#]hhx - 1 byte int (2 hex digits, lower case)
64 %[#]hhX - 1 byte int (2 hex digits, upper case)
65 - optional # prefixes 0x or 0X
66 %d - decimal int
67 %c - char
68 %s - string
69 %@ - Internet address in ddd.ddd.ddd.ddd notation
70 %! - Ethernet address in xx:xx:xx:xx:xx:xx notation
71 Note: width specification not supported
72 **************************************************************************/
73 static int
74 etherboot_vsprintf (char *buf, const char *fmt, const int *dp)
76 char *p, *s;
78 s = buf;
79 for ( ; *fmt != '\0'; ++fmt)
81 if (*fmt != '%')
83 buf ? *s++ = *fmt : grub_putchar (*fmt);
84 continue;
87 if (*++fmt == 's')
89 for (p = (char *) *dp++; *p != '\0'; p++)
90 buf ? *s++ = *p : grub_putchar (*p);
92 else
94 /* Length of item is bounded */
95 char tmp[20], *q = tmp;
96 int alt = 0;
97 int shift = 28;
99 if (*fmt == '#')
101 alt = 1;
102 fmt++;
105 if (*fmt == 'h')
107 shift = 12;
108 fmt++;
111 if (*fmt == 'h')
113 shift = 4;
114 fmt++;
118 * Before each format q points to tmp buffer
119 * After each format q points past end of item
121 if ((*fmt | 0x20) == 'x')
123 /* With x86 gcc, sizeof(long) == sizeof(int) */
124 const long *lp = (const long *) dp;
125 long h = *lp++;
126 int ncase = (*fmt & 0x20);
128 dp = (const int *) lp;
129 if (alt)
131 *q++ = '0';
132 *q++ = 'X' | ncase;
134 for (; shift >= 0; shift -= 4)
135 *q++ = "0123456789ABCDEF"[(h >> shift) & 0xF] | ncase;
137 else if (*fmt == 'd')
139 int i = *dp++;
140 char *r;
142 if (i < 0)
144 *q++ = '-';
145 i = -i;
148 p = q; /* save beginning of digits */
151 *q++ = '0' + (i % 10);
152 i /= 10;
154 while (i);
156 /* reverse digits, stop in middle */
157 r = q; /* don't alter q */
158 while (--r > p)
160 i = *r;
161 *r = *p;
162 *p++ = i;
165 else if (*fmt == '@')
167 unsigned char *r;
168 union
170 long l;
171 unsigned char c[4];
174 const long *lp = (const long *) dp;
176 u.l = *lp++;
177 dp = (const int *) lp;
179 for (r = &u.c[0]; r < &u.c[4]; ++r)
180 q += etherboot_sprintf (q, "%d.", *r);
182 --q;
184 else if (*fmt == '!')
186 char *r;
187 p = (char *) *dp++;
189 for (r = p + ETH_ALEN; p < r; ++p)
190 q += etherboot_sprintf (q, "%hhX:", *p);
192 --q;
194 else if (*fmt == 'c')
195 *q++ = *dp++;
196 else
197 *q++ = *fmt;
199 /* now output the saved string */
200 for (p = tmp; p < q; ++p)
201 buf ? *s++ = *p : grub_putchar (*p);
205 if (buf)
206 *s = '\0';
208 return (s - buf);
212 etherboot_sprintf (char *buf, const char *fmt, ...)
214 return etherboot_vsprintf (buf, fmt, ((const int *) &fmt) + 1);
217 void
218 etherboot_printf (const char *fmt, ...)
220 (void) etherboot_vsprintf (0, fmt, ((const int *) &fmt) + 1);
224 inet_aton (char *p, in_addr *addr)
226 unsigned long ip = 0;
227 int val;
228 int i;
230 for (i = 0; i < 4; i++)
232 val = getdec (&p);
234 if (val < 0 || val > 255)
235 return 0;
237 if (i != 3 && *p++ != '.')
238 return 0;
240 ip = (ip << 8) | val;
243 addr->s_addr = htonl (ip);
245 return 1;
249 getdec (char **ptr)
251 char *p = *ptr;
252 int ret = 0;
254 if (*p < '0' || *p > '9')
255 return -1;
257 while (*p >= '0' && *p <= '9')
259 ret = ret * 10 + (*p - '0');
260 p++;
263 *ptr = p;
265 return ret;