summaryrefslogtreecommitdiff
path: root/gpxe/src/drivers/block/ramdisk.c
blob: 4e6f1bca02292c534a0e9525d6361b03790c8f3b (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <gpxe/blockdev.h>
#include <gpxe/ramdisk.h>

/**
 * @file
 *
 * RAM disks
 *
 */

static inline __attribute__ (( always_inline )) struct ramdisk *
block_to_ramdisk ( struct block_device *blockdev ) {
	return container_of ( blockdev, struct ramdisk, blockdev );
}

/**
 * Read block
 *
 * @v blockdev		Block device
 * @v block		Block number
 * @v count		Block count
 * @v buffer		Data buffer
 * @ret rc		Return status code
 */
static int ramdisk_read ( struct block_device *blockdev, uint64_t block,
			  unsigned long count, userptr_t buffer ) {
	struct ramdisk *ramdisk = block_to_ramdisk ( blockdev );
	unsigned long offset = ( block * blockdev->blksize );
	unsigned long length = ( count * blockdev->blksize );

	DBGC ( ramdisk, "RAMDISK %p reading [%lx,%lx)\n",
	       ramdisk, offset, length );

	memcpy_user ( buffer, 0, ramdisk->data, offset, length );
	return 0;
}

/**
 * Write block
 *
 * @v blockdev		Block device
 * @v block		Block number
 * @v count		Block count
 * @v buffer		Data buffer
 * @ret rc		Return status code
 */
static int ramdisk_write ( struct block_device *blockdev, uint64_t block,
			   unsigned long count, userptr_t buffer ) {
	struct ramdisk *ramdisk = block_to_ramdisk ( blockdev );
	unsigned long offset = ( block * blockdev->blksize );
	unsigned long length = ( count * blockdev->blksize );

	DBGC ( ramdisk, "RAMDISK %p writing [%lx,%lx)\n",
	       ramdisk, offset, length );

	memcpy_user ( ramdisk->data, offset, buffer, 0, length );
	return 0;
}

static struct block_device_operations ramdisk_operations = {
	.read	= ramdisk_read,
	.write	= ramdisk_write
};

int init_ramdisk ( struct ramdisk *ramdisk, userptr_t data, size_t len,
		   unsigned int blksize ) {
	
	if ( ! blksize )
		blksize = 512;

	ramdisk->data = data;
	ramdisk->blockdev.op = &ramdisk_operations;
	ramdisk->blockdev.blksize = blksize;
	ramdisk->blockdev.blocks = ( len / blksize );

	return 0;
}