On the first chunk I would like to inform receiver about chunks count so it knows where to end transmission receiving. Also last chunk can be of unknowned length and it should also be taken into consideration
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Enhanced Firmware Update Protocol with Chunk Count
Here's an improved version of the firmware update protocol that includes chunk count information in the first packet and handles variable-length final chunks:
Updated Protocol Design
First Chunk Special Format:
Contains firmware metadata (total chunks, total size, etc.)
Followed by the first portion of firmware data
Middle Chunks:
Fixed size chunks (typically 1024 bytes)
No acknowledgments needed
Final Chunk:
Variable length (whatever remains)
Special end marker
Updated Receiver Implementation
c
/**
* @brief Receive firmware update with chunk count information
*
* @param uart_handle UART communication interface
* @param flash_start_address Starting address in flash
* @param max_fw_size Maximum allowed firmware size
* @param chunk_size Expected size of regular chunks
* @param inter_chunk_delay Expected delay between chunks
* @return SFU_ErrorStatus
*/
SFU_ErrorStatus SFU_Receive_FirmwareUpdate_Enhanced(
UART_HandleTypeDef *uart_handle,
uint32_t flash_start_address,
uint32_t max_fw_size,
uint16_t chunk_size,
uint32_t inter_chunk_delay
) {
// 1. Wait for start command
if (SFU_WaitForStartCommand(uart_handle) != SFU_SUCCESS) {
return SFU_ERROR_INIT;
}
// 2. Receive first chunk with metadata
uint8_t first_chunk[chunk_size + 128]; // Extra space for metadata
uint32_t total_chunks = 0;
uint32_t total_size = 0;
uint32_t bytes_received = 0;
uint32_t chunks_received = 0;
if (SFU_Receive_FirstChunk(uart_handle, first_chunk, sizeof(first_chunk),
&total_chunks, &total_size) != SFU_SUCCESS) {
return SFU_ERROR_FIRST_CHUNK;
}
// Validate firmware size
if (total_size > max_fw_size) {
return SFU_ERROR_SIZE_EXCEEDED;
}
// 3. Initialize decryption and flash
SE_ErrorStatus se_status;
SFU_FLASH_StatusTypeDef flash_status;
uint8_t fw_header[sizeof(SE_FwRawHeaderTypeDef)] __attribute__((aligned(8)));
uint8_t *flash_ptr = (uint8_t *)flash_start_address;
uint8_t decrypted_chunk[chunk_size];
// Process first chunk (contains header + initial data)
if (SFU_Process_FirstChunk(first_chunk, total_size, fw_header,
&se_status, flash_ptr, decrypted_chunk) != SFU_SUCCESS) {
return SFU_ERROR_PROCESSING;
}
// Update pointers and counters
uint32_t header_size = SE_FW_HEADER_TOT_LEN;
bytes_received += header_size;
flash_ptr += header_size;
chunks_received++;
// 4. Receive middle chunks
while (chunks_received < total_chunks - 1) {
uint8_t data_chunk[chunk_size + 5]; // +5 for framing
if (SFU_Receive_DataChunk(uart_handle, data_chunk, sizeof(data_chunk),
inter_chunk_delay * 2) != SFU_SUCCESS) {
return SFU_ERROR_TRANSFER;
}
// Process data chunk
int32_t decrypted_size;
se_status = SE_Decrypt_Append(&se_status, &data_chunk[4], chunk_size,
decrypted_chunk, &decrypted_size);
if (se_status != SE_SUCCESS) {
return SFU_ERROR_DECRYPTION;
}
// Write to flash
SFU_LL_FLASH_Write(&flash_status, flash_ptr, decrypted_chunk, decrypted_size);
if (flash_status != SFU_FLASH_SUCCESS) {
return SFU_ERROR_FLASH;
}
// Update counters
bytes_received += decrypted_size;
flash_ptr += decrypted_size;
chunks_received++;
// Optional: Add progress tracking
}
// 5. Receive final chunk (variable length)
if (total_chunks > 0) {
uint32_t remaining_bytes = total_size - bytes_received;
if (remaining_bytes > 0) {
uint8_t final_chunk[remaining_bytes + 5];
if (SFU_Receive_FinalChunk(uart_handle, final_chunk, remaining_bytes,
inter_chunk_delay * 3) != SFU_SUCCESS) {
return SFU_ERROR_FINAL_CHUNK;
}
// Process final chunk
int32_t decrypted_size;
se_status = SE_Decrypt_Append(&se_status, &final_chunk[4], remaining_bytes,
decrypted_chunk, &decrypted_size);
if (se_status != SE_SUCCESS) {
return SFU_ERROR_DECRYPTION;
}
// Write to flash
SFU_LL_FLASH_Write(&flash_status, flash_ptr, decrypted_chunk, decrypted_size);
if (flash_status != SFU_FLASH_SUCCESS) {
return SFU_ERROR_FLASH;
}
bytes_received += decrypted_size;
}
}
// 6. Verify completion
if (bytes_received != total_size) {
return SFU_ERROR_SIZE_MISMATCH;
}
// 7. Finalize update
return SFU_FinalizeUpdate(flash_start_address, total_size);
}
New Supporting Functions
c
/**
* @brief Receive and parse the first chunk with metadata
*/
SFU_ErrorStatus SFU_Receive_FirstChunk(
UART_HandleTypeDef *uart_handle,
uint8_t *buffer,
uint16_t buffer_size,
uint32_t *total_chunks,
uint32_t *total_size
) {
// Receive first chunk with extended timeout
if (SFU_Receive_UART_Data(uart_handle, buffer, buffer_size, 2000) != SFU_SUCCESS) {
return SFU_ERROR_TIMEOUT;
}
// Parse metadata (custom format example)
// First 4 bytes: total chunks (big-endian)
*total_chunks = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
// Next 4 bytes: total size (big-endian)
*total_size = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
// Remainder is actual data starting at buffer[8]
return SFU_SUCCESS;
}
/**
* @brief Process the first chunk containing header and initial data
*/
SFU_ErrorStatus SFU_Process_FirstChunk(
uint8_t *first_chunk,
uint32_t total_size,
uint8_t *fw_header,
SE_ErrorStatus *se_status,
uint8_t *flash_ptr,
uint8_t *decrypted_chunk
) {
// Metadata occupies first 8 bytes, header starts at byte 8
memcpy(fw_header, &first_chunk[8], SE_FW_HEADER_TOT_LEN);
// Initialize decryption
*se_status = SE_Decrypt_Init(se_status, (SE_FwRawHeaderTypeDef *)fw_header, SE_FW_IMAGE_COMPLETE);
if (*se_status != SE_SUCCESS) {
return SFU_ERROR_DECRYPTION;
}
// Write header to flash
SFU_FLASH_StatusTypeDef flash_status;
SFU_LL_FLASH_Write(&flash_status, flash_ptr, fw_header, SE_FW_HEADER_TOT_LEN);
if (flash_status != SFU_FLASH_SUCCESS) {
return SFU_ERROR_FLASH;
}
// Process remaining data in first chunk (after metadata and header)
uint32_t data_offset = 8 + SE_FW_HEADER_TOT_LEN;
uint32_t first_data_size = total_size > 1024 ? 1024 - SE_FW_HEADER_TOT_LEN : total_size - SE_FW_HEADER_TOT_LEN;
int32_t decrypted_size;
*se_status = SE_Decrypt_Append(se_status, &first_chunk[data_offset], first_data_size,
decrypted_chunk, &decrypted_size);
if (*se_status != SE_SUCCESS) {
return SFU_ERROR_DECRYPTION;
}
// Write initial data to flash
SFU_LL_FLASH_Write(&flash_status, flash_ptr + SE_FW_HEADER_TOT_LEN,
decrypted_chunk, decrypted_size);
return flash_status == SFU_FLASH_SUCCESS ? SFU_SUCCESS : SFU_ERROR_FLASH;
}
/**
* @brief Receive the final variable-length chunk
*/
SFU_ErrorStatus SFU_Receive_FinalChunk(
UART_HandleTypeDef