Background technology
Along with the development of mobile communication technology, the use of mobile phone is very universal, and mobile phone uses removable smart card to carry out communication mostly, as SIM card, UIM card etc.When the user used mobile phone communication, mobile phone carried out read-write operation to smart card, as read the users personal data that is stored in the smart card, write the associated person information of the renewal menu that receives or user's input etc. to smart card.But, when mobile phone writes data to smart card, the data that write are to be temporarily stored in the smart card, imperfect tense cut off the power supply in case mobile phone writes data to smart card, smart card is about to legacy data and wipes, and cuts off the power supply when not finishing write operation, more new data can't correctly be write in the smart card, and legacy data is lost, and not only causes damage to the user, even can influence the operate as normal of smart card.
Therefore, existing smart card is provided with CPU (central processing unit) and storer, and is provided with the power-off protection district mostly in the storer, is used for storing the original data of smart card.The storage organization of existing smart card memory as shown in Figure 1.As seen from Figure 1; the storer of existing smart card is divided into three districts; be respectively code area 1, power-off protection district 2 and user data area 3; wherein code area 1 stores software that smart card operation uses etc., the data that user data area 3 is used to store user's personal data, write by mobile phone etc.And storer shown in Figure 1 is a page virtual memory, and the storage space of storer is divided into the storage block of multipage, and the storage size of each page storage block is fixed and equate.
Referring to Fig. 2, be the storage organization synoptic diagram in power-off protection district 2.The power-off protection district 2 of existing smart card comprises index area 21, first backup area 22 and second backup area 23; wherein the size of index area 21 is one page storage block; store the Data Update sign, upgrade index informations such as data source address, destination address and data length; first backup area 22 is two storage blocks that storage size is one page, and second backup area 23 is that two storage size are two pages storage block.
By mobile phone when smart card writes data, at first searching more, which page storage block new data need write in, and all data of this storage block are copied in first backup area or second backup area, then, CPU (central processing unit) is provided with the Data Update sign of index area, and writes corresponding index information.And again will be more new data write to the corresponding position of this storage block.After finishing aforesaid operations, CPU (central processing unit) is removed the Data Update sign in the index area.
Thus; if in smart card writes the process of data, cut off the power supply by mobile phone; after smart card powers on once more; CPU (central processing unit) can be judged and is provided with the Data Update sign in the index area; and store original data in the storage block of new data more in the power-off protection district; CPU (central processing unit) knows that just write operation last time is unsuccessful, and the data in the power-off protection district are write back former storage block, guarantees that the data of former storage block are not lost.
But; this power-off protection method need be provided with the power-off protection district of seven pages of storage block sizes in the storer of smart card; if the storage space of each page storage block is bigger, then the power-off protection district will take a large amount of storage spaces, and the storage space of smart card reservoir can not obtain fully effectively utilizing.
Summary of the invention
Fundamental purpose of the present invention provides a kind of smart card power-off protection method that reduces the shared storage space in power-off protection district in the smart card memory;
Another object of the present invention provides a kind of smart card power-off protection method that effectively makes full use of the smart card memory storage space.
For realizing above-mentioned fundamental purpose, in the smart card power-off protection method provided by the invention, smart card has CPU (central processing unit) and storer, and storer is divided into the multipage storage block, and this power-off protection method comprises
Set the data backup district of one page storage block at least in storer, the data backup district is the storage block of integer page or leaf, and the storage block at its place is the back-up storage piece;
The footers of each page storage block is set, the footers of every page of storage block is write in the start-stop byte of corresponding stored piece;
CPU (central processing unit) judges whether data updated only is written in one page storage block, in this way, carries out the data step of updating and the first data recovering step;
The Data Update step comprises: CPU (central processing unit) is searched and need be write the more updated stored piece of new data, and calculates the more start byte of new data in the updated stored piece and termination byte; CPU (central processing unit) is revised as the start byte of back-up storage piece the footers of updated stored piece, and with data before the start byte of new data correspondence more in the updated stored piece, more in new data and the updated stored piece more the data after the termination byte of new data correspondence be copied in the back-up storage piece, wherein, data before the start byte of new data correspondence more in the updated stored piece, more in new data and the updated stored piece more the data after the termination byte of new data correspondence be stored in successively in the back-up storage piece, CPU (central processing unit) is revised as the termination byte of back-up storage piece the footers of updated stored piece; Wipe the data of updated stored piece, and the footers of updated stored piece is revised as the footers of back-up storage piece;
The first data recovering step: after smart card powers on, judge whether that the footers of start-stop bytes store of storage block is inconsistent, in this way, write the footers of back-up storage piece, and wipe the data of this storage block to the start-stop byte of this page storage block; And, after CPU (central processing unit) powers on, the footers that also can judge whether two pages of storage blocks is identical, footers if any two pages of storage blocks is identical, then will be wherein the footers of one page storage block be revised as the footers of back-up storage piece, and wipe footers and be modified to data in the storage block of back-up storage piece footers.
By such scheme as seen, smart card is more during new data, after not needing all data with the updated stored piece to be copied to the back-up storage piece, write more new data again, but with do not need in the updated stored piece data updated and more new data be copied to the back-up storage piece, and the back-up storage piece is changed to common storage block after finishing duplicating, change the updated stored piece into the back-up storage piece.Like this, the storage block that only needs to be provided with one page or two pages in the storer gets final product as the back-up storage piece, and the storage space that the power-off protection district takies is less, and improves the storage space utilization factor of storer.
And, writing when cutting off the power supply in the data procedures to smart card by mobile phone, the footers of the start-stop bytes store of back-up storage piece is inconsistent, and CPU (central processing unit) is can the judgment data write operation unsuccessful, the data of wiping the back-up storage piece.Like this, can guarantee that updated stored piece Central Plains data can not lose, guarantee the operate as normal of smart card.
Embodiment
Referring to Fig. 3, be the storage organization synoptic diagram of smart card memory in the embodiment of the invention.In the present embodiment, the storer that smart card is provided with CPU (central processing unit) and can be carried out read-write operation by CPU (central processing unit), wherein, storer is a page virtual memory, is made of the multipage storage block, the storage size of each page storage block is fixed and is equated.
In the present embodiment, need encode, the footers of each page just is set, and this footers be write in the start byte and termination byte of each page storage block each page storage block in the storer.As shown in Figure 3, the coding of storage block begins sequential encoding from 0x01, and the coding of last storage block is set at 0xFF, uses last storage block as the data backup district.When CPU (central processing unit) reads the data of storer, according to the coded sequence reading of data of each page storage block.Therefore, the coding of all storage blocks needs continuously in the storer, but storage block need not arranged according to coded sequence.
Certainly, in the present embodiment, the footers of storage block uses a byte representation, if storage block quantity is more, then can use the byte representation more than two or two.
One data backup district is set in storer, and this data backup district should comprise one page storage block at least, and data backup district storage size is the storage block of integer page or leaf.In the present embodiment, last one page storage block is set to the data backup district, and the storage block that just is numbered 0xFF is the data backup district, and this storage block is the back-up storage piece just.Not when smart card writes data, the back-up storage piece is not stored any data.
Also be provided with an index area in the storer, when the more new data that writes to smart card when mobile phone need be written to storage block more than two pages or two pages, need upgrade data storage in the index area, the storage organization of index area as shown in Figure 4.The index area comprises first index area 41 and second index area 42, wherein first index area 41 comprises four index slips, each index slip comprises Data Update sign, the more destination address that writes of new data, the more length of new data and the more new data that need write, and wherein more the length of new data is no more than 40 bytes.Second index area comprises an index slip, and this index slip comprises Data Update sign, the more destination address that writes of new data, the more length of new data and the more new data that need write, and wherein more the length of new data can surpass 40 bytes.
Whether as seen from Figure 4, include two data updating mark in each index slip, being used for identifying the manipulative indexing bar has more new data.Need be when the index area writes more new data, data that at first are provided with in the index slip are upgraded sign, will upgrade again data length, destination address and more new data be written in the corresponding index slip, another Data Update is set at last identifies.Like this, cut off the power supply if write in the data procedures, after smart card powered on, CPU (central processing unit) can successfully write more new data to the index area by judging that two data renewals identify whether consistent judging whether in the index slip.
In the same manner, CPU (central processing unit) is at first removed one of them Data Update sign after finishing the Data Update operation, then other index informations in this index slip is wiped, and removes another Data Update sign at last.As if cutting off the power supply in the process of wiping the index area index information in CPU (central processing unit), after smart card powered on, CPU (central processing unit) also can judge whether need continue to wipe corresponding information in the index slip by judging in the index slip that two data updating mark are whether consistent.
By mobile phone when smart card writes data, CPU (central processing unit) judges at first whether the data that write need to be written in the storage block more than two pages or two pages, in this way, judge further then whether the more new data that is written to storage block surpasses 40 bytes, if be no more than 40 bytes, then data updated is write in first index area 41 in any index slips, and the Data Update sign is set, write the destination address that new data more need write and the index informations such as length of new data more.If data updated surpasses 40 bytes, then data updated is write in second index area 42.
If the more new data that writes of mobile phone only is written in one page storage block, the destination address that the more new data that CPU (central processing unit) provides according to mobile phone writes searches more which page storage block the new data needs write in, and this storage block is the updated stored piece.Simultaneously, CPU (central processing unit) is also calculated more start byte and the termination byte of new data in the updated stored piece.
Then, CPU (central processing unit) is copied to the more new data that does not need data updated and mobile phone to write to smart card in the updated stored piece in the back-up storage piece, and the constitutional diagram that CPU (central processing unit) is inscribed when different when the back-up storage piece writes data is shown in Fig. 5 a to Fig. 5 e.
After CPU (central processing unit) is calculated more the start-stop byte of new data in the updated stored piece, at first the footers of updated stored piece is write to the start byte of back-up storage piece.Shown in Fig. 5 a, in the present embodiment, updated stored piece 51 is the storage blocks that are encoded to 0x01, and CPU (central processing unit) at first writes the footers of back-up storage piece 51 to the start byte of back-up storage piece 52, just write this coding of 0x01.Simultaneously, with in the updated stored piece 51 more the data before the start byte of new data correspondence write in the back-up storage piece 52, the data storage that writes is behind the start byte of back-up storage piece.
Then, shown in Fig. 5 b, CPU (central processing unit) writes the more new data that mobile phone need write in the back-up storage piece 52.Certainly, more new data should write between its corresponding start-stop byte, after just more new data is closelyed follow the data that write in Fig. 5 a.
Then, CPU (central processing unit) with in the updated stored piece 51 the later data of termination byte of new data correspondence be copied in the back-up storage piece 52, this data storage that writes is behind new data more, shown in Fig. 5 c.At this moment, the data of back-up storage piece 52 promptly comprise the more new data that does not need data updated in the updated stored piece 51 and need write, and just use new data more to replace to need in the updated stored piece 51 data that are replaced.
At last, CPU (central processing unit) writes the footers of updated stored piece 51 to the termination byte of back-up storage piece 52, just the footers of back-up storage piece 52 is revised as the footers of updated stored piece, shown in Fig. 5 d.Simultaneously, CPU (central processing unit) is wiped the data of updated stored piece 51, and writes the footers of back-up storage piece 52 to the start-stop byte of updated stored piece 51, i.e. 0xFF is shown in Fig. 5 e.Like this, updated stored piece is originally promptly changed into back-up storage piece 52, stores the data after the renewal in the back-up storage piece originally, promptly becomes common storage block.During the CPU (central processing unit) reading of data, be to fetch data, be arranged in the storer that as long as the footers of all storage blocks is continuous, CPU (central processing unit) can continuous reading corresponding data even storage block is discontinuous according to the footers sequential read of each page storage block.
When CPU (central processing unit) is cut off the power supply in updated stored piece 51 writes the process of data, after re-powering, CPU (central processing unit) judges whether that the footers of start-stop bytes store of storage block is inconsistent, just judge whether under the state that carries out shown in Fig. 5 a, Fig. 5 b, Fig. 5 c, to cut off the power supply, in this way, then wipe the data of back-up storage piece 52, can guarantee that so the former data that are stored in updated stored piece 51 can not lose, guarantee the operate as normal of smart card.
Simultaneously, after CPU (central processing unit) powers on, the footers that also can judge whether two pages of storage blocks is identical, judge that just the state whether CPU (central processing unit) carries out shown in Fig. 5 d cuts off the power supply down, in this way, then will be wherein the data erase of one page storage block, and the footers of the storage block of obliterated data is revised as the footers of back-up storage piece, so promptly finish the Data Update operation.
Certainly, aforesaid operations is the operation when more new data only is written to one page storage block, when if more new data need be written to storage block more than two pages or two pages, CPU (central processing unit) selects more according to the length of new data more that new data is written in first index area or second index area, be provided with the Data Update sign of index area, more the destination address that writes of new data, write index informations such as upgrading data length.
Then, CPU (central processing unit) will not need data updated, be stored in and need data updated to be written in the back-up storage piece 52 in the index area in the updated stored piece 51, these operations write the more operation basically identical of new data with aforementioned to one page storage block, just carry out twice to the more operation of new data of one page storage block, do not repeat them here.
After CPU (central processing unit) is finished aforesaid operations, also need Data Update sign corresponding in the index area is removed, the expression Data Update is operated successfully.
Certainly, in the said process, the more new data that CPU (central processing unit) will write different storage blocks writes respectively in the different index slips, and writes corresponding index information in this index slip, comprise this more new data need write in which page storage block, this is the length etc. of new data more.CPU (central processing unit) is carried out a Data Update operation at every turn, and promptly each more new data with an index slip correspondence writes in the corresponding storage block, until all more new datas are write to corresponding storage block.
This shows, if CPU (central processing unit) writes in the data procedures when outage takes place to back-up storage piece 52, the more new data that is stored in the index area is not wiped free of, and the data in the updated stored piece 51 are not wiped free of yet, therefore, CPU (central processing unit) can with in the updated stored piece 51 more the data before the corresponding start byte of new data, more in new data and the updated stored piece 51 the more corresponding later data of byte that stop of new data be copied in the back-up storage piece 52.
Simultaneously, CPU (central processing unit) can be by judging that whether the Data Update sign is set in the index area is judged whether the preceding Data Update operation of outage is successful, all be provided with as if two updating mark in a certain index slip in the index area, Data Update is unsuccessful before the outage of expression smart card, need carry out Data Update operation once more, new data more is written in the corresponding storage block.
In addition, if two data updating mark of a certain index slip are inconsistent in the index area, represent that then smart card finished the operation that writes data or do not carried out Data Update operation, CPU (central processing unit) can ask mobile phone to send the more new data or the updating mark that directly clears data to smart card once more, and with the renewal datum target address of index area, upgrade index information such as data length and wipe.
This shows; in the present embodiment; as long as the data backup district of one page storage block size is set in storer; and the index area of one page storage block is set; and write footers every page of storage block start-stop byte; can effectively prevent smart card when outage loss of data, the storage space that data backup district and index area take shared storage space in power-off protection district in the existing smart card, thus the storage space wait of smart card memory is made full use of.
Certainly, the foregoing description only is one embodiment of the invention, during practical application, more change can also be arranged, storage block more than two pages or two pages for example is set as the data backup district in storer, and the back-up storage piece is not arranged on the last page storage block of storer; Perhaps, in the updated stored piece more the data before the corresponding start byte of new data, more in new data and the updated stored piece the more corresponding later data of byte that stop of new data not necessarily copy in the back-up storage piece successively, its duplication sequence can change, and is kept at the back-up storage piece successively as long as guarantee above-mentioned data.
Simultaneously, power-off protection method of the present invention is not limited to be applied on the telecom intelligent card such as SIM card, can also be applied in the multiple smart cards such as mass transit card, social security card loss of data when preventing the smart card outage.
At last; it is emphasized that; the invention is not restricted to above-mentioned embodiment, also should be included in protection scope of the present invention such as the subtle change such as change of change, storage block quantity and the storage block storage size of index slip quantity in the change of index area storage index information, the index area.