UbixOS V2  2.0
ramdrive.cpp
Go to the documentation of this file.
1 /*****************************************************************************************
2  Copyright (c) 2002-2004 The UbixOS Project
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are
6  permitted provided that the following conditions are met:
7 
8  Redistributions of source code must retain the above copyright notice, this list of
9  conditions, the following disclaimer and the list of authors. Redistributions in binary
10  form must reproduce the above copyright notice, this list of conditions, the following
11  disclaimer and the list of authors in the documentation and/or other materials provided
12  with the distribution. Neither the name of the UbixOS Project nor the names of its
13  contributors may be used to endorse or promote products derived from this software
14  without specific prior written permission.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
23  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26  $Id: ramdrive.cpp 54 2016-01-11 01:29:55Z reddawg $
27 
28 *****************************************************************************************/
29 /*
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <assert.h>
34 #include "device.h"
35 
36 
37 static char *ram_data = 0x0;
38 
39 #define RAM_DRIVE_SIZE 1024*1024*100
40 
41 static
42 int
43 ramDrive_read(device_t *dev,void *ptr,off_t offset,size_t length) {
44  char *data = ram_data + (offset*512);
45  assert(ram_data);
46  assert(offset+length <= dev->sectors);
47  memcpy(ptr, data, length * 512);
48 
49  return(length);
50 }
51 
52 static
53 int
54 ramDrive_write(device_t *dev,void *ptr,off_t offset,size_t length) {
55  char *data = ram_data + (offset*512);
56  assert(ram_data);
57 
58  assert(offset+length <= dev->sectors);
59 
60  memcpy(data, ptr, length * 512);
61 
62  return(length);
63 }
64 
65 device_t *
66 dev_ramDrive(void) {
67  device_t *ramDrive = 0x0;
68 
69  FILE *ramDisk;
70 
71  ramDisk = fopen("./ram.dsk","rb");
72 
73  ramDrive = (device_t *)malloc(sizeof(device_t));
74 
75  ram_data = (char *)malloc(RAM_DRIVE_SIZE);
76 
77  if (ramDisk != 0x0) {
78  printf("Loaded Ram Disk\n");
79  fread(ram_data,RAM_DRIVE_SIZE,1,ramDisk);
80  fclose(ramDisk);
81  }
82 
83  ramDrive->major = 0x1;
84  ramDrive->sectors = RAM_DRIVE_SIZE / 512;
85 
86  ramDrive->read = ramDrive_read;
87  ramDrive->write = ramDrive_write;
88 
89  printf("leaving dev_ramDrive()\n");
90  return(ramDrive);
91 
92 } // dev_ramDrive
93 
94 int
95 dev_ramDestroy(void) {
96  printf("dev_ramDestroy()\n");
97  FILE *ramDisk;
98 
99  ramDisk = fopen("./ram.dsk","wb");
100 
101  fwrite(ram_data, RAM_DRIVE_SIZE ,1,ramDisk);
102 
103  fclose(ramDisk);
104 
105  printf("Saved Ram Disk\n");
106 
107  return(0x0);
108 } // dev_ramDestroy
109 */
110 /***
111  $Log: ramdrive.cpp,v $
112  Revision 1.1.1.1 2006/06/01 12:46:15 reddawg
113  ubix2
114 
115  Revision 1.2 2005/10/12 00:13:38 reddawg
116  Removed
117 
118  Revision 1.1.1.1 2005/09/26 17:24:45 reddawg
119  no message
120 
121  Revision 1.5 2004/09/20 00:53:04 flameshadow
122  chg: UbixFS::vfs_read() now works for direct block extents
123  chg: UbixFS::vfs_read() returns ~0 on error, or the size read in on success
124  chg: UbixFS::root member is now a fileDescriptor
125  chg: UbixFS::vfs_init() creates a file descriptor for the root dir
126  chg: UbixFS::vfs_format() now sets all values in the bTreeHeader before writing
127  chg: UbixFS::vfs_format() sets the inode magic number
128  chg: device_t::read(device_t *, void *, off_t, size_t)
129  chg: device_t::write(device_t *, void *, off_t, size_t)
130  chg: vfs_abstract::vfs_read(fileDescriptor *, void *, off_t, size_t)
131  chg: vfs_abstract::vfs_write(fileDescriptor *, void *, off_t, size_t)
132  chg: ramDrive_read(device_t *dev,void *ptr,off_t offset,size_t length)
133  chg: ramDrive_write(device_t *dev,void *ptr,off_t offset,size_t length)
134 
135  Revision 1.4 2004/09/13 20:10:11 flameshadow
136  chg: UbixFS::vfs_format() works
137  chg: UbixFS::vfs_init() works
138 
139  Revision 1.3 2004/09/13 15:48:29 flameshadow
140  chg: oops, the ramDrive is 100MB, not 512MB
141 
142  Revision 1.2 2004/09/13 15:21:26 flameshadow
143  add: ramdrive.h
144  chg: renamed device_t.size to sectors
145  chg: made #define for size of ramdisk
146  chg: calculated sectors of ramdisk and stored in the device_t struct
147 
148  Revision 1.1 2004/09/13 14:37:49 flameshadow
149  add: ramdrive.cpp
150  chg: added ramdrive to the makefile
151  chg: finished root dir inode initialization and saving in UbixFS::format()
152 
153  Revision 1.8 2004/08/30 13:29:08 reddawg
154  Fixed ramdisk to write/read 512 blocks
155 
156  Revision 1.7 2004/08/13 22:06:54 reddawg
157  UbixFS: fixed the test shell
158 
159  Revision 1.6 2004/08/13 21:47:49 reddawg
160  Fixed
161 
162  Revision 1.5 2004/08/13 17:25:06 reddawg
163  Testing better
164 
165  Revision 1.4 2004/08/13 17:07:32 reddawg
166  Works
167 
168  Revision 1.3 2004/08/13 16:58:57 reddawg
169  test
170 
171  Revision 1.2 2004/08/13 16:54:14 reddawg
172  fixed
173 
174  Revision 1.1 2004/08/13 16:51:55 reddawg
175  Start of ubixfs shell
176 
177  END
178  ***/
179