-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathram_device.c
39 lines (33 loc) · 906 Bytes
/
ram_device.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <linux/types.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/errno.h>
#include "ram_device.h"
#include "partition.h"
#define RB_DEVICE_SIZE 1024 /* sectors */
/* So, total device size = 1024 * 512 bytes = 512 KiB */
/* Array where the disk stores its data */
static u8 *dev_data;
int ramdevice_init(void)
{
dev_data = vmalloc(RB_DEVICE_SIZE * RB_SECTOR_SIZE);
if (dev_data == NULL)
return -ENOMEM;
/* Setup its partition table */
copy_mbr_n_br(dev_data);
return RB_DEVICE_SIZE;
}
void ramdevice_cleanup(void)
{
vfree(dev_data);
}
void ramdevice_write(sector_t sector_off, u8 *buffer, unsigned int sectors)
{
memcpy(dev_data + sector_off * RB_SECTOR_SIZE, buffer,
sectors * RB_SECTOR_SIZE);
}
void ramdevice_read(sector_t sector_off, u8 *buffer, unsigned int sectors)
{
memcpy(buffer, dev_data + sector_off * RB_SECTOR_SIZE,
sectors * RB_SECTOR_SIZE);
}