Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
200 lines (182 loc) · 5.79 KB
/
Copy pathmain.cpp
File metadata and controls
200 lines (182 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "ch.h"
#include "hal.h"
#include <string.h>
#include "flash.h"
#include "aes.h"
#include "usbcfg.h"
#define APPLICATION_ADDRESS 0x0800A000
#define SIZE_STORAGE_ADDRESS 0x0800A000 - 4
#define CRC_STORAGE_ADDRESS 0x0800A000 - 8
#define SECTOR_SIZE (8U * 1024U)
#define MAX_IMAGE_SIZE (250 * SECTOR_SIZE)
#define BANK_SIZE (128U * SECTOR_SIZE)
typedef void (*pAppEntry_t)(void);
struct AES_ctx ctx;
uint32_t crc32(const uint8_t *data, uint32_t length)
{
uint32_t crc = 0xFFFFFFFF;
for (uint32_t i = 0; i < length; i++)
{
crc ^= data[i];
for (uint8_t j = 0; j < 8; j++)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xEDB88320;
else
crc >>= 1;
}
}
return crc ^ 0xFFFFFFFF;
}
void decrypt_chunk(uint8_t *encrypted_chunk, uint8_t *output, uint32_t length)
{
memcpy(output, encrypted_chunk, length);
AES_CTR_xcrypt_buffer(&ctx, output, length);
}
bool check_application_crc(void)
{
uint32_t storedCRC = *reinterpret_cast<uint32_t *>(CRC_STORAGE_ADDRESS);
uint32_t crc = crc32(reinterpret_cast<const uint8_t *>(APPLICATION_ADDRESS),
*reinterpret_cast<uint32_t *>(SIZE_STORAGE_ADDRESS));
return (crc == storedCRC);
}
static uint8_t performUpdate(void)
{
uint8_t encrypted_buffer[1024] = {0};
uint8_t decrypted_buffer[1024] = {0};
size_t chunkSize = 0;
uint32_t imageSize = 0;
uint32_t bytesReceived = 0;
uintptr_t address = (uintptr_t)APPLICATION_ADDRESS;
uint32_t expectedCRC = 0;
uint32_t computedCRC = 0;
if (chnRead((BaseChannel *)&SDU2, (uint8_t *)&imageSize, 4) == 4)
{
uint8_t ack = 0x55;
chnWrite((BaseChannel *)&SDU2, &ack, 1);
}
else
{
return 1; // Error code 1: failed to receive image size.
}
if ((imageSize == 0) || (imageSize > MAX_IMAGE_SIZE))
{
return 2; // Error code 2: image size sanity check failed.
}
uint32_t sectorsN = (imageSize + SECTOR_SIZE - 1) / SECTOR_SIZE;
for (uint32_t i = 5; i < sectorsN + 5; i++)
{
Flash::EraseSector(i, 0);
if (i >= 128U) {
Flash::EraseSector(i - 128U, 1);
}
}
while (bytesReceived < imageSize)
{
chunkSize = ((sizeof(encrypted_buffer)) < (imageSize - bytesReceived)) ? (sizeof(encrypted_buffer)) : (imageSize - bytesReceived);
if (chnRead((BaseChannel *)&SDU2, encrypted_buffer, chunkSize) != chunkSize)
{
return 3; // Error code 3: failed to receive firmware data chunk.
}
decrypt_chunk(encrypted_buffer, decrypted_buffer, chunkSize);
if (address < 0x08100000) {
Flash::Write(address, reinterpret_cast<const char *>(decrypted_buffer), chunkSize, 0);
} else {
Flash::Write(address, reinterpret_cast<const char *>(decrypted_buffer), chunkSize, 1);
}
address += chunkSize;
bytesReceived += chunkSize;
}
if (bytesReceived != imageSize)
{
return 4; // Error code 4: total firmware data length mismatch.
}
uint8_t crcBytes[4] = {0};
if (chnRead((BaseChannel *)&SDU2, crcBytes, sizeof(crcBytes)) != 4)
{
return 5; // Error code 5: failed to receive CRC.
}
expectedCRC = (static_cast<uint32_t>(crcBytes[0]) |
((static_cast<uint32_t>(crcBytes[1])) << 8) |
((static_cast<uint32_t>(crcBytes[2])) << 16) |
((static_cast<uint32_t>(crcBytes[3])) << 24));
computedCRC = crc32(reinterpret_cast<const uint8_t *>(APPLICATION_ADDRESS), imageSize);
if (computedCRC != expectedCRC)
{
return 6; // Error code 6: CRC mismatch.
}
// Store imageSize and computed CRC in flash.
Flash::EraseSector(4, 0);
Flash::Write(SIZE_STORAGE_ADDRESS, reinterpret_cast<const char *>(&imageSize), sizeof(imageSize), 0);
Flash::Write(CRC_STORAGE_ADDRESS, reinterpret_cast<const char *>(&computedCRC), sizeof(computedCRC), 0);
return 0; // Success.
}
void read_command(void)
{
uint8_t command[4] = {0};
if (chnRead((BaseChannel *)&SDU2, command, 4) != 4)
{
return; // Error reading command
}
if (command[0] == 'U' && command[1] == 'P' && command[2] == 'D' && command[3] == 'T')
{
uint8_t result = performUpdate();
if (result != 0)
{
chnWrite((BaseChannel *)&SDU2, &result, 1); // Send error code
}
else
{
uint8_t ack = 0xAA; // Acknowledge success
chnWrite((BaseChannel *)&SDU2, &ack, 1);
chThdSleepMilliseconds(100);
NVIC_SystemReset();
}
}
else
{
uint8_t error = 0xFF; // Unknown command
chnWrite((BaseChannel *)&SDU2, &error, 1);
}
}
void boot_app(void)
{
uint32_t sp_value;
uint32_t reset_handler;
pAppEntry_t app_entry;
sduStop(&SDU2);
chSysDisable();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
SCB_DisableDCache();
SCB_DisableICache();
sp_value = *(__IO uint32_t*)(APPLICATION_ADDRESS);
reset_handler = *(__IO uint32_t*)(APPLICATION_ADDRESS + 4UL);
SCB->VTOR = APPLICATION_ADDRESS;
__set_MSP(sp_value);
app_entry = (pAppEntry_t)reset_handler;
app_entry();
}
int main(void) {
halInit();
chSysInit();
sduObjectInit(&SDU2);
sduStart(&SDU2, &serusbcfg);
usbDisconnectBus(serusbcfg.usbp);
chThdSleepMilliseconds(1500); // Wait for host to recognize disconnect
usbStart(serusbcfg.usbp, &usbcfg);
usbConnectBus(serusbcfg.usbp);
while (true) {
if (usbGetDriverStateI(serusbcfg.usbp) == USB_ACTIVE)
{
read_command();
} else {
if(check_application_crc()) {
boot_app();
}
}
palTogglePad(GPIOB, 15U);
chThdSleepMilliseconds(100);
}
}