Skip to content

Commit c5afd1a

Browse files
committed
binary_update: use memcpy to copy bootloader/firmware into RAM
1 parent 685733b commit c5afd1a

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

Bootloader/Src/binary_update.c

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040

4141
__attribute__ ((section(".restart_info")))
4242
volatile bootInfo_S boot_info; //!< Instruction on where to jump after the restart
43-
static uint64_t s_address; //!< Address from where to erase flash and write binary
43+
static uintptr_t s_address; //!< Address from where to erase flash and write binary
4444
static signatureType_E s_detected_binary; //!< Detected binary
4545

4646
static bool BinaryUpdate_writeToFlash(uint8_t* write_buffer, const uint32_t data_length);
47+
static bool BinaryUpdate_writeToRam(uint8_t* write_buffer, const uint32_t data_length);
4748

4849
bool
4950
BinaryUpdate_handleDetectedBinary(signatureType_E detected_binary) {
@@ -150,12 +151,15 @@ BinaryUpdate_erase(uint32_t firmware_size) {
150151
bool
151152
BinaryUpdate_write(uint8_t* write_buffer, const uint32_t packet_length) {
152153

153-
bool success = false;
154+
bool success = true;
155+
uint32_t data_length = packet_length;
156+
uint8_t* data = write_buffer;
154157

155158
bool is_secured = Security_isSecured();
156159

157160
if (is_secured) {
158161

162+
success = false;
159163
uint32_t data_length = packet_length - (MAC_SIZE + NONCE_SIZE);
160164

161165
if (data_length > 0U) {
@@ -172,12 +176,31 @@ BinaryUpdate_write(uint8_t* write_buffer, const uint32_t packet_length) {
172176
success = Security_decrypt(mac, nonce, encrypted_data, decrypted_data, data_length);
173177

174178
if (success) {
175-
success = BinaryUpdate_writeToFlash(decrypted_data, data_length);
179+
data = decrypted_data;
176180
}
177181
}
178182

179-
} else {
180-
success = BinaryUpdate_writeToFlash(write_buffer, packet_length);
183+
}
184+
185+
if (success) {
186+
187+
switch (s_detected_binary) {
188+
189+
case signatureType_FIRMWARE_FLASH:
190+
case signatureType_BOOTLOADER_FLASH:
191+
case signatureType_UNKNOWN:
192+
success = BinaryUpdate_writeToFlash(data, data_length);
193+
break;
194+
195+
case signatureType_FIRMWARE_RAM:
196+
case signatureType_BOOTLOADER_RAM:
197+
success = BinaryUpdate_writeToRam(data, data_length);
198+
break;
199+
200+
default:
201+
success = false;
202+
break;
203+
}
181204
}
182205

183206
return success;
@@ -257,3 +280,18 @@ BinaryUpdate_writeToFlash(uint8_t* write_buffer, const uint32_t data_length) {
257280

258281
return success;
259282
}
283+
284+
bool
285+
BinaryUpdate_writeToRam(uint8_t* write_buffer, const uint32_t data_length) {
286+
287+
bool success = false;
288+
289+
memcpy ((void*)s_address, write_buffer, data_length);
290+
291+
if (0 == memcmp ((void*)s_address, write_buffer, data_length)) {
292+
success = true;
293+
s_address += data_length;
294+
}
295+
296+
return success;
297+
}

0 commit comments

Comments
 (0)