annotate frbl/test/srecreader.c @ 323:cefa700d1b8f

frbl: beginning of frbl2test
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 05 Mar 2020 22:05:01 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
323
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module contains the functions for reading S-record files.
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdint.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <ctype.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include "srecreader.h"
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 open_srec_file(sr)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 struct srecreader *sr;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 sr->openfile = fopen(sr->filename, "r");
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 if (!sr->openfile) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 perror(sr->filename);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 return(-1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 sr->lineno = 0;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 return(0);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 static
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 srec2bin(sr, asciiline)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 struct srecreader *sr;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 char *asciiline;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 register int i, l, b;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 l = decode_hex_byte(asciiline + 2);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (l < 1) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 fprintf(stderr, "%s line %d: S-record length octet is bad\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 sr->filename, sr->lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 return(-1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 sr->record[0] = l;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 for (i = 1; i <= l; i++) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 b = decode_hex_byte(asciiline + i*2 + 2);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (b < 0) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 fprintf(stderr,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 "%s line %d: S-record hex decode error\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 sr->filename, sr->lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 return(-1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 sr->record[i] = b;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 return(0);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 static
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 srec_cksum(sr)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 struct srecreader *sr;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 u_char accum;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 register int i, len;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 len = sr->record[0] + 1;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 accum = 0;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 for (i = 0; i < len; i++)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 accum += sr->record[i];
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (accum != 0xFF) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 fprintf(stderr, "%s line %d: bad S-record checksum\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 sr->filename, sr->lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 return(-1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 return(0);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 read_s_record(sr)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 struct srecreader *sr;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 char asciiline[1024];
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (!fgets(asciiline, sizeof asciiline, sr->openfile)) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 fprintf(stderr, "%s: premature EOF after %d S-records\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 sr->filename, sr->lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 return(-1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 sr->lineno++;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (asciiline[0] != 'S' || !isdigit(asciiline[1])) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 fprintf(stderr, "%s line %d: S-record expected\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 sr->filename, sr->lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 return(-1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 sr->record_type = asciiline[1];
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (srec2bin(sr, asciiline) < 0)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 return(-1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 return srec_cksum(sr);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 s3s7_get_addr_data(sr)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 struct srecreader *sr;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 if (sr->record[0] < 5) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 fprintf(stderr, "%s line %d: S%c record is too short\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 sr->filename, sr->lineno, sr->record_type);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 return(-1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 sr->datalen = sr->record[0] - 5;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 sr->addr = ((uint32_t)sr->record[1] << 24) |
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 ((uint32_t)sr->record[2] << 16) |
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 ((uint32_t)sr->record[3] << 8) |
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 (uint32_t)sr->record[4];
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 return(0);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 }