examples/module: Add support for automounting removable media. This involves a wait for the block driver to become available before performing the mount.

This commit is contained in:
Gregory Nutt 2018-08-05 15:35:38 -06:00
parent b980e943c7
commit f5eeaf1fb6
3 changed files with 116 additions and 28 deletions

View File

@ -51,8 +51,8 @@ config EXAMPLES_ELF_FSMOUNT
default y
depends on EXAMPLES_ELF_EXTERN
---help---
Autmatically mount the external file system using the block
device provided by CONFIG_EXAMPLES_DEVPATH and the file
Automatically mount the external file system using the block
device provided by CONFIG_EXAMPLES_ELF_DEVPATH and the file
system type specified by CONFIG_EXAMPLES_ELF_FSTYPE.
config EXAMPLES_ELF_FSREMOVEABLE
@ -62,8 +62,8 @@ config EXAMPLES_ELF_FSREMOVEABLE
---help---
If the external file system is on removable media such as an
SD card or a USB FLASH driver, then this option may be selected
to build in logic to way for the media to be installed and
initialized before trying to open it.
to build in logic to wait for the media to be installed and
initialized before trying to mount it.
config EXAMPLES_ELF_DEVMINOR
int "ROMFS Minor Device Number"

View File

@ -31,7 +31,8 @@ config EXAMPLES_MODULE_BUILTINFS
the fsroot/ directory. You can then copy them to, say an SD card,
for testing on the target. That file system will need to be mounted
such that CONFIG_EXAMPLES_MODULE_BINDIR refers to the directory
containing the module binaries.
containing the module binaries (unless the file system is auto-
automounted)
NOTE: This option can only be used in the FLAT build mode because
it makes an illegal OS call to ramdisk_register().
@ -67,22 +68,51 @@ config EXAMPLES_MODULE_DEVMINOR
Used for registering the RAM block driver that will hold the ROMFS file system
containing the MODULE executables to be tested. Default: 0
config EXAMPLES_MODULE_DEVPATH
string "ROMFS Device Path"
default "/dev/ram0"
depends on EXAMPLES_MODULE_ROMFS
---help---
The path to the ROMFS block driver device. This must match EXAMPLES_MODULE_DEVMINOR.
Used for registering the RAM block driver that will hold the ROMFS file system
containing the MODULE executables to be tested. Default: "/dev/ram0"
config EXAMPLES_MODULE_BINDIR
string "Path to Test Modules"
default "/mnt/sdcard"
config EXAMPLES_MODULE_FSMOUNT
bool "Mount external file system"
default y
depends on !EXAMPLES_MODULE_BUILTINFS
---help---
The path to the directory on the mounted volume where the test
modules can found at runtime.
Automatically mount the external file system using the block
device provided by CONFIG_EXAMPLES_MODULE_DEVPATH and the file
system type specified by CONFIG_EXAMPLES_MODULE_FSTYPE.
config EXAMPLES_MODULE_FSREMOVEABLE
bool "Removable file system"
default n
depends on EXAMPLES_MODULE_FSMOUNT
---help---
If the external file system is on removable media such as an
SD card or a USB FLASH driver, then this option may be selected
to build in logic to wait for the media to be installed and
initialized before trying to mount it.
config EXAMPLES_MODULE_FSTYPE
string "External file system type"
default "vfat"
depends on EXAMPLES_MODULE_FSMOUNT
---help---
The type of the external file system as will be used in the mount()
command. Default: "vfat"
config EXAMPLES_MODULE_DEVPATH
string "Block driver device path"
default "/dev/ram0" if EXAMPLES_MODULE_ROMFS
default "/dev/mmcsd0" if EXAMPLES_MODULE_FSMOUNT
depends on EXAMPLES_MODULE_ROMFS || EXAMPLES_MODULE_FSMOUNT
---help---
The path to the ROMFS/External block driver device. This must match
EXAMPLES_MODULE_DEVMINOR for the case of ROMFS. Used for mounting the
file system containing the ELF executables to be tested. Default:
"/dev/ram0" for ROMFS, "/dev/mmcsd0" for the external file system.
config EXAMPLES_MODULE_BINDIR
string "Path to binaries"
default "/mnt/moddata"
depends on !EXAMPLES_MODULE_BUILTINFS && !EXAMPLES_MODULE_FSMOUNT
---help---
The full, absolte path to the location for the binaries can be
located in a pre-mounted external file system.
config EXAMPLES_MODULE_NOSTRIP
bool "Do not strip debug symbols"

View File

