Subversion Repositories freemyipod

Rev

Rev 786 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 786 Rev 898
Line 1... Line 1...
1
//
1
//
2
//
2
//
3
//    Copyright 2011 user890104
3
//    Copyright 2013 user890104
4
//
4
//
5
//
5
//
6
//    This file is part of emCORE.
6
//    This file is part of emCORE.
7
//
7
//
8
//    emCORE is free software: you can redistribute it and/or
8
//    emCORE is free software: you can redistribute it and/or
Line 18... Line 18...
18
//    You should have received a copy of the GNU General Public License along
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
20
//
21
//
21
//
22
 
22
 
23
 
-
 
24
#include "global.h"
23
#include "global.h"
25
 
24
 
26
#include "emcore.h"
25
#include "emcore.h"
27
#include "util.h"
26
#include "util.h"
28
#include "usb.h"
27
#include "usb.h"
29
 
28
 
30
 
-
 
31
struct emcore_usb_endpoints_addr emcore_usb_eps_addr;
-
 
32
struct emcore_usb_endpoints_max_packet_size emcore_usb_eps_mps;
-
 
33
 
-
 
34
int emcore_cout(const void* data, uint32_t length)
29
int32_t emcore_send(const void *data, uint32_t length) {
35
{
-
 
36
    return usb_bulk_transfer(emcore_usb_eps_addr.cout, (void*)data, length);
-
 
37
}
-
 
38
 
-
 
39
int emcore_cin(void* data, uint32_t length)
-
 
40
{
-
 
41
    return usb_bulk_transfer(emcore_usb_eps_addr.cin, data, length);
-
 
42
}
-
 
43
 
-
 
44
int emcore_dout(const void* data, uint32_t length)
-
 
45
{
-
 
46
    return usb_bulk_transfer(emcore_usb_eps_addr.dout, (void*)data, length);
30
    return usb_control_transfer(0x41, (void *)data, length);
47
}
31
}
48
 
32
 
49
int emcore_din(void* data, uint32_t length)
33
int32_t emcore_receive(void *data, uint32_t length) {
50
{
-
 
51
    return usb_bulk_transfer(emcore_usb_eps_addr.din, data, length);
34
    return usb_control_transfer(0xc1, data, length);
52
}
35
}
53
 
36
 
54
int emcore_monitor_command(const void* out, void* in,
37
int32_t emcore_monitor_command(const void *out, void *in,
55
    uint32_t send_length, uint32_t receive_length)
38
    uint32_t send_length, uint32_t receive_length) {
56
{
-
 
57
    int res;
39
    int32_t res;
58
    uint32_t status;
40
    uint32_t status;
59
 
41
 
60
#ifdef DEBUG_USB_PACKETS
42
#ifdef DEBUG_USB_PACKETS
61
    fprintf(stderr, "--------------------------------------------\n");
43
    fprintf(stderr, "--------------------------------------------\n");
62
    fprintf(stderr, "Sending %d bytes...\n", send_length);
44
    fprintf(stderr, "Sending %d bytes...\n", send_length);
63
 
45
 
64
    dump_packet(out, send_length);
46
    dump_packet(out, send_length);
65
 
47
 
66
#endif
48
#endif
67
    res = emcore_cout(out, send_length);
49
    res = emcore_send(out, send_length);
68
 
50
 
69
    if (LIBUSB_SUCCESS != res)
51
    if (res != LIBUSB_SUCCESS) {
70
    {
-
 
71
        return res;
52
        return res;
72
    }
53
    }
73
 
54
 
74
    if (in && receive_length)
55
    if (in && receive_length) {
75
    {
-
 
76
#ifdef DEBUG_USB_PACKETS
56
#ifdef DEBUG_USB_PACKETS
77
        fprintf(stderr, "Receiving %d bytes...\n", receive_length);
57
        fprintf(stderr, "Receiving %d bytes...\n", receive_length);
78
 
58
 
79
#endif
59
#endif
80
        res = emcore_cin(in, receive_length);
60
        res = emcore_receive(in, receive_length);
81
 
61
 
82
        if (LIBUSB_SUCCESS != res)
62
        if (res != LIBUSB_SUCCESS) {
83
        {
-
 
84
            return res;
63
            return res;
85
        }
64
        }
86
 
65
 
87
#ifdef DEBUG_USB_PACKETS
66
#ifdef DEBUG_USB_PACKETS
88
        dump_packet(in, receive_length);
67
        dump_packet(in, receive_length);
89
 
68
 
90
#endif
69
#endif
91
        status = *((int *)(in));
70
        status = *((int32_t *)(in));
92
    }
71
    }
93
    else
72
    else {
94
    {
-
 
95
        status = EMCORE_SUCCESS;
73
        status = EMCORE_SUCCESS;
96
    }
74
    }
97
 
75
 
98
#ifdef DEBUG_USB_PACKETS
76
#ifdef DEBUG_USB_PACKETS
99
    fprintf(stderr, "--------------------------------------------\n");
77
    fprintf(stderr, "--------------------------------------------\n");
100
 
78
 
101
#endif
79
#endif
102
    switch (status)
80
    switch (status) {
103
    {
-
 
104
        case 0:
81
        case 0:
105
            return EMCORE_ERROR_INVALID;
82
            return EMCORE_ERROR_INVALID;
106
        break;
83
        break;
107
        case 1:
84
        case 1:
108
            return EMCORE_SUCCESS;
85
            return EMCORE_SUCCESS;
Line 117... Line 94...
117
            return EMCORE_ERROR_INVALID;
94
            return EMCORE_ERROR_INVALID;
118
        break;
95
        break;
119
    }
96
    }
120
}
97
}
121
 
98
 
122
int emcore_get_version(struct emcore_dev_info* dev_info)
99
int32_t emcore_get_version(struct emcore_dev_info *dev_info) {
123
{
-
 
124
    int res;
100
    int32_t res;
125
    uint32_t out[4] = { 1, 0, 0, 0 }, in[4];
101
    uint32_t out[4] = { 1, 0, 0, 0 }, in[4];
126
 
102
 
127
    res = emcore_monitor_command(out, in, 16, 16);
103
    res = emcore_monitor_command(out, in, 16, 16);
128
 
104
 
129
    if (EMCORE_SUCCESS != res)
105
    if (res != EMCORE_SUCCESS) {
130
    {
-
 
131
        return res;
106
        return res;
132
    }
107
    }
133
 
108
 
134
    memcpy(dev_info, &in[1], sizeof(*dev_info));
109
    memcpy(dev_info, &in[1], sizeof(*dev_info));
135
 
110
 
136
    return EMCORE_SUCCESS;
111
    return EMCORE_SUCCESS;
137
}
112
}
138
 
113
 
139
int emcore_get_packet_info(struct emcore_usb_endpoints_max_packet_size* max_packet_size)
114
int32_t emcore_get_malloc_pool_bounds(struct emcore_malloc_pool_bounds *bounds) {
140
{
-
 
141
    int res;
115
    int32_t res;
142
    uint32_t out[4] = { 1, 1, 0, 0 }, in[4];
116
    uint32_t out[4] = { 1, 1, 0, 0 }, in[4];
143
 
117
 
144
    res = emcore_monitor_command(out, in, 16, 16);
118
    res = emcore_monitor_command(out, in, 16, 16);
145
 
119
 
146
    if (EMCORE_SUCCESS != res)
-
 
147
    {
-
 
148
        return res;
-
 
149
    }
-
 
150
 
-
 
151
    memcpy(max_packet_size, &in[1], sizeof(*max_packet_size));
-
 
152
 
-
 
153
    return EMCORE_SUCCESS;
120
    if (res != EMCORE_SUCCESS) {
154
}
-
 
155
 
-
 
156
int emcore_get_user_mem_range(struct emcore_user_mem_range* mem_range)
-
 
157
{
-
 
158
    int res;
-
 
159
    uint32_t out[4] = { 1, 2, 0, 0 }, in[4];
-
 
160
 
-
 
161
    res = emcore_monitor_command(out, in, 16, 16);
-
 
162
 
-
 
163
    if (EMCORE_SUCCESS != res)
-
 
164
    {
-
 
165
        return res;
121
        return res;
166
    }
122
    }
167
 
123
 
168
    memcpy(mem_range, &in[1], sizeof(*mem_range));
124
    memcpy(bounds, &in[1], sizeof(*bounds));
169
 
125
 
170
    return EMCORE_SUCCESS;
126
    return EMCORE_SUCCESS;
171
}
127
}
172
 
128
 
173
int emcore_reset(uint8_t graceful)
129
int32_t emcore_reset(uint8_t graceful) {
174
{
-
 
175
    int res;
130
    int32_t res;
176
    uint32_t out[4] = { 2, 0xdeadbeef, 0, 0 }, in[4];
131
    uint32_t out[4] = { 2, 0xdeadbeef, 0, 0 };
177
 
132
 
178
    out[1] = graceful;
133
    out[1] = graceful;
179
 
134
 
180
    res = emcore_monitor_command(out, in, 16, graceful ? 16 : 0);
135
    res = emcore_monitor_command(out, NULL, 16, 0);
181
 
136
 
182
    if (EMCORE_SUCCESS != res)
137
    if (res != EMCORE_SUCCESS) {
183
    {
-
 
184
        return res;
138
        return res;
185
    }
139
    }
186
 
140
 
187
    return EMCORE_SUCCESS;
141
    return EMCORE_SUCCESS;
188
}
142
}
189
 
143
 
190
int emcore_poweroff(uint8_t graceful)
144
int32_t emcore_poweroff(uint8_t graceful) {
191
{
-
 
192
    int res;
145
    int32_t res;
193
    uint32_t out[4] = { 3, 0xdeadbeef, 0, 0 }, in[4];
146
    uint32_t out[4] = { 3, 0xdeadbeef, 0, 0 };
194
 
147
 
195
    out[1] = graceful;
148
    out[1] = graceful;
196
 
149
 
197
    res = emcore_monitor_command(out, in, 16, graceful ? 16 : 0);
150
    res = emcore_monitor_command(out, NULL, 16, 0);
198
 
151
 
199
    if (EMCORE_SUCCESS != res)
152
    if (res != EMCORE_SUCCESS) {
200
    {
-
 
201
        return res;
153
        return res;
202
    }
154
    }
203
 
155
 
204
    return EMCORE_SUCCESS;
156
    return EMCORE_SUCCESS;
205
}
157
}
206
 
