weebo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "weebo_i.h"
  2. #define TAG "weebo"
  3. #define WEEBO_KEY_RETAIL_FILENAME "key_retail"
  4. #define FIGURE_ID_LIST APP_ASSETS_PATH("figure_ids.nfc")
  5. #define UNPACKED_FIGURE_ID 0x1dc
  6. #define NFC_APP_EXTENSION ".nfc"
  7. #define NFC_APP_PATH_PREFIX "/ext/nfc"
  8. static const char* nfc_resources_header = "Flipper NFC resources";
  9. static const uint32_t nfc_resources_file_version = 1;
  10. bool weebo_load_key_retail(Weebo* weebo) {
  11. FuriString* path = furi_string_alloc();
  12. bool parsed = false;
  13. uint8_t buffer[160];
  14. memset(buffer, 0, sizeof(buffer));
  15. Storage* storage = furi_record_open(RECORD_STORAGE);
  16. Stream* stream = file_stream_alloc(storage);
  17. do {
  18. furi_string_printf(
  19. path, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, WEEBO_KEY_RETAIL_FILENAME, ".bin");
  20. bool opened =
  21. file_stream_open(stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING);
  22. if(!opened) {
  23. FURI_LOG_E(TAG, "Failed to open file");
  24. break;
  25. }
  26. size_t bytes_read = stream_read(stream, buffer, sizeof(buffer));
  27. if(bytes_read != sizeof(buffer)) {
  28. FURI_LOG_E(TAG, "Insufficient data");
  29. break;
  30. }
  31. memcpy(&weebo->keys, buffer, bytes_read);
  32. // TODO: compare SHA1
  33. parsed = true;
  34. } while(false);
  35. file_stream_close(stream);
  36. furi_record_close(RECORD_STORAGE);
  37. furi_string_free(path);
  38. return parsed;
  39. }
  40. bool weebo_load_figure(Weebo* weebo, FuriString* path, bool show_dialog) {
  41. bool parsed = false;
  42. FuriString* reason = furi_string_alloc_set("Couldn't load file");
  43. uint8_t buffer[NTAG215_SIZE];
  44. memset(buffer, 0, sizeof(buffer));
  45. if(weebo->loading_cb) {
  46. weebo->loading_cb(weebo->loading_cb_ctx, true);
  47. }
  48. do {
  49. NfcDevice* nfc_device = weebo->nfc_device;
  50. if(!nfc_device_load(nfc_device, furi_string_get_cstr(path))) break;
  51. NfcProtocol protocol = nfc_device_get_protocol(nfc_device);
  52. if(protocol != NfcProtocolMfUltralight) {
  53. furi_string_printf(reason, "Not Ultralight protocol");
  54. break;
  55. }
  56. const MfUltralightData* data = nfc_device_get_data(nfc_device, NfcProtocolMfUltralight);
  57. if(data->type != MfUltralightTypeNTAG215) {
  58. furi_string_printf(reason, "Not NTAG215");
  59. break;
  60. }
  61. if(!mf_ultralight_is_all_data_read(data)) {
  62. furi_string_printf(reason, "Incomplete data");
  63. break;
  64. }
  65. uint8_t* uid = data->iso14443_3a_data->uid;
  66. uint8_t pwd[4];
  67. weebo_calculate_pwd(uid, pwd);
  68. if(memcmp(data->page[133].data, pwd, sizeof(pwd)) != 0) {
  69. furi_string_printf(reason, "Wrong password");
  70. break;
  71. }
  72. for(size_t i = 0; i < 135; i++) {
  73. memcpy(
  74. buffer + i * MF_ULTRALIGHT_PAGE_SIZE, data->page[i].data, MF_ULTRALIGHT_PAGE_SIZE);
  75. }
  76. if(!nfc3d_amiibo_unpack(&weebo->keys, buffer, weebo->figure)) {
  77. FURI_LOG_E(TAG, "Failed to unpack");
  78. break;
  79. }
  80. parsed = true;
  81. } while(false);
  82. if(weebo->loading_cb) {
  83. weebo->loading_cb(weebo->loading_cb_ctx, false);
  84. }
  85. if((!parsed) && (show_dialog)) {
  86. dialog_message_show_storage_error(weebo->dialogs, furi_string_get_cstr(reason));
  87. }
  88. furi_string_free(reason);
  89. return parsed;
  90. }
  91. static bool
  92. weebo_search_data(Storage* storage, const char* file_name, FuriString* key, FuriString* data) {
  93. bool parsed = false;
  94. FlipperFormat* file = flipper_format_file_alloc(storage);
  95. FuriString* temp_str;
  96. temp_str = furi_string_alloc();
  97. do {
  98. // Open file
  99. if(!flipper_format_file_open_existing(file, file_name)) break;
  100. // Read file header and version
  101. uint32_t version = 0;
  102. if(!flipper_format_read_header(file, temp_str, &version)) break;
  103. if(furi_string_cmp_str(temp_str, nfc_resources_header) ||
  104. (version != nfc_resources_file_version))
  105. break;
  106. if(!flipper_format_read_string(file, furi_string_get_cstr(key), data)) break;
  107. parsed = true;
  108. } while(false);
  109. furi_string_free(temp_str);
  110. flipper_format_free(file);
  111. return parsed;
  112. }
  113. uint16_t weebo_get_figure_id(Weebo* weebo) {
  114. uint16_t id = 0;
  115. id |= weebo->figure[UNPACKED_FIGURE_ID + 0] << 8;
  116. id |= weebo->figure[UNPACKED_FIGURE_ID + 1] << 0;
  117. FURI_LOG_D(TAG, "id = %04x", id);
  118. return id;
  119. }
  120. bool weebo_get_figure_name(Weebo* weebo, FuriString* name) {
  121. bool parsed = false;
  122. uint16_t id = weebo_get_figure_id(weebo);
  123. FuriString* key = furi_string_alloc_printf("%04x", id);
  124. if(weebo_search_data(weebo->storage, FIGURE_ID_LIST, key, name)) {
  125. parsed = true;
  126. }
  127. furi_string_free(key);
  128. return parsed;
  129. }
  130. void weebo_set_loading_callback(Weebo* weebo, WeeboLoadingCallback callback, void* context) {
  131. furi_assert(weebo);
  132. weebo->loading_cb = callback;
  133. weebo->loading_cb_ctx = context;
  134. }
  135. bool weebo_file_select(Weebo* weebo) {
  136. furi_assert(weebo);
  137. bool res = false;
  138. FuriString* weebo_app_folder;
  139. if(storage_dir_exists(weebo->storage, "/ext/nfc/SmashAmiibo")) {
  140. weebo_app_folder = furi_string_alloc_set("/ext/nfc/SmashAmiibo");
  141. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/Amiibo")) {
  142. weebo_app_folder = furi_string_alloc_set("/ext/nfc/Amiibo");
  143. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/Amiibos")) {
  144. weebo_app_folder = furi_string_alloc_set("/ext/nfc/Amiibos");
  145. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/amiibo")) {
  146. weebo_app_folder = furi_string_alloc_set("/ext/nfc/amiibo");
  147. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/amiibos")) {
  148. weebo_app_folder = furi_string_alloc_set("/ext/nfc/amiibos");
  149. } else {
  150. weebo_app_folder = furi_string_alloc_set(NFC_APP_PATH_PREFIX);
  151. }
  152. DialogsFileBrowserOptions browser_options;
  153. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  154. browser_options.base_path = NFC_APP_PATH_PREFIX;
  155. res = dialog_file_browser_show(
  156. weebo->dialogs, weebo->load_path, weebo_app_folder, &browser_options);
  157. furi_string_free(weebo_app_folder);
  158. if(res) {
  159. FuriString* filename;
  160. filename = furi_string_alloc();
  161. path_extract_filename(weebo->load_path, filename, true);
  162. strncpy(weebo->file_name, furi_string_get_cstr(filename), WEEBO_FILE_NAME_MAX_LENGTH);
  163. res = weebo_load_figure(weebo, weebo->load_path, true);
  164. furi_string_free(filename);
  165. }
  166. return res;
  167. }
  168. bool weebo_custom_event_callback(void* context, uint32_t event) {
  169. furi_assert(context);
  170. Weebo* weebo = context;
  171. return scene_manager_handle_custom_event(weebo->scene_manager, event);
  172. }
  173. bool weebo_back_event_callback(void* context) {
  174. furi_assert(context);
  175. Weebo* weebo = context;
  176. return scene_manager_handle_back_event(weebo->scene_manager);
  177. }
  178. void weebo_tick_event_callback(void* context) {
  179. furi_assert(context);
  180. Weebo* weebo = context;
  181. scene_manager_handle_tick_event(weebo->scene_manager);
  182. }
  183. Weebo* weebo_alloc() {
  184. Weebo* weebo = malloc(sizeof(Weebo));
  185. weebo->view_dispatcher = view_dispatcher_alloc();
  186. weebo->scene_manager = scene_manager_alloc(&weebo_scene_handlers, weebo);
  187. view_dispatcher_set_event_callback_context(weebo->view_dispatcher, weebo);
  188. view_dispatcher_set_custom_event_callback(weebo->view_dispatcher, weebo_custom_event_callback);
  189. view_dispatcher_set_navigation_event_callback(
  190. weebo->view_dispatcher, weebo_back_event_callback);
  191. view_dispatcher_set_tick_event_callback(
  192. weebo->view_dispatcher, weebo_tick_event_callback, 100);
  193. weebo->nfc = nfc_alloc();
  194. // Nfc device
  195. weebo->nfc_device = nfc_device_alloc();
  196. nfc_device_set_loading_callback(weebo->nfc_device, weebo_show_loading_popup, weebo);
  197. // Open GUI record
  198. weebo->gui = furi_record_open(RECORD_GUI);
  199. view_dispatcher_attach_to_gui(
  200. weebo->view_dispatcher, weebo->gui, ViewDispatcherTypeFullscreen);
  201. // Open Notification record
  202. weebo->notifications = furi_record_open(RECORD_NOTIFICATION);
  203. // Submenu
  204. weebo->submenu = submenu_alloc();
  205. view_dispatcher_add_view(
  206. weebo->view_dispatcher, WeeboViewMenu, submenu_get_view(weebo->submenu));
  207. // Popup
  208. weebo->popup = popup_alloc();
  209. view_dispatcher_add_view(weebo->view_dispatcher, WeeboViewPopup, popup_get_view(weebo->popup));
  210. // Loading
  211. weebo->loading = loading_alloc();
  212. view_dispatcher_add_view(
  213. weebo->view_dispatcher, WeeboViewLoading, loading_get_view(weebo->loading));
  214. // Text Input
  215. weebo->text_input = text_input_alloc();
  216. view_dispatcher_add_view(
  217. weebo->view_dispatcher, WeeboViewTextInput, text_input_get_view(weebo->text_input));
  218. // Number Input
  219. weebo->number_input = number_input_alloc();
  220. view_dispatcher_add_view(
  221. weebo->view_dispatcher, WeeboViewNumberInput, number_input_get_view(weebo->number_input));
  222. // TextBox
  223. weebo->text_box = text_box_alloc();
  224. view_dispatcher_add_view(
  225. weebo->view_dispatcher, WeeboViewTextBox, text_box_get_view(weebo->text_box));
  226. weebo->text_box_store = furi_string_alloc();
  227. // Custom Widget
  228. weebo->widget = widget_alloc();
  229. view_dispatcher_add_view(
  230. weebo->view_dispatcher, WeeboViewWidget, widget_get_view(weebo->widget));
  231. weebo->storage = furi_record_open(RECORD_STORAGE);
  232. weebo->dialogs = furi_record_open(RECORD_DIALOGS);
  233. weebo->load_path = furi_string_alloc();
  234. weebo->keys_loaded = false;
  235. return weebo;
  236. }
  237. void weebo_free(Weebo* weebo) {
  238. furi_assert(weebo);
  239. nfc_free(weebo->nfc);
  240. // Nfc device
  241. nfc_device_free(weebo->nfc_device);
  242. // Submenu
  243. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewMenu);
  244. submenu_free(weebo->submenu);
  245. // Popup
  246. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewPopup);
  247. popup_free(weebo->popup);
  248. // Loading
  249. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewLoading);
  250. loading_free(weebo->loading);
  251. // TextInput
  252. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewTextInput);
  253. text_input_free(weebo->text_input);
  254. // NumberInput
  255. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewNumberInput);
  256. number_input_free(weebo->number_input);
  257. // TextBox
  258. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewTextBox);
  259. text_box_free(weebo->text_box);
  260. furi_string_free(weebo->text_box_store);
  261. // Custom Widget
  262. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewWidget);
  263. widget_free(weebo->widget);
  264. // View Dispatcher
  265. view_dispatcher_free(weebo->view_dispatcher);
  266. // Scene Manager
  267. scene_manager_free(weebo->scene_manager);
  268. // GUI
  269. furi_record_close(RECORD_GUI);
  270. weebo->gui = NULL;
  271. // Notifications
  272. furi_record_close(RECORD_NOTIFICATION);
  273. weebo->notifications = NULL;
  274. furi_string_free(weebo->load_path);
  275. furi_record_close(RECORD_STORAGE);
  276. furi_record_close(RECORD_DIALOGS);
  277. free(weebo);
  278. }
  279. void weebo_text_store_set(Weebo* weebo, const char* text, ...) {
  280. va_list args;
  281. va_start(args, text);
  282. vsnprintf(weebo->text_store, sizeof(weebo->text_store), text, args);
  283. va_end(args);
  284. }
  285. void weebo_text_store_clear(Weebo* weebo) {
  286. memset(weebo->text_store, 0, sizeof(weebo->text_store));
  287. }
  288. static const NotificationSequence weebo_sequence_blink_start_blue = {
  289. &message_blink_start_10,
  290. &message_blink_set_color_blue,
  291. &message_do_not_reset,
  292. NULL,
  293. };
  294. static const NotificationSequence weebo_sequence_blink_stop = {
  295. &message_blink_stop,
  296. NULL,
  297. };
  298. void weebo_blink_start(Weebo* weebo) {
  299. notification_message(weebo->notifications, &weebo_sequence_blink_start_blue);
  300. }
  301. void weebo_blink_stop(Weebo* weebo) {
  302. notification_message(weebo->notifications, &weebo_sequence_blink_stop);
  303. }
  304. void weebo_show_loading_popup(void* context, bool show) {
  305. Weebo* weebo = context;
  306. if(show) {
  307. // Raise timer priority so that animations can play
  308. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  309. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewLoading);
  310. } else {
  311. // Restore default timer priority
  312. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  313. }
  314. }
  315. int32_t weebo_app(void* p) {
  316. UNUSED(p);
  317. Weebo* weebo = weebo_alloc();
  318. weebo->keys_loaded = weebo_load_key_retail(weebo);
  319. if(weebo->keys_loaded) {
  320. scene_manager_next_scene(weebo->scene_manager, WeeboSceneMainMenu);
  321. } else {
  322. scene_manager_next_scene(weebo->scene_manager, WeeboSceneKeysMissing);
  323. }
  324. view_dispatcher_run(weebo->view_dispatcher);
  325. weebo_free(weebo);
  326. return 0;
  327. }