bd: Adopted the block storage api in the FATFileSystem

pull/3762/head
Christopher Haster 2017-01-20 14:09:38 -06:00 committed by Simon Hughes
parent ba5e1427fc
commit 0176450c5a
4 changed files with 267 additions and 215 deletions

View File

@ -0,0 +1,96 @@
/* 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 <stdlib.h>
using namespace utest::v1;
// Test block device
#define BLOCK_SIZE 512
HeapBlockDevice bd(128*BLOCK_SIZE, BLOCK_SIZE);
void test_format() {
int err = FATFileSystem::format(&bd);
TEST_ASSERT_EQUAL(0, err);
}
// Simple test for reading/writing files
template <ssize_t TEST_SIZE>
void test_read_write() {
FATFileSystem fs("fat");
int err = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, err);
uint8_t *buffer = (uint8_t *)malloc(TEST_SIZE);
TEST_ASSERT(buffer);
// Fill with random sequence
srand(1);
for (int i = 0; i < TEST_SIZE; i++) {
buffer[i] = 0xff & rand();
}
// write and read file
FileHandle *file = fs.open("test_read_write.dat", O_WRONLY | O_CREAT);
TEST_ASSERT(file);
ssize_t size = file->write(buffer, TEST_SIZE);
TEST_ASSERT_EQUAL(TEST_SIZE, size);
err = file->close();
TEST_ASSERT_EQUAL(0, err);
file = fs.open("test_read_write.dat", O_RDONLY);
TEST_ASSERT(file);
size = file->read(buffer, 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(), buffer[i]);
}
err = fs.unmount();
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

@ -1,117 +0,0 @@
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */
/* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
#include "diskio.h"
#include "mbed_debug.h"
#include "FATFileSystem.h"
using namespace mbed;
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
debug_if(FFS_DBG, "disk_status on pdrv [%d]\n", pdrv);
return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_status();
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
debug_if(FFS_DBG, "disk_initialize on pdrv [%d]\n", pdrv);
return (DSTATUS)FATFileSystem::_ffs[pdrv]->disk_initialize();
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE* buff, /* Data buffer to store read data */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to read */
)
{
debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
if (FATFileSystem::_ffs[pdrv]->disk_read((uint8_t*)buff, sector, count))
return RES_PARERR;
else
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if _USE_WRITE
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE* buff, /* Data to be written */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to write */
)
{
debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
if (FATFileSystem::_ffs[pdrv]->disk_write((uint8_t*)buff, sector, count))
return RES_PARERR;
else
return RES_OK;
}
#endif
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
#if _USE_IOCTL
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void* buff /* Buffer to send/receive control data */
)
{
debug_if(FFS_DBG, "disk_ioctl(%d)\n", cmd);
switch(cmd) {
case CTRL_SYNC:
if(FATFileSystem::_ffs[pdrv] == NULL) {
return RES_NOTRDY;
} else if(FATFileSystem::_ffs[pdrv]->disk_sync()) {
return RES_ERROR;
}
return RES_OK;
case GET_SECTOR_COUNT:
if(FATFileSystem::_ffs[pdrv] == NULL) {
return RES_NOTRDY;
} else {
DWORD res = FATFileSystem::_ffs[pdrv]->disk_sectors();
if(res > 0) {
*((DWORD*)buff) = res; // minimum allowed
return RES_OK;
} else {
return RES_ERROR;
}
}
case GET_BLOCK_SIZE:
*((DWORD*)buff) = 1; // default when not known
return RES_OK;
}
return RES_PARERR;
}
#endif

View File

