154 lines
2.7 KiB
C
154 lines
2.7 KiB
C
#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;
|
|
} |