-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.cpp
868 lines (732 loc) · 18.9 KB
/
io.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
#include "config.h"
#include "wrap_fatfs.h"
#include <_ansi.h>
#include <_syslist.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#undef errno
extern int errno;
#include <circle/input/console.h>
#include <circle/sched/scheduler.h>
#include <circle/usb/usbhostcontroller.h>
#include <circle/string.h>
#include "circle_glue.h"
#include <assert.h>
struct _CIRCLE_DIR
{
_CIRCLE_DIR ()
:
mFirstRead (0), mOpen (0)
{
mEntry.d_ino = 0;
mEntry.d_name[0] = 0;
}
FATFS_DIR mCurrentEntry;
struct dirent mEntry;
unsigned int mFirstRead :1;
unsigned int mOpen :1;
};
namespace
{
FATFS *circle_fat_fs = nullptr;
constexpr unsigned int MAX_OPEN_FILES = 20;
constexpr unsigned int MAX_OPEN_DIRS = 20;
class CGlueIO;
struct CircleFile
{
CircleFile ()
:
mCGlueIO (nullptr)
{
}
CGlueIO *mCGlueIO;
};
CircleFile fileTab[MAX_OPEN_FILES];
CSpinLock fileTabLock(TASK_LEVEL);
_CIRCLE_DIR dirTab[MAX_OPEN_DIRS];
CSpinLock dirTabLock(TASK_LEVEL);
/**
* Helper class to acquire lock and to release it automatically
* when surrounding block is left.
*/
class SpinLockHolder
{
public:
SpinLockHolder (CSpinLock &lock) : lockRef(lock)
{
lockRef.Acquire ();
}
~SpinLockHolder ()
{
lockRef.Release ();
}
private:
CSpinLock &lockRef;
};
class CGlueIO
{
public:
virtual
~CGlueIO ()
{
}
virtual int
Read (void *pBuffer, int nCount) = 0;
virtual int
Write (const void *pBuffer, int nCount) = 0;
virtual int
LSeek(int ptr, int dir) = 0;
virtual int
Close (void) = 0;
};
class CGlueConsole : public CGlueIO
{
public:
enum TConsoleMode
{
ConsoleModeRead, ConsoleModeWrite
};
CGlueConsole (CConsole &rConsole, TConsoleMode mode)
:
mConsole (rConsole), mMode (mode)
{
}
int
Read (void *pBuffer, int nCount)
{
int nResult = -1;
if (mMode == ConsoleModeRead)
{
CScheduler * const scheduler =
CScheduler::IsActive () ? CScheduler::Get () : nullptr;
CUSBHostController * const usbhost =
CUSBHostController::IsActive () ?
CUSBHostController::Get () : nullptr;
while ((nResult = mConsole.Read (pBuffer,
static_cast<size_t> (nCount)))
== 0)
{
if (scheduler)
{
scheduler->Yield ();
}
if (usbhost && usbhost->UpdatePlugAndPlay ())
{
mConsole.UpdatePlugAndPlay ();
}
}
}
if (nResult < 0)
{
errno = EIO;
// Could be negative but different from -1.
nResult = -1;
}
return nResult;
}
int
Write (const void *pBuffer, int nCount)
{
int nResult =
mMode == ConsoleModeWrite ?
mConsole.Write (pBuffer, static_cast<size_t> (nCount)) : -1;
if (nResult < 0)
{
errno = EIO;
// Could be negative but different from -1.
nResult = -1;
}
return nResult;
}
int
LSeek(int ptr, int dir)
{
errno = ESPIPE;
return -1;
}
int
Close (void)
{
// TODO: Cannot close console file handle currently.
errno = EBADF;
return -1;
}
private:
CConsole &mConsole;
TConsoleMode const mMode;
};
/**
* FatFs: http://www.elm-chan.org/fsw/ff/00index_e.html
*/
struct CGlueIoFatFs : public CGlueIO
{
CGlueIoFatFs ()
{
memset (&mFile, 0, sizeof(mFile));
}
bool
Open (char *file, int flags, int /* mode */)
{
int fatfs_flags;
/*
* The OpenGroup documentation of the flags for open() says:
*
* "Applications shall specify exactly one of the first
* three values (file access modes) below in the value of oflag:
*
* O_RDONLY Open for reading only.
* O_WRONLY Open for writing only.
* O_RDWR Open for reading and writing."
*
* So combinations of those flags need not to be dealt with.
*/
if (flags & O_RDWR)
{
fatfs_flags = FA_READ | FA_WRITE;
}
else if (flags & O_WRONLY)
{
fatfs_flags = FA_WRITE;
}
else
{
fatfs_flags = FA_READ;
}
if (flags & O_TRUNC)
{
/*
* OpenGroup documentation:
* "The result of using O_TRUNC with O_RDONLY is undefined."
*/
fatfs_flags |= FA_CREATE_ALWAYS;
}
if (flags & O_APPEND)
{
fatfs_flags |= FA_OPEN_APPEND;
}
if (flags & O_CREAT)
{
if (flags & O_EXCL)
{
/*
* OpenGroup documentation:
* "O_EXCL If O_CREAT and O_EXCL are set, open() shall fail if the file exists.
* "If O_EXCL is set and O_CREAT is not set, the result is undefined.".
*/
fatfs_flags |= FA_CREATE_NEW;
}
else
{
fatfs_flags |= FA_OPEN_ALWAYS;
}
}
// TODO mode?
auto const fresult = f_open (&mFile, file, fatfs_flags);
bool result;
if (fresult == FR_OK)
{
result = true;
}
else
{
result = false;
switch (fresult)
{
case FR_EXIST:
errno = EEXIST;
break;
case FR_NO_FILE:
case FR_INVALID_NAME:
errno = ENOENT;
break;
case FR_NO_PATH:
case FR_INVALID_DRIVE:
errno = ENOTDIR;
break;
case FR_NOT_ENOUGH_CORE:
errno = ENOMEM;
break;
case FR_TOO_MANY_OPEN_FILES:
errno = ENFILE;
break;
case FR_DENIED:
case FR_DISK_ERR:
case FR_INT_ERR:
case FR_NOT_READY:
case FR_INVALID_OBJECT:
case FR_WRITE_PROTECTED:
case FR_NOT_ENABLED:
case FR_NO_FILESYSTEM:
case FR_TIMEOUT:
case FR_LOCKED:
default:
errno = EACCES;
break;
}
}
return result;
}
int
Read (void *pBuffer, int nCount)
{
UINT bytes_read = 0;
auto const fresult = f_read (&mFile, pBuffer,
static_cast<UINT> (nCount),
&bytes_read);
int result;
if (fresult == FR_OK)
{
result = static_cast<int> (bytes_read);
}
else
{
result = -1;
switch (fresult)
{
case FR_INVALID_OBJECT:
case FR_DENIED:
errno = EBADF;
break;
case FR_DISK_ERR:
case FR_INT_ERR:
case FR_TIMEOUT:
default:
errno = EIO;
break;
}
}
return result;
}
int
Write (const void *pBuffer, int nCount)
{
UINT bytesWritten = 0;
auto const fresult = f_write (&mFile, pBuffer,
static_cast<UINT> (nCount),
&bytesWritten);
int result;
if (fresult == FR_OK)
{
result = static_cast<int> (bytesWritten);
}
else
{
result = -1;
switch (fresult)
{
case FR_INVALID_OBJECT:
errno = EBADF;
break;
case FR_DISK_ERR:
case FR_INT_ERR:
case FR_DENIED:
case FR_TIMEOUT:
default:
errno = EIO;
break;
}
}
return result;
}
int
LSeek(int ptr, int dir)
{
/*
* If whence is SEEK_SET, the file offset shall be set to
* offset bytes.
*
* If whence is SEEK_CUR, the file offset shall be set to
* its current location plus offset.
*
* If whence is SEEK_END, the file offset shall be set to
* the size of the file plus offset.
*/
int new_pos;
switch (dir)
{
case SEEK_SET:
new_pos = ptr;
break;
case SEEK_CUR:
new_pos = static_cast<int>(f_tell(&mFile)) + ptr;
break;
case SEEK_END:
new_pos = static_cast<int>(f_size(&mFile)) + ptr;
break;
default:
new_pos = -1;
break;
}
int result;
if (new_pos >= 0)
{
int const fresult = f_lseek(&mFile, static_cast<FSIZE_t>(new_pos));
switch (fresult)
{
case FR_OK:
result = new_pos;
break;
case FR_DISK_ERR:
case FR_INT_ERR:
case FR_INVALID_OBJECT:
case FR_TIMEOUT:
default:
result = -1;
errno = EBADF;
break;
}
}
else
{
errno = EINVAL;
result = -1;
}
return result;
}
int
Close (void)
{
auto const close_result = f_close (&mFile);
int result;
switch (close_result)
{
case FR_OK:
result = 0;
break;
case FR_INVALID_OBJECT:
case FR_INT_ERR:
errno = EBADF;
result = -1;
break;
default:
errno = EIO;
result = -1;
break;
}
return result;
}
FIL mFile;
};
int
FindFreeFileSlot (void)
{
int slotNr = -1;
for (auto const &slot : fileTab)
{
if (slot.mCGlueIO == nullptr)
{
slotNr = &slot - fileTab;
break;
}
}
return slotNr;
}
int
FindFreeDirSlot (void)
{
int slotNr = -1;
for (auto const &slot : dirTab)
{
if (!slot.mOpen)
{
slotNr = &slot - dirTab;
break;
}
}
return slotNr;
}
void
CGlueInitFileSystem (FATFS &rFATFileSystem)
{
// Must only be called once
assert(!circle_fat_fs);
circle_fat_fs = &rFATFileSystem;
}
void
CGlueInitConsole (CConsole &rConsole)
{
CircleFile &stdin = fileTab[0];
CircleFile &stdout = fileTab[1];
CircleFile &stderr = fileTab[2];
// Must only be called once and not be called after a file has already been opened
assert(!stdin.mCGlueIO);
assert(!stdout.mCGlueIO);
assert(!stderr.mCGlueIO);
stdin.mCGlueIO = new CGlueConsole (rConsole,
CGlueConsole::ConsoleModeRead);
stdout.mCGlueIO = new CGlueConsole (rConsole,
CGlueConsole::ConsoleModeWrite);
stderr.mCGlueIO = new CGlueConsole (rConsole,
CGlueConsole::ConsoleModeWrite);
}
}
void
CGlueStdioInit (FATFS &rFATFileSystem, CConsole &rConsole)
{
CGlueInitConsole (rConsole);
CGlueInitFileSystem (rFATFileSystem);
}
void
CGlueStdioInit (FATFS &rFATFileSystem)
{
CGlueInitFileSystem (rFATFileSystem);
}
void
CGlueStdioInit (CConsole &rConsole)
{
CGlueInitConsole (rConsole);
}
extern "C" int
_open (char *file, int flags, int mode)
{
SpinLockHolder const lockHolder(fileTabLock);
int slot = FindFreeFileSlot ();
if (slot != -1)
{
auto const newFatFs = new CGlueIoFatFs ();
if (newFatFs->Open (file, flags, mode))
{
fileTab[slot].mCGlueIO = newFatFs;
}
else
{
delete newFatFs;
slot = -1;
}
}
else
{
errno = ENFILE;
}
return slot;
}
extern "C" int
_close (int fildes)
{
if (fildes < 0 || static_cast<unsigned int> (fildes) >= MAX_OPEN_FILES)
{
errno = EBADF;
return -1;
}
SpinLockHolder const lockHolder(fileTabLock);
CircleFile &file = fileTab[fildes];
if (file.mCGlueIO == nullptr)
{
errno = EBADF;
return -1;
}
unsigned const result = file.mCGlueIO->Close ();
delete file.mCGlueIO;
file.mCGlueIO = nullptr;
return result;
}
extern "C" int
_read (int fildes, char *ptr, int len)
{
if (fildes < 0 || static_cast<unsigned int> (fildes) >= MAX_OPEN_FILES)
{
errno = EBADF;
return -1;
}
CircleFile &file = fileTab[fildes];
if (file.mCGlueIO == nullptr)
{
errno = EBADF;
return -1;
}
return file.mCGlueIO->Read (ptr, len);
}
extern "C" int
_write (int fildes, char *ptr, int len)
{
if (fildes < 0 || static_cast<unsigned int> (fildes) >= MAX_OPEN_FILES)
{
errno = EBADF;
return -1;
}
CircleFile &file = fileTab[fildes];
if (file.mCGlueIO == nullptr)
{
errno = EBADF;
return -1;
}
return file.mCGlueIO->Write (ptr, len);
}
extern "C" int
_lseek(int fildes, int ptr, int dir)
{
if (fildes < 0 || static_cast<unsigned int> (fildes) >= MAX_OPEN_FILES)
{
errno = EBADF;
return -1;
}
CircleFile &file = fileTab[fildes];
if (file.mCGlueIO == nullptr)
{
errno = EBADF;
return -1;
}
return file.mCGlueIO->LSeek (ptr, dir);
}
extern "C" DIR*
opendir (const char *name)
{
assert(circle_fat_fs);
SpinLockHolder const lockHolder(dirTabLock);
int const slotNum = FindFreeDirSlot ();
if (slotNum == -1)
{
errno = ENFILE;
return nullptr;
}
auto &slot = dirTab[slotNum];
FRESULT const fresult = f_opendir (&slot.mCurrentEntry, name);
/*
* Best-effort mapping of FatFs error codes to errno values.
*/
DIR *result = nullptr;
switch (fresult)
{
case FR_OK:
slot.mOpen = 1;
slot.mFirstRead = 1;
result = &slot;
break;
case FR_DISK_ERR:
case FR_INT_ERR:
case FR_NOT_READY:
case FR_INVALID_OBJECT:
errno = EACCES;
break;
case FR_NO_PATH:
case FR_INVALID_NAME:
case FR_INVALID_DRIVE:
case FR_NOT_ENABLED:
case FR_NO_FILESYSTEM:
case FR_TIMEOUT:
errno = ENOENT;
break;
case FR_NOT_ENOUGH_CORE:
case FR_TOO_MANY_OPEN_FILES:
errno = ENFILE;
break;
}
return result;
}
static bool
read_next_entry (FATFS_DIR *currentEntry, FILINFO *filinfo)
{
bool result;
if (f_readdir (currentEntry, filinfo) == FR_OK)
{
result = filinfo->fname[0] != 0;
}
else
{
errno = EBADF;
result = false;
}
return result;
}
static struct dirent*
do_readdir (DIR *dir, struct dirent *de)
{
FILINFO filinfo;
bool haveEntry;
if (dir->mFirstRead)
{
if (f_readdir (&dir->mCurrentEntry, nullptr) == FR_OK)
{
haveEntry = read_next_entry (&dir->mCurrentEntry, &filinfo);
}
else
{
errno = EBADF;
haveEntry = false;
}
dir->mFirstRead = 0;
}
else
{
haveEntry = read_next_entry (&dir->mCurrentEntry, &filinfo);
}
struct dirent *result;
if (haveEntry)
{
memcpy (de->d_name, filinfo.fname, sizeof(de->d_name));
de->d_ino = 0; // inode number does not exist in fatfs
result = de;
}
else
{
// end of directory does not change errno
result = nullptr;
}
return result;
}
extern "C" struct dirent*
readdir (DIR *dir)
{
struct dirent *result;
if (dir->mOpen)
{
result = do_readdir (dir, &dir->mEntry);
}
else
{
errno = EBADF;
result = nullptr;
}
return result;
}
extern "C" int
readdir_r (DIR *__restrict dir, dirent *__restrict de, dirent **__restrict ode)
{
int result;
if (dir->mOpen)
{
*ode = do_readdir (dir, de);
result = 0;
}
else
{
*ode = nullptr;
result = EBADF;
}
return result;
}
extern "C" void
rewinddir (DIR *dir)
{
dir->mFirstRead = 1;
}
extern "C" int
closedir (DIR *dir)
{
SpinLockHolder const lockHolder(dirTabLock);
int result;
if (dir->mOpen)
{
dir->mOpen = 0;
if (f_closedir (&dir->mCurrentEntry) == FR_OK)
{
result = 0;
}
else
{
errno = EBADF;
result = -1;
}
}
else
{
errno = EBADF;
result = -1;
}
return result;
}