/**
* @brief Receive firmware update with chunk count information
*
* @param uart_handle UART handle for communication
* @param flash_start_address Starting address in flash for firmware
* @param max_chunk_size Maximum expected chunk size
* @return SFU_ErrorStatus SFU_SUCCESS if update completed successfully
*/
SFU_ErrorStatus SFU_Receive_FirmwareUpdate_Enhanced(
UART_HandleTypeDef *uart_handle,
uint32_t flash_start_address,
uint16_t max_chunk_size
) {
// 1. Wait for start command
if (SFU_WaitForStartCommand(uart_handle) != SFU_SUCCESS) {
return SFU_ERROR_INIT;
}
// 2. Receive first chunk with metadata
FirmwareMetadata metadata;
uint8_t first_chunk_data[max_chunk_size];
if (SFU_Receive_FirstChunk(uart_handle, &metadata, first_chunk_data, max_chunk_size) != SFU_SUCCESS) {
return SFU_ERROR_METADATA;
}
// 3. Initialize decryption and flash
SE_ErrorStatus se_status;
SE_StatusTypeDef e_se_status;
SFU_FLASH_StatusTypeDef flash_status;
uint8_t fw_header[sizeof(SE_FwRawHeaderTypeDef)] __attribute__((aligned(8)));
uint32_t flash_offset = 0;
// Process header from first chunk
memcpy(fw_header, first_chunk_data, SE_FW_HEADER_TOT_LEN);
se_status = SE_Decrypt_Init(&e_se_status, (SE_FwRawHeaderTypeDef *)fw_header, SE_FW_IMAGE_COMPLETE);
if (se_status != SE_SUCCESS) {
return SFU_ERROR_DECRYPTION;
}
// Write header to flash
SFU_LL_FLASH_Write(&flash_status, (uint8_t *)flash_start_address, fw_header, SE_FW_HEADER_TOT_LEN);
if (flash_status != SFU_FLASH_SUCCESS) {
return SFU_ERROR_FLASH;
}
flash_offset += SE_FW_HEADER_TOT_LEN;
// Decrypt and write remaining part of first chunk
uint8_t decrypted_chunk[max_chunk_size];
int32_t decrypted_size;
uint32_t first_chunk_payload_size = metadata.chunk_size - SE_FW_HEADER_TOT_LEN;
se_status = SE_Decrypt_Append(&e_se_status,
&first_chunk_data[SE_FW_HEADER_TOT_LEN],
first_chunk_payload_size,
decrypted_chunk,
&decrypted_size);
if (se_status != SE_SUCCESS) {
return SFU_ERROR_DECRYPTION;
}
SFU_LL_FLASH_Write(&flash_status,
(uint8_t *)(flash_start_address + flash_offset),
decrypted_chunk,
decrypted_size);
if (flash_status != SFU_FLASH_SUCCESS) {
return SFU_ERROR_FLASH;
}
flash_offset += decrypted_size;
// 4. Receive remaining chunks
uint32_t chunks_received = 1; // First chunk already received
while (chunks_received < metadata.total_chunks) {
uint8_t chunk_data[max_chunk_size];
uint16_t expected_size = (chunks_received == metadata.total_chunks - 1) ?
metadata.last_chunk_size : metadata.chunk_size;
// Receive chunk
uint16_t actual_size;
if (SFU_Receive_DataChunk_Enhanced(uart_handle,
chunk_data,
&actual_size,
chunks_received) != SFU_SUCCESS) {
return SFU_ERROR_TRANSFER;
}
// Verify size matches expected
if (actual_size != expected_size) {
return SFU_ERROR_SIZE_MISMATCH;
}
// Decrypt chunk
se_status = SE_Decrypt_Append(&e_se_status,
chunk_data,
actual_size,
decrypted_chunk,
&decrypted_size);
if (se_status != SE_SUCCESS) {
return SFU_ERROR_DECRYPTION;
}
// Write to flash
SFU_LL_FLASH_Write(&flash_status,
(uint8_t *)(flash_start_address + flash_offset),
decrypted_chunk,
decrypted_size);
if (flash_status != SFU_FLASH_SUCCESS) {
return SFU_ERROR_FLASH;
}
flash_offset += decrypted_size;
chunks_received++;
}
// 5. Wait for end command
if (SFU_WaitForEndCommand(uart_handle) != SFU_SUCCESS) {
return SFU_ERROR_COMPLETION;
}
// 6. Verify total size
if (flash_offset != metadata.total_size) {
return SFU_ERROR_SIZE_VERIFICATION;
}
// 7. Finalize update
return SFU_FinalizeUpdate(flash_start_address);
}
/**
* @brief Receive first chunk with metadata
*/
SFU_ErrorStatus SFU_Receive_FirstChunk(
UART_HandleTypeDef *uart_handle,
FirmwareMetadata *metadata,
uint8_t *data,
uint16_t max_size
) {
uint8_t packet[max_size + 32];
// Receive packet
if (SFU_Receive_UART_Data(uart_handle, packet, max_size + 15, 5000) != SFU_SUCCESS) {
return SFU_ERROR_TIMEOUT;
}
// Verify packet header
if (packet[0] != 0x01 || packet[1] != 0x00 || packet[2] != 0xFF) {
return SFU_ERROR_INVALID_HEADER;
}
// Extract metadata
metadata->total_chunks = (packet[3] << 8) | packet[4];
metadata->chunk_size = (packet[5] << 8) | packet[6];
metadata->last_chunk_size = (packet[7] << 8) | packet[8];
metadata->total_size = (packet[9] << 24) | (packet[10] << 16) |
(packet[11] << 8) | packet[12];
// Verify metadata checksum
uint8_t meta_checksum = 0;
for (int i = 3; i <= 12; i++) {
meta_checksum ^= packet[i];
}
if (meta_checksum != packet[13]) {
return SFU_ERROR_CHECKSUM;
}
// Extract and verify data
memcpy(data, &packet[14], metadata->chunk_size);
// Verify data checksum
uint8_t data_checksum = 0;
for (uint16_t i = 0; i < metadata->chunk_size; i++) {
data_checksum ^= data[i];
}
if (data_checksum != packet[14 + metadata->chunk_size]) {
return SFU_ERROR_CHECKSUM;
}
return SFU_SUCCESS;
}
/**
* @brief Receive a data chunk with size information
*/
SFU_ErrorStatus SFU_Receive_DataChunk_Enhanced(
UART_HandleTypeDef *uart_handle,
uint8_t *data,
uint16_t *actual_size,
uint32_t expected_chunk_index
) {
uint8_t packet[1024 + 6];
// Receive packet with timeout
if (SFU_Receive_UART_Data(uart_handle, packet, sizeof(packet), 5000) != SFU_SUCCESS) {
return SFU_ERROR_TIMEOUT;
}
// Verify header
if (packet[0] != 0x01) {
return SFU_ERROR_INVALID_HEADER;
}
// Verify chunk index
uint8_t chunk_index = packet[1];