Merge branch 'feature/vfs' into 'master'

Virtual filesystem APIs

This changeset adds virtual filesystem APIs. As an example, UART devices are mapped to `/dev/uart/x`.
Also fixes an issue with per-task FILE descriptors.

See merge request !149
This commit is contained in:
Ivan Grokhotkov 2016-10-27 17:47:45 +08:00
commit bdf2908057
17 changed files with 1036 additions and 268 deletions

View file

@ -42,7 +42,8 @@
#include "esp_spi_flash.h"
#include "esp_ipc.h"
#include "esp_log.h"
#include "esp_vfs_dev.h"
#include "esp_newlib.h"
#include "esp_brownout.h"
#include "esp_int_wdt.h"
#include "esp_task_wdt.h"
@ -59,7 +60,6 @@ static bool app_cpu_started = false;
static void do_global_ctors(void);
static void main_task(void* args);
extern void ets_setup_syscalls(void);
extern void app_main(void);
extern int _bss_start;
@ -160,7 +160,13 @@ void start_cpu0_default(void)
#if CONFIG_TASK_WDT
esp_task_wdt_init();
#endif
ets_setup_syscalls();
esp_setup_syscalls();
esp_vfs_dev_uart_register();
esp_reent_init(_GLOBAL_REENT);
const char* default_uart_dev = "/dev/uart/0";
_GLOBAL_REENT->_stdout = fopen(default_uart_dev, "w");
_GLOBAL_REENT->_stderr = fopen(default_uart_dev, "w");
_GLOBAL_REENT->_stdin = fopen(default_uart_dev, "r");
do_global_ctors();
esp_ipc_init();
spi_flash_init();

View file

@ -15,6 +15,9 @@
#ifndef _SOC_CPU_H
#define _SOC_CPU_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "xtensa/corebits.h"
/* C macros for xtensa special register read/write/exchange */

View file

@ -77,6 +77,7 @@ task.h is included from an application file. */
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
#include "rom/ets_sys.h"
#include "esp_newlib.h"
/* FreeRTOS includes. */
#include "FreeRTOS.h"
@ -963,7 +964,7 @@ UBaseType_t x;
#if ( configUSE_NEWLIB_REENTRANT == 1 )
{
/* Initialise this task's Newlib reent structure. */
_REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
esp_reent_init(&pxNewTCB->xNewLib_reent);
}
#endif
@ -3643,8 +3644,6 @@ BaseType_t xTaskGetAffinity( TaskHandle_t xTask )
#if ( INCLUDE_vTaskDelete == 1 )
// TODO: move this to newlib component and provide a header file
extern void _extra_cleanup_r(struct _reent* r);
static void prvDeleteTCB( TCB_t *pxTCB )
{
@ -3657,7 +3656,6 @@ BaseType_t xTaskGetAffinity( TaskHandle_t xTask )
to the task to free any memory allocated at the application level. */
#if ( configUSE_NEWLIB_REENTRANT == 1 )
{
pxTCB->xNewLib_reent.__cleanup = &_extra_cleanup_r;
_reclaim_reent( &( pxTCB->xNewLib_reent ) );
}
#endif /* configUSE_NEWLIB_REENTRANT */

View file

@ -1,8 +1,5 @@
COMPONENT_ADD_LDFLAGS := $(abspath lib/libc.a) $(abspath lib/libm.a)
COMPONENT_ADD_LDFLAGS := $(abspath lib/libc.a) $(abspath lib/libm.a) -lnewlib
define COMPONENT_BUILDRECIPE
#Nothing to do; this does not generate a library.
endef
COMPONENT_ADD_INCLUDEDIRS := include platform_include
include $(IDF_PATH)/make/component_common.mk

View file

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
@ -11,181 +11,17 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <sys/reent.h>
#include <sys/lock.h>
#include <stdlib.h>
#include <sys/reent.h>
#include "esp_attr.h"
#include "rom/libc_stubs.h"
#include "rom/uart.h"
#include "soc/cpu.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/portmacro.h"
#include "freertos/task.h"
void abort() {
do
{
__asm__ ("break 0,0");
*((int*) 0) = 0;
} while(true);
}
void* _malloc_r(struct _reent *r, size_t size) {
return pvPortMalloc(size);
}
void _free_r(struct _reent *r, void* ptr) {
return vPortFree(ptr);
}
void* _realloc_r(struct _reent *r, void* ptr, size_t size) {
void* new_chunk;
if (size == 0) {
if (ptr) {
vPortFree(ptr);
}
return NULL;
}
new_chunk = pvPortMalloc(size);
if (new_chunk && ptr) {
memcpy(new_chunk, ptr, size);
vPortFree(ptr);
}
// realloc behaviour: don't free original chunk if alloc failed
return new_chunk;
}
void* _calloc_r(struct _reent *r, size_t count, size_t size) {
void* result = pvPortMalloc(count * size);
if (result)
{
memset(result, 0, count * size);
}
return result;
}
int _system_r(struct _reent *r, const char *str) {
abort();
return 0;
}
int _rename_r(struct _reent *r, const char *src, const char *dst) {
abort();
return 0;
}
clock_t _times_r(struct _reent *r, struct tms *ptms) {
abort();
return 0;
}
// TODO: read time from RTC
int _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz) {
abort();
return 0;
}
void _raise_r(struct _reent *r) {
abort();
}
int _unlink_r(struct _reent *r, const char *path) {
abort();
return 0;
}
int _link_r(struct _reent *r, const char* n1, const char* n2) {
abort();
return 0;
}
int _stat_r(struct _reent *r, const char * path, struct stat * st) {
return 0;
}
int _fstat_r(struct _reent *r, int fd, struct stat * st) {
st->st_mode = S_IFCHR;
return 0;
}
void* _sbrk_r(struct _reent *r, ptrdiff_t sz) {
abort();
return 0;
}
int _getpid_r(struct _reent *r) {
abort();
return 0;
}
int _kill_r(struct _reent *r, int pid, int sig) {
abort();
return 0;
}
void _exit_r(struct _reent *r, int e) {
abort();
}
int _close_r(struct _reent *r, int fd) {
return 0;
}
int _open_r(struct _reent *r, const char * path, int flags, int mode) {
return 0;
}
void _exit(int __status) {
abort();
}
ssize_t _write_r(struct _reent *r, int fd, const void * data, size_t size) {
const char *data_c = (const char *)data;
if (fd == STDOUT_FILENO) {
static _lock_t stdout_lock; /* lazily initialised */
/* Even though newlib does stream locking on stdout, we need
a dedicated stdout UART lock...
This is because each task has its own _reent structure with
unique FILEs for stdin/stdout/stderr, so these are
per-thread (lazily initialised by __sinit the first time a
stdio function is used, see findfp.c:235.
It seems like overkill to allocate a FILE-per-task and lock
a thread-local stream, but I see no easy way to fix this
(pre-__sinit_, tasks have "fake" FILEs ie __sf_fake_stdout
which aren't fully valid.)
*/
_lock_acquire_recursive(&stdout_lock);
for (size_t i = 0; i < size; i++) {
#if CONFIG_NEWLIB_STDOUT_ADDCR
if (data_c[i]=='\n') {
uart_tx_one_char('\r');
}
#endif
uart_tx_one_char(data_c[i]);
}
_lock_release_recursive(&stdout_lock);
}
return size;
}
_off_t _lseek_r(struct _reent *r, int fd, _off_t size, int mode) {
return 0;
}
// TODO: implement reading from UART
ssize_t _read_r(struct _reent *r, int fd, void * dst, size_t size) {
return 0;
}
/* Notes on our newlib lock implementation:
*
* - Use FreeRTOS mutex semaphores as locks.
@ -369,89 +205,3 @@ void IRAM_ATTR _lock_release(_lock_t *lock) {
void IRAM_ATTR _lock_release_recursive(_lock_t *lock) {
lock_release_generic(lock, queueQUEUE_TYPE_RECURSIVE_MUTEX);
}
// This function is not part on newlib API, it is defined in libc/stdio/local.h
// It is called as part of _reclaim_reent via a pointer in __cleanup member
// of struct _reent.
// This function doesn't call _fclose_r for _stdin, _stdout, _stderr members
// of struct reent. Not doing so causes a memory leak each time a task is
// terminated. We replace __cleanup member with _extra_cleanup_r (below) to work
// around this.
extern void _cleanup_r(struct _reent* r);
void _extra_cleanup_r(struct _reent* r)
{
_cleanup_r(r);
_fclose_r(r, r->_stdout);
_fclose_r(r, r->_stderr);
_fclose_r(r, r->_stdin);
}
static struct _reent s_reent;
/*
General ToDo that the Xtensa newlib support code did but we do not: Close every open fd a running task had when the task
is killed. Do we want that too? - JD
*/
extern int _printf_float(struct _reent *rptr,
void *pdata,
FILE * fp,
int (*pfunc) (struct _reent *, FILE *, _CONST char *, size_t len),
va_list * ap);
extern int _scanf_float(struct _reent *rptr,
void *pdata,
FILE *fp,
va_list *ap);
static struct syscall_stub_table s_stub_table = {
.__getreent = &__getreent,
._malloc_r = &_malloc_r,
._free_r = &_free_r,
._realloc_r = &_realloc_r,
._calloc_r = &_calloc_r,
._abort = &abort,
._system_r = &_system_r,
._rename_r = &_rename_r,
._times_r = &_times_r,
._gettimeofday_r = &_gettimeofday_r,
._raise_r = &_raise_r,
._unlink_r = &_unlink_r,
._link_r = &_link_r,
._stat_r = &_stat_r,
._fstat_r = &_fstat_r,
._sbrk_r = &_sbrk_r,
._getpid_r = &_getpid_r,
._kill_r = &_kill_r,
._exit_r = &_exit_r,
._close_r = &_close_r,
._open_r = &_open_r,
._write_r = (int (*)(struct _reent *r, int, const void *, int)) &_write_r,
._lseek_r = (int (*)(struct _reent *r, int, int, int)) &_lseek_r,
._read_r = (int (*)(struct _reent *r, int, void *, int)) &_read_r,
._lock_init = &_lock_init,
._lock_init_recursive = &_lock_init_recursive,
._lock_close = &_lock_close,
._lock_close_recursive = &_lock_close,
._lock_acquire = &_lock_acquire,
._lock_acquire_recursive = &_lock_acquire_recursive,
._lock_try_acquire = &_lock_try_acquire,
._lock_try_acquire_recursive = &_lock_try_acquire_recursive,
._lock_release = &_lock_release,
._lock_release_recursive = &_lock_release_recursive,
._printf_float = &_printf_float,
._scanf_float = &_scanf_float,
};
void ets_setup_syscalls() {
syscall_table_ptr_pro = &s_stub_table;
syscall_table_ptr_app = &s_stub_table;
_GLOBAL_REENT = &s_reent;
environ = malloc(sizeof(char*));
environ[0] = NULL;
}

