1 /*
2  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <errno.h>
27 #include <ipxe/interface.h>
28 #include <ipxe/blockdev.h>
29 
30 /** @file
31  *
32  * Block devices
33  *
34  */
35 
36 /**
37  * Read from block device
38  *
39  * @v control		Control interface
40  * @v data		Data interface
41  * @v lba		Starting logical block address
42  * @v count		Number of logical blocks
43  * @v buffer		Data buffer
44  * @v len		Length of data buffer
45  * @ret rc		Return status code
46  */
block_read(struct interface * control,struct interface * data,uint64_t lba,unsigned int count,userptr_t buffer,size_t len)47 int block_read ( struct interface *control, struct interface *data,
48 		 uint64_t lba, unsigned int count,
49 		 userptr_t buffer, size_t len ) {
50 	struct interface *dest;
51 	block_read_TYPE ( void * ) *op =
52 		intf_get_dest_op ( control, block_read, &dest );
53 	void *object = intf_object ( dest );
54 	int rc;
55 
56 	if ( op ) {
57 		rc = op ( object, data, lba, count, buffer, len );
58 	} else {
59 		/* Default is to fail to issue the command */
60 		rc = -EOPNOTSUPP;
61 	}
62 
63 	intf_put ( dest );
64 	return rc;
65 }
66 
67 /**
68  * Write to block device
69  *
70  * @v control		Control interface
71  * @v data		Data interface
72  * @v lba		Starting logical block address
73  * @v count		Number of logical blocks
74  * @v buffer		Data buffer
75  * @v len		Length of data buffer
76  * @ret rc		Return status code
77  */
block_write(struct interface * control,struct interface * data,uint64_t lba,unsigned int count,userptr_t buffer,size_t len)78 int block_write ( struct interface *control, struct interface *data,
79 		  uint64_t lba, unsigned int count,
80 		  userptr_t buffer, size_t len ) {
81 	struct interface *dest;
82 	block_write_TYPE ( void * ) *op =
83 		intf_get_dest_op ( control, block_write, &dest );
84 	void *object = intf_object ( dest );
85 	int rc;
86 
87 	if ( op ) {
88 		rc = op ( object, data, lba, count, buffer, len );
89 	} else {
90 		/* Default is to fail to issue the command */
91 		rc = -EOPNOTSUPP;
92 	}
93 
94 	intf_put ( dest );
95 	return rc;
96 }
97 
98 /**
99  * Read block device capacity
100  *
101  * @v control		Control interface
102  * @v data		Data interface
103  * @ret rc		Return status code
104  */
block_read_capacity(struct interface * control,struct interface * data)105 int block_read_capacity ( struct interface *control, struct interface *data ) {
106 	struct interface *dest;
107 	block_read_capacity_TYPE ( void * ) *op =
108 		intf_get_dest_op ( control, block_read_capacity, &dest );
109 	void *object = intf_object ( dest );
110 	int rc;
111 
112 	if ( op ) {
113 		rc = op ( object, data );
114 	} else {
115 		/* Default is to fail to issue the command */
116 		rc = -EOPNOTSUPP;
117 	}
118 
119 	intf_put ( dest );
120 	return rc;
121 }
122 
123 /**
124  * Report block device capacity
125  *
126  * @v intf		Interface
127  * @v capacity		Block device capacity
128  */
block_capacity(struct interface * intf,struct block_device_capacity * capacity)129 void block_capacity ( struct interface *intf,
130 		      struct block_device_capacity *capacity ) {
131 	struct interface *dest;
132 	block_capacity_TYPE ( void * ) *op =
133 		intf_get_dest_op ( intf, block_capacity, &dest );
134 	void *object = intf_object ( dest );
135 
136 	if ( op ) {
137 		op ( object, capacity );
138 	} else {
139 		/* Default is to do nothing */
140 	}
141 
142 	intf_put ( dest );
143 }
144