Embodiment
Main thought of the present invention is: by set up the mode in a dynamic data backing-up district between file system cache (Cache) and Flash destination data district; realized power down protection, made universal document system can be used for the abominable embedded Flash storer of environment more reliably based on file system.
Thus, according to the embodiment of the invention, the storage space of flash memory comprises destination data district and Backup Data district, wherein, when file system cache during to described flash memory write-back, carry out the process that described data is write described Backup Data district earlier, after said write is complete, carry out the process that writes described destination data district again.
In storage system, further comprise the zone that is used to deposit the process byte, described proword feast-brand mark has shown the write phase of described data writing process.This process byte can leave in the flash memory identical with the Backup Data district with described destination data district, in NAND Flash, alternatively, this process byte also can leave in other storer of storage system, as NOR Flash, EEPROM etc., promptly be different from the storer at described destination data district and Backup Data place.
Wherein, this Backup Data district comprises data field and overhead area, and described overhead area comprises that data write the logical block number (LBN) and page or leaf skew in described destination data district.
For process byte of each stage record, indicate ablation process and enter corresponding write phase in ablation process, the realization system is recovered from unexpected power down.These process bytes can be pre-defined, is defined as process byte 1,2 and 3 as introducing below, and expression indicates the stage that power down protection begins respectively; Ablation process has been finished the work that writes in Backup Data district, may enter the work that writes the destination data district, and write partial data, also may just finish the work that writes the Backup Data district, does not also begin to write the stage of working in destination data district; With the stage that ablation process has been finished the operation that writes the destination data district, this three phases also is above-mentioned write phase.
As shown in Figure 2; be embedded system structure synoptic diagram according to the described adding power down protection of embodiments of the invention module; this embedded system comprises the embedded OS file system; NFTL; MTD and the parts such as storer that comprise flash memory etc.; compare with the described system architecture of Fig. 1; difference is: the storage space of described flash memory comprises destination data district and Backup Data district; described embedded system further comprises the power down protection module; be used for when file system cache during to this flash memory write-back; execution writes described data earlier the process in described Backup Data district; execute after this ablation process, carry out the process that data is write again described destination data district again.
Described power down protection module is further used for: according to current write phase, record indicates the process byte of current write phase in flash memory.Thereby when powering on, from unexpected power down, recover according to this process byte realization system.
After system powers on, after the NFTL loading is finished, not directly to load universal document system, but move the power loss recovery module earlier, reload universal document system.The power down protection module is judged current write phase according to the process byte, does not write the destination data district as yet if described data have write the Backup Data district, and the data taking-up that then will write the Backup Data district writes the destination data district as data source.
The Backup Data district is not the static reserved block of dividing on Flash, but realize by the logical blocks outside the Installed System Memory space; And the Backup Data district is the physical block of the described storer of dynamic assignment, can guarantee not destroy the erasable equilibrium that is realized by NFTL like this, can utilize the bad block management of being realized by NFTL simultaneously again, has simplified use.The storage space of flash memory comprises destination data district and Backup Data district in this embedded system, as described above, this Backup Data district comprises data field and overhead area, and described overhead area comprises that data write the logical block number (LBN) and page or leaf skew in described destination data district.And the record of described process byte can be recorded on the storer identical with the Backup Data district with described destination data district or on the storer different with the Backup Data district with described destination data district also with above-mentioned described.
Power down protection module of the present invention just acts on the ablation process of file system; with existing directly to call NFTL different the mode that data write the destination data district; need write the Backup Data district of dynamic assignment to data earlier after adding this module, call NFTL afterwards again data are write the destination data district.Add reading original file system after this module without any influence.
Fig. 3 is the principle schematic of power down protection mechanism of the present invention.As can be seen from Figure, data writing process of the present invention comprises three parts:
Step 301: the data to the flash memory write-back among the Cache are write the Backup Data district;
Step 302: the data to the flash memory write-back among the Cache are write the destination data district;
Step 303: this step is the operation that may take place, if power down occurs in above-mentioned steps 302, the back that powers on so more just needs to carry out this step, reads out and writes the destination data district being stored in data in the Backup Data district.
And before carrying out each step operation; the power down protection module writes the process byte of this process of carrying out earlier, and the record of process byte is recorded on the storer identical with the Backup Data district with described destination data district or on the storer different with the Backup Data district with described destination data district.With 0x55,0x5a, 0xa5 are that example is carried out following analysis.
As shown in Figure 4, the data of the power-down data protection method that realizes for the present invention write process flow diagram.This method may further comprise the steps as seen from the figure:
Step 401: in storer (as in flash memory NAND Flash or in other storer as NOR Flash, EEPROM) ablation process byte 1 is as 0x55, indicate power down protection and begin.
Step 402: collect and the Cache of system that arranges the document in need the data of write-back.The main upper strata of realizing writes the piece arrangement of sector by Flash, utilizes the characteristic of NFTL better, accelerates the writing speed of Flash.
Step 403: the data that step 402 is put in order write the Backup Data district.
Step 404: in storer ablation process byte 2 as 0x5a, the complete Backup Data district that writes of unlabeled data.
Step 405: the data that step 402 is put in order write the destination data district.
Step 406: in storer ablation process byte 3 as 0xa5, the complete destination data district that writes of unlabeled data.
Step 407: ablation process finishes.
Wherein, data write the Backup Data district and comprise step in the step 403: the current data field that writes page or leaf that the data of write-back is write the physical block in Backup Data district, the logical block number (LBN) in the destination data district that will write data and page or leaf skew are write the current overhead area that writes page or leaf of the physical block in Backup Data district, and page or leaf skew added 1, in order to write next time.
Nand Flash is divided into data field and overhead area two parts to storage space, is example with 2GBNand, and its page or leaf is made up of the overhead area of 2048 byte data districts and 64 bytes.Overhead area is normally used for ECC (error correcting code), loss equalization (wear leveling) and other software overhead function, and it has also comprised the key message of the reconstructed mapped table that powers on usually when using NFTL.
After system powered on, before the load document system, the power-down data protection method also comprised step:
(B-1) check the process byte that writes down in the storer;
(B-2) if be the second process byte, then the data in the described Backup Data district are read as data source, write described destination data district, and in storer, write down the 3rd process byte.Recovering process flow diagram by powering on of the described method for power fail safeguard of data of following Fig. 5 is described: Fig. 5 is the recovery process flow diagram that powers on of the method for power fail safeguard of data realized of the present invention.May further comprise the steps as seen from the figure:
Step 501: check the process byte (will introduce below in Fig. 7 smart card with large capacity system and describe in detail) that the power down protection module writes down in storer.According to three kinds of different situations of process byte, do different processing respectively.
If the process byte is that process byte 1 is as 0x55; illustrate that ablation process has entered power down protection; the write section divided data is in the Backup Data district; also may just enter power down protection; at this moment; the destination data district does not also write new data, and does not preserve complete new data as yet in the Backup Data district yet, then changes step 504 over to and carries out.
If the process byte is that process byte 2 is as 0x5a, illustrate that then ablation process finished the work that writes in Backup Data district, may enter the work that writes the destination data district, and write partial data, also may just finish the work that writes the Backup Data district, also not begin to write the work in destination data district.At this moment the Backup Data district has finished writing of new data, has promptly stored complete new data in the storage system, then changes step 502 over to and carries out.
If the process byte be process byte 3 as 0xa5, illustrate that then ablation process finished the operation that writes the destination data district, then change step 504 over to and carry out.
Step 502: read the data content in Backup Data district, write the destination data district.
Step 503: ablation process byte 3 is as 0xa5, and unlabeled data has write the destination data district.
Step 504: power loss recovery is finished dealing with, and withdraws from.
Wherein, the Backup Data district realizes that by the logical block outside the Installed System Memory space Backup Data district is the physical block of the storer of dynamic assignment, introduces the process of Backup Data district dynamic allocation memory physical block below to.As shown in Figure 6, be the process flow diagram of dynamic assignment of the physical block in Backup Data of the present invention district.May further comprise the steps:
Step 601: the logical block of search Flash is to the mapping table of physical block mapping relations, and whether existing corresponding physical block is corresponding with it to check the backup logical block.If having correspondence then to change step 602 over to carries out; If no correspondence then changes step 604 over to and carries out.
Mapping table is that NFTL is peculiar, it be in internal memory, set up one the current Flash of storage logical block to the table of physical block mapping relations, can find the pairing physical block of a certain logical block by it, promptly obtain the physical location of this logical block on Flash.Its expression formula can be expressed as:
Physical block number=mapping table [logical block number (LBN)]
Step 602: judge that the current page or leaf that writes of described physical block is offset whether (SectorCurrent) is 0.If 0, then illustrate this be last time the Cache write-back finish, or just powered on and write for the first time, change step 603 over to and carry out; If not 0, illustrate that then this is a sector in this Cache write-back, then finish.
Step 603: wipe described physical block.
Step 604: in free block list, distribute a physical block corresponding, finish with the backup logical block.
Referring to shown in Figure 7, be an application example of the present invention, the system architecture synoptic diagram of smart card with large capacity.As can be seen from the figure this system has comprised NAND Flash, stored program NOR Flash, ARM microprocessor, quickflashing controller (NFC) and some other module of storage data.
This system has comprised embedded OS, universal document system, and software modules such as NFTL, MTD, because of there being NOR Flash in the system, NOR Flash can read and write by byte, and simple to operate, can be as the process bytes of memory in the inventive method.
Below be the concrete operation method of process byte in this smart card with large capacity system:
1. the definition of each process byte
Process byte 1:0x55
Process byte 2:0xAA
Process byte 3:0x5A
Error detection byte: 0xA5
2. the wiring method of process byte, also i.e. recording process byte in described storer:
Writing of each process byte comprises five parts:
A. the process byte clear 0 that the last time is write;
B. judge that whether storing process bytes of memory address is the end in storing process bytes of memory district, if then wipe this memory block, begins to write from start address; Then continue if not to write.
C. write the proword joint of current write phase itself;
After the recording process byte, also to carry out D. and write the error detection byte.
E. if step C, D write and make mistakes, then storing process bytes of memory address adds 2, carries out from step B again.If step C, D correctly then finish.
Execution in step A mainly is in order to prevent that the effective procedure byte that the last time writes from exerting an influence to this operation.
3. the detection method of process byte, check that the step of the process byte that writes down in the storer comprises:
Step (1) begins to read one by one forward the process byte from the end in storing process bytes of memory district;
Step (2) judges whether to find error detection byte 0xA5, if find the error detection byte, then changes step (3) over to, if do not find the error detection byte up to the reference position in storing process bytes of memory district, then changes step (4) over to;
Step (3) judges whether the previous byte of described error detection byte is the process byte, if, then return described process byte, finish, if not, then continue to search next error detection byte 0xA5 forward, forward step (2) to;
Step (4), return course byte 3 finishes.
The present invention can guarantee not destroy the erasable equilibrium that is realized by NFTL, can utilize the bad block management of being realized by NFTL simultaneously again, simplifies and uses.Realized power down protection, made universal document system can be used for the abominable in-line memory of environment more reliably based on file system.
It should be noted last that, above embodiment is only unrestricted in order to the technical scheme of the explanation embodiment of the invention, although the embodiment of the invention is had been described in detail with reference to preferred embodiment, those of ordinary skill in the art is to be understood that, can make amendment or be equal to replacement the technical scheme of the embodiment of the invention, and not break away from the spirit and scope of embodiment of the invention technical scheme and claim.