fs: add MountSdCard

This commit is contained in:
Michael Scire 2020-03-04 00:56:05 -08:00
parent 6514fb75a8
commit e98c1dbf41
6 changed files with 80 additions and 16 deletions

View file

@ -29,3 +29,4 @@
#include "fs/fs_mount.hpp"
#include "fs/fs_path_tool.hpp"
#include "fs/fs_path_utils.hpp"
#include "fs/fs_sd_card.hpp"

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "fs_common.hpp"
namespace ams::fs {
Result MountSdCard(const char *name);
}

View file

@ -191,19 +191,20 @@ namespace ams::boot2 {
template<typename F>
void IterateOverFlaggedProgramsOnSdCard(F f) {
/* Validate that the contents directory exists. */
DIR *contents_dir = opendir("sdmc:/atmosphere/contents");
if (contents_dir == nullptr) {
fs::DirectoryHandle contents_dir;
if (R_FAILED(fs::OpenDirectory(&contents_dir, "sdmc:/atmosphere/contents", fs::OpenDirectoryMode_Directory))) {
return;
}
ON_SCOPE_EXIT { closedir(contents_dir); };
ON_SCOPE_EXIT { fs::CloseDirectory(contents_dir); };
/* Iterate over entries in the contents directory */
struct dirent *ent;
while ((ent = readdir(contents_dir)) != nullptr) {
fs::DirectoryEntry entry;
s64 count;
while (R_SUCCEEDED(fs::ReadDirectory(&count, &entry, contents_dir, 1)) && count == 1) {
/* Check that the subdirectory can be converted to a program id. */
if (std::strlen(ent->d_name) == 2 * sizeof(ncm::ProgramId) && IsHexadecimal(ent->d_name)) {
if (std::strlen(entry.name) == 2 * sizeof(ncm::ProgramId) && IsHexadecimal(entry.name)) {
/* Check if we've already launched the program. */
ncm::ProgramId program_id{std::strtoul(ent->d_name, nullptr, 16)};
ncm::ProgramId program_id{std::strtoul(entry.name, nullptr, 16)};
/* Check if the program is flagged. */
if (!cfg::HasContentSpecificFlag(program_id, "boot2")) {
@ -224,14 +225,16 @@ namespace ams::boot2 {
/* Read the mitm list off the SD card. */
{
char path[FS_MAX_PATH];
std::snprintf(mitm_list, sizeof(mitm_list), "sdmc:/atmosphere/contents/%016lx/mitm.lst", static_cast<u64>(program_id));
FILE *f = fopen(path, "rb");
if (f == nullptr) {
char path[fs::EntryNameLengthMax];
std::snprintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/mitm.lst", static_cast<u64>(program_id));
fs::FileHandle f;
if (R_FAILED(fs::OpenFile(&f, path, fs::OpenMode_Read))) {
return;
}
mitm_list_size = static_cast<size_t>(fread(mitm_list, 1, sizeof(mitm_list), f));
fclose(f);
ON_SCOPE_EXIT { fs::CloseFile(f); };
R_ABORT_UNLESS(fs::ReadFile(&mitm_list_size, f, 0, mitm_list, sizeof(mitm_list)));
}
/* Validate read size. */

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::fs {
Result MountSdCard(const char *name) {
/* Open the SD card. This uses libnx bindings. */
FsFileSystem fs;
R_TRY(fsOpenSdCardFileSystem(std::addressof(fs)));
/* Allocate a new filesystem wrapper. */
std::unique_ptr<fsa::IFileSystem> fsa(new RemoteFileSystem(fs));
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInSdCardA());
/* Register. */
return fsa::Register(name, std::move(fsa));
}
}

View file

@ -50,6 +50,8 @@ namespace ams::fs {
R_DEFINE_ERROR_RANGE(AllocationFailure, 3200, 3499);
R_DEFINE_ERROR_RESULT(AllocationFailureInFileSystemAccessorA, 3211);
R_DEFINE_ERROR_RESULT(AllocationFailureInFileSystemAccessorB, 3212);
R_DEFINE_ERROR_RESULT(AllocationFailureInSdCardA, 3244);
R_DEFINE_ERROR_RESULT(AllocationFailureInSdCardB, 3245);
R_DEFINE_ERROR_RESULT(AllocationFailureInDirectorySaveDataFileSystem, 3321);
R_DEFINE_ERROR_RESULT(AllocationFailureInSubDirectoryFileSystem, 3355);
R_DEFINE_ERROR_RESULT(AllocationFailureInRegisterA, 3365);

View file

@ -79,13 +79,10 @@ void __appInit(void) {
R_ABORT_UNLESS(gpioInitialize());
});
R_ABORT_UNLESS(fsdevMountSdmc());
ams::CheckApiVersion();
}
void __appExit(void) {
fsdevUnmountAll();
gpioExit();
setsysExit();
pmshellExit();
@ -96,6 +93,11 @@ void __appExit(void) {
int main(int argc, char **argv)
{
/* Mount the SD card. */
R_ABORT_UNLESS(fs::MountSdCard("sdmc"));
ON_SCOPE_EXIT { fs::Unmount("sdmc"); };
/* Launch all programs off of SYSTEM/the SD. */
boot2::LaunchPostSdCardBootPrograms();
}