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

Refactored checksum code into methods

This commit is contained in:
Dan Ware 2020-06-06 14:46:20 +01:00
parent 9d0149ad28
commit 9506b6cf9a

View file

@ -119,6 +119,32 @@ unsigned short int get_crc_16 (const char *p, int n) {
return(crc); return(crc);
} }
u16 calculateCrc(u32 offset, u32 size)
{
const char buffer[size + 1];
readData((u8 *)buffer, offset, size, NULL);
return get_crc_16(buffer, size);
}
u16 readCrc(u32 offset)
{
u16 buffer;
readData((u8 *)&buffer, offset, sizeof(u16), NULL);
return buffer;
}
bool validateCrc(u32 offset, u32 size)
{
return calculateCrc(offset, size) == readCrc(offset + size);
}
bool calculateAndWriteCrc(u32 offset, u32 size)
{
u16 crcValue = calculateCrc(offset, size);
u8 crc[2] = { crcValue & 0xff, crcValue >> 8 }; // bytes of u16
return writeData(crc, offset + size, sizeof(u16), NULL);
}
bool dump_keys() bool dump_keys()
{ {
display_backlight_brightness(100, 1000); display_backlight_brightness(100, 1000);
@ -330,27 +356,17 @@ bool dump_keys()
return false; return false;
} }
char serial[31] = ""; u32 serialOffset = 0x250;
readData((u8 *)serial, 0x250, 30, NULL);
char serial[15];
readData((u8 *)serial, serialOffset, 14, NULL);
gfx_printf("%kCurrent serial: [%s]\n\n", COLOR_BLUE, serial); gfx_printf("%kCurrent serial: [%s]\n\n", COLOR_BLUE, serial);
// Determine stored crc if (validateCrc(serialOffset, 0x1E))
u8 *storedCrc = (u8 *)calloc(2, sizeof(u8)); gfx_printf("%kValid serial checksum\n", COLOR_GREEN);
readData((u8 *)storedCrc, 0x250 + 30, 2, NULL);
// Calculate crc
const char *serialBytes = serial;
u16 crcValue = get_crc_16(serialBytes, 30);
u8 crc[2] = { crcValue & 0xff, crcValue >> 8 }; // bytes of u16
// Validate crc
if (memcmp(storedCrc, crc, 0x2) == 0)
gfx_printf("%kValid serial crc\n", COLOR_GREEN);
else else
gfx_printf("%kWarning - invalid serial crc\n", COLOR_RED); gfx_printf("%kWarning - invalid serial checksum\n", COLOR_RED);
free(storedCrc);
return true; return true;
} }
@ -380,24 +396,9 @@ bool writeSerial()
return false; return false;
// write crc at end of serial-number block // write crc at end of serial-number block
return writeCrc(serialOffset, 0x1E); return calculateAndWriteCrc(serialOffset, 0x1E);
} }
bool writeCrc(u32 offset, u32 size)
{
char buffer[size + 1];
if (!readData((u8 *)buffer, offset, size, NULL))
return false;
const char *bytes = buffer;
free(buffer);
u16 crcValue = get_crc_16(bytes, size);
u8 crc[2] = { crcValue & 0xff, crcValue >> 8 }; // bytes of u16
return writeData(crc, offset + size, 2, NULL);
}
// todo: write a method to add a crc!
// todo: include crc block in sizes // todo: include crc block in sizes
bool incognito() bool incognito()
{ {
@ -412,11 +413,14 @@ bool incognito()
gfx_printf("%kWriting fake serial...\n", COLOR_YELLOW); gfx_printf("%kWriting fake serial...\n", COLOR_YELLOW);
if (!writeSerial()) if (!writeSerial())
return false; return false;
/* // NOTE: This is crashing Atmosphere... /*
gfx_printf("%kErasing ECC-B233 device cert...\n", COLOR_YELLOW); gfx_printf("%kErasing ECC-B233 device cert...\n", COLOR_YELLOW);
if (!erase(0x0480, 0x180)) // (size 0x190 to include crc) if (!erase(0x0480, 0x180)) // (size 0x190 to include crc)
return false; return false;
*/ */
// if (!writeCrc(0x0480, 0x180)) // whatever I do it crashes Atmos..!
// return false;
gfx_printf("%kErasing SSL cert...\n", COLOR_YELLOW); gfx_printf("%kErasing SSL cert...\n", COLOR_YELLOW);
if (!erase(0x0AE0, 0x800)) if (!erase(0x0AE0, 0x800))
return false; return false;
@ -441,11 +445,11 @@ bool incognito()
if (!erase(0x3FC0, 0x240)) if (!erase(0x3FC0, 0x240))
return false; return false;
gfx_printf("%kWriting client cert hash...\n", COLOR_YELLOW); gfx_printf("%kWriting SSL cert hash...\n", COLOR_YELLOW);
if (!writeClientCertHash()) if (!writeClientCertHash())
return false; return false;
gfx_printf("%kWriting CAL0 hash...\n", COLOR_YELLOW); gfx_printf("%kWriting body hash...\n", COLOR_YELLOW);
if (!writeCal0Hash()) if (!writeCal0Hash())
return false; return false;
@ -993,6 +997,11 @@ bool restoreProdinfo()
goto out; goto out;
} }
if (validateCrc(0x250, 0x1E))
gfx_printf("%kValid serial checksum\n", COLOR_GREEN);
else
gfx_printf("%kWarning - invalid serial checksum\n", COLOR_RED);
result = true; result = true;
gfx_printf("\n%kRestore from %s done!\n\n", COLOR_GREEN, name); gfx_printf("\n%kRestore from %s done!\n\n", COLOR_GREEN, name);
out: out: