Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : /* 16 : ArduPilot filesystem interface. This offsets a minimal subset of 17 : full functionality offered by posix type interfaces, meeting the 18 : needs of ArduPilot 19 : */ 20 : #pragma once 21 : 22 : #include <stdint.h> 23 : #include <AP_HAL/AP_HAL_Boards.h> 24 : 25 : #include "AP_Filesystem_config.h" 26 : 27 : #ifndef MAX_NAME_LEN 28 : #define MAX_NAME_LEN 255 29 : #endif 30 : 31 : #if (CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS) || (CONFIG_HAL_BOARD == HAL_BOARD_ESP32) 32 : #define DT_REG 0 33 : #define DT_DIR 1 34 : #define DT_LNK 10 35 : #endif 36 : 37 : #if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS 38 : #if AP_FILESYSTEM_FATFS_ENABLED 39 : #include "AP_Filesystem_FATFS.h" 40 : #endif 41 : #if AP_FILESYSTEM_LITTLEFS_ENABLED 42 : #include "AP_Filesystem_FlashMemory_LittleFS.h" 43 : #endif 44 : 45 : struct dirent { 46 : char d_name[MAX_NAME_LEN]; /* filename */ 47 : uint8_t d_type; 48 : }; 49 : 50 : #endif // HAL_BOARD_CHIBIOS 51 : 52 : #include <fcntl.h> 53 : #include <errno.h> 54 : #include <unistd.h> 55 : 56 : #ifndef AP_FILESYSTEM_FORMAT_ENABLED 57 : #define AP_FILESYSTEM_FORMAT_ENABLED 1 58 : #endif 59 : 60 : #if CONFIG_HAL_BOARD == HAL_BOARD_LINUX || CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_QURT 61 : #include "AP_Filesystem_posix.h" 62 : #if AP_FILESYSTEM_LITTLEFS_ENABLED 63 : #include "AP_Filesystem_FlashMemory_LittleFS.h" 64 : #endif 65 : #endif 66 : 67 : #if CONFIG_HAL_BOARD == HAL_BOARD_ESP32 68 : #include "AP_Filesystem_ESP32.h" 69 : #endif 70 : 71 : #include "AP_Filesystem_backend.h" 72 : 73 : // used by LittleFS 74 : #define AP_FILESYSTEM_FLASH_JEDEC_NOR 1 75 : #define AP_FILESYSTEM_FLASH_W25NXX 2 76 : 77 : #ifndef AP_FILESYSTEM_LITTLEFS_FLASH_TYPE 78 : #define AP_FILESYSTEM_LITTLEFS_FLASH_TYPE AP_FILESYSTEM_FLASH_JEDEC_NOR 79 : #endif 80 : 81 : #ifndef AP_FILESYSTEM_POSIX_MAP_FILENAME_ALLOC 82 : // this requires AP_FILESYSTEM_POSIX_MAP_FILENAME_BASEDIR 83 : #define AP_FILESYSTEM_POSIX_MAP_FILENAME_ALLOC 0 84 : #endif 85 : 86 : #ifndef AP_FILESYSTEM_HAVE_DIRENT_DTYPE 87 : #define AP_FILESYSTEM_HAVE_DIRENT_DTYPE 1 88 : #endif 89 : 90 : #ifndef AP_FATFS_MAX_IO_SIZE 91 : #define AP_FATFS_MAX_IO_SIZE 4096 92 : #endif 93 : 94 : #ifndef AP_FATFS_MIN_IO_SIZE 95 : #define AP_FATFS_MIN_IO_SIZE 4096 96 : #endif 97 : 98 : class AP_Filesystem { 99 : private: 100 : struct DirHandle { 101 : uint8_t fs_index; 102 : void *dir; 103 : }; 104 : 105 : public: 106 2193 : AP_Filesystem() {} 107 : 108 : // functions that closely match the equivalent posix calls 109 : int open(const char *fname, int flags, bool allow_absolute_paths = false); 110 : int close(int fd); 111 : int32_t read(int fd, void *buf, uint32_t count); 112 : int32_t write(int fd, const void *buf, uint32_t count); 113 : int fsync(int fd); 114 : int32_t lseek(int fd, int32_t offset, int whence); 115 : int stat(const char *pathname, struct stat *stbuf); 116 : 117 : // stat variant for scripting 118 : typedef struct Stat { 119 : uint32_t size; 120 : int32_t mode; 121 : uint32_t mtime; 122 : uint32_t atime; 123 : uint32_t ctime; 124 0 : bool is_directory(void) const { 125 0 : return (mode & S_IFMT) == S_IFDIR; 126 : } 127 : } stat_t; 128 : bool stat(const char *pathname, stat_t &stbuf); 129 : 130 : int unlink(const char *pathname); 131 : int mkdir(const char *pathname); 132 : int rename(const char *oldpath, const char *newpath); 133 : 134 : DirHandle *opendir(const char *pathname); 135 : struct dirent *readdir(DirHandle *dirp); 136 : int closedir(DirHandle *dirp); 137 : 138 : // return number of bytes that should be written before fsync for optimal 139 : // streaming performance/robustness. if zero, any number can be written. 140 : uint32_t bytes_until_fsync(int fd); 141 : 142 : // return free disk space in bytes, -1 on error 143 : int64_t disk_free(const char *path); 144 : 145 : // return total disk space in bytes, -1 on error 146 : int64_t disk_space(const char *path); 147 : 148 : // set modification time on a file 149 : bool set_mtime(const char *filename, const uint32_t mtime_sec); 150 : 151 : // if filesystem is not running then try a remount. Return true if fs is mounted 152 : bool retry_mount(void); 153 : 154 : // unmount filesystem for reboot 155 : void unmount(void); 156 : 157 : // reads a line into buf, guaranteeing null-termination. buflen 158 : // is the full size of buf, including the space required for the 159 : // null terminator, so up to buflen-1 characters are returned. cr 160 : // or lf terminates the line and is consumed but not returned. 161 : bool fgets(char *buf, uint8_t buflen, int fd); 162 : 163 : // run crc32 over file with given name, returns true if successful 164 : bool crc32(const char *fname, uint32_t& checksum) WARN_IF_UNUSED; 165 : 166 : // format filesystem. This is async, monitor get_format_status for progress 167 : bool format(void); 168 : 169 : // retrieve status of format process: 170 : AP_Filesystem_Backend::FormatStatus get_format_status() const; 171 : 172 : /* 173 : Load a file's contents into memory. Returned object must be `delete`d to 174 : free the data. The data is guaranteed to be null-terminated such that it 175 : can be treated as a string. 176 : */ 177 : FileData *load_file(const char *filename); 178 : 179 : // get_singleton for scripting 180 : static AP_Filesystem *get_singleton(void); 181 : 182 : private: 183 : struct Backend { 184 : const char *prefix; 185 : AP_Filesystem_Backend &fs; 186 : }; 187 : static const struct Backend backends[]; 188 : 189 : /* 190 : find backend by path 191 : */ 192 : const Backend &backend_by_path(const char *&path) const; 193 : 194 : /* 195 : find backend by open fd 196 : */ 197 : const Backend &backend_by_fd(int &fd) const; 198 : 199 : // support for listing out virtual directory entries (e.g. @SYS 200 : // then @MISSION) 201 : struct { 202 : uint8_t backend_ofs; 203 : struct dirent de; 204 : uint8_t d_off; 205 : } virtual_dirent; 206 : }; 207 : 208 : namespace AP { 209 : AP_Filesystem &FS(); 210 : }; 211 :