comparison loadtools/srecreader.c @ 8:acaac9162574

loadtools modules coming along
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 30 Apr 2013 21:35:14 +0000
parents aa1f6fe16fef
children fea204bc7674
comparison
equal deleted inserted replaced
7:aa1f6fe16fef 8:acaac9162574
1 /* 1 /*
2 * This module contains the functions for reading S-record files. 2 * This module contains the functions for reading S-record files.
3 */ 3 */
4 4
5 #include <sys/types.h> 5 #include <sys/types.h>
6 #include <stdint.h>
6 #include <stdio.h> 7 #include <stdio.h>
7 #include <ctype.h> 8 #include <ctype.h>
8 #include <strings.h> 9 #include <strings.h>
9 #include "srecreader.h" 10 #include "srecreader.h"
10 11
18 } 19 }
19 sr->lineno = 0; 20 sr->lineno = 0;
20 return(0); 21 return(0);
21 } 22 }
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
23 read_s_record(sr) 70 read_s_record(sr)
24 struct srecreader *sr; 71 struct srecreader *sr;
25 { 72 {
73 char asciiline[1024];
26 74
27 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);
28 } 90 }