annotate miscutil/mokosrec2bin.c @ 484:68c7e4edc4da

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