mfc_editor_app.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_set_event_callback_context(instance->view_dispatcher, instance);
  44. view_dispatcher_set_custom_event_callback(
  45. instance->view_dispatcher, mfc_editor_app_custom_event_callback);
  46. view_dispatcher_set_navigation_event_callback(
  47. instance->view_dispatcher, mfc_editor_app_back_event_callback);
  48. view_dispatcher_set_tick_event_callback(
  49. instance->view_dispatcher, mfc_editor_app_tick_event_callback, 100);
  50. instance->gui = furi_record_open(RECORD_GUI);
  51. view_dispatcher_attach_to_gui(
  52. instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen);
  53. instance->storage = furi_record_open(RECORD_STORAGE);
  54. instance->dialogs = furi_record_open(RECORD_DIALOGS);
  55. instance->mf_classic_data = mf_classic_alloc();
  56. instance->file_path = furi_string_alloc_set(NFC_APP_FOLDER);
  57. instance->data_view_header = furi_string_alloc();
  58. instance->data_view_text = furi_string_alloc();
  59. instance->submenu = submenu_alloc();
  60. view_dispatcher_add_view(
  61. instance->view_dispatcher, MfcEditorAppViewSubmenu, submenu_get_view(instance->submenu));
  62. instance->popup = popup_alloc();
  63. view_dispatcher_add_view(
  64. instance->view_dispatcher, MfcEditorAppViewPopup, popup_get_view(instance->popup));
  65. instance->dialog_ex = dialog_ex_alloc();
  66. view_dispatcher_add_view(
  67. instance->view_dispatcher,
  68. MfcEditorAppViewDialogEx,
  69. dialog_ex_get_view(instance->dialog_ex));
  70. instance->byte_input = byte_input_alloc();
  71. view_dispatcher_add_view(
  72. instance->view_dispatcher,
  73. MfcEditorAppViewByteInput,
  74. byte_input_get_view(instance->byte_input));
  75. return instance;
  76. }
  77. void mfc_editor_app_free(MfcEditorApp* instance) {
  78. furi_assert(instance);
  79. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewSubmenu);
  80. submenu_free(instance->submenu);
  81. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewPopup);
  82. popup_free(instance->popup);
  83. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewDialogEx);
  84. dialog_ex_free(instance->dialog_ex);
  85. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewByteInput);
  86. byte_input_free(instance->byte_input);
  87. view_dispatcher_free(instance->view_dispatcher);
  88. scene_manager_free(instance->scene_manager);
  89. furi_record_close(RECORD_GUI);
  90. instance->gui = NULL;
  91. furi_record_close(RECORD_STORAGE);
  92. instance->storage = NULL;
  93. furi_record_close(RECORD_DIALOGS);
  94. instance->dialogs = NULL;
  95. mf_classic_free(instance->mf_classic_data);
  96. furi_string_free(instance->file_path);
  97. furi_string_free(instance->data_view_header);
  98. furi_string_free(instance->data_view_text);
  99. free(instance);
  100. }
  101. MfcEditorPromptResponse mfc_editor_load_file(MfcEditorApp* instance, FuriString* file_path) {
  102. furi_assert(instance);
  103. furi_assert(file_path);
  104. MfcEditorPromptResponse result = MfcEditorPromptResponseSuccess;
  105. NfcDevice* nfc_device = nfc_device_alloc();
  106. if(!nfc_device_load(nfc_device, furi_string_get_cstr(file_path))) {
  107. result = MfcEditorPromptResponseFailure;
  108. dialog_message_show_storage_error(instance->dialogs, "Cannot load\nkey file");
  109. } else {
  110. if(nfc_device_get_protocol(nfc_device) == NfcProtocolMfClassic) {
  111. const MfClassicData* mf_classic_data =
  112. nfc_device_get_data(nfc_device, NfcProtocolMfClassic);
  113. mf_classic_copy(instance->mf_classic_data, mf_classic_data);
  114. instance->is_unsaved_changes = false;
  115. } else {
  116. result = MfcEditorPromptResponseNotMfClassic;
  117. }
  118. }
  119. nfc_device_free(nfc_device);
  120. return result;
  121. }
  122. bool mfc_editor_save_file(MfcEditorApp* instance) {
  123. furi_assert(instance);
  124. furi_assert(instance->file_path);
  125. furi_assert(instance->mf_classic_data);
  126. NfcDevice* nfc_device = nfc_device_alloc();
  127. nfc_device_set_data(nfc_device, NfcProtocolMfClassic, instance->mf_classic_data);
  128. bool result = nfc_device_save(nfc_device, furi_string_get_cstr(instance->file_path));
  129. if(!result) {
  130. dialog_message_show_storage_error(instance->dialogs, "Cannot save\nkey file");
  131. }
  132. nfc_device_free(nfc_device);
  133. return result;
  134. }
  135. static DialogMessageButton mfc_editor_prompt_should_load_shadow(MfcEditorApp* instance) {
  136. DialogMessage* message = dialog_message_alloc();
  137. dialog_message_set_header(message, "File has modifications", 63, 3, AlignCenter, AlignTop);
  138. dialog_message_set_text(
  139. message,
  140. "Would you like to edit the\nmodified file (recommended)\nor the original file?",
  141. 63,
  142. 31,
  143. AlignCenter,
  144. AlignCenter);
  145. dialog_message_set_buttons(message, "Original", NULL, "Modified");
  146. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  147. dialog_message_free(message);
  148. return message_button;
  149. }
  150. static void mfc_editor_get_shadow_file_path(FuriString* file_path, FuriString* shadow_file_path) {
  151. furi_assert(file_path);
  152. furi_assert(shadow_file_path);
  153. // Remove NFC extension from end of string then append shadow extension
  154. furi_string_set_n(shadow_file_path, file_path, 0, furi_string_size(file_path) - 4);
  155. furi_string_cat_printf(shadow_file_path, "%s", NFC_APP_SHADOW_EXTENSION);
  156. }
  157. static bool mfc_editor_file_has_shadow_file(MfcEditorApp* instance, FuriString* file_path) {
  158. furi_assert(instance);
  159. furi_assert(file_path);
  160. FuriString* shadow_file_path = furi_string_alloc();
  161. mfc_editor_get_shadow_file_path(file_path, shadow_file_path);
  162. bool has_shadow_file =
  163. storage_common_exists(instance->storage, furi_string_get_cstr(shadow_file_path));
  164. furi_string_free(shadow_file_path);
  165. return has_shadow_file;
  166. }
  167. MfcEditorPromptResponse mfc_editor_prompt_load_file(MfcEditorApp* instance) {
  168. furi_assert(instance);
  169. DialogsFileBrowserOptions browser_options;
  170. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  171. browser_options.base_path = NFC_APP_FOLDER;
  172. browser_options.hide_dot_files = true;
  173. MfcEditorPromptResponse result = MfcEditorPromptResponseSuccess;
  174. if(!dialog_file_browser_show(
  175. instance->dialogs, instance->file_path, instance->file_path, &browser_options)) {
  176. result = MfcEditorPromptResponseExitedFile;
  177. } else {
  178. if(mfc_editor_file_has_shadow_file(instance, instance->file_path)) {
  179. DialogMessageButton message_button = mfc_editor_prompt_should_load_shadow(instance);
  180. if(message_button == DialogMessageButtonRight) {
  181. // User selected to use shadow file, so replace selected path with that path
  182. FuriString* shadow_file_path = furi_string_alloc();
  183. mfc_editor_get_shadow_file_path(instance->file_path, shadow_file_path);
  184. furi_string_set(instance->file_path, shadow_file_path);
  185. furi_string_free(shadow_file_path);
  186. } else if(message_button == DialogMessageButtonBack) {
  187. result = MfcEditorPromptResponseExitedShadow;
  188. }
  189. }
  190. // Don't load the file if user was prompted for shadow file use but went back
  191. if(result == MfcEditorPromptResponseSuccess) {
  192. result = mfc_editor_load_file(instance, instance->file_path);
  193. }
  194. }
  195. return result;
  196. }
  197. bool mfc_editor_warn_risky_operation(MfcEditorApp* instance) {
  198. DialogMessage* message = dialog_message_alloc();
  199. dialog_message_set_header(message, "Risky operation", 63, 3, AlignCenter, AlignTop);
  200. dialog_message_set_text(
  201. message,
  202. "Changing this data may\ninhibit writing to the card\nor could brick the card.",
  203. 63,
  204. 31,
  205. AlignCenter,
  206. AlignCenter);
  207. dialog_message_set_buttons(message, "Back", "Continue", NULL);
  208. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  209. dialog_message_free(message);
  210. return message_button == DialogMessageButtonCenter;
  211. }
  212. MfcEditorSaveResponse mfc_editor_warn_unsaved_changes(MfcEditorApp* instance) {
  213. DialogMessage* message = dialog_message_alloc();
  214. dialog_message_set_header(message, "Unsaved changes", 63, 3, AlignCenter, AlignTop);
  215. dialog_message_set_text(
  216. message,
  217. "Would you like to save?\nDiscarding your\nchanges is permanent.",
  218. 63,
  219. 31,
  220. AlignCenter,
  221. AlignCenter);
  222. dialog_message_set_buttons(message, "Discrd", "Save", "Cancel");
  223. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  224. dialog_message_free(message);
  225. if(message_button == DialogMessageButtonCenter) {
  226. return MfcEditorSaveResponseSave;
  227. } else if(message_button == DialogMessageButtonLeft) {
  228. return MfcEditorSaveResponseDiscard;
  229. } else {
  230. return MfcEditorSaveResponseCancel;
  231. }
  232. }
  233. int32_t mfc_editor_app(void* p) {
  234. UNUSED(p);
  235. MfcEditorApp* instance = mfc_editor_app_alloc();
  236. scene_manager_next_scene(instance->scene_manager, MfcEditorSceneFileSelect);
  237. view_dispatcher_run(instance->view_dispatcher);
  238. mfc_editor_app_free(instance);
  239. return 0;
  240. }