FATFS file system power-loss protection methods and non-volatile memory

By dividing the file storage medium into backup areas and saving sector data and CRC checksums, the power failure protection problem of the FATFS file system is solved without increasing hardware costs, thus achieving reliable data recovery and system integrity.

CN122132228APending Publication Date: 2026-06-02SHANGHAI HUAHONG INTEGRATED CIRCUIT

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
SHANGHAI HUAHONG INTEGRATED CIRCUIT
Filing Date
2026-01-13
Publication Date
2026-06-02

AI Technical Summary

Technical Problem

In existing technologies, the FATFS file system lacks an effective power-loss protection scheme without increasing hardware costs, which makes it prone to file system corruption and data loss during sector erasure and writing.

Method used

The system divides the file storage medium into backup areas, reads sector data, calculates CRC checksums, saves the data to the backup areas, performs sector erasure and write operations, and detects and restores backup area data upon power-on, thus achieving power-off protection.

Benefits of technology

Power loss protection for the FATFS file system can be achieved without increasing hardware costs, ensuring data integrity and consistency, preventing file system corruption and data loss, and maintaining the system's versatility and cross-platform compatibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122132228A_ABST
    Figure CN122132228A_ABST
Patent Text Reader

Abstract

This invention discloses a power-loss protection method for the FATFS file system, which is applied to an embedded file storage system using non-volatile memory as the storage medium. The power-loss protection is achieved solely through computer programming technology, and includes the following steps: S1, allocating a region in the file storage medium as a backup area for file write operations; S2, when the file system performs a write operation on any sector in the storage medium, before executing the erase operation on that sector, reading the data of that sector and saving it to the backup area; S3, calculating the CRC checksum, saving the CRC checksum and the sector number of the sector together to the backup area, making the data in the backup area valid; S4, executing the erase operation on the sector to complete the data writing; S5, after completing the writing of the data in that sector, invalidating the data in the backup area; S6, when the embedded file system is powered on again, checking whether the data in the backup area is valid, and if valid, restoring the sector data saved in the backup area and writing it back to the corresponding sector.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the IT field, and in particular to a power-loss protection method and storage medium for the FATFS file system. Background Technology

[0002] The core mechanism of file system power loss protection is to ensure that data maintains integrity and consistency during unexpected power outages through the coordinated work of software-level protection mechanisms (log structure, copy-on-write) and hardware-level mechanisms (adding energy storage components such as batteries, supercapacitors, and power detection).

[0003] The software-level protection mechanisms are as follows: 1. Log-structured file system: It uses a log to record all file modification operations instead of directly overwriting data blocks. After a power outage, incomplete operations can be recovered through the log, avoiding data loss. It provides a garbage collection mechanism to delay the erasure of dirty blocks, reduce invalid writes, and extend the life of FLASH.

[0004] 2. Copy-on-Write (COW) file system: When updating data, the new version is first written to free space, and then the reference to the old block is switched using atomic pointers. If a power outage occurs, the old data remains intact, avoiding a "half-written" state; combined with metadata checksums (such as CRC, SHA256), corrupted blocks are periodically detected and repaired, improving data integrity.

[0005] 3. Transaction Logs and Snapshots: The application layer transaction log records the sequence of operations, and incomplete operations can be replayed in order after a crash; the snapshot function allows the system to roll back to the most recent consistent state, sacrificing some of the latest data to quickly restore services.

[0006] Hardware-level solutions undoubtedly increase design and hardware costs. If reliable power-loss protection is only desired at the software level, then file systems with built-in power-loss protection mechanisms must be considered, but this presents challenges in various application scenarios. FATFS is a general-purpose, completely open-source and free file system suitable for small embedded systems, supporting cross-platform compatibility and relatively simple embedded porting with low operating costs. However, it lacks inherent power-loss recovery capabilities. If a power outage occurs during sector erasure and writing on the storage medium, the file system is highly susceptible to corruption, leading to data loss or inaccessible files. Currently, there is a lack of effective power-loss protection solutions for the FATFS file system without increasing hardware costs. Summary of the Invention

[0007] The summary of this invention introduces a series of simplified concepts, all of which are simplifications of existing technologies in the field, and will be further explained in detail in the detailed description section. This summary is not intended to limit the key features and essential technical features of the claimed technical solution, nor is it intended to determine the scope of protection of the claimed technical solution.

