FreeCalypso > hg > freecalypso-sw
annotate loadtools/srecreader.c @ 919:1dbc0f0851b3
.hgignore: fc-pirhackinit added
author | Space Falcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Tue, 08 Sep 2015 19:50:23 +0000 |
parents | fea204bc7674 |
children |
rev | line source |
---|---|
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * This module contains the functions for reading S-record files. |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 */ |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 #include <sys/types.h> |
8
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
6 #include <stdint.h> |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <stdio.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <ctype.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <strings.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include "srecreader.h" |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 open_srec_file(sr) |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 struct srecreader *sr; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 sr->openfile = fopen(sr->filename, "r"); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 if (!sr->openfile) { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 perror(sr->filename); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 return(-1); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 } |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 sr->lineno = 0; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 return(0); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 } |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 |
8
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
24 static |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
25 srec2bin(sr, asciiline) |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
26 struct srecreader *sr; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
27 char *asciiline; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
28 { |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
29 register int i, l, b; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
30 |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
31 l = decode_hex_byte(asciiline + 2); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
32 if (l < 1) { |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
33 fprintf(stderr, "%s line %d: S-record length octet is bad\n", |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
34 sr->filename, sr->lineno); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
35 return(-1); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
36 } |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
37 sr->record[0] = l; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
38 for (i = 1; i <= l; i++) { |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
39 b = decode_hex_byte(asciiline + i*2 + 2); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
40 if (b < 0) { |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
41 fprintf(stderr, |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
42 "%s line %d: S-record hex decode error\n", |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
43 sr->filename, sr->lineno); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
44 return(-1); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
45 } |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
46 sr->record[i] = b; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
47 } |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
48 return(0); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
49 } |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
50 |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
51 static |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
52 srec_cksum(sr) |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
53 struct srecreader *sr; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
54 { |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
55 u_char accum; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
56 register int i, len; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
57 |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
58 len = sr->record[0] + 1; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
59 accum = 0; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
60 for (i = 0; i < len; i++) |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
61 accum += sr->record[i]; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
62 if (accum != 0xFF) { |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
63 fprintf(stderr, "%s line %d: bad S-record checksum\n", |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
64 sr->filename, sr->lineno); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
65 return(-1); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
66 } |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
67 return(0); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
68 } |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
69 |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 read_s_record(sr) |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 struct srecreader *sr; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 { |
8
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
73 char asciiline[1024]; |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 |
8
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
75 if (!fgets(asciiline, sizeof asciiline, sr->openfile)) { |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
76 fprintf(stderr, "%s: premature EOF after %d S-records\n", |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
77 sr->filename, sr->lineno); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
78 return(-1); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
79 } |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
80 sr->lineno++; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
81 if (asciiline[0] != 'S' || !isdigit(asciiline[1])) { |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
82 fprintf(stderr, "%s line %d: S-record expected\n", |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
83 sr->filename, sr->lineno); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
84 return(-1); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
85 } |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
86 sr->record_type = asciiline[1]; |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
87 if (srec2bin(sr, asciiline) < 0) |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
88 return(-1); |
acaac9162574
loadtools modules coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
89 return srec_cksum(sr); |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 } |
9
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
91 |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
92 s3s7_get_addr_data(sr) |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
93 struct srecreader *sr; |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
94 { |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
95 if (sr->record[0] < 5) { |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
96 fprintf(stderr, "%s line %d: S%c record is too short\n", |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
97 sr->filename, sr->lineno, sr->record_type); |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
98 return(-1); |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
99 } |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
100 sr->datalen = sr->record[0] - 5; |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
101 sr->addr = ((uint32_t)sr->record[1] << 24) | |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
102 ((uint32_t)sr->record[2] << 16) | |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
103 ((uint32_t)sr->record[3] << 8) | |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
104 (uint32_t)sr->record[4]; |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
105 return(0); |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
8
diff
changeset
|
106 } |