@ -40,9 +40,8 @@
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#ifdef CONFIG_EXAMPLES_MODULE_BUILTINFS
# include <sys/mount.h>
#endif
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/boardctl.h>
#include <stdio.h>
@ -80,7 +79,7 @@
# error "You must select CONFIG_MODULE in your configuration file"
#endif
#ifdef CONFIG_EXAMPLES_MODULE_BUILTINFS
#if defined(CONFIG_EXAMPLES_MODULE_BUILTINFS)
# if !defined(CONFIG_FS_ROMFS) || !defined(CONFIG_FS_CROMFS)
# error "You must select CONFIG_FS_ROMFS or CONFIG_FS_CROMFS in your configuration file"
# endif
@ -91,7 +90,7 @@
/* Describe the ROMFS file system */
# if defined(CONFIG_EXAMPLES_ELF_CROMFS)
# if defined(CONFIG_EXAMPLES_MODULE_CROMFS)
# define SECTORSIZE 64
# define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
# define MOUNTPT "/mnt/romfs"
@ -103,7 +102,7 @@
# ifndef CONFIG_EXAMPLES_MODULE_DEVPATH
# define CONFIG_EXAMPLES_MODULE_DEVPATH "/dev/ram0"
# endif
# elif defined(CONFIG_EXAMPLES_ELF_CROMFS)
# elif defined(CONFIG_EXAMPLES_MODULE_CROMFS)
/* Describe the CROMFS file system */
# define MOUNTPT "/mnt/cromfs"
@ -111,8 +110,13 @@
# define BINDIR MOUNTPT
#elif defined(CONFIG_EXAMPLES_MODULE_FSMOUNT)
# define MOUNTPT "/mnt/" CONFIG_EXAMPLES_MODULE_FSTYPE
# define BINDIR MOUNTPT
#else
# define BINDIR CONFIG_EXAMPLES_MODULE_BINDIR
#endif /* CONFIG_EXAMPLES_MODULE_BUILTINFS */
/****************************************************************************
@ -143,6 +147,9 @@ int module_main(int argc, char *argv[])
#endif
{
struct boardioc_symtab_s symdesc;
#ifdef CONFIG_EXAMPLES_MODULE_FSREMOVEABLE
struct stat buf;
#endif
FAR void *handle;
char buffer[128];
ssize_t nbytes;
@ -161,7 +168,7 @@ int module_main(int argc, char *argv[])
}
#ifdef CONFIG_EXAMPLES_MODULE_BUILTINFS
#if defined(CONFIG_EXAMPLES_ELF_ROMFS)
#if defined(CONFIG_EXAMPLES_MODULE_ROMFS)
/* Create a ROM disk for the ROMFS filesystem */
printf("main: Registering romdisk at /dev/ram%d\n",
@ -206,7 +213,7 @@ int module_main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
#elif defined(CONFIG_EXAMPLES_ELF_CROMFS)
#elif defined(CONFIG_EXAMPLES_MODULE_CROMFS)
/* Mount the CROMFS file system */
printf("Mounting CROMFS filesystem at target=%s\n", MOUNTPT);
@ -217,7 +224,58 @@ int module_main(int argc, char *argv[])
errmsg("ERROR: mount(%s, cromfs) failed: %d\n", MOUNTPT, errno);
}
#endif /* CONFIG_EXAMPLES_ELF_ROMFS */
#endif /* CONFIG_EXAMPLES_MODULE_ROMFS */
#elif defined(CONFIG_EXAMPLES_MODULE_EXTERN)
/* An external file system is being used */
#if defined(CONFIG_EXAMPLES_MODULE_FSMOUNT)
#if defined(CONFIG_EXAMPLES_MODULE_FSREMOVEABLE)
/* The file system is removable, wait until the block driver is available */
do
{
ret = stat(CONFIG_EXAMPLES_MODULE_DEVPATH, &buf);
if (ret < 0)
{
int errcode = errno;
if (errcode == ENOENT)
{
printf("%s does not exist. Waiting...\n",
CONFIG_EXAMPLES_MODULE_DEVPATH);
sleep(1);
}
else
{
printf("ERROR: stat(%s) failed: %d Aborting...\n",
CONFIG_EXAMPLES_MODULE_DEVPATH, errcode);
exit(EXIT_FAILURE);
}
}
else if (!S_ISBLK(buf.st_mode))
{
printf("ERROR: stat(%s) exists but is not a block driver: %04x\n",
CONFIG_EXAMPLES_MODULE_DEVPATH, buf.st_mode);
exit(EXIT_FAILURE);
}
}
while (ret < 0);
#endif /* CONFIG_EXAMPLES_MODULE_FSREMOVEABLE */
/* Mount the external file system */
message("Mounting %s filesystem at target=%s\n",
CONFIG_EXAMPLES_MODULE_FSTYPE, MOUNTPT);
ret = mount(CONFIG_EXAMPLES_MODULE_DEVPATH, MOUNTPT,
CONFIG_EXAMPLES_MODULE_FSTYPE, MS_RDONLY, NULL);
if (ret < 0)
{
errmsg("ERROR: mount(%s, %s, %s) failed: %d\n",\
CONFIG_EXAMPLES_MODULE_DEVPATH, CONFIG_EXAMPLES_MODULE_FSTYPE,
MOUNTPT, errno);
}
#endif
#endif /* CONFIG_EXAMPLES_MODULE_BUILTINFS */
/* Install the character driver */