s_record.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <s_record.h>
  25. static int hex1_bin (char c);
  26. static int hex2_bin (char *s);
  27. int srec_decode (char *input, int *count, ulong *addr, char *data)
  28. {
  29. int i;
  30. int v; /* conversion buffer */
  31. int srec_type; /* S-Record type */
  32. unsigned char chksum; /* buffer for checksum */
  33. chksum = 0;
  34. /* skip anything before 'S', and the 'S' itself.
  35. * Return error if not found
  36. */
  37. for (; *input; ++input) {
  38. if (*input == 'S') { /* skip 'S' */
  39. ++input;
  40. break;
  41. }
  42. }
  43. if (*input == '\0') { /* no more data? */
  44. return (SREC_EMPTY);
  45. }
  46. v = *input++; /* record type */
  47. if ((*count = hex2_bin(input)) < 0) {
  48. return (SREC_E_NOSREC);
  49. }
  50. chksum += *count;
  51. input += 2;
  52. switch (v) { /* record type */
  53. case '0': /* start record */
  54. srec_type = SREC_START; /* 2 byte addr field */
  55. *count -= 3; /* - checksum and addr */
  56. break;
  57. case '1':
  58. srec_type = SREC_DATA2; /* 2 byte addr field */
  59. *count -= 3; /* - checksum and addr */
  60. break;
  61. case '2':
  62. srec_type = SREC_DATA3; /* 3 byte addr field */
  63. *count -= 4; /* - checksum and addr */
  64. break;
  65. case '3': /* data record with a */
  66. srec_type = SREC_DATA4; /* 4 byte addr field */
  67. *count -= 5; /* - checksum and addr */
  68. break;
  69. /*** case '4' ***/
  70. case '5': /* count record, addr field contains */
  71. srec_type = SREC_COUNT; /* a 2 byte record counter */
  72. *count = 0; /* no data */
  73. break;
  74. /*** case '6' -- not used ***/
  75. case '7': /* end record with a */
  76. srec_type = SREC_END4; /* 4 byte addr field */
  77. *count -= 5; /* - checksum and addr */
  78. break;
  79. case '8': /* end record with a */
  80. srec_type = SREC_END3; /* 3 byte addr field */
  81. *count -= 4; /* - checksum and addr */
  82. break;
  83. case '9': /* end record with a */
  84. srec_type = SREC_END2; /* 2 byte addr field */
  85. *count -= 3; /* - checksum and addr */
  86. break;
  87. default:
  88. return (SREC_E_BADTYPE);
  89. }
  90. /* read address field */
  91. *addr = 0;
  92. switch (v) {
  93. case '3': /* 4 byte addr field */
  94. case '7':
  95. if ((v = hex2_bin(input)) < 0) {
  96. return (SREC_E_NOSREC);
  97. }
  98. *addr += v;
  99. chksum += v;
  100. input += 2;
  101. /* FALL THRU */
  102. case '2': /* 3 byte addr field */
  103. case '8':
  104. if ((v = hex2_bin(input)) < 0) {
  105. return (SREC_E_NOSREC);
  106. }
  107. *addr <<= 8;
  108. *addr += v;
  109. chksum += v;
  110. input += 2;
  111. /* FALL THRU */
  112. case '0': /* 2 byte addr field */
  113. case '1':
  114. case '5':
  115. case '9':
  116. if ((v = hex2_bin(input)) < 0) {
  117. return (SREC_E_NOSREC);
  118. }
  119. *addr <<= 8;
  120. *addr += v;
  121. chksum += v;
  122. input += 2;
  123. if ((v = hex2_bin(input)) < 0) {
  124. return (SREC_E_NOSREC);
  125. }
  126. *addr <<= 8;
  127. *addr += v;
  128. chksum += v;
  129. input += 2;
  130. break;
  131. default:
  132. return (SREC_E_BADTYPE);
  133. }
  134. /* convert data and calculate checksum */
  135. for (i=0; i < *count; ++i) {
  136. if ((v = hex2_bin(input)) < 0) {
  137. return (SREC_E_NOSREC);
  138. }
  139. data[i] = v;
  140. chksum += v;
  141. input += 2;
  142. }
  143. /* read anc check checksum */
  144. if ((v = hex2_bin(input)) < 0) {
  145. return (SREC_E_NOSREC);
  146. }
  147. if ((unsigned char)v != (unsigned char)~chksum) {
  148. return (SREC_E_BADCHKS);
  149. }
  150. return (srec_type);
  151. }
  152. static int hex1_bin (char c)
  153. {
  154. if (c >= '0' && c <= '9')
  155. return (c - '0');
  156. if (c >= 'a' && c <= 'f')
  157. return (c + 10 - 'a');
  158. if (c >= 'A' && c <= 'F')
  159. return (c + 10 - 'A');
  160. return (-1);
  161. }
  162. static int hex2_bin (char *s)
  163. {
  164. int i, j;
  165. if ((i = hex1_bin(*s++)) < 0) {
  166. return (-1);
  167. }
  168. if ((j = hex1_bin(*s)) < 0) {
  169. return (-1);
  170. }
  171. return ((i<<4) + j);
  172. }