mirror of
https://github.com/DiUS/spiffsimg.git
synced 2025-05-08 20:40:34 +08:00
Work in progress.
This commit is contained in:
parent
23047b5a83
commit
96a8b2f5d8
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*.swp
|
||||||
|
spiffsimg
|
8
Makefile
Normal file
8
Makefile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
SRCS=\
|
||||||
|
main.c \
|
||||||
|
$(wildcard spiffs/src/*.c) \
|
||||||
|
|
||||||
|
CFLAGS=-Wall -Wextra -Wno-unused-parameter -Wno-unused-function -std=c99 -I. -Ispiffs/src
|
||||||
|
|
||||||
|
spiffsimg: $(SRCS)
|
||||||
|
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@
|
130
main.c
Normal file
130
main.c
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include "spiffs.h"
|
||||||
|
|
||||||
|
static spiffs fs;
|
||||||
|
static uint8_t *flash;
|
||||||
|
|
||||||
|
#define LOG_PAGE_SIZE 256
|
||||||
|
|
||||||
|
static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2];
|
||||||
|
static u8_t spiffs_fds[32*4];
|
||||||
|
|
||||||
|
static s32_t flash_read (u32_t addr, u32_t size, u8_t *dst) {
|
||||||
|
memcpy (dst, flash + addr, size);
|
||||||
|
return SPIFFS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static s32_t flash_write (u32_t addr, u32_t size, u8_t *src) {
|
||||||
|
memcpy (flash + addr, src, size);
|
||||||
|
return SPIFFS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static s32_t flash_erase (u32_t addr, u32_t size) {
|
||||||
|
memset (flash + addr, 0xff, size);
|
||||||
|
return SPIFFS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void die (const char *what)
|
||||||
|
{
|
||||||
|
perror (what);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int opt;
|
||||||
|
const char *fname = 0;
|
||||||
|
bool create = false;
|
||||||
|
enum { CMD_LIST, CMD_INTERACTIVE, CMD_SCRIPT } command;
|
||||||
|
size_t sz = 0;
|
||||||
|
while ((opt = getopt (argc, argv, "f:c:lir")) != -1)
|
||||||
|
{
|
||||||
|
switch (opt)
|
||||||
|
{
|
||||||
|
case 'f': fname = optarg; break;
|
||||||
|
case 'c': create = true; sz = strtoul (optarg, 0, 0); break;
|
||||||
|
case 'l': command = CMD_LIST; break;
|
||||||
|
case 'i': command = CMD_INTERACTIVE; break;
|
||||||
|
case 'r': command = CMD_SCRIPT; break;
|
||||||
|
default: die ("unknown option");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd = open (fname, (create ? (O_CREAT | O_TRUNC) : 0) | O_RDWR, 0660);
|
||||||
|
if (fd == -1)
|
||||||
|
die ("open");
|
||||||
|
|
||||||
|
if (create)
|
||||||
|
{
|
||||||
|
if (lseek (fd, sz -1, SEEK_SET) == -1)
|
||||||
|
die ("lseek");
|
||||||
|
if (write (fd, "", 1) != 1)
|
||||||
|
die ("write");
|
||||||
|
}
|
||||||
|
else if (!sz)
|
||||||
|
{
|
||||||
|
off_t offs = lseek (fd, 0, SEEK_END);
|
||||||
|
if (offs == -1)
|
||||||
|
die ("lseek");
|
||||||
|
sz = offs;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sz & (LOG_PAGE_SIZE -1))
|
||||||
|
die ("file size not multiple of page size");
|
||||||
|
|
||||||
|
flash = mmap (0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
|
if (!flash)
|
||||||
|
die ("mmap");
|
||||||
|
|
||||||
|
if (create)
|
||||||
|
memset (flash, 0xff, sz);
|
||||||
|
|
||||||
|
spiffs_config cfg;
|
||||||
|
cfg.phys_size = sz;
|
||||||
|
cfg.phys_addr = 0;
|
||||||
|
cfg.phys_erase_block = 0x1000;
|
||||||
|
cfg.log_block_size = 0x1000;
|
||||||
|
cfg.log_page_size = LOG_PAGE_SIZE;
|
||||||
|
cfg.hal_read_f = flash_read;
|
||||||
|
cfg.hal_write_f = flash_write;
|
||||||
|
cfg.hal_erase_f = flash_erase;
|
||||||
|
if (SPIFFS_mount (&fs, &cfg,
|
||||||
|
spiffs_work_buf,
|
||||||
|
spiffs_fds,
|
||||||
|
sizeof(spiffs_fds),
|
||||||
|
0, 0, 0) != 0)
|
||||||
|
die ("spiffs_mount");
|
||||||
|
|
||||||
|
// TODO: import/export support
|
||||||
|
spiffs_file fh = SPIFFS_open (&fs, "foo/bar", SPIFFS_CREAT | SPIFFS_RDWR, 0);
|
||||||
|
if (SPIFFS_write (&fs, fh, "hello, world\n", 12) < 0)
|
||||||
|
die ("spiffs_write");
|
||||||
|
SPIFFS_close (&fs, fh);
|
||||||
|
|
||||||
|
if (command == CMD_LIST)
|
||||||
|
{
|
||||||
|
spiffs_DIR dir;
|
||||||
|
if (!SPIFFS_opendir (&fs, "/", &dir))
|
||||||
|
die ("spiffs_opendir");
|
||||||
|
struct spiffs_dirent de;
|
||||||
|
while (SPIFFS_readdir (&dir, &de))
|
||||||
|
{
|
||||||
|
static const char types[] = "?fdhs"; // file, dir, hardlink, softlink
|
||||||
|
char name[sizeof(de.name)+1] = { 0 };
|
||||||
|
memcpy (name, de.name, sizeof(de.name));
|
||||||
|
printf("%c %6u %s\n", types[de.type], de.size, name);
|
||||||
|
}
|
||||||
|
SPIFFS_closedir (&dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
munmap (flash, sz);
|
||||||
|
return 0;
|
||||||
|
}
|
210
spiffs_config.h
Normal file
210
spiffs_config.h
Normal file
@ -0,0 +1,210 @@
|
|||||||
|
#ifndef SPIFFS_CONFIG_H_
|
||||||
|
#define SPIFFS_CONFIG_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef int32_t s32_t;
|
||||||
|
typedef uint32_t u32_t;
|
||||||
|
typedef int16_t s16_t;
|
||||||
|
typedef uint16_t u16_t;
|
||||||
|
typedef int8_t s8_t;
|
||||||
|
typedef uint8_t u8_t;
|
||||||
|
|
||||||
|
#define offsetof(type, member) __builtin_offsetof (type, member)
|
||||||
|
|
||||||
|
// compile time switches
|
||||||
|
|
||||||
|
// Set generic spiffs debug output call.
|
||||||
|
#ifndef SPIFFS_DBG
|
||||||
|
#define SPIFFS_DBG(...) //printf(__VA_ARGS__)
|
||||||
|
#endif
|
||||||
|
// Set spiffs debug output call for garbage collecting.
|
||||||
|
#ifndef SPIFFS_GC_DBG
|
||||||
|
#define SPIFFS_GC_DBG(...) //printf(__VA_ARGS__)
|
||||||
|
#endif
|
||||||
|
// Set spiffs debug output call for caching.
|
||||||
|
#ifndef SPIFFS_CACHE_DBG
|
||||||
|
#define SPIFFS_CACHE_DBG(...) //printf(__VA_ARGS__)
|
||||||
|
#endif
|
||||||
|
// Set spiffs debug output call for system consistency checks.
|
||||||
|
#ifndef SPIFFS_CHECK_DBG
|
||||||
|
#define SPIFFS_CHECK_DBG(...) //printf(__VA_ARGS__)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Enable/disable API functions to determine exact number of bytes
|
||||||
|
// for filedescriptor and cache buffers. Once decided for a configuration,
|
||||||
|
// this can be disabled to reduce flash.
|
||||||
|
#ifndef SPIFFS_BUFFER_HELP
|
||||||
|
#define SPIFFS_BUFFER_HELP 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Enables/disable memory read caching of nucleus file system operations.
|
||||||
|
// If enabled, memory area must be provided for cache in SPIFFS_mount.
|
||||||
|
#ifndef SPIFFS_CACHE
|
||||||
|
#define SPIFFS_CACHE 0
|
||||||
|
#endif
|
||||||
|
#if SPIFFS_CACHE
|
||||||
|
// Enables memory write caching for file descriptors in hydrogen
|
||||||
|
#ifndef SPIFFS_CACHE_WR
|
||||||
|
#define SPIFFS_CACHE_WR 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Enable/disable statistics on caching. Debug/test purpose only.
|
||||||
|
#ifndef SPIFFS_CACHE_STATS
|
||||||
|
#define SPIFFS_CACHE_STATS 0
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Always check header of each accessed page to ensure consistent state.
|
||||||
|
// If enabled it will increase number of reads, will increase flash.
|
||||||
|
#ifndef SPIFFS_PAGE_CHECK
|
||||||
|
#define SPIFFS_PAGE_CHECK 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Define maximum number of gc runs to perform to reach desired free pages.
|
||||||
|
#ifndef SPIFFS_GC_MAX_RUNS
|
||||||
|
#define SPIFFS_GC_MAX_RUNS 5
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Enable/disable statistics on gc. Debug/test purpose only.
|
||||||
|
#ifndef SPIFFS_GC_STATS
|
||||||
|
#define SPIFFS_GC_STATS 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Garbage collecting examines all pages in a block which and sums up
|
||||||
|
// to a block score. Deleted pages normally gives positive score and
|
||||||
|
// used pages normally gives a negative score (as these must be moved).
|
||||||
|
// To have a fair wear-leveling, the erase age is also included in score,
|
||||||
|
// whose factor normally is the most positive.
|
||||||
|
// The larger the score, the more likely it is that the block will
|
||||||
|
// picked for garbage collection.
|
||||||
|
|
||||||
|
// Garbage collecting heuristics - weight used for deleted pages.
|
||||||
|
#ifndef SPIFFS_GC_HEUR_W_DELET
|
||||||
|
#define SPIFFS_GC_HEUR_W_DELET (5)
|
||||||
|
#endif
|
||||||
|
// Garbage collecting heuristics - weight used for used pages.
|
||||||
|
#ifndef SPIFFS_GC_HEUR_W_USED
|
||||||
|
#define SPIFFS_GC_HEUR_W_USED (-1)
|
||||||
|
#endif
|
||||||
|
// Garbage collecting heuristics - weight used for time between
|
||||||
|
// last erased and erase of this block.
|
||||||
|
#ifndef SPIFFS_GC_HEUR_W_ERASE_AGE
|
||||||
|
#define SPIFFS_GC_HEUR_W_ERASE_AGE (50)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Object name maximum length.
|
||||||
|
#ifndef SPIFFS_OBJ_NAME_LEN
|
||||||
|
#define SPIFFS_OBJ_NAME_LEN (32)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Size of buffer allocated on stack used when copying data.
|
||||||
|
// Lower value generates more read/writes. No meaning having it bigger
|
||||||
|
// than logical page size.
|
||||||
|
#ifndef SPIFFS_COPY_BUFFER_STACK
|
||||||
|
#define SPIFFS_COPY_BUFFER_STACK (64)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Enable this to have an identifiable spiffs filesystem. This will look for
|
||||||
|
// a magic in all sectors to determine if this is a valid spiffs system or
|
||||||
|
// not on mount point. If not, SPIFFS_format must be called prior to mounting
|
||||||
|
// again.
|
||||||
|
#ifndef SPIFFS_USE_MAGIC
|
||||||
|
#define SPIFFS_USE_MAGIC (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// SPIFFS_LOCK and SPIFFS_UNLOCK protects spiffs from reentrancy on api level
|
||||||
|
// These should be defined on a multithreaded system
|
||||||
|
|
||||||
|
// define this to enter a mutex if you're running on a multithreaded system
|
||||||
|
#ifndef SPIFFS_LOCK
|
||||||
|
#define SPIFFS_LOCK(fs)
|
||||||
|
#endif
|
||||||
|
// define this to exit a mutex if you're running on a multithreaded system
|
||||||
|
#ifndef SPIFFS_UNLOCK
|
||||||
|
#define SPIFFS_UNLOCK(fs)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// Enable if only one spiffs instance with constant configuration will exist
|
||||||
|
// on the target. This will reduce calculations, flash and memory accesses.
|
||||||
|
// Parts of configuration must be defined below instead of at time of mount.
|
||||||
|
#ifndef SPIFFS_SINGLETON
|
||||||
|
#define SPIFFS_SINGLETON 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if SPIFFS_SINGLETON
|
||||||
|
// Instead of giving parameters in config struct, singleton build must
|
||||||
|
// give parameters in defines below.
|
||||||
|
#ifndef SPIFFS_CFG_PHYS_SZ
|
||||||
|
#define SPIFFS_CFG_PHYS_SZ(ignore) (1024*1024*2)
|
||||||
|
#endif
|
||||||
|
#ifndef SPIFFS_CFG_PHYS_ERASE_SZ
|
||||||
|
#define SPIFFS_CFG_PHYS_ERASE_SZ(ignore) (65536)
|
||||||
|
#endif
|
||||||
|
#ifndef SPIFFS_CFG_PHYS_ADDR
|
||||||
|
#define SPIFFS_CFG_PHYS_ADDR(ignore) (0)
|
||||||
|
#endif
|
||||||
|
#ifndef SPIFFS_CFG_LOG_PAGE_SZ
|
||||||
|
#define SPIFFS_CFG_LOG_PAGE_SZ(ignore) (256)
|
||||||
|
#endif
|
||||||
|
#ifndef SPIFFS_CFG_LOG_BLOCK_SZ
|
||||||
|
#define SPIFFS_CFG_LOG_BLOCK_SZ(ignore) (65536)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Enable this if your target needs aligned data for index tables
|
||||||
|
#ifndef SPIFFS_ALIGNED_OBJECT_INDEX_TABLES
|
||||||
|
#define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
|
||||||
|
// in the api. This function will visualize all filesystem using given printf
|
||||||
|
// function.
|
||||||
|
#ifndef SPIFFS_TEST_VISUALISATION
|
||||||
|
#define SPIFFS_TEST_VISUALISATION 0
|
||||||
|
#endif
|
||||||
|
#if SPIFFS_TEST_VISUALISATION
|
||||||
|
#ifndef spiffs_printf
|
||||||
|
#define spiffs_printf(...) printf(__VA_ARGS__)
|
||||||
|
#endif
|
||||||
|
// spiffs_printf argument for a free page
|
||||||
|
#ifndef SPIFFS_TEST_VIS_FREE_STR
|
||||||
|
#define SPIFFS_TEST_VIS_FREE_STR "_"
|
||||||
|
#endif
|
||||||
|
// spiffs_printf argument for a deleted page
|
||||||
|
#ifndef SPIFFS_TEST_VIS_DELE_STR
|
||||||
|
#define SPIFFS_TEST_VIS_DELE_STR "/"
|
||||||
|
#endif
|
||||||
|
// spiffs_printf argument for an index page for given object id
|
||||||
|
#ifndef SPIFFS_TEST_VIS_INDX_STR
|
||||||
|
#define SPIFFS_TEST_VIS_INDX_STR(id) "i"
|
||||||
|
#endif
|
||||||
|
// spiffs_printf argument for a data page for given object id
|
||||||
|
#ifndef SPIFFS_TEST_VIS_DATA_STR
|
||||||
|
#define SPIFFS_TEST_VIS_DATA_STR(id) "d"
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Types depending on configuration such as the amount of flash bytes
|
||||||
|
// given to spiffs file system in total (spiffs_file_system_size),
|
||||||
|
// the logical block size (log_block_size), and the logical page size
|
||||||
|
// (log_page_size)
|
||||||
|
|
||||||
|
// Block index type. Make sure the size of this type can hold
|
||||||
|
// the highest number of all blocks - i.e. spiffs_file_system_size / log_block_size
|
||||||
|
typedef u16_t spiffs_block_ix;
|
||||||
|
// Page index type. Make sure the size of this type can hold
|
||||||
|
// the highest page number of all pages - i.e. spiffs_file_system_size / log_page_size
|
||||||
|
typedef u16_t spiffs_page_ix;
|
||||||
|
// Object id type - most significant bit is reserved for index flag. Make sure the
|
||||||
|
// size of this type can hold the highest object id on a full system,
|
||||||
|
// i.e. 2 + (spiffs_file_system_size / (2*log_page_size))*2
|
||||||
|
typedef u16_t spiffs_obj_id;
|
||||||
|
// Object span index type. Make sure the size of this type can
|
||||||
|
// hold the largest possible span index on the system -
|
||||||
|
// i.e. (spiffs_file_system_size / log_page_size) - 1
|
||||||
|
typedef u16_t spiffs_span_ix;
|
||||||
|
|
||||||
|
#endif /* SPIFFS_CONFIG_H_ */
|
Loading…
x
Reference in New Issue
Block a user