A method for remote firmware upgrade of PLC

By optimizing the bsdiff algorithm, matchlen function, search function, Blake2s algorithm, and caching technology, the problems of long differential calculation time and high resource consumption in PLC firmware upgrades are solved, enabling fast and safe remote PLC firmware upgrades.

CN117032771BActive Publication Date: 2026-07-17SHANDONG COMP SCI CENTNAT SUPERCOMP CENT IN JINAN +1

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SHANDONG COMP SCI CENTNAT SUPERCOMP CENT IN JINAN
Filing Date
2023-08-16
Publication Date
2026-07-17

AI Technical Summary

Technical Problem

In existing technologies, the differential calculation time of PLC firmware is relatively long and consumes a lot of PLC resources, resulting in low upgrade efficiency.

Method used

An optimized bsdiff algorithm is used for differential calculation. Combined with optimization strategies for the matchlen and search functions, the Blake2s algorithm is used for data block verification. Caching technology is introduced to optimize the bspatch algorithm. Data blocks are upgraded through dual-connection transmission, and the block size is adaptively set according to network bandwidth.

Benefits of technology

It improves the efficiency of remote PLC firmware upgrades, reduces transmission time, enhances calculation speed and security, and reduces memory usage.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117032771B_ABST
    Figure CN117032771B_ABST
Patent Text Reader

Abstract

This invention belongs to the field of IoT device program updates, and more specifically, relates to an efficient method for remote PLC firmware upgrades. The method includes: obtaining a differential file through differential calculation; splitting the differential file into upgrade data blocks and transmitting them to the PLC; the PLC receiving the upgrade data blocks transmitted from the cloud server and performing data integrity verification on the PLC; synthesizing all upgrade data blocks according to identifiable numbers, and comparing the hash value of the synthesized differential file with the differential file in the index table on the cloud server; merging the synthesized differential file with the old version file using an optimized bspatch algorithm to generate a new version file; restarting the PLC, and the upgrade is complete. This invention solves the problems of long differential calculation times, excessive PLC resource usage, and low upgrade efficiency in existing technologies.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of IoT device program updates, and more specifically, relates to a method for remotely upgrading PLC firmware. Background Technology

[0002] PLC is an abbreviation for Programmable Logic Controller, a digital electronic device used in automated control systems. It is commonly used in industrial control systems, such as production line control, robot control, traffic light control, and power control. At its core is a programmable controller that can control the operation of input and output devices according to user-written programs.

[0003] The advantages of PLCs include high reliability, flexibility, and programmability. Programs can be easily modified and rewritten as needed to achieve different control operations. PLCs also feature fast response and high precision, enabling real-time monitoring and control of the production process, thus improving production efficiency and product quality.

[0004] PLCs are widely used, with equipment distributed throughout the country. When problems arise and PLC upgrades are needed, traditional offline maintenance consumes a significant amount of manpower and resources. Furthermore, PLCs are often used in complex factory environments, further increasing the burden on maintenance personnel. Remote PLC upgrades offer a convenient solution to these problems.

[0005] Chinese patent document CN115712447A discloses a method and system for incremental upgrades of smart meters, including the following steps: dividing the entire software firmware of the smart meter into multiple independent regions; during the upgrade, only the region to be upgraded is selected based on the modified content to create an upgrade file; the host computer uses a differential algorithm to generate difference information between the old and new firmware versions, compresses the difference information into a patch file, and adds header information and a checksum to the patch file header to generate an incremental upgrade file; after receiving the incremental upgrade file, the smart meter verifies the header information and checksum of the incremental upgrade file, decompresses and restores the patch file, and then uses a synthesis algorithm, the difference information in the patch file, and the old version file of the smart meter to restore the new version file; finally, according to the partition identifier, the old version file of the corresponding partition is replaced with the new version file to complete the upgrade.

[0006] Remote upgrades use a differential upgrade method, which has significant advantages over full package upgrades, especially when there is a lot of content to update. This invention optimizes the traditional differential upgrade method, which has a long calculation time and uses a lot of PLC resources, resulting in low upgrade efficiency. Summary of the Invention

[0007] The present invention aims to overcome at least one of the defects of the prior art and provide a remote firmware upgrade method for PLCs to solve the problems of long time and large amount of PLC resources used in traditional differential calculations.

[0008] The detailed technical solution of this invention is as follows:

[0009] To achieve the above objectives, the present invention provides a method for remote firmware upgrade of a PLC, comprising the following steps:

