1
0
Fork 0
mirror of https://github.com/Scandal-UK/Incognito_RCM.git synced 2024-12-23 10:52:06 +00:00
Incognito_RCM/source/incognito/io/io.c

169 lines
4.5 KiB
C
Raw Normal View History

2019-10-06 20:28:54 +01:00
#include "io.h"
2020-12-22 18:40:53 +00:00
#include <storage/sdmmc.h>
2019-10-06 20:28:54 +01:00
#include "../../storage/nx_emmc.h"
2020-06-06 14:46:46 +01:00
#include "../../storage/emummc.h"
2019-10-06 20:28:54 +01:00
#include <string.h>
2020-12-22 18:40:53 +00:00
#include <sec/se.h>
2019-10-06 20:28:54 +01:00
extern sdmmc_storage_t storage;
extern emmc_part_t *prodinfo_part;
static inline void _gf256_mul_x_le(void *block)
{
2020-12-22 18:40:53 +00:00
u32 *pdata = (u32 *)block;
2019-10-06 20:28:54 +01:00
u32 carry = 0;
2020-12-22 18:40:53 +00:00
for (u32 i = 0; i < 4; i++)
2019-10-06 20:28:54 +01:00
{
2020-12-22 18:40:53 +00:00
u32 b = pdata[i];
2019-10-06 20:28:54 +01:00
pdata[i] = (b << 1) | carry;
2020-12-22 18:40:53 +00:00
carry = b >> 31;
2019-10-06 20:28:54 +01:00
}
if (carry)
pdata[0x0] ^= 0x87;
}
2020-12-23 13:58:36 +00:00
static inline int _emmc_xts(u32 ks1, u32 ks2, u32 enc, u8 *tweak, bool regen_tweak, u32 tweak_exp, u64 sec, void *dst, void *src, u32 secsize)
2019-10-06 20:28:54 +01:00
{
2020-12-23 13:58:36 +00:00
int res = 0;
u8 *pdst = (u8 *)dst;
u8 *psrc = (u8 *)src;
if (regen_tweak)
{
for (int i = 0xF; i >= 0; i--)
{
tweak[i] = sec & 0xFF;
sec >>= 8;
}
if (!se_aes_crypt_block_ecb(ks1, 1, tweak, tweak))
goto out;
}
for (u32 i = 0; i < tweak_exp * 0x20; i++)
_gf256_mul_x_le(tweak);
u8 temptweak[0x10];
memcpy(temptweak, tweak, 0x10);
2019-10-06 20:28:54 +01:00
2020-12-23 13:58:36 +00:00
//We are assuming a 0x10-aligned sector size in this implementation.
for (u32 i = 0; i < secsize / 0x10; i++)
{
for (u32 j = 0; j < 0x10; j++)
pdst[j] = psrc[j] ^ tweak[j];
_gf256_mul_x_le(tweak);
psrc += 0x10;
pdst += 0x10;
}
se_aes_crypt_ecb(ks2, enc, dst, secsize, src, secsize);
pdst = (u8 *)dst;
memcpy(tweak, temptweak, 0x10);
for (u32 i = 0; i < secsize / 0x10; i++)
{
for (u32 j = 0; j < 0x10; j++)
pdst[j] = pdst[j] ^ tweak[j];
_gf256_mul_x_le(tweak);
pdst += 0x10;
}
2019-10-06 20:28:54 +01:00
2020-12-23 13:58:36 +00:00
res = 1;
2019-10-06 20:28:54 +01:00
2020-12-23 13:58:36 +00:00
out:;
return res;
2019-10-06 20:28:54 +01:00
}
// replacement for nx_emmc_part_write in storage/nx_emmc, which uses sdmmc_storage_write
int nx_emummc_part_write(sdmmc_storage_t *storage, emmc_part_t *part, u32 sector_off, u32 num_sectors, void *buf)
{
// The last LBA is inclusive.
if (part->lba_start + sector_off > part->lba_end)
return 0;
return emummc_storage_write(storage, part->lba_start + sector_off, num_sectors, buf);
}
2019-10-06 20:28:54 +01:00
bool prodinfo_read(
u8 *buff, /* Data buffer to store read data */
u32 sector, /* Start sector in LBA */
u32 count /* Number of sectors to read */
)
{
2019-10-07 18:26:40 +01:00
bool result = false;
2019-10-06 20:28:54 +01:00
__attribute__((aligned(16))) static u8 tweak[0x10];
__attribute__((aligned(16))) static u64 prev_cluster = -1;
__attribute__((aligned(16))) static u32 prev_sector = 0;
u32 tweak_exp = 0;
bool regen_tweak = true;
2020-12-23 13:58:36 +00:00
if (nx_emmc_part_read(&storage, prodinfo_part, sector, count, buff))
2019-10-06 20:28:54 +01:00
{
if (prev_cluster != sector / 0x20)
{ // sector in different cluster than last read
prev_cluster = sector / 0x20;
tweak_exp = sector % 0x20;
}
else if (sector > prev_sector)
{ // sector in same cluster and past last sector
tweak_exp = sector - prev_sector - 1;
regen_tweak = false;
}
else
{ // sector in same cluster and before or same as last sector
tweak_exp = sector % 0x20;
}
// fatfs will never pull more than a cluster
2020-12-23 13:58:36 +00:00
result = _emmc_xts(9, 8, 0, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200);
2019-10-06 20:28:54 +01:00
prev_sector = sector + count - 1;
2019-10-07 18:26:40 +01:00
return result;
2019-10-06 20:28:54 +01:00
}
2019-10-07 18:26:40 +01:00
return result;
2019-10-06 20:28:54 +01:00
}
bool prodinfo_write(
u8 *buff, /* Data buffer to store read data */
u32 sector, /* Start sector in LBA */
u32 count /* Number of sectors to read */
)
{
__attribute__((aligned(16))) static u8 tweak[0x10];
__attribute__((aligned(16))) static u64 prev_cluster = -1;
__attribute__((aligned(16))) static u32 prev_sector = 0;
u32 tweak_exp = 0;
bool regen_tweak = true;
if (prev_cluster != sector / 0x20)
{ // sector in different cluster than last read
prev_cluster = sector / 0x20;
tweak_exp = sector % 0x20;
}
else if (sector > prev_sector)
{ // sector in same cluster and past last sector
tweak_exp = sector - prev_sector - 1;
regen_tweak = false;
}
else
{ // sector in same cluster and before or same as last sector
tweak_exp = sector % 0x20;
}
// fatfs will never pull more than a cluster
2020-12-23 13:58:36 +00:00
if(!_emmc_xts(9, 8, 1, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200)){
2019-10-07 18:26:40 +01:00
return false;
}
2020-12-23 13:58:36 +00:00
if (nx_emummc_part_write(&storage, prodinfo_part, sector, count, buff))
2019-10-06 20:28:54 +01:00
{
prev_sector = sector + count - 1;
return true;
}
return false;
}