View file

@ -0,0 +1,37 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ESP_NEWLIB_H__
#define __ESP_NEWLIB_H__
#include <sys/reent.h>
/**
* Replacement for newlib's _REENT_INIT_PTR and __sinit.
*
* Called from startup code and FreeRTOS, not intended to be called from
* application code.
*/
void esp_reent_init(struct _reent* r);
/**
* Function which sets up syscall table used by newlib functions in ROM.
*
* Called from the startup code, not intended to be called from application
* code.
*/
void esp_setup_syscalls();
#endif //__ESP_NEWLIB_H__

View file

@ -0,0 +1,45 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include <sys/reent.h>
#include "esp_attr.h"
/* This function is not part on newlib API, it is defined in libc/stdio/local.h
* There is no nice way to get __cleanup member populated while avoiding __sinit,
* so extern declaration is used here.
*/
extern void _cleanup_r(struct _reent* r);
/**
* This is the replacement for newlib's _REENT_INIT_PTR and __sinit.
* The problem with __sinit is that it allocates three FILE structures
* (stdin, stdout, stderr). Having individual standard streams for each task
* is a bit too much on a small embedded system. So we point streams
* to the streams of the global struct _reent, which are initialized in
* startup code.
*/
void IRAM_ATTR esp_reent_init(struct _reent* r)
{
memset(r, 0, sizeof(*r));
r->_stdout = _GLOBAL_REENT->_stdout;
r->_stderr = _GLOBAL_REENT->_stderr;
r->_stdin = _GLOBAL_REENT->_stdin;
r->__cleanup = &_cleanup_r;
r->__sdidinit = 1;
r->__sglue._next = NULL;
r->__sglue._niobs = 0;
r->__sglue._iobs = NULL;
r->_current_locale = "C";
}

View file

@ -0,0 +1,91 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <sys/unistd.h>
#include <sys/reent.h>
#include "rom/libc_stubs.h"
#include "esp_vfs.h"
static struct _reent s_reent;
extern int _printf_float(struct _reent *rptr,
void *pdata,
FILE * fp,
int (*pfunc) (struct _reent *, FILE *, _CONST char *, size_t len),
va_list * ap);
extern int _scanf_float(struct _reent *rptr,
void *pdata,
FILE *fp,
va_list *ap);
static struct syscall_stub_table s_stub_table = {
.__getreent = &__getreent,
._malloc_r = &_malloc_r,
._free_r = &_free_r,
._realloc_r = &_realloc_r,
._calloc_r = &_calloc_r,
._abort = &abort,
._system_r = &_system_r,
._rename_r = &esp_vfs_rename,
._times_r = &_times_r,
._gettimeofday_r = &_gettimeofday_r,
._raise_r = (void (*)(struct _reent *r)) &_raise_r,
._unlink_r = &esp_vfs_unlink,
._link_r = &esp_vfs_link,
._stat_r = &esp_vfs_stat,
._fstat_r = &esp_vfs_fstat,
._sbrk_r = &_sbrk_r,
._getpid_r = &_getpid_r,
._kill_r = &_kill_r,
._exit_r = NULL, // never called in ROM
._close_r = &esp_vfs_close,
._open_r = &esp_vfs_open,
._write_r = (int (*)(struct _reent *r, int, const void *, int)) &esp_vfs_write,
._lseek_r = (int (*)(struct _reent *r, int, int, int)) &esp_vfs_lseek,
._read_r = (int (*)(struct _reent *r, int, void *, int)) &esp_vfs_read,
._lock_init = &_lock_init,
._lock_init_recursive = &_lock_init_recursive,
._lock_close = &_lock_close,
._lock_close_recursive = &_lock_close,
._lock_acquire = &_lock_acquire,
._lock_acquire_recursive = &_lock_acquire_recursive,
._lock_try_acquire = &_lock_try_acquire,
._lock_try_acquire_recursive = &_lock_try_acquire_recursive,
._lock_release = &_lock_release,
._lock_release_recursive = &_lock_release_recursive,
._printf_float = &_printf_float,
._scanf_float = &_scanf_float,
};
void esp_setup_syscalls()
{
syscall_table_ptr_pro = &s_stub_table;
syscall_table_ptr_app = &s_stub_table;
_GLOBAL_REENT = &s_reent;
environ = malloc(sizeof(char*));
environ[0] = NULL;
}

