add scard libs

This commit is contained in:
optixx 2009-08-11 11:47:10 +02:00
parent 07086b2a3c
commit b47af2c376
139 changed files with 31430 additions and 0 deletions

View File

@ -0,0 +1,39 @@
CHANGEFILE
----------
This is the Changefile for the development 0.3 branch of EFSL.
Recording began with EFSL-0.3.3
0.3.5
-----
* Added warning in documentation that it is outdated
* Changed structure definitions
* Implemnted full-feature cp
* Renamed some efsl-functions (all starting with EFSL_)
* Added another example for AVR.
* Updated docs on getting started on AVR.
0.3.4
-----
* Fixed avr support
* Created new avr example + makefile
* Some more work on new fsutils
0.3.3
-----
* Renamed src/core to src/base
* Implemented new hwInterface structure
Support for multiple hwEndpoints in one project
* Modified SD_SPI to work as a general protocol
* Modified Linuxfile to the new hwInterface model
* Created a new efs_configger, now supports every
combination of partitions/disc
* Implemented full support for little and big endian
machines, as well as for little and big endian
filesystems
* Created new build system, for multiple platforms,
configurable from one file
* Changes cpo to use the new library functions
* Broke both dsp & atmega support

31
tools/efsl-0.3.6/Makefile Normal file
View File

@ -0,0 +1,31 @@
include conf/config.makefile
linux: efsl-base efsl-fs-vfat efsl-hwd-linuxfile
avr: efsl-base efsl-fs-vfat efsl-prot-sdspi efsl-hwd-atmega_sd
efsl-base:
make -C src/base/
cp src/base/efsl-base.a lib/libefsl-base.a
efsl-fs-vfat:
make -C src/fs/vfat/
cp src/fs/vfat/efsl-fs-vfat.a lib/libefsl-fs-vfat.a
efsl-prot-sdspi:
make -C src/protocols/sdcard_spi/
cp src/protocols/sdcard_spi/efsl-prot-sdspi.a lib/libefsl-prot-sdspi.a
efsl-hwd-linuxfile:
make -C src/hwdrivers/linuxfile/
cp src/hwdrivers/linuxfile/efsl-hwd-linuxfile.a lib/libefsl-hwd-linuxfile.a
efsl-hwd-atmega_sd:
make -C src/hwdrivers/atmega_spi/
cp src/hwdrivers/atmega_spi/efsl-hwd-atmega_spi.a lib/libefsl-hwd-atmega_spi.a
clean:
make -C src/base/ clean
make -C src/fs/vfat/ clean
make -C src/hwdrivers/linuxfile/ clean
make -C src/hwdrivers/atmega_spi clean
rm -rf lib/*.a

View File

@ -0,0 +1,174 @@
#ifndef __EFSL_CONFIG_H__
#define __EFSL_CONFIG_H__
/* Hardware target
---------------
* Here you will define for what hardware-endpoint EFSL should be compiled.
* Look in interfaces.h to see what systems are supported, and add your own
* there if you need to write your own driver. Then, define the name you
* selected for your hardware there here. Make sure that you only select one
* device!
*/
/*#define HW_ENDPOINT_LINUX*/
#define HW_ENDPOINT_ATMEGA128_SD
/*#define HW_ENDPOINT_DSP_TI6713_SD*/
#define MULTIPLE_INTERFACE_SUPPORT
/* Architecture
------------
* In this section you should configure how large the default variable
* types in your system are. This is controlled in types.h in the general
* include directory. The selection you make here defines to what the various
* e(s|u)int(8,16,32) types will map.
* For 32 bit Linux : VARSIZE_LINUX32
* For 64 bit Linux : VARSIZE_LINUX64
* For AVR's : VARSIZE_ATMEGA
* For TMS67XX : VARSIZE_TMS67XX
*/
#define VARSIZE_ATMEGA
/* Memory configuration
--------------------
* Here you must configure wheter your processor can access memory byte
* oriented. All x86 processors can do it, AVR's can do it to. Some DSP
* or other microcontrollers can't. If you have an 8 bit system you're safe.
* If you are really unsure, leave the setting commented out, it will be slower
* but it will work for sure.
*/
#define BYTE_ALIGNMENT
/* Cache configuration
-------------------
* Here you must configure how much memory of cache you can/want to use.
* The number you put at IOMAN_NUMBUFFER is multiplied by 512. So 1 means
* 512 bytes cache, 4 means 2048 bytes cache. More is better.
* The number after IOMAN_NUMITERATIONS should be untouched.
* The last field (IOMAN_DO_MEMALLOC) is to tell ioman to allocate it's
* own memory in it's structure, or not. If you choose to do it yourself
* you will have to pass a pointer to the memory as the last argument of
* ioman_init.
*/
#define IOMAN_NUMBUFFER 1
#define IOMAN_NUMITERATIONS 3
#define IOMAN_DO_MEMALLOC
/* Cluster pre-allocation
----------------------
* When writing files, the function that performs the actual write has to
* calculate how many clusters it will need for that request. It then allocates
* that number of new clusters to the file. Since this involves some
* calculations and writing of the FAT, you might find it beneficial to limit
* the number of allocations, and allow fwrite to pre-allocate a number of
* clusters extra. This setting determines how many clusters will be extra
* allocated whenever this is required.
* Take in carefull consideration how large your clustersize is, putting 10 here
* with a clustersize of 32kb means you might waste 320 kb.
* The first option is for preallocating files, the other is used when enlarging
* a directory to accomodate more files
*/
#define CLUSTER_PREALLOC_FILE 0
#define CLUSTER_PREALLOC_DIRECTORY 0
/* Endianess configuration
-----------------------
* Here you can configure wheter your architecture is little or big endian. This
* is important since all FAT structures are stored in intel little endian
* order. So if you have a big endian system the library has to convert all
* figures to big endian in order to work.
*/
#define HOST_LITTLE_ENDIAN
/* Date and Time support
---------------------
* Here you can enable or disable date and time support. If you enable
* it you will have to create 6 functions, that are described in the
* EFSL manual. If the functions are not present when linking your
* program with the library you will get unresolved dependencies.
*/
/* #define DATE_TIME_SUPPORT */
/* Error reporting support
-----------------------
* When you receive an error in userland, it usually only gives limited
* information (most likely, fail or success). If error detection and
* reporting is important for you, you can enable more detailed error
* reporting here. This is optional, the costs are 1 byte per object,
* and a small increase in code size.
* You can enable error recording for all object, or you can select the
* object manually.
* For full error reporting use FULL_ERROR_SUPPORT
* For only the base-core of the library use BASE_ERROR_SUPPORT
* For IO/Man use ERRSUP_IOMAN
* For Disc use ERRSUP_IOMAN
* For Part use ERRSUP_PARTITION
* For Fs use ERRSUP_FILESYSTEM
* For File use ERRSUP_FILE
*/
#define FULL_ERROR_SUPPORT
/*#define BASE_ERROR_SUPPORT*/
/* List options
------------
* In this section you can configure what kind of data you will get from
* directory listing requests. Please refer to the documentation for
* more information
*/
#define LIST_MAXLENFILENAME 12
/* Debugging configuration
-----------------------
* Here you can configure the debugging behaviour. Debugging is different
* on every platform (see debug.h for more information).
* If your hardware has no means of output (printf) dont define any anything,
* and nothing will happen. For real world use debugging should be turned off.
*/
#define DEBUG
/* Debugging configuration - AVR Specific: PORT
--------------------------------------------
* Here you can select which UART you want to use for debugging.
* If you did not define DEBUG, this setting has no effect.
* Note that it is not a good idea to use a port that you use in userspace.
*/
/*#define DEBUG_PORT 0*/ /* Select UART0 */
#define DEBUG_PORT 1 /* Select UART1 */
/* Debugging configuration - AVR Specific: UBRR
--------------------------------------------
* Here you can set UBRR, this value will select the serial clock speed.
* This value depends on your baudrate and clockrate. U2X is by standard 0,
* if you would want this 1 for some reason, this can be done in debug.c.
*/
/*#define DEBUG_UBRR 51*/ /* 9600bps on 8Mhz */
#define DEBUG_UBRR 95 /* 9600bps on 14.7456Mhz */
/*#define DEBUG_UBRR 103*/ /* 9600bps on 16Mhz */
#endif

View File

@ -0,0 +1,46 @@
################################################################################
### EFSL - Embedded Filesystems Library ###
### ----------------------------------- ###
### ###
################################################################################
# This is the configuration file for EFSL. This file will enable your to build
# the library if you have GNU make, or compatible, on your system.
# If you do not have a make utility on your system, or it cannot be used in this
# fashion (when using IDE's, like MSVC or Code composer), please refer to the
# documentation on how to build EFSL. It is possible to build EFSL with any C
# compiler although it will be a bit more work.
# C compiler
# ----------
#
# Here you select with what binary the sourcefiles must be compiled
CC=avr-gcc
# AR archiver
# -----------
#
# This variable controls what archiver is to be used. This utility is optional,
# if you don't have GNU make, you probably need to link differently as well.
AR=avr-ar
# Objcopy
# --------
#
# This variable controls what objcopy is to be used. This utility will be used
# when the program is converted to an format that your uC understands.
OBJCOPY=avr-objcopy
# C compiler options
# ------------------
#
# Here you can configure several options about the compilation.
DEBUGGING=-g3
VERIFY=-Wall
ARCHITECTURE=-mmcu=atmega128
OPTIMISE=-Os
GCFLAGS=$(DEBUGGING) $(VERIFY) $(ARCHITECTURE) $(OPTIMISE)

View File

@ -0,0 +1,151 @@
#ifndef __EFSL_CONFIG_H__
#define __EFSL_CONFIG_H__
/* Hardware target
---------------
* Here you will define for what hardware-endpoint EFSL should be compiled.
* Look in interfaces.h to see what systems are supported, and add your own
* there if you need to write your own driver. Then, define the name you
* selected for your hardware there here. Make sure that you only select one
* device!
*/
/*#define HW_ENDPOINT_LINUX*/
/*#define HW_ENDPOINT_ATMEGA128_SD*/
/*#define HW_ENDPOINT_DSP_TI6713_SD*/
#define MULTIPLE_INTERFACE_SUPPORT
/*#define HWIFUNC_INIT(x) lf_init(x)
#define HWIFUNC_READ(x,y,z) lf_readBuf(x,y,z)
#define HWIFUNC_WRITE(x,y,z) lf_writeBuf(x,y,z)
#define HWIFUNC_HEADER interfaces/linuxfile.h */
/* Architecture
------------
* In this section you should configure how large the default variable
* types in your system are. This is controlled in types.h in the general
* include directory. The selection you make here defines to what the various
* e(s|u)int(8,16,32) types will map.
* For 32 bit Linux : VARSIZE_LINUX32
* For 64 bit Linux : VARSIZE_LINUX64
* For AVR's : VARSIZE_ATMEGA
* For TMS67XX : VARSIZE_TMS67XX
*/
#define VARSIZE_LINUX32
/* Memory configuration
--------------------
* Here you must configure wheter your processor can access memory byte
* oriented. All x86 processors can do it, AVR's can do it to. Some DSP
* or other microcontrollers can't. If you have an 8 bit system you're safe.
* If you are really unsure, leave the setting commented out, it will be slower
* but it will work for sure.
*/
#define BYTE_ALIGNMENT
/* Cache configuration
-------------------
* Here you must configure how much memory of cache you can/want to use.
* The number you put at IOMAN_NUMBUFFER is multiplied by 512. So 1 means
* 512 bytes cache, 4 means 2048 bytes cache. More is better.
* The number after IOMAN_NUMITERATIONS should be untouched.
* The last field (IOMAN_DO_MEMALLOC) is to tell ioman to allocate it's
* own memory in it's structure, or not. If you choose to do it yourself
* you will have to pass a pointer to the memory as the last argument of
* ioman_init.
*/
#define IOMAN_NUMBUFFER 10
#define IOMAN_NUMITERATIONS 3
#define IOMAN_DO_MEMALLOC
/* Cluster pre-allocation
----------------------
* When writing files, the function that performs the actual write has to
* calculate how many clusters it will need for that request. It then allocates
* that number of new clusters to the file. Since this involves some
* calculations and writing of the FAT, you might find it beneficial to limit
* the number of allocations, and allow fwrite to pre-allocate a number of
* clusters extra. This setting determines how many clusters will be extra
* allocated whenever this is required.
* Take in carefull consideration how large your clustersize is, putting 10 here
* with a clustersize of 32kb means you might waste 320 kb.
* The first option is for preallocating files, the other is used when enlarging
* a directory to accomodate more files
*/
#define CLUSTER_PREALLOC_FILE 5
#define CLUSTER_PREALLOC_DIRECTORY 2
/* Endianess configuration
-----------------------
* Here you can configure wheter your architecture is little or big endian. This
* is important since all FAT structures are stored in intel little endian
* order. So if you have a big endian system the library has to convert all
* figures to big endian in order to work.
*/
/*#define HOST_BIG_ENDIAN*/
#define HOST_LITTLE_ENDIAN
/* Date and Time support
---------------------
* Here you can enable or disable date and time support. If you enable
* it you will have to create 6 functions, that are described in the
* EFSL manual. If the functions are not present when linking your
* program with the library you will get unresolved dependencies.
*/
/* #define DATE_TIME_SUPPORT */
/* Error reporting support
-----------------------
* When you receive an error in userland, it usually only gives limited
* information (most likely, fail or success). If error detection and
* reporting is important for you, you can enable more detailed error
* reporting here. This is optional, the costs are 1 byte per object,
* and a small increase in code size.
* You can enable error recording for all object, or you can select the
* object manually.
* For full error reporting use FULL_ERROR_SUPPORT
* For only the base-core of the library use BASE_ERROR_SUPPORT
* For IO/Man use ERRSUP_IOMAN
* For Disc use ERRSUP_IOMAN
* For Part use ERRSUP_PARTITION
* For Fs use ERRSUP_FILESYSTEM
* For File use ERRSUP_FILE
*/
#define FULL_ERROR_SUPPORT
/*#define BASE_ERROR_SUPPORT*/
/* Debugging configuration
-----------------------
* Here you can configure the debugging behaviour. Debugging is different
* on every platform (see debug.h for more information).
* If your hardware has no means of output (printf) dont define any anything,
* and nothing will happen. For real world use debugging should be turned off.
*/
/*#define DEBUG*/
/*#define DO_FUNC_DEBUG*/
/* List options
------------
* In this section you can configure what kind of data you will get from
* directory listing requests. Please refer to the documentation for
* more information
*/
#define LIST_MAXLENFILENAME 12
#endif

View File

@ -0,0 +1,38 @@
################################################################################
### EFSL - Embedded Filesystems Library ###
### ----------------------------------- ###
### ###
################################################################################
# This is the configuration file for EFSL. This file will enable your to build
# the library if you have GNU make, or compatible, on your system.
# If you do not have a make utility on your system, or it cannot be used in this
# fashion (when using IDE's, like MSVC or Code composer), please refer to the
# documentation on how to build EFSL. It is possible to build EFSL with any C
# compiler although it will be a bit more work.
# C compiler
# ----------
#
# Here you select with what binary the sourcefiles must be compiled
CC=gcc
# AR archiver
# -----------
#
# This variable controls what archiver is to be used. This utility is optional,
# if you don't have GNU make, you probably need to link differently as well.
AR=ar
# C compiler options
# ------------------
#
# Here you can configure several options about the compilation.
DEBUGGING=-g3
VERIFY=-Wall -pedantic -ansi
ARCHITECTURE=-march=i386
OPTIMISE=-O0
GCFLAGS=$(DEBUGGING) $(VERIFY) $(ARCHITECTURE) $(OPTIMISE)

View File

@ -0,0 +1,151 @@
#ifndef __EFSL_CONFIG_H__
#define __EFSL_CONFIG_H__
/* Hardware target
---------------
* Here you will define for what hardware-endpoint EFSL should be compiled.
* Look in interfaces.h to see what systems are supported, and add your own
* there if you need to write your own driver. Then, define the name you
* selected for your hardware there here. Make sure that you only select one
* device!
*/
/*#define HW_ENDPOINT_LINUX*/
/*#define HW_ENDPOINT_ATMEGA128_SD*/
/*#define HW_ENDPOINT_DSP_TI6713_SD*/
#define MULTIPLE_INTERFACE_SUPPORT
/*#define HWIFUNC_INIT(x) lf_init(x)
#define HWIFUNC_READ(x,y,z) lf_readBuf(x,y,z)
#define HWIFUNC_WRITE(x,y,z) lf_writeBuf(x,y,z)
#define HWIFUNC_HEADER interfaces/linuxfile.h */
/* Architecture
------------
* In this section you should configure how large the default variable
* types in your system are. This is controlled in types.h in the general
* include directory. The selection you make here defines to what the various
* e(s|u)int(8,16,32) types will map.
* For 32 bit Linux : VARSIZE_LINUX32
* For 64 bit Linux : VARSIZE_LINUX64
* For AVR's : VARSIZE_ATMEGA
* For TMS67XX : VARSIZE_TMS67XX
*/
#define VARSIZE_LINUX64
/* Memory configuration
--------------------
* Here you must configure wheter your processor can access memory byte
* oriented. All x86 processors can do it, AVR's can do it to. Some DSP
* or other microcontrollers can't. If you have an 8 bit system you're safe.
* If you are really unsure, leave the setting commented out, it will be slower
* but it will work for sure.
*/
#define BYTE_ALIGNMENT
/* Cache configuration
-------------------
* Here you must configure how much memory of cache you can/want to use.
* The number you put at IOMAN_NUMBUFFER is multiplied by 512. So 1 means
* 512 bytes cache, 4 means 2048 bytes cache. More is better.
* The number after IOMAN_NUMITERATIONS should be untouched.
* The last field (IOMAN_DO_MEMALLOC) is to tell ioman to allocate it's
* own memory in it's structure, or not. If you choose to do it yourself
* you will have to pass a pointer to the memory as the last argument of
* ioman_init.
*/
#define IOMAN_NUMBUFFER 10
#define IOMAN_NUMITERATIONS 3
#define IOMAN_DO_MEMALLOC
/* Cluster pre-allocation
----------------------
* When writing files, the function that performs the actual write has to
* calculate how many clusters it will need for that request. It then allocates
* that number of new clusters to the file. Since this involves some
* calculations and writing of the FAT, you might find it beneficial to limit
* the number of allocations, and allow fwrite to pre-allocate a number of
* clusters extra. This setting determines how many clusters will be extra
* allocated whenever this is required.
* Take in carefull consideration how large your clustersize is, putting 10 here
* with a clustersize of 32kb means you might waste 320 kb.
* The first option is for preallocating files, the other is used when enlarging
* a directory to accomodate more files
*/
#define CLUSTER_PREALLOC_FILE 5
#define CLUSTER_PREALLOC_DIRECTORY 2
/* Endianess configuration
-----------------------
* Here you can configure wheter your architecture is little or big endian. This
* is important since all FAT structures are stored in intel little endian
* order. So if you have a big endian system the library has to convert all
* figures to big endian in order to work.
*/
/*#define HOST_BIG_ENDIAN*/
#define HOST_LITTLE_ENDIAN
/* Date and Time support
---------------------
* Here you can enable or disable date and time support. If you enable
* it you will have to create 6 functions, that are described in the
* EFSL manual. If the functions are not present when linking your
* program with the library you will get unresolved dependencies.
*/
/* #define DATE_TIME_SUPPORT */
/* Error reporting support
-----------------------
* When you receive an error in userland, it usually only gives limited
* information (most likely, fail or success). If error detection and
* reporting is important for you, you can enable more detailed error
* reporting here. This is optional, the costs are 1 byte per object,
* and a small increase in code size.
* You can enable error recording for all object, or you can select the
* object manually.
* For full error reporting use FULL_ERROR_SUPPORT
* For only the base-core of the library use BASE_ERROR_SUPPORT
* For IO/Man use ERRSUP_IOMAN
* For Disc use ERRSUP_IOMAN
* For Part use ERRSUP_PARTITION
* For Fs use ERRSUP_FILESYSTEM
* For File use ERRSUP_FILE
*/
#define FULL_ERROR_SUPPORT
/*#define BASE_ERROR_SUPPORT*/
/* Debugging configuration
-----------------------
* Here you can configure the debugging behaviour. Debugging is different
* on every platform (see debug.h for more information).
* If your hardware has no means of output (printf) dont define any anything,
* and nothing will happen. For real world use debugging should be turned off.
*/
/*#define DEBUG*/
/*#define DO_FUNC_DEBUG*/
/* List options
------------
* In this section you can configure what kind of data you will get from
* directory listing requests. Please refer to the documentation for
* more information
*/
#define LIST_MAXLENFILENAME 12
#endif

View File

@ -0,0 +1,38 @@
################################################################################
### EFSL - Embedded Filesystems Library ###
### ----------------------------------- ###
### ###
################################################################################
# This is the configuration file for EFSL. This file will enable your to build
# the library if you have GNU make, or compatible, on your system.
# If you do not have a make utility on your system, or it cannot be used in this
# fashion (when using IDE's, like MSVC or Code composer), please refer to the
# documentation on how to build EFSL. It is possible to build EFSL with any C
# compiler although it will be a bit more work.
# C compiler
# ----------
#
# Here you select with what binary the sourcefiles must be compiled
CC=gcc
# AR archiver
# -----------
#
# This variable controls what archiver is to be used. This utility is optional,
# if you don't have GNU make, you probably need to link differently as well.
AR=ar
# C compiler options
# ------------------
#
# Here you can configure several options about the compilation.
DEBUGGING=-g3
VERIFY=-Wall -pedantic -ansi
ARCHITECTURE=-march=k8
OPTIMISE=-O0
GCFLAGS=$(DEBUGGING) $(VERIFY) $(ARCHITECTURE) $(OPTIMISE)

View File

@ -0,0 +1 @@
config-linux.h

View File

@ -0,0 +1 @@
config-linux.makefile

View File

@ -0,0 +1,13 @@
all: manual.tex
latex manual.tex
latex manual.tex # Needs to be done a second time to make sure that the contents table is correct
dvips -o manual.ps manual.dvi
dvipdfm manual.dvi
clean:
rm -f manual.aux
rm -f manual.dvi
rm -f manual.log
rm -f manual.pdf
rm -f manual.ps
rm -f manual.toc

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,117 @@
\documentclass[a4paper,fleqn]{article}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{color}
\usepackage{array}
\usepackage{verbatim}
\usepackage{longtable}
\newcommand{\filename}[1]{
\textsf{#1}
}
\newcommand{\code}[1]{
\texttt{#1}
}
\newcommand{\external}[1]{
\textbf{#1}
}
\newcommand{\thead}[1]{
\textbf{#1}
}
%\usepackage[latin1]{inputenc}
%\usepackage[T1]{fontenc}
\lstset{language=C}
\begin{document}
\title{\Huge{EFSL}\\\Large{Embedded Filesystems Library - 0.3}}
\author{Lennart Yseboodt\\Michael De Nil}
\date{$\copyright$ 2005}
\maketitle
\newpage
\tableofcontents
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex}
\newpage
\section{Document Outdated!}
{\Huge{
This document is outdated and is in the progress of being renewed.\\
\newline\newline
If you are just starting with Efsl, we recommend you to start with the stable
0.2-branch. This version is currently not really usable, and is intended for people
working on the code.
}}
\newpage
\section{Preface}
\input{pages/preface}
\newpage
\section{Getting started}
\subsection{On Linux (file) (0.2)}
\input{pages/linux}
\newpage
\subsection{On AVR (SD-Card) (0.3)}
\input{pages/avr}
\newpage
\subsection{On DSP (SD-Card) (0.2)}
\input{pages/tms6713}
\newpage
\section{Configuring EFSL (0.2)}
\input{pages/config}
\newpage
\section{EFSL Functions}
\subsection{Date and time support (0.2)}
\input{pages/dateandtime}
\newpage
\subsection{efs\_init (0.2)}
\input{pages/efs_init}
\newpage
\subsection{file\_fopen (0.2)}
\input{pages/file_fopen}
\newpage
\subsection{file\_fclose (0.2)}
\input{pages/file_fclose}
\newpage
\subsection{file\_read (0.2)}
\input{pages/file_read}
\newpage
\subsection{file\_write (0.2)}
\input{pages/file_write}
\newpage
\subsection{mkdir (0.2)}
\input{pages/mkdir}
\newpage
\subsection{ls\_openDir (0.2)}
\input{pages/lsopendir}
\newpage
\subsection{ls\_getNext (0.2)}
\input{pages/lsgetnext}
\newpage
\newpage
\section{Developer notes}
\subsection{Integer types (0.2)}
\input{pages/types}
\subsection{Debugging (0.2)}
\input{pages/debug}
\subsection{Adding support for a new endpoint (0.2)}
\input{pages/driver}
\subsection{I/O Manager (0.2)}
\input{pages/ioman}
\subsection{C library for EFSL (0.2)}
\input{pages/plibc}
\newpage
\section{Legal notes}
\input{pages/license}
\end{document}

View File

@ -0,0 +1,223 @@
This section describes how to implement Efsl on a AVR $\mu C$ connected to
an SD-Card (SPI). For getting efsl to compile, the avr-gcc compiler and
avr-libc library are required. On Windows you should install WinAVR
(http://winavr.sourceforge.net/), on Linux you can install the packages
separately (see http://www.nongnu.org/avr-libc/user-manual/install\_tools.html
for a nice howto).
\subsubsection{Hardware}
First, you need set up a prototype in which you connect the CD, CMD, DAT0
\& CLK lines from the SD-Card to /CS, MOSI, MISO \& SCK from the Atmega.
\newline
\includegraphics[scale=0.65]{pics/sdcard.eps}
\newline
%\parbox[c]{.4\textwidth}{\begin{center}\includegraphics[width=.4\textwidth]{pics/sdconnection}\end{center}}
\parbox[c]{.5\textwidth}{
Connect the following lines on the SD-card:
\begin{itemize}
\item{Pin 9 (DAT2) - NC\\(or pull-up to 3.3V)}
\item{Pin 1 (CD) - Any pin on the Atmega128}
\item{Pin 2 (CMD) - MOSI\\(pin 12 on the Atmega128)}
\item{Pin 3 (Vss) - GND}
\item{Pin 4 (Vdd) - +3.3V}
\item{Pin 5 (CLK) - SCK\\(pin 11 on the Atmega128)}
\item{Pin 6 (Vss) - GND}
\item{Pin 7 (DAT0) - MISO\\(pin 12 on the Atmega128)}
\item{Pin 8 (DAT1) - NC\\(or pull-up to 3.3V)}
\end{itemize}
}
\parbox[c]{.5\textwidth}{\begin{center}
\includegraphics[width=.5\textwidth]{pics/sdconnection}
\newline\newline
Remark: this schematic includes pull-up's to 3.3V, which
can be left off.
\end{center}}
\newline
Remark 1: Make sure that your $\mu C$ is running on 3,3V, so you don't
damage your SD-Card.\newline
\newline
Remark 2: CD is currently static set to PB0, but will become variable
in future releases.
\subsubsection{Download \& Compile}
Let's get started:
\begin{enumerate}
\item{Get the latest release of efsl on http://www.sf.net/projects/efsl/}
\item{Unpack the library (on Windows, you can use WinACE or WinRAR)}
\item{Copy in directory \filename{conf} the file
\filename{config-avr.h} to \filename{config.h}}
\item{Copy in directory \filename{conf} the file
\filename{config-avr.makefile} to \filename{config.makefile}}
\item{Compile the library (\code{make avr})}
\end{enumerate}
Now you should have the following files in a directory called {lib}:
\begin{itemize}
\item{\filename{libefsl-base.a}}
\item{\filename{libefsl-fs-vfat.a}}
\item{\filename{libefsl-prot-sdspi.a}}
\item{\filename{libefsl-hwd-atmega\_spi.a}}
\end{itemize}
\subsubsection{Example}
Since Efsl itself is only a library, it's not supposed to do anything out of
the box, than just compile. To get started, we'll show here a small example
program that opens an existing file and writes the content to a new file.
\newline\newline
First, create a new directory in which you put the compiled efsl-library
(\filename{libefsl.a}) and create a new file called \filename{avrtest.c} containing:
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include <efs.h>
#include <sd.h>
#include <atmega_spi.h>
void hang(void);
void main(void)
{
efsl_storage_conf storage_conf;
efsl_fs_conf fs_conf;
efsl_storage storage;
efsl_fs fs;
File file_r;
File file_w;
atmegaSpiInterface spi_interface;
SdSpiProtocol sd_protocol;
char buf[512];
unsigned short e;
/* Init */
spi_interface.pinSelect=0x01;
sd_protocol.spiHwInterface=&spi_interface;
sd_protocol.spiHwInit=(void *)atmega_spi_init;
sd_protocol.spiSendByte=(void *)atmega_spi_send;
storage_conf.hwObject=&sd_protocol;
storage_conf.if_init_fptr=(void *)sd_Init;
storage_conf.if_read_fptr=(void *)sd_readSector;
storage_conf.if_write_fptr=(void *)sd_writeSector;
storage_conf.if_ioctl_fptr=(void *)sd_ioctl;
storage_conf.ioman_bufmem=0;
fs_conf.no_partitions=0;
fs_conf.storage=&storage;
if(efsl_initStorage(&storage,&storage_conf)){
hang();
}
if(efsl_initFs(&fs,&fs_conf)){
hang();
}
if(file_fopen(&file_r,&fs.filesystem,"orig.txt",'r')!=0){
hang();
}
if(file_fopen(&file_w,&fs.filesystem,"copy.txt",'w')!=0){
hang();
}
if(file_fopen(&file_r,&efs.myFs,"orig.txt",'r')!=0){
hang();
}
while((e=file_read(&file_r,512,buf))){
file_write(&file_w,e,buf);
}
file_fclose(&file_r);
file_fclose(&file_w);
fs_umount(&fs.filesystem);
hang();
}
void hang(void)
{
while((1))
_NOP();
}
\end{lstlisting}
$ $\newline
Some extra information on the code above: TODO
%\begin{itemize}
% \item{Line 1: The header file for efsl is included here. When using the
% basic efsl functions, \filename{efs.h} is the only header file on the
% efsl library that needs to be included.}
% \item{Line 7: The object efs is created, this object will contain
% information about the hardware layer, the partition table and
% the disc.}
% \item{Line 8: The objects \code{file\_r} and \code{file\_w} are created, these objects
% will contain information about the files that we will open on the
% efs-object.}
% \item{Line 9: A buffer of 512 bytes is allocated. This buffer will be
% used for reading and writing blocks of data.}
% \item{Line 12: Call of \code{efs\_init()}, which will initialize the efs-object.
% To this function we pass:
% \begin{enumerate}
% \item{A pointer to the efs-object.}
% \item{A pointer to the file that contains the partition table /
% file system (in this example, we select a device as file).}
% \end{enumerate}
% If this function returns 0, it means that a valid fat partition is
% found on the SD-card connected.
% If no valid fat-filesystem is found, or the file does not exist, the
% function returns a negative value. In this example we then go to an
% infinite loop to prevent the program to continue.}
% \item{Line 16 \& 20: Call of \code{file\_fopen()}, which will initialize the
% file-objects. To this function we pass:
% \begin{enumerate}
% \item{A pointer to the file-object.}
% \item{A pointer to the filesystem-object.}
% \item{A pointer to the filename.}
% \item{A char containing the the mode (read, write, append).}
% \end{enumerate}
% If this function returns 0, it means the file has successfully been
% opened for reading / writing / appending.
% If the file could not be opened (because for example a file already
% exists), a negative value is returned.}
% \item{Line 24: Call of \code{file\_read()}, which will read a given value of
% bytes (in this example 512) from a file and put it's content into
% the buffer passed (in this example called buf). This function returns
% the amount of bytes read, so the while-loop will be executed as long
% as there are bytes left in the file.}
% \item{Line 25: Call of \code{file\_write()}, which will write a given value
% of bytes (in this example, the amount of bytes that was read
% by \code{file\_read()}) from the buffer passed to a file. This function returns
% the amount of bytes written.}
% \item{Line 28 \& 29: Call of \code{file\_fclose()}, which will close the
% file-objects.}
% \item{Line 31: Call of \code{fs\_umount()}, which will write all buffers to
% the the SD-card.}
%\end{itemize}
\subsubsection{Testing}
So now let's test the program:
\begin{enumerate}
\item
{ Compile the program:
\begin{itemize}
\item{On Linux (with avr-gcc): avr-gcc -I/home/user/src/base/include -I/home/user/src/include -I/home/user/src/fs/vfat/include -I/home/user/src/hwdrivers/atmega\_spi/include -I/home/user/src/protocols/sdcard\_spi/include -I/home/user/conf -ffreestanding -mmcu=atmega128 -Os -o avrtest.o avrtest.c -L/home/user/lib -lefsl-base -lefsl-fs-vfat -lefsl-hwd-atmega\_spi -lefsl-prot-sdspi}
\item{On Windows (with WinAVR): replace all slashes with backslashes}
\end{itemize}
}
\item{Generate a hexfile
(avr-objcopy -j .text -j .data -O ihex avrtest.o avrtest.hex)}
\item{Connect an SD-card to your Atmega128 with a file called
\filename{orig.txt} on it.}
\item
{
Flash the hex file into your $\mu C$.
\begin{itemize}
\item{On Linux: avrdude -P /dev/ttyUSB0 -c stk500 -p m128 -Uflash:w:avrtest.hex}
\item{On Windows: use Atmel AVR-Studio}
\end{itemize}
}
\item{Reset your $\mu C$ and wait some time (depending on how big
the file \filename{orig.txt} is).}
\item{Disconnect the SD-card, so you can put it in your card reader
and find out if the file \filename{orig.txt} is copied to
\filename{copy.txt}.}
\end{enumerate}

View File

@ -0,0 +1,180 @@
In this section we're going to talk about the configuration file (\filename{config.h}),
that defines the behavior of the library. In the configuration files there are many
settings, most of which default to safe or 'standard' compliant settings.
For every platform we try to deliver a sample configuration, with setting tweaked for
that architecture. This documentation only refers to the general elements which are
tied to the library rather that the target hardware.
\subsection{Hardware target}
Here you will define what kind of hardware you will be using. Please refer to
section \ref{hwdriver} to learn how to write a hardware endpoint.
Here you must \code{\#define} the name of your hardware endpoint.
The following list contains the endpoints that the library ships with.\\
\begin{tabular}{|l|p{8cm}|}
\hline
\code{HW\_ENDPOINT\_LINUX}& This endpoint uses a regular file as
a "disc" containing a filesystem. This is a great endpoint for
testing and debugging. All development is done using this emulation.\\
\code{HW\_ENDPOINT\_ATMEGA128\_SD}& This endpoint is for the Atmel ATMega 128
with an SD card attached to the SPI pins of the device. Several settings
that are specific for this endpoint can be found in the AVR sample
configuration. A Makefile is also provided for compiling the EFSL library
using avr-gcc.\\
\code{HW\_ENDPOINT\_DSP\_TI6713\_SD}& This endpoint is for a TI DSP, it should
work with any McBSP port, due to the infinite amount of options, you should
refer to the source code of this endpoint for fine tuning, or selecting what
port to use (defaults to McBSP0).\\
\hline
\end{tabular}
\subsection{Memory configuration}
This section only has one option, called \code{BYTE\_ALIGNMENT}. If you define
this keyword the library will assume that your CPU is capable of accessing the
memory in any way it sees fit. This is the case on AVR, because they are 8 bit
processors, and it is also the case on Intel x86 hardware. Both architectures
can read and write words, or double words on any location in memory, be it
word aligned or not.
However, some CPU's, are not capable of doing this, and require that all double words
are aligned on a double word boundary, and all word are aligned on a word boundary.
This causes problems with some of the casts that are performed in EFSL. If you have such
a CPU, then you must comment this option out. The effect is that special functions
will be used to copy or cast memory. These functions work around the problem by
using memCpy, or manually copying elements of the structs that are normally cast when
\code{BYTE\_ALIGNMENT} is defined.
If you have an 8 bit architecture, or are running on PC, there is no need to turn this
off. If you do, the library will work fine, and maybe even without slowdown.
On architectures that do have the alignment problem, you should turn this flag off.
Failure to do so will result in undefined behavior.
\subsection{Cache configuration}
This section is dedicated to configuring the cache memory for the library. Caching
is performed by the IOMan object, see section \ref{ioman}.
\subsubsection*{IOMAN\_NUMBUFFER}
This number determines how much memory will be used for caching. Since this
is sector based one \code{IOMAN\_NUMBUFFER} equals to 512 byes of memory, plus
a small overhead in settings (approximately 8 bytes). This number is also affected
by \code{IOMAN\_NUMITERATIONS}.
You should carefully consider how much memory you will dedicate to caching. A too
low number will cause excessive data transfer to and from the disc, where a too high
number will simply be a waste of memory.
A good rule of thumb is to use 1 buffer per filesystem you create, and 2 buffers
per file you want to use simultaneously. So for a simple application with
one filesystem, and one file operation, 2 or 3 buffers will be fine. If you have memory
to spare, you can use 6 buffers. Using more buffers will have a minimal effect on
performance.
If you want to seek and rewrite portions of a file, add an extra buffer for that file.
Using the list function or creating directories will be disc intensive, try to smoothen
it by using an extra 3 buffer for either operation.
It is perfectly possible to have multiple files op for reading and writing, on different
filesystems, with listing etc and only using 1 buffer. It will be a tough blow on
performance though.
\subsubsection*{IOMAN\_NUMITERATION}
This number controls how many stack places each cache place gets. Refer to the IOMan
section for an explanation. In short, if you only have 1 buffer, leave it at 3. If you
use more than 4 buffers try decreasing the number to 2 or 1 for a small memory gain.
If you get errors, it means you have set it too low (see error support). It is best
to leave this at the default setting (do not increase it), unless you know what you
are doing.
\subsubsection*{IOMAN\_DOMEMALLOC}
This configures how IOMan will get it's memory. If you leave it enable, the memory
will be allocated by IOMan itself. That means that when you declare the IOMan object
it will have a member the size of $512 \cdot \mathrm{IOMAN\_NUMBUFFER}$.
That also means that that huge lump of memory will reside on the stack. On a true embedded platform with no malloc, this is your best option.
The last argument of \code{ioman\_init} will be ignored.
If you comment this out,IOMan will take a \code{euint8*} pointer as it's third
argument to \code{ioman\_init}. It will use the memory pointed to as cache.
You will have to make sure it's reserved and of the correct size.
This allows you to put the memory on the heap, or perform special tricks like
deallocating it without having to umount your filesystem and open files.
On systems with malloc, this is the recommended setting.
If you use the efs wrapper object, please look at the \code{efs\_init} documentation
on how to pass the ioman pointer.
\subsection{Pre-allocation}
Our VFAT module supports the concept of pre-allocation. When writing files, for
example log files, it is usually done with tiny bits a time. That is not the
most efficient way, but it is usually the only solution that works on embedded
systems. Every time you cross a cluster boundary with your write, the library
has to search a new cluster (reading the FAT), allocate it (write to the FAT).
Clearly, this is a waste. The solution we came up with was preallocating. This means
that when you write to a file, and fwrite sees that it needs to allocate more clusters,
it will allocate too many of them. Since this is done in one operation, it requires
usually only one read and one write to the FAT. This can save up to 50\% disc I/O
in some applications.
The drawback is that the allocation happens in larger chunks, if you do this with
many files, you might end up with larger than normal amounts of slackspace.
We have also implemented this feature for directories. This is very useful if you
have to create a lot of small files, since the directories grow by larger portions
then.
\subsubsection*{CLUSTER\_PREALLOC\_FILE}
This number determines the default value of extra clusters that will be allocated
with every sizeincrease. For example, if fwrite calculates that it needs 7 clusters,
and \code{CLUSTER\_PREALLOC\_FILE} is 30 then efsl will allocate 37 clusters.
This means (assuming every write needs 7 clusters) that the next 4 writes won't
require any write operation to the FAT (and due to the cluster cache the FAT will probably have to be read only once).
The value you put here will be the default value, it can be changed per file
object. (not yet implemented).
\subsubsection*{CLUSTER\_PREALLOC\_DIRECTORY}
The same explanation as above counts, only this value is used for directories.
Generally you should not put this above 10 (unless your speed tests prove otherwise
off course).
\subsection{Endianness}
The Microsoft FAT filesystem was originally created to be run on Intel compatible hardware.
Therefore the Microsoft programmers decided to record all data on the disc in little endian
format. Our library supports running on big endian devices. Here you can select whether your
target CPU is little or big endian.
Running on big endian will cause some performance lose because (rather simple) calculations have
to be made to all numbers that have to interpreted by the library. This does not apply to
data within the files off course.
If the flag \code{\#LITTLE\_ENDIAN} is set, efsl will assume that your hardware is little endian.
If you have a big endian system, you should comment this out. The function \code{fs\_checkEndian}
will tell you if you have selected the right endianness, this is a check you might want to use.
\subsection{Date and time}
This flag determines if you want to have date and time support. With date and time support we
mean that when you create or update a file the directory entry will receive the correct date and
time stamp.
Please refer to section \ref{dateandtime} to learn more about how this works.
If you disable date and time support by commenting the \code{\#DATE\_TIME\_SUPPORT} then
all dates and times that need to be created or updated will be set to zero, which in FAT land corresponds to the first of January of the year 1970.
\subsection{Errors}
When the library encounters an error, there be an error cascade moving from the error-causing object
to the topmost object where the request started. Seen from userland this gives you extremely little
information, usually nothing more than fail or success.
Every object in the library has an optional error field, that contains a unique number that
corresponds to a specific error. If you examine every error field you can see exactly where the
error was started and what the effect was on the higher level objects.
In a more practical sense you can display an error number or explanation to your users, giving
yourself or them a better chance to correct or avoid the problem.
Please see the section on error on what every value means.
\subsection{Debug}
This will turn debug support on or off. When enable (and your platform has a means of output that
is supported by EFSL) it you will see messages you have created yourself, or that are printed by the
library. By default the library is very silent, only very critical errors might get printed out.
This option is depreciated and is left in for backward compatibility.

View File

@ -0,0 +1,34 @@
\label{dateandtime}
The EFSL library supports setting and updating all date and time fields
supported by the filesystem. In order to do this the library must
know the current time and date at all times. Since it has to run everywhere,
there is no standard mechanism to get the date/time, and some systems do
not have a clock.
With default configuration there is no date or time support, you have to
turn it on manually in the configuration file \filename{config.h}.
You will have to uncomment the field named \code{\#define DATE\_TIME\_SUPPORT},
in order to activate date/time support.
Furthermore you will have to provide the library with date and time information.
A set of defines was used for this, when date/time support is not enabled,
the defines automatically return \code{0x0000} for all time and date fields,
so there is no performance suffer when you do not need date/time support.
If you do need it you will have to provide 6 functions to the library
that will tell it the time. Since these functions may get called often,
it is highly recommended that you cache the time result somewhere so
you can serve the library directly from ram. If you do not do this and
your RTC request take a lot of time, you may suffer large losses in read
or write operations depending on your hardware.
The six functions are:
\begin{itemize}
\item\code{euint16 efsl\_getYear(void)}
\item\code{euint8 efsl\_getMonth(void)}
\item\code{euint8 efsl\_getDay(void)}
\item\code{euint8 efsl\_getHour(void)}
\item\code{euint8 efsl\_getMinute(void)}
\item\code{euint8 efsl\_getSecond(void)}
\end{itemize}
Internally the library will recalculate these numbers to match the
filesystem that is currently in use.

View File

@ -0,0 +1,38 @@
Since debugging on every device is completely different, a DBG macro is
implemented. On Linux for example, this macro will print the string given
to the screen (using printf). On AVR, it will send debug strings through the
UART. For compatibility with other devices, it is necessary that you always use
the DBG-macro instead of a device-specific debugging commands.\newline
\newline
Because AVR-GCC puts strings in sram memory by default, every string should be
surrounded by the TXT-macro. On AVR, this macro will put the string in program
memory (flash), on any other device, this macro will be ignored.\newline
\newline
Example of a debug string:\\
\code{DBG((TXT("This is test nr \%d of \%d.$\backslash$n"),id,total));}
\subsubsection{Debugging on Linux}
On linux, debugging strings are sent to stdout using printf.\newline
\newline
To enable debugging, set DEBUG in \filename{config.h}.
\subsubsection{Debugging on AVR}
On AVR, debugging strings are sent through the UART and can be read using
a terminal like minicom (linux) or hyperterminal (windows). Standard, the
first UART is used, but this can be changed in \filename{debug.c} to the
second UART.\newline
\newline
To enable debugging:
\begin{itemize}
\item{Set DEBUG in \filename{config.h}}
\item{Set CLK to the clock speed of your AVR in \filename{config.h}}
\item{Set BAUDRATE to the baudrate you want in \filename{config.h}}
\item{Initialize debugging in your program by calling \code{debug\_init()}}
\end{itemize}
Remark: when you use the serial port in your main program, make sure you
use a different UART than the one efsl is using when sending debug string.
\subsubsection{Debugging on DSP}
On DSP, debugging strings are sent to Code Composer using the printf function.
\newline\newline
To enable debugging, set DEBUG in \filename{config.h}.\newline
\newline
Remark: this will only work when using a DSK-kit.

View File

@ -0,0 +1,166 @@
\label{hwdriver}
This section will describe step by step how to write an hardware endpoint.
You will be required to write your own endpoint in case non of the existing endpoints
matches your hardware.
First let's have a look at how EFSL is structured internally.\\\\
\includegraphics[scale=0.4]{schematics/objectmodel.eps}\\
As you can see we have created a linear object model that is quite simple.
The file en filesystem object deal with handling the filesystem specific stuff.
Below that we find the Partition object that is responsible for translating partition
relative addressing into disc-based LBA addressing.
The Disc object hold the partition table, and has a direct link to a cache manager, IOMan.
In IOMan, all requests for disc sectors come together. IOMan will perform checks to see
if sectors have to be read from disc (or from memory), or written back to disc.
In the latter case (reading or writing to disc), a request is made to the hardware layer.
The hardware interface has 3 responsibilities :
\begin{itemize}
\item Initialize the hardware
\item Read sectors from disc
\item Write sectors to disc
\end{itemize}
All requests are \textsl{sector}based, a sector is a 512 byte piece from the disc, that is aligned to
a 512 byte boundary.\\\\
\includegraphics[scale=0.4]{schematics/sector.eps}
In this example we will create a new endpoint that will add support for data over pigeon carrier
for the EFSL. Initializing the hardware will require feeding the pigeon and telling it where the
data is. Reading/Writing will entail giving the bird the sector and letting it fly.
Perform the following steps:
\begin{enumerate}
\item Choose a name for your endpoint\\
You will need this name to create the required defines in the source code.
For our example I've chosen the name \code{PIGEON\_CARRIER}.
For consistency the final name is then \code{HW\_ENDPOINT\_PIGEON\_CARRIER}.
\item Verify the sizes of integers\\
Open \filename{inc/types.h} and create a new entry for pigeon carriers. Perhaps
one of the existing sets is identical to yours and you can copy-paste it.
\item Add your endpoint to \filename{interface.h}\\
Locate the file \filename{interface.h} located in the directory \filename{inc/}
Add a pigeon entry (located above the \code{\#else ... NO INTERFACE DEFINED})
\begin{lstlisting}
#if defined(HW_ENDPOINT_0)
#include "interfaces/0.h"
#elif defined(HW_ENDPOINT_1)
#include "interfaces/1.h"
#elif defined(HW_ENDPOINT_PIGEON_CARRIER)
#include "interfaces/pigeon.h"
#else
#error "NO INTERFACE DEFINED - see interface.h"
#endif
\end{lstlisting}
\item Select your endpoint in \filename{conf/config.h}
\item Create your sourcefiles\\
Create a header file in \filename{inc/} and a sourcefile in \filename {src/interfaces}.
In this example I'm using \filename{pigeon.h} and \filename{pigeon.c}.
\item Add your object file to the Makefile
Take the Makefile that works best on your platform (they should all work with
GNU/Make), or create a new one, using the existing one's as a template.
Make sure to include your new pigeon object to the library.
If you have an 'ar' like utility you can create a static library, else you may
have to create a new project containing all required source files.
\end{enumerate}
The basic framework is now complete, now all that's left to do is to write the code
that will perform the actual flying work.
\subsubsection{hwInterface}
This structure represents the underlying hardware. There are some field that are required
to be present (because EFSL uses them), but you may put in as much or a little as
your driver requires to access the hardware.
As always in embedded design it is recommended to keep this structure as small
as possible.
Example:
\begin{lstlisting}
struct hwInterface{
/* Field created for THIS hardware */
Pigeon pigeon;
/* Obligatory fields */
euint32 sectorCount;
};
typedef struct hwInterface hwInterface;
\end{lstlisting}
\subsubsection{if\_initInterface}
This function will be called one time, when the hardware object is initialized
by \code{efs\_init()}. This code should bring the hardware in a ready to use
state.
The function's prototype is\\
\code{esint16 if\_initInterface(hwInterface *hw, euint8* opts);}
Optionally but recommended you should fill in the hw->sectorCount field with the number
of sectors. This field is used to validate sectorrequests.
An example of a initInterface function :
\begin{lstlisting}
esint16 if_initInterface(hwInterface *hw, euint8* opts)
{
/* Parse options */
parse_options(opts); /* Your application may not need options */
/* Check hardware state */
if(!alive(hw->pigeon)){
//printf("Pigeon died! :-(\n");
return(DEAD_PIGEON); /* #define DEAD_PIGEON -1 */
}
/* Initialize hardware */
feed(hw->pigeon);
pet (hw->pigeon);
/* Get sectors count */
hw->numSectors = ask_pigeon_num_sectors(hw->pigeon);
return(0);
}
\end{lstlisting}
\subsubsection{if\_readBuf}
This function is responsible to read a sector from the disc and store it in a user supplied buffer. You will receive the hardware object, an address and a pointer to memory for storing
the buffer.
Please be very careful to respect the boundaries of the buffers, since it will usually be IOMan
calling this function, and if you have a buffer overflow you might corrupt the cache of the
the next buffer, which in turn may produce extremely rare and impossible to retrace behavior.
The function prototype is:\\
\code{esint16 if\_readBuf(hwInterface *hw,euint32 address, euint8* buf);}
The address is an LBA address, relative to the beginning of the disc. Should you be
accessing an old hard disc, or a device which uses some other form of addressing you will have to
recalculate the address to your own addressing scheme. Please note that there is no support
for sectors that are not 512 bytes large.
\begin{lstlisting}
esint8 if_readBuf(hwInterface* hw,euint32 address,euint8* buf)
{
Message new_message;
new_message.address = address;
new_message.command = READ;
pigeon_send(hw->pigeon,new_message); /* Launches the pigeon */
while(!pigeon_returned(hw->pigeon)); /* Wait until the bird is back */
memcpy(new_message.data,buf,512); /* Copy buffer */
return(0);
}
\end{lstlisting}
\subsubsection{if\_writeBuf}
The function \code{if\_writeBuf} works exactly the same as it's reading variant.

View File

@ -0,0 +1,45 @@
\subsubsection*{Purpose}
Initializes the hardware and the software layer.
\subsubsection*{Prototype}
\code{esint8 efs\_init(EmbeddedFileSystem *efs, eint8* opts);}
\subsubsection*{Arguments}
Objects passed to \code{efs\_init}:
\begin{itemize}
\item{\code{efs}: empty EmbeddedFileSystem object}
\item
{
\code{opts}: character string containing options, depending on what
interface you are using:
\begin{itemize}
\item{Linux: opts points to the path to the device}
\item{AVR: opts points to the card enable pin (TODO)}
\item{DSP: opts points to the card enable memory address (TODO)}
\end{itemize}
}
\end{itemize}
\subsubsection*{Return value}
Returns 0 if no errors are detected.\\
\newline
Returns non-zero if a low-level error is detected:
\begin{itemize}
\item{Returns -1 if the interface could not be initialized.}
\item{Returns -2 if the filesystem could not be initialized.}
\end{itemize}
\subsubsection*{Example}
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include "efs.h"
void main(void)
{
EmbeddedFileSystem efsl;
esint8 ret;
DBG((TXT("Will init efsl now.\n")));
ret=efs_init(&efsl,"/dev/sda");
if(ret==0)
DBG((TXT("Filesystem correctly initialized.\n")));
else
DBG((TXT("Could not initialize filesystem (err \%d).\n"),ret));
}
\end{lstlisting}

View File

@ -0,0 +1,43 @@
\subsubsection*{Purpose}
Updates file records and closes file object.
\subsubsection*{Prototype}
\code{esint8 file\_fclose(File *file);}
\subsubsection*{Arguments}
Objects passed to \code{file\_fopen}:
\begin{itemize}
\item{\code{file}: pointer to a File object}
\end{itemize}
\subsubsection*{Return value}
Returns 0 if no errors are detected.\\
\newline
Returns non-zero if an error is detected.
\subsubsection*{Example}
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include "efs.h"
void main(void)
{
EmbeddedFileSystem efsl;
File file;
/* Initialize efsl */
DBG((TXT("Will init efsl now.\n")));
if(efs_init(&efsl,"/dev/sda")!=0){
DBG((TXT("Could not initialize filesystem (err \%d).\n"),ret));
exit(-1);
}
DBG((TXT("Filesystem correctly initialized.\n")));
/* Open file for reading */
if(file_fopen(&file, &efsl.myFs, "read.txt", 'r')!=0){
DBG((TXT("Could not open file for reading.\n")));
exit(-1);
}
DBG((TXT("File opened for reading.\n")));
/* Close file & filesystem */
fclose(&file);
fs_umount(&efs.myFs);
}
\end{lstlisting}

View File

@ -0,0 +1,70 @@
\subsubsection*{Purpose}
Searches for file and initializes the file object.
\subsubsection*{Prototype}
\code{esint8 file\_fopen(File *file, FileSystem *fs, eint8 *filename, eint8 mode);}
\subsubsection*{Arguments}
Objects passed to \code{file\_fopen}:
\begin{itemize}
\item{\code{file}: pointer to a File object}
\item{\code{fs}: pointer to the FileSystem object}
\item{\code{filename}: pointer to the path + filename}
\item
{
\code{mode}: mode of opening, this can be:
\begin{itemize}
\item{'r': open file for reading}
\item{'w': open file for writing}
\item{'a': open file for appending}
\end{itemize}
}
\end{itemize}
\subsubsection*{Return value}
Returns 0 if no errors are detected.\\
\newline
Returns non-zero if an error is detected:
\begin{itemize}
\item{Returns -1 if the file you are trying to open for reading could not
be found.}
\item{Returns -2 if the file you are trying to open for writing already
exists.}
\item{Returns -3 if no free spot could be found for writing or appending.}
\item{Returns -4 if mode is not correct (if it is not 'r', 'w' or 'a').}
\end{itemize}
\subsubsection*{Example}
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include "efs.h"
void main(void)
{
EmbeddedFileSystem efsl;
File file_read, file_write;
/* Initialize efsl */
DBG((TXT("Will init efsl now.\n")));
if(efs_init(&efsl,"/dev/sda")!=0){
DBG((TXT("Could not initialize filesystem (err \%d).\n"),ret));
exit(-1);
}
DBG((TXT("Filesystem correctly initialized.\n")));
/* Open file for reading */
if(file_fopen(&file_read, &efsl.myFs, "read.txt", 'r')!=0){
DBG((TXT("Could not open file for reading.\n")));
exit(-1);
}
DBG((TXT("File opened for reading.\n")));
/* Open file for writing */
if(file_fopen(&file_write, &efsl.myFs, "write.txt", 'w')!=0){
DBG((TXT("Could not open file for writing.\n")));
exit(-2);
}
DBG((TXT("File opened for writing.\n")));
/* Close files & filesystem */
fclose(&file_read);
fclose(&file_write);
fs_umount(&efs.myFs);
}
\end{lstlisting}

View File

@ -0,0 +1,51 @@
\subsubsection*{Purpose}
Reads a file and puts it's content in a buffer.
\subsubsection*{Prototype}
\code{euint32 file\_read (File *file, euint32 size, euint8 *buf);}
\subsubsection*{Arguments}
Objects passed to \code{file\_read}:
\begin{itemize}
\item{\code{file}: pointer to a File object}
\item{\code{size}: amount of bytes you want to read / put in buf}
\item{\code{buf}: pointer to the buffer you want to store the data}
\end{itemize}
\subsubsection*{Return value}
Returns the amount of bytes read.
\subsubsection*{Example}
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include "efs.h"
void main(void)
{
EmbeddedFileSystem efsl;
euint8 buffer[512];
euint16 e, f;
File file;
/* Initialize efsl */
DBG((TXT("Will init efsl now.\n")));
if(efs_init(&efsl,"/dev/sda")!=0){
DBG((TXT("Could not initialize filesystem (err \%d).\n"),ret));
exit(-1);
}
DBG((TXT("Filesystem correctly initialized.\n")));
/* Open file for reading */
if(file_fopen(&file, &efsl.myFs, "read.txt", 'r')!=0){
DBG((TXT("Could not open file for reading.\n")));
exit(-1);
}
DBG((TXT("File opened for reading.\n")));
/* Read file and print content */
while((e=file_read(&file,512,buffer))){
for(f=0;f<e;f++)
DBG((TXT("\%c"),buffer[f]));
}
/* Close file & filesystem */
fclose(&file);
fs_umount(&efs.myFs);
}
\end{lstlisting}

View File

@ -0,0 +1,52 @@
\subsubsection*{Purpose}
Reads a file and puts it's content in a buffer.
\subsubsection*{Prototype}
\code{euint32 file\_write(File *file, euint32 size, euint8 *buf)}
\subsubsection*{Arguments}
Objects passed to \code{file\_read}:
\begin{itemize}
\item{\code{file}: pointer to a File object}
\item{\code{size}: amount of bytes you want to write}
\item{\code{buf}: pointer to the buffer you want to write the data from}
\end{itemize}
\subsubsection*{Return value}
Returns the amount of bytes written.
\subsubsection*{Example}
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include <string.h>
#include "efs.h"
void main(void)
{
EmbeddedFileSystem efsl;
euint8 *buffer = "This is a test.\n";
euint16 e=0;
File file;
/* Initialize efsl */
DBG((TXT("Will init efsl now.\n")));
if(efs_init(&efsl,"/dev/sda")!=0){
DBG((TXT("Could not initialize filesystem (err \%d).\n"),ret));
exit(-1);
}
DBG((TXT("Filesystem correctly initialized.\n")));
/* Open file for writing */
if(file_fopen(&file, &efsl.myFs, "write.txt", 'w')!=0){
DBG((TXT("Could not open file for writing.\n")));
exit(-1);
}
DBG((TXT("File opened for reading.\n")));
/* Write buffer to file */
if( file_write(&file,strlen(buffer),buffer) == strlen(buffer) )
DBG((TXT("File written.\n")));
else
DBG((TXT("Could not write file.\n")));
/* Close file & filesystem */
fclose(&file);
fs_umount(&efs.myFs);
}
\end{lstlisting}

View File

@ -0,0 +1,236 @@
\label{ioman}
The IOManager that is the second lowest layer of the embedded filesystems library is
responsible for coordinating disk input and output, as well as managing a caching
system. This documentation describes the second implementation of IOMan, which includes
features such as :
\begin{itemize}
\item Delayed write
\item Buffer reference statistics
\item Buffer exportable to users
\item Support for cached direct I/O as well as indirect I/O
\item Can allocate memory itself (on the stack), or you can do it yourself (heap)
\end{itemize}
\subsubsection{General operation}
Because of the limited memory nature of most embedded devices for which this library is
intended several design decisions were made to minimize memory usage. Some of these required
that some concessions be made. One of them is that there is no memory protection, since
most devices don't have the memory to support this, or lack the ability to protect memory.
When IOMan receives a request for a sector, it will make sure it has the sector in it's
own memory cache and then give the caller a \code{euint8*} pointer to that cache. The
user is then free to do operations on that memory, and when it is done it should tell
IOMan so. Several things can go wrong with this: you can request a sector for reading,
and then write in the cache, thereby corrupting it. Or you can request a sector, but never
release it (sort of a memory leak), which may result in very bad performance, and a deadlocked
I/O manager.
But, taking into account that very little memory is required for operation, if you follow the I/O man rules, you will get a pretty clever caching object that will make writing new filesystems
a simple job.
\subsubsection{Cache decisions}
Whenever ioman receives a request to fetch a sector, be it read or write, it will have to make sure
it has, or can get the sector you want. It follows a certain path to do this.\label{cachemethod}
\begin{enumerate}
\item First of all it will scan it's cache range to see if it already has the sector.
If it is found, and it was a write request, the cache is marked writable. Usage and
reference get incremented and a pointer is then returned to the requester. If the
buffer cannot be found, ioman proceeds to step 2.
\item When an item is not in cache, it has to be fetched from the disc, the best place to
store it is in memory that does not contain anything useful yet. Ioman will search for
a place that is currently not occupied by anything. If it is found, the sector will be
placed on that spot and a pointer returned. Else, ioman proceeds to step 3.
\item Since there is no other choice than to overwrite an already existing cache, ioman will
try to find one that is the least interesting. First it will search for caches that
are marked not writable, and have no users. Ioman will then select the one that has the
least references. If there are none, it will search for caches that don't have users and
are writable. Once again the one with the least references is returned. Since it is
writable ioman will flush it to disc first. After that the requested sector is put there
and a pointer returned. If it cannot find any caches that have no users it will go to
step 4.
\item Since every cache spot is in use ioman will have to select one for overallocation.
Since this selection depends on many factors and is rather complex, a points
system is used. The algorithm considers every cache place and allocated a certain number
of points to it, lower means that it is a better candidate for overallocation. Fifty
percent of the points goes to the cache being marked writable, since having to write
a sector is expensive. Another 35 percent goes to how many overallocations have
already been done on that spot. It doesn't make sense to always overalloc the same buffer,
it is better to spread this. The remaining 15 percent is determined by the number of
references to the sector.
After a function has selected the best candidate, ioman will overwrite that spot with
the new sector. It will also push the status and sectornumber onto that cache's
retrieval stack, so that when the sector is released, the older sector can be retrieved.
If this fails go to step 5.
\item When ioman gets here it will return a (nil) pointer and flag an error.
\end{enumerate}
\subsubsection{Functions}
\begin{longtable}{|p{0.35\textwidth}|p{0.65\textwidth}|}
\hline
\multicolumn{2}{|c|}{
\textbf{I/O Manager Functions}
} \\
\multicolumn{2}{|c|}{} \\
\hline
\hline
\endfirsthead
\hline
\multicolumn{2}{|c|}{\textbf{I/O Manager Functions (continued)}} \\
\hline
\endhead
\hline
\endfoot
\hline
\endlastfoot
\code{ioman\_init} & \code{esint8 (IOManager *ioman, hwInterface *iface, euint8* bufferarea)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function is called to initialize the internal state of the I/O manager. It should be the
first function you call on an ioman object. Failure to do so will result in undefined behavior.
The function clears all internal variables to a default safe state, and sets up it's memory region.
There are two possibilities, if you supply a 0 pointer then a function will be called that contains
a static variable with a size of 512 * \code{IOMAN\_NUMBUFFERS}, else, it will be assumed that
you allocated that memory yourself and the pointer you provided will be used.
}\\
\hline
\code{\external{ioman\_reset}} & \code{void (IOManager *ioman)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function is called from the initialization function, it does the actual reset of all variables.
}\\
\hline
\code{ioman\_pop} & \code{esint8 (IOManager *ioman,euint16 bufplace)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function fetches settings (sector number, usage and status register) from stack \code{bufplace}
and puts it back on the main registers. It will return 0 on successful pop, and -1 on error, or when
there are no elements to pop.
}\\
\hline
\code{ioman\_push} & \code{esint8 (IOManager *ioman,euint16 bufplace)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function pushes the settings of cache \code{bufplace} onto that cache's stack. It does not
destroy the data in the main registers. It will return 0 for a successful push, and -1 on error, or
when there is no more space to push a new element.
}\\
\hline
\code{ioman\_readSector} & \code{esint8 (IOManager *ioman,euint32 address,euint8* buf)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function does the actual reading from the hardware, it is the one and only function that
calls \code{if\_readBuf()}, here a retry on failure policy could be implemented. This function
will correctly stream errors upwards. All calls made to this function in the iomanager are checked
for their return value, so errors propagate correctly upwards.
The address it receives is relative to the beginning of the disc, no assumptions about \code{buf}
may be made, it can be withing ioman's cache memory range, but it could also be a buffer from userspace.
The function will return 0 on success and -1 on failure.
}\\
\hline
\code{ioman\_writeSector} & \code{esint8 (IOManager *ioman, euint32 address, euint8* buf)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function does the actual writing to the hardware, it is the one and only function that
calls \code{if\_writeBuf()}, here a retry on failure policy could be implemented. This function
will correctly stream errors upwards. All calls made to this function in the iomanager are checked
for their return value, so errors propagate correctly upwards.
The address it receives is relative to the beginning of the disc, no assumptions about \code{buf}
may be made, it can be withing ioman's cache memory range, but it could also be a buffer from userspace.
The function will return 0 on success and -1 on failure.
}\\
\hline
\code{\external{ioman\_getSector}} & \code{euint8* (IOManager *ioman,euint32 address, euint8 mode)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function is the one that is called most from the higher library routines. It is the function
that will present you with a pointer to memory containing sector number \code{address}. There are
several modes that you can select or combine.\newline
\begin{tabular}{|l|p{.6\textwidth}|}
\hline
\code{IOM\_MODE\_READONLY} & This attribute says to ioman that it needs a buffer only for reading.
This does not mean that you are allowed to write to it, doing so results in undefined behavior.
You cannot combine this option with the \code{IOM\_MODE\_READWRITE} option.\\
\code{IOM\_MODE\_READWRITE} & This attribute says to ioman that it would like not only to read from
but also to write to that buffer. When you release the sector your changes will be written to disc.
This may not happen immediately though, if you want to force it take a look at the
\code{ioman\_flushRange()} function. This option cannot be combined with the
\code{IOM\_MODE\_READONLY} option.\\
\code{IOM\_MODE\_EXP\_REQ} & This option tell the iomanager that the request is exceptional, for
example that the request is unlikely to happen again. The library adds this flags to the options
when requesting the bootrecord, to prevent it from getting a high rating, which should prevent it
from being removed from the cache.\\
\hline
\end{tabular}\newline
These options can be combined by ORing them together.
}\\
\hline
\code{ioman\_releaseSector} & \code{esint8 (IOManager *ioman,euint8* buf)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function tells ioman that you are done with one of the cache elements and that it can do
it's bidding with it. Forgetting to call this function may result in deadlocked iomanagers.
}\\
\hline
\code{ioman\_directSectorRead} & \code{esint8 (IOManager *ioman,euint32 address, euint8* buf)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This is a variant of the normal getsector. Sometimes you need a sector from the disc, but all
you want to do with it is export it directly to userbuffers. It would be foolish to force a
caching of that sector if there is external space available for it.
This function will fetch sector \code{address} from disc and place it in the memory pointed to
by \code{buf}. Should there be a free spot available the sector will be cached there, so that
it may be used in the future. If the sector was available from cache in the first place, it
will simply be \code{memCpy()}'d from the cache to the userspace buffer.
}\\
\hline
\code{ioman\_directSectorWrite} & \code{esint8 (IOManager *ioman,euint32 address, euint8* buf)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function is based on the same philosophy as \code{ioman\_directSectorRead()}, however,
contrary to what the name may lead to believe it also passes through a caching layer. If
there is an unused spot (or the sector is in cache), the userbuffer will be copied to that
spot and will remain there until the space is needed or a flush is forced.
}\\
\hline
\code{ioman\_flushRange} & \code{esint8 (IOManager *ioman,euint32 address\_low, euint32 address\_high)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function is used to ask ioman to flush all sectors to disc that are in a specific
range. For example you might want to flush a specific range of your filesystem without
needlessly disturb other parts. The range is \code{address\_low <= n => address\_high}.
Off course only sectors that are marked as writable are flushed to disc.
}\\
\hline
\code{ioman\_flushAll} & \code{esint8 (IOManager *ioman)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function will cause ioman to flush out all cache units that are marked writable. If
they do not have any users, they will lose their writable mark.
}\\
\hline
\end{longtable}

View File

@ -0,0 +1,489 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the Lesser GPL. It also counts
as the successor of the GNU Library Public License, version 2, hence
the version number 2.1.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Lesser General Public License, applies to some
specially designated software packages--typically libraries--of the
Free Software Foundation and other authors who decide to use it. You
can use it too, but we suggest you first think carefully about whether
this license or the ordinary General Public License is the better
strategy to use in any particular case, based on the explanations below.
When we speak of free software, we are referring to freedom of use,
not price. Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and charge
for this service if you wish); that you receive source code or can get
it if you want it; that you can change the software and use pieces of
it in new free programs; and that you are informed that you can do
these things.
To protect your rights, we need to make restrictions that forbid
distributors to deny you these rights or to ask you to surrender these
rights. These restrictions translate to certain responsibilities for
you if you distribute copies of the library or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link other code with the library, you must provide
complete object files to the recipients, so that they can relink them
with the library after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
We protect your rights with a two-step method: (1) we copyright the
library, and (2) we offer you this license, which gives you legal
permission to copy, distribute and/or modify the library.
To protect each distributor, we want to make it very clear that
there is no warranty for the free library. Also, if the library is
modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.
Finally, software patents pose a constant threat to the existence of
any free program. We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
restrictive license from a patent holder. Therefore, we insist that
any patent license obtained for a version of the library must be
consistent with the full freedom of use specified in this license.
Most GNU software, including some libraries, is covered by the
ordinary GNU General Public License. This license, the GNU Lesser
General Public License, applies to certain designated libraries, and
is quite different from the ordinary General Public License. We use
this license for certain libraries in order to permit linking those
libraries into non-free programs.
When a program is linked with a library, whether statically or using
a shared library, the combination of the two is legally speaking a
combined work, a derivative of the original library. The ordinary
General Public License therefore permits such linking only if the
entire combination fits its criteria of freedom. The Lesser General
Public License permits more lax criteria for linking other code with
the library.
We call this license the "Lesser" General Public License because it
does Less to protect the user's freedom than the ordinary General
Public License. It also provides other free software developers Less
of an advantage over competing non-free programs. These disadvantages
are the reason we use the ordinary General Public License for many
libraries. However, the Lesser license provides advantages in certain
special circumstances.
For example, on rare occasions, there may be a special need to
encourage the widest possible use of a certain library, so that it becomes
a de-facto standard. To achieve this, non-free programs must be
allowed to use the library. A more frequent case is that a free
library does the same job as widely used non-free libraries. In this
case, there is little to gain by limiting the free library to free
software only, so we use the Lesser General Public License.
In other cases, permission to use a particular library in non-free
programs enables a greater number of people to use a large body of
free software. For example, permission to use the GNU C Library in
non-free programs enables many more people to use the whole GNU
operating system, as well as its variant, the GNU/Linux operating
system.
Although the Lesser General Public License is Less protective of the
users' freedom, it does ensure that the user of a program that is
linked with the Library has the freedom and the wherewithal to run
that program using a modified version of the Library.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.
GNU LESSER GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library or other
program which contains a notice placed by the copyright holder or
other authorized party saying it may be distributed under the terms of
this Lesser General Public License (also called "this License").
Each licensee is addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (1) uses at run time a
copy of the library already present on the user's computer system,
rather than copying library functions into the executable, and (2)
will operate properly with a modified version of the library, if
the user installs one, as long as the modified version is
interface-compatible with the version that the work was made with.
c) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
d) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
e) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the materials to be distributed need not include anything that is
normally distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded. In such case, this License incorporates the limitation as if
written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Lesser General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).
To apply these terms, attach the following notices to the library. It is
safest to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the library's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

View File

@ -0,0 +1,12 @@
This library is subject to the Lesser General Public License version 2.1.
We have chosen this license in stead of the BSD license because we feel strongly
that more effort was needed in the field of quality software in the embedded field.
Please note that if you make changes to the library itself, those modifications must be
made public, but that writing support for new hardware and linking it into the library,
does not fall under this category. However, we would off course appreciate it tremendously
if you would send us in code to support new hardware.
\subsection{GNU Lesser General Public License}
\verbatiminput{pages/lgpl.txt}

View File

@ -0,0 +1,138 @@
Debugging efsl on embedded devices is a rather hard job, because
you can't just printf debug strings or watch memory maps easily.
Because of that, core development has been performed under the
Linux operating system. Under Linux, efsl can be compiled as
library and used as a userspace filesystem handler. On Unix-
style operating system (like Linux), all devices (usb stick, disc, \ldots)
can be seen as a file, and as such been opened by efsl.\newline
\newline
In the following section, we will explain how to get started using
efsl as userspace filesystem handler. However, please note that the main
focus for efsl is to support embedded systems, which usually don't even
have 1\% of the memory you have on a PC. Accessing files on a FAT-filesystem
with efsl will be much slower than when accessing these files with the Linux
FAT kernel modules.
\subsubsection{Download \& Compile}
Let's get started:
\begin{enumerate}
\item{Get the latest release of efsl on http://www.sf.net/projects/efsl/
and put it in your homedir}
\item{Unpack the library (tar xvfj efsl-version.tar.bz2)}
\item{Get inside the directory (cd $\sim$/efsl)}
\item{Create a symlink from \filename{Makefile-LINUX} to \filename{Makefile}
(ln -s Makefile-LINUX Makefile)}
\item{Copy \filename{conf/config-sample-linux.h} to \filename{conf/config.h}
(cp conf/config-sample-linux.h conf/config.h)}
\item{Compile the library (make lib)}
\item{Find the compiled filesystem library (libefsl.a) in the current
directory}
\end{enumerate}
If you got any errors with the steps above, please check that that you have
the following packages installed: tar, gcc, libgcc, binutils \& make.
\subsubsection{Example}
Since efsl itself is only a library, it's not supposed to do anything
out of the box, than just compile. To get started, we'll show here a small
example program that opens a file on a disc/usb-stick/floppy that contains
a FAT-filesystem and prints it's content to stdout.\newline
\newline
First, create a new directory in which you put the compiled efsl-library
(\filename{libefsl.a}) and create a new file called \filename{linuxtest.c} containing:
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include <stdio.h>
#include <efs.h>
int main(void)
{
EmbeddedFileSystem efs;
EmbeddedFile file;
unsigned short i,e;
char buf[512];
if(efs_init(&efs,"/dev/sda")!=0){
printf("Could not open filesystem.\n");
return(-1);
}
if(file_fopen(&file,&efs.myFs,"group",'r')!=0){
printf("Could not open file.\n");
return(-2);
}
while(e=file_read(&file,512,buf)){
for(i=0;i<e;i++)
printf("\%c",buf[i]);
}
return(0);
}
\end{lstlisting}
$ $\newline
Some extra information on the code above:
\begin{itemize}
\item{Line 1-2: The header files for stdio (used for printf) and efsl
are included. When using the basic efsl functions, \filename{efs.h} is
the only header file of the efsl library that needs to be included.}
\item{Line 6: The object efs is created, this object will contain
information about the hardware layer, the partition table and
the disc.}
\item{Line 7: The object file is created, this object will contain
information about the file that we will open on the efs-object.}
\item{Line 9: A buffer of 512 bytes is allocated. This buffer will
be filled by fread with data.}
\item{Line 11-14: Call of \code{efs\_init}, which will initialize the efs-object.
To this function we pass:
\begin{enumerate}
\item{A pointer to the efs-object.}
\item{A pointer to the file that contains the partition table /
file system (in this example, we select a device as file).}
\end{enumerate}
If this function returns 0, it means that a valid fat partition is
found on the device given.
If no valid fat-filesystem is found, or the file does not exist, the
function returns a negative value. In this example we then print an
error message and quit.}
\item{Line 16-19: Call of \code{file\_fopen()}, which will initialize the
file-object. To this function we pass:
\begin{enumerate}
\item{A pointer to the file-object.}
\item{A pointer to the filesystem-object.}
\item{A pointer to the filename.}
\item{A char containing the the mode (read, write, append).}
\end{enumerate}
If this function returns 0, it means the file has successfully been
opened for reading / writing / appending.
If the file could not be opened, a negative value is returned.
}
\item{Line 21-24: Call of \code{file\_read()}, which will read a given value of
bytes (in this example 512) from a file and put it's content into
the buffer passed (in this example called buf). This function returns
the amount of bytes read, so the while-loop will be executed as long
as there are bytes left in the file. The code inside the while-loop
will print all characters in the buffer.}
\end{itemize}
\subsubsection{Testing}
So now let's test the program:
\begin{enumerate}
\item{Compile the program
(gcc -I/home/user/efsl/inc/ -I/home/user/efsl/conf -o linuxtest
linuxtest.c -L./ -lefsl).}
\item{Insert a usb-disc, floppy, mp3-stick, \ldots with a valid
fat-filesystem on it.}
\item{Mount the device, copy the file /etc/group on it's root dir \& umount
it.}
\item{Check that you have permission to access the device
(chown username /dev/sda*)}
\item{Run the program (./linuxtest)}
\end{enumerate}

View File

@ -0,0 +1,52 @@
\subsubsection*{Purpose}
This function fetches the next valid file in the current directory and copies
all relevant information to \code{dirlist->currentEntry}.
\subsubsection*{Prototype}
\code{esint8 ls\_getNext(DirList *dlist);}
\subsubsection*{Arguments}
Objects passed to \code{ls\_getNext}:
\begin{itemize}
\item{\code{dlist}: pointer to a DirList object}
\end{itemize}
\subsubsection*{Return value}
This function will return 0 when it has found a next file in the directory, and
was successful in copying it to \code{dirlist->currentEntry}. It will return -1
when there are no more files in the directory.
\subsubsection*{Example}
To browse through a directory you should first open it with \code{ls\_openDir} and
then you can call \code{ls\_getNext} in a loop to iterate through the files. Please
note that they are unsorted.
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include "efs.h"
#include "ls.h"
void main(void)
{
EmbeddedFileSystem efsl;
DirList list;
/* Initialize efsl */
if(efs_init(&efsl,"/dev/sda")!=0){
DBG((TXT("Could not initialize filesystem (err \%d).\n"),ret));
exit(-1);
}
/* Open the directory */
ls_openDir(list,&(efsl.myFs),"/usr/bin/");
/* Print a list of all files and their filesize */
while(ls_getNext(list)==0){
DBG((TXT("%s (%li bytes)\n"),
list->currentEntry.FileName,
list->currentEntry.FileSize));
}
/* Correctly close the filesystem */
fs_umount(&efs.myFs);
}
\end{lstlisting}
Please note that it is not required to close this object, if you wish to switch
to another directory you can just call \code{ls\_openDir} on the object again.

View File

@ -0,0 +1,42 @@
\subsubsection*{Purpose}
This function opens a directory for viewing, allowing you to iterate through
it's contents.
\subsubsection*{Prototype}
\code{esint8 ls\_openDir(DirList *dlist,FileSystem *fs,eint8* dirname);}
\subsubsection*{Arguments}
Objects passed to \code{ls\_openDir}:
\begin{itemize}
\item{\code{dlist}: pointer to a DirList object}
\item{\code{fs}: pointer to the FileSystem object}
\item{\code{dirname}: C string containing the directorypath}
\end{itemize}
\subsubsection*{Return value}
This function will return 0 when it has opened the directory, and -1 on error.\\
\subsubsection*{Example}
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include "efs.h"
#include "ls.h"
void main(void)
{
EmbeddedFileSystem efsl;
DirList list;
/* Initialize efsl */
if(efs_init(&efsl,"/dev/sda")!=0){
DBG((TXT("Could not initialize filesystem (err \%d).\n"),ret));
exit(-1);
}
/* Open the directory */
ls_openDir(list,&(efsl.myFs),"/usr/bin/");
/* Correctly close the filesystem */
fs_umount(&efs.myFs);
}
\end{lstlisting}
Please note that it is not required to close this object, if you wish to switch
to another directory you can just call \code{ls\_openDir} on the object again.

View File

@ -0,0 +1,47 @@
\subsubsection*{Purpose}
Creates a new directory.
\subsubsection*{Prototype}
\code{esint8 mkdir(FileSystem *fs,eint8* dirname);}
\subsubsection*{Arguments}
Objects passed to \code{mkdir}:
\begin{itemize}
\item{\code{fs}: pointer to the FileSystem object}
\item{\code{dir}: pointer to the path + name of the new directory}
\end{itemize}
\subsubsection*{Return value}
Returns 0 if no errors are detected.\\
\newline
Returns non-zero if an error is detected:
\begin{itemize}
\item{Returns -1 if the directory already exists.}
\item{Returns -2 if the path is incorrect (parent directory does not exists).}
\item{Returns -3 if no free space is available to create the directory.}
\end{itemize}
\subsubsection*{Example}
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
#include "efs.h"
void main(void)
{
EmbeddedFileSystem efsl;
/* Initialize efsl */
DBG((TXT("Will init efsl now.\n")));
if(efs_init(&efsl,"/dev/sda")!=0){
DBG((TXT("Could not initialize filesystem (err \%d).\n"),ret));
exit(-1);
}
DBG((TXT("Filesystem correctly initialized.\n")));
/* Create new directories */
if(mkdir(&efs.myFs,"dir1")==0){
mkdir(&efs.myFs,"dir1/subdir1");
mkdir(&efs.myFs,"dir1/subdir2");
mkdir(&efs.myFs,"dir1/subdir3");
}
/* Close filesystem */
fs_umount(&efs.myFs);
}
\end{lstlisting}

View File

@ -0,0 +1,62 @@
This section of the manual describes the minimalistic C library functions that were
created for EFSL. Since EFSL was designed for ultimate portability, no assumptions about the
workings or even the presence of a C library could be made. Fortunately only very few functions
had to be created that mimicked the operations of well known C library functions.
\\
\begin{longtable}{|p{0.35\textwidth}|p{0.65\textwidth}|}
\hline
\multicolumn{2}{|c|}{
\textbf{PLibC Functions}
} \\
\multicolumn{2}{|c|}{} \\
\hline
\hline
\endfirsthead
\hline
\multicolumn{2}{|c|}{\textbf{PLibC Functions (continued)}} \\
\hline
\endhead
\hline
\endfoot
\hline
\endlastfoot
\code{strMatch} & \code{euint16 strMatch(eint8* bufa, eint8*bufb,euint32 n)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function compares the strings \code{bufa} and \code{bufb} for \code{n} bytes.
It will return the number of bytes in that section that does not match. So if you
want to compare two strings the return value should be 0, for the strings to match over
the entire \code{n} area.
}\\
\hline
\code{memCpy} & \code{void memCpy(void* psrc, void* pdest, euint32 size)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function will copy the contents at location \code{psrc} to location \code{pdest} over
a range of \code{size} bytes.
}\\
\hline
\code{memClr} & \code{void memClr(void *pdest,euint32 size)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function will set the memory at \code{pdest} to value \code{0x00} for a range of
\code{size} bytes.
}\\
\hline
\code{memSet} & \code{void memSet(void *pdest,euint32 size,euint8 data)} \\
\hline
\multicolumn{2}{|p{\textwidth}|}{
This function will set the memory at \code{pdest} to value \code{data} for a range of
\code{size} bytes.
}\\
\hline
\end{longtable}

View File

@ -0,0 +1,25 @@
\subsection{Project aims}
The EFSL project aims to create a library for filesystems, to be used on
various embedded systems. Currently we support the Microsoft FAT filesystem
family. It is our intention to create pure ANSI C code that compiles on
anything that bears the name 'C compiler'. We don't make assumptions about
endianness or how the memory alignment is arranged on your architecture.
\newline\newline
Adding code for your specific hardware is straightforward, just add code that
fetches or writes a 512 byte sector, and the library will do the rest.
Existing code can be used, writing your own code is only required when you
have hardware for which no target exists.
\subsection{Project status}
Efsl currently supports FAT12, FAT16 and FAT32. Read and write has been tested
and is stable. Efsl runs on PC (GNU/Linux, development environment),
TMS C6000 DSP's from Texas instruments, and ATMega's from Atmel.
You can use this code with as little as 1 kilobyte RAM, however if you have
more at your disposal, an infinite amount can be used as cache memory.
The more memory you commit, the better the performance will be.
\subsection{License}
This project is released under the Lesser General Public license, which
means that you may use the library and it's sourcecode for any purpose you want,
that you may link with it and use it commercially, but that ANY change to the
code must be released under the same license. We would appreciate if you would send
us a patch when you add support for new hardware, but this is not obligatory, since it
falls under linking as far as the LGPL is concerned.

View File

@ -0,0 +1,98 @@
This section will tell you everything you need to know to start using the
embedded filesystems library on a TMS Digital Signal Processor from Texas Instruments.
The only thing that is required is that you have a McBSP port available, and that your DSP
support CLOCKSTOP mode, which is required to connect a SPI compatible device.
There are special DSP's from TI which have a special MMC/SD card controller, if you want to
use this special interface you will have to create a hardware endpoint for it. This section only
describes connecting an SD card to a normal McBSP port, since every TI DSP has at least one of them.
\subsubsection{Hardware}
Connecting the SD card to the McBSP is straightforward, you will have to make 4 data related
connections, Vcc and ground, resulting in a 6 wire interface.\\
\begin{tabular}{|l|l|l|l|l|}
\hline
\multicolumn{3}{|c|}{SD Card Interface}&\multicolumn{2}{|c|}{McBSP Interface}\\
\hline
1 & CS & Chip select & FSX & Frame Sync Transmit \\
2 & MOSI & Master out Slave In & DX & Data transmit \\
3 & GND & Supply Ground &&\\
4 & Vcc & Supply voltage (3.3 Volt) &&\\
5 & Clk & Clock & CLKX & Clock Transmit\\
6 & GND & Supply ground &&\\
7 & MISO & Master in Slave out & DR & Data receive \\
8 & NC & Not connected &&\\
9 & NC & Not connected &&\\
\hline
\end{tabular}\\
You can optionally pull the DataIn and DataOut lines up to Vcc with a $10k\Omega$ resistor, but
we found that this was not required for operation.\\
\includegraphics[scale=0.4]{schematics/sdcard.eps}\\
The frame sync from the McBSP port is used to select the card whenever a databyte has to be transferred, it is connected to the chip select of the SD card. The DX and DR pins are connected to the SDcard's DataIn and DataOut lines respectively. Finally the McBSP will have to generate a clock for
the SDcard so that it can perform operations, this is accomplished by connecting the clock transmit
line of the McBSP port to the CLK pin of the SDCard.
\subsubsection{McBSP configuration}
\begin{longtable}{|p{0.13\textwidth}|p{0.1\textwidth}|p{0.06\textwidth}|p{0.75\textwidth}|}
\hline
\multicolumn{4}{|c|}{
\textbf{McBSP Register Explanations}
} \\
\hline
\hline
\endfirsthead
\hline
\multicolumn{4}{|c|}{\textbf{mcbsp registers (continued)}} \\
\hline
\endhead
\hline
\endfoot
\hline
\endlastfoot
\multicolumn{3}{|c|}{SPCR}&
\multicolumn{1}{c|}{Serial Port Control Register}\\
\hline
Name & Bit & Value &\multicolumn{1}{c|}{Value \code{(0x00001800 | 0x00410001)}}\\
\hline
RRST&\code{0}&\code{1b} & The serial port receiver is enabled \\
XRST&\code{16}&\code{1b} & The serial port transmitter is enabled \\
CLKSTP&\code{12:11}&\code{11b} & Clock starts on falling edge without delay(see CLKXM) \\
GRST&\code{22}&\code{1b} & Sample rate generator is pulled out of reset \\
\hline
\multicolumn{3}{|c|}{PCR}&
\multicolumn{1}{c|}{Pin Control Register}\\
\hline
Name &Bit & Value &\multicolumn{1}{c|}{Value \code{0x00000A0C}}\\
\hline
CLKXP&\code{1} &\code{0b} & Transmit data on the rising edge ofthe clock\\
FSXP&\code{3} &\code{1b} & Frame Sync (Chip select on SD card) is active low\\
CLKXM&\code{9} &\code{1b} & McBSP is a master in SPI mode and generates the clock based on
the sample rate generator\\
FSXM&\code{10} &\code{1b} & Frame sync is determined by tge sample rate generator\\
\hline
\multicolumn{3}{|c|}{RCR/XCR}&
\multicolumn{1}{c|}{Receive/Transmit Control Register}\\
\hline
Name &Bit & Value &\multicolumn{1}{c|}{Value \code{0x00010000}}\\
\hline
RWDLEN&\code{7:5} &\code{000b} & Receive element is 8 bits (1byte) large\\
XDATDLY&\code{17:16} &\code{01b} & 1 bit data delay (after frame sync)\\
\hline
\multicolumn{3}{|c|}{SRGR}&
\multicolumn{1}{c|}{Sample Rate Genrator}\\
\hline
Name &Bit & Value &\multicolumn{1}{c|}{Value \code{0x20000002}}\\
\hline
CLKSM&\code{29} &\code{1b} & The sample rate generator clock is derived from the internal clock\\
FSGM&\code{28} &\code{0b} & The transmit frame sync signal is generated on every DXR to XSR copy\\
CLKGDV&\code{7:0}&\code{0x02h} & The clock divider\\
\hline
\end{longtable}

View File

@ -0,0 +1,26 @@
Standard C data types have the annoying tendency to have different sizes on difference compilers
and platforms. Therefore we have created 9 new types that are used everywhere throughout the library.
When you implement your platform you should check if any of the existing one matches your hardware,
or create a new one.\\
\\
Here's an overview:\\\\
\begin{tabular}{|p{4cm}|l|l|}
\hline
\textbf{Type} & \textbf{Size} & \textbf{Signedness}\\
\hline
\hline
\texttt{eint8} & 1 byte & default to platform \\
\texttt{esint8} & 1 byte & signed \\
\texttt{euint8} & 1 byte & unsigned \\
\hline
\texttt{eint16} & 2 bytes & default to platform \\
\texttt{esint16} & 2 bytes & signed \\
\texttt{euint16} & 2 bytes & unsigned \\
\hline
\texttt{eint32} & 4 bytes & default to platform \\
\texttt{esint32} & 4 bytes & signed \\
\texttt{euint32} & 4 bytes & unsigned \\
\hline
\end{tabular}
$ $\\\\\\
You will find the relevant code in the file \filename{types.h} in the directory \filename{inc/}.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,24 @@
include ../../conf/config.makefile
#BASE=c:\efsl\efsl-0.3
#INC=-I$(BASE)\src\base\include -I $(BASE)\src\include -I $(BASE)\src\fs\vfat\include -I $(BASE)\src\hwdrivers\atmega_spi\include -I $(BASE)\src\protocols\sdcard_spi\include -I$(BASE)\conf
BASE=../../
INC=-I$(BASE)/src/base/include -I $(BASE)/src/include -I $(BASE)/src/fs/vfat/include -I $(BASE)/src/hwdrivers/atmega_spi/include -I $(BASE)/src/protocols/sdcard_spi/include -I$(BASE)/conf
LIBS= -L $(BASE)\lib -lefsl-base -lefsl-fs-vfat -lefsl-hwd-atmega_spi -lefsl-prot-sdspi
avrtest: lib
$(CC) $(GCFLAGS) $(INC) -o avrtest.o avrtest.c $(LIBS)
$(OBJCOPY) -j .text -j .data -O ihex avrtest.o avrtest.hex
flash: avrtest
avrdude -P /dev/ttyS0 -c stk500v2 -pm128 -Uflash:w:avrtest.hex
debug: avrtest
avarice -j /dev/ttyS0 -e -p -f avrtest.o -D :4242
avr-gdb avrtest.o
lib:
make -C $(BASE) avr
clean:
make -C $(BASE) clean
rm -rf *.hex *.o

View File

@ -0,0 +1,95 @@
/*****************************************************************************/
#include <efs.h>
#include <sd.h>
#include <atmega_spi.h>
/*****************************************************************************/
/*****************************************************************************/
void hang(void);
/*****************************************************************************/
void main(void)
{
efsl_storage_conf storage_conf;
efsl_fs_conf fs_conf;
efsl_storage storage;
efsl_fs fs;
File file_r;
File file_w;
atmegaSpiInterface spi_interface;
SdSpiProtocol sd_protocol;
char buf[512];
unsigned short e;
/* Init */
debug_init();
spi_interface.pinSelect=0x01;
sd_protocol.spiHwInterface=&spi_interface;
sd_protocol.spiHwInit=(void *)atmega_spi_init;
sd_protocol.spiSendByte=(void *)atmega_spi_send;
storage_conf.hwObject=&sd_protocol;
storage_conf.if_init_fptr=(void *)sd_Init;
storage_conf.if_read_fptr=(void *)sd_readSector;
storage_conf.if_write_fptr=(void *)sd_writeSector;
storage_conf.if_ioctl_fptr=(void *)sd_ioctl;
storage_conf.ioman_bufmem=0;
fs_conf.no_partitions=0;
fs_conf.storage=&storage;
DBG((TXT("Let's go...\n")));
if(efsl_initStorage(&storage,&storage_conf)){
DBG((TXT("Error initializing storage: %d")));
hang();
}
if(efsl_initFs(&fs,&fs_conf)){
DBG((TXT("Unable to mount fs")));
hang();
}
if(file_fopen(&file_r,&fs.filesystem,"orig.txt",'r')!=0){
DBG((TXT("Could not open file for reading\n")));
hang();
}
DBG((TXT("File opened for reading\n")));
if(file_fopen(&file_w,&fs.filesystem,"copy.txt",'w')!=0){
DBG((TXT("Could not open file for writing\n")));
hang();
}
DBG((TXT("File opened for writing\n")));
while((e=file_read(&file_r,512,buf))){
DBG((TXT("Read sector\n")));
file_write(&file_w,e,buf);
DBG((TXT("Wrote sector\n")));
}
DBG((TXT("Done writing, now umount\n")));
file_fclose(&file_r);
file_fclose(&file_w);
fs_umount(&fs.filesystem);
DBG((TXT("Done :-)")));
hang();
}
/*****************************************************************************/
void hang(void)
{
while((1))
_NOP();
}
/*****************************************************************************/

View File

@ -0,0 +1 @@
avr-gcc -I../../src/base/include/ -I ../../src/include/ -I ../../src/fs/vfat/include/ -I ../../src/hwdrivers/atmega_spi/include/ -I ../../src/protocols/sdcard_spi/include/ -I../../conf -ffreestanding -mmcu=atmega128 -Os -o avrtest.o avrtest.c -L ../../lib/ -lefsl-base -lefsl-fs-vfat -lefsl-hwd-atmega_spi -lefsl-prot-sdspi && avr-objcopy -j .text -j .data -O ihex avrtest.o avrtest.hex && avrdude -P /dev/ttyS0 -c stk500v2 -pm128 -Uflash:w:avrtest.hex

View File

@ -0,0 +1,83 @@
/*****************************************************************************/
#include <efs.h>
#include <sd.h>
#include <atmega_spi.h>
/*****************************************************************************/
/*****************************************************************************/
void hang(void);
/*****************************************************************************/
void main(void)
{
efsl_storage_conf storage_conf;
efsl_fs_conf fs_conf;
efsl_storage storage;
efsl_fs fs;
File file_r;
atmegaSpiInterface spi_interface;
SdSpiProtocol sd_protocol;
char buf[512];
unsigned short i,e;
debug_init();
DBG((TXT("\nHello :-)\n")));
/* Init */
spi_interface.pinSelect=0x01;
sd_protocol.spiHwInterface=&spi_interface;
sd_protocol.spiHwInit=(void *)atmega_spi_init;
sd_protocol.spiSendByte=(void *)atmega_spi_send;
storage_conf.hwObject=&sd_protocol;
storage_conf.if_init_fptr=(void *)sd_Init;
storage_conf.if_read_fptr=(void *)sd_readSector;
storage_conf.if_write_fptr=(void *)sd_writeSector;
storage_conf.if_ioctl_fptr=(void *)sd_ioctl;
storage_conf.ioman_bufmem=0;
fs_conf.no_partitions=0;
fs_conf.storage=&storage;
DBG((TXT("Let's go...\n")));
if(efsl_initStorage(&storage,&storage_conf)){
DBG((TXT("Error initializing storage: %d")));
hang();
}
if(efsl_initFs(&fs,&fs_conf)){
DBG((TXT("Unable to mount fs")));
hang();
}
if(file_fopen(&file_r,&fs.filesystem,"orig.txt",'r')!=0){
DBG((TXT("Could not open file\n")));
hang();
}
DBG((TXT("File opened")));
while((e=file_read(&file_r,512,buf))){
for(i=0;i<e;i++){
DBG((TXT("%c"),buf[i]));
}
}
DBG((TXT("Done :-)")));
hang();
}
/*****************************************************************************/
void hang(void)
{
while((1))
_NOP();
}
/*****************************************************************************/

View File

@ -0,0 +1,16 @@
CONFDIR=../../conf
include $(CONFDIR)/config.makefile
# VFAT includes do NOT belong here, remove them when VFS is implemented !
INC_VFAT=../fs/vfat/include
INCLUDEDIRS=-I../include -Iinclude -I$(CONFDIR) -I$(INC_VFAT)
CFLAGS= $(GCFLAGS) $(INCLUDEDIRS)
OBJ=debug.o disc.o efs.o extract.o sextract.o interface.o ioman.o partition.o plibc.o
all: efsl-base
efsl-base: $(OBJ)
$(AR) rcs efsl-base.a $(OBJ)
clean:
rm -f $(OBJ) efsl-base.a

View File

@ -0,0 +1,333 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : debug.c *
* Release : 0.3 - devel *
* Description : These functions are used for debugging output on different *
* environments *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/* COMMENT REGARDING FUNCTION COMMENTS IN THIS FILE
* Only the linuxfile debug functions are commented since all functions
* perform the same logical task.
*/
/*****************************************************************************/
#include "debug.h"
/*****************************************************************************/
/*****************************************************************************/
#ifdef DEBUG
#ifdef HW_ENDPOINT_LINUX_ALL
/*****************************************************************************/
/* ****************************************************************************
* void debug(const eint8 *format, ...)
* Description: This function prints debug output to the screen (target dependant)
* and if DO_FUNC_DEBUG is defined also to a localfile.
* Return value: void
*/
void debug(const eint8 *format, ...)
{
va_list ap;
#ifdef DO_FUNC_DEBUG
euint8 c;
extern FILE* debugfile;
extern volatile euint8 tw;
#endif
va_start(ap, format);
vprintf(format,ap);
#ifdef DO_FUNC_DEBUG
for(c=0;c<tw+1;c++)
{
fprintf(debugfile," ");
}
vfprintf(debugfile,format,ap);
#endif
va_end(ap);
}
/*****************************************************************************/
/* ****************************************************************************
* void debug_funcin(const eint8 *format, ...)
* Description: This function marks the entrance of a function, which
* increments a tabfieldcounter. A tree like structure can the be found in the
* debugging file.
* Return value: void
*/
void debug_funcin(const eint8 *format, ...)
{
#ifdef DO_FUNC_DEBUG
eint8 c;
va_list ap;
extern FILE* debugfile;
extern volatile unsigned char tw;
if(debugfile==NULL)return;
for(c=0;c<tw;c++){
fprintf(debugfile," ");
}
va_start(ap, format);
vfprintf(debugfile,format,ap);
va_end(ap);
fprintf(debugfile,"\n");
tw++;
#endif
}
/*****************************************************************************/
/* ****************************************************************************
* void debug_funcout(const eint8 *format, ...)
* Description: Decrements the tabfieldcounter. This function is called everywhere
* a function is left.
* Return value: void
*/
void debug_funcout(const eint8 *format, ...)
{
#ifdef DO_FUNC_DEBUG
eint8 c;
va_list ap;
extern FILE* debugfile;
extern volatile euint8 tw;
if(debugfile==NULL)return;
if(tw>0)tw--;
for(c=0;c<tw;c++){
fprintf(debugfile," ");
}
va_start(ap, format);
vfprintf(debugfile,format,ap);
va_end(ap);
fprintf(debugfile,"\n");
#endif
}
/*****************************************************************************/
/* ****************************************************************************
* void debug_init()
* Description: This function optionally opens the debugfile, or does any other
* initialisation to enable debugoutput.
* Return value: void
*/
void debug_init()
{
#ifdef DO_FUNC_DEBUG
extern FILE* debugfile;
extern volatile unsigned char tw;
debugfile=NULL;
tw=0;
debugfile=fopen("DBG.OUT","w");
#endif
}
/*****************************************************************************/
/* ****************************************************************************
* void debug_end()
* Description: This function closes the debugfile.
* Return value: void
*/
void debug_end()
{
#ifdef DO_FUNC_DEBUG
extern FILE* debugfile;
fflush(debugfile);
fclose(debugfile);
#endif
}
/*****************************************************************************/
/*****************************************************************************/
#endif
#endif
/*****************************************************************************/
/*****************************************************************************/
#ifdef DEBUG
#ifdef HW_ENDPOINT_ATMEGA128_SD
/*****************************************************************************/
void debug(const eint8 *format, ...)
{
eint8 dbgstring[90];
va_list ap;
euint8 i=0;
eint8 c;
va_start(ap, format);
vsprintf_P(dbgstring, format, ap);
va_end(ap);
while (( (c=dbgstring[i++]) && (i<90) ))
debug_sendByte(c);
if(i>=90)
debug(PSTR("<BREAK>\n"));
}
/*****************************************************************************/
void debug_init(void)
{
unsigned short ubrr;
ubrr = ((unsigned short)DEBUG_UBRR);
switch(DEBUG_PORT){
case 0:
UBRR0H = (euint8) (ubrr>>8);
UBRR0L = (euint8) (ubrr);
UCSR0B = ( (1<<RXEN) | (1<<TXEN) );
break;
case 1:
UBRR1H = (euint8) (ubrr>>8);
UBRR1L = (euint8) (ubrr);
UCSR1B = ( (1<<RXEN) | (1<<TXEN) );
break;
default:
/* INVALID CONFIG FILE */
break;
}
}
/*****************************************************************************/
void debug_end(void)
{
/* Nothing to do here, function is here for compatibility */
}
/*****************************************************************************/
void debug_sendByte(euint8 data)
{
/* If sending a newline, add a return first */
if(data=='\n')
debug_sendByte('\r');
switch(DEBUG_PORT){
case 0:
while ( !(UCSR0A & (1<<UDRE0)) )
_NOP(); /* Wait for empty transmit buffer */
UDR0 = data; /* Start transmittion */
break;
case 1:
while ( !(UCSR1A & (1<<UDRE1)) )
_NOP(); /* Wait for empty transmit buffer */
UDR1 = data; /* Start transmittion */
break;
default:
/* INVALID CONFIG FILE */
break;
}
}
/*****************************************************************************/
unsigned char debug_getByte()
{
switch(DEBUG_PORT){
case 0:
while ( !(UCSR0A & (1<<RXC0)) )
_NOP();
return(UDR0);
break;
case 1:
while ( !(UCSR1A & (1<<RXC1)) )
_NOP();
return(UDR1);
break;
}
}
/*****************************************************************************/
euint8 debug_getString(euint8 *data,euint16 length)
{
euint8 i=0;
euint8 c;
do
{
c=debug_getByte();
if(c!='\n' && c!='\r')
data[i++]=c;
debug_sendByte(c);
}
while(c!='\n' && c!='\r' && i<length);
data[i]='\0';
return(i);
}
/*****************************************************************************/
/*****************************************************************************/
#endif
#endif
/*****************************************************************************/
/*****************************************************************************/
#ifdef DEBUG
#ifdef HW_ENDPOINT_DSP_TI6713_SD
/*****************************************************************************/
void dbg(const char *format, ...)
{
printf(format);
}
/*****************************************************************************/
void debug_init()
{
}
/*****************************************************************************/
void debug_end()
{
}
/*****************************************************************************/
/*****************************************************************************/
#endif
#endif
/*****************************************************************************/

View File

@ -0,0 +1,90 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : disc.c *
* Release : 0.3 - devel *
* Description : This file contains the functions regarding the whole disc *
* such as loading the MBR and performing read/write tests. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "disc.h"
/*****************************************************************************/
/* ****************************************************************************
* void disc_initDisc(Disc *disc,hcInterface* source)
* Description: This initialises the disc by loading the MBR and setting the
* pointer to the hardware object.
*/
void disc_initDisc(Disc *disc,IOManager* ioman)
{
disc->ioman=ioman;
disc_setError(disc,DISC_NOERROR);
}
/*****************************************************************************/
/* ****************************************************************************
* euint8 disc_findPartition(Disc *disc, euint8 partitionType, euint8 partitionIndex, euint32* startSector, euint32* sectorCount)
* Description: Locates partition of type partitionType w/ index partitionIndex on the disc and returns
* the startsector and the size to the caller.
*/
euint8 disc_findPartition(Disc *disc, euint8 partitionType, euint8 partitionIndex, euint32* startSector, euint32* sectorCount)
{
/* For now we ignore the partitionIndex */
/* Partition buffering is also not here for memory saving reasons.
Add it later with compile time option */
euint8 c, *buf, currentIndex=0;
PartitionField pf;
/* Get the MBR */
buf = ioman_getSector(disc->ioman,LBA_ADDR_MBR,IOM_MODE_READONLY|IOM_MODE_EXP_REQ);
for(c=0;c<4;c++){
ex_getPartitionField(buf+(c*SIZE_PARTITION_FIELD),&pf);
if(pf.type==partitionType){
if(partitionIndex==currentIndex){
ioman_releaseSector(disc->ioman,buf);
*startSector = pf.LBA_begin;
*sectorCount = pf.numSectors;
return(0);
}else{
currentIndex++;
}
}
}
ioman_releaseSector(disc->ioman,buf);
return(1); /* Nothing found */
}

View File

@ -0,0 +1,90 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : disc.c *
* Release : 0.3 - devel *
* Description : This file contains the functions regarding the whole disc *
* such as loading the MBR and performing read/write tests. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "disc.h"
/*****************************************************************************/
/* ****************************************************************************
* void disc_initDisc(Disc *disc,hcInterface* source)
* Description: This initialises the disc by loading the MBR and setting the
* pointer to the hardware object.
*/
void disc_initDisc(Disc *disc,IOManager* ioman)
{
disc->ioman=ioman;
disc_setError(disc,DISC_NOERROR);
}
/*****************************************************************************/
/* ****************************************************************************
* euint8 disc_findPartition(Disc *disc, euint8 partitionType, euint8 partitionIndex, euint32* startSector, euint32* sectorCount)
* Description: Locates partition of type partitionType w/ index partitionIndex on the disc and returns
* the startsector and the size to the caller.
*/
euint8 disc_findPartition(Disc *disc, euint8 partitionType, euint8 partitionIndex, euint32* startSector, euint32* sectorCount)
{
/* For now we ignore the partitionIndex */
/* Partition buffering is also not here for memory saving reasons.
Add it later with compile time option */
euint8 c, *buf, currentIndex=0;
PartitionField pf;
/* Get the MBR */
buf = ioman_getSector(disc->ioman,LBA_ADDR_MBR,IOM_MODE_READONLY|IOM_MODE_EXP_REQ);
for(c=0;c<4;c++){
ex_getPartitionField(buf+(c*SIZE_PARTITION_FIELD),&pf);
if(pf.type==partitionType){
if(partitionIndex==currentIndex){
ioman_releaseSector(disc->ioman,buf);
*startSector = pf.LBA_begin;
*sectorCount = pf.numSectors;
return(0);
}else{
currentIndex++;
}
}
}
ioman_releaseSector(disc->ioman,buf);
return(1); /* Nothing found */
}

View File

@ -0,0 +1,92 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : efs.c *
* Release : 0.3 - devel *
* Description : This should become the wrapper around efs. It will contain *
* functions like efs_init etc. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "efs.h"
/*****************************************************************************/
/* This stuff must be rewritten and causes compile errors while the datastructures are in flux */
#if 0
esint8 efsl_initStorage(efsl_storage *efsl_storage,efsl_storage_conf *config)
{
if(if_init(&(efsl_storage->interface),
config->hwObject,
config->if_init_fptr,
config->if_read_fptr,
config->if_write_fptr,
config->if_ioctl_fptr)
){
return(-1);
}
if(ioman_init(&(efsl_storage->ioman),
&(efsl_storage->interface),
config->ioman_bufmem)
){
return(-1);
}
disc_initDisc(&(efsl_storage->disc),&(efsl_storage->ioman));
return(0);
}
/*****************************************************************************/
esint8 efsl_initFs(efsl_fs *efsl_filesystem,efsl_fs_conf *config)
{
efsl_filesystem->storage=config->storage;
if(config->no_partitions){
efsl_filesystem->storage->disc.partitions[0].type=0x0B;
efsl_filesystem->storage->disc.partitions[0].LBA_begin=0;
efsl_filesystem->storage->disc.partitions[0].numSectors=
efsl_filesystem->storage->interface.sectorCount;
}
part_initPartition(
&(efsl_filesystem->partition),
&(efsl_filesystem->storage->disc)
);
if(fs_initFs(&(efsl_filesystem->filesystem),&(efsl_filesystem->partition))){
return(-1);
}
return(0);
}
/*****************************************************************************/
#endif

View File

@ -0,0 +1,91 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : efs.c *
* Release : 0.3 - devel *
* Description : This should become the wrapper around efs. It will contain *
* functions like efs_init etc. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "efs.h"
/*****************************************************************************/
/* This stuff must be rewritten and causes compile errors while the datastructures are in flux */
esint8 efsl_initStorage(efsl_storage *efsl_storage,efsl_storage_conf *config)
{
if(if_init(&(efsl_storage->interface),
config->hwObject,
config->if_init_fptr,
config->if_read_fptr,
config->if_write_fptr,
config->if_ioctl_fptr)
){
return(-1);
}
if(ioman_init(&(efsl_storage->ioman),
&(efsl_storage->interface),
config->ioman_bufmem)
){
return(-1);
}
disc_initDisc(&(efsl_storage->disc),&(efsl_storage->ioman));
return(0);
}
/*****************************************************************************/
esint8 efsl_initFs(efsl_fs *efsl_filesystem,efsl_fs_conf *config)
{
efsl_filesystem->storage=config->storage;
if(config->no_partitions){
efsl_filesystem->storage->disc.partitions[0].type=0x0B;
efsl_filesystem->storage->disc.partitions[0].LBA_begin=0;
efsl_filesystem->storage->disc.partitions[0].numSectors=
efsl_filesystem->storage->interface.sectorCount;
}
part_initPartition(
&(efsl_filesystem->partition),
&(efsl_filesystem->storage->disc)
);
if(fs_initFs(&(efsl_filesystem->filesystem),&(efsl_filesystem->partition))){
return(-1);
}
return(0);
}
/*****************************************************************************/

View File

@ -0,0 +1,57 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : extract.c *
* Release : 0.3 - devel *
* Description : This file contains functions to copy structures that get *
* corrupted when using direct memory copy *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "extract.h"
/*****************************************************************************/
#if !(defined(BYTE_ALIGNMENT))
#warning "Compiling f_setbxx"
void ex_setb16(euint8* buf,euint16 data)
{
buf[1] = data>>8;
buf[0] = data>>0;
}
void ex_setb32(euint8* buf,euint32 data)
{
buf[3] = data>>24;
buf[2] = data>>16;
buf[1] = data>>8;
buf[0] = data>>0;
}
#endif

View File

@ -0,0 +1,104 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : debug.h *
* Release : 0.3 - devel *
* Description : These functions are used for debugging output on different *
* environments *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __DEBUG_H__
#define __DEBUG_H__
/*****************************************************************************/
#include "types.h"
#include "config.h"
/*****************************************************************************/
#ifndef DEBUG
#define TXT(x) ;
#define DBG(x) ;
#define FUNC_IN(x) ;
#define FUNC_OUT(x) ;
#endif
#ifdef DEBUG
#ifdef HW_ENDPOINT_ATMEGA128_SD
#include <avr/io.h>
#include <compat/ina90.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#define TXT(x) PSTR(x)
#define DBG(x) debug x
#define FUNC_IN(x) ;
#define FUNC_OUT(x) ;
#endif
#ifdef HW_ENDPOINT_LINUX
#include <stdio.h>
#include <stdarg.h>
#define TXT(x) x
#define DBG(x) debug x
#define FUNC_IN(x) debug_funcin(x)
#define FUNC_OUT(x) debug_funcout(x)
#endif
#ifdef HW_ENDPOINT_DSP_TI6713_SD
#include <stdio.h>
#include <stdarg.h>
#define TXT(x) x
#define DBG(x) printf x
#define FUNC_IN(x) ;
#define FUNC_OUT(x) ;
#endif
void debug(const eint8 *format, ...);
void debug_init();
void debug_end();
#ifdef HW_ENDPOINT_LINUX
FILE* debugfile;
volatile euint8 tw;
void debug_funcin(const eint8 *format, ...);
void debug_funcout(const eint8 *format, ...);
euint8 debug_getByte();
euint8 debug_getString(euint8 *data,euint16 length);
#endif
#ifdef HW_ENDPOINT_ATMEGA128_SD
void debug_initUART(euint16 baudrate );
void debug_sendByte(euint8 data );
#endif
#endif
#endif

View File

@ -0,0 +1,91 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : disc.h *
* Release : 0.3 - devel *
* Description : This file contains the functions regarding the whole disc *
* such as loading the MBR and performing read/write tests. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __DISC_H_
#define __DISC_H_
/*****************************************************************************/
#include "config.h"
#include "types.h"
#include "extract.h"
#include "debug.h"
#include "error.h"
#include "interface.h"
#include "ioman.h"
/*****************************************************************************/
#define LBA_ADDR_MBR 0
#define PARTITION_TABLE_OFFSET 0x1BE
/**********************************************************\
PartitionField
------------
* uchar type Type of partition
* ulong LBA_begin LBA address of first sector.
* ulong numSectors Number of 512byte sectors
This structure is a literal representation of a 16 byte
partitionfield. Direct I/O is possible.
\**********************************************************/
struct _PartitionField{
euint8 bootFlag;
euint8 CHS_begin[3];
euint8 type;
euint8 CHS_end[3];
euint32 LBA_begin;
euint32 numSectors;
};
typedef struct _PartitionField PartitionField;
#define SIZE_PARTITION_FIELD 16
/***************************************************************************************\
Disc
--
* CompactFlash* sourcedisc Pointer to the hardwareobject that this disc is on.
* PartitionField* partitions Array of PartitionFields, containing the partition info
\***************************************************************************************/
struct _Disc{
IOManager *ioman;
DISC_ERR_EUINT8
};
typedef struct _Disc Disc;
void disc_initDisc(Disc *disc,IOManager *ioman);
euint8 disc_findPartition(Disc *disc, euint8 partitionType, euint8 partitionIndex, euint32* startSector, euint32* sectorCount);
#include "sextract.h"
#endif

View File

@ -0,0 +1,91 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : disc.h *
* Release : 0.3 - devel *
* Description : This file contains the functions regarding the whole disc *
* such as loading the MBR and performing read/write tests. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __DISC_H_
#define __DISC_H_
/*****************************************************************************/
#include "config.h"
#include "types.h"
#include "extract.h"
#include "debug.h"
#include "error.h"
#include "interface.h"
#include "ioman.h"
/*****************************************************************************/
#define LBA_ADDR_MBR 0
#define PARTITION_TABLE_OFFSET 0x1BE
/**********************************************************\
PartitionField
------------
* uchar type Type of partition
* ulong LBA_begin LBA address of first sector.
* ulong numSectors Number of 512byte sectors
This structure is a literal representation of a 16 byte
partitionfield. Direct I/O is possible.
\**********************************************************/
struct _PartitionField{
euint8 bootFlag;
euint8 CHS_begin[3];
euint8 type;
euint8 CHS_end[3];
euint32 LBA_begin;
euint32 numSectors;
};
typedef struct _PartitionField PartitionField;
#define SIZE_PARTITION_FIELD 16
/***************************************************************************************\
Disc
--
* CompactFlash* sourcedisc Pointer to the hardwareobject that this disc is on.
* PartitionField* partitions Array of PartitionFields, containing the partition info
\***************************************************************************************/
struct _Disc{
IOManager *ioman;
DISC_ERR_EUINT8
};
typedef struct _Disc Disc;
void disc_initDisc(Disc *disc,IOManager *ioman);
euint8 disc_findPartition(euint8 partitionType, euint8 partitionIndex, euint32* startSector, euint32* sectorCount);
#include "sextract.h"
#endif

View File

@ -0,0 +1,101 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : efs.c *
* Release : 0.3 - devel *
* Description : This should become the wrapper around efs. It will contain *
* functions like efs_init etc. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __EFS_H__
#define __EFS_H__
/*****************************************************************************/
#include "types.h"
#include "config.h"
#include "extract.h"
#include "sextract.h"
#include "interface.h"
#include "disc.h"
#include "partition.h"
/* WARNING !!!!
* These includes DO NOT BELONG HERE,
* remove them when the VFS layer is implemented !!!!
*/
#include "fs.h"
#include "file.h"
#include "time.h"
#include "ui.h"
/*****************************************************************************/
#define LINUX_FILE_CONFIG 0x00
#define AVR_SD_CONFIG 0x01
/*****************************************************************************/
struct _efsl_storage {
Interface interface;
IOManager ioman;
Disc disc;
};
typedef struct _efsl_storage efsl_storage;
struct _efsl_storage_conf {
void *hwObject;
esint8 (*if_init_fptr)(void*);
esint8 (*if_read_fptr)(void*,euint32,euint8*);
esint8 (*if_write_fptr)(void*,euint32,euint8*);
esint8 (*if_ioctl_fptr)(void*,euint16,void*);
void *ioman_bufmem;
};
typedef struct _efsl_storage_conf efsl_storage_conf;
struct _efsl_fs {
efsl_storage *storage;
Partition partition;
FileSystem filesystem;
};
typedef struct _efsl_fs efsl_fs;
struct _efsl_fs_conf {
efsl_storage *storage;
euint8 no_partitions;
};
typedef struct _efsl_fs_conf efsl_fs_conf;
/*****************************************************************************/
esint8 efsl_initStorage(efsl_storage *efsl_storage,efsl_storage_conf *config);
esint8 efsl_initFs(efsl_fs *efsl_filesystem,efsl_fs_conf *config);
#endif

View File

@ -0,0 +1,101 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : extract.h *
* Release : 0.3 - devel *
* Description : This file contains functions to copy structures that get *
* corrupted when using direct memory copy *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __EXTRACT_H_
#define __EXTRACT_H_
/*****************************************************************************/
#include "config.h"
#include "types.h"
/*****************************************************************************/
#if !(defined(HOST_LITTLE_ENDIAN)) && !(defined(HOST_BIG_ENDIAN))
#error Endianess undefined, see config.h
#elif defined(HOST_LITTLE_ENDIAN) && (defined(HOST_BIG_ENDIAN))
#error Endianess defined as little and big, see config.h
#endif
/*****************************************************************************/
#define end_conv16(x) ((((euint16)(x) & 0xff00) >> 8) | \
(((euint16)(x) & 0x00ff) << 8))
#define end_conv32(x) ((((euint32)(x) & 0xff000000) >> 24) | \
(((euint32)(x) & 0x00ff0000) >> 8) | \
(((euint32)(x) & 0x0000ff00) << 8) | \
(((euint32)(x) & 0x000000ff) << 24))
/*****************************************************************************/
#if defined(BYTE_ALIGNMENT)
#define ex_getb16(buf) (*((euint16*)(buf)))
#define ex_setb16(buf,data) *((euint16*)(buf))=(data)
#define ex_getb32(buf) (*((euint32*)(buf)))
#define ex_setb32(buf,data) *((euint32*)(buf))=(data)
#else
#define ex_getb16(buf) \
((euint16)(*((euint8*)(buf)+1)<<8) + \
(euint16)(*((euint8*)(buf)+0)<<0))
void ex_setb16(euint8* buf,euint16 data);
#define ex_getb32(buf) \
((euint32)(*((euint8*)(buf)+3)<<24) + \
(euint32)(*((euint8*)(buf)+2)<<16) + \
(euint32)(*((euint8*)(buf)+1)<<8) + \
(euint32)(*((euint8*)(buf)+0)<<0))
void ex_setb32(euint8* buf,euint32 data);
#endif
#if defined(HOST_LITTLE_ENDIAN)
#define ex_lth16(buf) ex_getb16(buf)
#define ex_lth32(buf) ex_getb32(buf)
#define ex_bth16(buf) end_conv16(ex_getb16(buf))
#define ex_bth32(buf) end_conv32(ex_getb32(buf))
#define ex_htl16(buf) ex_setb16(buf)
#define ex_htl32(buf) ex_setb32(buf)
#define ex_htb16(buf) ex_setb16(end_conv16(buf))
#define ex_htb32(buf) ex_setb32(end_conv32(buf))
#elif defined(HOST_BIG_ENDIAN)
#define ex_lth16(buf) end_conv16(ex_getb16(buf))
#define ex_lth32(buf) end_conv32(ex_getb32(buf))
#define ex_bth16(buf) ex_getb16(buf)
#define ex_bth32(buf) ex_getb32(buf)
#define ex_htl16(buf) ex_setb16(end_conv16(buf))
#define ex_htl32(buf) ex_setb32(end_conv32(buf))
#define ex_htb16(buf) ex_setb16(buf)
#define ex_htb32(buf) ex_setb32(buf)
#endif
#endif

View File

@ -0,0 +1,85 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : interface.c *
* Release : 0.3 - devel *
* Description : This file defines the general I/O interface functions *
* that can be performed on hardware. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#include "config.h"
#include "types.h"
#include "ioctl.h"
#ifndef MULTIPLE_INTERFACE_SUPPORT
#include HWIFUNC_HEADER
#endif
#ifndef __INTERFACE_H_
#define __INTERFACE_H_
/* If no multiple interfaces are defined, check if the
hw functions are defines */
#ifndef MULTIPLE_INTERFACE_SUPPORT
#ifndef HWIFUNC_INIT
#error "There is no HW_INIT function defined"
#endif
#ifndef HWIFUNC_READ
#error "There is no HW_READ function defined"
#endif
#ifndef HWIFUNC_WRITE
#error "There is no HW_WRITE function defined"
#endif
#endif
struct _Interface{
#ifdef MULTIPLE_INTERFACE_SUPPORT
esint8 (*initInterface)(void*);
esint8 (*readBuf)(void*,euint32,euint8*);
esint8 (*writeBuf)(void*,euint32,euint8*);
esint8 (*ioctl)(void*,euint16,void*);
#endif
void* interface_data;
euint32 sectorCount;
euint8 flags;
};
typedef struct _Interface Interface;
esint8 if_init(Interface *iface, void* hwData,
esint8 (*initInterface)(void*),
esint8 (*readBuf) (void*, euint32,euint8*),
esint8 (*writeBuf) (void*, euint32,euint8*),
esint8 (*ioctl) (void*,euint16, void*));
esint8 if_readBuf(Interface *iface, euint32 address, euint8* buf);
esint8 if_writeBuf(Interface *iface, euint32 address, euint8* buf);
#endif

View File

@ -0,0 +1,140 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : ioman.c *
* Release : 0.3 - devel *
* Description : The IO Manager receives all requests for sectors in a central *
* allowing it to make smart decision regarding caching. *
* The IOMAN_NUMBUFFER parameter determines how many sectors *
* ioman can cache. ioman also supports overallocating and *
* backtracking sectors. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __IOMAN_H__
#define __IOMAN_H__
/*****************************************************************************/
#include "interface.h"
#include "error.h"
#include "plibc.h"
#include "debug.h"
#include "types.h"
#include "config.h"
/*****************************************************************************/
#define IOMAN_STATUS_ATTR_VALIDDATA 0
#define IOMAN_STATUS_ATTR_USERBUFFER 1
#define IOMAN_STATUS_ATTR_WRITE 2
#define IOM_MODE_READONLY 1
#define IOM_MODE_READWRITE 2
#define IOM_MODE_EXP_REQ 4
struct _IOManStack{
euint32 sector;
euint8 status;
euint8 usage;
};
typedef struct _IOManStack IOManStack;
struct _IOManager{
Interface *iface;
euint8 *bufptr;
euint16 numbuf;
euint16 numit;
IOMAN_ERR_EUINT8
IOManStack stack[IOMAN_NUMBUFFER][IOMAN_NUMITERATIONS];
euint32 sector[IOMAN_NUMBUFFER];
euint8 status[IOMAN_NUMBUFFER];
euint8 usage[IOMAN_NUMBUFFER];
euint8 reference[IOMAN_NUMBUFFER];
euint8 itptr[IOMAN_NUMBUFFER];
#ifdef IOMAN_DO_MEMALLOC
euint8 cache_mem[IOMAN_NUMBUFFER * 512];
#endif
};
typedef struct _IOManager IOManager;
#define IOBJ ioman
#define ioman_isValid(bp) ioman_getAttr(IOBJ,bp,IOMAN_STATUS_ATTR_VALIDDATA)
#define ioman_isUserBuf(bp) ioman_getAttr(IOBJ,bp,IOMAN_STATUS_ATTR_USERBUFFER)
#define ioman_isWritable(bp) ioman_getAttr(IOBJ,bp,IOMAN_STATUS_ATTR_WRITE)
#define ioman_setValid(bp) ioman_setAttr(IOBJ,bp,IOMAN_STATUS_ATTR_VALIDDATA,1)
#define ioman_setUserBuf(bp) ioman_setAttr(IOBJ,bp,IOMAN_STATUS_ATTR_USERBUFFER,1)
#define ioman_setWritable(bp) ioman_setAttr(IOBJ,bp,IOMAN_STATUS_ATTR_WRITE,1)
#define ioman_setNotValid(bp) ioman_setAttr(IOBJ,bp,IOMAN_STATUS_ATTR_VALIDDATA,0)
#define ioman_setNotUserBuf(bp) ioman_setAttr(IOBJ,bp,IOMAN_STATUS_ATTR_USERBUFFER,0)
#define ioman_setNotWritable(bp) ioman_setAttr(IOBJ,bp,IOMAN_STATUS_ATTR_WRITE,0)
#define ioman_isReqRo(mode) ((mode)&(IOM_MODE_READONLY))
#define ioman_isReqRw(mode) ((mode)&(IOM_MODE_READWRITE))
#define ioman_isReqExp(mode) ((mode)&(IOM_MODE_EXP_REQ))
esint8 ioman_init(IOManager *ioman, Interface *iface, euint8* bufferarea);
void ioman_reset(IOManager *ioman);
euint8* ioman_getBuffer(IOManager *ioman,euint8* bufferarea);
void ioman_setAttr(IOManager *ioman,euint16 bufplace,euint8 attribute,euint8 val);
euint8 ioman_getAttr(IOManager *ioman,euint16 bufplace,euint8 attribute);
euint8 ioman_getUseCnt(IOManager *ioman,euint16 bufplace);
void ioman_incUseCnt(IOManager *ioman,euint16 bufplace);
void ioman_decUseCnt(IOManager *ioman,euint16 bufplace);
void ioman_resetUseCnt(IOManager *ioman,euint16 bufplace);
euint8 ioman_getRefCnt(IOManager *ioman,euint16 bufplace);
void ioman_incRefCnt(IOManager *ioman,euint16 bufplace);
void ioman_decRefCnt(IOManager *ioman,euint16 bufplace);
void ioman_resetRefCnt(IOManager *ioman,euint16 bufplace);
esint8 ioman_pop(IOManager *ioman,euint16 bufplace);
esint8 ioman_push(IOManager *ioman,euint16 bufplace);
euint8* ioman_getPtr(IOManager *ioman,euint16 bufplace);
esint16 ioman_getBp(IOManager *ioman,euint8* buf);
esint8 ioman_readSector(IOManager *ioman,euint32 address,euint8* buf);
esint8 ioman_writeSector(IOManager *ioman, euint32 address, euint8* buf);
void ioman_resetCacheItem(IOManager *ioman,euint16 bufplace);
esint32 ioman_findSectorInCache(IOManager *ioman, euint32 address);
esint32 ioman_findFreeSpot(IOManager *ioman);
esint32 ioman_findUnusedSpot(IOManager *ioman);
esint32 ioman_findOverallocableSpot(IOManager *ioman);
esint8 ioman_putSectorInCache(IOManager *ioman,euint32 address, euint16 bufplace);
esint8 ioman_flushSector(IOManager *ioman, euint16 bufplace);
euint8* ioman_getSector(IOManager *ioman,euint32 address, euint8 mode);
esint8 ioman_releaseSector(IOManager *ioman,euint8* buf);
esint8 ioman_directSectorRead(IOManager *ioman,euint32 address, euint8* buf);
esint8 ioman_directSectorWrite(IOManager *ioman,euint32 address, euint8* buf);
esint8 ioman_flushRange(IOManager *ioman,euint32 address_low, euint32 address_high);
esint8 ioman_flushAll(IOManager *ioman);
void ioman_printStatus(IOManager *ioman);
#endif

View File

@ -0,0 +1,97 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : ioman_small.c *
* Release : 0.3 - devel *
* Description : The IO Manager receives all requests for sectors in a central *
* allowing it to make smart decision regarding caching. *
* The IOMAN_NUMBUFFER parameter determines how many sectors *
* ioman can cache. ioman also supports overallocating and *
* backtracking sectors. *
* This is the small version of IOMan, for systems with limited *
* resources. It features only one fixed buffer, and has *
* simplified operation *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#include "interface.h"
#include "error.h"
#include "plibc.h"
#include "debug.h"
#include "types.h"
#include "config.h"
#define IOMAN_STATUS_ATTR_VALIDDATA 0
#define IOMAN_STATUS_ATTR_USERBUFFER 1
#define IOMAN_STATUS_ATTR_WRITE 2
#define IOM_MODE_READONLY 1
#define IOM_MODE_READWRITE 2
#define IOM_MODE_EXP_REQ 4
struct _IOManStack{
euint32 sector;
euint8 status;
};
typedef struct _IOManStack IOManStack;
struct _IOManager{
Interface *iface;
euint8 bufptr[512];
IOMAN_ERR_EUINT8
euint32 sector;
euint8 status;
euint8 itptr;
IOManStack stack;
};
typedef struct _IOManager IOManager;
#define IOBJ ioman
#define ioman_isValid() ioman_getAttr(IOBJ,IOMAN_STATUS_ATTR_VALIDDATA)
#define ioman_isUserBuf() ioman_getAttr(IOBJ,IOMAN_STATUS_ATTR_USERBUFFER)
#define ioman_isWritable() ioman_getAttr(IOBJ,IOMAN_STATUS_ATTR_WRITE)
#define ioman_setValid() ioman_setAttr(IOBJ,IOMAN_STATUS_ATTR_VALIDDATA,1)
#define ioman_setUserBuf() ioman_setAttr(IOBJ,IOMAN_STATUS_ATTR_USERBUFFER,1)
#define ioman_setWritable() ioman_setAttr(IOBJ,IOMAN_STATUS_ATTR_WRITE,1)
#define ioman_setNotValid() ioman_setAttr(IOBJ,IOMAN_STATUS_ATTR_VALIDDATA,0)
#define ioman_setNotUserBuf() ioman_setAttr(IOBJ,IOMAN_STATUS_ATTR_USERBUFFER,0)
#define ioman_setNotWritable() ioman_setAttr(IOBJ,IOMAN_STATUS_ATTR_WRITE,0)
#define ioman_isReqRo(mode) ((mode)&(IOM_MODE_READONLY))
#define ioman_isReqRw(mode) ((mode)&(IOM_MODE_READWRITE))
#define ioman_isReqExp(mode) ((mode)&(IOM_MODE_EXP_REQ))
esint8 ioman_init(IOManager *ioman, Interface *iface, euint8* bufferarea);
void ioman_reset(IOManager *ioman);
void ioman_setAttr(IOManager *ioman,euint8 attribute,euint8 val);
euint8 ioman_getAttr(IOManager *ioman,euint8 attribute);

View File

@ -0,0 +1,80 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : partition.c *
* Release : 0.3 - devel *
* Description : These functions are partition specific. Searching FAT type *
* partitions and read/write functions to partitions. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __PARTITION_H__
#define __PARTITION_H__
/*****************************************************************************/
#include "config.h"
#include "error.h"
#include "interface.h"
#include "disc.h"
#include "types.h"
/*****************************************************************************/
#define PT_EMPTY 0x00
#define PT_FAT12 0x01
#define PT_FAT16A 0x04
#define PT_EXTENDED 0x05
#define PT_FAT16 0x06
#define PT_FAT32 0x0B
#define PT_FAT32A 0x5C
#define PT_FAT16B 0x5E
/*************************************************************************************\
Partition
-------
* Disc* disc Pointer to disc containing this partition.
* eint8 activePartition Array subscript for disc->partitions[activePartition]
\*************************************************************************************/
struct _Partition{
Disc *disc;
euint32 LBA_offset;
euint32 LBA_sectorcount;
};
typedef struct _Partition Partition;
void part_initPartition(Partition *part,Disc* refDisc);
euint8* part_getSect(Partition *part, euint32 address,euint8 mode);
esint8 part_relSect(Partition *part, euint8* buf);
esint8 part_flushPart(Partition *part,euint32 addr_l, euint32 addr_h);
esint8 part_directSectorRead(Partition *part, euint32 address, euint8* buf);
esint8 part_directSectorWrite(Partition *part, euint32 address, euint8* buf);
#include "extract.h"
#endif

View File

@ -0,0 +1,81 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : partition.c *
* Release : 0.3 - devel *
* Description : These functions are partition specific. Searching FAT type *
* partitions and read/write functions to partitions. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __PARTITION_H__
#define __PARTITION_H__
/*****************************************************************************/
#include "config.h"
#include "error.h"
#include "interface.h"
#include "disc.h"
#include "types.h"
/*****************************************************************************/
#define PT_EMPTY 0x00
#define PT_FAT12 0x01
#define PT_FAT16A 0x04
#define PT_EXTENDED 0x05
#define PT_FAT16 0x06
#define PT_FAT32 0x0B
#define PT_FAT32A 0x5C
#define PT_FAT16B 0x5E
/*************************************************************************************\
Partition
-------
* Disc* disc Pointer to disc containing this partition.
* eint8 activePartition Array subscript for disc->partitions[activePartition]
\*************************************************************************************/
struct _Partition{
Disc *disc;
euint32 LBA_offset;
euint32 LBA_sectorcount;
};
typedef struct _Partition Partition;
void part_initPartition(Partition *part,Disc* refDisc);
euint8* part_getSect(Partition *part, euint32 address,euint8 mode);
esint8 part_relSect(Partition *part, euint8* buf);
esint8 part_flushPart(Partition *part,euint32 addr_l, euint32 addr_h);
esint8 part_directSectorRead(Partition *part, euint32 address, euint8* buf);
esint8 part_directSectorWrite(Partition *part, euint32 address, euint8* buf);
euint32 part_getRealLBA(Partition *part,euint32 address);
#include "extract.h"
#endif

View File

@ -0,0 +1,49 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : plibc.c *
* Release : 0.3 - devel *
* Description : This file contains replacements of common c library functions *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __PLIBC_H__
#define __PLIBC_H__
/*****************************************************************************/
#include "debug.h"
#include "types.h"
#include "config.h"
/*****************************************************************************/
euint16 strMatch(eint8* bufa, eint8*bufb,euint32 n);
void memCpy(void* psrc, void* pdest, euint32 size);
void memClr(void *pdest,euint32 size);
void memSet(void *pdest,euint32 size,euint8 data);
#endif

View File

@ -0,0 +1,52 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : sextract.c *
* Release : 0.3 - devel *
* Description : The function in this file are to load and set the structures *
* as found on the disc, only those structures for the entire *
* library are located here, filesystem specific ones are in *
* that filesystems directory *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __SEXTRACT_H_
#define __SEXTRACT_H_
#include "config.h"
#include "types.h"
#include "extract.h"
#include "disc.h"
/*#include "partition.h"*/
void ex_setPartitionField(euint8* buf,PartitionField* pf);
void ex_getPartitionField(euint8* buf,PartitionField* pf);
#endif

View File

@ -0,0 +1,83 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : interface.c *
* Release : 0.3 - devel *
* Description : This file defines the general I/O interface functions *
* that can be performed on hardware. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#include "interface.h"
esint8 if_init(Interface *iface, void* hwData,
esint8 (*initInterface)(void*),
esint8 (*readBuf) (void*, euint32,euint8*),
esint8 (*writeBuf) (void*, euint32,euint8*),
esint8 (*ioctl) (void*,euint16,void*))
{
esint8 r;
iface->flags=0;
iface->sectorCount=0;
iface->interface_data=hwData;
#ifdef MULTIPLE_INTERFACE_SUPPORT
iface->initInterface=initInterface;
iface->readBuf=readBuf;
iface->writeBuf=writeBuf;
iface->ioctl=ioctl;
r=iface->initInterface(iface->interface_data);
#else
r=HWIFUNC_INIT(iface->interface_data);
#endif
if(!r){ /* Init OK, try to get some info */
if(iface->ioctl(iface->interface_data,IOCTL_SECTORCOUNT,(void*)&(iface->sectorCount))){
iface->sectorCount=0; /* Device doesn't know */
}
}
return(r);
}
esint8 if_readBuf(Interface *iface, euint32 address, euint8* buf)
{
#ifdef MULTIPLE_INTERFACE_SUPPORT
return(iface->readBuf(iface->interface_data,address,buf));
#else
return(HWIFUNC_READ(iface->interface_data,address,buf));
#endif
}
esint8 if_writeBuf(Interface *iface, euint32 address, euint8* buf)
{
#ifdef MULTIPLE_INTERFACE_SUPPORT
return(iface->writeBuf(iface->interface_data,address,buf));
#else
return(HWIFUNC_WRITE(iface->interface_data,address,buf));
#endif
}

View File

@ -0,0 +1,83 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : interface.c *
* Release : 0.3 - devel *
* Description : This file defines the general I/O interface functions *
* that can be performed on hardware. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#include "interface.h"
esint8 if_init(Interface *iface, void* hwData,
esint8 (*initInterface)(void*),
esint8 (*readBuf) (void*, euint32,euint8*),
esint8 (*writeBuf) (void*, euint32,euint8*),
esint8 (*ioctl) (void*,euint16,void*))
{
esint8 r;
iface->flags=0;
iface->sectorCount=0;
iface->interface_data=hwData;
#ifdef MULTIPLE_INTERFACE_SUPPORT
iface->initInterface=initInterface;
iface->readBuf=readBuf;
iface->writeBuf=writeBuf;
iface->ioctl=ioctl;
r=iface->initInterface(iface->interface_data);
#else
r=HWIFUNC_INIT(iface->interface_data);
#endif
if(!r){ /* Init OK, try to get some info */
if(iface->ioctl(iface->interface_data,IOCTL_SECTORCOUNT,(void*)&(iface->sectorCount))){
iface->sectorCount=0; /* Device doesn't know */
}
}
return(r);
}
esint8 if_readBuf(Interface *iface, euint32 address, euint8* buf)
{
#ifdef MULTIPLE_INTERFACE_SUPPORT
return(iface->readBuf(iface->interface_data,address,buf));
#else
return(HWIFUNC_READ(iface->interface_data,address,buf));
#endif
}
esint8 if_writeBuf(Interface *iface, euint32 address, euint8* buf)
{
#ifdef MULTIPLE_INTERFACE_SUPPORT
return(iface->writeBuf(iface->interface_data,address,buf));
#else
return(HWIFUNC_WRITE(iface->interface_data,address,buf));
#endif
}

View File

@ -0,0 +1,595 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : ioman.c *
* Release : 0.3 - devel *
* Description : The IO Manager receives all requests for sectors in a central *
* allowing it to make smart decision regarding caching. *
* The IOMAN_NUMBUFFER parameter determines how many sectors *
* ioman can cache. ioman also supports overallocating and *
* backtracking sectors. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "ioman.h"
/*****************************************************************************/
esint8 ioman_init(IOManager *ioman, Interface *iface, euint8* bufferarea)
{
ioman->iface=iface;
ioman->bufptr = ioman_getBuffer(ioman,bufferarea);
ioman->numbuf = IOMAN_NUMBUFFER;
ioman->numit = IOMAN_NUMITERATIONS;
ioman_reset(ioman);
return(0);
}
/*****************************************************************************/
void ioman_reset(IOManager *ioman)
{
euint16 nb,ni;
memClr(ioman->sector,sizeof(euint32)*ioman->numbuf);
memClr(ioman->status,sizeof(euint8) *ioman->numbuf);
memClr(ioman->usage ,sizeof(euint8) *ioman->numbuf);
memClr(ioman->itptr ,sizeof(euint8) *ioman->numbuf);
ioman_setError(ioman,IOMAN_NOERROR);
for(nb=0;nb<ioman->numbuf;nb++){
for(ni=0;ni<ioman->numit;ni++){
ioman->stack[nb][ni].sector=0;
ioman->stack[nb][ni].status=0;
ioman->stack[nb][ni].usage =0;
}
}
}
/*****************************************************************************/
euint8* ioman_getBuffer(IOManager *ioman,euint8* bufferarea)
{
#ifdef IOMAN_DO_MEMALLOC
return(ioman->cache_mem);
#else
return(bufferarea);
#endif
}
/*****************************************************************************/
void ioman_setAttr(IOManager *ioman,euint16 bufplace,euint8 attribute,euint8 val)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_SETATTROUTOFBOUNDS);
return; /* Out of bounds */
}
if(val){
ioman->status[bufplace]|=1<<attribute;
}else{
ioman->status[bufplace]&=~(1<<attribute);
}
}
/*****************************************************************************/
euint8 ioman_getAttr(IOManager *ioman,euint16 bufplace,euint8 attribute)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_GETATTROUTOFBOUNDS);
return(0xFF); /* Out of bounds */
}
return(ioman->status[bufplace]&(1<<attribute));
}
/*****************************************************************************/
euint8 ioman_getUseCnt(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return(0x00);
}
return(ioman->usage[bufplace]);
}
/*****************************************************************************/
void ioman_incUseCnt(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return;
}
if(ioman->usage[bufplace]==0xFF)return;
else ioman->usage[bufplace]++;
}
/*****************************************************************************/
void ioman_decUseCnt(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return;
}
if(ioman->usage[bufplace]==0x0)return;
else ioman->usage[bufplace]--;
}
/*****************************************************************************/
void ioman_resetUseCnt(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return;
}
ioman->usage[bufplace]=0x00;
}
/*****************************************************************************/
euint8 ioman_getRefCnt(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return(0x00);
}
return(ioman->reference[bufplace]);
}
/*****************************************************************************/
void ioman_incRefCnt(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return;
}
if(ioman->reference[bufplace]==0xFF)return;
else ioman->reference[bufplace]++;
}
/*****************************************************************************/
void ioman_decRefCnt(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return;
}
if(ioman->reference[bufplace]==0x00)return;
else ioman->reference[bufplace]--;
}
/*****************************************************************************/
void ioman_resetRefCnt(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return;
}
ioman->reference[bufplace]=0x00;
}
/*****************************************************************************/
esint8 ioman_pop(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_POPEMPTYSTACK);
return(-1);
}
if(ioman->itptr[bufplace]==0 || ioman->itptr[bufplace]>IOMAN_NUMITERATIONS)return(-1);
ioman->sector[bufplace] = ioman->stack[bufplace][ioman->itptr[bufplace]].sector;
ioman->status[bufplace] = ioman->stack[bufplace][ioman->itptr[bufplace]].status;
ioman->usage[bufplace] = ioman->stack[bufplace][ioman->itptr[bufplace]].usage;
ioman->itptr[bufplace]--;
return(0);
}
/*****************************************************************************/
esint8 ioman_push(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return(-1);
}
if(ioman->itptr[bufplace]>=IOMAN_NUMITERATIONS){
ioman_setError(ioman,IOMAN_ERR_PUSHBEYONDSTACK);
return(-1);
}
ioman->itptr[bufplace]++;
ioman->stack[bufplace][ioman->itptr[bufplace]].sector = ioman->sector[bufplace];
ioman->stack[bufplace][ioman->itptr[bufplace]].status = ioman->status[bufplace];
ioman->stack[bufplace][ioman->itptr[bufplace]].usage = ioman->usage[bufplace];
return(0);
}
/*****************************************************************************/
euint8* ioman_getPtr(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return(0);
}
return(ioman->bufptr+bufplace*512);
}
/*****************************************************************************/
esint16 ioman_getBp(IOManager *ioman,euint8* buf)
{
if(buf<(ioman->bufptr) || buf>=( ioman->bufptr+(ioman->numbuf*512) )){
ioman_setError(ioman,IOMAN_ERR_CACHEPTROUTOFRANGE);
return(-1);
}
return((buf-(ioman->bufptr))/512);
}
/*****************************************************************************/
esint8 ioman_readSector(IOManager *ioman,euint32 address,euint8* buf)
{
esint8 r;
if(buf==0){
return(-1);
}
r=if_readBuf(ioman->iface,address,buf);
if(r!=0){
ioman_setError(ioman,IOMAN_ERR_READFAIL);
return(-1);
}
return(0);
}
/*****************************************************************************/
esint8 ioman_writeSector(IOManager *ioman, euint32 address, euint8* buf)
{
esint8 r;
if(buf==0)return(-1);
r=if_writeBuf(ioman->iface,address,buf);
if(r<=0){
ioman_setError(ioman,IOMAN_ERR_WRITEFAIL);
return(-1);
}
return(0);
}
/*****************************************************************************/
void ioman_resetCacheItem(IOManager *ioman,euint16 bufplace)
{
if(bufplace>=ioman->numbuf){
ioman_setError(ioman,IOMAN_ERR_OPOUTOFBOUNDS);
return;
}
ioman->sector[bufplace] = 0;
ioman->status[bufplace] = 0;
ioman->usage[bufplace] = 0;
ioman->reference[bufplace] = 0;
}
/*****************************************************************************/
esint32 ioman_findSectorInCache(IOManager *ioman, euint32 address)
{
euint16 c;
for(c=0;c<ioman->numbuf;c++){
if(ioman_isValid(c) && ioman->sector[c] == address)return(c);
}
return(-1);
}
/*****************************************************************************/
esint32 ioman_findFreeSpot(IOManager *ioman)
{
euint16 c;
for(c=0;c<ioman->numbuf;c++){
if(!ioman_isValid(c))return(c);
}
return(-1);
}
/*****************************************************************************/
esint32 ioman_findUnusedSpot(IOManager *ioman)
{
esint32 r=-1;
euint16 c;
euint8 fr=0,lr=0xFF;
for(c=0;c<ioman->numbuf;c++){
if(ioman_getUseCnt(ioman,c)==0){
if(!ioman_isWritable(c) && !fr){
fr=1;
lr=0xFF;
r=-1;
}
if(ioman_isWritable(c) && !fr){
if(ioman_getRefCnt(ioman,c)<=lr){
r=c;
lr=ioman_getRefCnt(ioman,c);
}
}
if(fr && !ioman_isWritable(c)){
if(ioman_getRefCnt(ioman,c)<=lr){
r=c;
lr=ioman_getRefCnt(ioman,c);
}
}
}
}
return(r);
}
/*****************************************************************************/
esint32 ioman_findOverallocableSpot(IOManager *ioman)
{
euint8 points,lp=0xFF;
euint16 c;
esint32 r=-1;
for(c=0;c<ioman->numbuf;c++){
if(ioman->itptr[c]<ioman->numit){
points = 0;
if(ioman_isWritable(c))points+=0x7F;
points += ((euint16)(ioman->itptr[c]*0x4D))/(ioman->numit);
points += ((euint16)(ioman_getRefCnt(ioman,c)*0x33))/0xFF;
if(points<lp){
lp=points;
r=c;
}
}
}
return(r);
}
/*****************************************************************************/
esint8 ioman_putSectorInCache(IOManager *ioman, euint32 address, euint16 bufplace)
{
euint8* buf;
if((buf = ioman_getPtr(ioman,bufplace))==0){
ioman_setError(ioman,IOMAN_ERR_CACHEPTROUTOFRANGE);
return(-1);
}
if((ioman_readSector(ioman,address,buf))){
ioman_setError(ioman,IOMAN_ERR_READFAIL);
return(-1);
}
ioman_setValid(bufplace);
ioman->sector[bufplace]=address;
return(0);
}
/***************** if(bufplace>=ioman->numbuf)return;
************************************************************/
esint8 ioman_flushSector(IOManager *ioman, euint16 bufplace)
{
euint8* buf;
if((buf = ioman_getPtr(ioman,bufplace))==0){
ioman_setError(ioman,IOMAN_ERR_CACHEPTROUTOFRANGE);
return(-1);
}
if(!ioman_isWritable(bufplace)){
ioman_setError(ioman,IOMAN_ERR_WRITEREADONLYSECTOR);
return(-1);
}
if(!(ioman_writeSector(ioman,ioman->sector[bufplace],buf))){ /* ERROR HERE STILL TO BE FIXED -> ! must be removed! */
ioman_setError(ioman,IOMAN_ERR_WRITEFAIL);
return(-1);
}
if(ioman->usage==0)ioman_setNotWritable(bufplace);
return(0);
}
/*****************************************************************************/
esint8 ioman_flushRange(IOManager *ioman,euint32 address_low, euint32 address_high)
{
euint32 c;
if(address_low>address_high){
c=address_low; address_low=address_high;address_high=c;
}
for(c=0;c<ioman->numbuf;c++){
if((ioman->sector[c]>=address_low) && (ioman->sector[c]<=address_high) && (ioman_isWritable(c))){
if(ioman_flushSector(ioman,c)){
return(-1);
}
if(ioman->usage[c]==0)ioman_setNotWritable(c);
}
}
return(0);
}
/*****************************************************************************/
esint8 ioman_flushAll(IOManager *ioman)
{
euint16 c;
for(c=0;c<ioman->numbuf;c++){
if(ioman_isWritable(c)){
if(ioman_flushSector(ioman,c)){
return(-1);
}
if(ioman->usage[c]==0)ioman_setNotWritable(c);
}
}
return(0);
}
/*****************************************************************************/
euint8* ioman_getSector(IOManager *ioman,euint32 address, euint8 mode)
{
esint32 bp;
if((bp=ioman_findSectorInCache(ioman,address))!=-1){
if(ioman_isReqRw(mode)){
ioman_setWritable(bp);
}
ioman_incUseCnt(ioman,bp);
if(!ioman_isReqExp(mode))ioman_incRefCnt(ioman,bp);
return(ioman_getPtr(ioman,bp));
}
if((bp=ioman_findFreeSpot(ioman))==-1){
if(((bp=ioman_findUnusedSpot(ioman))!=-1)&&(ioman_isWritable(bp))){
ioman_flushSector(ioman,bp);
}
}
if(bp!=-1){
ioman_resetCacheItem(ioman,bp);
if((ioman_putSectorInCache(ioman,address,bp))){
return(0);
}
if(mode==IOM_MODE_READWRITE){
ioman_setWritable(bp);
}
ioman_incUseCnt(ioman,bp);
if(!ioman_isReqExp(mode))ioman_incRefCnt(ioman,bp);
return(ioman_getPtr(ioman,bp));
}
if((bp=ioman_findOverallocableSpot(ioman))!=-1){
if(ioman_isWritable(bp)){
ioman_flushSector(ioman,bp);
}
if(ioman_push(ioman,bp)){
return(0);
}
ioman_resetCacheItem(ioman,bp);
if((ioman_putSectorInCache(ioman,address,bp))){
return(0);
}
if(ioman_isReqRw(mode)){
ioman_setWritable(bp);
}
ioman_incUseCnt(ioman,bp);
if(!ioman_isReqExp(mode))ioman_incRefCnt(ioman,bp);
return(ioman_getPtr(ioman,bp));
}
ioman_setError(ioman,IOMAN_ERR_NOMEMORY);
return(0);
}
/*****************************************************************************/
esint8 ioman_releaseSector(IOManager *ioman,euint8* buf)
{
euint16 bp;
bp=ioman_getBp(ioman,buf);
ioman_decUseCnt(ioman,bp);
if(ioman_getUseCnt(ioman,bp)==0 && ioman->itptr[bp]!=0){
if(ioman_isWritable(bp)){
ioman_flushSector(ioman,bp);
}
ioman_pop(ioman,bp);
ioman_putSectorInCache(ioman,ioman->sector[bp],bp);
}
return(0);
}
/*****************************************************************************/
esint8 ioman_directSectorRead(IOManager *ioman,euint32 address, euint8* buf)
{
euint8* ibuf;
esint16 bp;
if((bp=ioman_findSectorInCache(ioman,address))!=-1){
ibuf=ioman_getPtr(ioman,bp);
memCpy(ibuf,buf,512);
return(0);
}
if((bp=ioman_findFreeSpot(ioman))!=-1){
if((ioman_putSectorInCache(ioman,address,bp))){
return(-1);
}
ibuf=ioman_getPtr(ioman,bp);
memCpy(ibuf,buf,512);
return(0);
}
if(ioman_readSector(ioman,address,buf)){
return(-1);
}
return(0);
}
/*****************************************************************************/
esint8 ioman_directSectorWrite(IOManager *ioman,euint32 address, euint8* buf)
{
euint8* ibuf;
esint16 bp;
if((bp=ioman_findSectorInCache(ioman,address))!=-1){
ibuf=ioman_getPtr(ioman,bp);
memCpy(buf,ibuf,512);
ioman_setWritable(bp);
return(0);
}
if((bp=ioman_findFreeSpot(ioman))!=-1){
ibuf=ioman_getPtr(ioman,bp);
memCpy(buf,ibuf,512);
ioman_resetCacheItem(ioman,bp);
ioman->sector[bp]=address;
ioman_setWritable(bp);
ioman_setValid(bp);
return(0);
}
if(ioman_writeSector(ioman,address,buf)){
return(-1);
}
return(0);
}
/*****************************************************************************/
void ioman_printStatus(IOManager *ioman)
{
euint16 c;
DBG((TXT("IO-Manager -- Report\n====================\n")));
DBG((TXT("Buffer is %i sectors, from %p to %p\n"),
ioman->numbuf,ioman->bufptr,ioman->bufptr+(ioman->numbuf*512)));
for(c=0;c<ioman->numbuf;c++){
if(ioman_isValid(c)){
DBG((TXT("BP %3i\t SC %8li\t\t US %i\t RF %i\t %s %s\n"),
c,ioman->sector[c],ioman_getUseCnt(ioman,c),ioman_getRefCnt(ioman,c),
ioman_isUserBuf(c) ? "USRBUF" : " ",
ioman_isWritable(c) ? "WRITABLE" : "READONLY"));
}
}
}
/*****************************************************************************/

