git subrepo clone --force --branch=exo2 https://github.com/m4xw/emummc
subrepo: subdir: "emummc" merged: "3791be9f" upstream: origin: "https://github.com/m4xw/emummc" branch: "exo2" commit: "3791be9f" git-subrepo: version: "0.4.1" origin: "???" commit: "???"
This commit is contained in:
@@ -18,14 +18,12 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../soc/gpio.h"
|
||||
#include "../utils/fatal.h"
|
||||
#include "../libs/fatfs/diskio.h"
|
||||
#include "emummc.h"
|
||||
#include "emummc_ctx.h"
|
||||
#include "../utils/fatal.h"
|
||||
#include "../libs/fatfs/diskio.h"
|
||||
|
||||
static bool sdmmc_first_init = false;
|
||||
static bool storageMMCinitialized = false;
|
||||
static bool storageSDinitialized = false;
|
||||
|
||||
// hekate sdmmmc vars
|
||||
@@ -35,6 +33,7 @@ sdmmc_t sd_sdmmc;
|
||||
sdmmc_storage_t sd_storage;
|
||||
|
||||
// init vars
|
||||
bool init_done = false;
|
||||
bool custom_driver = true;
|
||||
|
||||
// FS funcs
|
||||
@@ -51,8 +50,8 @@ volatile int *active_partition;
|
||||
volatile Handle *sdmmc_das_handle;
|
||||
|
||||
// FatFS
|
||||
file_based_ctxt f_emu;
|
||||
static bool fat_mounted = false;
|
||||
static file_based_ctxt f_emu;
|
||||
|
||||
static void _sdmmc_ensure_device_attached(void)
|
||||
{
|
||||
@@ -68,8 +67,6 @@ static void _sdmmc_ensure_device_attached(void)
|
||||
|
||||
static void _sdmmc_ensure_initialized(void)
|
||||
{
|
||||
static bool init_done = false;
|
||||
|
||||
// First Initial init
|
||||
if (!sdmmc_first_init)
|
||||
{
|
||||
@@ -78,10 +75,11 @@ static void _sdmmc_ensure_initialized(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
// The boot sysmodule will eventually kill power to SD. Detect this, and reinitialize when it happens.
|
||||
// The boot sysmodule will eventually kill power to SD.
|
||||
// Detect this, and reinitialize when it happens.
|
||||
if (!init_done)
|
||||
{
|
||||
if (gpio_read(GPIO_PORT_E, GPIO_PIN_4) == 0)
|
||||
if (sdmmc_get_sd_power_enabled() == 0)
|
||||
{
|
||||
sdmmc_finalize();
|
||||
sdmmc_initialize();
|
||||
@@ -118,8 +116,6 @@ static void _file_based_emmc_finalize(void)
|
||||
|
||||
void sdmmc_finalize(void)
|
||||
{
|
||||
_file_based_emmc_finalize();
|
||||
|
||||
if (!sdmmc_storage_end(&sd_storage))
|
||||
{
|
||||
fatal_abort(Fatal_InitSD);
|
||||
@@ -132,7 +128,6 @@ static void _file_based_emmc_initialize(void)
|
||||
{
|
||||
char path[sizeof(emuMMC_ctx.storagePath) + 0x20];
|
||||
memset(&path, 0, sizeof(path));
|
||||
memset(&f_emu, 0, sizeof(file_based_ctxt));
|
||||
|
||||
memcpy(path, (void *)emuMMC_ctx.storagePath, sizeof(emuMMC_ctx.storagePath));
|
||||
strcat(path, "/eMMC/");
|
||||
@@ -141,18 +136,24 @@ static void _file_based_emmc_initialize(void)
|
||||
// Open BOOT0 physical partition.
|
||||
memcpy(path + path_len, "BOOT0", 6);
|
||||
if (f_open(&f_emu.fp_boot0, path, FA_READ | FA_WRITE) != FR_OK)
|
||||
fatal_abort(Fatal_InitSD);
|
||||
fatal_abort(Fatal_FatfsFileOpen);
|
||||
if (!f_expand_cltbl(&f_emu.fp_boot0, 0x400, f_emu.clmt_boot0, f_size(&f_emu.fp_boot0)))
|
||||
fatal_abort(Fatal_FatfsMemExhaustion);
|
||||
|
||||
// Open BOOT1 physical partition.
|
||||
memcpy(path + path_len, "BOOT1", 6);
|
||||
if (f_open(&f_emu.fp_boot1, path, FA_READ | FA_WRITE) != FR_OK)
|
||||
fatal_abort(Fatal_InitSD);
|
||||
fatal_abort(Fatal_FatfsFileOpen);
|
||||
if (!f_expand_cltbl(&f_emu.fp_boot1, 0x400, f_emu.clmt_boot1, f_size(&f_emu.fp_boot1)))
|
||||
fatal_abort(Fatal_FatfsMemExhaustion);
|
||||
|
||||
// Open handles for GPP physical partition files.
|
||||
_file_based_update_filename(path, path_len, 00);
|
||||
|
||||
if (f_open(&f_emu.fp_gpp[0], path, FA_READ | FA_WRITE) != FR_OK)
|
||||
fatal_abort(Fatal_InitSD);
|
||||
fatal_abort(Fatal_FatfsFileOpen);
|
||||
if (!f_expand_cltbl(&f_emu.fp_gpp[0], 0x400, &f_emu.clmt_gpp[0], f_size(&f_emu.fp_gpp[0])))
|
||||
fatal_abort(Fatal_FatfsMemExhaustion);
|
||||
|
||||
f_emu.part_size = f_size(&f_emu.fp_gpp[0]) >> 9;
|
||||
|
||||
@@ -171,38 +172,27 @@ static void _file_based_emmc_initialize(void)
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!f_expand_cltbl(&f_emu.fp_gpp[f_emu.parts], 0x400, &f_emu.clmt_gpp[f_emu.parts * 0x400], f_size(&f_emu.fp_gpp[f_emu.parts])))
|
||||
fatal_abort(Fatal_FatfsMemExhaustion);
|
||||
}
|
||||
}
|
||||
|
||||
bool sdmmc_initialize(void)
|
||||
{
|
||||
if (!storageMMCinitialized)
|
||||
{
|
||||
if (sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_4, SDMMC_BUS_WIDTH_8, 4))
|
||||
{
|
||||
if (sdmmc_storage_set_mmc_partition(&storage, FS_EMMC_PARTITION_GPP))
|
||||
storageMMCinitialized = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
fatal_abort(Fatal_InitMMC);
|
||||
}
|
||||
}
|
||||
|
||||
if (!storageSDinitialized)
|
||||
{
|
||||
int retries = 5;
|
||||
int retries = 3;
|
||||
while (retries)
|
||||
{
|
||||
if (sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, SDMMC_1, SDMMC_BUS_WIDTH_4, 11))
|
||||
if (nx_sd_initialize(false))
|
||||
{
|
||||
storageSDinitialized = true;
|
||||
|
||||
// File based emummc.
|
||||
if ((emuMMC_ctx.EMMC_Type == emuMMC_SD_File) && !fat_mounted)
|
||||
{
|
||||
f_emu.sd_fs = (FATFS *)malloc(sizeof(FATFS));
|
||||
if (f_mount(f_emu.sd_fs, "", 1) != FR_OK)
|
||||
if (f_mount(&f_emu.sd_fs, "", 1) != FR_OK)
|
||||
fatal_abort(Fatal_InitSD);
|
||||
else
|
||||
fat_mounted = true;
|
||||
@@ -214,7 +204,6 @@ bool sdmmc_initialize(void)
|
||||
}
|
||||
|
||||
retries--;
|
||||
msleep(100);
|
||||
}
|
||||
|
||||
if (!storageSDinitialized)
|
||||
@@ -223,7 +212,7 @@ bool sdmmc_initialize(void)
|
||||
}
|
||||
}
|
||||
|
||||
return storageMMCinitialized && storageSDinitialized;
|
||||
return storageSDinitialized;
|
||||
}
|
||||
|
||||
sdmmc_accessor_t *sdmmc_accessor_get(int mmc_id)
|
||||
@@ -295,38 +284,36 @@ static uint64_t emummc_read_write_inner(void *buf, unsigned int sector, unsigned
|
||||
}
|
||||
|
||||
// File based emummc.
|
||||
FIL *fp_tmp = NULL;
|
||||
FIL *fp = NULL;
|
||||
switch (*active_partition)
|
||||
{
|
||||
case FS_EMMC_PARTITION_GPP:
|
||||
if (f_emu.parts)
|
||||
{
|
||||
fp_tmp = &f_emu.fp_gpp[sector / f_emu.part_size];
|
||||
fp = &f_emu.fp_gpp[sector / f_emu.part_size];
|
||||
sector = sector % f_emu.part_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
fp_tmp = &f_emu.fp_gpp[0];
|
||||
fp = &f_emu.fp_gpp[0];
|
||||
}
|
||||
break;
|
||||
case FS_EMMC_PARTITION_BOOT1:
|
||||
fp_tmp = &f_emu.fp_boot1;
|
||||
fp = &f_emu.fp_boot1;
|
||||
break;
|
||||
case FS_EMMC_PARTITION_BOOT0:
|
||||
fp_tmp = &f_emu.fp_boot0;
|
||||
fp = &f_emu.fp_boot0;
|
||||
break;
|
||||
}
|
||||
|
||||
if (f_lseek(fp_tmp, sector << 9) != FR_OK)
|
||||
{
|
||||
; //TODO. Out of range. close stuff and fatal?
|
||||
}
|
||||
if (f_lseek(fp, sector << 9) != FR_OK)
|
||||
return 0; // Out of bounds.
|
||||
|
||||
uint64_t res = 0;
|
||||
if (!is_write)
|
||||
res = !(f_read(fp_tmp, buf, num_sectors << 9, NULL));
|
||||
res = !f_read_fast(fp, buf, num_sectors << 9);
|
||||
else
|
||||
res = !(f_write(fp_tmp, buf, num_sectors << 9, NULL));
|
||||
res = !f_write_fast(fp, buf, num_sectors << 9);
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -402,12 +389,15 @@ uint64_t sdmmc_wrapper_read(void *buf, uint64_t bufSize, int mmc_id, unsigned in
|
||||
if (first_sd_read)
|
||||
{
|
||||
first_sd_read = false;
|
||||
// Because some SD cards have issues with emuMMC's driver
|
||||
// we currently swap to FS's driver after first SD read
|
||||
// TODO: Fix remaining driver issues
|
||||
custom_driver = false;
|
||||
// FS will handle sd mutex w/o custom driver from here on
|
||||
unlock_mutex(sd_mutex);
|
||||
if (emuMMC_ctx.EMMC_Type == emuMMC_SD)
|
||||
{
|
||||
// Because some SD cards have issues with emuMMC's driver
|
||||
// we currently swap to FS's driver after first SD read
|
||||
// for raw based emuMMC
|
||||
custom_driver = false;
|
||||
// FS will handle sd mutex w/o custom driver from here on
|
||||
unlock_mutex(sd_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
// Call hekates driver.
|
||||
|
||||
@@ -28,6 +28,7 @@ extern "C" {
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../emmc/nx_sd.h"
|
||||
#include "../emmc/sdmmc.h"
|
||||
#include "../soc/i2c.h"
|
||||
#include "../soc/gpio.h"
|
||||
@@ -55,15 +56,17 @@ uint64_t sdmmc_wrapper_controller_close(int mmc_id);
|
||||
uint64_t sdmmc_wrapper_read(void *buf, uint64_t bufSize, int mmc_id, unsigned int sector, unsigned int num_sectors);
|
||||
uint64_t sdmmc_wrapper_write(int mmc_id, unsigned int sector, unsigned int num_sectors, void *buf, uint64_t bufSize);
|
||||
|
||||
// TODO: check if FatFS internal buffers are good (perf wise) to have a x16 alignment.
|
||||
typedef struct _file_based_ctxt
|
||||
{
|
||||
FATFS sd_fs;
|
||||
uint64_t parts;
|
||||
uint64_t part_size;
|
||||
FATFS *sd_fs;
|
||||
FIL fp_boot0;
|
||||
DWORD clmt_boot0[0x400];
|
||||
FIL fp_boot1;
|
||||
DWORD clmt_boot1[0x400];
|
||||
FIL fp_gpp[32];
|
||||
DWORD clmt_gpp[0x8000];
|
||||
} file_based_ctxt;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -44,7 +44,7 @@ typedef struct _emuMMC_ctx_t
|
||||
enum FS_VER fs_ver;
|
||||
enum emuMMC_Type EMMC_Type;
|
||||
enum emuMMC_Type SD_Type;
|
||||
|
||||
|
||||
/* Partition based */
|
||||
u64 EMMC_StoragePartitionOffset;
|
||||
u64 SD_StoragePartitionOffset;
|
||||
|
||||
Reference in New Issue
Block a user