[Embedded project from 0 to 1] Implementation of compiling, packaging and upgrading

Project compilation:

We take the nand partition, which is divided into bootloader, kernel, rootfs, and homefs as an example.
The rootfs is a collection of necessary file directory systems and tools for the Linux system. It is mounted under the root directory /. Homefs can be considered as our own application and our own dependent library, which is placed in homefs and placed in the /home directory.

Then the process of compiling and packaging is to package all the files required for the firmware to run into a package. During the upgrade, the upgrade program unpacks the package and saves the corresponding files in the corresponding partition.

To complete the compilation of the entire package, first compile the entire directory of bootloader, kernel and rootfs.
After generation, put it in the out directory and start to compile our own application.
1: Compile the public library of our own code first, generate .a, and put it in the corresponding directory;
2: Compile the applications of each module, and cp the generated executable files to out/home/app for later packaging ;
3: The dependent .a library has been compiled into the executable file at compile time, and the dependent .so library needs to be cp to out/home/lib;
4: The tools and files required by the firmware, such as sd format Tools, upgrade tools, encryption tools, prompt sound files, etc. cp to the corresponding directory.
After all the files required for running are placed in the out directory, at this time, bootloader and kernel are executable files, and rootfs and homefs are just directories and files, and we need to make them into file systems.
The jffs2 file system is used here, and the mkfs.jffs2 tool is used to make all the files in the rootfs and home directories into rootfs.jffs2 and home.jffs2 respectively.
Then use the dd tool to package the four independent files again, dd if=boot.bin of=bootimg bs=xx count=xx , dd kernel.img -> sysimg, dd rootfs.jffs2 -> sysimg seek=xxx, specify offset, package the kernel and rootfs together into sysimg. Likewise, package home.jffs2 as homeimg. This is the img package with three different parts.
Then package boot.bin kernel.img rootfs.jffs2 home.jffs2 into allimg using dd and offset. This package is the complete package.
In this way, there are four img, according to the part that needs to be upgraded, decide which img to use for upgrade.

In the home directory, use the 7za tool to encrypt and compress the home directory, and compress it into homeimg_m.7z Where does the
password come from?
Randomly generate a 32-bit long password from 0--z.

Execute md5sum on homeimg_m.7z, 
cat these files into one file
cat md5 password ver homeimg_m.7z > home1
dd if home1 of keyrsa bs=980 count=1 Put the first 777 bytes of the home1 file in keyrsa, as an encrypted key.
dd if home1 of home2 bs=980 skip=1 put the rest of home1 in home2 and

use keyrsa to generate the public key, enc_keyrsa, 
cat ver enc_keyrsa home2 > home_m

So far, the firmware package is completed, and the main firmware is bootimg/sysimg/home_m/allimg.

upgrade:

Upgrading allimg
from SD card and upgrading from SD card are carried out in the boot phase. After uboot detects the SD card, it reads the file name, reads the file into ram, calculates the crc checksum of the file, and sums the flash from the device. The corresponding crc read above is compared, and if it is consistent, the upgrade will not be performed.
If it is inconsistent, it means that it needs to be upgraded. At this time, according to the type of the package, if it is allimg, erase the entire area of ​​allimg, if it is bootimg, erase the boot area, and the others are the same, and then write the entire block of data into the corresponding flash. area, and then use the new crc as a new environment variable to write to a specific flash area. Reboot the device and it will run under the new firmware.

Network upgrade: The firmware for network upgrade is generally encrypted and compressed home_m.
trigger firmware to upgrade, the firmware obtains the latest firmware download url from the server, and the md5 of the firmware itself is used to verify the download integrity, send a request, and download home_m to the memory /tmp/downloadloadimg/home_m, the download process , you can determine the percentage of download according to the ratio of the file size from ls -l to the total file size. After the download is successful, execute md5sum, compare it with the md5 sent by the server, and the agreement is ok. At this time, mv /tmp/downloadloadimg/home_m to /home/home_m, access to this file, indicating that the download is 100% successful. reboo reboots the device.
At this time, the online upgrade has been half completed.

After the kernel is started, in the running init.sh script, we judge /home/home_m and start to decompress and upgrade.
dd if /home/home_m of /tmp/newver bs=22 count=1, that is, take out the first 22 bits of the firmware. According to the order of packaging, the firmware version number is in front. Compare with the current version number, and upgrade if it is different. .
Unpack home_m with the tool.
Create the /tmp/update directory, put home_m in the memory, cp the rsa decoding tool and the 7ZA tool, and start the solution.
dd if home_m of ver bs=22 count=1 version number
dd if home_m of home1 bs=22 skip=1 count=9999999 Except the version number
delete home_m because the memory may not be enough
dd if home1 of enc_key bs=1024 count=1 Take out the encrypted part
dd if home1 of home2 bs=1024 skip=1 count=9999999 The remaining part
rm home1

is decrypted using rsa_pub_dec enc_key dec_enc_key, if the size of the dec_enc_key file is 128, continue correctly, otherwise exit.
 cat dec_enc_key home2 > home3
 rm home2
  
 dd if home3 of home4 bs=66 skip=1 count=9999999 first 66 bits
 dd if home3 of md5 bs=33 count=1 take md5
 dd if home3 of pwd bs=33 skip=1 count= 1 Get the password
 dd if home4 of verin bs=22 count=1 Get the version
 
 dd if home4 of home_m.7z bs=22 skip=1 count=999999999
 
 Determine if there are any problems with the version numbers just obtained.
 Use the obtained decompression password to decompress home_m.7z with the 7za tool.
 
In the /home/ directory, sub-directory Compare the version, if the version is inconsistent, delete the files in the /home/xxx directory, and then copy the corresponding directory in the /tmp/update/home/ directory.
 
 Use md5 to compare the md5 in the /tmp directory and the /home directory, all of which are OK.
 cp the version file in the past, over, and finally the upgrade is successful. Much more complicated than the SD card.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325812987&siteId=291194637