Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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