[FL-2627] Flipper applications: SDK, build and debug system (#1387)
* Added support for running applications from SD card (FAPs - Flipper Application Packages) * Added plugin_dist target for fbt to build FAPs * All apps of type FlipperAppType.EXTERNAL and FlipperAppType.PLUGIN are built as FAPs by default * Updated VSCode configuration for new fbt features - re-deploy stock configuration to use them * Added debugging support for FAPs with fbt debug & VSCode * Added public firmware API with automated versioning Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: SG <who.just.the.doctor@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
13
applications/main/fap_loader/application.fam
Normal file
13
applications/main/fap_loader/application.fam
Normal file
@@ -0,0 +1,13 @@
|
||||
App(
|
||||
appid="fap_loader",
|
||||
name="Applications",
|
||||
apptype=FlipperAppType.APP,
|
||||
entry_point="fap_loader_app",
|
||||
requires=[
|
||||
"gui",
|
||||
"storage",
|
||||
],
|
||||
stack_size=int(1.5 * 1024),
|
||||
icon="A_Plugins_14",
|
||||
order=90,
|
||||
)
|
111
applications/main/fap_loader/elf_cpp/compilesort.hpp
Normal file
111
applications/main/fap_loader/elf_cpp/compilesort.hpp
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Implementation of compile-time sort for symbol table entries.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iterator>
|
||||
#include <array>
|
||||
|
||||
namespace cstd {
|
||||
|
||||
template <typename RAIt>
|
||||
constexpr RAIt next(RAIt it, typename std::iterator_traits<RAIt>::difference_type n = 1) {
|
||||
return it + n;
|
||||
}
|
||||
|
||||
template <typename RAIt>
|
||||
constexpr auto distance(RAIt first, RAIt last) {
|
||||
return last - first;
|
||||
}
|
||||
|
||||
template <class ForwardIt1, class ForwardIt2>
|
||||
constexpr void iter_swap(ForwardIt1 a, ForwardIt2 b) {
|
||||
auto temp = std::move(*a);
|
||||
*a = std::move(*b);
|
||||
*b = std::move(temp);
|
||||
}
|
||||
|
||||
template <class InputIt, class UnaryPredicate>
|
||||
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q) {
|
||||
for(; first != last; ++first) {
|
||||
if(!q(*first)) {
|
||||
return first;
|
||||
}
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
template <class ForwardIt, class UnaryPredicate>
|
||||
constexpr ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p) {
|
||||
first = cstd::find_if_not(first, last, p);
|
||||
if(first == last) return first;
|
||||
|
||||
for(ForwardIt i = cstd::next(first); i != last; ++i) {
|
||||
if(p(*i)) {
|
||||
cstd::iter_swap(i, first);
|
||||
++first;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template <class RAIt, class Compare = std::less<> >
|
||||
constexpr void quick_sort(RAIt first, RAIt last, Compare cmp = Compare{}) {
|
||||
auto const N = cstd::distance(first, last);
|
||||
if(N <= 1) return;
|
||||
auto const pivot = *cstd::next(first, N / 2);
|
||||
auto const middle1 =
|
||||
cstd::partition(first, last, [=](auto const& elem) { return cmp(elem, pivot); });
|
||||
auto const middle2 =
|
||||
cstd::partition(middle1, last, [=](auto const& elem) { return !cmp(pivot, elem); });
|
||||
quick_sort(first, middle1, cmp); // assert(std::is_sorted(first, middle1, cmp));
|
||||
quick_sort(middle2, last, cmp); // assert(std::is_sorted(middle2, last, cmp));
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
constexpr auto sort(Range&& range) {
|
||||
quick_sort(std::begin(range), std::end(range));
|
||||
return range;
|
||||
}
|
||||
|
||||
template <typename V, typename... T>
|
||||
constexpr auto array_of(T&&... t) -> std::array<V, sizeof...(T)> {
|
||||
return {{std::forward<T>(t)...}};
|
||||
}
|
||||
|
||||
template <typename T, typename... N>
|
||||
constexpr auto my_make_array(N&&... args) -> std::array<T, sizeof...(args)> {
|
||||
return {std::forward<N>(args)...};
|
||||
}
|
||||
|
||||
namespace traits {
|
||||
template <typename T, typename... Ts>
|
||||
struct array_type {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
static constexpr bool are_same_type() {
|
||||
return std::conjunction_v<std::is_same<T, Ts>...>;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
constexpr auto create_array(const T&&... values) {
|
||||
using array_type = typename traits::array_type<T...>::type;
|
||||
static_assert(sizeof...(T) > 0, "an array must have at least one element");
|
||||
static_assert(traits::are_same_type<T...>(), "all elements must have same type");
|
||||
return std::array<array_type, sizeof...(T)>{values...};
|
||||
}
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
constexpr auto create_array_t(const Ts&&... values) {
|
||||
using array_type = T;
|
||||
static_assert(sizeof...(Ts) > 0, "an array must have at least one element");
|
||||
static_assert(traits::are_same_type<Ts...>(), "all elements must have same type");
|
||||
return std::array<array_type, sizeof...(Ts)>{static_cast<T>(values)...};
|
||||
}
|
48
applications/main/fap_loader/elf_cpp/elf_hashtable.cpp
Normal file
48
applications/main/fap_loader/elf_cpp/elf_hashtable.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "compilesort.hpp"
|
||||
#include "elf_hashtable.h"
|
||||
#include "elf_hashtable_entry.h"
|
||||
#include "elf_hashtable_checks.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <algorithm>
|
||||
|
||||
/* Generated table */
|
||||
#include <symbols.h>
|
||||
|
||||
#define TAG "elf_hashtable"
|
||||
|
||||
static_assert(!has_hash_collisions(elf_api_table), "Detected API method hash collision!");
|
||||
|
||||
/**
|
||||
* Get function address by function name
|
||||
* @param name function name
|
||||
* @param address output for function address
|
||||
* @return true if the table contains a function
|
||||
*/
|
||||
|
||||
bool elf_resolve_from_hashtable(const char* name, Elf32_Addr* address) {
|
||||
bool result = false;
|
||||
uint32_t gnu_sym_hash = elf_gnu_hash(name);
|
||||
|
||||
sym_entry key = {
|
||||
.hash = gnu_sym_hash,
|
||||
.address = 0,
|
||||
};
|
||||
|
||||
auto find_res = std::lower_bound(elf_api_table.cbegin(), elf_api_table.cend(), key);
|
||||
if((find_res == elf_api_table.cend() || (find_res->hash != gnu_sym_hash))) {
|
||||
FURI_LOG_W(TAG, "Cant find symbol '%s' (hash %x)!", name, gnu_sym_hash);
|
||||
result = false;
|
||||
} else {
|
||||
result = true;
|
||||
*address = find_res->address;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const ElfApiInterface hashtable_api_interface = {
|
||||
.api_version_major = (elf_api_version >> 16),
|
||||
.api_version_minor = (elf_api_version & 0xFFFF),
|
||||
.resolver_callback = &elf_resolve_from_hashtable,
|
||||
};
|
14
applications/main/fap_loader/elf_cpp/elf_hashtable.h
Normal file
14
applications/main/fap_loader/elf_cpp/elf_hashtable.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#include <flipper_application/elf/elf_api_interface.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern const ElfApiInterface hashtable_api_interface;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Check for multiple entries with the same hash value at compilation time.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include "elf_hashtable_entry.h"
|
||||
|
||||
template <std::size_t N>
|
||||
constexpr bool has_hash_collisions(const std::array<sym_entry, N> api_methods) {
|
||||
for(std::size_t i = 0; i < (N - 1); ++i) {
|
||||
if(api_methods[i].hash == api_methods[i + 1].hash) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
41
applications/main/fap_loader/elf_cpp/elf_hashtable_entry.h
Normal file
41
applications/main/fap_loader/elf_cpp/elf_hashtable_entry.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct sym_entry {
|
||||
uint32_t hash;
|
||||
uint32_t address;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
#include <array>
|
||||
#include <algorithm>
|
||||
|
||||
#define API_METHOD(x, ret_type, args_type) \
|
||||
sym_entry { \
|
||||
.hash = elf_gnu_hash(#x), .address = (uint32_t)(static_cast<ret_type(*) args_type>(x)) \
|
||||
}
|
||||
|
||||
#define API_VARIABLE(x, var_type) \
|
||||
sym_entry { \
|
||||
.hash = elf_gnu_hash(#x), .address = (uint32_t)(&(x)), \
|
||||
}
|
||||
|
||||
constexpr bool operator<(const sym_entry& k1, const sym_entry& k2) {
|
||||
return k1.hash < k2.hash;
|
||||
}
|
||||
|
||||
constexpr uint32_t elf_gnu_hash(const char* s) {
|
||||
uint32_t h = 0x1505;
|
||||
for(unsigned char c = *s; c != '\0'; c = *++s) {
|
||||
h = (h << 5) + h + c;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
#endif
|
176
applications/main/fap_loader/fap_loader_app.c
Normal file
176
applications/main/fap_loader/fap_loader_app.c
Normal file
@@ -0,0 +1,176 @@
|
||||
#include <furi.h>
|
||||
#include <gui/gui.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
#include <gui/modules/loading.h>
|
||||
#include <storage/storage.h>
|
||||
#include <dialogs/dialogs.h>
|
||||
#include "elf_cpp/elf_hashtable.h"
|
||||
#include <flipper_application/flipper_application.h>
|
||||
|
||||
#define TAG "fap_loader_app"
|
||||
|
||||
typedef struct {
|
||||
FlipperApplication* app;
|
||||
Storage* storage;
|
||||
DialogsApp* dialogs;
|
||||
Gui* gui;
|
||||
string_t fap_path;
|
||||
} FapLoader;
|
||||
|
||||
static bool
|
||||
fap_loader_item_callback(string_t path, void* context, uint8_t** icon_ptr, string_t item_name) {
|
||||
FapLoader* loader = context;
|
||||
furi_assert(loader);
|
||||
|
||||
FlipperApplication* app = flipper_application_alloc(loader->storage, &hashtable_api_interface);
|
||||
|
||||
FlipperApplicationPreloadStatus preload_res =
|
||||
flipper_application_preload(app, string_get_cstr(path));
|
||||
|
||||
bool load_success = false;
|
||||
|
||||
if(preload_res == FlipperApplicationPreloadStatusSuccess) {
|
||||
const FlipperApplicationManifest* manifest = flipper_application_get_manifest(app);
|
||||
if(manifest->has_icon) {
|
||||
memcpy(*icon_ptr, manifest->icon, FAP_MANIFEST_MAX_ICON_SIZE);
|
||||
}
|
||||
string_set_str(item_name, manifest->name);
|
||||
load_success = true;
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "FAP Loader failed to preload %s", string_get_cstr(path));
|
||||
load_success = false;
|
||||
}
|
||||
|
||||
flipper_application_free(app);
|
||||
return load_success;
|
||||
}
|
||||
|
||||
static bool fap_loader_run_selected_app(FapLoader* loader) {
|
||||
furi_assert(loader);
|
||||
|
||||
string_t error_message;
|
||||
|
||||
string_init_set(error_message, "unknown error");
|
||||
|
||||
bool file_selected = false;
|
||||
bool show_error = true;
|
||||
do {
|
||||
file_selected = true;
|
||||
loader->app = flipper_application_alloc(loader->storage, &hashtable_api_interface);
|
||||
|
||||
FURI_LOG_I(TAG, "FAP Loader is loading %s", string_get_cstr(loader->fap_path));
|
||||
|
||||
FlipperApplicationPreloadStatus preload_res =
|
||||
flipper_application_preload(loader->app, string_get_cstr(loader->fap_path));
|
||||
if(preload_res != FlipperApplicationPreloadStatusSuccess) {
|
||||
const char* err_msg = flipper_application_preload_status_to_string(preload_res);
|
||||
string_printf(error_message, "Preload failed: %s", err_msg);
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"FAP Loader failed to preload %s: %s",
|
||||
string_get_cstr(loader->fap_path),
|
||||
err_msg);
|
||||
break;
|
||||
}
|
||||
|
||||
FURI_LOG_I(TAG, "FAP Loader is mapping");
|
||||
FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(loader->app);
|
||||
if(load_status != FlipperApplicationLoadStatusSuccess) {
|
||||
const char* err_msg = flipper_application_load_status_to_string(load_status);
|
||||
string_printf(error_message, "Load failed: %s", err_msg);
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"FAP Loader failed to map to memory %s: %s",
|
||||
string_get_cstr(loader->fap_path),
|
||||
err_msg);
|
||||
break;
|
||||
}
|
||||
|
||||
FURI_LOG_I(TAG, "FAP Loader is staring app");
|
||||
|
||||
FuriThread* thread = flipper_application_spawn(loader->app, NULL);
|
||||
furi_thread_start(thread);
|
||||
furi_thread_join(thread);
|
||||
|
||||
show_error = false;
|
||||
int ret = furi_thread_get_return_code(thread);
|
||||
|
||||
FURI_LOG_I(TAG, "FAP app returned: %i", ret);
|
||||
} while(0);
|
||||
|
||||
if(show_error) {
|
||||
DialogMessage* message = dialog_message_alloc();
|
||||
dialog_message_set_header(message, "Error", 64, 0, AlignCenter, AlignTop);
|
||||
dialog_message_set_buttons(message, NULL, NULL, NULL);
|
||||
|
||||
string_t buffer;
|
||||
string_init(buffer);
|
||||
string_printf(buffer, "%s", string_get_cstr(error_message));
|
||||
string_replace_str(buffer, ":", "\n");
|
||||
dialog_message_set_text(
|
||||
message, string_get_cstr(buffer), 64, 32, AlignCenter, AlignCenter);
|
||||
|
||||
dialog_message_show(loader->dialogs, message);
|
||||
dialog_message_free(message);
|
||||
string_clear(buffer);
|
||||
}
|
||||
|
||||
string_clear(error_message);
|
||||
|
||||
if(file_selected) {
|
||||
flipper_application_free(loader->app);
|
||||
}
|
||||
|
||||
return file_selected;
|
||||
}
|
||||
|
||||
static bool fap_loader_select_app(FapLoader* loader) {
|
||||
const DialogsFileBrowserOptions browser_options = {
|
||||
.extension = ".fap",
|
||||
.skip_assets = true,
|
||||
.icon = &I_badusb_10px,
|
||||
.hide_ext = true,
|
||||
.item_loader_callback = fap_loader_item_callback,
|
||||
.item_loader_context = loader,
|
||||
};
|
||||
|
||||
return dialog_file_browser_show(
|
||||
loader->dialogs, loader->fap_path, loader->fap_path, &browser_options);
|
||||
}
|
||||
|
||||
int32_t fap_loader_app(void* p) {
|
||||
FapLoader* loader = malloc(sizeof(FapLoader));
|
||||
loader->storage = furi_record_open(RECORD_STORAGE);
|
||||
loader->dialogs = furi_record_open(RECORD_DIALOGS);
|
||||
loader->gui = furi_record_open(RECORD_GUI);
|
||||
|
||||
ViewDispatcher* view_dispatcher = view_dispatcher_alloc();
|
||||
Loading* loading = loading_alloc();
|
||||
|
||||
view_dispatcher_enable_queue(view_dispatcher);
|
||||
view_dispatcher_attach_to_gui(view_dispatcher, loader->gui, ViewDispatcherTypeFullscreen);
|
||||
view_dispatcher_add_view(view_dispatcher, 0, loading_get_view(loading));
|
||||
|
||||
if(p) {
|
||||
string_init_set(loader->fap_path, (const char*)p);
|
||||
fap_loader_run_selected_app(loader);
|
||||
} else {
|
||||
string_init_set(loader->fap_path, EXT_PATH("apps"));
|
||||
|
||||
while(fap_loader_select_app(loader)) {
|
||||
view_dispatcher_switch_to_view(view_dispatcher, 0);
|
||||
fap_loader_run_selected_app(loader);
|
||||
};
|
||||
}
|
||||
|
||||
view_dispatcher_remove_view(view_dispatcher, 0);
|
||||
loading_free(loading);
|
||||
view_dispatcher_free(view_dispatcher);
|
||||
|
||||
string_clear(loader->fap_path);
|
||||
furi_record_close(RECORD_GUI);
|
||||
furi_record_close(RECORD_DIALOGS);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
free(loader);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user