InfiniTime.git

ref: e5b73212f6addcfdb5e306df63d7135e543c4f8d

src/components/fs/FS.h


 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
#pragma once

#include <cstdint>
#include "drivers/SpiNorFlash.h"
#include <littlefs/lfs.h>

namespace Pinetime {
  namespace Controllers {
    class FS {
    public:
      FS(Pinetime::Drivers::SpiNorFlash&);

      void Init();

      int FileOpen(lfs_file_t* file_p, const char* fileName, const int flags);
      int FileClose(lfs_file_t* file_p);
      int FileRead(lfs_file_t* file_p, uint8_t* buff, uint32_t size);
      int FileWrite(lfs_file_t* file_p, const uint8_t* buff, uint32_t size);
      int FileSeek(lfs_file_t* file_p, uint32_t pos);

      int FileDelete(const char* fileName);

      int DirOpen(const char* path, lfs_dir_t* lfs_dir);
      int DirClose(lfs_dir_t* lfs_dir);
      int DirRead(lfs_dir_t* dir, lfs_info* info);
      int DirRewind(lfs_dir_t* dir);
      int DirCreate(const char* path);

      lfs_ssize_t GetFSSize();
      int Rename(const char* oldPath, const char* newPath);
      int Stat(const char* path, lfs_info* info);
      void VerifyResource();

      static size_t getSize() {
        return size;
      }

      static size_t getBlockSize() {
        return blockSize;
      }

    private:
      Pinetime::Drivers::SpiNorFlash& flashDriver;

      /*
       * External Flash MAP (4 MBytes)
       *
       * 0x000000 +---------------------------------------+
       *          |  Bootloader Assets                    |
       *          |  256 KBytes                           |
       *          |                                       |
       * 0x040000 +---------------------------------------+
       *          |  OTA                                  |
       *          |  464 KBytes                           |
       *          |                                       |
       *          |                                       |
       *          |                                       |
       * 0x0B4000 +---------------------------------------+
       *          |  File System                          |
       *          |                                       |
       *          |                                       |
       *          |                                       |
       *          |                                       |
       * 0x400000 +---------------------------------------+
       *
       */
      static constexpr size_t startAddress = 0x0B4000;
      static constexpr size_t size = 0x34C000;
      static constexpr size_t blockSize = 4096;

      bool resourcesValid = false;
      const struct lfs_config lfsConfig;

      lfs_t lfs;

      static int SectorSync(const struct lfs_config* c);
      static int SectorErase(const struct lfs_config* c, lfs_block_t block);
      static int SectorProg(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, const void* buffer, lfs_size_t size);
      static int SectorRead(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size);
    };
  }
}