Add support for reading boot files from FAT partition on BOOT1/GPP/SD in that order

Emummc config is read from boot partition, file based files still read from SD
This commit is contained in:
Christoph Baumann
2025-04-22 13:42:20 +02:00
parent 9d2ec42a9f
commit 5798dcde0d
14 changed files with 310 additions and 38 deletions

View File

@@ -0,0 +1,154 @@
#include "boot_storage.h"
#include <libs/fatfs/ff.h>
#include "../libs/fatfs/ffconf.h"
#include <storage/sd.h>
#include <storage/emmc.h>
#include <utils/types.h>
#define DEV_INVALID 0xff
static FATFS boot_storage_fs;
static BYTE drive = -1;
static const char* drive_base_paths[] = {
[FF_DEV_SD] = XSTR(FF_DEV_SD) ":",
[FF_DEV_BOOT1] = XSTR(FF_DEV_BOOT1) ":",
[FF_DEV_BOOT1_1MB] = XSTR(FF_DEV_BOOT1_1MB) ":",
[FF_DEV_GPP] = XSTR(FF_DEV_GPP) ":",
};
static bool _is_eligible(){
if(f_stat(".no_boot_storage", NULL) == FR_OK){
return false;
}
return true;
}
bool boot_storage_get_mounted(){
return drive != DEV_INVALID;
}
bool boot_storage_get_initialized(){
switch(drive){
case FF_DEV_BOOT1:
case FF_DEV_GPP:
case FF_DEV_BOOT1_1MB:
return emmc_get_initialized();
case FF_DEV_SD:
return sd_get_card_initialized();
}
return false;
}
static bool _boot_storage_initialize(){
switch(drive){
case FF_DEV_BOOT1:
case FF_DEV_GPP:
case FF_DEV_BOOT1_1MB:
return emmc_initialize(false);
case FF_DEV_SD:
return sd_initialize(false);
}
return false;
}
static void _boot_storage_end(bool deinit){
if(boot_storage_get_mounted()){
if(drive == FF_DEV_SD){
sd_unmount();
}else{
f_mount(NULL, drive_base_paths[drive], false);
}
drive = DEV_INVALID;
}
if(deinit){
if(drive == FF_DEV_SD){
sd_end();
}else{
emmc_end();
}
}
}
void boot_storage_unmount(){
_boot_storage_end(false);
}
void boot_storage_end(){
_boot_storage_end(true);
}
static bool _boot_storage_mount(){
// may want to check sd card first and prioritize it
FRESULT res;
if(!emmc_get_initialized() && !emmc_initialize(false)){
goto emmc_init_fail;
}
static const BYTE emmc_drives[] = {FF_DEV_BOOT1_1MB, FF_DEV_BOOT1, FF_DEV_GPP};
for(BYTE i = 0; i < ARRAY_SIZE(emmc_drives); i++){
res = f_mount(&boot_storage_fs, drive_base_paths[i], true);
if(res == FR_OK){
res = f_chdrive(drive_base_paths[i]);
if(res == FR_OK && _is_eligible()){
drive = i;
break;
}else{
f_mount(NULL, drive_base_paths[i],false);
res = FR_INVALID_DRIVE;
}
}
}
if(res != FR_OK){
emmc_end();
}
if(res == FR_OK){
return true;
}
emmc_init_fail:
if(!sd_initialize(false)){
goto out;
}
if(!sd_mount()){
sd_end();
goto out;
}
res = f_chdrive(drive_base_paths[FF_DEV_SD]);
if(res == FR_OK && _is_eligible()){
drive = FF_DEV_SD;
return true;
}
sd_end();
out:
return false;
}
bool boot_storage_mount(){
bool mounted = boot_storage_get_mounted();
bool initialized = boot_storage_get_initialized();
if(mounted && initialized){
return true;
}
if(!mounted){
// not mounted. mounting will also initialize.
return _boot_storage_mount();
}
if(!initialized){
return _boot_storage_initialize();
}
return true;
}

View File

@@ -0,0 +1,16 @@
#ifndef _BOOT_STORAGE_H
#define _BOOT_STORAGE_H
#include <utils/types.h>
// check if boot1 (1mb), boot1, gpp, sd (in that order) have fat32 partition,
// mount the partition and set the current drive to it
bool boot_storage_mount();
void boot_storage_unmount();
void boot_storage_end();
bool boot_storage_get_mounted();
bool boot_storage_get_initialized();
#endif

View File

@@ -23,6 +23,8 @@
#include "../config.h"
#include <libs/fatfs/ff.h>
#define DEV_SD_BASE_PATH XSTR(FF_DEV_SD) ":"
extern hekate_config h_cfg;
emummc_cfg_t emu_cfg = { 0 };
@@ -77,7 +79,8 @@ bool emummc_set_path(char *path)
FIL fp;
bool found = false;
strcpy(emu_cfg.emummc_file_based_path, path);
strcpy(emu_cfg.emummc_file_based_path, DEV_SD_BASE_PATH);
strcat(emu_cfg.emummc_file_based_path, path);
strcat(emu_cfg.emummc_file_based_path, "/raw_based");
if (!f_open(&fp, emu_cfg.emummc_file_based_path, FA_READ))
@@ -88,7 +91,8 @@ bool emummc_set_path(char *path)
}
else
{
strcpy(emu_cfg.emummc_file_based_path, path);
strcpy(emu_cfg.emummc_file_based_path, DEV_SD_BASE_PATH);
strcat(emu_cfg.emummc_file_based_path, path);
strcat(emu_cfg.emummc_file_based_path, "/file_based");
if (!f_stat(emu_cfg.emummc_file_based_path, NULL))
@@ -149,7 +153,8 @@ int emummc_storage_init_mmc()
if (!emu_cfg.sector)
{
strcpy(emu_cfg.emummc_file_based_path, emu_cfg.path);
strcpy(emu_cfg.emummc_file_based_path, DEV_SD_BASE_PATH);
strcat(emu_cfg.emummc_file_based_path, emu_cfg.path);
strcat(emu_cfg.emummc_file_based_path, "/eMMC");
if (f_stat(emu_cfg.emummc_file_based_path, &fno))
@@ -279,7 +284,8 @@ int emummc_storage_set_mmc_partition(u32 partition)
return 1;
else
{
strcpy(emu_cfg.emummc_file_based_path, emu_cfg.path);
strcpy(emu_cfg.emummc_file_based_path, DEV_SD_BASE_PATH);
strcat(emu_cfg.emummc_file_based_path, emu_cfg.path);
strcat(emu_cfg.emummc_file_based_path, "/eMMC");
switch (partition)