annotate helpers/mokosrec2bin.c @ 692:555cdc5fda0d

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