comparison loadtools/srecreader.c @ 0:e7502631a0f9

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