Flashing Method, Device, ECU and Storage Medium of Vehicle ECU
By including the flashdrv executable machine code with secure processing in the ECU firmware, and using compilation and symmetric algorithms to generate function pointers for erasing and writing, the problems of long ECU flash writing time and complex management are solved, and efficient ECU flash writing is achieved.
Patent Information
- Application Number
- CN202211088511.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-07
- Publication Date
- 2025-07-22
- Estimated Expiration
- 2042-09-07
AI Technical Summary
The prior art requires downloading and managing flashdrv during the ECU flashing process, resulting in a long flashing time and high management complexity, increasing the possibility of errors.
The ECU firmware contains the flashdrv executable machine code that has been processed safely. Through compilation and symmetric algorithm processing, the converted binary machine code is generated, and the function pointer is called in RAM for erasing and writing operations, skipping the flashdrv download step.
On the premise of ensuring security, the ECU brushing time is shortened, the brushing efficiency is improved, the program version management is simplified, and the risk of misoperation is reduced.
Smart Images

Figure CN116302013B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of vehicles, and particularly to a method and device for flashing a vehicle ECU, an ECU, and a storage medium. Background Art
[0002] With the continuous development of vehicle technologies, electronic control units (ECUs) have been widely used in vehicles. An electronic control unit (ECU) is used to execute programs to process data. An automotive-grade ECU can be understood as a microcomputer controller in a vehicle, and can also be referred to as a vehicle-specific single-chip microcomputer. Each ECU includes a processor and a memory. The processor is used to execute the program stored in the memory to implement specific functions.
[0003] To meet the requirements of high reliability, in the firmware of an ECU, a driver program (abbreviated as flashdrv) that can directly erase the internal program memory (usually flash memory, abbreviated as code-flash) is generally not included to prevent accidental erasure of the code-flash area due to a program running wild, resulting in program damage. Therefore, during the UDS (Unified Diagnostic Services) flashing process, it is usually necessary to first download the flashdrv that can directly erase the code-flash and save it to the RAM where the program can run, and then call the interface in the flashdrv to erase the specified program storage area in the code-flash, and then start downloading and writing new program data. Although this method is safe, it is not convenient. It not only increases the time for flashing the flash, but also increases the workload of flashdrv program version management. In the mass production process of a product, one more process or management complexity means one more possibility of making mistakes. Summary of the Invention
[0004] Embodiments of the present invention provide a method and device for flashing a vehicle ECU, an ECU, and a storage medium, so as to simplify the sub-process of downloading an internal program memory driver program such as flashdrv during the UDS flashing process, shorten the flashing time, and improve the efficiency of the ECU flashing program on the premise of ensuring safety.
[0005] To achieve the above object, on the one hand, a method for flashing a vehicle electronic control unit ECU is provided, including:
[0006] S1, generating source code of the ECU firmware, and compiling the source code of the ECU firmware to generate the ECU firmware to be written into the ECU, wherein generating the source code of the ECU firmware includes the following steps:
[0007] S11, fix the erase and / or write functions in the source code of the internal program memory driver at a specified address;
[0008] S12, compile the driver source code to generate an executable binary machine code data file of the driver, and extract the original binary machine code corresponding to the erase and / or write functions from the specified address of the executable binary machine code data file;
[0009] S13, process the original binary machine code using a predetermined symmetric algorithm to obtain a converted binary machine code corresponding to the erase and write functions;
[0010] S14, include the converted binary machine code corresponding to the erase and / or write functions into the source code of the ECU firmware.
[0011] Wherein, the flashing method includes:
[0012] S21, obtain the converted binary machine code corresponding to the erase and / or write functions from the ECU firmware;
[0013] S22, perform an inverse operation corresponding to the predetermined symmetric algorithm on the converted binary machine code to restore the converted binary machine code to the original binary machine code corresponding to the erase and / or write functions;
[0014] S23, save the original binary machine code in an array in the RAM with the same data type and size, and assign the array name to a function pointer of the corresponding type;
[0015] S24, call the function pointer to perform erase and write operations, and after the call is completed, clear the array in the RAM.
[0016] Preferably, in the flashing method, fixing the erase and / or write functions at the specified address is achieved through the link control configuration of the compiler.
[0017] Preferably, in the flashing method, the process of processing the binary machine code using the predetermined symmetric algorithm includes taking the inverse code of the original binary machine code corresponding to the erase and / or write functions.
[0018] Preferably, in the flashing method, in step S14, the converted binary machine code corresponding to the erase and / or write functions is included in the source code of the ECU firmware in the form of a constant array; in step S21, the converted binary machine code in the form of a constant array is obtained from the ECU firmware.
[0019] Preferably, in the flashing method, it further includes encapsulating all operations in steps S21 to S24 into an inline function for calling.
[0020] Preferably, in the flashing method, it further includes adding one or more switch locks in the inline function for restricting the execution of erase and / or write operations.
[0021] Preferably, in the flashing method, the one or more switch locks are added by adding a global switch variable.
[0022] On the other hand, a flashing device for a vehicle electronic control unit (ECU) is provided, including a memory and a processor. The memory stores at least one segment of program, and the at least one segment of program is executed by the processor to implement the method as described in any one of the above.
[0023] On another aspect, a vehicle electronic control unit (ECU) is provided, including a memory and a processor. The memory stores at least one segment of program, and the at least one segment of program is executed by the processor to implement the method as described in any one of the above.
[0024] On yet another aspect, a computer-readable storage medium is provided, where the storage medium stores at least one segment of program, and the at least one segment of program is executed by the processor to implement the method as described above.
[0025] The above technical solutions have the following technical effects:
[0026] The technical solution of the embodiment of the present invention directly includes the executable machine code data of the internal program memory driver such as flashdrv in the ECU firmware. The executable machine code data includes special security processing for the erase and / or write functions, making it not easily misoperated. In this way, on the premise of ensuring security, the flashdrv executable machine code data is directly included in the firmware. When using the technical solution of the embodiment of the present invention for ECU flashing, the download step of flashdrv can be skipped, and directly download the program data to be updated, thereby shortening the ECU flashing time, improving the efficiency of the ECU flashing program, and at the same time simplifying the work of program version management. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] Figure 1 It is a flowchart of the flashing method of the ECU according to an embodiment of the present invention;
[0028] Figure 2 It is a structural diagram of the flashing device of the ECU according to an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0029] To further illustrate the various embodiments, the present invention provides drawings. These drawings are part of the disclosure of the present invention, which are mainly used to illustrate the embodiments and can be used in conjunction with the relevant descriptions in the specification to explain the operating principles of the embodiments. With reference to these contents, a person of ordinary skill in the art should be able to understand other possible implementations and advantages of the present invention. The components in the figures are not drawn to scale, and similar component symbols are generally used to represent similar components.
[0030] The present invention will now be further described with reference to the accompanying drawings and specific implementation methods.
[0031] Embodiment 1:
[0032] Figure 1 FIG. 1 is a flow chart of an ECU flashing method according to an embodiment of the present invention. In the ECU flashing method according to an embodiment of the present invention, the ECU firmware includes executable machine code of an internal program memory driver. Figure 1 , an ECU flashing method according to an embodiment of the present invention comprises:
[0033] S1, generating source code of ECU firmware, compiling the source code of ECU firmware to generate ECU firmware to be written into ECU, wherein the source code of ECU firmware includes executable machine code of internal program memory driver after conversion processing; specifically, generating source code of ECU firmware includes the following steps;
[0034] S11, fixing the erase and / or write function in the source code of the internal program memory driver such as the flash driver at a specified address; in a specific implementation, this step can be implemented by link control configuration of the compiler;
[0035] S12, compiling the internal program memory driver source code to generate an executable binary machine code data file of the driver, and extracting the original binary machine code corresponding to the erase and / or write function from a specified address of the binary machine code data file;
[0036] S13, using a predetermined symmetric algorithm to process the original binary machine code to obtain a converted binary machine code corresponding to the erase and / or write function;
[0037] S14, including the converted binary machine code corresponding to the erase and / or write function into the source code of the ECU firmware, that is, including it into the formal firmware program to be written into the ECU; illustratively, such as including it into the source code of the ECU firmware in the form of a const constant array;
[0038] S2. Use the ECU firmware to perform ECU flashing. The ECU firmware is an ECU firmware compiled from the above ECU firmware program containing the converted binary machine code. The specific steps of flashing include:
[0039] S21. Obtain the converted binary machine code corresponding to the erase and / or write functions from the executable machine code of the internal program memory driver; Exemplarily, the obtained converted binary machine code is in the form of a constant array;
[0040] S22. Perform an inverse operation on the converted binary machine code corresponding to a predetermined symmetric algorithm to restore the converted binary machine code to the original binary machine code corresponding to the erase and / or write functions;
[0041] S23. Save the original binary machine code in an array in the RAM with the same data type and size, and assign the array name to a function pointer of the corresponding type;
[0042] S24. Call the function pointer to perform the erase and / or write operation, and after the call is completed, clear the above array in the RAM.
[0043] Embodiment 2:
[0044] The ECU flashing method of another embodiment of the present invention is described in detail below.
[0045] The ECU flashing method of this embodiment includes: the step of obtaining the binary machine code data of the erase and write functions in the internal program memory driver such as the flash driver flashdrv.
[0046] Specifically, in the code project that generates the flashdrv C source code, of course, it can also be the source code of a driver in other languages. Fix the erase and write functions at a specified address, which can be achieved through the corresponding link control configuration of the compiler. For example, for the arm_gcc compiler, the __attribute__ keyword can be used to define the section name of a function, and then locate this section to the specified address in the.ld link file. It should be noted that to implement this function, no other functions should be called or external static variables should be referenced in the flashdrv C source code, and only local variables and access to register addresses can be used. The compiler compiles the original flashdrv C source code containing the erase and write functions, and generates an executable binary machine code data file corresponding to the original flashdrv C source code. Using the above specified address, copy the binary machine code data of the erase and write functions from the above specified address, and represent them as an array according to a predetermined instruction width, such as 16bit or 32bit:
[0047] const unsigned short FlashErase_C[] = {0x0080, 0x1821, 0x06E8, 0xD2C1, 0xD3D1, 0xD4E1, 0xD5F1, 0xD701, 0x013E, 0x0243, 0x0254, 0x0164, 0x0175, 0x030C, 0x513E, 0x0000, 0x031D, 0xC07D, 0x1949, 0x8010, 0x70E0, 0xC814, 0xE605, 0x73E0, 0x0300, 0x7800, 0x0118, 0xC37E,...};
[0048] The above binary machine code values are only exemplary and can vary according to the actual situation.
[0049] In this way, as long as a pointer to an erase or write function is defined and the address of this array is assigned to the function pointer, the above erase or write function can be called in the program. However, the data in the array at this time is the original code. When the data containing the binary original code of the executable code of the above erase or write function is included in the ECU firmware, there is still a certain risk of being misinvoked.
[0050] For safety reasons, to ensure that the erase and / or write function is not misinvoked, in the official code project of the ECU firmware, the original code of the above array data cannot be directly included. Therefore, the above array data is processed with an exclusive OR operation or other symmetric algorithms to be converted into another set of binary machine code data. In this way, the binary machine code data of the erase and write functions included in the official code project of the ECU firmware cannot be directly run, so even if it is misinvoked, the erase or write operation cannot be executed. The binary machine code array obtained after processing the above binary machine code array with an exclusive OR operation is as follows:
[0051] const unsigned short FlashErase_C[] = {0xFF7F, 0xE7DE, 0xF917, 0x2D3E, 0x2C2E, 0x2B1E, 0x2A0E, 0x28FE, 0xFEC1, 0xFDBC, 0xFDAB, 0xFE9B, 0xFE8A, 0xFCF3, 0xAEC1, 0xFFFF, 0xFCE2, 0x3F82, 0xE6B6, 0x7FEF, 0x8F1F, 0x37EB, 0x19FA, 0x8C1F, 0xFCFF, 0x87FF, 0xFEE7, 0x3C81,...};
[0052] Furthermore, in the firmware written into the ECU, it contains the executable binary machine code data of the above flashdrv. When the code-flash erasing or writing function is needed, first perform the inverse operation of the above symmetric algorithm on the converted binary machine code data processed by the symmetric algorithm to obtain the restored original binary machine code data, and save the restored data into an array with the same data type and size in the random access memory RAM. Assume the array name of this array is ram_FlashErase_C. In this way, only by assigning the array name to the function pointer of the corresponding type can the corresponding erasing and / or writing function in the driver program be called to perform the erasing and / or writing operation. After the call is completed, clear the array in this RAM. In this way, it can be ensured to the greatest extent that the array in this RAM does not store executable code data for most of the time. It ensures that the erasing and / or writing function is not mis-called and ensures the security of the internal program memory driver such as flashdrv contained in the firmware. Furthermore, in order to facilitate the business layer program to call the above erasing and / or writing function when needed, the operations described in this paragraph such as inverse operation processing, writing into the array in RAM, assigning the array name to the function pointer, and clearing after the call can be encapsulated in an inline function.
[0053] To improve security, one or more switch locks can be added to the above inline function. The call to the above inline function is included in complex business logic. Even if the program accidentally runs wild and reaches the program of this function block, it can be bypassed through layer-by-layer logical judgment to avoid accidentally executing the above inline function. For example, a global switch variable that can be erased and written can be added respectively for erasing and writing, and the on / off state of the global switch variable is used to control the erasing or writing operation. The global switch variable can default to the off state. When the upper-layer business logic determines that the flash writing function needs to be used, first set the switch variable to the on state, and then when the program calls the above inline function, judge the switch state in this function. If it is on, allow the subsequent operations to be executed. If it is off, directly return. For specific business logic, the switch variable can have a more flexible usage method, and its usage is not limited to the above description.
[0054] Through the processing of the above aspects, it is possible to directly include the internal program memory driver such as the flashdrv executable machine code data in the firmware while ensuring safety. In this way, when performing ECU flashing, the flashdrv download step can be skipped, and the program data to be updated can be directly downloaded. Through the actual application verification of previous mass-produced products, using this method, although the theoretical security is slightly lower than the original method of completely separating the flashdrv, in fact, the probability of accidental erasure of the code-flash has been extremely low, which can meet the customer's requirements for product reliability and is a feasible improvement method.
[0055] When using the method of the embodiment of the present invention to perform ECU flashing, it can achieve self-programming, that is, it can erase and write to the internal program memory during the running of the program.
[0056] Embodiment Three:
[0057] The present invention also provides a flashing device for an ECU, such as Figure 2 shown. The device includes a processor 201, a memory 202, a bus 203, and a computer program stored in the memory 202 and executable on the processor 201. The processor 201 includes one or more processing cores. The memory 202 is connected to the processor 201 through the bus 203. The memory 202 is used to store program instructions. When the processor executes the computer program, the steps in the above method embodiment of Embodiment One of the present invention are implemented.
[0058] Further, as an executable solution, the flashing device for the ECU can be a computer unit, and this computer unit can be a computing device such as a desktop computer, a notebook, a palm computer, and a cloud server. The computer unit may include, but is not limited to, a processor and a memory. Those skilled in the art can understand that the above composition structure of the computer unit is only an example of the computer unit and does not constitute a limitation on the computer unit. It may include more or fewer components than the above, or combine some components, or different components. For example, the computer unit may further include input / output devices, network access devices, a bus, etc., and the embodiment of the present invention does not make a limitation on this.
[0059] Further, as an executable solution, the processor may be a Central Processing Unit (CPU), or may also be other general-purpose processors, Digital Signal Processors (DSPs), or Application Specific Integrated Circuits (ASICs). The general-purpose processor may be a microprocessor or any conventional processor, etc. The processor is the control center of the computer unit, connecting various parts of the entire computer unit through various interfaces and circuits.
[0060] The memory can be used to store the computer program and / or modules. By running or executing the computer program and / or modules stored in the memory, and by invoking the data stored in the memory, the processor realizes various functions of the computer unit. The memory mainly includes a program storage area and a data storage area. Among them, the program storage area can store the operating system and application programs required for at least one function; the data storage area can store data created according to the use of the mobile phone, etc. In addition, the memory can include high-speed random access memory, and can also include non-volatile memory, such as hard disks, memory, plug-in hard disks, Smart Media Cards (SMCs), Secure Digital (SD) cards, Flash Cards, at least one magnetic disk storage device, flash memory device, or other volatile solid-state storage devices.
[0061] Embodiment 4:
[0062] The present invention also provides a vehicle electronic control unit ECU, which includes a memory and a processor. The memory stores at least one segment of program, and the at least one segment of program is executed by the processor to implement the flashing method as described in any one of the above.
[0063] Embodiment 5:
[0064] The present invention also provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method in the above embodiments of the present invention are realized.
[0065] If the modules / units integrated in the computer unit are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, to implement all or part of the processes in the above-mentioned embodiment methods of the present invention, it can also be completed by instructing relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by a processor, the steps of the above-mentioned various method embodiments can be realized. Among them, the computer program includes computer program code, and the computer program code can be in the form of source code, object code, executable file or some intermediate form, etc. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), and software distribution medium, etc. It should be noted that the content included in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction.
[0066] Although the present invention has been specifically shown and described in conjunction with the preferred embodiments, those skilled in the art should understand that various changes can be made to the present invention in terms of form and details without departing from the spirit and scope of the present invention defined by the appended claims, and all such changes are within the protection scope of the present invention.
Claims
1. A method for flashing an electronic control unit (ECU) of a vehicle, characterized in that, Including: S1. Generate the source code of the ECU firmware, and compile the source code of the ECU firmware to generate the ECU firmware to be written into the ECU. Among them, generating the source code of the ECU firmware includes the following steps: S11. Fix the erase and / or write functions in the source code of the internal program memory driver at a specified address; S12. Compile the source code of the driver to generate an executable binary machine code data file of the driver, and extract the original binary machine code corresponding to the erase and / or write functions from the specified address of the executable binary machine code data file; S13. Process the original binary machine code by using a predetermined symmetric algorithm to obtain the converted binary machine code corresponding to the erase and / or write functions; S14. Include the converted binary machine code corresponding to the erase and / or write functions into the source code of the ECU firmware; S2. Use the ECU firmware for flashing, specifically including: S21. Obtain the converted binary machine code corresponding to the erase and / or write functions from the ECU firmware; S22. Perform an inverse operation corresponding to the predetermined symmetric algorithm on the converted binary machine code to restore the converted binary machine code to the original binary machine code corresponding to the erase and / or write functions; S23. Save the original binary machine code in an array in the RAM with the same data type and size, and assign the array name to a function pointer of the corresponding type; S24. Call the function pointer to perform erase and write operations, and after the call is completed, clear the array in the RAM.
2. The flashing method according to claim 1, wherein Fix the erase and / or write functions at a specified address by means of the link control configuration of the compiler.
3. The flashing method according to claim 1, characterized in that, The processing of the binary machine code by using the predetermined symmetric algorithm includes taking the inverse code of the original binary machine code corresponding to the erase and / or write functions.
4. The flashing method according to claim 1, characterized in that In step S14, include the converted binary machine code corresponding to the erase and / or write functions into the source code of the ECU firmware in the form of a constant array; in step S21, obtain the converted binary machine code in the form of the constant array from the ECU firmware.
5. The flashing method according to claim 1, wherein It further includes encapsulating all the operations from step S21 to step 24 into an inline function for calling.
6. The flashing method according to claim 5, characterized in that It further includes adding one or more switch locks for restricting the execution of erase and / or write operations in the inline function.
7. The flashing method according to claim 6, wherein Add the one or more switch locks by adding a global switch variable.
8. A flashing device for a vehicle electronic control unit (ECU), characterized in that, It includes a memory and a processor, and the memory stores at least one program, and the at least one program is executed by the processor to implement the method according to any one of claims 1 to 7.
9. A vehicle electronic control unit ECU, characterized in that, It includes a memory and a processor, and the memory stores at least one program, and the at least one program is executed by the processor to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, At least one program is stored in the storage medium, and the at least one program is executed by a processor to implement the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Diesel commercial vehicle ECU (Electronic Control Unit) disassembly-free flashing system and flashing method
CN114115179A
Upgrading program design method for preventing mistaken erasure
CN114996717A