1N/A/*
1N/A * GRUB -- GRand Unified Bootloader
1N/A * Copyright (C) 2000,2001,2002 Free Software Foundation, Inc.
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1N/A */
1N/A
1N/A/* Based on "src/misc.c" in etherboot-5.0.5. */
1N/A
1N/A#include "grub.h"
1N/A#include "timer.h"
1N/A
1N/A#include "nic.h"
1N/A
1N/A/**************************************************************************
1N/ARANDOM - compute a random number between 0 and 2147483647L or 2147483562?
1N/A**************************************************************************/
1N/Aint32_t random(void)
1N/A{
1N/A static int32_t seed = 0;
1N/A int32_t q;
1N/A if (!seed) /* Initialize linear congruential generator */
1N/A seed = currticks() + *(int32_t *)&arptable[ARP_CLIENT].node
1N/A + ((int16_t *)arptable[ARP_CLIENT].node)[2];
1N/A /* simplified version of the LCG given in Bruce Schneier's
1N/A "Applied Cryptography" */
1N/A q = seed/53668;
1N/A if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
1N/A return seed;
1N/A}
1N/A
1N/A/**************************************************************************
1N/APOLL INTERRUPTIONS
1N/A**************************************************************************/
1N/Avoid poll_interruptions(void)
1N/A{
1N/A if (checkkey() != -1 && ASCII_CHAR(getkey()) == K_INTR) {
1N/A user_abort++;
1N/A }
1N/A}
1N/A
1N/A/**************************************************************************
1N/ASLEEP
1N/A**************************************************************************/
1N/Avoid sleep(int secs)
1N/A{
1N/A unsigned long tmo;
1N/A
1N/A for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
1N/A poll_interruptions();
1N/A }
1N/A}
1N/A
1N/A/**************************************************************************
1N/AINTERRUPTIBLE SLEEP
1N/A**************************************************************************/
1N/Avoid interruptible_sleep(int secs)
1N/A{
1N/A printf("<sleep>\n");
1N/A return sleep(secs);
1N/A}
1N/A
1N/A/**************************************************************************
1N/ATWIDDLE
1N/A**************************************************************************/
1N/Avoid twiddle(void)
1N/A{
1N/A#ifdef BAR_PROGRESS
1N/A static int count=0;
1N/A static const char tiddles[]="-\\|/";
1N/A static unsigned long lastticks = 0;
1N/A unsigned long ticks;
1N/A#endif
1N/A#ifdef FREEBSD_PXEEMU
1N/A extern char pxeemu_nbp_active;
1N/A if(pxeemu_nbp_active != 0)
1N/A return;
1N/A#endif
1N/A#ifdef BAR_PROGRESS
1N/A /* Limit the maximum rate at which characters are printed */
1N/A ticks = currticks();
1N/A if ((lastticks + (TICKS_PER_SEC/18)) > ticks)
1N/A return;
1N/A lastticks = ticks;
1N/A
1N/A putchar(tiddles[(count++)&3]);
1N/A putchar('\b');
1N/A#else
1N/A //putchar('.');
1N/A#endif /* BAR_PROGRESS */
1N/A}
1N/A
1N/A
1N/A/* Because Etherboot uses its own formats for the printf family,
1N/A define separate definitions from GRUB. */
1N/A/**************************************************************************
1N/APRINTF and friends
1N/A
1N/A Formats:
1N/A %[#]x - 4 bytes long (8 hex digits, lower case)
1N/A %[#]X - 4 bytes long (8 hex digits, upper case)
1N/A %[#]hx - 2 bytes int (4 hex digits, lower case)
1N/A %[#]hX - 2 bytes int (4 hex digits, upper case)
1N/A %[#]hhx - 1 byte int (2 hex digits, lower case)
1N/A %[#]hhX - 1 byte int (2 hex digits, upper case)
1N/A - optional # prefixes 0x or 0X
1N/A %d - decimal int
1N/A %c - char
1N/A %s - string
1N/A %@ - Internet address in ddd.ddd.ddd.ddd notation
1N/A %! - Ethernet address in xx:xx:xx:xx:xx:xx notation
1N/A Note: width specification not supported
1N/A**************************************************************************/
1N/Astatic int
1N/Aetherboot_vsprintf (char *buf, const char *fmt, const int *dp)
1N/A{
1N/A char *p, *s;
1N/A
1N/A s = buf;
1N/A for ( ; *fmt != '\0'; ++fmt)
1N/A {
1N/A if (*fmt != '%')
1N/A {
1N/A buf ? *s++ = *fmt : grub_putchar (*fmt);
1N/A continue;
1N/A }
1N/A
1N/A if (*++fmt == 's')
1N/A {
1N/A for (p = (char *) *dp++; *p != '\0'; p++)
1N/A buf ? *s++ = *p : grub_putchar (*p);
1N/A }
1N/A else
1N/A {
1N/A /* Length of item is bounded */
1N/A char tmp[20], *q = tmp;
1N/A int alt = 0;
1N/A int shift = 28;
1N/A
1N/A if (*fmt == '#')
1N/A {
1N/A alt = 1;
1N/A fmt++;
1N/A }
1N/A
1N/A if (*fmt == 'h')
1N/A {
1N/A shift = 12;
1N/A fmt++;
1N/A }
1N/A
1N/A if (*fmt == 'h')
1N/A {
1N/A shift = 4;
1N/A fmt++;
1N/A }
1N/A
1N/A /*
1N/A * Before each format q points to tmp buffer
1N/A * After each format q points past end of item
1N/A */
1N/A if ((*fmt | 0x20) == 'x')
1N/A {
1N/A /* With x86 gcc, sizeof(long) == sizeof(int) */
1N/A const long *lp = (const long *) dp;
1N/A long h = *lp++;
1N/A int ncase = (*fmt & 0x20);
1N/A
1N/A dp = (const int *) lp;
1N/A if (alt)
1N/A {
1N/A *q++ = '0';
1N/A *q++ = 'X' | ncase;
1N/A }
1N/A for (; shift >= 0; shift -= 4)
1N/A *q++ = "0123456789ABCDEF"[(h >> shift) & 0xF] | ncase;
1N/A }
1N/A else if (*fmt == 'd')
1N/A {
1N/A int i = *dp++;
1N/A char *r;
1N/A
1N/A if (i < 0)
1N/A {
1N/A *q++ = '-';
1N/A i = -i;
1N/A }
1N/A
1N/A p = q; /* save beginning of digits */
1N/A do
1N/A {
1N/A *q++ = '0' + (i % 10);
1N/A i /= 10;
1N/A }
1N/A while (i);
1N/A
1N/A /* reverse digits, stop in middle */
1N/A r = q; /* don't alter q */
1N/A while (--r > p)
1N/A {
1N/A i = *r;
1N/A *r = *p;
1N/A *p++ = i;
1N/A }
1N/A }
1N/A else if (*fmt == '@')
1N/A {
1N/A unsigned char *r;
1N/A union
1N/A {
1N/A long l;
1N/A unsigned char c[4];
1N/A }
1N/A u;
1N/A const long *lp = (const long *) dp;
1N/A
1N/A u.l = *lp++;
1N/A dp = (const int *) lp;
1N/A
1N/A for (r = &u.c[0]; r < &u.c[4]; ++r)
1N/A q += etherboot_sprintf (q, "%d.", *r);
1N/A
1N/A --q;
1N/A }
1N/A else if (*fmt == '!')
1N/A {
1N/A char *r;
1N/A p = (char *) *dp++;
1N/A
1N/A for (r = p + ETH_ALEN; p < r; ++p)
1N/A q += etherboot_sprintf (q, "%hhX:", *p);
1N/A
1N/A --q;
1N/A }
1N/A else if (*fmt == 'c')
1N/A *q++ = *dp++;
1N/A else
1N/A *q++ = *fmt;
1N/A
1N/A /* now output the saved string */
1N/A for (p = tmp; p < q; ++p)
1N/A buf ? *s++ = *p : grub_putchar (*p);
1N/A }
1N/A }
1N/A
1N/A if (buf)
1N/A *s = '\0';
1N/A
1N/A return (s - buf);
1N/A}
1N/A
1N/Aint
1N/Aetherboot_sprintf (char *buf, const char *fmt, ...)
1N/A{
1N/A return etherboot_vsprintf (buf, fmt, ((const int *) &fmt) + 1);
1N/A}
1N/A
1N/Avoid
1N/Aetherboot_printf (const char *fmt, ...)
1N/A{
1N/A (void) etherboot_vsprintf (0, fmt, ((const int *) &fmt) + 1);
1N/A}
1N/A
1N/Aint
1N/Ainet_aton (char *p, in_addr *addr)
1N/A{
1N/A unsigned long ip = 0;
1N/A int val;
1N/A int i;
1N/A
1N/A for (i = 0; i < 4; i++)
1N/A {
1N/A val = getdec (&p);
1N/A
1N/A if (val < 0 || val > 255)
1N/A return 0;
1N/A
1N/A if (i != 3 && *p++ != '.')
1N/A return 0;
1N/A
1N/A ip = (ip << 8) | val;
1N/A }
1N/A
1N/A addr->s_addr = htonl (ip);
1N/A
1N/A return 1;
1N/A}
1N/A
1N/Aint
1N/Agetdec (char **ptr)
1N/A{
1N/A char *p = *ptr;
1N/A int ret = 0;
1N/A
1N/A if (*p < '0' || *p > '9')
1N/A return -1;
1N/A
1N/A while (*p >= '0' && *p <= '9')
1N/A {
1N/A ret = ret * 10 + (*p - '0');
1N/A p++;
1N/A }
1N/A
1N/A *ptr = p;
1N/A
1N/A return ret;
1N/A}
1N/A
1N/A