forked from zurachu/isd_for_linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiecedev.cpp
More file actions
316 lines (252 loc) · 6.1 KB
/
piecedev.cpp
File metadata and controls
316 lines (252 loc) · 6.1 KB
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
#include "piecedev.h"
#include <stdint.h>
#include <vector>
#include <cstring>
#include <cstdio>
#include "debug.h"
#include <unistd.h>
#define USB_ID_PRODUCT 0x1000
#define USB_ID_VENDOR 0x0e19
#define PIECE_END_POINT_IN 0x82
#define PIECE_END_POINT_OUT 0x02
#define BULK_WAIT 500
namespace n {
namespace piece {
namespace {
class Init
{
public:
Init() {
int r = libusb_init( NULL );
if ( r < 0 ) {
throw libusb_strerror( static_cast<enum libusb_error>(r) );
}
}
~Init() {
libusb_exit( NULL );
}
};
Init init;
}
UsbDevHandle::UsbDevHandle()
{
libusb_device_handle *udev;
udev = libusb_open_device_with_vid_pid( NULL, USB_ID_VENDOR, USB_ID_PRODUCT );
if ( udev == NULL )
throw "can't find device";
usb_dev_ = udev;
}
UsbDevHandle::~UsbDevHandle()
{
libusb_reset_device( usb_dev_ );
libusb_close( usb_dev_ );
}
Device::Device()
:mblk_adr_(pffs_top_)
{
int r = libusb_set_configuration( usb_dev_, 1 );
if ( r < 0 )
throw libusb_strerror( static_cast<enum libusb_error>(r) );
r = libusb_claim_interface( usb_dev_, 0 );
if ( r < 0 )
throw libusb_strerror( static_cast<enum libusb_error>(r) );
readVersion();
}
Device::~Device()
{
libusb_release_interface( usb_dev_, 0 );
}
void Device::readVersion()
{
char tmp[4];
char info[32];
tmp[0] = 0;
tmp[1] = 32;
write( tmp, 2 );
read( info, 32 );
uint32_t size = *((uint16_t*)(info));
if( size != 32 )
throw "unexpected size of system info";
hard_ver_ = *((uint16_t*)(info+2));
bios_ver_ = *((uint16_t*)(info+4));
bios_date_ = *((uint16_t*)(info+6));
sys_clock_ = *((uint32_t*)(info+8));
vdde_voltage_ = *((uint16_t*)(info+12));
sram_top_ = *((uint32_t*)(info+16));
sram_end_ = *((uint32_t*)(info+20));
pffs_top_ = *((uint32_t*)(info+24));
pffs_end_ = *((uint32_t*)(info+28));
}
void Device::write( const char *buf, size_t len, int timeout )
{
unsigned char *data = reinterpret_cast<unsigned char*>(const_cast<char*>(buf));
int actual_length = 0;
int r = libusb_bulk_transfer( usb_dev_, PIECE_END_POINT_OUT
, data, len, &actual_length, timeout );
if ( r < 0 )
throw libusb_strerror( static_cast<enum libusb_error>(r) );
}
void Device::read( char *buf, size_t len, int timeout )
{
unsigned char *data = reinterpret_cast<unsigned char*>(buf);
int actual_length = 0;
int r = libusb_bulk_transfer( usb_dev_, PIECE_END_POINT_IN
, data, len, &actual_length, timeout );
if ( r < 0 )
throw libusb_strerror( static_cast<enum libusb_error>(r) );
}
void Device::readMem( uint32_t begin, char *buf, size_t len )
{
char tmp[10];
tmp[0] = READ_MEM;
*(uint32_t*)(tmp+1) = begin;
*(uint32_t*)(tmp+5) = len;
write( tmp, 9 );
read( buf, len );
}
void Device::writeMem( uint32_t begin, const char *buf, size_t len )
{
char tmp[10];
tmp[0] = 3;
*(uint32_t *)(tmp+1) = begin;
*(uint32_t *)(tmp+5) = len;
write( tmp, 9 );
write( buf, len );
}
bool Device::doWriteFlash( uint32_t rom_adr, uint32_t buf_adr, size_t len )
{
char tmp[16];
tmp[0] = DO_FLASH_WRITE;
*(uint32_t *)(tmp+1) = rom_adr;
*(uint32_t *)(tmp+5) = buf_adr;
*(uint32_t *)(tmp+9) = len;
write( tmp, 13 );
read( tmp, 2 );
if ( (*(uint16_t*)tmp) == 0 )
return true;
return false;
}
bool Device::eraseFlash( uint32_t adr )
{
char tmp[8];
tmp[0] = ERASE_FLASH;
*(uint32_t*)(tmp+1) = adr;
write( tmp, 5 );
read( tmp, 2 );
if ( *(uint16_t*)tmp == 0 )
return true;
return false;
}
bool Device::writeFlash( uint32_t rom_adr, const char *buf, size_t len, bool safe )
{
DEBUG_MSG( "rom_adr: %x, len: %d\n", rom_adr, len );
if ( ( safe
&& rom_adr<(pffs_top_+4096) )
|| rom_adr>pffs_end_ ) {
fprintf( stderr, "%x -> unsafe write flash\n", rom_adr);
return false;
}
writeMem( 0x130000, buf, len );
if ( eraseFlash( rom_adr ) == false )
return false;
bool ret = doWriteFlash( rom_adr, 0x130000, len );
return ret;
}
namespace {
void milisleep( int mili ) {
usleep( mili*1000 );
}
}
int Device::getAppStat( )
{
char tmp[4];
tmp[0] = GET_APPSTAT;
write( tmp, 1 );
read( tmp, 2 );
return *(short*)tmp;
}
void Device::setAppStat( int stat )
{
char tmp[4];
tmp[0] = SET_APPSTAT;
tmp[1] = stat;
int next;
if ( stat == 3 )
next = 0;
else if ( stat == 1 )
next = 2;
else
return;
write( tmp, 2 );
for ( int i=0; i<100; i++ ) {
int now;
milisleep( 20 );
now = getAppStat( );
if ( now==next )
return;
}
throw "appstat timeout";
}
namespace {
uint32_t read_big_u32( unsigned char *mem )
{
return (((((mem[0]<<8)+mem[1])<<8)+mem[2])<<8)+mem[3];
}
uint16_t read_big_u16( unsigned char *mem )
{
return (mem[0]<<8)+mem[1];
}
}
void Device::uploadSrf( std::FILE *in )
{
unsigned char buf[64];
std::vector<char> tbuff( 32*1024 );
fseek( in, 0, SEEK_SET );
if ( fread( buf, 1, 16, in ) != 16 ) {
std::perror("fread");
return;
}
if ( (read_big_u16(buf) | 8 ) != 0x000e ) {
std::perror("header");
return;
}
long next = read_big_u32( buf+8 );
while ( next ) {
fseek( in, next, SEEK_SET );
if ( fread( buf, 1, 44, in ) != 44 )
break;
next = read_big_u32(buf);
size_t len = read_big_u32( buf+38 );
if ( len ) {
uint32_t adr = read_big_u32( buf+10 );
long pos = read_big_u32( buf+34 );
if ( pos ) {
std::printf( " %06x-%06lx\n", adr, adr+len-1 );
fseek( in, pos, SEEK_SET );
while ( len ) {
size_t const len2 = std::min( len, tbuff.size() );
fread( &tbuff[0], 1, len2, in );
writeMem(adr, &tbuff[0], len2);
adr += len2;
len -= len2;
}
}
}
}
}
void Device::dumpVersion( )
{
std::printf( "BIOS version = %d.%02d\n", bios_ver_ >> 8, bios_ver_ & 0xFF );
std::printf( "BIOS date = %d.%02d.%02d\n", 2000 + ( bios_date_ >> 9 )
, ( bios_date_ >> 5 ) & 0x0F, bios_date_ & 0x1F );
std::printf( "SRAM top adr = 0x%06x\n", sram_top_ );
std::printf( "SRAM end adr = 0x%06x\n", sram_end_ - 1 );
std::printf( "SRAM size = %d KB\n", ( sram_end_ - sram_top_ ) >> 10 );
std::printf( "HW version = %d.%02d\n", hard_ver_ >> 8, hard_ver_ & 0xFF );
std::printf( "SYSTEM clock = %5.3f MHz\n", sys_clock_ / 1e6 );
std::printf( "VDDE voltage = %5.3f V\n", vdde_voltage_ / 1e3 );
std::printf( "PFFS top adr = 0x%06x\n", pffs_top_ );
std::printf( "PFFS end adr = 0x%06x\n", pffs_end_ - 1 );
}
}
}