[0008] The technical problem to be solved by the present invention is to provide a method for power loss protection of the FATFS file system through software holes without increasing hardware costs.

[0009] To address the aforementioned technical problems, this invention provides a power-loss protection method for the FATFS file system, applicable to embedded file storage systems using non-volatile memory as the storage medium. This method achieves power-loss protection solely through computer programming technology and includes the following steps: S1, allocate a region in the file storage medium as a backup area for file write operations; S2, when the file system performs a write operation on any sector in the storage medium, before performing the erase operation on that sector, the data of that sector is read out and saved to the backup area; S3, calculate the CRC checksum of the sector data, and save the CRC checksum and the sector number of the sector together to the backup area, making the data in the backup area valid; S4, perform the erase operation on the sector and complete the data writing of the sector; S5, after completing the writing of the data in this sector, invalidate the data in the backup area; S6: When the embedded file system is powered on again, check whether the backup area data is valid. If it is valid, restore the sector data saved in the backup area and write it back to the corresponding sector.

[0010] Preferably, in a further improvement to the FATFS file system power-loss protection method, the non-volatile memory is flash memory or an SD card.

[0011] Preferably, the FATFS file system power-loss protection method is further improved by, in step S5, invalidating the backup area data by clearing the sector number and CRC checksum stored in the backup area.

[0012] Preferably, the FATFS file system power-loss protection method is further improved. When implementing step S6, checking whether the backup area data is valid specifically includes: determining whether the sector number stored in the backup area is valid and verifying whether the sector data in the backup area is complete through CRC check code. If the sector number is valid and the data verification passes, the backup area data is determined to be valid.

[0013] Preferably, the FATFS file system power-loss protection method is further improved by rewriting the underlying write driver function of the non-volatile memory to implement the operations of steps S2 to S5.

[0014] Preferably, the FATFS file system power-loss protection method is further improved by rewriting the execution flow of the underlying write driver function as "read-backup-erase-modify-write".

[0015] The present invention also provides a non-volatile memory that internally stores a computer program, which, when executed, implements the steps of the power-loss protection method for the FATFS file system described above.

[0016] This invention can achieve at least the following technical effects; 1. This invention divides the file storage medium into backup areas. Before erasing the sectors, the data, sector numbers, and CRC check codes are backed up to the backup areas and made valid. After writing is completed, the backup areas are made invalid. This design allows the data of sectors damaged during power failure to be recovered through the data in the backup areas.

[0017] Existing hardware-based power-loss protection solutions require the addition of hardware such as energy storage components and power detection modules, which inevitably leads to increased costs. However, this invention eliminates hardware dependence and achieves protection solely through software design, which is something that existing hardware solutions cannot achieve.

[0018] Therefore, this invention does not require additional hardware costs and can achieve power loss protection of the FATFS file system solely through software methods, thereby reducing system design and usage costs. 2. While existing file systems with built-in power-loss protection mechanisms can achieve partial power-loss protection, they are limited by application scenarios. Compared with the FATFS file system, they lack universality, cross-platform compatibility, and easy embedded portability, and cannot take into account both the advantages of the FATFS file system and the power-loss protection function.

[0019] This invention employs dual verification using CRC checksum and sector number to ensure the validity and accuracy of backup data, avoiding invalid backups or erroneous recovery. Existing technologies lack such targeted verification methods, making it difficult to guarantee the reliability of recovered data. This invention effectively avoids problems such as file system corruption and data loss caused by power failure during sector erasure and writing, ensuring data integrity and consistency. 3. Existing technologies do not offer effective software-level power-loss protection solutions for the FATFS file system. Existing technologies lack backup and recovery mechanisms, and the underlying driver processes do not consider power-loss protection requirements. Therefore, existing technologies cannot prevent file system corruption caused by power failure without adding hardware.

[0020] This invention optimizes the original "read-erase-modify-write" process into a "read-backup-erase-modify-write" process by rewriting the underlying write driver function of the storage medium corresponding to the FATFS file system. This achieves integrated power-loss protection without altering the original core architecture and characteristics of the FATFS file system. Existing technologies do not perform such optimizations to the underlying driver and cannot achieve power-loss protection without adding hardware. Upon power-up, this invention can automatically detect the validity of backup data and perform data recovery. The recovery process is simple, efficient, and requires no manual intervention.