View file

@ -0,0 +1,105 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sys/reent.h>
#include <stdlib.h>
#include "esp_attr.h"
#include "freertos/FreeRTOS.h"
void IRAM_ATTR abort()
{
do
{
__asm__ ("break 0,0");
*((int*) 0) = 0;
} while(true);
}
void* IRAM_ATTR _malloc_r(struct _reent *r, size_t size)
{
return pvPortMalloc(size);
}
void IRAM_ATTR _free_r(struct _reent *r, void* ptr)
{
vPortFree(ptr);
}
void* IRAM_ATTR _realloc_r(struct _reent *r, void* ptr, size_t size)
{
void* new_chunk;
if (size == 0) {
if (ptr) {
vPortFree(ptr);
}
return NULL;
}
new_chunk = pvPortMalloc(size);
if (new_chunk && ptr) {
memcpy(new_chunk, ptr, size);
vPortFree(ptr);
}
// realloc behaviour: don't free original chunk if alloc failed
return new_chunk;
}
void* IRAM_ATTR _calloc_r(struct _reent *r, size_t count, size_t size)
{
void* result = pvPortMalloc(count * size);
if (result)
{
memset(result, 0, count * size);
}
return result;
}
int _system_r(struct _reent *r, const char *str)
{
__errno_r(r) = ENOSYS;
return -1;
}
void _raise_r(struct _reent *r)
{
abort();
}
void* _sbrk_r(struct _reent *r, ptrdiff_t sz)
{
abort();
}
int _getpid_r(struct _reent *r)
{
__errno_r(r) = ENOSYS;
return -1;
}
int _kill_r(struct _reent *r, int pid, int sig)
{
__errno_r(r) = ENOSYS;
return -1;
}
void _exit(int __status)
{
abort();
}

35
components/newlib/time.c Normal file
View file

@ -0,0 +1,35 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/reent.h>
#include <sys/time.h>
#include <sys/times.h>
#include "esp_attr.h"
clock_t _times_r(struct _reent *r, struct tms *ptms)
{
__errno_r(r) = ENOSYS;
return (clock_t) -1;
}
// TODO: read time from RTC
int _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
{
__errno_r(r) = ENOSYS;
return -1;
}

128
components/vfs/README.rst Normal file
View file

