-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset_memory.cpp
400 lines (361 loc) · 10.7 KB
/
reset_memory.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
/** @file reset_memory.cpp
*
* @author marco corvi
* @date jan 2009
*
* @brief reset a portion of disto memory
* --------------------------------------------------------
* Copyright This sowftare is distributed under GPL-3.0 or later
* See the file COPYING.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> // read write close
#include "defaults.h"
#include "Serial.h"
/** DistoX memory block types
- FREE: 00 ...
ff ...
- HOT: 80 ... - fe ...
- USED: 01 ... - 7f ...
head :=
tail :=
*/
unsigned char reset_block[8] = {
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
unsigned char clear_block[8] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
unsigned char int_eeprom[16] = {
0x06, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xb7, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
unsigned char ext_eeprom[40] = {
0x01, 0x94, 0x01, 0xeb, 0x92, 0x7b, 0x19, 0xbf,
0x02, 0xef, 0xfe, 0x72, 0xfe, 0x75, 0x36, 0x00,
0x03, 0x61, 0x13, 0xde, 0xe2, 0x6a, 0x43, 0x00,
0x01, 0x93, 0x01, 0xcb, 0x24, 0xc5, 0x00, 0x01,
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
/** write to memory 4 bytes at a time the eight bytes at given address
* @param serial serial line (communication channel)
* @param addr address (should be multiple of 4)
* @param byte eight-byte array to write at (addr,addr+4)
*/
void
write_memory( Serial * serial, unsigned long addr, unsigned char * byte )
{
unsigned long reply_addr;
unsigned char buf[8];
int i, k;
ssize_t nr;
addr = addr - (addr % 8);
for (k=0; k<2; ++k) {
buf[0] = 0x38;
buf[1] = (unsigned char)( addr & 0xff );
buf[2] = (unsigned char)( (addr>>8) & 0xff );
nr = serial->Write( buf, 3 ); // read data
nr = serial->Read( buf, 8 );
if ( nr > 0 ) {
if ( buf[0] != 0x38 ) {
fprintf(stderr,
"ERROR: read wrong reply packet at addr %04lx cnt %d\n",
addr, k);
break;
}
reply_addr = ((unsigned long)(buf[2]))<<8 | buf[1];
if ( reply_addr != addr ) {
fprintf(stderr,
"ERROR: read wrong reply addr %04lx at addr %04lx cnt %d\n",
reply_addr, addr, k);
break;
}
for (i=3; i<7; ++i) fprintf(stderr, "%02x ", buf[i] );
} else if ( nr < 0 ) {
fprintf(stderr, "ERROR: serial read error\n");
break;
} else {
fprintf(stderr, "ERROR: read returns 0 bytes\n");
break;
}
buf[0] = 0x39;
buf[1] = (unsigned char)( addr & 0xff );
buf[2] = (unsigned char)( (addr>>8) & 0xff );
buf[3] = byte[0+k*4];
buf[4] = byte[1+k*4];
buf[5] = byte[2+k*4];
buf[6] = byte[3+k*4];
nr = serial->Write( buf, 7 ); // write data
nr = serial->Read( buf, 8 );
if ( nr > 0 ) {
if ( buf[0] != 0x38 ) {
fprintf(stderr,
"write() wrong reply packet at addr %04lx cnt %d\n",
addr, k);
break;
}
reply_addr = ((unsigned long)(buf[2]))<<8 | buf[1];
if ( reply_addr != addr ) {
fprintf(stderr,
"write() wrong reply addr %04lx at addr %04lx cnt %d\n",
reply_addr, addr, k);
break;
}
for (i=3; i<7; ++i) fprintf(stderr, "%02x ", buf[i] );
} else if ( nr < 0 ) {
perror("write_memory() error **** ");
break;
} else {
fprintf(stderr, "write_memory() read returns 0 bytes\n");
break;
}
addr += 4;
}
fprintf(stderr, "\n");
}
/** turn memory hot/used/calib at the given address
* @param serial serial line (communication channel)
* @param addr address (should be multiple of 4)
* @param mode whether to turn the memory hot or used or calib
*/
void
turn_memory( Serial * serial, unsigned long addr, int mode )
{
unsigned long reply_addr;
unsigned char buf[8];
int i;
ssize_t nr;
addr = addr - (addr % 8);
{
buf[0] = 0x38;
buf[1] = (unsigned char)( addr & 0xff );
buf[2] = (unsigned char)( (addr>>8) & 0xff );
nr = serial->Write( buf, 3 ); // read data
nr = serial->Read( buf, 8 );
if ( nr > 0 ) {
if ( buf[0] != 0x38 ) {
fprintf(stderr,
"ERROR: read wrong reply packet at addr %04lx \n",
addr);
return;
}
reply_addr = ((unsigned long)(buf[2]))<<8 | buf[1];
if ( reply_addr != addr ) {
fprintf(stderr,
"ERROR: read wrong reply addr %04lx at addr %04lx \n",
reply_addr, addr);
return;
}
fprintf(stderr, "%04lx ", addr );
if ( buf[3] == 0 || ( buf[3] & 0x80 ) != 0 ) fprintf(stderr, "*** ");
for (i=3; i<7; ++i) fprintf(stderr, "%02x ", buf[i] );
} else if ( nr < 0 ) {
fprintf(stderr, "ERROR: serial read error\n");
return;
} else {
fprintf(stderr, "ERROR: read returns 0 bytes\n");
return;
}
buf[0] = 0x39;
buf[1] = (unsigned char)( addr & 0xff );
buf[2] = (unsigned char)( (addr>>8) & 0xff );
if ( mode == 1 ) { // HOT: turn on bit 7 of the first bye
buf[3] |= 0x80;
if ( buf[3] < 0x81 || buf[3] > 0x83 ) buf[3] = 0x81;
} else if ( mode == 2 ) { // USED: turn off bit 7 of the first byte
// buf[3] &= 0x7f;
// if ( buf[3] == 0 ) buf[3] = 0x01;
buf[3] = 0x01;
} else if ( mode == 3 ) {
buf[3] = 0x02;
} else if ( mode == 4 ) {
buf[3] = 0x03;
}
// buf[4] ... buf[6] unchanged
nr = serial->Write( buf, 7 ); // write data
nr = serial->Read( buf, 8 );
if ( nr > 0 ) {
if ( buf[0] != 0x38 ) {
fprintf(stderr,
"write() wrong reply packet at addr %04lx \n",
addr);
return;
}
reply_addr = ((unsigned long)(buf[2]))<<8 | buf[1];
if ( reply_addr != addr ) {
fprintf(stderr,
"write() wrong reply addr %04lx at addr %04lx \n",
reply_addr, addr);
return;
}
fprintf(stderr, " --> ");
for (i=3; i<7; ++i) fprintf(stderr, "%02x ", buf[i] );
} else if ( nr < 0 ) {
perror("turn_memory() error **** ");
return;
} else {
fprintf(stderr, "turn_memory() read returns 0 bytes\n");
return;
}
}
fprintf(stderr, "\n");
}
void usage()
{
fprintf(stderr, "Usage: reset_memory [-d device] addr [end_addr] byte\n");
fprintf(stderr, "where\n");
fprintf(stderr, " the device is usually %s\n", DEFAULT_DEVICE );
fprintf(stderr, " addr is 0x0000 - 0x8000 for external EEPROM\n");
fprintf(stderr, " 0x8000 - 0x8100 for internal EEPROM\n");
fprintf(stderr, " 0xC000 - 0xC100 for RAM\n");
fprintf(stderr, " byte is one of: \n");
fprintf(stderr, " - clear ff ff ff ff ff ff ff ff ff\n");
fprintf(stderr, " - reset 00 ff ff ff ff ff ff ff ff\n");
fprintf(stderr, " - hot 81 ... \n");
fprintf(stderr, " - used 01 ... \n");
fprintf(stderr, " - int_eeprom 06 00 ff ff ff ff ff ff ff\n");
fprintf(stderr, " b7 00 ff ff ff ff ff ff ff\n");
fprintf(stderr, " - ext_eeprom first five bytes of external eeprom\n");
fprintf(stderr, " The end_address is used only with byte \"clear\", \"hot\" and \"used\" \n");
}
int
check_addresses( unsigned long addr, unsigned long end_addr )
{
if (end_addr > 0x8000 ) end_addr = 0x8000;
if ( end_addr <= addr ) {
fprintf(stderr,
"End address 0x%04lx, larger than start address 0x%04lx\n",
end_addr, addr );
return -1;
}
if ( addr >= 0x8000 ) {
fprintf(stderr, "address 0x%04lx is out of ext EEPROM bounds\n", addr );
return -1;
}
return (end_addr - addr)/8;
}
int main( int argc, char ** argv )
{
const char * device = DEFAULT_DEVICE;
unsigned long addr = 0x0;
unsigned long end_addr = 0x0;
unsigned char * byte = NULL;
int cnt = 0;
int mode = -1; // -1 undefined
// 0 clear|reset|eeprom
// 1 turn hot
// 2 turn used
// 3 turn calib
int ac = 1;
if ( argc <= ac) {
usage();
return 0;
}
if ( strcmp(argv[ac], "-d" ) == 0 ) {
device = argv[++ac];
++ac;
}
if ( argc <= ac ) {
usage();
fprintf(stderr, "missing address specification\n");
return 0;
}
sscanf( argv[ac], "%lx", &addr );
++ ac;
addr = addr - (addr % 8);
if ( argc <= ac ) {
usage();
fprintf(stderr, "missing byte specification\n");
return 0;
}
if (strncmp( argv[ac], "0x", 2) == 0 ) {
sscanf( argv[ac], "%lx", &end_addr );
end_addr = end_addr - (end_addr % 8);
++ac;
} else {
end_addr = addr + 8;
}
if (strncmp( argv[ac], "clear", 5) == 0) {
mode = 0;
byte = clear_block;
cnt = check_addresses( addr, end_addr );
if ( cnt <= 0 ) return 0;
} else if ( strncmp( argv[ac], "hot", 3) == 0 ) {
mode = 1;
cnt = check_addresses( addr, end_addr );
if ( cnt <= 0 ) return 0;
} else if ( strncmp( argv[ac], "used", 4) == 0 ) {
mode = 2;
cnt = check_addresses( addr, end_addr );
if ( cnt <= 0 ) return 0;
} else if ( strncmp( argv[ac], "calib", 5) == 0 ) {
mode = 3;
cnt = check_addresses( addr, end_addr ) / 2;
if ( cnt <= 0 ) return 0;
} else if ( strncmp( argv[ac], "reset", 5) == 0 ) {
mode = 0;
byte = reset_block;
cnt = 1;
if ( addr >= 0x8000 ) {
fprintf(stderr, "address 0x%04lx is out of ext EEPROM bounds\n", addr );
return 0;
}
} else if ( strncmp( argv[ac], "ext_eeprom", 10) == 0 ) {
mode = 0;
byte = ext_eeprom;
cnt = 5;
if ( addr != 0x0000 ) {
fprintf(stderr, "wrong address: must be 0x0000 for ext. EEPROM 5\n");
return 0;
}
} else if ( strncmp( argv[ac], "int_eeprom", 10) == 0 ) {
mode = 0;
byte = int_eeprom;
cnt = 2;
if ( addr != 0x8000 ) {
fprintf(stderr, "wrong address: must be 0x8000 for int. EEPROM 0\n");
return 0;
}
}
if ( mode == -1 ) {
usage();
fprintf(stderr, "wrong byte specification\n");
return 0;
}
if ( addr >= 0x8100 && ( addr < 0xC000 || addr >= 0xC100 ) ) {
fprintf(stderr, "address 0x%04lx is out of bounds\n", addr );
return 0;
}
fprintf(stderr, "Device %s addr 0x%04lx \n", device, addr );
Serial serial( device );
if ( ! serial.Open( ) ) {
fprintf(stderr, "Failed to open device %s\n", device );
return 1;
}
if ( mode == 0 ) {
for ( int c = 0; c<cnt; ++c ) {
write_memory( &serial, addr, byte );
addr += 8;
if ( byte != clear_block ) { // eeprom
byte += 8;
}
}
} else { // mode == 1 (hot) or 2 (used) or 3 (calib)
for ( int c = 0; c<cnt; ++c ) {
turn_memory( &serial, addr, mode );
addr += 8;
if ( mode == 3 ) {
turn_memory( &serial, addr, 4 );
addr += 8;
}
}
}
serial.Close();
return 0;
}