load.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* control character used for download */
  2. #define ETX CNTRL('c')
  3. #define ACK CNTRL('f')
  4. #define NAK CNTRL('u')
  5. #define XON CNTRL('q')
  6. #define XOFF CNTRL('s')
  7. unsigned int csum;
  8. unsigned int dl_entry;
  9. static const unsigned char hextab[256] = {
  10. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  11.     255, 255, 255,
  12. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  13.     255, 255, 255,
  14. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  15.     255, 255, 255,
  16. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255,
  17. 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255,
  18.     255, 255,
  19. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  20.     255, 255, 255,
  21. 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255,
  22.     255, 255,
  23. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  24.     255, 255, 255,
  25. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  26.     255, 255, 255,
  27. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  28.     255, 255, 255,
  29. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  30.     255, 255, 255,
  31. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  32.     255, 255, 255,
  33. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  34.     255, 255, 255,
  35. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  36.     255, 255, 255,
  37. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  38.     255, 255, 255,
  39. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  40.     255, 255, 255,
  41. };
  42. unsigned char ascii_to_bin(unsigned char c)
  43. {
  44. return hextab[c];
  45. }
  46. unsigned char read_char_direct(void)
  47. {
  48. unsigned char c, *cp;
  49. cp = (unsigned char *) 0xbd000020;
  50. while (1) {
  51. if (*(cp + 0x14) & 0x01) {
  52. c = (volatile unsigned char) *cp;
  53. return c;
  54. }
  55. cp++;
  56. cp--;
  57. }
  58. }
  59. unsigned char get_pair(void)
  60. {
  61. unsigned char byte;
  62. byte = ascii_to_bin(read_char_direct()) << 4;
  63. byte |= ascii_to_bin(read_char_direct());
  64. csum += byte;
  65. return (byte);
  66. }
  67. void serial_putc(int ch)
  68. {
  69. unsigned long temp;
  70. for (temp = 0; temp < 1000; temp++) {
  71. }
  72. *(char *) 0xbd000020 = (char) ch;
  73. }
  74. int inline serial_getc(void)
  75. {
  76. return read_char_direct();
  77. }
  78. int serial_ischar(void)
  79. {
  80. unsigned char c, *cp;
  81. unsigned count;
  82. cp = (unsigned char *) 0xbd000020;
  83. count = 0;
  84. while (count != 100) {
  85. if (*(cp + 0x14) & 0x01) {
  86. c = (volatile unsigned char) *cp;
  87. return c;
  88. }
  89. cp++;
  90. cp--;
  91. count++;
  92. }
  93. return 0;
  94. }
  95. int serial_init(void)
  96. {
  97. return 0;
  98. }
  99. int galileo_dl(void)
  100. {
  101. #define display_char '.'
  102. #define display_error 'E'
  103. #define display_error_bad_7 '7'
  104. #define display_error_unknown 'U'
  105. #define display_error_length 'L'
  106. register int length, address, save_csum;
  107. int i, first, done, eof, reccount, type, client_pc;
  108. int src, dbl_length;
  109. unsigned char *buffptr, databuff[258], tempo;
  110. register int display_counter, chunks, leftovers, putter,
  111.     bytes_per_chunk;
  112. display_counter = 0;
  113. bytes_per_chunk = 16;
  114. csum = 0;
  115. reccount = 1;
  116. for (first = 1, done = 0; !done; first = 0, reccount++) {
  117. while (read_char_direct() != 'S')
  118. continue;
  119. csum = 0;
  120. type = read_char_direct();
  121. length = get_pair();
  122. if (length < 0 || length >= 256) {
  123. *(char *) 0xbd000020 = display_error_length;
  124. //      *(char*)0xbd00000c = display_error_length;
  125. return 0;
  126. }
  127. length--;
  128. switch (type) {
  129. case '0':
  130. while (length-- > 0)
  131. get_pair();
  132. break;
  133. case '3':
  134. address = 0;
  135. for (i = 0; i < 4; i++) {
  136. address <<= 8;
  137. address |= get_pair();
  138. length--;
  139. }
  140. if (address == -1) {
  141. eof = 1;
  142. continue;
  143. }
  144. buffptr = &databuff[0];
  145. dbl_length = length << 1;
  146. chunks = dbl_length / bytes_per_chunk;
  147. leftovers = dbl_length % bytes_per_chunk;
  148. putter = bytes_per_chunk >> 1;
  149. while (chunks--) {
  150. for (i = 0; i < bytes_per_chunk; i++)
  151. databuff[i] = read_char_direct();
  152. src = i = 0;
  153. while (i++ < putter) {
  154. tempo =
  155.     (ascii_to_bin(databuff[src++])
  156.      << 4) |
  157.     ascii_to_bin(databuff[src++]);
  158. csum += tempo;
  159. *(char *) address++ = tempo;
  160. }
  161. }
  162. if (leftovers) {
  163. putter = leftovers / 2;
  164. for (i = 0; i < leftovers; i++)
  165. databuff[i] = read_char_direct();
  166. src = i = 0;
  167. while (i++ < putter) {
  168. tempo =
  169.     (ascii_to_bin(databuff[src++])
  170.      << 4) |
  171.     ascii_to_bin(databuff[src++]);
  172. csum += tempo;
  173. *(char *) address++ = tempo;
  174. }
  175. }
  176. break;
  177. case '7':
  178. address = 0;
  179. for (i = 0; i < 4; i++) {
  180. address <<= 8;
  181. address |= get_pair();
  182. length--;
  183. }
  184. if (address == -1) {
  185. eof = 1;
  186. continue;
  187. }
  188. client_pc = address;
  189. if (length) {
  190. *(char *) 0xbd000020 = display_error_bad_7;
  191. //                      *(char*)0xbd00000c = display_error_bad_7;
  192. }
  193. done = 1;
  194. break;
  195. default:
  196. *(char *) 0xbd000020 = display_error_unknown;
  197. //              *(char*)0xbd00000c = display_error_unknown;
  198. break;
  199. }
  200. save_csum = (~csum) & 0xff;
  201. if ((csum = get_pair()) < 0) {
  202. eof = 1;
  203. continue;
  204. }
  205. if (csum != save_csum) {
  206. *(char *) 0xbd000020 = display_error;
  207. //                *(char*)0xbd00000c = display_error;
  208. } else {
  209. if (display_counter % 50 == 0) {
  210. *(char *) 0xbd000020 = display_char;
  211. display_counter = 0;
  212. }
  213. display_counter++;
  214. }
  215. }
  216. --reccount;
  217. dl_entry = client_pc;
  218. return dl_entry; /* Success */
  219. }