@ -0,0 +1,128 @@
Virtual filesystem component
============================
Overview
--------
Virtual filesystem (VFS) component provides a unified interface for drivers which can perform operations on file-like objects. This can be a real filesystems (FAT, SPIFFS, etc.), or device drivers which exposes file-like interface.
This component allows C library functions, such as fopen and fprintf, to work with FS drivers. At high level, each FS driver is associated with some path prefix. When one of C library functions needs to open a file, VFS component searches for the FS driver associated with the file's path, and forwards the call to that driver. VFS also forwards read, write, and other calls for the given file to the same FS driver.
For example, one can register a FAT filesystem driver with ``/fat`` prefix, and call ``fopen("/fat/file.txt", "w")``. VFS component will then call ``open`` function of FAT driver and pass ``/file.txt`` argument to it (and appropriate mode flags). All subsequent calls to C library functions for the returned ``FILE*`` stream will also be forwarded to the FAT driver.
FS registration
---------------
To register an FS driver, application needs to define in instance of esp_vfs_t structure and populate it with function pointers to FS APIs::
esp_vfs_t myfs = {
.fd_offset = 0,
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &myfs_write,
.open = &myfs_open,
.fstat = &myfs_fstat,
.close = &myfs_close,
.read = &myfs_read,
.lseek = NULL,
.stat = NULL,
.link = NULL,
.unlink = NULL,
.rename = NULL
};
ESP_ERROR_CHECK(esp_vfs_register("/data", &myfs, NULL));
Depending on the way FS driver declares its APIs, either ``read``, ``write``, etc., or ``read_p``, ``write_p``, etc. should be used.
Case 1: API functions are declared without an extra context pointer (FS driver is a singleton)::
size_t myfs_write(int fd, const void * data, size_t size);
// In definition of esp_vfs_t:
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &myfs_write,
// ... other members initialized
// When registering FS, context pointer (third argument) is NULL:
ESP_ERROR_CHECK(esp_vfs_register("/data", &myfs, NULL));
Case 2: API functions are declared with an extra context pointer (FS driver supports multiple instances)::
size_t myfs_write(myfs_t* fs, int fd, const void * data, size_t size);
// In definition of esp_vfs_t:
.flags = ESP_VFS_FLAG_CONTEXT_PTR,
.write_p = &myfs_write,
// ... other members initialized
// When registering FS, pass the FS context pointer into the third argument
// (hypothetical myfs_mount function is used for illustrative purposes)
myfs_t* myfs_inst1 = myfs_mount(partition1->offset, partition1->size);
ESP_ERROR_CHECK(esp_vfs_register("/data1", &myfs, myfs_inst1));
// Can register another instance:
myfs_t* myfs_inst2 = myfs_mount(partition2->offset, partition2->size);
ESP_ERROR_CHECK(esp_vfs_register("/data2", &myfs, myfs_inst2));
Paths
-----
Each registered FS has a path prefix associated with it. This prefix may be considered a "mount point" of this partition.
Registering mount points which have another mount point as a prefix is not supported and results in undefined behavior. For instance, the following is correct and supported:
- FS 1 on /data/fs1
- FS 2 on /data/fs2
This **will not work** as expected:
- FS 1 on /data
- FS 2 on /data/fs2
When opening files, FS driver will only be given relative path to files. For example:
- ``myfs`` driver is registered with ``/data`` as path prefix
- and application calls ``fopen("/data/config.json", ...)``
- then VFS component will call ``myfs_open("/config.json", ...)``.
- ``myfs`` driver will open ``/config.json`` file
VFS doesn't impose a limit on total file path length, but it does limit FS path prefix to ``ESP_VFS_PATH_MAX`` characters. Individual FS drivers may have their own filename length limitations.
File descriptors
----------------
It is suggested that filesystem drivers should use small positive integers as file descriptors. VFS component assumes that ``CONFIG_MAX_FD_BITS`` bits (12 by default) are sufficient to represent a file descriptor.
If filesystem is configured with an option to offset all file descriptors by a constant value, such value should be passed to ``fd_offset`` field of ``esp_vfs_t`` structure. VFS component will then remove this offset when working with FDs of that specific FS, bringing them into the range of small positive integers.
While file descriptors returned by VFS component to newlib library are rarely seen by the application, the following details may be useful for debugging purposes. File descriptors returned by VFS component are composed of two parts: FS driver ID, and the actual file descriptor. Because newlib stores file descriptors as 16-bit integers, VFS component is also limited by 16 bits to store both parts.
Lower ``CONFIG_MAX_FD_BITS`` bits are used to store zero-based file descriptor. If FS driver has a non-zero ``fd_offset`` field, this ``fd_offset`` is subtracted FDs obtained from the FS ``open`` call, and the result is stored in the lower bits of the FD. Higher bits are used to save the index of FS in the internal table of registered filesystems.
When VFS component receives a call from newlib which has a file descriptor, this file descriptor is translated back to the FS-specific file descriptor. First, higher bits of FD are used to identify the FS. Then ``fd_offset`` field of the FS is added to the lower ``CONFIG_MAX_FD_BITS`` bits of the fd, and resulting FD is passed to the FS driver.
::
FD as seen by newlib FD as seen by FS driver
+-----+
+-------+---------------+ | | +------------------------+
| FS id | Zero—based FD | +---------------> sum +----> |
+---+---+------+--------+ | | | +------------------------+
| | | +--^--+
| +--------------+ |
| |
| +-------------+ |
| | Table of | |
| | registered | |
| | filesystems | |
| +-------------+ +-------------+ |
+-------> entry +----> esp_vfs_t | |
index +-------------+ | structure | |
| | | | |
| | | + fd_offset +---+
+-------------+ | |
+-------------+

1
components/vfs/component.mk Executable file
View file

@ -0,0 +1 @@
include $(IDF_PATH)/make/component_common.mk

View file

@ -0,0 +1,157 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ESP_VFS_H__
#define __ESP_VFS_H__
#include <stdint.h>
#include <stddef.h>
#include "esp_err.h"
#include <sys/types.h>
#include <sys/reent.h>
#include <sys/stat.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Maximum length of path prefix (not including zero terminator)
*/
#define ESP_VFS_PATH_MAX 15
/**
* Default value of flags member in esp_vfs_t structure.
*/
#define ESP_VFS_FLAG_DEFAULT 0
/**
* Flag which indicates that FS needs extra context pointer in syscalls.
*/
#define ESP_VFS_FLAG_CONTEXT_PTR 1
/**
* @brief VFS definition structure
*
* This structure should be filled with pointers to corresponding
* FS driver functions.
*
* If the FS implementation has an option to use certain offset for
* all file descriptors, this value should be passed into fd_offset
* field. Otherwise VFS component will translate all FDs to start
* at zero offset.
*
* Some FS implementations expect some state (e.g. pointer to some structure)
* to be passed in as a first argument. For these implementations,
* populate the members of this structure which have _p suffix, set
* flags member to ESP_VFS_FLAG_CONTEXT_PTR and provide the context pointer
* to esp_vfs_register function.
* If the implementation doesn't use this extra argument, populate the
* members without _p suffix and set flags memeber to ESP_VFS_FLAG_DEFAULT.
*
* If the FS driver doesn't provide some of the functions, set corresponding
* members to NULL.
*/
typedef struct
{
int fd_offset;
int flags;
union {
size_t (*write_p)(void* p, int fd, const void * data, size_t size);
size_t (*write)(int fd, const void * data, size_t size);
};
union {
off_t (*lseek_p)(void* p, int fd, off_t size, int mode);
off_t (*lseek)(int fd, off_t size, int mode);
};
union {
ssize_t (*read_p)(void* ctx, int fd, void * dst, size_t size);
ssize_t (*read)(int fd, void * dst, size_t size);
};
union {
int (*open_p)(void* ctx, const char * path, int flags, int mode);
int (*open)(const char * path, int flags, int mode);
};
union {
int (*close_p)(void* ctx, int fd);
int (*close)(int fd);
};
union {
int (*fstat_p)(void* ctx, int fd, struct stat * st);
int (*fstat)(int fd, struct stat * st);
};
union {
int (*stat_p)(void* ctx, const char * path, struct stat * st);
int (*stat)(const char * path, struct stat * st);
};
union {
int (*link_p)(void* ctx, const char* n1, const char* n2);
int (*link)(const char* n1, const char* n2);
};
union {
int (*unlink_p)(void* ctx, const char *path);
int (*unlink)(const char *path);
};
union {
int (*rename_p)(void* ctx, const char *src, const char *dst);
int (*rename)(const char *src, const char *dst);
};
} esp_vfs_t;
/**
* Register a virtual filesystem for given path prefix.
*
* @param base_path file path prefix associated with the filesystem.
* Must be a zero-terminated C string, up to ESP_VFS_PATH_MAX
* characters long, and at least 2 characters long.
* Name must start with a "/" and must not end with "/".
* For example, "/data" or "/dev/spi" are valid.
* These VFSes would then be called to handle file paths such as
* "/data/myfile.txt" or "/dev/spi/0".
* @param vfs Pointer to esp_vfs_t, a structure which maps syscalls to
* the filesystem driver functions. VFS component doesn't
* assume ownership of this pointer.
* @param ctx If vfs->flags has ESP_VFS_FLAG_CONTEXT_PTR set, a pointer
* which should be passed to VFS functions. Otherwise, NULL.
*
* @return ESP_OK if successful, ESP_ERR_NO_MEM if too many VFSes are
* registered.
*/
esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx);
/**
* These functions are to be used in newlib syscall table. They will be called by
* newlib when it needs to use any of the syscalls.
*/
ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size);
off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode);
ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size);
int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode);
int esp_vfs_close(struct _reent *r, int fd);
int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st);
int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st);
int esp_vfs_link(struct _reent *r, const char* n1, const char* n2);
int esp_vfs_unlink(struct _reent *r, const char *path);
int esp_vfs_rename(struct _reent *r, const char *src, const char *dst);
#ifdef __cplusplus
} // extern "C"
#endif
#endif //__ESP_VFS_H__

View file

@ -0,0 +1,28 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ESP_VFS_DEV_H__
#define __ESP_VFS_DEV_H__
#include "esp_vfs.h"
/**
* @brief add /dev/uart virtual filesystem driver
*
* This function is called from startup code to enable serial output
*/
void esp_vfs_dev_uart_register();
#endif //__ESP_VFS_DEV_H__

285
components/vfs/vfs.c Normal file
View file