[0021] 4. The power-on detection and recovery mechanism of this invention can automatically determine whether data recovery is needed, and only perform the recovery operation if the data in the backup area is valid. This ensures data recoverability without affecting the system startup efficiency during normal power-on. Existing technologies lack such an automatic detection and recovery mechanism and cannot achieve automatic data recovery after power failure. This invention is compatible with the original characteristics of the FATFS file system and does not affect its universality, cross-platform compatibility, or ease of embedded porting. Attached Figure Description

[0022] The accompanying drawings are intended to illustrate the general characteristics of the methods, structures, and / or materials used in specific exemplary embodiments of the invention, supplementing the description in the specification. However, the drawings are schematic diagrams not drawn to scale and may not accurately reflect the precise structural or performance characteristics of any of the given embodiments. The drawings should not be construed as limiting or restricting the range of numerical values ​​or properties covered by exemplary embodiments of the invention. The invention will now be described in further detail with reference to the accompanying drawings and specific embodiments: Figure 1 Block diagram of the power-off protection of this invention.

[0023] Figure 2 This is a flowchart of the FLASH underlying write driver function.

[0024] Figure 3 This is a flowchart of the power-on detection and recovery process. Detailed Implementation

[0025] The following specific embodiments illustrate the implementation of the present invention. Those skilled in the art can fully understand other advantages and technical effects of the present invention from the content disclosed in this specification. The present invention can also be implemented or applied through different specific embodiments, and various details in this specification can also be applied based on different viewpoints, with various modifications or changes made without departing from the overall design concept of the invention. It should be noted that, in the absence of conflict, the following embodiments and features in the embodiments can be combined with each other. The following exemplary embodiments of the present invention can be implemented in many different forms and should not be construed as being limited to the specific embodiments set forth herein. It should be understood that these embodiments are provided to make the disclosure of the present invention thorough and complete, and to fully convey the technical solutions of these exemplary embodiments to those skilled in the art. It should be understood that when an element is referred to as "connected" or "combined" to another element, the element can be directly connected or combined to the other element, or there may be intermediate elements. The difference is that when an element is referred to as "directly connected" or "directly combined" to another element, there are no intermediate elements. Throughout the drawings, the same reference numerals always denote the same elements.

[0026] First embodiment; In this embodiment, the non-volatile memory is FLASH, and power-loss protection is implemented based on the FATFS file system. The specific implementation steps are as follows: S1, allocate a region in the file storage medium as a backup area for file write operations; S2, when the file system performs a write operation on any sector in the storage medium, before performing the erase operation on that sector, the data of that sector is read out and saved to the backup area; S3, calculate the CRC checksum of the sector data, and save the CRC checksum and the sector number of the sector together to the backup area, making the data in the backup area valid; S4, perform the erase operation on the sector and complete the data writing of the sector; S5, after completing the writing of the data in this sector, invalidate the data in the backup area; S6: When the embedded file system is powered on again, check whether the backup area data is valid. If it is valid, restore the sector data saved in the backup area and write it back to the corresponding sector.

[0027] Based on the above main design ideas, the implementation process of this invention will be further explained using FLASH as an example. When FLASH is used as the storage medium of the file system, due to the block-based storage structure of FLASH, if a write operation is to be performed on FLASH, the data in that block must first be erased before data can be written to that block. The block concept of FLASH corresponds to the sector concept in the FATFS file system. The disk read and write functions of the FATFS file system ultimately call the underlying read and write operation functions of FLASH. A power outage during the read function will not damage the file system data, but a power outage during the write function is very likely to damage the file system data. The write operation on FLASH adopts the process of "read-erase-modify-write". If a power outage occurs during the "erase-modify-write" process, the sector data of the write operation will inevitably be destroyed. After power is restored, the original data cannot be recovered, thus causing the file system to be damaged, which manifests as corrupted files that cannot be opened or even the complete loss of all files.

