Merge branch 'ul-dev' into xfw-dev

This commit is contained in:
Willy-JL
2023-03-17 23:52:05 +00:00
171 changed files with 3395 additions and 1252 deletions
+5
View File
@@ -6,6 +6,10 @@ env.Append(
],
SDK_HEADERS=[
File("flipper_application.h"),
File("plugins/plugin_manager.h"),
File("plugins/composite_resolver.h"),
File("api_hashtable/api_hashtable.h"),
File("api_hashtable/compilesort.hpp"),
],
)
@@ -14,6 +18,7 @@ libenv = env.Clone(FW_LIB_NAME="flipper_application")
libenv.ApplyLibFlags()
sources = libenv.GlobRecursive("*.c")
sources.append(File("api_hashtable/api_hashtable.cpp"))
lib = libenv.StaticLibrary("${FW_LIB_NAME}", sources)
libenv.Install("${LIB_DIST_DIR}", lib)
@@ -0,0 +1,38 @@
#include "api_hashtable.h"
#include <furi.h>
#include <algorithm>
#define TAG "hashtable_api"
bool elf_resolve_from_hashtable(
const ElfApiInterface* interface,
const char* name,
Elf32_Addr* address) {
const HashtableApiInterface* hashtable_interface =
static_cast<const HashtableApiInterface*>(interface);
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(hashtable_interface->table_cbegin, hashtable_interface->table_cend, key);
if((find_res == hashtable_interface->table_cend || (find_res->hash != gnu_sym_hash))) {
FURI_LOG_W(
TAG,
"Can't find symbol '%s' (hash %lx) @ %p!",
name,
gnu_sym_hash,
hashtable_interface->table_cbegin);
result = false;
} else {
result = true;
*address = find_res->address;
}
return result;
}
@@ -0,0 +1,85 @@
#pragma once
#include <flipper_application/elf/elf_api_interface.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Symbol table entry
*/
struct sym_entry {
uint32_t hash;
uint32_t address;
};
/**
* @brief Resolver for API entries using a pre-sorted table with hashes
* @param interface pointer to HashtableApiInterface
* @param name function name
* @param address output for function address
* @return true if the table contains a function
*/
bool elf_resolve_from_hashtable(
const ElfApiInterface* interface,
const char* name,
Elf32_Addr* address);
#ifdef __cplusplus
}
#include <array>
#include <algorithm>
/**
* @brief HashtableApiInterface is an implementation of ElfApiInterface
* that uses a hash table to resolve function addresses.
* table_cbegin and table_cend must point to a sorted array of sym_entry
*/
struct HashtableApiInterface : public ElfApiInterface {
const sym_entry *table_cbegin, *table_cend;
};
#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;
}
/**
* @brief Calculate hash for a string using the ELF GNU hash algorithm
* @param s string to calculate hash for
* @return hash value
*/
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;
}
/* Compile-time check for hash collisions in API table.
* Usage: static_assert(!has_hash_collisions(api_methods), "Hash collision detected");
*/
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;
}
#endif
@@ -0,0 +1,115 @@
/**
* Implementation of compile-time sort for symbol table entries.
*/
#pragma once
#ifdef __cplusplus
#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)...};
}
#endif
@@ -3,10 +3,14 @@
#include <elf.h>
#include <stdbool.h>
#define ELF_INVALID_ADDRESS 0xFFFFFFFF
typedef struct {
/**
* @brief Interface for ELF loader to resolve symbols
*/
typedef struct ElfApiInterface {
uint16_t api_version_major;
uint16_t api_version_minor;
bool (*resolver_callback)(const char* name, Elf32_Addr* address);
bool (*resolver_callback)(
const struct ElfApiInterface* interface,
const char* name,
Elf32_Addr* address);
} ElfApiInterface;
+26 -7
View File
@@ -17,6 +17,8 @@
#define FURI_LOG_D(...)
#endif
#define ELF_INVALID_ADDRESS 0xFFFFFFFF
#define TRAMPOLINE_CODE_SIZE 6
/**
@@ -166,7 +168,7 @@ static ELFSection* elf_section_of(ELFFile* elf, int index) {
static Elf32_Addr elf_address_of(ELFFile* elf, Elf32_Sym* sym, const char* sName) {
if(sym->st_shndx == SHN_UNDEF) {
Elf32_Addr addr = 0;
if(elf->api_interface->resolver_callback(sName, &addr)) {
if(elf->api_interface->resolver_callback(elf->api_interface, sName, &addr)) {
return addr;
}
} else {
@@ -514,10 +516,13 @@ static SectionType elf_preload_section(
section_p->sec_idx = section_idx;
if(section_header->sh_type == SHT_PREINIT_ARRAY) {
furi_assert(elf->preinit_array == NULL);
elf->preinit_array = section_p;
} else if(section_header->sh_type == SHT_INIT_ARRAY) {
furi_assert(elf->init_array == NULL);
elf->init_array = section_p;
} else if(section_header->sh_type == SHT_FINI_ARRAY) {
furi_assert(elf->fini_array == NULL);
elf->fini_array = section_p;
}
@@ -605,10 +610,17 @@ ELFFile* elf_file_alloc(Storage* storage, const ElfApiInterface* api_interface)
elf->api_interface = api_interface;
ELFSectionDict_init(elf->sections);
AddressCache_init(elf->trampoline_cache);
elf->init_array_called = false;
return elf;
}
void elf_file_free(ELFFile* elf) {
// furi_check(!elf->init_array_called);
if(elf->init_array_called) {
FURI_LOG_W(TAG, "Init array was called, but fini array wasn't");
elf_file_call_section_list(elf->fini_array, true);
}
// free sections data
{
ELFSectionDict_it_t it;
@@ -774,19 +786,26 @@ ELFFileLoadStatus elf_file_load_sections(ELFFile* elf) {
return status;
}
void elf_file_pre_run(ELFFile* elf) {
void elf_file_call_init(ELFFile* elf) {
furi_check(!elf->init_array_called);
elf_file_call_section_list(elf->preinit_array, false);
elf_file_call_section_list(elf->init_array, false);
elf->init_array_called = true;
}
int32_t elf_file_run(ELFFile* elf, void* args) {
int32_t result;
result = ((int32_t(*)(void*))elf->entry)(args);
return result;
bool elf_file_is_init_complete(ELFFile* elf) {
return elf->init_array_called;
}
void elf_file_post_run(ELFFile* elf) {
void* elf_file_get_entry_point(ELFFile* elf) {
furi_check(elf->init_array_called);
return (void*)elf->entry;
}
void elf_file_call_fini(ELFFile* elf) {
furi_check(elf->init_array_called);
elf_file_call_section_list(elf->fini_array, true);
elf->init_array_called = false;
}
const ElfApiInterface* elf_file_get_api_interface(ELFFile* elf_file) {
+16 -6
View File
@@ -82,24 +82,34 @@ bool elf_file_load_section_table(ELFFile* elf_file);
ELFFileLoadStatus elf_file_load_sections(ELFFile* elf_file);
/**
* @brief Execute ELF file pre-run stage, call static constructors for example (load stage #3)
* @brief Execute ELF file pre-run stage,
* call static constructors for example (load stage #3)
* Must be done before invoking any code from the ELF file
* @param elf
*/
void elf_file_pre_run(ELFFile* elf);
void elf_file_call_init(ELFFile* elf);
/**
* @brief Run ELF file (load stage #4)
* @brief Check if ELF file pre-run stage was executed and its code is runnable
* @param elf
*/
bool elf_file_is_init_complete(ELFFile* elf);
/**
* @brief Get actual entry point for ELF file
* @param elf_file
* @param args
* @return int32_t
*/
int32_t elf_file_run(ELFFile* elf_file, void* args);
void* elf_file_get_entry_point(ELFFile* elf_file);
/**
* @brief Execute ELF file post-run stage, call static destructors for example (load stage #5)
* @brief Execute ELF file post-run stage,
* call static destructors for example (load stage #5)
* Must be done if any code from the ELF file was executed
* @param elf
*/
void elf_file_post_run(ELFFile* elf);
void elf_file_call_fini(ELFFile* elf);
/**
* @brief Get ELF file API interface
+2
View File
@@ -45,6 +45,8 @@ struct ELFFile {
ELFSection* preinit_array;
ELFSection* init_array;
ELFSection* fini_array;
bool init_array_called;
};
#ifdef __cplusplus
+55 -9
View File
@@ -10,6 +10,7 @@ struct FlipperApplication {
FlipperApplicationManifest manifest;
ELFFile* elf;
FuriThread* thread;
void* ep_thread_args;
};
/* For debugger access to app state */
@@ -20,9 +21,14 @@ FlipperApplication*
FlipperApplication* app = malloc(sizeof(FlipperApplication));
app->elf = elf_file_alloc(storage, api_interface);
app->thread = NULL;
app->ep_thread_args = NULL;
return app;
}
bool flipper_application_is_plugin(FlipperApplication* app) {
return app->manifest.stack_size == 0;
}
void flipper_application_free(FlipperApplication* app) {
furi_assert(app);
@@ -31,9 +37,16 @@ void flipper_application_free(FlipperApplication* app) {
furi_thread_free(app->thread);
}
last_loaded_app = NULL;
if(!flipper_application_is_plugin(app)) {
last_loaded_app = NULL;
}
elf_file_clear_debug_info(&app->state);
if(elf_file_is_init_complete(app->elf)) {
elf_file_call_fini(app->elf);
}
elf_file_free(app->elf);
free(app);
}
@@ -140,7 +153,9 @@ const FlipperApplicationManifest* flipper_application_get_manifest(FlipperApplic
}
FlipperApplicationLoadStatus flipper_application_map_to_memory(FlipperApplication* app) {
last_loaded_app = app;
if(!flipper_application_is_plugin(app)) {
last_loaded_app = app;
}
ELFFileLoadStatus status = elf_file_load_sections(app->elf);
switch(status) {
@@ -157,9 +172,15 @@ FlipperApplicationLoadStatus flipper_application_map_to_memory(FlipperApplicatio
}
static int32_t flipper_application_thread(void* context) {
elf_file_pre_run(last_loaded_app->elf);
int32_t result = elf_file_run(last_loaded_app->elf, context);
elf_file_post_run(last_loaded_app->elf);
furi_assert(context);
FlipperApplication* app = (FlipperApplication*)context;
elf_file_call_init(app->elf);
FlipperApplicationEntryPoint entry_point = elf_file_get_entry_point(app->elf);
int32_t ret_code = entry_point(app->ep_thread_args);
elf_file_call_fini(app->elf);
// wait until all notifications from RAM are completed
NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
@@ -169,17 +190,17 @@ static int32_t flipper_application_thread(void* context) {
notification_message_block(notifications, &sequence_empty);
furi_record_close(RECORD_NOTIFICATION);
return result;
return ret_code;
}
FuriThread* flipper_application_spawn(FlipperApplication* app, void* args) {
furi_check(app->thread == NULL);
furi_check(!flipper_application_is_plugin(app));
app->ep_thread_args = args;
const FlipperApplicationManifest* manifest = flipper_application_get_manifest(app);
furi_check(manifest->stack_size > 0);
app->thread = furi_thread_alloc_ex(
manifest->name, manifest->stack_size, flipper_application_thread, args);
manifest->name, manifest->stack_size, flipper_application_thread, app);
return app->thread;
}
@@ -213,3 +234,28 @@ const char* flipper_application_load_status_to_string(FlipperApplicationLoadStat
}
return load_status_strings[status];
}
const FlipperAppPluginDescriptor*
flipper_application_plugin_get_descriptor(FlipperApplication* app) {
if(!flipper_application_is_plugin(app)) {
return NULL;
}
if(!elf_file_is_init_complete(app->elf)) {
elf_file_call_init(app->elf);
}
typedef const FlipperAppPluginDescriptor* (*get_lib_descriptor_t)(void);
get_lib_descriptor_t lib_ep = elf_file_get_entry_point(app->elf);
furi_check(lib_ep);
const FlipperAppPluginDescriptor* lib_descriptor = lib_ep();
FURI_LOG_D(
TAG,
"Library for %s, API v. %lu loaded",
lib_descriptor->appid,
lib_descriptor->ep_api_version);
return lib_descriptor;
}
+35 -1
View File
@@ -115,6 +115,40 @@ FlipperApplicationLoadStatus flipper_application_map_to_memory(FlipperApplicatio
*/
FuriThread* flipper_application_spawn(FlipperApplication* app, void* args);
/**
* @brief Check if application is a plugin (not a runnable standalone app)
* @param app Application pointer
* @return true if application is a plugin, false otherwise
*/
bool flipper_application_is_plugin(FlipperApplication* app);
/**
* @brief Entry point prototype for standalone applications
*/
typedef int32_t (*FlipperApplicationEntryPoint)(void*);
/**
* @brief An object that describes a plugin - must be returned by plugin's entry point
*/
typedef struct {
const char* appid;
const uint32_t ep_api_version;
const void* entry_point;
} FlipperAppPluginDescriptor;
/**
* @brief Entry point prototype for plugins
*/
typedef const FlipperAppPluginDescriptor* (*FlipperApplicationPluginEntryPoint)(void);
/**
* @brief Get plugin descriptor for preloaded plugin
* @param app Application pointer
* @return Pointer to plugin descriptor
*/
const FlipperAppPluginDescriptor*
flipper_application_plugin_get_descriptor(FlipperApplication* app);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,52 @@
#include "composite_resolver.h"
#include <m-list.h>
#include <m-algo.h>
LIST_DEF(ElfApiInterfaceList, const ElfApiInterface*, M_POD_OPLIST)
#define M_OPL_ElfApiInterfaceList_t() LIST_OPLIST(ElfApiInterfaceList, M_POD_OPLIST)
struct CompositeApiResolver {
ElfApiInterface api_interface;
ElfApiInterfaceList_t interfaces;
};
static bool composite_api_resolver_callback(
const ElfApiInterface* interface,
const char* name,
Elf32_Addr* address) {
CompositeApiResolver* resolver = (CompositeApiResolver*)interface;
for
M_EACH(interface, resolver->interfaces, ElfApiInterfaceList_t) {
if((*interface)->resolver_callback(*interface, name, address)) {
return true;
}
}
return false;
}
CompositeApiResolver* composite_api_resolver_alloc() {
CompositeApiResolver* resolver = malloc(sizeof(CompositeApiResolver));
resolver->api_interface.api_version_major = 0;
resolver->api_interface.api_version_minor = 0;
resolver->api_interface.resolver_callback = &composite_api_resolver_callback;
ElfApiInterfaceList_init(resolver->interfaces);
return resolver;
}
void composite_api_resolver_free(CompositeApiResolver* resolver) {
ElfApiInterfaceList_clear(resolver->interfaces);
free(resolver);
}
void composite_api_resolver_add(CompositeApiResolver* resolver, const ElfApiInterface* interface) {
if(ElfApiInterfaceList_empty_p(resolver->interfaces)) {
resolver->api_interface.api_version_major = interface->api_version_major;
resolver->api_interface.api_version_minor = interface->api_version_minor;
}
ElfApiInterfaceList_push_back(resolver->interfaces, interface);
}
const ElfApiInterface* composite_api_resolver_get(CompositeApiResolver* resolver) {
return &resolver->api_interface;
}
@@ -0,0 +1,46 @@
#pragma once
#include <flipper_application/elf/elf_api_interface.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Composite API resolver
* Resolves API interface by calling all resolvers in order
* Uses API version from first resolver
* Note: when using hashtable resolvers, collisions between tables are not detected
* Can be cast to ElfApiInterface*
*/
typedef struct CompositeApiResolver CompositeApiResolver;
/**
* @brief Allocate composite API resolver
* @return CompositeApiResolver* instance
*/
CompositeApiResolver* composite_api_resolver_alloc();
/**
* @brief Free composite API resolver
* @param resolver Instance
*/
void composite_api_resolver_free(CompositeApiResolver* resolver);
/**
* @brief Add API resolver to composite resolver
* @param resolver Instance
* @param interface API resolver
*/
void composite_api_resolver_add(CompositeApiResolver* resolver, const ElfApiInterface* interface);
/**
* @brief Get API interface from composite resolver
* @param resolver Instance
* @return API interface
*/
const ElfApiInterface* composite_api_resolver_get(CompositeApiResolver* resolver);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,153 @@
#include "plugin_manager.h"
#include <loader/firmware_api/firmware_api.h>
#include <storage/storage.h>
#include <toolbox/path.h>
#include <m-array.h>
#include <m-algo.h>
#include <furi.h>
#define TAG "libmgr"
ARRAY_DEF(FlipperApplicationList, FlipperApplication*, M_PTR_OPLIST)
#define M_OPL_FlipperApplicationList_t() ARRAY_OPLIST(FlipperApplicationList, M_PTR_OPLIST)
struct PluginManager {
const char* application_id;
uint32_t api_version;
Storage* storage;
FlipperApplicationList_t libs;
const ElfApiInterface* api_interface;
};
PluginManager* plugin_manager_alloc(
const char* application_id,
uint32_t api_version,
const ElfApiInterface* api_interface) {
PluginManager* manager = malloc(sizeof(PluginManager));
manager->application_id = application_id;
manager->api_version = api_version;
manager->api_interface = api_interface ? api_interface : firmware_api_interface;
manager->storage = furi_record_open(RECORD_STORAGE);
FlipperApplicationList_init(manager->libs);
return manager;
}
void plugin_manager_free(PluginManager* manager) {
for
M_EACH(loaded_lib, manager->libs, FlipperApplicationList_t) {
flipper_application_free(*loaded_lib);
}
FlipperApplicationList_clear(manager->libs);
furi_record_close(RECORD_STORAGE);
free(manager);
}
PluginManagerError plugin_manager_load_single(PluginManager* manager, const char* path) {
FlipperApplication* lib = flipper_application_alloc(manager->storage, manager->api_interface);
PluginManagerError error = PluginManagerErrorNone;
do {
FlipperApplicationPreloadStatus preload_res = flipper_application_preload(lib, path);
if(preload_res != FlipperApplicationPreloadStatusSuccess) {
FURI_LOG_E(TAG, "Failed to preload %s", path);
error = PluginManagerErrorLoaderError;
break;
}
if(!flipper_application_is_plugin(lib)) {
FURI_LOG_E(TAG, "Not a plugin %s", path);
error = PluginManagerErrorLoaderError;
break;
}
FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(lib);
if(load_status != FlipperApplicationLoadStatusSuccess) {
FURI_LOG_E(TAG, "Failed to load module_demo_plugin1.fal");
break;
}
const FlipperAppPluginDescriptor* app_descriptor =
flipper_application_plugin_get_descriptor(lib);
if(!app_descriptor) {
FURI_LOG_E(TAG, "Failed to get descriptor %s", path);
error = PluginManagerErrorLoaderError;
break;
}
if(strcmp(app_descriptor->appid, manager->application_id) != 0) {
FURI_LOG_E(TAG, "Application id mismatch %s", path);
error = PluginManagerErrorApplicationIdMismatch;
break;
}
if(app_descriptor->ep_api_version != manager->api_version) {
FURI_LOG_E(TAG, "API version mismatch %s", path);
error = PluginManagerErrorAPIVersionMismatch;
break;
}
FlipperApplicationList_push_back(manager->libs, lib);
} while(false);
if(error != PluginManagerErrorNone) {
flipper_application_free(lib);
}
return error;
}
PluginManagerError plugin_manager_load_all(PluginManager* manager, const char* path) {
File* directory = storage_file_alloc(manager->storage);
char file_name_buffer[256];
FuriString* file_name = furi_string_alloc();
do {
if(!storage_dir_open(directory, path)) {
FURI_LOG_E(TAG, "Failed to open directory %s", path);
break;
}
while(true) {
if(!storage_dir_read(directory, NULL, file_name_buffer, sizeof(file_name_buffer))) {
break;
}
furi_string_set(file_name, file_name_buffer);
if(!furi_string_end_with_str(file_name, ".fal")) {
continue;
}
path_concat(path, file_name_buffer, file_name);
FURI_LOG_D(TAG, "Loading %s", furi_string_get_cstr(file_name));
PluginManagerError error =
plugin_manager_load_single(manager, furi_string_get_cstr(file_name));
if(error != PluginManagerErrorNone) {
FURI_LOG_E(TAG, "Failed to load %s", furi_string_get_cstr(file_name));
break;
}
}
} while(false);
storage_dir_close(directory);
storage_file_free(directory);
furi_string_free(file_name);
return PluginManagerErrorNone;
}
uint32_t plugin_manager_get_count(PluginManager* manager) {
return FlipperApplicationList_size(manager->libs);
}
const FlipperAppPluginDescriptor* plugin_manager_get(PluginManager* manager, uint32_t index) {
FlipperApplication* app = *FlipperApplicationList_get(manager->libs, index);
return flipper_application_plugin_get_descriptor(app);
}
const void* plugin_manager_get_ep(PluginManager* manager, uint32_t index) {
const FlipperAppPluginDescriptor* lib_descr = plugin_manager_get(manager, index);
furi_check(lib_descr);
return lib_descr->entry_point;
}
@@ -0,0 +1,82 @@
#pragma once
#include <flipper_application/flipper_application.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Object that manages plugins for an application
* Implements mass loading of plugins and provides access to their descriptors
*/
typedef struct PluginManager PluginManager;
typedef enum {
PluginManagerErrorNone = 0,
PluginManagerErrorLoaderError,
PluginManagerErrorApplicationIdMismatch,
PluginManagerErrorAPIVersionMismatch,
} PluginManagerError;
/**
* @brief Allocates new PluginManager
* @param application_id Application ID filter - only plugins with matching ID will be loaded
* @param api_version Application API version filter - only plugins with matching API version
* @param api_interface Application API interface - used to resolve plugins' API imports
* If plugin uses private application's API, use CompoundApiInterface
* @return new PluginManager instance
*/
PluginManager* plugin_manager_alloc(
const char* application_id,
uint32_t api_version,
const ElfApiInterface* api_interface);
/**
* @brief Frees PluginManager
* @param manager PluginManager instance
*/
void plugin_manager_free(PluginManager* manager);
/**
* @brief Loads single plugin by full path
* @param manager PluginManager instance
* @param path Path to plugin
* @return Error code
*/
PluginManagerError plugin_manager_load_single(PluginManager* manager, const char* path);
/**
* @brief Loads all plugins from specified directory
* @param manager PluginManager instance
* @param path Path to directory
* @return Error code
*/
PluginManagerError plugin_manager_load_all(PluginManager* manager, const char* path);
/**
* @brief Returns number of loaded plugins
* @param manager PluginManager instance
* @return Number of loaded plugins
*/
uint32_t plugin_manager_get_count(PluginManager* manager);
/**
* @brief Returns plugin descriptor by index
* @param manager PluginManager instance
* @param index Plugin index
* @return Plugin descriptor
*/
const FlipperAppPluginDescriptor* plugin_manager_get(PluginManager* manager, uint32_t index);
/**
* @brief Returns plugin entry point by index
* @param manager PluginManager instance
* @param index Plugin index
* @return Plugin entry point
*/
const void* plugin_manager_get_ep(PluginManager* manager, uint32_t index);
#ifdef __cplusplus
}
#endif
+11 -2
View File
@@ -1072,7 +1072,7 @@ void nfc_worker_mf_classic_dict_attack(NfcWorker* nfc_worker) {
if(mf_classic_authenticate_skip_activate(
&tx_rx, block_num, key, MfClassicKeyA, !deactivated, cuid)) {
mf_classic_set_key_found(data, i, MfClassicKeyA, key);
FURI_LOG_D(TAG, "Key found");
FURI_LOG_D(TAG, "Key A found");
nfc_worker->callback(NfcWorkerEventFoundKeyA, nfc_worker->context);
uint64_t found_key;
@@ -1086,24 +1086,33 @@ void nfc_worker_mf_classic_dict_attack(NfcWorker* nfc_worker) {
}
nfc_worker_mf_classic_key_attack(nfc_worker, found_key, &tx_rx, i + 1);
break;
}
nfc_worker_mf_classic_key_attack(nfc_worker, key, &tx_rx, i + 1);
deactivated = true;
}
furi_hal_nfc_sleep();
deactivated = true;
} else {
mf_classic_set_key_not_found(data, i, MfClassicKeyA);
is_key_a_found = false;
FURI_LOG_D(TAG, "Key %dA not found in attack", i);
}
if(!is_key_b_found) {
is_key_b_found = mf_classic_is_key_found(data, i, MfClassicKeyB);
if(mf_classic_authenticate_skip_activate(
&tx_rx, block_num, key, MfClassicKeyB, !deactivated, cuid)) {
FURI_LOG_D(TAG, "Key found");
FURI_LOG_D(TAG, "Key B found");
mf_classic_set_key_found(data, i, MfClassicKeyB, key);
nfc_worker->callback(NfcWorkerEventFoundKeyB, nfc_worker->context);
nfc_worker_mf_classic_key_attack(nfc_worker, key, &tx_rx, i + 1);
deactivated = true;
}
deactivated = true;
} else {
mf_classic_set_key_not_found(data, i, MfClassicKeyB);
is_key_b_found = false;
FURI_LOG_D(TAG, "Key %dB not found in attack", i);
}
if(is_key_a_found && is_key_b_found) break;
if(nfc_worker->state != NfcWorkerStateMfClassicDictAttack) break;
+1 -1
View File
@@ -118,7 +118,7 @@ bool plantain_4k_parser_parse(NfcDeviceData* dev_data) {
}
furi_string_printf(
dev_data->parsed_data, "\e#Plantain\nN:%llu-\nBalance:%ld\n", card_number, balance);
dev_data->parsed_data, "\e#Plantain\nN:%llu-\nBalance:%lu\n", card_number, balance);
return true;
}
+1 -1
View File
@@ -91,7 +91,7 @@ bool plantain_parser_parse(NfcDeviceData* dev_data) {
}
furi_string_printf(
dev_data->parsed_data, "\e#Plantain\nN:%llu-\nBalance:%ld\n", card_number, balance);
dev_data->parsed_data, "\e#Plantain\nN:%llu-\nBalance:%lu\n", card_number, balance);
return true;
}
+1 -1
View File
@@ -99,7 +99,7 @@ bool troika_4k_parser_parse(NfcDeviceData* dev_data) {
number >>= 4;
furi_string_printf(
dev_data->parsed_data, "\e#Troika\nNum: %ld\nBalance: %d rur.", number, balance);
dev_data->parsed_data, "\e#Troika\nNum: %lu\nBalance: %u rur.", number, balance);
return true;
}
+1 -1
View File
@@ -79,7 +79,7 @@ bool troika_parser_parse(NfcDeviceData* dev_data) {
number >>= 4;
furi_string_printf(
dev_data->parsed_data, "\e#Troika\nNum: %ld\nBalance: %d rur.", number, balance);
dev_data->parsed_data, "\e#Troika\nNum: %lu\nBalance: %u rur.", number, balance);
troika_parsed = true;
} while(false);
+1 -1
View File
@@ -136,7 +136,7 @@ bool two_cities_parser_parse(NfcDeviceData* dev_data) {
furi_string_printf(
dev_data->parsed_data,
"\e#Troika+Plantain\nPN: %llu-\nPB: %ld rur.\nTN: %ld\nTB: %d rur.\n",
"\e#Troika+Plantain\nPN: %llu-\nPB: %lu rur.\nTN: %lu\nTB: %u rur.\n",
card_number,
balance,
troika_number,
+23 -4
View File
@@ -651,6 +651,12 @@ void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, u
if(!key_a_found) break;
FURI_LOG_D(TAG, "Try to read blocks with key A");
key = nfc_util_bytes2num(sec_tr->key_a, sizeof(sec_tr->key_a));
if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyA, &crypto, false, 0)) {
mf_classic_set_key_not_found(data, sec_num, MfClassicKeyA);
FURI_LOG_D(TAG, "Key %dA not found in read", sec_num);
break;
}
for(size_t i = start_block; i < start_block + total_blocks; i++) {
if(!mf_classic_is_block_read(data, i)) {
if(!mf_classic_auth(tx_rx, i, key, MfClassicKeyA, &crypto, false, 0)) continue;
@@ -660,7 +666,11 @@ void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, u
} else if(i > start_block) {
// Try to re-auth to read block in case prevous block was protected from read
furi_hal_nfc_sleep();
if(!mf_classic_auth(tx_rx, i, key, MfClassicKeyA, &crypto, false, 0)) break;
if(!mf_classic_auth(tx_rx, i, key, MfClassicKeyA, &crypto, false, 0)) {
mf_classic_set_key_not_found(data, sec_num, MfClassicKeyA);
FURI_LOG_D(TAG, "Key %dA not found in read", sec_num);
break;
}
if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
mf_classic_set_block_read(data, i, &block_tmp);
blocks_read++;
@@ -681,7 +691,12 @@ void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, u
}
FURI_LOG_D(TAG, "Try to read blocks with key B");
key = nfc_util_bytes2num(sec_tr->key_b, sizeof(sec_tr->key_b));
if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyB, &crypto, false, 0)) break;
if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyB, &crypto, false, 0)) {
mf_classic_set_key_not_found(data, sec_num, MfClassicKeyB);
FURI_LOG_D(TAG, "Key %dB not found in read", sec_num);
break;
}
for(size_t i = start_block; i < start_block + total_blocks; i++) {
if(!mf_classic_is_block_read(data, i)) {
if(!mf_classic_auth(tx_rx, i, key, MfClassicKeyB, &crypto, false, 0)) continue;
@@ -691,7 +706,11 @@ void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, u
} else if(i > start_block) {
// Try to re-auth to read block in case prevous block was protected from read
furi_hal_nfc_sleep();
if(!mf_classic_auth(tx_rx, i, key, MfClassicKeyB, &crypto, false, 0)) break;
if(!mf_classic_auth(tx_rx, i, key, MfClassicKeyB, &crypto, false, 0)) {
mf_classic_set_key_not_found(data, sec_num, MfClassicKeyB);
FURI_LOG_D(TAG, "Key %dB not found in read", sec_num);
break;
}
if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
mf_classic_set_block_read(data, i, &block_tmp);
blocks_read++;
@@ -1524,4 +1543,4 @@ bool mf_classic_write_sector(
}
return write_success;
}
}
@@ -21,6 +21,7 @@
#define BITS_IN_BYTE 8U
#define BITS_IN_KBIT 1024U
#define BITS_IN_MBIT (BITS_IN_KBIT * 1024U)
bool dallas_common_skip_rom(OneWireHost* host) {
onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
@@ -210,25 +211,35 @@ bool dallas_common_is_valid_crc(const DallasCommonRomData* rom_data) {
void dallas_common_render_brief_data(
FuriString* result,
const DallasCommonRomData* rom_data,
const uint8_t* sram_data,
size_t sram_data_size) {
const uint8_t* mem_data,
size_t mem_size,
const char* mem_name) {
for(size_t i = 0; i < sizeof(rom_data->bytes); ++i) {
furi_string_cat_printf(result, "%02X ", rom_data->bytes[i]);
}
const char* size_prefix = "";
size_t mem_size_bits = mem_size * BITS_IN_BYTE;
if(mem_size_bits >= BITS_IN_MBIT) {
size_prefix = "M";
mem_size_bits /= BITS_IN_MBIT;
} else if(mem_size_bits >= BITS_IN_KBIT) {
size_prefix = "K";
mem_size_bits /= BITS_IN_KBIT;
}
furi_string_cat_printf(
result,
"\nInternal SRAM: %zu Kbit\n",
(size_t)(sram_data_size * BITS_IN_BYTE / BITS_IN_KBIT));
result, "\nInternal %s: %zu %sbit\n", mem_name, mem_size_bits, size_prefix);
for(size_t i = 0; i < DALLAS_COMMON_BRIEF_HEAD_COUNT; ++i) {
furi_string_cat_printf(result, "%02X ", sram_data[i]);
furi_string_cat_printf(result, "%02X ", mem_data[i]);
}
furi_string_cat_printf(result, "[ . . . ]");
for(size_t i = sram_data_size - DALLAS_COMMON_BRIEF_TAIL_COUNT; i < sram_data_size; ++i) {
furi_string_cat_printf(result, " %02X", sram_data[i]);
for(size_t i = mem_size - DALLAS_COMMON_BRIEF_TAIL_COUNT; i < mem_size; ++i) {
furi_string_cat_printf(result, " %02X", mem_data[i]);
}
}
@@ -99,8 +99,9 @@ bool dallas_common_is_valid_crc(const DallasCommonRomData* rom_data);
void dallas_common_render_brief_data(
FuriString* result,
const DallasCommonRomData* rom_data,
const uint8_t* sram_data,
size_t sram_data_size);
const uint8_t* mem_data,
size_t mem_size,
const char* mem_name);
void dallas_common_render_crc_error(FuriString* result, const DallasCommonRomData* rom_data);
@@ -0,0 +1,270 @@
#include "protocol_ds1971.h"
#include <core/core_defines.h>
#include <toolbox/pretty_format.h>
#include "dallas_common.h"
#define DS1971_FAMILY_CODE 0x14U
#define DS1971_FAMILY_NAME "DS1971"
#define DS1971_EEPROM_DATA_SIZE 32U
#define DS1971_SRAM_PAGE_SIZE 32U
#define DS1971_COPY_SCRATCH_DELAY_US 250U
#define DS1971_DATA_BYTE_COUNT 4U
#define DS1971_EEPROM_DATA_KEY "Eeprom Data"
#define DS1971_MEMORY_TYPE "EEPROM"
#define DS1971_CMD_FINALIZATION 0xA5
typedef struct {
OneWireSlave* bus;
DallasCommonCommandState command_state;
} DS1971ProtocolState;
typedef struct {
DallasCommonRomData rom_data;
uint8_t eeprom_data[DS1971_EEPROM_DATA_SIZE];
DS1971ProtocolState state;
} DS1971ProtocolData;
static bool dallas_ds1971_read(OneWireHost*, void*);
static bool dallas_ds1971_write_copy(OneWireHost*, iButtonProtocolData*);
static void dallas_ds1971_emulate(OneWireSlave*, iButtonProtocolData*);
static bool dallas_ds1971_load(FlipperFormat*, uint32_t, iButtonProtocolData*);
static bool dallas_ds1971_save(FlipperFormat*, const iButtonProtocolData*);
static void dallas_ds1971_render_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1971_render_brief_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1971_render_error(FuriString*, const iButtonProtocolData*);
static bool dallas_ds1971_is_data_valid(const iButtonProtocolData*);
static void dallas_ds1971_get_editable_data(iButtonEditableData*, iButtonProtocolData*);
static void dallas_ds1971_apply_edits(iButtonProtocolData*);
static bool
dallas_ds1971_read_mem(OneWireHost* host, uint8_t address, uint8_t* data, size_t data_size);
static bool ds1971_emulate_read_mem(OneWireSlave* bus, const uint8_t* data, size_t data_size);
const iButtonProtocolDallasBase ibutton_protocol_ds1971 = {
.family_code = DS1971_FAMILY_CODE,
.features = iButtonProtocolFeatureExtData | iButtonProtocolFeatureWriteCopy,
.data_size = sizeof(DS1971ProtocolData),
.manufacturer = DALLAS_COMMON_MANUFACTURER_NAME,
.name = DS1971_FAMILY_NAME,
.read = dallas_ds1971_read,
.write_blank = NULL, /* No data to write a blank */
.write_copy = dallas_ds1971_write_copy,
.emulate = dallas_ds1971_emulate,
.save = dallas_ds1971_save,
.load = dallas_ds1971_load,
.render_data = dallas_ds1971_render_data,
.render_brief_data = dallas_ds1971_render_brief_data,
.render_error = dallas_ds1971_render_error,
.is_valid = dallas_ds1971_is_data_valid,
.get_editable_data = dallas_ds1971_get_editable_data,
.apply_edits = dallas_ds1971_apply_edits,
};
bool dallas_ds1971_read(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
return onewire_host_reset(host) && dallas_common_read_rom(host, &data->rom_data) &&
dallas_ds1971_read_mem(host, 0, data->eeprom_data, DS1971_EEPROM_DATA_SIZE);
}
bool dallas_ds1971_write_copy(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
onewire_host_reset(host);
onewire_host_skip(host);
// Starting writing from address 0x0000
onewire_host_write(host, DALLAS_COMMON_CMD_WRITE_SCRATCH);
onewire_host_write(host, 0x00);
// Write data to scratchpad
onewire_host_write_bytes(host, data->eeprom_data, DS1971_EEPROM_DATA_SIZE);
// Read data from scratchpad and verify
bool pad_valid = false;
if(onewire_host_reset(host)) {
pad_valid = true;
onewire_host_skip(host);
onewire_host_write(host, DALLAS_COMMON_CMD_READ_SCRATCH);
onewire_host_write(host, 0x00);
for(size_t i = 0; i < DS1971_EEPROM_DATA_SIZE; ++i) {
uint8_t scratch = onewire_host_read(host);
if(data->eeprom_data[i] != scratch) {
pad_valid = false;
break;
}
}
}
// Copy scratchpad to memory and confirm
if(pad_valid) {
if(onewire_host_reset(host)) {
onewire_host_skip(host);
onewire_host_write(host, DALLAS_COMMON_CMD_COPY_SCRATCH);
onewire_host_write(host, DS1971_CMD_FINALIZATION);
furi_delay_us(DS1971_COPY_SCRATCH_DELAY_US);
}
}
return pad_valid;
}
static void dallas_ds1971_reset_callback(void* context) {
furi_assert(context);
DS1971ProtocolData* data = context;
data->state.command_state = DallasCommonCommandStateIdle;
}
static bool dallas_ds1971_command_callback(uint8_t command, void* context) {
furi_assert(context);
DS1971ProtocolData* data = context;
OneWireSlave* bus = data->state.bus;
switch(command) {
case DALLAS_COMMON_CMD_SEARCH_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return dallas_common_emulate_search_rom(bus, &data->rom_data);
} else if(data->state.command_state == DallasCommonCommandStateRomCmd) {
data->state.command_state = DallasCommonCommandStateMemCmd;
ds1971_emulate_read_mem(bus, data->eeprom_data, DS1971_EEPROM_DATA_SIZE);
return false;
} else {
return false;
}
case DALLAS_COMMON_CMD_READ_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return dallas_common_emulate_read_rom(bus, &data->rom_data);
} else {
return false;
}
case DALLAS_COMMON_CMD_SKIP_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return true;
} else {
return false;
}
default:
return false;
}
}
void dallas_ds1971_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
data->state.bus = bus;
onewire_slave_set_reset_callback(bus, dallas_ds1971_reset_callback, protocol_data);
onewire_slave_set_command_callback(bus, dallas_ds1971_command_callback, protocol_data);
}
bool dallas_ds1971_load(
FlipperFormat* ff,
uint32_t format_version,
iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
bool success = false;
do {
if(format_version < 2) break;
if(!dallas_common_load_rom_data(ff, format_version, &data->rom_data)) break;
if(!flipper_format_read_hex(
ff, DS1971_EEPROM_DATA_KEY, data->eeprom_data, DS1971_EEPROM_DATA_SIZE))
break;
success = true;
} while(false);
return success;
}
bool dallas_ds1971_save(FlipperFormat* ff, const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
bool success = false;
do {
if(!dallas_common_save_rom_data(ff, &data->rom_data)) break;
if(!flipper_format_write_hex(
ff, DS1971_EEPROM_DATA_KEY, data->eeprom_data, DS1971_EEPROM_DATA_SIZE))
break;
success = true;
} while(false);
return success;
}
void dallas_ds1971_render_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
pretty_format_bytes_hex_canonical(
result,
DS1971_DATA_BYTE_COUNT,
PRETTY_FORMAT_FONT_MONOSPACE,
data->eeprom_data,
DS1971_EEPROM_DATA_SIZE);
}
void dallas_ds1971_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
dallas_common_render_brief_data(
result, &data->rom_data, data->eeprom_data, DS1971_EEPROM_DATA_SIZE, DS1971_MEMORY_TYPE);
}
void dallas_ds1971_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
if(!dallas_common_is_valid_crc(&data->rom_data)) {
dallas_common_render_crc_error(result, &data->rom_data);
}
}
bool dallas_ds1971_is_data_valid(const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
return dallas_common_is_valid_crc(&data->rom_data);
}
void dallas_ds1971_get_editable_data(
iButtonEditableData* editable_data,
iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
editable_data->ptr = data->rom_data.bytes;
editable_data->size = sizeof(DallasCommonRomData);
}
void dallas_ds1971_apply_edits(iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
dallas_common_apply_edits(&data->rom_data, DS1971_FAMILY_CODE);
}
bool dallas_ds1971_read_mem(OneWireHost* host, uint8_t address, uint8_t* data, size_t data_size) {
onewire_host_write(host, DALLAS_COMMON_CMD_READ_MEM);
onewire_host_write(host, address);
onewire_host_read_bytes(host, data, (uint8_t)data_size);
return true;
}
bool ds1971_emulate_read_mem(OneWireSlave* bus, const uint8_t* data, size_t data_size) {
bool success = false;
do {
uint8_t address;
if(!onewire_slave_receive(bus, &address, sizeof(address))) break;
if(address >= data_size) break;
if(!onewire_slave_send(bus, data + address, data_size - address)) break;
success = true;
} while(false);
return success;
}
@@ -0,0 +1,5 @@
#pragma once
#include "protocol_dallas_base.h"
extern const iButtonProtocolDallasBase ibutton_protocol_ds1971;
@@ -17,6 +17,7 @@
#define DS1992_DATA_BYTE_COUNT 4U
#define DS1992_SRAM_DATA_KEY "Sram Data"
#define DS1992_MEMORY_TYPE "SRAM"
typedef struct {
OneWireSlave* bus;
@@ -188,7 +189,7 @@ void dallas_ds1992_render_data(FuriString* result, const iButtonProtocolData* pr
void dallas_ds1992_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1992ProtocolData* data = protocol_data;
dallas_common_render_brief_data(
result, &data->rom_data, data->sram_data, DS1992_SRAM_DATA_SIZE);
result, &data->rom_data, data->sram_data, DS1992_SRAM_DATA_SIZE, DS1992_MEMORY_TYPE);
}
void dallas_ds1992_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
@@ -15,6 +15,7 @@
#define DS1996_DATA_BYTE_COUNT 4U
#define DS1996_SRAM_DATA_KEY "Sram Data"
#define DS1996_MEMORY_TYPE "SRAM"
typedef struct {
OneWireSlave* bus;
@@ -182,7 +183,7 @@ void dallas_ds1996_render_data(FuriString* result, const iButtonProtocolData* pr
void dallas_ds1996_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1996ProtocolData* data = protocol_data;
dallas_common_render_brief_data(
result, &data->rom_data, data->sram_data, DS1996_SRAM_DATA_SIZE);
result, &data->rom_data, data->sram_data, DS1996_SRAM_DATA_SIZE, DS1996_MEMORY_TYPE);
}
void dallas_ds1996_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
@@ -3,12 +3,14 @@
#include "protocol_ds1990.h"
#include "protocol_ds1992.h"
#include "protocol_ds1996.h"
#include "protocol_ds1971.h"
#include "protocol_ds_generic.h"
const iButtonProtocolDallasBase* ibutton_protocols_dallas[] = {
[iButtonProtocolDS1990] = &ibutton_protocol_ds1990,
[iButtonProtocolDS1992] = &ibutton_protocol_ds1992,
[iButtonProtocolDS1996] = &ibutton_protocol_ds1996,
[iButtonProtocolDS1971] = &ibutton_protocol_ds1971,
/* Add new 1-Wire protocols here */
/* Default catch-all 1-Wire protocol */
@@ -6,6 +6,7 @@ typedef enum {
iButtonProtocolDS1990,
iButtonProtocolDS1992,
iButtonProtocolDS1996,
iButtonProtocolDS1971,
/* Add new 1-Wire protocols here */
/* Default catch-all 1-Wire protocol */
+12 -2
View File
@@ -198,10 +198,12 @@ static bool subghz_protocol_keeloq_gen_data(
(instance->generic.serial & 0x3FF)
<< 16 | //ToDo in some protocols the discriminator is 0
instance->generic.cnt;
// DTM Neo uses 12bit -> simple learning -- FAAC_RC,XT , Mutanco_Mutancode -> 12bit normal learning
// DTM Neo, Came_Space uses 12bit -> simple learning -- FAAC_RC,XT , Mutanco_Mutancode, Stilmatic(Schellenberg) -> 12bit normal learning
if((strcmp(instance->manufacture_name, "DTM_Neo") == 0) ||
(strcmp(instance->manufacture_name, "FAAC_RC,XT") == 0) ||
(strcmp(instance->manufacture_name, "Mutanco_Mutancode") == 0)) {
(strcmp(instance->manufacture_name, "Mutanco_Mutancode") == 0) ||
(strcmp(instance->manufacture_name, "Stilmatic") == 0) ||
(strcmp(instance->manufacture_name, "Came_Space") == 0)) {
decrypt = btn << 28 | (instance->generic.serial & 0xFFF) << 16 | instance->generic.cnt;
}
@@ -567,6 +569,10 @@ SubGhzProtocolStatus
instance->generic.seed = seed_data[0] << 24 | seed_data[1] << 16 | seed_data[2] << 8 |
seed_data[3];
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
// Read manufacturer from file
if(flipper_format_read_string(
flipper_format, "Manufacture", instance->manufacture_from_file)) {
@@ -1247,6 +1253,10 @@ SubGhzProtocolStatus
instance->generic.seed = seed_data[0] << 24 | seed_data[1] << 16 | seed_data[2] << 8 |
seed_data[3];
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
// Read manufacturer from file
if(flipper_format_read_string(
flipper_format, "Manufacture", instance->manufacture_from_file)) {
+91 -1
View File
@@ -83,6 +83,25 @@ const SubGhzProtocol subghz_protocol_secplus_v2 = {
.encoder = &subghz_protocol_secplus_v2_encoder,
};
static uint8_t sc_btn_temp_id;
static uint8_t sc_btn_temp_id_original;
void secplus2_set_btn(uint8_t b) {
sc_btn_temp_id = b;
}
uint8_t secplus2_get_original_btn() {
return sc_btn_temp_id_original;
}
uint8_t secplus2_get_custom_btn() {
return sc_btn_temp_id;
}
void secplus2_reset_original_btn() {
sc_btn_temp_id_original = 0;
}
void* subghz_protocol_encoder_secplus_v2_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderSecPlus_v2* instance = malloc(sizeof(SubGhzProtocolEncoderSecPlus_v2));
@@ -338,6 +357,11 @@ static void
instance->btn = 0;
instance->serial = 0;
}
// Save original button for later use
if(sc_btn_temp_id_original == 0) {
sc_btn_temp_id_original = instance->btn;
}
}
/**
@@ -373,6 +397,72 @@ static uint64_t subghz_protocol_secplus_v2_encode_half(uint8_t roll_array[], uin
*/
static void subghz_protocol_secplus_v2_encode(SubGhzProtocolEncoderSecPlus_v2* instance) {
// Save original button for later use
if(sc_btn_temp_id_original == 0) {
sc_btn_temp_id_original = instance->generic.btn;
}
// Set custom button
if(sc_btn_temp_id == 1) {
switch(sc_btn_temp_id_original) {
case 0x68:
instance->generic.btn = 0x80;
break;
case 0x80:
instance->generic.btn = 0x68;
break;
case 0x81:
instance->generic.btn = 0x80;
break;
case 0xE2:
instance->generic.btn = 0x80;
break;
default:
break;
}
}
if(sc_btn_temp_id == 2) {
switch(sc_btn_temp_id_original) {
case 0x68:
instance->generic.btn = 0x81;
break;
case 0x80:
instance->generic.btn = 0x81;
break;
case 0x81:
instance->generic.btn = 0x68;
break;
case 0xE2:
instance->generic.btn = 0x81;
break;
default:
break;
}
}
if(sc_btn_temp_id == 3) {
switch(sc_btn_temp_id_original) {
case 0x68:
instance->generic.btn = 0xE2;
break;
case 0x80:
instance->generic.btn = 0xE2;
break;
case 0x81:
instance->generic.btn = 0xE2;
break;
case 0xE2:
instance->generic.btn = 0x68;
break;
default:
break;
}
}
if((sc_btn_temp_id == 0) && (sc_btn_temp_id_original != 0)) {
instance->generic.btn = sc_btn_temp_id_original;
}
uint32_t fixed_1[1] = {instance->generic.btn << 12 | instance->generic.serial >> 20};
uint32_t fixed_2[1] = {instance->generic.serial & 0xFFFFF};
uint8_t rolling_digits[18] = {0};
@@ -611,7 +701,7 @@ bool subghz_protocol_secplus_v2_create_data(
if((res == SubGhzProtocolStatusOk) &&
!flipper_format_write_hex(flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
res = SubGhzProtocolStatusError;
res = SubGhzProtocolStatusErrorParserOthers;
}
return res == SubGhzProtocolStatusOk;
}
+8
View File
@@ -10,6 +10,14 @@ extern const SubGhzProtocolDecoder subghz_protocol_secplus_v2_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_secplus_v2_encoder;
extern const SubGhzProtocol subghz_protocol_secplus_v2;
// Custom buttons
void secplus2_set_btn(uint8_t b);
uint8_t secplus2_get_original_btn();
uint8_t secplus2_get_custom_btn();
void secplus2_reset_original_btn();
/**
* Allocate SubGhzProtocolEncoderSecPlus_v2.
* @param environment Pointer to a SubGhzEnvironment instance
+1 -2
View File
@@ -73,9 +73,8 @@ static const uint32_t subghz_frequency_list[] = {
};
static const uint32_t subghz_hopper_frequency_list[] = {
310000000,
315000000,
330000000,
390000000,
433420000,
433920000,
868350000,