annotate miscprog/mokosrec2bin.c @ 323:cefa700d1b8f

frbl: beginning of frbl2test
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 05 Mar 2020 22:05:01 +0000
parents 597143ba1c37
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * The *.m0 format in which the Closedmoko firmware images have been
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * distributed is a form of SREC, but further examination quickly
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 * reveals that the data bytes are swapped: the byte order is neither
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 * the natural little-endian ARM one of the Calypso, nor a word-wise
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 * ARM BE; instead the upper and lower bytes of each 16-bit shortword
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 * are swapped relative to the natural byte/word order of the Calypso
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 * ARM processor.
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 *
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 * Before this Closedmoko firmware can be examined with any of the
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 * standard reverse eng tools (ARM disassemblers or even the good old
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 * strings), it needs to be converted from the weird byte-swapped SREC
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 * format to straight binary in the natural byte order. The present
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 * utility accomplishes that conversion.
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 *
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 * Written by Spacefalcon the Outlaw.
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 */
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 #include <sys/types.h>
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 #include <stdio.h>
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 #include <ctype.h>
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 #include <strings.h>
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 char *infname;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 FILE *inf, *outf;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 u_char fillbyte;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 char srecbuf[80];
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 u_char srecbin[40];
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 int lineno, state;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 u_long lastaddr;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 u_char header[6] = {0x06, 0x00, 0x00, 'H', 'D', 'R'};
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 decode_hex_byte(s)
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 char *s;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 register int u, l;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 if (!isxdigit(s[0]) || !isxdigit(s[1]))
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 return(-1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 if (isdigit(s[0]))
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 u = s[0] - '0';
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 else if (isupper(s[0]))
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 u = s[0] - 'A' + 10;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 else
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 u = s[0] - 'a' + 10;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 if (isdigit(s[1]))
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 l = s[1] - '0';
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 else if (isupper(s[1]))
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 l = s[1] - 'A' + 10;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 else
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 l = s[1] - 'a' + 10;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 return((u << 4) | l);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 srec2bin()
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 register int i, l, b;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 l = decode_hex_byte(srecbuf + 2);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 if (l < 1) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 fprintf(stderr, "%s line %d: S-record length octet is bad\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 srecbin[0] = l;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 if (l > 35) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 fprintf(stderr,
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 "%s line %d: S-record is longer than expected\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 for (i = 1; i <= l; i++) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 b = decode_hex_byte(srecbuf + i*2 + 2);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 if (b < 0) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 fprintf(stderr, "%s line %d: hex decode error\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 srecbin[i] = b;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 return(0);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 srec_cksum()
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 u_char accum;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 register int i, len;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90 len = srecbin[0] + 1;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 accum = 0;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 for (i = 0; i < len; i++)
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93 accum += srecbin[i];
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 if (accum != 0xFF) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 fprintf(stderr, "%s line %d: bad checksum\n", infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98 return(0);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 main(argc, argv)
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 char **argv;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103 {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 register int i;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 u_long curaddr;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 int datalen;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
107
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
108 if (argc < 3 || argc > 4) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
109 usage: fprintf(stderr, "usage: %s input.m0 output.bin [fill-byte]\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
110 argv[0]);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
111 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
112 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
113 infname = argv[1];
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
114 inf = fopen(infname, "r");
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
115 if (!inf) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
116 perror(infname);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
117 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
118 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
119 if (argc > 3) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
120 i = decode_hex_byte(argv[3]);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
121 if (i >= 0)
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
122 fillbyte = i;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
123 else
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
124 goto usage;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
125 } else
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
126 fillbyte = 0;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
127
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
128 state = 0;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
129 for (lineno = 1; ; lineno++) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
130 if (!fgets(srecbuf, sizeof srecbuf, inf)) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
131 fprintf(stderr, "%s: premature EOF\n", infname);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
132 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
133 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
134 if (srecbuf[0] != 'S') {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
135 fprintf(stderr, "%s line %d: not an S-record\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
136 infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
137 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
138 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
139 switch (srecbuf[1]) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
140 case '0':
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
141 if (state == 0)
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
142 break;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
143 else
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
144 goto badtype;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
145 case '3':
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
146 if (state == 0)
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
147 goto badtype;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
148 else
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
149 break;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
150 case '7':
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
151 if (state == 2)
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
152 break;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
153 else
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
154 goto badtype;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
155 default:
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
156 badtype:
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
157 fprintf(stderr,
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
158 "%s line %d: S-record type unexpected\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
159 infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
160 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
161 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
162 srec2bin();
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
163 srec_cksum();
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
164 if (state == 0) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
165 if (bcmp(srecbin, header, 6)) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
166 fprintf(stderr, "%s: expected header missing\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
167 infname);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
168 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
169 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
170 state = 1;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
171 continue;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
172 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
173 switch (srecbuf[1]) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
174 case '3':
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
175 if (srecbin[0] < 6) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
176 fprintf(stderr,
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
177 "%s line %d: S3 record is too short\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
178 infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
179 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
180 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
181 curaddr = (srecbin[1] << 24) | (srecbin[2] << 16) |
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
182 (srecbin[3] << 8) | srecbin[4];
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
183 if (curaddr & 1) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
184 fprintf(stderr, "%s line %d: odd address\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
185 infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
186 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
187 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
188 datalen = srecbin[0] - 5;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
189 if (datalen & 1) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
190 fprintf(stderr, "%s line %d: odd data length\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
191 infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
192 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
193 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
194 if (state < 2) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
195 outf = fopen(argv[2], "w");
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
196 if (!outf) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
197 perror(argv[2]);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
198 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
199 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
200 state = 2;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
201 lastaddr = 0;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
202 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
203 if (curaddr < lastaddr) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
204 fprintf(stderr,
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
205 "%s line %d: address going backwards\n",
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
206 infname, lineno);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
207 exit(1);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
208 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
209 while (lastaddr < curaddr) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
210 putc(fillbyte, outf);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
211 lastaddr++;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
212 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
213 for (i = 0; i < datalen; i += 2) {
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
214 putc(srecbin[i + 6], outf);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
215 putc(srecbin[i + 5], outf);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
216 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
217 lastaddr = curaddr + datalen;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
218 continue;
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
219 case '7':
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
220 fclose(outf);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
221 exit(0);
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
222 default:
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
223 abort();
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
224 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
225 }
cb736d95338d mokosrec2bin utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
226 }