@ -0,0 +1,285 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/errno.h>
#include "esp_vfs.h"
#include "esp_log.h"
/*
* File descriptors visible by the applications are composed of two parts.
* Lower CONFIG_MAX_FD_BITS bits are used for the actual file descriptor.
* Next (16 - CONFIG_MAX_FD_BITS - 1) bits are used to identify the VFS this
* descriptor corresponds to.
* Highest bit is zero.
* We can only use 16 bits because newlib stores file descriptor as short int.
*/
#ifndef CONFIG_MAX_FD_BITS
#define CONFIG_MAX_FD_BITS 12
#endif
#define MAX_VFS_ID_BITS (16 - CONFIG_MAX_FD_BITS - 1)
// mask of actual file descriptor (e.g. 0x00000fff)
#define VFS_FD_MASK ((1 << CONFIG_MAX_FD_BITS) - 1)
// max number of VFS entries
#define VFS_MAX_COUNT ((1 << MAX_VFS_ID_BITS) - 1)
// mask of VFS id (e.g. 0x00007000)
#define VFS_INDEX_MASK (VFS_MAX_COUNT << CONFIG_MAX_FD_BITS)
#define VFS_INDEX_S CONFIG_MAX_FD_BITS
typedef struct vfs_entry_ {
esp_vfs_t vfs; // contains pointers to VFS functions
char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS
size_t path_prefix_len; // micro-optimization to avoid doing extra strlen
void* ctx; // optional pointer which can be passed to VFS
int offset; // index of this structure in s_vfs array
} vfs_entry_t;
static vfs_entry_t* s_vfs[VFS_MAX_COUNT] = { 0 };
static size_t s_vfs_count = 0;
esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx)
{
if (s_vfs_count >= VFS_MAX_COUNT) {
return ESP_ERR_NO_MEM;
}
size_t len = strlen(base_path);
if (len < 2 || len > ESP_VFS_PATH_MAX) {
return ESP_ERR_INVALID_ARG;
}
if (base_path[0] != '/' || base_path[len - 1] == '/') {
return ESP_ERR_INVALID_ARG;
}
vfs_entry_t *entry = (vfs_entry_t*) malloc(sizeof(vfs_entry_t));
if (entry == NULL) {
return ESP_ERR_NO_MEM;
}
strcpy(entry->path_prefix, base_path); // we have already verified argument length
memcpy(&entry->vfs, vfs, sizeof(esp_vfs_t));
entry->path_prefix_len = len;
entry->ctx = ctx;
entry->offset = s_vfs_count;
s_vfs[s_vfs_count] = entry;
++s_vfs_count;
return ESP_OK;
}
static const vfs_entry_t* get_vfs_for_fd(int fd)
{
int index = ((fd & VFS_INDEX_MASK) >> VFS_INDEX_S);
if (index >= s_vfs_count) {
return NULL;
}
return s_vfs[index];
}
static int translate_fd(const vfs_entry_t* vfs, int fd)
{
return (fd & VFS_FD_MASK) + vfs->vfs.fd_offset;
}
static const char* translate_path(const vfs_entry_t* vfs, const char* src_path)
{
assert(strncmp(src_path, vfs->path_prefix, vfs->path_prefix_len) == 0);
return src_path + vfs->path_prefix_len;
}
static const vfs_entry_t* get_vfs_for_path(const char* path)
{
size_t len = strlen(path);
for (size_t i = 0; i < s_vfs_count; ++i) {
const vfs_entry_t* vfs = s_vfs[i];
if (len < vfs->path_prefix_len + 1) { // +1 is for the trailing slash after base path
continue;
}
if (memcmp(path, vfs->path_prefix, vfs->path_prefix_len) != 0) { // match prefix
continue;
}
if (path[vfs->path_prefix_len] != '/') { // don't match "/data" prefix for "/data1/foo.txt"
continue;
}
return vfs;
}
return NULL;
}
/*
* Using huge multi-line macros is never nice, but in this case
* the only alternative is to repeat this chunk of code (with different function names)
* for each syscall being implemented. Given that this define is contained within a single
* file, this looks like a good tradeoff.
*
* First we check if syscall is implemented by VFS (corresponding member is not NULL),
* then call the right flavor of the method (e.g. open or open_p) depending on
* ESP_VFS_FLAG_CONTEXT_PTR flag. If ESP_VFS_FLAG_CONTEXT_PTR is set, context is passed
* in as first argument and _p variant is used for the call.
* It is enough to check just one of them for NULL, as both variants are part of a union.
*/
#define CHECK_AND_CALL(ret, r, pvfs, func, ...) \
if (pvfs->vfs.func == NULL) { \
__errno_r(r) = ENOSYS; \
return -1; \
} \
if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
ret = (*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
} else { \
ret = (*pvfs->vfs.func)(__VA_ARGS__);\
}
int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode)
{
const vfs_entry_t* vfs = get_vfs_for_path(path);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}
const char* path_within_vfs = translate_path(vfs, path);
int ret;
CHECK_AND_CALL(ret, r, vfs, open, path_within_vfs, flags, mode);
return ret - vfs->vfs.fd_offset + (vfs->offset << VFS_INDEX_S);
}
ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
if (vfs == NULL) {
__errno_r(r) = EBADF;
return -1;
}
int local_fd = translate_fd(vfs, fd);
int ret;
CHECK_AND_CALL(ret, r, vfs, write, local_fd, data, size);
return ret;
}
off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
if (vfs == NULL) {
__errno_r(r) = EBADF;
return -1;
}
int local_fd = translate_fd(vfs, fd);
int ret;
CHECK_AND_CALL(ret, r, vfs, lseek, local_fd, size, mode);
return ret;
}
ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
if (vfs == NULL) {
__errno_r(r) = EBADF;
return -1;
}
int local_fd = translate_fd(vfs, fd);
int ret;
CHECK_AND_CALL(ret, r, vfs, read, local_fd, dst, size);
return ret;
}
int esp_vfs_close(struct _reent *r, int fd)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
if (vfs == NULL) {
__errno_r(r) = EBADF;
return -1;
}
int local_fd = translate_fd(vfs, fd);
int ret;
CHECK_AND_CALL(ret, r, vfs, close, local_fd);
return ret;
}
int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
if (vfs == NULL) {
__errno_r(r) = EBADF;
return -1;
}
int local_fd = translate_fd(vfs, fd);
int ret;
CHECK_AND_CALL(ret, r, vfs, fstat, local_fd, st);
return ret;
}
int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st)
{
const vfs_entry_t* vfs = get_vfs_for_path(path);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}
const char* path_within_vfs = translate_path(vfs, path);
int ret;
CHECK_AND_CALL(ret, r, vfs, stat, path_within_vfs, st);
return ret;
}
int esp_vfs_link(struct _reent *r, const char* n1, const char* n2)
{
const vfs_entry_t* vfs = get_vfs_for_path(n1);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}
const vfs_entry_t* vfs2 = get_vfs_for_path(n2);
if (vfs != vfs2) {
__errno_r(r) = EXDEV;
return -1;
}
const char* path1_within_vfs = translate_path(vfs, n1);
const char* path2_within_vfs = translate_path(vfs, n2);
int ret;
CHECK_AND_CALL(ret, r, vfs, link, path1_within_vfs, path2_within_vfs);
return ret;
}
int esp_vfs_unlink(struct _reent *r, const char *path)
{
const vfs_entry_t* vfs = get_vfs_for_path(path);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}
const char* path_within_vfs = translate_path(vfs, path);
int ret;
CHECK_AND_CALL(ret, r, vfs, unlink, path_within_vfs);
return ret;
}
int esp_vfs_rename(struct _reent *r, const char *src, const char *dst)
{
const vfs_entry_t* vfs = get_vfs_for_path(src);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}
const vfs_entry_t* vfs_dst = get_vfs_for_path(dst);
if (vfs != vfs_dst) {
__errno_r(r) = EXDEV;
return -1;
}
const char* src_within_vfs = translate_path(vfs, src);
const char* dst_within_vfs = translate_path(vfs, dst);
int ret;
CHECK_AND_CALL(ret, r, vfs, rename, src_within_vfs, dst_within_vfs);
return ret;
}

102
components/vfs/vfs_uart.c Normal file
View file

@ -0,0 +1,102 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include "esp_vfs.h"
#include "esp_attr.h"
#include "sys/errno.h"
#include "sys/lock.h"
#include "soc/uart_struct.h"
static uart_dev_t* s_uarts[3] = {&UART0, &UART1, &UART2};
static _lock_t s_uart_locks[3]; // per-UART locks, lazily initialized
static int IRAM_ATTR uart_open(const char * path, int flags, int mode)
{
// this is fairly primitive, we should check if file is opened read only,
// and error out if write is requested
if (strcmp(path, "/0") == 0) {
return 0;
} else if (strcmp(path, "/1") == 0) {
return 1;
} else if (strcmp(path, "/2") == 0) {
return 2;
}
errno = ENOENT;
return -1;
}
static void IRAM_ATTR uart_tx_char(uart_dev_t* uart, int c)
{
while (uart->status.txfifo_cnt >= 127) {
;
}
uart->fifo.rw_byte = c;
}
static size_t IRAM_ATTR uart_write(int fd, const void * data, size_t size)
{
assert(fd >=0 && fd < 3);
const char *data_c = (const char *)data;
uart_dev_t* uart = s_uarts[fd];
/*
* Even though newlib does stream locking on each individual stream, we need
* a dedicated UART lock if two streams (stdout and stderr) point to the
* same UART.
*/
_lock_acquire_recursive(&s_uart_locks[fd]);
for (size_t i = 0; i < size; i++) {
#if CONFIG_NEWLIB_STDOUT_ADDCR
if (data_c[i]=='\n') {
uart_tx_char(uart, '\r');
}
#endif
uart_tx_char(uart, data_c[i]);
}
_lock_release_recursive(&s_uart_locks[fd]);
return size;
}
static int IRAM_ATTR uart_fstat(int fd, struct stat * st)
{
assert(fd >=0 && fd < 3);
st->st_mode = S_IFCHR;
return 0;
}
static int IRAM_ATTR uart_close(int fd)
{
assert(fd >=0 && fd < 3);
return 0;
}
void esp_vfs_dev_uart_register()
{
esp_vfs_t vfs = {
.fd_offset = 0,
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &uart_write,
.open = &uart_open,
.fstat = &uart_fstat,
.close = &uart_close,
.read = NULL, // TODO: implement reading from UART
.lseek = NULL,
.stat = NULL,
.link = NULL,
.unlink = NULL,
.rename = NULL
};
ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &vfs, NULL));
}

View file

@ -59,9 +59,9 @@ function run_tests()
print_status "Updating component source file rebuilds component"
# touch a file & do a build
take_build_snapshot
touch ${IDF_PATH}/components/esp32/syscalls.c
touch ${IDF_PATH}/components/esp32/cpu_start.c
make || failure "Failed to partial build"
assert_rebuilt ${APP_BINS} esp32/libesp32.a esp32/syscalls.o
assert_rebuilt ${APP_BINS} esp32/libesp32.a esp32/cpu_start.o
assert_not_rebuilt lwip/liblwip.a freertos/libfreertos.a ${BOOTLOADER_BINS} partitions_singleapp.bin
print_status "Bootloader source file rebuilds bootloader"