[0010] S1. After the PLC is powered on, the old version file identifier in the storage area is automatically calculated by differential calculation with the new version file identifier on the cloud server to obtain the differential file.

[0011] S1 specifically includes:

[0012] After the PLC is powered on, the old version file identifier in the storage area is automatically compared with the new version file identifier on the cloud server.

[0013] If the comparison results are inconsistent, the upgrade flag in the PLC is set, and the cloud server creates a differential upgrade task. The old version file package obtained based on the PLC file identifier will be differentially calculated with the new version file on the cloud server to obtain the differential file.

[0014] If the comparison results are consistent, no upgrade is needed.

[0015] Furthermore, the difference calculation is performed using an optimized bsdiff algorithm, which includes the matchlen function and the search function;

[0016] The matching function processes the following steps:

[0017] 1) Function preprocessing;

[0018] 11) An array named bmbc was created to store the Bad Character rule table, with an array size of UCHAR_MAX+1;

[0019] 12) Initialize all elements of the bmbc array to newsize, which means that by default, the entire target string new needs to be skipped when characters do not match;

[0020] 13) Iterate through each character of the target string new, update the corresponding position in the bmbc array, and calculate the distance of the character from the end of the target string;

[0021] 2) Matching process;

[0022] 21) Initialize the variable last to -1 to record the position of the last mismatch; initialize the variable matchedLen to 0 to represent the length of the current match;

[0023] 22) Initialize scan, oldscan, and scsc to matchedLen, indicating that the comparison starts from the length of the already matched line;

[0024] 23) In the inner loop, compare old[oldscan] and new[scan]. If they are equal, continue matching backwards, shifting oldscan and scan forward by one position respectively; old[oldscan] is an element of the old version of the file, and new[scan] is an element of the new version of the file.

[0025] If they are not equal, the number of characters to be skipped is calculated according to the Bad Character rule table bmbc.

[0026] 24) Increase scsc by bmbc[old[oldscan]] to ensure that scsc is not less than 0;

[0027] 25) Select a jump step size and decide whether to jump based on the increase in the matched length or the increase in the scsc by comparing the difference between the matched length and scsc.

[0028] 26) Update the position of the scan, skipping non-matching characters;

[0029] 27) Reset oldscan to matchedLen to prepare for the next round of comparison;

[0030] scan: Scan index, used to traverse the target string new for comparison;

[0031] oldscan: Old scan index, used to traverse the original string old for comparison;

[0032] scsc: The distance to move under bad character rules, used to determine the number of characters to skip in case of a mismatch;

[0033] 3) Check if the entire target string has been matched. If it has, exit the loop and return the length of the matched string, matchedLen.

[0034] The optimization of the search function is as follows:

[0035] The search function uses heuristic search and binary search to accelerate the search process.

[0036] 1) Heuristic search threshold and binary search jump size:

[0037] threshold: A heuristic search threshold used to set a minimum match length. The search terminates immediately when the match length reaches or exceeds the threshold.

[0038] jump: The jump step size in binary search, used to quickly locate the position of the hash value match within the search range;

[0039] 2) Hash value calculation:

[0040] The hash value of the target string is calculated by calling the `calculateHash` function. The hash value is used to speed up the search process.

[0041] 3) Binary search for the target location:

[0042] Use binary search to find the position that matches the hash value of the target string within the search range;

[0043] If a matching hash value is found, perform an exact match, find the longest matching length, and save the matching position to the pos pointer;

[0044] 4) Linear search:

[0045] If the binary search does not find a matching position, then a linear search is performed (the original search function is just a linear search).

[0046] Perform precise matching at each position within the search range, and determine whether the matching condition is met based on the threshold.

[0047] If a position with a matching length exceeding the threshold is found, the matching position is saved to the pos pointer;

[0048] 5)) Return result:

[0049] If a position is found where the matching length exceeds the threshold, the matching length is returned.

[0050] If no matching position that meets the threshold is found, return 0;

[0051] By employing heuristic search and binary search optimization strategies, the search function can more quickly locate the optimal position that matches the target block, thereby improving the search efficiency of the bsdiff algorithm.

[0052] Here, pos represents a pointer that stores the best matching position.

[0053] S2. Divide the differential file into upgrade data blocks of the same size, adaptively set the size of the upgrade data blocks according to the network bandwidth, use Blake2s to calculate the checksum of each upgrade data block, save the checksum of the upgrade data block and the differential file in the banded index table, and send the upgrade data blocks one by one to the PLC through the second encrypted connection.

[0054] S3: When the PLC receives the upgrade data block transmitted from the cloud server, it performs data integrity verification in the PLC's BootLoader area.

[0055] S3 specifically includes:

[0056] The received upgrade data block is used to calculate the checksum using Blake2s and compared with the index table of checksums stored in the cloud server through the first encrypted connection;

[0057] If the comparison results are consistent, the upgrade data block transmission and integrity verification are successful. The cloud server then transmits other upgrade data blocks according to the identifiable number attached to the differential file when it is split into upgrade data blocks, until the transmission is complete.

[0058] If the comparison results are inconsistent, the current upgrade data block will be retransmitted;

[0059] S4. After verifying the integrity of each upgrade data block, synthesize all upgrade data blocks according to their identifiable numbers, and compare the synthesized differential file with the hash value of the differential file in the cloud server's index table.

[0060] If they match, the synthesized differential file will be temporarily placed in the APP2 area of ​​the PLC.

[0061] If there is a discrepancy, the upgrade data block will be retransmitted;

[0062] S5. The synthesized differential file is merged with the old version file in the APP2 area using an optimized bspatch algorithm to generate a new version file.

[0063] Furthermore, the optimized bspatch algorithm introduces caching technology into the original bspatch algorithm, as follows:

[0064] Introduce two cache arrays, diffCache and extraCache, and corresponding cache size variables, diffCacheSize and extraCacheSize;

[0065] Specifically, the caching technology introduced into the optimized bspatch algorithm includes optimizations for diff blocks and extra blocks:

[0066] The optimization of the difference block is as follows:

[0067] First, check if the cache diffCache already contains the required difference data, and whether the set cache size diffCacheSize is sufficient to accommodate the difference data;

[0068] If the required difference data is included, use the data in the cache directly to avoid repeatedly reading the file;

[0069] If the cache is empty or the cache size is insufficient, we use the BZ2_bzRead() function to read the difference data from the difference file and store it in a new cache array diffCache, while updating the effective data size of the current cache array.

[0070] The optimization of the additional block is as follows:

[0071] First, check if the extra cache for the extra block data already contains the required data, and whether the set cache size extraCacheSize is sufficient to accommodate the extra block data;

[0072] If the required difference data is included, use the data in the cache directly to avoid repeatedly reading the file;

[0073] If the cache is empty or the cache size is insufficient, we use the BZ2_bzRead() function to read extra block data from the differential file and store it in a new cache array extraCache, while updating the effective data size of the current cache array.

[0074] Through the modifications described above, we implemented a caching technique for reading differential data and additional block data. This caching technique helps us avoid repeatedly reading files and improves processing speed.

[0075] S6. Restart the PLC. The BootLoader in the PLC moves the new version file from the APP2 area to the application code area, resets the upgrade flag, executes the command, jumps to the application code area, runs the new version file, and the upgrade is complete.

[0076] Compared with the prior art, the beneficial effects of the present invention are as follows:

[0077] (1) The present invention provides a remote firmware upgrade method for PLC. The present invention uses dual connections to transmit upgrade data blocks and verify integrity respectively. The differential file block size is adaptively set according to the network bandwidth, which can quickly and safely perform file integrity verification. The block size is automatically set according to the bandwidth to minimize the time required to complete the transmission.

[0078] (2) The PLC firmware remote upgrade method provided by the present invention improves the efficiency of calculating the longest matching length starting from a specified position in the source file and the target file and finding the best position matching a given block in the target file by optimizing the matchlen function and the search function, thereby comprehensively improving the upgrade efficiency.

[0079] (3) The present invention provides a remote firmware upgrade method for PLCs, which uses Blake2s as the verification algorithm for upgrade data blocks, and has higher security, fewer conflicts and lower memory usage during verification. Attached Figure Description

[0080] Figure 1 This is a diagram illustrating the transmission process between the cloud server and the PLC as described in this invention.

[0081] Figure 2 This is a schematic diagram of the structural process described in Embodiment 1 of the present invention.

[0082] Figure 3 This is a comparison chart of the execution time of the differential file generated by the optimized Bsdiff and the original Bsdiff in Embodiment 1 of the present invention.

[0083] Figure 4 This is a comparison chart of the difference file data volume generated by the optimized Bsdiff and the original Bsdiff in Embodiment 1 of the present invention. Detailed Implementation

