2005-03-08 18:53:11 +00:00
|
|
|
/**************************************************************************
|
|
|
|
MISC Support Routines
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "etherboot.h"
|
2005-04-08 15:01:17 +00:00
|
|
|
#include "console.h"
|
2006-12-22 01:44:51 +00:00
|
|
|
#include <stdlib.h>
|
2005-03-08 18:53:11 +00:00
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
IPCHKSUM - Checksum IP Header
|
|
|
|
**************************************************************************/
|
|
|
|
uint16_t ipchksum(const void *data, unsigned long length)
|
|
|
|
{
|
|
|
|
unsigned long sum;
|
|
|
|
unsigned long i;
|
|
|
|
const uint8_t *ptr;
|
|
|
|
|
|
|
|
/* In the most straight forward way possible,
|
|
|
|
* compute an ip style checksum.
|
|
|
|
*/
|
|
|
|
sum = 0;
|
|
|
|
ptr = data;
|
|
|
|
for(i = 0; i < length; i++) {
|
|
|
|
unsigned long value;
|
|
|
|
value = ptr[i];
|
|
|
|
if (i & 1) {
|
|
|
|
value <<= 8;
|
|
|
|
}
|
|
|
|
/* Add the new value */
|
|
|
|
sum += value;
|
|
|
|
/* Wrap around the carry */
|
|
|
|
if (sum > 0xFFFF) {
|
|
|
|
sum = (sum + (sum >> 16)) & 0xFFFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (~cpu_to_le16(sum)) & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t add_ipchksums(unsigned long offset, uint16_t sum, uint16_t new)
|
|
|
|
{
|
|
|
|
unsigned long checksum;
|
|
|
|
sum = ~sum & 0xFFFF;
|
|
|
|
new = ~new & 0xFFFF;
|
|
|
|
if (offset & 1) {
|
|
|
|
/* byte swap the sum if it came from an odd offset
|
|
|
|
* since the computation is endian independant this
|
|
|
|
* works.
|
|
|
|
*/
|
|
|
|
new = bswap_16(new);
|
|
|
|
}
|
|
|
|
checksum = sum + new;
|
|
|
|
if (checksum > 0xFFFF) {
|
|
|
|
checksum -= 0xFFFF;
|
|
|
|
}
|
|
|
|
return (~checksum) & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
RANDOM - compute a random number between 0 and 2147483647L or 2147483562?
|
|
|
|
**************************************************************************/
|
2006-12-22 01:44:51 +00:00
|
|
|
long int random(void)
|
2005-03-08 18:53:11 +00:00
|
|
|
{
|
|
|
|
static int32_t seed = 0;
|
|
|
|
int32_t q;
|
|
|
|
if (!seed) /* Initialize linear congruential generator */
|
2006-06-16 14:48:31 +00:00
|
|
|
seed = currticks();
|
2005-03-08 18:53:11 +00:00
|
|
|
/* simplified version of the LCG given in Bruce Schneier's
|
|
|
|
"Applied Cryptography" */
|
|
|
|
q = seed/53668;
|
|
|
|
if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
SLEEP
|
|
|
|
**************************************************************************/
|
2006-12-20 03:14:59 +00:00
|
|
|
unsigned int sleep(unsigned int secs)
|
2005-03-08 18:53:11 +00:00
|
|
|
{
|
|
|
|
unsigned long tmo;
|
|
|
|
|
|
|
|
for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
|
|
|
|
}
|
2006-12-20 03:14:59 +00:00
|
|
|
return 0;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
INTERRUPTIBLE SLEEP
|
|
|
|
**************************************************************************/
|
|
|
|
void interruptible_sleep(int secs)
|
|
|
|
{
|
|
|
|
printf("<sleep>\n");
|
2006-12-20 03:14:59 +00:00
|
|
|
sleep(secs);
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
TWIDDLE
|
|
|
|
**************************************************************************/
|
|
|
|
void twiddle(void)
|
|
|
|
{
|
|
|
|
#ifdef BAR_PROGRESS
|
|
|
|
static int count=0;
|
|
|
|
static const char tiddles[]="-\\|/";
|
|
|
|
static unsigned long lastticks = 0;
|
|
|
|
unsigned long ticks;
|
|
|
|
#endif
|
|
|
|
if ( ! as_main_program ) return;
|
|
|
|
#ifdef BAR_PROGRESS
|
|
|
|
/* Limit the maximum rate at which characters are printed */
|
|
|
|
ticks = currticks();
|
|
|
|
if ((lastticks + (TICKS_PER_SEC/18)) > ticks)
|
|
|
|
return;
|
|
|
|
lastticks = ticks;
|
|
|
|
|
|
|
|
putchar(tiddles[(count++)&3]);
|
|
|
|
putchar('\b');
|
|
|
|
#else
|
|
|
|
putchar('.');
|
|
|
|
#endif /* BAR_PROGRESS */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
STRCASECMP (not entirely correct, but this will do for our purposes)
|
|
|
|
**************************************************************************/
|
|
|
|
int strcasecmp(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
|
|
|
|
return((*a & ~0x20) - (*b & ~0x20));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
INET_ATON - Convert an ascii x.x.x.x to binary form
|
|
|
|
**************************************************************************/
|
2005-04-30 13:50:34 +00:00
|
|
|
int inet_aton ( const char *cp, struct in_addr *inp ) {
|
|
|
|
const char *p = cp;
|
2005-03-08 18:53:11 +00:00
|
|
|
const char *digits_start;
|
|
|
|
unsigned long ip = 0;
|
|
|
|
unsigned long val;
|
|
|
|
int j;
|
|
|
|
for(j = 0; j <= 3; j++) {
|
|
|
|
digits_start = p;
|
2006-05-17 17:16:24 +00:00
|
|
|
val = strtoul(p, ( char ** ) &p, 10);
|
2005-03-08 18:53:11 +00:00
|
|
|
if ((p == digits_start) || (val > 255)) return 0;
|
|
|
|
if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
|
|
|
|
ip = (ip << 8) | val;
|
|
|
|
}
|
2005-04-30 13:50:34 +00:00
|
|
|
if ( *p == '\0' ) {
|
|
|
|
inp->s_addr = htonl(ip);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
|
|
|
|
2006-11-15 02:52:06 +00:00
|
|
|
unsigned long strtoul ( const char *p, char **endp, int base ) {
|
2005-03-08 18:53:11 +00:00
|
|
|
unsigned long ret = 0;
|
2006-11-15 02:52:06 +00:00
|
|
|
unsigned int charval;
|
|
|
|
|
|
|
|
if ( base == 0 ) {
|
2006-11-15 02:57:24 +00:00
|
|
|
base = 10;
|
|
|
|
if ( *p == '0' ) {
|
|
|
|
p++;
|
|
|
|
base = 8;
|
|
|
|
if ( ( *p | 0x20 ) == 'x' ) {
|
|
|
|
p++;
|
|
|
|
base = 16;
|
|
|
|
}
|
2006-11-15 02:52:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( 1 ) {
|
2006-11-15 04:16:26 +00:00
|
|
|
charval = *p;
|
|
|
|
if ( charval >= 'a' ) {
|
|
|
|
charval = ( charval - 'a' + 10 );
|
|
|
|
} else if ( charval >= 'A' ) {
|
|
|
|
charval = ( charval - 'A' + 10 );
|
|
|
|
} else {
|
|
|
|
charval = ( charval - '0' );
|
|
|
|
}
|
2006-11-15 02:52:06 +00:00
|
|
|
if ( charval >= ( unsigned int ) base )
|
|
|
|
break;
|
|
|
|
ret = ( ( ret * base ) + charval );
|
2006-11-15 02:54:28 +00:00
|
|
|
p++;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
2006-11-15 02:52:06 +00:00
|
|
|
|
|
|
|
if ( endp )
|
2006-05-17 17:16:24 +00:00
|
|
|
*endp = ( char * ) p;
|
2006-11-15 02:52:06 +00:00
|
|
|
|
|
|
|
return ( ret );
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|