2022-04-13 20:50:25 +00:00
|
|
|
#include "update_operation.h"
|
|
|
|
|
|
|
|
#include "update_manifest.h"
|
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
#include <furi_hal.h>
|
|
|
|
#include <loader/loader.h>
|
|
|
|
#include <lib/toolbox/path.h>
|
2022-04-27 15:53:48 +00:00
|
|
|
#include <lib/toolbox/crc32_calc.h>
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
#define UPDATE_ROOT_DIR EXT_PATH("update")
|
|
|
|
|
2022-07-04 13:36:12 +00:00
|
|
|
/* Need at least 4 free LFS pages before update */
|
2022-12-26 12:13:30 +00:00
|
|
|
#define UPDATE_MIN_INT_FREE_SPACE (2 * 4 * 1024)
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
static const char* update_prepare_result_descr[] = {
|
|
|
|
[UpdatePrepareResultOK] = "OK",
|
|
|
|
[UpdatePrepareResultManifestPathInvalid] = "Invalid manifest name or location",
|
|
|
|
[UpdatePrepareResultManifestFolderNotFound] = "Update folder not found",
|
|
|
|
[UpdatePrepareResultManifestInvalid] = "Invalid manifest data",
|
|
|
|
[UpdatePrepareResultStageMissing] = "Missing Stage2 loader",
|
|
|
|
[UpdatePrepareResultStageIntegrityError] = "Corrupted Stage2 loader",
|
2022-05-11 09:45:01 +00:00
|
|
|
[UpdatePrepareResultManifestPointerError] = "Failed to create update pointer file",
|
2023-02-07 16:33:05 +00:00
|
|
|
[UpdatePrepareResultTargetMismatch] = "Hardware target mismatch",
|
2022-05-11 09:45:01 +00:00
|
|
|
[UpdatePrepareResultOutdatedManifestVersion] = "Update package is too old",
|
2022-07-04 13:36:12 +00:00
|
|
|
[UpdatePrepareResultIntFull] = "Need more free space in internal storage",
|
2023-02-07 16:33:05 +00:00
|
|
|
[UpdatePrepareResultUnspecifiedError] = "Unknown error",
|
2022-04-13 20:50:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char* update_operation_describe_preparation_result(const UpdatePrepareResult value) {
|
|
|
|
if(value >= COUNT_OF(update_prepare_result_descr)) {
|
|
|
|
return "...";
|
|
|
|
} else {
|
|
|
|
return update_prepare_result_descr[value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static bool update_operation_get_current_package_path_rtc(Storage* storage, FuriString* out_path) {
|
2022-05-06 16:26:25 +00:00
|
|
|
const uint32_t update_index = furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(out_path, UPDATE_ROOT_DIR);
|
2022-05-06 16:26:25 +00:00
|
|
|
if(update_index == UPDATE_OPERATION_ROOT_DIR_PACKAGE_MAGIC) {
|
2022-04-13 20:50:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
uint32_t iter_index = 0;
|
|
|
|
File* dir = storage_file_alloc(storage);
|
|
|
|
FileInfo fi = {0};
|
2022-05-11 09:45:01 +00:00
|
|
|
char* name_buffer = malloc(UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN);
|
2022-04-13 20:50:25 +00:00
|
|
|
do {
|
|
|
|
if(!storage_dir_open(dir, UPDATE_ROOT_DIR)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
while(storage_dir_read(dir, &fi, name_buffer, UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN)) {
|
2022-04-13 20:50:25 +00:00
|
|
|
if(++iter_index == update_index) {
|
|
|
|
found = true;
|
|
|
|
path_append(out_path, name_buffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
free(name_buffer);
|
|
|
|
storage_file_free(dir);
|
|
|
|
if(!found) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_reset(out_path);
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
#define UPDATE_FILE_POINTER_FN EXT_PATH(UPDATE_MANIFEST_POINTER_FILE_NAME)
|
2022-05-11 09:45:01 +00:00
|
|
|
#define UPDATE_MANIFEST_MAX_PATH_LEN 256u
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
bool update_operation_get_current_package_manifest_path(Storage* storage, FuriString* out_path) {
|
|
|
|
furi_string_reset(out_path);
|
2022-05-11 09:45:01 +00:00
|
|
|
if(storage_common_stat(storage, UPDATE_FILE_POINTER_FN, NULL) == FSE_OK) {
|
|
|
|
char* manifest_name_buffer = malloc(UPDATE_MANIFEST_MAX_PATH_LEN);
|
|
|
|
File* upd_file = NULL;
|
|
|
|
do {
|
|
|
|
upd_file = storage_file_alloc(storage);
|
|
|
|
if(!storage_file_open(
|
|
|
|
upd_file, UPDATE_FILE_POINTER_FN, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint16_t bytes_read =
|
|
|
|
storage_file_read(upd_file, manifest_name_buffer, UPDATE_MANIFEST_MAX_PATH_LEN);
|
|
|
|
if((bytes_read == 0) || (bytes_read == UPDATE_MANIFEST_MAX_PATH_LEN)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(storage_common_stat(storage, manifest_name_buffer, NULL) != FSE_OK) {
|
|
|
|
break;
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(out_path, manifest_name_buffer);
|
2022-05-11 09:45:01 +00:00
|
|
|
} while(0);
|
|
|
|
free(manifest_name_buffer);
|
|
|
|
storage_file_free(upd_file);
|
|
|
|
} else {
|
|
|
|
/* legacy, will be deprecated */
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* rtcpath;
|
|
|
|
rtcpath = furi_string_alloc();
|
2022-05-11 09:45:01 +00:00
|
|
|
do {
|
|
|
|
if(!update_operation_get_current_package_path_rtc(storage, rtcpath)) {
|
|
|
|
break;
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
path_concat(furi_string_get_cstr(rtcpath), UPDATE_MANIFEST_DEFAULT_NAME, out_path);
|
2022-05-11 09:45:01 +00:00
|
|
|
} while(0);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(rtcpath);
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
return !furi_string_empty(out_path);
|
2022-05-11 09:45:01 +00:00
|
|
|
}
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
static bool update_operation_persist_manifest_path(Storage* storage, const char* manifest_path) {
|
2022-12-26 12:13:30 +00:00
|
|
|
const size_t manifest_path_len = strlen(manifest_path);
|
2022-05-11 09:45:01 +00:00
|
|
|
furi_check(manifest_path && manifest_path_len);
|
|
|
|
bool success = false;
|
|
|
|
File* file = storage_file_alloc(storage);
|
|
|
|
do {
|
|
|
|
if(manifest_path_len >= UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN) {
|
|
|
|
break;
|
|
|
|
}
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
if(!storage_file_open(file, UPDATE_FILE_POINTER_FN, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(storage_file_write(file, manifest_path, manifest_path_len) != manifest_path_len) {
|
|
|
|
break;
|
|
|
|
}
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
success = true;
|
|
|
|
} while(0);
|
|
|
|
storage_file_free(file);
|
|
|
|
return success;
|
|
|
|
}
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
UpdatePrepareResult update_operation_prepare(const char* manifest_file_path) {
|
2022-07-04 13:36:12 +00:00
|
|
|
UpdatePrepareResult result = UpdatePrepareResultIntFull;
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-04-13 20:50:25 +00:00
|
|
|
UpdateManifest* manifest = update_manifest_alloc();
|
2022-05-11 09:45:01 +00:00
|
|
|
File* file = storage_file_alloc(storage);
|
|
|
|
|
2022-07-04 13:36:12 +00:00
|
|
|
uint64_t free_int_space;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* stage_path;
|
|
|
|
stage_path = furi_string_alloc();
|
2022-05-11 09:45:01 +00:00
|
|
|
do {
|
2022-07-26 12:21:51 +00:00
|
|
|
if((storage_common_fs_info(storage, STORAGE_INT_PATH_PREFIX, NULL, &free_int_space) !=
|
|
|
|
FSE_OK) ||
|
2022-07-04 13:36:12 +00:00
|
|
|
(free_int_space < UPDATE_MIN_INT_FREE_SPACE)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
if(storage_common_stat(storage, manifest_file_path, NULL) != FSE_OK) {
|
2022-07-04 13:36:12 +00:00
|
|
|
result = UpdatePrepareResultManifestFolderNotFound;
|
2022-05-11 09:45:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!update_manifest_init(manifest, manifest_file_path)) {
|
|
|
|
result = UpdatePrepareResultManifestInvalid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(manifest->manifest_version < UPDATE_OPERATION_MIN_MANIFEST_VERSION) {
|
|
|
|
result = UpdatePrepareResultOutdatedManifestVersion;
|
|
|
|
break;
|
|
|
|
}
|
2023-02-07 16:33:05 +00:00
|
|
|
/* Only compare hardware target if it is set - pre-production devices accept any firmware*/
|
|
|
|
if(furi_hal_version_get_hw_target() &&
|
|
|
|
(furi_hal_version_get_hw_target() != manifest->target)) {
|
2022-05-11 09:45:01 +00:00
|
|
|
result = UpdatePrepareResultTargetMismatch;
|
|
|
|
break;
|
|
|
|
}
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
path_extract_dirname(manifest_file_path, stage_path);
|
2022-10-05 15:15:23 +00:00
|
|
|
path_append(stage_path, furi_string_get_cstr(manifest->staged_loader_file));
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(!storage_file_open(
|
|
|
|
file, furi_string_get_cstr(stage_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
|
2022-05-11 09:45:01 +00:00
|
|
|
result = UpdatePrepareResultStageMissing;
|
|
|
|
break;
|
|
|
|
}
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
uint32_t crc = crc32_calc_file(file, NULL, NULL);
|
|
|
|
if(crc != manifest->staged_loader_crc) {
|
2022-04-13 20:50:25 +00:00
|
|
|
result = UpdatePrepareResultStageIntegrityError;
|
2022-05-11 09:45:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
if(!update_operation_persist_manifest_path(storage, manifest_file_path)) {
|
|
|
|
result = UpdatePrepareResultManifestPointerError;
|
|
|
|
break;
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
2022-05-11 09:45:01 +00:00
|
|
|
|
|
|
|
result = UpdatePrepareResultOK;
|
|
|
|
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePreUpdate);
|
|
|
|
} while(false);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(stage_path);
|
2022-05-11 09:45:01 +00:00
|
|
|
storage_file_free(file);
|
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
update_manifest_free(manifest);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool update_operation_is_armed() {
|
2022-05-06 16:26:25 +00:00
|
|
|
FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
|
2022-05-11 09:45:01 +00:00
|
|
|
const uint32_t rtc_upd_index =
|
|
|
|
furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-05-11 09:45:01 +00:00
|
|
|
const bool upd_fn_ptr_exists =
|
|
|
|
(storage_common_stat(storage, UPDATE_FILE_POINTER_FN, NULL) == FSE_OK);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-05-06 16:26:25 +00:00
|
|
|
return (boot_mode >= FuriHalRtcBootModePreUpdate) &&
|
|
|
|
(boot_mode <= FuriHalRtcBootModePostUpdate) &&
|
2022-05-11 09:45:01 +00:00
|
|
|
((rtc_upd_index != INT_MAX) || upd_fn_ptr_exists);
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_operation_disarm() {
|
|
|
|
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
|
2022-05-11 09:45:01 +00:00
|
|
|
furi_hal_rtc_set_register(FuriHalRtcRegisterUpdateFolderFSIndex, INT_MAX);
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-05-11 09:45:01 +00:00
|
|
|
storage_simply_remove(storage, UPDATE_FILE_POINTER_FN);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
}
|