Skip to content

Commit a2220f3

Browse files
Cruz Monrrealcmonr
authored andcommitted
Merge pull request #9384 from davidsaada/david_mbr_erase_start
MBRBlockDevice: When partitioning, clear the rest of first erase unit
1 parent 5e0a2d9 commit a2220f3

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

features/storage/TESTS/filesystem/multipart_fat_filesystem/main.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "HeapBlockDevice.h"
2222
#include "FATFileSystem.h"
2323
#include "MBRBlockDevice.h"
24+
#include "LittleFileSystem.h"
2425
#include <stdlib.h>
2526
#include "mbed_retarget.h"
2627

@@ -221,9 +222,66 @@ void test_single_mbr()
221222
TEST_ASSERT_EQUAL(0, err);
222223

223224
delete bd;
224-
bd = 0;
225225
}
226226

227+
void test_with_other_fs()
228+
{
229+
TEST_SKIP_UNLESS_MESSAGE(bd, "Not enough heap memory to run test. Test skipped.");
230+
231+
// Stage 0 - LittleFS
232+
// Stage 1 - FatFS with MBR
233+
// Stage 2 - LittleFS
234+
// Make sure that at no stage we are able to mount the current file system after using the
235+
// previous one
236+
237+
// start from scratch in this test
238+
bd = new (std::nothrow) HeapBlockDevice(BLOCK_COUNT * BLOCK_SIZE, BLOCK_SIZE);
239+
TEST_SKIP_UNLESS_MESSAGE(bd, "Not enough heap memory to run test. Test skipped.");
240+
241+
int err;
242+
243+
for (int stage = 0; stage < 3; stage++) {
244+
245+
BlockDevice *part;
246+
FileSystem *fs;
247+
248+
if (stage == 1) {
249+
printf("Stage %d: FAT FS\n", stage + 1);
250+
err = MBRBlockDevice::partition(bd, 1, 0x83, 0, BLOCK_COUNT * BLOCK_SIZE);
251+
TEST_ASSERT_EQUAL(0, err);
252+
253+
part = new (std::nothrow) MBRBlockDevice(bd, 1);
254+
TEST_SKIP_UNLESS_MESSAGE(part, "Not enough heap memory to run test. Test skipped.");
255+
256+
err = part->init();
257+
TEST_ASSERT_EQUAL(0, err);
258+
259+
fs = new FATFileSystem("fat");
260+
} else {
261+
printf("Stage %d: Little FS\n", stage + 1);
262+
part = bd;
263+
fs = new LittleFileSystem("lfs");
264+
}
265+
TEST_SKIP_UNLESS_MESSAGE(fs, "Not enough heap memory to run test. Test skipped.");
266+
267+
err = fs->mount(part);
268+
TEST_ASSERT_NOT_EQUAL(0, err);
269+
270+
err = fs->reformat(part);
271+
TEST_ASSERT_EQUAL(0, err);
272+
273+
err = fs->unmount();
274+
TEST_ASSERT_EQUAL(0, err);
275+
276+
delete fs;
277+
if (stage == 1) {
278+
delete part;
279+
}
280+
}
281+
282+
delete bd;
283+
bd = 0;
284+
}
227285

228286
// Test setup
229287
utest::v1::status_t test_setup(const size_t number_of_cases)
@@ -237,6 +295,7 @@ Case cases[] = {
237295
Case("Testing read write < block", test_read_write < BLOCK_SIZE / 2 >),
238296
Case("Testing read write > block", test_read_write<2 * BLOCK_SIZE>),
239297
Case("Testing for no extra MBRs", test_single_mbr),
298+
Case("Testing with other file system", test_with_other_fs),
240299
};
241300

242301
Specification specification(test_setup, cases);

features/storage/blockdevice/MBRBlockDevice.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ static int partition_absolute(
9292
return err;
9393
}
9494

95+
uint32_t table_start_offset = buffer_size - sizeof(struct mbr_table);
9596
struct mbr_table *table = reinterpret_cast<struct mbr_table *>(
96-
&buffer[buffer_size - sizeof(struct mbr_table)]);
97+
&buffer[table_start_offset]);
9798
if (table->signature[0] != 0x55 || table->signature[1] != 0xaa) {
9899
// Setup default values for MBR
99100
table->signature[0] = 0x55;
@@ -143,6 +144,16 @@ static int partition_absolute(
143144
}
144145
}
145146

147+
// As the erase operation may do nothing, erase remainder of the buffer, to eradicate
148+
// any remaining programmed data (such as previously programmed file systems).
149+
if (table_start_offset > 0) {
150+
memset(buffer, 0xFF, table_start_offset);
151+
}
152+
if (table_start_offset + sizeof(struct mbr_table) < buffer_size) {
153+
memset(buffer + table_start_offset + sizeof(struct mbr_table), 0xFF,
154+
buffer_size - (table_start_offset + sizeof(struct mbr_table)));
155+
}
156+
146157
// Write out MBR
147158
err = bd->erase(0, bd->get_erase_size());
148159
if (err) {

0 commit comments

Comments
 (0)