From e4cdccae6c157d04b926f16685206ee11a1c8e33 Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 9 May 2007 00:06:00 +0000 Subject: [PATCH] Add simulated block device git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@208 42af7a65-404d-4744-a932-0658087f49c3 --- arch/sim/src/Makefile | 2 +- arch/sim/src/up_blockdevice.c | 202 ++++++++++++++++++++++++++++++++++ arch/sim/src/up_devconsole.c | 2 +- arch/sim/src/up_initialize.c | 5 +- arch/sim/src/up_internal.h | 1 + 5 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 arch/sim/src/up_blockdevice.c diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile index 5c7ed0d1f8..2694c7a8bf 100644 --- a/arch/sim/src/Makefile +++ b/arch/sim/src/Makefile @@ -45,7 +45,7 @@ CSRCS = up_initialize.c up_idle.c up_interruptcontext.c \ up_releasestack.c up_unblocktask.c up_blocktask.c \ up_releasepending.c up_reprioritizertr.c \ up_exit.c up_schedulesigaction.c up_allocateheap.c \ - up_devconsole.c + up_devconsole.c up_blockdevice.c COBJS = $(CSRCS:.c=$(OBJEXT)) SRCS = $(ASRCS) $(CSRCS) diff --git a/arch/sim/src/up_blockdevice.c b/arch/sim/src/up_blockdevice.c new file mode 100644 index 0000000000..1d4216e010 --- /dev/null +++ b/arch/sim/src/up_blockdevice.c @@ -0,0 +1,202 @@ +/**************************************************************************** + * up_blockdevice.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include "up_internal.h" + +/**************************************************************************** + * Private Definitions + ****************************************************************************/ + +#define NSECTORS 128 +#define SECTOR_SIZE 512 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int up_open(FAR struct file *filp); +static int up_close(FAR struct file *filp); +static ssize_t up_read(FAR struct file *filp, char *buffer, + size_t start_sector, size_t nsectors); +static ssize_t up_write(FAR struct file *filp, const char *buffer, + size_t start_sector, size_t nsectors); +static size_t up_geometry(FAR struct file *filp, struct geometry *geometry); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct block_operations g_bops = +{ + .open = up_open, + .close = up_close, + .read = up_read, + .write = up_write, + .geometry = up_geometry, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_open + * + * Description: Open the block device + * + ****************************************************************************/ + +static int up_open(FAR struct file *filp) +{ + filp->f_priv = (void*)malloc(NSECTORS*SECTOR_SIZE); + return 0; +} + +/**************************************************************************** + * Name: up_close + * + * Description: close the block device + * + ****************************************************************************/ + +static int up_close(FAR struct file *filp) +{ + if (filp->f_priv) + { + free(filp->f_priv); + filp->f_priv = NULL; + } + return 0; +} + +/**************************************************************************** + * Name: up_read + * + * Description: Read the specified numer of sectors + * + ****************************************************************************/ + +static ssize_t up_read(FAR struct file *filp, char *buffer, + size_t start_sector, size_t nsectors) +{ + char *src = filp->f_priv; + if (src && + start_sector < NSECTORS && + start_sector + nsectors < NSECTORS) + { + memcpy(buffer, &src[start_sector*SECTOR_SIZE], nsectors*SECTOR_SIZE); + return OK; + } + else + { + return -EINVAL; + } +} + +/**************************************************************************** + * Name: up_write + * + * Description: Write the specified number of sectors + * + ****************************************************************************/ + +static ssize_t up_write(FAR struct file *filp, const char *buffer, + size_t start_sector, size_t nsectors) +{ + char *dest = filp->f_priv; + if (dest && + start_sector < NSECTORS && + start_sector + nsectors < NSECTORS) + { + memcpy(&dest[start_sector*SECTOR_SIZE], buffer, nsectors*SECTOR_SIZE); + return OK; + } + else + { + return -EINVAL; + } +} + +/**************************************************************************** + * Name: up_geometry + * + * Description: Return device geometry + * + ****************************************************************************/ + +static size_t up_geometry(FAR struct file *filp, struct geometry *geometry) + { + if (geometry) + { + geometry->geo_available = (filp->f_priv != NULL); + geometry->geo_nsectors = NSECTORS; + geometry->geo_sectorsize = SECTOR_SIZE; + return OK; + } + return -EINVAL; + } + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_registerblockdevice + * + * Description: Register the simulated block device + + ****************************************************************************/ + +void up_registerblockdevice(void) +{ + (void)register_blockdriver("/dev/blkdev", &g_bops, 0, NULL); +} + + diff --git a/arch/sim/src/up_devconsole.c b/arch/sim/src/up_devconsole.c index b55df53dff..601a13bf3b 100644 --- a/arch/sim/src/up_devconsole.c +++ b/arch/sim/src/up_devconsole.c @@ -125,7 +125,7 @@ static ssize_t devconsole_write(struct file *filp, const char *buffer, size_t le void up_devconsole(void) { - (void)register_inode("/dev/console", &devconsole_fops, 0666, NULL); + (void)register_driver("/dev/console", &devconsole_fops, 0666, NULL); } int up_putc(int ch) diff --git a/arch/sim/src/up_initialize.c b/arch/sim/src/up_initialize.c index 90b0b49e47..805c88b456 100644 --- a/arch/sim/src/up_initialize.c +++ b/arch/sim/src/up_initialize.c @@ -83,6 +83,7 @@ void up_initialize(void) { /* Register devices */ - devnull_register(); /* Standard /dev/null */ - up_devconsole(); /* Our private /dev/console */ + devnull_register(); /* Standard /dev/null */ + up_devconsole(); /* Our private /dev/console */ + up_registerblockdevice(); /* Our simulated block device /dev/blkdev */ } diff --git a/arch/sim/src/up_internal.h b/arch/sim/src/up_internal.h index 29defe0296..5de1d7687b 100644 --- a/arch/sim/src/up_internal.h +++ b/arch/sim/src/up_internal.h @@ -86,6 +86,7 @@ extern void up_longjmp(int *jb, int val) __attribute__ ((noreturn)); /* up_devconsole **********************************************************/ extern void up_devconsole(void); +extern void up_registerblockdevice(void); #endif /* __ASSEMBLY__ */ #endif /* __ARCH_UP_INTERNAL_H */