3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 static int hex1_bin (char c
);
28 static int hex2_bin (char *s
);
30 int srec_decode (char *input
, int *count
, ulong
*addr
, char *data
)
33 int v
; /* conversion buffer */
34 int srec_type
; /* S-Record type */
35 unsigned char chksum
; /* buffer for checksum */
39 /* skip anything before 'S', and the 'S' itself.
40 * Return error if not found
43 for (; *input
; ++input
) {
44 if (*input
== 'S') { /* skip 'S' */
49 if (*input
== '\0') { /* no more data? */
53 v
= *input
++; /* record type */
55 if ((*count
= hex2_bin(input
)) < 0) {
56 return (SREC_E_NOSREC
);
62 switch (v
) { /* record type */
64 case '0': /* start record */
65 srec_type
= SREC_START
; /* 2 byte addr field */
66 *count
-= 3; /* - checksum and addr */
69 srec_type
= SREC_DATA2
; /* 2 byte addr field */
70 *count
-= 3; /* - checksum and addr */
73 srec_type
= SREC_DATA3
; /* 3 byte addr field */
74 *count
-= 4; /* - checksum and addr */
76 case '3': /* data record with a */
77 srec_type
= SREC_DATA4
; /* 4 byte addr field */
78 *count
-= 5; /* - checksum and addr */
81 case '5': /* count record, addr field contains */
82 srec_type
= SREC_COUNT
; /* a 2 byte record counter */
83 *count
= 0; /* no data */
85 /*** case '6' -- not used ***/
86 case '7': /* end record with a */
87 srec_type
= SREC_END4
; /* 4 byte addr field */
88 *count
-= 5; /* - checksum and addr */
90 case '8': /* end record with a */
91 srec_type
= SREC_END3
; /* 3 byte addr field */
92 *count
-= 4; /* - checksum and addr */
94 case '9': /* end record with a */
95 srec_type
= SREC_END2
; /* 2 byte addr field */
96 *count
-= 3; /* - checksum and addr */
99 return (SREC_E_BADTYPE
);
102 /* read address field */
106 case '3': /* 4 byte addr field */
108 if ((v
= hex2_bin(input
)) < 0) {
109 return (SREC_E_NOSREC
);
115 case '2': /* 3 byte addr field */
117 if ((v
= hex2_bin(input
)) < 0) {
118 return (SREC_E_NOSREC
);
125 case '0': /* 2 byte addr field */
129 if ((v
= hex2_bin(input
)) < 0) {
130 return (SREC_E_NOSREC
);
137 if ((v
= hex2_bin(input
)) < 0) {
138 return (SREC_E_NOSREC
);
147 return (SREC_E_BADTYPE
);
150 /* convert data and calculate checksum */
151 for (i
=0; i
< *count
; ++i
) {
152 if ((v
= hex2_bin(input
)) < 0) {
153 return (SREC_E_NOSREC
);
160 /* read anc check checksum */
161 if ((v
= hex2_bin(input
)) < 0) {
162 return (SREC_E_NOSREC
);
165 if ((unsigned char)v
!= (unsigned char)~chksum
) {
166 return (SREC_E_BADCHKS
);
172 static int hex1_bin (char c
)
174 if (c
>= '0' && c
<= '9')
176 if (c
>= 'a' && c
<= 'f')
177 return (c
+ 10 - 'a');
178 if (c
>= 'A' && c
<= 'F')
179 return (c
+ 10 - 'A');
183 static int hex2_bin (char *s
)
187 if ((i
= hex1_bin(*s
++)) < 0) {
190 if ((j
= hex1_bin(*s
)) < 0) {