mtdconfig: support ram_mtdconfig device && lomtdconfig device

For nvs test in qemu

Signed-off-by: guohao15 <guohao15@xiaomi.com>
This commit is contained in:
guohao15
2024-12-16 17:05:58 +08:00
committed by Donny(董九柱)
parent 2c1a615442
commit 5e117fda60
2 changed files with 52 additions and 2 deletions
+49 -2
View File
@@ -42,6 +42,9 @@
#include <nuttx/fs/ioctl.h> #include <nuttx/fs/ioctl.h>
#include <nuttx/fs/loopmtd.h> #include <nuttx/fs/loopmtd.h>
#include <nuttx/mtd/mtd.h> #include <nuttx/mtd/mtd.h>
#ifdef CONFIG_MTD_CONFIG
# include <nuttx/mtd/configdata.h>
#endif
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
@@ -551,8 +554,14 @@ static int filemtd_ioctl(FAR struct mtd_dev_s *dev, int cmd,
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_MTD_LOOP #ifdef CONFIG_MTD_LOOP
# ifdef CONFIG_MTD_CONFIG
static int mtd_loop_setup(FAR const char *devname, FAR const char *filename,
int sectsize, int erasesize, off_t offset,
int configdata)
# else
static int mtd_loop_setup(FAR const char *devname, FAR const char *filename, static int mtd_loop_setup(FAR const char *devname, FAR const char *filename,
int sectsize, int erasesize, off_t offset) int sectsize, int erasesize, off_t offset)
# endif
{ {
FAR struct mtd_dev_s *mtd; FAR struct mtd_dev_s *mtd;
int ret; int ret;
@@ -563,7 +572,29 @@ static int mtd_loop_setup(FAR const char *devname, FAR const char *filename,
return -ENOENT; return -ENOENT;
} }
ret = register_mtddriver(devname, mtd, 0755, NULL); # ifdef CONFIG_MTD_CONFIG
if (configdata)
{
if (configdata == 2)
{
/* Try to erase the entire device, before register */
FAR struct file_dev_s *fdev = (FAR struct file_dev_s *)mtd;
mtd->erase(mtd, offset / erasesize, fdev->nblocks);
}
ret = mtdconfig_register_by_path(mtd, devname);
if (ret == -EDEADLK)
{
ferr("ERROR: mtdconfig_register_by_path failed: %d\n", ret);
}
}
else
# endif
{
ret = register_mtddriver(devname, mtd, 0755, NULL);
}
if (ret != OK) if (ret != OK)
{ {
filemtd_teardown(mtd); filemtd_teardown(mtd);
@@ -615,7 +646,17 @@ static int mtd_loop_teardown(FAR const char *devname)
/* Now teardown the filemtd */ /* Now teardown the filemtd */
filemtd_teardown(&dev->mtd); filemtd_teardown(&dev->mtd);
unregister_mtddriver(devname);
# ifdef CONFIG_MTD_CONFIG
if (inode->i_private)
{
mtdconfig_unregister_by_path(devname);
}
else
# endif
{
unregister_mtddriver(devname);
}
return OK; return OK;
} }
@@ -674,9 +715,15 @@ static int mtd_loop_ioctl(FAR struct file *filep, int cmd,
} }
else else
{ {
# ifdef CONFIG_MTD_CONFIG
ret = mtd_loop_setup(setup->devname, setup->filename,
setup->sectsize, setup->erasesize,
setup->offset, setup->configdata);
# else
ret = mtd_loop_setup(setup->devname, setup->filename, ret = mtd_loop_setup(setup->devname, setup->filename,
setup->sectsize, setup->erasesize, setup->sectsize, setup->erasesize,
setup->offset); setup->offset);
# endif
} }
} }
break; break;
+3
View File
@@ -75,6 +75,9 @@ struct mtd_losetup_s
size_t erasesize; /* The erase size to use on the file */ size_t erasesize; /* The erase size to use on the file */
size_t sectsize; /* The sector / page size of the file */ size_t sectsize; /* The sector / page size of the file */
off_t offset; /* An offset that may be applied to the device */ off_t offset; /* An offset that may be applied to the device */
# ifdef CONFIG_MTD_CONFIG
int configdata; /* 1: register mtdconfig device, 2: erase before register */
# endif
}; };
#endif #endif