Skip to content

Commit f438251

Browse files
authored
Merge pull request #3936 from geky/bd-mbr
bd: Add MBR block device for standard storage partitioning
2 parents 1a37ea6 + 3ee77e3 commit f438251

File tree

8 files changed

+933
-6
lines changed

8 files changed

+933
-6
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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+
#include "mbed.h"
17+
#include "greentea-client/test_env.h"
18+
#include "unity.h"
19+
#include "utest.h"
20+
21+
#include "HeapBlockDevice.h"
22+
#include "MBRBlockDevice.h"
23+
#include <stdlib.h>
24+
25+
using namespace utest::v1;
26+
27+
#define BLOCK_COUNT 16
28+
#define BLOCK_SIZE 512
29+
30+
HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
31+
32+
// Testing formatting of master boot record
33+
void test_mbr_format()
34+
{
35+
// Create two partitions splitting device in ~half
36+
int err = MBRBlockDevice::partition(&bd, 1, 0x83, 0, (BLOCK_COUNT/2)*BLOCK_SIZE);
37+
TEST_ASSERT_EQUAL(0, err);
38+
39+
err = MBRBlockDevice::partition(&bd, 2, 0x83, -(BLOCK_COUNT/2)*BLOCK_SIZE);
40+
TEST_ASSERT_EQUAL(0, err);
41+
42+
// Load both partitions, as well as a third to check for invalid partitions
43+
MBRBlockDevice part1(&bd, 1);
44+
err = part1.init();
45+
TEST_ASSERT_EQUAL(0, err);
46+
47+
MBRBlockDevice part2(&bd, 2);
48+
err = part2.init();
49+
TEST_ASSERT_EQUAL(0, err);
50+
51+
MBRBlockDevice part3(&bd, 3);
52+
err = part3.init();
53+
TEST_ASSERT_EQUAL(BD_ERROR_INVALID_PARTITION, err);
54+
55+
// Deinit partitions
56+
err = part1.deinit();
57+
TEST_ASSERT_EQUAL(0, err);
58+
59+
err = part2.deinit();
60+
TEST_ASSERT_EQUAL(0, err);
61+
}
62+
63+
// Testing mbr attributes
64+
void test_mbr_attr()
65+
{
66+
// Load partitions
67+
MBRBlockDevice part1(&bd, 1);
68+
int err = part1.init();
69+
TEST_ASSERT_EQUAL(0, err);
70+
71+
MBRBlockDevice part2(&bd, 2);
72+
err = part2.init();
73+
TEST_ASSERT_EQUAL(0, err);
74+
75+
// Test attributes on partitions
76+
printf("partition 1 partition number: %d\n", part1.get_partition_number());
77+
printf("partition 1 partition start: 0x%llx\n", part1.get_partition_start());
78+
printf("partition 1 partition stop: 0x%llx\n", part1.get_partition_stop());
79+
printf("partition 1 partition type: 0x%02x\n", part1.get_partition_type());
80+
printf("partition 1 read size: %llu bytes\n", part1.get_read_size());
81+
printf("partition 1 program size: %llu bytes\n", part1.get_program_size());
82+
printf("partition 1 erase size: %llu bytes\n", part1.get_erase_size());
83+
printf("partition 1 size: %llu bytes\n", part1.size());
84+
TEST_ASSERT_EQUAL(1, part1.get_partition_number());
85+
TEST_ASSERT_EQUAL(1*BLOCK_SIZE, part1.get_partition_start());
86+
TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, part1.get_partition_stop());
87+
TEST_ASSERT_EQUAL(0x83, part1.get_partition_type());
88+
TEST_ASSERT_EQUAL(BLOCK_SIZE, part1.get_read_size());
89+
TEST_ASSERT_EQUAL(BLOCK_SIZE, part1.get_program_size());
90+
TEST_ASSERT_EQUAL(BLOCK_SIZE, part1.get_erase_size());
91+
TEST_ASSERT_EQUAL(((BLOCK_COUNT/2)-1)*BLOCK_SIZE, part1.size());
92+
93+
printf("partition 2 partition number: %d\n", part2.get_partition_number());
94+
printf("partition 2 partition start: 0x%llx\n", part2.get_partition_start());
95+
printf("partition 2 partition stop: 0x%llx\n", part2.get_partition_stop());
96+
printf("partition 2 partition type: 0x%02x\n", part2.get_partition_type());
97+
printf("partition 2 read size: %llu bytes\n", part2.get_read_size());
98+
printf("partition 2 program size: %llu bytes\n", part2.get_program_size());
99+
printf("partition 2 erase size: %llu bytes\n", part2.get_erase_size());
100+
printf("partition 2 size: %llu bytes\n", part2.size());
101+
TEST_ASSERT_EQUAL(2, part2.get_partition_number());
102+
TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, part2.get_partition_start());
103+
TEST_ASSERT_EQUAL(BLOCK_COUNT*BLOCK_SIZE, part2.get_partition_stop());
104+
TEST_ASSERT_EQUAL(0x83, part2.get_partition_type());
105+
TEST_ASSERT_EQUAL(BLOCK_SIZE, part2.get_read_size());
106+
TEST_ASSERT_EQUAL(BLOCK_SIZE, part2.get_program_size());
107+
TEST_ASSERT_EQUAL(BLOCK_SIZE, part2.get_erase_size());
108+
TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, part2.size());
109+
110+
// Deinit partitions
111+
err = part1.deinit();
112+
TEST_ASSERT_EQUAL(0, err);
113+
114+
err = part2.deinit();
115+
TEST_ASSERT_EQUAL(0, err);
116+
}
117+
118+
// Testing mbr read write
119+
void test_mbr_read_write()
120+
{
121+
// Load partitions
122+
MBRBlockDevice part1(&bd, 1);
123+
int err = part1.init();
124+
TEST_ASSERT_EQUAL(0, err);
125+
126+
MBRBlockDevice part2(&bd, 2);
127+
err = part2.init();
128+
TEST_ASSERT_EQUAL(0, err);
129+
130+
// Test reading/writing the partitions
131+
uint8_t *write_block = new uint8_t[BLOCK_SIZE];
132+
uint8_t *read_block = new uint8_t[BLOCK_SIZE];
133+
134+
// Fill with random sequence
135+
srand(1);
136+
for (int i = 0; i < BLOCK_SIZE; i++) {
137+
write_block[i] = 0xff & rand();
138+
}
139+
140+
// Write, sync, and read the block
141+
err = part1.erase(0, BLOCK_SIZE);
142+
TEST_ASSERT_EQUAL(0, err);
143+
144+
err = part1.program(write_block, 0, BLOCK_SIZE);
145+
TEST_ASSERT_EQUAL(0, err);
146+
147+
err = part1.read(read_block, 0, BLOCK_SIZE);
148+
TEST_ASSERT_EQUAL(0, err);
149+
150+
// Check that the data was unmodified
151+
srand(1);
152+
for (int i = 0; i < BLOCK_SIZE; i++) {
153+
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
154+
}
155+
156+
// Check with original block device
157+
err = bd.read(read_block, 1*BLOCK_SIZE, BLOCK_SIZE);
158+
TEST_ASSERT_EQUAL(0, err);
159+
160+
// Check that the data was unmodified
161+
srand(1);
162+
for (int i = 0; i < BLOCK_SIZE; i++) {
163+
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
164+
}
165+
166+
// Test with second slice of block device
167+
srand(1);
168+
for (int i = 0; i < BLOCK_SIZE; i++) {
169+
write_block[i] = 0xff & rand();
170+
}
171+
172+
// Write, sync, and read the block
173+
err = part2.erase(0, BLOCK_SIZE);
174+
TEST_ASSERT_EQUAL(0, err);
175+
176+
err = part2.program(write_block, 0, BLOCK_SIZE);
177+
TEST_ASSERT_EQUAL(0, err);
178+
179+
err = part2.read(read_block, 0, BLOCK_SIZE);
180+
TEST_ASSERT_EQUAL(0, err);
181+
182+
// Check that the data was unmodified
183+
srand(1);
184+
for (int i = 0; i < BLOCK_SIZE; i++) {
185+
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
186+
}
187+
188+
// Check with original block device
189+
err = bd.read(read_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE);
190+
TEST_ASSERT_EQUAL(0, err);
191+
192+
// Check that the data was unmodified
193+
srand(1);
194+
for (int i = 0; i < BLOCK_SIZE; i++) {
195+
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
196+
}
197+
198+
// Clean up
199+
delete[] write_block;
200+
delete[] read_block;
201+
202+
err = part1.deinit();
203+
TEST_ASSERT_EQUAL(0, err);
204+
205+
err = part2.deinit();
206+
TEST_ASSERT_EQUAL(0, err);
207+
}
208+
209+
210+
// Test setup
211+
utest::v1::status_t test_setup(const size_t number_of_cases) {
212+
GREENTEA_SETUP(10, "default_auto");
213+
return verbose_test_setup_handler(number_of_cases);
214+
}
215+
216+
Case cases[] = {
217+
Case("Testing formatting of master boot record", test_mbr_format),
218+
Case("Testing mbr attributes", test_mbr_attr),
219+
Case("Testing mbr read write", test_mbr_read_write),
220+
};
221+
222+
Specification specification(test_setup, cases);
223+
224+
int main() {
225+
return !Harness::run(specification);
226+
}

0 commit comments

Comments
 (0)