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

@ -25,12 +25,12 @@ namespace ams::fs {
} }
enum OpenMode { enum OpenMode {
OpenMode_Read = ::FsOpenMode_Read, OpenMode_Read = (1 << 0),
OpenMode_Write = ::FsOpenMode_Write, OpenMode_Write = (1 << 1),
OpenMode_Append = ::FsOpenMode_Append, OpenMode_AllowAppend = (1 << 2),
OpenMode_ReadWrite = (OpenMode_Read | OpenMode_Write), OpenMode_ReadWrite = (OpenMode_Read | OpenMode_Write),
OpenMode_All = (OpenMode_ReadWrite | OpenMode_Append), OpenMode_All = (OpenMode_ReadWrite | OpenMode_AllowAppend),
}; };
enum OpenDirectoryMode { enum OpenDirectoryMode {

View file

@ -15,6 +15,7 @@
*/ */
#pragma once #pragma once
#include <stratosphere/os.hpp> #include <stratosphere/os.hpp>
#include <stratosphere/fs/fs_directory.hpp>
#include <stratosphere/kvdb/kvdb_bounded_string.hpp> #include <stratosphere/kvdb/kvdb_bounded_string.hpp>
namespace ams::kvdb { namespace ams::kvdb {
@ -23,12 +24,11 @@ namespace ams::kvdb {
NON_COPYABLE(FileKeyValueStore); NON_COPYABLE(FileKeyValueStore);
NON_MOVEABLE(FileKeyValueStore); NON_MOVEABLE(FileKeyValueStore);
public: public:
static constexpr size_t MaxPathLength = 0x300; /* TODO: FS_MAX_PATH - 1? */
static constexpr size_t MaxFileLength = 0xFF; static constexpr size_t MaxFileLength = 0xFF;
static constexpr char FileExtension[5] = ".val"; static constexpr char FileExtension[5] = ".val";
static constexpr size_t FileExtensionLength = sizeof(FileExtension) - 1; static constexpr size_t FileExtensionLength = sizeof(FileExtension) - 1;
static constexpr size_t MaxKeySize = (MaxFileLength - FileExtensionLength) / 2; static constexpr size_t MaxKeySize = (MaxFileLength - FileExtensionLength) / 2;
using Path = kvdb::BoundedString<MaxPathLength>; using Path = kvdb::BoundedString<fs::EntryNameLengthMax>;
using FileName = kvdb::BoundedString<MaxFileLength>; using FileName = kvdb::BoundedString<MaxFileLength>;
private: private:
/* Subtypes. */ /* Subtypes. */

View file

@ -15,8 +15,9 @@
*/ */
#pragma once #pragma once
#include <stratosphere/fs/fs_filesystem.hpp>
#include <stratosphere/fs/fs_file.hpp> #include <stratosphere/fs/fs_file.hpp>
#include <stratosphere/fs/fs_directory.hpp>
#include <stratosphere/fs/fs_filesystem.hpp>
#include <stratosphere/kvdb/kvdb_auto_buffer.hpp> #include <stratosphere/kvdb/kvdb_auto_buffer.hpp>
#include <stratosphere/kvdb/kvdb_archive.hpp> #include <stratosphere/kvdb/kvdb_archive.hpp>
#include <stratosphere/kvdb/kvdb_bounded_string.hpp> #include <stratosphere/kvdb/kvdb_bounded_string.hpp>
@ -252,8 +253,7 @@ namespace ams::kvdb {
} }
}; };
private: private:
static constexpr size_t MaxPathLen = 0x300; /* TODO: FS_MAX_PATH - 1? */ using Path = kvdb::BoundedString<fs::EntryNameLengthMax>;
using Path = kvdb::BoundedString<MaxPathLen>;
private: private:
Index index; Index index;
Path path; Path path;

View file

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <vapours.hpp> #include <vapours.hpp>
#include <stratosphere/fs/fs_file.hpp>
namespace ams::util::ini { namespace ams::util::ini {
@ -24,8 +25,7 @@ namespace ams::util::ini {
/* Utilities for dealing with INI file configuration. */ /* Utilities for dealing with INI file configuration. */
int ParseString(const char *ini_str, void *user_ctx, Handler h); int ParseString(const char *ini_str, void *user_ctx, Handler h);
int ParseFile(FILE *f, void *user_ctx, Handler h); int ParseFile(fs::FileHandle file, void *user_ctx, Handler h);
int ParseFile(FsFile *f, void *user_ctx, Handler h);
int ParseFile(const char *path, void *user_ctx, Handler h); int ParseFile(const char *path, void *user_ctx, Handler h);
} }

View file

@ -20,7 +20,13 @@ namespace ams::cfg {
namespace { namespace {
std::atomic<u32> g_flag_mount_count;
/* Helper. */ /* 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) { bool HasFlagFile(const char *flag_path) {
/* All flags are not present until the SD card is. */ /* All flags are not present until the SD card is. */
if (!IsSdCardInitialized()) { if (!IsSdCardInitialized()) {
@ -28,20 +34,23 @@ namespace ams::cfg {
} }
/* Mount the SD card. */ /* Mount the SD card. */
FsFileSystem sd_fs = {}; char mount_name[fs::MountNameLengthMax + 1];
if (R_FAILED(fsOpenSdCardFileSystem(&sd_fs))) { GetFlagMountName(mount_name);
if (R_FAILED(fs::MountSdCard(mount_name))) {
return false; return false;
} }
ON_SCOPE_EXIT { serviceClose(&sd_fs.s); }; ON_SCOPE_EXIT { fs::Unmount(mount_name); };
/* Open the file. */ /* Check if the entry exists. */
FsFile flag_file; char full_path[fs::EntryNameLengthMax + 1];
if (R_FAILED(fsFsOpenFile(&sd_fs, flag_path, FsOpenMode_Read, &flag_file))) { 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; 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) { 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); std::snprintf(content_flag, sizeof(content_flag) - 1, "/atmosphere/contents/%016lx/flags/%s.flag", static_cast<u64>(program_id), flag);
return HasFlagFile(content_flag); return HasFlagFile(content_flag);
} }
bool HasGlobalFlag(const char *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); std::snprintf(global_flag, sizeof(global_flag) - 1, "/atmosphere/flags/%s.flag", flag);
return HasFlagFile(global_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); 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) { void ParseIniFile(util::ini::Handler handler, const char *path, void *user_ctx) {
/* Mount the SD card. */ /* Mount the SD card. */
FsFileSystem sd_fs = {}; char mount_name[fs::MountNameLengthMax + 1];
if (R_FAILED(fsOpenSdCardFileSystem(&sd_fs))) { GetIniMountName(mount_name);
if (R_FAILED(fs::MountSdCard(mount_name))) {
return; return;
} }
ON_SCOPE_EXIT { serviceClose(&sd_fs.s); }; ON_SCOPE_EXIT { fs::Unmount(mount_name); };
/* Open the file. */ /* Open the file. */
FsFile config_file; fs::FileHandle file;
if (R_FAILED(fsFsOpenFile(&sd_fs, path, FsOpenMode_Read, &config_file))) { {
return; 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. */ /* Parse the config. */
util::ini::ParseFile(&config_file, user_ctx, handler); util::ini::ParseFile(file, user_ctx, handler);
} }
void RefreshOverrideConfiguration() { void RefreshOverrideConfiguration() {
@ -296,8 +307,8 @@ namespace ams::cfg {
} }
ContentSpecificOverrideConfig GetContentOverrideConfig(ncm::ProgramId program_id) { ContentSpecificOverrideConfig GetContentOverrideConfig(ncm::ProgramId program_id) {
char path[FS_MAX_PATH]; char path[fs::EntryNameLengthMax + 1];
std::snprintf(path, sizeof(path) - 1, "/atmosphere/contents/%016lx/config.ini", static_cast<u64>(program_id)); std::snprintf(path, sizeof(path), "/atmosphere/contents/%016lx/config.ini", static_cast<u64>(program_id));
ContentSpecificOverrideConfig config = { ContentSpecificOverrideConfig config = {
.override_key = g_default_override_key, .override_key = g_default_override_key,

View file

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

View file

@ -13,7 +13,6 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <dirent.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
/* IPS Patching adapted from Luma3DS (https://github.com/AuroraWright/Luma3DS/blob/master/sysmodules/loader/source/patcher.c) */ /* 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 IpsFileExtensionLength = std::strlen(IpsFileExtension);
constexpr size_t ModuleIpsPatchLength = 2 * sizeof(ro::ModuleId) + IpsFileExtensionLength; constexpr size_t ModuleIpsPatchLength = 2 * sizeof(ro::ModuleId) + IpsFileExtensionLength;
/* Global data. */
os::Mutex apply_patch_lock;
u8 g_patch_read_buffer[os::MemoryPageSize];
/* Helpers. */ /* Helpers. */
inline u8 ConvertHexNybble(const char nybble) { inline u8 ConvertHexNybble(const char nybble) {
if ('0' <= nybble && nybble <= '9') { 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) { bool ParseModuleIdFromPath(ro::ModuleId *out_module_id, const char *name, size_t name_len, size_t extension_len) {
/* Validate name is hex module id. */ /* Validate name is hex module id. */
for (unsigned int i = 0; i < name_len - extension_len; i++) { 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; return false;
} }
} }
@ -70,6 +73,28 @@ namespace ams::patcher {
return std::memcmp(&module_id_from_name, module_id, sizeof(*module_id)) == 0; 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) { inline bool IsIpsTail(bool is_ips32, u8 *buffer) {
if (is_ips32) { if (is_ips32) {
return std::memcmp(buffer, Ips32TailMagic, sizeof(Ips32TailMagic)) == 0; return std::memcmp(buffer, Ips32TailMagic, sizeof(Ips32TailMagic)) == 0;
@ -90,13 +115,19 @@ namespace ams::patcher {
return (buffer[0] << 8) | (buffer[1]); 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. */ /* Validate offset/protected size. */
AMS_ABORT_UNLESS(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)]; u8 buffer[sizeof(Ips32TailMagic)];
while (true) { 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)) { if (IsIpsTail(is_ips32, buffer)) {
break; break;
@ -106,18 +137,18 @@ namespace ams::patcher {
u32 patch_offset = GetIpsPatchOffset(is_ips32, buffer); u32 patch_offset = GetIpsPatchOffset(is_ips32, buffer);
/* Size of patch. */ /* Size of patch. */
AMS_ABORT_UNLESS(fread(buffer, 2, 1, f_ips) == 1); ReadData(buffer, 2);
u32 patch_size = GetIpsPatchSize(is_ips32, buffer); u32 patch_size = GetIpsPatchSize(is_ips32, buffer);
/* Check for RLE encoding. */ /* Check for RLE encoding. */
if (patch_size == 0) { if (patch_size == 0) {
/* Size of RLE. */ /* Size of RLE. */
AMS_ABORT_UNLESS(fread(buffer, 2, 1, f_ips) == 1); ReadData(buffer, 2);
u32 rle_size = (buffer[0] << 8) | (buffer[1]); u32 rle_size = (buffer[0] << 8) | (buffer[1]);
/* Value for RLE. */ /* Value for RLE. */
AMS_ABORT_UNLESS(fread(buffer, 1, 1, f_ips) == 1); ReadData(buffer, 1);
/* Ensure we don't write to protected region. */ /* Ensure we don't write to protected region. */
if (patch_offset < protected_size) { if (patch_offset < protected_size) {
@ -145,9 +176,9 @@ namespace ams::patcher {
const u32 diff = protected_size - patch_offset; const u32 diff = protected_size - patch_offset;
patch_offset += diff; patch_offset += diff;
patch_size -= diff; patch_size -= diff;
fseek(f_ips, diff, SEEK_CUR); file_offset += diff;
} else { } else {
fseek(f_ips, patch_size, SEEK_CUR); file_offset += patch_size;
continue; continue;
} }
} }
@ -160,9 +191,19 @@ namespace ams::patcher {
if (patch_offset + read_size > mapped_size) { if (patch_offset + read_size > mapped_size) {
read_size = mapped_size - patch_offset; 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) { 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) { 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 */ /* Ensure only one thread tries to apply patches at a time. */
char path[FS_MAX_PATH+1] = {0}; std::scoped_lock lk(apply_patch_lock);
std::snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/%s", patch_dir_name);
DIR *patches_dir = opendir(path); /* Inspect all patches from /atmosphere/<patch_dir>/<*>/<*>.ips */
struct dirent *pdir_ent; char path[fs::EntryNameLengthMax + 1];
if (patches_dir != NULL) { std::snprintf(path, sizeof(path), "sdmc:/atmosphere/%s", patch_dir_name);
/* Iterate over the patches directory to find patch subdirectories. */ const size_t patches_dir_path_len = std::strlen(path);
while ((pdir_ent = readdir(patches_dir)) != NULL) {
if (std::strcmp(pdir_ent->d_name, ".") == 0 || std::strcmp(pdir_ent->d_name, "..") == 0) { /* 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; continue;
} }
std::snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/%s/%s", patch_dir_name, pdir_ent->d_name); /* Print the path for this file. */
DIR *patch_dir = opendir(path); std::snprintf(path + patch_dir_path_len, sizeof(path) - patch_dir_path_len, "/%s", entry.name);
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;
}
size_t name_len = strlen(ent->d_name); /* Open the file. */
if (!(IpsFileExtensionLength < name_len && name_len <= ModuleIpsPatchLength)) { fs::FileHandle file;
continue; if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) {
} continue;
if ((name_len & 1) != 0) { }
continue; ON_SCOPE_EXIT { fs::CloseFile(file); };
}
if (std::strcmp(ent->d_name + name_len - IpsFileExtensionLength, IpsFileExtension) != 0) {
continue;
}
if (!MatchesModuleId(ent->d_name, name_len, IpsFileExtensionLength, module_id)) {
continue;
}
std::snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/%s/%s/%s", patch_dir_name, pdir_ent->d_name, ent->d_name); /* Read the header. */
FILE *f_ips = fopen(path, "rb"); u8 header[sizeof(IpsHeadMagic)];
if (f_ips == NULL) { if (R_SUCCEEDED(fs::ReadFile(file, 0, header, sizeof(header)))) {
continue; if (std::memcmp(header, IpsHeadMagic, sizeof(header)) == 0) {
} ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, false, file);
ON_SCOPE_EXIT { fclose(f_ips); }; } else if (std::memcmp(header, Ips32HeadMagic, sizeof(header)) == 0) {
ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, true, file);
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);
} }
closedir(patch_dir);
} }
} }
closedir(patches_dir);
} }
} }

View file

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

View file

@ -28,7 +28,7 @@ namespace ams::creport {
public: public:
ScopedFile(const char *path) : file(), offset(), opened(false) { ScopedFile(const char *path) : file(), offset(), opened(false) {
if (R_SUCCEEDED(fs::CreateFile(path, 0))) { if (R_SUCCEEDED(fs::CreateFile(path, 0))) {
this->opened = R_SUCCEEDED(fs::OpenFile(std::addressof(this->file), path, fs::OpenMode_Write | fs::OpenMode_Append)); this->opened = R_SUCCEEDED(fs::OpenFile(std::addressof(this->file), path, fs::OpenMode_Write | fs::OpenMode_AllowAppend));
} }
} }

View file

@ -877,78 +877,74 @@ namespace ams::dmnt::cheat::impl {
this->ResetAllCheatEntries(); this->ResetAllCheatEntries();
/* Open the file for program/build_id. */ /* Open the file for program/build_id. */
FILE *f_cht = nullptr; fs::FileHandle file;
{ {
char path[FS_MAX_PATH+1] = {0}; char path[fs::EntryNameLengthMax + 1];
std::snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/contents/%016lx/cheats/%02x%02x%02x%02x%02x%02x%02x%02x.txt", static_cast<u64>(program_id), std::snprintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/cheats/%02x%02x%02x%02x%02x%02x%02x%02x.txt", program_id.value,
build_id[0], build_id[1], build_id[2], build_id[3], build_id[4], build_id[5], build_id[6], build_id[7]); build_id[0], build_id[1], build_id[2], build_id[3], build_id[4], build_id[5], build_id[6], build_id[7]);
if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) {
f_cht = fopen(path, "rb"); return false;
}
} }
ON_SCOPE_EXIT { fs::CloseFile(file); };
/* Check for open failure. */
if (f_cht == nullptr) {
return false;
}
ON_SCOPE_EXIT { fclose(f_cht); };
/* Get file size. */ /* Get file size. */
fseek(f_cht, 0L, SEEK_END); s64 file_size;
const size_t cht_sz = ftell(f_cht); if (R_FAILED(fs::GetFileSize(std::addressof(file_size), file))) {
fseek(f_cht, 0L, SEEK_SET); return false;
}
/* Allocate cheat txt buffer. */ /* Allocate cheat txt buffer. */
char *cht_txt = reinterpret_cast<char *>(std::malloc(cht_sz + 1)); char *cht_txt = static_cast<char *>(std::malloc(file_size + 1));
if (cht_txt == nullptr) { if (cht_txt == nullptr) {
return false; return false;
} }
ON_SCOPE_EXIT { std::free(cht_txt); }; ON_SCOPE_EXIT { std::free(cht_txt); };
/* Read cheats into buffer. */ /* Read cheats into buffer. */
if (fread(cht_txt, 1, cht_sz, f_cht) != cht_sz) { if (R_FAILED(fs::ReadFile(file, 0, cht_txt, file_size))) {
return false; return false;
} }
cht_txt[cht_sz] = 0; cht_txt[file_size] = '\x00';
/* Parse cheat buffer. */ /* Parse cheat buffer. */
return this->ParseCheats(cht_txt, std::strlen(cht_txt)); return this->ParseCheats(cht_txt, std::strlen(cht_txt));
} }
bool CheatProcessManager::LoadCheatToggles(const ncm::ProgramId program_id) { bool CheatProcessManager::LoadCheatToggles(const ncm::ProgramId program_id) {
/* Open the file for program_id. */
FILE *f_tg = nullptr;
{
char path[FS_MAX_PATH+1] = {0};
std::snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", static_cast<u64>(program_id));
f_tg = fopen(path, "rb");
}
/* Unless we successfully parse, don't save toggles on close. */ /* Unless we successfully parse, don't save toggles on close. */
this->should_save_cheat_toggles = false; this->should_save_cheat_toggles = false;
/* Check for null, which is allowed. */ /* Open the file for program_id. */
if (f_tg == nullptr) { fs::FileHandle file;
return true; {
char path[fs::EntryNameLengthMax + 1];
std::snprintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", program_id.value);
if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) {
/* No file presence is allowed. */
return true;
}
} }
ON_SCOPE_EXIT { fclose(f_tg); }; ON_SCOPE_EXIT { fs::CloseFile(file); };
/* Get file size. */ /* Get file size. */
fseek(f_tg, 0L, SEEK_END); s64 file_size;
const size_t tg_sz = ftell(f_tg); if (R_FAILED(fs::GetFileSize(std::addressof(file_size), file))) {
fseek(f_tg, 0L, SEEK_SET); return false;
}
/* Allocate toggle txt buffer. */ /* Allocate toggle txt buffer. */
char *tg_txt = reinterpret_cast<char *>(std::malloc(tg_sz + 1)); char *tg_txt = static_cast<char *>(std::malloc(file_size + 1));
if (tg_txt == nullptr) { if (tg_txt == nullptr) {
return false; return false;
} }
ON_SCOPE_EXIT { std::free(tg_txt); }; ON_SCOPE_EXIT { std::free(tg_txt); };
/* Read cheats into buffer. */ /* Read cheats into buffer. */
if (fread(tg_txt, 1, tg_sz, f_tg) != tg_sz) { if (R_FAILED(fs::ReadFile(file, 0, tg_txt, file_size))) {
return false; return false;
} }
tg_txt[tg_sz] = 0; tg_txt[file_size] = '\x00';
/* Parse toggle buffer. */ /* Parse toggle buffer. */
this->should_save_cheat_toggles = this->ParseCheatToggles(tg_txt, std::strlen(tg_txt)); this->should_save_cheat_toggles = this->ParseCheatToggles(tg_txt, std::strlen(tg_txt));
@ -957,24 +953,34 @@ namespace ams::dmnt::cheat::impl {
void CheatProcessManager::SaveCheatToggles(const ncm::ProgramId program_id) { void CheatProcessManager::SaveCheatToggles(const ncm::ProgramId program_id) {
/* Open the file for program_id. */ /* Open the file for program_id. */
FILE *f_tg = nullptr; fs::FileHandle file;
{ {
char path[FS_MAX_PATH+1] = {0}; char path[fs::EntryNameLengthMax + 1];
std::snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", static_cast<u64>(program_id)); std::snprintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", program_id.value);
if ((f_tg = fopen(path, "wb")) == nullptr) { fs::DeleteFile(path);
fs::CreateFile(path, 0);
if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Write | fs::OpenMode_AllowAppend))) {
return; return;
} }
} }
ON_SCOPE_EXIT { fclose(f_tg); }; ON_SCOPE_EXIT { fs::CloseFile(file); };
s64 offset = 0;
char buf[0x100];
/* Save all non-master cheats. */ /* Save all non-master cheats. */
for (size_t i = 1; i < MaxCheatCount; i++) { for (size_t i = 1; i < MaxCheatCount; i++) {
if (this->cheat_entries[i].definition.num_opcodes != 0) { if (this->cheat_entries[i].definition.num_opcodes != 0) {
fprintf(f_tg, "[%s]\n", this->cheat_entries[i].definition.readable_name); std::snprintf(buf, sizeof(buf), "[%s]\n", this->cheat_entries[i].definition.readable_name);
if (this->cheat_entries[i].enabled) { const size_t name_len = std::strlen(buf);
fprintf(f_tg, "true\n"); if (R_SUCCEEDED(fs::WriteFile(file, offset, buf, name_len, fs::WriteOption::Flush))) {
} else { offset += name_len;
fprintf(f_tg, "false\n"); }
const char *entry = this->cheat_entries[i].enabled ? "true\n" : "false\n";
const size_t entry_len = std::strlen(entry);
if (R_SUCCEEDED(fs::WriteFile(file, offset, entry, entry_len, fs::WriteOption::Flush))) {
offset += entry_len;
} }
} }
} }

View file

@ -21,43 +21,66 @@ namespace ams::dmnt::cheat::impl {
void CheatVirtualMachine::DebugLog(u32 log_id, u64 value) { void CheatVirtualMachine::DebugLog(u32 log_id, u64 value) {
/* Just unconditionally try to create the log folder. */ /* Just unconditionally try to create the log folder. */
mkdir("/atmosphere/cheat_vm_logs", 0777); fs::EnsureDirectoryRecursively("sdmc:/atmosphere/cheat_vm_logs");
FILE *f_log = NULL;
fs::FileHandle log_file;
{ {
char log_path[FS_MAX_PATH]; char log_path[fs::EntryNameLengthMax + 1];
snprintf(log_path, sizeof(log_path), "/atmosphere/cheat_vm_logs/%x.log", log_id); std::snprintf(log_path, sizeof(log_path), "sdmc:/atmosphere/cheat_vm_logs/%08x.log", log_id);
f_log = fopen(log_path, "ab"); if (R_FAILED(fs::OpenFile(std::addressof(log_file), log_path, fs::OpenMode_Write | fs::OpenMode_AllowAppend))) {
return;
}
} }
if (f_log != NULL) { ON_SCOPE_EXIT { fs::CloseFile(log_file); };
ON_SCOPE_EXIT { fclose(f_log); };
fprintf(f_log, "%016lx\n", value); s64 log_offset;
if (R_FAILED(fs::GetFileSize(std::addressof(log_offset), log_file))) {
return;
} }
char log_value[18];
std::snprintf(log_value, sizeof(log_value), "%016lx\n", value);
fs::WriteFile(log_file, log_offset, log_value, std::strlen(log_value), fs::WriteOption::Flush);
} }
void CheatVirtualMachine::OpenDebugLogFile() { void CheatVirtualMachine::OpenDebugLogFile() {
#ifdef DMNT_CHEAT_VM_DEBUG_LOG #ifdef DMNT_CHEAT_VM_DEBUG_LOG
CloseDebugLogFile(); CloseDebugLogFile();
this->debug_log_file = fopen("cheat_vm_log.txt", "ab"); R_ABORT_UNLESS(fs::OpenFile(std::addressof(this->debug_log_file), "sdmc:/atmosphere/cheat_vm_logs/debug_log.txt"));
this->debug_log_file_offset = 0;
#endif #endif
} }
void CheatVirtualMachine::CloseDebugLogFile() { void CheatVirtualMachine::CloseDebugLogFile() {
#ifdef DMNT_CHEAT_VM_DEBUG_LOG #ifdef DMNT_CHEAT_VM_DEBUG_LOG
if (this->debug_log_file != NULL) { if (this->has_debug_log_file) {
fclose(this->debug_log_file); fs::CloseFile(this->debug_log_file);
this->debug_log_file = NULL;
} }
this->has_debug_log_file = false;
#endif #endif
} }
void CheatVirtualMachine::LogToDebugFile(const char *format, ...) { void CheatVirtualMachine::LogToDebugFile(const char *format, ...) {
#ifdef DMNT_CHEAT_VM_DEBUG_LOG #ifdef DMNT_CHEAT_VM_DEBUG_LOG
if (this->debug_log_file != NULL) { if (!this->has_debug_log_file) {
va_list arglist; return;
va_start(arglist, format);
vfprintf(this->debug_log_file, format, arglist);
va_end(arglist);
} }
{
std::va_list vl;
va_start(vl, format);
std::vsnprintf(this->debug_log_format_buf, sizeof(this->debug_log_format_buf) - 1, format, vl);
va_end(vl);
}
size_t fmt_len = std::strlen(this->debug_log_format_buf);
if (this->debug_log_format_buf[fmt_len - 1] != '\n') {
this->debug_log_format_buf[fmt_len + 0] = '\n';
this->debug_log_format_buf[fmt_len + 1] = '\x00';
fmt_len += 1;
}
fs::WriteFile(this->debug_log_file, this->debug_log_offset, this->debug_log_format_buf, fmt_len, fs::WriteOption::Flush);
#endif #endif
} }

View file

@ -296,7 +296,10 @@ namespace ams::dmnt::cheat::impl {
void Execute(const CheatProcessMetadata *metadata); void Execute(const CheatProcessMetadata *metadata);
#ifdef DMNT_CHEAT_VM_DEBUG_LOG #ifdef DMNT_CHEAT_VM_DEBUG_LOG
private: private:
FILE *debug_log_file = NULL; fs::FileHandle debug_log_file;
s64 debug_log_file_offset;
bool has_debug_log_file;
char debug_log_format_buf[0x100];
#endif #endif
}; };

View file

@ -78,14 +78,13 @@ void __appInit(void) {
R_ABORT_UNLESS(fsInitialize()); R_ABORT_UNLESS(fsInitialize());
}); });
R_ABORT_UNLESS(fsdevMountSdmc()); R_ABORT_UNLESS(fs::MountSdCard("sdmc"));
ams::CheckApiVersion(); ams::CheckApiVersion();
} }
void __appExit(void) { void __appExit(void) {
/* Cleanup services. */ /* Cleanup services. */
fsdevUnmountAll();
fsExit(); fsExit();
hidExit(); hidExit();
setsysExit(); setsysExit();

View file

@ -103,7 +103,6 @@ void __appInit(void) {
void __appExit(void) { void __appExit(void) {
/* Cleanup services. */ /* Cleanup services. */
fsdevUnmountAll();
fsExit(); fsExit();
plExit(); plExit();
gpioExit(); gpioExit();

View file

@ -28,7 +28,7 @@ namespace ams::fatal::srv {
public: public:
ScopedFile(const char *path) : file(), offset(), opened(false) { ScopedFile(const char *path) : file(), offset(), opened(false) {
if (R_SUCCEEDED(fs::CreateFile(path, 0))) { if (R_SUCCEEDED(fs::CreateFile(path, 0))) {
this->opened = R_SUCCEEDED(fs::OpenFile(std::addressof(this->file), path, fs::OpenMode_Write | fs::OpenMode_Append)); this->opened = R_SUCCEEDED(fs::OpenFile(std::addressof(this->file), path, fs::OpenMode_Write | fs::OpenMode_AllowAppend));
} }
} }

View file

@ -70,13 +70,12 @@ void __appInit(void) {
} }
}); });
R_ABORT_UNLESS(fsdevMountSdmc()); R_ABORT_UNLESS(fs::MountSdCard("sdmc"));
ams::CheckApiVersion(); ams::CheckApiVersion();
} }
void __appExit(void) { void __appExit(void) {
fsdevUnmountAll();
fsExit(); fsExit();
if (hos::GetVersion() < hos::Version_300) { if (hos::GetVersion() < hos::Version_300) {
pminfoExit(); pminfoExit();