fix indicators, add emmc_mount, various fixes

This commit is contained in:
Christoph Baumann
2025-04-29 12:36:32 +02:00
parent 31c8ad0d88
commit 28796b417b
6 changed files with 792 additions and 423 deletions

View File

@@ -27,7 +27,16 @@ static bool _is_eligible(){
} }
bool boot_storage_get_mounted(){ bool boot_storage_get_mounted(){
return drive != DEV_INVALID; switch(drive){
case DRIVE_SD:
return sd_get_card_mounted();
case DRIVE_EMMC:
return emmc_get_mounted();
case DRIVE_BOOT1:
case DRIVE_BOOT1_1MB:
return drive != DEV_INVALID;
}
return false;
} }
bool boot_storage_get_initialized(){ bool boot_storage_get_initialized(){
@@ -51,23 +60,36 @@ static bool _boot_storage_initialize(){
case DRIVE_SD: case DRIVE_SD:
return sd_initialize(false); return sd_initialize(false);
} }
return false; return false;
} }
static void _boot_storage_end(bool deinit){ static void _boot_storage_end(bool deinit){
if(boot_storage_get_mounted()){ if(boot_storage_get_mounted()){
if(drive == DRIVE_SD){ switch(drive){
case DRIVE_SD:
sd_unmount(); sd_unmount();
}else{ break;
f_mount(NULL, drive_base_paths[drive], false); case DRIVE_EMMC:
emmc_unmount();
break;
case DRIVE_BOOT1:
case DRIVE_BOOT1_1MB:
f_mount(NULL, drive_base_paths[drive], 0);
} }
drive = DEV_INVALID; drive = DEV_INVALID;
} }
if(deinit){ if(deinit){
if(drive == DRIVE_SD){ switch(drive){
case DRIVE_SD:
sd_end(); sd_end();
}else{ break;
case DRIVE_EMMC:
case DRIVE_BOOT1:
case DRIVE_BOOT1_1MB:
emmc_end(); emmc_end();
break;
} }
} }
} }
@@ -93,7 +115,7 @@ static bool _boot_storage_mount(){
goto emmc_init_fail; goto emmc_init_fail;
} }
static const BYTE emmc_drives[] = {DRIVE_BOOT1_1MB, DRIVE_BOOT1, DRIVE_EMMC}; static const BYTE emmc_drives[] = {DRIVE_BOOT1_1MB, DRIVE_BOOT1};
for(BYTE i = 0; i < ARRAY_SIZE(emmc_drives); i++){ for(BYTE i = 0; i < ARRAY_SIZE(emmc_drives); i++){
res = f_mount(&boot_storage_fs, drive_base_paths[emmc_drives[i]], true); res = f_mount(&boot_storage_fs, drive_base_paths[emmc_drives[i]], true);
@@ -121,14 +143,28 @@ static bool _boot_storage_mount(){
} }
emmc_init_fail: emmc_init_fail:
gfx_printf("trying %s\n", drive_base_paths[DRIVE_SD]); if(!emmc_initialize(false)){
goto emmc_init_fail2;
}
if(!emmc_mount()){
emmc_end();
goto emmc_init_fail2;
}
res = f_chdrive(drive_base_paths[DRIVE_EMMC]);
if(res == FR_OK && _is_eligible()){
drive = DRIVE_EMMC;
return true;
}
emmc_init_fail2:
if(!sd_initialize(false)){ if(!sd_initialize(false)){
gfx_printf("%s fail\n", drive_base_paths[DRIVE_SD]);
goto out; goto out;
} }
if(!sd_mount()){ if(!sd_mount()){
gfx_printf("%s fail\n", drive_base_paths[DRIVE_SD]);
sd_end(); sd_end();
goto out; goto out;
} }
@@ -136,7 +172,6 @@ emmc_init_fail:
res = f_chdrive(drive_base_paths[DRIVE_SD]); res = f_chdrive(drive_base_paths[DRIVE_SD]);
if(res == FR_OK && _is_eligible()){ if(res == FR_OK && _is_eligible()){
gfx_printf("%s ok\n", drive_base_paths[DRIVE_SD]);
drive = DRIVE_SD; drive = DRIVE_SD;
return true; return true;
} }

View File

@@ -21,11 +21,13 @@
#include <mem/heap.h> #include <mem/heap.h>
#include <soc/fuse.h> #include <soc/fuse.h>
#include <storage/mbr_gpt.h> #include <storage/mbr_gpt.h>
#include <gfx_utils.h>
#include <utils/list.h> #include <utils/list.h>
static u16 emmc_errors[3] = { 0 }; // Init and Read/Write errors. static u16 emmc_errors[3] = { 0 }; // Init and Read/Write errors.
static u32 emmc_mode = EMMC_MMC_HS400; static u32 emmc_mode = EMMC_MMC_HS400;
static bool emmc_init_done = false; static bool emmc_init_done = false;
static bool emmc_mounted = false;
sdmmc_t emmc_sdmmc; sdmmc_t emmc_sdmmc;
sdmmc_storage_t emmc_storage; sdmmc_storage_t emmc_storage;
@@ -62,14 +64,22 @@ u32 emmc_get_mode()
return emmc_mode; return emmc_mode;
} }
static void _emmc_deinit(){ static void _emmc_deinit(bool deinit){
if(emmc_init_done){ if(emmc_init_done){
sdmmc_storage_end(&emmc_storage); // TODO: Allow unmount even when not init'd?
emmc_init_done = false; if(emmc_mounted){
f_mount(NULL, "emmc:", 0);
}
if(deinit){
sdmmc_storage_end(&emmc_storage);
emmc_init_done = false;
}
} }
emmc_mounted = false;
} }
void emmc_end() { _emmc_deinit(); } void emmc_end() { _emmc_deinit(true); }
bool emmc_get_initialized(){ bool emmc_get_initialized(){
return emmc_init_done; return emmc_init_done;
@@ -243,3 +253,44 @@ void nx_emmc_get_autorcm_masks(u8 *mod0, u8 *mod1)
*mod1 = 0x84; *mod1 = 0x84;
} }
} }
bool emmc_mount()
{
if (emmc_init_done && emmc_mounted)
return true;
int res = 0;
if (!emmc_init_done)
res = !emmc_initialize(false);
if (res)
{
gfx_con.mute = false;
EPRINTF("Failed to init eMMC.");
}
else
{
if (!emmc_mounted)
res = f_mount(&emmc_fs, "emmc:", 1); // Volume 0 is SD.
if (res == FR_OK)
{
emmc_mounted = true;
return true;
}
else
{
gfx_con.mute = false;
EPRINTFARGS("Failed to mount eMMC (FatFS Error %d).\nMake sure that a FAT partition exists..", res);
}
}
return false;
}
bool emmc_get_mounted(){
return emmc_mounted;
}
void emmc_unmount() { _emmc_deinit(false); }

View File

@@ -65,7 +65,10 @@ int emmc_init_retry(bool power_cycle);
bool emmc_initialize(bool power_cycle); bool emmc_initialize(bool power_cycle);
int emmc_set_partition(u32 partition); int emmc_set_partition(u32 partition);
void emmc_end(); void emmc_end();
bool emmc_mount();
void emmc_unmount();
bool emmc_get_initialized(); bool emmc_get_initialized();
bool emmc_get_mounted();
void emmc_gpt_parse(link_t *gpt); void emmc_gpt_parse(link_t *gpt);
void emmc_gpt_free(link_t *gpt); void emmc_gpt_free(link_t *gpt);

File diff suppressed because it is too large Load Diff

View File

@@ -21,6 +21,7 @@
static u32 sd_rsvd_sectors = 0; static u32 sd_rsvd_sectors = 0;
static u32 ramdisk_sectors = 0; static u32 ramdisk_sectors = 0;
static u32 emummc_sectors = 0; static u32 emummc_sectors = 0;
static u32 sfd_sectors = 0;
static bool ensure_partition(BYTE pdrv){ static bool ensure_partition(BYTE pdrv){
u8 part; u8 part;
@@ -99,7 +100,7 @@ DRESULT disk_read (
case DRIVE_BOOT1: case DRIVE_BOOT1:
return sdmmc_storage_read(&emmc_storage, sector, count, buff) ? RES_OK : RES_ERROR; return sdmmc_storage_read(&emmc_storage, sector, count, buff) ? RES_OK : RES_ERROR;
case DRIVE_SFD: case DRIVE_SFD:
return sfd_read(sector, count ,buff) ? RES_OK : RES_ERROR; return sfd_read(sector, count, buff) ? RES_OK : RES_ERROR;
} }
return RES_ERROR; return RES_ERROR;
@@ -187,8 +188,16 @@ DRESULT disk_ioctl (
*buf = 32768; // Align to 16MB. *buf = 32768; // Align to 16MB.
break; break;
} }
} }else if(pdrv == DRIVE_SFD){
else // Catch all for unknown devices. switch(cmd){
case GET_SECTOR_COUNT:
*buf = sfd_sectors;
break;
case GET_BLOCK_SIZE:
*buf = 32768;
break;
}
}else // Catch all for unknown devices.
{ {
switch (cmd) switch (cmd)
{ {
@@ -225,6 +234,9 @@ DRESULT disk_set_info (
case DRIVE_EMU: case DRIVE_EMU:
emummc_sectors = *buf; emummc_sectors = *buf;
break; break;
case DRIVE_SFD:
sfd_sectors = *buf;
break;
} }
} }

View File

@@ -1,6 +1,7 @@
#include "sfd.h" #include "sfd.h"
#include <storage/emmc.h> #include <storage/emmc.h>
#include <storage/sdmmc.h> #include <storage/sdmmc.h>
#include <libs/fatfs/diskio.h>
static sdmmc_storage_t *_storage; static sdmmc_storage_t *_storage;
static u32 _offset; static u32 _offset;
@@ -13,7 +14,7 @@ static void ensure_partition(){
} }
int sfd_read(u32 sector, u32 count, void *buff){ int sfd_read(u32 sector, u32 count, void *buff){
if(sector + _offset + count > _offset + _size){ if(sector + count > _size){
return 0; return 0;
} }
ensure_partition(); ensure_partition();
@@ -21,7 +22,7 @@ int sfd_read(u32 sector, u32 count, void *buff){
} }
int sfd_write(u32 sector, u32 count, void *buff){ int sfd_write(u32 sector, u32 count, void *buff){
if(sector + _offset + count > _offset + _size){ if(sector + count > _size){
return 0; return 0;
} }
ensure_partition(); ensure_partition();
@@ -32,6 +33,7 @@ bool sfd_init(sdmmc_storage_t *storage, u32 offset, u32 size){
_storage = storage; _storage = storage;
_offset = offset; _offset = offset;
_size = size; _size = size;
disk_set_info(DRIVE_SFD, SET_SECTOR_COUNT, &size);
return true; return true;
} }
@@ -39,4 +41,6 @@ void sfd_end(){
_storage = NULL; _storage = NULL;
_offset = 0; _offset = 0;
_size = 0; _size = 0;
u32 size = 0;
disk_set_info(DRIVE_SFD, SET_SECTOR_COUNT, &size);
} }