📄 file_sys.c
字号:
/***************************** C SOURCE FILE *********************************
**
** Project: FlashFile
** Filename: FILE_SYS.C
** Version: 3.0.1
** Date: April 7, 2006
**
******************************************************************************
**
** Source Code Solution Software
** License Agreement
**
** IMPORTANT: READ THIS AGREEMENT CAREFULLY.
**
** BY SELECTING THE "I ACCEPT THE AGREEMENT" OPTION AND CLICKING ON THE "NEXT"
** BUTTON, OR BY INSTALLING, COPYING, RUNNING, OR OTHERWISE USING THE PRIIO
** SOFTWARE, YOU AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE AGREEMENT. IF
** YOU DO NOT AGREE TO THE TERMS OF THIS LICENSE AGREEMENT, PLEASE CLICK THE
** "CANCEL" BUTTON, AND DO NOT INSTALL, RUN, COPY, OR OTHERWISE USE THE PRIIO
** SOFTWARE.
**
** This End User License Agreement ("License") is a legal agreement between
** you and Progressive Resources LLC DBA Priio, 4105 Vincennes Road,
** Indianapolis, IN 46268, USA ("Priio") concerning your use of the Priio
** Software, together with any electronic documentation that may be provided
** therewith (collectively, "the software") through the Software. YOU HEREBY
** AGREE, BOTH ON YOUR OWN BEHALF AND AS AN AUTHORIZED REPERSENTATIVE OF ANY
** ORGANIZATION FOR WHICH YOU ARE USING THE SOFTWARE ("EMPLOYER"), THAT YOU
** AND THE EMPLOYER WILL USE THE SOFTWARE ONLY IN ACCORDANCE WITH THE
** FOLLOWING TERMS:
**
** 1. Disclaimer of Warranty. You expressly acknowledge and agree that use
** of the Software is at your sole risk. THE SOFTWARE IS PROVIDED "AS
** IS" WITH ALL FAULTS AND WITHOUT WARRANTY OF ANY KIND, PRIIO does not
** warrant that the functions contained in the Software will meet your
** requirements or those of the Employer, or that the operation of the
** Software will be uninterrupted or error-free, or that defects in the
** Software will be corrected. Furthermore, Priio does not warrant or
** make any representation regarding the use or the results of the use of
** the Software (including the related documentation) in terms of their
** correctness, accuracy, reliability, or otherwise. Should the Software
** prove defective, you (and not Priio) assume the entire cost of all
** necessary servicing, repair, or correction.
**
** ** PRIIO EXPRESSLY DISCLAIMS ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR
** A PARTICULAR PURPOSE, TITLE, OR NONINFRINGEMENT WITH RESPECT TO THE
** SOFTWARE. **
**
** The Software may be provided with third-party plug-ins or other third-party
** software, or this Software may be provided as a plug-in for or otherwise in
** association with third-party software. Use of any such third-party
** software will be governed by the applicable license agreement, if any, with
** such third party.
**
** * PRIIO IS NOT RESPONSIBLE FOR ANY THIRD-PARTY SOFTWARE AND WILL HAVE NO
** LIABILITY OF ANY KIND FOR YOUR USE OF SUCH THIRD-PARTY SOFTWARE AND MAKES
** NO WARRANTY OF ANY KIND WITH RESPECT TO SUCH THIRD-PARTY SOFTWARE. *
**
** SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR LIMITATION OF IMPLIED
** WARRANTIES, SO THE ABOVE EXCLUSIONS MIGHT NOT APPLY TO YOU.
**
** 2. Limitation of Liability. In no event will Priio?s total liability for
** all damages exceed the amount of fifty dollars ($50.00).
**
** **UNDER NO CIRCUMSTANCES, INCLUDING NEGLIGENCE, WILL PRIIO BE LIABLE FOR
** ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE, OR CONSEQUENTIAL DAMAGES,
** INCLUDING LOST DATA, LOST REVENUE, OR LOST PROFITS, ARISING OUT OF OR
** RELATING TO THIS LICENSE OR THE SOFTWARE.**
**
** SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSTION OR LIMITATION OF
** CONSEQUENTIAL OR INDIRECT DAMAGES, SO THE ABOVE LIMITATION MAY NOT APPLY TO
** YOU.
**
** 3. License Grant. Priio grants to you, and you accept, a personal,
** nonexclusive, nontransferable license to install and use the Software
** in source code format as a single site. You may not transfer, share or
** distribute the Software in any reusable form, without the express
** written permission of Priio. You may embed and deploy the executable
** forms of this Software with your product without royalty. You may make
** one copy of the Software in human-readable form for backup purposes
** only. The backup copy must include all copyright and license
** information contained on the original. This License is effective
** until terminated as provided below. You may terminate this License by
** destroying the Software and any copies of the Software in your
** possession. This License will terminate automatically upon any
** violation of its terms by you or the Employer. You acknowledge that
** this License does not entitle you to any support, maintenance, or
** upgrade from Priio.
**
** 4. License Restrictions. You may not do any of the following yourself, or
** through any third party, and you may not permit any third party with
** whom you have a business relationship to do any of the following: (A)
** copy the Software, except as expressly set forth in paragraph 3 above;
** (B) sell, license, sublicense, lease, or rent, to any third party,
** whether for profit or without charge, any portion of the Software, or,
** in particular, without limiting the generality of the foregoing,
** distribute the Software on any media; make the Software accessible to
** the public or third parties, whether over networks, electronic
** bulletin boards, website, or otherwise; or allow any third to use the
** Software except for purpose of your internal business; (C) publish or
** otherwise communicate any review of or information about the
** performance of the Software to any third party without the prior
** written consent of Priio; (D) export, re-export, download, or
** otherwise use the Software in violation of any laws or regulations,
** including U.S. Department of Commerce Export Administration
** regulations and other applicable laws; or (E) use the Software in
** connection with life support systems, human implantation, medical
** devices, nuclear facilities, nuclear systems or weapons, aviation,
** mass transit, or any application where failure or malfunction could
** lead to possible loss of life or catastrophic property damage.
**
** 5. Title and Ownership. This software is protected by United State
** Patent, Copyright Law and International Treaty provisions. Except for
** the rights expressly granted above, this License transfers to you no
** right, title, or interest in the Software, or any copyright, patent,
** trademark, trade secret, or other intellectual property or proprietary
** right in the Software. Priio retains sole and exclusive title to all
** portions of the Software and any copies thereof, and you hereby assign
** to Priio all right, title and interest in and to any modifications you
** make to the Software, whether or not such modifications are permitted.
** You agree not to disclose the Software to anyone.
**
** 6. Export Law Assurances. You may not export, re-export, download, or
** otherwise use the Software except as authorized by United States law
** and the laws of the jurisdiction in which it is obtained.
**
** 7. Notice to Government End Users. The Software, including any
** documentation, is provided to the United States Government with
** restricted rights. If the Software is supplied to the United State
** Government, the software is classified as "restricted computer
** software" as defined in clause 52.227-19 of the FAR. The United
** States Government?s rights to the Software are as provided in clause
** 52.227-19 if the FAR.
**
** 8. Controlling Law and Severability. This License will be governed by
** the laws of the United States and the State of Indiana without regard
** to their provisions regarding conflicts of laws. This License will
** not be governed by the United Nations Convention on Contracts for the
** International Sale of Goods, the application of which is expressly
** excluded. You irrevocably submit to the jurisdiction of the state and
** federal courts sitting in Indiana, and any action or proceeding
** arising out of this License will be heard and determined in such
** courts. If for any reason a court of competent jurisdiction finds any
** provision, or portion thereof, to be unenforceable, such provision
** will be interpreted in order to give effect to such provision to the
** maximum extent permitted by law, and the remainder of this License
** will continue in full force and effect.
**
** 9. Complete Agreement. This License constitutes the entire agreement
** between the parties with respect to the use of the Software and
** supersedes all prior or contemporaneous understandings regarding such
** subject matter. No amendment to or modification of this License will
** be binding unless in writing and signed by Priio.
**
** BY SELECTING THE "I ACCEPT THE AGREEMENT" OPTION AND CLICKING ON THE "NEXT"
** BUTTON, OR BY INSTALLING, COPYING, RUNNING, OR OTHERWISE USING THE PRIIO
** SOFTWARE, YOU AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE AGREEMENT. IF
** YOU DO NOT AGREE TO THE TERMS OF THIS LICENSE AGREEMENT, PLEASE CLICK THE
** "CANCEL" BUTTON, AND DO NOT INSTALL, RUN, COPY, OR OTHERWISE USE THE PRIIO
** SOFTWARE.
**
******************************************************************************
**
** VERSION HISTORY:
** ----------------
** Version: 1.00
** Date: December 31, 2003
** Revised by: Erick M. Higa
** Description:
** - Original version.
**
** Version: 1.10
** Date: January 19, 2004
** Revised by: Erick M. Higa
** Description:
** - Fixed FAT access errors by allowing both FAT tables to be updated.
** - Fixed erase_cluster chain to stop if chain goes to '0'.
** - Fixed #include's so other non m128 processors could be used.
** - Fixed fcreate to match 'C' standard for function "creat".
** - Fixed fseek so it would not error when in "READ" mode.
** - Modified SPI interface to use _FF_spi() so it is more universal.
** (see the "sd_cmd.c" file for the function used).
** - Redifined global variables and #defines for more unique names.
** - Added string functions fputs, fputsc, & fgets.
** - Added functions fquickformat, fgetfileinfo, & GetVolID().
** - Added directory support.
** - Modified delays in "sd_cmd.c" to increase transfer speed to max.
** - Updated "options.h" to include additions, and to make #defines
** more universal to multiple platforms.
**
** Version: 1.20
** Date: January 21, 2004
** Revised by: Erick M. Higa
** Description:
** - Added ICC Support to the FlashFileSD.
** - Fixed card initialization error for MMC/SD's that have only a boot
** sector and no partition table.
** - Fixed intermittant error on fcreate when creating existing file.
** - Changed "options.h" to #include all required files.
**
** Version: 1.30
** Date: March 10, 2004
** Revised by: Erick M. Higa
** Description:
** - Replaced all "const" references to "flash" to support CodeVisionAVR
** v1.24.1b and above.
** - Changed Directory Functions to allow for multi-cluster directory
** entries.
** - Added function addr_to_clust() to support long directories.
** - Fixed FAT table address calculation to support multiple reserved
** sectors (previously) assumed one reserved sector, if XP formats card
** sometimes multiple reserved sectors. - thanks YW
** - Added support for a Compact Flash package.
** - Renamed read and write to flash function names for multiple media
** support.
**
** Version: 1.31
** Date: March 26, 2004
** Revised by: Erick M. Higa
** Description:
** - Added define for easy MEGA128Dev board setup
** - Changed demo projects so "option.h" is in the project directory
**
** Version: 1.32
** Date: April 1, 2004
** Revised by: Erick M. Higa
** Description:
** - Fixed bug in "prev_cluster()" that didn't use updated FAT table
** address calculations. (effects XP formatted cards see v1.30 notes).
**
** Version: 1.33
** Date: May 7, 2004
** Revised by: Erick M. Higa
** Description:
** - Fixed bug in "fflush()" and "fopen()" that was incorrectly handling
** blank files on certain MMC/SD cards.
**
** Version: 1.40
** Date: May 17, 2004
** Revised by: Erick M. Higa
** Description:
** - Fixed bug "fopen()" that was incorrectly writing to 0xFE00 +
** _FF_DirAddr if opening a file in "WRITE" mode (bug since v1.22).
** - Fixed bug in "append_toc()" that was incorrectly updating the
** time/date stamp if _RTC_ON_ is NOT defined.
** - Added IAR EWAVR Support to the FlashFile.
**
** Version: 1.41
** Date: June 14, 2004
** Revised by: Erick M. Higa
** Description:
** - Fixed bug in "fflush()" that was incorrectly saving the last sector
** of a cluster to the first if saving or closing a file at the end of
** file AND at the end of a cluster.
**
** Version: 2.00
** Date: November 4, 2004
** Revised by: Erick M. Higa
** Description:
** - Changing code to use structures and unions to optimize code to run
** faster and use less code space.
** - Fixed bug in fgets() that was reading too long if the line was >= n,
** and it was returning the wrong pointer.
** - Changing code to use sector addressing rather than direct memory
** addressing to save code space and speed up time.
** - Added _DIRECTORIES_SUPPORTED_ switch to options.h to enable or
** disable (less code space) the use of directories.
** - Added _NO_MALLOC_ switch to options.h to enable or disable the need
** of a malloc() function.
** - Added _BYTES_PER_SEC_512_ switch to options.h to enable or disable
** the BPB_BytsPerSec variable. In all current flash media, there are
** 512 bytes per sector, and there really is no need to have it as a
** variable. Enabling _BYTES_PER_SEC_512_ will hard code all
** references to BPB_BytsPerSec as 0x200 or 512. This will cut down on
** code space and speed the functions up a bit since << 9 and >> 9 can
** replace * 512 and / 512, and & 0x1FF can replace % 512.
** - Fixed bug in addr_to_clust() that was checking if sector # was <=
** FirstDataSector where it should just be < FirstDataSector.
** - Added ability in initialize_media() for the calculations to look for
** the first partition location in all four partition entry spaces
** rather than just the first.
** - Fixed bug in scan_directory() that did not always 0xE5 as an empty
** file location.
** - Fixed Directory support (scan_directory(), fcreate(), and mkdir())
** so that multi cluster sub-directories could be read.
** - Fixed Initialization of the File Structure that was intermittantly
** causing errors in fopen() and fcreate().
** - Fixed bug in fcreatec(), fopenc(), and fget_file_infoc() that was
** not always handling full length filenames of files correctly.
** - Combined code so that CodeVision, ImageCraft, and IAR all use the
** same base code.
** - Updated fprintf() in ImageCraft and IAR versions so that the use
** vsprintf and can be used just like a _FF_printf().
** - Added fread() and fwrite() functions.
**
** Version: 2.01
** Date: January 27, 2005
** Revised by: Erick M. Higa
** Description:
** - Fixed bug in scan_directory(), fopen(), fcreate(), rename(), and
** get_file_info() that gave compile errors when
** _DIRECTORIES_SUPPORTED_ was not defined.
** - Corrected return value of rename() to 0 when successful (previously
** returned a 1)
**
** Version: 2.02
** Date: February 10, 2005
** Revised by: Erick M. Higa
** Description:
** - Fixed error in wrong files uploaded in v2.01
** - Changed initialize_media() in "sd_cmd.c"/"cf_cmd.c" so that if an
** active partition is not found in the partition table, it will assume
** that the first partition entry is valid, and use that to calculate
** the Boot Sector of the disk.
** - Changed "options.h" file to fix compile errors when using
** CodeVisionAVR v1.24.4 and above.
**
** Version: 2.03
** Date: March 11, 2005
** Revised by: Erick M. Higa
** Description:
** - Updated "options.h" to define _FF_MAX_FILES_OPEN, for use when
** malloc() is not used (_NO_MALLOC_ is #defined) so the FlashFile will
** automatically allocate the n FILE structures into global variable
** space, where n is the number of files to be opened simultaneously
** defined by _FF_MAX_FILES_OPEN.
** - Fixed bug in fopen() that caused the first sector of a file to be
** wrongly updated when opened in APPEND mode for IAR and ICC users.
**
** Version: 2.10
** Date: June 7, 2005
** Revised by: Erick M. Higa
** Description:
** - Major change in accessing directory entries using structures and
** unions to cut down on code size. Effects all functions that access
** the directory entries (scan_directory(), read_directory(),
** append_toc(), mkdir(), rmdir(), chdir(), _FF_chdir(), fopen(),
** fcreate(), remove(), and rename()).
** - Changed internal functions so that most internal FlashFile functions
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -