fs.mitm: Automatically backup PRODINFO on boot.

This commit is contained in:
Michael Scire 2018-11-15 04:44:13 -08:00
parent 420361597e
commit 83644692fe
3 changed files with 56 additions and 20 deletions

View file

@ -27,25 +27,6 @@ enum FspSrvCmd : u32 {
FspSrvCmd_OpenDataStorageByDataId = 202, FspSrvCmd_OpenDataStorageByDataId = 202,
}; };
enum BisStorageId : u32 {
BisStorageId_Boot0 = 0,
BisStorageId_Boot1 = 10,
BisStorageId_RawNand = 20,
BisStorageId_BcPkg2_1 = 21,
BisStorageId_BcPkg2_2 = 22,
BisStorageId_BcPkg2_3 = 23,
BisStorageId_BcPkg2_4 = 24,
BisStorageId_BcPkg2_5 = 25,
BisStorageId_BcPkg2_6 = 26,
BisStorageId_Prodinfo = 27,
BisStorageId_ProdinfoF = 28,
BisStorageId_Safe = 29,
BisStorageId_User = 30,
BisStorageId_System = 31,
BisStorageId_SystemProperEncryption = 32,
BisStorageId_SystemProperPartition = 33,
};
class FsMitmService : public IMitmServiceObject { class FsMitmService : public IMitmServiceObject {
private: private:
bool has_initialized = false; bool has_initialized = false;

View file

@ -65,6 +65,42 @@ void Utils::InitializeSdThreadFunc(void *args) {
svcSleepThread(1000ULL); svcSleepThread(1000ULL);
} }
/* Back up CAL0, if it's not backed up already. */
fsFsCreateDirectory(&g_sd_filesystem, "/atmosphere/automatic_backups");
{
FsStorage cal0_storage;
FsFile cal0_file;
bool has_auto_backup = false;
static const char * const PRODINFO_BACKUP_PATH = "/atmosphere/automatic_backups/PRODINFO.bin";
constexpr size_t PRODINFO_SIZE = 0x4000;
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, PRODINFO_BACKUP_PATH, FS_OPEN_READ, &cal0_file))) {
char magic[4];
size_t read;
if (R_SUCCEEDED(fsFileRead(&cal0_file, 0, magic, sizeof(magic), &read)) && read == sizeof(magic) && memcmp(magic, "CAL0", sizeof(magic)) == 0) {
has_auto_backup = true;
}
fsFileClose(&cal0_file);
}
if (!has_auto_backup && R_SUCCEEDED(fsOpenBisStorage(&cal0_storage, BisStorageId_Prodinfo))) {
u8 *cal0 = new u8[PRODINFO_SIZE];
if (R_SUCCEEDED(fsStorageRead(&cal0_storage, 0, cal0, PRODINFO_SIZE)) ) {
fsFsCreateFile(&g_sd_filesystem, PRODINFO_BACKUP_PATH, PRODINFO_SIZE, 0);
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, PRODINFO_BACKUP_PATH, FS_OPEN_READ | FS_OPEN_WRITE, &cal0_file))) {
fsFileSetSize(&cal0_file, PRODINFO_SIZE);
fsFileWrite(&cal0_file, 0, cal0, PRODINFO_SIZE);
fsFileFlush(&cal0_file);
fsFileClose(&cal0_file);
}
}
delete cal0;
fsStorageClose(&cal0_storage);
}
}
/* Check for MitM flags. */ /* Check for MitM flags. */
FsDir titles_dir; FsDir titles_dir;
if (R_SUCCEEDED(fsFsOpenDirectory(&g_sd_filesystem, "/atmosphere/titles", FS_DIROPEN_DIRECTORY, &titles_dir))) { if (R_SUCCEEDED(fsFsOpenDirectory(&g_sd_filesystem, "/atmosphere/titles", FS_DIROPEN_DIRECTORY, &titles_dir))) {
@ -307,7 +343,7 @@ bool Utils::HasFlag(u64 tid, const char *flag) {
return false; return false;
} }
bool Utils::HasGlobalFlag(u64 tid, const char *flag) { bool Utils::HasGlobalFlag(const char *flag) {
if (IsSdInitialized()) { if (IsSdInitialized()) {
FsFile f; FsFile f;
char flag_path[FS_MAX_PATH] = {0}; char flag_path[FS_MAX_PATH] = {0};

View file

@ -18,6 +18,25 @@
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
enum BisStorageId : u32 {
BisStorageId_Boot0 = 0,
BisStorageId_Boot1 = 10,
BisStorageId_RawNand = 20,
BisStorageId_BcPkg2_1 = 21,
BisStorageId_BcPkg2_2 = 22,
BisStorageId_BcPkg2_3 = 23,
BisStorageId_BcPkg2_4 = 24,
BisStorageId_BcPkg2_5 = 25,
BisStorageId_BcPkg2_6 = 26,
BisStorageId_Prodinfo = 27,
BisStorageId_ProdinfoF = 28,
BisStorageId_Safe = 29,
BisStorageId_User = 30,
BisStorageId_System = 31,
BisStorageId_SystemProperEncryption = 32,
BisStorageId_SystemProperPartition = 33,
};
class Utils { class Utils {
public: public:
static bool IsSdInitialized(); static bool IsSdInitialized();