You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

misc.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**************************************************************************
  2. MISC Support Routines
  3. **************************************************************************/
  4. #include "etherboot.h"
  5. #include "console.h"
  6. #include <stdlib.h>
  7. /**************************************************************************
  8. IPCHKSUM - Checksum IP Header
  9. **************************************************************************/
  10. uint16_t ipchksum(const void *data, unsigned long length)
  11. {
  12. unsigned long sum;
  13. unsigned long i;
  14. const uint8_t *ptr;
  15. /* In the most straight forward way possible,
  16. * compute an ip style checksum.
  17. */
  18. sum = 0;
  19. ptr = data;
  20. for(i = 0; i < length; i++) {
  21. unsigned long value;
  22. value = ptr[i];
  23. if (i & 1) {
  24. value <<= 8;
  25. }
  26. /* Add the new value */
  27. sum += value;
  28. /* Wrap around the carry */
  29. if (sum > 0xFFFF) {
  30. sum = (sum + (sum >> 16)) & 0xFFFF;
  31. }
  32. }
  33. return (~cpu_to_le16(sum)) & 0xFFFF;
  34. }
  35. uint16_t add_ipchksums(unsigned long offset, uint16_t sum, uint16_t new)
  36. {
  37. unsigned long checksum;
  38. sum = ~sum & 0xFFFF;
  39. new = ~new & 0xFFFF;
  40. if (offset & 1) {
  41. /* byte swap the sum if it came from an odd offset
  42. * since the computation is endian independant this
  43. * works.
  44. */
  45. new = bswap_16(new);
  46. }
  47. checksum = sum + new;
  48. if (checksum > 0xFFFF) {
  49. checksum -= 0xFFFF;
  50. }
  51. return (~checksum) & 0xFFFF;
  52. }
  53. /**************************************************************************
  54. RANDOM - compute a random number between 0 and 2147483647L or 2147483562?
  55. **************************************************************************/
  56. long int random(void)
  57. {
  58. static int32_t seed = 0;
  59. int32_t q;
  60. if (!seed) /* Initialize linear congruential generator */
  61. seed = currticks();
  62. /* simplified version of the LCG given in Bruce Schneier's
  63. "Applied Cryptography" */
  64. q = seed/53668;
  65. if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
  66. return seed;
  67. }
  68. /**************************************************************************
  69. SLEEP
  70. **************************************************************************/
  71. unsigned int sleep(unsigned int secs)
  72. {
  73. unsigned long tmo;
  74. for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
  75. }
  76. return 0;
  77. }
  78. /**************************************************************************
  79. INTERRUPTIBLE SLEEP
  80. **************************************************************************/
  81. void interruptible_sleep(int secs)
  82. {
  83. printf("<sleep>\n");
  84. sleep(secs);
  85. }
  86. /**************************************************************************
  87. TWIDDLE
  88. **************************************************************************/
  89. void twiddle(void)
  90. {
  91. #ifdef BAR_PROGRESS
  92. static int count=0;
  93. static const char tiddles[]="-\\|/";
  94. static unsigned long lastticks = 0;
  95. unsigned long ticks;
  96. #endif
  97. if ( ! as_main_program ) return;
  98. #ifdef BAR_PROGRESS
  99. /* Limit the maximum rate at which characters are printed */
  100. ticks = currticks();
  101. if ((lastticks + (TICKS_PER_SEC/18)) > ticks)
  102. return;
  103. lastticks = ticks;
  104. putchar(tiddles[(count++)&3]);
  105. putchar('\b');
  106. #else
  107. putchar('.');
  108. #endif /* BAR_PROGRESS */
  109. }
  110. /**************************************************************************
  111. STRCASECMP (not entirely correct, but this will do for our purposes)
  112. **************************************************************************/
  113. int strcasecmp(const char *a, const char *b)
  114. {
  115. while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
  116. return((*a & ~0x20) - (*b & ~0x20));
  117. }
  118. /**************************************************************************
  119. INET_ATON - Convert an ascii x.x.x.x to binary form
  120. **************************************************************************/
  121. int inet_aton ( const char *cp, struct in_addr *inp ) {
  122. const char *p = cp;
  123. const char *digits_start;
  124. unsigned long ip = 0;
  125. unsigned long val;
  126. int j;
  127. for(j = 0; j <= 3; j++) {
  128. digits_start = p;
  129. val = strtoul(p, ( char ** ) &p, 10);
  130. if ((p == digits_start) || (val > 255)) return 0;
  131. if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
  132. ip = (ip << 8) | val;
  133. }
  134. if ( *p == '\0' ) {
  135. inp->s_addr = htonl(ip);
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. unsigned long strtoul ( const char *p, char **endp, int base ) {
  141. unsigned long ret = 0;
  142. unsigned int charval;
  143. if ( base == 0 ) {
  144. base = 10;
  145. if ( *p == '0' ) {
  146. p++;
  147. base = 8;
  148. if ( ( *p | 0x20 ) == 'x' ) {
  149. p++;
  150. base = 16;
  151. }
  152. }
  153. }
  154. while ( 1 ) {
  155. charval = *p;
  156. if ( charval >= 'a' ) {
  157. charval = ( charval - 'a' + 10 );
  158. } else if ( charval >= 'A' ) {
  159. charval = ( charval - 'A' + 10 );
  160. } else {
  161. charval = ( charval - '0' );
  162. }
  163. if ( charval >= ( unsigned int ) base )
  164. break;
  165. ret = ( ( ret * base ) + charval );
  166. p++;
  167. }
  168. if ( endp )
  169. *endp = ( char * ) p;
  170. return ( ret );
  171. }
  172. /*
  173. * Local variables:
  174. * c-basic-offset: 8
  175. * End:
  176. */