1
0
Fork 0
mirror of https://github.com/Scandal-UK/Incognito_RCM.git synced 2024-11-22 20:06:42 +00:00

add backup verification

This commit is contained in:
jimzrt 2019-10-07 20:02:42 +02:00
parent b6c552243a
commit e81b1e62c7
3 changed files with 47 additions and 29 deletions

View file

@ -49,7 +49,7 @@
#define RETRY(exp) \ #define RETRY(exp) \
({ \ ({ \
u8 _attemptc_ = RETRY_COUNT; \ u8 _attemptc_ = RETRY_COUNT; \
bool _resultb_; \ bool _resultb_ = false; \
while (_attemptc_--) \ while (_attemptc_--) \
{ \ { \
if ((_resultb_ = exp)) \ if ((_resultb_ = exp)) \
@ -298,8 +298,8 @@ bool dump_keys()
return false; return false;
} }
char serial[15]; char serial[15] = "";
readData((u8 *)serial, 0x250, 15, NULL); readData((u8 *)serial, 0x250, 14, NULL);
gfx_printf("%kCurrent serial:%s\n\n", COLOR_BLUE, serial); gfx_printf("%kCurrent serial:%s\n\n", COLOR_BLUE, serial);
@ -626,19 +626,27 @@ void screenshot(const char *filename)
} }
#endif #endif
bool verifyHash(u32 hashOffset, u32 offset, u32 sz) bool verifyHash(u32 hashOffset, u32 offset, u32 sz, u8 *blob)
{ {
bool result = false; bool result = false;
u8 *buffer = (u8 *)malloc(sz); u8 *buffer = (u8 *)malloc(sz);
if(!readData(buffer, offset, sz, NULL)) if(blob == NULL){
goto out; if(!readData(buffer, offset, sz, NULL))
goto out;
} else {
memcpy(buffer, blob + offset, sz);
}
u8 hash1[0x20]; u8 hash1[0x20];
se_calc_sha256(hash1, buffer, sz); se_calc_sha256(hash1, buffer, sz);
u8 hash2[0x20]; u8 hash2[0x20];
if(!readData(hash2, hashOffset, 0x20, NULL)) if(blob == NULL){
goto out; if(!readData(hash2, hashOffset, 0x20, NULL))
goto out;
} else {
memcpy(hash2, blob + hashOffset, 0x20);
}
if (memcmp(hash1, hash2, 0x20)) if (memcmp(hash1, hash2, 0x20))
{ {
@ -678,29 +686,33 @@ bool writeClientCertHash()
return writeHash(0x12E0, 0xAE0, certSize()); return writeHash(0x12E0, 0xAE0, certSize());
} }
bool verifyCal0Hash() bool verifyCal0Hash(u8 *blob)
{ {
return verifyHash(0x20, 0x40, calibrationDataSize()); return verifyHash(0x20, 0x40, calibrationDataSize(), blob);
} }
bool verifyClientCertHash() bool verifyClientCertHash(u8 *blob)
{ {
return verifyHash(0x12E0, 0xAE0, certSize(), blob);
return verifyHash(0x12E0, 0xAE0, certSize());
} }
bool verifyProdinfo() bool verifyProdinfo(u8 *blob)
{ {
gfx_printf("%kVerifying client cert hash and CAL0 hash...\n", COLOR_YELLOW); gfx_printf("%kVerifying client cert hash and CAL0 hash%s...\n", COLOR_YELLOW, blob != NULL ? "\nfrom backup" : "");
if (verifyClientCertHash() && verifyCal0Hash()) if (verifyClientCertHash(blob) && verifyCal0Hash(blob))
{ {
char serial[15]; char serial[15] = "";
readData((u8 *)serial, 0x250, 15, NULL); if(blob == NULL){
gfx_printf("%kVerification successful!\n%kNew Serial:%s\n", COLOR_GREEN, COLOR_BLUE, serial); readData((u8 *)serial, 0x250, 14, NULL);
} else {
memcpy(serial, blob + 0x250, 14);
}
gfx_printf("%kVerification successful!\n%kSerial:%s\n", COLOR_GREEN, COLOR_BLUE, serial);
return true; return true;
} }
gfx_printf("%kVerification not successful!\nPlease restore backup!\n", COLOR_RED); gfx_printf("%kVerification not successful!\n", COLOR_RED);
return false; return false;
} }
@ -795,7 +807,7 @@ bool backupProdinfo()
gfx_printf("%kBacking up %s...\n", COLOR_YELLOW, name); gfx_printf("%kBacking up %s...\n", COLOR_YELLOW, name);
if (checkBackupExists()) if (checkBackupExists())
{ {
gfx_printf("%kBackup already exists!\nWill rename old backup.\n", COLOR_YELLOW); gfx_printf("%kBackup already exists!\nWill rename old backup.\n", COLOR_ORANGE);
u32 filenameSuffix = 0; u32 filenameSuffix = 0;
char newName[255]; char newName[255];
do do
@ -821,6 +833,9 @@ bool backupProdinfo()
gfx_printf("\n%kError reading from NAND!\n", COLOR_RED); gfx_printf("\n%kError reading from NAND!\n", COLOR_RED);
goto out; goto out;
} }
if(!verifyProdinfo(bufferNX)){
goto out;
}
gfx_printf("%k\nWriting to file...\n", COLOR_YELLOW); gfx_printf("%k\nWriting to file...\n", COLOR_YELLOW);
u32 bytesWritten; u32 bytesWritten;
if (f_write(&fp, bufferNX, PRODINFO_SIZE, &bytesWritten) != FR_OK || bytesWritten != PRODINFO_SIZE) if (f_write(&fp, bufferNX, PRODINFO_SIZE, &bytesWritten) != FR_OK || bytesWritten != PRODINFO_SIZE)
@ -845,7 +860,7 @@ bool restoreProdinfo()
bool result = false; bool result = false;
sd_mount(); sd_mount();
char *name; const char *name;
if (isSysNAND()) if (isSysNAND())
{ {
name = BACKUP_NAME_SYSNAND; name = BACKUP_NAME_SYSNAND;
@ -872,6 +887,9 @@ bool restoreProdinfo()
gfx_printf("\n%kError reading from file!\n", COLOR_RED); gfx_printf("\n%kError reading from file!\n", COLOR_RED);
goto out; goto out;
} }
if(!verifyProdinfo(bufferNX)){
goto out;
}
gfx_printf("%kWriting to NAND...\n", COLOR_YELLOW); gfx_printf("%kWriting to NAND...\n", COLOR_YELLOW);
if (!writeData(bufferNX, 0, PRODINFO_SIZE, print_progress)) if (!writeData(bufferNX, 0, PRODINFO_SIZE, print_progress))
{ {

View file

@ -32,7 +32,7 @@ bool readData(u8 *buffer, u32 offset, u32 length, void (*progress_callback)(u32,
bool writeData(u8 *buffer, u32 offset, u32 length, void (*progress_callback)(u32, u32)); bool writeData(u8 *buffer, u32 offset, u32 length, void (*progress_callback)(u32, u32));
bool writeClientCertHash(); bool writeClientCertHash();
bool writeCal0Hash(); bool writeCal0Hash();
bool verifyProdinfo(); bool verifyProdinfo(u8 *blob);
bool backupProdinfo(); bool backupProdinfo();
bool restoreProdinfo(); bool restoreProdinfo();
bool checkBackupExists(); bool checkBackupExists();

View file

@ -161,7 +161,7 @@ void incognito_sysnand()
gfx_printf("%kError applying Incognito!\nWill restore backup!\n", COLOR_RED); gfx_printf("%kError applying Incognito!\nWill restore backup!\n", COLOR_RED);
backupProdinfo(); backupProdinfo();
} }
if (!verifyProdinfo()) if (!verifyProdinfo(NULL))
{ {
gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED); gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED);
} }
@ -184,7 +184,7 @@ void incognito_emunand()
gfx_printf("%kError applying Incognito!\nWill restore backup!\n", COLOR_RED); gfx_printf("%kError applying Incognito!\nWill restore backup!\n", COLOR_RED);
backupProdinfo(); backupProdinfo();
} }
if (!verifyProdinfo()) if (!verifyProdinfo(NULL))
{ {
gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED); gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED);
} }
@ -232,9 +232,9 @@ void restore_sysnand()
goto out; goto out;
restoreProdinfo(); restoreProdinfo();
if (!verifyProdinfo()) if (!verifyProdinfo(NULL))
{ {
gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED); gfx_printf("%kThis should not happen!\nTry again or restore via NAND backup from hekate!\n", COLOR_RED);
} }
out: out:
cleanUp(); cleanUp();
@ -252,9 +252,9 @@ void restore_emunand()
goto out; goto out;
restoreProdinfo(); restoreProdinfo();
if (!verifyProdinfo()) if (!verifyProdinfo(NULL))
{ {
gfx_printf("%kThis should not happen!\nTry restoring or restore via NAND backup from hekate!\n", COLOR_RED); gfx_printf("%kThis should not happen!\nTry again or restore via NAND backup from hekate!\n", COLOR_RED);
} }
out: out:
cleanUp(); cleanUp();