Skip to content

Commit 0960e8f

Browse files
gekysimonqhughes
authored andcommitted
bd: Added prototype block device api
1 parent 5d840af commit 0960e8f

File tree

2 files changed

+261
-0
lines changed

2 files changed

+261
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "BlockDevice.h"
18+
19+
20+
// Writes by default perform an erase + program
21+
bd_error_t BlockDevice::write(const void *buffer, bd_addr_t addr, bd_size_t size)
22+
{
23+
if (!is_valid_write(addr, size)) {
24+
return BD_ERROR_PARAMETER;
25+
}
26+
27+
bd_error_t err = erase(addr, size);
28+
if (err) {
29+
return err;
30+
}
31+
32+
return program(buffer, addr, size);
33+
}
34+
35+
bd_size_t BlockDevice::get_write_size()
36+
{
37+
return get_erase_size();
38+
}
39+
40+
bool BlockDevice::is_valid_write(bd_addr_t addr, bd_size_t size)
41+
{
42+
return is_valid_erase(addr, size);
43+
}
44+
45+
46+
// Convenience functions for checking block validity
47+
static bool is_aligned(uint64_t x, uint64_t alignment)
48+
{
49+
return (x / alignment) * alignment == x;
50+
}
51+
52+
bool BlockDevice::is_valid_read(bd_addr_t addr, bd_size_t size)
53+
{
54+
return (
55+
is_aligned(addr, get_read_size()) &&
56+
is_aligned(size, get_read_size()) &&
57+
addr + size <= this->size());
58+
}
59+
60+
bool BlockDevice::is_valid_erase(bd_addr_t addr, bd_size_t size)
61+
{
62+
return (
63+
is_aligned(addr, get_erase_size()) &&
64+
is_aligned(size, get_erase_size()) &&
65+
addr + size <= this->size());
66+
}
67+
68+
bool BlockDevice::is_valid_program(bd_addr_t addr, bd_size_t size)
69+
{
70+
return (
71+
is_aligned(addr, get_program_size()) &&
72+
is_aligned(size, get_program_size()) &&
73+
addr + size <= this->size());
74+
}
75+

features/filesystem/bd/BlockDevice.h

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MBED_BLOCK_DEVICE_H
18+
#define MBED_BLOCK_DEVICE_H
19+
20+
#include <stdint.h>
21+
22+
23+
/** Enum of standard error codes
24+
*
25+
* @enum bd_error_t
26+
*/
27+
enum bd_error {
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 */
36+
};
37+
38+
/** Type representing either 0 or a negative error code
39+
*/
40+
typedef int32_t bd_error_t;
41+
42+
/** Type representing the address of a specific block
43+
*/
44+
typedef uint64_t bd_addr_t;
45+
46+
/** Type representing a quantity of 8-bit bytes
47+
*/
48+
typedef uint64_t bd_size_t;
49+
50+
51+
/** A hardware device capable of writing and reading blocks
52+
*/
53+
class BlockDevice {
54+
public:
55+
/** Lifetime of a block device
56+
*/
57+
virtual ~BlockDevice() {};
58+
59+
/** Initialize a block device
60+
*
61+
* @return 0 on success or a negative error code on failure
62+
*/
63+
virtual bd_error_t init() = 0;
64+
65+
/** Deinitialize a block device
66+
*
67+
* @return 0 on success or a negative error code on failure
68+
*/
69+
virtual bd_error_t deinit() = 0;
70+
71+
/** Read blocks from a block device
72+
*
73+
* If a failure occurs, it is not possible to determine how many bytes succeeded
74+
*
75+
* @param buffer Buffer to write blocks to
76+
* @param addr Address of block to begin reading from
77+
* @param size Size to read in bytes, must be a multiple of read block size
78+
* @return 0 on success, negative error code on failure
79+
*/
80+
virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size) = 0;
81+
82+
/** Write blocks to a block device
83+
*
84+
* A write is equivalent to an erase followed by a program
85+
*
86+
* If a failure occurs, it is not possible to determine how many bytes succeeded
87+
*
88+
* @param buffer Buffer of data to write to blocks
89+
* @param addr Address of block to begin writing to
90+
* @param size Size to write in bytes, must be a multiple of write block size
91+
* @return 0 on success, negative error code on failure
92+
*/
93+
virtual bd_error_t write(const void *buffer, bd_addr_t addr, bd_size_t size);
94+
95+
/** Program blocks to a block device
96+
*
97+
* The blocks must have been erased prior to being programmed
98+
*
99+
* If a failure occurs, it is not possible to determine how many bytes succeeded
100+
*
101+
* @param buffer Buffer of data to write to blocks
102+
* @param addr Address of block to begin writing to
103+
* @param size Size to write in bytes, must be a multiple of program block size
104+
* @return 0 on success, negative error code on failure
105+
*/
106+
virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size) = 0;
107+
108+
/** Erase blocks on a block device
109+
*
110+
* The state of an erased block is undefined until it has been programmed
111+
*
112+
* @param addr Address of block to begin erasing
113+
* @param size Size to erase in bytes, must be a multiple of erase block size
114+
* @return 0 on success, negative error code on failure
115+
*/
116+
virtual bd_error_t erase(bd_addr_t addr, bd_size_t size) = 0;
117+
118+
/** Get the size of a readable block
119+
*
120+
* @return Size of a readable block in bytes
121+
*/
122+
virtual bd_size_t get_read_size() = 0;
123+
124+
/** Get the size of a writeable block
125+
*
126+
* @return Size of a writeable block in bytes
127+
* @note Must be a multiple of the read size, this is
128+
* equivalent to the erase size of the device
129+
*/
130+
virtual bd_size_t get_write_size();
131+
132+
/** Get the size of a programable block
133+
*
134+
* @return Size of a programable block in bytes
135+
* @note Must be a multiple of the read size
136+
*/
137+
virtual bd_size_t get_program_size() = 0;
138+
139+
/** Get the size of a eraseable block
140+
*
141+
* @return Size of a eraseable block in bytes
142+
* @note Must be a multiple of the program size
143+
*/
144+
virtual bd_size_t get_erase_size() = 0;
145+
146+
/** Get the total size of the underlying device
147+
*
148+
* @return Size of the underlying device in bytes
149+
*/
150+
virtual bd_size_t size() = 0;
151+
152+
/** Convenience function for checking block read validity
153+
*
154+
* @param addr Address of block to begin reading from
155+
* @param size Size to read in bytes
156+
* @return True if read is valid for underlying block device
157+
*/
158+
bool is_valid_read(bd_addr_t addr, bd_size_t size);
159+
160+
/** Convenience function for checking block write validity
161+
*
162+
* @param addr Address of block to begin writing to
163+
* @param size Size to write in bytes
164+
* @return True if write is valid for underlying block device
165+
*/
166+
bool is_valid_write(bd_addr_t addr, bd_size_t size);
167+
168+
/** Convenience function for checking block program validity
169+
*
170+
* @param addr Address of block to begin writing to
171+
* @param size Size to write in bytes
172+
* @return True if program is valid for underlying block device
173+
*/
174+
bool is_valid_program(bd_addr_t addr, bd_size_t size);
175+
176+
/** Convenience function for checking block erase validity
177+
*
178+
* @param addr Address of block to begin erasing
179+
* @param size Size to erase in bytes
180+
* @return True if erase is valid for underlying block device
181+
*/
182+
bool is_valid_erase(bd_addr_t addr, bd_size_t size);
183+
};
184+
185+
186+
#endif

0 commit comments

Comments
 (0)