[FL-2263] Flasher service & RAM exec (#1006)

* WIP on stripping fw
* Compact FW build - use RAM_EXEC=1 COMPACT=1 DEBUG=0
* Fixed uninitialized storage struct; small fixes to compact fw
* Flasher srv w/mocked flash ops
* Fixed typos & accomodated FFF changes
* Alternative fw startup branch
* Working load & jmp to RAM fw
* +manifest processing for stage loader; + crc verification for stage payload
* Fixed questionable code & potential leaks
* Lowered screen update rate; added radio stack update stubs; working dfu write
* Console EP with manifest & stage validation
* Added microtar lib; minor ui fixes for updater
* Removed microtar
* Removed mtar #2
* Added a better version of microtar
* TAR archive api; LFS backup & restore core
* Recursive backup/restore
* LFS worker thread
* Added system apps to loader - not visible in UI; full update process with restarts
* Typo fix
* Dropped BL & f6; tooling for updater WIP
* Minor py fixes
* Minor fixes to make it build after merge
* Ported flash workaround from BL + fixed visuals
* Minor cleanup
* Chmod + loader app search fix
* Python linter fix
* Removed usb stuff & float read support for staged loader == -10% of binary size
* Added backup/restore & update pb requests
* Added stub impl to RPC for backup/restore/update commands
* Reworked TAR to use borrowed Storage api; slightly reduced build size by removing `static string`; hidden update-related RPC behind defines
* Moved backup&restore to storage
* Fixed new message types
* Backup/restore/update RPC impl
* Moved furi_hal_crc to LL; minor fixes
* CRC HAL rework to LL
* Purging STM HAL
* Brought back minimal DFU boot mode (no gui); additional crc state checks
* Added splash screen, BROKEN usb function
* Clock init rework WIP
* Stripped graphics from DFU mode
* Temp fix for unused static fun
* WIP update picker - broken!
* Fixed UI
* Bumping version
* Fixed RTC setup
* Backup to update folder instead of ext root
* Removed unused scenes & more usb remnants from staged loader
* CI updates
* Fixed update bundle name
* Temporary restored USB handler
* Attempt to prevent .text corruption
* Comments on how I spent this Saturday
* Added update file icon
* Documentation updates
* Moved common code to lib folder
* Storage: more unit tests
* Storage: blocking dir open, differentiate file and dir when freed.
* Major refactoring; added input processing to updater to allow retrying on failures (not very useful prob). Added API for extraction of thread return value
* Removed re-init check for manifest
* Changed low-level path manipulation to toolbox/path.h; makefile cleanup; tiny fix in lint.py
* Increased update worker stack size
* Text fixes in backup CLI
* Displaying number of update stages to run; removed timeout in handling errors
* Bumping version
* Added thread cleanup for spawner thread
* Updated build targets to exclude firmware bundle from 'ALL'
* Fixed makefile for update_package; skipping VCP init for update mode (ugly)
* Switched github build from ALL to update_package
* Added +x for dist_update.sh
* Cli: add total heap size to "free" command
* Moved (RAM) suffix to build version instead of git commit no.
* DFU comment
* Some fixes suggested by clang-tidy
* Fixed recursive PREFIX macro
* Makefile: gather all new rules in updater namespace. FuriHal: rename bootloader to boot, isr safe delays
* Github: correct build target name in firmware build
* FuriHal: move target switch to boot
* Makefile: fix firmware flash
* Furi, FuriHal: move kernel start to furi, early init
* Drop bootloader related stuff
* Drop cube. Drop bootloader linker script.
* Renamed update_hl, moved constants to #defines
* Moved update-related boot mode to separate bitfield
* Reworked updater cli to single entry point; fixed crash on tar cleanup
* Added Python replacement for dist shell scripts
* Linter fixes for dist.py +x
* Fixes for environment suffix
* Dropped bash scripts
* Added dirty build flag to version structure & interfaces
* Version string escapes
* Fixed flag logic in dist.py; added support for App instances being imported and not terminating the whole program
* Fixed fw address in ReadMe.md
* Rpc: fix crash on double screen start
* Return back original boot behavior and fix jump to system bootloader
* Cleanup code, add error sequence for RTC
* Update firmware readme
* FuriHal: drop boot, restructure RTC registers usage and add header register check
* Furi goes first
* Toolchain: add ccache support
* Renamed update bundle dir

Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
hedger
2022-04-13 23:50:25 +03:00
committed by GitHub
parent a25552eb99
commit e02040107b
221 changed files with 4199 additions and 11704 deletions

View File

@@ -3,20 +3,20 @@
- `app-scened-template` - Scened template app library
- `app-template` - Template app library
- `callback-connector` - Callback connector library
- `common-api` - Common api declaration library
- `cyfral` - Cyfral library
- `drivers` - Drivers that we wrote
- `fatfs` - External storage file system
- `flipper_file` - Flipper File Format library
- `flipper_format` - Flipper File Format library
- `fnv1a-hash` - Fnv1a hash library
- `heatshrink` - Image compression library
- `infrared` - Infrared library
- `libusb_stm32` - STM32 USB library
- `littlefs` - Internal storage file system
- `micro-ecc` - Elyptic Curve Crpytography library
- `micro-ecc` - Elliptic Curve Crpytography library
- `microtar` - TAR archive support library
- `mlib` - Algorithms and containers
- `nanopb` - Nano Protobuf library
- `nfc_protocols` - Nfc protocols library
- `onewire` - One wire library
- `one_wire` - One wire library
- `qrcode` - Qr code generator library
- `ST25RFAL002` - ST253916 driver and NFC hal
- `STM32CubeWB` - STM32WB series cube package

View File

@@ -273,10 +273,12 @@ bool flipper_format_stream_write_value_line(Stream* stream, FlipperStreamWriteDa
const uint8_t* data = write_data->data;
string_printf(value, "%02X", data[i]);
}; break;
#ifndef FLIPPER_STREAM_LITE
case FlipperStreamValueFloat: {
const float* data = write_data->data;
string_printf(value, "%f", data[i]);
}; break;
#endif
case FlipperStreamValueInt32: {
const int32_t* data = write_data->data;
string_printf(value, "%" PRIi32, data[i]);
@@ -357,6 +359,7 @@ bool flipper_format_stream_read_value_line(
}
}
}; break;
#ifndef FLIPPER_STREAM_LITE
case FlipperStreamValueFloat: {
float* data = _data;
// newlib-nano does not have sscanf for floats
@@ -368,6 +371,7 @@ bool flipper_format_stream_read_value_line(
scan_values = 1;
}
}; break;
#endif
case FlipperStreamValueInt32: {
int32_t* data = _data;
scan_values = sscanf(string_get_cstr(value), "%" PRIi32, &data[i]);
@@ -503,4 +507,4 @@ bool flipper_format_stream_write_comment_cstr(Stream* stream, const char* data)
} while(false);
return result;
}
}

