22
22
23
23
/* * Enum of standard error codes
24
24
*
25
- * @enum bd_error_t
25
+ * @enum bd_error
26
26
*/
27
27
enum bd_error {
28
28
BD_ERROR_OK = 0 , /* !< no error */
29
- BD_ERROR_WOULD_BLOCK = -4001 , /* !< operation would block */
30
- BD_ERROR_UNSUPPORTED = -4002 , /* !< unsupported operation */
31
- BD_ERROR_PARAMETER = -4003 , /* !< invalid parameter */
32
- BD_ERROR_NO_INIT = -4004 , /* !< uninitialized */
33
- BD_ERROR_NO_DEVICE = -4005 , /* !< device is missing or not connected */
34
- BD_ERROR_WRITE_PROTECTED = -4006 , /* !< write protected */
35
- BD_ERROR_DEVICE_ERROR = -4007 , /* !< device specific error */
29
+ BD_ERROR_DEVICE_ERROR = -4001 , /* !< device specific error */
36
30
};
37
31
38
- /* * Type representing either 0 or a negative error code
39
- */
40
- typedef int32_t bd_error_t ;
41
-
42
32
/* * Type representing the address of a specific block
43
33
*/
44
34
typedef uint64_t bd_addr_t ;
@@ -61,13 +51,13 @@ class BlockDevice
61
51
*
62
52
* @return 0 on success or a negative error code on failure
63
53
*/
64
- virtual bd_error_t init () = 0;
54
+ virtual int init () = 0;
65
55
66
56
/* * Deinitialize a block device
67
57
*
68
58
* @return 0 on success or a negative error code on failure
69
59
*/
70
- virtual bd_error_t deinit () = 0;
60
+ virtual int deinit () = 0;
71
61
72
62
/* * Read blocks from a block device
73
63
*
@@ -78,20 +68,7 @@ class BlockDevice
78
68
* @param size Size to read in bytes, must be a multiple of read block size
79
69
* @return 0 on success, negative error code on failure
80
70
*/
81
- virtual bd_error_t read (void *buffer, bd_addr_t addr, bd_size_t size) = 0;
82
-
83
- /* * Write blocks to a block device
84
- *
85
- * A write is equivalent to an erase followed by a program
86
- *
87
- * If a failure occurs, it is not possible to determine how many bytes succeeded
88
- *
89
- * @param buffer Buffer of data to write to blocks
90
- * @param addr Address of block to begin writing to
91
- * @param size Size to write in bytes, must be a multiple of write block size
92
- * @return 0 on success, negative error code on failure
93
- */
94
- virtual bd_error_t write (const void *buffer, bd_addr_t addr, bd_size_t size);
71
+ virtual int read (void *buffer, bd_addr_t addr, bd_size_t size) = 0;
95
72
96
73
/* * Program blocks to a block device
97
74
*
@@ -104,7 +81,7 @@ class BlockDevice
104
81
* @param size Size to write in bytes, must be a multiple of program block size
105
82
* @return 0 on success, negative error code on failure
106
83
*/
107
- virtual bd_error_t program (const void *buffer, bd_addr_t addr, bd_size_t size) = 0;
84
+ virtual int program (const void *buffer, bd_addr_t addr, bd_size_t size) = 0;
108
85
109
86
/* * Erase blocks on a block device
110
87
*
@@ -114,22 +91,14 @@ class BlockDevice
114
91
* @param size Size to erase in bytes, must be a multiple of erase block size
115
92
* @return 0 on success, negative error code on failure
116
93
*/
117
- virtual bd_error_t erase (bd_addr_t addr, bd_size_t size) = 0;
94
+ virtual int erase (bd_addr_t addr, bd_size_t size) = 0;
118
95
119
96
/* * Get the size of a readable block
120
97
*
121
98
* @return Size of a readable block in bytes
122
99
*/
123
100
virtual bd_size_t get_read_size () const = 0;
124
101
125
- /* * Get the size of a writeable block
126
- *
127
- * @return Size of a writeable block in bytes
128
- * @note Must be a multiple of the read size, this is
129
- * equivalent to the erase size of the device
130
- */
131
- virtual bd_size_t get_write_size () const ;
132
-
133
102
/* * Get the size of a programable block
134
103
*
135
104
* @return Size of a programable block in bytes
@@ -156,31 +125,41 @@ class BlockDevice
156
125
* @param size Size to read in bytes
157
126
* @return True if read is valid for underlying block device
158
127
*/
159
- bool is_valid_read (bd_addr_t addr, bd_size_t size);
160
-
161
- /* * Convenience function for checking block write validity
162
- *
163
- * @param addr Address of block to begin writing to
164
- * @param size Size to write in bytes
165
- * @return True if write is valid for underlying block device
166
- */
167
- bool is_valid_write (bd_addr_t addr, bd_size_t size);
128
+ bool is_valid_read (bd_addr_t addr, bd_size_t size)
129
+ {
130
+ return (
131
+ addr % get_read_size () == 0 &&
132
+ size % get_read_size () == 0 &&
133
+ addr + size <= this ->size ());
134
+ }
168
135
169
136
/* * Convenience function for checking block program validity
170
137
*
171
138
* @param addr Address of block to begin writing to
172
139
* @param size Size to write in bytes
173
140
* @return True if program is valid for underlying block device
174
141
*/
175
- bool is_valid_program (bd_addr_t addr, bd_size_t size);
142
+ bool is_valid_program (bd_addr_t addr, bd_size_t size)
143
+ {
144
+ return (
145
+ addr % get_program_size () == 0 &&
146
+ size % get_program_size () == 0 &&
147
+ addr + size <= this ->size ());
148
+ }
176
149
177
150
/* * Convenience function for checking block erase validity
178
151
*
179
152
* @param addr Address of block to begin erasing
180
153
* @param size Size to erase in bytes
181
154
* @return True if erase is valid for underlying block device
182
155
*/
183
- bool is_valid_erase (bd_addr_t addr, bd_size_t size);
156
+ bool is_valid_erase (bd_addr_t addr, bd_size_t size)
157
+ {
158
+ return (
159
+ addr % get_erase_size () == 0 &&
160
+ size % get_erase_size () == 0 &&
161
+ addr + size <= this ->size ());
162
+ }
184
163
};
185
164
186
165
0 commit comments