[FL-2477] Updater support for resource bundles (#1131)

* Resource unpacking core
* Added more fields to manifest; updated dist scripts
* Python linter fixes
* Parsing manifest before unpacking
* Updated pipelines for separate resource build
* Removed raw path formatting
* Visual progress for resource extraction
* Renamed update status enum

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
hedger
2022-04-19 11:03:28 +03:00
committed by GitHub
parent 1623134a82
commit e8499e4ede
12 changed files with 260 additions and 47 deletions

View File

@@ -15,6 +15,8 @@
typedef struct TarArchive {
Storage* storage;
mtar_t tar;
tar_unpack_file_cb unpack_cb;
void* unpack_cb_context;
} TarArchive;
/* API WRAPPER */
@@ -51,6 +53,7 @@ TarArchive* tar_archive_alloc(Storage* storage) {
furi_check(storage);
TarArchive* archive = malloc(sizeof(TarArchive));
archive->storage = storage;
archive->unpack_cb = NULL;
return archive;
}
@@ -92,6 +95,28 @@ void tar_archive_free(TarArchive* archive) {
}
}
void tar_archive_set_file_callback(TarArchive* archive, tar_unpack_file_cb callback, void* context) {
furi_assert(archive);
archive->unpack_cb = callback;
archive->unpack_cb_context = context;
}
static int tar_archive_entry_counter(mtar_t* tar, const mtar_header_t* header, void* param) {
UNUSED(tar);
UNUSED(header);
int32_t* counter = param;
(*counter)++;
return 0;
}
int32_t tar_archive_get_entries_count(TarArchive* archive) {
int32_t counter = 0;
if(mtar_foreach(&archive->tar, tar_archive_entry_counter, &counter) != MTAR_ESUCCESS) {
counter = -1;
}
return counter;
}
bool tar_archive_dir_add_element(TarArchive* archive, const char* dirpath) {
furi_assert(archive);
return (mtar_write_dir_header(&archive->tar, dirpath) == MTAR_ESUCCESS);
@@ -142,14 +167,25 @@ typedef struct {
static int archive_extract_foreach_cb(mtar_t* tar, const mtar_header_t* header, void* param) {
TarArchiveDirectoryOpParams* op_params = param;
TarArchive* archive = op_params->archive;
string_t fname;
bool skip_entry = false;
if(archive->unpack_cb) {
skip_entry = !archive->unpack_cb(
header->name, header->type == MTAR_TDIR, archive->unpack_cb_context);
}
if(skip_entry) {
FURI_LOG_W(TAG, "filter: skipping entry \"%s\"", header->name);
return 0;
}
if(header->type == MTAR_TDIR) {
string_init(fname);
path_concat(op_params->work_dir, header->name, fname);
bool create_res =
storage_simply_mkdir(op_params->archive->storage, string_get_cstr(fname));
bool create_res = storage_simply_mkdir(archive->storage, string_get_cstr(fname));
string_clear(fname);
return create_res ? 0 : -1;
}
@@ -162,7 +198,7 @@ static int archive_extract_foreach_cb(mtar_t* tar, const mtar_header_t* header,
string_init(fname);
path_concat(op_params->work_dir, header->name, fname);
FURI_LOG_I(TAG, "Extracting %d bytes to '%s'", header->size, header->name);
File* out_file = storage_file_alloc(op_params->archive->storage);
File* out_file = storage_file_alloc(archive->storage);
uint8_t* readbuf = malloc(FILE_BLOCK_SIZE);
bool failed = false;
@@ -303,4 +339,4 @@ bool tar_archive_add_dir(TarArchive* archive, const char* fs_full_path, const ch
free(name);
storage_file_free(directory);
return success;
}
}

View File

@@ -34,6 +34,13 @@ bool tar_archive_add_file(
bool tar_archive_add_dir(TarArchive* archive, const char* fs_full_path, const char* path_prefix);
int32_t tar_archive_get_entries_count(TarArchive* archive);
/* Optional per-entry callback on unpacking - return false to skip entry */
typedef bool (*tar_unpack_file_cb)(const char* name, bool is_directory, void* context);
void tar_archive_set_file_callback(TarArchive* archive, tar_unpack_file_cb callback, void* context);
/* Low-level API */
bool tar_archive_dir_add_element(TarArchive* archive, const char* dirpath);

View File

@@ -4,12 +4,24 @@
#include <flipper_format/flipper_format.h>
#include <flipper_format/flipper_format_i.h>
#define MANIFEST_KEY_INFO "Info"
#define MANIFEST_KEY_TARGET "Target"
#define MANIFEST_KEY_LOADER_FILE "Loader"
#define MANIFEST_KEY_LOADER_CRC "Loader CRC"
#define MANIFEST_KEY_DFU_FILE "Firmware"
#define MANIFEST_KEY_RADIO_FILE "Radio"
#define MANIFEST_KEY_RADIO_ADDRESS "Radio address"
#define MANIFEST_KEY_RADIO_VERSION "Radio version"
#define MANIFEST_KEY_RADIO_CRC "Radio CRC"
#define MANIFEST_KEY_ASSETS_FILE "Assets"
UpdateManifest* update_manifest_alloc() {
UpdateManifest* update_manifest = malloc(sizeof(UpdateManifest));
string_init(update_manifest->version);
string_init(update_manifest->firmware_dfu_image);
string_init(update_manifest->radio_image);
string_init(update_manifest->staged_loader_file);
string_init(update_manifest->resource_bundle);
update_manifest->target = 0;
update_manifest->valid = false;
return update_manifest;
@@ -21,6 +33,7 @@ void update_manifest_free(UpdateManifest* update_manifest) {
string_clear(update_manifest->firmware_dfu_image);
string_clear(update_manifest->radio_image);
string_clear(update_manifest->staged_loader_file);
string_clear(update_manifest->resource_bundle);
free(update_manifest);
}
@@ -36,21 +49,47 @@ static bool
string_init(filetype);
update_manifest->valid =
flipper_format_read_header(flipper_file, filetype, &version) &&
flipper_format_read_string(flipper_file, "Info", update_manifest->version) &&
flipper_format_read_uint32(flipper_file, "Target", &update_manifest->target, 1) &&
flipper_format_read_string(flipper_file, "Loader", update_manifest->staged_loader_file) &&
flipper_format_read_string(flipper_file, MANIFEST_KEY_INFO, update_manifest->version) &&
flipper_format_read_uint32(
flipper_file, MANIFEST_KEY_TARGET, &update_manifest->target, 1) &&
flipper_format_read_string(
flipper_file, MANIFEST_KEY_LOADER_FILE, update_manifest->staged_loader_file) &&
flipper_format_read_hex(
flipper_file,
"Loader CRC",
MANIFEST_KEY_LOADER_CRC,
(uint8_t*)&update_manifest->staged_loader_crc,
sizeof(uint32_t));
string_clear(filetype);
/* Optional fields - we can have dfu, radio, or both */
flipper_format_read_string(flipper_file, "Firmware", update_manifest->firmware_dfu_image);
flipper_format_read_string(flipper_file, "Radio", update_manifest->radio_image);
flipper_format_read_hex(
flipper_file, "Radio address", (uint8_t*)&update_manifest->radio_address, sizeof(uint32_t));
if(update_manifest->valid) {
/* Optional fields - we can have dfu, radio, or both */
flipper_format_read_string(
flipper_file, MANIFEST_KEY_DFU_FILE, update_manifest->firmware_dfu_image);
flipper_format_read_string(
flipper_file, MANIFEST_KEY_RADIO_FILE, update_manifest->radio_image);
flipper_format_read_hex(
flipper_file,
MANIFEST_KEY_RADIO_ADDRESS,
(uint8_t*)&update_manifest->radio_address,
sizeof(uint32_t));
flipper_format_read_hex(
flipper_file,
MANIFEST_KEY_RADIO_VERSION,
(uint8_t*)&update_manifest->radio_version,
sizeof(uint32_t));
flipper_format_read_hex(
flipper_file,
MANIFEST_KEY_RADIO_CRC,
(uint8_t*)&update_manifest->radio_crc,
sizeof(uint32_t));
flipper_format_read_string(
flipper_file, MANIFEST_KEY_ASSETS_FILE, update_manifest->resource_bundle);
update_manifest->valid =
(!string_empty_p(update_manifest->firmware_dfu_image) ||
!string_empty_p(update_manifest->radio_image) ||
!string_empty_p(update_manifest->resource_bundle));
}
return update_manifest->valid;
}
@@ -83,4 +122,4 @@ bool update_manifest_init_mem(
flipper_format_free(flipper_file);
return update_manifest->valid;
}
}

View File

@@ -21,6 +21,9 @@ typedef struct {
string_t firmware_dfu_image;
string_t radio_image;
uint32_t radio_address;
uint32_t radio_version;
uint32_t radio_crc;
string_t resource_bundle;
bool valid;
} UpdateManifest;