From e09505ff6147815d7d3c7adcc0d260638cf49706 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 30 Sep 2018 14:04:48 -0400 Subject: [PATCH 1/5] nso/nro: Add NSO arguments structure to data section Only added if arguments string is non-empty and a pass is requested by loader. --- src/core/loader/deconstructed_rom_directory.cpp | 4 +++- src/core/loader/nro.cpp | 13 +++++++++++++ src/core/loader/nso.cpp | 15 ++++++++++++++- src/core/loader/nso.h | 9 ++++++++- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp index 9a86e5824..61808d094 100644 --- a/src/core/loader/deconstructed_rom_directory.cpp +++ b/src/core/loader/deconstructed_rom_directory.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include "common/common_funcs.h" #include "common/file_util.h" #include "common/logging/log.h" @@ -140,7 +141,8 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(Kernel::Process& process) const FileSys::VirtualFile module_file = dir->GetFile(module); if (module_file != nullptr) { const VAddr load_addr = next_load_addr; - next_load_addr = AppLoader_NSO::LoadModule(module_file, load_addr, pm); + next_load_addr = + AppLoader_NSO::LoadModule(module_file, load_addr, std::strcmp(module, "rtld") == 0, pm); LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); // Register module with GDBStub GDBStub::RegisterModule(module, load_addr, next_load_addr - 1, false); diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index c10f826a4..4109b9974 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -18,7 +18,9 @@ #include "core/hle/kernel/process.h" #include "core/hle/kernel/vm_manager.h" #include "core/loader/nro.h" +#include "core/loader/nso.h" #include "core/memory.h" +#include "core/settings.h" namespace Loader { @@ -150,6 +152,17 @@ bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) { codeset->segments[i].size = PageAlignSize(nro_header.segments[i].size); } + if (!Settings::values.program_args.empty()) { + const auto arg_data = Settings::values.program_args; + codeset->DataSegment().size += 0x9000; + NSOArgumentHeader args_header{0x9000, arg_data.size(), {}}; + program_image.resize(static_cast(program_image.size()) + 0x9000); + std::memcpy(program_image.data() + program_image.size() - 0x9000, &args_header, + sizeof(NSOArgumentHeader)); + std::memcpy(program_image.data() + program_image.size() - 0x8FE0, arg_data.data(), + arg_data.size()); + } + // Read MOD header ModHeader mod_header{}; // Default .bss to NRO header bss size if MOD0 section doesn't exist diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 2186b02af..8ee2c6f2b 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -17,6 +17,7 @@ #include "core/hle/kernel/vm_manager.h" #include "core/loader/nso.h" #include "core/memory.h" +#include "core/settings.h" namespace Loader { @@ -94,6 +95,7 @@ static constexpr u32 PageAlignSize(u32 size) { } VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base, + bool should_pass_arguments, boost::optional pm) { if (file == nullptr) return {}; @@ -125,6 +127,17 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base, codeset->segments[i].size = PageAlignSize(static_cast(data.size())); } + if (should_pass_arguments && !Settings::values.program_args.empty()) { + const auto arg_data = Settings::values.program_args; + codeset->DataSegment().size += 0x9000; + NSOArgumentHeader args_header{0x9000, arg_data.size(), {}}; + program_image.resize(static_cast(program_image.size()) + 0x9000); + std::memcpy(program_image.data() + program_image.size() - 0x9000, &args_header, + sizeof(NSOArgumentHeader)); + std::memcpy(program_image.data() + program_image.size() - 0x8FE0, arg_data.data(), + arg_data.size()); + } + // MOD header pointer is at .text offset + 4 u32 module_offset; std::memcpy(&module_offset, program_image.data() + 4, sizeof(u32)); @@ -172,7 +185,7 @@ ResultStatus AppLoader_NSO::Load(Kernel::Process& process) { // Load module const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress(); - LoadModule(file, base_address); + LoadModule(file, base_address, true); LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address); process.Run(base_address, Kernel::THREADPRIO_DEFAULT, Memory::DEFAULT_STACK_SIZE); diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 05353d4d9..7833af6ee 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -11,6 +11,13 @@ namespace Loader { +struct NSOArgumentHeader { + u32_le allocated_size; + u32_le actual_size; + INSERT_PADDING_BYTES(0x18); +}; +static_assert(sizeof(NSOArgumentHeader) == 0x20, "NSOArgumentHeader has incorrect size."); + /// Loads an NSO file class AppLoader_NSO final : public AppLoader, Linker { public: @@ -27,7 +34,7 @@ public: return IdentifyType(file); } - static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base, + static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base, bool should_pass_arguments, boost::optional pm = boost::none); ResultStatus Load(Kernel::Process& process) override; From 95dff555a4dbbf574126e408727a2774fe933b14 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 30 Sep 2018 14:05:12 -0400 Subject: [PATCH 2/5] settings: Add program_args string setting --- src/core/settings.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/settings.h b/src/core/settings.h index 1808f5937..83b9a04c8 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -155,6 +155,7 @@ struct Values { // Debugging bool use_gdbstub; u16 gdbstub_port; + std::string program_args; // WebService bool enable_telemetry; From 8bbc12b9c24b67a08afd2df884da8fb664eb00be Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 30 Sep 2018 14:05:38 -0400 Subject: [PATCH 3/5] qt: Add UI option to configure arguments --- src/yuzu/configuration/config.cpp | 2 ++ src/yuzu/configuration/configure_debug.cpp | 2 ++ src/yuzu/configuration/configure_debug.ui | 23 ++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index 650dd03c0..7fec15991 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -134,6 +134,7 @@ void Config::ReadValues() { qt_config->beginGroup("Debugging"); Settings::values.use_gdbstub = qt_config->value("use_gdbstub", false).toBool(); Settings::values.gdbstub_port = qt_config->value("gdbstub_port", 24689).toInt(); + Settings::values.program_args = qt_config->value("program_args", "").toString().toStdString(); qt_config->endGroup(); qt_config->beginGroup("WebService"); @@ -269,6 +270,7 @@ void Config::SaveValues() { qt_config->beginGroup("Debugging"); qt_config->setValue("use_gdbstub", Settings::values.use_gdbstub); qt_config->setValue("gdbstub_port", Settings::values.gdbstub_port); + qt_config->setValue("program_args", QString::fromStdString(Settings::values.program_args)); qt_config->endGroup(); qt_config->beginGroup("WebService"); diff --git a/src/yuzu/configuration/configure_debug.cpp b/src/yuzu/configuration/configure_debug.cpp index 45d84f19a..9e765fc93 100644 --- a/src/yuzu/configuration/configure_debug.cpp +++ b/src/yuzu/configuration/configure_debug.cpp @@ -33,6 +33,7 @@ void ConfigureDebug::setConfiguration() { ui->toggle_console->setEnabled(!Core::System::GetInstance().IsPoweredOn()); ui->toggle_console->setChecked(UISettings::values.show_console); ui->log_filter_edit->setText(QString::fromStdString(Settings::values.log_filter)); + ui->homebrew_args_edit->setText(QString::fromStdString(Settings::values.program_args)); } void ConfigureDebug::applyConfiguration() { @@ -40,6 +41,7 @@ void ConfigureDebug::applyConfiguration() { Settings::values.gdbstub_port = ui->gdbport_spinbox->value(); UISettings::values.show_console = ui->toggle_console->isChecked(); Settings::values.log_filter = ui->log_filter_edit->text().toStdString(); + Settings::values.program_args = ui->homebrew_args_edit->text().toStdString(); Debugger::ToggleConsole(); Log::Filter filter; filter.ParseFilterString(Settings::values.log_filter); diff --git a/src/yuzu/configuration/configure_debug.ui b/src/yuzu/configuration/configure_debug.ui index 5ae7276bd..ff4987604 100644 --- a/src/yuzu/configuration/configure_debug.ui +++ b/src/yuzu/configuration/configure_debug.ui @@ -106,6 +106,29 @@ + + + + Homebrew + + + + + + + + Arguments String + + + + + + + + + + + From 081f5c1dbf8f7a40c801832f56adb5293e2bac1a Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 30 Sep 2018 14:28:17 -0400 Subject: [PATCH 4/5] cmd: Support passing game arguments from command line Uses -p (--program) and following string as args. --- src/core/loader/nro.cpp | 2 +- src/core/loader/nso.cpp | 2 +- src/yuzu_cmd/config.cpp | 1 + src/yuzu_cmd/yuzu.cpp | 19 +++++++++++-------- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 4109b9974..0d7c1dcfa 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -155,7 +155,7 @@ bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) { if (!Settings::values.program_args.empty()) { const auto arg_data = Settings::values.program_args; codeset->DataSegment().size += 0x9000; - NSOArgumentHeader args_header{0x9000, arg_data.size(), {}}; + NSOArgumentHeader args_header{0x9000, static_cast(arg_data.size()), {}}; program_image.resize(static_cast(program_image.size()) + 0x9000); std::memcpy(program_image.data() + program_image.size() - 0x9000, &args_header, sizeof(NSOArgumentHeader)); diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 8ee2c6f2b..c225e2d24 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -130,7 +130,7 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base, if (should_pass_arguments && !Settings::values.program_args.empty()) { const auto arg_data = Settings::values.program_args; codeset->DataSegment().size += 0x9000; - NSOArgumentHeader args_header{0x9000, arg_data.size(), {}}; + NSOArgumentHeader args_header{0x9000, static_cast(arg_data.size()), {}}; program_image.resize(static_cast(program_image.size()) + 0x9000); std::memcpy(program_image.data() + program_image.size() - 0x9000, &args_header, sizeof(NSOArgumentHeader)); diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index 9d934e220..2470f4640 100644 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -138,6 +138,7 @@ void Config::ReadValues() { Settings::values.use_gdbstub = sdl2_config->GetBoolean("Debugging", "use_gdbstub", false); Settings::values.gdbstub_port = static_cast(sdl2_config->GetInteger("Debugging", "gdbstub_port", 24689)); + Settings::values.program_args = sdl2_config->Get("Debugging", "program_args", ""); // Web Service Settings::values.enable_telemetry = diff --git a/src/yuzu_cmd/yuzu.cpp b/src/yuzu_cmd/yuzu.cpp index 1d951ca3f..27aba95f6 100644 --- a/src/yuzu_cmd/yuzu.cpp +++ b/src/yuzu_cmd/yuzu.cpp @@ -56,9 +56,10 @@ static void PrintHelp(const char* argv0) { std::cout << "Usage: " << argv0 << " [options] \n" "-g, --gdbport=NUMBER Enable gdb stub on port NUMBER\n" - "-f, --fullscreen Start in fullscreen mode\n" + "-f, --fullscreen Start in fullscreen mode\n" "-h, --help Display this help and exit\n" - "-v, --version Output version information and exit\n"; + "-v, --version Output version information and exit\n" + "-p, --program Pass following string as arguments to executable\n"; } static void PrintVersion() { @@ -103,15 +104,13 @@ int main(int argc, char** argv) { bool fullscreen = false; static struct option long_options[] = { - {"gdbport", required_argument, 0, 'g'}, - {"fullscreen", no_argument, 0, 'f'}, - {"help", no_argument, 0, 'h'}, - {"version", no_argument, 0, 'v'}, - {0, 0, 0, 0}, + {"gdbport", required_argument, 0, 'g'}, {"fullscreen", no_argument, 0, 'f'}, + {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, + {"program", optional_argument, 0, 'p'}, {0, 0, 0, 0}, }; while (optind < argc) { - char arg = getopt_long(argc, argv, "g:fhv", long_options, &option_index); + char arg = getopt_long(argc, argv, "g:fhvp::", long_options, &option_index); if (arg != -1) { switch (arg) { case 'g': @@ -135,6 +134,10 @@ int main(int argc, char** argv) { case 'v': PrintVersion(); return 0; + case 'p': + Settings::values.program_args = argv[optind]; + ++optind; + break; } } else { #ifdef _WIN32 From f945e9767cd66567030e9daca13eb96d0a2d47ff Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Fri, 5 Oct 2018 13:52:07 -0400 Subject: [PATCH 5/5] nso/nro: Use default allocation size for arg_data --- src/core/loader/deconstructed_rom_directory.cpp | 4 ++-- src/core/loader/nro.cpp | 14 ++++++++------ src/core/loader/nso.cpp | 14 ++++++++------ src/core/loader/nso.h | 2 ++ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp index 61808d094..951fd8257 100644 --- a/src/core/loader/deconstructed_rom_directory.cpp +++ b/src/core/loader/deconstructed_rom_directory.cpp @@ -141,8 +141,8 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(Kernel::Process& process) const FileSys::VirtualFile module_file = dir->GetFile(module); if (module_file != nullptr) { const VAddr load_addr = next_load_addr; - next_load_addr = - AppLoader_NSO::LoadModule(module_file, load_addr, std::strcmp(module, "rtld") == 0, pm); + next_load_addr = AppLoader_NSO::LoadModule(module_file, load_addr, + std::strcmp(module, "rtld") == 0, pm); LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); // Register module with GDBStub GDBStub::RegisterModule(module, load_addr, next_load_addr - 1, false); diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 0d7c1dcfa..25dd3f04e 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -154,12 +154,14 @@ bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) { if (!Settings::values.program_args.empty()) { const auto arg_data = Settings::values.program_args; - codeset->DataSegment().size += 0x9000; - NSOArgumentHeader args_header{0x9000, static_cast(arg_data.size()), {}}; - program_image.resize(static_cast(program_image.size()) + 0x9000); - std::memcpy(program_image.data() + program_image.size() - 0x9000, &args_header, - sizeof(NSOArgumentHeader)); - std::memcpy(program_image.data() + program_image.size() - 0x8FE0, arg_data.data(), + codeset->DataSegment().size += NSO_ARGUMENT_DATA_ALLOCATION_SIZE; + NSOArgumentHeader args_header{ + NSO_ARGUMENT_DATA_ALLOCATION_SIZE, static_cast(arg_data.size()), {}}; + const auto end_offset = program_image.size(); + program_image.resize(static_cast(program_image.size()) + + NSO_ARGUMENT_DATA_ALLOCATION_SIZE); + std::memcpy(program_image.data() + end_offset, &args_header, sizeof(NSOArgumentHeader)); + std::memcpy(program_image.data() + end_offset + sizeof(NSOArgumentHeader), arg_data.data(), arg_data.size()); } diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index c225e2d24..28c6dd9b7 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -129,12 +129,14 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base, if (should_pass_arguments && !Settings::values.program_args.empty()) { const auto arg_data = Settings::values.program_args; - codeset->DataSegment().size += 0x9000; - NSOArgumentHeader args_header{0x9000, static_cast(arg_data.size()), {}}; - program_image.resize(static_cast(program_image.size()) + 0x9000); - std::memcpy(program_image.data() + program_image.size() - 0x9000, &args_header, - sizeof(NSOArgumentHeader)); - std::memcpy(program_image.data() + program_image.size() - 0x8FE0, arg_data.data(), + codeset->DataSegment().size += NSO_ARGUMENT_DATA_ALLOCATION_SIZE; + NSOArgumentHeader args_header{ + NSO_ARGUMENT_DATA_ALLOCATION_SIZE, static_cast(arg_data.size()), {}}; + const auto end_offset = program_image.size(); + program_image.resize(static_cast(program_image.size()) + + NSO_ARGUMENT_DATA_ALLOCATION_SIZE); + std::memcpy(program_image.data() + end_offset, &args_header, sizeof(NSOArgumentHeader)); + std::memcpy(program_image.data() + end_offset + sizeof(NSOArgumentHeader), arg_data.data(), arg_data.size()); } diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 7833af6ee..70ab3b718 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -11,6 +11,8 @@ namespace Loader { +constexpr u64 NSO_ARGUMENT_DATA_ALLOCATION_SIZE = 0x9000; + struct NSOArgumentHeader { u32_le allocated_size; u32_le actual_size;