Support for booting from eMMC

Fusee will Look for FAT32 partition on BOOT1 (1MB offset), BOOT1, GPP, SD, in this order
If a .no_boot_storage file is found in the root directory, the partition is skipped
This commit is contained in:
Christoph Baumann
2025-05-13 13:07:25 +02:00
parent c1b5cc411e
commit 867ff62dfa
15 changed files with 264 additions and 32 deletions

View File

@@ -15,6 +15,8 @@
#include "diskio_cpp.h"
#define BOOT1_OFFSET_SCT_1MB 0x800
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
@@ -57,6 +59,11 @@ DRESULT disk_read (
return diskio_read_sd_card(buff, count * 512, sector, count) ? RES_OK : RES_ERROR;
case DRIVE_SYS:
return diskio_read_system(buff, count * 512, sector, count) ? RES_OK : RES_ERROR;
case DRIVE_BOOT1:
return diskio_read_boot1(buff, count * 512, sector, count) ? RES_OK : RES_ERROR;
case DRIVE_BOOT1_OFF:
return diskio_read_boot1(buff, count * 512, sector + BOOT1_OFFSET_SCT_1MB, count) ? RES_OK : RES_ERROR;
default:
return RES_PARERR;
}
@@ -82,6 +89,10 @@ DRESULT disk_write (
return diskio_write_sd_card(sector, count, buff, count * 512) ? RES_OK : RES_ERROR;
case DRIVE_SYS:
return diskio_write_system(sector, count, buff, count * 512) ? RES_OK : RES_ERROR;
case DRIVE_BOOT1:
return diskio_write_boot1(sector, count, buff, count * 512) ? RES_OK : RES_ERROR;
case DRIVE_BOOT1_OFF:
return diskio_write_boot1(sector + DRIVE_BOOT1_OFF, count, buff, count * 512) ? RES_OK : RES_ERROR;
default:
return RES_PARERR;
}

View File

@@ -25,6 +25,8 @@ typedef enum {
typedef enum {
DRIVE_SD,
DRIVE_SYS,
DRIVE_BOOT1,
DRIVE_BOOT1_OFF,
} DDRIVE;

View File

@@ -25,6 +25,9 @@ bool diskio_write_sd_card(size_t sector_index, size_t sector_count, const void *
bool diskio_read_system(void *dst, size_t size, size_t sector_index, size_t sector_count);
bool diskio_write_system(size_t sector_index, size_t sector_count, const void *src, size_t size);
bool diskio_read_boot1(void *dst, size_t size, size_t sector_index, size_t sector_count);
bool diskio_write_boot1(size_t sector_index, size_t sector_count, const void *src, size_t size);
#ifdef __cplusplus
}
#endif

View File

@@ -150,7 +150,7 @@
*/
#define FF_FS_RPATH 0
#define FF_FS_RPATH 1
/* This option configures support for relative path.
/
/ 0: Disable relative path and remove related functions.
@@ -163,12 +163,12 @@
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/
#define FF_VOLUMES 2
#define FF_VOLUMES 4
/* Number of volumes (logical drives) to be used. (1-10) */
#define FF_STR_VOLUME_ID 1
#define FF_VOLUME_STRS "sdmc","sys"
#define FF_VOLUME_STRS "sdmc","sys","boot1","boot1_off"
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each

View File

@@ -33,4 +33,13 @@ bool diskio_read_system(void *dst, size_t size, size_t sector_index, size_t sect
bool diskio_write_system(size_t sector_index, size_t sector_count, const void *src, size_t size) {
/* Don't allow writes to eMMC GPP partition */
return false;
}
bool diskio_read_boot1(void *dst, size_t size, size_t sector_index, size_t sector_count) {
return R_SUCCEEDED(::ams::nxboot::ReadMmc(dst, size, ::ams::sdmmc::MmcPartition::MmcPartition_BootPartition2, sector_index, sector_count));
}
bool diskio_write_boot1(size_t sector_index, size_t sector_count, const void *src, size_t size) {
/* Don't allow writes to eMMC BOOT1 */
return false;
}

View File

@@ -26,11 +26,14 @@ namespace ams::fs {
constexpr size_t MaxFiles = 8 + 64;
constexpr size_t MaxDirectories = 2;
constinit bool g_is_sd_mounted = false;
constinit bool g_is_sys_mounted = false;
constinit bool g_is_sd_mounted = false;
constinit bool g_is_sys_mounted = false;
constinit bool g_is_boot1_mounted = false;
constinit bool g_is_boot1_off_mounted = false;
alignas(0x10) constinit FATFS g_sd_fs = {};
alignas(0x10) constinit FATFS g_sys_fs = {};
alignas(0x10) constinit FATFS g_sd_fs = {};
alignas(0x10) constinit FATFS g_sys_fs = {};
alignas(0x10) constinit FATFS g_boot1_fs = {};
alignas(0x10) constinit FIL g_files[MaxFiles] = {};
alignas(0x10) constinit DIR g_dirs[MaxDirectories] = {};
@@ -119,6 +122,32 @@ namespace ams::fs {
}
bool MountBoot1() {
/* Can't mount both */
AMS_ASSERT(!g_is_boot1_mounted && !g_is_boot1_off_mounted);
g_is_boot1_mounted = f_mount(std::addressof(g_boot1_fs), "boot1:", 1) == FR_OK;
return g_is_boot1_mounted;
}
void UnmountBoot1 () {
AMS_ASSERT(g_is_boot1_mounted);
f_unmount("boot1:");
g_is_boot1_mounted = false;
}
bool MountBoot1Off() {
/* Can't mount both */
AMS_ASSERT(!g_is_boot1_mounted && !g_is_boot1_off_mounted);
g_is_boot1_off_mounted = f_mount(std::addressof(g_boot1_fs), "boot1_off:", 1) == FR_OK;
return g_is_boot1_off_mounted;
}
void UnmountBoot1Off () {
AMS_ASSERT(g_is_boot1_off_mounted);
f_unmount("boot1_off:");
g_is_boot1_off_mounted = false;
}
bool MountSys() {
AMS_ASSERT(!g_is_sys_mounted);
g_is_sys_mounted = f_mount(std::addressof(g_sys_fs), "sys:", 1) == FR_OK;
@@ -143,6 +172,16 @@ namespace ams::fs {
g_is_sd_mounted = false;
}
Result ChangeDrive(const char *path) {
R_TRY(TranslateFatFsError(f_chdrive(path)));
R_SUCCEED();
}
Result ChangeDirectory(const char *path) {
R_TRY(TranslateFatFsError(f_chdir(path)));
R_SUCCEED();
}
Result GetEntryType(DirectoryEntryType *out_entry_type, bool *out_archive, const char *path) {
/* Get the file info. */
FILINFO info;

View File

@@ -101,6 +101,16 @@ namespace ams::fs {
bool MountSys();
void UnmountSys();
bool MountBoot1();
void UnmountBoot1();
/* Mount FAT32 FS on BOOT1 at 1MB offset */
bool MountBoot1Off();
void UnmountBoot1Off();
Result ChangeDrive(const char *path);
Result ChangeDirectory(const char *path);
Result GetEntryType(DirectoryEntryType *out_entry_type, bool *out_archive, const char *path);
Result CreateFile(const char *path, s64 size);

View File

@@ -0,0 +1,136 @@
#include "fusee_boot_storage.hpp"
#include "fs/fusee_fs_api.hpp"
#include "fusee_display.hpp"
#include "fusee_mmc.hpp"
#include "fusee_sd_card.hpp"
#include <exosphere.hpp>
#include <vapours/results/fs_results.hpp>
namespace ams::nxboot {
namespace {
constinit BootStorage g_boot_storage = BootDrive_Invalid;
Result IsBootStorageValid() {
fs::DirectoryEntryType entry_type;
bool is_archive;
Result r;
if(R_SUCCEEDED(r = fs::GetEntryType(std::addressof(entry_type), std::addressof(is_archive), ".no_boot_storage"))) {
if(entry_type == fs::DirectoryEntryType_File) {
R_THROW(fs::ResultInvalidMountName());
}
}
R_SUCCEED();
}
Result BootStorageInitializeAndMount(BootStorage storage) {
AMS_ASSERT(g_boot_storage == BootDrive_Invalid);
switch(storage) {
case BootDrive_Boot1Off:
{
R_TRY(InitializeMmc());
ON_RESULT_FAILURE { FinalizeMmc(); };
R_UNLESS(fs::MountBoot1Off(), fs::ResultInvalidMountName());
ON_RESULT_FAILURE_2 { fs::UnmountBoot1Off(); };
R_TRY(fs::ChangeDrive("boot1_off:"));
R_TRY(IsBootStorageValid());
g_boot_storage = storage;
R_SUCCEED();
} break;
case BootDrive_Boot1:
{
R_TRY(InitializeMmc());
ON_RESULT_FAILURE { FinalizeMmc(); };
R_UNLESS(fs::MountBoot1(), fs::ResultInvalidMountName());
ON_RESULT_FAILURE_2 { fs::UnmountBoot1(); };
R_TRY(fs::ChangeDrive("boot1:"));
R_TRY(IsBootStorageValid());
g_boot_storage = storage;
R_SUCCEED();
} break;
case BootDrive_System:
{
R_TRY(InitializeMmc());
ON_RESULT_FAILURE { FinalizeMmc(); };
R_UNLESS(fs::MountSys(), fs::ResultInvalidMountName());
ON_RESULT_FAILURE_2 { fs::UnmountSys(); };
R_TRY(fs::ChangeDrive("sys:"));
R_TRY(IsBootStorageValid());
g_boot_storage = storage;
R_SUCCEED();
} break;
case BootDrive_SD:
{
R_TRY(InitializeSdCard());
ON_RESULT_FAILURE { FinalizeSdCard(); };
R_UNLESS(fs::MountSdCard(), fs::ResultInvalidMountName());
ON_RESULT_FAILURE_2 { fs::UnmountSdCard(); };
R_TRY(fs::ChangeDrive("sdmc:"));
R_TRY(IsBootStorageValid());
g_boot_storage = storage;
R_SUCCEED();
} break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
R_THROW(fs::ResultInvalidMountName());
}
}
Result MountBootStorage() {
for(uint32_t i = BootDrive_Min; i < BootDrive_Max; i++) {
R_SUCCEED_IF(R_SUCCEEDED(BootStorageInitializeAndMount(static_cast<BootStorage>(i))));
}
R_THROW(fs::ResultInvalidMountName());
}
void FinalizeBootStorage() {
AMS_ASSERT(g_boot_storage != BootDrive_Invalid);
switch(g_boot_storage) {
case BootDrive_Boot1Off:
fs::UnmountBoot1Off();
FinalizeMmc();
break;
case BootDrive_Boot1:
fs::UnmountBoot1();
FinalizeMmc();
break;
case BootDrive_System:
fs::UnmountSys();
FinalizeMmc();
break;
case BootDrive_SD:
fs::UnmountSdCard();
FinalizeSdCard();
break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
void UnmountBootStorage() {
AMS_ASSERT(g_boot_storage != BootDrive_Invalid);
switch(g_boot_storage) {
case BootDrive_Boot1Off:
fs::UnmountBoot1Off();
break;
case BootDrive_Boot1:
fs::UnmountBoot1();
break;
case BootDrive_System:
fs::UnmountSys();
break;
case BootDrive_SD:
fs::UnmountSdCard();
break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
BootStorage GetBootStorage(){
return g_boot_storage;
}
}

View File

@@ -0,0 +1,19 @@
#include <exosphere.hpp>
namespace ams::nxboot {
enum BootStorage : u32 {
BootDrive_Min = 0,
BootDrive_Boot1Off = 0,
BootDrive_Boot1,
BootDrive_System,
BootDrive_SD,
BootDrive_Max,
BootDrive_Invalid = BootDrive_Max,
};
Result MountBootStorage();
void FinalizeBootStorage();
void UnmountBootStorage();
BootStorage GetBootStorage();
}

View File

@@ -30,7 +30,7 @@ namespace ams::nxboot {
{
/* Generate the file path. */
char path[0x40];
util::TSNPrintf(path, sizeof(path), "sdmc:/atmosphere/fatal_errors/report_%016" PRIx64 ".bin", ctx->report_identifier);
util::TSNPrintf(path, sizeof(path), "atmosphere/fatal_errors/report_%016" PRIx64 ".bin", ctx->report_identifier);
/* Create the file. */
R_TRY(fs::CreateFile(path, sizeof(*ctx)));

View File

@@ -14,6 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <exosphere.hpp>
#include "fusee_boot_storage.hpp"
#include "fusee_display.hpp"
#include "sein/fusee_secure_initialize.hpp"
#include "sdram/fusee_sdram.hpp"
@@ -30,7 +31,7 @@ namespace ams::nxboot {
namespace {
constexpr const char ExternalPackageFilePath[] = "sdmc:/atmosphere/package3";
constexpr const char ExternalPackageFilePath[] = "atmosphere/package3";
constinit fs::FileHandle g_package_file;
@@ -81,17 +82,9 @@ namespace ams::nxboot {
/* Initialize cache. */
hw::InitializeDataCache();
/* Initialize SD card. */
{
Result result = InitializeSdCard();
if (R_FAILED(result)) {
ShowFatalError("Failed to initialize the SD card: 0x%08" PRIx32 "\n", result.GetValue());
}
}
/* Mount SD card. */
if (!fs::MountSdCard()) {
ShowFatalError("Failed to mount the SD card.");
/* Initialize and mount boot storage. */
if (R_FAILED(MountBootStorage())) {
ShowFatalError("Failed to mount boot storage.");
}
/* If we have a fatal error, save and display it. */
@@ -135,6 +128,7 @@ namespace ams::nxboot {
FinalizeDisplay();
/* Finalize sd card. */
UnmountBootStorage();
FinalizeSdCard();
/* Finalize the data cache. */

View File

@@ -39,6 +39,14 @@ namespace ams::nxboot {
}
void FinalizeMmc() {
/* Deactivate MMC. */
sdmmc::Deactivate(MmcPort);
/* Finalize MMC. */
sdmmc::Finalize(MmcPort);
}
Result InitializeMmc() {
/* Initialize the mmc. */
sdmmc::Initialize(MmcPort);

View File

@@ -19,6 +19,7 @@
namespace ams::nxboot {
Result InitializeMmc();
void FinalizeMmc();
Result CheckMmcConnection(sdmmc::SpeedMode *out_sm, sdmmc::BusWidth *out_bw);
Result GetMmcMemoryCapacity(u32 *out_num_sectors, sdmmc::MmcPartition partition);

View File

@@ -143,7 +143,7 @@ namespace ams::nxboot {
{
IniSectionList sections;
if (ParseIniSafe(sections, "sdmc:/emummc/emummc.ini")) {
if (ParseIniSafe(sections, "emummc/emummc.ini")) {
for (const auto &section : sections){
/* Skip non-emummc sections */
if (std::strcmp(section.name, "emummc")) {
@@ -215,7 +215,7 @@ namespace ams::nxboot {
{
IniSectionList sections;
if (ParseIniSafe(sections, "sdmc:/emusd/emusd.ini")) {
if (ParseIniSafe(sections, "emusd/emusd.ini")) {
for (const auto &section : sections){
/* Skip non-emummc sections */
if (std::strcmp(section.name, "emusd")) {
@@ -431,7 +431,7 @@ namespace ams::nxboot {
std::memcpy(warmboot_dst + 0x248, std::addressof(target_firmware), sizeof(target_firmware));
} else /* if (soc_type == fuse::SocType_Mariko) */ {
/* Declare path for mariko warmboot files. */
char warmboot_path[0x80] = "sdmc:/warmboot_mariko/wb_xx.bin";
char warmboot_path[0x80] = "warmboot_mariko/wb_xx.bin";
auto UpdateWarmbootPath = [&warmboot_path](u8 fuses) {
warmboot_path[0x19] = "0123456789abcdef"[(fuses >> 4) & 0xF];
@@ -477,7 +477,7 @@ namespace ams::nxboot {
/* If we should, save the current warmboot firmware. */
UpdateWarmbootPath(expected_fuses);
if (!IsFileExist(warmboot_path)) {
fs::CreateDirectory("sdmc:/warmboot_mariko");
fs::CreateDirectory("warmboot_mariko");
fs::CreateFile(warmboot_path, warmboot_src_size);
Result result;
@@ -576,7 +576,7 @@ namespace ams::nxboot {
/* Parse fields from exosphere.ini */
{
IniSectionList sections;
if (ParseIniSafe(sections, "sdmc:/exosphere.ini")) {
if (ParseIniSafe(sections, "exosphere.ini")) {
for (const auto &section : sections) {
/* We only care about the [exosphere] section. */
if (std::strcmp(section.name, "exosphere")) {
@@ -651,7 +651,7 @@ namespace ams::nxboot {
/* Parse usb setting from system_settings.ini */
{
IniSectionList sections;
if (ParseIniSafe(sections, "sdmc:/atmosphere/config/system_settings.ini")) {
if (ParseIniSafe(sections, "atmosphere/config/system_settings.ini")) {
for (const auto &section : sections) {
/* We only care about the [usb] section. */
if (std::strcmp(section.name, "usb")) {
@@ -676,7 +676,7 @@ namespace ams::nxboot {
{
/* Try to use an sd card file, if present. */
fs::FileHandle exo_file;
if (R_SUCCEEDED(fs::OpenFile(std::addressof(exo_file), "sdmc:/atmosphere/exosphere.bin", fs::OpenMode_Read))) {
if (R_SUCCEEDED(fs::OpenFile(std::addressof(exo_file), "atmosphere/exosphere.bin", fs::OpenMode_Read))) {
ON_SCOPE_EXIT { fs::CloseFile(exo_file); };
/* Note that we're using sd_exo. */
@@ -708,7 +708,7 @@ namespace ams::nxboot {
{
/* Try to use an sd card file, if present. */
fs::FileHandle mariko_program_file;
if (R_SUCCEEDED(fs::OpenFile(std::addressof(mariko_program_file), "sdmc:/atmosphere/mariko_fatal.bin", fs::OpenMode_Read))) {
if (R_SUCCEEDED(fs::OpenFile(std::addressof(mariko_program_file), "atmosphere/mariko_fatal.bin", fs::OpenMode_Read))) {
ON_SCOPE_EXIT { fs::CloseFile(mariko_program_file); };
/* Note that we're using sd mariko fatal. */
@@ -754,7 +754,7 @@ namespace ams::nxboot {
/* First parse from ini. */
{
IniSectionList sections;
if (ParseIniSafe(sections, "sdmc:/atmosphere/config/stratosphere.ini")) {
if (ParseIniSafe(sections, "atmosphere/config/stratosphere.ini")) {
for (const auto &section : sections) {
/* We only care about the [stratosphere] section. */
if (std::strcmp(section.name, "stratosphere")) {

View File

@@ -769,7 +769,7 @@ namespace ams::nxboot {
{
/* Create kip dir path. */
char kip_path[0x120];
std::memcpy(kip_path, "sdmc:/atmosphere/kips", 0x16);
std::memcpy(kip_path, "atmosphere/kips", 0x16);
fs::DirectoryHandle kip_dir;
if (R_SUCCEEDED(fs::OpenDirectory(std::addressof(kip_dir), kip_path))) {
@@ -899,7 +899,7 @@ namespace ams::nxboot {
/* Load mesosphere. */
s64 meso_size;
if (void *sd_meso = ReadFile(std::addressof(meso_size), "sdmc:/atmosphere/mesosphere.bin"); sd_meso != nullptr) {
if (void *sd_meso = ReadFile(std::addressof(meso_size), "atmosphere/mesosphere.bin"); sd_meso != nullptr) {
std::memcpy(payload_data, sd_meso, meso_size);
} else {
meso_size = external_package.header.meso_size;
@@ -910,7 +910,7 @@ namespace ams::nxboot {
const InitialProcessHeader *emummc;
s64 emummc_size;
if (emummc_driver_enabled) {
emummc = static_cast<const InitialProcessHeader *>(ReadFile(std::addressof(emummc_size), "sdmc:/atmosphere/emummc.kip"));
emummc = static_cast<const InitialProcessHeader *>(ReadFile(std::addressof(emummc_size), "atmosphere/emummc.kip"));
if (emummc == nullptr) {
emummc = reinterpret_cast<const InitialProcessHeader *>(external_package.kips + external_package.header.emummc_meta.offset);
emummc_size = external_package.header.emummc_meta.size;