summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/core/blockdev.c
blob: c219d967376ec0b48811ec8d90fe15d7dff816f0 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <errno.h>
#include <ipxe/interface.h>
#include <ipxe/blockdev.h>

/** @file
 *
 * Block devices
 *
 */

/**
 * Read from block device
 *
 * @v control		Control interface
 * @v data		Data interface
 * @v lba		Starting logical block address
 * @v count		Number of logical blocks
 * @v buffer		Data buffer
 * @v len		Length of data buffer
 * @ret rc		Return status code
 */
int block_read ( struct interface *control, struct interface *data,
		 uint64_t lba, unsigned int count,
		 userptr_t buffer, size_t len ) {
	struct interface *dest;
	block_read_TYPE ( void * ) *op =
		intf_get_dest_op ( control, block_read, &dest );
	void *object = intf_object ( dest );
	int rc;

	if ( op ) {
		rc = op ( object, data, lba, count, buffer, len );
	} else {
		/* Default is to fail to issue the command */
		rc = -EOPNOTSUPP;
	}

	intf_put ( dest );
	return rc;
}

/**
 * Write to block device
 *
 * @v control		Control interface
 * @v data		Data interface
 * @v lba		Starting logical block address
 * @v count		Number of logical blocks
 * @v buffer		Data buffer
 * @v len		Length of data buffer
 * @ret rc		Return status code
 */
int block_write ( struct interface *control, struct interface *data,
		  uint64_t lba, unsigned int count,
		  userptr_t buffer, size_t len ) {
	struct interface *dest;
	block_write_TYPE ( void * ) *op =
		intf_get_dest_op ( control, block_write, &dest );
	void *object = intf_object ( dest );
	int rc;

	if ( op ) {
		rc = op ( object, data, lba, count, buffer, len );
	} else {
		/* Default is to fail to issue the command */
		rc = -EOPNOTSUPP;
	}

	intf_put ( dest );
	return rc;
}

/**
 * Read block device capacity
 *
 * @v control		Control interface
 * @v data		Data interface
 * @ret rc		Return status code
 */
int block_read_capacity ( struct interface *control, struct interface *data ) {
	struct interface *dest;
	block_read_capacity_TYPE ( void * ) *op =
		intf_get_dest_op ( control, block_read_capacity, &dest );
	void *object = intf_object ( dest );
	int rc;

	if ( op ) {
		rc = op ( object, data );
	} else {
		/* Default is to fail to issue the command */
		rc = -EOPNOTSUPP;
	}

	intf_put ( dest );
	return rc;
}

/**
 * Report block device capacity
 *
 * @v intf		Interface
 * @v capacity		Block device capacity
 */
void block_capacity ( struct interface *intf,
		      struct block_device_capacity *capacity ) {
	struct interface *dest;
	block_capacity_TYPE ( void * ) *op =
		intf_get_dest_op ( intf, block_capacity, &dest );
	void *object = intf_object ( dest );

	if ( op ) {
		op ( object, capacity );
	} else {
		/* Default is to do nothing */
	}

	intf_put ( dest );
}