dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Flash Filesystem Function

Log Structured Filesystem for internal or external NOR Flash. More...

Data Structures

struct  logfs_state
 
struct  arena_header
 
struct  slot_header
 
struct  streamfs_state
 
struct  streamfs_footer
 
struct  flashfs_logfs_cfg
 
struct  streamfs_cfg
 

Macros

#define MIN(x, y)   ((x) < (y) ? (x) : (y))
 
#define MIN(x, y)   ((x) < (y) ? (x) : (y))
 
#define PIOS_STREAMFS_TASK_PRIORITY   PIOS_THREAD_PRIO_LOW
 
#define PIOS_STREAMFS_TASK_STACK_BYTES   1000
 

Enumerations

enum  pios_flashfs_logfs_dev_magic { PIOS_FLASHFS_LOGFS_DEV_MAGIC = 0x94938201 }
 
enum  arena_state
 
enum  slot_state
 
enum  pios_flashfs_streamfs_dev_magic { PIOS_FLASHFS_STREAMFS_DEV_MAGIC = 0x93A40F82 }
 

Functions

static uintptr_t logfs_get_addr (const struct logfs_state *logfs, uint8_t arena_id, uint16_t slot_id)
 Return the offset in flash of a particular slot within an arena. More...
 
enum arena_state __attribute__ ((packed))
 
static int32_t logfs_erase_arena (const struct logfs_state *logfs, uint8_t arena_id)
 Erases all sectors within the given arena and sets arena to erased state. More...
 
static int32_t logfs_reserve_arena (const struct logfs_state *logfs, uint8_t arena_id)
 Marks the given arena as reserved so it can be filled. More...
 
static int32_t logfs_erase_all_arenas (const struct logfs_state *logfs)
 Erases all arenas available to this filesystem instance. More...
 
static int32_t logfs_activate_arena (const struct logfs_state *logfs, uint8_t arena_id)
 Marks the given arena as active so it can be mounted. More...
 
static int32_t logfs_obsolete_arena (const struct logfs_state *logfs, uint8_t arena_id)
 Marks the given arena as obsolete. More...
 
static int32_t logfs_find_active_arena (const struct logfs_state *logfs)
 Find the first active arena in flash. More...
 
static int32_t logfs_raw_copy_bytes (const struct logfs_state *logfs, uintptr_t src_addr, uint16_t src_size, uintptr_t dst_addr)
 
static bool logfs_fs_is_full (const struct logfs_state *logfs)
 
static bool logfs_log_is_full (const struct logfs_state *logfs)
 
static int32_t logfs_unmount_log (struct logfs_state *logfs)
 
static int32_t logfs_mount_log (struct logfs_state *logfs, uint8_t arena_id)
 
static bool PIOS_FLASHFS_Logfs_validate (const struct logfs_state *logfs)
 
static struct logfs_statePIOS_FLASHFS_Logfs_alloc (void)
 
static void PIOS_FLASHFS_Logfs_free (struct logfs_state *logfs)
 
int32_t PIOS_FLASHFS_Logfs_Init (uintptr_t *fs_id, const struct flashfs_logfs_cfg *cfg, enum pios_flash_partition_labels partition_label)
 Initialize the flash object setting FS. More...
 
int32_t PIOS_FLASHFS_Logfs_Destroy (uintptr_t fs_id)
 
static int32_t logfs_garbage_collect (struct logfs_state *logfs)
 
static int16_t logfs_object_find_next (const struct logfs_state *logfs, struct slot_header *slot_hdr, uint16_t *curr_slot, uint32_t obj_id, uint16_t obj_inst_id)
 
static int8_t logfs_delete_object (struct logfs_state *logfs, uint32_t obj_id, uint16_t obj_inst_id)
 
static int8_t logfs_reserve_free_slot (struct logfs_state *logfs, uint16_t *slot_id, struct slot_header *slot_hdr, uint32_t obj_id, uint16_t obj_inst_id, uint16_t obj_size)
 
static int8_t logfs_append_to_log (struct logfs_state *logfs, uint32_t obj_id, uint16_t obj_inst_id, uint8_t *obj_data, uint16_t obj_size)
 
