From 9130c4b69c318a3eef388ad76ac97c7ceae3df51 Mon Sep 17 00:00:00 2001 From: shchmue Date: Fri, 8 May 2020 15:24:04 -0600 Subject: [PATCH] diskio: Improve emmc xts, double sector cache --- source/libs/fatfs/diskio.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/source/libs/fatfs/diskio.c b/source/libs/fatfs/diskio.c index 592d8f0..669ae1c 100644 --- a/source/libs/fatfs/diskio.c +++ b/source/libs/fatfs/diskio.c @@ -40,14 +40,14 @@ extern emmc_part_t *system_part; typedef struct { u32 sector; u32 visit_count; + u8 align[8]; u8 tweak[0x10]; u8 cached_sector[0x200]; - u8 align[8]; } sector_cache_t; -#define MAX_SEC_CACHE_ENTRIES 64 +#define MAX_SEC_CACHE_ENTRIES 128 static sector_cache_t *sector_cache = NULL; -static u32 secindex = 0; +u32 secindex = 0; bool clear_sector_cache = false; DSTATUS disk_status ( @@ -80,8 +80,10 @@ static inline void _gf256_mul_x_le(void *block) { 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) { int res = 0; - u8 *pdst = (u8 *)dst; - u8 *psrc = (u8 *)src; + u8 *temptweak = (u8 *)malloc(0x10); + u32 *pdst = (u32 *)dst; + u32 *psrc = (u32 *)src; + u32 *ptweak = (u32 *)tweak; if (regen_tweak) { for (int i = 0xF; i >= 0; i--) { @@ -95,34 +97,33 @@ static inline int _emmc_xts(u32 ks1, u32 ks2, u32 enc, u8 *tweak, bool regen_twe for (u32 i = 0; i < tweak_exp * 0x20; i++) _gf256_mul_x_le(tweak); - u8 temptweak[0x10]; memcpy(temptweak, tweak, 0x10); //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]; + for (u32 j = 0; j < 4; j++) + pdst[j] = psrc[j] ^ ptweak[j]; _gf256_mul_x_le(tweak); - psrc += 0x10; - pdst += 0x10; + psrc += 4; + pdst += 4; } - se_aes_crypt_ecb(ks2, enc, dst, secsize, src, secsize); + se_aes_crypt_ecb(ks2, enc, dst, secsize, dst, secsize); - pdst = (u8 *)dst; + pdst = (u32 *)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]; + for (u32 j = 0; j < 4; j++) + pdst[j] = pdst[j] ^ ptweak[j]; _gf256_mul_x_le(tweak); - pdst += 0x10; + pdst += 4; } - res = 1; out:; + free(temptweak); return res; }