[0084] The present disclosure will be further described below with reference to the accompanying drawings and embodiments.

[0085] It should be noted that the following detailed descriptions are exemplary and intended to provide further illustration of this disclosure. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this disclosure pertains.

[0086] It should be noted that the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the exemplary embodiments according to this disclosure. As used herein, the singular form is intended to include the plural form as well, unless the context clearly indicates otherwise. Furthermore, it should be understood that when the terms “comprising” and / or “including” are used in this specification, they indicate the presence of features, steps, operations, devices, components, and / or combinations thereof.

[0087] Where there is no conflict, the embodiments and features described herein can be combined with each other.

[0088] Example 1

[0089] This embodiment provides a method for remotely upgrading PLC firmware, such as... Figure 1 , Figure 2 As shown:

[0090] S1. After the PLC is powered on, the old version file identifier in the storage area is automatically calculated by differential calculation with the new version file identifier on the cloud server to obtain the differential file.

[0091] S1 specifically includes:

[0092] After the PLC is powered on, the old version file identifier in the storage area is automatically compared with the new version file identifier on the cloud server.

[0093] If the comparison results are inconsistent, the upgrade flag in the PLC is set, and the cloud server creates a differential upgrade task. The old version file package obtained based on the PLC file identifier will be differentially calculated with the new version file on the cloud server to obtain the differential file.

[0094] If the comparison results are consistent, no upgrade is needed.

[0095] Furthermore, the difference calculation is performed using an optimized bsdiff algorithm, which includes the matchlen function and the search function;

[0096] The matching function processes the following steps:

[0097] 1) Function preprocessing

[0098] 11) An array named bmbc was created to store the Bad Character rule table, with an array size of UCHAR_MAX+1;

[0099] 12) Initialize all elements of the bmbc array to newsize, which means that by default, the entire target string new needs to be skipped when characters do not match;

[0100] 13) Iterate through each character of the target string new, update the corresponding position in the bmbc array, and calculate the distance of the character from the end of the target string.

[0101] 2) Matching process;

[0102] 21) Initialize the variable last to -1 to record the position of the last mismatch; initialize the variable matchedLen to 0 to represent the length of the current match;

[0103] 22) Initialize scan, oldscan, and scsc to matchedLen, indicating that the comparison starts from the length of the already matched length.

[0104] 23) In the inner loop, compare old[oldscan] and new[scan]. If they are equal, continue matching backwards, shifting oldscan and scan forward by one position respectively. old[oldscan] is an element of the old version of the file, and new[scan] is an element of the new version of the file. Comparing old[oldscan] and new[scan] means comparing the characters in the two files one by one.

[0105] If they are not equal, the number of characters to be skipped is calculated according to the Bad Character rule table bmbc.

[0106] 24) Increase scsc by bmbc[old[oldscan]] to ensure that scsc is not less than 0.

[0107] 25) Select a jump step size and decide whether to jump based on the increase in the matched length or the increase in the scsc by comparing the difference between the matched length and scsc.

[0108] 26) Update the position of scan and skip non-matching characters.

[0109] 27) Reset oldscan to matchedLen to prepare for the next round of comparison.

[0110] scan: Scan index, used to traverse the target string new for comparison.

[0111] oldscan: Old scan index, used to traverse the original string old for comparison.

[0112] scsc: The distance to move under bad character rules, used to determine the number of characters to skip in case of a mismatch.

[0113] 3) Check if the entire target string has been matched. If it has, exit the loop and return the length of the matched string, matchedLen.

[0114] The matchlen function uses the Boyer-Moore algorithm, the specific algorithm of which is as follows:

[0115] old:ABCDEFGHIJKLMNOPQRSTUVWXYZ;

[0116] new:ABCDEFGHIJKLMNOPQRSTUVWXYZ;

[0117] The `matchlen` function implementation without using the Boyer-Moore algorithm: In this case, since the original string and the target string are exactly the same, the loop will compare each character sequentially until the end of the shorter string is reached, and the final returned matching length is 26, which is the length of the shorter string.

[0118] The `matchlen` function optimized using the Boyer-Moore algorithm is implemented as follows: In the Boyer-Moore optimized version, the preprocessing step constructs a `BadCharacter` rule table `bmbc`, recording the distance of each character in the pattern string from the end. During the matching process, this table is used for fast skipping. For identical original and target strings, the Boyer-Moore optimized `matchlen` function directly returns the complete match length of 26 without comparing characters one by one.

[0119] The optimization of the search function is as follows:

[0120] The `search` function uses the ideas of heuristic search and binary search to speed up the search process.

[0121] 1) Heuristic search threshold and binary search jump size:

[0122] threshold: A heuristic search threshold used to set a minimum match length. The search terminates immediately when the match length reaches or exceeds the threshold.

[0123] jump: The jump step size in binary search, used to quickly locate the position of the hash value match within the search range;

[0124] 2) Hash value calculation:

[0125] The hash value of the target string is calculated by calling the calculateHash function. The hash value is used to speed up the search process.

[0126] 3) Binary search for the target location:

[0127] Use binary search to find the position that matches the hash value of the target string within the search range;

[0128] If a matching hash value is found, perform an exact match, find the longest matching length, and save the matching position to the pos pointer;

[0129] 4) Linear search:

[0130] If the binary search does not find a matching position, then a linear search is performed (the original search function is just a linear search).

[0131] Perform precise matching at each position within the search range, and determine whether the matching condition is met based on the threshold.

[0132] If a position with a matching length exceeding the threshold is found, the matching position is saved to the pos pointer;

[0133] 5)) Return result:

[0134] If a position is found where the matching length exceeds the threshold, the matching length is returned.

[0135] If no matching position that meets the threshold is found, return 0;

[0136] By employing heuristic search and binary search optimization strategies, the search function can more quickly locate the optimal position that matches the target block, thereby improving the search efficiency of the bsdiff algorithm.

[0137] pos: A pointer that stores the best matching position.

[0138] S2. Divide the differential file into upgrade data blocks and transmit them to the PLC: Divide the differential file into upgrade data blocks of the same size, adaptively set the size of the upgrade data blocks according to the network bandwidth, use Blake2s to calculate the checksum of each upgrade data block, save the checksum of the upgrade data block and the differential file in the banded index table, and send the upgrade data blocks one by one to the PLC through the second encrypted connection.

[0139] S3: When the PLC receives the upgrade data block transmitted from the cloud server, it performs data integrity verification in the PLC's BootLoader area.

[0140] S3 specifically includes:

[0141] The received upgrade data block is used to calculate the checksum using Blake2s and compared with the index table of checksums stored in the cloud server through the first encrypted connection;

[0142] If the comparison results are consistent, the upgrade data block transmission and integrity verification are successful. The cloud server then transmits other upgrade data blocks according to the identifiable number attached to the differential file when it is split into upgrade data blocks, until the transmission is complete.

[0143] If the comparison results are inconsistent, the current upgrade data block will be retransmitted;

[0144] S4. After verifying the integrity of each upgrade data block, synthesize all upgrade data blocks according to their identifiable numbers, and compare the synthesized differential file with the hash value of the differential file in the cloud server's index table.

[0145] If they match, the synthesized differential file will be temporarily placed in the APP2 area of ​​the PLC.

[0146] If there is a discrepancy, the upgrade data block will be retransmitted;

[0147] S5. The synthesized differential file is merged with the old version file in the APP2 area using an optimized bspatch algorithm to generate a new version file.

[0148] Furthermore, the optimized bspatch algorithm introduces caching technology into the original bspatch algorithm, as follows:

[0149] Introduce two cache arrays, diffCache and extraCache, and corresponding cache size variables, diffCacheSize and extraCacheSize;

[0150] Specifically, the caching technology introduced into the optimized bspatch algorithm includes optimizations for diff blocks and extra blocks:

[0151] The optimization of the difference block is as follows:

[0152] First, check if the cache diffCache already contains the required difference data, and whether the set cache size diffCacheSize is sufficient to accommodate the difference data;

[0153] If the required difference data is included, the data in the cache is used directly to avoid repeatedly reading the file;

[0154] If the cache is empty or the cache size is insufficient, we use the BZ2_bzRead() function to read the difference data from the difference file and store it in a new cache array diffCache, while updating the effective data size of the current cache array.

[0155] The same processing method can be used for processing additional block data:

[0156] First, check if the extra cache for the extra block data already contains the required data, and whether the set cache size extraCacheSize is sufficient to accommodate the extra block data;

[0157] If the required difference data is included, use the data in the cache directly to avoid repeatedly reading the file;

[0158] If the cache is empty or the cache size is insufficient, we use the BZ2_bzRead() function to read extra block data from the extra file and store it in a new cache array extraCache, while updating the effective data size of the current cache array.

[0159] Through the modifications described above, we implemented a caching technique for reading differential data and additional block data. This caching technique helps us avoid repeatedly reading files and improves processing speed.

[0160] S6. Restart the PLC. The BootLoader in the PLC moves the new version file from the APP2 area to the application code area, resets the upgrade flag, executes the command, jumps to the application code area, runs the new version file, and the upgrade is complete.

[0161] The following is a comparison of experimental results:

[0162] Table 1. Time required for the two algorithms to generate differential files

[0163]

[0164] Table 1 (in conjunction with) Figure 3 ,in Figure 3 The horizontal axis represents the algorithm name, and the bottom axis represents the comparison file group. a1, b1, c1, and d1 are different versions, and the unit is megabytes (MB). The vertical axis represents time, and the unit is seconds. From the implementation results, under the same Linux environment, when performing differential calculations on bin files of the same group, the Ddbsdiff algorithm has a significant improvement in differential calculation speed compared to the bsdiff algorithm. The unit used is seconds. The differential files are then merged using the bspatch algorithm to obtain a new file. The hash values ​​of the merged file and the original file are compared using the MD5 command, and the results show that they are consistent.

[0165] Table 2. Size of the difference files generated by the two algorithms

[0166]

[0167] Table 2 combined with Figure 4 ,in Figure 4 The horizontal axis represents the comparison file group, with the algorithm name at the bottom. a1, b1, c1, and d1 represent different versions, and the unit is megabytes. The vertical axis represents time, and the unit is seconds.

[0168] From the results, under the same Linux environment, when performing differential calculations on the same group of bin files, the differential packets generated by the Ddbsdiff algorithm are slightly larger than those generated by the original algorithm. Both algorithms use bytes (B) as the unit. It can be seen that for some groups, the difference in differential packet size is very small. Although the difference in data size is larger in some groups, the compression algorithms used have higher compression levels, so the actual data size of the differential packets calculated by both algorithms is relatively small, but the speed improvement of Ddbsdiff is significant. Overall, the Ddbsdiff algorithm is more efficient than the bsdiff algorithm for differential calculations on bin files. For remote transmission and upgrade files, Ddbsdiff improves the differential speed and reduces the differential time while maintaining the file size, thus improving the efficiency of the upgrade. Experiments using the MD5 command to compare the hash values ​​of the merged file and the original file show consistency.

[0169] Obviously, the above embodiments of the present invention are merely examples for clearly illustrating the technical solutions of the present invention, and are not intended to limit the specific implementation of the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the claims of the present invention should be included within the protection scope of the claims of the present invention.

Claims

