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.8KB

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