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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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() + *(int32_t *)&arptable[ARP_CLIENT].node
  61. + ((int16_t *)arptable[ARP_CLIENT].node)[2];
  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. POLL INTERRUPTIONS
  70. **************************************************************************/
  71. void poll_interruptions(void)
  72. {
  73. int ch;
  74. if ( ! as_main_program ) return;
  75. /* If an interruption has occured restart etherboot */
  76. if (iskey() && (ch = getchar(), (ch == K_ESC) || (ch == K_EOF) || (ch == K_INTR))) {
  77. int state = (ch != K_INTR)? -1 : -3;
  78. longjmp(restart_etherboot, state);
  79. }
  80. }
  81. /**************************************************************************
  82. SLEEP
  83. **************************************************************************/
  84. void sleep(int secs)
  85. {
  86. unsigned long tmo;
  87. for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
  88. poll_interruptions();
  89. }
  90. }
  91. /**************************************************************************
  92. INTERRUPTIBLE SLEEP
  93. **************************************************************************/
  94. void interruptible_sleep(int secs)
  95. {
  96. printf("<sleep>\n");
  97. return sleep(secs);
  98. }
  99. /**************************************************************************
  100. TWIDDLE
  101. **************************************************************************/
  102. void twiddle(void)
  103. {
  104. #ifdef BAR_PROGRESS
  105. static int count=0;
  106. static const char tiddles[]="-\\|/";
  107. static unsigned long lastticks = 0;
  108. unsigned long ticks;
  109. #endif
  110. if ( ! as_main_program ) return;
  111. #ifdef BAR_PROGRESS
  112. /* Limit the maximum rate at which characters are printed */
  113. ticks = currticks();
  114. if ((lastticks + (TICKS_PER_SEC/18)) > ticks)
  115. return;
  116. lastticks = ticks;
  117. putchar(tiddles[(count++)&3]);
  118. putchar('\b');
  119. #else
  120. putchar('.');
  121. #endif /* BAR_PROGRESS */
  122. }
  123. /**************************************************************************
  124. STRCASECMP (not entirely correct, but this will do for our purposes)
  125. **************************************************************************/
  126. int strcasecmp(const char *a, const char *b)
  127. {
  128. while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
  129. return((*a & ~0x20) - (*b & ~0x20));
  130. }
  131. /**************************************************************************
  132. INET_ATON - Convert an ascii x.x.x.x to binary form
  133. **************************************************************************/
  134. int inet_aton ( const char *cp, struct in_addr *inp ) {
  135. const char *p = cp;
  136. const char *digits_start;
  137. unsigned long ip = 0;
  138. unsigned long val;
  139. int j;
  140. for(j = 0; j <= 3; j++) {
  141. digits_start = p;
  142. val = strtoul(p, &p, 10);
  143. if ((p == digits_start) || (val > 255)) return 0;
  144. if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
  145. ip = (ip << 8) | val;
  146. }
  147. if ( *p == '\0' ) {
  148. inp->s_addr = htonl(ip);
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. unsigned long strtoul(const char *p, const char **endp, int base)
  154. {
  155. unsigned long ret = 0;
  156. if (base != 10) return 0;
  157. while((*p >= '0') && (*p <= '9')) {
  158. ret = ret*10 + (*p - '0');
  159. p++;
  160. }
  161. if (endp)
  162. *endp = p;
  163. return(ret);
  164. }
  165. #if DEBUG_UTILS
  166. void pause ( void ) {
  167. printf ( "\nPress a key" );
  168. getchar();
  169. printf ( "\r \r" );
  170. }
  171. void more ( void ) {
  172. printf ( "---more---" );
  173. getchar();
  174. printf ( "\r \r" );
  175. }
  176. /* Produce a paged hex dump of the specified data and length */
  177. void hex_dump ( const char *data, const unsigned int len ) {
  178. unsigned int index;
  179. for ( index = 0; index < len; index++ ) {
  180. if ( ( index % 16 ) == 0 ) {
  181. printf ( "\n" );
  182. }
  183. if ( ( index % 368 ) == 352 ) {
  184. more();
  185. }
  186. if ( ( index % 16 ) == 0 ) {
  187. printf ( "%X [%X] : %hX :", data + index,
  188. virt_to_phys ( data + index ), index );
  189. }
  190. printf ( " %hhX", data[index] );
  191. }
  192. printf ( "\n" );
  193. }
  194. #define GUARD_SYMBOL ( ( 'M' << 24 ) | ( 'I' << 16 ) | ( 'N' << 8 ) | 'E' )
  195. /* Fill a region with guard markers. We use a 4-byte pattern to make
  196. * it less likely that check_region will find spurious 1-byte regions
  197. * of non-corruption.
  198. */
  199. void guard_region ( void *region, size_t len ) {
  200. uint32_t offset = 0;
  201. len &= ~0x03;
  202. for ( offset = 0; offset < len ; offset += 4 ) {
  203. *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
  204. }
  205. }
  206. /* Check a region that has been guarded with guard_region() for
  207. * corruption.
  208. */
  209. int check_region ( void *region, size_t len ) {
  210. uint8_t corrupted = 0;
  211. uint8_t in_corruption = 0;
  212. uint32_t offset = 0;
  213. uint32_t test = 0;
  214. len &= ~0x03;
  215. for ( offset = 0; offset < len ; offset += 4 ) {
  216. test = *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
  217. if ( ( in_corruption == 0 ) &&
  218. ( test != GUARD_SYMBOL ) ) {
  219. /* Start of corruption */
  220. if ( corrupted == 0 ) {
  221. corrupted = 1;
  222. printf ( "Region %#x-%#x (physical %#x-%#x) "
  223. "corrupted\n",
  224. region, region + len,
  225. virt_to_phys ( region ),
  226. virt_to_phys ( region + len ) );
  227. }
  228. in_corruption = 1;
  229. printf ( "--- offset %#x ", offset );
  230. } else if ( ( in_corruption != 0 ) &&
  231. ( test == GUARD_SYMBOL ) ) {
  232. /* End of corruption */
  233. in_corruption = 0;
  234. printf ( "to offset %#x", offset );
  235. }
  236. }
  237. if ( in_corruption != 0 ) {
  238. printf ( "to offset %#x (end of region)\n", len-1 );
  239. }
  240. return corrupted;
  241. }
  242. #endif /* DEBUG_UTILS */
  243. /*
  244. * Local variables:
  245. * c-basic-offset: 8
  246. * End:
  247. */