View File

@@ -123,3 +123,10 @@ C_SOURCES += $(wildcard $(LIB_DIR)/micro-ecc/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/one_wire/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/one_wire/*/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/one_wire/*/*/*.c)
# microtar
CFLAGS += -I$(LIB_DIR)/microtar/src
C_SOURCES += $(wildcard $(LIB_DIR)/microtar/src/*.c)
# Update-related common code
C_SOURCES += $(wildcard $(LIB_DIR)/update_util/*.c)

1
lib/microtar Submodule

Submodule lib/microtar added at 1e921369b2

View File

@@ -2,7 +2,7 @@
#include <furi_hal.h>
#define CYFRAL_DATA_SIZE sizeof(uint16_t)
#define CYFRAL_PERIOD (125 * instructions_per_us)
#define CYFRAL_PERIOD (125 * furi_hal_delay_instructions_per_microsecond())
#define CYFRAL_0_LOW (CYFRAL_PERIOD * 0.66f)
#define CYFRAL_0_HI (CYFRAL_PERIOD * 0.33f)
#define CYFRAL_1_LOW (CYFRAL_PERIOD * 0.33f)

View File

@@ -2,7 +2,7 @@
#include <furi_hal.h>
#define METAKOM_DATA_SIZE sizeof(uint32_t)
#define METAKOM_PERIOD (125 * instructions_per_us)
#define METAKOM_PERIOD (125 * furi_hal_delay_instructions_per_microsecond())
#define METAKOM_0_LOW (METAKOM_PERIOD * 0.33f)
#define METAKOM_0_HI (METAKOM_PERIOD * 0.66f)
#define METAKOM_1_LOW (METAKOM_PERIOD * 0.66f)

View File

@@ -104,7 +104,7 @@ static void cyfral_reset(void* context) {
cyfral->nibble = 0;
cyfral->data_valid = true;
cyfral->max_period = CYFRAL_MAX_PERIOD_US * instructions_per_us;
cyfral->max_period = CYFRAL_MAX_PERIOD_US * furi_hal_delay_instructions_per_microsecond();
}
static bool cyfral_process_bit(

View File

@@ -38,14 +38,14 @@ struct OneWireSlave {
uint32_t onewire_slave_wait_while_gpio_is(OneWireSlave* bus, uint32_t time, const bool pin_value) {
uint32_t start = DWT->CYCCNT;
uint32_t time_ticks = time * instructions_per_us;
uint32_t time_ticks = time * furi_hal_delay_instructions_per_microsecond();
uint32_t time_captured;
do {
time_captured = DWT->CYCCNT;
if(furi_hal_ibutton_pin_get_level() != pin_value) {
uint32_t remaining_time = time_ticks - (time_captured - start);
remaining_time /= instructions_per_us;
remaining_time /= furi_hal_delay_instructions_per_microsecond();
return remaining_time;
}
} while((time_captured - start) < time_ticks);
@@ -211,7 +211,7 @@ static void exti_cb(void* context) {
static uint32_t pulse_start = 0;
if(input_state) {
uint32_t pulse_length = (DWT->CYCCNT - pulse_start) / instructions_per_us;
uint32_t pulse_length = (DWT->CYCCNT - pulse_start) / furi_hal_delay_instructions_per_microsecond();
if(pulse_length >= OWS_RESET_MIN) {
if(pulse_length <= OWS_RESET_MAX) {
// reset cycle ok

View File

@@ -85,7 +85,7 @@ static void subghz_keystore_mess_with_iv(uint8_t* iv) {
// Sharing them will bring some discomfort to legal owners
// And potential legal action against you
// While you reading this code think about your own personal responsibility
asm volatile("nani: \n"
asm volatile("nani%=: \n"
"ldrd r0, r2, [%0, #0x0] \n"
"lsl r1, r0, #8 \n"
"lsl r3, r2, #8 \n"
@@ -601,4 +601,4 @@ bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t*
string_clear(str_temp);
return result;
}
}

View File

@@ -18,3 +18,43 @@ void path_extract_filename_no_ext(const char* path, string_t filename) {
string_mid(filename, start_position, end_position - start_position);
}
static inline void path_cleanup(string_t path) {
string_strim(path);
while(string_end_with_str_p(path, "/")) {
string_left(path, string_size(path) - 1);
}
}
void path_extract_basename(const char* path, string_t basename) {
string_set(basename, path);
path_cleanup(basename);
size_t pos = string_search_rchar(basename, '/');
if(pos != STRING_FAILURE) {
string_right(basename, pos);
}
}
void path_extract_dirname(const char* path, string_t dirname) {
string_set(dirname, path);
path_cleanup(dirname);
size_t pos = string_search_rchar(dirname, '/');
if(pos != STRING_FAILURE) {
string_left(dirname, pos);
}
}
void path_append(string_t path, const char* suffix) {
path_cleanup(path);
string_t suffix_str;
string_init_set(suffix_str, suffix);
string_strim(suffix_str);
string_strim(suffix_str, "/");
string_cat_printf(path, "/%s", string_get_cstr(suffix_str));
string_clear(suffix_str);
}
void path_concat(const char* path, const char* suffix, string_t out_path) {
string_set(out_path, path);
path_append(out_path, suffix);
}

View File

@@ -14,6 +14,39 @@ extern "C" {
*/
void path_extract_filename_no_ext(const char* path, string_t filename);
/**
* @brief Extract last path component
*
* @param path path string
* @param filename output string. Must be initialized before.
*/
void path_extract_basename(const char* path, string_t basename);
/**
* @brief Extract path, except for last component
*
* @param path path string
* @param filename output string. Must be initialized before.
*/
void path_extract_dirname(const char* path, string_t dirname);
/**
* @brief Appends new component to path, adding path delimiter
*
* @param path path string
* @param suffix path part to apply
*/
void path_append(string_t path, const char* suffix);
/**
* @brief Appends new component to path, adding path delimiter
*
* @param path first path part
* @param suffix second path part
* @param out_path output string to combine parts into. Must be initialized
*/
void path_concat(const char* path, const char* suffix, string_t out_path);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,306 @@
#include "tar_archive.h"
#include <microtar.h>
#include <storage/storage.h>
#include <furi.h>
#include <toolbox/path.h>
#define TAG "TarArch"
#define MAX_NAME_LEN 255
#define FILE_BLOCK_SIZE 512
#define FILE_OPEN_NTRIES 10
#define FILE_OPEN_RETRY_DELAY 25
typedef struct TarArchive {
Storage* storage;
mtar_t tar;
} TarArchive;
/* API WRAPPER */
static int mtar_storage_file_write(void* stream, const void* data, unsigned size) {
uint16_t bytes_written = storage_file_write(stream, data, size);
return (bytes_written == size) ? bytes_written : MTAR_EWRITEFAIL;
}
static int mtar_storage_file_read(void* stream, void* data, unsigned size) {
uint16_t bytes_read = storage_file_read(stream, data, size);
return (bytes_read == size) ? bytes_read : MTAR_EREADFAIL;
}
static int mtar_storage_file_seek(void* stream, unsigned offset) {
bool res = storage_file_seek(stream, offset, true);
return res ? MTAR_ESUCCESS : MTAR_ESEEKFAIL;
}
static int mtar_storage_file_close(void* stream) {
if(stream) {
storage_file_close(stream);
}
return MTAR_ESUCCESS;
}
const struct mtar_ops filesystem_ops = {
.read = mtar_storage_file_read,
.write = mtar_storage_file_write,
.seek = mtar_storage_file_seek,
.close = mtar_storage_file_close,
};
TarArchive* tar_archive_alloc(Storage* storage) {
furi_check(storage);
TarArchive* archive = malloc(sizeof(TarArchive));
archive->storage = storage;
return archive;
}
bool tar_archive_open(TarArchive* archive, const char* path, TarOpenMode mode) {
furi_assert(archive);
FS_AccessMode access_mode;
FS_OpenMode open_mode;
int mtar_access = 0;
switch(mode) {
case TAR_OPEN_MODE_READ:
mtar_access = MTAR_READ;
access_mode = FSAM_READ;
open_mode = FSOM_OPEN_EXISTING;
break;
case TAR_OPEN_MODE_WRITE:
mtar_access = MTAR_WRITE;
access_mode = FSAM_WRITE;
open_mode = FSOM_CREATE_ALWAYS;
break;
default:
return false;
}
File* stream = storage_file_alloc(archive->storage);
if(!storage_file_open(stream, path, access_mode, open_mode)) {
storage_file_free(stream);
return false;
}
mtar_init(&archive->tar, mtar_access, &filesystem_ops, stream);
return true;
}
void tar_archive_free(TarArchive* archive) {
furi_assert(archive);
if(mtar_is_open(&archive->tar)) {
mtar_close(&archive->tar);
}
}
bool tar_archive_dir_add_element(TarArchive* archive, const char* dirpath) {
furi_assert(archive);
return (mtar_write_dir_header(&archive->tar, dirpath) == MTAR_ESUCCESS);
}
bool tar_archive_finalize(TarArchive* archive) {
furi_assert(archive);
return (mtar_finalize(&archive->tar) == MTAR_ESUCCESS);
}
bool tar_archive_store_data(
TarArchive* archive,
const char* path,
const uint8_t* data,
const int32_t data_len) {
furi_assert(archive);
return (
tar_archive_file_add_header(archive, path, data_len) &&
tar_archive_file_add_data_block(archive, data, data_len) &&
tar_archive_file_finalize(archive));
}
bool tar_archive_file_add_header(TarArchive* archive, const char* path, const int32_t data_len) {
furi_assert(archive);
return (mtar_write_file_header(&archive->tar, path, data_len) == MTAR_ESUCCESS);
}
bool tar_archive_file_add_data_block(
TarArchive* archive,
const uint8_t* data_block,
const int32_t block_len) {
furi_assert(archive);
return (mtar_write_data(&archive->tar, data_block, block_len) == block_len);
}
bool tar_archive_file_finalize(TarArchive* archive) {
furi_assert(archive);
return (mtar_end_data(&archive->tar) == MTAR_ESUCCESS);
}
typedef struct {
TarArchive* archive;
const char* work_dir;
} TarArchiveDirectoryOpParams;
static int archive_extract_foreach_cb(mtar_t* tar, const mtar_header_t* header, void* param) {
TarArchiveDirectoryOpParams* op_params = param;
string_t fname;
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));
string_clear(fname);
return create_res ? 0 : -1;
}
if(header->type != MTAR_TREG) {
FURI_LOG_W(TAG, "not extracting unsupported type \"%s\"", header->name);
return 0;
}
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);
uint8_t* readbuf = malloc(FILE_BLOCK_SIZE);
bool failed = false;
uint8_t n_tries = FILE_OPEN_NTRIES;
do {
while(
(n_tries-- > 0) &&
!storage_file_open(out_file, string_get_cstr(fname), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
FURI_LOG_W(TAG, "Failed to open '%s', reties: %d", string_get_cstr(fname), n_tries);
osDelay(FILE_OPEN_RETRY_DELAY);
continue;
}
if(!storage_file_is_open(out_file)) {
failed = true;
break;
}
while(!mtar_eof_data(tar)) {
int32_t readcnt = mtar_read_data(tar, readbuf, FILE_BLOCK_SIZE);
if(!readcnt || !storage_file_write(out_file, readbuf, readcnt)) {
failed = true;
break;
}
}
} while(false);
storage_file_free(out_file);
free(readbuf);
string_clear(fname);
return failed ? -1 : 0;
}
bool tar_archive_unpack_to(TarArchive* archive, const char* destination) {
furi_assert(archive);
TarArchiveDirectoryOpParams param = {
.archive = archive,
.work_dir = destination,
};
FURI_LOG_I(TAG, "Restoring '%s'", destination);
return (mtar_foreach(&archive->tar, archive_extract_foreach_cb, &param) == MTAR_ESUCCESS);
};
bool tar_archive_add_file(
TarArchive* archive,
const char* fs_file_path,
const char* archive_fname,
const int32_t file_size) {
furi_assert(archive);
uint8_t* file_buffer = malloc(FILE_BLOCK_SIZE);
bool success = false;
File* src_file = storage_file_alloc(archive->storage);
uint8_t n_tries = FILE_OPEN_NTRIES;
do {
while((n_tries-- > 0) &&
!storage_file_open(src_file, fs_file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
FURI_LOG_W(TAG, "Failed to open '%s', reties: %d", fs_file_path, n_tries);
osDelay(FILE_OPEN_RETRY_DELAY);
continue;
}
if(!storage_file_is_open(src_file) ||
!tar_archive_file_add_header(archive, archive_fname, file_size)) {
break;
}
uint16_t bytes_read = 0;
while((bytes_read = storage_file_read(src_file, file_buffer, FILE_BLOCK_SIZE))) {
success = tar_archive_file_add_data_block(archive, file_buffer, bytes_read);
if(!success) {
break;
}
}
success = success && tar_archive_file_finalize(archive);
} while(false);
storage_file_free(src_file);
free(file_buffer);
return success;
}
bool tar_archive_add_dir(TarArchive* archive, const char* fs_full_path, const char* path_prefix) {
furi_assert(archive);
furi_check(path_prefix);
File* directory = storage_file_alloc(archive->storage);
FileInfo file_info;
FURI_LOG_I(TAG, "Backing up '%s', '%s'", fs_full_path, path_prefix);
char* name = malloc(MAX_NAME_LEN);
bool success = false;
do {
if(!storage_dir_open(directory, fs_full_path)) {
break;
}
while(true) {
if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
success = true; /* empty dir / no more files */
break;
}
string_t element_name, element_fs_abs_path;
string_init(element_name);
string_init(element_fs_abs_path);
path_concat(fs_full_path, name, element_fs_abs_path);
if(strlen(path_prefix)) {
path_concat(path_prefix, name, element_name);
} else {
string_init_set(element_name, name);
}
if(file_info.flags & FSF_DIRECTORY) {
success = tar_archive_dir_add_element(archive, string_get_cstr(element_name)) &&
tar_archive_add_dir(
archive,
string_get_cstr(element_fs_abs_path),
string_get_cstr(element_name));
} else {
success = tar_archive_add_file(
archive,
string_get_cstr(element_fs_abs_path),
string_get_cstr(element_name),
file_info.size);
}
string_clear(element_name);
string_clear(element_fs_abs_path);
if(!success) {
break;
}
}
} while(false);
free(name);
storage_file_free(directory);
return success;
}

View File

@@ -0,0 +1,59 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct TarArchive TarArchive;
typedef struct Storage Storage;
typedef enum {
TAR_OPEN_MODE_READ = 'r',
TAR_OPEN_MODE_WRITE = 'w',
TAR_OPEN_MODE_STDOUT = 's' /* to be implemented */
} TarOpenMode;
TarArchive* tar_archive_alloc(Storage* storage);
bool tar_archive_open(TarArchive* archive, const char* path, TarOpenMode mode);
void tar_archive_free(TarArchive* archive);
/* High-level API - assumes archive is open */
bool tar_archive_unpack_to(TarArchive* archive, const char* destination);
bool tar_archive_add_file(
TarArchive* archive,
const char* fs_file_path,
const char* archive_fname,
const int32_t file_size);
bool tar_archive_add_dir(TarArchive* archive, const char* fs_full_path, const char* path_prefix);
/* Low-level API */
bool tar_archive_dir_add_element(TarArchive* archive, const char* dirpath);
bool tar_archive_file_add_header(TarArchive* archive, const char* path, const int32_t data_len);
bool tar_archive_file_add_data_block(
TarArchive* archive,
const uint8_t* data_block,
const int32_t block_len);
bool tar_archive_file_finalize(TarArchive* archive);
bool tar_archive_store_data(
TarArchive* archive,
const char* path,
const uint8_t* data,
const int32_t data_len);
bool tar_archive_finalize(TarArchive* archive);
#ifdef __cplusplus
}
#endif

