Add a ROMFS file system for testing the P-Code binary format

This commit is contained in:
Gregory Nutt
2014-05-08 11:08:01 -06:00
parent e2b8eb6aad
commit 7fffa72ee5
11 changed files with 387 additions and 12 deletions
+142 -5
View File
@@ -39,12 +39,16 @@
#include <nuttx/config.h>
#include <fcntl.h>
#include <sys/mount.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/poff.h>
#include <nuttx/fs/ramdisk.h>
#include <nuttx/binfmt/binfmt.h>
#include <nuttx/binfmt/pcode.h>
@@ -53,6 +57,48 @@
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Check configuration. This is not all of the configuration settings that
* are required -- only the more obvious.
*/
#if CONFIG_NFILE_DESCRIPTORS < 1
# error "You must provide file descriptors via CONFIG_NFILE_DESCRIPTORS in your configuration file"
#endif
#ifdef CONFIG_BINFMT_DISABLE
# error "The binary loader is disabled (CONFIG_BINFMT_DISABLE)!"
#endif
#ifndef CONFIG_PCODE
# error "You must select CONFIG_PCODE in your configuration file"
#endif
#ifdef CONFIG_PCODE_TEST_FS
# ifndef CONFIG_FS_ROMFS
# error "You must select CONFIG_FS_ROMFS in your configuration file"
# endif
# ifdef CONFIG_DISABLE_MOUNTPOINT
# error "You must not disable mountpoints via CONFIG_DISABLE_MOUNTPOINT in your configuration file"
# endif
# ifndef CONFIG_PCODE_TEST_DEVMINOR
# define CONFIG_PCODE_TEST_DEVMINOR 0
# endif
# ifndef CONFIG_PCODE_TEST_DEVPATH
# define CONFIG_PCODE_TEST_DEVPATH "/dev/ram0"
# endif
# ifndef CONFIG_PCODE_TEST_MOUNTPOINT
# define CONFIG_PCODE_TEST_MOUNTPOINT "/bin"
# endif
#endif
/* Describe the ROMFS file system */
#define SECTORSIZE 512
#define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
/****************************************************************************
* Private Function Prototypes
@@ -70,10 +116,70 @@ static struct binfmt_s g_pcode_binfmt =
pcode_loadbinary, /* load */
};
#ifdef CONFIG_PCODE_TEST_FS
# include "romfs.h"
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: pcode_mount_testfs
*
* Description:
* If so configured, then mount the P-Code test file system
*
****************************************************************************/
#ifdef CONFIG_PCODE_TEST_FS
static int pcode_mount_testfs(void)
{
int ret;
/* Create a ROM disk for the ROMFS filesystem */
bvdbg("Registering romdisk at /dev/ram%d\n", CONFIG_PCODE_TEST_DEVMINOR);
ret = romdisk_register(CONFIG_PCODE_TEST_DEVMINOR, (FAR uint8_t *)romfs_img,
NSECTORS(ROMFS_IMG_LEN), SECTORSIZE);
if (ret < 0)
{
bdbg("ERROR: romdisk_register failed: %d\n", ret);
return ret;
}
/* Mount the test file system */
bvdbg("Mounting ROMFS filesystem at target=%s with source=%s\n",
CONFIG_PCODE_TEST_MOUNTPOINT, CONFIG_PCODE_TEST_DEVPATH);
ret = mount(CONFIG_PCODE_TEST_DEVPATH, CONFIG_PCODE_TEST_MOUNTPOINT,
"romfs", MS_RDONLY, NULL);
if (ret < 0)
{
int errval = errno;
DEBUGASSERT(errval > 0);
bdbg("ERROR: mount(%s,%s,romfs) failed: %d\n",
CONFIG_PCODE_TEST_DEVPATH, CONFIG_PCODE_TEST_MOUNTPOINT, errval);
return -errval;
}
/* Does the system support the PATH variable? Has the PATH variable
* already been set? If YES and NO, then set the PATH variable to
* the ROMFS mountpoint.
*/
#if defined(CONFIG_BINFMT_EXEPATH) && !defined(CONFIG_PATH_INITIAL)
(void)setenv("PATH", CONFIG_PCODE_TEST_MOUNTPOINT, 1);
#endif
return OK;
}
#else
# define pcode_mount_testfs() (OK)
#endif
/****************************************************************************
* Name: pcode_proxy
*
@@ -105,7 +211,6 @@ static int pcode_proxy(int argc, char **argv)
static int pcode_loadbinary(struct binary_s *binp)
{
FAR struct poff_fileheader_s hdr;
FAR const struct pcode_s *b;
FAR uint8_t *ptr;
size_t remaining;
ssize_t nread;
@@ -154,7 +259,7 @@ static int pcode_loadbinary(struct binary_s *binp)
{
/* Set up for the next gulp */
DEBUASSERT(nread > 0 && nread <=remaining);
DEBUGASSERT(nread > 0 && nread <=remaining);
remaining -= nread;
ptr += nread;
}
@@ -213,6 +318,15 @@ int pcode_initialize(void)
{
int ret;
/* Mount the test file system */
ret = pcode_mount_testfs();
if (ret < 0)
{
bdbg("ERROR: Failed to mount test file system: %d\n", ret);
return ret;
}
/* Register ourselves as a binfmt loader */
bvdbg("Registering P-Code Loader\n");
@@ -239,8 +353,31 @@ int pcode_initialize(void)
void pcode_uninitialize(void)
{
unregister_binfmt(&g_pcode_binfmt);
int ret;
/* Unregister the binary format */
ret = unregister_binfmt(&g_pcode_binfmt);
if (ret < 0)
{
int errval = errno;
DEBUGASSERT(errval > 0);
bdbg("ERROR: unregister_binfmt() failed: %d\n", errval);
UNUSED(errval);
}
#ifdef CONFIG_PCODE_TEST_FS
ret = umount(CONFIG_PCODE_TEST_MOUNTPOINT);
if (ret < 0)
{
int errval = errno;
DEBUGASSERT(errval > 0);
bdbg("ERROR: umount(%s) failed: %d\n", CONFIG_PCODE_TEST_MOUNTPOINT, errval);
UNUSED(errval);
}
#endif
}
#endif /* CONFIG_PCODE */