-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSPIFlashFileSystem.device.lib.nut
1070 lines (859 loc) · 35.7 KB
/
SPIFlashFileSystem.device.lib.nut
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
// MIT License
//
// Copyright 2015-2019 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
const SPIFLASHFILESYSTEM_SPIFLASH_VERIFY = 1; // Don't verify = 0, Post Verify = 1, Pre Verify = 2, Pre and Post Verify = 3
enum SPIFLASHFILESYSTEM_SIZE {
SECTOR = 4096,
PAGE = 4096,
MAX_FNAME = 64,
TIMESTAMP = 4,
HEADER = 6 // id (2) + span (2) + size (2)
}
enum SPIFLASHFILESYSTEM_STATUS {
FREE,
USED,
ERASED,
BAD
}
enum SPIFLASHFILESYSTEM_ERROR {
FILE_OPEN = "Cannot perform operation with file(s) open.",
FILE_CLOSED = "Can not perform operation with closed file.",
FILE_NOT_FOUND = "The requested file does not exist.",
FILE_EXISTS = "Cannot (w)rite to an existing file.",
FILE_WRITE_R = "Cannot write to file with mode 'r'.",
UNKNOWN_MODE = "Tried opening file with unknown mode.",
VALIDATION = "Error validating SPI Flash write operation.",
INVALID_SPIFLASH_ADDRESS = "Tried writing to an invalid location.",
INVALID_WRITE_DATA = "Can only write blobs and strings to files.",
NO_FREE_SPACE = "File system out of space.",
INVALID_FILENAME = "Invalid filename.",
INVALID_PARAMETERS = "Invalid parameters.",
ERASE_OPEN_FILE = "Can't call eraseFiles() with open files"
}
class SPIFlashFileSystem {
// Library version
static VERSION = "3.0.1";
// Private:
_flash = null; // The SPI Flash object
_size = null; // The size of the SPI Flash
_start = null; // First byte of SPIFlash allocated to file system
_end = null; // Last byte of SPIFlash allocated to file system
_len = null; // Size of File System
_pages = 0; // Number of pages available in the File system
_fat = null; // The File Allocation Table
_openFiles = null; // Array of open files
_nextFileIdx = 1; // Next ID to use for files
_autoGcThreshold = 4; // If we fall below _autoGCThreshold # of free pages, we start GC
_collecting = false; // Flag to indicate an async gc is in progress
// Creates Filesystem objects, but does not initialize
constructor(start = null, end = null, flash = null) {
// Set the SPIFlash object (hardware.spiflash, or an object with equivalent interface)
_flash = flash ? flash : hardware.spiflash;
// Get the size of the spiflash
_flash.enable();
_size = _flash.size();
_flash.disable();
// Set start/end values
_start = start ? start : 0;
_end = end ? end : _size;
// Validate start/end values:
// Start is in the SPIFlash, and on a sector boundary
if (_start < 0 || _start >= _size || _start % SPIFLASHFILESYSTEM_SIZE.SECTOR != 0) throw SPIFLASHFILESYSTEM_ERROR.INVALID_SPIFLASH_ADDRESS;
// _end is after start, in the SPIFlash, and on a sector boundary
if (_end <= _start || (_end - _start) > _size || _end % SPIFLASHFILESYSTEM_SIZE.SECTOR != 0) throw SPIFLASHFILESYSTEM_ERROR.INVALID_SPIFLASH_ADDRESS;
// Calculate size of file system
_len = _end - _start;
_pages = _len / SPIFLASHFILESYSTEM_SIZE.PAGE;
// Initialize the filesystem
_openFiles = {};
_fat = SPIFlashFileSystem.FAT(this, _pages);
}
// Initializes File System / Builds FAT
function init(callback = null) {
// Make sure there aren't any open files:
if (_openFiles.len() > 0) throw SPIFLASHFILESYSTEM_ERROR.FILE_OPEN;
// Free up some memory
_fat = null;
// Scan the pages for files
_fat = SPIFlashFileSystem.FAT(this);
// if there was no callback, we're done
if (callback) {
// Otherwise, invoke callback against each file
callback(getFileList());
}
}
// Erases the portion of the SPIFlash dedicated to the fs
function eraseAll() {
// Can't erase if there's open files?
if (_openFiles.len() > 0) throw SPIFLASHFILESYSTEM_ERROR.FILE_OPEN;
_fat = SPIFlashFileSystem.FAT(this, _pages);
_flash.enable();
for (local p = 0; p < _pages; p++) {
local page = _start + (p * SPIFLASHFILESYSTEM_SIZE.SECTOR);
_flash.erasesector(page);
}
_flash.disable();
}
// Erases a single file
function eraseFile(fname) {
if (!_fat.fileExists(fname)) throw SPIFLASHFILESYSTEM_ERROR.FILE_NOT_FOUND;
if (isFileOpen(fname)) throw SPIFLASHFILESYSTEM_ERROR.FILE_OPEN;
// Build a blob to zero out the file
local zeros = blob(SPIFLASHFILESYSTEM_SIZE.HEADER + SPIFLASHFILESYSTEM_SIZE.TIMESTAMP + SPIFLASHFILESYSTEM_SIZE.MAX_FNAME + 1);
_flash.enable();
local pages = _fat.forEachPage(fname, function(addr) {
// Erase the page headers
local res = _flash.write(addr, zeros, SPIFLASHFILESYSTEM_SPIFLASH_VERIFY);
if (res != 0) {
_flash.disable();
throw SPIFLASHFILESYSTEM_ERROR.VALIDATION;
}
// Mark the page map
_fat.markPage(addr, SPIFLASHFILESYSTEM_STATUS.ERASED);
}.bindenv(this));
_flash.disable();
// Update the fat
_fat.removeFile(fname);
// Run the garbage collector if needed
_autoGc();
}
// Erases all files
function eraseFiles() {
if (_openFiles.len() > 0) throw SPIFLASHFILESYSTEM_ERROR.ERASE_OPEN_FILE;
_flash.enable();
local files_to_erase = _fat.getFileList();
foreach (file in files_to_erase) {
eraseFile(file.fname);
}
_flash.disable();
}
// Opens a file to (r)ead, (w)rite, or (a)ppend
function open(fname, mode) {
// Validate filename
if (typeof fname != "string" || fname.len() == 0 || fname.len() > SPIFLASHFILESYSTEM_SIZE.MAX_FNAME) throw SPIFLASHFILESYSTEM_ERROR.INVALID_FILENAME;
// Validate operation
if ((mode == "r") && !_fat.fileExists(fname)) throw SPIFLASHFILESYSTEM_ERROR.FILE_NOT_FOUND;
else if ((mode == "w") && _fat.fileExists(fname)) throw SPIFLASHFILESYSTEM_ERROR.FILE_EXISTS;
else if (mode != "r" && mode != "w") throw SPIFLASHFILESYSTEM_ERROR.UNKNOWN_MODE;
// Create a new file pointer from the FAT or a new file
local fileId = _fat.getFileId(fname);
local fileIdx = _nextFileIdx++;
_openFiles[fileIdx] <- fileId;
// Return a new file object
return SPIFlashFileSystem.File(this, fileId, fileIdx, fname, mode);
}
//-------------------- Utility Methods --------------------//
// Returns an array of file objects containing: { "id": int, "fname": string, "size": int }
// The files are order by date stamp or file name
function getFileList(orderByDate = false) {
return _fat.getFileList(orderByDate);
}
// Returns true if a file is open, false otherwise
function isFileOpen(fname) {
// Search through the open file table for matching ids
local id = _fat.get(fname).id;
foreach (ptr, fileId in _openFiles) {
if (id == fileId) return true;
}
return false;
}
// Returns true if a file exists, false otherwise
function fileExists(fname) {
return _fat.fileExists(fname);
}
// Returns the size of a file
function fileSize(fileRef) {
return _fat.get(fileRef).sizeTotal;
}
// Returns a table with the dimensions of the File System
function dimensions() {
return { "size": _size, "len": _len, "start": _start, "end": _end, "pages": _pages }
}
// Returns the created time stamp
function created(fileRef) {
return _fat.get(fileRef).created;
}
function getFreeSpace() {
// Smaller files have more overhead than larger files so its impossible to know exactly how much space is free.
// This is a total guess of how much space an average sector would have for data.
local stats = _fat.getStats();
const page_size_guess = 4000;
local free = stats.free * page_size_guess;
local freeable = (stats.free + stats.erased) * page_size_guess;
return { free = free, freeable = freeable };
}
//-------------------- GARBAGE COLLECTION --------------------//
// Sets the Garbage Collection threshold
function setAutoGc(maxPages) {
// Override the default auto garbage collection settings
if (maxPages != null) _autoGcThreshold = maxPages;
}
function gc(numPages = null) {
// Don't auto-garbage if we're already in the process of doing so
if (numPages == null && _collecting == true) return;
// Scan the headers of each page, working out what is in each
local collected = 0;
local sectorMap = _fat.getSectorMap().tostring()
local maplen = sectorMap.len();
// Asynchronous (auto-gc)
if (numPages == null) {
_collecting = true;
imp.wakeup(0, function() { _gc(sectorMap, 0); }.bindenv(this));
return;
}
// Synchronous
local firstSector = math.rand() % maplen;
for(local sector = 0; sector < maplen; sector++) {
// If the sector is dirty
local thisSector = (sector + firstSector) % maplen;
if (sectorMap[thisSector] == SPIFLASHFILESYSTEM_STATUS.ERASED || sectorMap[thisSector] == SPIFLASHFILESYSTEM_STATUS.BAD) {
// Erase the sector and mark the map
local addr = _start + thisSector*SPIFLASHFILESYSTEM_SIZE.SECTOR
_flash.enable();
_flash.erasesector(addr);
_fat.markPage(addr, SPIFLASHFILESYSTEM_STATUS.FREE);
_flash.disable();
collected++;
// If we've collected enough pages, we're done
if (numPages != null && collected >= numPages) break;
}
}
}
// Recursive asynchronous Garbage Collections
function _gc(sectorMap, sector, collected = 0) {
// Base case: we're at the end
if (sector >= sectorMap.len()) {
_collecting = false;
return;
}
// If the sector is dirty
if (sectorMap[sector] == SPIFLASHFILESYSTEM_STATUS.ERASED || sectorMap[sector] == SPIFLASHFILESYSTEM_STATUS.BAD) {
// Erase the sector and mark the map
local addr = _start + sector*SPIFLASHFILESYSTEM_SIZE.SECTOR
_flash.enable();
_flash.erasesector(addr);
_fat.markPage(addr, SPIFLASHFILESYSTEM_STATUS.FREE);
_flash.disable();
collected++;
}
// Collect the next dirty page
imp.wakeup(0, function() { _gc(sectorMap, sector+1, collected); }.bindenv(this));
}
// Checks whether we want to Garbage Collect, and starts process if we do
function _autoGc() {
// Don't garbage collect if there's open file, or user has turned off gc
if (_openFiles.len() > 0 || _autoGcThreshold <= 0) return;
// Is it worth gc'ing? If so, start it.
local _fatStats = _fat.getStats();
if (!_collecting && _fatStats.free <= _autoGcThreshold && _fatStats.erased > 0) {
gc();
}
}
//-------------------- PRIVATE METHODS --------------------//
// Closes a file, sets size, etc
function _close(fileId, fileIdx, dirty) {
// We have changes to write to disk
if (dirty) {
local file = _fat.get(fileId);
// Write the last span's size to disk
file.pages.seek(-2, 'e');
local page = file.pages.readn('w') * SPIFLASHFILESYSTEM_SIZE.PAGE;
file.sizes.seek(-2, 'e');
local size = file.sizes.readn('w');
_writeSize(page, size);
}
// Now drop the file pointer;
delete _openFiles[fileIdx];
// Auto garbage collect if required
_autoGc()
}
// Writes a string or blob to a file
function _write(fileId, addr, data) {
local type = typeof data;
if (type != "string" && type != "blob") throw SPIFLASHFILESYSTEM_ERROR.INVALID_WRITE_DATA;
// Turn strings into blobs
if (type == "string") {
local data_t = blob(data.len());
data_t.writestring(data);
data = data_t;
data.seek(0);
}
// Get the file object
local file = _fat.get(fileId);
// Write the data to free pages, one page at a time
local writtenToPage = 0, writtenFromData = 0;
while (!data.eos()) {
// If we need a new page
if (addr % SPIFLASHFILESYSTEM_SIZE.PAGE == 0) {
// Find and record the next page
try {
addr = _fat.getFreePage();
} catch (e) {
// No free pages, try garbage collection and then die if its still a problem
gc();
addr = _fat.getFreePage();
}
// Update file with new page in FAT
_fat.addPage(file.id, addr)
file.pageCount++;
file.spans++;
}
// Write to the page without the size
local info = _writePage(addr, data, file.id, file.spans, file.fname);
// If we are in the middle of a page then add the changes
_fat.addSizeToLastSpan(fileId, info.writtenFromData);
// Advance the pointers/counters
addr += info.writtenToPage;
data.seek(info.writtenFromData, 'c')
writtenFromData += info.writtenFromData;
writtenToPage += info.writtenToPage;
// Go back and write the size of the previous page
if (addr % SPIFLASHFILESYSTEM_SIZE.PAGE == 0) {
// if we are at the end of the page we can just write 0
_writeSize(addr - SPIFLASHFILESYSTEM_SIZE.PAGE, 0);
}
}
// Update the FAT
_fat.set(fileId, file);
// Return a summary of the _write action
return { "writtenFromData": writtenFromData, "writtenToPage": writtenToPage, "addr": addr };
}
// Writes a single page of a file
function _writePage(addr, data, id, span, fname, size = 0xFFFF) {
if (addr >= _end) throw SPIFLASHFILESYSTEM_ERROR.INVALID_SPIFLASH_ADDRESS;
// Figure out how much space is left in current page
local remInPage = SPIFLASHFILESYSTEM_SIZE.PAGE - (addr % SPIFLASHFILESYSTEM_SIZE.PAGE);
// Figure outhow much more data we need to write
local remInData = (data == null) ? 0 : data.len() - data.tell();
// Track how much we wrote
local writtenFromData = 0; // How much of "data" we wrote
local writtenToPage = 0; // How much we wrote to the page (including header)
// Mark the page as used
_fat.markPage(addr, SPIFLASHFILESYSTEM_STATUS.USED);
// If we're at the start of a page
if (remInPage == SPIFLASHFILESYSTEM_SIZE.PAGE) {
// Create and write the header
local headerBlob = blob(SPIFLASHFILESYSTEM_SIZE.HEADER)
headerBlob.writen(id, 'w');
headerBlob.writen(span, 'w');
headerBlob.writen(size, 'w');
// If this is a zero-index page (start of file)
if (span == 0) {
// Write the time stamp
headerBlob.writen(time(), 'i');
// Truncate and write the filename
headerBlob.writen(fname.len(), 'b');
if (fname.len() > 0) {
headerBlob.writestring(fname);
}
}
// write the header
_flash.enable();
local res = _flash.write(addr, headerBlob, SPIFLASHFILESYSTEM_SPIFLASH_VERIFY);
_flash.disable();
// Validate write
if (res != 0) throw SPIFLASHFILESYSTEM_ERROR.VALIDATION;
// Record how much we have written
local dataWritten = headerBlob.len();
// Update pointer information
addr += dataWritten;
remInPage -= dataWritten;
writtenToPage += dataWritten;
}
if (remInData > 0) {
// Work out how much to write - the lesser of the remaining in the page and the remaining in the data
local dataToWrite = (remInData < remInPage) ? remInData : remInPage;
// Write the data
_flash.enable();
local res = _flash.write(addr, data, SPIFLASHFILESYSTEM_SPIFLASH_VERIFY, data.tell(), data.tell() + dataToWrite);
_flash.disable();
// Validate write
if (res != 0) throw SPIFLASHFILESYSTEM_ERROR.VALIDATION;
// Update pointer information
addr += dataToWrite;
remInPage -= dataToWrite;
remInData -= dataToWrite;
writtenFromData += dataToWrite;
writtenToPage += dataToWrite;
}
// Return object with summary of what we wrote
return {
"remInPage": remInPage,
"remInData": remInData,
"writtenFromData": writtenFromData,
"writtenToPage": writtenToPage
};
}
// Writes the size of a file to the header
function _writeSize(addr, size) {
if (addr >= _end || addr % SPIFLASHFILESYSTEM_SIZE.PAGE != 0) throw SPIFLASHFILESYSTEM_ERROR.INVALID_SPIFLASH_ADDRESS;
local headerBlob = blob(SPIFLASHFILESYSTEM_SIZE.HEADER)
headerBlob.writen(0xFFFF, 'w'); // the id
headerBlob.writen(0xFFFF, 'w'); // The span
headerBlob.writen(size, 'w');
// Write it
_flash.enable();
local res = _flash.write(addr, headerBlob); // Verification will fail
_flash.disable();
if (res != 0) throw SPIFLASHFILESYSTEM_ERROR.FILE_EXISTS;
}
// Reads a file (or portion of a file)
function _read(fileId, start, len = null) {
// Get the file
local file = _fat.get(fileId);
// Create the result object
local result = blob();
// Fix the default length to everything
if (len == null) len = file.sizeTotal - start;
// find the initial address
local next = start, togo = len, pos = 0, page = null, size = null;
// Reset blob pointers for pages/sizes
file.pages.seek(0);
file.sizes.seek(0);
// Read all the pages
while (!file.pages.eos()) {
page = file.pages.readn('w') * SPIFLASHFILESYSTEM_SIZE.PAGE;
size = file.sizes.readn('w');
if (next < pos + size) {
// Read the data
local data = _readPage(page, true, next - pos, togo);
data.data.seek(0);
result.writeblob(data.data);
// Have we got everything?
togo -= data.data.len();
next += data.data.len();
if (togo == 0) break;
}
// Advance pointers
pos += size;
}
return result;
}
// Reads a single page of a file
function _readPage(addr, readData = false, from = 0, len = null) {
if (addr >= _end) throw SPIFLASHFILESYSTEM_ERROR.INVALID_SPIFLASH_ADDRESS;
_flash.enable();
// Read the header
local headerBlob = _flash.read(addr, SPIFLASHFILESYSTEM_SIZE.HEADER + SPIFLASHFILESYSTEM_SIZE.MAX_FNAME + SPIFLASHFILESYSTEM_SIZE.TIMESTAMP + 1);
// Parse the header
local pageData = {
"id": headerBlob.readn('w'),
"span": headerBlob.readn('w'),
"size": headerBlob.readn('w'),
"fname": null,
"created": null
};
if (pageData.span == 0) {
// Read the created timestamp
pageData.created = headerBlob.readn('i');
// Read the filename
local fnameLen = headerBlob.readn('b');
if (fnameLen > 0 && fnameLen <= SPIFLASHFILESYSTEM_SIZE.MAX_FNAME) {
pageData.fname = headerBlob.readstring(fnameLen);
}
}
// Correct the size
local maxSize = SPIFLASHFILESYSTEM_SIZE.PAGE - headerBlob.tell();
if ((pageData.span != 0xFFFF && pageData.id != 0)
&& (pageData.size == 0 || pageData.size > maxSize)) {
pageData.size = maxSize;
}
pageData.eof <- headerBlob.tell() + pageData.size;
// Read the data if required
if (readData) {
local dataOffset = headerBlob.tell();
if (from + len > pageData.size) len = pageData.size - from;
pageData.data <- _flash.read(addr + dataOffset + from, len);
}
_flash.disable();
// Check the results
pageData.status <- SPIFLASHFILESYSTEM_STATUS.BAD;
if (pageData.id == 0xFFFF && pageData.span == 0xFFFF && pageData.size == 0xFFFF && pageData.fname == null) {
pageData.status = SPIFLASHFILESYSTEM_STATUS.FREE; // Unwritten Page
} else if (pageData.id == 0 && pageData.span == 0 && pageData.size == 0 && pageData.fname == null) {
pageData.status = SPIFLASHFILESYSTEM_STATUS.ERASED; // Erased Page
} else if (pageData.id > 0 && pageData.id < 0xFFFF && pageData.span == 0 && pageData.size < 0xFFFF && pageData.fname != null) {
pageData.status = SPIFLASHFILESYSTEM_STATUS.USED; // Header Page (span = 0)
} else if (pageData.id > 0 && pageData.id < 0xFFFF && pageData.span > 0 && pageData.span < 0xFFFF && pageData.fname == null) {
pageData.status = SPIFLASHFILESYSTEM_STATUS.USED; // Normal Page
} else {
// NOOP - Broken Page?
}
return pageData;
}
}
class SPIFlashFileSystem.FAT {
_filesystem = null;
_names = null;
_pages = null;
_sizes = null;
_spans = null;
_creates = null;
_map = null;
_nextId = 1;
//--------------------------------------------------------------------------
constructor(filesystem, pages = null) {
// Mapping of fileId to pages, sizes and spanIds
_pages = {};
_sizes = {};
_spans = {};
_creates = {};
// Mapping of filename to fileId
_names = {};
// Store the parent file system object
_filesystem = filesystem;
if (typeof pages == "integer") {
// Make a new, empty page map
_map = blob(pages);
for (local i = 0; i < _map.len(); i++) {
_map[i] = SPIFLASHFILESYSTEM_STATUS.FREE;
}
} else {
// Build a new FAT by scanning the disk
scan();
}
}
// Scans the file system for FAT entries
function scan() {
local _pages_t = {};
local _sizes_t = {};
local dim = _filesystem.dimensions();
_map = blob(dim.pages);
local _flash = _filesystem._flash;
_flash.enable();
// Scan the headers of each page, working out what is in each
for (local p = 0; p < dim.pages; p++) {
local page = dim.start + (p * SPIFLASHFILESYSTEM_SIZE.PAGE);
local header = _filesystem._readPage(page, false);
// Record this page's status
_map.writen(header.status, 'b');
if (header.status == SPIFLASHFILESYSTEM_STATUS.USED) {
// Add the span to the fat
if (!(header.id in _pages_t)) {
_pages_t[header.id] <- {};
_sizes_t[header.id] <- {};
_creates[header.id] <- 0;
_spans[header.id] <- -1;
}
_pages_t[header.id][header.span] <- page;
_sizes_t[header.id][header.span] <- header.size;
if (header.created != null) _creates[header.id] <- header.created;
if (header.fname != null) _names[header.fname] <- header.id;
// Work out the highest spanId and fileId
if (header.span > _spans[header.id]) _spans[header.id] = header.span;
if (header.id >= _nextId) _nextId = header.id + 1;
}
}
_flash.disable();
// Pull the file details out and make a more efficient FAT
foreach (fileName,fileId in _names) {
// Save the pages as a single blob
_pages[fileId] <- blob(_pages_t[fileId].len() * 2);
local pages = pagesOrderedBySpan(_pages_t[fileId]);
foreach (page in pages) {
_pages[fileId].writen(page / SPIFLASHFILESYSTEM_SIZE.PAGE, 'w');
}
pages = null;
// Save the sizes
_sizes[fileId] <- blob(_sizes_t[fileId].len() * 2);
local sizes = pagesOrderedBySpan(_sizes_t[fileId]);
foreach (size in sizes) {
_sizes[fileId].writen(size, 'w');
}
sizes = null;
}
}
// Gets the FAT information for a specified file reference (id or name)
function get(fileRef) {
local fileId = null, fname = null;
// If a filename was passed
if (typeof fileRef == "string") {
fname = fileRef;
// Get the fileId
if (fname in _names) {
fileId = _names[fname];
}
} else {
// If a fileId was passed
fileId = fileRef;
// Get the filename
foreach (filename,id in _names) {
if (fileId == id) {
fname = filename;
break;
}
}
}
// Check the file is valid
if (fileId == null || fname == null) throw SPIFLASHFILESYSTEM_ERROR.FILE_NOT_FOUND;
// Add up the sizes
local sizeTotal = 0, size = 0;
_sizes[fileId].seek(0);
while (!_sizes[fileId].eos()) {
sizeTotal += _sizes[fileId].readn('w');
}
// Return the file object
return {
"id": fileId,
"fname": fname,
"spans": _spans[fileId],
"pages": _pages[fileId],
"pageCount": _pages[fileId].len() / 2,
"sizes": _sizes[fileId],
"sizeTotal": sizeTotal,
"created": _creates[fileId]
};
}
// Returns a simplified list of files for the dev to use
function getFileList(orderByDate = false) {
local list = [];
foreach (fname,id in _names) {
local file = get(fname);
list.push({
"id": file.id,
"fname": file.fname,
"size": file.sizeTotal,
"created": file.created
});
}
// Order the files
if (orderByDate) {
list.sort(function(a, b) { return a.created <=> b.created }.bindenv(this));
} else {
list.sort(function(a, b) { return a.fname <=> b.fname }.bindenv(this));
}
return list;
}
// Sets the spans array for the specified file id
function set(fileId, file) {
_spans[fileId] = file.spans;
}
// Returns the fileId for a specified filename
// + creates file if doesn't already exist
function getFileId(filename) {
// Create the file if it doesn't exist
if (!fileExists(filename)) {
// Create a new file
_names[filename] <- _nextId;
_pages[_nextId] <- blob();
_sizes[_nextId] <- blob();
_spans[_nextId] <- -1;
_creates[_nextId] <- time();
_nextId = (_nextId + 1) % 65535 + 1; // 1 ... 64k-1
}
// Return the fileId
return _names[filename];
}
// Returns true when a file exists in the FAT, false otherwise
function fileExists(fileRef) {
// Check the file is valid
return ((fileRef in _pages) || (fileRef in _names));
}
// Returns address of a random free page (or throws error if out of space)
function getFreePage() {
local map = _map.tostring();
// Find a random free page
local randStart = math.rand() % _map.len();
local next = map.find(SPIFLASHFILESYSTEM_STATUS.FREE.tochar(), randStart);
// If we didn't a free page from the random midpoint, start again from the beginning
if (next == null) {
next = map.find(SPIFLASHFILESYSTEM_STATUS.FREE.tochar());
}
// If there were 0 free pages, garbage collect, and start again from the beginning
if (next == null) {
_filesystem.gc(2 * _filesystem._autoGcThreshold);
next = map.find(SPIFLASHFILESYSTEM_STATUS.FREE.tochar());
}
// If we haven't found one after garbage collection, theow error
if (next == null) throw SPIFLASHFILESYSTEM_ERROR.NO_FREE_SPACE;
// If we did find a page, return it
return _filesystem.dimensions().start + (next * SPIFLASHFILESYSTEM_SIZE.PAGE);
}
// Updates a page to the specified status (free, used, erased, bad)
function markPage(addr, status) {
// Ammend the page map
local i = (addr - _filesystem.dimensions().start) / SPIFLASHFILESYSTEM_SIZE.PAGE;
_map[i] = status;
}
// Returns a reference to the sector map (_map)
function getSectorMap() {
return _map;
}
// Returns # of each type of page in fs (free, used, erased, bad)
function getStats() {
local stats = { "free": 0, "used": 0, "erased": 0, "bad": 0 };
local map = _map.tostring();
foreach (ch in map) {
switch (ch) {
case SPIFLASHFILESYSTEM_STATUS.FREE: stats.free++; break;
case SPIFLASHFILESYSTEM_STATUS.USED: stats.used++; break;
case SPIFLASHFILESYSTEM_STATUS.ERASED: stats.erased++; break;
case SPIFLASHFILESYSTEM_STATUS.BAD: stats.bad++; break;
}
}
return stats;
}
// Adds a page to the FAT for a specified file
function addPage(fileId, page) {
// Append the page
get(fileId).pages.writen(page / SPIFLASHFILESYSTEM_SIZE.PAGE, 'w')
get(fileId).sizes.writen(0, 'w');
}
// Updates the size of the last span in a file
function addSizeToLastSpan(fileId, bytes) {
// Read the last span's size, add the value and rewrite it
local sizes = get(fileId).sizes;
sizes.seek(-2, 'e');
local size = sizes.readn('w') + bytes
sizes.seek(-2, 'e');
sizes.writen(size, 'w');
}
// Returns the number of pages for a specified file
function getPageCount(fileRef) {
return get(fileRef).pages.len() / 2; // Each pageId is 2 bytes
}
// Removes a file from the FAT
function removeFile(fname) {
// Check the file is valid
if (!fileExists(fname)) throw SPIFLASHFILESYSTEM_ERROR.FILE_NOT_FOUND;
// Get the fileId
local id = _names[fname];
// Remove information from the FAT
delete _names[fname];
delete _pages[id];
delete _sizes[id];
delete _spans[id];
delete _creates[id];
}
// Iterates over each page of the specified file, and invokes the callback
function forEachPage(fileRef, callback) {
// Find the pages
local pages = get(fileRef).pages;
// Loop through the pages, calling the callback for each one
pages.seek(0);
while (!pages.eos()) {
local page = pages.readn('w') * SPIFLASHFILESYSTEM_SIZE.PAGE;
callback(page);
}
}
// Returns an array of pages ordered by the span
function pagesOrderedBySpan(pages) {
// Load the table contents into an array
local interim = [];
foreach (s,p in pages) {
interim.push({ s = s, p = p });
}
// Sort the array by the span
interim.sort(function(first, second) {
return first.s <=> second.s;
});
// Write them to a final array without the key
local result = [];
foreach (i in interim) {
result.push(i.p);
}
return result;
}
// Debugging function to server.log FAT information
function describe() {
server.log(format("FAT contained %d files", _names.len()))
foreach (fname,id in _names) {
server.log(format(" File: %s, spans: %d, bytes: %d", fname, getPageCount(id), get(id).sizeTotal))
}
}
}
class SPIFlashFileSystem.File {
_filesystem = null;
_fileIdx = null;
_fileId = null;
_fname = null;
_mode = null;
_pos = 0;
_wpos = 0;
_waddr = 0;
_dirty = false;
_isClosed = false;
constructor(filesystem, fileId, fileIdx, fname, mode) {
_filesystem = filesystem;
_fileIdx = fileIdx;
_fileId = fileId;
_fname = fname;