-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtdslib.c
333 lines (267 loc) · 7.96 KB
/
tdslib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* collection of funcs for working with Tektronix TDS7xx ROMs (5xx/6xx probably also)
* (c) fenugrec 2018
* GPLv3
*/
#include <stddef.h> //for offsetof
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> //malloc
#include "stypes.h"
#include "tdslib.h"
#define file_maxsize (8*1024*1024UL) //arbitrary 8MB limit
uint32_t reconst_32(const uint8_t *buf) {
// ret 4 bytes at *buf with SH endianness
// " reconst_4B"
return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
}
uint16_t reconst_16(const uint8_t *buf) {
return buf[0] << 8 | buf[1];
}
void write_32b(uint32_t val, uint8_t *buf) {
//write 4 bytes at *buf with SH endianness
// "decomp_4B"
buf += 3; //start at end
*(buf--) = val & 0xFF;
val >>= 8;
*(buf--) = val & 0xFF;
val >>= 8;
*(buf--) = val & 0xFF;
val >>= 8;
*(buf--) = val & 0xFF;
return;
}
//Painfully unoptimized, because it's easy to get it wrong
const uint8_t *u8memstr(const uint8_t *buf, uint32_t buflen, const uint8_t *needle, unsigned nlen) {
uint32_t hcur;
if (!buf || !needle || (nlen > buflen)) return NULL;
for (hcur=0; hcur < (buflen - nlen); hcur++) {
if (memcmp(buf + hcur, needle, nlen)==0) {
return &buf[hcur];
}
}
return NULL;
}
/* manual, aligned search for u16 val */
const uint8_t *u16memstr(const uint8_t *buf, uint32_t buflen, const uint16_t needle) {
buflen &= ~1;
uint32_t cur;
for (cur = 0; cur < buflen; cur += 2) {
if (reconst_16(&buf[cur]) == needle) return &buf[cur];
}
return NULL;
}
/* manual, aligned search instead of calling u8memstr; should be faster */
const uint8_t *u32memstr(const uint8_t *buf, uint32_t buflen, const uint32_t needle) {
buflen &= ~3;
uint32_t cur;
for (cur = 0; cur < buflen; cur += 4) {
if (reconst_32(&buf[cur]) == needle) return &buf[cur];
}
return NULL;
}
// hax, get file length but restore position
static u32 _flen(FILE *hf) {
long siz;
long orig;
if (!hf) return 0;
orig = ftell(hf);
if (orig < 0) return 0;
if (fseek(hf, 0, SEEK_END)) return 0;
siz = ftell(hf);
if (siz < 0) siz=0;
//the rest of the code just won't work if siz = UINT32_MAX
#if (LONG_MAX >= UINT32_MAX)
if ((long long) siz == (long long) UINT32_MAX) siz = 0;
#endif
if (fseek(hf, orig, SEEK_SET)) return 0;
return (u32) siz;
}
/** Find opcode pattern... bleh
* "patlen" is # of opcodes
*/
uint32_t find_pattern(const uint8_t *buf, uint32_t siz, unsigned patlen,
const uint16_t *pat, const uint16_t *mask) {
uint32_t bcur = 0; //base cursor for start of pattern
uint32_t hcur = 0; //iterating cursor
unsigned patcur = 0; //cursor within pattern
while (hcur < (siz - patlen * 2)) {
uint16_t val;
val = reconst_16(&buf[hcur + patcur * 2]);
if ((val & mask[patcur]) == pat[patcur]) {
if (patcur == 0) bcur = hcur;
patcur += 1;
if (patcur == patlen) {
//complete match !
return bcur;
}
continue;
}
//no match : continue where we started
patcur = 0;
//hcur = bcur;
hcur += 2;
}
//no match : ret -1 (illegal val)
return -1;
}
#define SYMTBL_MAXSDIST 8
#define SYMSIZE_LOBOUND 0x1000
#define SYMSIZE_HIBOUND 0x2000
/** Attempt to guess the start and size of the sym table.
*
* Probably won't work as-is on many ROMs since it depends
* on the ROM header (which varies, i.e. 7xxC and 7xxD are
* are not quite like 7xxA). The strategy is
* to start at the end of the idata section and work backwards
* to find the "symtbl_size" value which is conveniently stored
* right there.
*/
static void _find_symtbl(struct flashrom *flrom) {
u32 idata_end = flrom->fh.idata_start + (flrom->fh.bss_start - flrom->fh.sdata);
u32 cur = idata_end - ROM_BASE; //conv to file offs
u32 stop = cur - SYMTBL_MAXSDIST;
for (; cur >= stop; cur -= 2) {
u32 test = reconst_32(&flrom->rom[cur]);
if ((test <= SYMSIZE_HIBOUND) && (test >= SYMSIZE_LOBOUND)) {
flrom->sym_num = test;
flrom->symloc = cur - (test * sizeof(struct sym_entry));
return;
}
}
/* if not found : fill bogus value that should at least not crash */
flrom->sym_num = 1;
flrom->symloc = 0;
return;
}
void parse_romhdr(const uint8_t *buf, struct flashrom_hdr *fh) {
fh->rominit = reconst_32(&buf[offsetof(struct flashrom_hdr, rominit)]);
fh->bodyck_start = reconst_32(&buf[offsetof(struct flashrom_hdr, bodyck_start)]);
fh->idata_start = reconst_32(&buf[offsetof(struct flashrom_hdr, idata_start)]);
fh->sdata = reconst_32(&buf[offsetof(struct flashrom_hdr, sdata)]);
fh->bss_start = reconst_32(&buf[offsetof(struct flashrom_hdr, bss_start)]);
fh->body_cks = reconst_32(&buf[offsetof(struct flashrom_hdr, body_cks)]);
fh->hdr_cks = reconst_16(&buf[offsetof(struct flashrom_hdr, hdr_cks)]);
return;
}
void parse_sym(const u8 *buf, struct sym_entry *se) {
se->p_name = reconst_32(&buf[offsetof(struct sym_entry, p_name)]);
se->p_obj = reconst_32(&buf[offsetof(struct sym_entry, p_obj)]);
return;
}
uint32_t find_sym(struct flashrom *flrom, const uint8_t *name, size_t nlen) {
const u8 *buf = flrom->rom;
u32 siz = flrom->siz;
u32 cur = flrom->symloc;
u32 sym_idx;
for (sym_idx = 0; sym_idx <= flrom->sym_num; sym_idx++) {
cur += sizeof(struct sym_entry);
if (cur >= siz) return 0;
// cheat : don't parse whole entry
u32 pname = reconst_32(&buf[cur + offsetof(struct sym_entry, p_name)]);
pname -= ROM_BASE; //now a file offset
if ((pname + nlen) > siz) {
//invalid; continue anyway
continue;
}
if (memcmp(&buf[pname], name, nlen) == 0) {
//found !
return cur;
}
}
return 0;
}
bool pm_parse16v(struct flashrom *flrom, u16 *dest, const char *sym) {
const u8 *buf = flrom->rom;
u32 siz = flrom->siz;
u32 psymoffs = find_sym(flrom, (const u8 *) sym, strlen(sym));
if (!(psymoffs)) {
printf("Couldn't find %s !\n", sym);
return 0;
}
u32 pobj = reconst_32(&buf[psymoffs + offsetof(struct sym_entry, p_obj)]);
if (!pobj || (pobj < ROM_BASE) ||
((pobj - ROM_BASE) > siz)) {
printf("bad pobj\n");
return 0;
}
pobj -= ROM_BASE;
*dest = (u16) reconst_32(&buf[pobj]);
if (!*dest) {
printf("bad value for %s\n", sym);
return 0;
}
return 1;
}
bool pm_parse32(struct flashrom *flrom, u32 *dest, u32 base, const char *sym) {
const u8 *buf = flrom->rom;
u32 psymoffs = find_sym(flrom, (const u8 *) sym, strlen(sym));
if (!(psymoffs)) {
printf("Couldn't find %s !\n", sym);
return 0;
}
u32 pobj = reconst_32(&buf[psymoffs + offsetof(struct sym_entry, p_obj)]);
if (!pobj || (pobj < base)) {
printf("bad pobj\n");
return 0;
}
pobj -= base;
*dest = pobj;
if (!*dest) {
printf("bad value for %s\n", sym);
return 0;
}
return 1;
}
struct flashrom *loadrom(FILE *romfil) {
u32 file_len;
struct flashrom * flrom;
rewind(romfil);
file_len = _flen(romfil);
if ((!file_len) || (file_len > file_maxsize)) {
printf("huge file (length %lu)\n", (unsigned long) file_len);
return NULL;
}
flrom = malloc(sizeof(struct flashrom));
if (!flrom) {
printf("malloc choke\n");
return NULL;
}
u8 *src = malloc(file_len);
if (!src) {
free(flrom);
printf("malloc choke\n");
return NULL;
}
/* load whole ROM */
if (fread(src,1,file_len,romfil) != file_len) {
printf("trouble reading\n");
free(flrom);
free(src);
return NULL;
}
flrom->rom = src;
flrom->siz = file_len;
parse_romhdr(src, &flrom->fh);
_find_symtbl(flrom);
flrom->idata_offset = flrom->fh.sdata - flrom->fh.idata_start;
return flrom;
}
void closerom(struct flashrom *flrom) {
free(flrom->rom);
free(flrom);
return;
}
void print_rominfo(struct flashrom *flrom) {
u32 idata_siz;
printf("bodyck_start:\t%lX\n", (unsigned long) flrom->fh.bodyck_start);
printf("idata_start:\t%lX\n", (unsigned long) flrom->fh.idata_start);
printf("sdata:\t%lX\n", (unsigned long) flrom->fh.sdata);
printf("bss_start:\t%lX\n", (unsigned long) flrom->fh.bss_start);
idata_siz = flrom->fh.bss_start - flrom->fh.sdata;
printf("idata siz:\t%lX\n", (unsigned long) idata_siz);
printf("idata end:\t%lX\n", (unsigned long) flrom->fh.idata_start + idata_siz);
printf("symtbl @\t%lX\n", (unsigned long) flrom->symloc);
printf("sym entries:\t%lX\n", (unsigned long) flrom->sym_num);
return;
}