[0028] This invention modifies the underlying write driver function of the FLASH memory to insert a backup operation between the "read-erase" steps in the "read-erase-modify-write" process, thus changing it to a "read-backup-erase-modify-write" process. Because a file system data check and recovery is also required upon power-on, in addition to backing up the sector data for the write operation, the backed-up data also needs to be valid. This invention uses additional backup sector location values ​​and CRC checksums of the sector data to indicate the validity of the backup data. Finally, upon completion of the write operation, the backup data is invalidated by clearing the sector location values ​​and CRC checksums. The underlying write driver function flow is referenced below. Figure 2 As shown.

[0029] Under normal circumstances, the application system checks for an invalid flag each time it powers on and skips file system data recovery. In abnormal circumstances, the application system checks for a valid flag upon power-on, indicating a power outage occurred during a previous FLASH write operation. File system data recovery is then performed, and after recovery, the flag is set back to invalid. Subsequent power-on data recovery is not required. Refer to the power-on detection and recovery process for details. Figure 3 As shown.

[0030] In the FLASH storage medium, in addition to the original file system area of ​​the FATFS file system, a region is allocated as a backup area for file write operations. The address of the backup area is set to FS_BAK_MEM_ADDR, which is used to store sector backup data, sector number and CRC check code.

[0031] Retain the FLASH write function of the original driver file, and create a new FLASH write function with the prefix FS, FS_W25QXX_Write. The exemplary specific code is as follows: void FS_W25QXX_Write(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) { uint8_t W25QXX_BUF[4096+4+2]; / / +4 is for reserving storage space for sector position values, +2 is for reserving storage space for CRC check values uint32_t secpos = WriteAddr / 4096; / / Calculate the sector address uint16_t secoff = WriteAddr % 4096; / / Calculate the offset address within the sector uint16_t secremain = 4096 - secoff; / / Calculate the remaining bytes that can be written in the current sector uint16_t i = 0; uint16_t crc = 0; W25QXX_Read(W25QXX_BUF, secpos*4096, 4096); / / Read the data of the current sector / / Check if the sector needs to be erased for(i=0; i<secremain; i++) { if(W25QXX_BUF[secoff+i] != 0XFF) break; / / There is non-0XFF data, the sector needs to be erased } if(i<secremain) / / The sector needs to be erased { / * Back up the data before erasing * / *(uint32_t *)(W25QXX_BUF+4096) = secpos; / / Fill the sector position value into the buffer crc = chk_crc16(W25QXX_BUF, 4096+4); / / Calculate the CRC check value together with the sector position value *(uint16_t *)(W25QXX_BUF+4096+4) = crc; / / Fill the buffer with CRC checksum values. / / Erase backup area W25QXX_Erase_Sector(FS_BAK_MEM_ADDR / 4096); W25QXX_Erase_Sector(FS_BAK_MEM_ADDR / 4096 + 1); / / Back up sector data, sector location values, and CRC checksums to the backup area W25QXX_Write_NoCheck(W25QXX_BUF, FS_BAK_MEM_ADDR, 4096+4+2); / / Erase the current sector and write new data W25QXX_Erase_Sector(secpos); / / Erase the current sector for(i=0; i <secremain; i++) { W25QXX_BUF[i+secoff] = pBuffer[i]; / / Fill the buffer with new data } W25QXX_Write_NoCheck(W25QXX_BUF, secpos*4096, 4096); / / Write new data to the current sector / * Invalidate the backup partition to prevent data restoration from occurring every time power is powered on * / W25QXX_Erase_Sector(FS_BAK_MEM_ADDR / 4096 + 1); } else / / No need to erase sectors, write data directly { W25QXX_Write_NoCheck(pBuffer, WriteAddr, secret); } } In the above function, the data to be written to the sector is first read, and it is determined whether it needs to be erased. If it needs to be erased, the sector data, sector position value and calculated CRC check code are first written to the backup area, and then the sector erasure and new data writing operations are performed. Finally, a part of the backup area is erased to invalidate the data in the backup area. If it does not need to be erased, new data is written directly.

