From cc74572c5be92e5fdc30aac4208f94067ad7d99c Mon Sep 17 00:00:00 2001 From: Ramesh Thomas Date: Mon, 25 Jul 2016 16:20:54 -0700 Subject: [PATCH] fs: Adds diskio interface Adds the diskio interface for the FAT file system. This revision uses RAM to emulate disk storage. Origin: Original Jira: ZEP-285 Change-Id: I7a30c8761d5ed9b564f1d1e08482c5ef199d7372 Signed-off-by: Ramesh Thomas --- Kconfig.zephyr | 2 ++ Makefile | 2 +- ext/fs/fat/Kbuild | 2 +- ext/fs/fat/zfs_diskio.c | 75 +++++++++++++++++++++++++++++++++++++++++ fs/Kconfig | 67 ++++++++++++++++++++++++++++++++++++ fs/Makefile | 1 + fs/fat_ram_diskio.c | 71 ++++++++++++++++++++++++++++++++++++++ include/fs/fat_diskio.h | 30 +++++++++++++++++ 8 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 ext/fs/fat/zfs_diskio.c create mode 100644 fs/Kconfig create mode 100644 fs/Makefile create mode 100644 fs/fat_ram_diskio.c create mode 100644 include/fs/fat_diskio.h diff --git a/Kconfig.zephyr b/Kconfig.zephyr index edfb44f809c..ca6d5c9497c 100644 --- a/Kconfig.zephyr +++ b/Kconfig.zephyr @@ -32,6 +32,8 @@ source "misc/Kconfig" source "lib/Kconfig" +source "fs/Kconfig" + source "ext/Kconfig" source "usb/Kconfig" diff --git a/Makefile b/Makefile index 9fb9045f0ca..bdd51fa7797 100644 --- a/Makefile +++ b/Makefile @@ -518,7 +518,7 @@ scripts: scripts_basic include/config/auto.conf include/config/tristate.conf # arch/ must be last here so that .gnu.linkonce magic for interrupts/exceptions # works as expected -core-y := lib/ kernel/ misc/ net/ boards/ ext/ usb/ arch/ +core-y := lib/ kernel/ misc/ net/ boards/ ext/ usb/ fs/ arch/ drivers-y := drivers/ ifneq ($(strip $(MAKEFILE_APP_DIR)),) diff --git a/ext/fs/fat/Kbuild b/ext/fs/fat/Kbuild index a1873faa96b..937b84efe41 100644 --- a/ext/fs/fat/Kbuild +++ b/ext/fs/fat/Kbuild @@ -1,2 +1,2 @@ obj-$(CONFIG_FAT_FILESYSTEM_ELM) += ff.o -obj-$(CONFIG_FAT_FILESYSTEM_ELM) += diskio.o +obj-$(CONFIG_FAT_FILESYSTEM_ELM) += zfs_diskio.o diff --git a/ext/fs/fat/zfs_diskio.c b/ext/fs/fat/zfs_diskio.c new file mode 100644 index 00000000000..734aef75590 --- /dev/null +++ b/ext/fs/fat/zfs_diskio.c @@ -0,0 +1,75 @@ +/*-----------------------------------------------------------------------*/ +/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */ +/*-----------------------------------------------------------------------*/ +/* 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. */ +/*-----------------------------------------------------------------------*/ +/*----------------------------------------------------------------------------/ +/ FatFs - Generic FAT file system module R0.12a / +/-----------------------------------------------------------------------------/ +/ +/ Copyright (C) 2016, ChaN, all right reserved. +/ +/ FatFs module is an open source software. Redistribution and use of FatFs in +/ source and binary forms, with or without modification, are permitted provided +/ that the following condition is met: + +/ 1. Redistributions of source code must retain the above copyright notice, +/ this condition and the following disclaimer. +/ +/ This software is provided by the copyright holder and contributors "AS IS" +/ and any warranties related to this software are DISCLAIMED. +/ The copyright owner or contributors be NOT LIABLE for any damages caused +/ by use of this software. +/----------------------------------------------------------------------------*/ + +#include /* FatFs lower layer API */ +#include + +/*-----------------------------------------------------------------------*/ +/* Get Drive Status */ +/*-----------------------------------------------------------------------*/ + +DSTATUS disk_status(uint8_t pdrv) +{ + return fat_disk_status(); +} + +/*-----------------------------------------------------------------------*/ +/* Inidialize a Drive */ +/*-----------------------------------------------------------------------*/ + +DSTATUS disk_initialize(uint8_t pdrv) +{ + return fat_disk_initialize(); +} + +/*-----------------------------------------------------------------------*/ +/* Read Sector(s) */ +/*-----------------------------------------------------------------------*/ + +DRESULT disk_read(uint8_t pdrv, uint8_t *buff, unsigned long sector, + uint32_t count) +{ + return fat_disk_read(buff, sector, count); +} + +/*-----------------------------------------------------------------------*/ +/* Write Sector(s) */ +/*-----------------------------------------------------------------------*/ +DRESULT disk_write(uint8_t pdrv, + const uint8_t *buff, unsigned long sector, uint32_t count) +{ + return fat_disk_write(buff, sector, count); +} + +/*-----------------------------------------------------------------------*/ +/* Miscellaneous Functions */ +/*-----------------------------------------------------------------------*/ + +DRESULT disk_ioctl(uint8_t pdrv, uint8_t cmd, void *buff) +{ + return fat_disk_ioctl(cmd, buff); +} diff --git a/fs/Kconfig b/fs/Kconfig new file mode 100644 index 00000000000..b958a930ca9 --- /dev/null +++ b/fs/Kconfig @@ -0,0 +1,67 @@ +# +# Copyright (c) 2016 Intel Corporation +# +# 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. +# + +menu "File System" + +config FILE_SYSTEM + bool "File system support" + default n + help + Enables support for file system. + +config FILE_SYSTEM_FAT + bool "FAT file system support" + default y + depends on FILE_SYSTEM + help + Enables FAT file system support. + +choice + prompt "Select file system storage device" + depends on FILE_SYSTEM_FAT + default FS_FAT_RAM_DISK + help + The storage device where the file system + resides + +config FS_FAT_RAM_DISK + bool + prompt "RAM Disk" + help + RAM buffer used to emulate storage disk. + This option can used to test the file + system. + +endchoice + +if FS_FAT_RAM_DISK + +config FS_VOLUME_SIZE + hex + default 0x18000 + help + This is the file system volume size in bytes. + +config FS_BLOCK_SIZE + hex + default 0x1000 + help + This is typically the minimum block size that + is erased at one time in flash storage. + +endif # FS_RAM_DISK + +endmenu diff --git a/fs/Makefile b/fs/Makefile new file mode 100644 index 00000000000..4e618c7c037 --- /dev/null +++ b/fs/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_FS_FAT_RAM_DISK) += fat_ram_diskio.o diff --git a/fs/fat_ram_diskio.c b/fs/fat_ram_diskio.c new file mode 100644 index 00000000000..565e3037883 --- /dev/null +++ b/fs/fat_ram_diskio.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2016 Intel Corporation. + * + * 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 +#include +#include +#include +#include + +static uint8_t file_buff[CONFIG_FS_VOLUME_SIZE]; + +static void *lba_to_address(uint32_t lba) +{ + __ASSERT(((lba * _MIN_SS) < CONFIG_FS_VOLUME_SIZE), "FS bound error"); + return &file_buff[(lba * _MIN_SS)]; +} + +DSTATUS fat_disk_status(void) +{ + return RES_OK; +} + +DSTATUS fat_disk_initialize(void) +{ + return RES_OK; +} + +DRESULT fat_disk_read(void *buff, uint32_t sector, uint32_t count) +{ + memcpy(buff, lba_to_address(sector), count * _MIN_SS); + + return RES_OK; +} + +DRESULT fat_disk_write(void *buff, uint32_t sector, uint32_t count) +{ + memcpy(lba_to_address(sector), buff, count * _MIN_SS); + + return RES_OK; +} + +DRESULT fat_disk_ioctl(uint8_t cmd, void *buff) +{ + switch (cmd) { + case CTRL_SYNC: + return RES_OK; + case GET_SECTOR_COUNT: + *(uint32_t *) buff = CONFIG_FS_VOLUME_SIZE / _MIN_SS; + return RES_OK; + case GET_BLOCK_SIZE: + *(uint32_t *) buff = CONFIG_FS_BLOCK_SIZE / _MIN_SS; + return RES_OK; + case CTRL_TRIM: + break; + } + + return RES_PARERR; +} diff --git a/include/fs/fat_diskio.h b/include/fs/fat_diskio.h new file mode 100644 index 00000000000..378bafd79dc --- /dev/null +++ b/include/fs/fat_diskio.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Intel Corporation. + * + * 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. + */ + +#ifndef _FAT_DISKIO_H_ +#define _FAT_DISKIO_H_ + +#include +#include + +DSTATUS fat_disk_status(void); +DSTATUS fat_disk_initialize(void); +DRESULT fat_disk_read(uint8_t *buff, unsigned long sector, uint32_t count); +DRESULT fat_disk_write(const uint8_t *buff, unsigned long sector, + uint32_t count); +DRESULT fat_disk_ioctl(uint8_t cmd, void *buff); + +#endif /* _FAT_DISKIO_H_ */