View File

@@ -7,6 +7,7 @@ struct Version {
const char* build_date;
const char* version;
const uint8_t target;
const bool build_is_dirty;
};
/* version of current running firmware (bootloader/flipper) */
@@ -15,8 +16,13 @@ static const Version version = {
.git_branch = GIT_BRANCH,
.git_branch_num = GIT_BRANCH_NUM,
.build_date = BUILD_DATE,
.version = VERSION,
.version = VERSION
#ifdef FURI_RAM_EXEC
" (RAM)"
#endif
,
.target = TARGET,
.build_is_dirty = BUILD_DIRTY,
};
const Version* version_get(void) {
@@ -43,6 +49,10 @@ const char* version_get_version(const Version* v) {
return v ? v->version : version.version;
}
const uint8_t version_get_target(const Version* v) {
uint8_t version_get_target(const Version* v) {
return v ? v->target : version.target;
}
bool version_get_dirty_flag(const Version* v) {
return v ? v->build_is_dirty : version.build_is_dirty;
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
@@ -70,8 +71,17 @@ const char* version_get_version(const Version* v);
*
* @return build date
*/
const uint8_t version_get_target(const Version* v);
uint8_t version_get_target(const Version* v);
/** Get flag indicating if this build is "dirty" (source code had uncommited changes)
*
* @param v pointer to Version data. NULL for currently running
* software.
*
* @return build date
*/
bool version_get_dirty_flag(const Version* v);
#ifdef __cplusplus
}
#endif
#endif

183
lib/update_util/dfu_file.c Normal file
View File

@@ -0,0 +1,183 @@
#include "dfu_file.h"
#include <furi_hal.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);
/* Last 4 bytes of DFU file = CRC of previous file contents, inverted
* If we calculate whole file CRC32, incl. embedded CRC,
* that should give us 0xFFFFFFFF
*/
return file_crc == VALID_WHOLE_FILE_CRC;
}
uint8_t dfu_file_validate_headers(File* dfuf, const DfuValidationParams* reference_params) {
furi_assert(reference_params);
DfuPrefix dfu_prefix = {0};
DfuSuffix dfu_suffix = {0};
uint16_t bytes_read = 0;
if(!storage_file_is_open(dfuf) || !storage_file_seek(dfuf, 0, true)) {
return 0;
}
const uint32_t dfu_suffix_offset = storage_file_size(dfuf) - sizeof(DfuSuffix);
bytes_read = storage_file_read(dfuf, &dfu_prefix, sizeof(DfuPrefix));
if(bytes_read != sizeof(DfuPrefix)) {
return 0;
}
if(memcmp(dfu_prefix.szSignature, DFU_SIGNATURE, sizeof(dfu_prefix.szSignature))) {
return 0;
}
if((dfu_prefix.bVersion != 1) || (dfu_prefix.DFUImageSize != dfu_suffix_offset)) {
return 0;
}
if(!storage_file_seek(dfuf, dfu_suffix_offset, true)) {
return 0;
}
bytes_read = storage_file_read(dfuf, &dfu_suffix, sizeof(DfuSuffix));
if(bytes_read != sizeof(DfuSuffix)) {
return 0;
}
if((dfu_suffix.bLength != sizeof(DfuSuffix)) || (dfu_suffix.bcdDFU != DFU_SUFFIX_VERSION)) {
return 0;
}
/* TODO: check DfuSignature?.. */
if((dfu_suffix.idVendor != reference_params->vendor) ||
(dfu_suffix.idProduct != reference_params->product) ||
(dfu_suffix.bcdDevice != reference_params->device)) {
return 0;
}
return dfu_prefix.bTargets;
}
/* Assumes file is open, valid and read pointer is set at the start of image data
*/
static DfuUpdateBlockResult dfu_file_perform_task_for_update_pages(
const DfuUpdateTask* task,
File* dfuf,
const ImageElementHeader* header) {
furi_assert(task);
furi_assert(header);
task->progress_cb(0, task->context);
const size_t FLASH_PAGE_SIZE = furi_hal_flash_get_page_size();
const size_t FLASH_PAGE_ALIGNMENT_MASK = FLASH_PAGE_SIZE - 1;
if((header->dwElementAddress & FLASH_PAGE_ALIGNMENT_MASK) != 0) {
/* start address is not aligned by page boundary -- we don't support that. Yet. */
return UpdateBlockResult_Failed;
}
if(task->address_cb && (!task->address_cb(header->dwElementAddress) ||
!task->address_cb(header->dwElementAddress + header->dwElementSize))) {
storage_file_seek(dfuf, header->dwElementSize, false);
task->progress_cb(100, task->context);
return UpdateBlockResult_Skipped;
}
uint8_t* fw_block = malloc(FLASH_PAGE_SIZE);
uint16_t bytes_read = 0;
uint32_t element_offs = 0;
while(element_offs < header->dwElementSize) {
uint32_t n_bytes_to_read = FLASH_PAGE_SIZE;
if((element_offs + n_bytes_to_read) > header->dwElementSize) {
n_bytes_to_read = header->dwElementSize - element_offs;
}
bytes_read = storage_file_read(dfuf, fw_block, n_bytes_to_read);
if(bytes_read == 0) {
break;
}
int16_t i_page = furi_hal_flash_get_page_number(header->dwElementAddress + element_offs);
if(i_page < 0) {
break;
}
if(!task->task_cb(i_page, fw_block, bytes_read)) {
break;
}
element_offs += bytes_read;
task->progress_cb(element_offs * 100 / header->dwElementSize, task->context);
}
free(fw_block);
return (element_offs == header->dwElementSize) ? UpdateBlockResult_OK :
UpdateBlockResult_Failed;
}
bool dfu_file_process_targets(const DfuUpdateTask* task, File* dfuf, const uint8_t n_targets) {
TargetPrefix target_prefix = {0};
ImageElementHeader image_element = {0};
uint16_t bytes_read = 0;
if(!storage_file_seek(dfuf, sizeof(DfuPrefix), true)) {
return UpdateBlockResult_Failed;
};
for(uint8_t i_target = 0; i_target < n_targets; ++i_target) {
bytes_read = storage_file_read(dfuf, &target_prefix, sizeof(TargetPrefix));
if(bytes_read != sizeof(TargetPrefix)) {
return UpdateBlockResult_Failed;
}
/* TODO: look into TargetPrefix and validate/filter?.. */
for(uint32_t i_element = 0; i_element < target_prefix.dwNbElements; ++i_element) {
bytes_read = storage_file_read(dfuf, &image_element, sizeof(ImageElementHeader));
if(bytes_read != sizeof(ImageElementHeader)) {
return UpdateBlockResult_Failed;
}
if(dfu_file_perform_task_for_update_pages(task, dfuf, &image_element) ==
UpdateBlockResult_Failed) {
return false;
}
}
}
return true;
}

View File

@@ -0,0 +1,40 @@
#pragma once
#include "dfu_headers.h"
#include <stdbool.h>
#include <storage/storage.h>
typedef enum {
UpdateBlockResult_Unknown,
UpdateBlockResult_OK,
UpdateBlockResult_Skipped,
UpdateBlockResult_Failed
} DfuUpdateBlockResult;
typedef bool (
*DfuPageTaskCb)(const uint8_t i_page, const uint8_t* update_block, uint16_t update_block_len);
typedef void (*DfuPageTaskProgressCb)(const uint8_t progress, void* context);
typedef bool (*DfuAddressValidationCb)(const size_t address);
typedef struct {
DfuPageTaskCb task_cb;
DfuPageTaskProgressCb progress_cb;
DfuAddressValidationCb address_cb;
void* context;
} DfuUpdateTask;
typedef struct {
uint16_t vendor;
uint16_t product;
uint16_t device;
} DfuValidationParams;
bool dfu_file_validate_crc(File* dfuf, const DfuPageTaskProgressCb progress_cb, void* context);
/* Returns number of valid targets from file header
* If file is invalid, returns 0
*/
uint8_t dfu_file_validate_headers(File* dfuf, const DfuValidationParams* reference_params);
bool dfu_file_process_targets(const DfuUpdateTask* task, File* dfuf, const uint8_t n_targets);

View File

@@ -0,0 +1,41 @@
#pragma once
#include <stdint.h>
#pragma pack(push, 1)
typedef struct {
char szSignature[5];
uint8_t bVersion;
uint32_t DFUImageSize;
uint8_t bTargets;
} DfuPrefix;
typedef struct {
uint16_t bcdDevice;
uint16_t idProduct;
uint16_t idVendor;
uint16_t bcdDFU;
uint8_t ucDfuSignature_U;
uint8_t ucDfuSignature_F;
uint8_t ucDfuSignature_D;
uint8_t bLength;
uint32_t dwCRC;
} DfuSuffix;
typedef struct {
char szSignature[6];
uint8_t bAlternateSetting;
uint8_t bTargetNamed;
uint8_t _pad[3];
char szTargetName[255];
uint32_t dwTargetSize;
uint32_t dwNbElements;
} TargetPrefix;
typedef struct {
uint32_t dwElementAddress;
uint32_t dwElementSize;
} ImageElementHeader;
#pragma pack(pop)

View File

@@ -0,0 +1,22 @@
#include "lfs_backup.h"
#include <toolbox/tar/tar_archive.h>
#define LFS_BACKUP_DEFAULT_LOCATION "/ext/" LFS_BACKUP_DEFAULT_FILENAME
bool lfs_backup_create(Storage* storage, const char* destination) {
const char* final_destination =
destination && strlen(destination) ? destination : LFS_BACKUP_DEFAULT_LOCATION;
return storage_int_backup(storage, final_destination) == FSE_OK;
}
bool lfs_backup_exists(Storage* storage, const char* source) {
const char* final_source = source && strlen(source) ? source : LFS_BACKUP_DEFAULT_LOCATION;
FileInfo fi;
return storage_common_stat(storage, final_source, &fi) == FSE_OK;
}
bool lfs_backup_unpack(Storage* storage, const char* source) {
const char* final_source = source && strlen(source) ? source : LFS_BACKUP_DEFAULT_LOCATION;
return storage_int_restore(storage, final_source) == FSE_OK;
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include <stdbool.h>
#include <storage/storage.h>
#define LFS_BACKUP_DEFAULT_FILENAME "backup.tar"
#ifdef __cplusplus
extern "C" {
#endif
bool lfs_backup_create(Storage* storage, const char* destination);
bool lfs_backup_exists(Storage* storage, const char* source);
bool lfs_backup_unpack(Storage* storage, const char* source);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,86 @@
#include "update_manifest.h"
#include <storage/storage.h>
#include <flipper_format/flipper_format.h>
#include <flipper_format/flipper_format_i.h>
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);
update_manifest->target = 0;
update_manifest->valid = false;
return update_manifest;
}
void update_manifest_free(UpdateManifest* update_manifest) {
furi_assert(update_manifest);
string_clear(update_manifest->version);
string_clear(update_manifest->firmware_dfu_image);
string_clear(update_manifest->radio_image);
string_clear(update_manifest->staged_loader_file);
free(update_manifest);
}
static bool
update_manifest_init_from_ff(UpdateManifest* update_manifest, FlipperFormat* flipper_file) {
furi_assert(update_manifest);
furi_assert(flipper_file);
string_t filetype;
uint32_t version = 0;
// TODO: compare filetype?
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_hex(
flipper_file,
"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));
return update_manifest->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);
if(flipper_format_file_open_existing(flipper_file, manifest_filename)) {
update_manifest_init_from_ff(update_manifest, flipper_file);
}
flipper_format_free(flipper_file);
furi_record_close("storage");
return update_manifest->valid;
}
bool update_manifest_init_mem(
UpdateManifest* update_manifest,
const uint8_t* manifest_data,
const uint16_t length) {
FlipperFormat* flipper_file = flipper_format_string_alloc();
Stream* sstream = flipper_format_get_raw_stream(flipper_file);
stream_write(sstream, manifest_data, length);
stream_seek(sstream, 0, StreamOffsetFromStart);
update_manifest_init_from_ff(update_manifest, flipper_file);
flipper_format_free(flipper_file);
return update_manifest->valid;
}

View File

@@ -0,0 +1,40 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include <m-string.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 struct {
string_t version;
uint32_t target;
string_t staged_loader_file;
uint32_t staged_loader_crc;
string_t firmware_dfu_image;
string_t radio_image;
uint32_t radio_address;
bool valid;
} UpdateManifest;
UpdateManifest* update_manifest_alloc();
void update_manifest_free(UpdateManifest* update_manifest);
bool update_manifest_init(UpdateManifest* update_manifest, const char* manifest_filename);
bool update_manifest_init_mem(
UpdateManifest* update_manifest,
const uint8_t* manifest_data,
const uint16_t length);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,207 @@
#include "update_operation.h"
#include "update_manifest.h"
#include <furi.h>
#include <furi_hal.h>
#include <m-string.h>
#include <loader/loader.h>
#include <lib/toolbox/path.h>
static const char* UPDATE_ROOT_DIR = "/ext" UPDATE_DIR_DEFAULT_REL_PATH;
static const char* UPDATE_PREFIX = "/ext" UPDATE_DIR_DEFAULT_REL_PATH "/";
static const char* UPDATE_SUFFIX = "/" UPDATE_MANIFEST_DEFAULT_NAME;
static const uint32_t MAX_DIR_NAME_LEN = 250;
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",
};
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];
}
}
bool update_operation_get_package_dir_name(const char* full_path, string_t out_manifest_dir) {
bool path_ok = false;
string_t full_path_str;
string_init_set(full_path_str, full_path);
string_reset(out_manifest_dir);
bool start_end_ok = string_start_with_str_p(full_path_str, UPDATE_PREFIX) &&
string_end_with_str_p(full_path_str, UPDATE_SUFFIX);
int16_t dir_name_len =
strlen(full_path) - strlen(UPDATE_PREFIX) - strlen(UPDATE_MANIFEST_DEFAULT_NAME) - 1;
if(dir_name_len == -1) {
path_ok = true;
} else if(start_end_ok && (dir_name_len > 0)) {
string_set_n(out_manifest_dir, full_path_str, strlen(UPDATE_PREFIX), dir_name_len);
path_ok = true;
if(string_search_char(out_manifest_dir, '/') != STRING_FAILURE) {
string_reset(out_manifest_dir);
path_ok = false;
}
}
string_clear(full_path_str);
return path_ok;
}
int32_t update_operation_get_package_index(Storage* storage, const char* update_package_dir) {
furi_assert(storage);
furi_assert(update_package_dir);
if(strlen(update_package_dir) == 0) {
return 0;
}
bool found = false;
int32_t index = 0;
File* dir = storage_file_alloc(storage);
FileInfo fi = {0};
char* name_buffer = malloc(MAX_DIR_NAME_LEN);
do {
if(!storage_dir_open(dir, UPDATE_ROOT_DIR)) {
break;
}
while(storage_dir_read(dir, &fi, name_buffer, MAX_DIR_NAME_LEN)) {
index++;
if(strcmp(name_buffer, update_package_dir)) {
continue;
} else {
found = true;
break;
}
}
} while(false);
free(name_buffer);
storage_file_free(dir);
return found ? index : -1;
}
bool update_operation_get_current_package_path(Storage* storage, string_t out_path) {
uint32_t update_index = furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
string_set_str(out_path, UPDATE_ROOT_DIR);
if(update_index == 0) {
return true;
}
bool found = false;
uint32_t iter_index = 0;
File* dir = storage_file_alloc(storage);
FileInfo fi = {0};
char* name_buffer = malloc(MAX_DIR_NAME_LEN);
do {
if(!storage_dir_open(dir, UPDATE_ROOT_DIR)) {
break;
}
while(storage_dir_read(dir, &fi, name_buffer, MAX_DIR_NAME_LEN)) {
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) {
string_reset(out_path);
}
return found;
}
UpdatePrepareResult update_operation_prepare(const char* manifest_file_path) {
string_t update_folder;
string_init(update_folder);
if(!update_operation_get_package_dir_name(manifest_file_path, update_folder)) {
string_clear(update_folder);
return UpdatePrepareResultManifestPathInvalid;
}
Storage* storage = furi_record_open("storage");
int32_t update_index =
update_operation_get_package_index(storage, string_get_cstr(update_folder));
string_clear(update_folder);
if(update_index < 0) {
furi_record_close("storage");
return UpdatePrepareResultManifestFolderNotFound;
}
string_t update_dir_path;
string_init(update_dir_path);
path_extract_dirname(manifest_file_path, update_dir_path);
UpdatePrepareResult result = UpdatePrepareResultManifestInvalid;
UpdateManifest* manifest = update_manifest_alloc();
if(update_manifest_init(manifest, manifest_file_path)) {
result = UpdatePrepareResultStageMissing;
File* file = storage_file_alloc(storage);
string_t stage_path;
string_init(stage_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(
file, string_get_cstr(stage_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
break;
}
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();
} while(false);
string_clear(stage_path);
free(read_buffer);
storage_file_free(file);
if(crc == manifest->staged_loader_crc) {
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePreUpdate);
update_operation_persist_package_index(update_index);
result = UpdatePrepareResultOK;
}
}
furi_record_close("storage");
update_manifest_free(manifest);
return result;
}
bool update_operation_is_armed() {
return furi_hal_rtc_get_boot_mode() == FuriHalRtcBootModePreUpdate;
}
void update_operation_disarm() {
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
furi_hal_rtc_set_register(FuriHalRtcRegisterUpdateFolderFSIndex, 0);
}
void update_operation_persist_package_index(uint32_t index) {
furi_hal_rtc_set_register(FuriHalRtcRegisterUpdateFolderFSIndex, index);
}

View File

@@ -0,0 +1,72 @@
#pragma once
#include <stdbool.h>
#include <m-string.h>
#include <storage/storage.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Checks if supplied full manifest path is valid
* @param full_path Full path to manifest file. Must be named UPDATE_MANIFEST_DEFAULT_NAME
* @param out_manifest_dir Directory to apply update from, if supplied path is valid.
* May be empty if update is in root update directory
* @return bool if supplied path is valid and out_manifest_dir contains dir to apply
*/
bool update_operation_get_package_dir_name(const char* full_path, string_t out_manifest_dir);
typedef enum {
UpdatePrepareResultOK,
UpdatePrepareResultManifestPathInvalid,
UpdatePrepareResultManifestFolderNotFound,
UpdatePrepareResultManifestInvalid,
UpdatePrepareResultStageMissing,
UpdatePrepareResultStageIntegrityError,
} UpdatePrepareResult;
const char* update_operation_describe_preparation_result(const UpdatePrepareResult value);
/*
* Validates next stage and sets up registers to apply update after restart
* @param manifest_dir_path Full path to manifest for update to apply
* @return UpdatePrepareResult validation & arm result
*/
UpdatePrepareResult update_operation_prepare(const char* manifest_file_path);
/*
* Gets update package index to pass in RTC registers
* @param storage Storage API
* @param update_package_dir Package directory name
* @return int32_t <0 - error, >= 0 - update index value
*/
int32_t update_operation_get_package_index(Storage* storage, const char* update_package_dir);
/*
* Gets filesystem path for current update package
* @param storage Storage API
* @param out_path Path to directory with manifest & related files. Must be initialized
* @return true if path was restored successfully
*/
bool update_operation_get_current_package_path(Storage* storage, string_t out_path);
/*
* Stores given update index in RTC registers
* @param index Value to store
*/
void update_operation_persist_package_index(uint32_t index);
/*
* Sets up update operation to be performed on reset
*/
bool update_operation_is_armed();
/*
* Cancels pending update operation
*/
void update_operation_disarm();
#ifdef __cplusplus
}
#endif