Disclosure of Invention
Aiming at the defects in the prior art, the invention aims to provide a method for prolonging the writing life of a smart card FLASH, which updates file data in a FLASH page polling mode, prolongs the service life of the FLASH, and simultaneously adds a Cyclic Redundancy Check (CRC) to ensure the integrity of the data.
In order to achieve the above purposes, the technical scheme is as follows:
a method of improving smart card FLASH write life, the method comprising:
when a file system creates a file, a plurality of bytes are expanded at a file header to store the initial address and the polling page number of the file in a FLASH, a polling space comprising a plurality of physical pages is divided in the FLASH according to the initial address and the polling page number, a file body and a mark header are written into the polling space as a data block, and the mark header comprises a counter and a first CRC;
when the file system updates the file, the polling space is traversed by taking the block size of the data block as a unit until an effective block is found, the next continuous storage space of the effective block is judged to be a writable block, the updated file is written into the writable block, and the storage capacity of the next continuous storage space is the same as the block size of the data block.
Preferably, when the file system creates a file, after the file header is extended by 5 bytes, a start address of 4 bytes and the number of polling pages of 1 byte are written.
Preferably, the number of polling pages is the same as the number of physical pages in the polling space;
the storage capacity of the polling space is greater than the total number of bytes of the file.
Preferably, when the file system creates a file, a 16-byte flag header is configured for the file body, the 16-byte flag header includes a counter of 4 bytes and a first CRC of 2 bytes, and the remaining bytes are reserved.
Preferably, when the file system updates the file, the step of traversing the polling space by using the block size of the data block as a unit until a valid block is found is as follows:
a1, intercepting a current continuous storage space in the part which is not intercepted in the polling space of the file by taking the block size of the data block as a unit;
step a2, calculating a second CRC of the data stored in the current continuous storage space, and judging whether the first CRC and the second CRC in the marker header of the current continuous storage space are the same:
if yes, go to step a 3;
if not, judging that the current continuous storage space is an invalid block, and then turning to the step a 4;
step a3, judging whether the value of the counter of the current continuous storage space is larger than the value of the counter of the previous continuous storage space:
if yes, then the current continuous storage space is judged as a possible block, and then step a4 is executed;
if not, judging that the current continuous storage space is an invalid block, and then turning to the step a 4;
step a4, judging whether the storage capacity of the part which is not intercepted in the polling space of the file is larger than the block size of the data block:
if yes, go to step a 1;
and if not, the block with the maximum counter value in all the possible blocks is the valid block.
Preferably, the step of determining the next continuous storage space of the valid block as a writable block is as follows:
step b1, according to the address sequence of the polling space, judging whether the storage capacity behind the effective block in the physical page where the effective block is located is larger than the storage capacity of the effective block:
if so, taking the storage address behind the effective block in the physical page of the effective block as the initial address of the next continuous storage space, and judging the next continuous storage space as a writable block;
if not, the header address of the next physical page is used as the starting address of the next continuous storage space, and the next continuous storage space is judged to be a writable block.
Preferably, if the physical page in which the valid block is located is the last page of the polling space, and the storage capacity of the physical page in which the valid block is located after the valid block is smaller than the storage capacity of the valid block, the page head address of the start page of the polling space is used as the start address of the next continuous storage space.
Preferably, when the valid block and the writable block are located in the same physical page, if there is storage data in the writable block, the next continuous storage space of the writable block is used as the writable block and an updated file is written.
Preferably, when the valid block and the writable block are located in different physical pages, if the writable block has stored data, the physical page in which the writable block is located is subjected to data erasure and an updated file is written.
Preferably, the step of writing the updated file to the writable block is as follows:
step c1, obtaining the count value of the counter from the effective block, and writing the count value into the counter of the writable block after adding 1;
step c2, calculating CRC value for the updated file, and writing the CRC value into the first CRC of the writable block;
step c3, writing the file body of the updated file to the writable block.
The invention has the beneficial effects that: by the polling mode in the FLASH page, the polling space is traversed by taking the block size of the data block as a unit until a valid block and a writable block are found, and new data is directly written into the writable block, so that the method is much more efficient compared with the method that the page is firstly erased and then rewritten every time. The invention divides one page or a plurality of pages into a plurality of blocks, when updating the file each time, only the blocks are updated in sequence, and the blocks occupy the polling space and then are erased, so that one physical page can be repeatedly used, and the service life of the Flash is prolonged.
The CRC of the tag header and the file body is calculated before updating the file, and then the writable block is written. If the power is abnormally lost when the writable block is written, the data is not completely written, the CRC stored in the writable block is different from the CRC value calculated by actual data, and the data is treated as an invalid block because the CRC is not compared sufficiently when a valid block is searched in a subsequent traversal. At this time, the valid data of the file is still stored in the valid block, and the integrity of the file data is ensured.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific examples described herein are intended to be illustrative only and are not intended to be limiting. Moreover, all other embodiments that can be derived by a person skilled in the art from the embodiments of the present invention without making any creative effort belong to the protection scope of the present invention.
The present invention will be described in further detail with reference to the accompanying drawings and examples.
As shown in fig. 1, a method for improving FLASH write lifetime of a smart card includes:
step S1, when the file system creates the file, the file header extends a plurality of bytes to store the initial address and polling page number of the file in the FLASH, the polling space comprising a plurality of physical pages is divided in the FLASH according to the initial address and the polling page number, the file body and the marking header are written into the polling space as a data block, the marking header comprises a counter and a first CRC, and the first CRC is obtained by data in the calculation block.
Step S2, when the file system updates the file, the polling space is traversed by taking the block size of the data block as a unit until an effective block is found, the next continuous storage space of the effective block is judged to be a writable block, the updated file is written into the writable block, and the storage capacity of the next continuous storage space is the same as the block size of the data block.
In this embodiment, after traversing the polling space to find the valid block, it is determined whether the remaining storage space of the page where the valid block is located can form a continuous storage space with the same storage capacity as the block size of the data block, if so, a data block located behind the valid block in the page where the valid block is located is a writable block, and if not, a first database located at the head of the page in the next page where the valid block is located is a writable block. Subsequently, the updated file is written into the writable block.
By the polling mode in the FLASH page, the polling space is traversed by taking the block size of the data block as a unit until a valid block and a writable block are found, and new data is directly written into the writable block, so that the method is much more efficient compared with the method that the page is firstly erased and then rewritten every time. The invention divides one page or a plurality of pages into a plurality of blocks, when updating the file each time, only the blocks are updated in sequence, and the blocks occupy the polling space and then are erased, so that one physical page can be repeatedly used, and the service life of the Flash is prolonged.
Further, when the file system creates a file, after the file header is extended by 5 bytes, a start address of 4 bytes and the polling page number of 1 byte are written.
Further, the storage capacity of the polling space is larger than the total number of bytes of the file.
Further, when the file system creates a file, a 16-byte flag header is configured for the file body, and the 16-byte flag header includes a counter of 4 bytes and a first CRC of 2 bytes.
Further, as shown in fig. 2, when the file system updates the file, the specific steps of traversing the polling space by using the block size of the data block as a unit until a valid block is found are as follows:
a1, intercepting a current continuous storage space in the part which is not intercepted in the polling space of the file by taking the block size of the data block as a unit;
step a2, calculating a second CRC of the data stored in the current continuous storage space, and judging whether the first CRC and the second CRC in the marker header of the current continuous storage space are the same:
if yes, go to step a 3;
if not, judging that the current continuous storage space is an invalid block, and then turning to the step a 4;
step a3, judging whether the value of the counter of the current continuous storage space is larger than the value of the counter of the previous continuous storage space:
if yes, then the current continuous storage space is judged as a possible block, and then step a4 is executed;
if not, judging that the current continuous storage space is an invalid block, and then turning to the step a 4;
step a4, judging whether the storage capacity of the part which is not intercepted in the polling space of the file is larger than the block size of the data block:
if yes, go to step a 1;
and if not, the block with the maximum counter value in all the possible blocks is the valid block.
Specifically, when the file is updated every time, the file is searched from the start address backward in sequence by taking the block size of the data block as a unit according to the start address and the allocated polling page number, the second CRC of the data in the block is calculated in sequence, whether the second CRC is consistent with the first CRC in the marking head of the block where the second CRC is located is checked, and if the second CRC is consistent with the first CRC in the marking head of the block where the first CRC is located and the value of a counter in the marking head is larger than that in the previous block, the block is considered to be an effective block.
Further, as shown in fig. 3, the specific steps of determining that the next consecutive storage space of the valid block is a writable block are as follows:
step b1, according to the address sequence of the polling space, judging whether the storage capacity behind the effective block in the physical page where the effective block is located is larger than the storage capacity of the effective block:
if so, taking the storage address behind the effective block in the physical page of the effective block as the initial address of the next continuous storage space, and judging the next continuous storage space as a writable block.
If not, the header address of the next physical page is used as the starting address of the next continuous storage space, and the next continuous storage space is judged to be a writable block.
Furthermore, if the physical page in which the valid block is located is the last page of the polling space, and the storage capacity of the physical page in which the valid block is located after the valid block is smaller than the storage capacity of the valid block, the page head address of the start page of the polling space is used as the start address of the next continuous storage space.
And when the valid block and the writable block are located in the same physical page, if the writable block has stored data, using the next continuous storage space of the writable block as the writable block, and writing an updated file in the writable block.
And when the valid block and the writable block are located in different physical pages, if the writable block has stored data, performing data erasing on the physical page where the writable block is located, and writing an updated file into the writable block.
Specifically, the address of the valid block with the largest counter value is checked, the remaining space and the block size in the page where the valid block is located are checked, if the remaining space is enough, the tag head counter of the valid block is read, the counter value +1 is used for calculating the CRC of the file to be written, and then the CRC and the file body of the file to be written are written into the subsequent area of the block together, namely the writable block. And if the residual space is insufficient, checking whether a physical page of the effective block is the last page of the polling space, if so, erasing the first page of the polling space and then writing the data, if not, switching to the next page to write the data, before writing, checking whether the region can be written, and if not, searching backwards.
Further, as shown in fig. 4, the specific steps of writing the updated file into the writable block are as follows:
step c1, obtaining the counter value of the counter from the effective block, and writing the counter value into the counter of the writable block after adding 1.
Step c2, calculates a CRC value for the updated file and writes the CRC value to the first CRC of the writable block.
Step c3, writing the file body of the updated file to the writable block.
Specifically, the CRC of the file to be written is calculated and then written as the first CRC in the writable block together with the file body of the file to be written in the subsequent area of the block.
In one embodiment, 2 physical pages are divided into FLASH of the smart card as a polling space to store the file body of the file with high updating frequency, and the initial address of the polling space is set to 0x 0002000.
5 bytes (including a start address of 4 bytes and a polling page number of 1 byte) are extended in the header. And simultaneously writing data of a data block in the polling space, wherein the data block comprises a 16-byte marking head (comprising a counter of 4 bytes and a first CRC of 2 bytes, and other bytes are not written temporarily) and a file body.
When updating the file content, the initial address and polling page number of the file in the polling space are read from the file header, and are 0x0002000 and 2 pages respectively. Assuming that the block size of a data block (including a tag header and a file body) is 0x32 bytes, the page size of a physical page is 0x200 bytes, traversal is performed in units of 0x32 bytes from addresses 0x0002000 to 0x0002400, the value of a counter included in the tag header in each block is read, a second CRC of data within the block in each block is calculated and compared with a first CRC in the tag header, if the same and the value of the counter is greater than the value of a counter included in the tag header in a previous block, the current block is considered to be a valid block, the address of the current block is obtained after traversal, the current block is considered to be a valid block, and the address of the current block is the address of the valid block.
And searching the address of the next writable block backwards according to the address of the effective block, for example, if the address of the effective block is 0x00020032, the address range of the next block is 0x 00020064-0 x00020096, if the ending address of the next block is not in the same page as the starting address of the effective block, the header address of the next page is used as the starting address of the next block, and if the ending address of the next block exceeds the polling space, the header content of the polling space is erased, and the header address of the polling space is used as the address of the next block.
Checking whether the space where the address of the next block is located can be written, if the space can not be written, skipping the block, continuing to search backwards, if the space can not be written, obtaining the value of the marking head counter of the effective block, adding the value of the counter to +1, then calculating the first CRC of the data in the block which is to be updated and written into the writable block, and finally writing the file body content and the marking head content which need to be updated.
The CRC of the tag header and the file body is calculated before updating the file, and then the writable block is written. If the power is abnormally lost when the writable block is written, the data is not completely written, the CRC stored in the writable block is different from the CRC value calculated by actual data, and the data is treated as an invalid block because the CRC is not compared sufficiently when a valid block is searched in a subsequent traversal. At this time, the valid data of the file is still stored in the valid block, and the integrity of the file data is ensured.
The present invention is not limited to the above-described embodiments, and it will be apparent to those skilled in the art that various modifications and improvements can be made without departing from the principle of the present invention, and such modifications and improvements are also considered to be within the scope of the present invention. Those not described in detail in this specification are within the skill of the art.