[FL-2269] Core2 OTA (#1144)

* C2OTA: wip
* Update Cube to 1.13.3
* Fixed prio
* Functional Core2 updater
* Removed hardware CRC usage; code cleanup & linter fixes
* Moved hardcoded stack params to copro.mk
* Fixing CI bundling of core2 fw
* Removed last traces of hardcoded radio stack
* OB processing draft
* Python scripts cleanup
* Support for comments in ob data
* Sacrificed SD card icon in favor of faster update. Waiting for Storage fix
* Additional handling for OB mismatched values
* Description for new furi_hal apis; spelling fixes
* Rework of OB write, WIP
* Properly restarting OB verification loop
* Split update_task_workers.c
* Checking OBs after enabling post-update mode
* Moved OB verification before flashing
* Removed ob.data for custom stacks
* Fixed progress calculation for OB
* Removed unnecessary OB mask cast

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
hedger
2022-04-27 18:53:48 +03:00
committed by GitHub
parent 81aeda86db
commit 7ce305fca3
41 changed files with 1622 additions and 295 deletions

View File

@@ -1,42 +1,14 @@
#include "dfu_file.h"
#include <furi_hal.h>
#include <toolbox/crc32_calc.h>
#define VALID_WHOLE_FILE_CRC 0xFFFFFFFF
#define DFU_SUFFIX_VERSION 0x011A
#define DFU_DATA_BUFFER_MAX_LEN 512
#define DFU_SIGNATURE "DfuSe"
bool dfu_file_validate_crc(File* dfuf, const DfuPageTaskProgressCb progress_cb, void* context) {
if(!storage_file_is_open(dfuf) || !storage_file_seek(dfuf, 0, true)) {
return false;
}
furi_hal_crc_reset();
uint32_t file_crc = 0;
uint8_t* data_buffer = malloc(DFU_DATA_BUFFER_MAX_LEN);
uint16_t data_buffer_valid_len;
uint32_t file_size = storage_file_size(dfuf);
/* Feed file contents per sector into CRC calc */
furi_hal_crc_acquire(osWaitForever);
for(uint32_t fptr = 0; fptr < file_size;) {
data_buffer_valid_len = storage_file_read(dfuf, data_buffer, DFU_DATA_BUFFER_MAX_LEN);
if(data_buffer_valid_len == 0) {
break;
}
fptr += data_buffer_valid_len;
if((fptr % DFU_DATA_BUFFER_MAX_LEN == 0)) {
progress_cb(fptr * 100 / file_size, context);
}
file_crc = furi_hal_crc_feed(data_buffer, data_buffer_valid_len);
}
furi_hal_crc_reset();
free(data_buffer);
uint32_t file_crc = crc32_calc_file(dfuf, progress_cb, context);
/* Last 4 bytes of DFU file = CRC of previous file contents, inverted
* If we calculate whole file CRC32, incl. embedded CRC,

View File

@@ -14,6 +14,9 @@
#define MANIFEST_KEY_RADIO_VERSION "Radio version"
#define MANIFEST_KEY_RADIO_CRC "Radio CRC"
#define MANIFEST_KEY_ASSETS_FILE "Resources"
#define MANIFEST_KEY_OB_REFERENCE "OB reference"
#define MANIFEST_KEY_OB_MASK "OB mask"
#define MANIFEST_KEY_OB_WRITE_MASK "OB write mask"
UpdateManifest* update_manifest_alloc() {
UpdateManifest* update_manifest = malloc(sizeof(UpdateManifest));
@@ -23,6 +26,9 @@ UpdateManifest* update_manifest_alloc() {
string_init(update_manifest->staged_loader_file);
string_init(update_manifest->resource_bundle);
update_manifest->target = 0;
memset(update_manifest->ob_reference.bytes, 0, FURI_HAL_FLASH_OB_RAW_SIZE_BYTES);
memset(update_manifest->ob_compare_mask.bytes, 0, FURI_HAL_FLASH_OB_RAW_SIZE_BYTES);
memset(update_manifest->ob_write_mask.bytes, 0, FURI_HAL_FLASH_OB_RAW_SIZE_BYTES);
update_manifest->valid = false;
return update_manifest;
}
@@ -75,8 +81,8 @@ static bool
flipper_format_read_hex(
flipper_file,
MANIFEST_KEY_RADIO_VERSION,
(uint8_t*)&update_manifest->radio_version,
sizeof(uint32_t));
update_manifest->radio_version.raw,
sizeof(UpdateManifestRadioVersion));
flipper_format_read_hex(
flipper_file,
MANIFEST_KEY_RADIO_CRC,
@@ -85,6 +91,22 @@ static bool
flipper_format_read_string(
flipper_file, MANIFEST_KEY_ASSETS_FILE, update_manifest->resource_bundle);
flipper_format_read_hex(
flipper_file,
MANIFEST_KEY_OB_REFERENCE,
update_manifest->ob_reference.bytes,
FURI_HAL_FLASH_OB_RAW_SIZE_BYTES);
flipper_format_read_hex(
flipper_file,
MANIFEST_KEY_OB_MASK,
update_manifest->ob_compare_mask.bytes,
FURI_HAL_FLASH_OB_RAW_SIZE_BYTES);
flipper_format_read_hex(
flipper_file,
MANIFEST_KEY_OB_WRITE_MASK,
update_manifest->ob_write_mask.bytes,
FURI_HAL_FLASH_OB_RAW_SIZE_BYTES);
update_manifest->valid =
(!string_empty_p(update_manifest->firmware_dfu_image) ||
!string_empty_p(update_manifest->radio_image) ||
@@ -94,6 +116,41 @@ static bool
return update_manifest->valid;
}
// Verifies that mask values are same for adjacent words (value & inverted)
static bool ob_data_check_mask_valid(const FuriHalFlashRawOptionByteData* mask) {
bool mask_valid = true;
for(size_t idx = 0; mask_valid && (idx < FURI_HAL_FLASH_OB_TOTAL_VALUES); ++idx) {
mask_valid &= mask->obs[idx].values.base == mask->obs[idx].values.complementary_value;
}
return mask_valid;
}
// Verifies that all reference values have no unmasked bits
static bool ob_data_check_masked_values_valid(
const FuriHalFlashRawOptionByteData* data,
const FuriHalFlashRawOptionByteData* mask) {
bool valid = true;
for(size_t idx = 0; valid && (idx < FURI_HAL_FLASH_OB_TOTAL_VALUES); ++idx) {
valid &= (data->obs[idx]. dword & mask->obs[idx].dword) ==
data->obs[idx].dword;
}
return valid;
}
bool update_manifest_has_obdata(UpdateManifest* update_manifest) {
bool ob_data_valid = false;
// do we have at least 1 value?
for(size_t idx = 0; !ob_data_valid && (idx < FURI_HAL_FLASH_OB_RAW_SIZE_BYTES); ++idx) {
ob_data_valid |= update_manifest->ob_reference.bytes[idx] != 0;
}
// sanity checks
ob_data_valid &= ob_data_check_mask_valid(&update_manifest->ob_write_mask);
ob_data_valid &= ob_data_check_mask_valid(&update_manifest->ob_compare_mask);
ob_data_valid &= ob_data_check_masked_values_valid(
&update_manifest->ob_reference, &update_manifest->ob_compare_mask);
return ob_data_valid;
}
bool update_manifest_init(UpdateManifest* update_manifest, const char* manifest_filename) {
Storage* storage = furi_record_open("storage");
FlipperFormat* flipper_file = flipper_format_file_alloc(storage);

View File

@@ -7,12 +7,26 @@ extern "C" {
#include <stdint.h>
#include <stdbool.h>
#include <m-string.h>
#include <furi_hal_flash.h>
/* Paths don't include /ext -- because at startup SD card is mounted as root */
#define UPDATE_DIR_DEFAULT_REL_PATH "/update"
#define UPDATE_MANIFEST_DEFAULT_NAME "update.fuf"
#define UPDATE_MAINFEST_DEFAULT_PATH UPDATE_DIR_DEFAULT_REL_PATH "/" UPDATE_MANIFEST_DEFAULT_NAME
typedef union {
uint8_t raw[6];
struct {
uint8_t major;
uint8_t minor;
uint8_t sub;
uint8_t branch;
uint8_t release;
uint8_t type;
} version;
} UpdateManifestRadioVersion;
_Static_assert(sizeof(UpdateManifestRadioVersion) == 6, "UpdateManifestRadioVersion size error");
typedef struct {
string_t version;
uint32_t target;
@@ -21,9 +35,12 @@ typedef struct {
string_t firmware_dfu_image;
string_t radio_image;
uint32_t radio_address;
uint32_t radio_version;
UpdateManifestRadioVersion radio_version;
uint32_t radio_crc;
string_t resource_bundle;
FuriHalFlashRawOptionByteData ob_reference;
FuriHalFlashRawOptionByteData ob_compare_mask;
FuriHalFlashRawOptionByteData ob_write_mask;
bool valid;
} UpdateManifest;
@@ -38,6 +55,8 @@ bool update_manifest_init_mem(
const uint8_t* manifest_data,
const uint16_t length);
bool update_manifest_has_obdata(UpdateManifest* update_manifest);
#ifdef __cplusplus
}
#endif

View File

@@ -7,6 +7,7 @@
#include <m-string.h>
#include <loader/loader.h>
#include <lib/toolbox/path.h>
#include <lib/toolbox/crc32_calc.h>
static const char* UPDATE_ROOT_DIR = "/ext" UPDATE_DIR_DEFAULT_REL_PATH;
static const char* UPDATE_PREFIX = "/ext" UPDATE_DIR_DEFAULT_REL_PATH "/";
@@ -156,8 +157,6 @@ UpdatePrepareResult update_operation_prepare(const char* manifest_file_path) {
path_extract_dirname(manifest_file_path, stage_path);
path_append(stage_path, string_get_cstr(manifest->staged_loader_file));
const uint16_t READ_BLOCK = 0x1000;
uint8_t* read_buffer = malloc(READ_BLOCK);
uint32_t crc = 0;
do {
if(!storage_file_open(
@@ -166,19 +165,10 @@ UpdatePrepareResult update_operation_prepare(const char* manifest_file_path) {
}
result = UpdatePrepareResultStageIntegrityError;
furi_hal_crc_acquire(osWaitForever);
uint16_t bytes_read = 0;
do {
bytes_read = storage_file_read(file, read_buffer, READ_BLOCK);
crc = furi_hal_crc_feed(read_buffer, bytes_read);
} while(bytes_read == READ_BLOCK);
furi_hal_crc_reset();
crc = crc32_calc_file(file, NULL, NULL);
} while(false);
string_clear(stage_path);
free(read_buffer);
storage_file_free(file);
if(crc == manifest->staged_loader_crc) {