stratosphere-except-ldr: use fs bindings (this temporarily breaks loader)

This commit is contained in:
Michael Scire 2020-03-08 16:33:49 -07:00
parent 4eb3109c93
commit 6eee3f5fe7
17 changed files with 283 additions and 187 deletions

View file

@ -20,7 +20,13 @@ namespace ams::cfg {
namespace {
std::atomic<u32> g_flag_mount_count;
/* Helper. */
void GetFlagMountName(char *dst) {
std::snprintf(dst, fs::MountNameLengthMax + 1, "#flag%08x", g_flag_mount_count.fetch_add(1));
}
bool HasFlagFile(const char *flag_path) {
/* All flags are not present until the SD card is. */
if (!IsSdCardInitialized()) {
@ -28,20 +34,23 @@ namespace ams::cfg {
}
/* Mount the SD card. */
FsFileSystem sd_fs = {};
if (R_FAILED(fsOpenSdCardFileSystem(&sd_fs))) {
char mount_name[fs::MountNameLengthMax + 1];
GetFlagMountName(mount_name);
if (R_FAILED(fs::MountSdCard(mount_name))) {
return false;
}
ON_SCOPE_EXIT { serviceClose(&sd_fs.s); };
ON_SCOPE_EXIT { fs::Unmount(mount_name); };
/* Open the file. */
FsFile flag_file;
if (R_FAILED(fsFsOpenFile(&sd_fs, flag_path, FsOpenMode_Read, &flag_file))) {
/* Check if the entry exists. */
char full_path[fs::EntryNameLengthMax + 1];
std::snprintf(full_path, sizeof(full_path), "%s:/%s", mount_name, flag_path[0] == '/' ? flag_path + 1 : flag_path);
bool has_file;
if (R_FAILED(fs::HasFile(std::addressof(has_file), full_path))) {
return false;
}
fsFileClose(&flag_file);
return true;
return has_file;
}
}
@ -52,13 +61,13 @@ namespace ams::cfg {
}
bool HasContentSpecificFlag(ncm::ProgramId program_id, const char *flag) {
char content_flag[FS_MAX_PATH];
char content_flag[fs::EntryNameLengthMax + 1];
std::snprintf(content_flag, sizeof(content_flag) - 1, "/atmosphere/contents/%016lx/flags/%s.flag", static_cast<u64>(program_id), flag);
return HasFlagFile(content_flag);
}
bool HasGlobalFlag(const char *flag) {
char global_flag[FS_MAX_PATH];
char global_flag[fs::EntryNameLengthMax + 1];
std::snprintf(global_flag, sizeof(global_flag) - 1, "/atmosphere/flags/%s.flag", flag);
return HasFlagFile(global_flag);
}

View file

@ -272,23 +272,34 @@ namespace ams::cfg {
return g_hbl_override_config.override_any_app && ncm::IsApplicationId(program_id) && !IsAnySpecificHblProgramId(program_id);
}
std::atomic<u32> g_ini_mount_count;
void GetIniMountName(char *dst) {
std::snprintf(dst, fs::MountNameLengthMax + 1, "#ini%08x", g_ini_mount_count.fetch_add(1));
}
void ParseIniFile(util::ini::Handler handler, const char *path, void *user_ctx) {
/* Mount the SD card. */
FsFileSystem sd_fs = {};
if (R_FAILED(fsOpenSdCardFileSystem(&sd_fs))) {
char mount_name[fs::MountNameLengthMax + 1];
GetIniMountName(mount_name);
if (R_FAILED(fs::MountSdCard(mount_name))) {
return;
}
ON_SCOPE_EXIT { serviceClose(&sd_fs.s); };
ON_SCOPE_EXIT { fs::Unmount(mount_name); };
/* Open the file. */
FsFile config_file;
if (R_FAILED(fsFsOpenFile(&sd_fs, path, FsOpenMode_Read, &config_file))) {
return;
fs::FileHandle file;
{
char full_path[fs::EntryNameLengthMax + 1];
std::snprintf(full_path, sizeof(full_path), "%s:/%s", mount_name, path[0] == '/' ? path + 1 : path);
if (R_FAILED(fs::OpenFile(std::addressof(file), full_path, fs::OpenMode_Read))) {
return;
}
}
ON_SCOPE_EXIT { fsFileClose(&config_file); };
ON_SCOPE_EXIT { fs::CloseFile(file); };
/* Parse the config. */
util::ini::ParseFile(&config_file, user_ctx, handler);
util::ini::ParseFile(file, user_ctx, handler);
}
void RefreshOverrideConfiguration() {
@ -296,8 +307,8 @@ namespace ams::cfg {
}
ContentSpecificOverrideConfig GetContentOverrideConfig(ncm::ProgramId program_id) {
char path[FS_MAX_PATH];
std::snprintf(path, sizeof(path) - 1, "/atmosphere/contents/%016lx/config.ini", static_cast<u64>(program_id));
char path[fs::EntryNameLengthMax + 1];
std::snprintf(path, sizeof(path), "/atmosphere/contents/%016lx/config.ini", static_cast<u64>(program_id));
ContentSpecificOverrideConfig config = {
.override_key = g_default_override_key,

View file

@ -179,7 +179,7 @@ namespace ams::fs::impl {
}
if (this->path_cache_attached) {
if (mode & OpenMode_Append) {
if (mode & OpenMode_AllowAppend) {
/* TODO: Append Path cache */
} else {
/* TODO: Non-append path cache */

View file

@ -13,7 +13,6 @@
* 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 <dirent.h>
#include <stratosphere.hpp>
/* IPS Patching adapted from Luma3DS (https://github.com/AuroraWright/Luma3DS/blob/master/sysmodules/loader/source/patcher.c) */
@ -31,6 +30,10 @@ namespace ams::patcher {
constexpr size_t IpsFileExtensionLength = std::strlen(IpsFileExtension);
constexpr size_t ModuleIpsPatchLength = 2 * sizeof(ro::ModuleId) + IpsFileExtensionLength;
/* Global data. */
os::Mutex apply_patch_lock;
u8 g_patch_read_buffer[os::MemoryPageSize];
/* Helpers. */
inline u8 ConvertHexNybble(const char nybble) {
if ('0' <= nybble && nybble <= '9') {
@ -45,7 +48,7 @@ namespace ams::patcher {
bool ParseModuleIdFromPath(ro::ModuleId *out_module_id, const char *name, size_t name_len, size_t extension_len) {
/* Validate name is hex module id. */
for (unsigned int i = 0; i < name_len - extension_len; i++) {
if (std::isxdigit(name[i]) == 0) {
if (!std::isxdigit(static_cast<unsigned char>(name[i]))) {
return false;
}
}
@ -70,6 +73,28 @@ namespace ams::patcher {
return std::memcmp(&module_id_from_name, module_id, sizeof(*module_id)) == 0;
}
bool IsIpsFileForModule(const char *name, const ro::ModuleId *module_id) {
const size_t name_len = std::strlen(name);
/* The path must be correct size for a build id (with trailing zeroes optionally trimmed) + ".ips". */
if (!(IpsFileExtensionLength < name_len && name_len <= ModuleIpsPatchLength)) {
return false;
}
/* The path must be an even number of characters to conform. */
if (!util::IsAligned(name_len, 2)) {
return false;
}
/* The path needs to end with .ips. */
if (std::strcmp(name + name_len - IpsFileExtensionLength, IpsFileExtension) != 0) {
return false;
}
/* The path needs to match the module id. */
return MatchesModuleId(name, name_len, IpsFileExtensionLength, module_id);
}
inline bool IsIpsTail(bool is_ips32, u8 *buffer) {
if (is_ips32) {
return std::memcmp(buffer, Ips32TailMagic, sizeof(Ips32TailMagic)) == 0;
@ -90,13 +115,19 @@ namespace ams::patcher {
return (buffer[0] << 8) | (buffer[1]);
}
void ApplyIpsPatch(u8 *mapped_module, size_t mapped_size, size_t protected_size, size_t offset, bool is_ips32, FILE *f_ips) {
void ApplyIpsPatch(u8 *mapped_module, size_t mapped_size, size_t protected_size, size_t offset, bool is_ips32, fs::FileHandle file) {
/* Validate offset/protected size. */
AMS_ABORT_UNLESS(offset <= protected_size);
s64 file_offset = sizeof(IpsHeadMagic);
auto ReadData = [&](void *dst, size_t size) ALWAYS_INLINE_LAMBDA {
R_ABORT_UNLESS(fs::ReadFile(file, file_offset, dst, size));
file_offset += size;
};
u8 buffer[sizeof(Ips32TailMagic)];
while (true) {
AMS_ABORT_UNLESS(fread(buffer, is_ips32 ? sizeof(Ips32TailMagic) : sizeof(IpsTailMagic), 1, f_ips) == 1);
ReadData(buffer, is_ips32 ? sizeof(Ips32TailMagic) : sizeof(IpsTailMagic));
if (IsIpsTail(is_ips32, buffer)) {
break;
@ -106,18 +137,18 @@ namespace ams::patcher {
u32 patch_offset = GetIpsPatchOffset(is_ips32, buffer);
/* Size of patch. */
AMS_ABORT_UNLESS(fread(buffer, 2, 1, f_ips) == 1);
ReadData(buffer, 2);
u32 patch_size = GetIpsPatchSize(is_ips32, buffer);
/* Check for RLE encoding. */
if (patch_size == 0) {
/* Size of RLE. */
AMS_ABORT_UNLESS(fread(buffer, 2, 1, f_ips) == 1);
ReadData(buffer, 2);
u32 rle_size = (buffer[0] << 8) | (buffer[1]);
/* Value for RLE. */
AMS_ABORT_UNLESS(fread(buffer, 1, 1, f_ips) == 1);
ReadData(buffer, 1);
/* Ensure we don't write to protected region. */
if (patch_offset < protected_size) {
@ -145,9 +176,9 @@ namespace ams::patcher {
const u32 diff = protected_size - patch_offset;
patch_offset += diff;
patch_size -= diff;
fseek(f_ips, diff, SEEK_CUR);
file_offset += diff;
} else {
fseek(f_ips, patch_size, SEEK_CUR);
file_offset += patch_size;
continue;
}
}
@ -160,9 +191,19 @@ namespace ams::patcher {
if (patch_offset + read_size > mapped_size) {
read_size = mapped_size - patch_offset;
}
AMS_ABORT_UNLESS(fread(mapped_module + patch_offset, read_size, 1, f_ips) == 1);
{
size_t remaining = read_size;
size_t copy_offset = 0;
while (remaining > 0) {
const size_t cur_read = std::min(remaining, sizeof(g_patch_read_buffer));
ReadData(g_patch_read_buffer, cur_read);
std::memcpy(mapped_module + copy_offset, g_patch_read_buffer, cur_read);
remaining -= cur_read;
copy_offset += cur_read;
}
}
if (patch_size > read_size) {
fseek(f_ips, patch_size - read_size, SEEK_CUR);
file_offset += patch_size - read_size;
}
}
}
@ -171,64 +212,72 @@ namespace ams::patcher {
}
void LocateAndApplyIpsPatchesToModule(const char *patch_dir_name, size_t protected_size, size_t offset, const ro::ModuleId *module_id, u8 *mapped_module, size_t mapped_size) {
/* Inspect all patches from /atmosphere/<patch_dir>/<*>/<*>.ips */
char path[FS_MAX_PATH+1] = {0};
std::snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/%s", patch_dir_name);
/* Ensure only one thread tries to apply patches at a time. */
std::scoped_lock lk(apply_patch_lock);
DIR *patches_dir = opendir(path);
struct dirent *pdir_ent;
if (patches_dir != NULL) {
/* Iterate over the patches directory to find patch subdirectories. */
while ((pdir_ent = readdir(patches_dir)) != NULL) {
if (std::strcmp(pdir_ent->d_name, ".") == 0 || std::strcmp(pdir_ent->d_name, "..") == 0) {
/* Inspect all patches from /atmosphere/<patch_dir>/<*>/<*>.ips */
char path[fs::EntryNameLengthMax + 1];
std::snprintf(path, sizeof(path), "sdmc:/atmosphere/%s", patch_dir_name);
const size_t patches_dir_path_len = std::strlen(path);
/* Open the patch directory. */
fs::DirectoryHandle patches_dir;
if (R_FAILED(fs::OpenDirectory(std::addressof(patches_dir), path, fs::OpenDirectoryMode_Directory))) {
return;
}
ON_SCOPE_EXIT { fs::CloseDirectory(patches_dir); };
/* Iterate over the patches directory to find patch subdirectories. */
while (true) {
/* Read the next entry. */
s64 count;
fs::DirectoryEntry entry;
if (R_FAILED(fs::ReadDirectory(std::addressof(count), std::addressof(entry), patches_dir, 1)) || count == 0) {
break;
}
/* Print the path for this directory. */
std::snprintf(path + patches_dir_path_len, sizeof(path) - patches_dir_path_len, "/%s", entry.name);
const size_t patch_dir_path_len = patches_dir_path_len + 1 + std::strlen(entry.name);
/* Open the patch directory. */
fs::DirectoryHandle patch_dir;
if (R_FAILED(fs::OpenDirectory(std::addressof(patch_dir), path, fs::OpenDirectoryMode_File))) {
continue;
}
ON_SCOPE_EXIT { fs::CloseDirectory(patch_dir); };
/* Iterate over files in the patch directory. */
while (true) {
if (R_FAILED(fs::ReadDirectory(std::addressof(count), std::addressof(entry), patch_dir, 1)) || count == 0) {
break;
}
/* Check if this file is an ips. */
if (!IsIpsFileForModule(entry.name, module_id)) {
continue;
}
std::snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/%s/%s", patch_dir_name, pdir_ent->d_name);
DIR *patch_dir = opendir(path);
struct dirent *ent;
if (patch_dir != NULL) {
/* Iterate over the patch subdirectory to find .ips patches. */
while ((ent = readdir(patch_dir)) != NULL) {
if (std::strcmp(ent->d_name, ".") == 0 || std::strcmp(ent->d_name, "..") == 0) {
continue;
}
/* Print the path for this file. */
std::snprintf(path + patch_dir_path_len, sizeof(path) - patch_dir_path_len, "/%s", entry.name);
size_t name_len = strlen(ent->d_name);
if (!(IpsFileExtensionLength < name_len && name_len <= ModuleIpsPatchLength)) {
continue;
}
if ((name_len & 1) != 0) {
continue;
}
if (std::strcmp(ent->d_name + name_len - IpsFileExtensionLength, IpsFileExtension) != 0) {
continue;
}
if (!MatchesModuleId(ent->d_name, name_len, IpsFileExtensionLength, module_id)) {
continue;
}
/* Open the file. */
fs::FileHandle file;
if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) {
continue;
}
ON_SCOPE_EXIT { fs::CloseFile(file); };
std::snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/%s/%s/%s", patch_dir_name, pdir_ent->d_name, ent->d_name);
FILE *f_ips = fopen(path, "rb");
if (f_ips == NULL) {
continue;
}
ON_SCOPE_EXIT { fclose(f_ips); };
u8 header[5];
if (fread(header, 5, 1, f_ips) == 1) {
if (std::memcmp(header, IpsHeadMagic, 5) == 0) {
ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, false, f_ips);
} else if (std::memcmp(header, Ips32HeadMagic, 5) == 0) {
ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, true, f_ips);
}
}
fclose(f_ips);
/* Read the header. */
u8 header[sizeof(IpsHeadMagic)];
if (R_SUCCEEDED(fs::ReadFile(file, 0, header, sizeof(header)))) {
if (std::memcmp(header, IpsHeadMagic, sizeof(header)) == 0) {
ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, false, file);
} else if (std::memcmp(header, Ips32HeadMagic, sizeof(header)) == 0) {
ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, true, file);
}
closedir(patch_dir);
}
}
closedir(patches_dir);
}
}

View file

@ -24,34 +24,30 @@ namespace ams::util::ini {
namespace {
struct FsFileContext {
FsFile *f;
size_t offset;
size_t num_left;
struct FileContext {
fs::FileHandle file;
s64 offset;
s64 num_left;
explicit FsFileContext(FsFile *f) : f(f), offset(0) {
s64 size;
R_ABORT_UNLESS(fsFileGetSize(this->f, &size));
this->num_left = size_t(size);
explicit FileContext(fs::FileHandle f) : file(f), offset(0) {
R_ABORT_UNLESS(fs::GetFileSize(std::addressof(this->num_left), this->file));
}
};
char *ini_reader_fs_file(char *str, int num, void *stream) {
FsFileContext *ctx = reinterpret_cast<FsFileContext *>(stream);
char *ini_reader_file_handle(char *str, int num, void *stream) {
FileContext *ctx = static_cast<FileContext *>(stream);
if (ctx->num_left == 0 || num < 2) {
return nullptr;
}
/* Read as many bytes as we can. */
size_t try_read = std::min(size_t(num - 1), ctx->num_left);
size_t actually_read;
R_ABORT_UNLESS(fsFileRead(ctx->f, ctx->offset, str, try_read, FsReadOption_None, &actually_read));
AMS_ABORT_UNLESS(actually_read == try_read);
s64 cur_read = std::min<s64>(num - 1, ctx->num_left);
R_ABORT_UNLESS(fs::ReadFile(ctx->file, ctx->offset, str, cur_read, fs::ReadOption()));
/* Only "read" up to the first \n. */
size_t offset = actually_read;
for (size_t i = 0; i < actually_read; i++) {
size_t offset = cur_read;
for (auto i = 0; i < cur_read; i++) {
if (str[i] == '\n') {
offset = i + 1;
break;
@ -62,7 +58,7 @@ namespace ams::util::ini {
str[offset] = '\0';
/* Update context. */
ctx->offset += offset;
ctx->offset += offset;
ctx->num_left -= offset;
return str;
@ -75,17 +71,19 @@ namespace ams::util::ini {
return ini_parse_string(ini_str, h, user_ctx);
}
int ParseFile(FILE *f, void *user_ctx, Handler h) {
return ini_parse_file(f, h, user_ctx);
}
int ParseFile(FsFile *f, void *user_ctx, Handler h) {
FsFileContext ctx(f);
return ini_parse_stream(ini_reader_fs_file, &ctx, h, user_ctx);
int ParseFile(fs::FileHandle file, void *user_ctx, Handler h) {
FileContext ctx(file);
return ini_parse_stream(ini_reader_file_handle, &ctx, h, user_ctx);
}
int ParseFile(const char *path, void *user_ctx, Handler h) {
return ini_parse(path, h, user_ctx);
fs::FileHandle file;
if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) {
return -1;
}
ON_SCOPE_EXIT { fs::CloseFile(file); };
return ParseFile(file, user_ctx, h);
}
}