spiffs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #include <stdio.h>
  2. #include "platform.h"
  3. #include "spiffs.h"
  4. /*
  5. * With the intoduction of a unified FatFS and SPIFFS support (#1397), the SPIFFS
  6. * interface is now abstracted through a uses a single SPIFFS entry point
  7. * myspiffs_realm() which returns a vfs_fs_fns object (as does myfatfs_realm()).
  8. * All other functions and data are static.
  9. *
  10. * Non-OS SDK V3.0 introduces a flash partition table (PT) and SPIFFS has now been
  11. * updated to support this:
  12. * - SPIFFS limits search to the specifed SPIFFS0 address and size.
  13. * - Any headroom / offset from other partitions is reflected in the PT allocations.
  14. * - Unforced mounts will attempt to mount any valid SPIFSS found in this range
  15. * (NodeMCU uses the SPIFFS_USE_MAGIC setting to make existing FS discoverable).
  16. * - Subject to the following, no offset or FS search is done. The FS is assumed
  17. * to be at the first valid location at the start of the partition.
  18. */
  19. #include "spiffs_nucleus.h"
  20. static spiffs fs;
  21. #define LOG_PAGE_SIZE 256
  22. #define LOG_BLOCK_SIZE (INTERNAL_FLASH_SECTOR_SIZE * 2)
  23. #define LOG_BLOCK_SIZE_SMALL_FS (INTERNAL_FLASH_SECTOR_SIZE)
  24. #define MIN_BLOCKS_FS 4
  25. #define MASK_1MB (0x100000-1)
  26. #define ALIGN (0x2000)
  27. static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2];
  28. static u8_t spiffs_fds[sizeof(spiffs_fd) * SPIFFS_MAX_OPEN_FILES];
  29. #if SPIFFS_CACHE
  30. static u8_t myspiffs_cache[20 + (LOG_PAGE_SIZE+20)*4];
  31. #endif
  32. static s32_t my_spiffs_read(u32_t addr, u32_t size, u8_t *dst) {
  33. platform_flash_read(dst, addr, size);
  34. return SPIFFS_OK;
  35. }
  36. static s32_t my_spiffs_write(u32_t addr, u32_t size, u8_t *src) {
  37. platform_flash_write(src, addr, size);
  38. return SPIFFS_OK;
  39. }
  40. static int erase_cnt = -1; // If set to >=0 then erasing gives a ... feedback
  41. static s32_t my_spiffs_erase(u32_t addr, u32_t size) {
  42. u32_t sect_first = platform_flash_get_sector_of_address(addr);
  43. u32_t sect_last = sect_first;
  44. while( sect_first <= sect_last ) {
  45. if (erase_cnt >= 0 && (erase_cnt++ & 0xF) == 0) {
  46. dbg_printf(".");
  47. }
  48. if( platform_flash_erase_sector( sect_first ++ ) == PLATFORM_ERR ) {
  49. return SPIFFS_ERR_INTERNAL;
  50. }
  51. }
  52. return SPIFFS_OK;
  53. }
  54. void myspiffs_check_callback(spiffs_check_type type, spiffs_check_report report, u32_t arg1, u32_t arg2){
  55. // if(SPIFFS_CHECK_PROGRESS == report) return;
  56. // NODE_ERR("type: %d, report: %d, arg1: %d, arg2: %d\n", type, report, arg1, arg2);
  57. }
  58. /*******************
  59. * Note that the W25Q32BV array is organized into 16,384 programmable pages of 256-bytes 
  60. * each. Up to 256 bytes can be programmed at a time. Pages can be erased in groups of 
  61. * 16 (4KB sector erase), groups of 128 (32KB block erase), groups of 256 (64KB block 
  62. * erase) or the entire chip (chip erase). The W25Q32BV has 1,024 erasable sectors and 
  63. * 64 erasable blocks respectively. The small 4KB sectors allow for greater flexibility 
  64. * in applications that require data and parameter storage. 
  65. *
  66. * Returns TRUE if FS was found.
  67. */
  68. static bool myspiffs_set_cfg(spiffs_config *cfg, bool force_create) {
  69. uint32 pt_start, pt_size, pt_end;
  70. pt_size = platform_flash_get_partition (NODEMCU_SPIFFS0_PARTITION, &pt_start);
  71. if (pt_size == 0) {
  72. return FALSE;
  73. }
  74. pt_end = pt_start + pt_size;
  75. cfg->hal_read_f = my_spiffs_read;
  76. cfg->hal_write_f = my_spiffs_write;
  77. cfg->hal_erase_f = my_spiffs_erase;
  78. cfg->phys_erase_block = INTERNAL_FLASH_SECTOR_SIZE;
  79. cfg->log_page_size = LOG_PAGE_SIZE;
  80. cfg->phys_addr = (pt_start + ALIGN - 1) & ~(ALIGN - 1);
  81. cfg->phys_size = (pt_end & ~(ALIGN - 1)) - cfg->phys_addr;
  82. if (cfg->phys_size < MIN_BLOCKS_FS * LOG_BLOCK_SIZE_SMALL_FS) {
  83. return FALSE;
  84. } else if (cfg->phys_size < MIN_BLOCKS_FS * LOG_BLOCK_SIZE) {
  85. cfg->log_block_size = LOG_BLOCK_SIZE_SMALL_FS;
  86. } else {
  87. cfg->log_block_size = LOG_BLOCK_SIZE;
  88. }
  89. #ifdef SPIFFS_USE_MAGIC_LENGTH
  90. if (!force_create) {
  91. int size = SPIFFS_probe_fs(cfg);
  92. if (size > 0 && size < cfg->phys_size) {
  93. NODE_DBG("Overriding size:%x\n",size);
  94. cfg->phys_size = size;
  95. }
  96. if (size <= 0) {
  97. return FALSE;
  98. }
  99. }
  100. #endif
  101. NODE_DBG("myspiffs set cfg block: %x %x %x %x %x %x\n", pt_start, pt_end,
  102. cfg->phys_size, cfg->phys_addr, cfg->phys_size, cfg->log_block_size);
  103. return TRUE;
  104. }
  105. static bool myspiffs_mount(bool force_mount) {
  106. spiffs_config cfg;
  107. if (!myspiffs_set_cfg(&cfg, force_mount) && !force_mount) {
  108. return FALSE;
  109. }
  110. fs.err_code = 0;
  111. int res = SPIFFS_mount(&fs,
  112. &cfg,
  113. spiffs_work_buf,
  114. spiffs_fds,
  115. sizeof(spiffs_fds),
  116. #if SPIFFS_CACHE
  117. myspiffs_cache,
  118. sizeof(myspiffs_cache),
  119. #else
  120. 0, 0,
  121. #endif
  122. // myspiffs_check_callback);
  123. 0);
  124. NODE_DBG("mount res: %d, %d\n", res, fs.err_code);
  125. return res == SPIFFS_OK;
  126. }
  127. void myspiffs_unmount() {
  128. SPIFFS_unmount(&fs);
  129. }
  130. // FS formatting function
  131. // Returns 1 if OK, 0 for error
  132. int myspiffs_format( void )
  133. {
  134. SPIFFS_unmount(&fs);
  135. myspiffs_mount(TRUE);
  136. SPIFFS_unmount(&fs);
  137. NODE_DBG("Formatting: size 0x%x, addr 0x%x\n", fs.cfg.phys_size, fs.cfg.phys_addr);
  138. erase_cnt = 0;
  139. int status = SPIFFS_format(&fs);
  140. erase_cnt = -1;
  141. return status < 0 ? 0 : myspiffs_mount(FALSE);
  142. }
  143. // ***************************************************************************
  144. // vfs API
  145. // ***************************************************************************
  146. #include <stdlib.h>
  147. #include "vfs_int.h"
  148. #define MY_LDRV_ID "FLASH"
  149. // default current drive
  150. static int is_current_drive = TRUE;
  151. // forward declarations
  152. static sint32_t myspiffs_vfs_close( const struct vfs_file *fd );
  153. static sint32_t myspiffs_vfs_read( const struct vfs_file *fd, void *ptr, size_t len );
  154. static sint32_t myspiffs_vfs_write( const struct vfs_file *fd, const void *ptr, size_t len );
  155. static sint32_t myspiffs_vfs_lseek( const struct vfs_file *fd, sint32_t off, int whence );
  156. static sint32_t myspiffs_vfs_eof( const struct vfs_file *fd );
  157. static sint32_t myspiffs_vfs_tell( const struct vfs_file *fd );
  158. static sint32_t myspiffs_vfs_flush( const struct vfs_file *fd );
  159. static uint32_t myspiffs_vfs_size( const struct vfs_file *fd );
  160. static sint32_t myspiffs_vfs_ferrno( const struct vfs_file *fd );
  161. static sint32_t myspiffs_vfs_closedir( const struct vfs_dir *dd );
  162. static sint32_t myspiffs_vfs_readdir( const struct vfs_dir *dd, struct vfs_stat *buf );
  163. static vfs_vol *myspiffs_vfs_mount( const char *name, int num );
  164. static vfs_file *myspiffs_vfs_open( const char *name, const char *mode );
  165. static vfs_dir *myspiffs_vfs_opendir( const char *name );
  166. static sint32_t myspiffs_vfs_stat( const char *name, struct vfs_stat *buf );
  167. static sint32_t myspiffs_vfs_remove( const char *name );
  168. static sint32_t myspiffs_vfs_rename( const char *oldname, const char *newname );
  169. static sint32_t myspiffs_vfs_fsinfo( uint32_t *total, uint32_t *used );
  170. static sint32_t myspiffs_vfs_fscfg( uint32_t *phys_addr, uint32_t *phys_size );
  171. static sint32_t myspiffs_vfs_format( void );
  172. static sint32_t myspiffs_vfs_errno( void );
  173. static void myspiffs_vfs_clearerr( void );
  174. static sint32_t myspiffs_vfs_umount( const struct vfs_vol *vol );
  175. // ---------------------------------------------------------------------------
  176. // function tables
  177. //
  178. static vfs_fs_fns myspiffs_fs_fns = {
  179. .mount = myspiffs_vfs_mount,
  180. .open = myspiffs_vfs_open,
  181. .opendir = myspiffs_vfs_opendir,
  182. .stat = myspiffs_vfs_stat,
  183. .remove = myspiffs_vfs_remove,
  184. .rename = myspiffs_vfs_rename,
  185. .mkdir = NULL,
  186. .fsinfo = myspiffs_vfs_fsinfo,
  187. .fscfg = myspiffs_vfs_fscfg,
  188. .format = myspiffs_vfs_format,
  189. .chdrive = NULL,
  190. .chdir = NULL,
  191. .ferrno = myspiffs_vfs_errno,
  192. .clearerr = myspiffs_vfs_clearerr
  193. };
  194. static vfs_file_fns myspiffs_file_fns = {
  195. .close = myspiffs_vfs_close,
  196. .read = myspiffs_vfs_read,
  197. .write = myspiffs_vfs_write,
  198. .lseek = myspiffs_vfs_lseek,
  199. .eof = myspiffs_vfs_eof,
  200. .tell = myspiffs_vfs_tell,
  201. .flush = myspiffs_vfs_flush,
  202. .size = myspiffs_vfs_size,
  203. .ferrno = myspiffs_vfs_ferrno
  204. };
  205. static vfs_dir_fns myspiffs_dd_fns = {
  206. .close = myspiffs_vfs_closedir,
  207. .readdir = myspiffs_vfs_readdir
  208. };
  209. // ---------------------------------------------------------------------------
  210. // specific struct extensions
  211. //
  212. struct myvfs_file {
  213. struct vfs_file vfs_file;
  214. spiffs_file fh;
  215. };
  216. struct myvfs_dir {
  217. struct vfs_dir vfs_dir;
  218. spiffs_DIR d;
  219. };
  220. // ---------------------------------------------------------------------------
  221. // volume functions
  222. //
  223. static sint32_t myspiffs_vfs_umount( const struct vfs_vol *vol ) {
  224. // not implemented
  225. return VFS_RES_ERR;
  226. }
  227. // ---------------------------------------------------------------------------
  228. // dir functions
  229. //
  230. #define GET_DIR_D(descr) \
  231. const struct myvfs_dir *mydd = (const struct myvfs_dir *)descr; \
  232. spiffs_DIR *d = (spiffs_DIR *)&(mydd->d);
  233. static sint32_t myspiffs_vfs_closedir( const struct vfs_dir *dd ) {
  234. GET_DIR_D(dd);
  235. sint32_t res = SPIFFS_closedir( d );
  236. // free descriptor memory
  237. free( (void *)dd );
  238. }
  239. static sint32_t myspiffs_vfs_readdir( const struct vfs_dir *dd, struct vfs_stat *buf ) {
  240. GET_DIR_D(dd);
  241. struct spiffs_dirent dirent;
  242. if (SPIFFS_readdir( d, &dirent )) {
  243. memset( buf, 0, sizeof( struct vfs_stat ) );
  244. // copy entries to item
  245. // fill in supported stat entries
  246. strncpy( buf->name, dirent.name, FS_OBJ_NAME_LEN+1 );
  247. buf->name[FS_OBJ_NAME_LEN] = '\0';
  248. buf->size = dirent.size;
  249. return VFS_RES_OK;
  250. }
  251. return VFS_RES_ERR;
  252. }
  253. // ---------------------------------------------------------------------------
  254. // file functions
  255. //
  256. #define GET_FILE_FH(descr) \
  257. const struct myvfs_file *myfd = (const struct myvfs_file *)descr; \
  258. spiffs_file fh = myfd->fh;
  259. static sint32_t myspiffs_vfs_close( const struct vfs_file *fd ) {
  260. GET_FILE_FH(fd);
  261. sint32_t res = SPIFFS_close( &fs, fh );
  262. // free descriptor memory
  263. free( (void *)fd );
  264. return res;
  265. }
  266. static sint32_t myspiffs_vfs_read( const struct vfs_file *fd, void *ptr, size_t len ) {
  267. GET_FILE_FH(fd);
  268. sint32_t n = SPIFFS_read( &fs, fh, ptr, len );
  269. return n >= 0 ? n : VFS_RES_ERR;
  270. }
  271. static sint32_t myspiffs_vfs_write( const struct vfs_file *fd, const void *ptr, size_t len ) {
  272. GET_FILE_FH(fd);
  273. sint32_t n = SPIFFS_write( &fs, fh, (void *)ptr, len );
  274. return n >= 0 ? n : VFS_RES_ERR;
  275. }
  276. static sint32_t myspiffs_vfs_lseek( const struct vfs_file *fd, sint32_t off, int whence ) {
  277. GET_FILE_FH(fd);
  278. int spiffs_whence;
  279. switch (whence) {
  280. default:
  281. case VFS_SEEK_SET:
  282. spiffs_whence = SPIFFS_SEEK_SET;
  283. break;
  284. case VFS_SEEK_CUR:
  285. spiffs_whence = SPIFFS_SEEK_CUR;
  286. break;
  287. case VFS_SEEK_END:
  288. spiffs_whence = SPIFFS_SEEK_END;
  289. break;
  290. }
  291. sint32_t res = SPIFFS_lseek( &fs, fh, off, spiffs_whence );
  292. return res >= 0 ? res : VFS_RES_ERR;
  293. }
  294. static sint32_t myspiffs_vfs_eof( const struct vfs_file *fd ) {
  295. GET_FILE_FH(fd);
  296. return SPIFFS_eof( &fs, fh );
  297. }
  298. static sint32_t myspiffs_vfs_tell( const struct vfs_file *fd ) {
  299. GET_FILE_FH(fd);
  300. return SPIFFS_tell( &fs, fh );
  301. }
  302. static sint32_t myspiffs_vfs_flush( const struct vfs_file *fd ) {
  303. GET_FILE_FH(fd);
  304. return SPIFFS_fflush( &fs, fh ) >= 0 ? VFS_RES_OK : VFS_RES_ERR;
  305. }
  306. static uint32_t myspiffs_vfs_size( const struct vfs_file *fd ) {
  307. GET_FILE_FH(fd);
  308. int32_t curpos = SPIFFS_tell( &fs, fh );
  309. int32_t size = SPIFFS_lseek( &fs, fh, 0, SPIFFS_SEEK_END );
  310. (void) SPIFFS_lseek( &fs, fh, curpos, SPIFFS_SEEK_SET );
  311. return size;
  312. }
  313. static sint32_t myspiffs_vfs_ferrno( const struct vfs_file *fd ) {
  314. return SPIFFS_errno( &fs );
  315. }
  316. static int fs_mode2flag(const char *mode){
  317. if(strlen(mode)==1){
  318. if(strcmp(mode,"w")==0)
  319. return SPIFFS_WRONLY|SPIFFS_CREAT|SPIFFS_TRUNC;
  320. else if(strcmp(mode, "r")==0)
  321. return SPIFFS_RDONLY;
  322. else if(strcmp(mode, "a")==0)
  323. return SPIFFS_WRONLY|SPIFFS_CREAT|SPIFFS_APPEND;
  324. else
  325. return SPIFFS_RDONLY;
  326. } else if (strlen(mode)==2){
  327. if(strcmp(mode,"r+")==0)
  328. return SPIFFS_RDWR;
  329. else if(strcmp(mode, "w+")==0)
  330. return SPIFFS_RDWR|SPIFFS_CREAT|SPIFFS_TRUNC;
  331. else if(strcmp(mode, "a+")==0)
  332. return SPIFFS_RDWR|SPIFFS_CREAT|SPIFFS_APPEND;
  333. else
  334. return SPIFFS_RDONLY;
  335. } else {
  336. return SPIFFS_RDONLY;
  337. }
  338. }
  339. // ---------------------------------------------------------------------------
  340. // filesystem functions
  341. //
  342. static vfs_file *myspiffs_vfs_open( const char *name, const char *mode ) {
  343. struct myvfs_file *fd;
  344. int flags = fs_mode2flag( mode );
  345. if (fd = (struct myvfs_file *)malloc( sizeof( struct myvfs_file ) )) {
  346. if (0 < (fd->fh = SPIFFS_open( &fs, name, flags, 0 ))) {
  347. fd->vfs_file.fs_type = VFS_FS_SPIFFS;
  348. fd->vfs_file.fns = &myspiffs_file_fns;
  349. return (vfs_file *)fd;
  350. } else {
  351. free( fd );
  352. }
  353. }
  354. return NULL;
  355. }
  356. static vfs_dir *myspiffs_vfs_opendir( const char *name ){
  357. struct myvfs_dir *dd;
  358. if (dd = (struct myvfs_dir *)malloc( sizeof( struct myvfs_dir ) )) {
  359. if (SPIFFS_opendir( &fs, name, &(dd->d) )) {
  360. dd->vfs_dir.fs_type = VFS_FS_SPIFFS;
  361. dd->vfs_dir.fns = &myspiffs_dd_fns;
  362. return (vfs_dir *)dd;
  363. } else {
  364. free( dd );
  365. }
  366. }
  367. return NULL;
  368. }
  369. static sint32_t myspiffs_vfs_stat( const char *name, struct vfs_stat *buf ) {
  370. spiffs_stat stat;
  371. if (0 <= SPIFFS_stat( &fs, name, &stat )) {
  372. memset( buf, 0, sizeof( struct vfs_stat ) );
  373. // fill in supported stat entries
  374. strncpy( buf->name, stat.name, FS_OBJ_NAME_LEN+1 );
  375. buf->name[FS_OBJ_NAME_LEN] = '\0';
  376. buf->size = stat.size;
  377. return VFS_RES_OK;
  378. } else {
  379. return VFS_RES_ERR;
  380. }
  381. }
  382. static sint32_t myspiffs_vfs_remove( const char *name ) {
  383. return SPIFFS_remove( &fs, name );
  384. }
  385. static sint32_t myspiffs_vfs_rename( const char *oldname, const char *newname ) {
  386. return SPIFFS_rename( &fs, oldname, newname );
  387. }
  388. static sint32_t myspiffs_vfs_fsinfo( uint32_t *total, uint32_t *used ) {
  389. return SPIFFS_info( &fs, total, used );
  390. }
  391. static sint32_t myspiffs_vfs_fscfg( uint32_t *phys_addr, uint32_t *phys_size ) {
  392. *phys_addr = fs.cfg.phys_addr;
  393. *phys_size = fs.cfg.phys_size;
  394. return VFS_RES_OK;
  395. }
  396. static vfs_vol *myspiffs_vfs_mount( const char *name, int num ) {
  397. // volume descriptor not supported, just return TRUE / FALSE
  398. return myspiffs_mount(FALSE) ? (vfs_vol *)1 : NULL;
  399. }
  400. static sint32_t myspiffs_vfs_format( void ) {
  401. return myspiffs_format();
  402. }
  403. static sint32_t myspiffs_vfs_errno( void ) {
  404. return SPIFFS_errno( &fs );
  405. }
  406. static void myspiffs_vfs_clearerr( void ) {
  407. SPIFFS_clearerr( &fs );
  408. }
  409. // ---------------------------------------------------------------------------
  410. // VFS interface functions
  411. //
  412. vfs_fs_fns *myspiffs_realm( const char *inname, char **outname, int set_current_drive ) {
  413. if (inname[0] == '/') {
  414. // logical drive is specified, check if it's our id
  415. if (0 == strncmp(inname + 1, MY_LDRV_ID, sizeof(MY_LDRV_ID)-1)) {
  416. *outname = (char *)(inname + sizeof(MY_LDRV_ID));
  417. if (*outname[0] == '/') {
  418. // skip leading /
  419. (*outname)++;
  420. }
  421. if (set_current_drive) is_current_drive = TRUE;
  422. return &myspiffs_fs_fns;
  423. }
  424. } else {
  425. // no logical drive in patchspec, are we current drive?
  426. if (is_current_drive) {
  427. *outname = (char *)inname;
  428. return &myspiffs_fs_fns;
  429. }
  430. }
  431. if (set_current_drive) is_current_drive = FALSE;
  432. return NULL;
  433. }