From 02504c8a36a6b999910545e2f0fe43267dc94b9b Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Mon, 6 Jan 2020 14:24:29 +0100 Subject: [PATCH] Make emmc writable + merge hekate fixes --- source/gfx/gfx.c | 3 ++- source/libs/fatfs/diskio.c | 46 +++++++++++++++++++++++++++++--------- source/libs/fatfs/diskio.h | 2 +- source/mem/minerva.c | 1 + source/tegraexplorer/fs.c | 28 ++++++++++++++--------- 5 files changed, 57 insertions(+), 23 deletions(-) diff --git a/source/gfx/gfx.c b/source/gfx/gfx.c index 3d2c9cb..fc1dc29 100644 --- a/source/gfx/gfx.c +++ b/source/gfx/gfx.c @@ -188,7 +188,7 @@ void gfx_putc(char c) for (u32 i = 0; i < 16; i+=2) { - u8 v = *cbuf++; + u8 v = *cbuf; for (u32 k = 0; k < 2; k++) { for (u32 j = 0; j < 8; j++) @@ -213,6 +213,7 @@ void gfx_putc(char c) fb += gfx_ctxt.stride - 16; v = *cbuf; } + cbuf++; } gfx_con.x += 16; if (gfx_con.x >= gfx_ctxt.width - 16) { diff --git a/source/libs/fatfs/diskio.c b/source/libs/fatfs/diskio.c index c0e70c9..211b4ef 100644 --- a/source/libs/fatfs/diskio.c +++ b/source/libs/fatfs/diskio.c @@ -150,7 +150,7 @@ DRESULT disk_read ( __attribute__ ((aligned (16))) static u8 tweak[0x10]; __attribute__ ((aligned (16))) static u64 prev_cluster = -1; __attribute__ ((aligned (16))) static u32 prev_sector = 0; - bool needs_cache_sector = false; + //bool needs_cache_sector = false; /* if (secindex == 0 || clear_sector_cache) { if (!sector_cache) @@ -212,20 +212,46 @@ DRESULT disk_read ( DRESULT disk_write ( BYTE pdrv, /* Physical drive number to identify the drive */ - const BYTE *buff, /* Data to be written */ + BYTE *buff, /* Data to be written */ DWORD sector, /* Start sector in LBA */ UINT count /* Number of sectors to write */ ) { - if (pdrv == 1) - return RES_WRPRT; + switch (pdrv){ + case 0: + if (((u32)buff >= DRAM_START) && !((u32)buff % 8)) + return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR; + u8 *buf = (u8 *)SDMMC_UPPER_BUFFER; //TODO: define this somewhere. + memcpy(buf, buff, 512 * count); + if (sdmmc_storage_write(&sd_storage, sector, count, buf)) + return RES_OK; + return RES_ERROR; + case 1:; + __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 (((u32)buff >= DRAM_START) && !((u32)buff % 8)) - return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR; - u8 *buf = (u8 *)SDMMC_UPPER_BUFFER; //TODO: define this somewhere. - memcpy(buf, buff, 512 * count); - if (sdmmc_storage_write(&sd_storage, sector, count, buf)) - return RES_OK; + 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; + } + + if (_emmc_xts(9, 8, 1, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200)){ + if (nx_emmc_part_write(&storage, system_part, sector, count, buff)){ + prev_sector = sector + count - 1; + return RES_OK; + } + } + + return RES_ERROR; + } return RES_ERROR; } diff --git a/source/libs/fatfs/diskio.h b/source/libs/fatfs/diskio.h index d6ae8f8..b5528f5 100644 --- a/source/libs/fatfs/diskio.h +++ b/source/libs/fatfs/diskio.h @@ -31,7 +31,7 @@ typedef enum { DSTATUS disk_initialize (BYTE pdrv); DSTATUS disk_status (BYTE pdrv); DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); -DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); +DRESULT disk_write (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); diff --git a/source/mem/minerva.c b/source/mem/minerva.c index 68aa66d..d2c966e 100644 --- a/source/mem/minerva.c +++ b/source/mem/minerva.c @@ -32,6 +32,7 @@ u32 minerva_init() { u32 curr_ram_idx = 0; + minerva_cfg = NULL; mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg; // Set table to nyx storage. diff --git a/source/tegraexplorer/fs.c b/source/tegraexplorer/fs.c index b01ad08..8377576 100644 --- a/source/tegraexplorer/fs.c +++ b/source/tegraexplorer/fs.c @@ -174,22 +174,23 @@ int copy(const char *locin, const char *locout, bool print, bool canCancel){ UINT temp1, temp2; u8 *buff; unsigned int x, y, i = 0; + int res; gfx_con_getpos(&x, &y); if (!strcmp(locin, locout)){ - return 3; + return -1; } if (f_open(&in, locin, FA_READ | FA_OPEN_EXISTING)){ - return 1; + return -2; } if (f_stat(locin, &in_info)) - return 1; + return -2; if (f_open(&out, locout, FA_CREATE_ALWAYS | FA_WRITE)){ - return 2; + return -3; } buff = malloc (BUFSIZE); @@ -197,13 +198,13 @@ int copy(const char *locin, const char *locout, bool print, bool canCancel){ totalsize = sizeoffile; while (sizeoffile > 0){ - if (f_read(&in, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp1)) - return 3; - if (f_write(&out, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp2)) - return 4; + if ((res = f_read(&in, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp1))) + return res; + if ((res = f_write(&out, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp2))) + return res; if (temp1 != temp2) - return 5; + return -4; sizeoffile -= temp1; sizecopied += temp1; @@ -487,12 +488,17 @@ void copyfolder(char *in, char *out){ int res; if (!strcmp(in, rootpath)){ - message(COLOR_RED, "In is root\nAborting!"); + message(COLOR_RED, "\nIn is root\nAborting!"); fatalerror = true; } if (strstr(out, in) != NULL && !fatalerror){ - message(COLOR_RED, "\nOut is a part of in!\nAborting"); + message(COLOR_RED, "\nOut is a part of in!\nAborting!"); + fatalerror = true; + } + + if (!strcmp(in, out) && !fatalerror){ + message(COLOR_RED, "\nIn is the same as out!\nAborting!"); fatalerror = true; }