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

Annotation of prex/dev/gen/ramdisk.c, Revision 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 DPRINTF(a) printf a
        !            40: #else
        !            41: #define DPRINTF(a)
        !            42: #endif
        !            43:
        !            44: /* Block size */
        !            45: #define BSIZE          512
        !            46:
        !            47: static int ramdisk_read(device_t, char *, size_t *, int);
        !            48: static int ramdisk_write(device_t, char *, size_t *, int);
        !            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: /*
        !            61:  * Device I/O table
        !            62:  */
        !            63: static struct devio ramdisk_io = {
        !            64:        /* open */      NULL,
        !            65:        /* close */     NULL,
        !            66:        /* read */      ramdisk_read,
        !            67:        /* write */     ramdisk_write,
        !            68:        /* ioctl */     NULL,
        !            69:        /* event */     NULL,
        !            70: };
        !            71:
        !            72: static device_t ramdisk_dev;   /* Device object */
        !            73:
        !            74: static char *img_start;
        !            75: static size_t img_size;
        !            76:
        !            77: static int
        !            78: ramdisk_read(device_t dev, char *buf, size_t *nbyte, int blkno)
        !            79: {
        !            80:        void *kbuf;
        !            81:        size_t nr_read;
        !            82:
        !            83:        DPRINTF(("ramdisk_read: buf=%x nbyte=%d blkno=%x\n",
        !            84:                 buf, *nbyte, blkno));
        !            85:
        !            86:        /* Check overrun */
        !            87:        if ((size_t)blkno * BSIZE > img_size) {
        !            88:                DPRINTF(("ramdisk_read: overrun!\n"));
        !            89:                return EIO;
        !            90:        }
        !            91:        nr_read = *nbyte;
        !            92:        if ((size_t)blkno * BSIZE + nr_read > img_size)
        !            93:                nr_read = img_size - blkno * BSIZE;
        !            94:
        !            95:        /* Translate buffer address to kernel address */
        !            96:        kbuf = kmem_map(buf, nr_read);
        !            97:        if (kbuf == NULL)
        !            98:                return EFAULT;
        !            99:
        !           100:        /* Copy data */
        !           101:        memcpy(kbuf, img_start + blkno * BSIZE, nr_read);
        !           102:        *nbyte = nr_read;
        !           103:        return 0;
        !           104: }
        !           105:
        !           106: static int
        !           107: ramdisk_write(device_t dev, char *buf, size_t *nbyte, int blkno)
        !           108: {
        !           109:        void *kbuf;
        !           110:        size_t nr_write;
        !           111:
        !           112:        DPRINTF(("ramdisk_write: buf=%x nbyte=%d blkno=%x\n",
        !           113:                 buf, *nbyte, blkno));
        !           114:
        !           115:        /* Check overrun */
        !           116:        if ((size_t)blkno * BSIZE > img_size)
        !           117:                return EIO;
        !           118:        nr_write = *nbyte;
        !           119:        if ((size_t)blkno * BSIZE + nr_write > img_size)
        !           120:                nr_write = img_size - blkno * BSIZE;
        !           121:
        !           122:        /* Translate buffer address to kernel address */
        !           123:        kbuf = kmem_map(buf, nr_write);
        !           124:        if (kbuf == NULL)
        !           125:                return EFAULT;
        !           126:
        !           127:        /* Copy data */
        !           128:        memcpy(img_start + blkno * BSIZE, kbuf, nr_write);
        !           129:        *nbyte = nr_write;
        !           130:        return 0;
        !           131: }
        !           132:
        !           133: /*
        !           134:  * Initialize
        !           135:  */
        !           136: static int
        !           137: ramdisk_init(void)
        !           138: {
        !           139:        struct boot_info *boot_info;
        !           140:        struct mem_map *rd;
        !           141:
        !           142:        machine_bootinfo(&boot_info);
        !           143:        rd = (struct mem_map *)&boot_info->ram_disk;
        !           144:        img_start = (char *)phys_to_virt((void *)rd->start);
        !           145:        img_size = rd->size;
        !           146:        if (img_size == 0)
        !           147:                return -1;
        !           148: #ifdef DEBUG
        !           149:        printf("RAM disk at 0x%08x (%dK bytes)\n", img_start, img_size/1024);
        !           150: #endif
        !           151:        /* Create device object */
        !           152:        ramdisk_dev = device_create(&ramdisk_io, "ram0", DF_BLK);
        !           153:        ASSERT(ramdisk_dev);
        !           154:        return 0;
        !           155: }

CVSweb