Skip to content

Commit 3f92a15

Browse files
committed
FAT: Added support for multiple active filesystems
- Increased _VOLUMES to 4 - Fixed a few issues in the FATFileSystem's _fsid - Added tests for multiple partitions of fatfs
1 parent d1468a6 commit 3f92a15

File tree

4 files changed

+190
-4
lines changed

4 files changed

+190
-4
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 "FATFileSystem.h"
23+
#include "MBRBlockDevice.h"
24+
#include <stdlib.h>
25+
#include "mbed_retarget.h"
26+
27+
using namespace utest::v1;
28+
29+
#ifndef MBED_EXTENDED_TESTS
30+
#error [NOT_SUPPORTED] Filesystem tests not supported by default
31+
#endif
32+
33+
// Test block device
34+
#define BLOCK_SIZE 512
35+
#define BLOCK_COUNT 512
36+
HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
37+
38+
39+
// Test formatting and partitioning
40+
void test_format() {
41+
// Create two partitions splitting device in ~half
42+
int err = MBRBlockDevice::partition(&bd, 1, 0x83, 0, (BLOCK_COUNT/2)*BLOCK_SIZE);
43+
TEST_ASSERT_EQUAL(0, err);
44+
45+
err = MBRBlockDevice::partition(&bd, 2, 0x83, -(BLOCK_COUNT/2)*BLOCK_SIZE);
46+
TEST_ASSERT_EQUAL(0, err);
47+
48+
// Load both partitions
49+
MBRBlockDevice part1(&bd, 1);
50+
err = part1.init();
51+
TEST_ASSERT_EQUAL(0, err);
52+
53+
MBRBlockDevice part2(&bd, 2);
54+
err = part2.init();
55+
TEST_ASSERT_EQUAL(0, err);
56+
57+
// Format both partitions
58+
err = FATFileSystem::format(&part1);
59+
TEST_ASSERT_EQUAL(0, err);
60+
61+
err = FATFileSystem::format(&part2);
62+
TEST_ASSERT_EQUAL(0, err);
63+
64+
// Unload the partitions
65+
err = part1.deinit();
66+
TEST_ASSERT_EQUAL(0, err);
67+
68+
err = part2.deinit();
69+
TEST_ASSERT_EQUAL(0, err);
70+
}
71+
72+
73+
// Simple multipartition test for reading/writing files
74+
template <ssize_t TEST_SIZE>
75+
void test_read_write() {
76+
// Load both partitions
77+
MBRBlockDevice part1(&bd, 1);
78+
int err = part1.init();
79+
TEST_ASSERT_EQUAL(0, err);
80+
81+
MBRBlockDevice part2(&bd, 2);
82+
err = part2.init();
83+
TEST_ASSERT_EQUAL(0, err);
84+
85+
// Create fat filesystems on both partitions
86+
FATFileSystem fs1("fat1");
87+
FATFileSystem fs2("fat2");
88+
89+
err = fs1.mount(&part1);
90+
TEST_ASSERT_EQUAL(0, err);
91+
92+
err = fs2.mount(&part2);
93+
TEST_ASSERT_EQUAL(0, err);
94+
95+
uint8_t *buffer1 = (uint8_t *)malloc(TEST_SIZE);
96+
TEST_ASSERT(buffer1);
97+
98+
uint8_t *buffer2 = (uint8_t *)malloc(TEST_SIZE);
99+
TEST_ASSERT(buffer2);
100+
101+
// Fill with random sequence
102+
srand(1);
103+
104+
for (int i = 0; i < TEST_SIZE; i++) {
105+
buffer1[i] = 0xff & rand();
106+
}
107+
108+
for (int i = 0; i < TEST_SIZE; i++) {
109+
buffer2[i] = 0xff & rand();
110+
}
111+
112+
// write and read files on both partitions
113+
File file;
114+
err = file.open(&fs1, "test_read_write.dat", O_WRONLY | O_CREAT);
115+
TEST_ASSERT_EQUAL(0, err);
116+
ssize_t size = file.write(buffer1, TEST_SIZE);
117+
TEST_ASSERT_EQUAL(TEST_SIZE, size);
118+
err = file.close();
119+
TEST_ASSERT_EQUAL(0, err);
120+
121+
err = file.open(&fs2, "test_read_write.dat", O_WRONLY | O_CREAT);
122+
TEST_ASSERT_EQUAL(0, err);
123+
size = file.write(buffer2, TEST_SIZE);
124+
TEST_ASSERT_EQUAL(TEST_SIZE, size);
125+
err = file.close();
126+
TEST_ASSERT_EQUAL(0, err);
127+
128+
err = file.open(&fs1, "test_read_write.dat", O_RDONLY);
129+
TEST_ASSERT_EQUAL(0, err);
130+
size = file.read(buffer1, TEST_SIZE);
131+
TEST_ASSERT_EQUAL(TEST_SIZE, size);
132+
err = file.close();
133+
TEST_ASSERT_EQUAL(0, err);
134+
135+
err = file.open(&fs2, "test_read_write.dat", O_RDONLY);
136+
TEST_ASSERT_EQUAL(0, err);
137+
size = file.read(buffer2, TEST_SIZE);
138+
TEST_ASSERT_EQUAL(TEST_SIZE, size);
139+
err = file.close();
140+
TEST_ASSERT_EQUAL(0, err);
141+
142+
// Check that the data was unmodified
143+
srand(1);
144+
145+
for (int i = 0; i < TEST_SIZE; i++) {
146+
TEST_ASSERT_EQUAL(0xff & rand(), buffer1[i]);
147+
}
148+
149+
for (int i = 0; i < TEST_SIZE; i++) {
150+
TEST_ASSERT_EQUAL(0xff & rand(), buffer2[i]);
151+
}
152+
153+
err = fs1.unmount();
154+
TEST_ASSERT_EQUAL(0, err);
155+
156+
err = fs2.unmount();
157+
TEST_ASSERT_EQUAL(0, err);
158+
159+
err = part1.deinit();
160+
TEST_ASSERT_EQUAL(0, err);
161+
162+
err = part2.deinit();
163+
TEST_ASSERT_EQUAL(0, err);
164+
}
165+
166+
167+
// Test setup
168+
utest::v1::status_t test_setup(const size_t number_of_cases) {
169+
GREENTEA_SETUP(10, "default_auto");
170+
return verbose_test_setup_handler(number_of_cases);
171+
}
172+
173+
Case cases[] = {
174+
Case("Testing formating", test_format),
175+
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
176+
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
177+
};
178+
179+
Specification specification(test_setup, cases);
180+
181+
int main() {
182+
return !Harness::run(specification);
183+
}

features/filesystem/fat/ChaN/ffconf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
/ Drive/Volume Configurations
141141
/---------------------------------------------------------------------------*/
142142

143-
#define _VOLUMES 1
143+
#define _VOLUMES 4
144144
/* Number of volumes (logical drives) to be used. */
145145

146146

features/filesystem/fat/FATFileSystem.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ int FATFileSystem::mount(BlockDevice *bd, bool force) {
252252
_id = i;
253253
_ffs[_id] = bd;
254254
_fsid[0] = '0' + _id;
255-
_fsid[1] = '\0';
255+
_fsid[1] = ':';
256+
_fsid[2] = '\0';
256257
debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
257258
FRESULT res = f_mount(&_fs, _fsid, force);
258259
unlock();
@@ -377,7 +378,9 @@ int FATFileSystem::file_open(fs_file_t *file, const char *path, int flags) {
377378

378379
FIL *fh = new FIL;
379380
char *buffer = new char[strlen(_fsid) + strlen(path) + 3];
380-
sprintf(buffer, "%s:/%s", _fsid, path);
381+
strcpy(buffer, _fsid);
382+
strcat(buffer, "/");
383+
strcat(buffer, path);
381384

382385
/* POSIX flags -> FatFS open mode */
383386
BYTE openmode;

features/filesystem/fat/FATFileSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class FATFileSystem : public FileSystem {
227227

228228
private:
229229
FATFS _fs; // Work area (file system object) for logical drive
230-
char _fsid[2];
230+
char _fsid[sizeof("0:")];
231231
int _id;
232232

233233
protected:

0 commit comments

Comments
 (0)