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
pull/3936/head
Christopher Haster 2017-05-24 11:16:52 -05:00
parent d1468a68ab
commit 3f92a15960
4 changed files with 190 additions and 4 deletions

View File

@ -0,0 +1,183 @@
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "HeapBlockDevice.h"
#include "FATFileSystem.h"
#include "MBRBlockDevice.h"
#include <stdlib.h>
#include "mbed_retarget.h"
using namespace utest::v1;
#ifndef MBED_EXTENDED_TESTS
#error [NOT_SUPPORTED] Filesystem tests not supported by default
#endif
// Test block device
#define BLOCK_SIZE 512
#define BLOCK_COUNT 512
HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
// Test formatting and partitioning
void test_format() {
// Create two partitions splitting device in ~half
int err = MBRBlockDevice::partition(&bd, 1, 0x83, 0, (BLOCK_COUNT/2)*BLOCK_SIZE);
TEST_ASSERT_EQUAL(0, err);
err = MBRBlockDevice::partition(&bd, 2, 0x83, -(BLOCK_COUNT/2)*BLOCK_SIZE);
TEST_ASSERT_EQUAL(0, err);
// Load both partitions
MBRBlockDevice part1(&bd, 1);
err = part1.init();
TEST_ASSERT_EQUAL(0, err);
MBRBlockDevice part2(&bd, 2);
err = part2.init();
TEST_ASSERT_EQUAL(0, err);
// Format both partitions
err = FATFileSystem::format(&part1);
TEST_ASSERT_EQUAL(0, err);
err = FATFileSystem::format(&part2);
TEST_ASSERT_EQUAL(0, err);
// Unload the partitions
err = part1.deinit();
TEST_ASSERT_EQUAL(0, err);
err = part2.deinit();
TEST_ASSERT_EQUAL(0, err);
}
// Simple multipartition test for reading/writing files
template <ssize_t TEST_SIZE>
void test_read_write() {
// Load both partitions
MBRBlockDevice part1(&bd, 1);
int err = part1.init();
TEST_ASSERT_EQUAL(0, err);
MBRBlockDevice part2(&bd, 2);
err = part2.init();
TEST_ASSERT_EQUAL(0, err);
// Create fat filesystems on both partitions
FATFileSystem fs1("fat1");
FATFileSystem fs2("fat2");
err = fs1.mount(&part1);
TEST_ASSERT_EQUAL(0, err);
err = fs2.mount(&part2);
TEST_ASSERT_EQUAL(0, err);
uint8_t *buffer1 = (uint8_t *)malloc(TEST_SIZE);
TEST_ASSERT(buffer1);
uint8_t *buffer2 = (uint8_t *)malloc(TEST_SIZE);
TEST_ASSERT(buffer2);
// Fill with random sequence
srand(1);
for (int i = 0; i < TEST_SIZE; i++) {
buffer1[i] = 0xff & rand();
}
for (int i = 0; i < TEST_SIZE; i++) {
buffer2[i] = 0xff & rand();
}
// write and read files on both partitions
File file;
err = file.open(&fs1, "test_read_write.dat", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, err);
ssize_t size = file.write(buffer1, TEST_SIZE);
TEST_ASSERT_EQUAL(TEST_SIZE, size);
err = file.close();
TEST_ASSERT_EQUAL(0, err);
err = file.open(&fs2, "test_read_write.dat", O_WRONLY | O_CREAT);
TEST_ASSERT_EQUAL(0, err);
size = file.write(buffer2, TEST_SIZE);
TEST_ASSERT_EQUAL(TEST_SIZE, size);
err = file.close();
TEST_ASSERT_EQUAL(0, err);
err = file.open(&fs1, "test_read_write.dat", O_RDONLY);
TEST_ASSERT_EQUAL(0, err);
size = file.read(buffer1, TEST_SIZE);
TEST_ASSERT_EQUAL(TEST_SIZE, size);
err = file.close();
TEST_ASSERT_EQUAL(0, err);
err = file.open(&fs2, "test_read_write.dat", O_RDONLY);
TEST_ASSERT_EQUAL(0, err);
size = file.read(buffer2, TEST_SIZE);
TEST_ASSERT_EQUAL(TEST_SIZE, size);
err = file.close();
TEST_ASSERT_EQUAL(0, err);
// Check that the data was unmodified
srand(1);
for (int i = 0; i < TEST_SIZE; i++) {
TEST_ASSERT_EQUAL(0xff & rand(), buffer1[i]);
}
for (int i = 0; i < TEST_SIZE; i++) {
TEST_ASSERT_EQUAL(0xff & rand(), buffer2[i]);
}
err = fs1.unmount();
TEST_ASSERT_EQUAL(0, err);
err = fs2.unmount();
TEST_ASSERT_EQUAL(0, err);
err = part1.deinit();
TEST_ASSERT_EQUAL(0, err);
err = part2.deinit();
TEST_ASSERT_EQUAL(0, err);
}
// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(10, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("Testing formating", test_format),
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

View File

@ -140,7 +140,7 @@
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/
#define _VOLUMES 1
#define _VOLUMES 4
/* Number of volumes (logical drives) to be used. */

View File

@ -252,7 +252,8 @@ int FATFileSystem::mount(BlockDevice *bd, bool force) {
_id = i;
_ffs[_id] = bd;
_fsid[0] = '0' + _id;
_fsid[1] = '\0';
_fsid[1] = ':';
_fsid[2] = '\0';
debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
FRESULT res = f_mount(&_fs, _fsid, force);
unlock();
@ -377,7 +378,9 @@ int FATFileSystem::file_open(fs_file_t *file, const char *path, int flags) {
FIL *fh = new FIL;
char *buffer = new char[strlen(_fsid) + strlen(path) + 3];
sprintf(buffer, "%s:/%s", _fsid, path);
strcpy(buffer, _fsid);
strcat(buffer, "/");
strcat(buffer, path);
/* POSIX flags -> FatFS open mode */
BYTE openmode;

View File

@ -227,7 +227,7 @@ protected:
private:
FATFS _fs; // Work area (file system object) for logical drive
char _fsid[2];
char _fsid[sizeof("0:")];
int _id;
protected: