A flash-based data security storage method
By partitioning the FLASH memory and performing bad sector detection and data verification, the problem of data corruption during power outages or erasures in FLASH memory is solved, thereby achieving data accuracy and extending its lifespan.
Patent Information
- Application Number
- CN202311021320.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-14
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2043-08-14
AI Technical Summary
Existing FLASH memory is prone to data corruption and unreliability during power outages or erasures, and cannot detect bad sectors, resulting in unreliable data. Furthermore, erasing small amounts of data wastes space and extends the lifespan of the memory.
The FLASH memory is divided into multiple storage areas, each with a preset number of occupiable sectors. Bad sector detection and data verification are performed, the area size is dynamically adjusted, and data is inherited after the detection is passed, reducing the number of erase cycles.
To ensure data accuracy, extend FLASH lifespan, reduce the number of erase cycles, avoid the impact of bad sectors, and improve data reliability.
Smart Images

Figure CN116860178B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a FLASH-based secure data storage method, belonging to the field of data storage technology. Background Technology
[0002] Flash memory is a type of memory device, specifically a non-volatile memory. Non-volatile means that the stored data will not be lost when the power is off. Based on this characteristic, devices equipped with flash memory will not lose the data saved before the power outage if a sudden or unexpected power failure occurs.
[0003] If current non-volatile memory is used directly without a management solution, a power outage during the erase or write process can lead to situations where old data is erased or incompletely erased before new data is written. This results in severe data corruption, rendering the data unreliable and potentially causing unpredictable errors in actual products.
[0004] Flash memory has a minimum erasable unit: a sector. Each sector typically has a size of 4096 bytes (32768 bits), and each bit has an independent lifespan. During erasure, lifespan is consumed only when a bit changes from 0 to 1. This means that repeated erasures do not affect lifespan if the data in certain bits is not changed. However, if you need to store and repeatedly update data much smaller than a sector (4096 bytes), some bits within a sector will eventually run out of lifespan, rendering the entire sector unusable.
[0005] Without bad sector control, when a sector becomes unusable due to lifespan issues, the FLASH memory cannot detect the bad sector and will still store data according to the normal process, thus making the data unreliable and posing a risk to its use.
[0006] Flash memory lacks data verification functionality. This means that once data is stored, unforeseen circumstances such as electronic misalignment or power outages may cause the data retrieved later to differ from the previously stored data. Flash memory itself cannot determine the correctness of stored data.
[0007] Flash erase can only erase one sector. If you want to store some small data, such as "device cumulative running time", which only takes up 4 bytes, you have to store it once every minute. However, each storage requires erasing one sector (4KB), which is a huge waste of space and lifespan. Summary of the Invention
[0008] To address the problems existing in the prior art, this invention proposes a data security storage method based on FLASH.
[0009] The technical solution of the present invention is as follows:
[0010] On the one hand, the present invention provides a FLASH-based secure data storage method, comprising the following steps:
[0011] The FLASH memory is divided into multiple storage areas for storing data. Each storage area has N pre-set occupiable sectors. When each storage area is used for the first time, it occupies only one pre-set occupiable sector. As the amount of stored data increases, it dynamically occupies other pre-set occupiable sectors.
[0012] When a storage area needs to occupy a new preset available sector to write data, a bad sector check is performed on the new preset available sector before the data is written. If the check passes, the data is written to the new preset available sector. If the check fails, the new preset available sector is determined to be a bad sector, and the storage area occupies another new preset available sector and performs the above bad sector check until the check passes. When the data is successfully stored in the storage area, a readback check is performed. If the check fails, the data is rewritten.
[0013] When the number of pre-defined available sectors in the storage area reaches a threshold, each time a new pre-defined available sector needs to be occupied, the data stored in the current storage area is checked. If the data check fails, a sector is added to the available sectors of the current storage area. If the data check passes, the subsequent data is written to the new storage area, and it is determined whether the data in the current storage area should be inherited into the new storage area.
[0014] In a preferred embodiment of the present invention, each storage area is provided with a fixed area header information that occupies the first X bytes of the current storage area;
[0015] The region header information includes: the number of sectors occupied by the storage region, the storage region status, the storage region identifier, and the number of times the storage region sectors have been erased.
[0016] In a preferred embodiment of the present invention, each time data is stored in the storage area, a data header information that occupies the first Y bytes of the current data is set;
[0017] The header information includes: data type, data content verification information, data length, data identifier, data write time, number of times the data type was written, and the number of data entries saved.
[0018] In a preferred embodiment of the present invention, the specific steps of bad area detection are as follows:
[0019] Before writing data to the new preset available sector, the entire new preset available sector is first erased. After erasing, it is checked whether all bits in the current sector are 1. If all bits are 1, the current sector is determined to have been erased successfully, and data is written normally. A "valid" flag is written to the current sector address header identifier byte. If there are any bits that are not 1, the current sector is determined to have not been erased successfully, and it is erased again. If it is still not erased successfully after Z erasures, the current sector is regarded as a bad sector, and an "invalid" flag is written to the current sector address header identifier byte.
[0020] In a preferred embodiment of the present invention, when the storage area is in the data writing state, the storage area status in its area header information is written as "in use" and when the data writing is completed, the storage area status in its area header information is written as "used up". The status "used up" must be written based on the status "in use".
[0021] In a preferred embodiment of the present invention, the specific steps of data detection are as follows:
[0022] All data stored in the current storage area is screened, all valid data is counted and the total size is calculated. If the size of the valid data exceeds 20% of the current storage area size, it is judged as failing the data detection.
[0023] In a preferred embodiment of the present invention, the specific steps of the data inheritance determination are as follows:
[0024] If the remaining available sector space in the current storage area is less than twice the current storage area space, then the data in the current storage area is read back, all valid data is read and written to the new storage area in sequence;
[0025] After all data is inherited, the storage region status in the region header information of the new storage region is written to "in use", and then the current storage region and other unused sectors are erased.
[0026] On the other hand, the present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the method as described in any embodiment of the present invention.
[0027] In another aspect, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described in any embodiment of the present invention.
[0028] The present invention has the following beneficial effects:
[0029] 1. This invention checks each piece of data written to ensure its accuracy, and the written data includes verification information to ensure the accuracy of the read data.
[0030] 2. This invention dynamically adjusts the size of each storage area to ensure that a large amount of new data is added, which would lead to data loss.
[0031] 3. This invention automatically inherits data, reducing the number of flash erase cycles and increasing lifespan.
[0032] 4. This invention includes a bad sector detection feature. After each erasure, the erased data is checked. If there are bad sectors, they are marked and the bad sector information is written into the storage area header information. The next time this data is needed, this sector will be skipped. Attached Figure Description
[0033] Figure 1 This is a flowchart of the method of the present invention. Detailed Implementation
[0034] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0035] It should be understood that the step numbers used in the text are for ease of description only and are not intended to limit the order in which the steps are performed.
[0036] It should be understood that the terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the invention. As used in this specification and the appended claims, the singular forms “a,” “an,” and “the” are intended to include the plural forms unless the context clearly indicates otherwise.
[0037] The terms “comprising” and “including” indicate the presence of the described feature, whole, step, operation, element and / or component, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or collections thereof.
[0038] The term “and / or” refers to any combination of one or more of the associated listed items, as well as all possible combinations, and includes these combinations.
[0039] Example 1:
[0040] See Figure 1 A method for secure data storage based on FLASH memory includes the following steps:
[0041] The FLASH memory is divided into multiple storage areas for storing data. Each storage area has N pre-set occupiable sectors. When each storage area is used for the first time, it occupies only one pre-set occupiable sector. As the amount of stored data increases, it dynamically occupies other pre-set occupiable sectors.
[0042] When a storage area needs to occupy a new preset available sector to write data, a bad sector check is performed on the new preset available sector before the data is written. If the check passes, the data is written to the new preset available sector. If the check fails, the new preset available sector is determined to be a bad sector, and the storage area occupies another new preset available sector and performs the above bad sector check until the check passes. When the data is successfully stored in the storage area, a readback check is performed. If the check fails, the data is rewritten.
[0043] When the number of pre-defined available sectors in the storage area reaches a threshold, each time a new pre-defined available sector needs to be occupied, the data stored in the current storage area is checked. If the data check fails, a sector is added to the available sectors of the current storage area. If the data check passes, the subsequent data is written to the new storage area, and it is determined whether the data in the current storage area should be inherited into the new storage area.
[0044] In a preferred embodiment of this invention, each storage region is provided with a region header information that occupies the first X bytes of the current storage region.
[0045] The region header information includes: the number of sectors occupied by the storage region, the storage region status, the storage region identifier, and the number of times the storage region sectors have been erased.
[0046] In a preferred embodiment of this invention, each time data is stored in the storage area, a data header information that occupies the first Y bytes of the current data is set.
[0047] The header information includes: data type, data content verification information (including but not limited to CRC check, cumulative check, MD5 check, etc. for data content), data length, data identifier, data write time, number of times data type is written, and number of data entries saved.
[0048] In a preferred embodiment of this invention, the specific steps for bad sector detection are as follows:
[0049] Before writing data to the new preset available sector, the entire new preset available sector is first erased. After erasing, it is checked whether all bits in the current sector are 1. If all bits are 1, the current sector is determined to have been erased successfully, and data is written normally. A "valid" flag is written to the current sector address header identifier byte. If there are any bits that are not 1, the current sector is determined to have not been erased successfully, and it is erased again. If it is still not erased successfully after Z erasures, the current sector is regarded as a bad sector, and an "invalid" flag is written to the current sector address header identifier byte.
[0050] In a preferred embodiment of this example, when the storage area is in the data writing state, the storage area status in its area header information is written as "in use". When the data writing is completed, the storage area status in its area header information is written as "used up". Since each bit of FLASH is written from data "1" to data "0", the status "used up" must be written based on the status "in use". For example, if the status "in use" is 1 and "used up" is 0, the area status only needs to occupy one bit.
[0051] In a preferred embodiment of this invention, the specific steps of data detection are as follows:
[0052] All data stored in the current storage area is screened, all valid data is counted and the total size is calculated. If the size of the valid data exceeds 20% of the current storage area size, it is judged as failing the data detection.
[0053] In a preferred embodiment of this invention, the specific steps for determining data inheritance are as follows:
[0054] If the remaining available sector space in the current storage area is less than twice the current storage area space, then the data in the current storage area is read back, all valid data is read and written to the new storage area in sequence;
[0055] After all data is inherited, the storage region status in the region header information of the new storage region is written to "in use", and then the current storage region and other unused sectors are erased.
[0056] Example 2:
[0057] This embodiment proposes an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, it implements the method described in any embodiment of the present invention.
[0058] Example 3:
[0059] This embodiment proposes a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the method described in any embodiment of the present invention.
[0060] In this application embodiment, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent the existence of A alone, A and B simultaneously, or B alone. A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. "At least one of the following" and similar expressions refer to any combination of these items, including any combination of singular or plural items. For example, at least one of a, b, and c can represent: a, b, c, a and b, a and c, b and c, or a and b and c, where a, b, and c can be single or multiple.
[0061] Those skilled in the art will recognize that the units and algorithm steps described in the embodiments disclosed herein can be implemented using electronic hardware, computer software, or a combination of electronic hardware and software. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0062] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0063] In the several embodiments provided in this application, any function, if implemented as a software functional unit and sold or used as an independent product, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0064] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent structural or procedural transformations made based on the content of the present invention's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A FLASH-based data security storage method, characterized in that, Includes the following steps: The FLASH memory is divided into multiple storage areas for storing data. Each storage area has N pre-set occupiable sectors. When each storage area is used for the first time, it occupies only one pre-set occupiable sector. As the amount of stored data increases, it dynamically occupies other pre-set occupiable sectors. When a storage area needs to occupy a new preset available sector to write data, a bad sector check is performed on the new preset available sector before the data is written. If the check passes, the data is written to the new preset available sector. If the check fails, the new preset available sector is determined to be a bad sector, and the storage area occupies another new preset available sector and performs the above bad sector check until the check passes. When the data is successfully stored in the storage area, a readback check is performed. If the check fails, the data is rewritten. When the number of pre-defined available sectors in the storage area reaches a threshold, each time a new pre-defined available sector needs to be occupied, data detection is performed on the data stored in the current storage area. The specific steps of data detection are as follows: all data stored in the current storage area is screened, all valid data is counted, and the total size of all valid data is calculated. If the total size of valid data exceeds 20% of the current storage area size, it is determined that the data detection has failed; otherwise, it is determined that the data detection has passed. If the data detection fails, a sector is added to the available sectors of the current storage area. If the data detection passes, subsequent data is written to the new storage area, and it is determined whether the data in the current storage area should be inherited into the new storage area.
2. The FLASH-based data security storage method of claim 1, wherein, Each storage region has a fixed region header information that occupies the first X bytes of the current storage region; The region header information includes: the number of sectors occupied by the storage region, the storage region status, the storage region identifier, and the number of times the storage region sectors have been erased.
3. The FLASH-based data security storage method of claim 1, wherein, Each time data is stored in the storage area, a data header information that occupies the first Y bytes of the current data is set; The header information includes: data type, data content verification information, data length, data identifier, data write time, number of times the data type was written, and the number of data entries saved.
4. The data security storage method based on FLASH according to claim 1, characterized in that, The specific steps for bad sector detection are as follows: Before writing data to the new preset available sector, the entire new preset available sector is first erased. After erasing, it is checked whether all bits in the current sector are 1. If all bits are 1, the current sector is determined to have been erased successfully, and data is written normally. A "valid" flag is written to the current sector address header identifier byte. If there are any bits that are not 1, the current sector is determined to have not been erased successfully, and it is erased again. If it is still not erased successfully after Z erasures, the current sector is regarded as a bad sector, and an "invalid" flag is written to the current sector address header identifier byte.
5. A data security storage method based on FLASH according to claim 2, characterized in that, When the storage area is in the data writing state, the storage area status in its area header information is written with the status "in use". When the data writing is completed, the storage area status in its area header information is written with the status "completed". The status "completed" must be written based on the status "in use".
6. A data security storage method based on FLASH according to claim 2, characterized in that, The specific steps to determine whether to inherit data from the current storage area to the new storage area are as follows: If the total space of the preset available sectors that are not occupied in the current storage area is less than twice the sector space corresponding to the number of sectors occupied by the storage area in the area header information, then the data in the current storage area is read back, all valid data is read and written into the new storage area in sequence. After all data is inherited, the storage region status in the region header information of the new storage region is written to "in use", and then the current storage region and other unused sectors are erased.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the method as described in any one of claims 1 to 6.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Data read-write method of Flash memory and device, medium and equipment
CN110413223A
Field programmable gate array (FPGA) remote online updating method based on PowerPC
CN116204204A
