File: MicroBitFileSystem.h

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (520 lines) | stat: -rw-r--r-- 18,003 bytes parent folder | download | duplicates (3)
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
/*
The MIT License (MIT)
Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.
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.
*/

#ifndef MICROBIT_FILE_SYSTEM_H
#define MICROBIT_FILE_SYSTEM_H

#include "MicroBitConfig.h"
#include "MicroBitFlash.h"


// Configuration options.
#define MBFS_FILENAME_LENGTH        16        
#define MBFS_MAGIC                  "MICROBIT_FS_1_0"

// open() flags.
#define MB_READ     0x01
#define MB_WRITE    0x02
#define MB_CREAT    0x04
#define MB_APPEND   0x08

// seek() flags.
#define MB_SEEK_SET 0x01
#define MB_SEEK_END 0x02
#define MB_SEEK_CUR 0x04

// Status flags
#define MBFS_STATUS_INITIALISED           0x01

// FileTable codes
#define MBFS_UNUSED                       0xFFFF
#define MBFS_EOF                          0xEFFF
#define MBFS_DELETED                      0x0000

// DirectorEntry flags
#define MBFS_DIRECTORY_ENTRY_FREE         0x8000
#define MBFS_DIRECTORY_ENTRY_VALID        0x4000
#define MBFS_DIRECTORY_ENTRY_DIRECTORY    0x2000
#define MBFS_DIRECTORY_ENTRY_NEW          0xffff
#define MBFS_DIRECTORY_ENTRY_DELETED      0x0000

// Enumeration of BLOCK YPES
#define MBFS_BLOCK_TYPE_FILE              1
#define MBFS_BLOCK_TYPE_DIRECTORY         2
#define MBFS_BLOCK_TYPE_FILETABLE         3


//
// Every file in the file system has a file descriptor.
// These are held in directory entries, using the following
// structure.
//
struct DirectoryEntry
{
    char file_name[MBFS_FILENAME_LENGTH];       // Name of the file.
    uint16_t first_block;                       // Logical block address of the start of the file.
    uint16_t flags;                             // Status of the file.
    uint32_t length;                            // Length of the file in bytes.
};

//
// A directory is a list of DirectoryEntry structures:
//
struct Directory
{
    DirectoryEntry entry[0];
};

//
// A FileDescriptor holds contextual information needed for each OPEN file.
//
struct FileDescriptor
{
    // read/write/creat flags.
    uint16_t flags;

    // FileDescriptor id
    uint16_t id;

    // current file position, in bytes.
    uint32_t seek;

    // the current file size. n.b. this may be different to that stored in the DirectoryEntry.
    uint32_t length;

    // the directory entry of this file. 
    DirectoryEntry *dirent;

    // the directory entry of our parent directory. 
    DirectoryEntry *directory;

    // We maintain a chain of open file descriptors. Reference to the next FileDescriptor in the chain.
    FileDescriptor *next;

    // Optional writeback cache, to minimise FLASH write operations at the expense of RAM.
    uint16_t cacheLength;
    uint8_t cache[MBFS_CACHE_SIZE];
};

/**
  * @brief Class definition for the MicroBit File system
  *
  * Microbit file system class. Presents a POSIX-like interface consisting of:
  * - open()
  * - close()
  * - read()
  * - write()
  * - seek()
  * - remove()
  *
  * Only a single instance shoud exist at any given time.
  */
class MicroBitFileSystem
{
    private:

    // Status flags
    uint32_t status;

    // The instance of MicroBitFlash - the interface used for all flash writes/erasures
    MicroBitFlash flash;

    // Total Number of logical pages available for file data (including the file table)
    int    fileSystemSize;

    // Memory address of the start of the file system.
    uint16_t* fileSystemTable;

    // Size of the file table (blocks)
    uint16_t fileSystemTableSize;

    // Cache of the last block allocated. Used to enable round robin use of blocks.
    uint16_t lastBlockAllocated;

    // Reference to the root directory of the file system.
    DirectoryEntry *rootDirectory;

    // Chain of open files.
    FileDescriptor *openFiles;

    /**
      * Initialize the flash storage system
      *
      * The file system is located dynamically, based on where the program code
      * and code data finishes. This avoids having to allocate a fixed flash
      * region for builds even without MicroBitFileSystem. 
      *      
      * This method checks if the file system already exists, and loads it. 
      * If not, it will determines the optimal size of the file system, if necessary, and format the space
      *
      * @return MICROBIT_OK on success, or an error code.
      */
    int init(uint32_t flashStart, int flashPages);

    /**
      * Attempts to detect and load an existing file system.
      *
      * @return MICROBIT_OK on success, or MICROBIT_NO_DATA if the file system could not be found.
      */
    int load();

    /**
      * Allocate a free logical block.
      * A round robin algorithm is used to even out the wear on the physical device.
      * @return NULL on error, page address on success
      */
    uint16_t getFreeBlock();

    /**
    * Allocates a free physical block.
    * A round robin algorithm is used to even out the wear on the physical device.
    * @return NULL on error, page address on success
    */
    uint32_t* getFreePage();

    /**
    * Retrieve the DirectoryEntry assoiated with the given file's DIRECTORY (not the file itself).
    *
    * @param filename A fully qualified filename, from the root.
    * @return A pointer to the DirectoryEntry for the given file's directory, or NULL if no entry is found.
    */
    DirectoryEntry* getDirectoryOf(char const * filename);

    /**
    * Retrieve the DirectoryEntry for the given filename.
    *
    * @param filename A fully or partially qualified filename.
    * @param directory The directory to search. If ommitted, the root directory will be used.
    * @return A pointer to the DirectoryEntry for the given file, or NULL if no entry is found.
    */
    DirectoryEntry* getDirectoryEntry(char const * filename, const DirectoryEntry *directory = NULL);
    
    /**
    * Create a new DirectoryEntry with the given filename and flags.
    *
    * @param filename A fully or partially qualified filename.
    * @param directory The directory in which to create the entry
    * @param isDirectory true if the entry being created is itself a directory
    *
    * @return A pointer to the new DirectoryEntry for the given file, or NULL if it was not possible to allocated resources.
    */
    DirectoryEntry* createFile(char const * filename, DirectoryEntry *directory, bool isDirectory);

    /**
    * Allocate a free DiretoryEntry in the given directory, extending and refreshing the directory block if necessary.
    *
    * @param directory The directory to add a DirectoryEntry to
    * @return A pointer to the new DirectoryEntry for the given file, or NULL if it was not possible to allocated resources.
    */
    DirectoryEntry* createDirectoryEntry(DirectoryEntry *directory);

    /**
    * Refresh the physical page associated with the given block.
    * Any logical blocks marked for deletion on that page are recycled.
    *
    * @param block the block to recycle.
    * @param type One of MBFS_BLOCK_TYPE_FILE, MBFS_BLOCK_TYPE_DIRECTORY, MBFS_BLOCK_TYPE_FILETABLE. 
    * Erases and regenerates the given block, recycling any data marked for deletion.
    * @return MICROBIT_OK on success.
    */
    int recycleBlock(uint16_t block, int type = MBFS_BLOCK_TYPE_FILE);

    /**
    * Refresh the physical pages associated with the file table.
    * Any logical blocks marked for deletion on those pages are recycled back to UNUSED.
    *
    * @return MICROBIT_OK on success.
    */
    int recycleFileTable();

    /**
    * Retrieve a memory pointer for the start of the physical memory page containing the given block.
    *
    * @param block A valid block number.
    *
    * @return A pointer to the physical page in FLASH memory holding the given block.
    */
    uint32_t *getPage(uint16_t block);

    /**
    * Retrieve a memory pointer for the start of the given block.
    *
    * @param block A valid block number.
    *
    * @return A pointer to the FLASH memory associated with the given block.
    */
    uint32_t *getBlock(uint16_t block);

    /**
    * Retrieve the next block in a chain.
    *
    * @param block A valid block number.
    *
    * @return The block number of the next block in the file.
    */
    uint16_t getNextFileBlock(uint16_t block);

    /**
    * Determine the logical block that contains the given address.
    *
    * @param address A valid memory location within the file system space.
    *
    * @return The block number containing the given address.
    */
    uint16_t getBlockNumber(void *address);

    /**
    * Determine the number of logical blocks required to hold the file table.
    *
    * @return The number of logical blocks required to hold the file table.
    */
    uint16_t calculateFileTableSize();

    /*
    * Update a file table entry to a given value.
    *
    * @param block The block to update.
    * @param value The value to store in the file table.
    * @return MICROBIT_OK on success.
    */
    int fileTableWrite(uint16_t block, uint16_t value);

    /**
    * Searches the list of open files for one with the given identifier.
    *
    * @param fd A previsouly opened file identifier, as returned by open().
    * @param remove Remove the file descriptor from the list if true.
    * @return A FileDescriptor matching the given ID, or NULL if the file is not open.
    */
    FileDescriptor* getFileDescriptor(int fd, bool remove = false);

    /**
    * Initialises a new file system
    *
    * @return MICROBIT_OK on success, or an error code.
    */
    int format();

    /**
      * Flush a given file's cache back to FLASH memory.
      *
      * @param file File descriptor to flush.
      * @return The number of bytes written.
      *
      */
    int writeBack(FileDescriptor *file);

    /**
      * Write a given buffer to the file provided.
      * 
      * @param file FileDescriptor of the file to write
      * @param buffer The start of the buffer to write
      * @param length The number of bytes to write
      * @return The number of bytes written.
      */
    int writeBuffer(FileDescriptor *file, uint8_t* buffer, int length);


    /**
     * Determines if the given filename is a valid filename for use in MicroBitFileSystem. 
     * valid filenames must be >0 characters in lenght, NULL temrinated and contain
     * only printable characters.
     *
     * @param name The name of the file to test.
     * @return true if the filename is valid, false otherwsie.
     */
    bool isValidFilename(const char *name);

    public:

    static MicroBitFileSystem *defaultFileSystem;

    /**
      * Constructor. Creates an instance of a MicroBitFileSystem.
      */
    MicroBitFileSystem(uint32_t flashStart = 0, int flashPages = 0);

    /**
      * Open a new file, and obtain a new file handle (int) to
      * read/write/seek the file. The flags are:
      *  - MB_READ : read from the file.
      *  - MB_WRITE : write to the file.
      *  - MB_CREAT : create a new file, if it doesn't already exist.
      *
      * If a file is opened that doesn't exist, and MB_CREAT isn't passed,
      * an error is returned, otherwise the file is created.
      *
      * @param filename name of the file to open, must contain only printable characters.
      * @param flags One or more of MB_READ, MB_WRITE or MB_CREAT. 
      * @return return the file handle,MICROBIT_NOT_SUPPORTED if the file system has
      *         not been initialised MICROBIT_INVALID_PARAMETER if the filename is
      *         too large, MICROBIT_NO_RESOURCES if the file system is full.
      *
      * @code
      * MicroBitFileSystem f();
      * int fd = f.open("test.txt", MB_WRITE|MB_CREAT);
      * if(fd<0)
      *    print("file open error");
      * @endcode
      */
    int open(char const * filename, uint32_t flags);

    /**
     * Writes back all state associated with the given file to FLASH memory, 
     * leaving the file open.
     *
     * @param fd file descriptor - obtained with open().
     * @return MICROBIT_OK on success, MICROBIT_NOT_SUPPORTED if the file system has not
     *         been initialised, MICROBIT_INVALID_PARAMETER if the given file handle
     *         is invalid.
     *
     * @code
     * MicroBitFileSystem f();
     * int fd = f.open("test.txt", MB_READ);
     *
     * ...
     *
     * f.flush(fd);
     * @endcode
     */
    int flush(int fd);