View File

@ -0,0 +1,119 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : ioman_small.c *
* Release : 0.3 - devel *
* Description : The IO Manager receives all requests for sectors in a central *
* allowing it to make smart decision regarding caching. *
* The IOMAN_NUMBUFFER parameter determines how many sectors *
* ioman can cache. ioman also supports overallocating and *
* backtracking sectors. *
* This is the small version of IOMan, for systems with limited *
* resources. It features only one fixed buffer, and has *
* simplified operation *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#include "ioman_small.h"
esint8 ioman_init(IOManager *ioman, Interface *iface, euint8* bufferarea)
{
ioman->iface=iface;
ioman_reset(ioman);
return(0);
}
/*****************************************************************************/
void ioman_reset(IOManager *ioman)
{
ioman->sector=ioman->status=ioman->itptr=0;
ioman->stack.sector=ioman->stack.status=0;
ioman_setError(ioman,IOMAN_NOERROR);
}
/*****************************************************************************/
void ioman_setAttr(IOManager *ioman,euint8 attribute,euint8 val)
{
if(val){
ioman->status|=1<<attribute;
}else{
ioman->status&=~(1<<attribute);
}
}
/*****************************************************************************/
euint8 ioman_getAttr(IOManager *ioman,euint8 attribute)
{
return(ioman->status&(1<<attribute));
}
/*****************************************************************************/
euint8* ioman_getSector(IOManager *ioman,euint32 address, euint8 mode)
{
if(address==ioman->sector){
if(mode==IOM_MODE_READWRITE)ioman_setWritable();
return(ioman->bufptr);
}
}
esint8 ioman_readSector(IOManager *ioman,euint32 address,euint8* buf)
{
esint8 r;
if(buf==0){
return(-1);
}
r=if_readBuf(ioman->iface,address,buf);
if(r!=0){
ioman_setError(ioman,IOMAN_ERR_READFAIL);
return(-1);
}
return(0);
}
/*****************************************************************************/
esint8 ioman_writeSector(IOManager *ioman, euint32 address, euint8* buf)
{
esint8 r;
if(buf==0)return(-1);
r=if_writeBuf(ioman->iface,address,buf);
if(r<=0){
ioman_setError(ioman,IOMAN_ERR_WRITEFAIL);
return(-1);
}
return(0);
}
/*****************************************************************************/

