[FL-3097] fbt, faploader: minimal app module implementation (#2420)
* fbt, faploader: minimal app module implementation * faploader, libs: moved API hashtable core to flipper_application * example: compound api * lib: flipper_application: naming fixes, doxygen comments * fbt: changed `requires` manifest field behavior for app extensions * examples: refactored plugin apps; faploader: changed new API naming; fbt: changed PLUGIN app type meaning * loader: dropped support for debug apps & plugin menus * moved applications/plugins -> applications/external * Restored x bit on chiplist_convert.py * git: fixed free-dap submodule path * pvs: updated submodule paths * examples: example_advanced_plugins.c: removed potential memory leak on errors * examples: example_plugins: refined requires * fbt: not deploying app modules for debug/sample apps; extra validation for .PLUGIN-type apps * apps: removed cdefines for external apps * fbt: moved ext app path definition * fbt: reworked fap_dist handling; f18: synced api_symbols.csv * fbt: removed resources_paths for extapps * scripts: reworked storage * scripts: reworked runfap.py & selfupdate.py to use new api * wip: fal runner * fbt: moved file packaging into separate module * scripts: storage: fixes * scripts: storage: minor fixes for new api * fbt: changed internal artifact storage details for external apps * scripts: storage: additional fixes and better error reporting; examples: using APP_DATA_PATH() * fbt, scripts: reworked launch_app to deploy plugins; moved old runfap.py to distfap.py * fbt: extra check for plugins descriptors * fbt: additional checks in emitter * fbt: better info message on SDK rebuild * scripts: removed requirements.txt * loader: removed remnants of plugins & debug menus * post-review fixes
This commit is contained in:
		| @@ -7,6 +7,7 @@ App( | ||||
|     requires=[ | ||||
|         "gui", | ||||
|         "storage", | ||||
|         "loader", | ||||
|     ], | ||||
|     stack_size=int(1.5 * 1024), | ||||
|     icon="A_Plugins_14", | ||||
|   | ||||
| @@ -1,111 +0,0 @@ | ||||
| /** | ||||
|  * 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)...}; | ||||
| } | ||||
| @@ -1,48 +0,0 @@ | ||||
| #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, "Can't find symbol '%s' (hash %lx)!", 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, | ||||
| }; | ||||
| @@ -1,14 +0,0 @@ | ||||
| #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 | ||||
| @@ -1,18 +0,0 @@ | ||||
| /** | ||||
|  * 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; | ||||
| } | ||||
| @@ -1,41 +0,0 @@ | ||||
| #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 | ||||
| @@ -7,7 +7,7 @@ | ||||
| #include <dialogs/dialogs.h> | ||||
| #include <toolbox/path.h> | ||||
| #include <flipper_application/flipper_application.h> | ||||
| #include "elf_cpp/elf_hashtable.h" | ||||
| #include <loader/firmware_api/firmware_api.h> | ||||
| #include "fap_loader_app.h" | ||||
|  | ||||
| #define TAG "fap_loader_app" | ||||
| @@ -27,7 +27,7 @@ bool fap_loader_load_name_and_icon( | ||||
|     Storage* storage, | ||||
|     uint8_t** icon_ptr, | ||||
|     FuriString* item_name) { | ||||
|     FlipperApplication* app = flipper_application_alloc(storage, &hashtable_api_interface); | ||||
|     FlipperApplication* app = flipper_application_alloc(storage, firmware_api_interface); | ||||
|  | ||||
|     FlipperApplicationPreloadStatus preload_res = | ||||
|         flipper_application_preload_manifest(app, furi_string_get_cstr(path)); | ||||
| @@ -71,7 +71,7 @@ static bool fap_loader_run_selected_app(FapLoader* loader) { | ||||
|     bool show_error = true; | ||||
|     do { | ||||
|         file_selected = true; | ||||
|         loader->app = flipper_application_alloc(loader->storage, &hashtable_api_interface); | ||||
|         loader->app = flipper_application_alloc(loader->storage, firmware_api_interface); | ||||
|         size_t start = furi_get_tick(); | ||||
|  | ||||
|         FURI_LOG_I(TAG, "FAP Loader is loading %s", furi_string_get_cstr(loader->fap_path)); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user