mfc_editor_app.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. instance->is_unsaved_changes = false;
  116. } else {
  117. result = MfcEditorPromptResponseNotMfClassic;
  118. }
  119. }
  120. nfc_device_free(nfc_device);
  121. return result;
  122. }
  123. bool mfc_editor_save_file(MfcEditorApp* instance) {
  124. furi_assert(instance);
  125. furi_assert(instance->file_path);
  126. furi_assert(instance->mf_classic_data);
  127. NfcDevice* nfc_device = nfc_device_alloc();
  128. nfc_device_set_data(nfc_device, NfcProtocolMfClassic, instance->mf_classic_data);
  129. bool result = nfc_device_save(nfc_device, furi_string_get_cstr(instance->file_path));
  130. if(!result) {
  131. dialog_message_show_storage_error(instance->dialogs, "Cannot save\nkey file");
  132. }
  133. nfc_device_free(nfc_device);
  134. return result;
  135. }
  136. static DialogMessageButton mfc_editor_prompt_should_load_shadow(MfcEditorApp* instance) {
  137. DialogMessage* message = dialog_message_alloc();
  138. dialog_message_set_header(message, "File has modifications", 63, 3, AlignCenter, AlignTop);
  139. dialog_message_set_text(
  140. message,
  141. "Would you like to edit the\nmodified file (recommended)\nor the original file?",
  142. 63,
  143. 31,
  144. AlignCenter,
  145. AlignCenter);
  146. dialog_message_set_buttons(message, "Original", NULL, "Modified");
  147. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  148. dialog_message_free(message);
  149. return message_button;
  150. }
  151. static void mfc_editor_get_shadow_file_path(FuriString* file_path, FuriString* shadow_file_path) {
  152. furi_assert(file_path);
  153. furi_assert(shadow_file_path);
  154. // Remove NFC extension from end of string then append shadow extension
  155. furi_string_set_n(shadow_file_path, file_path, 0, furi_string_size(file_path) - 4);
  156. furi_string_cat_printf(shadow_file_path, "%s", NFC_APP_SHADOW_EXTENSION);
  157. }
  158. static bool mfc_editor_file_has_shadow_file(MfcEditorApp* instance, FuriString* file_path) {
  159. furi_assert(instance);
  160. furi_assert(file_path);
  161. FuriString* shadow_file_path = furi_string_alloc();
  162. mfc_editor_get_shadow_file_path(file_path, shadow_file_path);
  163. bool has_shadow_file =
  164. storage_common_exists(instance->storage, furi_string_get_cstr(shadow_file_path));
  165. furi_string_free(shadow_file_path);
  166. return has_shadow_file;
  167. }
  168. MfcEditorPromptResponse mfc_editor_prompt_load_file(MfcEditorApp* instance) {
  169. furi_assert(instance);
  170. DialogsFileBrowserOptions browser_options;
  171. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  172. browser_options.base_path = NFC_APP_FOLDER;
  173. browser_options.hide_dot_files = true;
  174. MfcEditorPromptResponse result = MfcEditorPromptResponseSuccess;
  175. if(!dialog_file_browser_show(
  176. instance->dialogs, instance->file_path, instance->file_path, &browser_options)) {
  177. result = MfcEditorPromptResponseExitedFile;
  178. } else {
  179. if(mfc_editor_file_has_shadow_file(instance, instance->file_path)) {
  180. DialogMessageButton message_button = mfc_editor_prompt_should_load_shadow(instance);
  181. if(message_button == DialogMessageButtonRight) {
  182. // User selected to use shadow file, so replace selected path with that path
  183. FuriString* shadow_file_path = furi_string_alloc();
  184. mfc_editor_get_shadow_file_path(instance->file_path, shadow_file_path);
  185. furi_string_set(instance->file_path, shadow_file_path);
  186. furi_string_free(shadow_file_path);
  187. } else if(message_button == DialogMessageButtonBack) {
  188. result = MfcEditorPromptResponseExitedShadow;
  189. }
  190. }
  191. // Don't load the file if user was prompted for shadow file use but went back
  192. if(result == MfcEditorPromptResponseSuccess) {
  193. result = mfc_editor_load_file(instance, instance->file_path);
  194. }
  195. }
  196. return result;
  197. }
  198. bool mfc_editor_warn_risky_operation(MfcEditorApp* instance) {
  199. DialogMessage* message = dialog_message_alloc();
  200. dialog_message_set_header(message, "Risky operation", 63, 3, AlignCenter, AlignTop);
  201. dialog_message_set_text(
  202. message,
  203. "Changing this data may\ninhibit writing to the card\nor could brick the card.",
  204. 63,
  205. 31,
  206. AlignCenter,
  207. AlignCenter);
  208. dialog_message_set_buttons(message, "Back", "Continue", NULL);
  209. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  210. dialog_message_free(message);
  211. return message_button == DialogMessageButtonCenter;
  212. }
  213. MfcEditorSaveResponse mfc_editor_warn_unsaved_changes(MfcEditorApp* instance) {
  214. DialogMessage* message = dialog_message_alloc();
  215. dialog_message_set_header(message, "Unsaved changes", 63, 3, AlignCenter, AlignTop);
  216. dialog_message_set_text(
  217. message,
  218. "Would you like to save?\nDiscarding your\nchanges is permanent.",
  219. 63,
  220. 31,
  221. AlignCenter,
  222. AlignCenter);
  223. dialog_message_set_buttons(message, "Discrd", "Save", "Cancel");
  224. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  225. dialog_message_free(message);
  226. if(message_button == DialogMessageButtonCenter) {
  227. return MfcEditorSaveResponseSave;
  228. } else if(message_button == DialogMessageButtonLeft) {
  229. return MfcEditorSaveResponseDiscard;
  230. } else {
  231. return MfcEditorSaveResponseCancel;
  232. }
  233. }
  234. int32_t mfc_editor_app(void* p) {
  235. UNUSED(p);
  236. MfcEditorApp* instance = mfc_editor_app_alloc();
  237. scene_manager_next_scene(instance->scene_manager, MfcEditorSceneFileSelect);
  238. view_dispatcher_run(instance->view_dispatcher);
  239. mfc_editor_app_free(instance);
  240. return 0;
  241. }