Make an application upgrade file for embedded Linux


Embedded Linux systems generally put applications and file systems, kernels and resource files in different partitions. After mass production, kernels and file systems will not be upgraded, and applications may be upgraded more.

The application partition of some embedded systems is also readable and writable, which is more convenient. Package the files and extract them to the corresponding partition directory for replacement. Some application partitions are made read-only, so the application needs to be packaged into a flash image, downloaded to the device and updated to flash.

Either way, the corresponding application needs to be packaged, which can be a compressed package or a mirror file. When importing these files into the system for upgrading, we need to do some necessary verification on this file, such as version number, version flag, verification, etc; Version number is generally used to display reminders; Version mark is a mark made for different configurations of the equipment of this model (such as several serial ports, 4G version or Ethernet version, etc.); File verification is to check the validity of the file to avoid that the program cannot be started after upgrading due to file errors.

Creation and version information addition of upgrade files

So where is this information added? How? The following methods are common:

  • Directly name the upgrade file with model + version ID + version number, and then add a verification to the file; We have used this method before. This method is relatively simple, but it also has a disadvantage, that is, the file name is easy to be modified, or it is easy to be modified during transmission. Once the file name is modified, the information will be lost.
  • A special text file is added to store this information; Two files are not easy to save.
  • Write the information into the upgrade file; This method is relatively better. The information is directly written into the file and retrieved from the file during verification. It should be noted that the upgrade file must be smaller than the partition size.

The third method described below is to write information into the upgrade file. So how to write it into the upgrade file? Here is a simple and practical method.

The following production process is carried out under the virtual machine Ubuntu system. Take the production of squashfs format image as an example. Add the version information, flag and check code to the end of the file. You can directly use the echo command to add these information to the end of the file. Of course, the check code is added by a gadget written by c voice (an article on adding check code to the file was sent earlier).

The commands for adding version information and identification information are as follows:

echo "V1.0.1" >> app.img
echo "F-P4-4G" >> app.img #Note that "> >", instead of ">", must be used here. Using "> >" means adding, and using ">" means emptying the file and rewriting.

Test it

The following is a directory for making upgrade files I tested. The directory file structure is as follows:

app  bin

app is the directory of the application
bin upgrade file storage directory package creation script, which is used to generate image files

The script is as follows:


if [ $# -ne 2 ]
	echo "Usage: <version no> <flag info>"

VNum=$1  #Version information
Flag=$2  #Version identification information
installDir=./bin  #Upgrade file storage directory
rootDir="app"     #Application directory to package

Ver=`date '+%Y%m%d'`   #Take the time as part of the version information

echo "start make app img..."

echo $Ver> $rootDir/version 
echo $VNum >> $rootDir/version 

echo "app version: $Ver, version num: V$VNum"

echo "make squashfs img ..."
mksquashfs $rootDir $imgName -b 64K -comp xz

echo "package app img ok! "

echo "V$Ver $VNum" >> $imgName   #Add version information
echo "F$Flag" >> $imgName		#Add identification information
addCrc16 $imgName 			    #Check the file and add the check code to the upgrade file. addCrc16 is a program written in c language to add check to the file
rm $imgName					   #Delete temporary file
mv $imgName.crc16 $installDir/$imgName 

echo "mkfs ok: $installDir/$imgName"

Execute the script to create a file:

$./ 1.0.1 -P2-4G
start make rootfs...
app version: 20210924, version num: V1.0.1
make squashfs img ...
Parallel mksquashfs: Using 4 processors
Creating 4.0 filesystem on test_app-20210924-V1.0.1.img, block size 65536.
[=============================================================================/] 33/33 100%

Exportable Squashfs 4.0 filesystem, xz compressed, data block size 65536
	compressed data, compressed metadata, compressed fragments, compressed xattrs
	duplicates are removed
Filesystem size 621.31 Kbytes (0.61 Mbytes)
	32.52% of uncompressed filesystem size (1910.41 Kbytes)
Inode table size 302 bytes (0.29 Kbytes)
	64.81% of uncompressed inode table size (466 bytes)
Directory table size 155 bytes (0.15 Kbytes)
	100.00% of uncompressed directory table size (155 bytes)
Number of duplicate files found 0
Number of inodes 10
Number of files 4
Number of fragments 1
Number of symbolic links  2
Number of device nodes 0
Number of fifo nodes 0
Number of socket nodes 0
Number of directories 4
Number of ids (unique uids + gids) 1
Number of uids 1
	fens (1000)
Number of gids 1
	fens (1000)
make suqashfs img ok! 
file size = 639000
file crc16 = 0x001d
add  crc16 ok! 
mkfs ok: ./bin/test_app-20210924-V1.0.1.img

see file

After the file is generated, let's take a look at the information we added in the upgrade file.

You can use Uedit to view the file. Here, only the last part is intercepted to see the information we added:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zB9UGRTR-1632498443508)(1.png)]

As shown in the figure above, the three parts of information have been marked. When detecting the file information, you can read the information in the file for judgment and processing.

c language sample code for reading version information

    char readBuf[128] = {0};
    FILE *fp = fopen("app.img", "r");
	int ret = fseek(fp,-10L,SEEK_END);//Offset to the beginning of the version number.
	fread(buf,16,1,fp);               //Read version number
    ret = fseek(fp,-10L,SEEK_END);    //Offset to the location of the version identification information
    fread(buf,16,1,fp);               //Read version identification information

WeChat official account

Tags: Linux Ubuntu

Posted on Fri, 24 Sep 2021 21:20:10 -0400 by lenhewitt