annotate helpers/mokosrec2bin.c @ 206:fdde05764380

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