[0032] The file system recovery function `recover_fs_data` is called when the system powers on. The specific code is as follows: / ** * @brief Restore file system data from the backup area * @retval 0 = Recovery successful, 1 = Invalid backup sector location value, 2 = Backup sector data verification failed / uint8_t recover_fs_data(void) { uint8_t buf[4096+4+2] = {0}; uint32_t secpos; / / Address of the backed-up sector uint16_t crc; / / Checksum value W25QXX_Init(); W25QXX_Read(buf, FS_BAK_MEM_ADDR, 4096+4+2); / / Read data from the backup area secpos = *(uint32_t *)(buf+4096); / / Retrieve the backup sector location value if(secpos>= BAK1_MEM_BASE_ADDR / 4096) { / * Invalid sector location value * / return 1; } crc = chk_crc16(buf, 4096+4); / / Calculate the CRC checksum along with the sector location value. if (crc != *(uint16_t *)(buf+4096+4)) { / * Validation failed * / return 2; } / * Restore file system data * / W25QXX_Write(buf, secpos*4096, 4096); / * Invalidate backup data * / W25QXX_Erase_Sector(FS_BAK_MEM_ADDR / 4096 + 1); return 0; } After the system powers on, it initializes the FLASH and reads the backup area data, extracts the sector location value and checks its validity, and calculates the CRC check value of the backup data and compares it with the backup check value. If the sector location value is valid and the check passes, the backup sector data is written back to the corresponding sector to complete the data recovery. Then, a part of the backup area is erased to invalidate the backup data. If the sector location value is invalid or the check fails, the recovery operation is not performed and the corresponding error code is returned. Example

[0033] This invention provides a non-volatile memory, which includes MROM (mask read-only memory), EPROM (erasable programmable ROM), EEPROM (electrically erasable programmable ROM), NOR Flash, NAND Flash, eMMC, UFS, NVMe SSD, FRAM / FeRAM (ferroelectric memory), MRAM (magnetic memory), PCRAM / PRAM (phase change memory), RRAM / ReRAM (resistive random access memory), HDD (hard disk drive) or magnetic tape. The non-volatile memory internally stores a computer program, which, when executed, implements the steps of the FATFS file system power-loss protection method described in the first embodiment.

[0034] Unless otherwise defined, all terms used herein (including technical and scientific terms) shall have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. It will also be understood that, unless explicitly defined herein, terms such as those defined in a general dictionary shall be interpreted as having the meaning consistent with their meaning in the relevant field context, and not as having an idealized or overly formal meaning.

[0035] The present invention has been described in detail above through specific embodiments and examples, but these are not intended to limit the invention. Many modifications and improvements can be made by those skilled in the art without departing from the principles of the invention, and these should also be considered within the scope of protection of the present invention.

Claims

1. A power-loss protection method for a FATFS file system, applied to an embedded file storage system using non-volatile memory as the storage medium, characterized in that, It achieves power failure protection solely through computer programming technology, including the following steps: S1, allocate a region in the file storage medium as a backup area for file write operations; S2, when the file system performs a write operation on any sector in the storage medium, before performing the erase operation on that sector, the data of that sector is read out and saved to the backup area; S3, calculate the CRC checksum of the sector data, and save the CRC checksum and the sector number of the sector together to the backup area, making the data in the backup area valid; S4, perform the erase operation on the sector and complete the data writing of the sector; S5, after completing the writing of the data in this sector, invalidate the data in the backup area; S6: When the embedded file system is powered on again, check whether the backup area data is valid. If it is valid, restore the sector data saved in the backup area and write it back to the corresponding sector.

2. The power-loss protection method for the FATFS file system according to claim 1, characterized in that, The non-volatile memory is a flash memory or an SD card.

3. The power-loss protection method for the FATFS file system according to claim 1, characterized in that: When implementing step S5, the data in the backup area is invalidated by clearing the sector numbers and CRC checksums stored in the backup area.

4. The power-loss protection method for the FATFS file system according to claim 1, characterized in that: When implementing step S6, checking whether the backup area data is valid specifically includes: determining whether the sector number stored in the backup area is valid and verifying whether the sector data in the backup area is complete through CRC checksum. If the sector number is valid and the data verification passes, then the backup area data is determined to be valid.

5. The power-loss protection method for the FATFS file system according to claim 1, characterized in that, Steps S2 to S5 are performed by rewriting the underlying write driver function of the non-volatile memory.

6. The power-loss protection method for the FATFS file system according to claim 5, characterized in that: The execution flow of the rewritten underlying write driver function is "read-backup-erase-modify-write".

7. A non-volatile memory, characterized in that: It internally stores a computer program, which, when executed, implements the steps of the FATFS file system power-loss protection method according to any one of claims 1-6.