mirror of
https://github.com/Scandal-UK/Incognito_RCM.git
synced 2024-11-22 20:06:42 +00:00
cluser and sector aligned reading and writing, needs testing
This commit is contained in:
parent
35f38a1526
commit
ed43620605
2 changed files with 253 additions and 207 deletions
|
@ -393,42 +393,7 @@ static inline u32 _read_le_u32(const void *buffer, u32 offset)
|
||||||
|
|
||||||
bool readData(u8 *buffer, u32 offset, u32 length, u8 enc)
|
bool readData(u8 *buffer, u32 offset, u32 length, u8 enc)
|
||||||
{
|
{
|
||||||
|
bool result = false;
|
||||||
// u8 *tmp = (u8 *)malloc(NX_EMMC_BLOCKSIZE);
|
|
||||||
// u32 sector = (offset / NX_EMMC_BLOCKSIZE);
|
|
||||||
// u32 newOffset = offset % NX_EMMC_BLOCKSIZE;
|
|
||||||
// u32 read = 0;
|
|
||||||
|
|
||||||
// if (newOffset > 0 && length >= NX_EMMC_BLOCKSIZE)
|
|
||||||
// {
|
|
||||||
// u32 toRead = NX_EMMC_BLOCKSIZE - newOffset;
|
|
||||||
// disk_read_prod(tmp, sector, 1);
|
|
||||||
// memcpy(buffer, tmp + newOffset, toRead);
|
|
||||||
|
|
||||||
// length -= toRead;
|
|
||||||
// read += toRead;
|
|
||||||
// sector++;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// while (length > NX_EMMC_BLOCKSIZE)
|
|
||||||
// {
|
|
||||||
|
|
||||||
// disk_read_prod(tmp, sector, 1);
|
|
||||||
// memcpy(buffer + read, tmp, NX_EMMC_BLOCKSIZE);
|
|
||||||
|
|
||||||
// length -= NX_EMMC_BLOCKSIZE;
|
|
||||||
// read += NX_EMMC_BLOCKSIZE;
|
|
||||||
// sector++;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (length > 0)
|
|
||||||
// {
|
|
||||||
// disk_read_prod(tmp, sector, 1);
|
|
||||||
// memcpy(buffer + read, tmp, length);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// free(tmp);
|
|
||||||
|
|
||||||
u32 sector = (offset / NX_EMMC_BLOCKSIZE);
|
u32 sector = (offset / NX_EMMC_BLOCKSIZE);
|
||||||
u32 newOffset = (offset % NX_EMMC_BLOCKSIZE);
|
u32 newOffset = (offset % NX_EMMC_BLOCKSIZE);
|
||||||
|
|
||||||
|
@ -441,77 +406,133 @@ bool readData(u8 *buffer, u32 offset, u32 length, u8 enc)
|
||||||
u32 sectorOffset = 0;
|
u32 sectorOffset = 0;
|
||||||
while (clusterOffset + sectorCount > SECTORS_IN_CLUSTER)
|
while (clusterOffset + sectorCount > SECTORS_IN_CLUSTER)
|
||||||
{
|
{
|
||||||
u32 sectorToRead = SECTORS_IN_CLUSTER - clusterOffset;
|
u32 sectorsToRead = SECTORS_IN_CLUSTER - clusterOffset;
|
||||||
disk_read_prod(tmp + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorToRead, enc);
|
if (disk_read_prod(tmp + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorsToRead, enc) != RES_OK)
|
||||||
sector += sectorToRead;
|
{
|
||||||
sectorCount -= sectorToRead;
|
goto out;
|
||||||
|
}
|
||||||
|
sector += sectorsToRead;
|
||||||
|
sectorCount -= sectorsToRead;
|
||||||
clusterOffset = 0;
|
clusterOffset = 0;
|
||||||
sectorOffset += sectorToRead;
|
sectorOffset += sectorsToRead;
|
||||||
|
}
|
||||||
|
if(sectorCount == 0) goto done;
|
||||||
|
|
||||||
|
if (disk_read_prod(tmp + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorCount, enc) != RES_OK)
|
||||||
|
{
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
disk_read_prod(tmp + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorCount, enc);
|
|
||||||
|
|
||||||
memcpy(buffer, tmp + newOffset, length);
|
memcpy(buffer, tmp + newOffset, length);
|
||||||
|
done:
|
||||||
|
result = true;
|
||||||
|
out:
|
||||||
free(tmp);
|
free(tmp);
|
||||||
|
return result;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool writeData(u8 *buffer, u32 offset, u32 length, u8 enc)
|
bool writeData(u8 *buffer, u32 offset, u32 length, u8 enc)
|
||||||
{
|
{
|
||||||
|
bool result = false;
|
||||||
|
|
||||||
// u8 *tmp = (u8 *)malloc(NX_EMMC_BLOCKSIZE);
|
u8 *tmp_sec = (u8 *)malloc(NX_EMMC_BLOCKSIZE);
|
||||||
// u32 sector = (offset / NX_EMMC_BLOCKSIZE);
|
u8 *tmp = NULL;
|
||||||
// u32 newOffset = offset % NX_EMMC_BLOCKSIZE;
|
|
||||||
// u32 read = 0;
|
|
||||||
|
|
||||||
// if (newOffset > 0 && length >= NX_EMMC_BLOCKSIZE)
|
|
||||||
// {
|
|
||||||
// u32 toRead = NX_EMMC_BLOCKSIZE - newOffset;
|
|
||||||
// disk_read_prod(tmp, sector, 1);
|
|
||||||
// memcpy(tmp + newOffset, buffer, toRead);
|
|
||||||
// disk_write_prod(tmp, sector, 1);
|
|
||||||
// length -= toRead;
|
|
||||||
// read += toRead;
|
|
||||||
// sector++;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// while (length > NX_EMMC_BLOCKSIZE)
|
|
||||||
// {
|
|
||||||
|
|
||||||
// disk_write_prod(buffer + read, sector, 1);
|
|
||||||
|
|
||||||
// length -= NX_EMMC_BLOCKSIZE;
|
|
||||||
// read += NX_EMMC_BLOCKSIZE;
|
|
||||||
// sector++;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (length > 0)
|
|
||||||
// {
|
|
||||||
// disk_read_prod(tmp, sector, 1);
|
|
||||||
// memcpy(tmp, buffer + read, length);
|
|
||||||
// disk_write_prod(buffer + read, sector, 1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// free(tmp);
|
|
||||||
|
|
||||||
u32 sector = (offset / NX_EMMC_BLOCKSIZE);
|
u32 sector = (offset / NX_EMMC_BLOCKSIZE);
|
||||||
u32 newOffset = (offset % NX_EMMC_BLOCKSIZE);
|
u32 newOffset = (offset % NX_EMMC_BLOCKSIZE);
|
||||||
|
|
||||||
u8 sectorCount = ((newOffset + length - 1) / (NX_EMMC_BLOCKSIZE)) + 1;
|
// if there is a sector offset, read involved sector, write data to it with offset and write back whole sector to be sector aligned
|
||||||
|
if(newOffset > 0){
|
||||||
|
|
||||||
|
u32 bytesToRead;
|
||||||
|
if(length > NX_EMMC_BLOCKSIZE){
|
||||||
|
bytesToRead = NX_EMMC_BLOCKSIZE - newOffset;
|
||||||
|
} else {
|
||||||
|
bytesToRead = length - newOffset;
|
||||||
|
}
|
||||||
|
if(disk_read_prod(tmp_sec, sector, 1, enc) != RES_OK){
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
memcpy(tmp_sec + newOffset, buffer, bytesToRead);
|
||||||
|
if(disk_write_prod(tmp_sec, sector, 1, enc) != RES_OK){
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
sector++;
|
||||||
|
length -= bytesToRead;
|
||||||
|
newOffset = bytesToRead;
|
||||||
|
|
||||||
u8 *tmp = (u8 *)malloc(sectorCount * NX_EMMC_BLOCKSIZE);
|
// are we done?
|
||||||
|
if(length == 0) goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
// write whole sectors in chunks while being cluster aligned
|
||||||
|
u32 sectorCount = (length - 1 / NX_EMMC_BLOCKSIZE) + 1;
|
||||||
|
tmp = (u8 *)malloc(sectorCount * NX_EMMC_BLOCKSIZE);
|
||||||
|
|
||||||
disk_read_prod(tmp, sector, sectorCount, 1);
|
u32 clusterOffset = sector % SECTORS_IN_CLUSTER;
|
||||||
|
u32 sectorOffset = 0;
|
||||||
|
while (clusterOffset + sectorCount > SECTORS_IN_CLUSTER)
|
||||||
|
{
|
||||||
|
u32 sectorsToRead = SECTORS_IN_CLUSTER - clusterOffset;
|
||||||
|
if(disk_write_prod(buffer + newOffset + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorsToRead, enc) != RES_OK){
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
sector += sectorsToRead;
|
||||||
|
sectorOffset += sectorsToRead;
|
||||||
|
sectorCount -= sectorsToRead;
|
||||||
|
clusterOffset = 0;
|
||||||
|
length -= sectorsToRead * NX_EMMC_BLOCKSIZE;
|
||||||
|
}
|
||||||
|
|
||||||
memcpy(tmp + newOffset, buffer, length);
|
// write remaining sectors
|
||||||
|
if(sectorCount > 0){
|
||||||
|
if(disk_write_prod(buffer + newOffset + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorCount, enc) != RES_OK){
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
length -= sectorCount * NX_EMMC_BLOCKSIZE;
|
||||||
|
sector += sectorCount;
|
||||||
|
sectorOffset += sectorCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if there is data remaining that is smaller than a sector, read that sector, write remaining data to it and write back whole sector
|
||||||
|
if(length == 0) goto done;
|
||||||
|
if(length >= NX_EMMC_BLOCKSIZE){
|
||||||
|
gfx_printf("\n%kERROR, ERROR!! remaining length: %d\n", COLOR_RED, length);
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if(disk_read_prod(tmp_sec, sector, 1, enc) != RES_OK){
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
memcpy(tmp, buffer + newOffset + (sectorOffset * NX_EMMC_BLOCKSIZE), length);
|
||||||
|
if(disk_write_prod(tmp_sec, sector, 1, enc) != RES_OK){
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
disk_write_prod(tmp, sector, sectorCount, enc);
|
|
||||||
|
|
||||||
|
done:
|
||||||
|
result = true;
|
||||||
|
out:
|
||||||
|
free(tmp_sec);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
|
return result;
|
||||||
|
|
||||||
return true;
|
|
||||||
|
// u32 sector = (offset / NX_EMMC_BLOCKSIZE);
|
||||||
|
// u32 newOffset = (offset % NX_EMMC_BLOCKSIZE);
|
||||||
|
|
||||||
|
// u8 sectorCount = ((newOffset + length - 1) / (NX_EMMC_BLOCKSIZE)) + 1;
|
||||||
|
|
||||||
|
// u8 *tmp = (u8 *)malloc(sectorCount * NX_EMMC_BLOCKSIZE);
|
||||||
|
|
||||||
|
// disk_read_prod(tmp, sector, sectorCount, 1);
|
||||||
|
|
||||||
|
// memcpy(tmp + newOffset, buffer, length);
|
||||||
|
|
||||||
|
// disk_write_prod(tmp, sector, sectorCount, enc);
|
||||||
|
|
||||||
|
// free(tmp);
|
||||||
|
|
||||||
|
// return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool writeHash(u32 hashOffset, u32 offset, u32 sz)
|
bool writeHash(u32 hashOffset, u32 offset, u32 sz)
|
||||||
|
|
|
@ -24,50 +24,52 @@
|
||||||
/*-----------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "diskio.h" /* FatFs lower layer API */
|
#include "diskio.h" /* FatFs lower layer API */
|
||||||
#include "../../mem/heap.h"
|
#include "../../mem/heap.h"
|
||||||
#include "../../sec/se.h"
|
#include "../../sec/se.h"
|
||||||
|
|
||||||
|
|
||||||
#define SDMMC_UPPER_BUFFER 0xB8000000
|
#define SDMMC_UPPER_BUFFER 0xB8000000
|
||||||
#define DRAM_START 0x80000000
|
#define DRAM_START 0x80000000
|
||||||
|
|
||||||
extern sdmmc_storage_t sd_storage;
|
extern sdmmc_storage_t sd_storage;
|
||||||
extern sdmmc_storage_t storage;
|
extern sdmmc_storage_t storage;
|
||||||
extern emmc_part_t *system_part;
|
extern emmc_part_t *system_part;
|
||||||
extern emmc_part_t *prodinfo_part;
|
extern emmc_part_t *prodinfo_part;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
u32 sector;
|
u32 sector;
|
||||||
u32 visit_count;
|
u32 visit_count;
|
||||||
u8 tweak[0x10];
|
u8 tweak[0x10];
|
||||||
u8 cached_sector[0x200];
|
u8 cached_sector[0x200];
|
||||||
u8 align[8];
|
u8 align[8];
|
||||||
} sector_cache_t;
|
} sector_cache_t;
|
||||||
|
|
||||||
#define MAX_SEC_CACHE_ENTRIES 64
|
#define MAX_SEC_CACHE_ENTRIES 64
|
||||||
static sector_cache_t *sector_cache = (sector_cache_t*)0x40022000;
|
static sector_cache_t *sector_cache = (sector_cache_t *)0x40022000;
|
||||||
static u32 secindex = 0;
|
static u32 secindex = 0;
|
||||||
|
|
||||||
DSTATUS disk_status (
|
DSTATUS disk_status(
|
||||||
BYTE pdrv /* Physical drive number to identify the drive */
|
BYTE pdrv /* Physical drive number to identify the drive */
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DSTATUS disk_initialize (
|
DSTATUS disk_initialize(
|
||||||
BYTE pdrv /* Physical drive number to identify the drive */
|
BYTE pdrv /* Physical drive number to identify the drive */
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void _gf256_mul_x_le(void *block) {
|
static inline void _gf256_mul_x_le(void *block)
|
||||||
|
{
|
||||||
u8 *pdata = (u8 *)block;
|
u8 *pdata = (u8 *)block;
|
||||||
u32 carry = 0;
|
u32 carry = 0;
|
||||||
|
|
||||||
for (u32 i = 0; i < 0x10; i++) {
|
for (u32 i = 0; i < 0x10; i++)
|
||||||
|
{
|
||||||
u8 b = pdata[i];
|
u8 b = pdata[i];
|
||||||
pdata[i] = (b << 1) | carry;
|
pdata[i] = (b << 1) | carry;
|
||||||
carry = b >> 7;
|
carry = b >> 7;
|
||||||
|
@ -77,13 +79,16 @@ static inline void _gf256_mul_x_le(void *block) {
|
||||||
pdata[0x0] ^= 0x87;
|
pdata[0x0] ^= 0x87;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
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;
|
int res = 0;
|
||||||
u8 *pdst = (u8 *)dst;
|
u8 *pdst = (u8 *)dst;
|
||||||
u8 *psrc = (u8 *)src;
|
u8 *psrc = (u8 *)src;
|
||||||
|
|
||||||
if (regen_tweak) {
|
if (regen_tweak)
|
||||||
for (int i = 0xF; i >= 0; i--) {
|
{
|
||||||
|
for (int i = 0xF; i >= 0; i--)
|
||||||
|
{
|
||||||
tweak[i] = sec & 0xFF;
|
tweak[i] = sec & 0xFF;
|
||||||
sec >>= 8;
|
sec >>= 8;
|
||||||
}
|
}
|
||||||
|
@ -98,7 +103,8 @@ static inline int _emmc_xts(u32 ks1, u32 ks2, u32 enc, u8 *tweak, bool regen_twe
|
||||||
memcpy(temptweak, tweak, 0x10);
|
memcpy(temptweak, tweak, 0x10);
|
||||||
|
|
||||||
//We are assuming a 0x10-aligned sector size in this implementation.
|
//We are assuming a 0x10-aligned sector size in this implementation.
|
||||||
for (u32 i = 0; i < secsize / 0x10; i++) {
|
for (u32 i = 0; i < secsize / 0x10; i++)
|
||||||
|
{
|
||||||
for (u32 j = 0; j < 0x10; j++)
|
for (u32 j = 0; j < 0x10; j++)
|
||||||
pdst[j] = psrc[j] ^ tweak[j];
|
pdst[j] = psrc[j] ^ tweak[j];
|
||||||
_gf256_mul_x_le(tweak);
|
_gf256_mul_x_le(tweak);
|
||||||
|
@ -111,116 +117,124 @@ static inline int _emmc_xts(u32 ks1, u32 ks2, u32 enc, u8 *tweak, bool regen_twe
|
||||||
pdst = (u8 *)dst;
|
pdst = (u8 *)dst;
|
||||||
|
|
||||||
memcpy(tweak, temptweak, 0x10);
|
memcpy(tweak, temptweak, 0x10);
|
||||||
for (u32 i = 0; i < secsize / 0x10; i++) {
|
for (u32 i = 0; i < secsize / 0x10; i++)
|
||||||
|
{
|
||||||
for (u32 j = 0; j < 0x10; j++)
|
for (u32 j = 0; j < 0x10; j++)
|
||||||
pdst[j] = pdst[j] ^ tweak[j];
|
pdst[j] = pdst[j] ^ tweak[j];
|
||||||
_gf256_mul_x_le(tweak);
|
_gf256_mul_x_le(tweak);
|
||||||
pdst += 0x10;
|
pdst += 0x10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
res = 1;
|
res = 1;
|
||||||
|
|
||||||
out:;
|
out:;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DRESULT disk_read_prod(
|
||||||
|
|
||||||
|
BYTE *buff, /* Data buffer to store read data */
|
||||||
|
DWORD sector, /* Start sector in LBA */
|
||||||
DRESULT disk_read_prod (
|
UINT count, /* Number of sectors to read */
|
||||||
|
BYTE enc)
|
||||||
BYTE *buff, /* Data buffer to store read data */
|
|
||||||
DWORD sector, /* Start sector in LBA */
|
|
||||||
UINT count, /* Number of sectors to read */
|
|
||||||
BYTE enc
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
if(enc == 0 ){
|
if (enc == 0)
|
||||||
if (nx_emmc_part_read(&storage, prodinfo_part, sector, count, buff)) {
|
{
|
||||||
return RES_OK;
|
if (nx_emmc_part_read(&storage, prodinfo_part, sector, count, buff))
|
||||||
}
|
{
|
||||||
return RES_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
__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 (nx_emmc_part_read(&storage, prodinfo_part, sector, count, buff)) {
|
|
||||||
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
|
|
||||||
_emmc_xts(9, 8, 0, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200);
|
|
||||||
|
|
||||||
prev_sector = sector + count - 1;
|
|
||||||
return RES_OK;
|
return RES_OK;
|
||||||
}
|
}
|
||||||
|
return RES_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
__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 (nx_emmc_part_read(&storage, prodinfo_part, sector, count, buff))
|
||||||
|
{
|
||||||
|
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
|
||||||
|
_emmc_xts(9, 8, 0, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200);
|
||||||
|
|
||||||
|
prev_sector = sector + count - 1;
|
||||||
|
return RES_OK;
|
||||||
|
}
|
||||||
|
|
||||||
return RES_ERROR;
|
return RES_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
DRESULT disk_write_prod (
|
DRESULT disk_write_prod(
|
||||||
|
|
||||||
BYTE *buff, /* Data buffer to store read data */
|
BYTE *buff, /* Data buffer to store read data */
|
||||||
DWORD sector, /* Start sector in LBA */
|
DWORD sector, /* Start sector in LBA */
|
||||||
UINT count, /* Number of sectors to read */
|
UINT count, /* Number of sectors to read */
|
||||||
BYTE enc
|
BYTE enc)
|
||||||
)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
if(enc == 0){
|
if (enc == 0)
|
||||||
nx_emmc_part_write(&storage, prodinfo_part, sector, count, buff);
|
{
|
||||||
return RES_OK;
|
if (nx_emmc_part_write(&storage, prodinfo_part, sector, count, buff))
|
||||||
}
|
{
|
||||||
|
|
||||||
__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
|
|
||||||
_emmc_xts(9, 8, 1, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200);
|
|
||||||
nx_emmc_part_write(&storage, prodinfo_part, sector, count, buff);
|
|
||||||
prev_sector = sector + count - 1;
|
|
||||||
return RES_OK;
|
return RES_OK;
|
||||||
|
}
|
||||||
|
return RES_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
__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
|
||||||
|
_emmc_xts(9, 8, 1, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200);
|
||||||
|
if (nx_emmc_part_write(&storage, prodinfo_part, sector, count, buff))
|
||||||
|
{
|
||||||
|
prev_sector = sector + count - 1;
|
||||||
|
return RES_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
return RES_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DRESULT disk_read(
|
||||||
DRESULT disk_read (
|
BYTE pdrv, /* Physical drive number to identify the drive */
|
||||||
BYTE pdrv, /* Physical drive number to identify the drive */
|
BYTE *buff, /* Data buffer to store read data */
|
||||||
BYTE *buff, /* Data buffer to store read data */
|
DWORD sector, /* Start sector in LBA */
|
||||||
DWORD sector, /* Start sector in LBA */
|
UINT count /* Number of sectors to read */
|
||||||
UINT count /* Number of sectors to read */
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
switch (pdrv)
|
switch (pdrv)
|
||||||
|
@ -229,16 +243,19 @@ DRESULT disk_read (
|
||||||
return sdmmc_storage_read(&sd_storage, sector, count, buff) ? RES_OK : RES_ERROR;
|
return sdmmc_storage_read(&sd_storage, sector, count, buff) ? RES_OK : RES_ERROR;
|
||||||
|
|
||||||
case 1:;
|
case 1:;
|
||||||
__attribute__ ((aligned (16))) static u8 tweak[0x10];
|
__attribute__((aligned(16))) static u8 tweak[0x10];
|
||||||
__attribute__ ((aligned (16))) static u64 prev_cluster = -1;
|
__attribute__((aligned(16))) static u64 prev_cluster = -1;
|
||||||
__attribute__ ((aligned (16))) static u32 prev_sector = 0;
|
__attribute__((aligned(16))) static u32 prev_sector = 0;
|
||||||
u32 tweak_exp = 0;
|
u32 tweak_exp = 0;
|
||||||
bool regen_tweak = true, cache_sector = false;
|
bool regen_tweak = true, cache_sector = false;
|
||||||
|
|
||||||
u32 s = 0;
|
u32 s = 0;
|
||||||
if (count == 1) {
|
if (count == 1)
|
||||||
for ( ; s < secindex; s++) {
|
{
|
||||||
if (sector_cache[s].sector == sector) {
|
for (; s < secindex; s++)
|
||||||
|
{
|
||||||
|
if (sector_cache[s].sector == sector)
|
||||||
|
{
|
||||||
sector_cache[s].visit_count++;
|
sector_cache[s].visit_count++;
|
||||||
memcpy(buff, sector_cache[s].cached_sector, 0x200);
|
memcpy(buff, sector_cache[s].cached_sector, 0x200);
|
||||||
memcpy(tweak, sector_cache[s].tweak, 0x10);
|
memcpy(tweak, sector_cache[s].tweak, 0x10);
|
||||||
|
@ -248,7 +265,8 @@ DRESULT disk_read (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// add to cache
|
// add to cache
|
||||||
if (s == secindex && s < MAX_SEC_CACHE_ENTRIES) {
|
if (s == secindex && s < MAX_SEC_CACHE_ENTRIES)
|
||||||
|
{
|
||||||
sector_cache[s].sector = sector;
|
sector_cache[s].sector = sector;
|
||||||
sector_cache[s].visit_count++;
|
sector_cache[s].visit_count++;
|
||||||
cache_sector = true;
|
cache_sector = true;
|
||||||
|
@ -256,20 +274,27 @@ DRESULT disk_read (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nx_emmc_part_read(&storage, system_part, sector, count, buff)) {
|
if (nx_emmc_part_read(&storage, system_part, sector, count, buff))
|
||||||
if (prev_cluster != sector / 0x20) { // sector in different cluster than last read
|
{
|
||||||
|
if (prev_cluster != sector / 0x20)
|
||||||
|
{ // sector in different cluster than last read
|
||||||
prev_cluster = sector / 0x20;
|
prev_cluster = sector / 0x20;
|
||||||
tweak_exp = sector % 0x20;
|
tweak_exp = sector % 0x20;
|
||||||
} else if (sector > prev_sector) { // sector in same cluster and past last sector
|
}
|
||||||
|
else if (sector > prev_sector)
|
||||||
|
{ // sector in same cluster and past last sector
|
||||||
tweak_exp = sector - prev_sector - 1;
|
tweak_exp = sector - prev_sector - 1;
|
||||||
regen_tweak = false;
|
regen_tweak = false;
|
||||||
} else { // sector in same cluster and before or same as last sector
|
}
|
||||||
|
else
|
||||||
|
{ // sector in same cluster and before or same as last sector
|
||||||
tweak_exp = sector % 0x20;
|
tweak_exp = sector % 0x20;
|
||||||
}
|
}
|
||||||
|
|
||||||
// fatfs will never pull more than a cluster
|
// fatfs will never pull more than a cluster
|
||||||
_emmc_xts(9, 8, 0, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200);
|
_emmc_xts(9, 8, 0, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200);
|
||||||
if (cache_sector) {
|
if (cache_sector)
|
||||||
|
{
|
||||||
memcpy(sector_cache[s].cached_sector, buff, 0x200);
|
memcpy(sector_cache[s].cached_sector, buff, 0x200);
|
||||||
memcpy(sector_cache[s].tweak, tweak, 0x10);
|
memcpy(sector_cache[s].tweak, tweak, 0x10);
|
||||||
}
|
}
|
||||||
|
@ -281,11 +306,11 @@ DRESULT disk_read (
|
||||||
return RES_ERROR;
|
return RES_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
DRESULT disk_write (
|
DRESULT disk_write(
|
||||||
BYTE pdrv, /* Physical drive number to identify the drive */
|
BYTE pdrv, /* Physical drive number to identify the drive */
|
||||||
const BYTE *buff, /* Data to be written */
|
const BYTE *buff, /* Data to be written */
|
||||||
DWORD sector, /* Start sector in LBA */
|
DWORD sector, /* Start sector in LBA */
|
||||||
UINT count /* Number of sectors to write */
|
UINT count /* Number of sectors to write */
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (pdrv == 1)
|
if (pdrv == 1)
|
||||||
|
@ -293,10 +318,10 @@ DRESULT disk_write (
|
||||||
return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
|
return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
DRESULT disk_ioctl (
|
DRESULT disk_ioctl(
|
||||||
BYTE pdrv, /* Physical drive number (0..) */
|
BYTE pdrv, /* Physical drive number (0..) */
|
||||||
BYTE cmd, /* Control code */
|
BYTE cmd, /* Control code */
|
||||||
void *buff /* Buffer to send/receive control data */
|
void *buff /* Buffer to send/receive control data */
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return RES_OK;
|
return RES_OK;
|
||||||
|
|
Loading…
Reference in a new issue