OpenWRT sysupgrade upgrade process

1. Introduction

OpenWRT provides a system upgrade mechanism, which can be done through the command line or through the web page. This article mainly introduces the common operations and usage of sysupgrade under OpenWRT and the sysupgrade upgrade process. Here it mainly introduces the processes involved in the entire upgrade process, including After understanding this macro-level upgrade process, we go deep into each process to view the corresponding functions. After such a combination, I believe everyone will have a deeper understanding of the OpenWRT upgrade process.

2. sysupgrade upgrade operation process

The main entrance to system upgrade is /sbin/sysupgrade, which is an upgrade script that comes with the OpenWrt system. The figure below shows the common usage of sysupgrade.

The upgrade operation process is as follows:

1) Upload the sysupgrade firmware package to the device (it can be stored in RAM, such as the /tmp directory, or in Flash, such as the /data directory, etc.)

# adb push sysupgrade.bin /tmp
# adb push sysupgrade.bin /data

2) Execute the sysupgrade command to start the upgrade

NOTICES: After starting the firmware upgrade, adb shell will exit because the sysupgrade upgrade program will close other processes except the upgrade process.

3) After the upgrade is completed, the module automatically restarts.

3. sysupgrade backup and restore mechanism

By default, the sysupgrade upgrade will back up the specified files and then automatically restore them after the upgrade is completed. These files that need to be saved are inopenwrt/target/linux///base-files/etc Set in /sysupgrade.conf.

3.1. Backup mechanism code process

1) After executing the sysupgrade /tmp/sysupgrade.bin command, the backup operation starts by default within the module. The default SAVE_CONFIG is 1. It will be set to 0 only when the -n option is configured. Therefore, the default is automatic execution. Backup operations.

openwrt/package/base-files/files/sbin/sysupgrade

2) When executing do_stage2, the second stage of the upgrade, the backup file will be copied to the /data directory.

openwrt/package/base-files/files/lib/upgrade/do_stage2
openwrt/target/linux/gem6xxx/XXX/base-files/lib/upgrade/platform.sh

3.2. Restore mechanism process

After the system starts, execute the 1restore script and automatically decompress the sysupgrade.tgz data previously backed up in the /data directory.

openwrt/target/linux/gem6xxx/base-files/etc/init.d/1restore

4. Analysis of sysupgrade upgrade process

The following figure describes the flow chart of process conversion during the entire sysupgrade upgrade process. It reflects the changes and key actions between processes. The following will start with each process and gradually analyze the functions of each process.

4.1, /bin/sh /sbin/sysupgrade

The sysupgrade process executed first is located at openwrt/package/base-files/files/sbin/sysupgrade. It is essentially a shell script. Its main functions are:

1) Parse parameters

2) Verify the legality of the firmware

3) Check whether the sysupgrade compressed package is normal, and then check whether the upgrade package is in the /tmp directory. If not, it will be copied to the /tmp directory.

4) Extract the vendor_info file from sysupgrade and then use it for ENDC verification

Here, the current ENDC information is obtained by sending the QKENDC command, and then compared with the information in the vendor_info file to verify.

5) Save the config configuration file and package it into a compressed package

The default SAVE_CONFIG is 1, which means the configuration file is saved by default.

6) ubus call system sysupgrade

A message will be sent to the procd process to continue the sysupgrade operation under system_methods.

This process is actually a child process of /bin/ash. Under normal circumstances, it will be forcibly deleted in the /lib/upgrade/stage2 process.

4.2, /sbin/procd

1) sysupgrade_exec_upgraded()

This function is located inopenwrt/build_dir/target-aarch64_cortex-a55 + neon-vfpv4_musl/procd-default/procd-2021-03-08-2cfc26f8/sysupgrade.c, main The function is that after executing execvp(“/sbin/upgraded”, argv), the procd process with PID=1 will be replaced by the /sbin/upgraded process.

4.3, /sbin/upgraded

The implementation of this process is located at openwrt/build_dir/target-aarch64_cortex-a55 + neon-vfpv4_musl/procd-default/procd-2021-03-08-2cfc26f8/upgraded/upgraded.c strong>, in the sysupgrade function, a child process will be created through fork/execvp to execute /lib/upgrade/stage2.

4.4, /lib/upgrade/stage2

The process is located at openwrt/target/linux/gem6xxx/evb6990_cpe_mt7990_emmc/base-files/lib/upgrade/stage2. It is actually a shell script. Its main functions are:

1) Set the LED flashing logic to indicate the upgrade status

2) Close other processes except this process and child processes

3) Switch to ramfs, where the relevant commands will be copied to the /tmp/root directory, and a temporary file system will be built in the /tmp/root directory.

4) After executing the exec /bin/busybox ash -c “/lib/upgrade/do_stage2” command, the current process will be replaced by the /lib/upgrade/do_stage2 process to continue execution.

4.5, /lib/upgrade/do_stage2

This process is located at openwrt/package/base-files/files/lib/upgrade/do_stage2. It is actually a shell script. This is the core part of the upgrade process. Its main functions are:

1) Execute upgrade action – platform_do_upgrade

2) If platform_copy_config is implemented, execute

3) Output the log of the upgrade completion

4) Restart the device to boot to the upgrade partition

At this point, we not only have a concept of the overall framework of the upgrade process as a whole. This flow chart is very critical. At the same time, after understanding the details inside, we will not have any big problems when dealing with upgrade-related issues. In addition, generally Generally speaking, the upgrade process will be strongly related to the system startup process. For example, the project I am currently working on is AB dual system. There is also a lot of related processing logic for startup after the upgrade and restart. This is strongly related to the platform, so I won’t go into details here. .

syntaxbug.com © 2021 All Rights Reserved.