Add error handling to OTA process
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
j3d1 2021-09-12 21:55:17 +02:00
parent 8f052ecb37
commit 24f5e4398d
6 changed files with 119 additions and 51 deletions

View file

@ -68,7 +68,7 @@ void system_otaflash_init() {
otaflash_context.seq = 0;
}
int system_otaflash_chunk(uint8_t *data, uint16_t len, uint16_t seq, uint32_t hash) {
enum return_code system_otaflash_chunk(uint8_t *data, uint16_t len, uint16_t seq, uint32_t hash, uint16_t *ack) {
uint32_t local_hash = crc32(data, len);
if(hash == local_hash && otaflash_context.seq == seq) {
if(otaflash_context.head % SECTOR_SIZE == 0) {
@ -83,9 +83,13 @@ int system_otaflash_chunk(uint8_t *data, uint16_t len, uint16_t seq, uint32_t ha
}
otaflash_context.head += len;
otaflash_context.seq++;
return 0x88;
return OK;
} else if(hash != local_hash) {
return CHECKSUM_MISMATCH;
} else {
return 0xff;
if(ack)
*ack = otaflash_context.seq;
return SEQUENCE_OUT_OF_ORDER;
}
}
@ -96,21 +100,22 @@ void system_otaflash_verify_chunk(void *ctx, void *data, size_t len) {
*(uint32_t *) ctx = digest;
}
int system_otaflash_verify_and_switch(uint32_t len, uint32_t hash) {
enum return_code system_otaflash_verify_and_switch(uint32_t len, uint32_t hash) {
uint32_t digest = 0;
rboot_digest_image(otaflash_context.base, min(len, MAX_IMAGE_SIZE), system_otaflash_verify_chunk, &digest);
if(hash != digest) {
printf("OTA failed to verify firmware\r\n");
return 0x99;
return CHECKSUM_MISMATCH;
}
vPortEnterCritical();
if(!rboot_set_current_rom(otaflash_context.slot)) {
printf("OTA failed to set new rboot slot\r\n");
printf("OTA Update failed to set new rboot slot\r\n");
vPortExitCritical();
return RBOOT_SWITCH_FAILED;
}
sdk_system_restart();
vPortExitCritical(); // | should not be reached
return 0x77; // |
vPortExitCritical();
return OK;
}