ICARUS OS (Intelligent Cooperative Architecture for Real-time Unified Systems) 0.1.0
Preemptive Real-Time Operating System for ARM Cortex-M7
Loading...
Searching...
No Matches
fs.c File Reference

Internal RAM-backed flat-file filesystem implementation. More...

#include "icarus/icarus.h"
#include "icarus/svc.h"
#include <string.h>

Go to the source code of this file.

Data Structures

struct  fs_entry_t
 

Functions

static int16_t fs_find (const char *name)
 Find slot index for name, or -1 if not found.
 
static int16_t fs_free_slot (void)
 Find first free slot, or -1 if full.
 
void __fs_init (void)
 Privileged implementation of fs_init().
 
bool __fs_create (const char *name, fs_file_t *out)
 Privileged implementation of fs_create().
 
bool __fs_open (const char *name, fs_file_t *out)
 Privileged implementation of fs_open().
 
bool __fs_write (fs_file_t *f, const uint8_t *data, uint16_t len)
 Privileged implementation of fs_write().
 
uint16_t __fs_read (fs_file_t *f, uint8_t *buf, uint16_t len, uint16_t offset)
 Privileged implementation of fs_read().
 
bool __fs_delete (const char *name)
 Privileged implementation of fs_delete().
 
uint8_t __fs_list (fs_file_info_t *out, uint8_t max)
 Privileged implementation of fs_list().
 
void __fs_stats (fs_stats_t *out)
 Privileged implementation of fs_stats().
 
void fs_init (void)
 Clear all file table entries and zero the data store via SVC gate.
 
bool fs_create (const char *name, fs_file_t *out)
 Create a new file via SVC gate.
 
bool fs_open (const char *name, fs_file_t *out)
 Open an existing file by name via SVC gate.
 
bool fs_write (fs_file_t *f, const uint8_t *data, uint16_t len)
 Append data to an open file via SVC gate.
 
uint16_t fs_read (fs_file_t *f, uint8_t *buf, uint16_t len, uint16_t offset)
 Read bytes from an open file at a given offset via SVC gate.
 
bool fs_delete (const char *name)
 Delete a file by name via SVC gate.
 
uint8_t fs_list (fs_file_info_t *out, uint8_t max)
 Enumerate all files via SVC gate.
 
void fs_stats (fs_stats_t *out)
 Return aggregate filesystem statistics via SVC gate.
 

Variables

static fs_entry_t fs_table [16]
 
static uint8_t fs_data [16][2048]
 

Detailed Description

Internal RAM-backed flat-file filesystem implementation.

The store is a static byte array divided into FS_MAX_FILES fixed-size blocks. A parallel table of fs_entry_t structs tracks name, size, and occupancy for each block.

The __fs_* prefixed functions are the privileged implementations that execute inside the SVC handler with full access to kernel data. The unprefixed public functions issue SVC instructions so that unprivileged tasks can use the filesystem without triggering MemManage faults.

Author
Souham Biswas
Date
2026

Definition in file fs.c.

Function Documentation

◆ __fs_create()

bool __fs_create ( const char *  name,
fs_file_t out 
)

Privileged implementation of fs_create().

Parameters
[in]nameNull-terminated file name.
[out]outFile handle written on success.
Return values
trueFile created.
falseFull, duplicate, or invalid name.

Definition at line 88 of file fs.c.

References fs_data, fs_find(), fs_free_slot(), FS_MAX_FILE_SIZE, FS_MAX_NAME_LEN, fs_table, fs_entry_t::name, fs_entry_t::size, fs_file_t::slot, fs_entry_t::used, and fs_file_t::valid.

Referenced by fs_create(), and SVC_Handler_C().

◆ __fs_delete()

bool __fs_delete ( const char *  name)

Privileged implementation of fs_delete().

Parameters
[in]nameFile name to remove.
Return values
trueFile found and deleted.
falseFile does not exist.

Definition at line 205 of file fs.c.

References fs_data, fs_find(), FS_MAX_FILE_SIZE, and fs_table.

Referenced by fs_delete(), and SVC_Handler_C().

◆ __fs_init()

void __fs_init ( void  )

Privileged implementation of fs_init().

Zeroes the file table and data store.

Definition at line 76 of file fs.c.

References fs_data, and fs_table.

Referenced by fs_init(), and SVC_Handler_C().

◆ __fs_list()

uint8_t __fs_list ( fs_file_info_t out,
uint8_t  max 
)

Privileged implementation of fs_list().

Parameters
[out]outArray of file info entries.
[in]maxMaximum entries to write.
Returns
Number of entries written.

Definition at line 227 of file fs.c.

References FS_MAX_FILES, FS_MAX_NAME_LEN, fs_table, fs_file_info_t::name, fs_file_info_t::size, and fs_entry_t::size.

Referenced by fs_list(), and SVC_Handler_C().

◆ __fs_open()

bool __fs_open ( const char *  name,
fs_file_t out 
)

Privileged implementation of fs_open().

Parameters
[in]nameFile name to look up.
[out]outFile handle written on success.
Return values
trueFile found and handle populated.
falseFile does not exist.

Definition at line 124 of file fs.c.

References fs_find(), fs_file_t::slot, and fs_file_t::valid.

Referenced by fs_open(), and SVC_Handler_C().

◆ __fs_read()

uint16_t __fs_read ( fs_file_t f,
uint8_t *  buf,
uint16_t  len,
uint16_t  offset 
)

Privileged implementation of fs_read().

