@@ -34,4 +34,161 @@ This example demonstrates how to use the EcuBus-Pro to upgrade Application firmw
3434
3535---
3636
37+
38+
39+ ## Diagnostic Steps
40+
41+ This example implements firmware upgrade through UDS diagnostic protocol. The main steps are as follows:
42+
43+ 1 . Session Control and Communication Control
44+ - DiagnosticSessionControl (0x10) switch to programming session (0x03)
45+ - CommunicationControl (0x28) disable normal communication (controlType=0x03)
46+ - DiagnosticSessionControl (0x10) switch to extended session (0x02)
47+
48+ 2 . Security Access
49+ - SecurityAccess (0x27, subfunction=0x01) request seed
50+ - SecurityAccess (0x27, subfunction=0x02) send key
51+ - Key calculation uses AES-128-CBC algorithm, key is [ 0-15] , IV is all zeros
52+
53+ 3 . Write Identifier
54+ - WriteDataByIdentifier (0x2E, DID=0xF15A) write specific identifier
55+
56+ 4 . Download Program
57+ For each firmware file:
58+ 1 . RequestDownload (0x34) request download, specify memory address and size
59+ 2 . RoutineControl (0x31, routineId=0x0202) verify CRC
60+ 3 . TransferData (0x36) transfer data in blocks
61+ 4 . RequestTransferExit (0x37) end transfer
62+
63+ 5 . Firmware Verification and Reset
64+ - RoutineControl (0x31, routineId=0xFF00) verify firmware
65+ - RoutineControl (0x31, routineId=0xFF01) verification complete
66+ - ECUReset (0x11) reset ECU
67+
68+ ## Firmware Files
69+
70+ The example includes two firmware files:
71+ 1 . S32K344_FlsDrvRTD100.bin
72+ - Download Address: 0x20000010
73+ - Driver firmware
74+
75+ 2 . S32K344_CAN_App_RTD200.bin
76+ - Download Address: 0x00440200
77+ - Application firmware
78+
79+ ## Notes
80+
81+ 1 . Ensure firmware files are placed in the project's bin directory
82+ 2 . Do not disconnect or power off during download
83+ 3 . If download fails, you can retry the entire process
84+ 4 . Each firmware file needs CRC verification
85+
86+ ---
87+
3788** [ Demo Video] ( https://www.bilibili.com/video/BV1KcmfYNEkQ ) **
89+
90+ ## Script Implementation Details
91+
92+ The bootloader.ts script implements the diagnostic sequence. Here's a detailed explanation of each part:
93+
94+ ### Initialization and Imports
95+ ``` typescript
96+ import crypto from ' crypto'
97+ import { CRC , DiagRequest , DiagResponse } from ' ECB'
98+ import path from ' path'
99+ import fs from ' fs/promises'
100+ ```
101+ - Imports required modules for cryptography, CRC calculation, and file operations
102+ - ` ECB ` provides UDS diagnostic communication utilities
103+
104+ ### Configuration
105+ ``` typescript
106+ const crc = new CRC (' self' , 16 , 0x3d65 , 0 , 0xffff , true , true )
107+ let maxChunkSize: number | undefined = undefined
108+ let content: undefined | Buffer = undefined
109+ ```
110+ - Configures CRC-16 calculator for firmware verification
111+ - Variables to store transfer block size and firmware content
112+
113+ ### Firmware Files Configuration
114+ ``` typescript
115+ const fileList = [{
116+ addr: 0x20000010 ,
117+ file: path .join (process .env .PROJECT_ROOT , ' bin' , ' S32K344_FlsDrvRTD100.bin' )
118+ }, {
119+ addr: 0x00440200 ,
120+ file: path .join (process .env .PROJECT_ROOT , ' bin' , ' S32K344_CAN_App_RTD200.bin' )
121+ }]
122+ ```
123+ - Defines firmware files to be downloaded with their target addresses
124+
125+ ### Initialization Handler
126+ ``` typescript
127+ Util .Init (async () => {
128+ const req = DiagRequest .from (' Tester_1.RoutineControl491' )
129+ req .diagSetParameter (' routineControlType' , 1 )
130+ await req .changeService ()
131+ const resp = DiagResponse .from (' Tester_1.RoutineControl491' )
132+ resp .diagSetParameter (' routineControlType' , 1 )
133+ await resp .changeService ()
134+ })
135+ ```
136+ - Modifies RoutineControl491 service to use type 1 (start routine)
137+ - Updates both request and response parameters
138+
139+ ### Security Access Handler
140+ ``` typescript
141+ Util .On (' Tester_1.SecurityAccess390.recv' , async (v ) => {
142+ const data = v .diagGetParameterRaw (' securitySeed' )
143+ const cipher = crypto .createCipheriv (
144+ ' aes-128-cbc' ,
145+ Buffer .from ([0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 ]),
146+ Buffer .alloc (16 , 0 )
147+ )
148+ let encrypted = cipher .update (data )
149+ cipher .final ()
150+ const req = DiagRequest .from (' Tester_1.SecurityAccess391' )
151+ req .diagSetParameterSize (' data' , 128 )
152+ req .diagSetParameterRaw (' data' , encrypted )
153+ await req .changeService ()
154+ })
155+ ```
156+ - Handles security access seed-key exchange
157+ - Uses AES-128-CBC to calculate key from received seed
158+ - Sends calculated key back to ECU
159+
160+ ### Download Process Handlers
161+ ``` typescript
162+ Util .Register (" Tester_1.JobFunction0" , async () => {
163+ // Prepare next firmware file for download
164+ const item = fileList .shift ()
165+ if (item ) {
166+ // Request download and verify CRC
167+ // Returns array of requests to be sent
168+ }
169+ return []
170+ })
171+
172+ Util .Register (" Tester_1.JobFunction1" , () => {
173+ // Handle actual data transfer
174+ // Splits firmware into chunks and sends them
175+ // Ends with transfer exit request
176+ // Returns array of requests to be sent
177+ })
178+ ```
179+ - JobFunction0: Prepares download by:
180+ 1 . Getting next firmware file
181+ 2 . Setting up download request with correct address
182+ 3 . Calculating and verifying CRC
183+ - JobFunction1: Handles data transfer by:
184+ 1 . Splitting firmware into appropriate chunk sizes
185+ 2 . Creating TransferData requests for each chunk
186+ 3 . Adding RequestTransferExit at the end
187+ 4 . Triggering firmware verification after last file
188+
189+ The script works in conjunction with the sequence defined in the ECB file, which executes:
190+ 1 . Session and communication control services
191+ 2 . Security access sequence
192+ 3 . JobFunction0 to prepare download
193+ 4 . JobFunction1 to transfer data
194+ 5 . Final verification and reset
0 commit comments