2021-11-26 13:53:51 +00:00
|
|
|
#include "bad_usb_app_i.h"
|
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2022-02-02 14:27:33 +00:00
|
|
|
#include <storage/storage.h>
|
2022-02-10 13:01:49 +00:00
|
|
|
#include <lib/toolbox/path.h>
|
2024-03-25 08:39:33 +00:00
|
|
|
#include <flipper_format/flipper_format.h>
|
2021-11-26 13:53:51 +00:00
|
|
|
|
2024-03-25 08:39:33 +00:00
|
|
|
#define BAD_USB_SETTINGS_PATH BAD_USB_APP_BASE_FOLDER "/.badusb.settings"
|
|
|
|
#define BAD_USB_SETTINGS_FILE_TYPE "Flipper BadUSB Settings File"
|
|
|
|
#define BAD_USB_SETTINGS_VERSION 1
|
|
|
|
#define BAD_USB_SETTINGS_DEFAULT_LAYOUT BAD_USB_APP_PATH_LAYOUT_FOLDER "/en-US.kl"
|
2023-02-08 15:01:00 +00:00
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
static bool bad_usb_app_custom_event_callback(void* context, uint32_t event) {
|
|
|
|
furi_assert(context);
|
|
|
|
BadUsbApp* app = context;
|
|
|
|
return scene_manager_handle_custom_event(app->scene_manager, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool bad_usb_app_back_event_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
BadUsbApp* app = context;
|
|
|
|
return scene_manager_handle_back_event(app->scene_manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bad_usb_app_tick_event_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
BadUsbApp* app = context;
|
|
|
|
scene_manager_handle_tick_event(app->scene_manager);
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:01:00 +00:00
|
|
|
static void bad_usb_load_settings(BadUsbApp* app) {
|
2024-03-25 08:39:33 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
FlipperFormat* fff = flipper_format_file_alloc(storage);
|
|
|
|
bool state = false;
|
|
|
|
|
|
|
|
FuriString* temp_str = furi_string_alloc();
|
|
|
|
uint32_t version = 0;
|
|
|
|
uint32_t interface = 0;
|
|
|
|
|
|
|
|
if(flipper_format_file_open_existing(fff, BAD_USB_SETTINGS_PATH)) {
|
|
|
|
do {
|
|
|
|
if(!flipper_format_read_header(fff, temp_str, &version)) break;
|
|
|
|
if((strcmp(furi_string_get_cstr(temp_str), BAD_USB_SETTINGS_FILE_TYPE) != 0) ||
|
|
|
|
(version != BAD_USB_SETTINGS_VERSION))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(!flipper_format_read_string(fff, "layout", temp_str)) break;
|
|
|
|
if(!flipper_format_read_uint32(fff, "interface", &interface, 1)) break;
|
|
|
|
if(interface > BadUsbHidInterfaceBle) break;
|
|
|
|
|
|
|
|
state = true;
|
|
|
|
} while(0);
|
2023-02-08 15:01:00 +00:00
|
|
|
}
|
2024-03-25 08:39:33 +00:00
|
|
|
flipper_format_free(fff);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
|
|
|
|
if(state) {
|
|
|
|
furi_string_set(app->keyboard_layout, temp_str);
|
|
|
|
app->interface = interface;
|
2023-02-08 15:01:00 +00:00
|
|
|
|
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
|
|
|
FileInfo layout_file_info;
|
|
|
|
FS_Error file_check_err = storage_common_stat(
|
|
|
|
fs_api, furi_string_get_cstr(app->keyboard_layout), &layout_file_info);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
2024-03-25 08:39:33 +00:00
|
|
|
if((file_check_err != FSE_OK) || (layout_file_info.size != 256)) {
|
|
|
|
furi_string_set(app->keyboard_layout, BAD_USB_SETTINGS_DEFAULT_LAYOUT);
|
2023-02-08 15:01:00 +00:00
|
|
|
}
|
2024-03-25 08:39:33 +00:00
|
|
|
} else {
|
|
|
|
furi_string_set(app->keyboard_layout, BAD_USB_SETTINGS_DEFAULT_LAYOUT);
|
|
|
|
app->interface = BadUsbHidInterfaceUsb;
|
2023-02-08 15:01:00 +00:00
|
|
|
}
|
2024-03-25 08:39:33 +00:00
|
|
|
|
|
|
|
furi_string_free(temp_str);
|
2023-02-08 15:01:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bad_usb_save_settings(BadUsbApp* app) {
|
2024-03-25 08:39:33 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
FlipperFormat* fff = flipper_format_file_alloc(storage);
|
|
|
|
|
|
|
|
if(flipper_format_file_open_always(fff, BAD_USB_SETTINGS_PATH)) {
|
|
|
|
do {
|
|
|
|
if(!flipper_format_write_header_cstr(
|
|
|
|
fff, BAD_USB_SETTINGS_FILE_TYPE, BAD_USB_SETTINGS_VERSION))
|
|
|
|
break;
|
|
|
|
if(!flipper_format_write_string(fff, "layout", app->keyboard_layout)) break;
|
|
|
|
uint32_t interface_id = app->interface;
|
|
|
|
if(!flipper_format_write_uint32(fff, "interface", (const uint32_t*)&interface_id, 1))
|
|
|
|
break;
|
|
|
|
} while(0);
|
2023-02-08 15:01:00 +00:00
|
|
|
}
|
2024-03-25 08:39:33 +00:00
|
|
|
|
|
|
|
flipper_format_free(fff);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
2023-02-08 15:01:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
BadUsbApp* bad_usb_app_alloc(char* arg) {
|
2022-02-18 19:53:46 +00:00
|
|
|
BadUsbApp* app = malloc(sizeof(BadUsbApp));
|
2021-11-26 13:53:51 +00:00
|
|
|
|
2023-02-08 15:01:00 +00:00
|
|
|
app->bad_usb_script = NULL;
|
2022-05-27 11:19:21 +00:00
|
|
|
|
2023-02-08 15:01:00 +00:00
|
|
|
app->file_path = furi_string_alloc();
|
|
|
|
app->keyboard_layout = furi_string_alloc();
|
2022-08-03 15:47:10 +00:00
|
|
|
if(arg && strlen(arg)) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(app->file_path, arg);
|
2022-02-10 13:01:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:01:00 +00:00
|
|
|
bad_usb_load_settings(app);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
app->gui = furi_record_open(RECORD_GUI);
|
|
|
|
app->notifications = furi_record_open(RECORD_NOTIFICATION);
|
|
|
|
app->dialogs = furi_record_open(RECORD_DIALOGS);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
app->view_dispatcher = view_dispatcher_alloc();
|
|
|
|
view_dispatcher_enable_queue(app->view_dispatcher);
|
2022-02-10 13:01:49 +00:00
|
|
|
|
|
|
|
app->scene_manager = scene_manager_alloc(&bad_usb_scene_handlers, app);
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
|
|
|
|
view_dispatcher_set_tick_event_callback(
|
|
|
|
app->view_dispatcher, bad_usb_app_tick_event_callback, 500);
|
|
|
|
view_dispatcher_set_custom_event_callback(
|
|
|
|
app->view_dispatcher, bad_usb_app_custom_event_callback);
|
|
|
|
view_dispatcher_set_navigation_event_callback(
|
|
|
|
app->view_dispatcher, bad_usb_app_back_event_callback);
|
|
|
|
|
2022-02-02 14:27:33 +00:00
|
|
|
// Custom Widget
|
|
|
|
app->widget = widget_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
app->view_dispatcher, BadUsbAppViewError, widget_get_view(app->widget));
|
|
|
|
|
2024-03-25 08:39:33 +00:00
|
|
|
app->var_item_list = variable_item_list_alloc();
|
2023-02-08 15:01:00 +00:00
|
|
|
view_dispatcher_add_view(
|
2024-03-25 08:39:33 +00:00
|
|
|
app->view_dispatcher,
|
|
|
|
BadUsbAppViewConfig,
|
|
|
|
variable_item_list_get_view(app->var_item_list));
|
2023-02-08 15:01:00 +00:00
|
|
|
|
2024-03-25 08:39:33 +00:00
|
|
|
app->bad_usb_view = bad_usb_view_alloc();
|
2021-11-26 13:53:51 +00:00
|
|
|
view_dispatcher_add_view(
|
2024-03-25 08:39:33 +00:00
|
|
|
app->view_dispatcher, BadUsbAppViewWork, bad_usb_view_get_view(app->bad_usb_view));
|
2021-11-26 13:53:51 +00:00
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
|
|
|
|
|
2022-03-24 15:45:03 +00:00
|
|
|
if(furi_hal_usb_is_locked()) {
|
|
|
|
app->error = BadUsbAppErrorCloseRpc;
|
2023-02-13 15:43:29 +00:00
|
|
|
app->usb_if_prev = NULL;
|
2022-02-02 14:27:33 +00:00
|
|
|
scene_manager_next_scene(app->scene_manager, BadUsbSceneError);
|
2022-03-24 15:45:03 +00:00
|
|
|
} else {
|
2023-02-13 15:43:29 +00:00
|
|
|
app->usb_if_prev = furi_hal_usb_get_config();
|
|
|
|
furi_check(furi_hal_usb_set_config(NULL, NULL));
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(!furi_string_empty(app->file_path)) {
|
2022-03-24 15:45:03 +00:00
|
|
|
scene_manager_next_scene(app->scene_manager, BadUsbSceneWork);
|
|
|
|
} else {
|
2023-02-08 15:01:00 +00:00
|
|
|
furi_string_set(app->file_path, BAD_USB_APP_BASE_FOLDER);
|
2022-05-27 11:19:21 +00:00
|
|
|
scene_manager_next_scene(app->scene_manager, BadUsbSceneFileSelect);
|
2022-03-24 15:45:03 +00:00
|
|
|
}
|
2022-02-02 14:27:33 +00:00
|
|
|
}
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bad_usb_app_free(BadUsbApp* app) {
|
|
|
|
furi_assert(app);
|
|
|
|
|
2023-02-08 15:01:00 +00:00
|
|
|
if(app->bad_usb_script) {
|
|
|
|
bad_usb_script_close(app->bad_usb_script);
|
|
|
|
app->bad_usb_script = NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
// Views
|
|
|
|
view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewWork);
|
2024-03-25 08:39:33 +00:00
|
|
|
bad_usb_view_free(app->bad_usb_view);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
2022-02-02 14:27:33 +00:00
|
|
|
// Custom Widget
|
|
|
|
view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewError);
|
|
|
|
widget_free(app->widget);
|
|
|
|
|
2024-03-25 08:39:33 +00:00
|
|
|
// Config menu
|
2023-02-08 15:01:00 +00:00
|
|
|
view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewConfig);
|
2024-03-25 08:39:33 +00:00
|
|
|
variable_item_list_free(app->var_item_list);
|
2023-02-08 15:01:00 +00:00
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
// View dispatcher
|
|
|
|
view_dispatcher_free(app->view_dispatcher);
|
|
|
|
scene_manager_free(app->scene_manager);
|
|
|
|
|
|
|
|
// Close records
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_GUI);
|
|
|
|
furi_record_close(RECORD_NOTIFICATION);
|
|
|
|
furi_record_close(RECORD_DIALOGS);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
2023-02-08 15:01:00 +00:00
|
|
|
bad_usb_save_settings(app);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(app->file_path);
|
2023-02-08 15:01:00 +00:00
|
|
|
furi_string_free(app->keyboard_layout);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
2023-02-27 14:04:14 +00:00
|
|
|
if(app->usb_if_prev) {
|
|
|
|
furi_check(furi_hal_usb_set_config(app->usb_if_prev, NULL));
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
free(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t bad_usb_app(void* p) {
|
2022-02-10 13:01:49 +00:00
|
|
|
BadUsbApp* bad_usb_app = bad_usb_app_alloc((char*)p);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
view_dispatcher_run(bad_usb_app->view_dispatcher);
|
|
|
|
|
|
|
|
bad_usb_app_free(bad_usb_app);
|
|
|
|
return 0;
|
|
|
|
}
|