root / Whitix / branches / hybrid / devices / storage / ramdisk.c

Revision 521, 2.5 kB (checked in by mwhitworth, 6 months ago)

Update build process.

Line 
1#include <sdevice.h>
2#include <llist.h>
3#include <error.h>
4#include <request.h>
5#include <console.h>
6#include <malloc.h>
7#include <fs/vfs.h>
8#include <fs/devfs.h>
9#include <init.h>
10#include <i386/virtual.h>
11
12struct RamDisk
13{
14        char* data;
15        struct StorageDevice* sDev;
16};
17
18/* Useful defines */
19
20#define MAX_RAMDISKS 2
21#define RAMDISK_SIZE (4*1024*1024) /* In bytes */
22#define RAMDISK_SSIZE 1024 /* Sector size, in bytes */
23
24static struct RamDisk disks[MAX_RAMDISKS];
25struct StorageDevice rdDevices[MAX_RAMDISKS];
26
27/***********************************************************************
28 *
29 * FUNCTION:    RamDiskRequest
30 *
31 * DESCRIPTION: Service a read or write request to a ramdisk.
32 *
33 * PARAMETERS: data - pointer to a RamDisk structure.
34 *
35 * RETURNS: 0 if successful, -EIO if request could not be serviced.
36 *
37 ***********************************************************************/
38
39int RamDiskRequest(void* data)
40{
41        struct RamDisk* disk=(struct RamDisk*)data;
42        struct StorageDevice* sDev=disk->sDev;
43        struct Request* request=StorageGetCurrRequest(sDev);
44        int reqSize=request->numSectors*RAMDISK_SSIZE;
45        int memOffset=request->sector*RAMDISK_SSIZE;
46        if (!request)
47                return 0;
48
49        if (!disk->data)
50        {
51                /* Allocate from the malloc range - maybe should choose somewhere else? */
52                disk->data=(char*)VirtMapPhysRange(0xC0000000,0xD0000000,RAMDISK_SIZE >> PAGE_SHIFT,3);
53
54                if (!disk->data)
55                {
56                        StorageEndRequest(sDev,ENOMEM);
57                        return -ENOMEM;
58                }
59        }
60
61        /* Past the end of the ramdisk? */
62        if (memOffset >= (RAMDISK_SIZE-reqSize))
63        {
64                StorageEndRequest(sDev,EIO);
65                return -EIO;
66        }
67
68        if (request->type == REQUEST_WRITE)
69                memcpy((char*)(disk->data+memOffset),(char*)request->data,reqSize);
70        else if (request->type == REQUEST_READ)
71                memcpy((char*)request->data,(char*)(disk->data+memOffset),reqSize);
72
73        StorageEndRequest(sDev,0);
74        return 0;
75}
76
77int RamDiskInit()
78{
79        int i;
80
81        for (i=0; i<MAX_RAMDISKS; i++)
82        {
83                char buf[32];
84
85                sprintf(buf,"Storage/RamDisk%d",i);
86                disks[i].sDev=&rdDevices[i];
87                disks[i].data=NULL;
88
89                /* Set up the storage device structure */
90                rdDevices[i].request=RamDiskRequest;
91                rdDevices[i].blockSize=RAMDISK_SSIZE;
92                rdDevices[i].priv=&disks[i];
93                rdDevices[i].totalSectors=RAMDISK_SIZE/RAMDISK_SSIZE;
94
95                DevAddDevice(buf,7,i,DEVICE_BLOCK,&rdDevices[i]);
96                StorageAddDevice(&rdDevices[i]);
97                StorageQueueInit(&rdDevices[i]);
98        }
99
100        KePrint("%d ramdisks loaded, of size %d\n",MAX_RAMDISKS,RAMDISK_SIZE);
101
102        return 0;
103}
104
105DeviceInit(RamDiskInit);
Note: See TracBrowser for help on using the browser.