[BACK]Return to ramdisk.c CVS log [TXT][DIR] Up to [local] / prex-old / dev / gen

Annotation of prex-old/dev/gen/ramdisk.c, Revision 1.1.1.1

1.1       nbrk        1: /*-
                      2:  * Copyright (c) 2006, Kohsuke Ohtani
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. Neither the name of the author nor the names of any co-contributors
                     14:  *    may be used to endorse or promote products derived from this software
                     15:  *    without specific prior written permission.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
                     30: /*
                     31:  * ramdisk.c - RAM disk driver
                     32:  */
                     33:
                     34: #include <driver.h>
                     35:
                     36: /* #define DEBUG_RAMDISK 1 */
                     37:
                     38: #ifdef DEBUG_RAMDISK
                     39: #define rd_printf(fmt, args...)        printk("%s: " fmt, __FUNCTION__ , ## args)
                     40: #else
                     41: #define rd_printf(fmt...)      do {} while (0)
                     42: #endif
                     43:
                     44: /* Block size */
                     45: #define BSIZE          512
                     46:
                     47: static int ramdisk_read(device_t dev, char *buf, size_t *nbyte, int blkno);
                     48: static int ramdisk_write(device_t dev, char *buf, size_t *nbyte, int blkno);
                     49: static int ramdisk_init(void);
                     50:
                     51: /*
                     52:  * Driver structure
                     53:  */
                     54: struct driver ramdisk_drv = {
                     55:        /* name */      "RAM disk",
                     56:        /* order */     6,
                     57:        /* init */      ramdisk_init,
                     58: };
                     59:
                     60: static struct devio ramdisk_io = {
                     61:        /* open */      NULL,
                     62:        /* close */     NULL,
                     63:        /* read */      ramdisk_read,
                     64:        /* write */     ramdisk_write,
                     65:        /* ioctl */     NULL,
                     66:        /* event */     NULL,
                     67: };
                     68:
                     69: static device_t ramdisk_dev;   /* Device object */
                     70:
                     71: static char *img_start;
                     72: static size_t img_size;
                     73:
                     74: static int
                     75: ramdisk_read(device_t dev, char *buf, size_t *nbyte, int blkno)
                     76: {
                     77:        void *kbuf;
                     78:        size_t nr_read;
                     79:
                     80:        rd_printf("read buf=%x nbyte=%d blkno=%x\n", buf, *nbyte, blkno);
                     81:
                     82:        /* Check overrun */
                     83:        if ((size_t)blkno * BSIZE > img_size) {
                     84:                rd_printf("Overrun!\n");
                     85:                return EIO;
                     86:        }
                     87:        nr_read = *nbyte;
                     88:        if ((size_t)blkno * BSIZE + nr_read > img_size)
                     89:                nr_read = img_size - blkno * BSIZE;
                     90:
                     91:        /* Translate buffer address to kernel address */
                     92:        kbuf = kmem_map(buf, nr_read);
                     93:        if (kbuf == NULL)
                     94:                return EFAULT;
                     95:
                     96:        /* Copy data */
                     97:        memcpy(kbuf, img_start + blkno * BSIZE, nr_read);
                     98:        *nbyte = nr_read;
                     99:        return 0;
                    100: }
                    101:
                    102: /*
                    103:  * Data written to this device is discarded.
                    104:  */
                    105: static int
                    106: ramdisk_write(device_t dev, char *buf, size_t *nbyte, int blkno)
                    107: {
                    108:        void *kbuf;
                    109:        size_t nr_write;
                    110:
                    111:        rd_printf("write buf=%x nbyte=%d blkno=%x\n", buf, *nbyte, blkno);
                    112:
                    113:        /* Check overrun */
                    114:        if ((size_t)blkno * BSIZE > img_size)
                    115:                return EIO;
                    116:        nr_write = *nbyte;
                    117:        if ((size_t)blkno * BSIZE + nr_write > img_size)
                    118:                nr_write = img_size - blkno * BSIZE;
                    119:
                    120:        /* Translate buffer address to kernel address */
                    121:        kbuf = kmem_map(buf, nr_write);
                    122:        if (kbuf == NULL)
                    123:                return EFAULT;
                    124:
                    125:        /* Copy data */
                    126:        memcpy(img_start + blkno * BSIZE, kbuf, nr_write);
                    127:        *nbyte = nr_write;
                    128:        return 0;
                    129: }
                    130:
                    131: /*
                    132:  * Initialize
                    133:  */
                    134: static int
                    135: ramdisk_init(void)
                    136: {
                    137:        struct boot_info *boot_info;
                    138:        struct mem_map *rd;
                    139:
                    140:        machine_bootinfo(&boot_info);
                    141:        rd = (struct mem_map *)&boot_info->ram_disk;
                    142:        img_start = (char *)phys_to_virt((void *)rd->start);
                    143:        img_size = rd->size;
                    144:        if (img_size == 0)
                    145:                return -1;
                    146:        printk("RAM disk at 0x%08x (%dK bytes)\n", img_start, img_size/1024);
                    147:
                    148:        /* Create device object */
                    149:        ramdisk_dev = device_create(&ramdisk_io, "ram0", DF_BLK);
                    150:        ASSERT(ramdisk_dev);
                    151:        return 0;
                    152: }

CVSweb