158
 
207
int emcore_readmem(void* data, uint32_t addr, uint32_t size)
159
int32_t emcore_read(void *data, uint32_t addr, uint32_t size) {
208
{
-
 
209
    int res;
160
    int32_t res;
210
    uint32_t data_length, out[4] = { 4, 0xdeadbeef, 0xdeadbeef, 0 };
161
    uint32_t readsize, out[4] = { 4, 0xdeadbeef, 0xdeadbeef, 0 };
211
    void* in;
162
    void *in;
-
 
163
    
-
 
164
    in = malloc(EMCORE_HEADER_SIZE + 0x0F00);
212
 
165
    
-
 
166
    while (size) {
-
 
167
        readsize = MIN(size, 0x0F00);
-
 
168
        
213
    out[1] = addr;
169
        out[1] = addr;
214
    out[2] = size;
170
        out[2] = readsize;
215
 
-
 
216
    if (size + EMCORE_HEADER_SIZE > emcore_usb_eps_mps.cin)
-
 
217
    {
171
        
218
        return EMCORE_ERROR_OVERFLOW;
172
        res = emcore_monitor_command(out, in, 16, readsize + EMCORE_HEADER_SIZE);
219
    }
173
        
220
 
-
 
221
    data_length = size + EMCORE_HEADER_SIZE;
174
        if (res != EMCORE_SUCCESS) {
222
    in = malloc(data_length);
175
            free(in);
223
 
-
 
224
    res = emcore_monitor_command(out, in, 16, data_length);
176
            return res;
-
 
177
        }
225
 
178
        
226
    if (EMCORE_SUCCESS != res)
179
        memcpy(data, in + EMCORE_HEADER_SIZE, readsize);
227
    {
180
        
228
        free(in);
181
        data += readsize;
229
 
-
 
-
 
182
        addr += readsize;
230
        return res;
183
        size -= readsize;
231
    }
184
    }
232
 
185
 
233
    memcpy(data, in + EMCORE_HEADER_SIZE, size);
-
 
234
 
-
 
235
    free(in);
186
    free(in);
236
 
187
    
237
    return EMCORE_SUCCESS;
188
    return EMCORE_SUCCESS;
238
}
189
}
239
 
190
 
