ramdisk.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <gpxe/blockdev.h>
00022 #include <gpxe/ramdisk.h>
00023 
00024 /**
00025  * @file
00026  *
00027  * RAM disks
00028  *
00029  */
00030 
00031 static inline __attribute__ (( always_inline )) struct ramdisk *
00032 block_to_ramdisk ( struct block_device *blockdev ) {
00033         return container_of ( blockdev, struct ramdisk, blockdev );
00034 }
00035 
00036 /**
00037  * Read block
00038  *
00039  * @v blockdev          Block device
00040  * @v block             Block number
00041  * @v count             Block count
00042  * @v buffer            Data buffer
00043  * @ret rc              Return status code
00044  */
00045 static int ramdisk_read ( struct block_device *blockdev, uint64_t block,
00046                           unsigned long count, userptr_t buffer ) {
00047         struct ramdisk *ramdisk = block_to_ramdisk ( blockdev );
00048         unsigned long offset = ( block * blockdev->blksize );
00049         unsigned long length = ( count * blockdev->blksize );
00050 
00051         DBGC ( ramdisk, "RAMDISK %p reading [%lx,%lx)\n",
00052                ramdisk, offset, length );
00053 
00054         memcpy_user ( buffer, 0, ramdisk->data, offset, length );
00055         return 0;
00056 }
00057 
00058 /**
00059  * Write block
00060  *
00061  * @v blockdev          Block device
00062  * @v block             Block number
00063  * @v count             Block count
00064  * @v buffer            Data buffer
00065  * @ret rc              Return status code
00066  */
00067 static int ramdisk_write ( struct block_device *blockdev, uint64_t block,
00068                            unsigned long count, userptr_t buffer ) {
00069         struct ramdisk *ramdisk = block_to_ramdisk ( blockdev );
00070         unsigned long offset = ( block * blockdev->blksize );
00071         unsigned long length = ( count * blockdev->blksize );
00072 
00073         DBGC ( ramdisk, "RAMDISK %p writing [%lx,%lx)\n",
00074                ramdisk, offset, length );
00075 
00076         memcpy_user ( ramdisk->data, offset, buffer, 0, length );
00077         return 0;
00078 }
00079 
00080 static struct block_device_operations ramdisk_operations = {
00081         .read   = ramdisk_read,
00082         .write  = ramdisk_write
00083 };
00084 
00085 int init_ramdisk ( struct ramdisk *ramdisk, userptr_t data, size_t len,
00086                    unsigned int blksize ) {
00087         
00088         if ( ! blksize )
00089                 blksize = 512;
00090 
00091         ramdisk->data = data;
00092         ramdisk->blockdev.op = &ramdisk_operations;
00093         ramdisk->blockdev.blksize = blksize;
00094         ramdisk->blockdev.blocks = ( len / blksize );
00095 
00096         return 0;
00097 }

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1