Skip to content

Commit

Permalink
Add a simple heap-backed VFS device
Browse files Browse the repository at this point in the history
Mostly for testing.

CL: Add a simple heap-backed VFS device, mostly for testing

PUBLISHED_FROM=0ccc10057fe394f70fd49079a039728e188462e8
  • Loading branch information
Deomid Ryabkov authored and cesantabot committed Jan 25, 2018
1 parent e17ee31 commit 255db75
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fw/platforms/cc3200/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ MGOS_SRCS = mgos_event.c \
mgos_time.c mgos_hw_timers.c mgos_timers.c \
mgos_config_util.c mgos_sys_config.c \
mgos_dlsym.c mgos_system.c \
mgos_vfs.c mgos_vfs_dev.c mgos_vfs_fs_spiffs.c \
mgos_vfs.c mgos_vfs_dev.c mgos_vfs_dev_ram.c mgos_vfs_fs_spiffs.c \
$(notdir $(MGOS_CONFIG_C)) $(notdir $(MGOS_RO_VARS_C)) \
cs_crc32.c cs_file.c \
cs_frbuf.c mgos_utils.c \
Expand Down
2 changes: 1 addition & 1 deletion fw/platforms/cc3220/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ MGOS_SRCS += $(notdir $(wildcard $(MGOS_CC3220_PATH)/src/*.c)) \
mgos_hal_freertos.c mgos_init.c \
mgos_mongoose.c mgos_sys_config.c \
mgos_hw_timers.c mgos_system.c mgos_time.c mgos_timers.c mgos_uart.c mgos_utils.c \
mgos_vfs.c mgos_vfs_dev.c mgos_vfs_fs_spiffs.c \
mgos_vfs.c mgos_vfs_dev.c mgos_vfs_dev_ram.c mgos_vfs_fs_spiffs.c \
cc32xx_crypto.c cc32xx_exc.c arm_exc.c arm_exc_top.S cc32xx_fs.c cc32xx_gpio.c \
cc32xx_hal.c cc32xx_hw_timers.c cc32xx_libc.c cc32xx_main.c cc32xx_sl_spawn.c cc32xx_uart.c \
cc32xx_vfs.c cc32xx_vfs_fs_slfs.c \
Expand Down
2 changes: 1 addition & 1 deletion fw/platforms/esp32/src/esp32_src.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MGOS_SRCS += mgos_config_util.c mgos_core_dump.c mgos_dlsym.c mgos_event.c mgos_
mgos_gpio.c mgos_init.c mgos_mmap_esp.c mgos_mongoose.c \
mgos_sys_config.c $(notdir $(MGOS_CONFIG_C)) $(notdir $(MGOS_RO_VARS_C)) \
mgos_hw_timers.c mgos_system.c mgos_time.c mgos_timers.c mgos_uart.c mgos_utils.c \
mgos_vfs.c mgos_vfs_dev.c mgos_vfs_fs_spiffs.c \
mgos_vfs.c mgos_vfs_dev.c mgos_vfs_dev_ram.c mgos_vfs_fs_spiffs.c \
esp32_crypto.c esp32_debug.c esp32_exc.c esp32_fs.c esp32_fs_crypt.c \
esp32_vfs_dev_partition.c \
esp32_gpio.c esp32_hal.c esp32_hw_timers.c \
Expand Down
1 change: 1 addition & 0 deletions fw/platforms/esp8266/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ MGOS_SRCS = cs_file.c cs_rbuf.c \
mgos_utils.c \
mgos_vfs.c \
mgos_vfs_dev.c \
mgos_vfs_dev_ram.c \
mgos_vfs_fs_spiffs.c \
cs_crc32.c \
rboot-bigflash.c rboot-api.c \
Expand Down
152 changes: 152 additions & 0 deletions fw/src/mgos_vfs_dev_ram.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright (c) 2014-2018 Cesanta Software Limited
* All rights reserved
*/

#include "mgos_vfs_dev_ram.h"

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include "common/cs_dbg.h"

#include "frozen/frozen.h"

#include "mgos_hal.h"
#include "mgos_vfs.h"
#include "mgos_vfs_dev.h"

struct mgos_vfs_dev_ram_data {
size_t size;
uint8_t *data;
bool flash_check;
uint8_t erase_byte;
};

static bool mgos_vfs_dev_ram_open(struct mgos_vfs_dev *dev, const char *opts) {
bool res = false;
struct mgos_vfs_dev_ram_data *dd = NULL;
uint8_t fb = 0xff;
int size = 0, flash_check = false, erase_byte = 0xff, fill_byte = 0xff;
json_scanf(opts, strlen(opts),
"{size: %d, erase_byte: %d, fill_byte: %d, flash_check: %B}",
&size, &erase_byte, &fill_byte, &flash_check);
if (size <= 0) {
LOG(LL_ERROR, ("Size is required for RAM device"));
goto out;
}
dd = (struct mgos_vfs_dev_ram_data *) calloc(1, sizeof(*dd));
dd->size = size;
dd->data = (uint8_t *) malloc(dd->size);
dd->flash_check = flash_check;
dd->erase_byte = (uint8_t) erase_byte;
if (dd->data == NULL) goto out;
fb = (uint8_t) fill_byte;
memset(dd->data, fb, dd->size);
dev->dev_data = dd;
res = true;

out:
if (!res && dd != NULL) {
free(dd->data);
free(dd);
} else {
LOG(LL_INFO, ("%u bytes, eb 0x%02x, fb 0x%02x, fc %s", dd->size,
dd->erase_byte, fb, (dd->flash_check ? "yes" : "no")));
}
return res;
}

static bool mgos_vfs_dev_ram_read(struct mgos_vfs_dev *dev, size_t offset,
size_t len, void *dst) {
bool res = false;
struct mgos_vfs_dev_ram_data *dd =
(struct mgos_vfs_dev_ram_data *) dev->dev_data;
if (len > dd->size || offset + len > dd->size) {
goto out;
}
memcpy(dst, dd->data + offset, len);
res = true;

out:
LOG((res ? LL_DEBUG : LL_ERROR),
("%s %u @ 0x%x = %d", "read", len, offset, res));
return res;
}

static bool mgos_vfs_dev_ram_write(struct mgos_vfs_dev *dev, size_t offset,
size_t len, const void *src) {
bool res = false;
uint8_t *srcb = (uint8_t *) src;
struct mgos_vfs_dev_ram_data *dd =
(struct mgos_vfs_dev_ram_data *) dev->dev_data;
if (len > dd->size || offset + len > dd->size) {
goto out;
}
for (size_t i = 0; i < len; i++) {
uint8_t src_byte = srcb[i];
uint8_t dst_byte = dd->data[offset + i];
if (dd->flash_check) {
if ((src_byte & dst_byte) != src_byte) {
LOG(LL_ERROR, ("NOR flash check violation @ %u: 0x%02x -> 0x%02x",
offset + i, dst_byte, src_byte));
goto out;
}
}
dd->data[offset + i] = src_byte;
}

res = true;

out:
LOG((res ? LL_DEBUG : LL_ERROR),
("%s %u @ 0x%x = %d", "write", len, offset, res));
return res;
}

static bool mgos_vfs_dev_ram_erase(struct mgos_vfs_dev *dev, size_t offset,
size_t len) {
bool res = false;
struct mgos_vfs_dev_ram_data *dd =
(struct mgos_vfs_dev_ram_data *) dev->dev_data;
if (len > dd->size || offset + len > dd->size) {
goto out;
}
memset(dd->data + offset, 0xff, len);
res = true;

out:
LOG((res ? LL_DEBUG : LL_ERROR),
("%s %u @ 0x%x = %d", "erase", len, offset, res));
return res;
}

static size_t mgos_vfs_dev_ram_get_size(struct mgos_vfs_dev *dev) {
struct mgos_vfs_dev_ram_data *dd =
(struct mgos_vfs_dev_ram_data *) dev->dev_data;
return dd->size;
}

static bool mgos_vfs_dev_ram_close(struct mgos_vfs_dev *dev) {
struct mgos_vfs_dev_ram_data *dd =
(struct mgos_vfs_dev_ram_data *) dev->dev_data;
free(dd->data);
free(dd);
return true;
}

static const struct mgos_vfs_dev_ops mgos_vfs_dev_ram_ops = {
.open = mgos_vfs_dev_ram_open,
.read = mgos_vfs_dev_ram_read,
.write = mgos_vfs_dev_ram_write,
.erase = mgos_vfs_dev_ram_erase,
.get_size = mgos_vfs_dev_ram_get_size,
.close = mgos_vfs_dev_ram_close,
};

bool mgos_vfs_dev_ram_register_type(void) {
return mgos_vfs_dev_register_type(MGOS_VFS_DEV_TYPE_RAM,
&mgos_vfs_dev_ram_ops);
}
25 changes: 25 additions & 0 deletions fw/src/mgos_vfs_dev_ram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2014-2018 Cesanta Software Limited
* All rights reserved
*/

#ifndef CS_FW_SRC_MGOS_VFS_DEV_RAM_H_
#define CS_FW_SRC_MGOS_VFS_DEV_RAM_H_

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/* A simple heap-backed VFS device, mostly for testing. */

#define MGOS_VFS_DEV_TYPE_RAM "RAM"

bool mgos_vfs_dev_ram_register_type(void);

#ifdef __cplusplus
}
#endif

#endif /* CS_FW_SRC_MGOS_VFS_DEV_RAM_H_ */

0 comments on commit 255db75

Please sign in to comment.