annotate helpers/mokosrec2bin.c @ 108:d61a07309733

mmiEm.h: bogotab fixes
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 09 Nov 2020 01:07:01 +0000
parents 1fb47f5b597a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * GSM device firmwares that are built with TI's TMS470 toolchain in TI's
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * canonical way come out in TI's *.m0 format produced by TI's hex470 tool.
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * TI's *.m0 is a variant of the classic S-record format from Motorola,
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * but the specific variant depends on the -memwidth and -romwidth options
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * with which the hex470 tool is run.
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 * In TI's canonical architecture (as opposed to Mot/Compal's heavily modified
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 * version) this hex470 tool is run with -memwidth 16 -romwidth 16 options,
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * and the *.m0 file comes out in the format variant which we have nicknamed
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * "moko-style" after its most famous user. This variant is a byte-reversed
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 * S-record format in that each 16-bit word is byte-reversed relative to the
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 * native byte order of the ARM7 processor. (This strange byte order actually
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 * makes some sense if one views the image as a long array of 16-bit hex
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 * values; 16 bits is the width of the flash memory on Calypso GSM devices and
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 * thus the natural unit size for flash programming.)
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 *
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 * The present mokosrec2bin utility converts these "moko-style" S-record files
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 * to straight binary, a conversion that includes flipping the order of bytes.
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 */
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 #include <sys/types.h>
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 #include <stdio.h>
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 #include <ctype.h>
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 #include <string.h>
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 #include <strings.h>
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 #include <stdlib.h>
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 char *infname;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 FILE *inf, *outf;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 u_char fillbyte;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 char srecbuf[80];
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 u_char srecbin[40];
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 int lineno, state;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 u_long lastaddr;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 u_char header[6] = {0x06, 0x00, 0x00, 'H', 'D', 'R'};
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 decode_hex_byte(s)
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 char *s;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 register int u, l;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (!isxdigit(s[0]) || !isxdigit(s[1]))
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 return(-1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (isdigit(s[0]))
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 u = s[0] - '0';
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 else if (isupper(s[0]))
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 u = s[0] - 'A' + 10;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 else
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 u = s[0] - 'a' + 10;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (isdigit(s[1]))
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 l = s[1] - '0';
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 else if (isupper(s[1]))
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 l = s[1] - 'A' + 10;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 else
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 l = s[1] - 'a' + 10;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 return((u << 4) | l);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 srec2bin()
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 register int i, l, b;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 l = decode_hex_byte(srecbuf + 2);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 if (l < 1) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 fprintf(stderr, "%s line %d: S-record length octet is bad\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 srecbin[0] = l;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (l > 35) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 fprintf(stderr,
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 "%s line %d: S-record is longer than expected\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 for (i = 1; i <= l; i++) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 b = decode_hex_byte(srecbuf + i*2 + 2);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 if (b < 0) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 fprintf(stderr, "%s line %d: hex decode error\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 srecbin[i] = b;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 return(0);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 srec_cksum()
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 u_char accum;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 register int i, len;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 len = srecbin[0] + 1;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 accum = 0;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 for (i = 0; i < len; i++)
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 accum += srecbin[i];
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 if (accum != 0xFF) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 fprintf(stderr, "%s line %d: bad checksum\n", infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 return(0);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 main(argc, argv)
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 char **argv;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 register int i;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 u_long curaddr;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 int datalen;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 if (argc < 3 || argc > 4) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 usage: fprintf(stderr, "usage: %s input.m0 output.bin [fill-byte]\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 argv[0]);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 infname = argv[1];
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 inf = fopen(infname, "r");
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 if (!inf) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 perror(infname);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 if (argc > 3) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 i = decode_hex_byte(argv[3]);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 if (i >= 0)
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 fillbyte = i;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 else
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 goto usage;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 } else
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 fillbyte = 0xFF;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 state = 0;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 for (lineno = 1; ; lineno++) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 if (!fgets(srecbuf, sizeof srecbuf, inf)) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 fprintf(stderr, "%s: premature EOF\n", infname);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 if (srecbuf[0] != 'S') {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 fprintf(stderr, "%s line %d: not an S-record\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 switch (srecbuf[1]) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 case '0':
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 if (state == 0)
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 break;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 else
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 goto badtype;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 case '3':
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 if (state == 0)
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 goto badtype;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 else
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 break;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 case '7':
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 if (state == 2)
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 break;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 else
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 goto badtype;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 default:
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 badtype:
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 fprintf(stderr,
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 "%s line %d: S-record type unexpected\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 srec2bin();
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 srec_cksum();
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 if (state == 0) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 if (bcmp(srecbin, header, 6)) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 fprintf(stderr, "%s: expected header missing\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 infname);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 state = 1;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 continue;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 switch (srecbuf[1]) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 case '3':
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 if (srecbin[0] < 6) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 fprintf(stderr,
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 "%s line %d: S3 record is too short\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 curaddr = (srecbin[1] << 24) | (srecbin[2] << 16) |
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 (srecbin[3] << 8) | srecbin[4];
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 if (curaddr & 1) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 fprintf(stderr, "%s line %d: odd address\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 datalen = srecbin[0] - 5;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 if (datalen & 1) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 fprintf(stderr, "%s line %d: odd data length\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 if (state < 2) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 outf = fopen(argv[2], "w");
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 if (!outf) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 perror(argv[2]);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 state = 2;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 lastaddr = 0;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 if (curaddr < lastaddr) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 fprintf(stderr,
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 "%s line %d: address going backwards\n",
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 infname, lineno);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 exit(1);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 while (lastaddr < curaddr) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 putc(fillbyte, outf);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 lastaddr++;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 for (i = 0; i < datalen; i += 2) {
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 putc(srecbin[i + 6], outf);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 putc(srecbin[i + 5], outf);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 lastaddr = curaddr + datalen;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 continue;
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 case '7':
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 fclose(outf);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 exit(0);
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 default:
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 abort();
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 }
1fb47f5b597a helpers: import from Magnetite
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 }