diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2014-05-08 11:08:01 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2014-05-08 11:08:01 -0600 |
commit | 3b785276e679bc8be4cb3046d757fb6caa40927d (patch) | |
tree | 6daec662562b3453114111b9f596f576ee9e633e /nuttx/binfmt/pcode.c | |
parent | 28c530634b32b793249bb66f0f08dd138d8de7bf (diff) | |
download | px4-nuttx-3b785276e679bc8be4cb3046d757fb6caa40927d.tar.gz px4-nuttx-3b785276e679bc8be4cb3046d757fb6caa40927d.tar.bz2 px4-nuttx-3b785276e679bc8be4cb3046d757fb6caa40927d.zip |
Add a ROMFS file system for testing the P-Code binary format
Diffstat (limited to 'nuttx/binfmt/pcode.c')
-rw-r--r-- | nuttx/binfmt/pcode.c | 147 |
1 files changed, 142 insertions, 5 deletions
diff --git a/nuttx/binfmt/pcode.c b/nuttx/binfmt/pcode.c index c5963b672..44ff16201 100644 --- a/nuttx/binfmt/pcode.c +++ b/nuttx/binfmt/pcode.c @@ -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,11 +116,71 @@ 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 * * Description: @@ -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 */ - |