From 0aae5408b8ef9e111359260bc3a730907e7d543f Mon Sep 17 00:00:00 2001 From: d0k3 Date: Sun, 29 May 2016 14:45:12 +0200 Subject: [PATCH] Totally revised write permission system ... now allows granular control --- source/fs.c | 103 ++++++++++++++++++++++++++++------------------- source/fs.h | 11 ++++- source/godmode.c | 19 ++++----- source/ui.c | 10 +++-- source/ui.h | 1 + 5 files changed, 87 insertions(+), 57 deletions(-) diff --git a/source/fs.c b/source/fs.c index e81c6e9..33b2626 100644 --- a/source/fs.c +++ b/source/fs.c @@ -14,8 +14,8 @@ // don't use this area for anything else! static FATFS* fs = (FATFS*)0x20316000; -// write permission level - careful with this -static u32 write_permission_level = 1; +// write permissions - careful with this +static u32 write_permissions = PERM_BASE; // number of currently open file systems static bool fs_mounted[NORM_FS] = { false }; @@ -79,72 +79,93 @@ bool IsMountedFS(const char* path) { } bool CheckWritePermissions(const char* path) { + char area_name[16]; int pdrv = PathToNumFS(path); - if (pdrv < 0) { - if (GetVirtualSource(path)) // this is a hack, but okay for now - pdrv = (GetVirtualSource(path) == VRT_MEMORY) ? 10 : - (GetVirtualSource(path) == VRT_SYSNAND) ? 1 : - (GetVirtualSource(path) == VRT_EMUNAND) ? 4 : 7; - else return false; - } else if ((pdrv == 7) && (GetMountState() == IMG_RAMDRV)) { - pdrv = 0; // ...and another hack + u32 perm; + + if (pdrv == 0) { + perm = PERM_SDCARD; + snprintf(area_name, 16, "the SD card"); + } else if (GetMountState() == IMG_RAMDRV) { + perm = PERM_RAMDRIVE; + snprintf(area_name, 16, "the RAM drive"); + } else if (((pdrv >= 1) && (pdrv <= 3)) || (GetVirtualSource(path) == VRT_SYSNAND)) { + perm = PERM_SYSNAND; + snprintf(area_name, 16, "the SysNAND"); + } else if (((pdrv >= 4) && (pdrv <= 6)) || (GetVirtualSource(path) == VRT_EMUNAND)) { + perm = PERM_EMUNAND; + snprintf(area_name, 16, "the EmuNAND"); + } else if ((pdrv >= 7) && (pdrv <= 9)) { + perm = PERM_IMAGE; + snprintf(area_name, 16, "images"); + } else if (GetVirtualSource(path) == VRT_MEMORY) { + perm = PERM_MEMORY; + snprintf(area_name, 16, "memory areas"); + } else { + return false; } - if ((pdrv >= 1) && (pdrv <= 3) && (write_permission_level < 3)) { - if (ShowPrompt(true, "Writing to the SysNAND is locked!\nUnlock it now?")) - return SetWritePermissions(3); + // check permission, return if already set + if ((write_permissions & perm) == perm) + return true; + + // ask the user + if (!ShowPrompt(true, "Writing to %s is locked!\nUnlock it now?", area_name)) return false; - } else if ((pdrv >= 4) && (pdrv <= 6) && (write_permission_level < 2)) { - if (ShowPrompt(true, "Writing to the EmuNAND is locked!\nUnlock it now?")) - return SetWritePermissions(2); - return false; - } else if ((pdrv >= 7) && (pdrv <= 9) && (write_permission_level < 2)) { - if (ShowPrompt(true, "Writing to images is locked!\nUnlock it now?")) - return SetWritePermissions(2); - return false; - } else if ((pdrv == 0) && (write_permission_level < 1)) { - if (ShowPrompt(true, "Writing to the SD card is locked!\nUnlock it now?")) - return SetWritePermissions(1); - return false; - } else if (pdrv >= 10) { - ShowPrompt(false, "Writing to memory is not allowed!"); - return false; - } - return true; + return SetWritePermissions(perm, true); } -bool SetWritePermissions(u32 level) { - if (write_permission_level >= level) { - // no need to ask the user here - write_permission_level = level; +bool SetWritePermissions(u32 perm, bool add_perm) { + if ((write_permissions & perm) == perm) { // write permissions already given + if (!add_perm) write_permissions = perm; return true; } - switch (level) { - case 1: + switch (perm) { + case PERM_SDCARD: if (!ShowUnlockSequence(1, "You want to enable SD card\nwriting permissions.")) return false; break; - case 2: - if (!ShowUnlockSequence(2, "You want to enable EmuNAND &\nimage writing permissions.\nKeep backups, just in case.")) + case PERM_RAMDRIVE: + if (!ShowUnlockSequence(1, "You want to enable RAM drive\nwriting permissions.")) + return false; + case PERM_EMUNAND: + if (!ShowUnlockSequence(2, "You want to enable EmuNAND\nwriting permissions.\nKeep backups, just in case.")) return false; break; - case 3: + case PERM_IMAGE: + if (!ShowUnlockSequence(2, "You want to enable image\nwriting permissions.\nKeep backups, just in case.")) + return false; + break; + case PERM_SYSNAND: if (!ShowUnlockSequence(3, "!This is your only warning!\n \nYou want to enable SysNAND\nwriting permissions.\nThis enables you to do some\nreally dangerous stuff!\nHaving a SysNAND backup and\nNANDmod is recommended.")) return false; break; + case PERM_MEMORY: + if (!ShowUnlockSequence(4, "!Better be careful!\n \nYou want to enable memory\nwriting permissions.\nWriting to certain areas may\nlead to unexpected results.")) + return false; + break; + case PERM_BASE: + if (!ShowUnlockSequence(1, "You want to enable base\nwriting permissions.")) + return false; + break; + case PERM_ALL: + if (!ShowUnlockSequence(3, "!This is your only warning!\n \nYou want to enable ALL\nwriting permissions.\nThis enables you to do some\nreally dangerous stuff!\nHaving a SysNAND backup and\nNANDmod is recommended.")) + return false; + break; default: + return false; break; } - write_permission_level = level; + write_permissions = add_perm ? write_permissions | perm : perm; return true; } u32 GetWritePermissions() { - return write_permission_level; + return write_permissions; } bool GetTempFileName(char* path) { diff --git a/source/fs.h b/source/fs.h index bcb82ee..350e113 100644 --- a/source/fs.h +++ b/source/fs.h @@ -11,6 +11,15 @@ typedef enum { #define MAX_ENTRIES 1024 +#define PERM_SDCARD (1<<0) +#define PERM_RAMDRIVE (1<<1) +#define PERM_EMUNAND (1<<2) +#define PERM_SYSNAND (1<<3) +#define PERM_IMAGE (1<<4) +#define PERM_MEMORY (1<<5) +#define PERM_BASE (PERM_SDCARD | PERM_RAMDRIVE) +#define PERM_ALL (PERM_SDCARD | PERM_RAMDRIVE | PERM_EMUNAND | PERM_SYSNAND | PERM_IMAGE | PERM_MEMORY) + typedef struct { char* name; // should point to the correct portion of the path char path[256]; @@ -33,7 +42,7 @@ void DeinitSDCardFS(); bool CheckWritePermissions(const char* path); /** Set new write permissions */ -bool SetWritePermissions(u32 level); +bool SetWritePermissions(u32 perm, bool add_perm); /** Get write permissions */ u32 GetWritePermissions(); diff --git a/source/godmode.c b/source/godmode.c index 62d2260..18f1e8f 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -7,14 +7,14 @@ #include "virtual.h" #include "image.h" -#define VERSION "0.4.9" +#define VERSION "0.5.0rc1" #define N_PANES 2 #define IMG_DRV "789I" #define WORK_BUFFER ((u8*)0x21100000) -#define COLOR_TOP_BAR ((GetWritePermissions() == 0) ? COLOR_WHITE : (GetWritePermissions() == 1) ? COLOR_BRIGHTGREEN : (GetWritePermissions() == 2) ? COLOR_BRIGHTYELLOW : COLOR_RED) +#define COLOR_TOP_BAR ((GetWritePermissions() & PERM_SYSNAND) ? COLOR_RED : (GetWritePermissions() & PERM_MEMORY) ? COLOR_BRIGHTBLUE : (GetWritePermissions() & (PERM_EMUNAND|PERM_IMAGE)) ? COLOR_BRIGHTYELLOW : GetWritePermissions() ? COLOR_BRIGHTGREEN : COLOR_WHITE) #define COLOR_SIDE_BAR COLOR_DARKGREY #define COLOR_MARKED COLOR_TINTEDYELLOW #define COLOR_FILE COLOR_TINTEDGREEN @@ -107,9 +107,8 @@ void DrawUserInterface(const char* curr_path, DirEntry* curr_entry, DirStruct* c "GodMode9 Explorer v", VERSION, // generic start part (*curr_path) ? ((clipboard->n_entries == 0) ? "L - MARK files (use with \x18\x19\x1A\x1B)\nX - DELETE / [+R] RENAME file(s)\nY - COPY file(s) / [+R] CREATE dir\n" : "L - MARK files (use with \x18\x19\x1A\x1B)\nX - DELETE / [+R] RENAME file(s)\nY - PASTE file(s) / [+R] CREATE dir\n") : - ((GetWritePermissions() <= 1) ? "X - Unlock EmuNAND / image writing\nY - Unlock SysNAND writing\nR+B - Unmount SD card\n" : - (GetWritePermissions() == 2) ? "X - Relock EmuNAND / image writing\nY - Unlock SysNAND writing\nR+B - Unmount SD card\n" : - "X - Relock EmuNAND writing\nY - Relock SysNAND writing\nR+B - Unmount SD card\n"), + ((GetWritePermissions() > PERM_BASE) ? "R+Y - Relock write permissions\nR+B - Unmount SD card\n" : + "R+Y - Unlock write permissions\nR+B - Unmount SD card\n"), (*curr_path) ? "" : ((GetMountState() == IMG_RAMDRV) ? "R+X - Unmount RAM drive\n" : (GetMountState()) ? "R+X - Unmount image\n" : "R+X - Mount RAM drive\n"), "R+L - Make a Screenshot\n", @@ -553,8 +552,8 @@ u32 GodMode() { } // highly specific commands - if (!(*current_path)) { // in the root folder... - if (switched && !*current_path && (pad_state & BUTTON_X)) { // unmount image + if (!*current_path) { // in the root folder... + if (switched && (pad_state & BUTTON_X)) { // unmount image DeinitExtFS(); if (!GetMountState()) MountRamDrive(); else MountImage(NULL); @@ -562,10 +561,8 @@ u32 GodMode() { GetDirContents(current_dir, current_path); if (clipboard->n_entries && (strcspn(clipboard->entry[0].path, IMG_DRV) == 0)) clipboard->n_entries = 0; // remove invalid clipboard stuff - } else if (pad_state & BUTTON_X) { - SetWritePermissions((GetWritePermissions() >= 2) ? 1 : 2); - } else if (pad_state & BUTTON_Y) { - SetWritePermissions((GetWritePermissions() >= 3) ? 2 : 3); + } else if (switched && (pad_state & BUTTON_Y)) { + SetWritePermissions((GetWritePermissions() > PERM_BASE) ? PERM_BASE : PERM_ALL, false); } } else if (!switched) { // standard unswitched command set if (GetVirtualSource(current_path) && (pad_state & BUTTON_X)) { diff --git a/source/ui.c b/source/ui.c index c4a025f..aec6e80 100644 --- a/source/ui.c +++ b/source/ui.c @@ -192,18 +192,20 @@ bool ShowPrompt(bool ask, const char *format, ...) } bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) { - const int seqcolors[4] = { COLOR_STD_FONT, COLOR_GREEN, COLOR_YELLOW, COLOR_RED }; - const u32 sequences[4][5] = { + const int seqcolors[5] = { COLOR_STD_FONT, COLOR_BRIGHTGREEN, COLOR_BRIGHTYELLOW, COLOR_RED, COLOR_BRIGHTBLUE }; + const u32 sequences[5][5] = { { BUTTON_RIGHT, BUTTON_DOWN, BUTTON_RIGHT, BUTTON_DOWN, BUTTON_A }, { BUTTON_LEFT, BUTTON_DOWN, BUTTON_RIGHT, BUTTON_UP, BUTTON_A }, { BUTTON_LEFT, BUTTON_RIGHT, BUTTON_DOWN, BUTTON_UP, BUTTON_A }, - { BUTTON_LEFT, BUTTON_UP, BUTTON_RIGHT, BUTTON_UP, BUTTON_A } + { BUTTON_LEFT, BUTTON_UP, BUTTON_RIGHT, BUTTON_UP, BUTTON_A }, + { BUTTON_RIGHT, BUTTON_DOWN, BUTTON_LEFT, BUTTON_DOWN, BUTTON_A } }; - const char seqsymbols[4][5] = { + const char seqsymbols[5][5] = { { '\x1A', '\x19', '\x1A', '\x19', 'A' }, { '\x1B', '\x19', '\x1A', '\x18', 'A' }, { '\x1B', '\x1A', '\x19', '\x18', 'A' }, { '\x1B', '\x18', '\x1A', '\x18', 'A' }, + { '\x1A', '\x19', '\x1B', '\x19', 'A' } }; const u32 len = 5; u32 lvl = 0; diff --git a/source/ui.h b/source/ui.h index d93e531..2fcafc0 100644 --- a/source/ui.h +++ b/source/ui.h @@ -26,6 +26,7 @@ #define COLOR_BRIGHTRED RGB(0xFF, 0x30, 0x30) #define COLOR_BRIGHTYELLOW RGB(0xFF, 0xFF, 0x30) #define COLOR_BRIGHTGREEN RGB(0x30, 0xFF, 0x30) +#define COLOR_BRIGHTBLUE RGB(0x30, 0x30, 0xFF) #define COLOR_TINTEDBLUE RGB(0x60, 0x60, 0x80) #define COLOR_TINTEDYELLOW RGB(0xD0, 0xD0, 0x60)