Parameters
[in]fFile handle.
[out]bufDestination buffer.
[in]lenMaximum bytes to read.
[in]offsetByte offset into the file.
Returns
Bytes actually read (may be less than len near EOF), or 0 on error.

Definition at line 175 of file fs.c.

References fs_data, FS_MAX_FILES, fs_table, fs_entry_t::size, fs_file_t::slot, and fs_file_t::valid.

Referenced by fs_read(), and SVC_Handler_C().

◆ __fs_stats()

void __fs_stats ( fs_stats_t out)

Privileged implementation of fs_stats().

Parameters
[out]outAggregate filesystem statistics.

Definition at line 249 of file fs.c.

References fs_stats_t::file_count, fs_stats_t::free_bytes, FS_MAX_FILE_SIZE, FS_MAX_FILES, fs_table, fs_entry_t::size, fs_stats_t::total_bytes, and fs_stats_t::used_bytes.

Referenced by fs_stats(), and SVC_Handler_C().

◆ __fs_write()

bool __fs_write ( fs_file_t f,
const uint8_t *  data,
uint16_t  len 
)

Privileged implementation of fs_write().

Parameters
[in]fFile handle.
[in]dataBytes to append.
[in]lenNumber of bytes.
Return values
trueData appended.
falseInvalid handle, null data, or would exceed max size.

Definition at line 146 of file fs.c.

References fs_data, FS_MAX_FILE_SIZE, FS_MAX_FILES, fs_table, fs_entry_t::size, fs_file_t::slot, and fs_file_t::valid.

Referenced by fs_write(), and SVC_Handler_C().

◆ fs_create()

bool fs_create ( const char *  name,
fs_file_t out 
)

Create a new file via SVC gate.

Create a new file with the given name.

Parameters
[in]nameNull-terminated file name.
[out]outFile handle written on success.
Return values
trueFile created.
falseFull, duplicate, or invalid name.

Definition at line 292 of file fs.c.

References __fs_create(), and SVC_FS_CREATE.

◆ fs_delete()

bool fs_delete ( const char *  name)

Delete a file by name via SVC gate.

Delete a file by name.

Parameters
[in]nameFile name to remove.
Return values
trueFile deleted.
falseFile does not exist.

Definition at line 401 of file fs.c.

References __fs_delete(), and SVC_FS_DELETE.

◆ fs_find()

static int16_t fs_find ( const char *  name)
static

Find slot index for name, or -1 if not found.

Definition at line 48 of file fs.c.

References FS_MAX_FILES, FS_MAX_NAME_LEN, and fs_table.

Referenced by __fs_create(), __fs_delete(), and __fs_open().

◆ fs_free_slot()

static int16_t fs_free_slot ( void  )
static

Find first free slot, or -1 if full.

Definition at line 59 of file fs.c.

References FS_MAX_FILES, and fs_table.

Referenced by __fs_create().

◆ fs_init()

void fs_init ( void  )

Clear all file table entries and zero the data store via SVC gate.

Clear all file table entries and zero the data store.

Definition at line 277 of file fs.c.

References __fs_init(), and SVC_FS_INIT.

◆ fs_list()

uint8_t fs_list ( fs_file_info_t out,
uint8_t  max 
)

Enumerate all files via SVC gate.

Enumerate all files currently in the filesystem.

Parameters
[out]outArray of file info entries.
[in]maxMaximum entries to write.
Returns
Number of entries written.

Definition at line 424 of file fs.c.

References __fs_list(), and SVC_FS_LIST.

◆ fs_open()

bool fs_open ( const char *  name,
fs_file_t out 
)

Open an existing file by name via SVC gate.

Open an existing file by name.

Parameters
[in]nameFile name to look up.
[out]outFile handle written on success.
Return values
trueFile found.
falseFile does not exist.

Definition at line 318 of file fs.c.

References __fs_open(), and SVC_FS_OPEN.

◆ fs_read()

uint16_t fs_read ( fs_file_t f,
uint8_t *  buf,
uint16_t  len,
uint16_t  offset 
)

Read bytes from an open file at a given offset via SVC gate.

Read bytes from an open file at a given offset.

Parameters
[in]fFile handle.
[out]bufDestination buffer.
[in]lenMaximum bytes to read.
[in]offsetByte offset into the file.
Returns
Bytes actually read, or 0 on error.

Definition at line 373 of file fs.c.

References __fs_read(), and SVC_FS_READ.

◆ fs_stats()

void fs_stats ( fs_stats_t out)

Return aggregate filesystem statistics via SVC gate.

Return aggregate filesystem statistics.

Parameters
[out]outStatistics structure to fill.

Definition at line 447 of file fs.c.

References __fs_stats(), and SVC_FS_STATS.

◆ fs_write()

bool fs_write ( fs_file_t f,
const uint8_t *  data,
uint16_t  len 
)

Append data to an open file via SVC gate.

Append data to an open file.

Parameters
[in]fFile handle.
[in]dataBytes to append.
[in]lenNumber of bytes.
Return values
trueData appended.
falseInvalid handle, null data, or would exceed max size.

Definition at line 345 of file fs.c.

References __fs_write(), and SVC_FS_WRITE.

Variable Documentation

◆ fs_data

uint8_t fs_data[16][2048]
static

Definition at line 43 of file fs.c.

Referenced by __fs_create(), __fs_delete(), __fs_init(), __fs_read(), and __fs_write().

◆ fs_table

fs_entry_t fs_table[16]
static