    /**
      * Close the specified file handle.
      * File handle resources are then made available for future open() calls.
      *
      * close() must be called to ensure all pending data is written back to FLASH memory. 
      *
      * @param fd file descriptor - obtained with open().
      * @return non-zero on success, MICROBIT_NOT_SUPPORTED if the file system has not
      *         been initialised, MICROBIT_INVALID_PARAMETER if the given file handle
      *         is invalid.
      *
      * @code
      * MicroBitFileSystem f();
      * int fd = f.open("test.txt", MB_READ);
      * if(!f.close(fd))
      *    print("error closing file.");
      * @endcode
      */
    int close(int fd);

    /**
      * Move the current position of a file handle, to be used for
      * subsequent read/write calls.
      *
      * The offset modifier can be:
      *  - MB_SEEK_SET set the absolute seek position.
      *  - MB_SEEK_CUR set the seek position based on the current offset.
      *  - MB_SEEK_END set the seek position from the end of the file.
      * E.g. to seek to 2nd-to-last byte, use offset=-1.
      *
      * @param fd file handle, obtained with open()
      * @param offset new offset, can be positive/negative.
      * @param flags
      * @return new offset position on success, MICROBIT_NOT_SUPPORTED if the file system
      *         is not intiialised, MICROBIT_INVALID_PARAMETER if the flag given is invalid
      *         or the file handle is invalid.
      *
      * @code
      * MicroBitFileSystem f;
      * int fd = f.open("test.txt", MB_READ);
      * f.seek(fd, -100, MB_SEEK_END); //100 bytes before end of file.
      * @endcode
      */
    int seek(int fd, int offset, uint8_t flags);

    /**
      * Write data to the file.
      *
      * Write from buffer, length bytes to the current seek position.
      * On each invocation to write, the seek position of the file handle
      * is incremented atomically, by the number of bytes returned.
      *
      * @param fd File handle
      * @param buffer the buffer from which to write data
      * @param size number of bytes to write
      * @return number of bytes written on success, MICROBIT_NO_RESOURCES if data did
      *         not get written to flash or the file system has not been initialised,
      *         or this file was not opened with the MB_WRITE flag set, MICROBIT_INVALID_PARAMETER
      *         if the given file handle is invalid.
      *
      * @code
      * MicroBitFileSystem f();
      * int fd = f.open("test.txt", MB_WRITE);
      * if(f.write(fd, "hello!", 7) != 7)
      *    print("error writing");
      * @endcode
      */
    int write(int fd, uint8_t* buffer, int size);

    /**
      * Read data from the file.
      *
      * Read len bytes from the current seek position in the file, into the
      * buffer. On each invocation to read, the seek position of the file
      * handle is incremented atomically, by the number of bytes returned.
      *
      * @param fd File handle, obtained with open()
      * @param buffer to store data
      * @param size number of bytes to read
      * @return number of bytes read on success, MICROBIT_NOT_SUPPORTED if the file
      *         system is not initialised, or this file was not opened with the
      *         MB_READ flag set, MICROBIT_INVALID_PARAMETER if the given file handle
      *         is invalid.
      *
      * @code
      * MicroBitFileSystem f;
      * int fd = f.open("read.txt", MB_READ);
      * if(f.read(fd, buffer, 100) != 100)
      *    print("read error");
      * @endcode
      */
    int read(int fd, uint8_t* buffer, int size);

    /**
      * Remove a file from the system, and free allocated assets
      * (including assigned blocks which are returned for use by other files).
      *
      * @todo the file must not already have an open file handle.
      *
      * @param filename name of the file to remove.
      * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the given filename
      *         does not exist, MICROBIT_CANCELLED if something went wrong
      *
      * @code
      * MicroBitFileSystem f;
      * if(!f.remove("file.txt"))
      *     printf("file could not be removed");
      * @endcode
      */
    int remove(char const * filename);

    /**
    * Creates a new directory with the given name and location
    *
    * @param name The fully qualified name of the new directory.
    * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the path is invalid, or MICROBT_NO_RESOURCES if the FileSystem is full.
    */
    int createDirectory(char const *name);
};

#endif