From be325924fb606204bdd9d62c871448b7bb490c28 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 26 Oct 2019 09:35:32 -0600 Subject: [PATCH] This commit corrects a problem with NSH: NSH was calling the OS internal function ramdisk_register() in violation of the portable POSIX interface. This commit solves the problem by introducing a new boardctl() function BOARDIOC_MKRD which moves the RAM disk creation into the OS. Squashed commit of the following: drivers/: Run tools/nxstyle against all drivers/*.c. boards/boardctl.c: Add new boardctl() command, BOARDIOC_MKRD, that can be used to create a RAM disk. This will replace the illegal call to ramdisk_register() currently used by NSH. drivers/mkrd.c: Add wrapper around ramdisk_register() for creating a proper ramdisk. --- boards/boardctl.c | 30 +++++++++++++++++++++++++ drivers/Makefile | 2 +- drivers/dev_null.c | 6 +++-- drivers/dev_zero.c | 1 + drivers/rwbuffer.c | 6 ++--- include/nuttx/drivers/ramdisk.h | 30 +++++++++++++++++++++++-- include/sys/boardctl.h | 39 ++++++++++++++++++++++++--------- 7 files changed, 96 insertions(+), 18 deletions(-) diff --git a/boards/boardctl.c b/boards/boardctl.c index 31ba396f73..0b90cd1bef 100644 --- a/boards/boardctl.c +++ b/boards/boardctl.c @@ -49,6 +49,10 @@ #include #include +#ifdef CONFIG_FS_WRITABLE +# include +#endif + #ifdef CONFIG_NX # include #endif @@ -354,6 +358,32 @@ int boardctl(unsigned int cmd, uintptr_t arg) break; #endif +#ifdef CONFIG_FS_WRITABLE + /* CMD: BOARDIOC_MKRD + * DESCRIPTION: Create a RAM disk + * ARG: Pointer to read-only instance of struct boardioc_mkrd_s. + * CONFIGURATION: CONFIG_FS_WRITABLE + * DEPENDENCIES: None + */ + + case BOARDIOC_MKRD: + { + FAR const struct boardioc_mkrd_s *desc = + (FAR const struct boardioc_mkrd_s *)arg; + + if (desc == NULL) + { + ret = -EINVAL; + } + else + { + ret = mkrd((int)desc->minor, desc->nsectors, desc->sectsize, + desc->rdflags); + } + } + break; +#endif + #ifdef CONFIG_BOARDCTL_APP_SYMTAB /* CMD: BOARDIOC_APP_SYMTAB * DESCRIPTION: Select the application symbol table. This symbol table diff --git a/drivers/Makefile b/drivers/Makefile index 0edbb7ac63..90b3aec00f 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -90,7 +90,7 @@ endif CSRCS += dev_null.c dev_zero.c ifneq ($(CONFIG_DISABLE_MOUNTPOINT),y) - CSRCS += ramdisk.c + CSRCS += ramdisk.c mkrd.c ifeq ($(CONFIG_DRVR_WRITEBUFFER),y) CSRCS += rwbuffer.c else diff --git a/drivers/dev_null.c b/drivers/dev_null.c index d5aadc7262..11e872f3b9 100644 --- a/drivers/dev_null.c +++ b/drivers/dev_null.c @@ -86,7 +86,8 @@ static const struct file_operations devnull_fops = * Name: devnull_read ****************************************************************************/ -static ssize_t devnull_read(FAR struct file *filep, FAR char *buffer, size_t len) +static ssize_t devnull_read(FAR struct file *filep, FAR char *buffer, + size_t len) { return 0; /* Return EOF */ } @@ -95,7 +96,8 @@ static ssize_t devnull_read(FAR struct file *filep, FAR char *buffer, size_t len * Name: devnull_write ****************************************************************************/ -static ssize_t devnull_write(FAR struct file *filep, FAR const char *buffer, size_t len) +static ssize_t devnull_write(FAR struct file *filep, FAR const char *buffer, + size_t len) { return len; /* Say that everything was written */ } diff --git a/drivers/dev_zero.c b/drivers/dev_zero.c index 4fe39fb9dd..d376371953 100644 --- a/drivers/dev_zero.c +++ b/drivers/dev_zero.c @@ -118,6 +118,7 @@ static int devzero_poll(FAR struct file *filep, FAR struct pollfd *fds, nxsem_post(fds->sem); } } + return OK; } diff --git a/drivers/rwbuffer.c b/drivers/rwbuffer.c index e87fe5f51c..5c02a3048e 100644 --- a/drivers/rwbuffer.c +++ b/drivers/rwbuffer.c @@ -185,7 +185,6 @@ static void rwb_wrflush(struct rwbuffer_s *rwb) rwb_resetwrbuffer(rwb); } - } #endif @@ -933,12 +932,13 @@ ssize_t rwb_read(FAR struct rwbuffer_s *rwb, off_t startblock, ret = rwb_read_(rwb, startblock, nblocks, rdbuffer); if (ret < 0) - return ret; + { + return ret; + } return readblocks + ret; } - /**************************************************************************** * Name: rwb_write ****************************************************************************/ diff --git a/include/nuttx/drivers/ramdisk.h b/include/nuttx/drivers/ramdisk.h index 86bb5f4eff..090a303a14 100644 --- a/include/nuttx/drivers/ramdisk.h +++ b/include/nuttx/drivers/ramdisk.h @@ -48,6 +48,7 @@ /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ + /* Values for rdflags */ #define RDFLAG_WRENABLED (1 << 0) /* Bit 0: 1=Can write to RAM disk */ @@ -93,11 +94,36 @@ extern "C" #ifdef CONFIG_FS_WRITABLE int ramdisk_register(int minor, FAR uint8_t *buffer, uint32_t nsectors, - uint16_t sectize, uint8_t rdflags); + uint16_t sectsize, uint8_t rdflags); #define romdisk_register(m,b,n,s) ramdisk_register(m,(FAR uint8_t *)b,n,s,0) #else int romdisk_register(int minor, FAR const uint8_t *buffer, uint32_t nsectors, - uint16_t sectize); + uint16_t sectsize); +#endif + +/**************************************************************************** + * Name: mkrd + * + * Description: + * This is a wrapper function around ramdisk_register. It combines the + * necessary operations to create a RAM disk into a single callable + * function. Memory for the RAM disk is allocated, appropriated, from + * the kernel heap (in build modes where there is a distinct kernel heap). + * + * Input Parameters: + * minor: Selects suffix of device named /dev/ramN, N={1,2,3...} + * nsectors: Number of sectors on device + * sectize: The size of one sector + * rdflags: See RDFLAG_* definitions. Typically + * RDFLAG_WRENABLED | RDFLAG_FUNLINK + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_FS_WRITABLE +int mkrd(int minor, uint32_t nsectors, uint16_t sectsize, uint8_t rdflags); #endif #undef EXTERN diff --git a/include/sys/boardctl.h b/include/sys/boardctl.h index 9c61bf16e2..ed24a682aa 100644 --- a/include/sys/boardctl.h +++ b/include/sys/boardctl.h @@ -96,6 +96,12 @@ * which to receive the board unique ID. * DEPENDENCIES: Board logic must provide the board_uniqueid() interface. * + * CMD: BOARDIOC_MKRD + * DESCRIPTION: Create a RAM disk + * ARG: Pointer to read-only instance of struct boardioc_mkrd_s. + * CONFIGURATION: CONFIG_FS_WRITABLE + * DEPENDENCIES: None + * * CMD: BOARDIOC_APP_SYMTAB * DESCRIPTION: Select the application symbol table. This symbol table * provides the symbol definitions exported to application @@ -181,15 +187,16 @@ #define BOARDIOC_POWEROFF _BOARDIOC(0x0003) #define BOARDIOC_RESET _BOARDIOC(0x0004) #define BOARDIOC_UNIQUEID _BOARDIOC(0x0005) -#define BOARDIOC_APP_SYMTAB _BOARDIOC(0x0006) -#define BOARDIOC_OS_SYMTAB _BOARDIOC(0x0007) -#define BOARDIOC_BUILTINS _BOARDIOC(0x0008) -#define BOARDIOC_USBDEV_CONTROL _BOARDIOC(0x0009) -#define BOARDIOC_NX_START _BOARDIOC(0x000a) -#define BOARDIOC_VNC_START _BOARDIOC(0x000b) -#define BOARDIOC_NXTERM _BOARDIOC(0x000c) -#define BOARDIOC_NXTERM_IOCTL _BOARDIOC(0x000d) -#define BOARDIOC_TESTSET _BOARDIOC(0x000e) +#define BOARDIOC_MKRD _BOARDIOC(0x0006) +#define BOARDIOC_APP_SYMTAB _BOARDIOC(0x0007) +#define BOARDIOC_OS_SYMTAB _BOARDIOC(0x0008) +#define BOARDIOC_BUILTINS _BOARDIOC(0x0009) +#define BOARDIOC_USBDEV_CONTROL _BOARDIOC(0x000a) +#define BOARDIOC_NX_START _BOARDIOC(0x000b) +#define BOARDIOC_VNC_START _BOARDIOC(0x000c) +#define BOARDIOC_NXTERM _BOARDIOC(0x000d) +#define BOARDIOC_NXTERM_IOCTL _BOARDIOC(0x000e) +#define BOARDIOC_TESTSET _BOARDIOC(0x000f) /* If CONFIG_BOARDCTL_IOCTL=y, then board-specific commands will be support. * In this case, all commands not recognized by boardctl() will be forwarded @@ -198,7 +205,7 @@ * User defined board commands may begin with this value: */ -#define BOARDIOC_USER _BOARDIOC(0x000f) +#define BOARDIOC_USER _BOARDIOC(0x0010) /**************************************************************************** * Public Type Definitions @@ -206,6 +213,18 @@ /* Structures used with IOCTL commands */ +#ifdef CONFIG_FS_WRITABLE +/* Describes the RAM disk to be created */ + +struct boardioc_mkrd_s +{ + uint8_t minor; /* Minor device number of the RAM disk. */ + uint32_t nsectors; /* The number of sectors in the RAM disk */ + uint16_t sectsize; /* The size of one sector in bytes */ + uint8_t rdflags; /* See RD_FLAGS_* definitions in include/nuttx/ramdisk.h */ +}; +#endif + /* In order to full describe a symbol table, a vector containing the address * of the symbol table and the number of elements in the symbol table is * required.