forked from randyrossi/bmc64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle_newlib_patch.diff
1324 lines (1272 loc) · 31.4 KB
/
circle_newlib_patch.diff
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/libgloss/circle/errno.c b/libgloss/circle/errno.c
index 27d8e3a3e..f563bb7db 100644
--- a/libgloss/circle/errno.c
+++ b/libgloss/circle/errno.c
@@ -1,3 +1,7 @@
/* Supply a definition of errno if one not already provided. */
int errno;
+
+int *__errno_location(void) {
+ return &errno;
+}
diff --git a/libgloss/circle/fstat.c b/libgloss/circle/fstat.c
index 3c5d7a6c2..9727a640a 100644
--- a/libgloss/circle/fstat.c
+++ b/libgloss/circle/fstat.c
@@ -12,11 +12,4 @@
extern int errno;
#include "warning.h"
-int
-_fstat(int fildes, struct stat *st)
-{
- errno = ENOSYS;
- return -1;
-}
-
-stub_warning(_fstat)
+// Moved to io.cpp
diff --git a/libgloss/circle/getpid.c b/libgloss/circle/getpid.c
index affc0f644..8c4d47f03 100644
--- a/libgloss/circle/getpid.c
+++ b/libgloss/circle/getpid.c
@@ -13,8 +13,8 @@ extern int errno;
int
_getpid(void)
{
- errno = ENOSYS;
- return -1;
+ // For BMC64 : Any number will do here.
+ return 100;
}
stub_warning(_getpid)
diff --git a/libgloss/circle/io.cpp b/libgloss/circle/io.cpp
index 4c8358738..5598f1c8f 100644
--- a/libgloss/circle/io.cpp
+++ b/libgloss/circle/io.cpp
@@ -1,375 +1,825 @@
#include "config.h"
#include <_ansi.h>
#include <_syslist.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/dirent.h>
-#include <fcntl.h>
#include <errno.h>
+#include <fcntl.h>
#include <string.h>
+#include <sys/dirent.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#undef errno
extern int errno;
#include "warning.h"
-#include <circle/fs/fat/fatfs.h>
-#include <circle/input/console.h>
-#include <circle/string.h>
+// Pi0 build fails unless we define these. But why??
+#ifndef S_IREAD
+#define S_IREAD 0000400
+#endif
+#ifndef S_IWRITE
+#define S_IWRITE 0000200
+#endif
+// End Pi0 hack
+
#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;
- }
+#include <malloc.h>
+#include <sys/unistd.h>
+#include <circle/serial.h>
+
+#include <ff.h>
+
+// This is a replacement io.cpp specifically for BMC64.
+// This implementation will sometimes load the entire file
+// into memory to provide faster seek operations, improving
+// performance on slow SD cards. Since any file the emulator
+// attempts to load is relatively small (<200k), this works out
+// just fine for our needs. Obviously, this would not be a
+// viable solution for most other circumstances. It also
+// works around an issue with circle/fatfs integration that
+// was causing memory corruption.
+//
+// When a file is opened for READ ONLY, fatfs is used to open
+// the file. As long as the client never seeks, the file will
+// not be loaded into ram and the disk still backs the data. As
+// soon as seek is called, the file will be loaded into ram and
+// from then on, ram backs the data. NOTE the fatfs file remains
+// open even after the file is loaded into ram in this case. If
+// the client never calls seek, the data will be read from fatfs.
+//
+// When a file is opened for WRITE ONLY, fatfs is used to create
+// the file. However, all write operations write to ram and only
+// when the file is finally closed will the data be dumped to
+// the fat fs filesystem. The fatfs file remains open during
+// the entire time between open/close. Seek is technically
+// supported in this case but attempting to seek past the
+// current file size is not. Call to fstat on a file in WRTE_ONLY
+// mode will not work as expected.
+//
+// When a file is opened for READ_WRITE, fat fs is used to
+// immediately load the contents of the existing file into ram.
+// The input fat fs file is immediatly closed in this case.
+// Writes & seeks use the ram copy. Only when the file is closed
+// will the fatfs system be used to create a new file from the ram.
+// Again, seeking past the file's current length is not supported.
+
+#define MAX_OPEN_FILES 10
+#define MAX_OPEN_DIRS 10
+#define READ_BUF_SIZE 1024
+
+static const char *pattern = "*";
+
+static char currentDir[256];
+
+/**
+ * @fn int strend(const char *s, const char *t)
+ * @brief Searches the end of string s for string t
+ * @param s the string to be searched
+ * @param t the substring to locate at the end of string s
+ * @return one if the string t occurs at the end of the string s, and zero otherwise
+ */
+int strend(const char *s, const char *t)
+{
+ size_t ls = strlen(s); // find length of s
+ size_t lt = strlen(t); // find length of t
+ if (ls >= lt) // check if t can fit in s
+ {
+ // point s to where t should start and compare the strings from there
+ return (0 == memcmp(t, s + (ls - lt), lt));
+ }
+ return 0; // t was longer than s
+}
- TFindCurrentEntry mCurrentEntry;
- struct dirent mEntry;
- unsigned int mFirstRead : 1;
- unsigned int mOpen : 1;
+static void reverse(char *x, int begin, int end) {
+ char c;
+ if (begin >= end)
+ return;
+
+ c = *(x + begin);
+ *(x + begin) = *(x + end);
+ *(x + end) = c;
+
+ reverse(x, ++begin, --end);
+}
+
+static void itoa2(int i, char *dst) {
+ int q = 0;
+ int j;
+ do {
+ j = i % 10;
+ dst[q] = '0' + j;
+ q++;
+ i = i / 10;
+ } while (i > 0);
+ dst[q] = '\0';
+
+ reverse(dst, 0, strlen(dst) - 1);
+}
+
+CSerialDevice *g_serial;
+
+static void logm(const char *msg) {
+ if (g_serial) {
+ g_serial->Write(msg, strlen(msg));
+ }
+}
+
+static void logi(int i) {
+ char nn[16];
+ itoa2(i,nn);
+ if (g_serial) {
+ g_serial->Write(nn, strlen(nn));
+ }
+}
+
+struct CirclePath {
+ CirclePath(const char* p) {
+ path[0] = '\0';
+
+ if (p == nullptr) {
+ return;
+ }
+
+ int len = strlen(p);
+ if (len == 0) {
+ return;
+ }
+
+ if (p[0] == '/') {
+ // Absolute
+ strcpy (path, p);
+ return;
+ }
+
+ // Relative
+ strcpy (path, currentDir);
+ if (len == 1 && p[0] == '.') {
+ // Treat as current dir
+ return;
+ }
+
+ // Handle ./ at start but we don't in the middle.
+ if (len >= 2 && p[0] == '.' && p[1] == '/') {
+ strcat (path, p+2);
+ } else {
+ strcpy (path, p);
+ }
+
+ // Fat fs doesn't like trailing slashes for dirs
+ if (strlen(path) > 1 && path[strlen(p)-1] == '/') {
+ path[strlen(p)-1] = '\0';
+ }
+ }
+
+ char path[256];
};
-namespace
-{
- struct CircleFile
- {
- CircleFile() : mCGlueIO(nullptr) {}
- CGlueIO *mCGlueIO;
- };
+struct CircleFile {
+ FIL file;
+ int in_use;
+ char fname[256];
+
+ char readBuf[READ_BUF_SIZE]; // tmp read buffer
+ char *contents; // bytes for file in memory
+ int allocated; // total bytes allocated for in memory file
+ unsigned size; // total size of file in memory file
+ unsigned position; // current in memory write position
+ int mode; // remembers mode this file was opened under
+ int written_to; // at least one write was performed on this file
+ int fopen_called; // f_open was called and thus f_close needs to be called
+};
+
+struct CircleDir {
+ CircleDir() {
+ mEntry.d_ino = 0;
+ mEntry.d_name[0] = 0;
+ dir.pat = pattern;
+ in_use = 0;
+ }
+
+ DIR dir;
+ int in_use;
+ struct dirent mEntry;
+};
- constexpr unsigned int MAX_OPEN_FILES = 20;
- constexpr unsigned int MAX_OPEN_DIRS = 20;
+CircleFile fileTab[MAX_OPEN_FILES];
+CircleDir dirTab[MAX_OPEN_DIRS];
- CFATFileSystem *circle_fat_fs = nullptr;
+static const char* const VolumeStr[FF_VOLUMES] = {FF_VOLUME_STRS};
+PARTITION VolToPart[FF_VOLUMES];
- CircleFile fileTab[MAX_OPEN_FILES];
- _CIRCLE_DIR dirTab[MAX_OPEN_DIRS];
+void CGlueStdioInit(CSerialDevice *serial) {
+ g_serial = serial;
- int FindFreeFileSlot(void)
- {
- int slotNr = -1;
-
- for (auto const& slot: fileTab)
- {
- if (slot.mCGlueIO == nullptr)
- {
- slotNr = &slot - fileTab;
- break;
- }
- }
-
- return slotNr;
+ // Initialize stdio, stderr and stdin
+ fileTab[0].in_use = 1;
+ fileTab[1].in_use = 1;
+ fileTab[2].in_use = 1;
+
+ // By default, use the first partition of each physical drive.
+ for (int pd = 0; pd < FF_VOLUMES; pd++) {
+ VolToPart[pd].pd = pd;
+ VolToPart[pd].pt = 0;
+ }
+
+ strcpy (currentDir, "/");
+}
+
+static int g_bootStatNum = 0;
+static int *g_bootStatWhat;
+static const char **g_bootStatFile;
+static int *g_bootStatSize;
+
+// Set global vars pointing to bootstat info
+void CGlueStdioInitBootStat (int num,
+ int *bootStatWhat,
+ const char **bootStatFile,
+ int *bootStatSize) {
+ g_bootStatNum = num;
+ g_bootStatWhat = bootStatWhat;
+ g_bootStatFile = bootStatFile;
+ g_bootStatSize = bootStatSize;
+}
+
+void CGlueStdioSetPartitionForVolume (const char* volume, int part, unsigned int ss) {
+ for (int pd = 0; pd < FF_VOLUMES; pd++) {
+ if (strcmp(volume, VolumeStr[pd]) == 0) {
+ VolToPart[pd].pt = part;
+ // Start sector only forced if part == 5
+ VolToPart[pd].ss = ss;
+ return;
+ }
+ }
+}
+
+static int FindFreeFileSlot(void) {
+ int slotNr = -1;
+
+ for (const CircleFile &slot : fileTab) {
+ if (slot.in_use == 0) {
+ slotNr = &slot - fileTab;
+ break;
}
+ }
- int FindFreeDirSlot(void)
- {
- int slotNr = -1;
-
- for (auto const& slot: dirTab)
- {
- if (!slot.mOpen)
- {
- slotNr = &slot - dirTab;
- break;
- }
- }
-
- return slotNr;
+ return slotNr;
+}
+
+static char *strdup2(const char *s) {
+ char *d = (char *)malloc(strlen(s) + 1);
+ if (d == nullptr)
+ return nullptr;
+ strcpy(d, s);
+ return d;
+}
+
+
+static int FindFreeDirSlot(void) {
+ int slotNr = -1;
+
+ for (const CircleDir &slot : dirTab) {
+ if (!slot.in_use) {
+ slotNr = &slot - dirTab;
+ break;
}
+ }
- void
- CGlueInitFileSystem (CFATFileSystem& rFATFileSystem)
- {
- // Must only be called once
- assert (!circle_fat_fs);
+ return slotNr;
+}
- circle_fat_fs = &rFATFileSystem;
+static CircleDir *FindCircleDirFromDIR(DIR *dir) {
+ for (CircleDir &slot : dirTab) {
+ if (slot.in_use && dir == &slot.dir) {
+ return &slot;
}
+ }
+ return nullptr;
+}
- 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);
+// Returns non zero value on any failure. Any memory will be
+// freed on error and file.contents nulled.
+static int slurp_file(CircleFile &file) {
+ if (file.contents == nullptr) {
+ // Read the entire contents of the file into memory.
+ file.size = 0;
+ unsigned total = 0;
+ if (f_lseek(&file.file, 0) != FR_OK) {
+ return -1;
}
+ while (true) {
+ unsigned int num_read;
+ if (f_read(&file.file, file.readBuf, READ_BUF_SIZE, &num_read) != FR_OK) {
+ if (file.contents) {
+ free(file.contents);
+ file.contents = nullptr;
+ }
+ return -1;
+ }
+
+ if (num_read == 0) {
+ break;
+ }
+
+ if (file.contents == nullptr) {
+ file.allocated = READ_BUF_SIZE;
+ file.contents = (char *)malloc(file.allocated);
+ } else if (file.allocated < total + num_read) {
+ file.allocated *= 2;
+ file.contents = (char *)realloc(file.contents, file.allocated);
+ }
+
+ memcpy(file.contents + total, file.readBuf, num_read);
+ total += num_read;
+ file.size = total;
+ }
+ }
+ return 0;
}
-void CGlueStdioInit(CFATFileSystem& rFATFileSystem, CConsole& rConsole)
-{
- CGlueInitConsole (rConsole);
- CGlueInitFileSystem (rFATFileSystem);
+extern "C" int _open(char *file, int flags, int mode) {
+ int const masked_flags = flags & 7;
+ if (masked_flags != O_RDONLY && masked_flags != O_WRONLY &&
+ masked_flags != O_RDWR) {
+ errno = ENOSYS;
+ return -1;
+ }
+
+ // Handle fast fail here
+ for (int i=0;i<g_bootStatNum;i++) {
+ if (g_bootStatWhat[i] == BOOTSTAT_WHAT_FAIL) {
+ if (strend(file, g_bootStatFile[i])) {
+ errno = EACCES;
+ return -1;
+ }
+ }
+ }
+ int slot = FindFreeFileSlot();
+
+ if (slot != -1) {
+ CirclePath circlePath(file);
+ CircleFile &newFile = fileTab[slot];
+
+ int result;
+ if (masked_flags == O_RDONLY) {
+ result = f_open(&newFile.file, circlePath.path, FA_READ);
+ } else if (masked_flags == O_WRONLY) {
+ result = f_open(&newFile.file, circlePath.path,
+ FA_WRITE | FA_CREATE_ALWAYS);
+ } else {
+ assert(masked_flags == O_RDWR);
+ // Note: We open read only because this will be slurped and changed
+ // in memory.
+ result = f_open(&newFile.file, circlePath.path, FA_READ);
+ }
+
+ if (result != FR_OK) {
+ errno = EACCES;
+ return -1;
+ }
+
+ newFile.fopen_called = 1;
+ newFile.contents = nullptr;
+ newFile.position = 0;
+ newFile.size = 0;
+ newFile.allocated = 0;
+ newFile.mode = masked_flags;
+ newFile.written_to = 0;
+ strcpy(newFile.fname, circlePath.path);
+
+ // When file is opened O_RDWR, slurp it into memory.
+ if (masked_flags == O_RDWR) {
+ if (slurp_file(newFile)) {
+ errno = ENFILE;
+ return -1;
+ }
+ if (f_close(&newFile.file) != FR_OK) {
+ errno = ENFILE;
+ return -1;
+ }
+ }
+
+ newFile.in_use = 1;
+ } else {
+ errno = ENFILE;
+ }
+
+ return slot;
}
-void CGlueStdioInit (CFATFileSystem& rFATFileSystem)
-{
- CGlueInitFileSystem (rFATFileSystem);
+extern "C" int _close(int fildes) {
+ if (fildes < 0 || static_cast<unsigned int>(fildes) >= MAX_OPEN_FILES) {
+ errno = EBADF;
+ return -1;
+ }
+
+ CircleFile &file = fileTab[fildes];
+ if (!file.in_use) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (file.contents) {
+ // Only open if something was actually written to memory
+ if (file.mode == O_RDWR && file.written_to) {
+ // Assert FIL is not used
+ file.fopen_called = 1;
+ if (f_open(&file.file, file.fname,
+ FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) {
+ // We won't be able to flush in memory changes back to disk.
+ }
+ }
+
+ // Always flush to disk for WRONLY but only if written to for RDRW
+ if ((file.mode == O_RDWR && file.written_to) || file.mode == O_WRONLY) {
+ // Dump contents of memory buffer to actual file.
+ unsigned int num_written;
+ if (f_write(&file.file, file.contents,
+ file.size, &num_written) != FR_OK) {
+ // Can't write new file or modified file contents back to disk.
+ }
+ }
+ }
+
+ int need_close = file.fopen_called;
+
+ file.allocated = 0;
+ file.size = 0;
+ file.mode = 0;
+ file.in_use = 0;
+ file.written_to = 0;
+ file.fopen_called = 0;
+ file.fname[0] = '\0';
+
+ if (file.contents) {
+ free(file.contents);
+ file.contents = nullptr;
+ }
+
+ // If we opened for RDWR but never wrote, nothing do to.
+ if (need_close && f_close(&file.file) != FR_OK) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
}
-void CGlueStdioInit (CConsole& rConsole)
-{
- CGlueInitConsole (rConsole);
+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.in_use) {
+ errno = EBADF;
+ return -1;
+ }
+
+ unsigned int num_read;
+ if (file.contents == nullptr) {
+ // Assert file.FIL has been opened
+ // else EBADF -1
+
+ // Read data from the file
+ if (f_read(&file.file, ptr, len, &num_read) != FR_OK) {
+ errno = EIO;
+ return -1;
+ }
+
+ file.position += num_read;
+ return static_cast<int>(num_read);
+ } else {
+ // Read data from our internal buffer
+ unsigned int max = len;
+ unsigned int remain = file.size - file.position;
+
+ if (max > remain) {
+ max = remain;
+ }
+
+ if (max > 0) {
+ memcpy(ptr, file.contents + file.position, max);
+ file.position += max;
+ }
+ return static_cast<int>(max);
+ }
}
-extern "C"
-int
-_open(char *file, int flags, int mode)
-{
- int slot = -1;
-
- // Only supported modes are read and write. The mask is
- // determined from the newlib header.
- int const masked_flags = flags & 7;
- if (masked_flags != O_RDONLY && masked_flags != O_WRONLY)
- {
- errno = ENOSYS;
- }
- else
- {
- slot = FindFreeFileSlot();
-
- if (slot != -1)
- {
- CircleFile& newFile = fileTab[slot];
- unsigned handle;
- if (masked_flags == O_RDONLY)
- {
- handle = circle_fat_fs->FileOpen (file);
- }
- else
- {
- assert(masked_flags == O_WRONLY);
- handle = circle_fat_fs->FileCreate (file);
- }
- if (handle != 0)
- {
- newFile.mCGlueIO = new CGlueIoFatFs(*circle_fat_fs, handle);
- }
- else
- {
- slot = -1;
- errno = EACCES;
- }
- }
- else
- {
- errno = ENFILE;
- }
- }
-
- return slot;
+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;
+ }
+
+ if (fildes == 1 || fildes == 2) {
+ if (g_serial) {
+ return g_serial->Write(ptr, len);
+ }
+ return len;
+ }
+
+ CircleFile &file = fileTab[fildes];
+ if (!file.in_use) {
+ errno = EBADF;
+ return -1;
+ }
+
+ // Mark this dirty so it will be flushed from memory to disk on close
+ file.written_to = 1;
+
+ // Nothing allocated yet? Allocate now.
+ if (file.contents == nullptr) {
+ file.allocated = READ_BUF_SIZE;
+ file.contents = (char *) malloc(file.allocated);
+ }
+
+ // Make sure we always have enough room allocated for the
+ // next write.
+ while (file.position + len >= file.allocated) {
+ file.allocated *= 2;
+ file.contents = (char *)realloc(file.contents, file.allocated);
+ }
+
+ // Do the write.
+ memcpy(file.contents + file.position, ptr, len);
+ file.position += len;
+ if (file.position > file.size) {
+ file.size = file.position;
+ }
+
+ return len;
}
-extern "C"
-int
-_close(int fildes)
-{
- 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;
- }
-
- unsigned const circle_close_result = file.mCGlueIO->Close();
-
- delete file.mCGlueIO;
- file.mCGlueIO = nullptr;
-
- if (circle_close_result == 0)
- {
- errno = EIO;
- return -1;
- }
-
- return 0;
+extern "C" DIR *opendir(const char *name) {
+ CirclePath circlePath(name);
+
+ int const slotNum = FindFreeDirSlot();
+ if (slotNum == -1) {
+ errno = ENFILE;
+ return 0;
+ }
+
+ CircleDir &slot = dirTab[slotNum];
+ if (f_opendir(&slot.dir, circlePath.path) != FR_OK) {
+ errno = ENFILE;
+ return 0;
+ }
+
+ slot.in_use = 1;
+ return &slot.dir;
}
-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;
- }
-
- unsigned const read_result = file.mCGlueIO->Read(ptr, static_cast<unsigned>(len));
-
- if (read_result == CGlueIO::GeneralFailure)
- {
- errno = EIO;
- return -1;
- }
-
- return static_cast<int>(read_result);
+static struct dirent *do_readdir(CircleDir *dir, struct dirent *de) {
+
+ assert(dir->in_use);
+
+ FILINFO fno;
+ struct dirent *result = nullptr;
+
+ FRESULT res = f_findnext(&dir->dir, &fno);
+ if (res == FR_OK && fno.fname[0] != 0) {
+ strcpy(de->d_name, fno.fname);
+ de->d_ino = 0;
+ de->d_type = 0;
+ if (fno.fattrib & AM_DIR) {
+ de->d_type |= DT_DIR;
+ } else {
+ de->d_type |= DT_REG;
+ }
+ result = de;
+ }
+
+ return result;
}
-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;
- }
-
- unsigned const write_result = file.mCGlueIO->Write(ptr, static_cast<unsigned>(len));
-
- if (write_result == CGlueIO::GeneralFailure)
- {
- errno = EIO;
- return -1;
- }
-
- return static_cast<int>(write_result);
+extern "C" struct dirent *readdir(DIR *dir) {
+ struct dirent *result;
+
+ CircleDir *c_dir = FindCircleDirFromDIR(dir);
+ if (c_dir == nullptr) {
+ errno = EBADF;
+ return nullptr;
+ }
+
+ return do_readdir(c_dir, &c_dir->mEntry);
}
-extern "C"
-DIR *
-opendir (const char *name)
-{
- assert (circle_fat_fs);
+extern "C" int readdir_r(DIR *__restrict dir, dirent *__restrict de,
+ dirent **__restrict ode) {
+ int result;
+ CircleDir *c_dir = FindCircleDirFromDIR(dir);
- /* For now only the single root directory and the current directory are supported */
- if (strcmp(name, "/") != 0 && strcmp(name, ".") != 0)
- {
- errno = ENOENT;
- return 0;
- }
+ if (c_dir == nullptr) {
+ *ode = nullptr;
+ result = EBADF;
+ } else {
+ *ode = do_readdir(c_dir, de);
+ result = 0;
+ }
- int const slotNum = FindFreeDirSlot ();
- if (slotNum == -1)
- {
- errno = ENFILE;
- return 0;
- }
+ return result;
+}
+
+extern "C" void rewinddir(DIR *dir) { f_rewinddir(dir); }
- auto &slot = dirTab[slotNum];
+extern "C" int closedir(DIR *dir) {
+ CircleDir *c_dir = FindCircleDirFromDIR(dir);
+ if (c_dir == nullptr) {
+ errno = EBADF;
+ return -1;
+ }
- slot.mOpen = 1;
- slot.mFirstRead = 1;
+ c_dir->in_use = 0;
- return &slot;
+ if (f_closedir(dir) != FR_OK) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
}
-static struct dirent *
-do_readdir (DIR *dir, struct dirent *de)
-{
- TDirentry Direntry;
- bool haveEntry;
- if (dir->mFirstRead)
- {
- haveEntry = circle_fat_fs->RootFindFirst (&Direntry, &dir->mCurrentEntry);
- dir->mFirstRead = 0;
+extern "C" int _stat(const char *file, struct stat *st) {
+ CirclePath circlePath(file);
+ memset(st, 0, sizeof(struct stat));
+
+ // Fastfail or fastsucceed
+ for (int i=0;i<g_bootStatNum;i++) {
+ if (g_bootStatWhat[i] == BOOTSTAT_WHAT_STAT) {
+ if (strend(circlePath.path, g_bootStatFile[i])) {
+ st->st_mode = S_IFREG | S_IREAD | S_IWRITE;
+ st->st_size = g_bootStatSize[i];
+ return 0;
}
- else
- {
- haveEntry = circle_fat_fs->RootFindNext (&Direntry, &dir->mCurrentEntry);
+ }
+ else if (g_bootStatWhat[i] == BOOTSTAT_WHAT_FAIL) {
+ if (strend(circlePath.path, g_bootStatFile[i])) {
+ errno = EBADF;
+ return -1;
}
+ }
+ }
+
+ FILINFO fno;
+ if (f_stat(circlePath.path, &fno) == FR_OK) {
+ if (fno.fattrib & AM_DIR) {
+ st->st_mode |= S_IFDIR;
+ } else {
+ st->st_mode |= S_IFREG;
+ }
+ if (fno.fattrib & AM_RDO) {
+ st->st_mode |= S_IREAD;
+ } else {
+ st->st_mode |= S_IREAD | S_IWRITE;
+ }
- struct dirent *result;
- if (haveEntry)
- {
- memcpy (de->d_name, Direntry.chTitle, sizeof(de->d_name));
- de->d_ino = 0; // TODO: how to determine an inode number in Circle?
- result = de;
- }
- else
- {
- // end of directory does not change errno
- result = nullptr;
- }
+ st->st_size = fno.fsize;
+ return 0;
+ }
- return result;
+ errno = EBADF;
+ return -1;
}
-extern "C" struct dirent *
-readdir (DIR *dir)
-{