1. A method for remotely upgrading PLC firmware, characterized in that, include; S1. The old version file identifier in the PLC storage area is automatically differentially calculated with the new version file identifier on the cloud server to obtain the differential file; S2. Divide the differential file into upgrade data blocks of the same size, adaptively set the size of the upgrade data blocks according to the network bandwidth, use Blake2s to calculate the checksum of each upgrade data block, save the checksum of the upgrade data block and the differential file in the banded index table, and send the upgrade data blocks one by one to the PLC through the second encrypted connection. S3: When the PLC receives the upgrade data block transmitted from the cloud server, it performs data integrity verification in the PLC's BootLoader area. S4. After verifying the integrity of each upgrade data block, synthesize all upgrade data blocks according to their identifiable numbers, and compare the synthesized differential file with the hash value of the differential file in the cloud server's index table. If they match, the synthesized differential file will be temporarily placed in the APP2 area of ​​the PLC. If there is a discrepancy, the upgrade data block will be retransmitted; S5. The synthesized differential file is merged with the old version file in the APP2 area using an optimized bspatch algorithm to generate a new version file. S6. Restart the PLC. The PLC bootloader moves the new version file from APP2 area to the application code area, resets the upgrade flag, executes the command, jumps to the application code area, runs the new version file, and the upgrade is complete. The difference calculation is performed using an optimized bsdiff algorithm. The optimized bspatch algorithm replaces the original BZ2_bzRead function with a buffer for data reading. The optimized bsdiff algorithm includes an optimized matchlen function, and the specific processing steps of the optimized matchlen function are as follows: 1) Function preprocessing; 11) Create an array named bmbc to store the Bad Character rule table, with an array size of UCHAR_MAX+1; 12) Initialize all elements of the bmbc array to newsize, indicating that the entire target string needs to be skipped when characters do not match; 13) Iterate through each character of the target string, update the corresponding position in the bmbc array, and calculate the distance of the character from the end of the target string; 2) Matching process; 21) Initialize the variable last to -1 to record the position of the last mismatch; initialize the variable matchedLen to 0 to represent the length of the current match; 22) Initialize scan, oldscan, and scsc to matchedLen, indicating that the comparison starts from the length of the already matched line; 23) In the inner loop, compare old[oldscan] and new[scan]. If they are equal, continue matching and shift oldscan and scan one position forward respectively. If they are not equal, the number of characters to be skipped is calculated according to the Bad Character rule table bmbc. 24) Increase scsc by bmbc[old[oldscan]] to ensure that scsc is not less than 0; 25) Select a jump step size and decide whether to jump based on the increase in the matched length or the increase in the scsc by comparing the difference between the matched length and scsc. 26) Update the position of the scan, skipping non-matching characters; 27) Reset oldscan to matchedLen to prepare for the next round of comparison; 3) Check if the entire target string has been matched. If it has, exit the loop and return the length of the matched string, matchedLen. The optimized bsdiff algorithm also includes an optimized search function; The optimization of the search function is as follows: 1) Heuristic search threshold and binary search jump size: threshold: Heuristic search threshold, used to set a threshold for the length of the match. When the length of the match reaches or exceeds the threshold, the search is terminated immediately. jump: The jump step size in binary search, used to quickly locate the position of the hash value match within the search range; 2) Hash value calculation: The hash value of the target string is calculated by calling the calculateHash function. The hash value is used to speed up the search process. 3) Binary search for the target location: Use binary search to find the position that matches the hash value of the target string within the search range; If a matching hash value is found, perform an exact match, find the longest matching length, and save the matching position to the pos pointer; 4) Linear search: If the binary search does not find a matching position, then a linear search is performed; Perform precise matching at each position within the search range, and determine whether the matching conditions are met based on the threshold. If a position with a matching length exceeding the threshold is found, the matching position is saved to the pos pointer; 5)) Return result: If a position is found where the matching length exceeds the threshold, the matching length is returned. If no matching position that meets the threshold is found, return 0.

2. The PLC firmware remote upgrade method according to claim 1, characterized in that, S1 specifically includes: After the PLC is powered on, the old version file identifier in the storage area is automatically compared with the new version file identifier on the cloud server. If the comparison results are inconsistent, the upgrade flag in the PLC is set, and the cloud server creates a differential upgrade task. The old version file package obtained based on the PLC file identifier will be differentially calculated with the new version file on the cloud server to obtain the differential file. If the comparison results are consistent, no upgrade is needed.

3. The PLC firmware remote upgrade method according to claim 1, characterized in that, S3 specifically includes: The received upgrade data block is used to calculate the checksum using Blake2s and compared with the index table of checksums stored in the cloud server through the first encrypted connection; If the comparison results are consistent, the upgrade data block transmission and integrity verification are successful. The cloud server then transmits other upgrade data blocks according to the identifiable number attached to the differential file when it is split into upgrade data blocks, until the transmission is complete. If the comparison results are inconsistent, the current upgrade data block will be retransmitted.

4. The PLC firmware remote upgrade method according to claim 1, characterized in that, The optimized bspatch algorithm changes the original BZ2_bzRead function from reading data to using a buffer, as detailed below: Introduce two cache arrays, diffCache and extraCache, and corresponding cache size variables, diffCacheSize and extraCacheSize; The optimized bspatch algorithm incorporates caching techniques, including optimizations for diff blocks and extra blocks: The optimization of the difference block is as follows: First, check if the cache diffCache already contains the required difference data, and whether the set cache size diffCacheSize is sufficient to accommodate the difference data; If the required difference data is included, use the data in the cache directly; If the cache is empty or the cache size is insufficient, use the BZ2_bzRead() function to read the difference data from the difference file and store it in a new cache array diffCache, while updating the effective data size of the current cache array. The optimization of the additional block is as follows: First, check if the extra cache for the extra block data already contains the required data, and whether the set cache size extraCacheSize is sufficient to accommodate the extra block data; If the required difference data is included, use the data in the cache directly; If the cache is empty or the cache size is insufficient, use the BZ2_bzRead() function to read additional data from the extra block file and store it in a new cache array extraCache, while updating the effective data size of the current cache array.