@ -21,6 +21,7 @@
*/
#include "mbed.h"
#include "diskio.h"
#include "ffconf.h"
#include "mbed_debug.h"
@ -29,6 +30,7 @@
#include "FATDirHandle.h"
//<<<<<<< HEAD
//<<<<<<< HEAD
//<<<<<<< HEAD
#include "mbed_critical.h"
//=======
//#include "critical.h"
@ -36,9 +38,19 @@
//#include "critical.h"
#include "ff.h"
//>>>>>>> Added errno codes to retarget, mkdir() and ftell() tests.
//=======
//#include "critical.h"
//>>>>>>> bd: Adopted the block storage api in the FATFileSystem
#include <errno.h>
//>>>>>>> Filesystem: Added EEXIST reporting to mkdir through errno
// Global access to block device from FAT driver
static BlockDevice *_ffs[_VOLUMES] = {0};
static SingletonPtr<PlatformMutex> _ffs_mutex;
// FAT driver functions
DWORD get_fattime(void) {
time_t rawtime;
time(&rawtime);
@ -51,22 +63,62 @@ DWORD get_fattime(void) {
| (DWORD)(ptm->tm_sec/2 );
}
FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
static PlatformMutex * mutex = NULL;
// Implementation of diskio functions (see ChaN/diskio.h)
DSTATUS disk_status(BYTE pdrv) {
debug_if(FFS_DBG, "disk_status on pdrv [%d]\n", pdrv);
return RES_OK;
}
PlatformMutex * get_fat_mutex() {
PlatformMutex * new_mutex = new PlatformMutex;
DSTATUS disk_initialize(BYTE pdrv) {
debug_if(FFS_DBG, "disk_initialize on pdrv [%d]\n", pdrv);
return (DSTATUS)_ffs[pdrv]->init();
}
core_util_critical_section_enter();
if (NULL == mutex) {
mutex = new_mutex;
DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count) {
debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
bd_size_t ssize = _ffs[pdrv]->get_write_size();
int err = _ffs[pdrv]->read(buff, sector*ssize, count*ssize);
return err ? RES_PARERR : RES_OK;
}
DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count) {
debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
bd_size_t ssize = _ffs[pdrv]->get_write_size();
int err = _ffs[pdrv]->write(buff, sector*ssize, count*ssize);
return err ? RES_PARERR : RES_OK;
}
DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
debug_if(FFS_DBG, "disk_ioctl(%d)\n", cmd);
switch (cmd) {
case CTRL_SYNC:
if (_ffs[pdrv] == NULL) {
return RES_NOTRDY;
} else {
return RES_OK;
}
case GET_SECTOR_COUNT:
if (_ffs[pdrv] == NULL) {
return RES_NOTRDY;
} else {
DWORD count = _ffs[pdrv]->size() / _ffs[pdrv]->get_write_size();
*((DWORD*)buff) = count;
return RES_OK;
}
case GET_SECTOR_SIZE:
if (_ffs[pdrv] == NULL) {
return RES_NOTRDY;
} else {
DWORD size = _ffs[pdrv]->get_write_size();
*((DWORD*)buff) = size;
return RES_OK;
}
case GET_BLOCK_SIZE:
*((DWORD*)buff) = 1; // default when not known
return RES_OK;
}
core_util_critical_section_exit();
if (mutex != new_mutex) {
delete new_mutex;
}
return mutex;
return RES_PARERR;
}
/* @brief Set errno based on the error code returned from underlying filesystem
@ -119,33 +171,91 @@ static void FATFileSystemSetErrno(FRESULT res)
return;
}
FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n), _mutex(get_fat_mutex()) {
lock();
debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
for(int i=0; i<_VOLUMES; i++) {
if(_ffs[i] == 0) {
_ffs[i] = this;
_fsid[0] = '0' + i;
_fsid[1] = '\0';
debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
f_mount(&_fs, _fsid, 0);
unlock();
return;
}
// Filesystem implementation (See FATFilySystem.h)
FATFileSystem::FATFileSystem(const char *n, BlockDevice *bd)
: FileSystemLike(n), _id(-1) {
if (bd) {
mount(bd);
}
error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
unlock();
}
FATFileSystem::~FATFileSystem() {
// nop if unmounted
unmount();
}
int FATFileSystem::mount(BlockDevice *bd) {
return mount(bd, true);
}
int FATFileSystem::mount(BlockDevice *bd, bool force) {
lock();
for (int i=0; i<_VOLUMES; i++) {
if (_ffs[i] == this) {
_ffs[i] = 0;
f_mount(NULL, _fsid, 0);
if (_id != -1) {
unlock();
return -1;
}
for (int i = 0; i < _VOLUMES; i++) {
if (!_ffs[i]) {
_id = i;
_ffs[_id] = bd;
_fsid[0] = '0' + _id;
_fsid[1] = '\0';
debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
FRESULT res = f_mount(&_fs, _fsid, force);
unlock();
return res == 0 ? 0 : -1;
}
}
unlock();
return -1;
}
int FATFileSystem::unmount() {
lock();
if (_id == -1) {
unlock();
return -1;
}
FRESULT res = f_mount(NULL, _fsid, 0);
_ffs[_id] = NULL;
_id = -1;
unlock();
return res == 0 ? 0 : -1;
}
int FATFileSystem::sync() {
lock();
if (_id == -1) {
unlock();
return -1;
}
// Always synchronized
unlock();
return 0;
}
int FATFileSystem::format(BlockDevice *bd) {
FATFileSystem fs("");
int err = fs.mount(bd, false);
if (err) {
return -1;
}
// Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
fs.lock();
FRESULT res = f_mkfs(fs._fsid, 0, 512);
fs.unlock();
err = fs.unmount();
if (err) {
return -1;
}
return res == 0 ? 0 : -1;
}
FileHandle *FATFileSystem::open(const char* name, int flags) {
@ -176,13 +286,12 @@ FileHandle *FATFileSystem::open(const char* name, int flags) {
if (res) {
debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
unlock();
FATFileSystemSetErrno(res);
return NULL;
}
if (flags & O_APPEND) {
f_lseek(&fh, fh.fsize);
}
FATFileHandle * handle = new FATFileHandle(fh, _mutex);
FATFileHandle *handle = new FATFileHandle(fh, _ffs_mutex.get());
unlock();
return handle;
}
@ -211,18 +320,6 @@ int FATFileSystem::rename(const char *oldname, const char *newname) {
return 0;
}
int FATFileSystem::format() {
lock();
FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
if (res) {
debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
unlock();
return -1;
}
unlock();
return 0;
}
DirHandle *FATFileSystem::opendir(const char *name) {
lock();
FATFS_DIR dir;
@ -231,7 +328,7 @@ DirHandle *FATFileSystem::opendir(const char *name) {
unlock();
return NULL;
}
FATDirHandle *handle = new FATDirHandle(dir, _mutex);
FATDirHandle *handle = new FATDirHandle(dir, _ffs_mutex.get());
unlock();
return handle;
}
@ -269,32 +366,14 @@ int FATFileSystem::stat(const char *name, struct stat *st) {
(S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) :
(S_IRWXU | S_IRWXG | S_IRWXO);
#endif
unlock();
return 0;
}
int FATFileSystem::mount() {
lock();
FRESULT res = f_mount(&_fs, _fsid, 1);
unlock();
return res == 0 ? 0 : -1;
}
int FATFileSystem::unmount() {
lock();
if (disk_sync()) {
unlock();
return -1;
}
FRESULT res = f_mount(NULL, _fsid, 0);
unlock();
return res == 0 ? 0 : -1;
}
void FATFileSystem::lock() {
_mutex->lock();
_ffs_mutex->lock();
}
void FATFileSystem::unlock() {
_mutex->unlock();
_ffs_mutex->unlock();
}

View File

@ -23,6 +23,7 @@
#define MBED_FATFILESYSTEM_H
#include "FileSystemLike.h"
#include "BlockDevice.h"
#include "FileHandle.h"
#include "ff.h"
#include <stdint.h>
@ -35,13 +36,28 @@ using namespace mbed;
*/
class FATFileSystem : public FileSystemLike {
public:
FATFileSystem(const char* n);
FATFileSystem(const char* n, BlockDevice *bd = NULL);
virtual ~FATFileSystem();
/**
* Mounts the filesystem
*/
virtual int mount(BlockDevice *bd);
/**
* Unmounts the filesystem
*/
virtual int unmount();
static FATFileSystem * _ffs[_VOLUMES]; // FATFileSystem objects, as parallel to FatFs drives array
FATFS _fs; // Work area (file system object) for logical drive
char _fsid[2];
/**
* Flush any underlying transactions
*/
virtual int sync();
/**
* Formats a logical drive, FDISK partitioning rule, 512 bytes per cluster
*/
static int format(BlockDevice *bd);
/**
* Opens a file on the filesystem
@ -58,11 +74,6 @@ public:
*/
virtual int rename(const char *oldname, const char *newname);
/**
* Formats a logical drive, FDISK artitioning rule, 512 bytes per cluster
*/
virtual int format();
/**
* Opens a directory on the filesystem
*/
@ -78,32 +89,15 @@ public:
*/
virtual int stat(const char *name, struct stat *st);
/**
* Mounts the filesystem
*/
virtual int mount();
/**
* Unmounts the filesystem
*/
virtual int unmount();
virtual int disk_initialize() { return 0; }
virtual int disk_status() { return 0; }
virtual int disk_read(uint8_t *buffer, uint32_t sector, uint32_t count) = 0;
virtual int disk_write(const uint8_t *buffer, uint32_t sector, uint32_t count) = 0;
virtual int disk_sync() { return 0; }
virtual uint32_t disk_sectors() = 0;
protected:
FATFS _fs; // Work area (file system object) for logical drive
char _fsid[2];
int _id;
virtual int mount(BlockDevice *bd, bool force);
virtual void lock();
virtual void unlock();
private:
PlatformMutex *_mutex;
};
#endif