int32_t PIOS_FLASHFS_ObjSave (uintptr_t fs_id, uint32_t obj_id, uint16_t obj_inst_id, uint8_t *obj_data, uint16_t obj_size)
 Saves one object instance to the filesystem. More...
 
int32_t PIOS_FLASHFS_ObjLoad (uintptr_t fs_id, uint32_t obj_id, uint16_t obj_inst_id, uint8_t *obj_data, uint16_t obj_size)
 Load one object instance from the filesystem. More...
 
int32_t PIOS_FLASHFS_ObjDelete (uintptr_t fs_id, uint32_t obj_id, uint16_t obj_inst_id)
 Delete one instance of an object from the filesystem. More...
 
int32_t PIOS_FLASHFS_Format (uintptr_t fs_id)
 Erases all filesystem arenas and activate the first arena. More...
 
static void PIOS_STREAMFS_RegisterTxCallback (uintptr_t fs_id, pios_com_callback tx_out_cb, uintptr_t context)
 
static void PIOS_STREAMFS_TxStart (uintptr_t fs_id, uint16_t tx_bytes_avail)
 
static uintptr_t streamfs_get_addr (const struct streamfs_state *streamfs, uint32_t arena_id, uint16_t arena_offset)
 Return the offset in flash of a particular slot within an arena. More...
 
static int32_t streamfs_erase_arena (const struct streamfs_state *streamfs, uint32_t arena_id)
 Erases all sectors within the given arena and sets arena to erased state. More...
 
static int32_t streamfs_erase_all_arenas (const struct streamfs_state *streamfs)
 Erases all arenas available to this filesystem instance. More...
 
static bool streamfs_validate (const struct streamfs_state *streamfs)
 
static struct streamfs_statestreamfs_alloc (void)
 
static int32_t streamfs_new_sector (struct streamfs_state *streamfs)
 
static int32_t streamfs_close_sector (struct streamfs_state *streamfs)
 
static int32_t streamfs_find_first_arena (struct streamfs_state *streamfs, int32_t file_id)
 
static int32_t streamfs_find_last_arena (struct streamfs_state *streamfs, int32_t file_id)
 
static int32_t streamfs_find_new_sector (struct streamfs_state *streamfs)
 
static int32_t streamfs_append_to_file (struct streamfs_state *streamfs, uint8_t *data, uint32_t len)
 
static int32_t streamfs_read_from_file (struct streamfs_state *streamfs, uint8_t *data, uint32_t len)
 
static int32_t streamfs_scan_filesystem (struct streamfs_state *streamfs)
 
static void PIOS_STREAMFS_Task (void *parameters)
 
int32_t PIOS_STREAMFS_Init (uintptr_t *fs_id, const struct streamfs_cfg *cfg, enum pios_flash_partition_labels partition_label)
 Initialize the flash object setting FS. More...
 
int32_t PIOS_STREAMFS_Format (uintptr_t fs_id)
 Erases all filesystem arenas and activate the first arena. More...
 
int32_t PIOS_STREAMFS_OpenWrite (uintptr_t fs_id)
 
int32_t PIOS_STREAMFS_OpenRead (uintptr_t fs_id, uint32_t file_id)
 
int32_t PIOS_STREAMFS_MinFileId (uintptr_t fs_id)
 
int32_t PIOS_STREAMFS_MaxFileId (uintptr_t fs_id)
 
int32_t PIOS_STREAMFS_Close (uintptr_t fs_id)
 
int32_t PIOS_STREAMFS_Read (uintptr_t fs_id, uint8_t *data, uint32_t len)
 
int32_t PIOS_STREAMFS_Testing_Write (uintptr_t fs_id, uint8_t *data, uint32_t len)
 

Variables

const struct pios_com_driver pios_streamfs_com_driver
 
uint32_t flashfs_logfs_cfg::fs_magic
 
uint32_t flashfs_logfs_cfg::arena_size
 
uint32_t flashfs_logfs_cfg::slot_size
 
uint32_t streamfs_cfg::fs_magic
 
uint32_t streamfs_cfg::arena_size
 
uint32_t streamfs_cfg::write_size
 
const struct pios_com_driver pios_streamfs_com_driver
 

Detailed Description