240
int emcore_writemem(const void* data, uint32_t addr, uint32_t size)
191
int32_t emcore_write(const void *data, uint32_t addr, uint32_t size) {
241
{
-
 
242
    int res;
192
    int32_t res;
243
    uint32_t data_length, in[4], *out;
193
    uint32_t writesize, in[4], *out;
244
 
-
 
245
    if (size + EMCORE_HEADER_SIZE > emcore_usb_eps_mps.cout)
-
 
246
    {
-
 
247
        return EMCORE_ERROR_OVERFLOW;
-
 
248
    }
194
    
249
 
-
 
250
    data_length = size + EMCORE_HEADER_SIZE;
195
    out = calloc(sizeof(char), EMCORE_HEADER_SIZE + 0x0F00);
251
    out = malloc(data_length);
-
 
252
 
196
 
253
    *(out) = 5;
197
    *(out) = 5;
254
    *(out + 1) = addr;
-
 
255
    *(out + 2) = size;
-
 
256
    *(out + 3) = 0;
-
 
257
    memcpy(out + 4, data, size);
-
 
258
 
-
 
259
    res = emcore_monitor_command(out, in, data_length, 16);
-
 
260
 
-
 
261
    if (EMCORE_SUCCESS != res)
-
 
262
    {
198
    
263
        return res;
-
 
264
    }
-
 
265
 
-
 
266
    return EMCORE_SUCCESS;
-
 
267
}
-
 
268
 
-
 
269
int emcore_readdma(void* data, uint32_t addr, uint32_t size)
-
 
270
{
-
 
271
    int res;
-
 
272
    uint32_t out[4] = { 6, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
-
 
273
 
-
 
274
    out[1] = addr;
-
 
275
    out[2] = size;
199
    while (size) {
276
 
-
 
277
    if (size > emcore_usb_eps_mps.din)
200
        writesize = MIN(size, 0x0F00);
278
    {
201
        
279
        return EMCORE_ERROR_OVERFLOW;
202
        memcpy(out + 4, data, writesize);
280
    }
-
 
281
 
203
 
-
 
204
        *(out + 1) = addr;
-
 
205
        *(out + 2) = writesize;
-
 
206
        
282
    res = emcore_monitor_command(out, in, 16, 16);
207
        res = emcore_monitor_command(out, in, writesize + EMCORE_HEADER_SIZE, 16);
283
 
208
 
284
    if (EMCORE_SUCCESS != res)
209
        if (res != EMCORE_SUCCESS) {
-
 
210
            free(out);
-
 
211
            return res;
-
 
212
        }
285
    {
213
        
286
        return res;
214
        data += writesize;
-
 
215
        addr += writesize;
-
 
216
        size -= writesize;
287
    }
217
    }
288
 
218
    
289
    res = emcore_din(data, size);
219
    free(out);
290
 
220
 
291
    return EMCORE_SUCCESS;
221
    return EMCORE_SUCCESS;
292
}
222
}
293
 
223
 
294
int emcore_writedma(const void* data, uint32_t addr, uint32_t size)
224
int32_t emcore_readi2c(void *data, uint8_t bus, uint8_t slave, uint8_t addr, uint8_t size) {
295
{
-
 
296
    int res;
225
    int32_t res;
297
    uint32_t out[4] = { 7, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
226
    uint32_t data_length, out[4] = { 8, 0xdeadbeef, 0, 0 };
298
 
-
 
299
    out[1] = addr;
-
 
300
    out[2] = size;
227
    void *in;
301
 
-
 
302
    if (size > emcore_usb_eps_mps.dout)
-
 
303
    {
-
 
304
        return EMCORE_ERROR_OVERFLOW;
-
 
305
    }
-
 
306
 
-
 
307
    res = emcore_monitor_command(out, in, 16, 16);
-
 
308
 
228
 
309
    if (EMCORE_SUCCESS != res)
229
    if (!size) {
310
    {
-
 
311
        return res;
230
        return EMCORE_SUCCESS;
312
    }
231
    }
313
 
232
 
314
    res = emcore_dout(data, size);
-
 
315
 
-
 
316
    return EMCORE_SUCCESS;
-
 
317
}
-
 
318
 
-
 
319
int emcore_readi2c(void* data, uint8_t bus, uint8_t slave, uint8_t addr, uint8_t size)
-
 
320
{
-
 
321
    int res;
-
 
322
    uint32_t data_length, out[4] = { 8, 0xdeadbeef, 0, 0 };
-
 
323
    void* in;
-
 
324
 
-
 
325
    out[1] = bus | (slave << 8) | (addr << 16) | (size << 24);
233
    out[1] = bus | (slave << 8) | (addr << 16) | (size << 24);
326
 
234
 
327
    if (size + EMCORE_HEADER_SIZE > emcore_usb_eps_mps.cin)
-
 
328
    {
-
 
329
        return EMCORE_ERROR_OVERFLOW;
-
 
330
    }
-
 
331
 
-
 
332
    data_length = 16 + size;
235
    data_length = EMCORE_HEADER_SIZE + size;
333
    in = malloc(data_length);
236
    in = malloc(data_length);
334
 
237
 
335
    res = emcore_monitor_command(out, in, 16, data_length);
238
    res = emcore_monitor_command(out, in, 16, data_length);
336
 
239
 
337
    if (EMCORE_SUCCESS != res)
240
    if (res != EMCORE_SUCCESS) {
338
    {
-
 
339
        free(in);
241
        free(in);
340
 
242
 
341
        return res;
243
        return res;
342
    }
244
    }
343
 
245
 
344
    memcpy(data, in + 16, size);
246
    memcpy(data, in + EMCORE_HEADER_SIZE, size);
345
 
247
 
346
    free(in);
248
    free(in);
347
 
249
 
348
    return EMCORE_SUCCESS;
250
    return EMCORE_SUCCESS;
349
}
251
}
350
 
252
 
351
int emcore_writei2c(const void* data, uint8_t bus, uint8_t slave, uint8_t addr, uint8_t size)
253
int32_t emcore_writei2c(const void *data, uint8_t bus, uint8_t slave, uint8_t addr, uint8_t size) {
352
{
-
 
353
    int res;
254
    int32_t res;
354
    uint32_t data_length, in[4], *out;
255
    uint32_t data_length, in[4], *out;
355
 
256
 
356
    if (size + EMCORE_HEADER_SIZE > emcore_usb_eps_mps.cout)
-
 
357
    {
257
    if (!size) {
358
        return EMCORE_ERROR_OVERFLOW;
258
        return EMCORE_SUCCESS;
359
    }
259
    }
360
 
260
 
-
 
261
    if (size > 48) {
-
 
262
        return EMCORE_ERROR_OVERFLOW;
-
 
263
    }
-
 
264
    
361
    data_length = 16 + size;
265
    data_length = size + EMCORE_HEADER_SIZE;
362
    out = calloc(data_length, 1);
266
    out = calloc(data_length, 1);
363
 
267
 
364
    *(out) = 9; // bytes 0-3
-
 
365
    *(out + 4) = bus;
268
    *(out) = 9;
366
    *(out + 5) = slave;
269
    out[1] = bus | (slave << 8) | (addr << 16) | (size << 24);
367
    *(out + 6) = addr;
-
 
368
    *(out + 7) = size;
-
 
369
 
270
 
370
    memcpy(out + 16, data, size); // bytes 16+
271
    memcpy(out + 4, data, size);
371
 
272
 
372
    res = emcore_monitor_command(out, in, data_length, 16);
273
    res = emcore_monitor_command(out, in, data_length, 16);
373
 
274
 
374
    if (EMCORE_SUCCESS != res)
275
    if (res != EMCORE_SUCCESS) {
375
    {
-
 
376
        return res;
276
        return res;
377
    }
277
    }
378
 
278
 
379
    return EMCORE_SUCCESS;
279
    return EMCORE_SUCCESS;
380
}
280
}
381
 
281
 
382
int emcore_file_open(uint32_t* handle, const char* path, int flags)
282
int32_t emcore_file_open(uint32_t *handle, const char *path, uint32_t flags) {
383
{
-
 
384
    int res;
283
    int32_t res;
385
    uint32_t str_length, data_length, in[4], *out;
284
    uint32_t str_length, data_length, in[4], *out;
386
    int flags_emcore = 0;
285
    int32_t flags_emcore = 0;
387
    
286
    
388
    *handle = 0;
287
    *handle = 0;
389
    
288
    
390
    str_length = strlen(path);
-
 
391
    data_length = str_length + 1 + EMCORE_HEADER_SIZE;
-
 
392
    
-
 
393
    if (data_length > emcore_usb_eps_mps.cout)
-
 
394
    {
-
 
395
        return EMCORE_ERROR_OVERFLOW;
-
 
396
    }
-
 
397
 
-
 
398
    out = calloc(sizeof(char), data_length);
-
 
399
 
-
 
400
    *(out) = 30;
-
 
401
    
-
 
402
    /*
289
    /*
403
    #define O_RDONLY 0
290
    #define O_RDONLY 0
404
    #define O_WRONLY 1
291
    #define O_WRONLY 1
405
    #define O_RDWR   2
292
    #define O_RDWR   2
406
    #define O_CREAT  4
293
    #define O_CREAT  4
Line 426... Line 313...
426
    
313
    
427
    if (flags & O_TRUNC) {
314
    if (flags & O_TRUNC) {
428
        flags_emcore |= 0x10;
315
        flags_emcore |= 0x10;
429
    }
316
    }
430
    
317
    
431
    *(out + 1) = flags_emcore;
318
    str_length = strlen(path) + 1;
432
    
319
    
-
 
320
    if (str_length > 48) {
-
 
321
        uint32_t buf = 0;
-
 
322
        
-
 
323
        res = emcore_malloc(&buf, str_length);
-
 
324
        
-
 
325
        if (res != EMCORE_SUCCESS) {
-
 
326
            return res;
-
 
327
        }
-
 
328
        
433
    strncpy(((char*)(out + 4)), path, str_length);
329
        res = emcore_write(path, buf, str_length);
-
 
330
        
-
 
331
        if (res != EMCORE_SUCCESS) {
-
 
332
            return res;
-
 
333
        }
-
 
334
        
-
 
335
        out = calloc(sizeof(char), EMCORE_HEADER_SIZE);
434
 
336
 
-
 
337
        *(out) = 30;
-
 
338
        *(out + 1) = flags_emcore;
-
 
339
        *(out + 3) = buf;
-
 
340
        
-
 
341
        res = emcore_monitor_command(out, in, EMCORE_HEADER_SIZE, 16);
-
 
342
    }
-
 
343
    else {
-
 
344
        data_length = EMCORE_HEADER_SIZE + str_length;
-
 
345
        
-
 
346
        out = calloc(sizeof(char), data_length);
-
 
347
 
-
 
348
        *(out) = 30;
-
 
349
        *(out + 1) = flags_emcore;
-
 
350
        
-
 
351
        strncpy(((char *)(out + 4)), path, str_length - 1);
-
 
352
        
435
    res = emcore_monitor_command(out, in, data_length, 16);
353
        res = emcore_monitor_command(out, in, data_length, 16);
-
 
354
    }
436
 
355
 
437
    if (EMCORE_SUCCESS != res)
356
    if (res != EMCORE_SUCCESS) {
438
    {
-
 
439
        return res;
357
        return res;
440
    }
358
    }
441
    
359
    
442
    if (in[1] > 0x80000000)
360
    if (in[1] > 0x80000000) {
443
    {
-
 
444
        return EMCORE_ERROR_IO;
361
        return EMCORE_ERROR_IO;
445
    }
362
    }
446
    
363
    
447
    *handle = in[1];
364
    *handle = in[1];
448
 
365
 
449
    return EMCORE_SUCCESS;
366
    return EMCORE_SUCCESS;
450
}
367
}
451
 
368
 
452
int emcore_file_size(uint32_t* size, uint32_t handle)
369
int32_t emcore_file_size(uint32_t *size, uint32_t handle) {
453
{
-
 
454
    int res;
370
    int32_t res;
455
    uint32_t out[4] = { 31, 0xdeadbeef, 0, 0 }, in[4];
371
    uint32_t out[4] = { 31, 0xdeadbeef, 0, 0 }, in[4];
456
    
372
    
457
    out[1] = handle;
373
    out[1] = handle;
458
 
374
 
459
    res = emcore_monitor_command(out, in, 16, 16);
375
    res = emcore_monitor_command(out, in, 16, 16);
460
 
376
 
461
    if (EMCORE_SUCCESS != res)
377
    if (res != EMCORE_SUCCESS) {
462
    {
-
 
463
        return res;
378
        return res;
464
    }
379
    }
465
    
380
    
466
    if (in[1] > 0x80000000)
381
    if (in[1] > 0x80000000) {
467
    {
-
 
468
        return EMCORE_ERROR_IO;
382
        return EMCORE_ERROR_IO;
469
    }
383
    }
470
    
384
    
471
    *size = in[1];
385
    *size = in[1];
472
    
386
    
473
    return EMCORE_SUCCESS;
387
    return EMCORE_SUCCESS;
474
}
388
}
475
 
389
 
476
int emcore_file_read(uint32_t* nread, uint32_t handle, uint32_t addr, uint32_t size)
390
int32_t emcore_file_read(uint32_t *nread, uint32_t handle, uint32_t addr, uint32_t size) {
477
{
-
 
478
    int res;
391
    int32_t res;
479
    uint32_t out[4] = { 32, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef }, in[4];
392
    uint32_t out[4] = { 32, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef }, in[4];
480
    
393
    
481
    out[1] = handle;
394
    out[1] = handle;
482
    out[2] = addr;
395
    out[2] = addr;
483
    out[3] = size;
396
    out[3] = size;
484
 
397
 
485
    res = emcore_monitor_command(out, in, 16, 16);
398
    res = emcore_monitor_command(out, in, 16, 16);
486
 
399
 
487
    if (EMCORE_SUCCESS != res)
400
    if (res != EMCORE_SUCCESS) {
488
    {
-
 
489
        return res;
401
        return res;
490
    }
402
    }
491
    
403
    
492
    if (in[1] > 0x80000000)
404
    if (in[1] > 0x80000000) {
493
    {
-
 
494
        return EMCORE_ERROR_IO;
405
        return EMCORE_ERROR_IO;
495
    }
406
    }
496
    
407
    
497
    *nread = in[1];
408
    *nread = in[1];
498
    
409
    
499
    return EMCORE_SUCCESS;
410
    return EMCORE_SUCCESS;
500
}
411
}
501
 
412
 
502
int emcore_file_write(uint32_t* nwrite, uint32_t handle, uint32_t addr, uint32_t size)
413
int32_t emcore_file_write(uint32_t *nwrite, uint32_t handle, uint32_t addr, uint32_t size) {
503
{
-
 
504
    int res;
414
    int32_t res;
505
    uint32_t out[4] = { 33, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef }, in[4];
415
    uint32_t out[4] = { 33, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef }, in[4];
506
    
416
    
507
    out[1] = handle;
417
    out[1] = handle;
508
    out[2] = addr;
418
    out[2] = addr;
509
    out[3] = size;
419
    out[3] = size;
510
 
420
 
511
    res = emcore_monitor_command(out, in, 16, 16);
421
    res = emcore_monitor_command(out, in, 16, 16);
512
 
422
 
513
    if (EMCORE_SUCCESS != res)
423
    if (res != EMCORE_SUCCESS) {
514
    {
-
 
515
        return res;
424
        return res;
516
    }
425
    }
517
    
426
    
518
    if (in[1] > 0x80000000)
427
    if (in[1] > 0x80000000) {
519
    {
-
 
520
        return EMCORE_ERROR_IO;
428
        return EMCORE_ERROR_IO;
521
    }
429
    }
522
    
430
    
523
    *nwrite = in[1];
431
    *nwrite = in[1];
524
    
432
    
525
    return EMCORE_SUCCESS;
433
    return EMCORE_SUCCESS;
526
}
434
}
527
 
435
 
528
int emcore_file_seek(uint32_t handle, uint32_t offset, uint32_t whence)
436
int32_t emcore_file_seek(uint32_t handle, uint32_t offset, uint32_t whence) {
529
{
-
 
530
    int res;
437
    int32_t res;
531
    uint32_t out[4] = { 34, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef }, in[4];
438
    uint32_t out[4] = { 34, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef }, in[4];
532
    
439
    
533
    out[1] = handle;
440
    out[1] = handle;
534
    out[2] = offset;
441
    out[2] = offset;
535
    out[3] = whence;
442
    out[3] = whence;
536
 
443
 
537
    res = emcore_monitor_command(out, in, 16, 16);
444
    res = emcore_monitor_command(out, in, 16, 16);
538
 
445
 
539
    if (EMCORE_SUCCESS != res)
446
    if (res != EMCORE_SUCCESS) {
540
    {
-
 
541
        return res;
447
        return res;
542
    }
448
    }
543
    
449
    
544
    if (in[1] > 0x80000000)
450
    if (in[1] > 0x80000000) {
545
    {
-
 
546
        return EMCORE_ERROR_IO;
451
        return EMCORE_ERROR_IO;
547
    }
452
    }
548
    
453
    
549
    return EMCORE_SUCCESS;
454
    return EMCORE_SUCCESS;
550
}
455
}
551
 
456
 
552
int emcore_file_truncate(uint32_t handle, uint32_t length)
457
int32_t emcore_file_truncate(uint32_t handle, uint32_t length) {
553
{
-
 
554
    int res;
458
    int32_t res;
555
    uint32_t out[4] = { 35, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
459
    uint32_t out[4] = { 35, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
556
    
460
    
557
    out[1] = handle;
461
    out[1] = handle;
558
    out[2] = length;
462
    out[2] = length;
559
 
463
 
560
    res = emcore_monitor_command(out, in, 16, 16);
464
    res = emcore_monitor_command(out, in, 16, 16);
561
 
465
 
562
    if (EMCORE_SUCCESS != res)
466
    if (res != EMCORE_SUCCESS) {
563
    {
-
 
564
        return res;
467
        return res;
565
    }
468
    }
566
    
469
    
567
    if (in[1] > 0x80000000)
470
    if (in[1] > 0x80000000) {
568
    {
-
 
569
        return EMCORE_ERROR_IO;
471
        return EMCORE_ERROR_IO;
570
    }
472
    }
571
    
473
    
572
    return EMCORE_SUCCESS;
474
    return EMCORE_SUCCESS;
573
}
475
}
574
 
476
 
575
int emcore_file_sync(uint32_t handle)
477
int32_t emcore_file_sync(uint32_t handle) {
576
{
-
 
577
    int res;
478
    int32_t res;
578
    uint32_t out[4] = { 36, 0xdeadbeef, 0, 0 }, in[4];
479
    uint32_t out[4] = { 36, 0xdeadbeef, 0, 0 }, in[4];
579
 
480
 
580
    out[1] = handle;
481
    out[1] = handle;
581
 
482
 
582
    res = emcore_monitor_command(out, in, 16, 16);
483
    res = emcore_monitor_command(out, in, 16, 16);
583
 
484
 
584
    if (EMCORE_SUCCESS != res)
485
    if (res != EMCORE_SUCCESS) {
585
    {
-
 
586
        return res;
486
        return res;
587
    }
487
    }
588
    
488
    
589
    if (in[1] > 0x80000000)
489
    if (in[1] > 0x80000000) {
590
    {
-
 
591
        return EMCORE_ERROR_IO;
490
        return EMCORE_ERROR_IO;
592
    }
491
    }
593
    
492
    
594
    return EMCORE_SUCCESS;
493
    return EMCORE_SUCCESS;
595
}
494
}
596
 
495
 
597
int emcore_file_close(uint32_t handle)
496
int32_t emcore_file_close(uint32_t handle) {
598
{
-
 
599
    int res;
497
    int32_t res;
600
    uint32_t out[4] = { 37, 0xdeadbeef, 0, 0 }, in[4];
498
    uint32_t out[4] = { 37, 0xdeadbeef, 0, 0 }, in[4];
601
 
499
 
602
    out[1] = handle;
500
    out[1] = handle;
603
 
501
 
604
    res = emcore_monitor_command(out, in, 16, 16);
502
    res = emcore_monitor_command(out, in, 16, 16);
605
 
503
 
606
    if (EMCORE_SUCCESS != res)
504
    if (res != EMCORE_SUCCESS) {
607
    {
-
 
608
        return res;
505
        return res;
609
    }
506
    }
610
    
507
    
611
    if (in[1] > 0x80000000)
508
    if (in[1] > 0x80000000) {
612
    {
-
 
613
        return EMCORE_ERROR_IO;
509
        return EMCORE_ERROR_IO;
614
    }
510
    }
615
    
511
    
616
    return EMCORE_SUCCESS;
512
    return EMCORE_SUCCESS;
617
}
513
}
618
 
514
 
619
int emcore_file_close_all(uint32_t* count)
515
int32_t emcore_file_close_all(uint32_t *count) {
620
{
-
 
621
    int res;
516
    int32_t res;
622
    uint32_t out[4] = { 38, 0, 0, 0 }, in[4];
517
    uint32_t out[4] = { 38, 0, 0, 0 }, in[4];
623
 
518
 
624
    res = emcore_monitor_command(out, in, 16, 16);
519
    res = emcore_monitor_command(out, in, 16, 16);
625
 
520
 
626
    if (EMCORE_SUCCESS != res)
521
    if (res != EMCORE_SUCCESS) {
627
    {
-
 
628
        return res;
522
        return res;
629
    }
523
    }
630
 
524
 
631
    if (in[1] > 0x80000000)
525
    if (in[1] > 0x80000000) {
632
    {
-
 
633
        return EMCORE_ERROR_IO;
526
        return EMCORE_ERROR_IO;
634
    }
527
    }
635
    
528
    
636
    *count = in[1];
529
    *count = in[1];
637
 
530
 
638
    return EMCORE_SUCCESS;
531
    return EMCORE_SUCCESS;
639
}
532
}
640
 
533
 
641
int emcore_file_kill_all(uint32_t volume)
534
int32_t emcore_file_kill_all(uint32_t volume) {
642
{
-
 
643
    int res;
535
    int32_t res;
644
    uint32_t out[4] = { 39, 0xdeadbeef, 0, 0 }, in[4];
536
    uint32_t out[4] = { 39, 0xdeadbeef, 0, 0 }, in[4];
645
 
537
 
646
    out[1] = volume;
538
    out[1] = volume;
647
 
539
 
648
    res = emcore_monitor_command(out, in, 16, 16);
540
    res = emcore_monitor_command(out, in, 16, 16);
649
 
541
 
650
    if (EMCORE_SUCCESS != res)
542
    if (res != EMCORE_SUCCESS) {
651
    {
-
 
652
        return res;
543
        return res;
653
    }
544
    }
654
    
545
    
655
    if (in[1] > 0x80000000)
546
    if (in[1] > 0x80000000) {
656
    {
-
 
657
        return EMCORE_ERROR_IO;
547
        return EMCORE_ERROR_IO;
658
    }
548
    }
659
    
549
    
660
    return EMCORE_SUCCESS;
550
    return EMCORE_SUCCESS;
661
}
551
}
662
 
552
 
663
int emcore_file_unlink(const char* name)
553
int32_t emcore_file_unlink(const char *path) {
664
{
-
 
665
    int res;
554
    int32_t res;
666
    uint32_t str_length, data_length, in[4], *out;
555
    uint32_t str_length, data_length, in[4], *out;
667
    
556
    
668
    str_length = strlen(name);
557
    str_length = strlen(path) + 1;
669
    data_length = str_length + 1 + EMCORE_HEADER_SIZE;
-
 
670
    
558
    
671
    if (data_length > emcore_usb_eps_mps.cout)
559
    if (str_length > 48) {
-
 
560
        uint32_t buf;
-
 
561
        
-
 
562
        res = emcore_malloc(&buf, str_length);
-
 
563
        
-
 
564
        if (res != EMCORE_SUCCESS) {
-
 
565
            return res;
-
 
566
        }
-
 
567
        
-
 
568
        res = emcore_write(path, buf, str_length);
672
    {
569
        
673
        return EMCORE_ERROR_OVERFLOW;
570
        if (res != EMCORE_SUCCESS) {
-
 
571
            return res;
674
    }
572
        }
675
 
573
        
676
    out = calloc(sizeof(char), data_length);
574
        out = calloc(sizeof(char), EMCORE_HEADER_SIZE);
677
 
575
 
678
    *(out) = 40;
576
        *(out) = 40;
-
 
577
        *(out + 3) = buf;
-
 
578
        
-
 
579
        res = emcore_monitor_command(out, in, EMCORE_HEADER_SIZE, 16);
679
    
580
    }
-
 
581
    else {
-
 
582
        data_length = EMCORE_HEADER_SIZE + str_length;
-
 
583
        
680
    strncpy(((char*)(out + 4)), name, str_length);
584
        out = calloc(sizeof(char), data_length);
681
 
585
 
-
 
586
        *(out) = 40;
-
 
587
        
-
 
588
        strncpy(((char *)(out + 4)), path, str_length - 1);
-
 
589
        
682
    res = emcore_monitor_command(out, in, data_length, 16);
590
        res = emcore_monitor_command(out, in, data_length, 16);
-
 
591
    }
683
 
592
 
684
    if (EMCORE_SUCCESS != res)
593
    if (res != EMCORE_SUCCESS) {
685
    {
-
 
686
        return res;
594
        return res;
687
    }
595
    }
688
    
596
    
689
    if (in[1] > 0x80000000)
597
    if (in[1] > 0x80000000) {
690
    {
-
 
691
        return EMCORE_ERROR_IO;
598
        return EMCORE_ERROR_IO;
692
    }
599
    }
693
    
600
    
694
    return EMCORE_SUCCESS;
601
    return EMCORE_SUCCESS;
695
}
602
}
696
 
603
 
697
int emcore_file_rename(const char* path, const char* newpath)
604
int32_t emcore_file_rename(const char *path, const char *newpath) {
698
{
-
 
699
    int res;
605
    // TODO: currently, calling file_rename crashes the emCORE kernel
700
    uint32_t str_length, data_length, in[4], *out;
606
    return EMCORE_ERROR_NOT_IMPLEMENTED;
701
    
607
    
-
 
608
    int32_t res;
-
 
609
    uint32_t in[4], out[4] = { 41, 0, 0xdeadbeef, 0xdeadbeef }, buf;
-
 
610
    void *data;
-
 
611
    size_t obytes, nbytes;
-
 
612
    
702
    if (strlen(path) > 247)
613
    obytes = strlen(path) + 1;
-
 
614
    nbytes = strlen(newpath) + 1;
-
 
615
    
-
 
616
    res = emcore_malloc(&buf, obytes + nbytes);
703
    {
617
    
704
        return EMCORE_ERROR_OVERFLOW;
618
    if (res != EMCORE_SUCCESS) {
-
 
619
        return res;
705
    }
620
    }
706
    
621
    
707
    str_length = MIN(247, strlen(newpath));
622
    data = calloc(sizeof(char), obytes + nbytes);
708
    data_length = str_length + 1 + 248 + EMCORE_HEADER_SIZE;
-
 
709
    
623
    
-
 
624
    strcpy(data, path);
-
 
625
    strcpy(data + obytes, newpath);
-
 
626
    
710
    if (data_length > emcore_usb_eps_mps.cout)
627
    res = emcore_write(data, buf, obytes + nbytes);
711
    {
628
    
-
 
629
    free(data);
-
 
630
    
712
        return EMCORE_ERROR_OVERFLOW;
631
    if (res != EMCORE_SUCCESS) {
-
 
632
        return res;
713
    }
633
    }
714
 
634
 
715
    out = calloc(sizeof(char), data_length);
635
    out[2] = buf;
716
 
-
 
717
    *(out) = 41;
636
    out[3] = buf + obytes;
718
    
637
    
719
    strncpy(((char*)(out + 4)), path, strlen(path));
-
 
720
    strncpy(((char*)(out) + 248), newpath, str_length);
638
    res = emcore_monitor_command(out, in, EMCORE_HEADER_SIZE, 16);
721
 
639
 
-
 
640
    if (res != EMCORE_SUCCESS) {
-
 
641
        return res;
-
 
642
    }
-
 
643
    
722
    res = emcore_monitor_command(out, in, data_length, 16);
644
    res = emcore_free(buf);
723
 
645
 
724
    if (EMCORE_SUCCESS != res)
646
    if (res != EMCORE_SUCCESS) {
725
    {
-
 
726
        return res;
647
        return res;
727
    }
648
    }
728
    
649
    
729
    if (in[1] > 0x80000000)
650
    if (in[1] > 0x80000000) {
730
    {
-
 
731
        return EMCORE_ERROR_IO;
651
        return EMCORE_ERROR_IO;
732
    }
652
    }
733
    
653
    
734
    return EMCORE_SUCCESS;
654
    return EMCORE_SUCCESS;
735
}
655
}
736
 
656
 
737
int emcore_dir_open(uint32_t* handle, const char* name)
657
int32_t emcore_dir_open(uint32_t *handle, const char *name) {
738
{
-
 
739
    int res;
658
    int32_t res;
740
    size_t name_length;
-
 
741
    uint32_t data_length, in[4], *out;
659
    uint32_t str_length, data_length, in[4], *out;
742
 
660
    
743
    name_length = strlen(name);
661
    str_length = strlen(name) + 1;
-
 
662
    
-
 
663
    if (str_length > 48) {
-
 
664
        uint32_t buf = 0;
-
 
665
        
-
 
666
        res = emcore_malloc(&buf, str_length);
-
 
667
        
-
 
668
        if (res != EMCORE_SUCCESS) {
-
 
669
            return res;
-
 
670
        }
-
 
671
        
-
 
672
        res = emcore_write(name, buf, str_length);
-
 
673
        
744
    data_length = name_length + 1 + EMCORE_HEADER_SIZE;
674
        if (res != EMCORE_SUCCESS) {
-
 
675
            return res;
-
 
676
        }
-
 
677
        
745
    out = calloc(data_length, 1);
678
        out = calloc(sizeof(char), EMCORE_HEADER_SIZE);
746
 
679
 
747
    *out = 42;
680
        *(out) = 42;
-
 
681
        *(out + 3) = buf;
-
 
682
        
-
 
683
        res = emcore_monitor_command(out, in, EMCORE_HEADER_SIZE, 16);
-
 
684
    }
-
 
685
    else {
-
 
686
        data_length = EMCORE_HEADER_SIZE + str_length;
-
 
687
        
748
    strncpy(((char*)(out + 4)), name, name_length + 1);
688
        out = calloc(sizeof(char), data_length);
749
 
689
 
-
 
690
        *(out) = 42;
-
 
691
        
-
 
692
        strncpy(((char *)(out + 4)), name, str_length - 1);
-
 
693
        
750
    res = emcore_monitor_command(out, in, data_length, 16);
694
        res = emcore_monitor_command(out, in, data_length, 16);
-
 
695
    }
751
 
696
 
752
    if (EMCORE_SUCCESS != res)
697
    if (res != EMCORE_SUCCESS) {
753
    {
-
 
754
        return res;
698
        return res;
755
    }
699
    }
-
 
700
    
-
 
701
    if (in[1] > 0x80000000) {
-
 
702
        return EMCORE_ERROR_IO;
-
 
703
    }
756
 
704
    
757
    *handle = in[1];
705
    *handle = in[1];
758
 
706
 
759
    return EMCORE_SUCCESS;
707
    return EMCORE_SUCCESS;
760
}
708
}
761
 
709
 
762
int emcore_dir_read(struct emcore_dir_entry* entry, uint32_t handle)
710
int32_t emcore_dir_read(struct emcore_dir_entry *entry, uint32_t handle) {
763
{
-
 
764
    int res;
711
    int32_t res;
765
    uint32_t maxpath, ptr, dirent_size, emcore_errno_value, filename_buf_len,
712
    uint32_t maxpath, ptr, dirent_size, emcore_errno_value, filename_buf_len,
766
        out[4] = { 43, 0xdeadbeef, 0, 0 }, in[4];
713
        out[4] = { 43, 0xdeadbeef, 0, 0 }, in[4];
767
    void *buf;
714
    void *buf;
768
 
715
 
769
    memset(entry, 0, sizeof(*entry));
716
    memset(entry, 0, sizeof(*entry));
770
 
717
 
771
    out[1] = handle;
718
    out[1] = handle;
772
 
719
 
773
    res = emcore_monitor_command(out, in, 16, 16);
720
    res = emcore_monitor_command(out, in, 16, 16);
774
 
721
 
775
    if (EMCORE_SUCCESS != res)
722
    if (res != EMCORE_SUCCESS) {
776
    {
-
 
777
        return res;
723
        return res;
778
    }
724
    }
779
 
725
 
780
    ptr = in[3];
726
    ptr = in[3];
781
 
727
 
782
    if (0 == ptr)
728
    if (!ptr) {
783
    {
-
 
784
        res = emcore_errno(&emcore_errno_value);
729
        res = emcore_errno(&emcore_errno_value);
785
 
730
 
786
        if (EMCORE_SUCCESS != res)
731
        if (res != EMCORE_SUCCESS) {
787
        {
-
 
788
            return res;
732
            return res;
789
        }
733
        }
790
        
734
        
791
        if (EMCORE_SUCCESS != emcore_errno_value && 2 != emcore_errno_value)
735
        if (emcore_errno_value != EMCORE_SUCCESS && emcore_errno_value != 2) {
792
        {
-
 
793
            return EMCORE_ERROR_IO;
736
            return EMCORE_ERROR_IO;
794
        }
737
        }
795
 
738
 
796
        return EMCORE_ERROR_NO_MORE_ENTRIES;
739
        return EMCORE_ERROR_NO_MORE_ENTRIES;
797
    }
740
    }
798
 
741
 
799
    if (1 != in[1]) // version
742
    if (in[1] != 1) { // version
800
    {
-
 
801
        return EMCORE_ERROR_NOT_IMPLEMENTED;
743
        return EMCORE_ERROR_NOT_IMPLEMENTED;
802
    }
744
    }
803
 
745
 
804
    maxpath = in[2];
746
    maxpath = in[2];
805
 
747
 
Line 807... Line 749...
807
 
749
 
808
    buf = malloc(dirent_size);
750
    buf = malloc(dirent_size);
809
 
751
 
810
    res = emcore_read(buf, ptr, dirent_size);
752
    res = emcore_read(buf, ptr, dirent_size);
811
 
753
 
812
    if (EMCORE_SUCCESS != res)
754
    if (res != EMCORE_SUCCESS) {
813
    {
755
        free(buf);
814
        return res;
756
        return res;
815
    }
757
    }
816
 
758
 
817
    filename_buf_len = strlen((char*)buf) + 1;
759
    filename_buf_len = strlen((char *)buf) + 1;
818
 
760
 
819
    entry->name = malloc(filename_buf_len);
761
    entry->name = malloc(filename_buf_len);
820
    strncpy(entry->name, buf, filename_buf_len);
762
    strncpy(entry->name, buf, filename_buf_len);
821
    memcpy(&entry->attributes, buf + maxpath, 16);
763
    memcpy(&entry->attributes, buf + maxpath, 16);
-
 
764
    
-
 
765
    free(buf);
822
 
766
 
823
#ifdef DEBUG_DIR_ENTRIES
767
#ifdef DEBUG_DIR_ENTRIES
824
    fprintf(stderr, "Read directory entry: %s\n", entry->name);
768
    fprintf(stderr, "Read directory entry: %s\n", entry->name);
825
    fprintf(stderr, "Attributes: 0x%08x\n", entry->attributes);
769
    fprintf(stderr, "Attributes: 0x%08x\n", entry->attributes);
826
    fprintf(stderr, "Size: %d\n", entry->size);
770
    fprintf(stderr, "Size: %d\n", entry->size);
827
    fprintf(stderr, "Start cluster: %d\n", entry->startcluster);
771
    fprintf(stderr, "Start cluster: %d\n", entry->startcluster);
828
    fprintf(stderr, "Last written date: 0x%04x\n", entry->wrtdate);
772
    fprintf(stderr, "Last written date: 0x%04x\n", entry->wrtdate);
829
    fprintf(stderr, "Last written time: 0x%04x\n", entry->wrttime);
773
    fprintf(stderr, "Last written time: 0x%04x\n", entry->wrttime);
830
    fprintf(stderr, "Last written TS: %lu\n",
774
    fprintf(stderr, "Last written TS: %d\n",
831
        (unsigned long) fat_time_to_unix_ts(entry->wrttime, entry->wrtdate));
775
        fat_time_to_unix_ts(entry->wrttime, entry->wrtdate));
832
#endif
776
#endif
833
    return EMCORE_SUCCESS;
777
    return EMCORE_SUCCESS;
834
}
778
}
835
 
779
 
836
int emcore_dir_close(uint32_t handle)
780
int32_t emcore_dir_close(uint32_t handle) {
837
{
-
 
838
    int res;
781
    int32_t res;
839
    uint32_t out[4] = { 44, 0xdeadbeef, 0, 0 }, in[4];
782
    uint32_t out[4] = { 44, 0xdeadbeef, 0, 0 }, in[4];
840
 
783
 
841
    out[1] = handle;
784
    out[1] = handle;
842
 
785
 
843
    res = emcore_monitor_command(out, in, 16, 16);
786
    res = emcore_monitor_command(out, in, 16, 16);
844
 
787
 
845
    if (EMCORE_SUCCESS != res)
788
    if (res != EMCORE_SUCCESS) {
846
    {
-
 
847
        return res;
789
        return res;
848
    }
790
    }
849
    
791
    
850
    if (in[1] > 0x80000000)
792
    if (in[1] > 0x80000000) {
851
    {
-
 
852
        return EMCORE_ERROR_IO;
793
        return EMCORE_ERROR_IO;
853
    }
794
    }
854
    
795
    
855
    return EMCORE_SUCCESS;
796
    return EMCORE_SUCCESS;
856
}
797
}
857
 
798
 
858
int emcore_dir_close_all(uint32_t* count)
799
int32_t emcore_dir_close_all(uint32_t *count) {
859
{
-
 
860
    int res;
800
    int32_t res;
861
    uint32_t out[4] = { 45, 0, 0, 0 }, in[4];
801
    uint32_t out[4] = { 45, 0, 0, 0 }, in[4];
862
 
802
 
863
    res = emcore_monitor_command(out, in, 16, 16);
803
    res = emcore_monitor_command(out, in, 16, 16);
864
 
804
 
865
    if (EMCORE_SUCCESS != res)
805
    if (res != EMCORE_SUCCESS) {
866
    {
-
 
867
        return res;
806
        return res;
868
    }
807
    }
869
    
808
    
870
    if (in[1] > 0x80000000)
809
    if (in[1] > 0x80000000) {
871
    {
-
 
872
        return EMCORE_ERROR_IO;
810
        return EMCORE_ERROR_IO;
873
    }
811
    }
874
    
812
    
875
    *count = in[1];
813
    *count = in[1];
876
    
814
    
877
    return EMCORE_SUCCESS;
815
    return EMCORE_SUCCESS;
878
}
816
}
879
 
817
 
880
int emcore_dir_create(const char* name)
818
int32_t emcore_dir_create(const char *name) {
881
{
-
 
882
    int res;
819
    int32_t res;
883
    uint32_t str_length, data_length, in[4], *out;
820
    uint32_t str_length, data_length, in[4], *out;
884
    
821
    
885
    str_length = strlen(name);
822
    str_length = strlen(name) + 1;
886
    data_length = str_length + 1 + EMCORE_HEADER_SIZE;
-
 
887
    
823
    
888
    if (data_length > emcore_usb_eps_mps.cout)
824
    if (str_length > 48) {
-
 
825
        uint32_t buf = 0;
-
 
826
        
-
 
827
        res = emcore_malloc(&buf, str_length);
-
 
828
        
-
 
829
        if (res != EMCORE_SUCCESS) {
-
 
830
            return res;
-
 
831
        }
-
 
832
        
-
 
833
        res = emcore_write(name, buf, str_length);
889
    {
834
        
890
        return EMCORE_ERROR_OVERFLOW;
835
        if (res != EMCORE_SUCCESS) {
-
 
836
            return res;
891
    }
837
        }
892
 
838
        
893
    out = calloc(sizeof(char), data_length);
839
        out = calloc(sizeof(char), EMCORE_HEADER_SIZE);
894
 
840
 
895
    *(out) = 47;
841
        *(out) = 47;
-
 
842
        *(out + 3) = buf;
-
 
843
        
-
 
844
        res = emcore_monitor_command(out, in, EMCORE_HEADER_SIZE, 16);
896
    
845
    }
-
 
846
    else {
-
 
847
        data_length = EMCORE_HEADER_SIZE + str_length;
-
 
848
        
897
    strncpy(((char*)(out + 4)), name, str_length);
849
        out = calloc(sizeof(char), data_length);
898
 
850
 
-
 
851
        *(out) = 47;
-
 
852
        
-
 
853
        strncpy(((char *)(out + 4)), name, str_length - 1);
-
 
854
        
899
    res = emcore_monitor_command(out, in, data_length, 16);
855
        res = emcore_monitor_command(out, in, data_length, 16);
-
 
856
    }
900
 
857
 
901
    if (EMCORE_SUCCESS != res)
858
    if (res != EMCORE_SUCCESS) {
902
    {
-
 
903
        return res;
859
        return res;
904
    }
860
    }
905
    
861
    
906
    if (in[1] > 0x80000000)
862
    if (in[1] > 0x80000000) {
907
    {
-
 
908
        return EMCORE_ERROR_IO;
863
        return EMCORE_ERROR_IO;
909
    }
864
    }
910
    
865
    
911
    return EMCORE_SUCCESS;
866
    return EMCORE_SUCCESS;
912
}
867
}
913
 
868
 
914
int emcore_dir_remove(const char* name)
869
int32_t emcore_dir_remove(const char *name) {
915
{
-
 
916
    int res;
870
    int32_t res;
917
    uint32_t str_length, data_length, in[4], *out;
871
    uint32_t str_length, data_length, in[4], *out;
918
    
872
    
919
    str_length = strlen(name);
873
    str_length = strlen(name) + 1;
920
    data_length = str_length + 1 + EMCORE_HEADER_SIZE;
-
 
921
    
874
    
922
    if (data_length > emcore_usb_eps_mps.cout)
875
    if (str_length > 48) {
-
 
876
        uint32_t buf = 0;
-
 
877
        
-
 
878
        res = emcore_malloc(&buf, str_length);
-
 
879
        
-
 
880
        if (res != EMCORE_SUCCESS) {
-
 
881
            return res;
-
 
882
        }
-
 
883
        
-
 
884
        res = emcore_write(name, buf, str_length);
923
    {
885
        
924
        return EMCORE_ERROR_OVERFLOW;
886
        if (res != EMCORE_SUCCESS) {
-
 
887
            return res;
925
    }
888
        }
926
 
889
        
927
    out = calloc(sizeof(char), data_length);
890
        out = calloc(sizeof(char), EMCORE_HEADER_SIZE);
928
 
891
 
929
    *(out) = 48;
892
        *(out) = 48;
-
 
893
        *(out + 3) = buf;
-
 
894
        
-
 
895
        res = emcore_monitor_command(out, in, EMCORE_HEADER_SIZE, 16);
930
    
896
    }
-
 
897
    else {
-
 
898
        data_length = EMCORE_HEADER_SIZE + str_length;
-
 
899
        
931
    strncpy(((char*)(out + 4)), name, str_length);
900
        out = calloc(sizeof(char), data_length);
932
 
901
 
-
 
902
        *(out) = 48;
-
 
903
        
-
 
904
        strncpy(((char *)(out + 4)), name, str_length - 1);
-
 
905
        
933
    res = emcore_monitor_command(out, in, data_length, 16);
906
        res = emcore_monitor_command(out, in, data_length, 16);
-
 
907
    }
934
 
908
 
935
    if (EMCORE_SUCCESS != res)
909
    if (res != EMCORE_SUCCESS) {
936
    {
-
 
937
        return res;
910
        return res;
938
    }
911
    }
939
    
912
    
940
    if (in[1] > 0x80000000)
913
    if (in[1] > 0x80000000) {
941
    {
-
 
942
        return EMCORE_ERROR_IO;
914
        return EMCORE_ERROR_IO;
943
    }
915
    }
944
    
916
    
945
    return EMCORE_SUCCESS;
917
    return EMCORE_SUCCESS;
946
}
918
}
947
 
919
 
948
int emcore_errno(uint32_t* emcore_errno_value)
920
int32_t emcore_errno(uint32_t *emcore_errno_value) {
949
{
-
 
950
    int res;
921
    int32_t res;
951
    uint32_t out[4] = { 49, 0, 0, 0 }, in[4];
922
    uint32_t out[4] = { 49, 0, 0, 0 }, in[4];
952
 
923
 
953
    res = emcore_monitor_command(out, in, 16, 16);
924
    res = emcore_monitor_command(out, in, 16, 16);
954
 
925
 
955
    if (EMCORE_SUCCESS != res)
926
    if (res != EMCORE_SUCCESS) {
956
    {
-
 
957
        return res;
927
        return res;
958
    }
928
    }
959
 
929
 
960
    *emcore_errno_value = in[1];
930
    *emcore_errno_value = in[1];
961
 
931
 
962
    return EMCORE_SUCCESS;
932
    return EMCORE_SUCCESS;
963
}
933
}
964
 
934
 
965
int emcore_malloc(uint32_t* ptr, uint32_t size)
935
int32_t emcore_malloc(uint32_t *ptr, uint32_t size) {
966
{
-
 
967
    int res;
936
    int32_t res;
968
    uint32_t out[4] = { 52, 0xdeadbeef, 0, 0 }, in[4];
937
    uint32_t out[4] = { 52, 0xdeadbeef, 0, 0 }, in[4];
969
 
938
 
970
    out[1] = size;
939
    out[1] = size;
971
 
940
 
972
    res = emcore_monitor_command(out, in, 16, 16);
941
    res = emcore_monitor_command(out, in, 16, 16);
973
 
942
 
974
    if (EMCORE_SUCCESS != res)
943
    if (res != EMCORE_SUCCESS) {
975
    {
-
 
976
        return res;
944
        return res;
977
    }
945
    }
978
 
946
 
979
    *ptr = in[1];
947
    *ptr = in[1];
980
 
948
 
981
    return EMCORE_SUCCESS;
949
    return EMCORE_SUCCESS;
982
}
950
}
983
 
951
 
984
int emcore_memalign(uint32_t* ptr, uint32_t align, uint32_t size)
952
int32_t emcore_memalign(uint32_t *ptr, uint32_t align, uint32_t size) {
985
{
-
 
986
    int res;
953
    int32_t res;
987
    uint32_t out[4] = { 53, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
954
    uint32_t out[4] = { 53, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
988
 
955
 
989
    out[1] = align;
956
    out[1] = align;
990
    out[2] = size;
957
    out[2] = size;
991
 
958
 
992
    res = emcore_monitor_command(out, in, 16, 16);
959
    res = emcore_monitor_command(out, in, 16, 16);
993
 
960
 
994
    if (EMCORE_SUCCESS != res)
961
    if (res != EMCORE_SUCCESS) {
995
    {
-
 
996
        return res;
962
        return res;
997
    }
963
    }
998
 
964
 
999
    *ptr = in[1];
965
    *ptr = in[1];
1000
 
966
 
1001
    return EMCORE_SUCCESS;
967
    return EMCORE_SUCCESS;
1002
}
968
}
1003
 
969
 
1004
int emcore_realloc(uint32_t* new_ptr, uint32_t ptr, uint32_t size)
970
int32_t emcore_realloc(uint32_t *new_ptr, uint32_t ptr, uint32_t size) {
1005
{
-
 
1006
    int res;
971
    int32_t res;
1007
    uint32_t out[4] = { 54, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
972
    uint32_t out[4] = { 54, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
1008
 
973
 
1009
    out[1] = ptr;
974
    out[1] = ptr;
1010
    out[2] = size;
975
    out[2] = size;
1011
 
976
 
1012
    res = emcore_monitor_command(out, in, 16, 16);
977
    res = emcore_monitor_command(out, in, 16, 16);
1013
 
978
 
1014
    if (EMCORE_SUCCESS != res)
979
    if (res != EMCORE_SUCCESS) {
1015
    {
-
 
1016
        return res;
980
        return res;
1017
    }
981
    }
1018
 
982
 
1019
    *new_ptr = in[1];
983
    *new_ptr = in[1];
1020
 
984
 
1021
    return EMCORE_SUCCESS;
985
    return EMCORE_SUCCESS;
1022
}
986
}
1023
 
987
 
1024
int emcore_reownalloc(uint32_t ptr, uint32_t owner)
988
int32_t emcore_reownalloc(uint32_t ptr, uint32_t owner) {
1025
{
-
 
1026
    uint32_t out[4] = { 55, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
989
    uint32_t out[4] = { 55, 0xdeadbeef, 0xdeadbeef, 0 }, in[4];
1027
 
990
 
1028
    out[1] = ptr;
991
    out[1] = ptr;
1029
    out[2] = owner;
992
    out[2] = owner;
1030
 
993
 
1031
    return emcore_monitor_command(out, in, 16, 16);
994
    return emcore_monitor_command(out, in, 16, 16);
1032
}
995
}
1033
 
996
 
1034
int emcore_free(uint32_t ptr)
997
int32_t emcore_free(uint32_t ptr) {
1035
{
-
 
1036
    uint32_t out[4] = { 56, 0xdeadbeef, 0, 0 }, in[4];
998
    uint32_t out[4] = { 56, 0xdeadbeef, 0, 0 }, in[4];
1037
 
999
 
1038
    out[1] = ptr;
1000
    out[1] = ptr;
1039
 
1001
 
1040
    return emcore_monitor_command(out, in, 16, 16);
1002
    return emcore_monitor_command(out, in, 16, 16);
1041
}
1003
}
1042
 
1004
 
1043
int emcore_free_all(void)
1005
int32_t emcore_free_all(void) {
1044
{
-
 
1045
    uint32_t out[4] = { 57, 0, 0, 0 }, in[4];
1006
    uint32_t out[4] = { 57, 0, 0, 0 }, in[4];
1046
 
1007
 
1047
    return emcore_monitor_command(out, in, 16, 16);
1008
    return emcore_monitor_command(out, in, 16, 16);
1048
}
1009
}
1049
 
1010
 
1050
int emcore_read(void* data, uint32_t addr, uint32_t size)
-
 
1051
{
-
 
1052
    int res;
-
 
1053
    struct alignsizes* sizes;
-
 
1054
    uint32_t cin_maxsize, readsize, curraddr;
-
 
1055
 
-
 
1056
    cin_maxsize = emcore_usb_eps_mps.cin - EMCORE_HEADER_SIZE;
-
 
1057
    sizes = malloc(sizeof(*sizes));
-
 
1058
 
-
 
1059
    alignsplit(sizes, addr, size, cin_maxsize, 16);
-
 
1060
#ifdef DEBUG_ALIGN_SPLIT
-
 
1061
    fprintf(stderr, "Downloading %d bytes from 0x%08x, split as (%d/%d/%d)\n",
-
 
1062
        size, addr, sizes->head, sizes->body, sizes->tail);
-
 
1063
 
-
 
1064
#endif
-
 
1065
    curraddr = addr;
-
 
1066
 
-
 
1067
    if (sizes->head > 0)
-
 
1068
    {
-
 
1069
        res = emcore_readmem(data, curraddr, sizes->head);
-
 
1070
 
-
 
1071
        if (EMCORE_SUCCESS != res)
-
 
1072
        {
-
 
1073
            return res;
-
 
1074
        }
-
 
1075
 
-
 
1076
        data += sizes->head;
-
 
1077
        curraddr += sizes->head;
-
 
1078
    }
-
 
1079
 
-
 
1080
    while (sizes->body > 0)
-
 
1081
    {
-
 
1082
        if (sizes->body >= cin_maxsize * 2)
-
 
1083
        {
-
 
1084
            readsize = MIN(sizes->body, emcore_usb_eps_mps.din);
-
 
1085
            res = emcore_readdma(data, curraddr, readsize);
-
 
1086
        }
-
 
1087
        else
-
 
1088
        {
-
 
1089
            readsize = MIN(sizes->body, cin_maxsize);
-
 
1090
            res = emcore_readmem(data, curraddr, readsize);
-
 
1091
        }
-
 
1092
 
-
 
1093
        if (EMCORE_SUCCESS != res)
-
 
1094
        {
-
 
1095
            return res;
-
 
1096
        }
-
 
1097
 
-
 
1098
        data += readsize;
-
 
1099
        curraddr += readsize;
-
 
1100
        sizes->body -= readsize;
-
 
1101
    }
-
 
1102
 
-
 
1103
    if (sizes->tail > 0)
-
 
1104
    {
-
 
1105
        res = emcore_readmem(data, curraddr, sizes->tail);
-
 
1106
 
-
 
1107
        if (EMCORE_SUCCESS != res)
-
 
1108
        {
-
 
1109
            return res;
-
 
1110
        }
-
 
1111
 
-
 
1112
        data += sizes->tail;
-
 
1113
    }
-
 
1114
 
-
 
1115
    return EMCORE_SUCCESS;
-
 
1116
}
-
 
1117
 
-
 
1118
int emcore_write(const void* data, uint32_t addr, uint32_t size)
-
 
1119
{
-
 
1120
    int res;
-
 
1121
    struct alignsizes* sizes;
-
 
1122
    uint32_t cout_maxsize, writesize, curraddr;
-
 
1123
 
-
 
1124
    cout_maxsize = emcore_usb_eps_mps.cout - 16;
-
 
1125
    sizes = malloc(sizeof(*sizes));
-
 
1126
 
-
 
1127
    alignsplit(sizes, addr, size, cout_maxsize, 16);
-
 
1128
#ifdef DEBUG_ALIGN_SPLIT
-
 
1129
    fprintf(stderr, "Uploading %d bytes from 0x%08x, split as (%d/%d/%d)\n",
-
 
1130
        size, addr, sizes->head, sizes->body, sizes->tail);
-
 
1131
 
-
 
1132
#endif
-
 
1133
    curraddr = addr;
-
 
1134
 
-
 
1135
    if (sizes->head > 0)
-
 
1136
    {
-
 
1137
        res = emcore_writemem(data, curraddr, sizes->head);
-
 
1138
 
-
 
1139
        if (EMCORE_SUCCESS != res)
-
 
1140
        {
-
 
1141
            return res;
-
 
1142
        }
-
 
1143
 
-
 
1144
        data += sizes->head;
-
 
1145
        curraddr += sizes->head;
-
 
1146
    }
-
 
1147
 
-
 
1148
    while (sizes->body > 0)
-
 
1149
    {
-
 
1150
        if (sizes->body >= 2 * cout_maxsize)
-
 
1151
        {
-
 
1152
            writesize = MIN(sizes->body, emcore_usb_eps_mps.dout);
-
 
1153
            res = emcore_writedma(data, curraddr, writesize);
-
 
1154
        }
-
 
1155
        else
-
 
1156
        {
-
 
1157
            writesize = MIN(sizes->body, cout_maxsize);
-
 
1158
            res = emcore_writemem(data, curraddr, writesize);
-
 
1159
        }
-
 
1160
 
-
 
1161
        if (EMCORE_SUCCESS != res)
-
 
1162
        {
-
 
1163
            return res;
-
 
1164
        }
-
 
1165
 
-
 
1166
        data += writesize;
-
 
1167
        curraddr += writesize;
-
 
1168
        sizes->body -= writesize;
-
 
1169
    }
-
 
1170
 
-
 
1171
    if (sizes->tail > 0)
-
 
1172
    {
-
 
1173
        res = emcore_writemem(data, curraddr, sizes->tail);
-
 
1174
 
-
 
1175
        if (EMCORE_SUCCESS != res)
-
 
1176
        {
-
 
1177
            return res;
-
 
1178
        }
-
 
1179
 
-
 
1180
        data += sizes->tail;
-
 
1181
    }
-
 
1182
 
-
 
1183
    return EMCORE_SUCCESS;
-
 
1184
}
-
 
1185
 
-
 
1186
int emcore_ls(uint32_t handle)
1011
int32_t emcore_ls(uint32_t handle) {
1187
{
-
 
1188
    int res = 0;
1012
    uint32_t res = 0;
1189
    struct emcore_dir_entry entry;
1013
    struct emcore_dir_entry entry;
1190
 
1014
 
1191
    while (1)
1015
    while (1) {
1192
    {
-
 
1193
        res = emcore_dir_read(&entry, handle);
1016
        res = emcore_dir_read(&entry, handle);
1194
 
1017
 
1195
        if (EMCORE_ERROR_NO_MORE_ENTRIES == res)
1018
        if (res == EMCORE_ERROR_NO_MORE_ENTRIES) {
1196
        {
-
 
1197
            res = EMCORE_SUCCESS;
1019
            res = EMCORE_SUCCESS;
1198
            break;
1020
            break;
1199
        }
1021
        }
1200
 
1022
 
1201
        if (EMCORE_SUCCESS != res)
1023
        if (res != EMCORE_SUCCESS) {
1202
        {
-
 
1203
            return res;
1024
            return res;
1204
        }
1025
        }
1205
 
1026
 
1206
#ifdef DEBUG_DIR_ENTRIES
1027
#ifdef DEBUG_DIR_ENTRIES
1207
        fprintf(stderr, "Read directory entry:\n");
1028
        fprintf(stderr, "Read directory entry:\n");
Line 1209... Line 1030...
1209
        fprintf(stderr, "Attributes: 0x%08x\n", entry.attributes);
1030
        fprintf(stderr, "Attributes: 0x%08x\n", entry.attributes);
1210
        fprintf(stderr, "Size: %d\n", entry.size);
1031
        fprintf(stderr, "Size: %d\n", entry.size);
1211
        fprintf(stderr, "Start cluster: %d\n", entry.startcluster);
1032
        fprintf(stderr, "Start cluster: %d\n", entry.startcluster);
1212
        fprintf(stderr, "Last written date: 0x%04x\n", entry.wrtdate);
1033
        fprintf(stderr, "Last written date: 0x%04x\n", entry.wrtdate);
1213
        fprintf(stderr, "Last written time: 0x%04x\n", entry.wrttime);
1034
        fprintf(stderr, "Last written time: 0x%04x\n", entry.wrttime);
1214
        fprintf(stderr, "Last written TS: %lu\n",
1035
        fprintf(stderr, "Last written TS: %d\n",
1215
            (unsigned long) fat_time_to_unix_ts(entry.wrttime, entry.wrtdate));
1036
            fat_time_to_unix_ts(entry.wrttime, entry.wrtdate));
1216
#endif
1037
#endif
1217
        if (entry.attributes & 0x10)
1038
        if (entry.attributes & 0x10) {
1218
        {
-
 
1219
            printf("     [DIR]");
1039
            printf("     [DIR]");
1220
        }
1040
        }
1221
        else
1041
        else {
1222
        {
-
 
1223
            printf("%10d", entry.size);
1042
            printf("%10d", entry.size);
1224
        }
1043
        }
1225
 
1044
 
1226
        printf(" %s\n", entry.name);
1045
        printf(" %s\n", entry.name);
1227
    }
1046
    }
1228
 
1047
 
1229
    return res;
1048
    return res;
1230
}
1049
}
1231
 
-
 
1232
int emcore_test(void)
-
 
1233
{
-
 
1234
    int res;
-
 
1235
 
-
 
1236
    /* emcore_get_version */
-
 
1237
    struct emcore_dev_info dev_info;
-
 
1238
    char *hw_type;
-
 
1239
 
-
 
1240
    /* emcore_get_packet_info */
-
 
1241
    struct emcore_usb_endpoints_max_packet_size max_packet_size;
-
 
1242
 
-
 
1243
    /* emcore_get_user_mem_range */
-
 
1244
    struct emcore_user_mem_range mem_range;
-
 
1245
 
-
 
1246
    /* emcore_readmem */
-
 
1247
    void* buf;
-
 
1248
    uint16_t buf_size;
-
 
1249
    uint32_t read_addr;
-
 
1250
 
-
 
1251
    /* emcore_readi2c */
-
 
1252
    /* uint8_t i2cdata; */
-
 
1253
 
-
 
1254
    /* emcore_dir_open */
-
 
1255
    uint32_t dir_handle;
-
 
1256
 
-
 
1257
    /* emcore_dir_close_all */
-
 
1258
    uint32_t count;
-
 
1259
 
-
 
1260
    res = emcore_get_version(&dev_info);
-
 
1261
 
-
 
1262
    if (EMCORE_SUCCESS != res)
-
 
1263
    {
-
 
1264
        return res;
-
 
1265
    }
-
 
1266
 
-
 
1267
    hw_type = malloc(5);
-
 
1268
 
-
 
1269
    strncpy(hw_type, ((char*)&dev_info.hw_type), 4);
-
 
1270
 
-
 
1271
    printf("Connected to %4s running %s v%d.%d.%d r%d\n",
-
 
1272
        hw_type, (1 == dev_info.sw_type ? "emBIOS" : (2 == dev_info.sw_type ? "emCORE" : "UNKNOWN")),
-
 
1273
        dev_info.major, dev_info.minor, dev_info.patch, dev_info.svn_revision
-
 
1274
    );
-
 
1275
 
-
 
1276
    free(hw_type);
-
 
1277
 
-
 
1278
    res = emcore_get_packet_info(&max_packet_size);
-
 
1279
 
-
 
1280
    if (EMCORE_SUCCESS != res)
-
 
1281
    {
-
 
1282
        return res;
-
 
1283
    }
-
 
1284
 
-
 
1285
    printf("COUT max pckt: %d, CIN max pckt: %d, DOUT max pckt: %d, DIN max pckt: %d\n",
-
 
1286
        max_packet_size.cout, max_packet_size.cin, max_packet_size.dout, max_packet_size.din
-
 
1287
    );
-
 
1288
 
-
 
1289
    res = emcore_get_user_mem_range(&mem_range);
-
 
1290
 
-
 
1291
    if (EMCORE_SUCCESS != res)
-
 
1292
    {
-
 
1293
        return res;
-
 
1294
    }
-
 
1295
 
-
 
1296
    printf("User mem range: 0x%08x - 0x%08x\n", mem_range.lower, mem_range.upper);
-
 
1297
 
-
 
1298
    read_addr = 0x09000000;
-
 
1299
    buf_size = 0x1000;
-
 
1300
    buf = malloc(buf_size);
-
 
1301
 
-
 
1302
    printf("Reading 0x%08x bytes from 0x%08x\n", buf_size, read_addr);
-
 
1303
 
-
 
1304
    res = emcore_read(buf, read_addr, buf_size);
-
 
1305
 
-
 
1306
    if (EMCORE_SUCCESS != res)
-
 
1307
    {
-
 
1308
        return res;
-
 
1309
    }
-
 
1310
 
-
 
1311
#ifdef DEBUG
-
 
1312
    dump_packet(buf, buf_size);
-
 
1313
 
-
 
1314
#endif
-
 
1315
    printf("Writing 0x%08x bytes to 0x%08x\n", buf_size, read_addr);
-
 
1316
 
-
 
1317
    res = emcore_write(buf, read_addr, buf_size);
-
 
1318
 
-
 
1319
    if (EMCORE_SUCCESS != res)
-
 
1320
    {
-
 
1321
        return res;
-
 
1322
    }
-
 
1323
 
-
 
1324
    free(buf);
-
 
1325
 
-
 
1326
    /*
-
 
1327
    printf("Reading 1 byte from I2C\n");
-
 
1328
 
-
 
1329
    res = emcore_readi2c(&i2cdata, 0, 0xe6, 0x29, 1);
-
 
1330
 
-
 
1331
    if (EMCORE_SUCCESS != res)
-
 
1332
    {
-
 
1333
        return res;
-
 
1334
    }
-
 
1335
 
-
 
1336
#ifdef DEBUG
-
 
1337
    dump_packet(&i2cdata, 1);
-
 
1338
 
-
 
1339
#endif
-
 
1340
    */
-
 
1341
    /* nano2g - turns on/off the backlight */
-
 
1342
    /*
-
 
1343
    i2cdata = 1;
-
 
1344
 
-
 
1345
    printf("Writing 1 byte to I2C\n");
-
 
1346
 
-
 
1347
    res = emcore_writei2c(&i2cdata, 0, 0xe6, 0x29, 1);
-
 
1348
 
-
 
1349
    if (EMCORE_SUCCESS != res)
-
 
1350
    {
-
 
1351
        return res;
-
 
1352
    }
-
 
1353
 
-
 
1354
    sleep(1);
-
 
1355
 
-
 
1356
    i2cdata = 0;
-
 
1357
 
-
 
1358
    printf("Writing 1 byte to I2C\n");
-
 
1359
 
-
 
1360
    res = emcore_writei2c(&i2cdata, 0, 0xe6, 0x29, 1);
-
 
1361
 
-
 
1362
    if (EMCORE_SUCCESS != res)
-
 
1363
    {
-
 
1364
        return res;
-
 
1365
    }
-
 
1366
    */
-
 
1367
    res = emcore_dir_open(&dir_handle, "/");
-
 
1368
 
-
 
1369
    if (EMCORE_SUCCESS != res)
-
 
1370
    {
-
 
1371
        return res;
-
 
1372
    }
-
 
1373
 
-
 
1374
    printf("Opened dir handle: 0x%08x\n", dir_handle);
-
 
1375
 
-
 
1376
    res = emcore_ls(dir_handle);
-
 
1377
 
-
 
1378
    if (EMCORE_SUCCESS != res)
-
 
1379
    {
-
 
1380
        return res;
-
 
1381
    }
-
 
1382
 
-
 
1383
    printf("Listed dir handle: 0x%08x\n", dir_handle);
-
 
1384
 
-
 
1385
    res = emcore_dir_close(dir_handle);
-
 
1386
 
-
 
1387
    if (EMCORE_SUCCESS != res)
-
 
1388
    {
-
 
1389
        return res;
-
 
1390
    }
-
 
1391
 
-
 
1392
    printf("Closed dir handle 0x%08x\n", dir_handle);
-
 
1393
 
-
 
1394
    res = emcore_dir_close_all(&count);
-
 
1395
 
-
 
1396
    if (EMCORE_SUCCESS != res)
-
 
1397
    {
-
 
1398
        return res;
-
 
1399
    }
-
 
1400
 
-
 
1401
    printf("Closed %d dir handles\n", count);
-
 
1402
    /* powers off the device - graceful
-
 
1403
    res = emcore_poweroff(1);
-
 
1404
 
-
 
1405
    if (EMCORE_SUCCESS != res)
-
 
1406
    {
-
 
1407
        return res;
-
 
1408
    }
-
 
1409
 
-
 
1410
    printf("Device powered off successfully!\n");
-
 
1411
    */
-
 
1412
    return EMCORE_SUCCESS;
-
 
1413
}
-