Background
At present, with the development of power communication, terminal devices such as a concentrator and a collection terminal are increasingly used. In order to ensure the security of the device, a Terminal Embedded security control Module (TESAM) is usually installed in a Terminal device such as a concentrator and an acquisition Terminal, and is used as a security authentication Module of the device. When the terminal equipment interacts with the application platform through the TESAM, various counters can be used, and the reading and writing speed of the counters can seriously affect the processing efficiency of the concentrator, the acquisition terminal and other terminal equipment.
The counter generally occupies 4 bytes of storage in a Flash Memory (Flash EEPROM Memory for short). In the prior art, when a counter is read, the counter is directly read to a FLASH address corresponding to the counter; when updating the counter, according to the existing power-down protection mode, data needs to be backed up to the write protection area, data marks of the write protection area are marked in the write protection control area, data is written in a written actual address, and the write protection control area is erased for 4 times.
Based on this, the inventor of the present invention finds that, in the existing counter read-write method in the prior art, multiple times of erasing and writing are required when the counter is updated, so that the execution time of the instruction is increased, and the processing efficiency of the terminal devices such as the concentrator and the acquisition terminal is reduced.
The information disclosed in this background section is only for enhancement of understanding of the general background of the invention and should not be taken as an acknowledgement or any form of suggestion that this information forms the prior art already known to a person skilled in the art.
Disclosure of Invention
In view of the above, the technical problem to be solved by the present invention is how to provide a method and an apparatus for reading and writing a counter, so as to solve the problems of long instruction execution time and low reading and writing efficiency of the counter in the prior art.
In order to solve the above technical problem, the present invention provides, in a first aspect, a method for reading and writing a counter, including: receiving an operation request, wherein the operation request comprises an address of a first storage page, and the first storage page is a first page in a plurality of storage pages corresponding to the counter in a FLASH chip; acquiring counter information stored in the first storage page and the second storage page, wherein the counter information includes: the second storage page is the next storage page of the first storage page corresponding to the counter in the FLASH chip; determining an effective storage page according to the counter information stored in the first storage page and the second storage page; and executing operation processing corresponding to the operation request according to the operation request and the counter information in the effective storage page.
In a possible implementation manner, the determining a valid storage page according to the counter information stored in the first storage page and the second storage page includes: respectively judging whether the first storage page and the second storage page are empty or not according to the counter information stored in the first storage page and the second storage page; and if the first storage page and the second storage page are both empty, the first storage page is a valid storage page.
In a possible implementation manner, after respectively determining whether the first storage page and the second storage page are empty according to the counter information stored in the first storage page and the second storage page, the method further includes: if at least one of the first storage page and the second storage page is not empty, respectively judging whether the check values of the first storage page and the second storage page are valid; if the check value of only one storage page is valid, the storage page with the valid check value is a valid storage page.
In a possible implementation manner, the respectively determining whether the check values of the first storage page and the second storage page are valid includes: respectively judging whether the check values of the counter information stored in the first storage page and the second storage page are correct or not; if at least one of the check values of the counter information stored in the first storage page is correct, the check value of the first storage page is valid; and if at least one of the check values of the counter information stored in the second storage page is correct, the check value of the second storage page is valid.
In a possible implementation manner, after respectively determining whether the check values of the first storage page and the second storage page are valid, the method further includes: if the check values of the first storage page and the second storage page are both valid, respectively determining the maximum value of the serial number of the counter in the first storage page and the second storage page according to the counter information stored in the first storage page and the second storage page; and comparing the maximum value of the counter serial numbers in the first storage page with the maximum value of the counter serial numbers in the second storage page, and taking the storage page stored with the maximum value of the counter serial numbers as a valid storage page.
In a possible implementation manner, when the operation request is a read operation request, before executing operation processing corresponding to the operation request according to the operation request and counter information in the valid storage page, the method further includes: respectively determining the maximum value of the serial number of the counter in the first storage page and the second storage page according to the counter information stored in the first storage page and the second storage page; respectively determining offset addresses of the maximum value of the counter sequence numbers in the first storage page and the second storage page relative to the first address of the first storage page; the executing, according to the operation request and the counter information in the valid storage page, operation processing corresponding to the operation request includes: and reading the value of the counter according to the operation request and the offset address of the maximum value of the serial number of the counter in the effective storage page relative to the first address of the first storage page.
In a possible implementation manner, when the operation request is a write operation request, the operation request further includes counter information to be written; before the executing the operation processing corresponding to the operation request according to the operation request and the counter information in the valid storage page, the method further includes: respectively acquiring offset addresses of the next available address of the first storage page and the second storage page relative to the first address of the first storage page; the executing, according to the operation request and the counter information in the valid storage page, operation processing corresponding to the operation request includes: if the check value of only one storage page is valid, erasing the storage page with the invalid check value; and writing the counter information into the next available address of the effective storage page according to the counter information to be written in the operation request.
In a possible implementation manner, the executing, according to the operation request and the counter information in the valid memory page, operation processing corresponding to the operation request further includes: and if the check values of the first storage page and the second storage page are valid, erasing the storage page stored with the maximum value of the small counter serial number.
In order to solve the above technical problem, the present invention provides, in a second aspect, a reading/writing apparatus for a counter, including: a receiving module, configured to receive an operation request, where the operation request includes an address of a first storage page, where the first storage page is a first page of a plurality of storage pages corresponding to the counter in a FLASH chip; an obtaining module, configured to obtain counter information stored in the first storage page and the second storage page, where the counter information includes: the second storage page is the next storage page of the first storage page corresponding to the counter in the FLASH chip; the calculation module is used for determining an effective storage page according to the counter information stored in the first storage page and the second storage page; and the processing module is used for executing operation processing corresponding to the operation request according to the operation request and the counter information in the effective storage page.
The reading and writing method and device for the counter provided by the invention can be used for acquiring the counter information stored in the first storage page and the second storage page, wherein the counter information comprises the following steps: a serial number, a check value, and a counter value; determining an effective storage page according to the counter information stored in the first storage page and the second storage page; and executing operation processing corresponding to the operation request according to the operation request and the counter information in the effective storage page, so that power-off protection of the counter information can be realized, multiple operations required by backup operation are reduced, and the reading and writing speed of the counter is improved.
Other features and aspects of the present invention will become apparent from the following detailed description of exemplary embodiments, which proceeds with reference to the accompanying drawings.
Detailed Description
The following detailed description of the present invention is provided in conjunction with the accompanying drawings, but it should be understood that the scope of the present invention is not limited to the specific embodiments.
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention. Throughout the specification and claims, unless explicitly stated otherwise, the word "comprise", or variations such as "comprises" or "comprising", will be understood to imply the inclusion of a stated element or component but not the exclusion of any other element or component.
The word "exemplary" is used exclusively herein to mean "serving as an example, embodiment, or illustration. Any embodiment described herein as "exemplary" is not necessarily to be construed as preferred or advantageous over other embodiments.
Furthermore, in the following detailed description, numerous specific details are set forth in order to provide a better understanding of the present invention. It will be understood by those skilled in the art that the present invention may be practiced without some of these specific details. In some instances, methods, means, elements well known to those skilled in the art have not been described in detail so as not to obscure the present invention.
Example 1
The read and write operations of the FLASH chip are generally performed in blocks or pages, and even if the write data only has 1 byte, the entire block or page data including the byte needs to be erased and written during actual write. The read-write characteristic of the FLASH chip can greatly increase the data write-in time, and has great influence on the execution time of some instructions which need to frequently update FLASH. When the counter is updated according to the conventional power-down protection scheme, in the case of requiring updating of 4 bytes of data, the operations of erasing and writing 4 pages of data are actually performed, and the time of these operations becomes a key factor of the length of the instruction execution time.
FLASH chips are classified into two categories based on their differences in their erase and write performance. The first type of chip is to erase the rewrite as long as the data at a certain position is modified; and in the second type of chips, the bits of the support data are written from 1 to 0, and the bits of the support data are written from 0 to 1. The proposal is optimized for the second type of chip.
The method has the advantages that the correctness and the validity of the counter are guaranteed, meanwhile, the erasing times are reduced, a plurality of complete pages are used for storing one counter, and the legality of the counter is guaranteed by increasing the storage space and the storage content.
Fig. 1 is a flowchart illustrating a method for reading and writing a counter according to an embodiment of the present invention, as shown in fig. 1, the method includes the following steps.
Step S1, receiving an operation request, where the operation request includes an address of a first storage page, where the first storage page is a first page of a plurality of storage pages corresponding to the counter in the FLASH chip.
Step S2, obtaining counter information stored in the first storage page and the second storage page, where the counter information includes: the second storage page is the next storage page of the first storage page corresponding to the counter in the FLASH chip.
Specifically, the memory page is a memory unit divided by the FLASH chip for the convenience of management, and a plurality of pieces of counter information are stored in one memory page. When reading and writing, a page address and an offset address in a page need to be specified at the same time. The storage space corresponding to the counter in this embodiment may be a plurality of pages, and in this embodiment, the first storage page and the second storage page are specifically described as an example, but not limited to two pages.
The counter information includes: a sequence number, a check value to verify the validity of the counter, the counter value, and may also include reserved bytes. The structure of the counter information can be seen in table 1:
| element(s)
|
Length of
|
Description of the invention
|
| num
|
1byte
|
Counter serial number
|
| counter
|
4byte
|
Actual valid counter value
|
| crc
|
1byte
|
CRC checksum of num and counter
|
| rf
|
2byte
|
Reserved bytes for alignment |
TABLE 1
When the chip initialization instruction is executed, a plurality of memory pages corresponding to the counter need to be initialized. The storage space of a plurality of storage pages corresponding to the counter is initialized to 0xFF, and the first piece of counter information of the first storage page is initialized to 0100000000010000 to ensure that the page is valid.
Step S3, determining a valid storage page according to the counter information stored in the first storage page and the second storage page.
The counter information stored in the valid storage page is correct counter information.
Step S4, executing an operation process corresponding to the operation request according to the operation request and the counter information in the valid memory page.
Specifically, the value of the counter is read or updated according to a read operation request or a write operation request.
Thereby, by acquiring counter information stored in the first storage page and the second storage page, the counter information includes: a serial number, a check value, and a counter value; determining an effective storage page according to the counter information stored in the first storage page and the second storage page; and executing operation processing corresponding to the operation request according to the operation request and the counter information in the effective storage page, so that power-off protection of the counter information can be realized, multiple operations required by backup operation are reduced, and the reading and writing speed of the counter is improved.
Example 2
Fig. 2 is a flowchart of a read-write method of a counter according to another embodiment of the present invention, please refer to fig. 2, and this embodiment further limits step S3 based on the first embodiment.
Step S3 in this embodiment may include:
s31, respectively determining whether the first storage page and the second storage page are empty according to the counter information stored in the first storage page and the second storage page.
Specifically, when the FLASH chip is shipped from the factory, the storage space in the storage page is initialized to 0xFF, that is, whether the storage page is empty can be determined by determining whether the data of the entire page is 0 xFF. And obtaining the counter information stored in the storage page, wherein if the counter information stored in the current storage page is all 0xFF, the current storage page is empty.
S32, if the first storage page and the second storage page are both empty, the first storage page is a valid storage page.
S33, if at least one of the first storage page and the second storage page is not empty, determining whether the check values of the first storage page and the second storage page are valid.
Specifically, it is judged by the check value in the counter information.
The method can comprise the following steps:
s331, respectively determining whether check values of counter information stored in the first storage page and the second storage page are correct;
the method specifically comprises the following steps: when the operation is executed, the current counter value is checked and calculated, and if the check value calculated according to the counter value is the same as the check value of the counter information, the check value of the counter information is correct.
S332, if at least one of the check values of the counter information stored in the first storage page is correct, the check value of the first storage page is valid.
S333, if at least one of the check values of the counter information stored in the second storage page is correct, the check value of the second storage page is valid.
Thus, the validity of the counter value is guaranteed by whether the check value of the memory page is correct. The problem that the counter value is inaccurate when updating or writing is not finished in the power failure process is solved.
S34, if the check value of only one storage page is valid, the storage page with the valid check value is a valid storage page.
Specifically, when at least one of the first storage page and the second storage page is not empty and only one storage page has a correct check value, the first storage page is not empty and valid, and the second storage page is empty or invalid; the second memory page is not empty and valid, the first memory page is empty or invalid; neither the first memory page nor the second memory page is empty, but there is only one valid case.
S35, if the check values of the first storage page and the second storage page are both valid, determining maximum values of the counter numbers in the first storage page and the second storage page according to the counter information stored in the first storage page and the second storage page, respectively.
Specifically, the maximum values of the counter serial numbers in the first storage page and the second storage page are respectively determined, all the counter serial numbers in the first storage page are obtained, the counter serial numbers in the storage pages are compared, and the maximum value is selected as the maximum value of the counter serial numbers of the first storage page; and acquiring all the counter serial numbers in the second storage page, comparing the counter serial numbers in the storage page, and selecting the maximum value as the maximum value of the counter serial numbers of the second storage page. It should be noted that, when updating the counter value, the system automatically allocates the counter number, and the largest counter number is the latest stored counter value.
S36, comparing the maximum value of the counter numbers in the first memory page with the maximum value of the counter numbers in the second memory page, and setting the memory page storing the maximum value of the counter numbers as a valid memory page.
Specifically, if the maximum value of the counter serial number in the first storage page is greater than the maximum value of the counter serial number in the second storage page, the first storage page is a valid storage page; and if the maximum value of the counter serial number in the first storage page is smaller than the maximum value of the counter serial number in the second storage page, the second storage page is a valid storage page.
Therefore, in the embodiment, by judging whether the first storage page and the second storage page are empty or not, and determining the effective storage page by judging the check value of the storage page when the first storage page and the second storage page are not empty, the power-off protection of the counter information can be realized, the multiple operations required by the backup operation are reduced, and the read-write speed of the counter is further improved.
Example 3
Fig. 3 is a flowchart of a method for reading and writing a counter according to a next embodiment of the present invention, please refer to fig. 3, in which an operation request is a read operation request, and the method is further defined based on the above embodiment.
Before step S4, the method further includes:
step S51, determining maximum values of the counter numbers in the first storage page and the second storage page respectively according to the counter information stored in the first storage page and the second storage page;
step S52, determining offset addresses of the maximum value of the counter numbers in the first storage page and the second storage page with respect to the first address of the first storage page, respectively;
step S4 includes:
step S41, reading the value of the counter according to the operation request and the offset address of the maximum value of the counter sequence number in the valid storage page relative to the first address of the first storage page.
The method may further include erasing the memory page in which the largest value of the small counter numbers is stored if the check values of the first memory page and the second memory page are both correct.
It should be noted that step S51 in this embodiment has the same function as step S35 in the second embodiment in determining the maximum value of the counter numbers in the first storage page and the second storage page, respectively, based on the counter information stored in the first storage page and the second storage page.
Thereby, the offset addresses of the maximum value of the counter sequence numbers in the first storage page and the second storage page relative to the first address of the first storage page are respectively determined; reading the value of the counter according to the operation request and the offset address of the maximum value of the serial number of the counter in the effective storage page relative to the first address of the first storage page, so that the power-off protection of the counter information can be realized, the multiple operations required by the backup operation are reduced, and the reading and writing speed of the counter is further improved.
Example 4
Fig. 4 is a flowchart of a method for reading from and writing to a counter according to yet another embodiment of the present invention, please refer to fig. 4, in which in the present embodiment, when the operation request is a write operation request, further limitations are performed on the basis of the above embodiments. The operation request also comprises counter information to be written;
before step S4, the method further includes:
step S53, respectively obtaining offset addresses of the next available address of the first storage page and the second storage page relative to the first address of the first storage page.
Step S4 includes:
in step S42, if the check value of only one storage page is valid, the storage page with the invalid check value is erased.
Step S43, writing the counter information in the next available address of the valid storage page according to the counter information to be written in the operation request.
In step S44, if the check values of the first storage page and the second storage page are both valid, the storage page storing the largest value of the small counter numbers is erased.
Specifically, when the first storage page space is used up, the counter information needing to be updated is saved to the second page, and then the first page is subjected to whole-page erasing.
Therefore, when only the check value of one storage page is valid, the storage page with the invalid check value is erased, and the counter information is written in the next available address of the valid storage page according to the counter information to be written in the operation request; if the check values of the first storage page and the second storage page are both valid, the storage page storing the maximum value of the small counter serial number is erased, so that when the counter is updated, only the counter information, namely 8 bytes, is updated, and only one erasing operation needs to be performed when a page is completely written, thereby reducing the updating time of the counter and further improving the instruction execution efficiency.
Example 5
Fig. 5 is a schematic structural diagram of a read/write apparatus for a counter according to another embodiment of the present invention, and as shown in fig. 5, the apparatus includes a receiving module 1, an obtaining module 2, a calculating module 3, and a processing module 4.
The receiving module 1 is configured to receive an operation request, where the operation request includes a read operation request or a write operation request, and an address of a first storage page, where the first storage page is a first page of a plurality of storage pages corresponding to the counter in the FLASH chip.
An obtaining module 2, configured to obtain counter information stored in the first storage page and the second storage page, where the counter information includes: the second storage page is the next storage page of the first storage page corresponding to the counter in the FLASH chip.
And the calculating module 3 is used for determining an effective storage page according to the counter information stored in the first storage page and the second storage page.
And the processing module 4 is configured to execute operation processing corresponding to the operation request according to the operation request and the counter information in the valid storage page.
Thereby, by acquiring counter information stored in the first storage page and the second storage page, the counter information includes: a serial number, a check value, and a counter value; determining an effective storage page according to the counter information stored in the first storage page and the second storage page; and executing operation processing corresponding to the operation request according to the operation request and the counter information in the effective storage page, so that power-off protection of the counter information can be realized, multiple operations required by backup operation are reduced, and the reading and writing speed of the counter is improved.
The foregoing descriptions of specific exemplary embodiments of the present invention have been presented for purposes of illustration and description. It is not intended to limit the invention to the precise form disclosed, and obviously many modifications and variations are possible in light of the above teaching. The exemplary embodiments were chosen and described in order to explain certain principles of the invention and its practical application to enable one skilled in the art to make and use various exemplary embodiments of the invention and various alternatives and modifications as are suited to the particular use contemplated. It is intended that the scope of the invention be defined by the claims and their equivalents.
The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.