Log Structured Filesystem for internal or external NOR Flash.

Flash Filesystem API Definition.

Streaming circular buffer for external NOR Flash.

Macro Definition Documentation

#define MIN (   x,
 
)    ((x) < (y) ? (x) : (y))

Definition at line 35 of file pios_flashfs_logfs.c.

#define MIN (   x,
 
)    ((x) < (y) ? (x) : (y))

Definition at line 39 of file pios_streamfs.c.

#define PIOS_STREAMFS_TASK_PRIORITY   PIOS_THREAD_PRIO_LOW

This file system provides the ability to create numbered files and stream to flash. These buffers will wrap around the available flash partition and the file system makes no attempt to prevent that happening. It is intended for applications of storing large files or logging information

Files are written into continuous sectors of the flash chip. Each sector has a footer to indicate the file id and the sector id.

Arenas map onto sectors.

Definition at line 57 of file pios_streamfs.c.

#define PIOS_STREAMFS_TASK_STACK_BYTES   1000

Definition at line 58 of file pios_streamfs.c.

Enumeration Type Documentation

Definition at line 85 of file pios_flashfs_logfs.c.

Enumerator
PIOS_FLASHFS_LOGFS_DEV_MAGIC 

Definition at line 41 of file pios_flashfs_logfs.c.

Enumerator
PIOS_FLASHFS_STREAMFS_DEV_MAGIC 

Definition at line 73 of file pios_streamfs.c.

enum slot_state

Definition at line 314 of file pios_flashfs_logfs.c.

Function Documentation

enum arena_state __attribute__ ( (packed)  )

Definition at line 38 of file serial_4way.h.

static int32_t logfs_activate_arena ( const struct logfs_state logfs,
uint8_t  arena_id 
)
static

Marks the given arena as active so it can be mounted.

Returns
0 if success, < 0 on failure
Note
Arena must have been previously erased or reserved before calling this
Must be called while holding the flash transaction lock

Definition at line 203 of file pios_flashfs_logfs.c.

static int8_t logfs_append_to_log ( struct logfs_state logfs,
uint32_t  obj_id,
uint16_t  obj_inst_id,
uint8_t *  obj_data,
uint16_t  obj_size 
)
static

Definition at line 796 of file pios_flashfs_logfs.c.

static int8_t logfs_delete_object ( struct logfs_state logfs,
uint32_t  obj_id,
uint16_t  obj_inst_id 
)
static

Definition at line 699 of file pios_flashfs_logfs.c.

static int32_t logfs_erase_all_arenas ( const struct logfs_state logfs)
static

Erases all arenas available to this filesystem instance.

Returns
0 if success, < 0 on failure
Note
Must be called while holding the flash transaction lock

Definition at line 185 of file pios_flashfs_logfs.c.

static int32_t logfs_erase_arena ( const struct logfs_state logfs,
uint8_t  arena_id 
)
static

Erases all sectors within the given arena and sets arena to erased state.

Returns
0 if success, < 0 on failure
Note
Must be called while holding the flash transaction lock

Definition at line 116 of file pios_flashfs_logfs.c.

static int32_t logfs_find_active_arena ( const struct logfs_state logfs)
static

Find the first active arena in flash.

Returns
arena_id (>=0) of first active arena
-1 if no active arena is found
-2 if failed to read arena header
Note
Must be called while holding the flash transaction lock

Definition at line 283 of file pios_flashfs_logfs.c.

static bool logfs_fs_is_full ( const struct logfs_state logfs)
static

Definition at line 385 of file pios_flashfs_logfs.c.

static int32_t logfs_garbage_collect ( struct logfs_state logfs)
static

Definition at line 590 of file pios_flashfs_logfs.c.

static uintptr_t logfs_get_addr ( const struct logfs_state logfs,
uint8_t  arena_id,
uint16_t  slot_id 
)
static

Return the offset in flash of a particular slot within an arena.

Returns
address of the requested slot

Definition at line 71 of file pios_flashfs_logfs.c.

static bool logfs_log_is_full ( const struct logfs_state logfs)
static

Definition at line 395 of file pios_flashfs_logfs.c.

static int32_t logfs_mount_log ( struct logfs_state logfs,
uint8_t  arena_id 
)
static

Definition at line 411 of file pios_flashfs_logfs.c.

static int16_t logfs_object_find_next ( const struct logfs_state logfs,
struct slot_header slot_hdr,
uint16_t *  curr_slot,
uint32_t  obj_id,
uint16_t  obj_inst_id 
)
static

Definition at line 661 of file pios_flashfs_logfs.c.

static int32_t logfs_obsolete_arena ( const struct logfs_state logfs,
uint8_t  arena_id 
)
static

Marks the given arena as obsolete.

Returns
0 if success, < 0 on failure
Note
Arena must have been previously active before calling this
Must be called while holding the flash transaction lock

Definition at line 241 of file pios_flashfs_logfs.c.

static int32_t logfs_raw_copy_bytes ( const struct logfs_state logfs,
uintptr_t  src_addr,
uint16_t  src_size,
uintptr_t  dst_addr 
)
static

Definition at line 338 of file pios_flashfs_logfs.c.

static int32_t logfs_reserve_arena ( const struct logfs_state logfs,
uint8_t  arena_id 
)
static

Marks the given arena as reserved so it can be filled.

Returns
0 if success, < 0 on failure
Note
Arena must have been previously erased before calling this
Must be called while holding the flash transaction lock

Definition at line 148 of file pios_flashfs_logfs.c.

static int8_t logfs_reserve_free_slot ( struct logfs_state logfs,
uint16_t *  slot_id,
struct slot_header slot_hdr,
uint32_t  obj_id,
uint16_t  obj_inst_id,
uint16_t  obj_size 
)
static

Definition at line 740 of file pios_flashfs_logfs.c.

static int32_t logfs_unmount_log ( struct logfs_state logfs)
static

Definition at line 400 of file pios_flashfs_logfs.c.

int32_t PIOS_FLASHFS_Format ( uintptr_t  fs_id)

Erases all filesystem arenas and activate the first arena.

Parameters
[in]fs_idThe filesystem to use for this action
Returns
0 if success or error code
Return values
-1if fs_id is not a valid filesystem instance
-2if failed to start transaction
-3if failed to erase all arenas
-4if failed to activate arena 0
-5if failed to mount arena 0

Definition at line 1055 of file pios_flashfs_logfs.c.

static struct logfs_state* PIOS_FLASHFS_Logfs_alloc ( void  )
static

Definition at line 464 of file pios_flashfs_logfs.c.

int32_t PIOS_FLASHFS_Logfs_Destroy ( uintptr_t  fs_id)

Definition at line 571 of file pios_flashfs_logfs.c.

static void PIOS_FLASHFS_Logfs_free ( struct logfs_state logfs)
static

Definition at line 474 of file pios_flashfs_logfs.c.

int32_t PIOS_FLASHFS_Logfs_Init ( uintptr_t *  fs_id,
const struct flashfs_logfs_cfg cfg,
enum pios_flash_partition_labels  partition_label 
)

Initialize the flash object setting FS.

Returns
0 if success, -1 if failure

Definition at line 485 of file pios_flashfs_logfs.c.

static bool PIOS_FLASHFS_Logfs_validate ( const struct logfs_state logfs)
static

Definition at line 459 of file pios_flashfs_logfs.c.

int32_t PIOS_FLASHFS_ObjDelete ( uintptr_t  fs_id,
uint32_t  obj_id,
uint16_t  obj_inst_id 
)

Delete one instance of an object from the filesystem.

Parameters
[in]fs_idThe filesystem to use for this action
[in]objUAVObject ID of the object to delete
[in]obj_inst_idThe instance of the object to delete
Returns
0 if success or error code
Return values
-1if fs_id is not a valid filesystem instance
-2if failed to start transaction
-3if failed to delete the object from the filesystem

Definition at line 1014 of file pios_flashfs_logfs.c.

int32_t PIOS_FLASHFS_ObjLoad ( uintptr_t  fs_id,
uint32_t  obj_id,
uint16_t  obj_inst_id,
uint8_t *  obj_data,
uint16_t  obj_size 
)

Load one object instance from the filesystem.

Parameters
[in]fs_idThe filesystem to use for this action
[in]objUAVObject ID of the object to load
[in]obj_inst_idThe instance of the object to load
[in]obj_dataBuffer to hold the contents of the loaded object
[in]obj_sizeSize of the object to be loaded
Returns
0 if success or error code
Return values
-1if fs_id is not a valid filesystem instance
-2if failed to start transaction
-3if object not found in filesystem
-4if object size in filesystem does not exactly match buffer size
-5if reading the object data from flash fails

Definition at line 947 of file pios_flashfs_logfs.c.

int32_t PIOS_FLASHFS_ObjSave ( uintptr_t  fs_id,
uint32_t  obj_id,
uint16_t  obj_inst_id,
uint8_t *  obj_data,
uint16_t  obj_size 
)

Saves one object instance to the filesystem.

Parameters
[in]fs_idThe filesystem to use for this action
[in]objUAVObject ID of the object to save
[in]obj_inst_idThe instance number of the object being saved
[in]obj_dataContents of the object being saved
[in]obj_sizeSize of the object being saved
Returns
0 if success or error code
Return values
-1if fs_id is not a valid filesystem instance
-2if failed to start transaction
-3if failure to delete any previous versions of the object
-4if filesystem is entirely full and garbage collection won't help
-5if garbage collection failed
-6if filesystem is full even after garbage collection should have freed space
-7if writing the new object to the filesystem failed

Definition at line 861 of file pios_flashfs_logfs.c.

int32_t PIOS_STREAMFS_Close ( uintptr_t  fs_id)

Definition at line 871 of file pios_streamfs.c.

int32_t PIOS_STREAMFS_Format ( uintptr_t  fs_id)

Erases all filesystem arenas and activate the first arena.

Parameters
[in]fs_idThe filesystem to use for this action
Returns
0 if success or error code
Return values
-1if fs_id is not a valid filesystem instance
-2if failed to start transaction
-3if failed to erase all arenas
-4if failed to activate arena 0
-5if failed to mount arena 0

Definition at line 681 of file pios_streamfs.c.

int32_t PIOS_STREAMFS_Init ( uintptr_t *  fs_id,
const struct streamfs_cfg cfg,
enum pios_flash_partition_labels  partition_label 
)

Initialize the flash object setting FS.

Returns
0 if success, -1 if failure

Definition at line 581 of file pios_streamfs.c.

int32_t PIOS_STREAMFS_MaxFileId ( uintptr_t  fs_id)

Definition at line 859 of file pios_streamfs.c.

int32_t PIOS_STREAMFS_MinFileId ( uintptr_t  fs_id)

Definition at line 847 of file pios_streamfs.c.

int32_t PIOS_STREAMFS_OpenRead ( uintptr_t  fs_id,
uint32_t  file_id 
)

Definition at line 786 of file pios_streamfs.c.

int32_t PIOS_STREAMFS_OpenWrite ( uintptr_t  fs_id)

Create a new file for streaming to

Parameters
[in]fs_idthe streaming device handle
Returns
0 if successful, <0 if not

Definition at line 724 of file pios_streamfs.c.

int32_t PIOS_STREAMFS_Read ( uintptr_t  fs_id,
uint8_t *  data,
uint32_t  len 
)

Definition at line 941 of file pios_streamfs.c.

static void PIOS_STREAMFS_RegisterTxCallback ( uintptr_t  fs_id,
pios_com_callback  tx_out_cb,
uintptr_t  context 
)
static

Definition at line 1021 of file pios_streamfs.c.

static void PIOS_STREAMFS_Task ( void *  parameters)
static

Definition at line 513 of file pios_streamfs.c.

int32_t PIOS_STREAMFS_Testing_Write ( uintptr_t  fs_id,
uint8_t *  data,
uint32_t  len 
)

Definition at line 975 of file pios_streamfs.c.

static void PIOS_STREAMFS_TxStart ( uintptr_t  fs_id,
uint16_t  tx_bytes_avail 
)
static

Definition at line 1011 of file pios_streamfs.c.

static struct streamfs_state* streamfs_alloc ( void  )
static

Definition at line 178 of file pios_streamfs.c.

