Disclosure of Invention
In view of this, the main objective of the present invention is to provide a method for starting an embedded device, which improves the reliability of starting the embedded device.
Another object of the present invention is to provide an upgrade method for an embedded device, which can simplify the peripheral circuit of the memory in the hardware system of the device and simultaneously enable the embedded device to realize secure upgrade.
In order to achieve the purpose, the technical scheme of the invention is realized as follows:
a starting method of embedded equipment divides a flash memory used for storing mirror image data in the equipment into three logic partitions, namely a control information area, a first mirror image area and a second mirror image area; the control information area comprises a current mirror image area state mark, the attribute of the first mirror image area and the attribute of the second mirror image area; the first mirror image area and the second mirror image area both comprise mirror image data and a mirror image loading counter; the method further comprises the following steps:
a. checking whether the mirror image loading counter value of each mirror image area exceeds a preset threshold value, if so, setting the mirror image area as invalid, and then executing the step b; otherwise, directly executing the step b;
b. reading the state mark of the selected current mirror image area, judging whether the mirror image area is a primary mirror image area or not, and executing the step c if the mirror image area is a standby mirror image area; otherwise, executing step d;
c. judging whether the mirror image data of the standby mirror image area is available, if so, executing the step d, otherwise, exiting the starting process and waiting for upgrading the standby mirror image area;
d. and loading available mirror image data and initial starting codes related to hardware into a memory of the embedded device.
Wherein step d is preceded by: and adding 1 to the mirror image loading counter value of the mirror image area to be loaded.
The step d is further followed by: and clearing the mirror image loading counter value of the mirror image area.
The control information area is arranged at any position in the flash memory.
The capacity of the first mirror area is the same as that of the second mirror area.
The current mirror image area state mark is in a main state or a standby state.
The attributes of the first mirror image area and the second mirror image area at least comprise initial positions, lengths, validity and version numbers for storing the first mirror image area and the second mirror image area.
The mirror image data comprises software for enabling the embedded equipment to work, working space required by software operation and space occupied by user data.
The counting process of the mirror image loading counter is as follows: when the mirror image data is successfully loaded into the memory, the count value is cleared; otherwise, if the loading fails, the mirror loading count value is automatically increased by 1.
A method for upgrading software of embedded equipment divides a flash memory used for storing mirror image data in the equipment into three logic partitions, namely a control information area, a first mirror image area and a second mirror image area; the control information area comprises a current mirror image area state mark, the attribute of the first mirror image area and the attribute of the second mirror image area; the first mirror image area and the second mirror image area both comprise mirror image data and a mirror image loading counter; the method further comprises the following steps:
A. selecting a mirror image area to be upgraded and setting the mirror image area as invalid;
B. writing an upgrade file into the mirror image area;
C. and after the upgrade file is completely written into the mirror image area, setting the mirror image area to be effective.
Wherein, further include after step B:
b1, judging whether the written upgrade file is complete, if not, returning to the step B; otherwise, executing step C.
Step B1, the step of determining whether the written upgrade file is complete specifically includes: comparing whether the byte number of the source upgrading file is consistent with that of the mirror image data file, and if so, writing the upgrading file completely; otherwise, the upgrade file is incomplete.
The control information area is arranged at any position in the flash memory.
The capacity of the first mirror area is the same as that of the second mirror area.
The current mirror image area state mark is in a main state or a standby state.
The attributes of the first mirror image area and the second mirror image area at least comprise initial positions, lengths, validity and version numbers for storing the first mirror image area and the second mirror image area.
The mirror image data comprises software for enabling the embedded equipment to work, working space required by software operation and space occupied by user data.
The starting method and the upgrading method of the embedded equipment provided by the invention have the following advantages that:
1) the invention adopts the method of logic partition of the single-chip flash memory, thereby simplifying the complexity of hardware circuit design.
2) The invention adopts the method of logically partitioning the flash memory, and the capacity of the partitioned first mirror image area and the capacity of the partitioned second mirror image area are completely the same, thereby providing objective conditions for realizing mutual backup and safe upgrade of the two mirror image areas.
3) The invention can realize the work which can be finished by two different memories in the past only by adopting one flash memory chip, thereby saving the cost of the embedded equipment.
4) The upgrading method of the invention can optionally select the first mirror image area or the second mirror image area for upgrading, breaks through the convention that the prior embedded equipment can not be upgraded in a safe mode, and enhances the usability of the embedded equipment.
Detailed Description
The method of the present invention will be described in further detail below with reference to the accompanying drawings and embodiments of the invention.
Fig. 1 is a schematic diagram of logical partitioning of a storage space of a flash memory according to an embodiment of the present invention, and as shown in fig. 1, the storage space of the flash memory is logically partitioned into: the control information area, the first mirror area and the second mirror area; the first mirror area and the second mirror area are identical in size and structure, and the difference between the first mirror area and the second mirror area is only that the first mirror area and the second mirror area are located at different positions in the flash memory.
The following factors need to be considered for logically partitioning the storage space of the flash memory and determining the size of the storage space occupied by each partition:
the sum of the number of bytes of software required by the operation of the current embedded device, the number of bytes of a working space required by the operation of the software and the number of bytes of a space occupied by user data is used as a storage space required by the image file, namely the size of the image area. Estimating the storage capacity of the flash memory required by the embedded equipment according to the size of the mirror image area, and then adding the capacity required by a small amount of control information to jointly determine the flash memory chips with large capacity to be selected. At present, a single-chip flash memory chip with 1M, 2M, 4M, 8M, 16M, 32M, 64M, 128M, 256M bytes and the like and even with larger capacity exists, and as the larger the storage capacity of the flash memory chip is, the higher the price is, the smaller the capacity of the selected chip is, the working requirement of embedded equipment cannot be met; the selected chip has too large capacity, which will increase unnecessary cost and cause resource waste, so the capacity and cost need to be considered comprehensively.
Here, the estimating the storage capacity of the flash memory required by the embedded device according to the size of the mirror area specifically includes: the sum of twice the memory capacity of the mirror area, plus the capacity of control information of size not more than 1 megabyte, is optimized for chips smaller than but close to the existing capacity. For example, if the size of the image file is about 14M, a single flash memory chip of 32M should be selected as the memory of the embedded device.
In application, the storage space of the 32M monolithic flash memory is divided into three logical partitions, and a typical division is as follows: allocating a space of 1M for the control information area, referred to as a control information area; the remaining storage space is then divided equally into two parts: the first mirror area allocation is 15.5M and the second mirror area allocation is 15.5M. The position of the control information area can be positioned between the first mirror area and the second mirror area, and can also be arranged at one end of the internal storage space of the flash memory.
Fig. 2 is a schematic diagram of an internal logic structure of the control information area in fig. 1 according to an embodiment of the present invention, and as shown in fig. 2, the control information area includes: the current mirror image area state mark, the attribute of the first mirror image area and the attribute of the second mirror image area. Wherein,
and the current mirror image area state mark is used for indicating whether the first mirror image area and the second mirror image area in the current flash memory are in the primary state or the standby state. If the first mirror image area is indicated to be in the primary state and the mirror image area is effective, the content of the first mirror image area is called into the memory of the equipment to run when the equipment is started, and the second mirror image area is used as a backup; otherwise, the content of the second mirror image area is called into the memory of the equipment to run, and the first mirror image area is used as a backup.
And the attribute of the first mirror area is used for storing information such as the starting position, the length, the validity, the version number and the like of the first mirror area.
And the attribute of the second mirror image area is used for storing information such as the starting position, the length, the validity, the version number and the like of the second mirror image area.
Fig. 3 is a schematic diagram illustrating an internal logic structure of the first mirror area or the second mirror area in fig. 1 according to an embodiment of the present invention, where as shown in fig. 3, the first mirror area or the second mirror area includes mirror data and a mirror load counter. Wherein,
and mirroring data, which comprises software for enabling the embedded equipment to work, working space required by software operation and space occupied by user data.
The mirror image loading counter is used for counting the loading times when the mirror image data of the first mirror image area or the second mirror image area is loaded into the memory, and when the mirror image data is loaded successfully, the count value is cleared; and if the loading fails, automatically adding 1 to the mirror loading count value.
And if the failure times of the mirror image data loading exceed a certain preset threshold value, setting the mirror image area to which the mirror image data belongs as invalid, and then selecting to read the mirror image data loading from another mirror image area. At this time, the mirror image area with failed loading can be updated or selectively upgraded to recover the complete mirror image data, so that at least one mirror image area is ensured to be available, and the backup function of the mirror image data of the equipment is realized.
For example: in the equipment upgrading process, the mirror image data of the first mirror image area is updated, the first mirror image area needs to be set to be invalid firstly, then the first mirror image area is upgraded, and after the upgrading is finished, the first mirror image area is reset to be valid. Therefore, even if the upgrading fails due to accidents in the upgrading process, the second mirror image area can be ensured to be a safe and complete backup, so that the disastrous consequences are avoided.
Fig. 4 is a schematic diagram of a starting process of an embedded device in the embodiment of the present invention, as shown in fig. 4, the process includes the following steps:
step 401: checking whether the mirror loading count value of each mirror area exceeds a preset threshold, and if so, executing step 402; otherwise, step 403 is performed.
Step 402: the mirror area is set to invalid and then step 403 is performed.
Here, the setting invalidation specifically is: and setting the value of one item of validity in the mirror area attribute to be in an invalid state.
Step 403: and reading the state mark of the currently selected mirror area.
Here, the status flag of the current mirror area is obtained from the control information area, and there are two kinds of statuses of the mirror area: a primary state and a standby state.
Step 404: judging whether the mirror area is in a main state, if so, executing step 405; otherwise, step 407 is executed.
Here, the determination of whether the mirror area is active or standby is performed according to the current mirror area state indicator. When the embedded device is started, the mirror area used in the last normal operation can be automatically saved as the main state. Typically, the mirror data of the mirror region of the active state is complete and available by default.
At this time, the mirror load counter value of the primary mirror area needs to be increased by 1.
Step 405: judging whether the standby mirror image data is valid, if not, executing step 406; otherwise, step 407 is executed.
Here, the judging whether the spare mirror image data is valid is realized by verifying whether the mirror image data of the mirror image area is complete, and if the mirror image data is complete, the mirror image area is determined to be available; otherwise, the system is judged to be unavailable.
Step 406: and judging that the loading process fails, ending the starting process, and waiting for software upgrading of the mirror image area.
Here, the mirror load counter value of the current mirror area to be loaded needs to be increased by 1.
And the current mirror image data is unavailable, so that the system enters a paralyzed state, and at the moment, the embedded equipment can be selected to enter an upgrading mode to upgrade the software of the equipment.
Step 407: and loading the effective mirror image data into the equipment memory, calling an application program after the system is started, and executing a corresponding function.
The flash memory contains a piece of initial boot code associated with the hardware, which is usually placed at the beginning of the address of the internal memory space of the flash memory. When the image data is loaded to the device memory, the initial boot code is loaded into the memory along with the image data.
And after the system is started, clearing the mirror image loading counter value of the mirror image area, wherein the equipment can start to work normally at the moment.
The following is a detailed description of the software upgrade process for the embedded device described in step 406 of fig. 4.
Fig. 5 is a schematic diagram of a process of upgrading software of an embedded device in an embodiment of the present invention, and as shown in fig. 5, the method includes:
step 501: and selecting a mirror area to be upgraded and setting the mirror area as invalid.
Step 502: and writing the upgrade file into a corresponding mirror image area of the flash memory.
Step 503: judging whether the written upgrade file is complete, if not, returning to the step 502; otherwise, step 504 is performed.
Judging whether the written upgrade file is complete or not, comparing whether the byte number of the source upgrade file is consistent with that of the mirror image data file or not, and if so, considering that the written upgrade file is complete; otherwise, the written upgrading file is determined to be incomplete and needs to be rewritten.
Step 504: and setting the mirror image area to be effective, and ending the upgrading process.
When the upgrade process is interrupted unexpectedly, which results in the failure of the upgrade, the steps 501 to 504 may be repeated to upgrade the selected mirror image area again.
The above description is only a preferred embodiment of the present invention, and is not intended to limit the scope of the present invention.