Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

misc.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 *start, in_addr *i)
  135. {
  136. const char *p = start;
  137. const char *digits_start;
  138. unsigned long ip = 0;
  139. unsigned long val;
  140. int j;
  141. for(j = 0; j <= 3; j++) {
  142. digits_start = p;
  143. val = strtoul(p, &p, 10);
  144. if ((p == digits_start) || (val > 255)) return 0;
  145. if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
  146. ip = (ip << 8) | val;
  147. }
  148. i->s_addr = htonl(ip);
  149. return p - start;
  150. }
  151. unsigned long strtoul(const char *p, const char **endp, int base)
  152. {
  153. unsigned long ret = 0;
  154. if (base != 10) return 0;
  155. while((*p >= '0') && (*p <= '9')) {
  156. ret = ret*10 + (*p - '0');
  157. p++;
  158. }
  159. if (endp)
  160. *endp = p;
  161. return(ret);
  162. }
  163. #if DEBUG_UTILS
  164. void pause ( void ) {
  165. printf ( "\nPress a key" );
  166. getchar();
  167. printf ( "\r \r" );
  168. }
  169. void more ( void ) {
  170. printf ( "---more---" );
  171. getchar();
  172. printf ( "\r \r" );
  173. }
  174. /* Produce a paged hex dump of the specified data and length */
  175. void hex_dump ( const char *data, const unsigned int len ) {
  176. unsigned int index;
  177. for ( index = 0; index < len; index++ ) {
  178. if ( ( index % 16 ) == 0 ) {
  179. printf ( "\n" );
  180. }
  181. if ( ( index % 368 ) == 352 ) {
  182. more();
  183. }
  184. if ( ( index % 16 ) == 0 ) {
  185. printf ( "%X [%X] : %hX :", data + index,
  186. virt_to_phys ( data + index ), index );
  187. }
  188. printf ( " %hhX", data[index] );
  189. }
  190. printf ( "\n" );
  191. }
  192. #define GUARD_SYMBOL ( ( 'M' << 24 ) | ( 'I' << 16 ) | ( 'N' << 8 ) | 'E' )
  193. /* Fill a region with guard markers. We use a 4-byte pattern to make
  194. * it less likely that check_region will find spurious 1-byte regions
  195. * of non-corruption.
  196. */
  197. void guard_region ( void *region, size_t len ) {
  198. uint32_t offset = 0;
  199. len &= ~0x03;
  200. for ( offset = 0; offset < len ; offset += 4 ) {
  201. *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
  202. }
  203. }
  204. /* Check a region that has been guarded with guard_region() for
  205. * corruption.
  206. */
  207. int check_region ( void *region, size_t len ) {
  208. uint8_t corrupted = 0;
  209. uint8_t in_corruption = 0;
  210. uint32_t offset = 0;
  211. uint32_t test = 0;
  212. len &= ~0x03;
  213. for ( offset = 0; offset < len ; offset += 4 ) {
  214. test = *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
  215. if ( ( in_corruption == 0 ) &&
  216. ( test != GUARD_SYMBOL ) ) {
  217. /* Start of corruption */
  218. if ( corrupted == 0 ) {
  219. corrupted = 1;
  220. printf ( "Region %#x-%#x (physical %#x-%#x) "
  221. "corrupted\n",
  222. region, region + len,
  223. virt_to_phys ( region ),
  224. virt_to_phys ( region + len ) );
  225. }
  226. in_corruption = 1;
  227. printf ( "--- offset %#x ", offset );
  228. } else if ( ( in_corruption != 0 ) &&
  229. ( test == GUARD_SYMBOL ) ) {
  230. /* End of corruption */
  231. in_corruption = 0;
  232. printf ( "to offset %#x", offset );
  233. }
  234. }
  235. if ( in_corruption != 0 ) {
  236. printf ( "to offset %#x (end of region)\n", len-1 );
  237. }
  238. return corrupted;
  239. }
  240. #endif /* DEBUG_UTILS */
  241. /*
  242. * Local variables:
  243. * c-basic-offset: 8
  244. * End:
  245. */