static int32_t streamfs_append_to_file ( struct streamfs_state streamfs,
uint8_t *  data,
uint32_t  len 
)
static

Definition at line 360 of file pios_streamfs.c.

static int32_t streamfs_close_sector ( struct streamfs_state streamfs)
static

Close this sector by writing footer. Does not prepare next sector.

Definition at line 237 of file pios_streamfs.c.

static int32_t streamfs_erase_all_arenas ( const struct streamfs_state streamfs)
static

Erases all arenas available to this filesystem instance.

Returns
0 if success, < 0 on failure
Note
Must be called while holding the flash transaction lock

Definition at line 161 of file pios_streamfs.c.

static int32_t streamfs_erase_arena ( const struct streamfs_state streamfs,
uint32_t  arena_id 
)
static

Erases all sectors within the given arena and sets arena to erased state.

Returns
0 if success, < 0 on failure
Note
Must be called while holding the flash transaction lock

Definition at line 143 of file pios_streamfs.c.

static int32_t streamfs_find_first_arena ( struct streamfs_state streamfs,
int32_t  file_id 
)
static

Find the first arena for a file

Parameters
[in]streamfsthe file system handle
[in]file_idthe file to find
Returns
the sector number if found, or negative if there was an error

: Must be called while holding the flash transaction lock

Definition at line 264 of file pios_streamfs.c.

static int32_t streamfs_find_last_arena ( struct streamfs_state streamfs,
int32_t  file_id 
)
static

Find the last arena for a file

Parameters
[in]streamfsthe file system handle
[in]file_idthe file to find
Returns
the sector number if found, or negative if there was an error

: Must be called while holding the flash transaction lock

Definition at line 305 of file pios_streamfs.c.

static int32_t streamfs_find_new_sector ( struct streamfs_state streamfs)
static

Find the first sector for a file

Parameters
[in]streamfsthe file system handle
Returns
the sector for the new file

: Must be called while holding the flash transaction lock

Definition at line 345 of file pios_streamfs.c.

static uintptr_t streamfs_get_addr ( const struct streamfs_state streamfs,
uint32_t  arena_id,
uint16_t  arena_offset 
)
static

Return the offset in flash of a particular slot within an arena.

Returns
address of the requested slot

Definition at line 118 of file pios_streamfs.c.

static int32_t streamfs_new_sector ( struct streamfs_state streamfs)
static

Write footer to current sector and reset pointers for writing to next sector

Definition at line 194 of file pios_streamfs.c.

static int32_t streamfs_read_from_file ( struct streamfs_state streamfs,
uint8_t *  data,
uint32_t  len 
)
static

Definition at line 402 of file pios_streamfs.c.

static int32_t streamfs_scan_filesystem ( struct streamfs_state streamfs)
static

Definition at line 473 of file pios_streamfs.c.

static bool streamfs_validate ( const struct streamfs_state streamfs)
static

Definition at line 173 of file pios_streamfs.c.

Variable Documentation

uint32_t streamfs_cfg::arena_size

Definition at line 37 of file pios_streamfs_priv.h.

uint32_t flashfs_logfs_cfg::arena_size

Definition at line 40 of file pios_flashfs_logfs_priv.h.

uint32_t streamfs_cfg::fs_magic

Definition at line 36 of file pios_streamfs_priv.h.

uint32_t flashfs_logfs_cfg::fs_magic

Definition at line 39 of file pios_flashfs_logfs_priv.h.

const struct pios_com_driver pios_streamfs_com_driver

Definition at line 64 of file pios_streamfs.c.

const struct pios_com_driver pios_streamfs_com_driver
Initial value:
= {
.tx_start = PIOS_STREAMFS_TxStart,
}
static void PIOS_STREAMFS_TxStart(uintptr_t fs_id, uint16_t tx_bytes_avail)
static void PIOS_STREAMFS_RegisterTxCallback(uintptr_t fs_id, pios_com_callback tx_out_cb, uintptr_t context)

Definition at line 64 of file pios_streamfs.c.

uint32_t flashfs_logfs_cfg::slot_size

Definition at line 41 of file pios_flashfs_logfs_priv.h.

uint32_t streamfs_cfg::write_size

Definition at line 38 of file pios_streamfs_priv.h.