From 31740bbb206b1660d3a0532cab4668c6af3c8b68 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Thu, 23 Mar 2017 14:51:17 +0000 Subject: [PATCH] flash: map existing file to memory to use as sandbox flash --- source/drivers/sandbox_flash.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/source/drivers/sandbox_flash.c b/source/drivers/sandbox_flash.c index afeadfb..ca11cd2 100644 --- a/source/drivers/sandbox_flash.c +++ b/source/drivers/sandbox_flash.c @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -11,6 +12,7 @@ #define CONFIG_FLASH_SIZE 4 * MIB struct stdio_flash_priv { + FILE *fp; char *data; size_t size; }; @@ -76,6 +78,7 @@ static int stdio_flash_erase(struct flash_dev *dev, uint32_t addr, struct flash_dev *flash_probe(void) { + struct stat st; struct flash_dev *dev; struct stdio_flash_priv *priv; @@ -85,18 +88,25 @@ struct flash_dev *flash_probe(void) if (!(priv = malloc(sizeof *priv))) goto err_free_dev; - if (!(priv->data = mmap(NULL, CONFIG_FLASH_SIZE, PROT_READ | PROT_WRITE, - MAP_ANONYMOUS | MAP_PRIVATE, -1, 0))) + if (!(priv->fp = fopen("flash.img", "r+b"))) goto err_free_priv; - memset(priv->data, 0xFF, CONFIG_FLASH_SIZE); - priv->size = CONFIG_FLASH_SIZE; + if (fstat(fileno(priv->fp), &st) < 0) + goto err_close_fp; + + priv->size = st.st_size; + + if (!(priv->data = mmap(NULL, priv->size, PROT_READ | PROT_WRITE, + MAP_PRIVATE, fileno(priv->fp), 0))) + goto err_close_fp; dev->priv = priv; dev->ops = &stdio_flash_ops; return dev; +err_close_fp: + fclose(priv->fp); err_free_priv: free(priv); err_free_dev: