forked from xobs/nandsee
/
joiner.cpp
419 lines (362 loc) · 12.4 KB
/
joiner.cpp
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <QFile>
#include "packet-struct.h"
#include "state.h"
#define SKIP_AMOUNT 80
#define SEARCH_LIMIT 20
struct pkt packet_buffer[SKIP_AMOUNT];
enum prog_state {
ST_UNINITIALIZED, // Starting state
ST_DONE, // Finished operation
ST_SEARCHING, // Searching for either a NAND block or a sync point
ST_JOINING, // Found NAND block, joining data
ST_DRAINING, // Found NAND block, just copying
ST_BACKTRACK, // Hit a seek point, so copying data before last NAND block
ST_OVERFLOWED, // FIFO overflowed, we'll be doing another loop
};
static int st_uninitialized(struct state *st);
static int st_searching(struct state *st);
static int st_joining(struct state *st);
static int st_draining(struct state *st);
static int st_backtrack(struct state *st);
static int st_done(struct state *st);
static int st_overflowed(struct state *st);
static int (*st_funcs[])(struct state *st) = {
st_uninitialized,
st_done,
st_searching,
st_joining,
st_draining,
st_backtrack,
st_overflowed,
};
/* Pulls a packet out of the buffer.
* It pulls it out of the given offset.
*/
static int buffer_get_packet(struct state *st, struct pkt *pkt) {
memcpy(pkt, &packet_buffer[(st->buffer_offset+st->search_limit)%SKIP_AMOUNT], sizeof(*pkt));
st->buffer_offset++;
st->buffer_offset %= SKIP_AMOUNT;
return 0;
}
static int buffer_unget_packet(struct state *st, struct pkt *pkt) {
Q_UNUSED(pkt);
st->buffer_offset--;
if (st->buffer_offset < 0)
st->buffer_offset = SKIP_AMOUNT-1;
return 0;
}
static int buffer_put_packet(struct state *st, struct pkt *pkt) {
st->buffer_offset++;
st->buffer_offset %= SKIP_AMOUNT;
memcpy(&packet_buffer[(st->buffer_offset+st->search_limit)%SKIP_AMOUNT],
pkt,
sizeof(*pkt));
return 0;
}
static int is_ib_command(struct state *st, struct pkt *pkt) {
Q_UNUSED(st);
return (pkt->header.type == PACKET_COMMAND
&& (pkt->data.command.cmd[0] == 'i'
&& pkt->data.command.cmd[1] == 'b')
);
}
static int is_sync_point(struct state *st, struct pkt *pkt) {
Q_UNUSED(st);
return ((pkt->header.type == PACKET_HELLO)
|| (pkt->header.type == PACKET_COMMAND
&& (pkt->data.command.cmd[0] == 'i'
&& pkt->data.command.cmd[1] == 'b'
&& pkt->data.command.arg == 0))
|| (pkt->header.type == PACKET_COMMAND
&& (pkt->data.command.cmd[0] == 'i'
&& pkt->data.command.cmd[1] == 'b'
&& pkt->data.command.arg == 4026531839))
);
}
static int is_nand(struct state *st, struct pkt *pkt) {
Q_UNUSED(st);
return (pkt->header.type == PACKET_NAND_CYCLE);
}
// Initialize the "joiner" state machine
struct state *jstate_init() {
struct state *st = (struct state *)malloc(sizeof(struct state));
memset(st, 0, sizeof(*st));
st->is_logging = 0;
st->st = ST_SEARCHING;
st->last_run_offset = 0;
st->join_buffer_capacity = 0;
st->buffer_offset = -1;
st->search_limit = 0;
return st;
}
int jstate_state(struct state *st) {
return st->st;
}
int jstate_set(struct state *st, enum prog_state new_state) {
st->st = new_state;
return 0;
}
int jstate_run(struct state *st) {
return st_funcs[st->st](st);
}
int jstate_free(struct state **st) {
free(*st);
*st = NULL;
return 0;
}
// Dummy state that should never be reached
static int st_uninitialized(struct state *st) {
Q_UNUSED(st);
printf("state error: should not be in this state\n");
return -1;
}
// Searching for either a NAND block or a sync point
static int st_searching(struct state *st) {
struct pkt pkt;
int ret;
while ((ret = packet_get_next_raw(st, &pkt)) == 0) {
if (is_sync_point(st, &pkt)) {
jstate_set(st, ST_BACKTRACK);
packet_unget(st, &pkt);
break;
}
else if (is_nand(st, &pkt)) {
jstate_set(st, ST_JOINING);
packet_unget(st, &pkt);
break;
}
// If it's a regular "IB" command, we're re-syncing. Backtrack to
// here later on.
else if (is_ib_command(st, &pkt)) {
packet_get_next_raw(st, &pkt);
st->last_run_offset = st->fdh->pos();
}
}
// -2 is the EOF error. Backtrack and fill things out.
if (ret == -2) {
jstate_set(st, ST_BACKTRACK);
return st_backtrack(st);
}
return ret;
}
// Go back to the previous sync point, then read everything from there to
// the current sync point, not including the NAND blocks.
static int st_backtrack(struct state *st) {
struct pkt pkt;
int ret;
int before_nand = 1;
ret = st->fdh->seek(st->last_run_offset);
if (-1 == ret) {
perror("Unable to backtrack");
}
// Read every packet from the last
while ((ret = packet_get_next_raw(st, &pkt)) == 0) {
// Eventually we'll hit the sync point that brought us here.
// Return to searching.
if (is_sync_point(st, &pkt)) {
if (pkt.header.type == PACKET_HELLO) {
pkt.header.sec = 0;
pkt.header.nsec = 0;
packet_write(st, &pkt);
}
jstate_set(st, ST_SEARCHING);
st->last_run_offset = st->fdh->pos();
break;
}
else if (is_nand(st, &pkt))
before_nand = 0;
else {
int nsec_dif, sec_dif;
if (before_nand) {
sec_dif = st->last_sec_dif;
nsec_dif = st->last_nsec_dif;
}
else {
sec_dif = st->sec_dif;
nsec_dif = st->nsec_dif;
}
if (nsec_dif > 0) {
pkt.header.nsec += nsec_dif;
if (pkt.header.nsec > 1000000000L) {
pkt.header.nsec -= 1000000000L;
pkt.header.sec++;
}
pkt.header.sec += sec_dif;
}
else {
pkt.header.nsec -= nsec_dif;
if (pkt.header.nsec <= 0) {
pkt.header.nsec += 1000000000L;
pkt.header.sec--;
}
pkt.header.sec -= sec_dif;
}
// Fudge the time for the "reset card" command
// (due to timing weirdness, it can vary widely.)
if (pkt.header.type == PACKET_COMMAND
&& pkt.data.command.cmd[0] == 'r'
&& pkt.data.command.cmd[1] == 'c') {
pkt.header.sec = 0;
pkt.header.nsec = 8;
}
st->last_sec = pkt.header.sec;
st->last_nsec = pkt.header.nsec;
packet_write(st, &pkt);
}
}
return ret;
}
static int st_done(struct state *st) {
Q_UNUSED(st);
return 1;
}
static int st_overflowed(struct state *st) {
Q_UNUSED(st);
printf("Overflowed not supported\n");
return 1;
}
static int st_draining(struct state *st) {
Q_UNUSED(st);
printf("Draining not supported\n");
return 1;
}
static int fill_buffer(struct state *st, struct pkt *pkts, int count, int
(*get_data)(struct state *st, struct pkt *arg)) {
int i;
for (i=0; i<count; i++) {
get_data(st, &pkts[i]);
}
return 0;
}
static int empty_buffer(struct state *st, struct pkt *pkts, int count, int
(*unget_data)(struct state *st, struct pkt *arg)) {
int i;
for (i=0; i<count; i++) {
unget_data(st, &pkts[i]);
}
return 0;
}
// We hit a "NAND" packet. This means we should write out data to the
// output file.
// If this is a new stretch of joining, just write packets out.
// If it's a continuation, try to match up the output.
#define REQUIRED_MATCHES (SKIP_AMOUNT*30/100)
static int st_joining(struct state *st) {
struct pkt pkt;
int ret;
// Actually attempt to join the data
if (st->buffer_offset >= 0) {
struct pkt pkts[REQUIRED_MATCHES];
struct pkt old_pkts[REQUIRED_MATCHES];
int synced = 0;
int disk_offset = 0;
int buffer_start = st->buffer_offset;
for (disk_offset=disk_offset;
(disk_offset+REQUIRED_MATCHES) < SKIP_AMOUNT && !synced;
disk_offset++) {
fill_buffer(st, pkts, REQUIRED_MATCHES, packet_get_next_raw);
for (st->search_limit = 0;
(st->search_limit + REQUIRED_MATCHES) < SKIP_AMOUNT && !synced;
st->search_limit++)
{
int i;
int matches_found = 0;
fill_buffer(st, old_pkts, REQUIRED_MATCHES, buffer_get_packet);
// Check to see if our run matches up
for (i=0; i<REQUIRED_MATCHES; i++) {
int dat = pkts[i].data.nand_cycle.data;
int old_dat = old_pkts[i].data.nand_cycle.data;
int ctrl = pkts[i].data.nand_cycle.control;
int old_ctrl = old_pkts[i].data.nand_cycle.control;
if (dat == old_dat && ctrl == old_ctrl)
matches_found++;
}
// If enough packets match, we're synced
if (matches_found >= REQUIRED_MATCHES-1) {
st->last_sec_dif = st->sec_dif;
st->last_nsec_dif = st->nsec_dif;
st->sec_dif =
-(pkts[REQUIRED_MATCHES/2].header.sec-old_pkts[REQUIRED_MATCHES/2].header.sec);
st->nsec_dif =
-(pkts[REQUIRED_MATCHES/2].header.nsec-old_pkts[REQUIRED_MATCHES/2].header.nsec);
if (st->nsec_dif > 1000000000L) {
st->nsec_dif -= 1000000000L;
st->sec_dif++;
}
else if (st->nsec_dif < 0) {
st->nsec_dif += 1000000000L;
st->sec_dif--;
}
synced=1;
buffer_unget_packet(st, old_pkts);
}
else {
empty_buffer(st, old_pkts, REQUIRED_MATCHES, buffer_unget_packet);
buffer_get_packet(st, old_pkts);
}
}
if (!synced) {
empty_buffer(st, pkts, REQUIRED_MATCHES, packet_unget);
empty_buffer(st, old_pkts, REQUIRED_MATCHES,
buffer_unget_packet);
packet_get_next_raw(st, pkts);
}
}
if (!synced)
printf("Couldn't join\n");
// Now we're synced, just ignore the rest of the matched-buffer
// packets. This is because if they're in the buffer, they've
// already been written out.
int tries = 0;
while ((st->buffer_offset+st->search_limit)%SKIP_AMOUNT != buffer_start-1) {
struct pkt old_pkt;
int dat, old_dat, ctrl, old_ctrl;
buffer_get_packet(st, &old_pkt);
packet_get_next_raw(st, &pkt);
dat = pkt.data.nand_cycle.data;
old_dat = old_pkt.data.nand_cycle.data;
ctrl = pkt.data.nand_cycle.control;
old_ctrl = old_pkt.data.nand_cycle.control;
tries++;
if ((dat != old_dat) || (ctrl != old_ctrl)) {
printf("Join anomaly after %d tries: %d/%d and %d/%d\n",
tries, old_dat, dat, old_ctrl, ctrl);
}
}
st->buffer_offset = -1;
st->search_limit = 0;
}
// Done now, copy data
while ((ret = packet_get_next_raw(st, &pkt)) == 0) {
if (!is_nand(st, &pkt)) {
packet_unget(st, &pkt);
jstate_set(st, ST_SEARCHING);
break;
}
if (st->nsec_dif > 0) {
pkt.header.nsec += st->nsec_dif;
if (pkt.header.nsec > 1000000000L) {
pkt.header.nsec -= 1000000000L;
pkt.header.sec++;
}
pkt.header.sec += st->sec_dif;
}
else {
pkt.header.nsec -= st->nsec_dif;
if (pkt.header.nsec <= 0) {
pkt.header.nsec += 1000000000L;
pkt.header.sec--;
}
pkt.header.sec -= st->sec_dif;
}
packet_write(st, &pkt);
buffer_put_packet(st, &pkt);
}
return ret;
}