mfc_editor_app.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "mfc_editor_app_i.h"
  2. const char* access_data_block_labels[8] = {
  3. // C3, C2, C1
  4. "Key A: Read, Write, Inc, Dec\nKey B: Read, Write, Inc, Dec", // 000
  5. "Key A: Read\nKey B: Read, Write", // 001
  6. "Key A: Read\nKey B: Read", // 010
  7. "Key A: Read, Dec\nKey B: Read, Write, Inc, Dec", // 011
  8. "Key A: Read, Dec\nKey B: Read, Dec", // 100
  9. "Key A: No Access\nKey B: Read", // 101
  10. "Key A: No Access\nKey B: Read, Write", // 110
  11. "Key A: No Access\nKey B: No Access", // 111
  12. };
  13. const char* access_sector_trailer_labels[8] = {
  14. // C3, C2, C1
  15. "Key A: KA-W, AB-R, KB-RW\nKey B: No Access", // 000
  16. "Key A: AB-R\nKey B: KA+KB-W, AB-R", // 001
  17. "Key A: AB+KB-R\nKey B: No Access", // 010
  18. "Key A: AB-R\nKey B: AB-R", // 011
  19. "Key A: KA-W, AB+KB-RW\nKey B: No Access", // 100
  20. "Key A: AB-R\nKey B: AB-RW", // 101
  21. "Key A: AB-R\nKey B: KA+KB-W, AB-RW", // 110
  22. "Key A: AB-R\nKey B: AB-R", // 111
  23. };
  24. bool mfc_editor_app_custom_event_callback(void* context, uint32_t event) {
  25. furi_assert(context);
  26. MfcEditorApp* instance = context;
  27. return scene_manager_handle_custom_event(instance->scene_manager, event);
  28. }
  29. bool mfc_editor_app_back_event_callback(void* context) {
  30. furi_assert(context);
  31. MfcEditorApp* instance = context;
  32. return scene_manager_handle_back_event(instance->scene_manager);
  33. }
  34. void mfc_editor_app_tick_event_callback(void* context) {
  35. furi_assert(context);
  36. MfcEditorApp* instance = context;
  37. scene_manager_handle_tick_event(instance->scene_manager);
  38. }
  39. MfcEditorApp* mfc_editor_app_alloc() {
  40. MfcEditorApp* instance = malloc(sizeof(MfcEditorApp));
  41. instance->view_dispatcher = view_dispatcher_alloc();
  42. instance->scene_manager = scene_manager_alloc(&mfc_editor_scene_handlers, instance);
  43. view_dispatcher_enable_queue(instance->view_dispatcher);
  44. view_dispatcher_set_event_callback_context(instance->view_dispatcher, instance);
  45. view_dispatcher_set_custom_event_callback(
  46. instance->view_dispatcher, mfc_editor_app_custom_event_callback);
  47. view_dispatcher_set_navigation_event_callback(
  48. instance->view_dispatcher, mfc_editor_app_back_event_callback);
  49. view_dispatcher_set_tick_event_callback(
  50. instance->view_dispatcher, mfc_editor_app_tick_event_callback, 100);
  51. instance->gui = furi_record_open(RECORD_GUI);
  52. view_dispatcher_attach_to_gui(
  53. instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen);
  54. instance->storage = furi_record_open(RECORD_STORAGE);
  55. instance->dialogs = furi_record_open(RECORD_DIALOGS);
  56. instance->mf_classic_data = mf_classic_alloc();
  57. instance->file_path = furi_string_alloc_set(NFC_APP_FOLDER);
  58. instance->data_view_header = furi_string_alloc();
  59. instance->data_view_text = furi_string_alloc();
  60. instance->submenu = submenu_alloc();
  61. view_dispatcher_add_view(
  62. instance->view_dispatcher, MfcEditorAppViewSubmenu, submenu_get_view(instance->submenu));
  63. instance->popup = popup_alloc();
  64. view_dispatcher_add_view(
  65. instance->view_dispatcher, MfcEditorAppViewPopup, popup_get_view(instance->popup));
  66. instance->dialog_ex = dialog_ex_alloc();
  67. view_dispatcher_add_view(
  68. instance->view_dispatcher,
  69. MfcEditorAppViewDialogEx,
  70. dialog_ex_get_view(instance->dialog_ex));
  71. instance->byte_input = byte_input_alloc();
  72. view_dispatcher_add_view(
  73. instance->view_dispatcher,
  74. MfcEditorAppViewByteInput,
  75. byte_input_get_view(instance->byte_input));
  76. return instance;
  77. }
  78. void mfc_editor_app_free(MfcEditorApp* instance) {
  79. furi_assert(instance);
  80. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewSubmenu);
  81. submenu_free(instance->submenu);
  82. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewPopup);
  83. popup_free(instance->popup);
  84. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewDialogEx);
  85. dialog_ex_free(instance->dialog_ex);
  86. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewByteInput);
  87. byte_input_free(instance->byte_input);
  88. view_dispatcher_free(instance->view_dispatcher);
  89. scene_manager_free(instance->scene_manager);
  90. furi_record_close(RECORD_GUI);
  91. instance->gui = NULL;
  92. furi_record_close(RECORD_STORAGE);
  93. instance->storage = NULL;
  94. furi_record_close(RECORD_DIALOGS);
  95. instance->dialogs = NULL;
  96. mf_classic_free(instance->mf_classic_data);
  97. furi_string_free(instance->file_path);
  98. furi_string_free(instance->data_view_header);
  99. furi_string_free(instance->data_view_text);
  100. free(instance);
  101. }
  102. MfcEditorPromptResponse mfc_editor_load_file(MfcEditorApp* instance, FuriString* file_path) {
  103. furi_assert(instance);
  104. furi_assert(file_path);
  105. MfcEditorPromptResponse result = MfcEditorPromptResponseSuccess;
  106. NfcDevice* nfc_device = nfc_device_alloc();
  107. if(!nfc_device_load(nfc_device, furi_string_get_cstr(file_path))) {
  108. result = MfcEditorPromptResponseFailure;
  109. dialog_message_show_storage_error(instance->dialogs, "Cannot load\nkey file");
  110. } else {
  111. if(nfc_device_get_protocol(nfc_device) == NfcProtocolMfClassic) {
  112. const MfClassicData* mf_classic_data =
  113. nfc_device_get_data(nfc_device, NfcProtocolMfClassic);
  114. mf_classic_copy(instance->mf_classic_data, mf_classic_data);
  115. } else {
  116. result = MfcEditorPromptResponseNotMfClassic;
  117. }
  118. }
  119. nfc_device_free(nfc_device);
  120. return result;
  121. }
  122. static DialogMessageButton mfc_editor_prompt_should_load_shadow(MfcEditorApp* instance) {
  123. DialogMessage* message = dialog_message_alloc();
  124. dialog_message_set_header(message, "File has modifications", 63, 3, AlignCenter, AlignTop);
  125. dialog_message_set_text(
  126. message,
  127. "Would you like to load the\nmodified file (recommended)\nor the original file?",
  128. 63,
  129. 31,
  130. AlignCenter,
  131. AlignCenter);
  132. dialog_message_set_buttons(message, "Original", NULL, "Modified");
  133. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  134. dialog_message_free(message);
  135. return message_button;
  136. }
  137. static void mfc_editor_get_shadow_file_path(FuriString* file_path, FuriString* shadow_file_path) {
  138. furi_assert(file_path);
  139. furi_assert(shadow_file_path);
  140. // Remove NFC extension from end of string then append shadow extension
  141. furi_string_set_n(shadow_file_path, file_path, 0, furi_string_size(file_path) - 4);
  142. furi_string_cat_printf(shadow_file_path, "%s", NFC_APP_SHADOW_EXTENSION);
  143. }
  144. static bool mfc_editor_file_has_shadow_file(MfcEditorApp* instance, FuriString* file_path) {
  145. furi_assert(instance);
  146. furi_assert(file_path);
  147. FuriString* shadow_file_path = furi_string_alloc();
  148. mfc_editor_get_shadow_file_path(file_path, shadow_file_path);
  149. bool has_shadow_file =
  150. storage_common_exists(instance->storage, furi_string_get_cstr(shadow_file_path));
  151. furi_string_free(shadow_file_path);
  152. return has_shadow_file;
  153. }
  154. MfcEditorPromptResponse mfc_editor_prompt_load_file(MfcEditorApp* instance) {
  155. furi_assert(instance);
  156. DialogsFileBrowserOptions browser_options;
  157. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  158. browser_options.base_path = NFC_APP_FOLDER;
  159. browser_options.hide_dot_files = true;
  160. MfcEditorPromptResponse result = MfcEditorPromptResponseSuccess;
  161. if(!dialog_file_browser_show(
  162. instance->dialogs, instance->file_path, instance->file_path, &browser_options)) {
  163. result = MfcEditorPromptResponseExitedFile;
  164. } else {
  165. if(mfc_editor_file_has_shadow_file(instance, instance->file_path)) {
  166. DialogMessageButton message_button = mfc_editor_prompt_should_load_shadow(instance);
  167. if(message_button == DialogMessageButtonRight) {
  168. // User selected to use shadow file, so replace selected path with that path
  169. FuriString* shadow_file_path = furi_string_alloc();
  170. mfc_editor_get_shadow_file_path(instance->file_path, shadow_file_path);
  171. furi_string_set(instance->file_path, shadow_file_path);
  172. furi_string_free(shadow_file_path);
  173. } else if(message_button == DialogMessageButtonBack) {
  174. result = MfcEditorPromptResponseExitedShadow;
  175. }
  176. }
  177. // Don't load the file if user was prompted for shadow file use but went back
  178. if(result == MfcEditorPromptResponseSuccess) {
  179. result = mfc_editor_load_file(instance, instance->file_path);
  180. }
  181. }
  182. return result;
  183. }
  184. bool mfc_editor_warn_risky_operation(MfcEditorApp* instance) {
  185. DialogMessage* message = dialog_message_alloc();
  186. dialog_message_set_header(message, "Risky operation", 63, 3, AlignCenter, AlignTop);
  187. dialog_message_set_text(
  188. message,
  189. "Changing this data may\ninhibit writing to the card\nor could brick the card.",
  190. 63,
  191. 31,
  192. AlignCenter,
  193. AlignCenter);
  194. dialog_message_set_buttons(message, "Back", "Continue", NULL);
  195. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  196. dialog_message_free(message);
  197. return message_button == DialogMessageButtonCenter;
  198. }
  199. int32_t mfc_editor_app(void* p) {
  200. UNUSED(p);
  201. MfcEditorApp* instance = mfc_editor_app_alloc();
  202. scene_manager_next_scene(instance->scene_manager, MfcEditorSceneStart);
  203. view_dispatcher_run(instance->view_dispatcher);
  204. mfc_editor_app_free(instance);
  205. return 0;
  206. }