提交 0b4c769f 编写于 作者: G goprife@gmail.com

add uffs (new version, new port)

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2015 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 34678294

要显示的变更太多。

To preserve performance only 1000 of 1000+ files are displayed.
Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
CMAKE_MINIMUM_REQUIRED(VERSION 2.6 )
PROJECT( uffs )
SET(CMAKE_CXX_FLAGS_VGL "-O0 -g")
SET(CMAKE_C_FLAGS_VGL "-O0 -g")
SET(CMAKE_C_FLAGS_GCOV "-g -O0 -Wall -fprofile-arcs -ftest-coverage")
SET(CMAKE_CXX_FLAGS_GCOV "-g -O0 -Wall -fprofile-arcs -ftest-coverage")
SET(CMAKE_EXE_LINKER_FLAGS_GCOV "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage -lgcov")
IF (CMAKE_COMPILER_IS_GNUCC)
IF (APPLE)
SET(CMAKE_CXX_FLAGS "-fPIC -Wall -Werror -g -rdynamic")
SET(CMAKE_C_FLAGS "-fPIC -Wall -Werror -g -rdynamic")
ELSE ()
SET(CMAKE_CXX_FLAGS "-fPIC -Wall -Werror -Wclobbered -Wempty-body -Wignored-qualifiers -Wmissing-parameter-type -Woverride-init -Wtype-limits -Wuninitialized -g -rdynamic")
SET(CMAKE_C_FLAGS "-fPIC -Wall -Werror -Wclobbered -Wempty-body -Wignored-qualifiers -Wmissing-parameter-type -Woverride-init -Wtype-limits -Wuninitialized -g -rdynamic")
ENDIF()
ENDIF()
IF (UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNIX")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUNIX")
ENDIF()
ADD_SUBDIRECTORY( src )
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, 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.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, 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 or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
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 give any other recipients of the Program a copy of this License
along with the Program.
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 Program or any portion
of it, thus forming a work based on the Program, 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) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
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 Program, 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 Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) 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; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, 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 executable. However, as a
special exception, the source code 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.
If distribution of executable or 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 counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program 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.
5. 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 Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program 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 to
this License.
7. 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 Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program 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 Program.
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.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program 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.
9. The Free Software Foundation may publish revised and/or new versions
of the 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 Program
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 Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, 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
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "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 PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. 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 PROGRAM 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 PROGRAM (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 PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), 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 Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. 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 program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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; either version 2 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.
# Doxyfile 1.4.1-KDevelop
#---------------------------------------------------------------------------
# Project related configuration options
#---------------------------------------------------------------------------
PROJECT_NAME = uffs-doc
PROJECT_NUMBER = 0.1
OUTPUT_DIRECTORY = doc/doxygen-doc
CREATE_SUBDIRS = NO
OUTPUT_LANGUAGE = English
USE_WINDOWS_ENCODING = NO
BRIEF_MEMBER_DESC = YES
REPEAT_BRIEF = YES
ABBREVIATE_BRIEF = "The $name class" \
"The $name widget" \
"The $name file" \
is \
provides \
specifies \
contains \
represents \
a \
an \
the
ALWAYS_DETAILED_SEC = NO
INLINE_INHERITED_MEMB = NO
FULL_PATH_NAMES = YES
STRIP_FROM_PATH = ./
STRIP_FROM_INC_PATH =
SHORT_NAMES = NO
JAVADOC_AUTOBRIEF = NO
MULTILINE_CPP_IS_BRIEF = NO
DETAILS_AT_TOP = NO
INHERIT_DOCS = YES
DISTRIBUTE_GROUP_DOC = NO
TAB_SIZE = 4
ALIASES =
OPTIMIZE_OUTPUT_FOR_C = YES
OPTIMIZE_OUTPUT_JAVA = NO
SUBGROUPING = YES
#---------------------------------------------------------------------------
# Build related configuration options
#---------------------------------------------------------------------------
EXTRACT_ALL = YES
EXTRACT_PRIVATE = NO
EXTRACT_STATIC = NO
EXTRACT_LOCAL_CLASSES = NO
EXTRACT_LOCAL_METHODS = NO
HIDE_UNDOC_MEMBERS = NO
HIDE_UNDOC_CLASSES = NO
HIDE_FRIEND_COMPOUNDS = YES
HIDE_IN_BODY_DOCS = NO
INTERNAL_DOCS = YES
CASE_SENSE_NAMES = NO
HIDE_SCOPE_NAMES = NO
SHOW_INCLUDE_FILES = YES
INLINE_INFO = YES
SORT_MEMBER_DOCS = YES
SORT_BRIEF_DOCS = YES
SORT_BY_SCOPE_NAME = YES
GENERATE_TODOLIST = YES
GENERATE_TESTLIST = YES
GENERATE_BUGLIST = YES
GENERATE_DEPRECATEDLIST= YES
ENABLED_SECTIONS =
MAX_INITIALIZER_LINES = 30
SHOW_USED_FILES = YES
SHOW_DIRECTORIES = YES
FILE_VERSION_FILTER =
#---------------------------------------------------------------------------
# configuration options related to warning and progress messages
#---------------------------------------------------------------------------
QUIET = NO
WARNINGS = YES
WARN_IF_UNDOCUMENTED = YES
WARN_IF_DOC_ERROR = YES
WARN_NO_PARAMDOC = NO
WARN_FORMAT = "$file:$line: $text"
WARN_LOGFILE =
#---------------------------------------------------------------------------
# configuration options related to the input files
#---------------------------------------------------------------------------
INPUT = ./src
FILE_PATTERNS = *.c \
*.cc \
*.cxx \
*.cpp \
*.c++ \
*.java \
*.ii \
*.ixx \
*.ipp \
*.i++ \
*.inl \
*.h \
*.hh \
*.hxx \
*.hpp \
*.h++ \
*.idl \
*.odl \
*.cs \
*.php \
*.php3 \
*.inc \
*.m \
*.mm \
*.dox \
*.C \
*.CC \
*.C++ \
*.II \
*.I++ \
*.H \
*.HH \
*.H++ \
*.CS \
*.PHP \
*.PHP3 \
*.M \
*.MM \
*.C \
*.H \
*.tlh \
*.diff \
*.patch \
*.moc \
*.xpm \
*.dox
RECURSIVE = YES
EXCLUDE =
EXCLUDE_SYMLINKS = NO
EXCLUDE_PATTERNS =
EXAMPLE_PATH =
EXAMPLE_PATTERNS = *
EXAMPLE_RECURSIVE = NO
IMAGE_PATH =
INPUT_FILTER =
FILTER_PATTERNS =
FILTER_SOURCE_FILES = NO
#---------------------------------------------------------------------------
# configuration options related to source browsing
#---------------------------------------------------------------------------
SOURCE_BROWSER = YES
INLINE_SOURCES = NO
STRIP_CODE_COMMENTS = YES
REFERENCED_BY_RELATION = YES
REFERENCES_RELATION = YES
VERBATIM_HEADERS = YES
#---------------------------------------------------------------------------
# configuration options related to the alphabetical class index
#---------------------------------------------------------------------------
ALPHABETICAL_INDEX = YES
COLS_IN_ALPHA_INDEX = 5
IGNORE_PREFIX =
#---------------------------------------------------------------------------
# configuration options related to the HTML output
#---------------------------------------------------------------------------
GENERATE_HTML = YES
HTML_OUTPUT = html
HTML_FILE_EXTENSION = .html
HTML_HEADER =
HTML_FOOTER =
HTML_STYLESHEET =
HTML_ALIGN_MEMBERS = YES
GENERATE_HTMLHELP = NO
CHM_FILE =
HHC_LOCATION =
GENERATE_CHI = NO
BINARY_TOC = NO
TOC_EXPAND = NO
DISABLE_INDEX = NO
ENUM_VALUES_PER_LINE = 4
GENERATE_TREEVIEW = YES
TREEVIEW_WIDTH = 250
#---------------------------------------------------------------------------
# configuration options related to the LaTeX output
#---------------------------------------------------------------------------
GENERATE_LATEX = NO
LATEX_OUTPUT = latex
LATEX_CMD_NAME = latex
MAKEINDEX_CMD_NAME = makeindex
COMPACT_LATEX = NO
PAPER_TYPE = a4wide
EXTRA_PACKAGES =
LATEX_HEADER =
PDF_HYPERLINKS = NO
USE_PDFLATEX = NO
LATEX_BATCHMODE = NO
LATEX_HIDE_INDICES = NO
#---------------------------------------------------------------------------
# configuration options related to the RTF output
#---------------------------------------------------------------------------
GENERATE_RTF = NO
RTF_OUTPUT = rtf
COMPACT_RTF = NO
RTF_HYPERLINKS = NO
RTF_STYLESHEET_FILE =
RTF_EXTENSIONS_FILE =
#---------------------------------------------------------------------------
# configuration options related to the man page output
#---------------------------------------------------------------------------
GENERATE_MAN = NO
MAN_OUTPUT = man
MAN_EXTENSION = .3
MAN_LINKS = NO
#---------------------------------------------------------------------------
# configuration options related to the XML output
#---------------------------------------------------------------------------
GENERATE_XML = NO
XML_OUTPUT = xml
XML_SCHEMA =
XML_DTD =
XML_PROGRAMLISTING = YES
#---------------------------------------------------------------------------
# configuration options for the AutoGen Definitions output
#---------------------------------------------------------------------------
GENERATE_AUTOGEN_DEF = NO
#---------------------------------------------------------------------------
# configuration options related to the Perl module output
#---------------------------------------------------------------------------
GENERATE_PERLMOD = NO
PERLMOD_LATEX = NO
PERLMOD_PRETTY = YES
PERLMOD_MAKEVAR_PREFIX =
#---------------------------------------------------------------------------
# Configuration options related to the preprocessor
#---------------------------------------------------------------------------
ENABLE_PREPROCESSING = YES
MACRO_EXPANSION = NO
EXPAND_ONLY_PREDEF = NO
SEARCH_INCLUDES = YES
INCLUDE_PATH =
INCLUDE_FILE_PATTERNS =
PREDEFINED =
EXPAND_AS_DEFINED =
SKIP_FUNCTION_MACROS = YES
#---------------------------------------------------------------------------
# Configuration::additions related to external references
#---------------------------------------------------------------------------
TAGFILES =
GENERATE_TAGFILE = uffs.tag
ALLEXTERNALS = NO
EXTERNAL_GROUPS = YES
PERL_PATH = /usr/bin/perl
#---------------------------------------------------------------------------
# Configuration options related to the dot tool
#---------------------------------------------------------------------------
CLASS_DIAGRAMS = YES
HIDE_UNDOC_RELATIONS = YES
HAVE_DOT = NO
CLASS_GRAPH = YES
COLLABORATION_GRAPH = YES
GROUP_GRAPHS = YES
UML_LOOK = NO
TEMPLATE_RELATIONS = NO
INCLUDE_GRAPH = YES
INCLUDED_BY_GRAPH = YES
CALL_GRAPH = NO
GRAPHICAL_HIERARCHY = YES
DIRECTORY_GRAPH = YES
DOT_IMAGE_FORMAT = png
DOT_PATH =
DOTFILE_DIRS =
MAX_DOT_GRAPH_WIDTH = 1024
MAX_DOT_GRAPH_HEIGHT = 1024
MAX_DOT_GRAPH_DEPTH = 1000
DOT_TRANSPARENT = NO
DOT_MULTI_TARGETS = NO
GENERATE_LEGEND = YES
DOT_CLEANUP = YES
#---------------------------------------------------------------------------
# Configuration::additions related to the search engine
#---------------------------------------------------------------------------
SEARCHENGINE = NO
UFFS: Ultra-low-cost Flash File System
Project: http://uffs.sf.net/
Blog: http://all-about-uffs.blogspot.com/
Q/A: http://groups.google.com/group/uffs/
Author: Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
INTRODUCTION
------------
UFFS is a nand flash file system designed for embedded system.
UFFS have some unique and advanced features:
* Low cost: e.g. it needs only 41K bytes RAM for 64MB NAND flash (page size 2048).
* Fast booting: it reads only a few spares from each block, typically
mounting a fully filled file system (Gbits) within one second.
* Superb Reliability:
- The file system is designed for the embedded system which may
frequently lost power/reset without care.
- Journal file system, the file system will automatically rollback
to the last state when lost power on the middle of flash programing.
- When 'write' return without error, the data is guarenteed been
saved on flash.
* Fast file create/read/write/seek.
* Bad-block tolerant, ECC enable and good ware-leveling.
* There is no garbage collection needed for UFFS.
* Support multiple NAND flash class in one system.
* Support bare flash hardware, no operating system needed.
* Support static memory allocation (works without 'malloc').
* Fully simulated on PC (Windows/Linux) platform.
Disadvantage:
* space inefficency for small files: UFFS use at least one
'block'(the minial erase unit for NAND flash, e.g. 16K ) for a file.
* maximum supported blocks: 2^16 = 65535
Memory consuming example:
For page size = 512:
[VARY]Tree nodes: 16 * total_blocks
[CONST]Page Bufs: MAX_CACHED_BUFFERS(10) * (40 + pageSize(512)) = 5.4K
[CONST]Block Info caches: (24 + 14 * pages_per_block (32)) * MAX_CACHED_BLOCK_INFO (10) = 4.6K
Example 1: 128M bytes NAND, 8192 blocks, total memory cost:
(16 * 8192)128K + 5.4K + 4.6K = 138K bytes.
Example 2: 32M Bytes NAND, 2048 blocks, total memory cost:
(16 * 2048)32K + 5.4K + 4.6K = 42K bytes.
Example 3: 16M bytes NAND, 1024 blocks, total memory cost:
(16 * 1024)16K + 5.4K + 4.6K = 26K bytes.
For page size = 2048:
[VARY]Tree nodes: 16 * total_blocks
[CONST]Page Bufs: MAX_CACHED_BUFFERS(10) * (40 + pageSize(2048)) = 20.4K
[CONST]Block Info caches: (24 + 14 * pages_per_block (32)) * MAX_CACHED_BLOCK_INFO (10) = 4.6K
Example 1: 512M bytes NAND, 8192 blocks, total memory cost:
(16 * 8192)128K + 20.4K + 4.6K = 153K bytes.
Example 2: 128M Bytes NAND, 2048 blocks, total memory cost:
(16 * 2048)32K + 20.4K + 4.6K = 57K bytes.
Example 3: 64M bytes NAND, 1024 blocks, total memory cost:
(16 * 1024)16K + 20.4K + 4.6K = 41K bytes.
BUILD SIMULATOR REQUIREMENT
---------------------------
From V1.2.0, build uffs simulator requires 'cmake'.
'cmake' can be downloaded from: http://www.cmake.org/
or, under Debian/Ubuntu:
sudo apt-get install cmake
BUILD SIMULATOR ON LINUX
------------------------
1) create a 'build' dir:
mkdir -p ~/build/uffs
2) create Makefiles and build:
cd ~/build/uffs
cmake <path_to_uffs>
make
5) run simulator (interactive mode):
src/utils/mkuffs
BUILD SIMULATOR ON WINDOWS
--------------------------
1) create a 'build' dir along with uffs source dir,
d:\build\uffs
2) Create VC project files:
cd build\uffs
cmake <path_to_uffs>
3) Open uffs.dsw (or uffs.sln for VC > 6 ), compile & run.
LATEST SOURCE CODE
------------------
You can get the latest source code from git repository:
git clone git://uffs.git.sourceforge.net/gitroot/uffs/uffs
CURRENT STATUS
--------------
UFFS 0.1.x is a working version on PC simulator, also has been ported to
uBase embedded OS as a 'real world' product for thousands of copies,
it works fine so far.
UFFS 0.2.0 implementes full directory.
UFFS 1.0.0 is the first stable release at sf.net.
UFFS 1.1.0: support NAND flash with large page size (up to 2K).
UFFS 1.1.1: bug fixes. a tool for making uffs disk image.
UFFS 1.1.2: bug fixes. add more Flash Class. change Licence from GNU GPLv2 to GNU LGPLv2
UFFS 1.2.0:
- eliminate 'current path' and relatives. Now you should use absolute path in all
uffs APIs. For dir, the fullname should end with '/'.
- allow using static memory allocation, 'malloc' is no longer needed.
- using cmake for building simulator.
- bug fixes & minor changes.
UFFS 1.2.1:
- improve bad block management
- bug fixes
- change Licence to modified GNU GPLv2.
UFFS 1.3.0:
- improved flash interface
- support hardware ECC
- support user defined spare layout (for customized NAND flash controller)
- support 4K page size
- no partial page program required, support MLC NAND flash
- reduced buffer flushes by grouping buffers
- structual improvments and bug fixes
UFFS v1.3.1:
- Tidy up three memory allocators: static, native and system.
- Fix bugs in flash interface example.
- Fix memory allocation bugs when using static memory allocator.
- Add flash driver interface 'WriteFullPage()'.
- Fix compilation errors for BlackFin DSP compiler.
UFFS v1.3.2:
- Add POSIX like file system APIs.
- Bug fixes.
UFFS v1.3.3:
- Change Flash Interface, simplify interface.
- Improved bad block handling.
- Better support for MLC NAND flash.
- Added hardware ECC and RS-ECC controller emulator.
- Bug fixes.
UFFS v1.3.4
- New UO_NOECC option for skipping ECC (fast reading).
- POSIX compliance uffs_seek().
- Improved unclean page detection (add new 'seal' byte in spare area).
- Optional page data CRC.
- Bug fixes.
- Other improvements.
LICENCE
-------
From v1.2.1, UFFS is released under a modified GNU GPLv2. (the same as eCos Licence)
The full licence text can be found in the header of source files:
UFFS is free software; you can redistribute it and/or modify it under
the GNU Library General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
UFFS 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
or GNU Library General Public License, as applicable, for more details.
You should have received a copy of the GNU General Public License
and GNU Library General Public License along with UFFS; if not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
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 v2.
This exception does not invalidate any other reasons why a work based
on this file might be covered by the GNU General Public License.
TESTING UFFS WITH SQLITE3 REGRESSION TEST CASES
-----------------------------------------------
UFFS can be tested with sqlite3 regression test cases (on Linux).
1) install tcl8.5-dev on host PC:
apt-get install tcl8.5 tcl8.5-dev
# make sure your Linux is using tcl8.5 as the default tclsh:
sudo update-alternatives --config tclsh
(select "tclsh8.5" from the list.)
2) build uffs:
mkdir -p ~/build/uffs
cd ~/build/uffs
cmake <path_to_uffs>
3) build sqlite3:
cd <path_to_uffs>/src/test/sqlite3/sqlite-src-3070900
./configure # create some build support files
git checkout -f Makefile config.h # restore modified Makefile and config.h
make test # start build 'testfixture' program.
# now you'll see something like:
Connect: Connection refused
Assert (uffs_ret == ret && ret == bak_ret) fail at /home/.../src/test/api_test/os_uffs.c:os_unlink:329: unlink("/home/.../src/test/sqlite3/sqlite-src-3070900/./test.db-journal"), unix return 0, uffs return -1, bak return -1
make: *** [test] Error 1
4) run test cases:
Open two terminals.
on termional A:
cd ~/build/uffs
src/utils/mkuffs -t 1024
#on uffs simulator command line, enter:
format /
apisrv
on terminal B:
cd <path_to_uffs>/src/test/sqlite3/sqlite-src-3070900
./test-uffs.sh
Note: if you want to run mkuffs on another PC, for example, a Windows PC, you need to specify the IP address in test-uffs.sh.
The test will take 1~4 hours, depends on how fast your Linux box is.
ACKNOWLEDGMENT
---------------
Special thanks for your contributions to:
(list in no particular order)
* Chen Jun <chj@nlscan.com>
* Michail <digiolog@mail.ru>
* Sjpu <sjpu@163.com>
* RobertGray <xennex@hotmail.com>
* Dongbo <dongbo@ftsafe.com>
* Cag <seucag@hotmail.com>
* Sergey <s_sazonov@m2m-t.ru>
* Chris Conrad <chris.conrad@halliburton.com>
* Vladimir <decoder@rambler.ru>
* Thien Pham <thienpham2008@yahoo.com>
* Emmanuel Blot <eblot.ml@gmail.com>
* Michael <yowong2@gmail.com>
* Mick D <mick-eng@sourceforge.net>
* Paul <paulr227@gmail.com>
* Rogerz <rogerz.zhang@gmail.com>
TODO list for v1.3:
* New API: int uffs_SkipObject(uffs_Object *obj, int size);
* Introduce buffer group
* Interface to Linux MTD
/*
* File : rtthread.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006-2012, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE.
*
* Change Logs:
* Date Author Notes
* 2011-10-22 prife the first version
* 2012-03-28 prife use mtd device interface
*/
#include <rtthread.h>
#include <dfs_fs.h>
#include <dfs_def.h>
#include <rtdevice.h>
#include "dfs_uffs.h"
#include "uffs/uffs_fd.h" /* posix file api is here */
#include "uffs/uffs_mtb.h"
#include "uffs/uffs_mem.h"
#include "uffs/uffs_utils.h"
static URET uffs_mount_device(struct uffs_MountTableEntrySt *work);
static URET uffs_unmount_device(struct uffs_MountTableEntrySt *work);
static URET uffs_UnRegisterMountTable(uffs_MountTable *mtab);
/*
* RT-Thread DFS Interface for uffs
*/
#define UFFS_DEVICE_MAX 2 /* the max partions on a nand deivce*/
#define UFFS_MOUNT_PATH_MAX 128 /* the mount point max length */
#define FILE_PATH_MAX 256 /* the longest file path */
struct _nand_dev
{
struct rt_mtd_nand_device * dev;
struct uffs_StorageAttrSt storage;
uffs_Device uffs_dev;
uffs_MountTable mount_table;
char mount_path[UFFS_MOUNT_PATH_MAX];
void * data; /* when uffs use static buf, it will save ptr here */
};
/* make sure the following struct var had been initilased to 0! */
static struct _nand_dev nand_part[UFFS_DEVICE_MAX] = {0};
static int uffs_result_to_dfs(int result)
{
int status = -1;
switch (result)
{
case UENOERR:/** no error */
break;
case UEACCES:/** Tried to open read-only file for writing, or files sharing mode
does not allow specified operations, or given path is directory */
status = -DFS_STATUS_EINVAL;
break;/* no suitable */
case UEEXIST: /** _O_CREAT and _O_EXCL flags specified, but filename already exists */
status = -DFS_STATUS_EEXIST;
break;
case UEINVAL: /** Invalid oflag or pmode argument */
status = -DFS_STATUS_EINVAL;
break;
case UEMFILE: /** No more file handles available(too many open files) */
status = -1;
break;
case UENOENT: /** file or path not found */
status = -DFS_STATUS_ENOENT;
break;
case UETIME: /** can't set file time */
status = -1;
break;
case UEBADF: /** invalid file handle */
status = -DFS_STATUS_EBADF;
break;
case UENOMEM:/** no enough memory */
status = -DFS_STATUS_ENOMEM;
break;
case UEIOERR: /** I/O error from lower level flash operation */
status = -DFS_STATUS_EIO;
break;
case UENOTDIR: /** Not a directory */
status = -DFS_STATUS_ENOTDIR;
break;
case UEISDIR: /** Is a directory */
status = -DFS_STATUS_EISDIR;
break;
case UEUNKNOWN_ERR:
default:
status = -1;
break; /* unknown error! */
}
return status;
}
static URET _device_init(uffs_Device *dev)
{
dev->attr->_private = NULL; // hook nand_chip data structure to attr->_private
dev->ops = (struct uffs_FlashOpsSt *)&nand_ops;
return U_SUCC;
}
static URET _device_release(uffs_Device *dev)
{
return U_SUCC;
}
static int init_uffs_fs(
struct _nand_dev * nand_part)
{
uffs_MountTable * mtb;
struct rt_mtd_nand_device * nand;
struct uffs_StorageAttrSt * flash_storage;
mtb = &nand_part->mount_table;
nand = nand_part->dev;
flash_storage = &nand_part->storage;
/* setup nand storage attributes */
uffs_setup_storage(flash_storage, nand);
/* register mount table */
if(mtb->dev)
{
/* set memory allocator for uffs */
#if CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR > 0
uffs_MemSetupSystemAllocator(&mtb->dev->mem);
#endif
/* setup device init/release entry */
mtb->dev->Init = _device_init;
mtb->dev->Release = _device_release;
mtb->dev->attr = flash_storage;
uffs_RegisterMountTable(mtb);
}
/* mount uffs partion on nand device */
return uffs_mount_device(mtb) == U_SUCC ? 0 : -1;
}
static int dfs_uffs_mount(
struct dfs_filesystem* fs,
unsigned long rwflag,
const void* data)
{
rt_base_t index;
uffs_MountTable * mount_part;
struct rt_mtd_nand_device * dev;
RT_ASSERT(rt_strlen(fs->path) < (UFFS_MOUNT_PATH_MAX-1));
dev = RT_MTD_NAND_DEVICE(fs->dev_id);
/*1. find a empty entry in partition table */
for (index = 0; index < UFFS_DEVICE_MAX ; index ++)
{
if (nand_part[index].dev == RT_NULL)
break;
}
if (index == UFFS_DEVICE_MAX)
return -DFS_STATUS_ENOENT;
/*2. fill partition structure */
nand_part[index].dev = dev;
/* make a right mount path for uffs, end with '/' */
rt_snprintf(nand_part[index].mount_path, UFFS_MOUNT_PATH_MAX, "%s/", fs->path);
if (nand_part[index].mount_path[1] == '/')
nand_part[index].mount_path[1] = 0;
mount_part = &(nand_part[index].mount_table);
mount_part->mount = nand_part[index].mount_path;
mount_part->dev = &(nand_part[index].uffs_dev);
rt_memset(mount_part->dev, 0, sizeof(uffs_Device));//in order to make uffs happy.
mount_part->dev->_private = dev; /* save dev_id into uffs */
mount_part->start_block = dev->block_start;
mount_part->end_block = dev->block_end;
/*3. mount uffs */
if (init_uffs_fs(&nand_part[index]) < 0)
{
return uffs_result_to_dfs(uffs_get_error());
}
return 0;
}
static int dfs_uffs_unmount(struct dfs_filesystem* fs)
{
rt_base_t index;
int result;
/* find the device index and then unmount it */
for (index = 0; index < UFFS_DEVICE_MAX; index++)
{
if (nand_part[index].dev == RT_MTD_NAND_DEVICE(fs->dev_id))
{
nand_part[index].dev = RT_NULL;
result = uffs_unmount_device(& nand_part[index].mount_table);
if (result != U_SUCC)
break;
result = uffs_UnRegisterMountTable(& nand_part[index].mount_table);
return (result == U_SUCC) ? DFS_STATUS_OK : -1;
}
}
return -DFS_STATUS_ENOENT;
}
static int dfs_uffs_mkfs(const char* device_name)
{
rt_base_t index;
rt_uint32_t block;
struct rt_mtd_nand_device * mtd;
/*1. find the device index */
for (index = 0; index < UFFS_DEVICE_MAX; index++)
{
if (rt_strncmp(nand_part[index].dev->parent.parent.name,
device_name, RT_NAME_MAX) == 0)
break;
}
if (index == UFFS_DEVICE_MAX)
{
/* can't find device driver */
rt_kprintf("can not find device driver: %s\n", device_name);
return -DFS_STATUS_ENOENT;
}
/*2. then unmount the partition */
uffs_mount_device(&nand_part[index].mount_table);
mtd = nand_part[index].dev;
/*3. erase all blocks on the partition */
block = mtd->block_start;
for (; block <= mtd->block_end; block++)
{
rt_mtd_nand_erase_block(mtd, block);
if (rt_mtd_nand_check_block(mtd, block) != RT_EOK)
{
rt_kprintf("found bad block %d\n", block);
rt_mtd_nand_mark_badblock(mtd, block);
}
}
/*4. remount it */
if (init_uffs_fs(&nand_part[index]) < 0)
{
return uffs_result_to_dfs(uffs_get_error());
}
return DFS_STATUS_OK;
}
static int dfs_uffs_statfs(struct dfs_filesystem* fs,
struct statfs *buf)
{
rt_base_t index;
struct rt_mtd_nand_device * mtd = RT_MTD_NAND_DEVICE(fs->dev_id);
RT_ASSERT(mtd != RT_NULL);
/* find the device index */
for (index = 0; index < UFFS_DEVICE_MAX; index++)
{
if (nand_part[index].dev == (void *)mtd)
break;
}
if (index == UFFS_DEVICE_MAX)
return -DFS_STATUS_ENOENT;
buf->f_bsize = mtd->page_size;
buf->f_blocks = (mtd->block_size)/(mtd->page_size)*
(mtd->block_start - mtd->block_end + 1);
buf->f_bfree = uffs_GetDeviceFree(&nand_part[index].uffs_dev) / mtd->page_size;
return 0;
}
struct _uffs_data
{
int fd; /* the handle of an open file/entry */
char * path; /* the path of the file/entry */
};
static int dfs_uffs_open(struct dfs_fd* file)
{
int fd;
int oflag, mode;
struct _uffs_data * uffs_data;
char * file_path;
oflag = file->flags;
if (oflag & DFS_O_DIRECTORY) /* operations about dir */
{
uffs_DIR * dir;
if (oflag & DFS_O_CREAT) /* create a dir*/
{
if (uffs_mkdir(file->path) < 0)
return uffs_result_to_dfs(uffs_get_error());
}
/* open dir */
file_path = rt_malloc(FILE_PATH_MAX);
if(file_path == RT_NULL)
return -DFS_STATUS_ENOMEM;
if (file->path[0] == '/' && !(file->path[1] == 0))
rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->path);
else
{
file_path[0] = '/';
file_path[1] = 0;
}
dir = uffs_opendir(file_path);
if (dir == RT_NULL)
{
rt_free(file_path);
return uffs_result_to_dfs(uffs_get_error());
}
/* save this pointer,will used by dfs_uffs_getdents*/
file->data = dir;
rt_free(file_path);
return DFS_STATUS_OK;
}
/* regular file operations */
/* int uffs_open(const char *name, int oflag, ...); what is this?
* uffs_open can open dir!! **/
mode = 0;
if (oflag & DFS_O_RDONLY) mode |= UO_RDONLY;
if (oflag & DFS_O_WRONLY) mode |= UO_WRONLY;
if (oflag & DFS_O_RDWR) mode |= UO_RDWR;
/* Opens the file, if it is existing. If not, a new file is created. */
if (oflag & DFS_O_CREAT) mode |= UO_CREATE;
/* Creates a new file. If the file is existing, it is truncated and overwritten. */
if (oflag & DFS_O_TRUNC) mode |= UO_TRUNC;
/* Creates a new file. The function fails if the file is already existing. */
if (oflag & DFS_O_EXCL) mode |= UO_EXCL;
fd = uffs_open(file->path, mode);
if (fd < 0)
{
return uffs_result_to_dfs(uffs_get_error());
}
uffs_data = rt_malloc(sizeof(struct _uffs_data));
if (uffs_data == RT_NULL)
return -DFS_STATUS_ENOMEM;
/* save file info */
uffs_data->fd = fd;
uffs_data->path = file->path;
/* save this pointer, it will be used when calling read()write(),
* flush(), seek(), and will be free when calling close()*/
file->data = uffs_data;
file->pos = uffs_seek(fd, 0, USEEK_CUR);
file->size = uffs_seek(fd, 0, USEEK_END);
uffs_seek(fd, file->pos, USEEK_SET);
if (oflag & DFS_O_APPEND)
{
file->pos = uffs_seek(fd, 0, USEEK_END);
}
return 0;
}
static int dfs_uffs_close(struct dfs_fd* file)
{
int oflag;
struct _uffs_data * uffs_data;
oflag = file->flags;
if (oflag & DFS_O_DIRECTORY)
{
/* operations about dir */
if (uffs_closedir((uffs_DIR *)(file->data)) < 0)
return uffs_result_to_dfs(uffs_get_error());
return 0;
}
/* regular file operations */
uffs_data = (struct _uffs_data *)(file->data);
RT_ASSERT(uffs_data != RT_NULL);
if (uffs_close(uffs_data->fd) == 0)
{
/* release memory */
rt_free(uffs_data);
return 0;
}
return uffs_result_to_dfs(uffs_get_error());
}
static int dfs_uffs_ioctl(struct dfs_fd * file, int cmd, void* args)
{
return -DFS_STATUS_ENOSYS;
}
static int dfs_uffs_read(struct dfs_fd * file, void* buf, rt_size_t len)
{
int fd;
struct _uffs_data * uffs_data;
int char_read;
uffs_data = (struct _uffs_data *)(file->data);
RT_ASSERT(uffs_data != RT_NULL);
fd = uffs_data->fd;
char_read = uffs_read(fd, buf, len);
if (char_read < 0)
return uffs_result_to_dfs(uffs_get_error());
/* update position */
file->pos = uffs_seek(fd, 0, USEEK_CUR);
return char_read;
}
static int dfs_uffs_write(struct dfs_fd* file,
const void* buf,
rt_size_t len)
{
int fd;
struct _uffs_data * uffs_data;
int char_write;
uffs_data = (struct _uffs_data *)(file->data);
RT_ASSERT(uffs_data != RT_NULL);
fd = uffs_data->fd;
char_write = uffs_write(fd, buf, len);
if (char_write < 0)
return uffs_result_to_dfs(uffs_get_error());
/* update position */
file->pos = uffs_seek(fd, 0, USEEK_CUR);
return char_write;
}
static int dfs_uffs_flush(struct dfs_fd* file)
{
int fd;
int result;
struct _uffs_data * uffs_data;
uffs_data = (struct _uffs_data *)(file->data);
RT_ASSERT(uffs_data != RT_NULL);
fd = uffs_data->fd;
result = uffs_flush(fd);
if (result < 0 )
return uffs_result_to_dfs(uffs_get_error());
return 0;
}
static int dfs_uffs_seek(struct dfs_fd* file,
rt_off_t offset)
{
int fd;
struct _uffs_data * uffs_data;
int result;
uffs_data = (struct _uffs_data *)(file->data);
RT_ASSERT(uffs_data != RT_NULL);
fd = uffs_data->fd;
/* set offset as current offset */
result = uffs_seek(fd, offset, USEEK_SET);
if (result < 0)
return uffs_result_to_dfs(uffs_get_error());
return result;
}
/* return the size of struct dirent*/
static int dfs_uffs_getdents(
struct dfs_fd* file,
struct dirent* dirp,
rt_uint32_t count)
{
rt_uint32_t index;
char * file_path;
struct dirent* d;
uffs_DIR* dir;
struct uffs_dirent * uffs_d;
dir = (uffs_DIR*)(file->data);
RT_ASSERT(dir != RT_NULL);
/* round count, count is always 1 */
count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
if (count == 0) return -DFS_STATUS_EINVAL;
/* allocate file name */
file_path = rt_malloc(FILE_PATH_MAX);
if (file_path == RT_NULL)
return -DFS_STATUS_ENOMEM;
index = 0;
/* usually, the while loop should only be looped only once! */
while (1)
{
struct uffs_stat s;
d = dirp + index;
uffs_d = uffs_readdir(dir);
if (uffs_d == RT_NULL)
{
rt_free(file_path);
return (uffs_result_to_dfs(uffs_get_error()));
}
if (file->path[0] == '/' && !(file->path[1] == 0))
rt_snprintf(file_path, FILE_PATH_MAX, "%s/%s", file->path, uffs_d->d_name);
else
rt_strncpy(file_path, uffs_d->d_name, FILE_PATH_MAX);
uffs_stat(file_path, &s);
switch(s.st_mode & US_IFMT) /* file type mark */
{
case US_IFREG: /* directory */
d->d_type = DFS_DT_REG;
break;
case US_IFDIR: /* regular file */
d->d_type = DFS_DT_DIR;
break;
case US_IFLNK: /* symbolic link */
case US_IREAD: /* read permission */
case US_IWRITE:/* write permission */
default:
d->d_type = DFS_DT_UNKNOWN;
break;
}
/* write the rest args of struct dirent* dirp */
d->d_namlen = rt_strlen(uffs_d->d_name);
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
rt_strncpy(d->d_name, uffs_d->d_name, rt_strlen(uffs_d->d_name) + 1);
index ++;
if (index * sizeof(struct dirent) >= count)
break;
}
/* free file name buf */
rt_free(file_path);
if (index == 0)
return uffs_result_to_dfs(uffs_get_error());
return index * sizeof(struct dirent);
}
static int dfs_uffs_unlink(struct dfs_filesystem* fs, const char* path)
{
int result;
struct uffs_stat s;
/* judge file type, dir is to be delete by uffs_rmdir, others by uffs_remove */
if (uffs_lstat(path, &s) < 0)
{
return uffs_result_to_dfs(uffs_get_error());
}
switch(s.st_mode & US_IFMT)
{
case US_IFREG:
result = uffs_remove(path);
break;
case US_IFDIR:
result = uffs_rmdir(path);
break;
default:
/* unknown file type */
return -1;
}
if (result < 0)
return uffs_result_to_dfs(uffs_get_error());
return 0;
}
static int dfs_uffs_rename(
struct dfs_filesystem* fs,
const char* oldpath,
const char* newpath)
{
int result;
result = uffs_rename(oldpath, newpath);
if (result < 0)
return uffs_result_to_dfs(uffs_get_error());
return 0;
}
static int dfs_uffs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
{
int result;
struct uffs_stat s;
struct rt_mtd_nand_device * mtd;
result = uffs_stat(path, &s);
if (result < 0)
return uffs_result_to_dfs(uffs_get_error());
/* convert uffs stat to dfs stat structure */
/* FIXME, these field may not be the same */
st->st_dev = 0;
st->st_mode = s.st_mode;
st->st_size = s.st_size;
st->st_mtime = s.st_mtime;
mtd = RT_MTD_NAND_DEVICE(fs->dev_id);
st->st_blksize = mtd->page_size;
return 0;
}
static const struct dfs_filesystem_operation dfs_uffs_ops =
{
"uffs", /* file system type: uffs */
#if RTTHREAD_VERSION >= 10100
DFS_FS_FLAG_FULLPATH,
#else
#error "uffs can only work with rtthread whose version should >= 1.01\n"
#endif
dfs_uffs_mount,
dfs_uffs_unmount,
dfs_uffs_mkfs,
dfs_uffs_statfs,
dfs_uffs_open,
dfs_uffs_close,
dfs_uffs_ioctl,
dfs_uffs_read,
dfs_uffs_write,
dfs_uffs_flush,
dfs_uffs_seek,
dfs_uffs_getdents,
dfs_uffs_unlink,
dfs_uffs_stat,
dfs_uffs_rename,
};
int dfs_uffs_init(void)
{
/* register uffs file system */
dfs_register(&dfs_uffs_ops);
if (uffs_InitObjectBuf() == U_SUCC)
{
if (uffs_DirEntryBufInit() == U_SUCC)
{
uffs_InitGlobalFsLock();
return RT_EOK;
}
}
return -RT_ERROR;
}
/* */
#include "uffs/uffs_public.h"
static URET uffs_mount_device(struct uffs_MountTableEntrySt *work)
{
int dev_num = 0;
work->dev->par.start = work->start_block;
if (work->end_block < 0) {
work->dev->par.end =
work->dev->attr->total_blocks + work->end_block;
}
else {
work->dev->par.end = work->end_block;
}
if (work->dev->Init(work->dev) == U_FAIL) {
return U_FAIL;
}
if (uffs_InitDevice(work->dev) != U_SUCC) {
return U_FAIL;
}
work->dev->dev_num = dev_num++;
return U_SUCC;
}
static URET uffs_unmount_device(struct uffs_MountTableEntrySt *work)
{
int result;
result = uffs_ReleaseDevice(work->dev);
work->dev->Release(work->dev);
return result;
#if 0
if (uffs_ReleaseObjectBuf() == U_SUCC) {
if (uffs_DirEntryBufRelease() == U_SUCC) {
uffs_ReleaseGlobalFsLock();
return U_SUCC;
}
}
return U_FAIL;
#endif
}
static URET uffs_UnRegisterMountTable(uffs_MountTable *mtab)
{
struct uffs_MountTableEntrySt *tbl = uffs_GetMountTable();
if (tbl == NULL || mtab == NULL)
return U_FAIL;
while (tbl)
{
if (tbl == mtab)
{
tbl = mtab->next;
mtab->next = NULL;
return U_SUCC;
}
tbl = tbl->next;
}
return U_FAIL;
}
/*
* dfs_uffs.h
*
* Created on: 2012-3-30
* Author: prife
*/
#ifndef DFS_UFFS_H_
#define DFS_UFFS_H_
#include "uffs_config.h"
#include "uffs/uffs_public.h"
#define UFFS_BLOCK_MARK_SPARE_OFFSET 4 /* indicate block bad or good, offset in spare */
#define RT_CONFIG_UFFS_ECC_MODE UFFS_ECC_SOFT
/* enable this ,you need provide a mark_badblock/check_block funciton */
#define RT_UFFS_USE_CHECK_MARK_FUNCITON
#if RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_SOFT /* let uffs do soft ecc */
#define RT_CONFIG_UFFS_LAYOUT UFFS_LAYOUT_UFFS /* UFFS_LAYOUT_FLASH */
#define RT_CONFIG_UFFS_ECC_SIZE 0 /* if 0, then uffs will caculate a proper ecc size */
#elif RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_HW_AUTO /* nand driver make ecc and do ecc correct */
#define RT_CONFIG_UFFS_LAYOUT UFFS_LAYOUT_FLASH
#define RT_CONFIG_UFFS_ECC_SIZE 4 /* if 0, then uffs will caculate a proper ecc size */
#elif RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_NONE
#define RT_CONFIG_UFFS_LAYOUT UFFS_LAYOUT_UFFS /* UFFS_LAYOUT_FLASH */
#define RT_CONFIG_UFFS_ECC_SIZE 0 /* if 0, then uffs will caculate a proper ecc size */
#else
#error "uffs under rt-thread do not support this ECC mode"
#endif
#if (!CONFIG_USE_STATIC_MEMORY_ALLOCATOR) && (CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR)
#define RT_UFFS_MEMORY_ALLOCATOR 1 /* use system memory allocator */
#elif (CONFIG_USE_STATIC_MEMORY_ALLOCATOR) && (!CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR)
#define RT_UFFS_MEMORY_ALLOCATOR 0 /* use static memory allocator */
#else
#error "UFFS: CONFIG_USE_STATIC_MEMORY_ALLOCATOR ,CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR are invalid!"
#endif
#if CONFIG_USE_STATIC_MEMORY_ALLOCATOR > 0
#error "dfs_uffs only support CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR"
#endif
#if defined(CONFIG_UFFS_AUTO_LAYOUT_USE_MTD_SCHEME)
#error "dfs_uffs not support CONFIG_UFFS_AUTO_LAYOUT_USE_MTD_SCHEME"
#endif
#if (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_HW_AUTO) && (RT_CONFIG_UFFS_LAYOUT != UFFS_LAYOUT_FLASH)
#error "when use UFFS_ECC_HW_AUTO, you must use UFFS_LAYOUT_FLASH"
#elif (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_SOFT) && (RT_CONFIG_UFFS_LAYOUT != UFFS_LAYOUT_UFFS)
#warning "when use UFFS_ECC_SOFT, it is recommended to use UFFS_LAYOUT_UFFS"
#endif
extern const uffs_FlashOps nand_ops;
extern void uffs_setup_storage(
struct uffs_StorageAttrSt *attr,
struct rt_mtd_nand_device * nand);
#endif /* DFS_UFFS_H_ */
/*
* File : rtthread.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006-2012, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE.
*
* Change Logs:
* Date Author Notes
* 2011-10-13 prife the first version
* 2012-03-11 prife use mtd device interface
*/
#include <rtdevice.h>
#include <s3c24x0.h>
/* nand flash commands. This appears to be generic across all NAND flash chips */
#define CMD_READ 0x00 // Read
#define CMD_READ1 0x01 // Read1
#define CMD_READ2 0x50 // Read2
#define CMD_READ3 0x30 // Read3
#define CMD_READID 0x90 // ReadID
#define CMD_WRITE1 0x80 // Write phase 1
#define CMD_WRITE2 0x10 // Write phase 2
#define CMD_ERASE1 0x60 // Erase phase 1
#define CMD_ERASE2 0xd0 // Erase phase 2
#define CMD_STATUS 0x70 // Status read
#define CMD_RESET 0xff // Reset
#define CMD_RANDOMREAD1 0x05 // random read phase 1
#define CMD_RANDOMREAD2 0xE0 // random read phase 2
#define CMD_RANDOMWRITE 0x85 // random write phase 1
#define NF_CMD(cmd) {NFCMD = (cmd); }
#define NF_ADDR(addr) {NFADDR = (addr); }
#define NF_CE_L() {NFCONT &= ~(1<<1); }
#define NF_CE_H() {NFCONT |= (1<<1); }
#define NF_RSTECC() {NFCONT |= (1<<4); }
#define NF_RDMECC() (NFMECC0 )
#define NF_RDSECC() (NFSECC )
#define NF_RDDATA() (NFDATA)
#define NF_RDDATA8() (NFDATA8)
#define NF_WRDATA(data) {NFDATA = (data); }
#define NF_WRDATA8(data) {NFDATA8 = (data); }
#define NF_WAITRB() {while(!(NFSTAT&(1<<0)));}
#define NF_CLEAR_RB() {NFSTAT |= (1<<2); }
#define NF_DETECT_RB() {while(!(NFSTAT&(1<<2)));}
#define NF_MECC_UNLOCK() {NFCONT &= ~(1<<5); }
#define NF_MECC_LOCK() {NFCONT |= (1<<5); }
#define NF_SECC_UNLOCK() {NFCONT &= ~(1<<6); }
#define NF_SECC_LOCK() {NFCONT |= (1<<6); }
/* HCLK=100Mhz, TACLS + TWRPH0 + TWRPH1 >= 50ns */
#define TACLS 1 // 1-clock(0ns)
#define TWRPH0 4 // 3-clock(25ns)
#define TWRPH1 0 // 1-clock(10ns)
/* status bit pattern */
#define STATUS_READY 0x40 // ready
#define STATUS_ERROR 0x01 // error
#define STATUS_ILLACC 0x08 // illegal access
/* configurations */
#define PAGE_DATA_SIZE 2048
#define BLOCK_MARK_SPARE_OFFSET 4
//#define CONFIG_USE_HW_ECC
static struct rt_mutex nand;
#define BLOCK_MARK_OFFSET (PAGE_DATA_SIZE + BLOCK_MARK_SPARE_OFFSET)
/*
* In a page, data's ecc code is stored in spare area, from BYTE 0 to BYTEE 3.
* Block's status byte which indicate a block is bad or not is BYTE4.
*/
static void nand_hw_init(void)
{
/* initialize GPIO nFWEALECLEnFCEnFRE */
GPACON |= (1<<17) | (1<<18) | (1<<19) | (1<<20) | (1<<22);
/* enable PCLK for nand controller */
CLKCON |= 1 << 4;
NFCONF = (TACLS<<12)|(TWRPH0<<8)|(TWRPH1<<4)|(0<<0);
NFCONT = (0<<13)|(0<<12)|(0<<10)|(0<<9)|(0<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1)|(1<<0);
NFSTAT = 0;
/* reset nand flash */
NF_CE_L();
NF_CLEAR_RB();
NF_CMD(CMD_RESET);
NF_DETECT_RB();
NF_CE_H();
}
/*
*check the first byte in spare of the block's first page
*return
* good block, RT_EOK
* bad blcok, return -RT_ERROR
*/
static rt_err_t k9f1g08_mtd_check_block(
struct rt_mtd_nand_device* device,
rt_uint32_t block)
{
rt_uint8_t status;
block = block << 6;
NF_CE_L();
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(BLOCK_MARK_OFFSET);
NF_ADDR((BLOCK_MARK_OFFSET >> 8) & 0xff);
NF_ADDR(block & 0xff);
NF_ADDR((block >> 8) & 0xff);
NF_ADDR((block >>16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB(); /* wait for ready bit */
status = NF_RDDATA8();
NF_CE_H();
/* TODO: more check about status */
return status == 0xFF ? RT_EOK : -RT_ERROR;
#if 0
/* check the second page */
block ++;
NF_CE_L();
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(BLOCK_MARK_OFFSET);
NF_ADDR((BLOCK_MARK_OFFSET >> 8) & 0xff);
NF_ADDR(block & 0xff);
NF_ADDR((block >> 8) & 0xff);
NF_ADDR((block >>16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB(); /* wait for ready bit */
status = NF_RDDATA8();
NF_CE_H();
return status == 0xFF ? RT_EOK : -RT_ERROR;
#endif
}
static rt_err_t k9f1g08_mtd_mark_bad_block(
struct rt_mtd_nand_device* device,
rt_uint32_t block)
{
/* get address of the fisrt page in the block */
rt_err_t result = RT_EOK;
block = block << 6;
NF_CE_L();
NF_CLEAR_RB();
NF_CMD(CMD_WRITE1);
NF_ADDR(BLOCK_MARK_OFFSET);
NF_ADDR((BLOCK_MARK_OFFSET >> 8) & 0xff);
NF_ADDR(block & 0xff);
NF_ADDR((block >> 8) & 0xff);
NF_ADDR((block >>16) & 0xff);
/* write bad block mark in spare*/
NF_WRDATA8(0);
NF_CMD(CMD_WRITE2);
NF_DETECT_RB(); /* wait for ready bit */
if ( NFSTAT & STATUS_ILLACC )
{
NFSTAT |= STATUS_ILLACC; /* write 1 to clear.*/
result = -RT_ERROR;
}
else
{
NF_CMD(CMD_STATUS); /* get the status */
if (NF_RDDATA() & STATUS_ERROR)
result = -RT_ERROR;
}
NF_CE_H(); /* disable chip select */
return result;
}
static rt_err_t k9f1g08_mtd_erase_block(
struct rt_mtd_nand_device* device,
rt_uint32_t block)
{
/* 1 block = 64 page= 2^6*/
rt_err_t result = RT_EOK;
block <<= 6; /* get the first page's address in this block*/
rt_mutex_take(&nand, RT_WAITING_FOREVER);
NF_CE_L(); /* enable chip */
NF_CLEAR_RB();
NF_CMD(CMD_ERASE1); /* erase one block 1st command */
NF_ADDR(block & 0xff);
NF_ADDR((block >> 8) & 0xff);
// NF_ADDR((block >> 16) & 0xff);
NF_CMD(CMD_ERASE2);
NF_DETECT_RB(); /* wait for ready bit */
if ( NFSTAT & STATUS_ILLACC )
{
NFSTAT |= STATUS_ILLACC; /* write 1 to clear.*/
result = -RT_ERROR;
} else {
NF_CMD(CMD_STATUS); /* check status */
if (NF_RDDATA() & STATUS_ERROR) {
result = -RT_ERROR;
}
}
NF_CE_H();
rt_mutex_release(&nand);
return result;
}
/* return 0, ecc ok, 1, can be fixed , -1 can not be fixed */
static rt_err_t k9f1g08_mtd_read(
struct rt_mtd_nand_device * dev,
rt_off_t page,
rt_uint8_t * data, rt_uint32_t data_len, //may not always be 2048
rt_uint8_t * spare, rt_uint32_t spare_len)
{
rt_uint32_t i;
rt_uint32_t mecc;
rt_uint32_t status;
rt_err_t result = RT_EOK;
rt_mutex_take(&nand, RT_WAITING_FOREVER);
NF_RSTECC(); /* reset ECC*/
NF_MECC_UNLOCK();/* unlock MECC */
NF_CE_L(); /* enable chip */
if (data != RT_NULL && data_len != 0)
{
/* read page data area */
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(0);
NF_ADDR(0);
NF_ADDR((page) & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB();/* wait for ready bit */
/*TODO: use a more quick method */
for (i = 0; i < data_len; i++)
data[i] = NF_RDDATA8();
NF_MECC_LOCK();
#if defined(CONFIG_USE_HW_ECC)
/* if read whole page data, then check ecc status */
if (data_len == PAGE_DATA_SIZE)
{
mecc = NF_RDDATA();
NFMECCD0 = ((mecc&0xff00)<<8)|(mecc&0xff);
NFMECCD1 = ((mecc&0xff000000)>>8)|((mecc&0xff0000)>>16);
/* check data ecc */
status = NFESTAT0 & 0x03;
if (status == 0x00) /* no error */
result = RT_EOK;
else if (status == 0x01) /* error can be fixed */
{
//TODO add code to do ecc correct operation
result = -1;
}
else /* error can't be fixed */
result = -2;
}
#endif
}
if (spare != RT_NULL && spare_len != 0)
{
/* read page spare area */
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(PAGE_DATA_SIZE);
NF_ADDR((PAGE_DATA_SIZE >> 8) & 0xff);
NF_ADDR((page) & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB();/* wait for ready bit */
/*TODO: use a more quick method */
for (i = 0; i < spare_len; i++)
spare[i] = NF_RDDATA8();
NF_MECC_LOCK();
result = RT_EOK;
}
NF_CE_H();
rt_mutex_release(&nand);
/* TODO: more check about status */
return result;
}
static rt_err_t k9f1g08_mtd_write (
struct rt_mtd_nand_device * dev,
rt_off_t page,
const rt_uint8_t * data, rt_uint32_t data_len,//will be 2048 always!
const rt_uint8_t * spare, rt_uint32_t spare_len)
{
rt_uint32_t i;
rt_uint32_t mecc0;
rt_err_t result = RT_EOK;
#if defined(CONFIG_USE_HW_ECC)
rt_uint8_t ecc_data[4];
#endif
rt_mutex_take(&nand, RT_WAITING_FOREVER);
NF_CE_L(); /* enable chip */
NF_RSTECC();
NF_MECC_UNLOCK();
if (data != RT_NULL && data_len != 0)
{
RT_ASSERT(data_len == PAGE_DATA_SIZE);
NF_CLEAR_RB();
NF_CMD(CMD_WRITE1);
NF_ADDR(0);
NF_ADDR(0);
NF_ADDR( page & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
for(i=0; i<PAGE_DATA_SIZE; i++)
NF_WRDATA8(data[i]);
NF_MECC_LOCK();
#if defined(CONFIG_USE_HW_ECC)
/* produce HARDWARE ECC */
mecc0=NFMECC0;
ecc_data[0]=(rt_uint8_t)(mecc0 & 0xff);
ecc_data[1]=(rt_uint8_t)((mecc0 >> 8) & 0xff);
ecc_data[2]=(rt_uint8_t)((mecc0 >> 16) & 0xff);
ecc_data[3]=(rt_uint8_t)((mecc0 >> 24) & 0xff);
/* write ecc to spare[0]..[3] */
for(i=0; i<4; i++)
NF_WRDATA8(ecc_data[i]);
#endif
NF_CMD(CMD_WRITE2);
NF_DETECT_RB(); /* wait for ready bit */
if (NFSTAT & STATUS_ILLACC)
{
NFSTAT |= STATUS_ILLACC;
result = -RT_ERROR;
goto __ret;
}
else
{
NF_CMD(CMD_STATUS);
if (NF_RDDATA() & STATUS_ERROR)
{
result = -RT_ERROR;
goto __ret;
}
}
}
if (spare != RT_NULL && spare_len != 0)
{
NF_CLEAR_RB();
NF_CMD(CMD_WRITE1);
NF_ADDR(PAGE_DATA_SIZE);
NF_ADDR((PAGE_DATA_SIZE >> 8) & 0xff);
NF_ADDR( page & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
for(i=0; i<spare_len; i++)
NF_WRDATA8(spare[i]);
NF_CMD(CMD_WRITE2);
NF_DETECT_RB();
if (NFSTAT & STATUS_ILLACC)
{
NFSTAT |= STATUS_ILLACC;
result = -RT_ERROR;
goto __ret;
}
else
{
NF_CMD(CMD_STATUS);
if (NF_RDDATA() & STATUS_ERROR)
{
result = -RT_ERROR;
goto __ret;
}
}
}
__ret:
NF_CE_H(); /* disable chip */
rt_mutex_release(&nand);
return result;
}
static rt_err_t k9f1g08_read_id(
struct rt_mtd_nand_device * dev)
{
return RT_EOK;
}
const static struct rt_mtd_nand_driver_ops k9f1g08_mtd_ops =
{
k9f1g08_read_id,
k9f1g08_mtd_read,
k9f1g08_mtd_write,
k9f1g08_mtd_erase_block,
k9f1g08_mtd_check_block,
k9f1g08_mtd_mark_bad_block,
};
/* interface of nand and rt-thread device */
static struct rt_mtd_nand_device nand_part[4];
void k9f1g08_mtd_init()
{
/* initialize nand controller of S3C2440 */
nand_hw_init();
/* initialize mutex */
if (rt_mutex_init(&nand, "nand", RT_IPC_FLAG_FIFO) != RT_EOK)
{
rt_kprintf("init nand lock mutex failed\n");
}
/* the first partition of nand */
nand_part[0].page_size = PAGE_DATA_SIZE;
nand_part[0].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[0].block_start = 0;
nand_part[0].block_end = 255;
nand_part[0].oob_size = 64;
nand_part[0].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand0", &nand_part[0]);
/* the second partition of nand */
nand_part[1].page_size = PAGE_DATA_SIZE;
nand_part[1].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[1].block_start = 256;
nand_part[1].block_end = 512-1;
nand_part[1].oob_size = 64;
nand_part[1].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand1", &nand_part[1]);
/* the third partition of nand */
nand_part[2].page_size = PAGE_DATA_SIZE;
nand_part[2].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[2].block_start = 512;
nand_part[2].block_end = 512+256-1;
nand_part[2].oob_size = 64;
nand_part[2].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand2", &nand_part[2]);
/* the 4th partition of nand */
nand_part[3].page_size = PAGE_DATA_SIZE;
nand_part[3].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[3].block_start = 512+256;
nand_part[3].block_end = 1024-1;
nand_part[3].oob_size = 64;
nand_part[3].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand3", &nand_part[3]);
}
#include "finsh.h"
static char buf[PAGE_DATA_SIZE+64];
static char spare[64];
void nand_erase(int start, int end)
{
int page;
for(; start <= end; start ++)
{
page = start * 64;
rt_memset(buf, 0, PAGE_DATA_SIZE);
rt_memset(spare, 0, 64);
k9f1g08_mtd_erase_block(RT_NULL, start);
k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, spare, 64);
if (spare[0] != 0xFF)
{
rt_kprintf("block %d is bad, mark it bad\n", start);
//rt_memset(spare, 0xFF, 64);
if (spare[4] == 0xFF)
{
spare[4] = 0x00;
k9f1g08_mtd_write(RT_NULL, page, RT_NULL, 0, spare, 64);
}
}
}
}
int nand_read(int page)
{
int i;
int res;
rt_memset(buf, 0, sizeof(buf));
// rt_memset(spare, 0, 64);
// res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, spare, 64);
res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE+64, RT_NULL, 0);
rt_kprintf("block=%d, page=%d\n", page/64, page%64);
for(i=0; i<PAGE_DATA_SIZE; i++)
{
rt_kprintf("%02x ", buf[i]);
if((i+1)%16 == 0)
rt_kprintf("\n");
}
rt_kprintf("spare:\n");
for(i=0; i<64; i++)
{
// rt_kprintf("%02x ", spare[i]);
rt_kprintf("%02x ", buf[2048+i]);
if((i+1)%8 == 0)
rt_kprintf("\n");
}
return res;
}
int nand_write(int page)
{
int i;
rt_memset(buf, 0, PAGE_DATA_SIZE);
for(i=0; i<PAGE_DATA_SIZE; i++)
buf[i] = (i % 2) + i / 2;
return k9f1g08_mtd_write(RT_NULL, page, buf, PAGE_DATA_SIZE, RT_NULL, 0);
}
int nand_read2(int page)
{
int i;
int res;
rt_memset(buf, 0, sizeof(buf));
res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, RT_NULL, 0);
rt_kprintf("block=%d, page=%d\n", page/64, page%64);
for(i=0; i<PAGE_DATA_SIZE; i++)
{
rt_kprintf("%02x ", buf[i]);
if((i+1)%16 == 0)
rt_kprintf("\n");
}
rt_memset(spare, 0, 64);
res = k9f1g08_mtd_read(RT_NULL, page, RT_NULL, 0, spare, 64);
rt_kprintf("spare:\n");
for(i=0; i<64; i++)
{
rt_kprintf("%02x ", spare[i]);
if((i+1)%8 == 0)
rt_kprintf("\n");
}
return res;
}
int nand_read3(int page)
{
int i;
int res;
rt_memset(buf, 0, sizeof(buf));
rt_memset(spare, 0, 64);
res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, spare, 64);
rt_kprintf("block=%d, page=%d\n", page/64, page%64);
for(i=0; i<PAGE_DATA_SIZE; i++)
{
rt_kprintf("%02x ", buf[i]);
if((i+1)%16 == 0)
rt_kprintf("\n");
}
rt_kprintf("spare:\n");
for(i=0; i<64; i++)
{
rt_kprintf("%02x ", spare[i]);
if((i+1)%8 == 0)
rt_kprintf("\n");
}
return res;
}
int nand_check(int block)
{
if ( k9f1g08_mtd_check_block(RT_NULL, block) != RT_EOK)
rt_kprintf("block %d is bad\n", block);
else
rt_kprintf("block %d is good\n", block);
}
int nand_mark(int block)
{
return k9f1g08_mtd_mark_bad_block(RT_NULL, block);
}
FINSH_FUNCTION_EXPORT(nand_read, nand_read(1).);
FINSH_FUNCTION_EXPORT(nand_read2, nand_read(1).);
FINSH_FUNCTION_EXPORT(nand_read3, nand_read(1).);
FINSH_FUNCTION_EXPORT(nand_write, nand_write(1).);
FINSH_FUNCTION_EXPORT(nand_check, nand_check(1).);
FINSH_FUNCTION_EXPORT(nand_mark, nand_mark(1).);
FINSH_FUNCTION_EXPORT(nand_erase, nand_erase(100, 200). erase block in nand);
/*
* File : rtthread.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006-2012, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE.
*
* Change Logs:
* Date Author Notes
* 2011-10-13 prife the first version
* 2012-03-11 prife use mtd device interface
*/
#include <rtdevice.h>
#include <s3c24x0.h>
//#include "nand.h"
// For flash chip that is bigger than 32 MB, we need to have 4 step address
//
#define NFCONF_INIT 0xF830 // 512-byte 4 Step Address
#define NEED_EXT_ADDR 1
//#define NFCONF_INIT 0xA830 // 256-byte 4 Step Address
//#define NEED_EXT_ADDR 0
//#define NFCONF_INIT 0xF840
// NAND Flash Command. This appears to be generic across all NAND flash chips
#define CMD_READ 0x00 // Read
#define CMD_READ1 0x01 // Read1
#define CMD_READ2 0x50 // Read2
#define CMD_READ3 0x30 // Read3
#define CMD_READID 0x90 // ReadID
#define CMD_WRITE1 0x80 // Write phase 1
#define CMD_WRITE2 0x10 // Write phase 2
#define CMD_ERASE1 0x60 // Erase phase 1
#define CMD_ERASE2 0xd0 // Erase phase 2
#define CMD_STATUS 0x70 // Status read
#define CMD_RESET 0xff // Reset
#define CMD_RANDOMREAD1 0x05 // random read phase 1
#define CMD_RANDOMREAD2 0xE0 // random read phase 2
#define CMD_RANDOMWRITE 0x85 // random write phase 1
#define NF_CMD(cmd) {NFCMD = (cmd); }
#define NF_ADDR(addr) {NFADDR = (addr); }
#define NF_nFCE_L() {NFCONT &= ~(1<<1); }
#define NF_nFCE_H() {NFCONT |= (1<<1); }
#define NF_RSTECC() {NFCONT |= (1<<4); }
#define NF_RDMECC() (NFMECC0 )
#define NF_RDSECC() (NFSECC )
#define NF_RDDATA() (NFDATA)
#define NF_RDDATA8() (NFDATA8)
#define NF_WRDATA(data) {NFDATA = (data); }
#define NF_WRDATA8(data) {NFDATA8 = (data); }
#define NF_WAITRB() {while(!(NFSTAT&(1<<0)));}
#define NF_CLEAR_RB() {NFSTAT |= (1<<2); }
#define NF_DETECT_RB() {while(!(NFSTAT&(1<<2)));}
#define NF_MECC_UnLock() {NFCONT &= ~(1<<5); }
#define NF_MECC_Lock() {NFCONT |= (1<<5); }
#define NF_SECC_UnLock() {NFCONT &= ~(1<<6); }
#define NF_SECC_Lock() {NFCONT |= (1<<6); }
#define RdNFDat8() (NFDATA8) //byte access
#define RdNFDat() RdNFDat8() //for 8 bit nand flash, use byte access
#define WrNFDat8(dat) (NFDATA8 = (dat)) //byte access
#define WrNFDat(dat) WrNFDat8() //for 8 bit nand flash, use byte access
#define NF_CE_L() NF_nFCE_L()
#define NF_CE_H() NF_nFCE_H()
#define NF_DATA_R() NFDATA
#define NF_ECC() NFECC0
// HCLK=100Mhz
#define TACLS 1 // 1-clk(0ns)
#define TWRPH0 4 // 3-clk(25ns)
#define TWRPH1 0 // 1-clk(10ns) //TACLS+TWRPH0+TWRPH1>=50ns
// Status bit pattern
#define STATUS_READY 0x40 // Ready
#define STATUS_ERROR 0x01 // Error
#define STATUS_ILLACC 0x08 // Illigar Access
//
// ERROR_Xxx
//
#define ERR_SUCCESS 0
#define ERR_DISK_OP_FAIL1 1
#define ERR_DISK_OP_FAIL2 2
#define ERR_INVALID_BOOT_SECTOR 3
#define ERR_INVALID_LOAD_ADDR 4
#define ERR_GEN_FAILURE 5
#define ERR_INVALID_PARAMETER 6
#define ERR_JUMP_FAILED 7
#define ERR_INVALID_TOC 8
#define ERR_INVALID_FILE_TYPE 9
//#define NF_READID 1
#define READ_SECTOR_INFO
#define NAND_BASE 0xB0E00000
#define IOP_BASE 0xB1600000
#define PAGE_DATA_SIZE 2048
static struct rt_mutex nand;
/*
* In a page, data's ecc code is stored in spare area, spare BYTE0 to BYTEE 3
* block's status byte which indicate a block is bad is BYTE4 in spare area
*/
static void nand_hw_init(void)
{
/* Init GPIO nFWEALECLEnFCEnFRE */
GPACON |= (1<<17) | (1<<18) | (1<<19) | (1<<20) | (1<<22);
/* Enable PCLK into nand Controller */
CLKCON |= 1 << 4;
NFCONF = (TACLS<<12)|(TWRPH0<<8)|(TWRPH1<<4)|(0<<0);
NFCONT = (0<<13)|(0<<12)|(0<<10)|(0<<9)|(0<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1)|(1<<0);
NFSTAT = 0;
/* reset nand flash */
NF_CE_L();
NF_CLEAR_RB();
NF_CMD(CMD_RESET);
NF_DETECT_RB();
NF_CE_H();
}
static rt_err_t k9f1g08_mtd_erase_block(
struct rt_mtd_nand_device* device,
rt_uint32_t block)
{
/* 1 block = 64 page= 2^6*/
rt_err_t result = RT_EOK;
block <<= 6; /* get the first page's address in this block*/
rt_mutex_take(&nand, RT_WAITING_FOREVER);
NF_nFCE_L(); /* enable chip */
NF_CLEAR_RB();
NF_CMD(CMD_ERASE1); /* Erase one block 1st command */
NF_ADDR(block & 0xff);
NF_ADDR((block >> 8) & 0xff);
// NF_ADDR((block >> 16) & 0xff);
NF_CMD(CMD_ERASE2);
NF_DETECT_RB(); /* Wait for ready bit */
if ( NFSTAT & STATUS_ILLACC )
{
NFSTAT |= STATUS_ILLACC; /* Write 1 to clear.*/
result = -RT_ERROR;
} else {
NF_CMD(CMD_STATUS); /* Check the status */
if (NF_DATA_R() & STATUS_ERROR) {
result = -RT_ERROR;
}
}
NF_nFCE_H();
rt_mutex_release(&nand);
return result;
/* TODO: more check about status */
}
/* return 0, ecc ok, 1, can be fixed , -1 can not be fixed */
static rt_err_t k9f1g08_mtd_read(
struct rt_mtd_nand_device * dev,
rt_off_t page,
rt_uint8_t * data, rt_uint32_t data_len, //may not always be 2048
rt_uint8_t * spare, rt_uint32_t spare_len)
{
rt_uint32_t i;
rt_uint32_t mecc;
rt_uint32_t status;
rt_err_t result;
rt_mutex_take(&nand, RT_WAITING_FOREVER);
NF_RSTECC(); /* reset ECC*/
NF_MECC_UnLock();/* unlock MECC */
NF_nFCE_L(); /* enable chip */
if (data != RT_NULL && data_len != 0)
{
/* read page data area */
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(0);
NF_ADDR(0);
NF_ADDR((page) & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB();/* Wait for RB */
/*TODO: use a more quick method */
for (i = 0; i < data_len; i++)
data[i] = NF_RDDATA8();
NF_MECC_Lock();
/* if read whole page data, then check ecc status */
if (data_len == PAGE_DATA_SIZE)
{
mecc = NF_RDDATA();
NFMECCD0 = ((mecc&0xff00)<<8)|(mecc&0xff);
NFMECCD1 = ((mecc&0xff000000)>>8)|((mecc&0xff0000)>>16);
/* check data ecc */
status = NFESTAT0 & 0x03;
if (status == 0x00)
result = RT_EOK; /* no error */
else if (status == 0x01)
result = -1;/* error can be fixed */
else
result = -2; /* erroe can't be fixed */
}
else
result = RT_EOK;
}
if (spare != RT_NULL && spare_len != 0)
{
/* read page spare area */
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(PAGE_DATA_SIZE);
NF_ADDR((PAGE_DATA_SIZE >> 8) & 0xff);
NF_ADDR((page) & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB();/* Wait for RB */
/*TODO: use a more quick method */
for (i = 0; i < spare_len; i++)
spare[i] = NF_RDDATA8();
NF_MECC_Lock();
result = RT_EOK;
}
NF_nFCE_H();
rt_mutex_release(&nand);
return result;
}
static rt_err_t k9f1g08_mtd_write (
struct rt_mtd_nand_device * dev,
rt_off_t page,
const rt_uint8_t * data, rt_uint32_t data_len,//will be 2048 always!
const rt_uint8_t * spare, rt_uint32_t spare_len)
{
rt_uint32_t i;
rt_uint32_t mecc0;
rt_err_t result = RT_EOK;
rt_uint8_t ecc_data[4];
rt_mutex_take(&nand, RT_WAITING_FOREVER);
NF_nFCE_L(); /* enable chip */
NF_RSTECC();
NF_MECC_UnLock();
if (data != RT_NULL && data_len != 0)
{
RT_ASSERT(data_len == PAGE_DATA_SIZE);
NF_CLEAR_RB(); /* clear RB */
NF_CMD(CMD_WRITE1);
NF_ADDR(0);
NF_ADDR(0);
NF_ADDR( page & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
for(i=0; i<PAGE_DATA_SIZE; i++) //PAGE_DATA_SIZE
NF_WRDATA8(data[i]);
NF_MECC_Lock();
/* produce HARDWARE ECC */
mecc0=NFMECC0;
ecc_data[0]=(rt_uint8_t)(mecc0 & 0xff);
ecc_data[1]=(rt_uint8_t)((mecc0 >> 8) & 0xff);
ecc_data[2]=(rt_uint8_t)((mecc0 >> 16) & 0xff);
ecc_data[3]=(rt_uint8_t)((mecc0 >> 24) & 0xff);
/* write ecc to spare[0]..[3] */
for(i=0; i<4; i++)
NF_WRDATA8(ecc_data[i]);
NF_CMD(CMD_WRITE2);
NF_DETECT_RB(); /* Wait for RB */
if (NFSTAT & STATUS_ILLACC)
{
NFSTAT |= STATUS_ILLACC;
result = -RT_ERROR;
goto __ret;
}
else
{
NF_CMD(CMD_STATUS);
if (NF_DATA_R() & STATUS_ERROR)
{
result = -RT_ERROR;
goto __ret;
}
}
}
if (spare != RT_NULL && spare_len != 0)
{
NF_CLEAR_RB();
NF_CMD(CMD_WRITE1);
NF_ADDR(PAGE_DATA_SIZE);
NF_ADDR((PAGE_DATA_SIZE >> 8) & 0xff);
NF_ADDR( page & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
for(i=0; i<spare_len; i++)
NF_WRDATA8(spare[i]);
NF_CMD(CMD_WRITE2);
NF_DETECT_RB();
if (NFSTAT & STATUS_ILLACC)
{
NFSTAT |= STATUS_ILLACC;
result = -RT_ERROR;
goto __ret;
}
else
{
NF_CMD(CMD_STATUS);
if (NF_DATA_R() & STATUS_ERROR)
{
result = -RT_ERROR;
goto __ret;
}
}
}
__ret:
NF_nFCE_H(); /* disable chip */
rt_mutex_release(&nand);
return result;
}
static rt_err_t k9f1g08_read_id(
struct rt_mtd_nand_device * dev)
{
return RT_EOK;
}
const static struct rt_mtd_nand_driver_ops k9f1g08_mtd_ops =
{
k9f1g08_read_id,
k9f1g08_mtd_read,
k9f1g08_mtd_write,
k9f1g08_mtd_erase_block,
};
/* interface of nand and rt-thread device */
static struct rt_mtd_nand_device nand_part[4];
void k9f1g08_mtd_init()
{
/* initialize nand controller of S3C2440 */
nand_hw_init();
/* initialize mutex */
if (rt_mutex_init(&nand, "nand", RT_IPC_FLAG_FIFO) != RT_EOK)
{
rt_kprintf("init nand lock mutex failed\n");
}
/* the first partition of nand */
nand_part[0].page_size = PAGE_DATA_SIZE;
nand_part[0].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[0].block_start = 0;
nand_part[0].block_end = 255;
nand_part[0].oob_size = 64;
nand_part[0].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand0", &nand_part[0]);
/* the second partition of nand */
nand_part[1].page_size = PAGE_DATA_SIZE;
nand_part[1].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[1].block_start = 256;
nand_part[1].block_end = 512-1;
nand_part[1].oob_size = 64;
nand_part[1].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand1", &nand_part[1]);
/* the third partition of nand */
nand_part[2].page_size = PAGE_DATA_SIZE;
nand_part[2].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[2].block_start = 512;
nand_part[2].block_end = 512+256-1;
nand_part[2].oob_size = 64;
nand_part[2].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand2", &nand_part[2]);
/* the 4th partition of nand */
nand_part[3].page_size = PAGE_DATA_SIZE;
nand_part[3].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[3].block_start = 512+256;
nand_part[3].block_end = 1024-1;
nand_part[3].oob_size = 64;
nand_part[3].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand3", &nand_part[3]);
}
#include "finsh.h"
static char buf[PAGE_DATA_SIZE+64];
static char spare[64];
void nand_erase(int start, int end)
{
int page;
for(; start <= end; start ++)
{
page = start * 64;
rt_memset(buf, 0, PAGE_DATA_SIZE);
rt_memset(spare, 0, 64);
k9f1g08_mtd_erase_block(RT_NULL, start);
k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, spare, 64);
if (spare[0] != 0xFF)
{
rt_kprintf("block %d is bad, mark it bad\n", start);
//rt_memset(spare, 0xFF, 64);
if (spare[4] == 0xFF)
{
spare[4] = 0x00;
k9f1g08_mtd_write(RT_NULL, page, RT_NULL, 0, spare, 64);
}
}
}
}
int nand_read(int page)
{
int i;
int res;
rt_memset(buf, 0, sizeof(buf));
// rt_memset(spare, 0, 64);
// res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, spare, 64);
res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE+64, RT_NULL, 0);
rt_kprintf("block=%d, page=%d\n", page/64, page%64);
for(i=0; i<PAGE_DATA_SIZE; i++)
{
rt_kprintf("%02x ", buf[i]);
if((i+1)%16 == 0)
rt_kprintf("\n");
}
rt_kprintf("spare:\n");
for(i=0; i<64; i++)
{
// rt_kprintf("%02x ", spare[i]);
rt_kprintf("%02x ", buf[2048+i]);
if((i+1)%8 == 0)
rt_kprintf("\n");
}
return res;
}
int nand_write(int page)
{
int i;
rt_memset(buf, 0, PAGE_DATA_SIZE);
for(i=0; i<PAGE_DATA_SIZE; i++)
buf[i] = (i % 2) + i / 2;
return k9f1g08_mtd_write(RT_NULL, page, buf, PAGE_DATA_SIZE, RT_NULL, 0);
}
int nand_read2(int page)
{
int i;
int res;
rt_memset(buf, 0, sizeof(buf));
res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, RT_NULL, 0);
rt_kprintf("block=%d, page=%d\n", page/64, page%64);
for(i=0; i<PAGE_DATA_SIZE; i++)
{
rt_kprintf("%02x ", buf[i]);
if((i+1)%16 == 0)
rt_kprintf("\n");
}
rt_memset(spare, 0, 64);
res = k9f1g08_mtd_read(RT_NULL, page, RT_NULL, 0, spare, 64);
rt_kprintf("spare:\n");
for(i=0; i<64; i++)
{
rt_kprintf("%02x ", spare[i]);
if((i+1)%8 == 0)
rt_kprintf("\n");
}
return res;
}
int nand_read3(int page)
{
int i;
int res;
rt_memset(buf, 0, sizeof(buf));
rt_memset(spare, 0, 64);
res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, spare, 64);
rt_kprintf("block=%d, page=%d\n", page/64, page%64);
for(i=0; i<PAGE_DATA_SIZE; i++)
{
rt_kprintf("%02x ", buf[i]);
if((i+1)%16 == 0)
rt_kprintf("\n");
}
rt_kprintf("spare:\n");
for(i=0; i<64; i++)
{
rt_kprintf("%02x ", spare[i]);
if((i+1)%8 == 0)
rt_kprintf("\n");
}
return res;
}
FINSH_FUNCTION_EXPORT(nand_read, nand_read(1).);
FINSH_FUNCTION_EXPORT(nand_read2, nand_read(1).);
FINSH_FUNCTION_EXPORT(nand_read3, nand_read(1).);
FINSH_FUNCTION_EXPORT(nand_write, nand_write(1).);
FINSH_FUNCTION_EXPORT(nand_erase, nand_erase(100, 200). erase block in nand);
/*
* File : rtthread.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006-2012, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE.
*
* Change Logs:
* Date Author Notes
* 2011-10-13 prife the first version
* 2012-03-11 prife use mtd device interface
*/
#include <rtdevice.h>
#include <s3c24x0.h>
/* nand flash commands. This appears to be generic across all NAND flash chips */
#define CMD_READ 0x00 // Read
#define CMD_READ1 0x01 // Read1
#define CMD_READ2 0x50 // Read2
#define CMD_READ3 0x30 // Read3
#define CMD_READID 0x90 // ReadID
#define CMD_WRITE1 0x80 // Write phase 1
#define CMD_WRITE2 0x10 // Write phase 2
#define CMD_ERASE1 0x60 // Erase phase 1
#define CMD_ERASE2 0xd0 // Erase phase 2
#define CMD_STATUS 0x70 // Status read
#define CMD_RESET 0xff // Reset
#define CMD_RANDOMREAD1 0x05 // random read phase 1
#define CMD_RANDOMREAD2 0xE0 // random read phase 2
#define CMD_RANDOMWRITE 0x85 // random write phase 1
#define NF_CMD(cmd) {NFCMD = (cmd); }
#define NF_ADDR(addr) {NFADDR = (addr); }
#define NF_CE_L() {NFCONT &= ~(1<<1); }
#define NF_CE_H() {NFCONT |= (1<<1); }
#define NF_RSTECC() {NFCONT |= (1<<4); }
#define NF_RDMECC() (NFMECC0 )
#define NF_RDSECC() (NFSECC )
#define NF_RDDATA() (NFDATA)
#define NF_RDDATA8() (NFDATA8)
#define NF_WRDATA(data) {NFDATA = (data); }
#define NF_WRDATA8(data) {NFDATA8 = (data); }
#define NF_WAITRB() {while(!(NFSTAT&(1<<0)));}
#define NF_CLEAR_RB() {NFSTAT |= (1<<2); }
#define NF_DETECT_RB() {while(!(NFSTAT&(1<<2)));}
#define NF_MECC_UNLOCK() {NFCONT &= ~(1<<5); }
#define NF_MECC_LOCK() {NFCONT |= (1<<5); }
#define NF_SECC_UNLOCK() {NFCONT &= ~(1<<6); }
#define NF_SECC_LOCK() {NFCONT |= (1<<6); }
/* HCLK=100Mhz, TACLS + TWRPH0 + TWRPH1 >= 50ns */
#define TACLS 1 // 1-clock(0ns)
#define TWRPH0 4 // 3-clock(25ns)
#define TWRPH1 0 // 1-clock(10ns)
/* status bit pattern */
#define STATUS_READY 0x40 // ready
#define STATUS_ERROR 0x01 // error
#define STATUS_ILLACC 0x08 // illegal access
/* configurations */
#define PAGE_DATA_SIZE 2048
#define BLOCK_MARK_SPARE_OFFSET 4
//#define CONFIG_USE_HW_ECC
static struct rt_mutex nand;
#define BLOCK_MARK_OFFSET (PAGE_DATA_SIZE + BLOCK_MARK_SPARE_OFFSET)
/*
* In a page, data's ecc code is stored in spare area, from BYTE 0 to BYTEE 3.
* Block's status byte which indicate a block is bad or not is BYTE4.
*/
static void nand_hw_init(void)
{
/* initialize GPIO nFWEALECLEnFCEnFRE */
GPACON |= (1<<17) | (1<<18) | (1<<19) | (1<<20) | (1<<22);
/* enable PCLK for nand controller */
CLKCON |= 1 << 4;
NFCONF = (TACLS<<12)|(TWRPH0<<8)|(TWRPH1<<4)|(0<<0);
NFCONT = (0<<13)|(0<<12)|(0<<10)|(0<<9)|(0<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1)|(1<<0);
NFSTAT = 0;
/* reset nand flash */
NF_CE_L();
NF_CLEAR_RB();
NF_CMD(CMD_RESET);
NF_DETECT_RB();
NF_CE_H();
}
/*
*check the first byte in spare of the block's first page
*return
* good block, RT_EOK
* bad blcok, return -RT_ERROR
*/
static rt_err_t k9f1g08_mtd_check_block(
struct rt_mtd_nand_device* device,
rt_uint32_t block)
{
rt_uint8_t status;
block = block << 6;
NF_CE_L();
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(BLOCK_MARK_OFFSET);
NF_ADDR((BLOCK_MARK_OFFSET >> 8) & 0xff);
NF_ADDR(block & 0xff);
NF_ADDR((block >> 8) & 0xff);
NF_ADDR((block >>16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB(); /* wait for ready bit */
status = NF_RDDATA8();
NF_CE_H();
/* TODO: more check about status */
return status == 0xFF ? RT_EOK : -RT_ERROR;
#if 0
/* check the second page */
block ++;
NF_CE_L();
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(BLOCK_MARK_OFFSET);
NF_ADDR((BLOCK_MARK_OFFSET >> 8) & 0xff);
NF_ADDR(block & 0xff);
NF_ADDR((block >> 8) & 0xff);
NF_ADDR((block >>16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB(); /* wait for ready bit */
status = NF_RDDATA8();
NF_CE_H();
return status == 0xFF ? RT_EOK : -RT_ERROR;
#endif
}
static rt_err_t k9f1g08_mtd_mark_bad_block(
struct rt_mtd_nand_device* device,
rt_uint32_t block)
{
/* get address of the fisrt page in the block */
block = block << 6;
rt_err_t result = RT_EOK;
NF_CE_L();
NF_CLEAR_RB();
NF_CMD(CMD_WRITE1);
NF_ADDR(BLOCK_MARK_OFFSET);
NF_ADDR((BLOCK_MARK_OFFSET >> 8) & 0xff);
NF_ADDR(block & 0xff);
NF_ADDR((block >> 8) & 0xff);
NF_ADDR((block >>16) & 0xff);
/* write bad block mark in spare*/
NF_WRDATA8(0);
NF_CMD(CMD_WRITE2);
NF_DETECT_RB(); /* wait for ready bit */
if ( NFSTAT & STATUS_ILLACC )
{
NFSTAT |= STATUS_ILLACC; /* write 1 to clear.*/
result = -RT_ERROR;
}
else
{
NF_CMD(CMD_STATUS); /* get the status */
if (NF_RDDATA() & STATUS_ERROR)
result = -RT_ERROR;
}
NF_CE_H(); /* disable chip select */
return result;
}
static rt_err_t k9f1g08_mtd_erase_block(
struct rt_mtd_nand_device* device,
rt_uint32_t block)
{
/* 1 block = 64 page= 2^6*/
rt_err_t result = RT_EOK;
block <<= 6; /* get the first page's address in this block*/
rt_mutex_take(&nand, RT_WAITING_FOREVER);
NF_CE_L(); /* enable chip */
NF_CLEAR_RB();
NF_CMD(CMD_ERASE1); /* erase one block 1st command */
NF_ADDR(block & 0xff);
NF_ADDR((block >> 8) & 0xff);
// NF_ADDR((block >> 16) & 0xff);
NF_CMD(CMD_ERASE2);
NF_DETECT_RB(); /* wait for ready bit */
if ( NFSTAT & STATUS_ILLACC )
{
NFSTAT |= STATUS_ILLACC; /* write 1 to clear.*/
result = -RT_ERROR;
} else {
NF_CMD(CMD_STATUS); /* check status */
if (NF_RDDATA() & STATUS_ERROR) {
result = -RT_ERROR;
}
}
NF_CE_H();
rt_mutex_release(&nand);
return result;
}
/* return 0, ecc ok, 1, can be fixed , -1 can not be fixed */
static rt_err_t k9f1g08_mtd_read(
struct rt_mtd_nand_device * dev,
rt_off_t page,
rt_uint8_t * data, rt_uint32_t data_len, //may not always be 2048
rt_uint8_t * spare, rt_uint32_t spare_len)
{
rt_uint32_t i;
rt_uint32_t mecc;
rt_uint32_t status;
rt_err_t result = RT_EOK;
rt_mutex_take(&nand, RT_WAITING_FOREVER);
NF_RSTECC(); /* reset ECC*/
NF_MECC_UNLOCK();/* unlock MECC */
NF_CE_L(); /* enable chip */
if (data != RT_NULL && data_len != 0)
{
/* read page data area */
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(0);
NF_ADDR(0);
NF_ADDR((page) & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB();/* wait for ready bit */
/*TODO: use a more quick method */
for (i = 0; i < data_len; i++)
data[i] = NF_RDDATA8();
NF_MECC_LOCK();
#if defined(CONFIG_USE_HW_ECC)
/* if read whole page data, then check ecc status */
if (data_len == PAGE_DATA_SIZE)
{
mecc = NF_RDDATA();
NFMECCD0 = ((mecc&0xff00)<<8)|(mecc&0xff);
NFMECCD1 = ((mecc&0xff000000)>>8)|((mecc&0xff0000)>>16);
/* check data ecc */
status = NFESTAT0 & 0x03;
if (status == 0x00) /* no error */
result = RT_EOK;
else if (status == 0x01) /* error can be fixed */
{
//TODO add code to do ecc correct operation
result = -1;
}
else /* error can't be fixed */
result = -2;
}
#endif
}
if (spare != RT_NULL && spare_len != 0)
{
/* read page spare area */
NF_CLEAR_RB();
NF_CMD(CMD_READ);
NF_ADDR(PAGE_DATA_SIZE);
NF_ADDR((PAGE_DATA_SIZE >> 8) & 0xff);
NF_ADDR((page) & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
NF_CMD(CMD_READ3);
NF_DETECT_RB();/* wait for ready bit */
/*TODO: use a more quick method */
for (i = 0; i < spare_len; i++)
spare[i] = NF_RDDATA8();
NF_MECC_LOCK();
result = RT_EOK;
}
NF_CE_H();
rt_mutex_release(&nand);
/* TODO: more check about status */
return result;
}
static rt_err_t k9f1g08_mtd_write (
struct rt_mtd_nand_device * dev,
rt_off_t page,
const rt_uint8_t * data, rt_uint32_t data_len,//will be 2048 always!
const rt_uint8_t * spare, rt_uint32_t spare_len)
{
rt_uint32_t i;
rt_uint32_t mecc0;
rt_err_t result = RT_EOK;
#if defined(CONFIG_USE_HW_ECC)
rt_uint8_t ecc_data[4];
#endif
rt_mutex_take(&nand, RT_WAITING_FOREVER);
NF_CE_L(); /* enable chip */
NF_RSTECC();
NF_MECC_UNLOCK();
if (data != RT_NULL && data_len != 0)
{
RT_ASSERT(data_len == PAGE_DATA_SIZE);
NF_CLEAR_RB();
NF_CMD(CMD_WRITE1);
NF_ADDR(0);
NF_ADDR(0);
NF_ADDR( page & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
for(i=0; i<PAGE_DATA_SIZE; i++)
NF_WRDATA8(data[i]);
NF_MECC_LOCK();
#if defined(CONFIG_USE_HW_ECC)
/* produce HARDWARE ECC */
mecc0=NFMECC0;
ecc_data[0]=(rt_uint8_t)(mecc0 & 0xff);
ecc_data[1]=(rt_uint8_t)((mecc0 >> 8) & 0xff);
ecc_data[2]=(rt_uint8_t)((mecc0 >> 16) & 0xff);
ecc_data[3]=(rt_uint8_t)((mecc0 >> 24) & 0xff);
/* write ecc to spare[0]..[3] */
for(i=0; i<4; i++)
NF_WRDATA8(ecc_data[i]);
#endif
NF_CMD(CMD_WRITE2);
NF_DETECT_RB(); /* wait for ready bit */
if (NFSTAT & STATUS_ILLACC)
{
NFSTAT |= STATUS_ILLACC;
result = -RT_ERROR;
goto __ret;
}
else
{
NF_CMD(CMD_STATUS);
if (NF_RDDATA() & STATUS_ERROR)
{
result = -RT_ERROR;
goto __ret;
}
}
}
if (spare != RT_NULL && spare_len != 0)
{
NF_CLEAR_RB();
NF_CMD(CMD_WRITE1);
NF_ADDR(PAGE_DATA_SIZE);
NF_ADDR((PAGE_DATA_SIZE >> 8) & 0xff);
NF_ADDR( page & 0xff);
NF_ADDR((page >> 8) & 0xff);
// NF_ADDR((page >> 16) & 0xff);
for(i=0; i<spare_len; i++)
NF_WRDATA8(spare[i]);
NF_CMD(CMD_WRITE2);
NF_DETECT_RB();
if (NFSTAT & STATUS_ILLACC)
{
NFSTAT |= STATUS_ILLACC;
result = -RT_ERROR;
goto __ret;
}
else
{
NF_CMD(CMD_STATUS);
if (NF_RDDATA() & STATUS_ERROR)
{
result = -RT_ERROR;
goto __ret;
}
}
}
__ret:
NF_CE_H(); /* disable chip */
rt_mutex_release(&nand);
return result;
}
static rt_err_t k9f1g08_read_id(
struct rt_mtd_nand_device * dev)
{
return RT_EOK;
}
const static struct rt_mtd_nand_driver_ops k9f1g08_mtd_ops =
{
k9f1g08_read_id,
k9f1g08_mtd_read,
k9f1g08_mtd_write,
k9f1g08_mtd_erase_block,
k9f1g08_mtd_check_block,
k9f1g08_mtd_mark_bad_block,
};
/* interface of nand and rt-thread device */
static struct rt_mtd_nand_device nand_part[4];
void k9f1g08_mtd_init()
{
/* initialize nand controller of S3C2440 */
nand_hw_init();
/* initialize mutex */
if (rt_mutex_init(&nand, "nand", RT_IPC_FLAG_FIFO) != RT_EOK)
{
rt_kprintf("init nand lock mutex failed\n");
}
/* the first partition of nand */
nand_part[0].page_size = PAGE_DATA_SIZE;
nand_part[0].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[0].block_start = 0;
nand_part[0].block_end = 255;
nand_part[0].oob_size = 64;
nand_part[0].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand0", &nand_part[0]);
/* the second partition of nand */
nand_part[1].page_size = PAGE_DATA_SIZE;
nand_part[1].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[1].block_start = 256;
nand_part[1].block_end = 512-1;
nand_part[1].oob_size = 64;
nand_part[1].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand1", &nand_part[1]);
/* the third partition of nand */
nand_part[2].page_size = PAGE_DATA_SIZE;
nand_part[2].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[2].block_start = 512;
nand_part[2].block_end = 512+256-1;
nand_part[2].oob_size = 64;
nand_part[2].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand2", &nand_part[2]);
/* the 4th partition of nand */
nand_part[3].page_size = PAGE_DATA_SIZE;
nand_part[3].block_size = PAGE_DATA_SIZE*64;//don't caculate oob size
nand_part[3].block_start = 512+256;
nand_part[3].block_end = 1024-1;
nand_part[3].oob_size = 64;
nand_part[3].ops = &k9f1g08_mtd_ops;
rt_mtd_nand_register_device("nand3", &nand_part[3]);
}
#include "finsh.h"
static char buf[PAGE_DATA_SIZE+64];
static char spare[64];
void nand_erase(int start, int end)
{
int page;
for(; start <= end; start ++)
{
page = start * 64;
rt_memset(buf, 0, PAGE_DATA_SIZE);
rt_memset(spare, 0, 64);
k9f1g08_mtd_erase_block(RT_NULL, start);
k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, spare, 64);
if (spare[0] != 0xFF)
{
rt_kprintf("block %d is bad, mark it bad\n", start);
//rt_memset(spare, 0xFF, 64);
if (spare[4] == 0xFF)
{
spare[4] = 0x00;
k9f1g08_mtd_write(RT_NULL, page, RT_NULL, 0, spare, 64);
}
}
}
}
int nand_read(int page)
{
int i;
int res;
rt_memset(buf, 0, sizeof(buf));
// rt_memset(spare, 0, 64);
// res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, spare, 64);
res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE+64, RT_NULL, 0);
rt_kprintf("block=%d, page=%d\n", page/64, page%64);
for(i=0; i<PAGE_DATA_SIZE; i++)
{
rt_kprintf("%02x ", buf[i]);
if((i+1)%16 == 0)
rt_kprintf("\n");
}
rt_kprintf("spare:\n");
for(i=0; i<64; i++)
{
// rt_kprintf("%02x ", spare[i]);
rt_kprintf("%02x ", buf[2048+i]);
if((i+1)%8 == 0)
rt_kprintf("\n");
}
return res;
}
int nand_write(int page)
{
int i;
rt_memset(buf, 0, PAGE_DATA_SIZE);
for(i=0; i<PAGE_DATA_SIZE; i++)
buf[i] = (i % 2) + i / 2;
return k9f1g08_mtd_write(RT_NULL, page, buf, PAGE_DATA_SIZE, RT_NULL, 0);
}
int nand_read2(int page)
{
int i;
int res;
rt_memset(buf, 0, sizeof(buf));
res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, RT_NULL, 0);
rt_kprintf("block=%d, page=%d\n", page/64, page%64);
for(i=0; i<PAGE_DATA_SIZE; i++)
{
rt_kprintf("%02x ", buf[i]);
if((i+1)%16 == 0)
rt_kprintf("\n");
}
rt_memset(spare, 0, 64);
res = k9f1g08_mtd_read(RT_NULL, page, RT_NULL, 0, spare, 64);
rt_kprintf("spare:\n");
for(i=0; i<64; i++)
{
rt_kprintf("%02x ", spare[i]);
if((i+1)%8 == 0)
rt_kprintf("\n");
}
return res;
}
int nand_read3(int page)
{
int i;
int res;
rt_memset(buf, 0, sizeof(buf));
rt_memset(spare, 0, 64);
res = k9f1g08_mtd_read(RT_NULL, page, buf, PAGE_DATA_SIZE, spare, 64);
rt_kprintf("block=%d, page=%d\n", page/64, page%64);
for(i=0; i<PAGE_DATA_SIZE; i++)
{
rt_kprintf("%02x ", buf[i]);
if((i+1)%16 == 0)
rt_kprintf("\n");
}
rt_kprintf("spare:\n");
for(i=0; i<64; i++)
{
rt_kprintf("%02x ", spare[i]);
if((i+1)%8 == 0)
rt_kprintf("\n");
}
return res;
}
FINSH_FUNCTION_EXPORT(nand_read, nand_read(1).);
FINSH_FUNCTION_EXPORT(nand_read2, nand_read(1).);
FINSH_FUNCTION_EXPORT(nand_read3, nand_read(1).);
FINSH_FUNCTION_EXPORT(nand_write, nand_write(1).);
FINSH_FUNCTION_EXPORT(nand_erase, nand_erase(100, 200). erase block in nand);
ADD_SUBDIRECTORY(emu)
ADD_SUBDIRECTORY(uffs)
ADD_SUBDIRECTORY(utils)
ADD_SUBDIRECTORY(example)
ADD_SUBDIRECTORY(platform)
ADD_SUBDIRECTORY(test/api_test)
ADD_SUBDIRECTORY(test/clients)
SET (libemu_SRCS
cmdline.c
cmdline.h
helper_cmds.c
uffs_fileem.c
uffs_fileem_share.c
uffs_fileem_wrap.c
uffs_fileem_ecc_soft.c
uffs_fileem_ecc_hw.c
uffs_fileem_ecc_hw_auto.c
uffs_fileem.h
test_cmds.c
)
IF (UNIX)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/platform/posix)
ENDIF()
IF (WIN32)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/platform/win32)
ENDIF()
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/inc)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/test/api_test)
ADD_LIBRARY(emu STATIC ${libemu_SRCS} )
/*
This file is part of UFFS, the Ultra-low-cost Flash File System.
Copyright (C) 2005-2009 Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
UFFS is free software; you can redistribute it and/or modify it under
the GNU Library General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
UFFS 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
or GNU Library General Public License, as applicable, for more details.
You should have received a copy of the GNU General Public License
and GNU Library General Public License along with UFFS; if not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
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 v2.
This exception does not invalidate any other reasons why a work based
on this file might be covered by the GNU General Public License.
*/
/**
* \file cmdline.c
* \brief command line test interface
* \author Ricky Zheng, created in 22th Feb, 2007
*/
#include <string.h>
#include <stdio.h>
//#include <conio.h>
#include "uffs_config.h"
#include "cmdline.h"
#include "uffs/uffs_fs.h"
#define PROMPT "UFFS>"
#define PFX "cli : "
#define MSGLN(msg,...) uffs_Perror(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
#define MSG(msg,...) uffs_PerrorRaw(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
#define MAX_CLI_ARGS_BUF_LEN 120
#define MAX_CLI_ARGS_NUM 20
#define MAX_CLI_ENV_NUM 11 // '?', '0' - '9'
struct cli_arg {
int argc;
char *argv[MAX_CLI_ARGS_NUM];
char _buf[MAX_CLI_ARGS_BUF_LEN];
};
static BOOL m_exit = FALSE;
static BOOL m_abort = FALSE;
static struct cli_commandset *m_cmdset_head = NULL;
// Note: last command return code stored in env 0.
static int m_cli_envs[MAX_CLI_ENV_NUM] = {0}; // cli environment variables
static const struct cli_command * cli_find(const char *cmd);
static int cmd_help(int argc, char *argv[]);
#define FOR_EACH_CLI_CMD(set, cmd) \
for (set = m_cmdset_head; set && set->cmds; set = set->next) \
for (cmd = set->cmds; cmd && cmd->handler; cmd++)
/*** filter out leading and tailing spaces, discard comments
* return pointer to start of new command line
*/
static char * cli_process_line(char *p)
{
char *s;
char *x;
if (!p)
return NULL;
// skip leading spaces
while (p && (*p == ' ' || *p == '\t'))
p++;
for (s = x = p; *p; x++, p++) {
switch(*p) {
case '\\':
p++;
if (*p) {
switch(*p) {
case 'n':
*x = '\n';
break;
case 'r':
*x = '\r';
break;
case 't':
*x = '\t';
break;
case 'b':
*x = '\b';
break;
default:
if (*p >= '0' && *p <= '9')
*x = *p - '0';
else
*x = *p;
break;
}
}
break;
default:
if (*p == '\r' || *p == '\n' || *p == '#') *p = '\0';
*x = *p;
break;
}
if (*p == 0)
break;
}
// trim tailing spaces
p--;
while (p > s && (*p == ' ' || *p == '\t'))
*p-- = '\0';
return s;
}
static int cli_env_to_idx(char env)
{
int idx = -1;
if (env >= '0' && env <= '9') {
idx = env - '0' + 1;
}
else if (env == '?') {
idx = 0;
}
return idx;
}
int cli_env_set(char env, int val)
{
int idx = cli_env_to_idx(env);
if (idx >= 0) {
m_cli_envs[idx] = val;
return 0;
}
else
return -1;
}
int cli_env_get(char env)
{
int idx = cli_env_to_idx(env);
return idx >= 0 ? m_cli_envs[idx] : 0;
}
/** exec command <n> times:
* exec <n> <cmd> [...]
*/
static int cmd_exec(int argc, char *argv[])
{
int n = 0;
const struct cli_command *cmd;
CHK_ARGC(3, 0);
if (sscanf(argv[1], "%d", &n) != 1)
return CLI_INVALID_ARG;
if (n <= 0)
return CLI_INVALID_ARG;
cmd = cli_find(argv[2]);
if (cmd == NULL) {
MSG("Unknown command '%s'\n", argv[2]);
return -1;
}
else {
argv += 2;
while (n-- >= 0) {
if (cmd->handler(argc - 2, argv) != 0)
return -1;
}
}
return 0;
}
/**
* test expression
* test <a> <op> <b>
* for example:
* test 1 > 0 ==> return 0
* test 1 <= 0 ==> return -1
*/
static int cmd_test(int argc, char *argv[])
{
int a, b;
char *op;
BOOL tst = FALSE;
CHK_ARGC(4, 4);
if (sscanf(argv[1], "%d", &a) != 1 ||
sscanf(argv[3], "%d", &b) != 1)
{
return CLI_INVALID_ARG;
}
op = argv[2];
if (!strcmp(op, ">")) {
tst = (a > b);
}
else if (!strcmp(op, "<")) {
tst = (a < b);
}
else if (!strcmp(op, "==")) {
tst = (a == b);
}
else if (!strcmp(op, ">=")) {
tst = (a >= b);
}
else if (!strcmp(op, "<=")) {
tst = (a <= b);
}
else if (!strcmp(op, "!=")) {
tst = (a != b);
}
else {
return CLI_INVALID_ARG;
}
return tst ? 0 : -1;
}
/** if last command failed (return != 0), run <cmd>
* ! <cmd>
*/
static int cmd_failed(int argc, char *argv[])
{
const struct cli_command *cmd;
CHK_ARGC(2, 0);
cmd = cli_find(argv[1]);
if (cmd == NULL) {
MSG("Unknown command '%s'\n", argv[1]);
return -1;
}
else {
argv++;
return (cli_env_get('?') == 0 ? 0 : cmd->handler(argc - 1, argv));
}
}
/** print messages
* echo [<arg> ...]
*/
static int cmd_echo(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++) {
MSG("%s%s", i > 1 ? " " : "", argv[i]);
}
MSG("\n");
return 0;
}
/** set cli environment variable
* set <env> <value>
*/
static int cmd_set(int argc, char *argv[])
{
int val;
int ret = -1;
CHK_ARGC(3, 0);
if (sscanf(argv[2], "%d", &val) == 1) {
ret = cli_env_set(argv[1][0], val);
}
return ret;
}
/** evaluation the expresstion, result to $1
* evl <value> <op> <value>
*/
static int cmd_evl(int argc, char *argv[])
{
int val1, val2, result = 0;
int ret = -1;
CHK_ARGC(4, 4);
if (sscanf(argv[1], "%d", &val1) == 1 &&
sscanf(argv[3], "%d", &val2) == 1) {
ret = 0;
switch(argv[2][0]) {
case '+':
result = val1 + val2;
break;
case '-':
result = val1 - val2;
break;
case '*':
result = val1 * val2;
break;
case '/':
if (val2 == 0)
ret = -1;
else
result = val1 / val2;
break;
case '%':
if (val2 == 0)
ret = -1;
else
result = val1 % val2;
break;
default:
ret = CLI_INVALID_ARG;
break;
}
}
if (ret == 0)
ret = cli_env_set('1', result);
return ret;
}
static int cmd_exit(int argc, char *argv[])
{
m_exit = TRUE;
return 0;
}
/** Abort current script
* abort [...]
*/
static int cmd_abort(int argc, char *argv[])
{
if (argc > 1) {
cmd_echo(argc, argv);
}
m_abort = TRUE;
return 0;
}
/** run local file system's script
* script <filename>
*/
static int cmd_script(int argc, char *argv[])
{
char line_buf[256];
char *p;
FILE *fp;
const char *name;
int ret = 0;
static int stack = 0;
CHK_ARGC(2, 0);
if (stack++ == 0)
m_abort = FALSE;
name = argv[1];
fp = fopen(name, "r");
if (fp) {
memset(line_buf, 0, sizeof(line_buf));
while (!m_abort && fgets(line_buf, sizeof(line_buf) - 1, fp)) {
p = line_buf + sizeof(line_buf) - 1;
while (*p == 0 && p > line_buf)
p--;
while ((*p == '\r' || *p == '\n') && p > line_buf) {
*p-- = 0;
}
p = cli_process_line(line_buf);
if (*p)
ret = cli_interpret(p);
memset(line_buf, 0, sizeof(line_buf));
}
fclose(fp);
}
else {
MSG("Can't open host script file '%s' for read\n", name);
ret = -1;
}
stack--;
return ret;
}
static const struct cli_command default_cmds[] =
{
{ cmd_help, "help|?", "[<command>]", "show commands or help on one command" },
{ cmd_exit, "exit", NULL, "exit command line" },
{ cmd_exec, "*", "<n> <cmd> [...]>", "run <cmd> <n> times" },
{ cmd_failed, "!", "<cmd> [...]", "run <cmd> if last command failed" },
{ cmd_echo, "echo", "[...]", "print messages" },
{ cmd_set, "set", "<env> <val>", "set env variable" },
{ cmd_evl, "evl", "<val> <op> <val>", "evaluation expresstion" },
{ cmd_test, "test", "<a> <op> <b>", "test expression: <a> <op> <b>" },
{ cmd_script, "script", "<file>", "run host script <file>" },
{ cmd_abort, "abort", NULL, "abort from the running script" },
{ NULL, NULL, NULL, NULL }
};
static struct cli_commandset default_cmdset = {
default_cmds,
};
static BOOL match_cmd(const char *src, int start, int end, const char *des)
{
while (src[start] == ' ' && start < end)
start++;
while (src[end] == ' ' && start < end)
end--;
if ((int)strlen(des) == (end - start + 1)) {
if (memcmp(src + start, des, end - start + 1) == 0) {
return TRUE;
}
}
return FALSE;
}
static BOOL check_cmd(const char *cmds, const char *cmd)
{
int start, end;
for (start = end = 0; cmds[end] != 0 && cmds[end] != '|'; end++);
while (end > start) {
if (match_cmd(cmds, start, end - 1, cmd) == TRUE)
return TRUE;
if (cmds[end] == 0)
break;
if (cmds[end] == '|') {
end++;
for (start = end; cmds[end] != 0 && cmds[end] != '|'; end++);
}
}
return FALSE;
}
static const struct cli_command * cli_find(const char *cmd)
{
struct cli_commandset *work;
const struct cli_command *s;
FOR_EACH_CLI_CMD(work, s) {
if (check_cmd(s->cmd, cmd) == TRUE)
return s;
}
return NULL;
}
static void show_cmd_usage(const struct cli_command *cmd)
{
MSG("%s: %s\n", cmd->cmd, cmd->descr);
MSG("Usage: %s %s\n", cmd->cmd, cmd->args ? cmd->args : "");
}
static int cmd_help(int argc, char *argv[])
{
const struct cli_command *cmd;
struct cli_commandset *cmdset;
int i, n;
if (argc < 2) {
MSG("Available commands:\n");
n = 0;
FOR_EACH_CLI_CMD(cmdset, cmd) {
MSG("%s", cmd->cmd);
for (i = strlen(cmd->cmd); i%10; i++, MSG(" "));
if ((++n % 5) == 0) MSG("\n");
}
MSG("\n");
}
else {
cmd = cli_find(argv[1]);
if (cmd == NULL) {
MSG("No such command\n");
return -1;
}
else {
show_cmd_usage(cmd);
}
}
return 0;
}
static void cli_parse_args(const char *cmd, struct cli_arg *arg)
{
char *p;
int val;
if (arg) {
arg->argc = 0;
if (cmd) {
p = arg->_buf;
while (*cmd && arg->argc < MAX_CLI_ARGS_NUM && (p - arg->_buf < MAX_CLI_ARGS_BUF_LEN)) {
while(*cmd && (*cmd == ' ' || *cmd == '\t'))
cmd++;
arg->argv[arg->argc] = p;
while (*cmd && (*cmd != ' ' && *cmd != '\t') && (p - arg->_buf < MAX_CLI_ARGS_BUF_LEN)) {
if (*cmd == '$') {
// command env replacement
cmd++;
val = cli_env_get(*cmd++);
if (p - arg->_buf < MAX_CLI_ARGS_BUF_LEN - 12) { // 12 is long enough for 32bit 'int'
p += sprintf(p, "%d", val & 0xFFFFFFFF);
}
}
else
*p++ = *cmd++;
}
*p++ = '\0';
if (*(arg->argv[arg->argc]) == '\0')
break;
arg->argc++;
}
}
}
}
int cli_interpret(const char *line)
{
struct cli_arg arg = {0};
const struct cli_command *cmd;
int ret = -1;
cli_parse_args(line, &arg);
if (arg.argc > 0) {
cmd = cli_find(arg.argv[0]);
if (cmd == NULL) {
MSG("Unknown command '%s'\n", arg.argv[0]);
}
else {
ret = cmd->handler(arg.argc, arg.argv);
if (ret == CLI_INVALID_ARG) {
MSG("\n");
show_cmd_usage(cmd);
}
}
}
cli_env_set('?', ret); // $? = last command return code
return ret;
}
void cli_add_commandset(struct cli_commandset *set)
{
if (set) {
set->next = m_cmdset_head;
m_cmdset_head = set;
}
}
void cli_main_entry()
{
char line[80];
int linelen = 0;
char *p;
MSG("$ ");
cli_add_commandset(&default_cmdset);
while (!m_exit) {
char ch;
if (linelen >= sizeof(line))
continue;
ch = getc(stdin);
switch (ch) {
case 8:
case 127:
if (linelen > 0) {
--linelen;
MSG("\x08 \x08");
}
break;
case '\r':
case '\n':
//MSG("\r\n");
if (linelen > 0) {
line[linelen] = 0;
p = cli_process_line(line);
if (*p)
cli_interpret(p);
linelen = 0;
}
MSG("$ ");
break;
case 21:
while (linelen > 0) {
--linelen;
MSG("\x08 \x08");
}
break;
default:
if (ch >= ' ' && ch < 127 && linelen < sizeof(line) - 1) {
line[linelen++] = ch;
//MSG("%c", ch);
}
}
}
}
/*
This file is part of UFFS, the Ultra-low-cost Flash File System.
Copyright (C) 2005-2009 Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
UFFS is free software; you can redistribute it and/or modify it under
the GNU Library General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
UFFS 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
or GNU Library General Public License, as applicable, for more details.
You should have received a copy of the GNU General Public License
and GNU Library General Public License along with UFFS; if not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
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 v2.
This exception does not invalidate any other reasons why a work based
on this file might be covered by the GNU General Public License.
*/
#ifndef _UFFS_CLI_H_
#define _UFFS_CLI_H_
#ifndef BOOL
#define BOOL int
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define CLI_INVALID_ARG -100
typedef int command_t(int argc, char *argv[]);
struct cli_command {
command_t *handler;
const char *cmd;
const char *args;
const char *descr;
};
struct cli_commandset {
const struct cli_command *cmds;
struct cli_commandset *next;
};
void cli_add_commandset(struct cli_commandset *set);
int cli_interpret(const char *line);
int cli_env_get(char env);
int cli_env_set(char env, int val);
void cli_main_entry();
#define u_assert(x) \
((x) ? TRUE : \
(uffs_PerrorRaw(UFFS_MSG_NORMAL, \
"Assert failed at %s:%s:%d: '%s' is not true.\n", \
__FILE__, __FUNCTION__, __LINE__, #x), FALSE))
#define CHK_ARGC(min, max) \
if (argc < min || (max > 0 && argc > max)) \
return CLI_INVALID_ARG
#endif
/*
This file is part of UFFS, the Ultra-low-cost Flash File System.
Copyright (C) 2005-2009 Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
UFFS is free software; you can redistribute it and/or modify it under
the GNU Library General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
UFFS 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
or GNU Library General Public License, as applicable, for more details.
You should have received a copy of the GNU General Public License
and GNU Library General Public License along with UFFS; if not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
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 v2.
This exception does not invalidate any other reasons why a work based
on this file might be covered by the GNU General Public License.
*/
/**
* \file helper_cmds.c
* \brief helper commands for test uffs
* \author Ricky Zheng
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "uffs_config.h"
#include "uffs/uffs_public.h"
#include "uffs/uffs_fs.h"
#include "uffs/uffs_utils.h"
#include "uffs/uffs_core.h"
#include "uffs/uffs_mtb.h"
#include "uffs/uffs_find.h"
#include "cmdline.h"
#include "uffs/uffs_fd.h"
#include "uffs/uffs_mtb.h"
#include "uffs_fileem.h"
#define PFX "cmd : "
#define MAX_PATH_LENGTH 128
#define MSGLN(msg,...) uffs_Perror(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
#define MSG(msg,...) uffs_PerrorRaw(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
/** format [<mount>] */
static int cmd_format(int argc, char *argv[])
{
URET ret;
const char *mount = "/";
uffs_Device *dev;
UBOOL force = U_FALSE;
if (argc > 1) {
mount = argv[1];
if (argc > 2 && strcmp(argv[2], "-f") == 0)
force = U_TRUE;
}
MSGLN("Formating %s ... ", mount);
dev = uffs_GetDeviceFromMountPoint(mount);
if (dev == NULL) {
MSGLN("Can't get device from mount point.");
return -1;
}
else {
ret = uffs_FormatDevice(dev, force);
if (ret != U_SUCC) {
MSGLN("Format fail.");
return -1;
}
else {
MSGLN("Format succ.");
}
uffs_PutDevice(dev);
}
return 0;
}
/** mkf <file> */
static int cmd_mkf(int argc, char *argv[])
{
int fd;
const char *name;
int oflags = UO_RDWR | UO_CREATE;
CHK_ARGC(2, 2);
name = argv[1];
fd = uffs_open(name, oflags);
if (fd < 0) {
MSGLN("Create %s fail, err: %d", name, uffs_get_error());
return -1;
}
else {
MSGLN("Create %s succ.", name);
uffs_close(fd);
}
return 0;
}
/** mkdir <dir> */
static int cmd_mkdir(int argc, char *argv[])
{
const char *name;
CHK_ARGC(2, 0);
name = argv[1];
if (uffs_mkdir(name) < 0) {
MSGLN("Create %s fail, err: %d", name, uffs_get_error());
return -1;
}
else {
MSGLN("Create %s succ.", name);
}
return 0;
}
static int CountObjectUnder(const char *dir)
{
int count = 0;
uffs_DIR *dirp;
dirp = uffs_opendir(dir);
if (dirp) {
while (uffs_readdir(dirp) != NULL)
count++;
uffs_closedir(dirp);
}
return count;
}
static int cmd_pwd(int argc, char *argv[])
{
MSGLN("not supported.");
return 0;
}
static int cmd_cd(int argc, char *argv[])
{
MSGLN("Not supported");
return 0;
}
/** ls [<dir>] */
static int cmd_ls(int argc, char *argv[])
{
uffs_DIR *dirp;
struct uffs_dirent *ent;
struct uffs_stat stat_buf;
int count = 0;
char buf[MAX_PATH_LENGTH+2];
const char *name = "/";
char *sub;
int ret = 0;
CHK_ARGC(1, 2);
if (argc > 1)
name = argv[1];
dirp = uffs_opendir(name);
if (dirp == NULL) {
MSGLN("Can't open '%s' for list", name);
ret = -1;
}
else {
MSG("------name-----------size---------serial-----" TENDSTR);
ent = uffs_readdir(dirp);
while (ent) {
MSG("%9s", ent->d_name);
strcpy(buf, name);
sub = buf;
if (name[strlen(name)-1] != '/')
sub = strcat(buf, "/");
sub = strcat(sub, ent->d_name);
if (ent->d_type & FILE_ATTR_DIR) {
sub = strcat(sub, "/");
MSG("/ \t<%8d>", CountObjectUnder(sub));
}
else {
uffs_stat(sub, &stat_buf);
MSG(" \t %8d ", stat_buf.st_size);
}
MSG("\t%6d" TENDSTR, ent->d_ino);
count++;
ent = uffs_readdir(dirp);
}
uffs_closedir(dirp);
MSG("Total: %d objects." TENDSTR, count);
}
return ret;
}
/** rm <obj> */
static int cmd_rm(int argc, char *argv[])
{
const char *name = NULL;
int ret = 0;
struct uffs_stat st;
CHK_ARGC(2, 2);
name = argv[1];
ret = uffs_stat(name, &st);
if (ret < 0) {
MSGLN("Can't stat '%s'", name);
return ret;
}
if (st.st_mode & US_IFDIR) {
ret = uffs_rmdir(name);
}
else {
ret = uffs_remove(name);
}
if (ret == 0)
MSGLN("Delete '%s' succ.", name);
else
MSGLN("Delete '%s' fail!", name);
return ret;
}
/** ren|mv <old> <new> */
static int cmd_ren(int argc, char *argv[])
{
const char *oldname;
const char *newname;
int ret;
CHK_ARGC(3, 3);
oldname = argv[1];
newname = argv[2];
if ((ret = uffs_rename(oldname, newname)) == 0) {
MSGLN("Rename from '%s' to '%s' succ.", oldname, newname);
}
else {
MSGLN("Rename from '%s' to '%s' fail!", oldname, newname);
}
return ret;
}
static void dump_msg_to_stdout(struct uffs_DeviceSt *dev, const char *fmt, ...)
{
uffs_FileEmu *emu = (uffs_FileEmu *)(dev->attr->_private);
va_list args;
va_start(args, fmt);
//vprintf(fmt, args);
if (emu && emu->dump_fp)
vfprintf(emu->dump_fp, fmt, args);
va_end(args);
}
/** dump [<mount>] */
static int cmd_dump(int argc, char *argv[])
{
uffs_Device *dev;
uffs_FileEmu *emu;
const char *mount = "/";
const char *dump_file = "dump.txt";
if (argc > 1) {
mount = argv[1];
if (argc > 2)
dump_file = argv[2];
}
dev = uffs_GetDeviceFromMountPoint(mount);
if (dev == NULL) {
MSGLN("Can't get device from mount point %s", mount);
return -1;
}
emu = (uffs_FileEmu *)(dev->attr->_private);
emu->dump_fp = fopen(dump_file, "w");
uffs_DumpDevice(dev, dump_msg_to_stdout);
if (emu->dump_fp)
fclose(emu->dump_fp);
uffs_PutDevice(dev);
return 0;
}
/** st [<mount>] */
static int cmd_st(int argc, char *argv[])
{
uffs_Device *dev;
const char *mount = "/";
uffs_FlashStat *s;
TreeNode *node;
if (argc > 1) {
mount = argv[1];
}
dev = uffs_GetDeviceFromMountPoint(mount);
if (dev == NULL) {
MSGLN("Can't get device from mount point %s", mount);
return -1;
}
s = &(dev->st);
MSG("----------- basic info -----------" TENDSTR);
MSG("TreeNode size: %d" TENDSTR, sizeof(TreeNode));
MSG("TagStore size: %d" TENDSTR, sizeof(struct uffs_TagStoreSt));
MSG("MaxCachedBlockInfo: %d" TENDSTR, dev->cfg.bc_caches);
MSG("MaxPageBuffers: %d" TENDSTR, dev->cfg.page_buffers);
MSG("MaxDirtyPagesPerBlock: %d" TENDSTR, dev->cfg.dirty_pages);
MSG("MaxPathLength: %d" TENDSTR, MAX_PATH_LENGTH);
MSG("MaxObjectHandles: %d" TENDSTR, MAX_OBJECT_HANDLE);
MSG("FreeObjectHandles: %d" TENDSTR, uffs_GetFreeObjectHandlers());
MSG("MaxDirHandles: %d" TENDSTR, MAX_DIR_HANDLE);
MSG("FreeDirHandles: %d" TENDSTR, uffs_PoolGetFreeCount(uffs_DirEntryBufGetPool()));
MSG("----------- statistics for '%s' -----------" TENDSTR, mount);
MSG("Device Ref: %d" TENDSTR, dev->ref_count);
MSG("Block Erased: %d" TENDSTR, s->block_erase_count);
MSG("Write Page: %d" TENDSTR, s->page_write_count);
MSG("Write Spare: %d" TENDSTR, s->spare_write_count);
MSG("Read Page: %d" TENDSTR, s->page_read_count - s->page_header_read_count);
MSG("Read Header: %d" TENDSTR, s->page_header_read_count);
MSG("Read Spare: %d" TENDSTR, s->spare_read_count);
MSG("I/O Read: %lu" TENDSTR, s->io_read);
MSG("I/O Write: %lu" TENDSTR, s->io_write);
MSG("--------- partition info for '%s' ---------" TENDSTR, mount);
MSG("Space total: %d" TENDSTR, uffs_GetDeviceTotal(dev));
MSG("Space used: %d" TENDSTR, uffs_GetDeviceUsed(dev));
MSG("Space free: %d" TENDSTR, uffs_GetDeviceFree(dev));
MSG("Page Size: %d" TENDSTR, dev->attr->page_data_size);
MSG("Spare Size: %d" TENDSTR, dev->attr->spare_size);
MSG("Pages Per Block: %d" TENDSTR, dev->attr->pages_per_block);
MSG("Block size: %d" TENDSTR, dev->attr->page_data_size * dev->attr->pages_per_block);
MSG("Total blocks: %d of %d" TENDSTR, (dev->par.end - dev->par.start + 1), dev->attr->total_blocks);
if (dev->tree.bad) {
MSG("Bad blocks: ");
node = dev->tree.bad;
while(node) {
MSG("%d, ", node->u.list.block);
node = node->u.list.next;
}
MSG(TENDSTR);
}
uffs_PutDevice(dev);
return 0;
}
/** cp <src> <des> */
static int cmd_cp(int argc, char *argv[])
{
const char *src;
const char *des;
char buf[100];
int fd1 = -1, fd2 = -1;
int len;
BOOL src_local = FALSE, des_local = FALSE;
FILE *fp1 = NULL, *fp2 = NULL;
int ret = -1;
CHK_ARGC(3, 3);
src = argv[1];
des = argv[2];
if (memcmp(src, "::", 2) == 0) {
src += 2;
src_local = TRUE;
}
if (memcmp(des, "::", 2) == 0) {
des += 2;
des_local = TRUE;
}
if (src_local) {
if ((fp1 = fopen(src, "rb")) == NULL) {
MSGLN("Can't open %s for copy.", src);
goto fail_ext;
}
}
else {
if ((fd1 = uffs_open(src, UO_RDONLY)) < 0) {
MSGLN("Can't open %s for copy.", src);
goto fail_ext;
}
}
if (des_local) {
if ((fp2 = fopen(des, "wb")) == NULL) {
MSGLN("Can't open %s for copy.", des);
goto fail_ext;
}
}
else {
if ((fd2 = uffs_open(des, UO_RDWR|UO_CREATE|UO_TRUNC)) < 0) {
MSGLN("Can't open %s for copy.", des);
goto fail_ext;
}
}
ret = 0;
while ( (src_local ? (feof(fp1) == 0) : (uffs_eof(fd1) == 0)) ) {
ret = -1;
if (src_local) {
len = fread(buf, 1, sizeof(buf), fp1);
}
else {
len = uffs_read(fd1, buf, sizeof(buf));
}
if (len == 0) {
ret = -1;
break;
}
if (len < 0) {
MSGLN("read file %s fail ?", src);
break;
}
if (des_local) {
if ((int)fwrite(buf, 1, len, fp2) != len) {
MSGLN("write file %s fail ? ", des);
break;
}
}
else {
if (uffs_write(fd2, buf, len) != len) {
MSGLN("write file %s fail ? ", des);
break;
}
}
ret = 0;
}
fail_ext:
if (fd1 > 0)
uffs_close(fd1);
if (fd2 > 0)
uffs_close(fd2);
if (fp1)
fclose(fp1);
if (fp2)
fclose(fp2);
return ret;
}
/** cat <file> [<offset>] [<size>] */
static int cmd_cat(int argc, char *argv[])
{
int fd;
const char *name = NULL;
char buf[100];
int start = 0, size = 0, printed = 0, n, len;
int ret = -1;
CHK_ARGC(2, 4);
name = argv[1];
if ((fd = uffs_open(name, UO_RDONLY)) < 0) {
MSGLN("Can't open %s", name);
goto fail;
}
if (argc > 2) {
start = strtol(argv[2], NULL, 10);
if (argc > 3) size = strtol(argv[3], NULL, 10);
}
if (start >= 0)
uffs_seek(fd, start, USEEK_SET);
else
uffs_seek(fd, -start, USEEK_END);
while (uffs_eof(fd) == 0) {
len = uffs_read(fd, buf, sizeof(buf) - 1);
if (len == 0)
break;
if (len > 0) {
if (size == 0 || printed < size) {
n = (size == 0 ? len : (size - printed > len ? len : size - printed));
buf[n] = 0;
MSG("%s", buf);
printed += n;
}
else {
break;
}
}
}
MSG(TENDSTR);
uffs_close(fd);
ret = 0;
fail:
return ret;
}
/** show mount table
* mount
*/
static int cmd_mount(int argc, char *argv[])
{
uffs_MountTable *tab = uffs_GetMountTable();
while (tab) {
MSGLN(" %s : (%d) ~ (%d)", tab->mount, tab->start_block, tab->end_block);
tab = tab->next;
}
return 0;
}
/** inspect buffers
* inspb [<mount>]
*/
static int cmd_inspb(int argc, char *argv[])
{
uffs_Device *dev;
const char *mount = "/";
CHK_ARGC(1, 2);
dev = uffs_GetDeviceFromMountPoint(mount);
if (dev == NULL) {
MSGLN("Can't get device from mount point %s", mount);
return -1;
}
uffs_BufInspect(dev);
uffs_PutDevice(dev);
return 0;
}
/** print block wear-leveling information
* wl [<mount>]
*/
static int cmd_wl(int argc, char *argv[])
{
const char *mount = "/";
uffs_Device *dev;
struct uffs_PartitionSt *par;
uffs_FileEmu *emu;
int i, max;
u32 n;
#define NUM_PER_LINE 10
CHK_ARGC(1, 2);
if (argc > 1) {
mount = argv[1];
}
dev = uffs_GetDeviceFromMountPoint(mount);
if (dev == NULL) {
MSGLN("Can't get device from mount point %s", mount);
return -1;
}
par = &dev->par;
emu = (uffs_FileEmu *)(dev->attr->_private);
max = -1;
for (i = 0; i < par->end - par->start; i++) {
if ((i % NUM_PER_LINE) == 0) {
MSG("%04d:", i + par->start);
}
n = i + par->start;
max = (max == -1 ? n :
(emu->em_monitor_block[n] > emu->em_monitor_block[max] ? n : max)
);
MSG(" %4d", emu->em_monitor_block[n]);
if (uffs_TreeFindBadNodeByBlock(dev, n))
MSG("%c", 'x');
else if (uffs_TreeFindErasedNodeByBlock(dev, n))
MSG("%c", ' ');
else
MSG("%c", '.');
if (((i + 1) % NUM_PER_LINE) == 0)
MSG("\n");
}
MSG("\n");
MSG("Total blocks %d, peak erase count %d at block %d\n",
par->end - par->start, max == -1 ? 0 : emu->em_monitor_block[max], max);
uffs_PutDevice(dev);
return 0;
}
static const struct cli_command helper_cmds[] =
{
{ cmd_format, "format", "[<mount>]", "Format device" },
{ cmd_mkf, "mkfile", "<name>", "create a new file" },
{ cmd_mkdir, "mkdir", "<name>", "create a new directory" },
{ cmd_rm, "rm", "<name>", "delete file/directory" },
{ cmd_ren, "mv|ren", "<old> <new>", "rename file/directory" },
{ cmd_ls, "ls", "<dir>", "list dirs and files" },
{ cmd_st, "info|st", "<mount>", "show statistic infomation" },
{ cmd_cp, "cp", "<src> <des>", "copy files. the local file name start with '::'" },
{ cmd_cat, "cat", "<name>", "show file content" },
{ cmd_pwd, "pwd", NULL, "show current dir" },
{ cmd_cd, "cd", "<path>", "change current dir" },
{ cmd_mount, "mount", NULL, "list mounted file systems" },
{ cmd_dump, "dump", "[<mount>]", "dump file system", },
{ cmd_wl, "wl", "[<mount>]", "show block wear-leveling info", },
{ cmd_inspb, "inspb", "[<mount>]", "inspect buffer", },
{ NULL, NULL, NULL, NULL }
};
static struct cli_commandset helper_cmdset = {
helper_cmds,
};
struct cli_commandset * get_helper_cmds()
{
return &helper_cmdset;
};
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
IF (UNIX)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/platform/posix)
ENDIF()
IF (WIN32)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/platform/win32)
ENDIF()
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/inc)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/emu)
LINK_DIRECTORIES(${uffs_BINARY_DIR}/src/emu)
LINK_DIRECTORIES(${uffs_BINARY_DIR}/src/uffs)
SET(static_mem_SRCS static-mem-allocate.c)
SET(flash_if_SRCS flash-interface-example.c)
ADD_EXECUTABLE(static-mem-example ${static_mem_SRCS})
ADD_EXECUTABLE(flash-if-example ${flash_if_SRCS})
TARGET_LINK_LIBRARIES(static-mem-example emu uffs emu platform apitest_server)
TARGET_LINK_LIBRARIES(flash-if-example emu uffs emu platform)
IF (UNIX)
TARGET_LINK_LIBRARIES(static-mem-example pthread)
ENDIF ()
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
IF (UNIX)
SET (libplatform_SRCS posix/uffs_os.c posix/uffs_config.h)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/platform/posix)
ENDIF()
IF (WIN32)
SET (libplatform_SRCS win32/uffs_os.c win32/uffs_config.h)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/platform/win32)
ENDIF()
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/inc)
ADD_LIBRARY(platform STATIC ${libplatform_SRCS} )
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/inc)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/emu)
INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/test/api_test)
IF (UNIX)
SET(example_SRCS example.c)
SET(example2_SRCS example-2.c)
ADD_EXECUTABLE(example ${example_SRCS})
ADD_EXECUTABLE(example-2 ${example2_SRCS})
TARGET_LINK_LIBRARIES(example apitest_client)
TARGET_LINK_LIBRARIES(example-2 apitest_client)
ENDIF()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册