View File

@ -0,0 +1,104 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : partition.c *
* Release : 0.3 - devel *
* Description : These functions are partition specific. Searching FAT type *
* partitions and read/write functions to partitions. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "partition.h"
/*****************************************************************************/
/* ****************************************************************************
* void part_initPartition(Partition *part,Disc* refDisc)
* Description: This function searches the 4 partitions for a FAT class partition
* and marks the first one found as the active to be used partition.
*/
void part_initPartition(Partition *part,Disc* refDisc)
{
eint16 c;
part->disc=refDisc;
part->LBA_offset=0;
part->LBA_sectorcount=0;
part_setError(part,PART_NOERROR);
}
/*****************************************************************************/
euint8 part_openPartitionType(Partition *part,euint8 type)
{
return(
disc_findPartition(part->disc,type,0,&(part->LBA_offset),&(part->LBA_sectorcount))
);
}
/* ****************************************************************************
* euint8* part_getSect(Partition *part, euint32 address, euint8 mode)
* Description: This function calls ioman_getSector, but recalculates the sector
* address to be partition relative.
* Return value: Whatever getSector returns. (pointer or 0)
*/
euint8* part_getSect(Partition *part, euint32 address, euint8 mode)
{
return(ioman_getSector(part->disc->ioman,part_getRealLBA(part,address),mode));
}
/* ****************************************************************************
* esint8 part_relSect(Partition *part, euint8* buf)
* Description: This function calls ioman_releaseSector.
* Return value: Whatever releaseSector returns.
*/
esint8 part_relSect(Partition *part, euint8* buf)
{
return(ioman_releaseSector(part->disc->ioman,buf));
}
esint8 part_flushPart(Partition *part,euint32 addr_l, euint32 addr_h)
{
return(
ioman_flushRange(part->disc->ioman,part_getRealLBA(part,addr_l),part_getRealLBA(part,addr_h))
);
}
esint8 part_directSectorRead(Partition *part,euint32 address, euint8* buf)
{
return(
ioman_directSectorRead(part->disc->ioman,part_getRealLBA(part,address),buf)
);
}
esint8 part_directSectorWrite(Partition *part,euint32 address, euint8* buf)
{
return(
ioman_directSectorWrite(part->disc->ioman,part_getRealLBA(part,address),buf)
);
}

View File

@ -0,0 +1,104 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : partition.c *
* Release : 0.3 - devel *
* Description : These functions are partition specific. Searching FAT type *
* partitions and read/write functions to partitions. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "partition.h"
/*****************************************************************************/
/* ****************************************************************************
* void part_initPartition(Partition *part,Disc* refDisc)
* Description: This function searches the 4 partitions for a FAT class partition
* and marks the first one found as the active to be used partition.
*/
void part_initPartition(Partition *part,Disc* refDisc)
{
eint16 c;
part->disc=refDisc;
part->LBA_offset=0;
part->LBA_sectorcount=0;
part_setError(part,PART_NOERROR);
}
/*****************************************************************************/
euint8 part_openPartitionType(Partition *part,euint8 type)
{
return(
disc_findPartition(disc,type,0,&(part->LBA_offset),&(part->LBA_sectorcount))
);
}
/* ****************************************************************************
* euint8* part_getSect(Partition *part, euint32 address, euint8 mode)
* Description: This function calls ioman_getSector, but recalculates the sector
* address to be partition relative.
* Return value: Whatever getSector returns. (pointer or 0)
*/
euint8* part_getSect(Partition *part, euint32 address, euint8 mode)
{
return(ioman_getSector(part->disc->ioman,part_getRealLBA(part,address),mode));
}
/* ****************************************************************************
* esint8 part_relSect(Partition *part, euint8* buf)
* Description: This function calls ioman_releaseSector.
* Return value: Whatever releaseSector returns.
*/
esint8 part_relSect(Partition *part, euint8* buf)
{
return(ioman_releaseSector(part->disc->ioman,buf));
}
esint8 part_flushPart(Partition *part,euint32 addr_l, euint32 addr_h)
{
return(
ioman_flushRange(part->disc->ioman,part_getRealLBA(part,addr_l),part_getRealLBA(part,addr_h))
);
}
esint8 part_directSectorRead(Partition *part,euint32 address, euint8* buf)
{
return(
ioman_directSectorRead(part->disc->ioman,part_getRealLBA(part,address),buf)
);
}
esint8 part_directSectorWrite(Partition *part,euint32 address, euint8* buf)
{
return(
ioman_directSectorWrite(part->disc->ioman,part_getRealLBA(part,address),buf)
);
}

View File

@ -0,0 +1,83 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : plibc.c *
* Release : 0.3 - devel *
* Description : This file contains replacements of common c library functions *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "plibc.h"
/*****************************************************************************/
/* ****************************************************************************
* unsigned short strMatch(char* bufa, char*bufb, unsigned long n)
* Description: Compares bufa and bufb for a length of n bytes.
* Return value: Returns the number of character NOT matching.
*/
euint16 strMatch(eint8* bufa, eint8*bufb,euint32 n)
{
euint32 c;
euint16 res=0;
for(c=0;c<n;c++)if(bufa[c]!=bufb[c])res++;
return(res);
}
/*****************************************************************************/
/* ****************************************************************************
* void memCpy(void* psrc, void* pdest, unsigned long size)
* Description: Copies the contents of psrc into pdest on a byte per byte basis.
* The total number of bytes copies is size.
*/
void memCpy(void* psrc, void* pdest, euint32 size)
{
while(size>0){
*((eint8*)pdest+size-1)=*((eint8*)psrc+size-1);
size--;
}
}
/*****************************************************************************/
void memClr(void *pdest,euint32 size)
{
while(size>0){
*(((eint8*)pdest)+size-1)=0x00;
size--;
}
}
void memSet(void *pdest,euint32 size,euint8 data)
{
while(size>0){
*(((eint8*)pdest)+size-1)=data;
size--;
}
}

View File

@ -0,0 +1,70 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : sextract.c *
* Release : 0.3 - devel *
* Description : The function in this file are to load and set the structures *
* as found on the disc, only those structures for the entire *
* library are located here, filesystem specific ones are in *
* that filesystems directory *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#include "sextract.h"
void ex_setPartitionField(euint8* buf,PartitionField* pf)
{
*(buf) = pf->bootFlag;
*(buf+1) = pf->CHS_begin[0];
*(buf+2) = pf->CHS_begin[1];
*(buf+3) = pf->CHS_begin[2];
*(buf+4) = pf->type;
*(buf+5) = pf->CHS_end[0];
*(buf+6) = pf->CHS_end[1];
*(buf+7) = pf->CHS_end[2];
ex_setb32(buf+8,pf->LBA_begin);
ex_setb32(buf+12,pf->numSectors);
}
/*****************************************************************************/
void ex_getPartitionField(euint8* buf,PartitionField* pf)
{
pf->bootFlag = *(buf);
pf->CHS_begin[0] = *(buf + 1);
pf->CHS_begin[1] = *(buf + 2);
pf->CHS_begin[2] = *(buf + 3);
pf->type = *(buf + 4);
pf->CHS_end[0] = *(buf + 5);
pf->CHS_end[1] = *(buf + 6);
pf->CHS_end[2] = *(buf + 7);
pf->LBA_begin = ex_getb32(buf + 8);
pf->numSectors = ex_getb32(buf + 12);
}
/*****************************************************************************/

View File

@ -0,0 +1,70 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : sextract.c *
* Release : 0.3 - devel *
* Description : The function in this file are to load and set the structures *
* as found on the disc, only those structures for the entire *
* library are located here, filesystem specific ones are in *
* that filesystems directory *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#include "sextract.h"
void ex_setPartitionField(euint8* buf,PartitionField* pf)
{
*(buf) = pf->bootFlag;
*(buf+1) = pf->CHS_begin[0];
*(buf+2) = pf->CHS_begin[1];
*(buf+3) = pf->CHS_begin[2];
*(buf+4) = pf->type;
*(buf+5) = pf->CHS_end[0];
*(buf+6) = pf->CHS_end[1];
*(buf+7) = pf->CHS_end[2];
ex_setb32(buf+8,pf->LBA_begin);
ex_setb32(buf+12,pf->numSectors);
}
/*****************************************************************************/
void ex_getPartitionField(euint8* buf,PartitionField* pf)
{
pf->bootFlag = *(buf);
pf->CHS_begin[0] = *(buf + 1);
pf->CHS_begin[1] = *(buf + 2);
pf->CHS_begin[2] = *(buf + 3);
pf->type = *(buf + 4);
pf->CHS_end[0] = *(buf + 5);
pf->CHS_end[1] = *(buf + 6);
pf->CHS_end[2] = *(buf + 7);
pf->LBA_begin = ex_getb32(buf + 8);
pf->numSectors = ex_getb32(buf + 12);
}
/*****************************************************************************/

View File

@ -0,0 +1,15 @@
CONFDIR=../../../conf
include $(CONFDIR)/config.makefile
BASEDIR=../../base/include
INCLUDEDIRS=-I../../include -Iinclude -I$(BASEDIR) -I$(CONFDIR)
CFLAGS=$(GCFLAGS) $(INCLUDEDIRS)
OBJ=dir.o fat.o file.o fs.o ls.o mkfs.o time.o ui.o
all: efsl-fs-vfat
efsl-fs-vfat: $(OBJ)
$(AR) rcs efsl-fs-vfat.a $(OBJ)
clean:
rm -f $(OBJ) efsl-fs-vfat.a

View File

@ -0,0 +1,361 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : dir.c *
* Release : 0.3 - devel *
* Description : The functions of dir.c are part of fs.c, they deal with all *
* the directory specific stuff. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "dir.h"
/*****************************************************************************/
/* ****************************************************************************
* void dir_getFileStructure(FileSystem *fs,FileRecord *filerec,FileLocation *loc)
* Description: This function stores the filerecord located at loc in filerec.
* It fetches the required sector for this.
* Return value: void
*/
void dir_getFileStructure(FileSystem *fs,FileRecord *filerec,FileLocation *loc)
{
euint8 *buf;
buf=part_getSect(fs->part,loc->Sector,IOM_MODE_READONLY);
*filerec=*(((FileRecord*)buf)+loc->Offset);
part_relSect(fs->part,buf);
}
/*****************************************************************************/
/* ****************************************************************************
* void dir_createDirectoryEntry(FileSystem *fs,FileRecord *filerec,FileLocation *loc)
* Description: This function writes the filerecord stored in filerec to disc at
* location loc.
* Return value: void
*/
void dir_createDirectoryEntry(FileSystem *fs,FileRecord *filerec,FileLocation *loc)
{
euint8 *buf;
buf = part_getSect(fs->part,loc->Sector,IOM_MODE_READWRITE);
memCpy(filerec,buf+(loc->Offset*sizeof(*filerec)),sizeof(*filerec));
part_relSect(fs->part,buf);
}
/*****************************************************************************/
/* ****************************************************************************
* void dir_createDefaultEntry(FileSystem *fs,FileRecord *filerec,eint8* fatfilename)
* Description: This function fills in a filerecord with safe default values, and
* a given fatfilename. If your system has a means of knowing time, here is an
* excellent place to apply it to the filerecord.
* Return value: void
*/
void dir_createDefaultEntry(FileSystem *fs,FileRecord *filerec,eint8* fatfilename)
{
memCpy(fatfilename,filerec->FileName,11);
filerec->Attribute=0x00;
filerec->NTReserved=0x00;
filerec->MilliSecTimeStamp=0x00;
filerec->CreatedTime=time_getTime();
filerec->CreatedDate=time_getDate();
filerec->AccessDate=filerec->CreatedDate;
filerec->FirstClusterHigh=0x0000;
filerec->WriteTime=filerec->CreatedTime;
filerec->WriteDate=filerec->CreatedDate;
filerec->FirstClusterLow=0x0000;
filerec->FileSize=0x00000000;
}
/*****************************************************************************/
/* ****************************************************************************
* void dir_setFirstCluster(File *file,euint32 cluster_addr)
* Description: This function requires modification to release it from
* depending on the file object.
* Return value:
*/
void dir_setFirstCluster(FileSystem *fs,FileLocation *loc,euint32 cluster_addr)
{
euint8 *buf;
buf = part_getSect(fs->part,loc->Sector,IOM_MODE_READWRITE);
(((FileRecord*)buf)+loc->Offset)->FirstClusterHigh=cluster_addr>>16;
(((FileRecord*)buf)+loc->Offset)->FirstClusterLow=cluster_addr&0xFFFF;
part_relSect(fs->part,buf);
}
/*****************************************************************************/
/* ****************************************************************************
* void dir_setFileSize(FileSystem *fs, FileLocation *loc,euint32 numbytes)
* Description: This function changes the filesize recorded at loc->Sector
* to 'numbytes'.
* Return value: void
*/
void dir_setFileSize(FileSystem *fs, FileLocation *loc,euint32 numbytes)
{
euint8 *buf;
buf = part_getSect(fs->part,loc->Sector,IOM_MODE_READWRITE);
(((FileRecord*)buf)+loc->Offset)->FileSize=numbytes;
part_relSect(fs->part,buf);
}
/*****************************************************************************/
/* ****************************************************************************
* esint8 dir_updateDirectoryEntry(FileSystem *fs,FileRecord *entry,FileLocation *loc))
* This function changes the entire entity stores at loc to the data recorded
* in entry. This is for custom updates to the directoryentry.
* Return value: 0 on success, -1 on failure
*/
esint8 dir_updateDirectoryEntry(FileSystem *fs,FileRecord *entry,FileLocation *loc)
{
euint8 *buf;
buf = part_getSect(fs->part,loc->Sector,IOM_MODE_READWRITE);
memCpy(entry,buf+(loc->Offset*sizeof(*entry)),sizeof(*entry));
part_relSect(fs->part,buf);
return(0);
}
/* ****************************************************************************
* euint32 dir_findFileinBuf(euint8 *buf, eint8 *fatname, FileLocation *loc)
* This function searches for a given fatfilename in the buffer provided.
* It will iterate through the 16 direntry's in the buffer and searches
* for the fatfilename. If found, it will store the offset and attribute
* entry of the directoryrecord in the loc structure.
* If loc is 0, then it's members are not touched.
* Return value: This function returns 0 when it cannot find the file,
* if it can find the file it will return the first cluster number.
*/
euint32 dir_findFileinBuf(euint8 *buf, eint8 *fatname, FileLocation *loc)
{
FileRecord fileEntry;
euint8 c;
for(c=0; c<16; c++)
{
fileEntry = *(((FileRecord*)buf) + c);
/* Check if the entry is for short filenames */
if( !( (fileEntry.Attribute & 0x0F) == 0x0F ) )
{
if( strMatch((eint8*)fileEntry.FileName,fatname,11) == 0 )
{
/* The entry has been found, return the location in the dir */
if(loc)loc->Offset = c;
if(loc)loc->attrib = fileEntry.Attribute;
if((((euint32 )fileEntry.FirstClusterHigh)<<16)+ fileEntry.FirstClusterLow==0){
return(1); /* Lie about cluster, 0 means not found! */
}else{
return
(
(((euint32 )fileEntry.FirstClusterHigh)<<16)
+ fileEntry.FirstClusterLow
);
}
}
}
}
return(0);
}
/* ****************************************************************************
* euint32 dir_findFreeEntryinBuf(euint8* buf, FileLocation *loc)
* This function searches for a free entry in a given sector 'buf'.
* It will put the offset into the loc->Offset field, given that loc is not 0.
* Return value: 1 when it found a free spot, 0 if it hasn't.
*/
euint32 dir_findFreeEntryinBuf(euint8* buf, FileLocation *loc)
{
FileRecord fileEntry;
euint8 c;
for(c=0;c<16;c++){
fileEntry = *(((FileRecord*)buf) + c);
if( !( (fileEntry.Attribute & 0x0F) == 0x0F ) ){
if(fileEntry.FileName[0] == 0x00 ||
fileEntry.FileName[0] == 0xE5 ){
if(loc)loc->Offset=c;
return(1);
}
}
}
return(0);
}
/* ****************************************************************************
* euint32 dir_findinBuf(euint8 *buf, eint8 *fatname, FileLocation *loc)
* Description: This function searches for a given fatfilename in a buffer.
* Return value: Returns 0 on not found, and the firstcluster when the name is found.
*/
euint32 dir_findinBuf(euint8 *buf, eint8 *fatname, FileLocation *loc, euint8 mode)
{
switch(mode){
case DIRFIND_FILE:
return(dir_findFileinBuf(buf,fatname,loc));
break;
case DIRFIND_FREE:
return(dir_findFreeEntryinBuf(buf,loc));
break;
default:
return(0);
break;
}
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 dir_findinCluster(FileSystem *fs,euint32 cluster,eint8 *fatname, FileLocation *loc, euint8 mode)
* This function will search for an existing (fatname) or free directory entry
* in a full cluster.
* Return value: 0 on failure, firstcluster on finding file, and 1 on finding free spot.
*/
euint32 dir_findinCluster(FileSystem *fs,euint32 cluster,eint8 *fatname, FileLocation *loc, euint8 mode)
{
euint8 c,*buf=0;
euint32 fclus;
for(c=0;c<fs->volumeId.SectorsPerCluster;c++){
buf = part_getSect(fs->part,fs_clusterToSector(fs,cluster)+c,IOM_MODE_READONLY);
if((fclus=dir_findinBuf(buf,fatname,loc,mode))){
if(loc)loc->Sector=fs_clusterToSector(fs,cluster)+c;
part_relSect(fs->part,buf);
return(fclus);
}
}
part_relSect(fs->part,buf);
return(0);
}
/* ****************************************************************************
* euint32 dir_findinDir(FileSystem *fs, eint8* fatname,euint32 firstcluster, FileLocation *loc, euint8 mode)
* This function will search for an existing (fatname) or free directory entry
* in a directory, following the clusterchains.
* Return value: 0 on failure, firstcluster on finding file, and 1 on finding free spot.
*/
euint32 dir_findinDir(FileSystem *fs, eint8* fatname,euint32 firstcluster, FileLocation *loc, euint8 mode)
{
euint32 c=0,cluster;
ClusterChain Cache;
Cache.DiscCluster = Cache.FirstCluster = firstcluster;
Cache.LogicCluster = Cache.LastCluster = Cache.Linear = 0;
if(firstcluster <= 1){
return(dir_findinRootArea(fs,fatname,loc,mode));
}
while(!fat_LogicToDiscCluster(fs,&Cache,c++)){
if((cluster=dir_findinCluster(fs,Cache.DiscCluster,fatname,loc,mode))){
return(cluster);
}
}
return(0);
}
/* ****************************************************************************
* euint32 dir_findinDir(FileSystem *fs, eint8* fatname,euint32 firstcluster, FileLocation *loc, euint8 mode)
* This function will search for an existing (fatname) or free directory entry
* in the rootdirectory-area of a FAT12/FAT16 filesystem.
* Return value: 0 on failure, firstcluster on finding file, and 1 on finding free spot.
*/
euint32 dir_findinRootArea(FileSystem *fs,eint8* fatname, FileLocation *loc, euint8 mode)
{
euint32 c,fclus;
euint8 *buf=0;
if((fs->type != FAT12) && (fs->type != FAT16))return(0);
for(c=fs->FirstSectorRootDir;c<(fs->FirstSectorRootDir+fs->volumeId.RootEntryCount/32);c++){
buf = part_getSect(fs->part,c,IOM_MODE_READONLY);
if((fclus=dir_findinBuf(buf,fatname,loc,mode))){
if(loc)loc->Sector=c;
part_relSect(fs->part,buf);
return(fclus);
}
part_relSect(fs->part,buf);
}
part_relSect(fs->part,buf);
return(0);
}
/* ****************************************************************************
* esint8 dir_getFatFileName(eint8* filename, eint8* fatfilename)
* This function will take a full directory path, and strip off all leading
* dirs and characters, leaving you with the MS-DOS notation of the actual filename.
* Return value: 1 on success, 0 on not being able to produca a filename
*/
esint8 dir_getFatFileName(eint8* filename, eint8* fatfilename)
{
eint8 ffnamec[11],*next,nn=0;
memClr(ffnamec,11); memClr(fatfilename,11);
next = filename;
if(*filename=='/')next++;
while((next=file_normalToFatName(next,ffnamec))){
memCpy(ffnamec,fatfilename,11);
nn++;
}
if(nn)return(1);
return(0);
}
/* ****************************************************************************
* esint8 dir_addCluster(FileSystem *fs,euint32 firstCluster)
* This function extends a directory by 1 cluster + optional the number of
* clusters you want pre-allocated. It will also delete the contents of that
* cluster. (or clusters)
* Return value: 0 on success, -1 on fail
*/
esint8 dir_addCluster(FileSystem *fs,euint32 firstCluster)
{
euint32 lastc,logicalc;
ClusterChain cache;
fs_initClusterChain(fs,&cache,firstCluster);
if(fat_allocClusterChain(fs,&cache,1)){
return(-1);
}
lastc = fs_getLastCluster(fs,&cache);
if(CLUSTER_PREALLOC_DIRECTORY){
if(fat_allocClusterChain(fs,&cache,CLUSTER_PREALLOC_DIRECTORY)){
return(-1);
}
logicalc = fat_DiscToLogicCluster(fs,firstCluster,lastc);
while(!fat_LogicToDiscCluster(fs,&cache,++logicalc)){
fs_clearCluster(fs,cache.DiscCluster);
}
}else{
fs_clearCluster(fs,lastc);
}
return(0);
}

View File

@ -0,0 +1,553 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : fat.c *
* Release : 0.3 - devel *
* Description : This file contains all the functions dealing with the FAT *
* in a Microsoft FAT filesystem. It belongs under fs.c *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "fs.h"
/*****************************************************************************/
/* ****************************************************************************
* unsigned long fat_getSectorAddressFatEntry(FileSystem *fs,unsigned long cluster_addr)
* Description: Returns the sectornumber that holds the fat entry for cluster cluster_addr.
* This works for all FAT types.
* Return value: Sectornumber, or 0. Warning, no boundary check.
*/
euint32 fat_getSectorAddressFatEntry(FileSystem *fs,euint32 cluster_addr)
{
euint32 base = fs->volumeId.ReservedSectorCount,res;
switch(fs->type){
case FAT12:
res=(cluster_addr*3/1024);
if(res>=fs->FatSectorCount){
return(0);
}else{
return(base+res);
}
break;
case FAT16:
res=cluster_addr/256;
if(res>=fs->FatSectorCount){
return(0);
}else{
return(base+res);
}
break;
case FAT32:
res=cluster_addr/128;
if(res>=fs->FatSectorCount){
return(0);
}else{
return(base+res);
}
break;
}
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* unsigned long fat_getNextClusterAddress(FileSystem *fs,unsigned long cluster_addr
* Description: This function loads the sector of the fat which contains the entry
* for cluster_addr. It then fetches and (if required) calculates it's value.
* This value is the EoC marker -or- the number of the next cluster in the chain.
* Return value: Clusternumber or EoC
*/
euint32 fat_getNextClusterAddress(FileSystem *fs,euint32 cluster_addr,euint16 *linear)
{
euint8 *buf;
euint8 hb,lb;
euint16 offset;
euint32 sector;
euint32 nextcluster=0;
sector=fat_getSectorAddressFatEntry(fs,cluster_addr);
if( (fs->FatSectorCount <= (sector-fs->volumeId.ReservedSectorCount)) || sector==0 )
{
return(0);
}
buf=part_getSect(fs->part,sector,IOM_MODE_READONLY);
switch(fs->type)
{
case FAT12:
offset = ((cluster_addr%1024)*3/2)%512;
hb = buf[offset];
if(offset == 511){
part_relSect(fs->part,buf);
buf=part_getSect(fs->part,sector+1,IOM_MODE_READONLY);
lb = buf[0];
}else{
lb = buf[offset + 1];
}
if(cluster_addr%2==0){
nextcluster = ( ((lb&0x0F)<<8) + (hb) );
}else{
nextcluster = ( (lb<<4) + (hb>>4) );
}
break;
case FAT16:
offset=cluster_addr%256;
nextcluster = *((euint16 *)buf + offset);
break;
case FAT32:
offset=cluster_addr%128;
nextcluster = *((euint32 *)buf + offset);
break;
}
part_relSect(fs->part,buf);
return(nextcluster);
}
/*****************************************************************************/
/* ****************************************************************************
* void fat_setNextClusterAddress(FileSystem *fs,unsigned long cluster_addr,unsigned long next_cluster_addr)
* Description: This function makes an entry in the fattable for cluster_addr. The value it puts there
* is next_cluster_addr.
*/
void fat_setNextClusterAddress(FileSystem *fs,euint32 cluster_addr,euint32 next_cluster_addr)
{
euint8 *buf,*buf2;
euint16 offset;
euint32 sector;
sector=fat_getSectorAddressFatEntry(fs,cluster_addr);
if(fs->FatSectorCount<sector){
DBG((TXT("HARDERROR:::fat_getNextClusterAddress READ PAST FAT BOUNDARY\n")));
return;
}
buf=part_getSect(fs->part,sector,IOM_MODE_READWRITE);
switch(fs->type){
case FAT12:
offset = ((cluster_addr%1024)*3/2)%512;
if(offset == 511){
if(cluster_addr%2==0){
buf[offset]=next_cluster_addr&0xFF;
}else{
buf[offset]=(buf[offset]&0xF)+((next_cluster_addr<<4)&0xF0);
}
buf2=part_getSect(fs->part,fat_getSectorAddressFatEntry(fs,cluster_addr)+1,IOM_MODE_READWRITE);
if(cluster_addr%2==0){
buf2[0]=(buf2[0]&0xF0)+((next_cluster_addr>>8)&0xF);
}else{
buf2[0]=(next_cluster_addr>>4)&0xFF;
}
part_relSect(fs->part,buf2);
}else{
if(cluster_addr%2==0){
buf[offset]=next_cluster_addr&0xFF;
buf[offset+1]=(buf[offset+1]&0xF0)+((next_cluster_addr>>8)&0xF);
}else{
buf[offset]=(buf[offset]&0xF)+((next_cluster_addr<<4)&0xF0);
buf[offset+1]=(next_cluster_addr>>4)&0xFF;
}
}
part_relSect(fs->part,buf);
break;
case FAT16:
offset=cluster_addr%256;
*((euint16*)buf+offset)=next_cluster_addr;
part_relSect(fs->part,buf);
break;
case FAT32:
offset=cluster_addr%128;
*((euint32*)buf+offset)=next_cluster_addr;
part_relSect(fs->part,buf);
break;
}
}
/*****************************************************************************/
/* ****************************************************************************
* short fat_isEocMarker(FileSystem *fs,unsigned long fat_entry)
* Description: Checks if a certain value is the EoC marker for the filesystem
* noted in fs->type.
* Return value: Returns 0 when it is the EoC marker, and 1 otherwise.
*/
eint16 fat_isEocMarker(FileSystem *fs,euint32 fat_entry)
{
switch(fs->type){
case FAT12:
if(fat_entry<0xFF8){
return(0);
}
break;
case FAT16:
if(fat_entry<0xFFF8){
return(0);
}
break;
case FAT32:
if((fat_entry&0x0FFFFFFF)<0xFFFFFF8){
return(0);
}
break;
}
return(1);
}
/*****************************************************************************/
/* ****************************************************************************
* unsigned long fat_giveEocMarker(FileSystem *fs)
* Description: Returns an EoC markernumber valid for the filesystem noted in
* fs->type.
* Note, for FAT32, the upper 4 bits are set to zero, although they should be un
* touched according to MicroSoft specifications. I didn't care.
* Return value: The EoC marker cast to an ulong.
*/
euint32 fat_giveEocMarker(FileSystem *fs)
{
switch(fs->type)
{
case FAT12:
return(0xFFF);
break;
case FAT16:
return(0xFFFF);
break;
case FAT32:
return(0x0FFFFFFF);
break;
}
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 fat_getNextClusterAddressWBuf(FileSystem *fs,euint32 cluster_addr, euint8* buf)
* Description: This function retrieves the contents of a FAT field. It does not fetch
* it's own buffer, it is given as a parameter. (ioman makes this function rather obsolete)
* Only in the case of a FAT12 crosssector data entry a sector is retrieved here.
* Return value: The value of the clusterfield is returned.
*/
euint32 fat_getNextClusterAddressWBuf(FileSystem *fs,euint32 cluster_addr, euint8* buf)
{
euint8 *buf2; /* For FAT12 fallover only */
euint8 hb,lb;
euint16 offset;
euint32 nextcluster=0;
switch(fs->type)
{
case FAT12:
offset = ((cluster_addr%1024)*3/2)%512;
hb = buf[offset];
if(offset == 511){
buf2=part_getSect(fs->part,fat_getSectorAddressFatEntry(fs,cluster_addr)+1,IOM_MODE_READONLY);
lb = buf2[0];
part_relSect(fs->part,buf2);
}else{
lb = buf[offset + 1];
}
if(cluster_addr%2==0){
nextcluster = ( ((lb&0x0F)<<8) + (hb) );
}else{
nextcluster = ( (lb<<4) + (hb>>4) );
}
break;
case FAT16:
offset=cluster_addr%256;
nextcluster = *((euint16*)buf + offset);
break;
case FAT32:
offset=cluster_addr%128;
nextcluster = *((euint32*)buf + offset);
break;
}
return(nextcluster);
}
/*****************************************************************************/
/* ****************************************************************************
* void fat_setNextClusterAddressWBuf(FileSystem *fs,euint32 cluster_addr,euint32 next_cluster_addr,euint8* buf)
* Description: This function fills in a fat entry. The entry is cluster_addr and the
* data entered is next_cluster_addr. This function is also given a *buf, so it does
* not write the data itself, except in the case of FAT 12 cross sector data, where
* the second sector is handled by this function.
* Return value:
*/
void fat_setNextClusterAddressWBuf(FileSystem *fs,euint32 cluster_addr,euint32 next_cluster_addr,euint8* buf)
{
euint16 offset;
euint8 *buf2;
switch(fs->type)
{
case FAT12:
offset = ((cluster_addr%1024)*3/2)%512;
if(offset == 511){
if(cluster_addr%2==0){
buf[offset]=next_cluster_addr&0xFF;
}else{
buf[offset]=(buf[offset]&0xF)+((next_cluster_addr<<4)&0xF0);
}
buf2=part_getSect(fs->part,fat_getSectorAddressFatEntry(fs,cluster_addr)+1,IOM_MODE_READWRITE);
if(cluster_addr%2==0){
buf2[0]=(buf2[0]&0xF0)+((next_cluster_addr>>8)&0xF);
}else{
buf2[0]=(next_cluster_addr>>4)&0xFF;
}
part_relSect(fs->part,buf2);
}else{
if(cluster_addr%2==0){
buf[offset]=next_cluster_addr&0xFF;
buf[offset+1]=(buf[offset+1]&0xF0)+((next_cluster_addr>>8)&0xF);
}else{
buf[offset]=(buf[offset]&0xF)+((next_cluster_addr<<4)&0xF0);
buf[offset+1]=(next_cluster_addr>>4)&0xFF;
}
}
break;
case FAT16:
offset=cluster_addr%256;
*((euint16*)buf+offset)=next_cluster_addr;
break;
case FAT32:
offset=cluster_addr%128;
*((euint32*)buf+offset)=next_cluster_addr;
break;
}
}
/*****************************************************************************/
/* ****************************************************************************
* esint16 fat_getNextClusterChain(FileSystem *fs, ClusterChain *Cache)
* Description: This function is to advance the clusterchain of a Cache.
* First, the function verifies if the Cache is valid. It could correct it if it
* is not, but this is not done at the time. If the cachen is valid, the next step is
* to see what the next cluster is, if this is the End of Clustermark, the cache is
* updated to know the lastcluster but will remain untouched otherwise. -1 is returned.
* If there are more clusters the function scans the rest of the chain until the next
* cluster is no longer lineair, or until it has run out of fat data (only 1 sector) is
* examined, namely the one fetched to check for EoC.
* With lineair is meant that logical cluster n+1 should be 1 more than logical cluster n
* at the disc level.
* Return value: 0 on success, or -1 when EoC.
*/
esint16 fat_getNextClusterChain(FileSystem *fs, ClusterChain *Cache)
{
euint32 sect,lr,nlr,dc;
esint16 lin=0;
euint8 *buf;
if(Cache->DiscCluster==0)
{
return(-1);
}
sect=fat_getSectorAddressFatEntry(fs,Cache->DiscCluster);
buf=part_getSect(fs->part,sect,IOM_MODE_READONLY);
dc=fat_getNextClusterAddressWBuf(fs,Cache->DiscCluster,buf);
if(fat_isEocMarker(fs,dc))
{
Cache->LastCluster=Cache->DiscCluster;
part_relSect(fs->part,buf);
return(-1);
}
Cache->DiscCluster=dc;
Cache->LogicCluster++;
lr=Cache->DiscCluster-1;
nlr=lr+1;
while(nlr-1==lr && fat_getSectorAddressFatEntry(fs,nlr)==sect)
{
lr=nlr;
nlr=fat_getNextClusterAddressWBuf(fs,lr,buf);
lin++;
}
Cache->Linear=lin-1<0?0:lin-1;
part_relSect(fs->part,buf);
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* esint16 fat_LogicToDiscCluster(FileSystem *fs, ClusterChain *Cache,euint32 logiccluster)
* Description: This function is used to follow clusterchains. When called it will convert
* a logical cluster, to a disc cluster, using a Cache object. All it does is call
* getNextClusterChain in the proper manner, and rewind clusterchains if required.
* It is NOT recommended to go backwards in clusterchains, since this will require
* scanning the entire chain every time.
* Return value: 0 on success and -1 on failure (meaning out of bounds).
*/
esint16 fat_LogicToDiscCluster(FileSystem *fs, ClusterChain *Cache,euint32 logiccluster)
{
if(logiccluster<Cache->LogicCluster || Cache->DiscCluster==0){
Cache->LogicCluster=0;
Cache->DiscCluster=Cache->FirstCluster;
Cache->Linear=0;
}
if(Cache->LogicCluster==logiccluster){
return(0);
}
while(Cache->LogicCluster!=logiccluster)
{
if(Cache->Linear!=0)
{
Cache->Linear--;
Cache->LogicCluster++;
Cache->DiscCluster++;
}
else
{
if((fat_getNextClusterChain(fs,Cache))!=0){
return(-1);
}
}
}
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* eint16 fat_allocClusterChain(FileSystem *fs,ClusterChain *Cache,euint32 num_clusters)
* Description: This function extends a clusterchain by num_clusters. It returns the
* number of clusters it *failed* to allocate.
* Return value: 0 on success, all other values are the number of clusters it could
* not allocate.
*/
eint16 fat_allocClusterChain(FileSystem *fs,ClusterChain *Cache,euint32 num_clusters)
{
euint32 cc,ncl=num_clusters,lc;
euint8 *bufa=0,*bufb=0;
euint8 overflow=0;
if(Cache->FirstCluster<=1)return(num_clusters);
lc=fs_getLastCluster(fs,Cache);
cc=lc;
while(ncl > 0){
cc++;
if(cc>=fs->DataClusterCount+1){
if(overflow){
bufa=part_getSect(fs->part,fat_getSectorAddressFatEntry(fs,lc),IOM_MODE_READWRITE);
fat_setNextClusterAddressWBuf(fs,lc,fat_giveEocMarker(fs),bufa);
Cache->LastCluster=lc;
part_relSect(fs->part,bufa);
return(num_clusters-ncl);
}
cc=2;
overflow++;
}
bufa=part_getSect(fs->part,fat_getSectorAddressFatEntry(fs,cc),IOM_MODE_READONLY);
if(fat_getNextClusterAddressWBuf(fs,cc,bufa)==0){
bufb=part_getSect(fs->part,fat_getSectorAddressFatEntry(fs,lc),IOM_MODE_READWRITE);
fat_setNextClusterAddressWBuf(fs,lc,cc,bufb);
part_relSect(fs->part,bufb);
ncl--;
lc=cc;
}
part_relSect(fs->part,bufa);
if(ncl==0){
bufa=part_getSect(fs->part,fat_getSectorAddressFatEntry(fs,lc),IOM_MODE_READWRITE);
fat_setNextClusterAddressWBuf(fs,lc,fat_giveEocMarker(fs),bufa);
Cache->LastCluster=lc;
part_relSect(fs->part,bufa);
}
}
return(0);
}
/* ****************************************************************************
* eint16 fat_unlinkClusterChain(FileSystem *fs,ClusterChain *Cache)
* Description: This function removes a clusterchain. Starting at FirstCluster
* it follows the chain until the end, resetting all values to 0.
* Return value: 0 on success.
*/
eint16 fat_unlinkClusterChain(FileSystem *fs,ClusterChain *Cache)
{
euint32 c,tbd=0;
Cache->LogicCluster=0;
Cache->DiscCluster=Cache->FirstCluster;
c=0;
while(!fat_LogicToDiscCluster(fs,Cache,c++)){
if(tbd!=0){
fat_setNextClusterAddress(fs,tbd,0);
}
tbd=Cache->DiscCluster;
}
fat_setNextClusterAddress(fs,Cache->DiscCluster,0);
return(0);
}
euint32 fat_countClustersInChain(FileSystem *fs,euint32 firstcluster)
{
ClusterChain cache;
euint32 c=0;
if(firstcluster<=1)return(0);
cache.DiscCluster = cache.LogicCluster = cache.LastCluster = cache.Linear = 0;
cache.FirstCluster = firstcluster;
while(!(fat_LogicToDiscCluster(fs,&cache,c++)));
return(c-1);
}
euint32 fat_DiscToLogicCluster(FileSystem *fs,euint32 firstcluster,euint32 disccluster)
{
ClusterChain cache;
euint32 c=0,r=0;
cache.DiscCluster = cache.LogicCluster = cache.LastCluster = cache.Linear = 0;
cache.FirstCluster = firstcluster;
while(!(fat_LogicToDiscCluster(fs,&cache,c++)) && !r){
if(cache.DiscCluster == disccluster){
r = cache.LogicCluster;
}
}
return(r);
}

View File

@ -0,0 +1,497 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : file.c *
* Release : 0.3 - devel *
* Description : This file contains functions dealing with files such as: *
* fopen, fread and fwrite. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "file.h"
/*****************************************************************************/
/* ****************************************************************************
* euint32 file_fread(File *file,euint32 offset, euint32 size,euint8 *buf)
* Description: This function reads 'size' bytes from 'file' starting at
* 'offset' and puts the result in '*buf'.
* Return value: amount of bytes actually read (can differ from the given
* size when the file was smaller
*/
euint32 file_fread(File *file,euint32 offset, euint32 size,euint8 *buf)
{
euint32 bytes_read=0,size_left=size,coffset=offset;
euint32 cclus,csec,cbyte;
euint32 rclus,rsec;
euint32 btr;
euint8 *tbuf;
if(!file_getAttr(file,FILE_STATUS_OPEN))return(0);
if(offset>=file->FileSize)
size_left=0; /* Offset check */
if( (offset+size > file->FileSize) && size_left!=0)
size_left=file->FileSize-offset;
while(size_left>0){
cclus = coffset/(512*file->fs->volumeId.SectorsPerCluster);
csec = (coffset/(512))%file->fs->volumeId.SectorsPerCluster;
cbyte = coffset%512;
if(cbyte!=0 || size_left<512){
btr = 512-(coffset%512)>=size_left?size_left:512-(coffset%512);
}else{
btr = 512;
}
if((fat_LogicToDiscCluster(file->fs,&(file->Cache),cclus))!=0){
return(0);
}
rclus=file->Cache.DiscCluster;
rsec=fs_clusterToSector(file->fs,rclus);
if(btr==512){
/*part_readBuf(file->fs->part,rsec+csec,buf+bytes_read);*/
part_directSectorRead(file->fs->part,rsec+csec,buf+bytes_read);
}else{
/*part_readBuf(file->fs->part,rsec+csec,tbuf);*/
tbuf = part_getSect(file->fs->part,rsec+csec,IOM_MODE_READONLY);
memCpy(tbuf+(coffset%512),buf+bytes_read,btr);
part_relSect(file->fs->part,tbuf);
}
coffset+=btr;
bytes_read+=btr;
size_left-=btr;
}
return(bytes_read);
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 file_read (File *file,euint32 size,euint8 *buf)
* Description: This function reads from a file, taking the FilePtr into account
* and advancing it according to the freadcall.
* Return value: Value obtained from fread
*/
euint32 file_read(File *file,euint32 size,euint8 *buf)
{
euint32 r;
r=file_fread(file,file->FilePtr,size,buf);
file->FilePtr+=r;
return(r);
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 file_write(File *file, euint32 size,euint8 *buf)
* Description: This function writes to a file, taking FilePtr into account
* and advancing it according to the fwritecall.
* Return value: Value obtained from fread
*/
euint32 file_write(File *file, euint32 size,euint8 *buf)
{
euint32 r;
r=file_fwrite(file,file->FilePtr,size,buf);
file->FilePtr+=r;
return(r);
}
/*****************************************************************************/
/* ****************************************************************************
* esint16 file_setpos(File *file,euint32 pos)
* Description: This function does a sanity check on the requested position
* and changes the fileptr accordingly.
* Return value: 0 on success and -1 on failure.
*/
esint16 file_setpos(File *file,euint32 pos)
{
if(pos<=file->FileSize){
file->FilePtr=pos;
return(0);
}
return(-1);
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 file_fwrite(File* file,euint32 offset,euint32 size,euint8* buf)
* Description: This function writes to a file, at offset 'offset' and size 'size'.
* It also updates the FileSize in the object, and discstructure.
* Return value: Bytes actually written.
*/
euint32 file_fwrite(File* file,euint32 offset,euint32 size,euint8* buf)
{
euint32 need_cluster;
euint32 cclus,csec,cbyte;
euint32 size_left=size,bytes_written=0;
euint32 rclus,rsec;
euint32 coffset=offset;
euint16 btr;
euint8 *tbuf;
if(!file_getAttr(file,FILE_STATUS_OPEN) || !file_getAttr(file,FILE_STATUS_WRITE))return(0);
if(offset>file->FileSize){
offset=file->FileSize;
}
need_cluster = file_requiredCluster(file,offset,size);
if(need_cluster){
if(fat_allocClusterChain(file->fs,&(file->Cache),need_cluster+CLUSTER_PREALLOC_FILE)!=0){
return(0);
}
}
while(size_left>0){
cclus = coffset/(512*file->fs->volumeId.SectorsPerCluster);
csec = (coffset/(512))%file->fs->volumeId.SectorsPerCluster;
cbyte = coffset%512;
if(cbyte!=0 || size_left<512){
btr = 512-(coffset%512)>=size_left?size_left:512-(coffset%512);
}else{
btr = 512;
}
if((fat_LogicToDiscCluster(file->fs,&(file->Cache),cclus))!=0){
file->FileSize+=bytes_written;
dir_setFileSize(file->fs,&(file->Location),file->FileSize);
return(bytes_written);
}
rclus=file->Cache.DiscCluster;
rsec=fs_clusterToSector(file->fs,rclus);
if(btr==512){
/*part_writeBuf(file->fs->part,rsec+csec,buf+bytes_written);*/
part_directSectorWrite(file->fs->part,rsec+csec,buf+bytes_written);
}else{
/*part_readBuf(file->fs->part,rsec+csec,tbuf);*/
tbuf = part_getSect(file->fs->part,rsec+csec,IOM_MODE_READWRITE);
memCpy(buf+bytes_written,tbuf+(coffset%512),btr);
/*part_writeBuf(file->fs->part,rsec+csec,tbuf);*/
part_relSect(file->fs->part,tbuf);
}
coffset+=btr;
bytes_written+=btr;
size_left-=btr;
}
if(bytes_written>file->FileSize-offset){
file->FileSize+=bytes_written-(file->FileSize-offset);
}
return(bytes_written);
}
/* ***************************************************************************\
* signed eint8 file_fopen(FileSystem *fs,File* file,eint8* filename)
* Description: This functions opens a file.
* This function is about to be redesigned. No Docs.
* Return value:
*/
esint8 file_fopen(File* file,FileSystem *fs,eint8* filename,eint8 mode)
{
FileLocation loc;
FileRecord wtmp;
eint8 fatfilename[11];
euint32 sec;
dir_getFatFileName(filename,fatfilename);
switch(mode)
{
case MODE_READ:
if(fs_findFile(fs,filename,&loc,0)==1)
{
dir_getFileStructure(fs,&(file->DirEntry), &loc);
file_initFile(file,fs,&loc);
file_setAttr(file,FILE_STATUS_OPEN,1);
file_setAttr(file,FILE_STATUS_WRITE,0);
return(0);
}
return(-1);
break;
case MODE_WRITE:
if(fs_findFile(fs,filename,&loc,0))
{
return(-2);
}
if(fs_findFreeFile(fs,filename,&loc,0))
{
dir_createDefaultEntry(fs,&wtmp,fatfilename);
dir_createDirectoryEntry(fs,&wtmp,&loc);
memCpy(&wtmp,&(file->DirEntry),sizeof(wtmp));
file_initFile(file,fs,&loc);
sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs));
dir_setFirstCluster(file->fs,&(file->Location),sec);
fs_setFirstClusterInDirEntry(&(file->DirEntry),sec);
fs_initClusterChain(fs,&(file->Cache),sec);
fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs));
file_setAttr(file,FILE_STATUS_OPEN,1);
file_setAttr(file,FILE_STATUS_WRITE,1);
return(0);
}
else
{
return(-3);
}
break;
case MODE_APPEND:
if(fs_findFile(fs,filename,&loc,0)==1) /* File exists */
{
dir_getFileStructure(fs,&(file->DirEntry), &loc);
file_initFile(file,fs,&loc);
if(file->Cache.FirstCluster==0){
sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs));
dir_setFirstCluster(file->fs,&(file->Location),sec);
fs_setFirstClusterInDirEntry(&(file->DirEntry),sec);
fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs));
file_initFile(file,fs,&loc);
}
file_setpos(file,file->FileSize);
file_setAttr(file,FILE_STATUS_OPEN,1);
file_setAttr(file,FILE_STATUS_WRITE,1);
}
else /* File does not excist */
{
if(fs_findFreeFile(fs,filename,&loc,0))
{
dir_createDefaultEntry(fs,&wtmp,fatfilename);
dir_createDirectoryEntry(fs,&wtmp,&loc);
memCpy(&wtmp,&(file->DirEntry),sizeof(wtmp));
file_initFile(file,fs,&loc);
sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs));
dir_setFirstCluster(file->fs,&(file->Location),sec);
fs_setFirstClusterInDirEntry(&(file->DirEntry),sec);
fs_initClusterChain(fs,&(file->Cache),sec);
fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs));
file_setAttr(file,FILE_STATUS_OPEN,1);
file_setAttr(file,FILE_STATUS_WRITE,1);
}
else
{
return(-3);
}
}
return(0);
break;
default:
return(-4);
break;
}
return(-5);
}
/*****************************************************************************/
/* ****************************************************************************
* esint8 file_fclose(File *file)
* Description: This function closes a file, by clearing the object.
* Return value: 0 on success.
*/
esint8 file_fclose(File *file)
{
if(fs_hasTimeSupport()){
file->DirEntry.AccessDate = time_getDate();
if(file_getAttr(file,FILE_STATUS_WRITE)){
file->DirEntry.FileSize = file->FileSize;
file->DirEntry.WriteDate = file->DirEntry.AccessDate;
file->DirEntry.WriteTime = time_getTime();
}
dir_updateDirectoryEntry(file->fs,&(file->DirEntry),&(file->Location));
}else{
if(file_getAttr(file,FILE_STATUS_WRITE)){
dir_setFileSize(file->fs,&(file->Location),file->FileSize);
}
}
memClr(file,sizeof(*file));
file_setAttr(file,FILE_STATUS_OPEN,0);
file_setAttr(file,FILE_STATUS_WRITE,0);
return(0);
}
/* ****************************************************************************
* void file_initFile(File *file, FileSystem *fs, FileLocation *loc)
* Description: This function initialises a new file object, by filling in
* the fs pointer, filesize (note, that DirEntry must already be filled in)
* and known cache parameters.
* Return value: void
*/
void file_initFile(File *file, FileSystem *fs, FileLocation *loc)
{
file->fs=fs;
file->FileSize=file->DirEntry.FileSize;
file->FilePtr=0;
file->Location.Sector=loc->Sector;
file->Location.Offset=loc->Offset;
file->Cache.Linear=0;
file->Cache.FirstCluster=(((euint32)file->DirEntry.FirstClusterHigh)<<16)+
file->DirEntry.FirstClusterLow;
file->Cache.LastCluster=0;
file->Cache.LogicCluster=0;
file->Cache.DiscCluster=file->Cache.FirstCluster;
}
/*****************************************************************************/
/* ****************************************************************************
* euint8* file_normalToFatName(eint8* filename,eint8* fatfilename)
* Description: This function converts a human readable filename (limited to
* 8.3 eint8 character) to a valid FAT (not VFAT) filename. Invalid characters are
* changed to capital X and only the first 11 characters are used.
* Furthermore all letters are capitalised.
* Return value: pointer after the filename
*/
eint8* file_normalToFatName(eint8* filename,eint8* fatfilename)
{
euint8 c,dot=0,vc=0;
for(c=0;c<11;c++)fatfilename[c]=' ';
c=0;
if(*filename == '.'){
fatfilename[0]='.';
vc++;
if(*(filename+1) == '.'){
fatfilename[1]='.';
filename+=2;
}else{
filename++;
}
}else{
while(*filename != '\0' && *filename != ' ' && *filename != '/'){
if(*filename=='.' && !dot){
dot=1;
c=8;
}else{
if(dot){
if(c<=10){
fatfilename[c]=file_validateChar(*filename);
c++;
}
}else{
if(c<=7){
fatfilename[c]=file_validateChar(*filename);
c++; vc++;
}
}
}
filename++;
}
}
if(vc>0){
if(*filename=='\0'){
return(filename);
}else{
return(filename+1);
}
}else{
return(0);
}
}
/*****************************************************************************/
/* ****************************************************************************
*
* Description: This function takes the character c, and if it is not a *
* valid FAT Filename character returns X. If it is a lowercase letter the *
* uppercase equivalent is returned. The remaining characters are returned *
* as they are.
* Return value: The validated char
*/
euint8 file_validateChar(euint8 c)
{
if( (c<0x20) || (c>0x20&&c<0x30&&c!='-') || (c>0x39&&c<0x41) || (c>0x5A&&c<0x61&&c!='_') || (c>0x7A&&c!='~') )
return(0x58);
if( c>=0x61 && c<=0x7A )
return(c-32);
return(c);
}
/*****************************************************************************/
/* ****************************************************************************
* void ioman_setAttr(IOManager *ioman,euint16 bufplace,euint8 attribute,euint8 val)
* Description: This sets the attribute of 'bufplace' to the given value (binary).
*
* Return value: void
*/
void file_setAttr(File* file,euint8 attribute,euint8 val)
{
if(val){
file->FileStatus|=1<<attribute;
}else{
file->FileStatus&=~(1<<attribute);
}
}
/*****************************************************************************/
/* ****************************************************************************
* euint8 ioman_getAttr(IOManager *ioman,euint16 bufplace,euint8 attribute)
* Description: This function retrieves an attribute from the bufstat array.
* It returns nonzero when it attribute is true and 0 when it is false.
* Please note, I said "nonzero", not 1.
* Return value: Attribute.
*/
euint8 file_getAttr(File* file,euint8 attribute)
{
return(file->FileStatus&(1<<attribute));
}
/*****************************************************************************/
euint32 file_requiredCluster(File *file,euint32 offset, euint32 size)
{
euint32 clusters_required,clustersize;
euint32 hc;
if((offset+size)>file->FileSize){
hc = fat_countClustersInChain(file->fs,file->Cache.FirstCluster);
clustersize = file->fs->volumeId.BytesPerSector * file->fs->volumeId.SectorsPerCluster;
if((size-file->FileSize+offset)>
((hc-((file->FileSize+clustersize-1)/clustersize))*clustersize)){
clusters_required = (((offset+size)-(hc*clustersize))+clustersize-1)/clustersize;
}else{
clusters_required = 0;
}
}else{
clusters_required = 0;
}
return(clusters_required);
}

View File

@ -0,0 +1,512 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : fs.c *
* Release : 0.3 - devel *
* Description : These are general filesystem functions, supported by the *
* functions of dir.c and fat.c file.c uses these functions *
* heavily, but is not used by fs.c (not true anymore) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "fs.h"
#include "fat.h"
#include "dir.h"
/*****************************************************************************/
/* ****************************************************************************
* eint16 fs_initFs(FileSystem *fs,Partition *part)
* Description: This functions glues the initialisation of the filesystem together.
* It loads the volumeID, computes the FS type and searches for the rootsector.
* Return value: Returns 0 on succes and -1 on error (if magic code is wrong)
*/
eint16 fs_initFs(FileSystem *fs,Partition *part)
{
if(!fs_isValidFat(part)){
return(-1);
}
fs->part=part;
fs_loadVolumeId(fs,part);
if(!fs_verifySanity(fs))return(-2);
fs_countDataSectors(fs);
fs_determineFatType(fs);
fs_findFirstSectorRootDir(fs);
fs_initCurrentDir(fs);
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* eint16 fs_isValidFat(Partition *part)
* Description: This functions loads the volumeID and checks if the magic
* value is present.
* Return value: returns 0 when magic code is missing, 1 if it is there.
*/
eint16 fs_isValidFat(Partition *part)
{
euint8 *buf;
buf=part_getSect(part,0,IOM_MODE_READONLY|IOM_MODE_EXP_REQ); /* Load Volume label */
if( ex_getb16(buf+0x1FE) != 0xAA55 ){
return (0);
}
part_relSect(part,buf);
return(1);
}
/*****************************************************************************/
/* ****************************************************************************
* void fs_loadVolumeId(FileSystem *fs, Partition *part)
* Description: This function loads all relevant fields from the volumeid.
*/
void fs_loadVolumeId(FileSystem *fs, Partition *part)
{
euint8 *buf;
buf=part_getSect(part,0,IOM_MODE_READONLY|IOM_MODE_EXP_REQ);
fs->volumeId.BytesPerSector=ex_getb16(buf+0x0B);
fs->volumeId.SectorsPerCluster=*((eint8*)(buf+0x0D));
fs->volumeId.ReservedSectorCount=ex_getb16(buf+0x0E);
fs->volumeId.NumberOfFats=*((eint8*)(buf+0x10));
fs->volumeId.RootEntryCount=ex_getb16(buf+0x11);
fs->volumeId.SectorCount16=ex_getb16(buf+0x13);
fs->volumeId.FatSectorCount16=ex_getb16(buf+0x16);
fs->volumeId.SectorCount32=ex_getb32(buf+0x20);
fs->volumeId.FatSectorCount32=ex_getb32(buf+0x24);
fs->volumeId.RootCluster=ex_getb32(buf+0x2C);
part_relSect(part,buf);
}
/*****************************************************************************/
/* ****************************************************************************
* esint16 fs_verifySanity(FileSystem *fs)
* Description: Does some sanity calculations.
* Return value: 1 on success, 0 when discrepancies were found.
*/
esint16 fs_verifySanity(FileSystem *fs)
{
esint16 sane=1; /* Sane until proven otherwise */
/* First check, BPS, we only support 512 */
if(fs->volumeId.BytesPerSector!=512)sane=0;
/* Check is SPC is valid (multiple of 2, and clustersize >=32KB */
if(!((fs->volumeId.SectorsPerCluster == 1 ) |
(fs->volumeId.SectorsPerCluster == 2 ) |
(fs->volumeId.SectorsPerCluster == 4 ) |
(fs->volumeId.SectorsPerCluster == 8 ) |
(fs->volumeId.SectorsPerCluster == 16) |
(fs->volumeId.SectorsPerCluster == 32) |
(fs->volumeId.SectorsPerCluster == 64) ))sane=0;
/* Any number of FAT's should be supported... (untested) */
/* There should be at least 1 reserved sector */
if(fs->volumeId.ReservedSectorCount==0)sane=0;
/* Can no longer do the check for filesystem size until partition supports this */
/* if(fs->volumeId.FatSectorCount16 != 0){
if(fs->volumeId.FatSectorCount16 > fs->part->disc->partitions[fs->part->activePartition].numSectors)sane=0;
}else{
if(fs->volumeId.FatSectorCount32 > fs->part->disc->partitions[fs->part->activePartition].numSectors)sane=0;
} */
return(sane);
}
/*****************************************************************************/
/* ****************************************************************************
* void fs_countDataSectors(FileSystem *fs)
* Description: This functions calculates the sectorcounts, fatsectorcounts and
* dataclustercounts. It fills in the general fields.
*/
void fs_countDataSectors(FileSystem *fs)
{
euint32 rootDirSectors,dataSectorCount;
rootDirSectors=((fs->volumeId.RootEntryCount*32) +
(fs->volumeId.BytesPerSector - 1)) /
fs->volumeId.BytesPerSector;
if(fs->volumeId.FatSectorCount16 != 0)
{
fs->FatSectorCount=fs->volumeId.FatSectorCount16;
fs->volumeId.FatSectorCount32=0;
}
else
{
fs->FatSectorCount=fs->volumeId.FatSectorCount32;
fs->volumeId.FatSectorCount16=0;
}
if(fs->volumeId.SectorCount16!=0)
{
fs->SectorCount=fs->volumeId.SectorCount16;
fs->volumeId.SectorCount32=0;
}
else
{
fs->SectorCount=fs->volumeId.SectorCount32;
fs->volumeId.SectorCount16=0;
}
dataSectorCount=fs->SectorCount - (
fs->volumeId.ReservedSectorCount +
(fs->volumeId.NumberOfFats * fs->FatSectorCount) +
rootDirSectors);
fs->DataClusterCount=dataSectorCount/fs->volumeId.SectorsPerCluster;
}
/*****************************************************************************/
/* ****************************************************************************
* void fs_determineFatType(FileSystem *fs)
* Description: This function looks af the Dataclustercount and determines the
* FAT type. It fills in fs->type.
*/
void fs_determineFatType(FileSystem *fs)
{
if(fs->DataClusterCount < 4085)
{
fs->type=FAT12;
fs->volumeId.RootCluster=0;
}
else if(fs->DataClusterCount < 65525)
{
fs->type=FAT16;
fs->volumeId.RootCluster=0;
}
else
{
fs->type=FAT32;
}
}
/*****************************************************************************/
/* ****************************************************************************
* void fs_findFirstSectorRootDir(FileSystem *fs)
* Description: This functions fills in the fs->FirstSectorRootDir field, even
* for FAT32, although that is not necessary (because you have FirstClusterRootDir).
*/
void fs_findFirstSectorRootDir(FileSystem *fs)
{
if(fs->type==FAT32)
fs->FirstSectorRootDir = fs->volumeId.ReservedSectorCount +
(fs->volumeId.NumberOfFats*fs->volumeId.FatSectorCount32) +
(fs->volumeId.RootCluster-2)*fs->volumeId.SectorsPerCluster;
else
fs->FirstSectorRootDir = fs->volumeId.ReservedSectorCount +
(fs->volumeId.NumberOfFats*fs->volumeId.FatSectorCount16);
}
/*****************************************************************************/
void fs_initCurrentDir(FileSystem *fs)
{
fs->FirstClusterCurrentDir = fs_getFirstClusterRootDir(fs);
}
/*****************************************************************************/
/* ****************************************************************************
* long fs_clusterToSector(FileSystem *fs,euint32 cluster)
* Description: This function converts a clusternumber in the effective sector
* number where this cluster starts. Boundary check is not implemented
* Return value: A long is returned representing the sectornumber.
*/
euint32 fs_clusterToSector(FileSystem *fs,euint32 cluster)
{
eint32 base;
if(fs->type==FAT32)
{
base=
fs->volumeId.ReservedSectorCount+
fs->FatSectorCount*fs->volumeId.NumberOfFats;
}
else
{
base=
fs->volumeId.ReservedSectorCount+
fs->FatSectorCount*fs->volumeId.NumberOfFats+
fs->volumeId.RootEntryCount/16;
}
return( base + (cluster-2)*fs->volumeId.SectorsPerCluster );
}
/*****************************************************************************/
/* Function is unused, but may be usefull */
euint32 fs_sectorToCluster(FileSystem *fs,euint32 sector)
{
eint32 base;
if(fs->type==FAT32)
{
base=
fs->volumeId.ReservedSectorCount+
fs->FatSectorCount*fs->volumeId.NumberOfFats;
}
else
{
base=
fs->volumeId.ReservedSectorCount+
fs->FatSectorCount*fs->volumeId.NumberOfFats+
fs->volumeId.RootEntryCount/16;
}
return(((sector-base)-((sector-base)%fs->volumeId.SectorsPerCluster))/fs->volumeId.SectorsPerCluster+2 );
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 fs_getNextFreeCluster(FileSystem *fs,euint32 startingcluster)
* Description: This functions searches for a free cluster, starting it's search at
* cluster startingcluster. This allow to speed up searches and try to avoid
* fragmentation. Implementing rollover search is still to be done.
* Return value: If a free cluster is found it's number is returned. If none is
* found 0 is returned.
*/
euint32 fs_getNextFreeCluster(FileSystem *fs,euint32 startingcluster)
{
euint32 r;
while(startingcluster<fs->DataClusterCount){
r=fat_getNextClusterAddress(fs,startingcluster,0);
if(r==0){
return(startingcluster);
}
startingcluster++;
}
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 fs_giveFreeClusterHint(FileSystem *fs)
*
* Description: This function should return a clusternumber that is free or
* lies close before free clusters. The result MUST be checked to see if
* it is free! Implementationhint: search the largest clusternumber in the
* files in the rootdirectory.
*
* Return value: Returns it's best guess.
*/
euint32 fs_giveFreeClusterHint(FileSystem *fs)
{
return(2); /* Now THIS is a hint ;) */
}
/*****************************************************************************/
/* ****************************************************************************
* esint8 fs_findFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint32 *lastDir)
*
* Description: This function looks if the given filename is on the given fs
* and, if found, fills in its location in loc.
* The function will first check if the pathname starts with a slash. If so it will
* set the starting directory to the rootdirectory. Else, it will take the firstcluster-
* currentdir (That you can change with chdir()) as startingpoint.
* The lastdir pointer will be the first cluster of the last directory fs_findfile
* enters. It starts out at the root/current dir and then traverses the path along with
* fs_findFile.
* It is set to 0 in case of errors (like dir/dir/dir/file/dir/dir...)
* Return value: Returns 0 when nothing was found, 1 when the thing found
* was a file and 2 if the thing found was a directory.
*/
esint8 fs_findFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint32 *lastDir)
{
euint32 fccd,tmpclus;
eint8 ffname[11],*next,it=0,filefound=0;
if(*filename=='/'){
fccd = fs_getFirstClusterRootDir(fs);
filename++;
if(!*filename){
if(lastDir)*lastDir=fccd;
return(2);
}
}else{
fccd = fs->FirstClusterCurrentDir;
}
if(lastDir)*lastDir=fccd;
while((next=file_normalToFatName(filename,ffname))!=0){
if((tmpclus=dir_findinDir(fs,ffname,fccd,loc,DIRFIND_FILE))==0)return(0);
it++;
if(loc->attrib&ATTR_DIRECTORY){
fccd = tmpclus;
filename = next;
if(lastDir)*lastDir=fccd;
if(filefound)*lastDir=0;
}else{
filefound=1;
if((file_normalToFatName(next,ffname))!=0){
return(0);
}else{
filename=next;
}
}
}
if(it==0)return(0);
if(loc->attrib&ATTR_DIRECTORY || !filefound)return(2);
return(1);
}
/*****************************************************************************/
esint16 fs_findFreeFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint8 mode)
{
euint32 targetdir=0;
eint8 ffname[11];
if(fs_findFile(fs,filename,loc,&targetdir))return(0);
if(!dir_getFatFileName(filename,ffname))return(0);
if(dir_findinDir(fs,ffname,targetdir,loc,DIRFIND_FREE)){
return(1);
}else{
if(dir_addCluster(fs,targetdir)){
return(0);
}else{
if(dir_findinDir(fs,ffname,targetdir,loc,DIRFIND_FREE)){
return(1);
}
}
}
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 fs_getLastCluster(FileSystem *fs,ClusterChain *Cache)
* Description: This function searches the last cluster of a chain.
* Return value: The LastCluster (also stored in cache);
*/
euint32 fs_getLastCluster(FileSystem *fs,ClusterChain *Cache)
{
if(Cache->DiscCluster==0){
Cache->DiscCluster=Cache->FirstCluster;
Cache->LogicCluster=0;
}
if(Cache->LastCluster==0)
{
while(fat_getNextClusterChain(fs, Cache)==0)
{
Cache->LogicCluster+=Cache->Linear;
Cache->DiscCluster+=Cache->Linear;
Cache->Linear=0;
}
}
return(Cache->LastCluster);
}
/*****************************************************************************/
euint32 fs_getFirstClusterRootDir(FileSystem *fs)
{
switch(fs->type){
case FAT32:
return(fs->volumeId.RootCluster);
break;
default:
return(1);
break;
}
}
/*****************************************************************************/
void fs_initClusterChain(FileSystem *fs,ClusterChain *cache,euint32 cluster_addr)
{
cache->FirstCluster=cluster_addr;
cache->DiscCluster=cluster_addr;
cache->LogicCluster=0;
cache->LastCluster=0; /* Warning flag here */
cache->Linear=0;
}
/*****************************************************************************/
void fs_setFirstClusterInDirEntry(FileRecord *rec,euint32 cluster_addr)
{
rec->FirstClusterHigh=cluster_addr>>16;
rec->FirstClusterLow=cluster_addr&0xFFFF;
}
/*****************************************************************************/
esint8 fs_flushFs(FileSystem *fs)
{
return(part_flushPart(fs->part,0,fs->SectorCount));
}
/*****************************************************************************/
esint8 fs_umount(FileSystem *fs)
{
return(fs_flushFs(fs));
}
/*****************************************************************************/
esint8 fs_clearCluster(FileSystem *fs,euint32 cluster)
{
euint16 c;
euint8* buf;
for(c=0;c<(fs->volumeId.SectorsPerCluster);c++){
buf = part_getSect(fs->part,fs_clusterToSector(fs,cluster)+c,IOM_MODE_READWRITE);
memClr(buf,512);
part_relSect(fs->part,buf);
}
return(0);
}
esint8 fs_getFsInfo(FileSystem *fs)
{
euint8 *buf;
if(!fs->type==FAT32)return(0);
buf = part_getSect(fs->part,FS_INFO_SECTOR,IOM_MODE_READONLY);
if(ex_getb32(buf+0)!=FSINFO_MAGIC_BEGIN || ex_getb32(buf+508)!=FSINFO_MAGIC_END){
part_relSect(fs->part,buf);
return(-1);
}
fs->FreeClusterCount = ex_getb32(buf+488);
fs->NextFreeCluster = ex_getb32(buf+492);
part_relSect(fs->part,buf);
return(0);
}
esint8 fs_setFsInfo(FileSystem *fs)
{
euint8* buf;
if(!fs->type==FAT32)return(0);
buf = part_getSect(fs->part,FS_INFO_SECTOR,IOM_MODE_READWRITE);
if(ex_getb32(buf+0)!=FSINFO_MAGIC_BEGIN || ex_getb32(buf+508)!=FSINFO_MAGIC_END){
part_relSect(fs->part,buf);
return(-1);
}
ex_setb32(buf+488,fs->FreeClusterCount);
ex_setb32(buf+492,fs->NextFreeCluster);
part_relSect(fs->part,buf);
return(0);
}

View File

@ -0,0 +1,511 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : fs.c *
* Release : 0.3 - devel *
* Description : These are general filesystem functions, supported by the *
* functions of dir.c and fat.c file.c uses these functions *
* heavily, but is not used by fs.c (not true anymore) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "fs.h"
#include "fat.h"
#include "dir.h"
/*****************************************************************************/
/* ****************************************************************************
* eint16 fs_initFs(FileSystem *fs,Partition *part)
* Description: This functions glues the initialisation of the filesystem together.
* It loads the volumeID, computes the FS type and searches for the rootsector.
* Return value: Returns 0 on succes and -1 on error (if magic code is wrong)
*/
eint16 fs_initFs(FileSystem *fs,Partition *part)
{
if(!fs_isValidFat(part)){
return(-1);
}
fs->part=part;
fs_loadVolumeId(fs,part);
if(!fs_verifySanity(fs))return(-2);
fs_countDataSectors(fs);
fs_determineFatType(fs);
fs_findFirstSectorRootDir(fs);
fs_initCurrentDir(fs);
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* eint16 fs_isValidFat(Partition *part)
* Description: This functions loads the volumeID and checks if the magic
* value is present.
* Return value: returns 0 when magic code is missing, 1 if it is there.
*/
eint16 fs_isValidFat(Partition *part)
{
euint8 *buf;
buf=part_getSect(part,0,IOM_MODE_READONLY|IOM_MODE_EXP_REQ); /* Load Volume label */
if( ex_getb16(buf+0x1FE) != 0xAA55 ){
return (0);
}
part_relSect(part,buf);
return(1);
}
/*****************************************************************************/
/* ****************************************************************************
* void fs_loadVolumeId(FileSystem *fs, Partition *part)
* Description: This function loads all relevant fields from the volumeid.
*/
void fs_loadVolumeId(FileSystem *fs, Partition *part)
{
euint8 *buf;
buf=part_getSect(part,0,IOM_MODE_READONLY|IOM_MODE_EXP_REQ);
fs->volumeId.BytesPerSector=ex_getb16(buf+0x0B);
fs->volumeId.SectorsPerCluster=*((eint8*)(buf+0x0D));
fs->volumeId.ReservedSectorCount=ex_getb16(buf+0x0E);
fs->volumeId.NumberOfFats=*((eint8*)(buf+0x10));
fs->volumeId.RootEntryCount=ex_getb16(buf+0x11);
fs->volumeId.SectorCount16=ex_getb16(buf+0x13);
fs->volumeId.FatSectorCount16=ex_getb16(buf+0x16);
fs->volumeId.SectorCount32=ex_getb32(buf+0x20);
fs->volumeId.FatSectorCount32=ex_getb32(buf+0x24);
fs->volumeId.RootCluster=ex_getb32(buf+0x2C);
part_relSect(part,buf);
}
/*****************************************************************************/
/* ****************************************************************************
* esint16 fs_verifySanity(FileSystem *fs)
* Description: Does some sanity calculations.
* Return value: 1 on success, 0 when discrepancies were found.
*/
esint16 fs_verifySanity(FileSystem *fs)
{
esint16 sane=1; /* Sane until proven otherwise */
/* First check, BPS, we only support 512 */
if(fs->volumeId.BytesPerSector!=512)sane=0;
/* Check is SPC is valid (multiple of 2, and clustersize >=32KB */
if(!((fs->volumeId.SectorsPerCluster == 1 ) |
(fs->volumeId.SectorsPerCluster == 2 ) |
(fs->volumeId.SectorsPerCluster == 4 ) |
(fs->volumeId.SectorsPerCluster == 8 ) |
(fs->volumeId.SectorsPerCluster == 16) |
(fs->volumeId.SectorsPerCluster == 32) |
(fs->volumeId.SectorsPerCluster == 64) ))sane=0;
/* Any number of FAT's should be supported... (untested) */
/* There should be at least 1 reserved sector */
if(fs->volumeId.ReservedSectorCount==0)sane=0;
/* if(fs->volumeId.FatSectorCount16 != 0){
if(fs->volumeId.FatSectorCount16 > fs->part->disc->partitions[fs->part->activePartition].numSectors)sane=0;
}else{
if(fs->volumeId.FatSectorCount32 > fs->part->disc->partitions[fs->part->activePartition].numSectors)sane=0;
} */
return(sane);
}
/*****************************************************************************/
/* ****************************************************************************
* void fs_countDataSectors(FileSystem *fs)
* Description: This functions calculates the sectorcounts, fatsectorcounts and
* dataclustercounts. It fills in the general fields.
*/
void fs_countDataSectors(FileSystem *fs)
{
euint32 rootDirSectors,dataSectorCount;
rootDirSectors=((fs->volumeId.RootEntryCount*32) +
(fs->volumeId.BytesPerSector - 1)) /
fs->volumeId.BytesPerSector;
if(fs->volumeId.FatSectorCount16 != 0)
{
fs->FatSectorCount=fs->volumeId.FatSectorCount16;
fs->volumeId.FatSectorCount32=0;
}
else
{
fs->FatSectorCount=fs->volumeId.FatSectorCount32;
fs->volumeId.FatSectorCount16=0;
}
if(fs->volumeId.SectorCount16!=0)
{
fs->SectorCount=fs->volumeId.SectorCount16;
fs->volumeId.SectorCount32=0;
}
else
{
fs->SectorCount=fs->volumeId.SectorCount32;
fs->volumeId.SectorCount16=0;
}
dataSectorCount=fs->SectorCount - (
fs->volumeId.ReservedSectorCount +
(fs->volumeId.NumberOfFats * fs->FatSectorCount) +
rootDirSectors);
fs->DataClusterCount=dataSectorCount/fs->volumeId.SectorsPerCluster;
}
/*****************************************************************************/
/* ****************************************************************************
* void fs_determineFatType(FileSystem *fs)
* Description: This function looks af the Dataclustercount and determines the
* FAT type. It fills in fs->type.
*/
void fs_determineFatType(FileSystem *fs)
{
if(fs->DataClusterCount < 4085)
{
fs->type=FAT12;
fs->volumeId.RootCluster=0;
}
else if(fs->DataClusterCount < 65525)
{
fs->type=FAT16;
fs->volumeId.RootCluster=0;
}
else
{
fs->type=FAT32;
}
}
/*****************************************************************************/
/* ****************************************************************************
* void fs_findFirstSectorRootDir(FileSystem *fs)
* Description: This functions fills in the fs->FirstSectorRootDir field, even
* for FAT32, although that is not necessary (because you have FirstClusterRootDir).
*/
void fs_findFirstSectorRootDir(FileSystem *fs)
{
if(fs->type==FAT32)
fs->FirstSectorRootDir = fs->volumeId.ReservedSectorCount +
(fs->volumeId.NumberOfFats*fs->volumeId.FatSectorCount32) +
(fs->volumeId.RootCluster-2)*fs->volumeId.SectorsPerCluster;
else
fs->FirstSectorRootDir = fs->volumeId.ReservedSectorCount +
(fs->volumeId.NumberOfFats*fs->volumeId.FatSectorCount16);
}
/*****************************************************************************/
void fs_initCurrentDir(FileSystem *fs)
{
fs->FirstClusterCurrentDir = fs_getFirstClusterRootDir(fs);
}
/*****************************************************************************/
/* ****************************************************************************
* long fs_clusterToSector(FileSystem *fs,euint32 cluster)
* Description: This function converts a clusternumber in the effective sector
* number where this cluster starts. Boundary check is not implemented
* Return value: A long is returned representing the sectornumber.
*/
euint32 fs_clusterToSector(FileSystem *fs,euint32 cluster)
{
eint32 base;
if(fs->type==FAT32)
{
base=
fs->volumeId.ReservedSectorCount+
fs->FatSectorCount*fs->volumeId.NumberOfFats;
}
else
{
base=
fs->volumeId.ReservedSectorCount+
fs->FatSectorCount*fs->volumeId.NumberOfFats+
fs->volumeId.RootEntryCount/16;
}
return( base + (cluster-2)*fs->volumeId.SectorsPerCluster );
}
/*****************************************************************************/
/* Function is unused, but may be usefull */
euint32 fs_sectorToCluster(FileSystem *fs,euint32 sector)
{
eint32 base;
if(fs->type==FAT32)
{
base=
fs->volumeId.ReservedSectorCount+
fs->FatSectorCount*fs->volumeId.NumberOfFats;
}
else
{
base=
fs->volumeId.ReservedSectorCount+
fs->FatSectorCount*fs->volumeId.NumberOfFats+
fs->volumeId.RootEntryCount/16;
}
return(((sector-base)-((sector-base)%fs->volumeId.SectorsPerCluster))/fs->volumeId.SectorsPerCluster+2 );
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 fs_getNextFreeCluster(FileSystem *fs,euint32 startingcluster)
* Description: This functions searches for a free cluster, starting it's search at
* cluster startingcluster. This allow to speed up searches and try to avoid
* fragmentation. Implementing rollover search is still to be done.
* Return value: If a free cluster is found it's number is returned. If none is
* found 0 is returned.
*/
euint32 fs_getNextFreeCluster(FileSystem *fs,euint32 startingcluster)
{
euint32 r;
while(startingcluster<fs->DataClusterCount){
r=fat_getNextClusterAddress(fs,startingcluster,0);
if(r==0){
return(startingcluster);
}
startingcluster++;
}
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 fs_giveFreeClusterHint(FileSystem *fs)
*
* Description: This function should return a clusternumber that is free or
* lies close before free clusters. The result MUST be checked to see if
* it is free! Implementationhint: search the largest clusternumber in the
* files in the rootdirectory.
*
* Return value: Returns it's best guess.
*/
euint32 fs_giveFreeClusterHint(FileSystem *fs)
{
return(2); /* Now THIS is a hint ;) */
}
/*****************************************************************************/
/* ****************************************************************************
* esint8 fs_findFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint32 *lastDir)
*
* Description: This function looks if the given filename is on the given fs
* and, if found, fills in its location in loc.
* The function will first check if the pathname starts with a slash. If so it will
* set the starting directory to the rootdirectory. Else, it will take the firstcluster-
* currentdir (That you can change with chdir()) as startingpoint.
* The lastdir pointer will be the first cluster of the last directory fs_findfile
* enters. It starts out at the root/current dir and then traverses the path along with
* fs_findFile.
* It is set to 0 in case of errors (like dir/dir/dir/file/dir/dir...)
* Return value: Returns 0 when nothing was found, 1 when the thing found
* was a file and 2 if the thing found was a directory.
*/
esint8 fs_findFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint32 *lastDir)
{
euint32 fccd,tmpclus;
eint8 ffname[11],*next,it=0,filefound=0;
if(*filename=='/'){
fccd = fs_getFirstClusterRootDir(fs);
filename++;
if(!*filename){
if(lastDir)*lastDir=fccd;
return(2);
}
}else{
fccd = fs->FirstClusterCurrentDir;
}
if(lastDir)*lastDir=fccd;
while((next=file_normalToFatName(filename,ffname))!=0){
if((tmpclus=dir_findinDir(fs,ffname,fccd,loc,DIRFIND_FILE))==0)return(0);
it++;
if(loc->attrib&ATTR_DIRECTORY){
fccd = tmpclus;
filename = next;
if(lastDir)*lastDir=fccd;
if(filefound)*lastDir=0;
}else{
filefound=1;
if((file_normalToFatName(next,ffname))!=0){
return(0);
}else{
filename=next;
}
}
}
if(it==0)return(0);
if(loc->attrib&ATTR_DIRECTORY || !filefound)return(2);
return(1);
}
/*****************************************************************************/
esint16 fs_findFreeFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint8 mode)
{
euint32 targetdir=0;
eint8 ffname[11];
if(fs_findFile(fs,filename,loc,&targetdir))return(0);
if(!dir_getFatFileName(filename,ffname))return(0);
if(dir_findinDir(fs,ffname,targetdir,loc,DIRFIND_FREE)){
return(1);
}else{
if(dir_addCluster(fs,targetdir)){
return(0);
}else{
if(dir_findinDir(fs,ffname,targetdir,loc,DIRFIND_FREE)){
return(1);
}
}
}
return(0);
}
/*****************************************************************************/
/* ****************************************************************************
* euint32 fs_getLastCluster(FileSystem *fs,ClusterChain *Cache)
* Description: This function searches the last cluster of a chain.
* Return value: The LastCluster (also stored in cache);
*/
euint32 fs_getLastCluster(FileSystem *fs,ClusterChain *Cache)
{
if(Cache->DiscCluster==0){
Cache->DiscCluster=Cache->FirstCluster;
Cache->LogicCluster=0;
}
if(Cache->LastCluster==0)
{
while(fat_getNextClusterChain(fs, Cache)==0)
{
Cache->LogicCluster+=Cache->Linear;
Cache->DiscCluster+=Cache->Linear;
Cache->Linear=0;
}
}
return(Cache->LastCluster);
}
/*****************************************************************************/
euint32 fs_getFirstClusterRootDir(FileSystem *fs)
{
switch(fs->type){
case FAT32:
return(fs->volumeId.RootCluster);
break;
default:
return(1);
break;
}
}
/*****************************************************************************/
void fs_initClusterChain(FileSystem *fs,ClusterChain *cache,euint32 cluster_addr)
{
cache->FirstCluster=cluster_addr;
cache->DiscCluster=cluster_addr;
cache->LogicCluster=0;
cache->LastCluster=0; /* Warning flag here */
cache->Linear=0;
}
/*****************************************************************************/
void fs_setFirstClusterInDirEntry(FileRecord *rec,euint32 cluster_addr)
{
rec->FirstClusterHigh=cluster_addr>>16;
rec->FirstClusterLow=cluster_addr&0xFFFF;
}
/*****************************************************************************/
esint8 fs_flushFs(FileSystem *fs)
{
return(part_flushPart(fs->part,0,fs->SectorCount));
}
/*****************************************************************************/
esint8 fs_umount(FileSystem *fs)
{
return(fs_flushFs(fs));
}
/*****************************************************************************/
esint8 fs_clearCluster(FileSystem *fs,euint32 cluster)
{
euint16 c;
euint8* buf;
for(c=0;c<(fs->volumeId.SectorsPerCluster);c++){
buf = part_getSect(fs->part,fs_clusterToSector(fs,cluster)+c,IOM_MODE_READWRITE);
memClr(buf,512);
part_relSect(fs->part,buf);
}
return(0);
}
esint8 fs_getFsInfo(FileSystem *fs)
{
euint8 *buf;
if(!fs->type==FAT32)return(0);
buf = part_getSect(fs->part,FS_INFO_SECTOR,IOM_MODE_READONLY);
if(ex_getb32(buf+0)!=FSINFO_MAGIC_BEGIN || ex_getb32(buf+508)!=FSINFO_MAGIC_END){
part_relSect(fs->part,buf);
return(-1);
}
fs->FreeClusterCount = ex_getb32(buf+488);
fs->NextFreeCluster = ex_getb32(buf+492);
part_relSect(fs->part,buf);
return(0);
}
esint8 fs_setFsInfo(FileSystem *fs)
{
euint8* buf;
if(!fs->type==FAT32)return(0);
buf = part_getSect(fs->part,FS_INFO_SECTOR,IOM_MODE_READWRITE);
if(ex_getb32(buf+0)!=FSINFO_MAGIC_BEGIN || ex_getb32(buf+508)!=FSINFO_MAGIC_END){
part_relSect(fs->part,buf);
return(-1);
}
ex_setb32(buf+488,fs->FreeClusterCount);
ex_setb32(buf+492,fs->NextFreeCluster);
part_relSect(fs->part,buf);
return(0);
}

View File

@ -0,0 +1,85 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : dir.h *
* Release : 0.3 - devel *
* Description : The functions of dir.c are part of fs.c, they deal with all *
* the directory specific stuff. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __DDIR_H__
#define __DDIR_H__
/*****************************************************************************/
#include "config.h"
#include "error.h"
#include "fat.h"
#include "plibc.h"
#include "types.h"
#include "ioman.h"
#include "time.h"
#include "fs.h"
/*****************************************************************************/
#define ATTR_READ_ONLY 0x01
#define ATTR_HIDDEN 0x02
#define ATTR_SYSTEM 0x04
#define ATTR_VOLUME_ID 0x08
#define ATTR_DIRECTORY 0x10
#define ATTR_ARCHIVE 0x20
#define OFFSET_DE_FILENAME 0
#define OFFSET_DE_ATTRIBUTE 11
#define OFFSET_DE_NTRESERVED 12
#define OFFSET_DE_CRTIMETNT 13
#define OFFSET_DE_CREATETIME 14
#define OFFSET_DE_CREATEDATE 16
#define OFFSET_DE_LASTACCESSDATE 18
#define OFFSET_DE_FIRSTCLUSTERHIGH 20
#define OFFSET_DE_WRITETIME 22
#define OFFSET_DE_WRITEDATE 24
#define OFFSET_DE_FIRSTCLUSTERLOW 26
#define OFFSET_DE_FILESIZE 28
#define DIRFIND_FILE 0
#define DIRFIND_FREE 1
void dir_getFileStructure(FileSystem *fs,FileRecord *filerec,FileLocation *loc);
void dir_createDirectoryEntry(FileSystem *fs,FileRecord *filerec,FileLocation *loc);
void dir_createDefaultEntry(FileSystem *fs,FileRecord *filerec,eint8* fatfilename);
void dir_setFirstCluster(FileSystem *fs,FileLocation *loc,euint32 cluster_addr);
void dir_setFileSize(FileSystem *fs,FileLocation *loc,euint32 numbytes);
euint32 dir_findinRoot(FileSystem *fs,eint8 * fatname, FileLocation *loc);
euint32 dir_findinDir(FileSystem *fs, eint8 * fatname, euint32 startCluster, FileLocation *loc, euint8 mode);
euint32 dir_findinBuf(euint8 *buf,eint8 *fatname, FileLocation *loc, euint8 mode);
euint32 dir_findinCluster(FileSystem *fs,euint32 cluster,eint8 *fatname, FileLocation *loc, euint8 mode);
euint32 dir_findinRootArea(FileSystem *fs,eint8* fatname, FileLocation *loc, euint8 mode);
esint8 dir_getFatFileName(eint8* filename, eint8* fatfilename);
esint8 dir_updateDirectoryEntry(FileSystem *fs,FileRecord *entry,FileLocation *loc);
esint8 dir_addCluster(FileSystem *fs,euint32 firstCluster);
#endif

View File

@ -0,0 +1,62 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : fat.h *
* Release : 0.3 - devel *
* Description : This file contains all the functions dealing with the FAT *
* in a Microsoft FAT filesystem. It belongs under fs.c *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __FAT_H_
#define __FAT_H_
/*****************************************************************************/
#include "config.h"
#include "error.h"
#include "file.h"
#include "debug.h"
#include "types.h"
/*****************************************************************************/
euint32 fat_getSectorAddressFatEntry(FileSystem *fs,euint32 cluster_addr);
euint32 fat_getNextClusterAddress(FileSystem *fs, euint32 cluster_addr, euint16 *linear);
void fat_setNextClusterAddress(FileSystem *fs,euint32 cluster_addr,euint32 next_cluster_addr);
eint16 fat_isEocMarker(FileSystem *fs,euint32 fat_entry);
euint32 fat_giveEocMarker(FileSystem *fs);
euint32 fat_findClusterAddress(FileSystem *fs,euint32 cluster,euint32 offset, euint8 *linear);
euint32 fat_getNextClusterAddressWBuf(FileSystem *fs,euint32 cluster_addr, euint8 * buf);
void fat_setNextClusterAddressWBuf(FileSystem *fs,euint32 cluster_addr,euint32 next_cluster_addr,euint8 * buf);
esint16 fat_getNextClusterChain(FileSystem *fs, ClusterChain *Cache);
void fat_bogus(void);
esint16 fat_LogicToDiscCluster(FileSystem *fs, ClusterChain *Cache,euint32 logiccluster);
eint16 fat_allocClusterChain(FileSystem *fs,ClusterChain *Cache,euint32 num_clusters);
eint16 fat_unlinkClusterChain(FileSystem *fs,ClusterChain *Cache);
euint32 fat_countClustersInChain(FileSystem *fs,euint32 firstcluster);
euint32 fat_DiscToLogicCluster(FileSystem *fs,euint32 firstcluster,euint32 disccluster);
#endif

View File

@ -0,0 +1,97 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : file.h *
* Release : 0.3 - devel *
* Description : This file contains functions dealing with files such as: *
* fopen, fread and fwrite. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __FILE_H_
#define __FILE_H_
/*****************************************************************************/
#include "config.h"
#include "error.h"
#include "time.h"
#include "fs.h"
#include "dir.h"
#include "plibc.h"
#include "debug.h"
#include "types.h"
#include "fat.h"
/*****************************************************************************/
#define MODE_READ 0x72
#define MODE_WRITE 0x77
#define MODE_APPEND 0x61
#define FILE_STATUS_OPEN 0
#define FILE_STATUS_WRITE 1
/*****************************************************************************\
* File
* ------
* FileRecord DirEntry Copy of the FileRecord for this file
* FileLocation Location Location of the direntry
* FileSystem *fs Pointer to the filesystem this file is on
* FileCache Cache Pointer to the cache object of the file
* euint8 FileStatus Contains bitfield regarding filestatus
* euint32 FilePtr Offsetpointer for fread/fwrite functions
* euint32 FileSize Working copy of the filesize, always use this,
it is more up to date than DirEntry->FileSize,
which is only updated when flushing to disc.
\*****************************************************************************/
struct _File{
FileRecord DirEntry;
FileLocation Location; /* Location in directory!! */
FileSystem *fs;
ClusterChain Cache;
euint8 FileStatus;
euint32 FilePtr;
euint32 FileSize;
};
typedef struct _File File;
esint8 file_fopen(File *file, FileSystem *fs,eint8 *filename, eint8 mode);
esint8 file_fclose(File *file);
esint16 file_setpos(File *file,euint32 pos);
euint32 file_fread(File *file,euint32 offset, euint32 size,euint8 *buf);
euint32 file_read (File *file,euint32 size,euint8 *buf);
euint32 file_fwrite(File* file,euint32 offset,euint32 size,euint8* buf);
euint32 file_write (File* file,euint32 size,euint8* buf);
eint8* file_normalToFatName(eint8* filename,eint8* fatfilename);
euint8 file_validateChar(euint8 c);
void file_initFile(File *file, FileSystem *fs, FileLocation *loc);
eint16 file_allocClusterChain(File *file,euint32 num_clusters);
void file_setAttr(File* file,euint8 attribute,euint8 val);
euint8 file_getAttr(File* file,euint8 attribute);
euint32 file_requiredCluster(File *file,euint32 offset, euint32 size);
#endif

View File

@ -0,0 +1,200 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : fs.h *
* Release : 0.3 - devel *
* Description : These are general filesystem functions, supported by the *
* functions of dir.c and fat.c file.c uses these functions *
* heavily, but is not used by fs.c (not true anymore) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __FS_H_
#define __FS_H_
/*****************************************************************************/
#include "config.h"
#include "types.h"
#include "error.h"
#include "partition.h"
#include "debug.h"
#include "time.h"
#include "extract.h"
/*****************************************************************************/
#define FAT12 1
#define FAT16 2
#define FAT32 3
#define FS_INFO_SECTOR 1
#define FSINFO_MAGIC_BEGIN 0x41615252
#define FSINFO_MAGIC_END 0xAA550000
/*****************************************************************************************\
VolumeId
------
* ushort BytesPerSector Must be 512 or shit happens.
* uchar SectorsPerCluster Must be multiple of 2 (1,2,4,8,16 or 32)
* ushort ReservedSectorCount Number of sectors after which the first FAT begins.
* uchar NumberOfFats Should be 2
* ushort RootEntryCount Number of filerecords the Rootdir can contain. NOT for FAT32
* ushort SectorCount16 Number of Sectors for 12/16 bit FAT
* ushort FatSectorCount16 Number of Sectors for 1 FAT on FAT12/16 bit FAT's
* ulong SectorCount32 Number of Sectors for 32 bit FAT
* ulong FatSectorCount32 Number of Sectors for 1 FAT on FAT32
* ulong RootCluster Clusternumber of the first cluster of the RootDir on FAT 32
This is NOT a complete volumeId copy, no direct I/O is possible.
\*****************************************************************************************/
struct _VolumeId{
euint16 BytesPerSector;
euint8 SectorsPerCluster;
euint16 ReservedSectorCount;
euint8 NumberOfFats;
euint16 RootEntryCount;
euint16 SectorCount16;
euint16 FatSectorCount16;
euint32 SectorCount32;
euint32 FatSectorCount32;
euint32 RootCluster;
};
typedef struct _VolumeId VolumeId;
/**************************************************************************************************\
FileSystem
--------
* Partition* part Pointer to partition on which this FS resides.
* VolumeId volumeId Contains important FS info.
* ulong DataClusterCount Number of dataclusters. This number determines the FATType.
* ulong FatSectorCount Number of sectors for 1 FAT, regardless of FATType
* ulong SectorCount Number of sectors, regardless of FATType
* ulong FirstSectorRootDir First sector of the RootDir.
* uchar type Determines FATType (FAT12 FAT16 or FAT32 are defined)
\**************************************************************************************************/
struct _FileSystem{
Partition *part;
VolumeId volumeId;
euint32 DataClusterCount;
euint32 FatSectorCount;
euint32 SectorCount;
euint32 FirstSectorRootDir;
euint32 FirstClusterCurrentDir;
euint32 FreeClusterCount;
euint32 NextFreeCluster;
euint8 type;
};
typedef struct _FileSystem FileSystem;
/**************************************************************************************************\ FileLocation
----------
* euint32 Sector Sector where the directoryentry of the file/directory can be found.
* euint8 Offset Offset (in 32byte segments) where in the sector the entry is.
\**************************************************************************************************/
struct _FileLocation{
euint32 Sector;
euint8 Offset;
euint8 attrib;
};
typedef struct _FileLocation FileLocation;
/*****************************************************************************\
* FileCache
* -----------
* This struct acts as cache for the current file. It contains the current
* FATPointer (next location in the FAT table), LogicCluster
* (the last part of the file that was read) and DataCluster
* (the last cluster that was read).
* euint8 Linear For how many more clusters the file is nonfragmented
* euint32 LogicCluster This field determines the n'th cluster of the file as current
* euint32 DiscCluster If this field is 0, it means the cache is invalid. Otherwise
it is the clusternumber corresponding with
logic(FirstCluster+LogicCluster).
* euint32 FirstCluster First cluster of the chain. Zero or one are invalid.
* euint32 LastCluster Last cluster of the chain (is not always filled in)
\*****************************************************************************/
struct _ClusterChain{
euint8 Linear;
esint32 LogicCluster;
euint32 DiscCluster;
euint32 FirstCluster;
euint32 LastCluster;
};
typedef struct _ClusterChain ClusterChain;
/*****************************************************************************\
* FileRecord *
* ------------ *
* This struct represents a 32 byte file entry as it occurs in the data area *
* of the filesystem. Direct I/O is possible. *
\*****************************************************************************/
struct _FileRecord{
euint8 FileName[11];
euint8 Attribute;
euint8 NTReserved;
euint8 MilliSecTimeStamp;
euint16 CreatedTime;
euint16 CreatedDate;
euint16 AccessDate;
euint16 FirstClusterHigh;
euint16 WriteTime;
euint16 WriteDate;
euint16 FirstClusterLow;
euint32 FileSize;
};
typedef struct _FileRecord FileRecord;
eint16 fs_initFs(FileSystem *fs,Partition *part);
eint16 fs_isValidFat(Partition *part);
void fs_loadVolumeId(FileSystem *fs, Partition *part);
esint16 fs_verifySanity(FileSystem *fs);
void fs_countDataSectors(FileSystem *fs);
void fs_determineFatType(FileSystem *fs);
void fs_findFirstSectorRootDir(FileSystem *fs);
void fs_initCurrentDir(FileSystem *fs);
euint32 fs_getSectorAddressRootDir(FileSystem *fs,euint32 secref);
euint32 fs_clusterToSector(FileSystem *fs,euint32 cluster);
euint32 fs_sectorToCluster(FileSystem *fs,euint32 sector);
euint32 fs_getNextFreeCluster(FileSystem *fs,euint32 startingcluster);
euint32 fs_giveFreeClusterHint(FileSystem *fs);
esint16 fs_findFreeFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint8 mode);
esint8 fs_findFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint32 *lastDir);
esint8 fs_findFile_broken(FileSystem *fs,eint8* filename,FileLocation *loc);
euint32 fs_getLastCluster(FileSystem *fs,ClusterChain *Cache);
euint32 fs_getFirstClusterRootDir(FileSystem *fs);
euint16 fs_makeDate(void);
euint16 fs_makeTime(void);
void fs_setFirstClusterInDirEntry(FileRecord *rec,euint32 cluster_addr);
void fs_initClusterChain(FileSystem *fs,ClusterChain *cache,euint32 cluster_addr);
esint8 fs_flushFs(FileSystem *fs);
esint8 fs_umount(FileSystem *fs);
esint8 fs_clearCluster(FileSystem *fs,euint32 cluster);
esint8 fs_getFsInfo(FileSystem *fs);
esint8 fs_setFsInfo(FileSystem *fs);
#endif

View File

@ -0,0 +1,69 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : ls.h *
* Release : 0.3 - devel *
* Description : This file contains functions to list the files in a directory *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __LS_H__
#define __LS_H__
/*****************************************************************************/
#include "config.h"
#include "fs.h"
#include "dir.h"
#include "fat.h"
/*****************************************************************************/
struct _ListDirEntry{
euint8 FileName[LIST_MAXLENFILENAME];
euint32 FileSize;
euint8 Attribute;
};
typedef struct _ListDirEntry ListDirEntry;
struct _DirList{
FileSystem *fs;
euint16 cEntry,rEntry;
/*FileRecord currentEntry;*/
ListDirEntry currentEntry;
ClusterChain Cache;
};
typedef struct _DirList DirList;
esint8 ls_openDir(DirList *dlist,FileSystem *fs,eint8* dirname);
esint8 ls_getNext(DirList *dlist);
esint8 ls_getDirEntry(DirList *dlist);
esint8 ls_getRealDirEntry(DirList *dlist);
esint8 ls_getRootAreaEntry(DirList *dlist);
esint8 ls_isValidFileEntry(ListDirEntry *entry);
void ls_fileEntryToDirListEntry(DirList *dlist, euint8* buf, euint16 offset);
#endif

View File

@ -0,0 +1,50 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : mkfs.h *
* Release : 0.3 - devel *
* Description : These functions are used for creating an empty filesystem. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __MKFS_H_
#define __MKFS_H_
/*****************************************************************************/
#include "partition.h"
#include "plibc.h"
#include "debug.h"
#include "types.h"
#include "extract.h"
#include "config.h"
/*****************************************************************************/
#define MKFS_ERR_TOOLITTLESECTORS 1
esint16 mkfs_makevfat(Partition *part);
#endif

View File

@ -0,0 +1,74 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : time.h *
* Release : 0.3 - devel *
* Description : This file contains functions for time support *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __TIME_H_
#define __TIME_H_
/*****************************************************************************/
#include "types.h"
/*****************************************************************************/
#ifdef DATE_TIME_SUPPORT
#define time_getYear(void) efsl_getYear()
#define time_getMonth(void) efsl_getMonth()
#define time_getDay(void) efsl_getDay()
#define time_getHour(void) efsl_getHour()
#define time_getMinute(void) efsl_getMinute()
#define time_getSecond(void) efsl_getSecond()
#define time_getDate(void) fs_makeDate()
#define time_getTime(void) fs_makeTime()
#else
#define time_getYear(void) 0x0;
#define time_getMonth(void) 0x0;
#define time_getDay(void) 0x0;
#define time_getHour(void) 0x0;
#define time_getMinute(void) 0x0;
#define time_getSecond(void) 0x0;
#define time_getDate(void) 0x0;
#define time_getTime(void) 0x0;
#endif
#ifdef DATE_TIME_SUPPORT
euint16 efsl_getYear(void);
euint8 efsl_getMonth(void);
euint8 efsl_getDay(void);
euint8 efsl_getHour(void);
euint8 efsl_getMinute(void);
euint8 efsl_getSecond(void);
euint16 fs_makeDate(void);
euint16 fs_makeTime(void);
#endif
euint8 fs_hasTimeSupport(void);
#endif

View File

@ -0,0 +1,50 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : ui.h *
* Release : 0.3 - devel *
* Description : This file contains functions which will be presented to the *
* user of this library. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __UI_H__
#define __UI_H__
/*****************************************************************************/
#include "fs.h"
#include "types.h"
#include "fat.h"
#include "dir.h"
#include "config.h"
/*****************************************************************************/
short listFiles(FileSystem *fs, char *dir);
esint16 rmfile(FileSystem *fs,euint8* filename);
esint8 mkdir(FileSystem *fs,eint8* dirname);
#endif

View File

@ -0,0 +1,141 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : ls.c *
* Release : 0.3 - devel *
* Description : This file contains functions to list the files in a directory *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "ls.h"
/*****************************************************************************/
esint8 ls_openDir(DirList *dlist,FileSystem *fs,eint8* dirname)
{
FileLocation loc;
euint32 fc;
dlist->fs=fs;
if(fs_findFile(dlist->fs,dirname,&loc,&fc)!=2)
{
return(-1);
}
fs_initClusterChain(dlist->fs,&(dlist->Cache),fc);
memClr(&(dlist->currentEntry),sizeof(dlist->currentEntry));
dlist->rEntry=0;
dlist->cEntry=0xFFFF;
return(0);
}
/*****************************************************************************/
esint8 ls_getDirEntry(DirList *dlist)
{
if(dlist->Cache.FirstCluster == 1){
return(ls_getRootAreaEntry(dlist));
}else if(dlist->Cache.FirstCluster){
return(ls_getRealDirEntry(dlist));
}
return(-1);
}
/*****************************************************************************/
esint8 ls_getNext(DirList *dlist)
{
do{
if(ls_getDirEntry(dlist))return(-1);
dlist->rEntry++;
}while(!ls_isValidFileEntry(&(dlist->currentEntry)));
dlist->cEntry++;
return(0);
}
/*****************************************************************************/
esint8 ls_getRealDirEntry(DirList *dlist)
{
euint8* buf;
if(dlist->Cache.FirstCluster<=1)return(-1);
if(fat_LogicToDiscCluster(dlist->fs,
&(dlist->Cache),
(dlist->rEntry)/(16 * dlist->fs->volumeId.SectorsPerCluster))){
return(-1);
}
buf = part_getSect(dlist->fs->part,
fs_clusterToSector(dlist->fs,dlist->Cache.DiscCluster) + (dlist->rEntry/16)%dlist->fs->volumeId.SectorsPerCluster,
IOM_MODE_READONLY);
/*memCpy(buf+(dlist->rEntry%16)*32,&(dlist->currentEntry),32);*/
ls_fileEntryToDirListEntry(dlist,buf,32*(dlist->rEntry%16));
part_relSect(dlist->fs->part,buf);
return(0);
}
/*****************************************************************************/
esint8 ls_getRootAreaEntry(DirList *dlist)
{
euint8 *buf=0;
if((dlist->fs->type != FAT12) && (dlist->fs->type != FAT16))return(-1);
if(dlist->rEntry>=dlist->fs->volumeId.RootEntryCount)return(-1);
buf = part_getSect(dlist->fs->part,
dlist->fs->FirstSectorRootDir+dlist->rEntry/16,
IOM_MODE_READONLY);
/*memCpy(buf+32*(dlist->rEntry%16),&(dlist->currentEntry),32);*/
ls_fileEntryToDirListEntry(dlist,buf,32*(dlist->rEntry%16));
part_relSect(dlist->fs->part,buf);
return(0);
}
/*****************************************************************************/
esint8 ls_isValidFileEntry(ListDirEntry *entry)
{
if(entry->FileName[0] == 0 || entry->FileName[0] == 0xE5 || entry->FileName[0] == '.')return(0);
if((entry->Attribute&0x0F)==0x0F)return(0);
return(1);
}
/*****************************************************************************/
void ls_fileEntryToDirListEntry(DirList *dlist, euint8* buf, euint16 offset)
{
if(offset>480 || offset%32)return;
buf+=offset;
memCpy(buf+OFFSET_DE_FILENAME,dlist->currentEntry.FileName,LIST_MAXLENFILENAME);
dlist->currentEntry.Attribute = *(buf+OFFSET_DE_ATTRIBUTE);
dlist->currentEntry.FileSize = ex_getb32(buf+OFFSET_DE_FILESIZE);
}
/*****************************************************************************/

View File

@ -0,0 +1,170 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : mkfs.c *
* Release : 0.3 - devel *
* Description : These functions are used for creating an empty filesystem. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "mkfs.h"
/*****************************************************************************/
/* This functions begs for a re-write, I'm disabling it */
#if 0
esint16 mkfs_makevfat(Partition *part)
{
unsigned long c,cc,ret;
unsigned long ns,fs,ds,dc;
unsigned char buf[512];
ns=part->disc->partitions[part->activePartition].numSectors;
if( ns < 66581 ){
DBG((TXT("This is not possible due to insufficient sectors. Sorry\n")));
return(MKFS_ERR_TOOLITTLESECTORS);
}
ret=0;
for(c=1<<6;c>=1;c>>=1){
/* First guess */
ds = ns - 32;
fs = ((ds/c)+127)/128;
/* ds was guess too large, so fs is too large now too. */
for(cc=0;cc<2;cc++){
/* Round 2, error round */
ds = ns - 32 - 2*fs;
fs = ((ds/c)+127)/128;
/* Since fs was too large, ds became too small. So the fs for this small ds is too small as well. */
/* Round 3, correction round */
ds = ns - 32 - 2*fs;
fs = ((ds/c)+127)/128;
/* The fs was too small, so ds was too large. The calculated fs should be slightly too large. */
}
/* Round 4, finalise */
ds = ns - 32 - 2*fs;
dc = ds / c;
if(ret<(fs*128-dc)/128)ret=(fs*128-dc)/128;
/* Check if with current setting we have a valid fat ? */
if(dc >= 65525 + 16){
break;
}
}
/* Generate BPB */
memClr(buf,512);
/* Boot code */
*(buf+0)=0xE9; *(buf+1)=0x00; *(buf+2)=0x00; /* RESET */
/* OEM name */
memCpy("DSCOSMSH",buf+3,8);
/* Bytes/Sector */
/* *((unsigned short*)(buf+11)) = 512; */
ex_setb16(buf+11,512);
/* Sectors/Cluster */
*(buf+13) = c;
/* Reserved Sectors */
/* *((unsigned short*)(buf+14)) = 32; */
ex_setb16(buf+14,32);
/* Number of FAT Tables */
*(buf+16) = 2;
/* RootEntryCount */
/* *((unsigned short*)(buf+17)) = 0; */
ex_setb16(buf+17,0);
/* Total Sector Count __16 */
/* *((unsigned short*)(buf+19)) = 0; */
ex_setb16(buf+19,0);
/* Media (crap) */
*(buf+21) = 0xF8;
/* FAT size 16 */
/* *((unsigned short*)(buf+22)) = 0; */
ex_setb16(buf+22,0);
/* Total Sector Count __32 */
/* *((unsigned long*)(buf+32)) = ns; */
ex_setb32(buf+32,ns);
/* Fat Size 32 */
/* *((unsigned long*)(buf+36)) = fs; */
ex_setb32(buf+36,fs);
/* First Cluster Root Dir */
/* *((unsigned long*)(buf+44)) = 2; */
ex_setb32(buf+44,2);
/* VolumeID */
/* *((unsigned long*)(buf+67)) = 0x13371337; */
ex_setb32(buf+67,0);
/* Volume Label */
memCpy("EFSL-0.3.5 ",buf+71,11);
/* Filesystemtype */
memCpy("FAT32 ",buf+82,8);
/* Magic */
*(buf+510) = 0x55; *(buf+511) = 0xAA;
part_writeBuf(part,0,buf);
memClr(buf,512);
for(c=32;c<(32+2*fs);c++){
part_writeBuf(part,c,buf);
}
/* *(((unsigned long*)buf) )=0x0FFFFFF8;
*(((unsigned long*)buf)+1)=0x0FFFFFFF;
*(((unsigned long*)buf)+2)=0x0FFFFFF8; */
ex_setb32(buf+0,(euint32)0x0FFFFFF8);
ex_setb32(buf+4,(euint32)0x0FFFFFFF);
ex_setb32(buf+8,(euint32)0x0FFFFFF8);
part_writeBuf(part,32,buf);
part_writeBuf(part,32+fs,buf);
return(0);
}
#endif

View File

@ -0,0 +1,170 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : mkfs.c *
* Release : 0.3 - devel *
* Description : These functions are used for creating an empty filesystem. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "mkfs.h"
/*****************************************************************************/
/* This functions begs for a re-write, I'm disabling it */
#if 0
esint16 mkfs_makevfat(Partition *part)
{
unsigned long c,cc,ret;
unsigned long ns,fs,ds,dc;
unsigned char buf[512];
ns=part->disc->partitions[part->activePartition].numSectors;
if( ns < 66581 ){
DBG((TXT("This is not possible due to insufficient sectors. Sorry\n")));
return(MKFS_ERR_TOOLITTLESECTORS);
}
ret=0;
for(c=1<<6;c>=1;c>>=1){
/* First guess */
ds = ns - 32;
fs = ((ds/c)+127)/128;
/* ds was guess too large, so fs is too large now too. */
for(cc=0;cc<2;cc++){
/* Round 2, error round */
ds = ns - 32 - 2*fs;
fs = ((ds/c)+127)/128;
/* Since fs was too large, ds became too small. So the fs for this small ds is too small as well. */
/* Round 3, correction round */
ds = ns - 32 - 2*fs;
fs = ((ds/c)+127)/128;
/* The fs was too small, so ds was too large. The calculated fs should be slightly too large. */
}
/* Round 4, finalise */
ds = ns - 32 - 2*fs;
dc = ds / c;
if(ret<(fs*128-dc)/128)ret=(fs*128-dc)/128;
/* Check if with current setting we have a valid fat ? */
if(dc >= 65525 + 16){
break;
}
}
/* Generate BPB */
memClr(buf,512);
/* Boot code */
*(buf+0)=0xE9; *(buf+1)=0x00; *(buf+2)=0x00; /* RESET */
/* OEM name */
memCpy("DSCOSMSH",buf+3,8);
/* Bytes/Sector */
/* *((unsigned short*)(buf+11)) = 512; */
ex_setb16(buf+11,512);
/* Sectors/Cluster */
*(buf+13) = c;
/* Reserved Sectors */
/* *((unsigned short*)(buf+14)) = 32; */
ex_setb16(buf+14,32);
/* Number of FAT Tables */
*(buf+16) = 2;
/* RootEntryCount */
/* *((unsigned short*)(buf+17)) = 0; */
ex_setb16(buf+17,0);
/* Total Sector Count __16 */
/* *((unsigned short*)(buf+19)) = 0; */
ex_setb16(buf+19,0);
/* Media (crap) */
*(buf+21) = 0xF8;
/* FAT size 16 */
/* *((unsigned short*)(buf+22)) = 0; */
ex_setb16(buf+22,0);
/* Total Sector Count __32 */
/* *((unsigned long*)(buf+32)) = ns; */
ex_setb32(buf+32,ns);
/* Fat Size 32 */
/* *((unsigned long*)(buf+36)) = fs; */
ex_setb32(buf+36,fs);
/* First Cluster Root Dir */
/* *((unsigned long*)(buf+44)) = 2; */
ex_setb32(buf+44,2);
/* VolumeID */
/* *((unsigned long*)(buf+67)) = 0x13371337; */
ex_setb32(buf+67,0);
/* Volume Label */
memCpy("EFSL-0.3.3 ",buf+71,11);
/* Filesystemtype */
memCpy("FAT32 ",buf+82,8);
/* Magic */
*(buf+510) = 0x55; *(buf+511) = 0xAA;
part_writeBuf(part,0,buf);
memClr(buf,512);
for(c=32;c<(32+2*fs);c++){
part_writeBuf(part,c,buf);
}
/* *(((unsigned long*)buf) )=0x0FFFFFF8;
*(((unsigned long*)buf)+1)=0x0FFFFFFF;
*(((unsigned long*)buf)+2)=0x0FFFFFF8; */
ex_setb32(buf+0,(euint32)0x0FFFFFF8);
ex_setb32(buf+4,(euint32)0x0FFFFFFF);
ex_setb32(buf+8,(euint32)0x0FFFFFF8);
part_writeBuf(part,32,buf);
part_writeBuf(part,32+fs,buf);
return(0);
}
#endif

View File

@ -0,0 +1,89 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : time.c *
* Release : 0.3 - devel *
* Description : This file contains functions for time support *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "time.h"
/*****************************************************************************/
euint16 fs_makeDate(void)
{
#ifndef DATE_TIME_SUPPORT
return(0);
#else
euint8 m,d;
euint16 y;
y = time_getYear()-1980;
m = time_getMonth();
d = time_getDay();
return(
(y>127?127<<9:(y&0x3F)<<9) |
((m==0||m>12)?1:(m&0xF)<<5) |
((d==0||d>31)?1:(d&0x1F))
);
#endif
}
/*****************************************************************************/
euint16 fs_makeTime(void)
{
#ifndef DATE_TIME_SUPPORT
return(0);
#else
euint8 s,m,h;
s = time_getSecond();
m = time_getMinute();
h = time_getHour();
return(
(h>23?0:(h&0x1F)<<11) |
(m>59?0:(m&0x3F)<<5) |
(s>59?0:(s-s%2)/2)
);
#endif
}
/*****************************************************************************/
euint8 fs_hasTimeSupport(void)
{
#ifdef DATE_TIME_SUPPORT
return(1);
#else
return(0);
#endif
}
/*****************************************************************************/

View File

@ -0,0 +1,226 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : ui.c *
* Release : 0.3 - devel *
* Description : This file contains functions which will be presented to the *
* user of this library. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "ui.h"
/*****************************************************************************/
/*****************************************************************************
* short listfiles(char *dir)
*
* Deschription: This function returns the number of files / directories
* inside the given directory.
*
* Return value: number of files/directories in the given directory or -1
* if directory does not exist.
\*****************************************************************************/
short listFiles(FileSystem *fs, char *dirname)
{
unsigned long startCluster;
unsigned char fileEntryCount;
unsigned short counter=0;
unsigned long offset=0;
FileRecord fileEntry;
FileLocation loc;
unsigned char buf[512];
File dir;
unsigned short i;
/* Find out if we are searching in the root dir or in */
if(dirname[0]=='/' && dirname[1]=='\0')
{
if( (fs->type == FAT12) || (fs->type == FAT16) )
{
for(i=0;i<=(fs->volumeId.RootEntryCount/16);i++)
{
loc.Sector=fs->FirstSectorRootDir + i;
part_readBuf(fs->part,loc.Sector,buf);
/* I STOPPED HERE*/
/* FIXME */
}
}
}
else /* Normal directory */
{
/* Check if path given is a directory */
if(fs_findFile(fs,dirname,&loc,0)!=2)
{
FUNC_OUT((TXT("")));
return(-1);
}
/* Find out what the startcluster of the directory is */
part_readBuf(fs->part,loc.Sector, buf);
fileEntry = *(((FileRecord*)buf) + loc.Offset);
startCluster = (((unsigned long)fileEntry.FirstClusterHigh)<<16)
+ fileEntry.FirstClusterLow;
/* Init of dir */
dir.fs=fs;
dir.Cache.LogicCluster=-1;
dir.Cache.FirstCluster=startCluster;
dir.DirEntry.Attribute=ATTR_DIRECTORY;
while((file_fread(&dir,offset,512,buf)))
{
DBG((TXT("Read 512 bytes from dir with offset %li.\n"),offset));
for(fileEntryCount=0;fileEntryCount<16;fileEntryCount++)
{
fileEntry = *(((FileRecord*)buf) + fileEntryCount);
if( !( (fileEntry.Attribute & 0x0F) == 0x0F ) )
{
if
(
(fileEntry.FileName[0]>='A' && fileEntry.FileName[0]<='Z')
||
(fileEntry.FileName[0]>='0' && fileEntry.FileName[0]<='9')
)
{
DBG((TXT("Filename: %s\n"),fileEntry.FileName));
counter++;
}
}
}
offset+=512;
}
}
FUNC_OUT((TXT("")));
return(counter);
return(-1);
}
/*****************************************************************************/
/* ****************************************************************************
* esint16 rmfile(FileSystem *fs,euint8* filename)
* Description: This function takes a filename as argument and deletes it,
* by freeing it's clusterchain, and deleting it's entry from the directory.
* Return value: 0 on success, -1 on errors, like file not found.
*/
esint16 rmfile(FileSystem *fs,euint8* filename)
{
FileLocation loc;
ClusterChain cache;
euint8* buf;
euint32 firstCluster=0;
if((fs_findFile(fs,(eint8*)filename,&loc,0))==1){
buf=part_getSect(fs->part,loc.Sector,IOM_MODE_READWRITE);
firstCluster = ex_getb16(buf+loc.Offset*32+20);
firstCluster <<= 16;
firstCluster += ex_getb16(buf+loc.Offset*32+26);
memClr(buf+(loc.Offset*32),32);
part_relSect(fs->part,buf);
cache.DiscCluster = cache.LastCluster = cache.Linear = cache.LogicCluster = 0;
cache.FirstCluster = firstCluster;
fat_unlinkClusterChain(fs,&cache);
return(0);
}
return(-1);
}
/*****************************************************************************/
esint8 mkdir(FileSystem *fs,eint8* dirname)
{
FileLocation loc;
FileRecord direntry;
euint32 nc,parentdir;
euint8* buf;
eint8 ffname[11];
if( fs_findFile(fs,dirname,&loc,&parentdir) ){
return(-1);
}
if(parentdir==0)return(-2);
if(!fs_findFreeFile(fs,dirname,&loc,0))return(-3);
/* You may never search for a free cluster, and the call
* functions that may cause changes to the FAT table, that
* is why getNextFreeCluster has to be called AFTER calling
* fs_findFreeFile, which may have to expand a directory in
* order to store the new filerecord !!
*/
nc = fs_getNextFreeCluster(fs,fs_giveFreeClusterHint(fs));
if(nc==0)return(0);
fs_clearCluster(fs,nc);
buf = part_getSect(fs->part,loc.Sector,IOM_MODE_READWRITE);
dir_getFatFileName(dirname,ffname);
memClr(&direntry,sizeof(direntry));
memCpy(ffname,&direntry,11);
direntry.FileSize = 0;
direntry.FirstClusterHigh=nc>>16;
direntry.FirstClusterLow=nc&0xFFFF;
direntry.Attribute = ATTR_DIRECTORY;
memCpy(&direntry,buf+(32*loc.Offset),32);
part_relSect(fs->part,buf);
buf = part_getSect(fs->part,fs_clusterToSector(fs,nc),IOM_MODE_READWRITE);
memClr(&direntry,sizeof(direntry));
memCpy(". ",&direntry,11);
direntry.Attribute = ATTR_DIRECTORY;
direntry.FileSize = 0;
direntry.FirstClusterHigh=nc>>16;
direntry.FirstClusterLow=nc&0xFFFF;
memCpy(&direntry,buf,32);
if(fs->type == FAT32 && parentdir == fs->volumeId.RootCluster){
parentdir = 0;
}
if(fs->type != FAT32 && parentdir<=1){
parentdir = 0;
}
memClr(&direntry,sizeof(direntry));
memCpy(".. ",&direntry,11);
direntry.Attribute = ATTR_DIRECTORY;
direntry.FileSize = 0;
direntry.FirstClusterHigh=parentdir>>16;
direntry.FirstClusterLow=parentdir&0xFFFF;
memCpy(&direntry,buf+32,32);
part_relSect(fs->part,buf);
fat_setNextClusterAddress(fs,nc,fat_giveEocMarker(fs));
return(0);
}

View File

@ -0,0 +1,15 @@
CONFDIR=../../../conf
include $(CONFDIR)/config.makefile
BASEDIR=../../base/include
INCLUDEDIRS=-I../../include -Iinclude -I$(BASEDIR) -I$(CONFDIR)
CFLAGS=$(GCFLAGS) $(INCLUDEDIRS)
OBJ=atmega_spi.o
all: efsl-hwd-atmega_spi
efsl-hwd-atmega_spi: $(OBJ)
$(AR) rcs efsl-hwd-atmega_spi.a $(OBJ)
clean:
rm -f $(OBJ) efsl-hwd-atmega_spi.a

View File

@ -0,0 +1,79 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : atmega_spi.c *
* Release : 0.3 - devel *
* Description : This file contains the functions needed to use efs for *
* accessing files on an SD-card connected to an ATMega's. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
/*****************************************************************************/
#include "include/atmega_spi.h"
/*****************************************************************************/
euint8 atmega_spi_init(atmegaSpiInterface *iface)
{
euint8 i;
/* Unselect card */
PORTB |= iface->pinSelect;
/* Set as master, clock and chip select output */
DDR_SPI = (1<<DD_MOSI) | (1<<DD_SCK) | 1;
/* Enable SPI, master, set clock rate to fck/2 */
SPCR = (1<<SPE) | (1<<MSTR); /* fsck / 4 */
SPSR = 1; /* fsck / 2 */
/* Send 10 spi commands with card not selected */
for(i=0;i<10;i++)
atmega_spi_send(iface,0xff);
/* Select card */
PORTB &= ~(iface->pinSelect);
return(0);
}
/*****************************************************************************/
euint8 atmega_spi_send(atmegaSpiInterface *iface, euint8 data)
{
euint8 incoming=0;
PORTB &= ~(iface->pinSelect);
SPDR = data;
while(!(SPSR & (1<<SPIF)))
incoming = SPDR;
PORTB |= iface->pinSelect;
return(incoming);
}
/*****************************************************************************/

View File

@ -0,0 +1,69 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : atmega_spi.h *
* Release : 0.3 - devel *
* Description : This file contains the functions needed to use efs for *
* accessing files on an SD-card connected to an ATMega's. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __ATMEGA_SPI_H__
#define __ATMEGA_SPI_H__
#define __AVR_ATmega128__ 1
#define FALSE 0X00
#define TRUE 0x01
#define DDR_SPI DDRB
#define DD_MOSI DDB2
#define DD_SCK DDB1
#include <avr/io.h>
#include <compat/ina90.h>
#include "debug.h"
#include "config.h"
/*************************************************************\
hwInterface
----------
* long sectorCount Number of sectors on the file.
*
* Remark: Configuration of the spi-port should become part of
* this configuration.
\*************************************************************/
struct _atmegaSpiInterface{
/*euint8 portSelect;*/ /* TODO */
euint8 pinSelect;
};
typedef struct _atmegaSpiInterface atmegaSpiInterface;
euint8 atmega_spi_init(atmegaSpiInterface *iface);
euint8 atmega_spi_send(atmegaSpiInterface *iface, euint8 data);
#endif

View File

@ -0,0 +1,15 @@
CONFDIR=../../../conf
include $(CONFDIR)/config.makefile
BASEDIR=../../base/include
INCLUDEDIRS=-I../../include -Iinclude -I$(BASEDIR) -I$(CONFDIR)
CFLAGS=$(GCFLAGS) $(INCLUDEDIRS)
OBJ=helper.o linuxfile.o
all: efsl-hwd-linuxfile
efsl-hwd-linuxfile: $(OBJ)
$(AR) rcs efsl-hwd-linuxfile.a $(OBJ)
clean:
rm -f $(OBJ) efsl-hwd-linuxfile.a

View File

@ -0,0 +1,95 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : helper.c *
* Release : 0.3 - devel *
* Description : These functions may NOT BE USED ANYWHERE ! They are helper *
* functions for the Linux based developement. They use the GNU *
* C library and headers. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#include "helper.h"
void* Malloc(eint32 size)
{
void*x;
if((x=malloc(size))==NULL){
perror("Malloc: ");
exit(-1);
}
return(x);
}
void Fopen(FILE **f,eint8* filename)
{
*f=fopen(filename,"r+");
if(*f==NULL){
perror("Fopen: ");
exit(-1);
}
}
void MMap(eint8* filename,void**x,eint32 *size)
{
FILE *tmp;
eint32 filesize,c;
Fopen(&tmp,filename);
filesize=getFileSize(tmp);
*x=Malloc(filesize);
for(c=0;c<filesize;c++)*((char*)(*x)+c)=fgetc(tmp);
*size=filesize;
fclose(tmp);
}
int getFileSize(FILE* file)
{
eint32 c=0;
fseek(file,0,SEEK_END);
c=ftell(file);
return(c);
}
void PrintBuf(euint8* buf)
{
euint16 c,cc;
for(c=0 ; c<32 ; c++){
printf("\n%4x : ",c*16);
for(cc=0;cc<16;cc++){
printf("%2x ",buf[c*16+cc]);
}
printf(" ");
for(cc=0;cc<16;cc++){
printf("%c",buf[c*16+cc]>=32?buf[c*16+cc]:'*');
}
}
printf("\n");
}

View File

@ -0,0 +1,52 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : helper.c *
* Release : 0.3 - devel *
* Description : These functions may NOT BE USED ANYWHERE ! They are helper *
* functions for the Linux based developement. They use the GNU *
* C library and headers. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __HELPER_H__
#define __HELPER_H__
/*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "debug.h"
#include "types.h"
#include "config.h"
/*****************************************************************************/
void* Malloc(eint32 size);
void Fopen(FILE **f,eint8* filename);
void MMap(eint8* filename,void**x,eint32* size);
int getFileSize(FILE* file);
#endif

View File

@ -0,0 +1,73 @@
/*****************************************************************************\
* EFSL - Embedded Filesystems Library *
* ----------------------------------- *
* *
* Filename : linuxfile.h *
* Release : 0.3 - devel *
* Description : This file contains the functions needed to use efs for *
* accessing files under linux. This interface is meant *
* to be used for debugging purposes. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; version 2 *
* of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or *
* use macros or inline functions from this file, or you compile this *
* file and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with section (3) of *
* the GNU General Public License. *
* *
* This exception does not invalidate any other reasons why a work based *
* on this file might be covered by the GNU General Public License. *
* *
* (c)2006 Lennart Yseboodt *
* (c)2006 Michael De Nil *
\*****************************************************************************/
#ifndef __LINUXFILE_H__
#define __LINUXFILE_H__
/*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
#include "debug.h"
#include "types.h"
#include "ioctl.h"
#include "config.h"
/*****************************************************************************/
#define _LARGEFILE_SOURCE
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
/*************************************************************\
hwInterface
----------
* FILE* imagefile File emulation of hw interface.
* long sectorCount Number of sectors on the file.
\*************************************************************/
struct _linuxFileInterface{
FILE *imageFile;
euint8* fileName;
eint32 sectorCount;
};
typedef struct _linuxFileInterface linuxFileInterface;
esint8 lf_init(void* LFI);
esint8 lf_readBuf(void* LFI,euint32 address,euint8* buf);
esint8 lf_writeBuf(void* LFI,euint32 address,euint8* buf);
esint8 lf_ioctl(void* LFI,euint16 ctl,void* data);
esint8 lf_setPos(void* LFI,euint32 address);
#endif

Some files were not shown because too many files have changed in this diff Show More