diff options
Diffstat (limited to 'src')
41 files changed, 1126 insertions, 627 deletions
diff --git a/src/audio_core/codec.cpp b/src/audio_core/codec.cpp index 7a3bd7eb3..6fba9fdae 100644 --- a/src/audio_core/codec.cpp +++ b/src/audio_core/codec.cpp | |||
| @@ -117,7 +117,9 @@ StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data, | |||
| 117 | ret[i].fill(sample); | 117 | ret[i].fill(sample); |
| 118 | } | 118 | } |
| 119 | } else { | 119 | } else { |
| 120 | std::memcpy(ret.data(), data, sample_count * 2 * sizeof(u16)); | 120 | for (size_t i = 0; i < sample_count; ++i) { |
| 121 | std::memcpy(&ret[i], data + i * sizeof(s16) * 2, 2 * sizeof(s16)); | ||
| 122 | } | ||
| 121 | } | 123 | } |
| 122 | 124 | ||
| 123 | return ret; | 125 | return ret; |
diff --git a/src/audio_core/codec.h b/src/audio_core/codec.h index 2b0c395e6..877b2202d 100644 --- a/src/audio_core/codec.h +++ b/src/audio_core/codec.h | |||
| @@ -5,13 +5,13 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <vector> | 8 | #include <deque> |
| 9 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 10 | 10 | ||
| 11 | namespace Codec { | 11 | namespace Codec { |
| 12 | 12 | ||
| 13 | /// A variable length buffer of signed PCM16 stereo samples. | 13 | /// A variable length buffer of signed PCM16 stereo samples. |
| 14 | using StereoBuffer16 = std::vector<std::array<s16, 2>>; | 14 | using StereoBuffer16 = std::deque<std::array<s16, 2>>; |
| 15 | 15 | ||
| 16 | /// See: Codec::DecodeADPCM | 16 | /// See: Codec::DecodeADPCM |
| 17 | struct ADPCMState { | 17 | struct ADPCMState { |
diff --git a/src/audio_core/hle/source.h b/src/audio_core/hle/source.h index ccb7f064f..c4d2debc2 100644 --- a/src/audio_core/hle/source.h +++ b/src/audio_core/hle/source.h | |||
| @@ -108,7 +108,7 @@ private: | |||
| 108 | 108 | ||
| 109 | u32 current_sample_number = 0; | 109 | u32 current_sample_number = 0; |
| 110 | u32 next_sample_number = 0; | 110 | u32 next_sample_number = 0; |
| 111 | std::vector<std::array<s16, 2>> current_buffer; | 111 | AudioInterp::StereoBuffer16 current_buffer; |
| 112 | 112 | ||
| 113 | // buffer_id state | 113 | // buffer_id state |
| 114 | 114 | ||
diff --git a/src/audio_core/interpolate.cpp b/src/audio_core/interpolate.cpp index 16e68bc5c..83573d772 100644 --- a/src/audio_core/interpolate.cpp +++ b/src/audio_core/interpolate.cpp | |||
| @@ -47,7 +47,7 @@ static void StepOverSamples(State& state, StereoBuffer16& input, float rate, | |||
| 47 | state.xn1 = input[inputi + 1]; | 47 | state.xn1 = input[inputi + 1]; |
| 48 | state.fposition = fposition - inputi * scale_factor; | 48 | state.fposition = fposition - inputi * scale_factor; |
| 49 | 49 | ||
| 50 | input.erase(input.begin(), input.begin() + inputi + 2); | 50 | input.erase(input.begin(), std::next(input.begin(), inputi + 2)); |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | void None(State& state, StereoBuffer16& input, float rate, DSP::HLE::StereoFrame16& output, | 53 | void None(State& state, StereoBuffer16& input, float rate, DSP::HLE::StereoFrame16& output, |
diff --git a/src/audio_core/interpolate.h b/src/audio_core/interpolate.h index 59f59bc14..8dff6111a 100644 --- a/src/audio_core/interpolate.h +++ b/src/audio_core/interpolate.h | |||
| @@ -5,14 +5,14 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <vector> | 8 | #include <deque> |
| 9 | #include "audio_core/hle/common.h" | 9 | #include "audio_core/hle/common.h" |
| 10 | #include "common/common_types.h" | 10 | #include "common/common_types.h" |
| 11 | 11 | ||
| 12 | namespace AudioInterp { | 12 | namespace AudioInterp { |
| 13 | 13 | ||
| 14 | /// A variable length buffer of signed PCM16 stereo samples. | 14 | /// A variable length buffer of signed PCM16 stereo samples. |
| 15 | using StereoBuffer16 = std::vector<std::array<s16, 2>>; | 15 | using StereoBuffer16 = std::deque<std::array<s16, 2>>; |
| 16 | 16 | ||
| 17 | struct State { | 17 | struct State { |
| 18 | /// Two historical samples. | 18 | /// Two historical samples. |
diff --git a/src/citra_qt/configuration/configure_graphics.ui b/src/citra_qt/configuration/configure_graphics.ui index b340149d5..5667b14b6 100644 --- a/src/citra_qt/configuration/configure_graphics.ui +++ b/src/citra_qt/configuration/configure_graphics.ui | |||
| @@ -63,57 +63,57 @@ | |||
| 63 | <widget class="QComboBox" name="resolution_factor_combobox"> | 63 | <widget class="QComboBox" name="resolution_factor_combobox"> |
| 64 | <item> | 64 | <item> |
| 65 | <property name="text"> | 65 | <property name="text"> |
| 66 | <string notr="true">Auto (Window Size)</string> | 66 | <string>Auto (Window Size)</string> |
| 67 | </property> | 67 | </property> |
| 68 | </item> | 68 | </item> |
| 69 | <item> | 69 | <item> |
| 70 | <property name="text"> | 70 | <property name="text"> |
| 71 | <string notr="true">Native (400x240)</string> | 71 | <string>Native (400x240)</string> |
| 72 | </property> | 72 | </property> |
| 73 | </item> | 73 | </item> |
| 74 | <item> | 74 | <item> |
| 75 | <property name="text"> | 75 | <property name="text"> |
| 76 | <string notr="true">2x Native (800x480)</string> | 76 | <string>2x Native (800x480)</string> |
| 77 | </property> | 77 | </property> |
| 78 | </item> | 78 | </item> |
| 79 | <item> | 79 | <item> |
| 80 | <property name="text"> | 80 | <property name="text"> |
| 81 | <string notr="true">3x Native (1200x720)</string> | 81 | <string>3x Native (1200x720)</string> |
| 82 | </property> | 82 | </property> |
| 83 | </item> | 83 | </item> |
| 84 | <item> | 84 | <item> |
| 85 | <property name="text"> | 85 | <property name="text"> |
| 86 | <string notr="true">4x Native (1600x960)</string> | 86 | <string>4x Native (1600x960)</string> |
| 87 | </property> | 87 | </property> |
| 88 | </item> | 88 | </item> |
| 89 | <item> | 89 | <item> |
| 90 | <property name="text"> | 90 | <property name="text"> |
| 91 | <string notr="true">5x Native (2000x1200)</string> | 91 | <string>5x Native (2000x1200)</string> |
| 92 | </property> | 92 | </property> |
| 93 | </item> | 93 | </item> |
| 94 | <item> | 94 | <item> |
| 95 | <property name="text"> | 95 | <property name="text"> |
| 96 | <string notr="true">6x Native (2400x1440)</string> | 96 | <string>6x Native (2400x1440)</string> |
| 97 | </property> | 97 | </property> |
| 98 | </item> | 98 | </item> |
| 99 | <item> | 99 | <item> |
| 100 | <property name="text"> | 100 | <property name="text"> |
| 101 | <string notr="true">7x Native (2800x1680)</string> | 101 | <string>7x Native (2800x1680)</string> |
| 102 | </property> | 102 | </property> |
| 103 | </item> | 103 | </item> |
| 104 | <item> | 104 | <item> |
| 105 | <property name="text"> | 105 | <property name="text"> |
| 106 | <string notr="true">8x Native (3200x1920)</string> | 106 | <string>8x Native (3200x1920)</string> |
| 107 | </property> | 107 | </property> |
| 108 | </item> | 108 | </item> |
| 109 | <item> | 109 | <item> |
| 110 | <property name="text"> | 110 | <property name="text"> |
| 111 | <string notr="true">9x Native (3600x2160)</string> | 111 | <string>9x Native (3600x2160)</string> |
| 112 | </property> | 112 | </property> |
| 113 | </item> | 113 | </item> |
| 114 | <item> | 114 | <item> |
| 115 | <property name="text"> | 115 | <property name="text"> |
| 116 | <string notr="true">10x Native (4000x2400)</string> | 116 | <string>10x Native (4000x2400)</string> |
| 117 | </property> | 117 | </property> |
| 118 | </item> | 118 | </item> |
| 119 | </widget> | 119 | </widget> |
diff --git a/src/citra_qt/configuration/configure_system.cpp b/src/citra_qt/configuration/configure_system.cpp index 9b1e6711d..88a067c12 100644 --- a/src/citra_qt/configuration/configure_system.cpp +++ b/src/citra_qt/configuration/configure_system.cpp | |||
| @@ -78,7 +78,8 @@ void ConfigureSystem::ReadSystemSettings() { | |||
| 78 | 78 | ||
| 79 | // set the console id | 79 | // set the console id |
| 80 | u64 console_id = Service::CFG::GetConsoleUniqueId(); | 80 | u64 console_id = Service::CFG::GetConsoleUniqueId(); |
| 81 | ui->label_console_id->setText("Console ID: 0x" + QString::number(console_id, 16).toUpper()); | 81 | ui->label_console_id->setText( |
| 82 | tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper())); | ||
| 82 | } | 83 | } |
| 83 | 84 | ||
| 84 | void ConfigureSystem::applyConfiguration() { | 85 | void ConfigureSystem::applyConfiguration() { |
diff --git a/src/citra_qt/configuration/configure_web.cpp b/src/citra_qt/configuration/configure_web.cpp index 38ce19c0f..bf8c21ac7 100644 --- a/src/citra_qt/configuration/configure_web.cpp +++ b/src/citra_qt/configuration/configure_web.cpp | |||
| @@ -24,15 +24,15 @@ ConfigureWeb::~ConfigureWeb() {} | |||
| 24 | void ConfigureWeb::setConfiguration() { | 24 | void ConfigureWeb::setConfiguration() { |
| 25 | ui->web_credentials_disclaimer->setWordWrap(true); | 25 | ui->web_credentials_disclaimer->setWordWrap(true); |
| 26 | ui->telemetry_learn_more->setOpenExternalLinks(true); | 26 | ui->telemetry_learn_more->setOpenExternalLinks(true); |
| 27 | ui->telemetry_learn_more->setText("<a " | 27 | ui->telemetry_learn_more->setText(tr("<a " |
| 28 | "href='https://citra-emu.org/entry/" | 28 | "href='https://citra-emu.org/entry/" |
| 29 | "telemetry-and-why-thats-a-good-thing/'>Learn more</a>"); | 29 | "telemetry-and-why-thats-a-good-thing/'>Learn more</a>")); |
| 30 | 30 | ||
| 31 | ui->web_signup_link->setOpenExternalLinks(true); | 31 | ui->web_signup_link->setOpenExternalLinks(true); |
| 32 | ui->web_signup_link->setText("<a href='https://services.citra-emu.org/'>Sign up</a>"); | 32 | ui->web_signup_link->setText(tr("<a href='https://services.citra-emu.org/'>Sign up</a>")); |
| 33 | ui->web_token_info_link->setOpenExternalLinks(true); | 33 | ui->web_token_info_link->setOpenExternalLinks(true); |
| 34 | ui->web_token_info_link->setText( | 34 | ui->web_token_info_link->setText( |
| 35 | "<a href='https://citra-emu.org/wiki/citra-web-service/'>What is my token?</a>"); | 35 | tr("<a href='https://citra-emu.org/wiki/citra-web-service/'>What is my token?</a>")); |
| 36 | 36 | ||
| 37 | ui->toggle_telemetry->setChecked(Settings::values.enable_telemetry); | 37 | ui->toggle_telemetry->setChecked(Settings::values.enable_telemetry); |
| 38 | ui->edit_username->setText(QString::fromStdString(Settings::values.citra_username)); | 38 | ui->edit_username->setText(QString::fromStdString(Settings::values.citra_username)); |
| @@ -40,8 +40,8 @@ void ConfigureWeb::setConfiguration() { | |||
| 40 | // Connect after setting the values, to avoid calling OnLoginChanged now | 40 | // Connect after setting the values, to avoid calling OnLoginChanged now |
| 41 | connect(ui->edit_token, &QLineEdit::textChanged, this, &ConfigureWeb::OnLoginChanged); | 41 | connect(ui->edit_token, &QLineEdit::textChanged, this, &ConfigureWeb::OnLoginChanged); |
| 42 | connect(ui->edit_username, &QLineEdit::textChanged, this, &ConfigureWeb::OnLoginChanged); | 42 | connect(ui->edit_username, &QLineEdit::textChanged, this, &ConfigureWeb::OnLoginChanged); |
| 43 | ui->label_telemetry_id->setText("Telemetry ID: 0x" + | 43 | ui->label_telemetry_id->setText( |
| 44 | QString::number(Core::GetTelemetryId(), 16).toUpper()); | 44 | tr("Telemetry ID: 0x%1").arg(QString::number(Core::GetTelemetryId(), 16).toUpper())); |
| 45 | user_verified = true; | 45 | user_verified = true; |
| 46 | } | 46 | } |
| 47 | 47 | ||
| @@ -60,8 +60,8 @@ void ConfigureWeb::applyConfiguration() { | |||
| 60 | 60 | ||
| 61 | void ConfigureWeb::RefreshTelemetryID() { | 61 | void ConfigureWeb::RefreshTelemetryID() { |
| 62 | const u64 new_telemetry_id{Core::RegenerateTelemetryId()}; | 62 | const u64 new_telemetry_id{Core::RegenerateTelemetryId()}; |
| 63 | ui->label_telemetry_id->setText("Telemetry ID: 0x" + | 63 | ui->label_telemetry_id->setText( |
| 64 | QString::number(new_telemetry_id, 16).toUpper()); | 64 | tr("Telemetry ID: 0x%1").arg(QString::number(new_telemetry_id, 16).toUpper())); |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | void ConfigureWeb::OnLoginChanged() { | 67 | void ConfigureWeb::OnLoginChanged() { |
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index cd1a8de2d..3ed619991 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -26,6 +26,7 @@ set(SRCS | |||
| 26 | file_sys/archive_systemsavedata.cpp | 26 | file_sys/archive_systemsavedata.cpp |
| 27 | file_sys/disk_archive.cpp | 27 | file_sys/disk_archive.cpp |
| 28 | file_sys/ivfc_archive.cpp | 28 | file_sys/ivfc_archive.cpp |
| 29 | file_sys/ncch_container.cpp | ||
| 29 | file_sys/path_parser.cpp | 30 | file_sys/path_parser.cpp |
| 30 | file_sys/savedata_archive.cpp | 31 | file_sys/savedata_archive.cpp |
| 31 | frontend/camera/blank_camera.cpp | 32 | frontend/camera/blank_camera.cpp |
diff --git a/src/core/arm/arm_interface.h b/src/core/arm/arm_interface.h index ccd43f431..2aa017a54 100644 --- a/src/core/arm/arm_interface.h +++ b/src/core/arm/arm_interface.h | |||
| @@ -41,6 +41,9 @@ public: | |||
| 41 | /// Clear all instruction cache | 41 | /// Clear all instruction cache |
| 42 | virtual void ClearInstructionCache() = 0; | 42 | virtual void ClearInstructionCache() = 0; |
| 43 | 43 | ||
| 44 | /// Notify CPU emulation that page tables have changed | ||
| 45 | virtual void PageTableChanged() = 0; | ||
| 46 | |||
| 44 | /** | 47 | /** |
| 45 | * Set the Program Counter to an address | 48 | * Set the Program Counter to an address |
| 46 | * @param addr Address to set PC to | 49 | * @param addr Address to set PC to |
diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp index 34c5aa381..42ae93ae8 100644 --- a/src/core/arm/dynarmic/arm_dynarmic.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic.cpp | |||
| @@ -41,7 +41,7 @@ static bool IsReadOnlyMemory(u32 vaddr) { | |||
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | static Dynarmic::UserCallbacks GetUserCallbacks( | 43 | static Dynarmic::UserCallbacks GetUserCallbacks( |
| 44 | const std::shared_ptr<ARMul_State>& interpeter_state) { | 44 | const std::shared_ptr<ARMul_State>& interpeter_state, Memory::PageTable* current_page_table) { |
| 45 | Dynarmic::UserCallbacks user_callbacks{}; | 45 | Dynarmic::UserCallbacks user_callbacks{}; |
| 46 | user_callbacks.InterpreterFallback = &InterpreterFallback; | 46 | user_callbacks.InterpreterFallback = &InterpreterFallback; |
| 47 | user_callbacks.user_arg = static_cast<void*>(interpeter_state.get()); | 47 | user_callbacks.user_arg = static_cast<void*>(interpeter_state.get()); |
| @@ -56,16 +56,14 @@ static Dynarmic::UserCallbacks GetUserCallbacks( | |||
| 56 | user_callbacks.memory.Write16 = &Memory::Write16; | 56 | user_callbacks.memory.Write16 = &Memory::Write16; |
| 57 | user_callbacks.memory.Write32 = &Memory::Write32; | 57 | user_callbacks.memory.Write32 = &Memory::Write32; |
| 58 | user_callbacks.memory.Write64 = &Memory::Write64; | 58 | user_callbacks.memory.Write64 = &Memory::Write64; |
| 59 | // TODO(Subv): Re-add the page table pointers once dynarmic supports switching page tables at | 59 | user_callbacks.page_table = ¤t_page_table->pointers; |
| 60 | // runtime. | ||
| 61 | user_callbacks.page_table = nullptr; | ||
| 62 | user_callbacks.coprocessors[15] = std::make_shared<DynarmicCP15>(interpeter_state); | 60 | user_callbacks.coprocessors[15] = std::make_shared<DynarmicCP15>(interpeter_state); |
| 63 | return user_callbacks; | 61 | return user_callbacks; |
| 64 | } | 62 | } |
| 65 | 63 | ||
| 66 | ARM_Dynarmic::ARM_Dynarmic(PrivilegeMode initial_mode) { | 64 | ARM_Dynarmic::ARM_Dynarmic(PrivilegeMode initial_mode) { |
| 67 | interpreter_state = std::make_shared<ARMul_State>(initial_mode); | 65 | interpreter_state = std::make_shared<ARMul_State>(initial_mode); |
| 68 | jit = std::make_unique<Dynarmic::Jit>(GetUserCallbacks(interpreter_state)); | 66 | PageTableChanged(); |
| 69 | } | 67 | } |
| 70 | 68 | ||
| 71 | void ARM_Dynarmic::SetPC(u32 pc) { | 69 | void ARM_Dynarmic::SetPC(u32 pc) { |
| @@ -136,6 +134,7 @@ void ARM_Dynarmic::AddTicks(u64 ticks) { | |||
| 136 | MICROPROFILE_DEFINE(ARM_Jit, "ARM JIT", "ARM JIT", MP_RGB(255, 64, 64)); | 134 | MICROPROFILE_DEFINE(ARM_Jit, "ARM JIT", "ARM JIT", MP_RGB(255, 64, 64)); |
| 137 | 135 | ||
| 138 | void ARM_Dynarmic::ExecuteInstructions(int num_instructions) { | 136 | void ARM_Dynarmic::ExecuteInstructions(int num_instructions) { |
| 137 | ASSERT(Memory::GetCurrentPageTable() == current_page_table); | ||
| 139 | MICROPROFILE_SCOPE(ARM_Jit); | 138 | MICROPROFILE_SCOPE(ARM_Jit); |
| 140 | 139 | ||
| 141 | std::size_t ticks_executed = jit->Run(static_cast<unsigned>(num_instructions)); | 140 | std::size_t ticks_executed = jit->Run(static_cast<unsigned>(num_instructions)); |
| @@ -178,3 +177,16 @@ void ARM_Dynarmic::PrepareReschedule() { | |||
| 178 | void ARM_Dynarmic::ClearInstructionCache() { | 177 | void ARM_Dynarmic::ClearInstructionCache() { |
| 179 | jit->ClearCache(); | 178 | jit->ClearCache(); |
| 180 | } | 179 | } |
| 180 | |||
| 181 | void ARM_Dynarmic::PageTableChanged() { | ||
| 182 | current_page_table = Memory::GetCurrentPageTable(); | ||
| 183 | |||
| 184 | auto iter = jits.find(current_page_table); | ||
| 185 | if (iter != jits.end()) { | ||
| 186 | jit = iter->second.get(); | ||
| 187 | return; | ||
| 188 | } | ||
| 189 | |||
| 190 | jit = new Dynarmic::Jit(GetUserCallbacks(interpreter_state, current_page_table)); | ||
| 191 | jits.emplace(current_page_table, std::unique_ptr<Dynarmic::Jit>(jit)); | ||
| 192 | } | ||
diff --git a/src/core/arm/dynarmic/arm_dynarmic.h b/src/core/arm/dynarmic/arm_dynarmic.h index 834dc989e..96148a1a5 100644 --- a/src/core/arm/dynarmic/arm_dynarmic.h +++ b/src/core/arm/dynarmic/arm_dynarmic.h | |||
| @@ -4,12 +4,17 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <map> | ||
| 7 | #include <memory> | 8 | #include <memory> |
| 8 | #include <dynarmic/dynarmic.h> | 9 | #include <dynarmic/dynarmic.h> |
| 9 | #include "common/common_types.h" | 10 | #include "common/common_types.h" |
| 10 | #include "core/arm/arm_interface.h" | 11 | #include "core/arm/arm_interface.h" |
| 11 | #include "core/arm/skyeye_common/armstate.h" | 12 | #include "core/arm/skyeye_common/armstate.h" |
| 12 | 13 | ||
| 14 | namespace Memory { | ||
| 15 | struct PageTable; | ||
| 16 | } // namespace Memory | ||
| 17 | |||
| 13 | class ARM_Dynarmic final : public ARM_Interface { | 18 | class ARM_Dynarmic final : public ARM_Interface { |
| 14 | public: | 19 | public: |
| 15 | ARM_Dynarmic(PrivilegeMode initial_mode); | 20 | ARM_Dynarmic(PrivilegeMode initial_mode); |
| @@ -36,8 +41,11 @@ public: | |||
| 36 | void ExecuteInstructions(int num_instructions) override; | 41 | void ExecuteInstructions(int num_instructions) override; |
| 37 | 42 | ||
| 38 | void ClearInstructionCache() override; | 43 | void ClearInstructionCache() override; |
| 44 | void PageTableChanged() override; | ||
| 39 | 45 | ||
| 40 | private: | 46 | private: |
| 41 | std::unique_ptr<Dynarmic::Jit> jit; | 47 | Dynarmic::Jit* jit = nullptr; |
| 48 | Memory::PageTable* current_page_table = nullptr; | ||
| 49 | std::map<Memory::PageTable*, std::unique_ptr<Dynarmic::Jit>> jits; | ||
| 42 | std::shared_ptr<ARMul_State> interpreter_state; | 50 | std::shared_ptr<ARMul_State> interpreter_state; |
| 43 | }; | 51 | }; |
diff --git a/src/core/arm/dyncom/arm_dyncom.cpp b/src/core/arm/dyncom/arm_dyncom.cpp index 81f9bf99e..da955c9b9 100644 --- a/src/core/arm/dyncom/arm_dyncom.cpp +++ b/src/core/arm/dyncom/arm_dyncom.cpp | |||
| @@ -25,6 +25,10 @@ void ARM_DynCom::ClearInstructionCache() { | |||
| 25 | trans_cache_buf_top = 0; | 25 | trans_cache_buf_top = 0; |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | void ARM_DynCom::PageTableChanged() { | ||
| 29 | ClearInstructionCache(); | ||
| 30 | } | ||
| 31 | |||
| 28 | void ARM_DynCom::SetPC(u32 pc) { | 32 | void ARM_DynCom::SetPC(u32 pc) { |
| 29 | state->Reg[15] = pc; | 33 | state->Reg[15] = pc; |
| 30 | } | 34 | } |
diff --git a/src/core/arm/dyncom/arm_dyncom.h b/src/core/arm/dyncom/arm_dyncom.h index 62c174f3c..0ae535671 100644 --- a/src/core/arm/dyncom/arm_dyncom.h +++ b/src/core/arm/dyncom/arm_dyncom.h | |||
| @@ -16,6 +16,7 @@ public: | |||
| 16 | ~ARM_DynCom(); | 16 | ~ARM_DynCom(); |
| 17 | 17 | ||
| 18 | void ClearInstructionCache() override; | 18 | void ClearInstructionCache() override; |
| 19 | void PageTableChanged() override; | ||
| 19 | 20 | ||
| 20 | void SetPC(u32 pc) override; | 21 | void SetPC(u32 pc) override; |
| 21 | u32 GetPC() const override; | 22 | u32 GetPC() const override; |
diff --git a/src/core/file_sys/archive_selfncch.cpp b/src/core/file_sys/archive_selfncch.cpp index 298a37a44..a16941c70 100644 --- a/src/core/file_sys/archive_selfncch.cpp +++ b/src/core/file_sys/archive_selfncch.cpp | |||
| @@ -3,12 +3,14 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <array> | 5 | #include <array> |
| 6 | #include <cinttypes> | ||
| 6 | #include "common/common_types.h" | 7 | #include "common/common_types.h" |
| 7 | #include "common/logging/log.h" | 8 | #include "common/logging/log.h" |
| 8 | #include "common/swap.h" | 9 | #include "common/swap.h" |
| 9 | #include "core/file_sys/archive_selfncch.h" | 10 | #include "core/file_sys/archive_selfncch.h" |
| 10 | #include "core/file_sys/errors.h" | 11 | #include "core/file_sys/errors.h" |
| 11 | #include "core/file_sys/ivfc_archive.h" | 12 | #include "core/file_sys/ivfc_archive.h" |
| 13 | #include "core/hle/kernel/process.h" | ||
| 12 | 14 | ||
| 13 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 15 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 14 | // FileSys namespace | 16 | // FileSys namespace |
| @@ -102,8 +104,7 @@ public: | |||
| 102 | 104 | ||
| 103 | switch (static_cast<SelfNCCHFilePathType>(file_path.type)) { | 105 | switch (static_cast<SelfNCCHFilePathType>(file_path.type)) { |
| 104 | case SelfNCCHFilePathType::UpdateRomFS: | 106 | case SelfNCCHFilePathType::UpdateRomFS: |
| 105 | LOG_WARNING(Service_FS, "(STUBBED) open update RomFS"); | 107 | return OpenUpdateRomFS(); |
| 106 | return OpenRomFS(); | ||
| 107 | 108 | ||
| 108 | case SelfNCCHFilePathType::RomFS: | 109 | case SelfNCCHFilePathType::RomFS: |
| 109 | return OpenRomFS(); | 110 | return OpenRomFS(); |
| @@ -179,6 +180,17 @@ private: | |||
| 179 | } | 180 | } |
| 180 | } | 181 | } |
| 181 | 182 | ||
| 183 | ResultVal<std::unique_ptr<FileBackend>> OpenUpdateRomFS() const { | ||
| 184 | if (ncch_data.update_romfs_file) { | ||
| 185 | return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>( | ||
| 186 | ncch_data.update_romfs_file, ncch_data.update_romfs_offset, | ||
| 187 | ncch_data.update_romfs_size)); | ||
| 188 | } else { | ||
| 189 | LOG_INFO(Service_FS, "Unable to read update RomFS"); | ||
| 190 | return ERROR_ROMFS_NOT_FOUND; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 182 | ResultVal<std::unique_ptr<FileBackend>> OpenExeFS(const std::string& filename) const { | 194 | ResultVal<std::unique_ptr<FileBackend>> OpenExeFS(const std::string& filename) const { |
| 183 | if (filename == "icon") { | 195 | if (filename == "icon") { |
| 184 | if (ncch_data.icon) { | 196 | if (ncch_data.icon) { |
| @@ -217,30 +229,57 @@ private: | |||
| 217 | NCCHData ncch_data; | 229 | NCCHData ncch_data; |
| 218 | }; | 230 | }; |
| 219 | 231 | ||
| 220 | ArchiveFactory_SelfNCCH::ArchiveFactory_SelfNCCH(Loader::AppLoader& app_loader) { | 232 | void ArchiveFactory_SelfNCCH::Register(Loader::AppLoader& app_loader) { |
| 233 | u64 program_id = 0; | ||
| 234 | if (app_loader.ReadProgramId(program_id) != Loader::ResultStatus::Success) { | ||
| 235 | LOG_WARNING( | ||
| 236 | Service_FS, | ||
| 237 | "Could not read program id when registering with SelfNCCH, this might be a 3dsx file"); | ||
| 238 | } | ||
| 239 | |||
| 240 | LOG_DEBUG(Service_FS, "Registering program %016" PRIX64 " with the SelfNCCH archive factory", | ||
| 241 | program_id); | ||
| 242 | |||
| 243 | if (ncch_data.find(program_id) != ncch_data.end()) { | ||
| 244 | LOG_WARNING(Service_FS, "Registering program %016" PRIX64 | ||
| 245 | " with SelfNCCH will override existing mapping", | ||
| 246 | program_id); | ||
| 247 | } | ||
| 248 | |||
| 249 | NCCHData& data = ncch_data[program_id]; | ||
| 250 | |||
| 221 | std::shared_ptr<FileUtil::IOFile> romfs_file_; | 251 | std::shared_ptr<FileUtil::IOFile> romfs_file_; |
| 222 | if (Loader::ResultStatus::Success == | 252 | if (Loader::ResultStatus::Success == |
| 223 | app_loader.ReadRomFS(romfs_file_, ncch_data.romfs_offset, ncch_data.romfs_size)) { | 253 | app_loader.ReadRomFS(romfs_file_, data.romfs_offset, data.romfs_size)) { |
| 254 | |||
| 255 | data.romfs_file = std::move(romfs_file_); | ||
| 256 | } | ||
| 257 | |||
| 258 | std::shared_ptr<FileUtil::IOFile> update_romfs_file; | ||
| 259 | if (Loader::ResultStatus::Success == | ||
| 260 | app_loader.ReadUpdateRomFS(update_romfs_file, data.update_romfs_offset, | ||
| 261 | data.update_romfs_size)) { | ||
| 224 | 262 | ||
| 225 | ncch_data.romfs_file = std::move(romfs_file_); | 263 | data.update_romfs_file = std::move(update_romfs_file); |
| 226 | } | 264 | } |
| 227 | 265 | ||
| 228 | std::vector<u8> buffer; | 266 | std::vector<u8> buffer; |
| 229 | 267 | ||
| 230 | if (Loader::ResultStatus::Success == app_loader.ReadIcon(buffer)) | 268 | if (Loader::ResultStatus::Success == app_loader.ReadIcon(buffer)) |
| 231 | ncch_data.icon = std::make_shared<std::vector<u8>>(std::move(buffer)); | 269 | data.icon = std::make_shared<std::vector<u8>>(std::move(buffer)); |
| 232 | 270 | ||
| 233 | buffer.clear(); | 271 | buffer.clear(); |
| 234 | if (Loader::ResultStatus::Success == app_loader.ReadLogo(buffer)) | 272 | if (Loader::ResultStatus::Success == app_loader.ReadLogo(buffer)) |
| 235 | ncch_data.logo = std::make_shared<std::vector<u8>>(std::move(buffer)); | 273 | data.logo = std::make_shared<std::vector<u8>>(std::move(buffer)); |
| 236 | 274 | ||
| 237 | buffer.clear(); | 275 | buffer.clear(); |
| 238 | if (Loader::ResultStatus::Success == app_loader.ReadBanner(buffer)) | 276 | if (Loader::ResultStatus::Success == app_loader.ReadBanner(buffer)) |
| 239 | ncch_data.banner = std::make_shared<std::vector<u8>>(std::move(buffer)); | 277 | data.banner = std::make_shared<std::vector<u8>>(std::move(buffer)); |
| 240 | } | 278 | } |
| 241 | 279 | ||
| 242 | ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SelfNCCH::Open(const Path& path) { | 280 | ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SelfNCCH::Open(const Path& path) { |
| 243 | auto archive = std::make_unique<SelfNCCHArchive>(ncch_data); | 281 | auto archive = std::make_unique<SelfNCCHArchive>( |
| 282 | ncch_data[Kernel::g_current_process->codeset->program_id]); | ||
| 244 | return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); | 283 | return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); |
| 245 | } | 284 | } |
| 246 | 285 | ||
diff --git a/src/core/file_sys/archive_selfncch.h b/src/core/file_sys/archive_selfncch.h index f1b971296..0d6d6766e 100644 --- a/src/core/file_sys/archive_selfncch.h +++ b/src/core/file_sys/archive_selfncch.h | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include <memory> | 7 | #include <memory> |
| 8 | #include <string> | 8 | #include <string> |
| 9 | #include <unordered_map> | ||
| 9 | #include <vector> | 10 | #include <vector> |
| 10 | #include "common/common_types.h" | 11 | #include "common/common_types.h" |
| 11 | #include "core/file_sys/archive_backend.h" | 12 | #include "core/file_sys/archive_backend.h" |
| @@ -24,12 +25,19 @@ struct NCCHData { | |||
| 24 | std::shared_ptr<FileUtil::IOFile> romfs_file; | 25 | std::shared_ptr<FileUtil::IOFile> romfs_file; |
| 25 | u64 romfs_offset = 0; | 26 | u64 romfs_offset = 0; |
| 26 | u64 romfs_size = 0; | 27 | u64 romfs_size = 0; |
| 28 | |||
| 29 | std::shared_ptr<FileUtil::IOFile> update_romfs_file; | ||
| 30 | u64 update_romfs_offset = 0; | ||
| 31 | u64 update_romfs_size = 0; | ||
| 27 | }; | 32 | }; |
| 28 | 33 | ||
| 29 | /// File system interface to the SelfNCCH archive | 34 | /// File system interface to the SelfNCCH archive |
| 30 | class ArchiveFactory_SelfNCCH final : public ArchiveFactory { | 35 | class ArchiveFactory_SelfNCCH final : public ArchiveFactory { |
| 31 | public: | 36 | public: |
| 32 | explicit ArchiveFactory_SelfNCCH(Loader::AppLoader& app_loader); | 37 | ArchiveFactory_SelfNCCH() = default; |
| 38 | |||
| 39 | /// Registers a loaded application so that we can open its SelfNCCH archive when requested. | ||
| 40 | void Register(Loader::AppLoader& app_loader); | ||
| 33 | 41 | ||
| 34 | std::string GetName() const override { | 42 | std::string GetName() const override { |
| 35 | return "SelfNCCH"; | 43 | return "SelfNCCH"; |
| @@ -39,7 +47,8 @@ public: | |||
| 39 | ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override; | 47 | ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override; |
| 40 | 48 | ||
| 41 | private: | 49 | private: |
| 42 | NCCHData ncch_data; | 50 | /// Mapping of ProgramId -> NCCHData |
| 51 | std::unordered_map<u64, NCCHData> ncch_data; | ||
| 43 | }; | 52 | }; |
| 44 | 53 | ||
| 45 | } // namespace FileSys | 54 | } // namespace FileSys |
diff --git a/src/core/file_sys/ncch_container.cpp b/src/core/file_sys/ncch_container.cpp new file mode 100644 index 000000000..59c72f3e9 --- /dev/null +++ b/src/core/file_sys/ncch_container.cpp | |||
| @@ -0,0 +1,316 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cinttypes> | ||
| 6 | #include <cstring> | ||
| 7 | #include <memory> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "core/core.h" | ||
| 11 | #include "core/file_sys/ncch_container.h" | ||
| 12 | #include "core/loader/loader.h" | ||
| 13 | |||
| 14 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 15 | // FileSys namespace | ||
| 16 | |||
| 17 | namespace FileSys { | ||
| 18 | |||
| 19 | static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs | ||
| 20 | static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes) | ||
| 21 | |||
| 22 | /** | ||
| 23 | * Get the decompressed size of an LZSS compressed ExeFS file | ||
| 24 | * @param buffer Buffer of compressed file | ||
| 25 | * @param size Size of compressed buffer | ||
| 26 | * @return Size of decompressed buffer | ||
| 27 | */ | ||
| 28 | static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) { | ||
| 29 | u32 offset_size = *(u32*)(buffer + size - 4); | ||
| 30 | return offset_size + size; | ||
| 31 | } | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Decompress ExeFS file (compressed with LZSS) | ||
| 35 | * @param compressed Compressed buffer | ||
| 36 | * @param compressed_size Size of compressed buffer | ||
| 37 | * @param decompressed Decompressed buffer | ||
| 38 | * @param decompressed_size Size of decompressed buffer | ||
| 39 | * @return True on success, otherwise false | ||
| 40 | */ | ||
| 41 | static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed, | ||
| 42 | u32 decompressed_size) { | ||
| 43 | const u8* footer = compressed + compressed_size - 8; | ||
| 44 | u32 buffer_top_and_bottom = *reinterpret_cast<const u32*>(footer); | ||
| 45 | u32 out = decompressed_size; | ||
| 46 | u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF); | ||
| 47 | u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF); | ||
| 48 | |||
| 49 | memset(decompressed, 0, decompressed_size); | ||
| 50 | memcpy(decompressed, compressed, compressed_size); | ||
| 51 | |||
| 52 | while (index > stop_index) { | ||
| 53 | u8 control = compressed[--index]; | ||
| 54 | |||
| 55 | for (unsigned i = 0; i < 8; i++) { | ||
| 56 | if (index <= stop_index) | ||
| 57 | break; | ||
| 58 | if (index <= 0) | ||
| 59 | break; | ||
| 60 | if (out <= 0) | ||
| 61 | break; | ||
| 62 | |||
| 63 | if (control & 0x80) { | ||
| 64 | // Check if compression is out of bounds | ||
| 65 | if (index < 2) | ||
| 66 | return false; | ||
| 67 | index -= 2; | ||
| 68 | |||
| 69 | u32 segment_offset = compressed[index] | (compressed[index + 1] << 8); | ||
| 70 | u32 segment_size = ((segment_offset >> 12) & 15) + 3; | ||
| 71 | segment_offset &= 0x0FFF; | ||
| 72 | segment_offset += 2; | ||
| 73 | |||
| 74 | // Check if compression is out of bounds | ||
| 75 | if (out < segment_size) | ||
| 76 | return false; | ||
| 77 | |||
| 78 | for (unsigned j = 0; j < segment_size; j++) { | ||
| 79 | // Check if compression is out of bounds | ||
| 80 | if (out + segment_offset >= decompressed_size) | ||
| 81 | return false; | ||
| 82 | |||
| 83 | u8 data = decompressed[out + segment_offset]; | ||
| 84 | decompressed[--out] = data; | ||
| 85 | } | ||
| 86 | } else { | ||
| 87 | // Check if compression is out of bounds | ||
| 88 | if (out < 1) | ||
| 89 | return false; | ||
| 90 | decompressed[--out] = compressed[--index]; | ||
| 91 | } | ||
| 92 | control <<= 1; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | return true; | ||
| 96 | } | ||
| 97 | |||
| 98 | NCCHContainer::NCCHContainer(const std::string& filepath) : filepath(filepath) { | ||
| 99 | file = FileUtil::IOFile(filepath, "rb"); | ||
| 100 | } | ||
| 101 | |||
| 102 | Loader::ResultStatus NCCHContainer::OpenFile(const std::string& filepath) { | ||
| 103 | this->filepath = filepath; | ||
| 104 | file = FileUtil::IOFile(filepath, "rb"); | ||
| 105 | |||
| 106 | if (!file.IsOpen()) { | ||
| 107 | LOG_WARNING(Service_FS, "Failed to open %s", filepath.c_str()); | ||
| 108 | return Loader::ResultStatus::Error; | ||
| 109 | } | ||
| 110 | |||
| 111 | LOG_DEBUG(Service_FS, "Opened %s", filepath.c_str()); | ||
| 112 | return Loader::ResultStatus::Success; | ||
| 113 | } | ||
| 114 | |||
| 115 | Loader::ResultStatus NCCHContainer::Load() { | ||
| 116 | if (is_loaded) | ||
| 117 | return Loader::ResultStatus::Success; | ||
| 118 | |||
| 119 | // Reset read pointer in case this file has been read before. | ||
| 120 | file.Seek(0, SEEK_SET); | ||
| 121 | |||
| 122 | if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) | ||
| 123 | return Loader::ResultStatus::Error; | ||
| 124 | |||
| 125 | // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... | ||
| 126 | if (Loader::MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { | ||
| 127 | LOG_DEBUG(Service_FS, "Only loading the first (bootable) NCCH within the NCSD file!"); | ||
| 128 | ncch_offset = 0x4000; | ||
| 129 | file.Seek(ncch_offset, SEEK_SET); | ||
| 130 | file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); | ||
| 131 | } | ||
| 132 | |||
| 133 | // Verify we are loading the correct file type... | ||
| 134 | if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) | ||
| 135 | return Loader::ResultStatus::ErrorInvalidFormat; | ||
| 136 | |||
| 137 | // System archives and DLC don't have an extended header but have RomFS | ||
| 138 | if (ncch_header.extended_header_size) { | ||
| 139 | if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header)) | ||
| 140 | return Loader::ResultStatus::Error; | ||
| 141 | |||
| 142 | is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; | ||
| 143 | u32 entry_point = exheader_header.codeset_info.text.address; | ||
| 144 | u32 code_size = exheader_header.codeset_info.text.code_size; | ||
| 145 | u32 stack_size = exheader_header.codeset_info.stack_size; | ||
| 146 | u32 bss_size = exheader_header.codeset_info.bss_size; | ||
| 147 | u32 core_version = exheader_header.arm11_system_local_caps.core_version; | ||
| 148 | u8 priority = exheader_header.arm11_system_local_caps.priority; | ||
| 149 | u8 resource_limit_category = | ||
| 150 | exheader_header.arm11_system_local_caps.resource_limit_category; | ||
| 151 | |||
| 152 | LOG_DEBUG(Service_FS, "Name: %s", exheader_header.codeset_info.name); | ||
| 153 | LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64, ncch_header.program_id); | ||
| 154 | LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no"); | ||
| 155 | LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point); | ||
| 156 | LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size); | ||
| 157 | LOG_DEBUG(Service_FS, "Stack size: 0x%08X", stack_size); | ||
| 158 | LOG_DEBUG(Service_FS, "Bss size: 0x%08X", bss_size); | ||
| 159 | LOG_DEBUG(Service_FS, "Core version: %d", core_version); | ||
| 160 | LOG_DEBUG(Service_FS, "Thread priority: 0x%X", priority); | ||
| 161 | LOG_DEBUG(Service_FS, "Resource limit category: %d", resource_limit_category); | ||
| 162 | LOG_DEBUG(Service_FS, "System Mode: %d", | ||
| 163 | static_cast<int>(exheader_header.arm11_system_local_caps.system_mode)); | ||
| 164 | |||
| 165 | if (exheader_header.system_info.jump_id != ncch_header.program_id) { | ||
| 166 | LOG_ERROR(Service_FS, "ExHeader Program ID mismatch: the ROM is probably encrypted."); | ||
| 167 | return Loader::ResultStatus::ErrorEncrypted; | ||
| 168 | } | ||
| 169 | |||
| 170 | has_exheader = true; | ||
| 171 | } | ||
| 172 | |||
| 173 | // DLC can have an ExeFS and a RomFS but no extended header | ||
| 174 | if (ncch_header.exefs_size) { | ||
| 175 | exefs_offset = ncch_header.exefs_offset * kBlockSize; | ||
| 176 | u32 exefs_size = ncch_header.exefs_size * kBlockSize; | ||
| 177 | |||
| 178 | LOG_DEBUG(Service_FS, "ExeFS offset: 0x%08X", exefs_offset); | ||
| 179 | LOG_DEBUG(Service_FS, "ExeFS size: 0x%08X", exefs_size); | ||
| 180 | |||
| 181 | file.Seek(exefs_offset + ncch_offset, SEEK_SET); | ||
| 182 | if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) | ||
| 183 | return Loader::ResultStatus::Error; | ||
| 184 | |||
| 185 | has_exefs = true; | ||
| 186 | } | ||
| 187 | |||
| 188 | if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) | ||
| 189 | has_romfs = true; | ||
| 190 | |||
| 191 | is_loaded = true; | ||
| 192 | return Loader::ResultStatus::Success; | ||
| 193 | } | ||
| 194 | |||
| 195 | Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { | ||
| 196 | if (!file.IsOpen()) | ||
| 197 | return Loader::ResultStatus::Error; | ||
| 198 | |||
| 199 | Loader::ResultStatus result = Load(); | ||
| 200 | if (result != Loader::ResultStatus::Success) | ||
| 201 | return result; | ||
| 202 | |||
| 203 | if (!has_exefs) | ||
| 204 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 205 | |||
| 206 | LOG_DEBUG(Service_FS, "%d sections:", kMaxSections); | ||
| 207 | // Iterate through the ExeFs archive until we find a section with the specified name... | ||
| 208 | for (unsigned section_number = 0; section_number < kMaxSections; section_number++) { | ||
| 209 | const auto& section = exefs_header.section[section_number]; | ||
| 210 | |||
| 211 | // Load the specified section... | ||
| 212 | if (strcmp(section.name, name) == 0) { | ||
| 213 | LOG_DEBUG(Service_FS, "%d - offset: 0x%08X, size: 0x%08X, name: %s", section_number, | ||
| 214 | section.offset, section.size, section.name); | ||
| 215 | |||
| 216 | s64 section_offset = | ||
| 217 | (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); | ||
| 218 | file.Seek(section_offset, SEEK_SET); | ||
| 219 | |||
| 220 | if (strcmp(section.name, ".code") == 0 && is_compressed) { | ||
| 221 | // Section is compressed, read compressed .code section... | ||
| 222 | std::unique_ptr<u8[]> temp_buffer; | ||
| 223 | try { | ||
| 224 | temp_buffer.reset(new u8[section.size]); | ||
| 225 | } catch (std::bad_alloc&) { | ||
| 226 | return Loader::ResultStatus::ErrorMemoryAllocationFailed; | ||
| 227 | } | ||
| 228 | |||
| 229 | if (file.ReadBytes(&temp_buffer[0], section.size) != section.size) | ||
| 230 | return Loader::ResultStatus::Error; | ||
| 231 | |||
| 232 | // Decompress .code section... | ||
| 233 | u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size); | ||
| 234 | buffer.resize(decompressed_size); | ||
| 235 | if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size)) | ||
| 236 | return Loader::ResultStatus::ErrorInvalidFormat; | ||
| 237 | } else { | ||
| 238 | // Section is uncompressed... | ||
| 239 | buffer.resize(section.size); | ||
| 240 | if (file.ReadBytes(&buffer[0], section.size) != section.size) | ||
| 241 | return Loader::ResultStatus::Error; | ||
| 242 | } | ||
| 243 | return Loader::ResultStatus::Success; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 247 | } | ||
| 248 | |||
| 249 | Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, | ||
| 250 | u64& offset, u64& size) { | ||
| 251 | if (!file.IsOpen()) | ||
| 252 | return Loader::ResultStatus::Error; | ||
| 253 | |||
| 254 | Loader::ResultStatus result = Load(); | ||
| 255 | if (result != Loader::ResultStatus::Success) | ||
| 256 | return result; | ||
| 257 | |||
| 258 | if (!has_romfs) { | ||
| 259 | LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS"); | ||
| 260 | return Loader::ResultStatus::ErrorNotUsed; | ||
| 261 | } | ||
| 262 | |||
| 263 | u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; | ||
| 264 | u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; | ||
| 265 | |||
| 266 | LOG_DEBUG(Service_FS, "RomFS offset: 0x%08X", romfs_offset); | ||
| 267 | LOG_DEBUG(Service_FS, "RomFS size: 0x%08X", romfs_size); | ||
| 268 | |||
| 269 | if (file.GetSize() < romfs_offset + romfs_size) | ||
| 270 | return Loader::ResultStatus::Error; | ||
| 271 | |||
| 272 | // We reopen the file, to allow its position to be independent from file's | ||
| 273 | romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb"); | ||
| 274 | if (!romfs_file->IsOpen()) | ||
| 275 | return Loader::ResultStatus::Error; | ||
| 276 | |||
| 277 | offset = romfs_offset; | ||
| 278 | size = romfs_size; | ||
| 279 | |||
| 280 | return Loader::ResultStatus::Success; | ||
| 281 | } | ||
| 282 | |||
| 283 | Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) { | ||
| 284 | Loader::ResultStatus result = Load(); | ||
| 285 | if (result != Loader::ResultStatus::Success) | ||
| 286 | return result; | ||
| 287 | |||
| 288 | program_id = ncch_header.program_id; | ||
| 289 | return Loader::ResultStatus::Success; | ||
| 290 | } | ||
| 291 | |||
| 292 | bool NCCHContainer::HasExeFS() { | ||
| 293 | Loader::ResultStatus result = Load(); | ||
| 294 | if (result != Loader::ResultStatus::Success) | ||
| 295 | return false; | ||
| 296 | |||
| 297 | return has_exefs; | ||
| 298 | } | ||
| 299 | |||
| 300 | bool NCCHContainer::HasRomFS() { | ||
| 301 | Loader::ResultStatus result = Load(); | ||
| 302 | if (result != Loader::ResultStatus::Success) | ||
| 303 | return false; | ||
| 304 | |||
| 305 | return has_romfs; | ||
| 306 | } | ||
| 307 | |||
| 308 | bool NCCHContainer::HasExHeader() { | ||
| 309 | Loader::ResultStatus result = Load(); | ||
| 310 | if (result != Loader::ResultStatus::Success) | ||
| 311 | return false; | ||
| 312 | |||
| 313 | return has_exheader; | ||
| 314 | } | ||
| 315 | |||
| 316 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/ncch_container.h b/src/core/file_sys/ncch_container.h new file mode 100644 index 000000000..8af9032b4 --- /dev/null +++ b/src/core/file_sys/ncch_container.h | |||
| @@ -0,0 +1,244 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstddef> | ||
| 8 | #include <memory> | ||
| 9 | #include <string> | ||
| 10 | #include <vector> | ||
| 11 | #include "common/bit_field.h" | ||
| 12 | #include "common/common_types.h" | ||
| 13 | #include "common/file_util.h" | ||
| 14 | #include "common/swap.h" | ||
| 15 | #include "core/core.h" | ||
| 16 | |||
| 17 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 18 | /// NCCH header (Note: "NCCH" appears to be a publicly unknown acronym) | ||
| 19 | |||
| 20 | struct NCCH_Header { | ||
| 21 | u8 signature[0x100]; | ||
| 22 | u32_le magic; | ||
| 23 | u32_le content_size; | ||
| 24 | u8 partition_id[8]; | ||
| 25 | u16_le maker_code; | ||
| 26 | u16_le version; | ||
| 27 | u8 reserved_0[4]; | ||
| 28 | u64_le program_id; | ||
| 29 | u8 reserved_1[0x10]; | ||
| 30 | u8 logo_region_hash[0x20]; | ||
| 31 | u8 product_code[0x10]; | ||
| 32 | u8 extended_header_hash[0x20]; | ||
| 33 | u32_le extended_header_size; | ||
| 34 | u8 reserved_2[4]; | ||
| 35 | u8 flags[8]; | ||
| 36 | u32_le plain_region_offset; | ||
| 37 | u32_le plain_region_size; | ||
| 38 | u32_le logo_region_offset; | ||
| 39 | u32_le logo_region_size; | ||
| 40 | u32_le exefs_offset; | ||
| 41 | u32_le exefs_size; | ||
| 42 | u32_le exefs_hash_region_size; | ||
| 43 | u8 reserved_3[4]; | ||
| 44 | u32_le romfs_offset; | ||
| 45 | u32_le romfs_size; | ||
| 46 | u32_le romfs_hash_region_size; | ||
| 47 | u8 reserved_4[4]; | ||
| 48 | u8 exefs_super_block_hash[0x20]; | ||
| 49 | u8 romfs_super_block_hash[0x20]; | ||
| 50 | }; | ||
| 51 | |||
| 52 | static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong"); | ||
| 53 | |||
| 54 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 55 | // ExeFS (executable file system) headers | ||
| 56 | |||
| 57 | struct ExeFs_SectionHeader { | ||
| 58 | char name[8]; | ||
| 59 | u32 offset; | ||
| 60 | u32 size; | ||
| 61 | }; | ||
| 62 | |||
| 63 | struct ExeFs_Header { | ||
| 64 | ExeFs_SectionHeader section[8]; | ||
| 65 | u8 reserved[0x80]; | ||
| 66 | u8 hashes[8][0x20]; | ||
| 67 | }; | ||
| 68 | |||
| 69 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 70 | // ExHeader (executable file system header) headers | ||
| 71 | |||
| 72 | struct ExHeader_SystemInfoFlags { | ||
| 73 | u8 reserved[5]; | ||
| 74 | u8 flag; | ||
| 75 | u8 remaster_version[2]; | ||
| 76 | }; | ||
| 77 | |||
| 78 | struct ExHeader_CodeSegmentInfo { | ||
| 79 | u32 address; | ||
| 80 | u32 num_max_pages; | ||
| 81 | u32 code_size; | ||
| 82 | }; | ||
| 83 | |||
| 84 | struct ExHeader_CodeSetInfo { | ||
| 85 | u8 name[8]; | ||
| 86 | ExHeader_SystemInfoFlags flags; | ||
| 87 | ExHeader_CodeSegmentInfo text; | ||
| 88 | u32 stack_size; | ||
| 89 | ExHeader_CodeSegmentInfo ro; | ||
| 90 | u8 reserved[4]; | ||
| 91 | ExHeader_CodeSegmentInfo data; | ||
| 92 | u32 bss_size; | ||
| 93 | }; | ||
| 94 | |||
| 95 | struct ExHeader_DependencyList { | ||
| 96 | u8 program_id[0x30][8]; | ||
| 97 | }; | ||
| 98 | |||
| 99 | struct ExHeader_SystemInfo { | ||
| 100 | u64 save_data_size; | ||
| 101 | u64_le jump_id; | ||
| 102 | u8 reserved_2[0x30]; | ||
| 103 | }; | ||
| 104 | |||
| 105 | struct ExHeader_StorageInfo { | ||
| 106 | u8 ext_save_data_id[8]; | ||
| 107 | u8 system_save_data_id[8]; | ||
| 108 | u8 reserved[8]; | ||
| 109 | u8 access_info[7]; | ||
| 110 | u8 other_attributes; | ||
| 111 | }; | ||
| 112 | |||
| 113 | struct ExHeader_ARM11_SystemLocalCaps { | ||
| 114 | u64_le program_id; | ||
| 115 | u32_le core_version; | ||
| 116 | u8 reserved_flags[2]; | ||
| 117 | union { | ||
| 118 | u8 flags0; | ||
| 119 | BitField<0, 2, u8> ideal_processor; | ||
| 120 | BitField<2, 2, u8> affinity_mask; | ||
| 121 | BitField<4, 4, u8> system_mode; | ||
| 122 | }; | ||
| 123 | u8 priority; | ||
| 124 | u8 resource_limit_descriptor[0x10][2]; | ||
| 125 | ExHeader_StorageInfo storage_info; | ||
| 126 | u8 service_access_control[0x20][8]; | ||
| 127 | u8 ex_service_access_control[0x2][8]; | ||
| 128 | u8 reserved[0xf]; | ||
| 129 | u8 resource_limit_category; | ||
| 130 | }; | ||
| 131 | |||
| 132 | struct ExHeader_ARM11_KernelCaps { | ||
| 133 | u32_le descriptors[28]; | ||
| 134 | u8 reserved[0x10]; | ||
| 135 | }; | ||
| 136 | |||
| 137 | struct ExHeader_ARM9_AccessControl { | ||
| 138 | u8 descriptors[15]; | ||
| 139 | u8 descversion; | ||
| 140 | }; | ||
| 141 | |||
| 142 | struct ExHeader_Header { | ||
| 143 | ExHeader_CodeSetInfo codeset_info; | ||
| 144 | ExHeader_DependencyList dependency_list; | ||
| 145 | ExHeader_SystemInfo system_info; | ||
| 146 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 147 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 148 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 149 | struct { | ||
| 150 | u8 signature[0x100]; | ||
| 151 | u8 ncch_public_key_modulus[0x100]; | ||
| 152 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 153 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 154 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 155 | } access_desc; | ||
| 156 | }; | ||
| 157 | |||
| 158 | static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong"); | ||
| 159 | |||
| 160 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 161 | // FileSys namespace | ||
| 162 | |||
| 163 | namespace FileSys { | ||
| 164 | |||
| 165 | /** | ||
| 166 | * Helper which implements an interface to deal with NCCH containers which can | ||
| 167 | * contain ExeFS archives or RomFS archives for games or other applications. | ||
| 168 | */ | ||
| 169 | class NCCHContainer { | ||
| 170 | public: | ||
| 171 | NCCHContainer(const std::string& filepath); | ||
| 172 | NCCHContainer() {} | ||
| 173 | |||
| 174 | Loader::ResultStatus OpenFile(const std::string& filepath); | ||
| 175 | |||
| 176 | /** | ||
| 177 | * Ensure ExeFS and exheader is loaded and ready for reading sections | ||
| 178 | * @return ResultStatus result of function | ||
| 179 | */ | ||
| 180 | Loader::ResultStatus Load(); | ||
| 181 | |||
| 182 | /** | ||
| 183 | * Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.) | ||
| 184 | * @param name Name of section to read out of NCCH file | ||
| 185 | * @param buffer Vector to read data into | ||
| 186 | * @return ResultStatus result of function | ||
| 187 | */ | ||
| 188 | Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer); | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Get the RomFS of the NCCH container | ||
| 192 | * Since the RomFS can be huge, we return a file reference instead of copying to a buffer | ||
| 193 | * @param romfs_file The file containing the RomFS | ||
| 194 | * @param offset The offset the romfs begins on | ||
| 195 | * @param size The size of the romfs | ||
| 196 | * @return ResultStatus result of function | ||
| 197 | */ | ||
| 198 | Loader::ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 199 | u64& size); | ||
| 200 | |||
| 201 | /** | ||
| 202 | * Get the Program ID of the NCCH container | ||
| 203 | * @return ResultStatus result of function | ||
| 204 | */ | ||
| 205 | Loader::ResultStatus ReadProgramId(u64_le& program_id); | ||
| 206 | |||
| 207 | /** | ||
| 208 | * Checks whether the NCCH container contains an ExeFS | ||
| 209 | * @return bool check result | ||
| 210 | */ | ||
| 211 | bool HasExeFS(); | ||
| 212 | |||
| 213 | /** | ||
| 214 | * Checks whether the NCCH container contains a RomFS | ||
| 215 | * @return bool check result | ||
| 216 | */ | ||
| 217 | bool HasRomFS(); | ||
| 218 | |||
| 219 | /** | ||
| 220 | * Checks whether the NCCH container contains an ExHeader | ||
| 221 | * @return bool check result | ||
| 222 | */ | ||
| 223 | bool HasExHeader(); | ||
| 224 | |||
| 225 | NCCH_Header ncch_header; | ||
| 226 | ExeFs_Header exefs_header; | ||
| 227 | ExHeader_Header exheader_header; | ||
| 228 | |||
| 229 | private: | ||
| 230 | bool has_exheader = false; | ||
| 231 | bool has_exefs = false; | ||
| 232 | bool has_romfs = false; | ||
| 233 | |||
| 234 | bool is_loaded = false; | ||
| 235 | bool is_compressed = false; | ||
| 236 | |||
| 237 | u32 ncch_offset = 0; // Offset to NCCH header, can be 0 or after NCSD header | ||
| 238 | u32 exefs_offset = 0; | ||
| 239 | |||
| 240 | std::string filepath; | ||
| 241 | FileUtil::IOFile file; | ||
| 242 | }; | ||
| 243 | |||
| 244 | } // namespace FileSys | ||
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 324415a36..61378211f 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp | |||
| @@ -178,16 +178,13 @@ static void SwitchContext(Thread* new_thread) { | |||
| 178 | ready_queue.remove(new_thread->current_priority, new_thread); | 178 | ready_queue.remove(new_thread->current_priority, new_thread); |
| 179 | new_thread->status = THREADSTATUS_RUNNING; | 179 | new_thread->status = THREADSTATUS_RUNNING; |
| 180 | 180 | ||
| 181 | Core::CPU().LoadContext(new_thread->context); | ||
| 182 | Core::CPU().SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress()); | ||
| 183 | |||
| 184 | if (previous_process != current_thread->owner_process) { | 181 | if (previous_process != current_thread->owner_process) { |
| 185 | Kernel::g_current_process = current_thread->owner_process; | 182 | Kernel::g_current_process = current_thread->owner_process; |
| 186 | Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; | 183 | SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); |
| 187 | // We have switched processes and thus, page tables, clear the instruction cache so we | ||
| 188 | // don't keep stale data from the previous process. | ||
| 189 | Core::CPU().ClearInstructionCache(); | ||
| 190 | } | 184 | } |
| 185 | |||
| 186 | Core::CPU().LoadContext(new_thread->context); | ||
| 187 | Core::CPU().SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress()); | ||
| 191 | } else { | 188 | } else { |
| 192 | current_thread = nullptr; | 189 | current_thread = nullptr; |
| 193 | // Note: We do not reset the current process and current page table when idling because | 190 | // Note: We do not reset the current process and current page table when idling because |
diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp index 033fbc9aa..4ccb3cd32 100644 --- a/src/core/hle/service/fs/archive.cpp +++ b/src/core/hle/service/fs/archive.cpp | |||
| @@ -20,6 +20,7 @@ | |||
| 20 | #include "core/file_sys/archive_savedata.h" | 20 | #include "core/file_sys/archive_savedata.h" |
| 21 | #include "core/file_sys/archive_sdmc.h" | 21 | #include "core/file_sys/archive_sdmc.h" |
| 22 | #include "core/file_sys/archive_sdmcwriteonly.h" | 22 | #include "core/file_sys/archive_sdmcwriteonly.h" |
| 23 | #include "core/file_sys/archive_selfncch.h" | ||
| 23 | #include "core/file_sys/archive_systemsavedata.h" | 24 | #include "core/file_sys/archive_systemsavedata.h" |
| 24 | #include "core/file_sys/directory_backend.h" | 25 | #include "core/file_sys/directory_backend.h" |
| 25 | #include "core/file_sys/errors.h" | 26 | #include "core/file_sys/errors.h" |
| @@ -48,7 +49,7 @@ struct hash<Service::FS::ArchiveIdCode> { | |||
| 48 | return std::hash<Type>()(static_cast<Type>(id_code)); | 49 | return std::hash<Type>()(static_cast<Type>(id_code)); |
| 49 | } | 50 | } |
| 50 | }; | 51 | }; |
| 51 | } | 52 | } // namespace std |
| 52 | 53 | ||
| 53 | static constexpr Kernel::Handle INVALID_HANDLE{}; | 54 | static constexpr Kernel::Handle INVALID_HANDLE{}; |
| 54 | 55 | ||
| @@ -564,6 +565,21 @@ void RegisterArchiveTypes() { | |||
| 564 | auto systemsavedata_factory = | 565 | auto systemsavedata_factory = |
| 565 | std::make_unique<FileSys::ArchiveFactory_SystemSaveData>(nand_directory); | 566 | std::make_unique<FileSys::ArchiveFactory_SystemSaveData>(nand_directory); |
| 566 | RegisterArchiveType(std::move(systemsavedata_factory), ArchiveIdCode::SystemSaveData); | 567 | RegisterArchiveType(std::move(systemsavedata_factory), ArchiveIdCode::SystemSaveData); |
| 568 | |||
| 569 | auto selfncch_factory = std::make_unique<FileSys::ArchiveFactory_SelfNCCH>(); | ||
| 570 | RegisterArchiveType(std::move(selfncch_factory), ArchiveIdCode::SelfNCCH); | ||
| 571 | } | ||
| 572 | |||
| 573 | void RegisterSelfNCCH(Loader::AppLoader& app_loader) { | ||
| 574 | auto itr = id_code_map.find(ArchiveIdCode::SelfNCCH); | ||
| 575 | if (itr == id_code_map.end()) { | ||
| 576 | LOG_ERROR(Service_FS, | ||
| 577 | "Could not register a new NCCH because the SelfNCCH archive hasn't been created"); | ||
| 578 | return; | ||
| 579 | } | ||
| 580 | |||
| 581 | auto* factory = static_cast<FileSys::ArchiveFactory_SelfNCCH*>(itr->second.get()); | ||
| 582 | factory->Register(app_loader); | ||
| 567 | } | 583 | } |
| 568 | 584 | ||
| 569 | void UnregisterArchiveTypes() { | 585 | void UnregisterArchiveTypes() { |
diff --git a/src/core/hle/service/fs/archive.h b/src/core/hle/service/fs/archive.h index 3a3371c88..e3c8fc2ef 100644 --- a/src/core/hle/service/fs/archive.h +++ b/src/core/hle/service/fs/archive.h | |||
| @@ -21,6 +21,10 @@ static constexpr char SYSTEM_ID[]{"00000000000000000000000000000000"}; | |||
| 21 | /// The scrambled SD card CID, also known as ID1 | 21 | /// The scrambled SD card CID, also known as ID1 |
| 22 | static constexpr char SDCARD_ID[]{"00000000000000000000000000000000"}; | 22 | static constexpr char SDCARD_ID[]{"00000000000000000000000000000000"}; |
| 23 | 23 | ||
| 24 | namespace Loader { | ||
| 25 | class AppLoader; | ||
| 26 | } | ||
| 27 | |||
| 24 | namespace Service { | 28 | namespace Service { |
| 25 | namespace FS { | 29 | namespace FS { |
| 26 | 30 | ||
| @@ -259,6 +263,9 @@ void ArchiveInit(); | |||
| 259 | /// Shutdown archives | 263 | /// Shutdown archives |
| 260 | void ArchiveShutdown(); | 264 | void ArchiveShutdown(); |
| 261 | 265 | ||
| 266 | /// Registers a new NCCH file with the SelfNCCH archive factory | ||
| 267 | void RegisterSelfNCCH(Loader::AppLoader& app_loader); | ||
| 268 | |||
| 262 | /// Register all archive types | 269 | /// Register all archive types |
| 263 | void RegisterArchiveTypes(); | 270 | void RegisterArchiveTypes(); |
| 264 | 271 | ||
diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp index 893bbb1e7..4e2af9ae6 100644 --- a/src/core/hle/service/nwm/nwm_uds.cpp +++ b/src/core/hle/service/nwm/nwm_uds.cpp | |||
| @@ -2,8 +2,10 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 5 | #include <array> | 6 | #include <array> |
| 6 | #include <cstring> | 7 | #include <cstring> |
| 8 | #include <list> | ||
| 7 | #include <mutex> | 9 | #include <mutex> |
| 8 | #include <unordered_map> | 10 | #include <unordered_map> |
| 9 | #include <vector> | 11 | #include <vector> |
| @@ -37,9 +39,12 @@ static ConnectionStatus connection_status{}; | |||
| 37 | /* Node information about the current network. | 39 | /* Node information about the current network. |
| 38 | * The amount of elements in this vector is always the maximum number | 40 | * The amount of elements in this vector is always the maximum number |
| 39 | * of nodes specified in the network configuration. | 41 | * of nodes specified in the network configuration. |
| 40 | * The first node is always the host, so this always contains at least 1 entry. | 42 | * The first node is always the host. |
| 41 | */ | 43 | */ |
| 42 | static NodeList node_info(1); | 44 | static NodeList node_info; |
| 45 | |||
| 46 | // Node information about our own system. | ||
| 47 | static NodeInfo current_node; | ||
| 43 | 48 | ||
| 44 | // Mapping of bind node ids to their respective events. | 49 | // Mapping of bind node ids to their respective events. |
| 45 | static std::unordered_map<u32, Kernel::SharedPtr<Kernel::Event>> bind_node_events; | 50 | static std::unordered_map<u32, Kernel::SharedPtr<Kernel::Event>> bind_node_events; |
| @@ -54,6 +59,10 @@ static NetworkInfo network_info; | |||
| 54 | // Event that will generate and send the 802.11 beacon frames. | 59 | // Event that will generate and send the 802.11 beacon frames. |
| 55 | static int beacon_broadcast_event; | 60 | static int beacon_broadcast_event; |
| 56 | 61 | ||
| 62 | // Mutex to synchronize access to the connection status between the emulation thread and the | ||
| 63 | // network thread. | ||
| 64 | static std::mutex connection_status_mutex; | ||
| 65 | |||
| 57 | // Mutex to synchronize access to the list of received beacons between the emulation thread and the | 66 | // Mutex to synchronize access to the list of received beacons between the emulation thread and the |
| 58 | // network thread. | 67 | // network thread. |
| 59 | static std::mutex beacon_mutex; | 68 | static std::mutex beacon_mutex; |
| @@ -63,14 +72,26 @@ static std::mutex beacon_mutex; | |||
| 63 | constexpr size_t MaxBeaconFrames = 15; | 72 | constexpr size_t MaxBeaconFrames = 15; |
| 64 | 73 | ||
| 65 | // List of the last <MaxBeaconFrames> beacons received from the network. | 74 | // List of the last <MaxBeaconFrames> beacons received from the network. |
| 66 | static std::deque<Network::WifiPacket> received_beacons; | 75 | static std::list<Network::WifiPacket> received_beacons; |
| 67 | 76 | ||
| 68 | /** | 77 | /** |
| 69 | * Returns a list of received 802.11 beacon frames from the specified sender since the last call. | 78 | * Returns a list of received 802.11 beacon frames from the specified sender since the last call. |
| 70 | */ | 79 | */ |
| 71 | std::deque<Network::WifiPacket> GetReceivedBeacons(const MacAddress& sender) { | 80 | std::list<Network::WifiPacket> GetReceivedBeacons(const MacAddress& sender) { |
| 72 | std::lock_guard<std::mutex> lock(beacon_mutex); | 81 | std::lock_guard<std::mutex> lock(beacon_mutex); |
| 73 | // TODO(Subv): Filter by sender. | 82 | if (sender != Network::BroadcastMac) { |
| 83 | std::list<Network::WifiPacket> filtered_list; | ||
| 84 | const auto beacon = std::find_if(received_beacons.begin(), received_beacons.end(), | ||
| 85 | [&sender](const Network::WifiPacket& packet) { | ||
| 86 | return packet.transmitter_address == sender; | ||
| 87 | }); | ||
| 88 | if (beacon != received_beacons.end()) { | ||
| 89 | filtered_list.push_back(*beacon); | ||
| 90 | // TODO(B3N30): Check if the complete deque is cleared or just the fetched entries | ||
| 91 | received_beacons.erase(beacon); | ||
| 92 | } | ||
| 93 | return filtered_list; | ||
| 94 | } | ||
| 74 | return std::move(received_beacons); | 95 | return std::move(received_beacons); |
| 75 | } | 96 | } |
| 76 | 97 | ||
| @@ -83,6 +104,15 @@ void SendPacket(Network::WifiPacket& packet) { | |||
| 83 | // limit is exceeded. | 104 | // limit is exceeded. |
| 84 | void HandleBeaconFrame(const Network::WifiPacket& packet) { | 105 | void HandleBeaconFrame(const Network::WifiPacket& packet) { |
| 85 | std::lock_guard<std::mutex> lock(beacon_mutex); | 106 | std::lock_guard<std::mutex> lock(beacon_mutex); |
| 107 | const auto unique_beacon = | ||
| 108 | std::find_if(received_beacons.begin(), received_beacons.end(), | ||
| 109 | [&packet](const Network::WifiPacket& new_packet) { | ||
| 110 | return new_packet.transmitter_address == packet.transmitter_address; | ||
| 111 | }); | ||
| 112 | if (unique_beacon != received_beacons.end()) { | ||
| 113 | // We already have a beacon from the same mac in the deque, remove the old one; | ||
| 114 | received_beacons.erase(unique_beacon); | ||
| 115 | } | ||
| 86 | 116 | ||
| 87 | received_beacons.emplace_back(packet); | 117 | received_beacons.emplace_back(packet); |
| 88 | 118 | ||
| @@ -91,14 +121,33 @@ void HandleBeaconFrame(const Network::WifiPacket& packet) { | |||
| 91 | received_beacons.pop_front(); | 121 | received_beacons.pop_front(); |
| 92 | } | 122 | } |
| 93 | 123 | ||
| 124 | void HandleAssociationResponseFrame(const Network::WifiPacket& packet) { | ||
| 125 | auto assoc_result = GetAssociationResult(packet.data); | ||
| 126 | |||
| 127 | ASSERT_MSG(std::get<AssocStatus>(assoc_result) == AssocStatus::Successful, | ||
| 128 | "Could not join network"); | ||
| 129 | { | ||
| 130 | std::lock_guard<std::mutex> lock(connection_status_mutex); | ||
| 131 | ASSERT(connection_status.status == static_cast<u32>(NetworkStatus::Connecting)); | ||
| 132 | } | ||
| 133 | |||
| 134 | // Send the EAPoL-Start packet to the server. | ||
| 135 | using Network::WifiPacket; | ||
| 136 | WifiPacket eapol_start; | ||
| 137 | eapol_start.channel = network_channel; | ||
| 138 | eapol_start.data = GenerateEAPoLStartFrame(std::get<u16>(assoc_result), current_node); | ||
| 139 | // TODO(B3N30): Encrypt the packet. | ||
| 140 | eapol_start.destination_address = packet.transmitter_address; | ||
| 141 | eapol_start.type = WifiPacket::PacketType::Data; | ||
| 142 | |||
| 143 | SendPacket(eapol_start); | ||
| 144 | } | ||
| 145 | |||
| 94 | /* | 146 | /* |
| 95 | * Returns an available index in the nodes array for the | 147 | * Returns an available index in the nodes array for the |
| 96 | * currently-hosted UDS network. | 148 | * currently-hosted UDS network. |
| 97 | */ | 149 | */ |
| 98 | static u16 GetNextAvailableNodeId() { | 150 | static u16 GetNextAvailableNodeId() { |
| 99 | ASSERT_MSG(connection_status.status == static_cast<u32>(NetworkStatus::ConnectedAsHost), | ||
| 100 | "Can not accept clients if we're not hosting a network"); | ||
| 101 | |||
| 102 | for (u16 index = 0; index < connection_status.max_nodes; ++index) { | 151 | for (u16 index = 0; index < connection_status.max_nodes; ++index) { |
| 103 | if ((connection_status.node_bitmask & (1 << index)) == 0) | 152 | if ((connection_status.node_bitmask & (1 << index)) == 0) |
| 104 | return index; | 153 | return index; |
| @@ -113,35 +162,46 @@ static u16 GetNextAvailableNodeId() { | |||
| 113 | * authentication frame with SEQ1. | 162 | * authentication frame with SEQ1. |
| 114 | */ | 163 | */ |
| 115 | void StartConnectionSequence(const MacAddress& server) { | 164 | void StartConnectionSequence(const MacAddress& server) { |
| 116 | ASSERT(connection_status.status == static_cast<u32>(NetworkStatus::NotConnected)); | ||
| 117 | |||
| 118 | // TODO(Subv): Handle timeout. | ||
| 119 | |||
| 120 | // Send an authentication frame with SEQ1 | ||
| 121 | using Network::WifiPacket; | 165 | using Network::WifiPacket; |
| 122 | WifiPacket auth_request; | 166 | WifiPacket auth_request; |
| 123 | auth_request.channel = network_channel; | 167 | { |
| 124 | auth_request.data = GenerateAuthenticationFrame(AuthenticationSeq::SEQ1); | 168 | std::lock_guard<std::mutex> lock(connection_status_mutex); |
| 125 | auth_request.destination_address = server; | 169 | ASSERT(connection_status.status == static_cast<u32>(NetworkStatus::NotConnected)); |
| 126 | auth_request.type = WifiPacket::PacketType::Authentication; | 170 | |
| 171 | // TODO(Subv): Handle timeout. | ||
| 172 | |||
| 173 | // Send an authentication frame with SEQ1 | ||
| 174 | auth_request.channel = network_channel; | ||
| 175 | auth_request.data = GenerateAuthenticationFrame(AuthenticationSeq::SEQ1); | ||
| 176 | auth_request.destination_address = server; | ||
| 177 | auth_request.type = WifiPacket::PacketType::Authentication; | ||
| 178 | } | ||
| 127 | 179 | ||
| 128 | SendPacket(auth_request); | 180 | SendPacket(auth_request); |
| 129 | } | 181 | } |
| 130 | 182 | ||
| 131 | /// Sends an Association Response frame to the specified mac address | 183 | /// Sends an Association Response frame to the specified mac address |
| 132 | void SendAssociationResponseFrame(const MacAddress& address) { | 184 | void SendAssociationResponseFrame(const MacAddress& address) { |
| 133 | ASSERT_MSG(connection_status.status == static_cast<u32>(NetworkStatus::ConnectedAsHost)); | ||
| 134 | |||
| 135 | using Network::WifiPacket; | 185 | using Network::WifiPacket; |
| 136 | WifiPacket assoc_response; | 186 | WifiPacket assoc_response; |
| 137 | assoc_response.channel = network_channel; | 187 | |
| 138 | // TODO(Subv): This will cause multiple clients to end up with the same association id, but | 188 | { |
| 139 | // we're not using that for anything. | 189 | std::lock_guard<std::mutex> lock(connection_status_mutex); |
| 140 | u16 association_id = 1; | 190 | if (connection_status.status != static_cast<u32>(NetworkStatus::ConnectedAsHost)) { |
| 141 | assoc_response.data = GenerateAssocResponseFrame(AssocStatus::Successful, association_id, | 191 | LOG_ERROR(Service_NWM, "Connection sequence aborted, because connection status is %u", |
| 142 | network_info.network_id); | 192 | connection_status.status); |
| 143 | assoc_response.destination_address = address; | 193 | return; |
| 144 | assoc_response.type = WifiPacket::PacketType::AssociationResponse; | 194 | } |
| 195 | |||
| 196 | assoc_response.channel = network_channel; | ||
| 197 | // TODO(Subv): This will cause multiple clients to end up with the same association id, but | ||
| 198 | // we're not using that for anything. | ||
| 199 | u16 association_id = 1; | ||
| 200 | assoc_response.data = GenerateAssocResponseFrame(AssocStatus::Successful, association_id, | ||
| 201 | network_info.network_id); | ||
| 202 | assoc_response.destination_address = address; | ||
| 203 | assoc_response.type = WifiPacket::PacketType::AssociationResponse; | ||
| 204 | } | ||
| 145 | 205 | ||
| 146 | SendPacket(assoc_response); | 206 | SendPacket(assoc_response); |
| 147 | } | 207 | } |
| @@ -155,16 +215,23 @@ void SendAssociationResponseFrame(const MacAddress& address) { | |||
| 155 | void HandleAuthenticationFrame(const Network::WifiPacket& packet) { | 215 | void HandleAuthenticationFrame(const Network::WifiPacket& packet) { |
| 156 | // Only the SEQ1 auth frame is handled here, the SEQ2 frame doesn't need any special behavior | 216 | // Only the SEQ1 auth frame is handled here, the SEQ2 frame doesn't need any special behavior |
| 157 | if (GetAuthenticationSeqNumber(packet.data) == AuthenticationSeq::SEQ1) { | 217 | if (GetAuthenticationSeqNumber(packet.data) == AuthenticationSeq::SEQ1) { |
| 158 | ASSERT_MSG(connection_status.status == static_cast<u32>(NetworkStatus::ConnectedAsHost)); | ||
| 159 | |||
| 160 | // Respond with an authentication response frame with SEQ2 | ||
| 161 | using Network::WifiPacket; | 218 | using Network::WifiPacket; |
| 162 | WifiPacket auth_request; | 219 | WifiPacket auth_request; |
| 163 | auth_request.channel = network_channel; | 220 | { |
| 164 | auth_request.data = GenerateAuthenticationFrame(AuthenticationSeq::SEQ2); | 221 | std::lock_guard<std::mutex> lock(connection_status_mutex); |
| 165 | auth_request.destination_address = packet.transmitter_address; | 222 | if (connection_status.status != static_cast<u32>(NetworkStatus::ConnectedAsHost)) { |
| 166 | auth_request.type = WifiPacket::PacketType::Authentication; | 223 | LOG_ERROR(Service_NWM, |
| 167 | 224 | "Connection sequence aborted, because connection status is %u", | |
| 225 | connection_status.status); | ||
| 226 | return; | ||
| 227 | } | ||
| 228 | |||
| 229 | // Respond with an authentication response frame with SEQ2 | ||
| 230 | auth_request.channel = network_channel; | ||
| 231 | auth_request.data = GenerateAuthenticationFrame(AuthenticationSeq::SEQ2); | ||
| 232 | auth_request.destination_address = packet.transmitter_address; | ||
| 233 | auth_request.type = WifiPacket::PacketType::Authentication; | ||
| 234 | } | ||
| 168 | SendPacket(auth_request); | 235 | SendPacket(auth_request); |
| 169 | 236 | ||
| 170 | SendAssociationResponseFrame(packet.transmitter_address); | 237 | SendAssociationResponseFrame(packet.transmitter_address); |
| @@ -180,6 +247,9 @@ void OnWifiPacketReceived(const Network::WifiPacket& packet) { | |||
| 180 | case Network::WifiPacket::PacketType::Authentication: | 247 | case Network::WifiPacket::PacketType::Authentication: |
| 181 | HandleAuthenticationFrame(packet); | 248 | HandleAuthenticationFrame(packet); |
| 182 | break; | 249 | break; |
| 250 | case Network::WifiPacket::PacketType::AssociationResponse: | ||
| 251 | HandleAssociationResponseFrame(packet); | ||
| 252 | break; | ||
| 183 | } | 253 | } |
| 184 | } | 254 | } |
| 185 | 255 | ||
| @@ -305,7 +375,7 @@ static void InitializeWithVersion(Interface* self) { | |||
| 305 | u32 sharedmem_size = rp.Pop<u32>(); | 375 | u32 sharedmem_size = rp.Pop<u32>(); |
| 306 | 376 | ||
| 307 | // Update the node information with the data the game gave us. | 377 | // Update the node information with the data the game gave us. |
| 308 | rp.PopRaw(node_info[0]); | 378 | rp.PopRaw(current_node); |
| 309 | 379 | ||
| 310 | u16 version = rp.Pop<u16>(); | 380 | u16 version = rp.Pop<u16>(); |
| 311 | 381 | ||
| @@ -315,10 +385,14 @@ static void InitializeWithVersion(Interface* self) { | |||
| 315 | 385 | ||
| 316 | ASSERT_MSG(recv_buffer_memory->size == sharedmem_size, "Invalid shared memory size."); | 386 | ASSERT_MSG(recv_buffer_memory->size == sharedmem_size, "Invalid shared memory size."); |
| 317 | 387 | ||
| 318 | // Reset the connection status, it contains all zeros after initialization, | 388 | { |
| 319 | // except for the actual status value. | 389 | std::lock_guard<std::mutex> lock(connection_status_mutex); |
| 320 | connection_status = {}; | 390 | |
| 321 | connection_status.status = static_cast<u32>(NetworkStatus::NotConnected); | 391 | // Reset the connection status, it contains all zeros after initialization, |
| 392 | // except for the actual status value. | ||
| 393 | connection_status = {}; | ||
| 394 | connection_status.status = static_cast<u32>(NetworkStatus::NotConnected); | ||
| 395 | } | ||
| 322 | 396 | ||
| 323 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); | 397 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); |
| 324 | rb.Push(RESULT_SUCCESS); | 398 | rb.Push(RESULT_SUCCESS); |
| @@ -348,12 +422,16 @@ static void GetConnectionStatus(Interface* self) { | |||
| 348 | IPC::RequestBuilder rb = rp.MakeBuilder(13, 0); | 422 | IPC::RequestBuilder rb = rp.MakeBuilder(13, 0); |
| 349 | 423 | ||
| 350 | rb.Push(RESULT_SUCCESS); | 424 | rb.Push(RESULT_SUCCESS); |
| 351 | rb.PushRaw(connection_status); | 425 | { |
| 352 | 426 | std::lock_guard<std::mutex> lock(connection_status_mutex); | |
| 353 | // Reset the bitmask of changed nodes after each call to this | 427 | rb.PushRaw(connection_status); |
| 354 | // function to prevent falsely informing games of outstanding | 428 | |
| 355 | // changes in subsequent calls. | 429 | // Reset the bitmask of changed nodes after each call to this |
| 356 | connection_status.changed_nodes = 0; | 430 | // function to prevent falsely informing games of outstanding |
| 431 | // changes in subsequent calls. | ||
| 432 | // TODO(Subv): Find exactly where the NWM module resets this value. | ||
| 433 | connection_status.changed_nodes = 0; | ||
| 434 | } | ||
| 357 | 435 | ||
| 358 | LOG_DEBUG(Service_NWM, "called"); | 436 | LOG_DEBUG(Service_NWM, "called"); |
| 359 | } | 437 | } |
| @@ -434,31 +512,36 @@ static void BeginHostingNetwork(Interface* self) { | |||
| 434 | // The real UDS module throws a fatal error if this assert fails. | 512 | // The real UDS module throws a fatal error if this assert fails. |
| 435 | ASSERT_MSG(network_info.max_nodes > 1, "Trying to host a network of only one member."); | 513 | ASSERT_MSG(network_info.max_nodes > 1, "Trying to host a network of only one member."); |
| 436 | 514 | ||
| 437 | connection_status.status = static_cast<u32>(NetworkStatus::ConnectedAsHost); | 515 | { |
| 438 | 516 | std::lock_guard<std::mutex> lock(connection_status_mutex); | |
| 439 | // Ensure the application data size is less than the maximum value. | 517 | connection_status.status = static_cast<u32>(NetworkStatus::ConnectedAsHost); |
| 440 | ASSERT_MSG(network_info.application_data_size <= ApplicationDataSize, "Data size is too big."); | 518 | |
| 441 | 519 | // Ensure the application data size is less than the maximum value. | |
| 442 | // Set up basic information for this network. | 520 | ASSERT_MSG(network_info.application_data_size <= ApplicationDataSize, |
| 443 | network_info.oui_value = NintendoOUI; | 521 | "Data size is too big."); |
| 444 | network_info.oui_type = static_cast<u8>(NintendoTagId::NetworkInfo); | 522 | |
| 445 | 523 | // Set up basic information for this network. | |
| 446 | connection_status.max_nodes = network_info.max_nodes; | 524 | network_info.oui_value = NintendoOUI; |
| 447 | 525 | network_info.oui_type = static_cast<u8>(NintendoTagId::NetworkInfo); | |
| 448 | // Resize the nodes list to hold max_nodes. | 526 | |
| 449 | node_info.resize(network_info.max_nodes); | 527 | connection_status.max_nodes = network_info.max_nodes; |
| 450 | 528 | ||
| 451 | // There's currently only one node in the network (the host). | 529 | // Resize the nodes list to hold max_nodes. |
| 452 | connection_status.total_nodes = 1; | 530 | node_info.resize(network_info.max_nodes); |
| 453 | network_info.total_nodes = 1; | 531 | |
| 454 | // The host is always the first node | 532 | // There's currently only one node in the network (the host). |
| 455 | connection_status.network_node_id = 1; | 533 | connection_status.total_nodes = 1; |
| 456 | node_info[0].network_node_id = 1; | 534 | network_info.total_nodes = 1; |
| 457 | connection_status.nodes[0] = connection_status.network_node_id; | 535 | // The host is always the first node |
| 458 | // Set the bit 0 in the nodes bitmask to indicate that node 1 is already taken. | 536 | connection_status.network_node_id = 1; |
| 459 | connection_status.node_bitmask |= 1; | 537 | current_node.network_node_id = 1; |
| 460 | // Notify the application that the first node was set. | 538 | connection_status.nodes[0] = connection_status.network_node_id; |
| 461 | connection_status.changed_nodes |= 1; | 539 | // Set the bit 0 in the nodes bitmask to indicate that node 1 is already taken. |
| 540 | connection_status.node_bitmask |= 1; | ||
| 541 | // Notify the application that the first node was set. | ||
| 542 | connection_status.changed_nodes |= 1; | ||
| 543 | node_info[0] = current_node; | ||
| 544 | } | ||
| 462 | 545 | ||
| 463 | // If the game has a preferred channel, use that instead. | 546 | // If the game has a preferred channel, use that instead. |
| 464 | if (network_info.channel != 0) | 547 | if (network_info.channel != 0) |
| @@ -495,9 +578,13 @@ static void DestroyNetwork(Interface* self) { | |||
| 495 | // Unschedule the beacon broadcast event. | 578 | // Unschedule the beacon broadcast event. |
| 496 | CoreTiming::UnscheduleEvent(beacon_broadcast_event, 0); | 579 | CoreTiming::UnscheduleEvent(beacon_broadcast_event, 0); |
| 497 | 580 | ||
| 498 | // TODO(Subv): Check if connection_status is indeed reset after this call. | 581 | { |
| 499 | connection_status = {}; | 582 | std::lock_guard<std::mutex> lock(connection_status_mutex); |
| 500 | connection_status.status = static_cast<u8>(NetworkStatus::NotConnected); | 583 | |
| 584 | // TODO(Subv): Check if connection_status is indeed reset after this call. | ||
| 585 | connection_status = {}; | ||
| 586 | connection_status.status = static_cast<u8>(NetworkStatus::NotConnected); | ||
| 587 | } | ||
| 501 | connection_status_event->Signal(); | 588 | connection_status_event->Signal(); |
| 502 | 589 | ||
| 503 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | 590 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); |
| @@ -540,17 +627,24 @@ static void SendTo(Interface* self) { | |||
| 540 | 627 | ||
| 541 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | 628 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); |
| 542 | 629 | ||
| 543 | if (connection_status.status != static_cast<u32>(NetworkStatus::ConnectedAsClient) && | 630 | u16 network_node_id; |
| 544 | connection_status.status != static_cast<u32>(NetworkStatus::ConnectedAsHost)) { | ||
| 545 | rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::UDS, | ||
| 546 | ErrorSummary::InvalidState, ErrorLevel::Status)); | ||
| 547 | return; | ||
| 548 | } | ||
| 549 | 631 | ||
| 550 | if (dest_node_id == connection_status.network_node_id) { | 632 | { |
| 551 | rb.Push(ResultCode(ErrorDescription::NotFound, ErrorModule::UDS, | 633 | std::lock_guard<std::mutex> lock(connection_status_mutex); |
| 552 | ErrorSummary::WrongArgument, ErrorLevel::Status)); | 634 | if (connection_status.status != static_cast<u32>(NetworkStatus::ConnectedAsClient) && |
| 553 | return; | 635 | connection_status.status != static_cast<u32>(NetworkStatus::ConnectedAsHost)) { |
| 636 | rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::UDS, | ||
| 637 | ErrorSummary::InvalidState, ErrorLevel::Status)); | ||
| 638 | return; | ||
| 639 | } | ||
| 640 | |||
| 641 | if (dest_node_id == connection_status.network_node_id) { | ||
| 642 | rb.Push(ResultCode(ErrorDescription::NotFound, ErrorModule::UDS, | ||
| 643 | ErrorSummary::WrongArgument, ErrorLevel::Status)); | ||
| 644 | return; | ||
| 645 | } | ||
| 646 | |||
| 647 | network_node_id = connection_status.network_node_id; | ||
| 554 | } | 648 | } |
| 555 | 649 | ||
| 556 | // TODO(Subv): Do something with the flags. | 650 | // TODO(Subv): Do something with the flags. |
| @@ -567,8 +661,8 @@ static void SendTo(Interface* self) { | |||
| 567 | 661 | ||
| 568 | // TODO(Subv): Increment the sequence number after each sent packet. | 662 | // TODO(Subv): Increment the sequence number after each sent packet. |
| 569 | u16 sequence_number = 0; | 663 | u16 sequence_number = 0; |
| 570 | std::vector<u8> data_payload = GenerateDataPayload( | 664 | std::vector<u8> data_payload = |
| 571 | data, data_channel, dest_node_id, connection_status.network_node_id, sequence_number); | 665 | GenerateDataPayload(data, data_channel, dest_node_id, network_node_id, sequence_number); |
| 572 | 666 | ||
| 573 | // TODO(Subv): Retrieve the MAC address of the dest_node_id and our own to encrypt | 667 | // TODO(Subv): Retrieve the MAC address of the dest_node_id and our own to encrypt |
| 574 | // and encapsulate the payload. | 668 | // and encapsulate the payload. |
| @@ -595,6 +689,7 @@ static void GetChannel(Interface* self) { | |||
| 595 | IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1A, 0, 0); | 689 | IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1A, 0, 0); |
| 596 | IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | 690 | IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); |
| 597 | 691 | ||
| 692 | std::lock_guard<std::mutex> lock(connection_status_mutex); | ||
| 598 | bool is_connected = connection_status.status != static_cast<u32>(NetworkStatus::NotConnected); | 693 | bool is_connected = connection_status.status != static_cast<u32>(NetworkStatus::NotConnected); |
| 599 | 694 | ||
| 600 | u8 channel = is_connected ? network_channel : 0; | 695 | u8 channel = is_connected ? network_channel : 0; |
| @@ -766,6 +861,7 @@ static void BeaconBroadcastCallback(u64 userdata, int cycles_late) { | |||
| 766 | * @param network_node_id Network Node Id of the connecting client. | 861 | * @param network_node_id Network Node Id of the connecting client. |
| 767 | */ | 862 | */ |
| 768 | void OnClientConnected(u16 network_node_id) { | 863 | void OnClientConnected(u16 network_node_id) { |
| 864 | std::lock_guard<std::mutex> lock(connection_status_mutex); | ||
| 769 | ASSERT_MSG(connection_status.status == static_cast<u32>(NetworkStatus::ConnectedAsHost), | 865 | ASSERT_MSG(connection_status.status == static_cast<u32>(NetworkStatus::ConnectedAsHost), |
| 770 | "Can not accept clients if we're not hosting a network"); | 866 | "Can not accept clients if we're not hosting a network"); |
| 771 | ASSERT_MSG(connection_status.total_nodes < connection_status.max_nodes, | 867 | ASSERT_MSG(connection_status.total_nodes < connection_status.max_nodes, |
| @@ -827,8 +923,11 @@ NWM_UDS::~NWM_UDS() { | |||
| 827 | connection_status_event = nullptr; | 923 | connection_status_event = nullptr; |
| 828 | recv_buffer_memory = nullptr; | 924 | recv_buffer_memory = nullptr; |
| 829 | 925 | ||
| 830 | connection_status = {}; | 926 | { |
| 831 | connection_status.status = static_cast<u32>(NetworkStatus::NotConnected); | 927 | std::lock_guard<std::mutex> lock(connection_status_mutex); |
| 928 | connection_status = {}; | ||
| 929 | connection_status.status = static_cast<u32>(NetworkStatus::NotConnected); | ||
| 930 | } | ||
| 832 | 931 | ||
| 833 | CoreTiming::UnscheduleEvent(beacon_broadcast_event, 0); | 932 | CoreTiming::UnscheduleEvent(beacon_broadcast_event, 0); |
| 834 | } | 933 | } |
diff --git a/src/core/hle/service/nwm/uds_connection.cpp b/src/core/hle/service/nwm/uds_connection.cpp index c8a76ec2a..c74f51253 100644 --- a/src/core/hle/service/nwm/uds_connection.cpp +++ b/src/core/hle/service/nwm/uds_connection.cpp | |||
| @@ -75,5 +75,14 @@ std::vector<u8> GenerateAssocResponseFrame(AssocStatus status, u16 association_i | |||
| 75 | return data; | 75 | return data; |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | std::tuple<AssocStatus, u16> GetAssociationResult(const std::vector<u8>& body) { | ||
| 79 | AssociationResponseFrame frame; | ||
| 80 | memcpy(&frame, body.data(), sizeof(frame)); | ||
| 81 | |||
| 82 | constexpr u16 AssociationIdMask = 0x3FFF; | ||
| 83 | return std::make_tuple(static_cast<AssocStatus>(frame.status_code), | ||
| 84 | frame.assoc_id & AssociationIdMask); | ||
| 85 | } | ||
| 86 | |||
| 78 | } // namespace NWM | 87 | } // namespace NWM |
| 79 | } // namespace Service | 88 | } // namespace Service |
diff --git a/src/core/hle/service/nwm/uds_connection.h b/src/core/hle/service/nwm/uds_connection.h index 73f55a4fd..a664f8471 100644 --- a/src/core/hle/service/nwm/uds_connection.h +++ b/src/core/hle/service/nwm/uds_connection.h | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <tuple> | ||
| 7 | #include <vector> | 8 | #include <vector> |
| 8 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 9 | #include "common/swap.h" | 10 | #include "common/swap.h" |
| @@ -47,5 +48,9 @@ AuthenticationSeq GetAuthenticationSeqNumber(const std::vector<u8>& body); | |||
| 47 | /// network id, starting at the frame body. | 48 | /// network id, starting at the frame body. |
| 48 | std::vector<u8> GenerateAssocResponseFrame(AssocStatus status, u16 association_id, u32 network_id); | 49 | std::vector<u8> GenerateAssocResponseFrame(AssocStatus status, u16 association_id, u32 network_id); |
| 49 | 50 | ||
| 51 | /// Returns a tuple of (association status, association id) from the body of an AssociationResponse | ||
| 52 | /// frame. | ||
| 53 | std::tuple<AssocStatus, u16> GetAssociationResult(const std::vector<u8>& body); | ||
| 54 | |||
| 50 | } // namespace NWM | 55 | } // namespace NWM |
| 51 | } // namespace Service | 56 | } // namespace Service |
diff --git a/src/core/hle/service/nwm/uds_data.cpp b/src/core/hle/service/nwm/uds_data.cpp index 8c6742dba..0fd9b8b8c 100644 --- a/src/core/hle/service/nwm/uds_data.cpp +++ b/src/core/hle/service/nwm/uds_data.cpp | |||
| @@ -274,5 +274,26 @@ std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 | |||
| 274 | return buffer; | 274 | return buffer; |
| 275 | } | 275 | } |
| 276 | 276 | ||
| 277 | std::vector<u8> GenerateEAPoLStartFrame(u16 association_id, const NodeInfo& node_info) { | ||
| 278 | EAPoLStartPacket eapol_start{}; | ||
| 279 | eapol_start.association_id = association_id; | ||
| 280 | eapol_start.friend_code_seed = node_info.friend_code_seed; | ||
| 281 | |||
| 282 | for (int i = 0; i < node_info.username.size(); ++i) | ||
| 283 | eapol_start.username[i] = node_info.username[i]; | ||
| 284 | |||
| 285 | // Note: The network_node_id and unknown bytes seem to be uninitialized in the NWM module. | ||
| 286 | // TODO(B3N30): The last 8 bytes seem to have a fixed value of 07 88 15 00 04 e9 13 00 in | ||
| 287 | // EAPoL-Start packets from different 3DSs to the same host during a Super Smash Bros. 4 game. | ||
| 288 | // Find out what that means. | ||
| 289 | |||
| 290 | std::vector<u8> eapol_buffer(sizeof(EAPoLStartPacket)); | ||
| 291 | std::memcpy(eapol_buffer.data(), &eapol_start, sizeof(eapol_start)); | ||
| 292 | |||
| 293 | std::vector<u8> buffer = GenerateLLCHeader(EtherType::EAPoL); | ||
| 294 | buffer.insert(buffer.end(), eapol_buffer.begin(), eapol_buffer.end()); | ||
| 295 | return buffer; | ||
| 296 | } | ||
| 297 | |||
| 277 | } // namespace NWM | 298 | } // namespace NWM |
| 278 | } // namespace Service | 299 | } // namespace Service |
diff --git a/src/core/hle/service/nwm/uds_data.h b/src/core/hle/service/nwm/uds_data.h index a23520a41..76e8f546b 100644 --- a/src/core/hle/service/nwm/uds_data.h +++ b/src/core/hle/service/nwm/uds_data.h | |||
| @@ -67,6 +67,27 @@ struct DataFrameCryptoCTR { | |||
| 67 | 67 | ||
| 68 | static_assert(sizeof(DataFrameCryptoCTR) == 16, "DataFrameCryptoCTR has the wrong size"); | 68 | static_assert(sizeof(DataFrameCryptoCTR) == 16, "DataFrameCryptoCTR has the wrong size"); |
| 69 | 69 | ||
| 70 | constexpr u16 EAPoLStartMagic = 0x201; | ||
| 71 | |||
| 72 | /* | ||
| 73 | * Nintendo EAPoLStartPacket, is used to initaliaze a connection between client and host | ||
| 74 | */ | ||
| 75 | struct EAPoLStartPacket { | ||
| 76 | u16_be magic = EAPoLStartMagic; | ||
| 77 | u16_be association_id; | ||
| 78 | // This value is hardcoded to 1 in the NWM module. | ||
| 79 | u16_be unknown = 1; | ||
| 80 | INSERT_PADDING_BYTES(2); | ||
| 81 | |||
| 82 | u64_be friend_code_seed; | ||
| 83 | std::array<u16_be, 10> username; | ||
| 84 | INSERT_PADDING_BYTES(4); | ||
| 85 | u16_be network_node_id; | ||
| 86 | INSERT_PADDING_BYTES(6); | ||
| 87 | }; | ||
| 88 | |||
| 89 | static_assert(sizeof(EAPoLStartPacket) == 0x30, "EAPoLStartPacket has the wrong size"); | ||
| 90 | |||
| 70 | /** | 91 | /** |
| 71 | * Generates an unencrypted 802.11 data payload. | 92 | * Generates an unencrypted 802.11 data payload. |
| 72 | * @returns The generated frame payload. | 93 | * @returns The generated frame payload. |
| @@ -74,5 +95,12 @@ static_assert(sizeof(DataFrameCryptoCTR) == 16, "DataFrameCryptoCTR has the wron | |||
| 74 | std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node, | 95 | std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node, |
| 75 | u16 src_node, u16 sequence_number); | 96 | u16 src_node, u16 sequence_number); |
| 76 | 97 | ||
| 98 | /* | ||
| 99 | * Generates an unencrypted 802.11 data frame body with the EAPoL-Start format for UDS | ||
| 100 | * communication. | ||
| 101 | * @returns The generated frame body. | ||
| 102 | */ | ||
| 103 | std::vector<u8> GenerateEAPoLStartFrame(u16 association_id, const NodeInfo& node_info); | ||
| 104 | |||
| 77 | } // namespace NWM | 105 | } // namespace NWM |
| 78 | } // namespace Service | 106 | } // namespace Service |
diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 5e7fc68f9..854ab9a05 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp | |||
| @@ -36,6 +36,10 @@ ResultVal<Kernel::SharedPtr<Kernel::ServerPort>> ServiceManager::RegisterService | |||
| 36 | std::string name, unsigned int max_sessions) { | 36 | std::string name, unsigned int max_sessions) { |
| 37 | 37 | ||
| 38 | CASCADE_CODE(ValidateServiceName(name)); | 38 | CASCADE_CODE(ValidateServiceName(name)); |
| 39 | |||
| 40 | if (registered_services.find(name) != registered_services.end()) | ||
| 41 | return ERR_ALREADY_REGISTERED; | ||
| 42 | |||
| 39 | Kernel::SharedPtr<Kernel::ServerPort> server_port; | 43 | Kernel::SharedPtr<Kernel::ServerPort> server_port; |
| 40 | Kernel::SharedPtr<Kernel::ClientPort> client_port; | 44 | Kernel::SharedPtr<Kernel::ClientPort> client_port; |
| 41 | std::tie(server_port, client_port) = Kernel::ServerPort::CreatePortPair(max_sessions, name); | 45 | std::tie(server_port, client_port) = Kernel::ServerPort::CreatePortPair(max_sessions, name); |
diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index 8f0dbf2db..9f60a7965 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h | |||
| @@ -32,6 +32,9 @@ constexpr ResultCode ERR_ACCESS_DENIED(6, ErrorModule::SRV, ErrorSummary::Invali | |||
| 32 | ErrorLevel::Permanent); // 0xD8E06406 | 32 | ErrorLevel::Permanent); // 0xD8E06406 |
| 33 | constexpr ResultCode ERR_NAME_CONTAINS_NUL(7, ErrorModule::SRV, ErrorSummary::WrongArgument, | 33 | constexpr ResultCode ERR_NAME_CONTAINS_NUL(7, ErrorModule::SRV, ErrorSummary::WrongArgument, |
| 34 | ErrorLevel::Permanent); // 0xD9006407 | 34 | ErrorLevel::Permanent); // 0xD9006407 |
| 35 | constexpr ResultCode ERR_ALREADY_REGISTERED(ErrorDescription::AlreadyExists, ErrorModule::OS, | ||
| 36 | ErrorSummary::WrongArgument, | ||
| 37 | ErrorLevel::Permanent); // 0xD9001BFC | ||
| 35 | 38 | ||
| 36 | class ServiceManager { | 39 | class ServiceManager { |
| 37 | public: | 40 | public: |
diff --git a/src/core/hle/service/sm/srv.cpp b/src/core/hle/service/sm/srv.cpp index 352941e69..5c955cf54 100644 --- a/src/core/hle/service/sm/srv.cpp +++ b/src/core/hle/service/sm/srv.cpp | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | #include "core/hle/kernel/errors.h" | 13 | #include "core/hle/kernel/errors.h" |
| 14 | #include "core/hle/kernel/hle_ipc.h" | 14 | #include "core/hle/kernel/hle_ipc.h" |
| 15 | #include "core/hle/kernel/semaphore.h" | 15 | #include "core/hle/kernel/semaphore.h" |
| 16 | #include "core/hle/kernel/server_port.h" | ||
| 16 | #include "core/hle/kernel/server_session.h" | 17 | #include "core/hle/kernel/server_session.h" |
| 17 | #include "core/hle/service/sm/sm.h" | 18 | #include "core/hle/service/sm/sm.h" |
| 18 | #include "core/hle/service/sm/srv.h" | 19 | #include "core/hle/service/sm/srv.h" |
| @@ -184,12 +185,35 @@ void SRV::PublishToSubscriber(Kernel::HLERequestContext& ctx) { | |||
| 184 | flags); | 185 | flags); |
| 185 | } | 186 | } |
| 186 | 187 | ||
| 188 | void SRV::RegisterService(Kernel::HLERequestContext& ctx) { | ||
| 189 | IPC::RequestParser rp(ctx, 0x3, 4, 0); | ||
| 190 | |||
| 191 | auto name_buf = rp.PopRaw<std::array<char, 8>>(); | ||
| 192 | size_t name_len = rp.Pop<u32>(); | ||
| 193 | u32 max_sessions = rp.Pop<u32>(); | ||
| 194 | |||
| 195 | std::string name(name_buf.data(), std::min(name_len, name_buf.size())); | ||
| 196 | |||
| 197 | auto port = service_manager->RegisterService(name, max_sessions); | ||
| 198 | |||
| 199 | if (port.Failed()) { | ||
| 200 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | ||
| 201 | rb.Push(port.Code()); | ||
| 202 | LOG_ERROR(Service_SRV, "called service=%s -> error 0x%08X", name.c_str(), port.Code().raw); | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); | ||
| 207 | rb.Push(RESULT_SUCCESS); | ||
| 208 | rb.PushObjects(port.Unwrap()); | ||
| 209 | } | ||
| 210 | |||
| 187 | SRV::SRV(std::shared_ptr<ServiceManager> service_manager) | 211 | SRV::SRV(std::shared_ptr<ServiceManager> service_manager) |
| 188 | : ServiceFramework("srv:", 4), service_manager(std::move(service_manager)) { | 212 | : ServiceFramework("srv:", 4), service_manager(std::move(service_manager)) { |
| 189 | static const FunctionInfo functions[] = { | 213 | static const FunctionInfo functions[] = { |
| 190 | {0x00010002, &SRV::RegisterClient, "RegisterClient"}, | 214 | {0x00010002, &SRV::RegisterClient, "RegisterClient"}, |
| 191 | {0x00020000, &SRV::EnableNotification, "EnableNotification"}, | 215 | {0x00020000, &SRV::EnableNotification, "EnableNotification"}, |
| 192 | {0x00030100, nullptr, "RegisterService"}, | 216 | {0x00030100, &SRV::RegisterService, "RegisterService"}, |
| 193 | {0x000400C0, nullptr, "UnregisterService"}, | 217 | {0x000400C0, nullptr, "UnregisterService"}, |
| 194 | {0x00050100, &SRV::GetServiceHandle, "GetServiceHandle"}, | 218 | {0x00050100, &SRV::GetServiceHandle, "GetServiceHandle"}, |
| 195 | {0x000600C2, nullptr, "RegisterPort"}, | 219 | {0x000600C2, nullptr, "RegisterPort"}, |
diff --git a/src/core/hle/service/sm/srv.h b/src/core/hle/service/sm/srv.h index 75cca5184..aad839563 100644 --- a/src/core/hle/service/sm/srv.h +++ b/src/core/hle/service/sm/srv.h | |||
| @@ -28,6 +28,7 @@ private: | |||
| 28 | void Subscribe(Kernel::HLERequestContext& ctx); | 28 | void Subscribe(Kernel::HLERequestContext& ctx); |
| 29 | void Unsubscribe(Kernel::HLERequestContext& ctx); | 29 | void Unsubscribe(Kernel::HLERequestContext& ctx); |
| 30 | void PublishToSubscriber(Kernel::HLERequestContext& ctx); | 30 | void PublishToSubscriber(Kernel::HLERequestContext& ctx); |
| 31 | void RegisterService(Kernel::HLERequestContext& ctx); | ||
| 31 | 32 | ||
| 32 | std::shared_ptr<ServiceManager> service_manager; | 33 | std::shared_ptr<ServiceManager> service_manager; |
| 33 | Kernel::SharedPtr<Kernel::Semaphore> notification_semaphore; | 34 | Kernel::SharedPtr<Kernel::Semaphore> notification_semaphore; |
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp index 69cdc0867..5ad5c5287 100644 --- a/src/core/loader/3dsx.cpp +++ b/src/core/loader/3dsx.cpp | |||
| @@ -270,7 +270,7 @@ ResultStatus AppLoader_THREEDSX::Load() { | |||
| 270 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); | 270 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); |
| 271 | Kernel::g_current_process->svc_access_mask.set(); | 271 | Kernel::g_current_process->svc_access_mask.set(); |
| 272 | Kernel::g_current_process->address_mappings = default_address_mappings; | 272 | Kernel::g_current_process->address_mappings = default_address_mappings; |
| 273 | Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; | 273 | Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); |
| 274 | 274 | ||
| 275 | // Attach the default resource limit (APPLICATION) to the process | 275 | // Attach the default resource limit (APPLICATION) to the process |
| 276 | Kernel::g_current_process->resource_limit = | 276 | Kernel::g_current_process->resource_limit = |
| @@ -278,8 +278,7 @@ ResultStatus AppLoader_THREEDSX::Load() { | |||
| 278 | 278 | ||
| 279 | Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE); | 279 | Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE); |
| 280 | 280 | ||
| 281 | Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_SelfNCCH>(*this), | 281 | Service::FS::RegisterSelfNCCH(*this); |
| 282 | Service::FS::ArchiveIdCode::SelfNCCH); | ||
| 283 | 282 | ||
| 284 | is_loaded = true; | 283 | is_loaded = true; |
| 285 | return ResultStatus::Success; | 284 | return ResultStatus::Success; |
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 2f27606a1..2de1f4e81 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp | |||
| @@ -397,7 +397,7 @@ ResultStatus AppLoader_ELF::Load() { | |||
| 397 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); | 397 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); |
| 398 | Kernel::g_current_process->svc_access_mask.set(); | 398 | Kernel::g_current_process->svc_access_mask.set(); |
| 399 | Kernel::g_current_process->address_mappings = default_address_mappings; | 399 | Kernel::g_current_process->address_mappings = default_address_mappings; |
| 400 | Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; | 400 | Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); |
| 401 | 401 | ||
| 402 | // Attach the default resource limit (APPLICATION) to the process | 402 | // Attach the default resource limit (APPLICATION) to the process |
| 403 | Kernel::g_current_process->resource_limit = | 403 | Kernel::g_current_process->resource_limit = |
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index e731888a2..3160fd2fd 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h | |||
| @@ -167,6 +167,19 @@ public: | |||
| 167 | } | 167 | } |
| 168 | 168 | ||
| 169 | /** | 169 | /** |
| 170 | * Get the update RomFS of the application | ||
| 171 | * Since the RomFS can be huge, we return a file reference instead of copying to a buffer | ||
| 172 | * @param romfs_file The file containing the RomFS | ||
| 173 | * @param offset The offset the romfs begins on | ||
| 174 | * @param size The size of the romfs | ||
| 175 | * @return ResultStatus result of function | ||
| 176 | */ | ||
| 177 | virtual ResultStatus ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 178 | u64& size) { | ||
| 179 | return ResultStatus::ErrorNotImplemented; | ||
| 180 | } | ||
| 181 | |||
| 182 | /** | ||
| 170 | * Get the title of the application | 183 | * Get the title of the application |
| 171 | * @param title Reference to store the application title into | 184 | * @param title Reference to store the application title into |
| 172 | * @return ResultStatus result of function | 185 | * @return ResultStatus result of function |
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 79ea50147..5107135f9 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | #include "common/swap.h" | 13 | #include "common/swap.h" |
| 14 | #include "core/core.h" | 14 | #include "core/core.h" |
| 15 | #include "core/file_sys/archive_selfncch.h" | 15 | #include "core/file_sys/archive_selfncch.h" |
| 16 | #include "core/file_sys/ncch_container.h" | ||
| 16 | #include "core/hle/kernel/process.h" | 17 | #include "core/hle/kernel/process.h" |
| 17 | #include "core/hle/kernel/resource_limit.h" | 18 | #include "core/hle/kernel/resource_limit.h" |
| 18 | #include "core/hle/service/cfg/cfg.h" | 19 | #include "core/hle/service/cfg/cfg.h" |
| @@ -27,87 +28,7 @@ | |||
| 27 | 28 | ||
| 28 | namespace Loader { | 29 | namespace Loader { |
| 29 | 30 | ||
| 30 | static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs | 31 | static const u64 UPDATE_MASK = 0x0000000e00000000; |
| 31 | static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes) | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Get the decompressed size of an LZSS compressed ExeFS file | ||
| 35 | * @param buffer Buffer of compressed file | ||
| 36 | * @param size Size of compressed buffer | ||
| 37 | * @return Size of decompressed buffer | ||
| 38 | */ | ||
| 39 | static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) { | ||
| 40 | u32 offset_size = *(u32*)(buffer + size - 4); | ||
| 41 | return offset_size + size; | ||
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Decompress ExeFS file (compressed with LZSS) | ||
| 46 | * @param compressed Compressed buffer | ||
| 47 | * @param compressed_size Size of compressed buffer | ||
| 48 | * @param decompressed Decompressed buffer | ||
| 49 | * @param decompressed_size Size of decompressed buffer | ||
| 50 | * @return True on success, otherwise false | ||
| 51 | */ | ||
| 52 | static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed, | ||
| 53 | u32 decompressed_size) { | ||
| 54 | const u8* footer = compressed + compressed_size - 8; | ||
| 55 | u32 buffer_top_and_bottom = *reinterpret_cast<const u32*>(footer); | ||
| 56 | u32 out = decompressed_size; | ||
| 57 | u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF); | ||
| 58 | u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF); | ||
| 59 | |||
| 60 | memset(decompressed, 0, decompressed_size); | ||
| 61 | memcpy(decompressed, compressed, compressed_size); | ||
| 62 | |||
| 63 | while (index > stop_index) { | ||
| 64 | u8 control = compressed[--index]; | ||
| 65 | |||
| 66 | for (unsigned i = 0; i < 8; i++) { | ||
| 67 | if (index <= stop_index) | ||
| 68 | break; | ||
| 69 | if (index <= 0) | ||
| 70 | break; | ||
| 71 | if (out <= 0) | ||
| 72 | break; | ||
| 73 | |||
| 74 | if (control & 0x80) { | ||
| 75 | // Check if compression is out of bounds | ||
| 76 | if (index < 2) | ||
| 77 | return false; | ||
| 78 | index -= 2; | ||
| 79 | |||
| 80 | u32 segment_offset = compressed[index] | (compressed[index + 1] << 8); | ||
| 81 | u32 segment_size = ((segment_offset >> 12) & 15) + 3; | ||
| 82 | segment_offset &= 0x0FFF; | ||
| 83 | segment_offset += 2; | ||
| 84 | |||
| 85 | // Check if compression is out of bounds | ||
| 86 | if (out < segment_size) | ||
| 87 | return false; | ||
| 88 | |||
| 89 | for (unsigned j = 0; j < segment_size; j++) { | ||
| 90 | // Check if compression is out of bounds | ||
| 91 | if (out + segment_offset >= decompressed_size) | ||
| 92 | return false; | ||
| 93 | |||
| 94 | u8 data = decompressed[out + segment_offset]; | ||
| 95 | decompressed[--out] = data; | ||
| 96 | } | ||
| 97 | } else { | ||
| 98 | // Check if compression is out of bounds | ||
| 99 | if (out < 1) | ||
| 100 | return false; | ||
| 101 | decompressed[--out] = compressed[--index]; | ||
| 102 | } | ||
| 103 | control <<= 1; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | return true; | ||
| 107 | } | ||
| 108 | |||
| 109 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 110 | // AppLoader_NCCH class | ||
| 111 | 32 | ||
| 112 | FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { | 33 | FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { |
| 113 | u32 magic; | 34 | u32 magic; |
| @@ -124,15 +45,25 @@ FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { | |||
| 124 | return FileType::Error; | 45 | return FileType::Error; |
| 125 | } | 46 | } |
| 126 | 47 | ||
| 48 | static std::string GetUpdateNCCHPath(u64_le program_id) { | ||
| 49 | u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32); | ||
| 50 | u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF); | ||
| 51 | |||
| 52 | return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/%08x/%08x/content/00000000.app", | ||
| 53 | FileUtil::GetUserPath(D_SDMC_IDX).c_str(), SYSTEM_ID, SDCARD_ID, | ||
| 54 | high, low); | ||
| 55 | } | ||
| 56 | |||
| 127 | std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { | 57 | std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { |
| 128 | if (!is_loaded) { | 58 | if (!is_loaded) { |
| 129 | ResultStatus res = LoadExeFS(); | 59 | ResultStatus res = base_ncch.Load(); |
| 130 | if (res != ResultStatus::Success) { | 60 | if (res != ResultStatus::Success) { |
| 131 | return std::make_pair(boost::none, res); | 61 | return std::make_pair(boost::none, res); |
| 132 | } | 62 | } |
| 133 | } | 63 | } |
| 64 | |||
| 134 | // Set the system mode as the one from the exheader. | 65 | // Set the system mode as the one from the exheader. |
| 135 | return std::make_pair(exheader_header.arm11_system_local_caps.system_mode.Value(), | 66 | return std::make_pair(overlay_ncch->exheader_header.arm11_system_local_caps.system_mode.Value(), |
| 136 | ResultStatus::Success); | 67 | ResultStatus::Success); |
| 137 | } | 68 | } |
| 138 | 69 | ||
| @@ -144,183 +75,65 @@ ResultStatus AppLoader_NCCH::LoadExec() { | |||
| 144 | return ResultStatus::ErrorNotLoaded; | 75 | return ResultStatus::ErrorNotLoaded; |
| 145 | 76 | ||
| 146 | std::vector<u8> code; | 77 | std::vector<u8> code; |
| 147 | if (ResultStatus::Success == ReadCode(code)) { | 78 | u64_le program_id; |
| 79 | if (ResultStatus::Success == ReadCode(code) && | ||
| 80 | ResultStatus::Success == ReadProgramId(program_id)) { | ||
| 148 | std::string process_name = Common::StringFromFixedZeroTerminatedBuffer( | 81 | std::string process_name = Common::StringFromFixedZeroTerminatedBuffer( |
| 149 | (const char*)exheader_header.codeset_info.name, 8); | 82 | (const char*)overlay_ncch->exheader_header.codeset_info.name, 8); |
| 150 | 83 | ||
| 151 | SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, ncch_header.program_id); | 84 | SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, program_id); |
| 152 | 85 | ||
| 153 | codeset->code.offset = 0; | 86 | codeset->code.offset = 0; |
| 154 | codeset->code.addr = exheader_header.codeset_info.text.address; | 87 | codeset->code.addr = overlay_ncch->exheader_header.codeset_info.text.address; |
| 155 | codeset->code.size = exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE; | 88 | codeset->code.size = |
| 89 | overlay_ncch->exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE; | ||
| 156 | 90 | ||
| 157 | codeset->rodata.offset = codeset->code.offset + codeset->code.size; | 91 | codeset->rodata.offset = codeset->code.offset + codeset->code.size; |
| 158 | codeset->rodata.addr = exheader_header.codeset_info.ro.address; | 92 | codeset->rodata.addr = overlay_ncch->exheader_header.codeset_info.ro.address; |
| 159 | codeset->rodata.size = exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE; | 93 | codeset->rodata.size = |
| 94 | overlay_ncch->exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE; | ||
| 160 | 95 | ||
| 161 | // TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just | 96 | // TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just |
| 162 | // to the regular size. Playing it safe for now. | 97 | // to the regular size. Playing it safe for now. |
| 163 | u32 bss_page_size = (exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF; | 98 | u32 bss_page_size = (overlay_ncch->exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF; |
| 164 | code.resize(code.size() + bss_page_size, 0); | 99 | code.resize(code.size() + bss_page_size, 0); |
| 165 | 100 | ||
| 166 | codeset->data.offset = codeset->rodata.offset + codeset->rodata.size; | 101 | codeset->data.offset = codeset->rodata.offset + codeset->rodata.size; |
| 167 | codeset->data.addr = exheader_header.codeset_info.data.address; | 102 | codeset->data.addr = overlay_ncch->exheader_header.codeset_info.data.address; |
| 168 | codeset->data.size = | 103 | codeset->data.size = |
| 169 | exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + bss_page_size; | 104 | overlay_ncch->exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + |
| 105 | bss_page_size; | ||
| 170 | 106 | ||
| 171 | codeset->entrypoint = codeset->code.addr; | 107 | codeset->entrypoint = codeset->code.addr; |
| 172 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(code)); | 108 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(code)); |
| 173 | 109 | ||
| 174 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); | 110 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); |
| 175 | Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; | 111 | Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table); |
| 176 | 112 | ||
| 177 | // Attach a resource limit to the process based on the resource limit category | 113 | // Attach a resource limit to the process based on the resource limit category |
| 178 | Kernel::g_current_process->resource_limit = | 114 | Kernel::g_current_process->resource_limit = |
| 179 | Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>( | 115 | Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>( |
| 180 | exheader_header.arm11_system_local_caps.resource_limit_category)); | 116 | overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category)); |
| 181 | 117 | ||
| 182 | // Set the default CPU core for this process | 118 | // Set the default CPU core for this process |
| 183 | Kernel::g_current_process->ideal_processor = | 119 | Kernel::g_current_process->ideal_processor = |
| 184 | exheader_header.arm11_system_local_caps.ideal_processor; | 120 | overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor; |
| 185 | 121 | ||
| 186 | // Copy data while converting endianness | 122 | // Copy data while converting endianness |
| 187 | std::array<u32, ARRAY_SIZE(exheader_header.arm11_kernel_caps.descriptors)> kernel_caps; | 123 | std::array<u32, ARRAY_SIZE(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors)> |
| 188 | std::copy_n(exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), | 124 | kernel_caps; |
| 125 | std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), | ||
| 189 | begin(kernel_caps)); | 126 | begin(kernel_caps)); |
| 190 | Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size()); | 127 | Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size()); |
| 191 | 128 | ||
| 192 | s32 priority = exheader_header.arm11_system_local_caps.priority; | 129 | s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority; |
| 193 | u32 stack_size = exheader_header.codeset_info.stack_size; | 130 | u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size; |
| 194 | Kernel::g_current_process->Run(priority, stack_size); | 131 | Kernel::g_current_process->Run(priority, stack_size); |
| 195 | return ResultStatus::Success; | 132 | return ResultStatus::Success; |
| 196 | } | 133 | } |
| 197 | return ResultStatus::Error; | 134 | return ResultStatus::Error; |
| 198 | } | 135 | } |
| 199 | 136 | ||
| 200 | ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { | ||
| 201 | if (!file.IsOpen()) | ||
| 202 | return ResultStatus::Error; | ||
| 203 | |||
| 204 | ResultStatus result = LoadExeFS(); | ||
| 205 | if (result != ResultStatus::Success) | ||
| 206 | return result; | ||
| 207 | |||
| 208 | LOG_DEBUG(Loader, "%d sections:", kMaxSections); | ||
| 209 | // Iterate through the ExeFs archive until we find a section with the specified name... | ||
| 210 | for (unsigned section_number = 0; section_number < kMaxSections; section_number++) { | ||
| 211 | const auto& section = exefs_header.section[section_number]; | ||
| 212 | |||
| 213 | // Load the specified section... | ||
| 214 | if (strcmp(section.name, name) == 0) { | ||
| 215 | LOG_DEBUG(Loader, "%d - offset: 0x%08X, size: 0x%08X, name: %s", section_number, | ||
| 216 | section.offset, section.size, section.name); | ||
| 217 | |||
| 218 | s64 section_offset = | ||
| 219 | (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); | ||
| 220 | file.Seek(section_offset, SEEK_SET); | ||
| 221 | |||
| 222 | if (strcmp(section.name, ".code") == 0 && is_compressed) { | ||
| 223 | // Section is compressed, read compressed .code section... | ||
| 224 | std::unique_ptr<u8[]> temp_buffer; | ||
| 225 | try { | ||
| 226 | temp_buffer.reset(new u8[section.size]); | ||
| 227 | } catch (std::bad_alloc&) { | ||
| 228 | return ResultStatus::ErrorMemoryAllocationFailed; | ||
| 229 | } | ||
| 230 | |||
| 231 | if (file.ReadBytes(&temp_buffer[0], section.size) != section.size) | ||
| 232 | return ResultStatus::Error; | ||
| 233 | |||
| 234 | // Decompress .code section... | ||
| 235 | u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size); | ||
| 236 | buffer.resize(decompressed_size); | ||
| 237 | if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size)) | ||
| 238 | return ResultStatus::ErrorInvalidFormat; | ||
| 239 | } else { | ||
| 240 | // Section is uncompressed... | ||
| 241 | buffer.resize(section.size); | ||
| 242 | if (file.ReadBytes(&buffer[0], section.size) != section.size) | ||
| 243 | return ResultStatus::Error; | ||
| 244 | } | ||
| 245 | return ResultStatus::Success; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | return ResultStatus::ErrorNotUsed; | ||
| 249 | } | ||
| 250 | |||
| 251 | ResultStatus AppLoader_NCCH::LoadExeFS() { | ||
| 252 | if (is_exefs_loaded) | ||
| 253 | return ResultStatus::Success; | ||
| 254 | |||
| 255 | if (!file.IsOpen()) | ||
| 256 | return ResultStatus::Error; | ||
| 257 | |||
| 258 | // Reset read pointer in case this file has been read before. | ||
| 259 | file.Seek(0, SEEK_SET); | ||
| 260 | |||
| 261 | if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) | ||
| 262 | return ResultStatus::Error; | ||
| 263 | |||
| 264 | // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... | ||
| 265 | if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { | ||
| 266 | LOG_DEBUG(Loader, "Only loading the first (bootable) NCCH within the NCSD file!"); | ||
| 267 | ncch_offset = 0x4000; | ||
| 268 | file.Seek(ncch_offset, SEEK_SET); | ||
| 269 | file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); | ||
| 270 | } | ||
| 271 | |||
| 272 | // Verify we are loading the correct file type... | ||
| 273 | if (MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) | ||
| 274 | return ResultStatus::ErrorInvalidFormat; | ||
| 275 | |||
| 276 | // Read ExHeader... | ||
| 277 | |||
| 278 | if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header)) | ||
| 279 | return ResultStatus::Error; | ||
| 280 | |||
| 281 | is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; | ||
| 282 | entry_point = exheader_header.codeset_info.text.address; | ||
| 283 | code_size = exheader_header.codeset_info.text.code_size; | ||
| 284 | stack_size = exheader_header.codeset_info.stack_size; | ||
| 285 | bss_size = exheader_header.codeset_info.bss_size; | ||
| 286 | core_version = exheader_header.arm11_system_local_caps.core_version; | ||
| 287 | priority = exheader_header.arm11_system_local_caps.priority; | ||
| 288 | resource_limit_category = exheader_header.arm11_system_local_caps.resource_limit_category; | ||
| 289 | |||
| 290 | LOG_DEBUG(Loader, "Name: %s", exheader_header.codeset_info.name); | ||
| 291 | LOG_DEBUG(Loader, "Program ID: %016" PRIX64, ncch_header.program_id); | ||
| 292 | LOG_DEBUG(Loader, "Code compressed: %s", is_compressed ? "yes" : "no"); | ||
| 293 | LOG_DEBUG(Loader, "Entry point: 0x%08X", entry_point); | ||
| 294 | LOG_DEBUG(Loader, "Code size: 0x%08X", code_size); | ||
| 295 | LOG_DEBUG(Loader, "Stack size: 0x%08X", stack_size); | ||
| 296 | LOG_DEBUG(Loader, "Bss size: 0x%08X", bss_size); | ||
| 297 | LOG_DEBUG(Loader, "Core version: %d", core_version); | ||
| 298 | LOG_DEBUG(Loader, "Thread priority: 0x%X", priority); | ||
| 299 | LOG_DEBUG(Loader, "Resource limit category: %d", resource_limit_category); | ||
| 300 | LOG_DEBUG(Loader, "System Mode: %d", | ||
| 301 | static_cast<int>(exheader_header.arm11_system_local_caps.system_mode)); | ||
| 302 | |||
| 303 | if (exheader_header.arm11_system_local_caps.program_id != ncch_header.program_id) { | ||
| 304 | LOG_ERROR(Loader, "ExHeader Program ID mismatch: the ROM is probably encrypted."); | ||
| 305 | return ResultStatus::ErrorEncrypted; | ||
| 306 | } | ||
| 307 | |||
| 308 | // Read ExeFS... | ||
| 309 | |||
| 310 | exefs_offset = ncch_header.exefs_offset * kBlockSize; | ||
| 311 | u32 exefs_size = ncch_header.exefs_size * kBlockSize; | ||
| 312 | |||
| 313 | LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset); | ||
| 314 | LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size); | ||
| 315 | |||
| 316 | file.Seek(exefs_offset + ncch_offset, SEEK_SET); | ||
| 317 | if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) | ||
| 318 | return ResultStatus::Error; | ||
| 319 | |||
| 320 | is_exefs_loaded = true; | ||
| 321 | return ResultStatus::Success; | ||
| 322 | } | ||
| 323 | |||
| 324 | void AppLoader_NCCH::ParseRegionLockoutInfo() { | 137 | void AppLoader_NCCH::ParseRegionLockoutInfo() { |
| 325 | std::vector<u8> smdh_buffer; | 138 | std::vector<u8> smdh_buffer; |
| 326 | if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) { | 139 | if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) { |
| @@ -339,23 +152,32 @@ void AppLoader_NCCH::ParseRegionLockoutInfo() { | |||
| 339 | } | 152 | } |
| 340 | 153 | ||
| 341 | ResultStatus AppLoader_NCCH::Load() { | 154 | ResultStatus AppLoader_NCCH::Load() { |
| 155 | u64_le ncch_program_id; | ||
| 156 | |||
| 342 | if (is_loaded) | 157 | if (is_loaded) |
| 343 | return ResultStatus::ErrorAlreadyLoaded; | 158 | return ResultStatus::ErrorAlreadyLoaded; |
| 344 | 159 | ||
| 345 | ResultStatus result = LoadExeFS(); | 160 | ResultStatus result = base_ncch.Load(); |
| 346 | if (result != ResultStatus::Success) | 161 | if (result != ResultStatus::Success) |
| 347 | return result; | 162 | return result; |
| 348 | 163 | ||
| 349 | std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_header.program_id)}; | 164 | ReadProgramId(ncch_program_id); |
| 165 | std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_program_id)}; | ||
| 350 | 166 | ||
| 351 | LOG_INFO(Loader, "Program ID: %s", program_id.c_str()); | 167 | LOG_INFO(Loader, "Program ID: %s", program_id.c_str()); |
| 352 | 168 | ||
| 169 | update_ncch.OpenFile(GetUpdateNCCHPath(ncch_program_id)); | ||
| 170 | result = update_ncch.Load(); | ||
| 171 | if (result == ResultStatus::Success) { | ||
| 172 | overlay_ncch = &update_ncch; | ||
| 173 | } | ||
| 174 | |||
| 353 | Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id); | 175 | Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id); |
| 354 | 176 | ||
| 355 | if (auto room_member = Network::GetRoomMember().lock()) { | 177 | if (auto room_member = Network::GetRoomMember().lock()) { |
| 356 | Network::GameInfo game_info; | 178 | Network::GameInfo game_info; |
| 357 | ReadTitle(game_info.name); | 179 | ReadTitle(game_info.name); |
| 358 | game_info.id = ncch_header.program_id; | 180 | game_info.id = ncch_program_id; |
| 359 | room_member->SendGameInfo(game_info); | 181 | room_member->SendGameInfo(game_info); |
| 360 | } | 182 | } |
| 361 | 183 | ||
| @@ -365,8 +187,7 @@ ResultStatus AppLoader_NCCH::Load() { | |||
| 365 | if (ResultStatus::Success != result) | 187 | if (ResultStatus::Success != result) |
| 366 | return result; | 188 | return result; |
| 367 | 189 | ||
| 368 | Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_SelfNCCH>(*this), | 190 | Service::FS::RegisterSelfNCCH(*this); |
| 369 | Service::FS::ArchiveIdCode::SelfNCCH); | ||
| 370 | 191 | ||
| 371 | ParseRegionLockoutInfo(); | 192 | ParseRegionLockoutInfo(); |
| 372 | 193 | ||
| @@ -374,61 +195,40 @@ ResultStatus AppLoader_NCCH::Load() { | |||
| 374 | } | 195 | } |
| 375 | 196 | ||
| 376 | ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) { | 197 | ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) { |
| 377 | return LoadSectionExeFS(".code", buffer); | 198 | return overlay_ncch->LoadSectionExeFS(".code", buffer); |
| 378 | } | 199 | } |
| 379 | 200 | ||
| 380 | ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) { | 201 | ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) { |
| 381 | return LoadSectionExeFS("icon", buffer); | 202 | return overlay_ncch->LoadSectionExeFS("icon", buffer); |
| 382 | } | 203 | } |
| 383 | 204 | ||
| 384 | ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) { | 205 | ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) { |
| 385 | return LoadSectionExeFS("banner", buffer); | 206 | return overlay_ncch->LoadSectionExeFS("banner", buffer); |
| 386 | } | 207 | } |
| 387 | 208 | ||
| 388 | ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) { | 209 | ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) { |
| 389 | return LoadSectionExeFS("logo", buffer); | 210 | return overlay_ncch->LoadSectionExeFS("logo", buffer); |
| 390 | } | 211 | } |
| 391 | 212 | ||
| 392 | ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) { | 213 | ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) { |
| 393 | if (!file.IsOpen()) | 214 | ResultStatus result = base_ncch.ReadProgramId(out_program_id); |
| 394 | return ResultStatus::Error; | ||
| 395 | |||
| 396 | ResultStatus result = LoadExeFS(); | ||
| 397 | if (result != ResultStatus::Success) | 215 | if (result != ResultStatus::Success) |
| 398 | return result; | 216 | return result; |
| 399 | 217 | ||
| 400 | out_program_id = ncch_header.program_id; | ||
| 401 | return ResultStatus::Success; | 218 | return ResultStatus::Success; |
| 402 | } | 219 | } |
| 403 | 220 | ||
| 404 | ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | 221 | ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, |
| 405 | u64& size) { | 222 | u64& size) { |
| 406 | if (!file.IsOpen()) | 223 | return base_ncch.ReadRomFS(romfs_file, offset, size); |
| 407 | return ResultStatus::Error; | 224 | } |
| 408 | |||
| 409 | // Check if the NCCH has a RomFS... | ||
| 410 | if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { | ||
| 411 | u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; | ||
| 412 | u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; | ||
| 413 | |||
| 414 | LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset); | ||
| 415 | LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size); | ||
| 416 | |||
| 417 | if (file.GetSize() < romfs_offset + romfs_size) | ||
| 418 | return ResultStatus::Error; | ||
| 419 | |||
| 420 | // We reopen the file, to allow its position to be independent from file's | ||
| 421 | romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb"); | ||
| 422 | if (!romfs_file->IsOpen()) | ||
| 423 | return ResultStatus::Error; | ||
| 424 | 225 | ||
| 425 | offset = romfs_offset; | 226 | ResultStatus AppLoader_NCCH::ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, |
| 426 | size = romfs_size; | 227 | u64& offset, u64& size) { |
| 228 | ResultStatus result = update_ncch.ReadRomFS(romfs_file, offset, size); | ||
| 427 | 229 | ||
| 428 | return ResultStatus::Success; | 230 | if (result != ResultStatus::Success) |
| 429 | } | 231 | return base_ncch.ReadRomFS(romfs_file, offset, size); |
| 430 | LOG_DEBUG(Loader, "NCCH has no RomFS"); | ||
| 431 | return ResultStatus::ErrorNotUsed; | ||
| 432 | } | 232 | } |
| 433 | 233 | ||
| 434 | ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) { | 234 | ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) { |
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index e40cef764..9b56465cb 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h | |||
| @@ -5,155 +5,12 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <memory> | 7 | #include <memory> |
| 8 | #include "common/bit_field.h" | ||
| 9 | #include "common/common_types.h" | 8 | #include "common/common_types.h" |
| 10 | #include "common/swap.h" | 9 | #include "common/swap.h" |
| 10 | #include "core/file_sys/ncch_container.h" | ||
| 11 | #include "core/loader/loader.h" | 11 | #include "core/loader/loader.h" |
| 12 | 12 | ||
| 13 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 13 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 14 | /// NCCH header (Note: "NCCH" appears to be a publicly unknown acronym) | ||
| 15 | |||
| 16 | struct NCCH_Header { | ||
| 17 | u8 signature[0x100]; | ||
| 18 | u32_le magic; | ||
| 19 | u32_le content_size; | ||
| 20 | u8 partition_id[8]; | ||
| 21 | u16_le maker_code; | ||
| 22 | u16_le version; | ||
| 23 | u8 reserved_0[4]; | ||
| 24 | u64_le program_id; | ||
| 25 | u8 reserved_1[0x10]; | ||
| 26 | u8 logo_region_hash[0x20]; | ||
| 27 | u8 product_code[0x10]; | ||
| 28 | u8 extended_header_hash[0x20]; | ||
| 29 | u32_le extended_header_size; | ||
| 30 | u8 reserved_2[4]; | ||
| 31 | u8 flags[8]; | ||
| 32 | u32_le plain_region_offset; | ||
| 33 | u32_le plain_region_size; | ||
| 34 | u32_le logo_region_offset; | ||
| 35 | u32_le logo_region_size; | ||
| 36 | u32_le exefs_offset; | ||
| 37 | u32_le exefs_size; | ||
| 38 | u32_le exefs_hash_region_size; | ||
| 39 | u8 reserved_3[4]; | ||
| 40 | u32_le romfs_offset; | ||
| 41 | u32_le romfs_size; | ||
| 42 | u32_le romfs_hash_region_size; | ||
| 43 | u8 reserved_4[4]; | ||
| 44 | u8 exefs_super_block_hash[0x20]; | ||
| 45 | u8 romfs_super_block_hash[0x20]; | ||
| 46 | }; | ||
| 47 | |||
| 48 | static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong"); | ||
| 49 | |||
| 50 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 51 | // ExeFS (executable file system) headers | ||
| 52 | |||
| 53 | struct ExeFs_SectionHeader { | ||
| 54 | char name[8]; | ||
| 55 | u32 offset; | ||
| 56 | u32 size; | ||
| 57 | }; | ||
| 58 | |||
| 59 | struct ExeFs_Header { | ||
| 60 | ExeFs_SectionHeader section[8]; | ||
| 61 | u8 reserved[0x80]; | ||
| 62 | u8 hashes[8][0x20]; | ||
| 63 | }; | ||
| 64 | |||
| 65 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 66 | // ExHeader (executable file system header) headers | ||
| 67 | |||
| 68 | struct ExHeader_SystemInfoFlags { | ||
| 69 | u8 reserved[5]; | ||
| 70 | u8 flag; | ||
| 71 | u8 remaster_version[2]; | ||
| 72 | }; | ||
| 73 | |||
| 74 | struct ExHeader_CodeSegmentInfo { | ||
| 75 | u32 address; | ||
| 76 | u32 num_max_pages; | ||
| 77 | u32 code_size; | ||
| 78 | }; | ||
| 79 | |||
| 80 | struct ExHeader_CodeSetInfo { | ||
| 81 | u8 name[8]; | ||
| 82 | ExHeader_SystemInfoFlags flags; | ||
| 83 | ExHeader_CodeSegmentInfo text; | ||
| 84 | u32 stack_size; | ||
| 85 | ExHeader_CodeSegmentInfo ro; | ||
| 86 | u8 reserved[4]; | ||
| 87 | ExHeader_CodeSegmentInfo data; | ||
| 88 | u32 bss_size; | ||
| 89 | }; | ||
| 90 | |||
| 91 | struct ExHeader_DependencyList { | ||
| 92 | u8 program_id[0x30][8]; | ||
| 93 | }; | ||
| 94 | |||
| 95 | struct ExHeader_SystemInfo { | ||
| 96 | u64 save_data_size; | ||
| 97 | u8 jump_id[8]; | ||
| 98 | u8 reserved_2[0x30]; | ||
| 99 | }; | ||
| 100 | |||
| 101 | struct ExHeader_StorageInfo { | ||
| 102 | u8 ext_save_data_id[8]; | ||
| 103 | u8 system_save_data_id[8]; | ||
| 104 | u8 reserved[8]; | ||
| 105 | u8 access_info[7]; | ||
| 106 | u8 other_attributes; | ||
| 107 | }; | ||
| 108 | |||
| 109 | struct ExHeader_ARM11_SystemLocalCaps { | ||
| 110 | u64_le program_id; | ||
| 111 | u32_le core_version; | ||
| 112 | u8 reserved_flags[2]; | ||
| 113 | union { | ||
| 114 | u8 flags0; | ||
| 115 | BitField<0, 2, u8> ideal_processor; | ||
| 116 | BitField<2, 2, u8> affinity_mask; | ||
| 117 | BitField<4, 4, u8> system_mode; | ||
| 118 | }; | ||
| 119 | u8 priority; | ||
| 120 | u8 resource_limit_descriptor[0x10][2]; | ||
| 121 | ExHeader_StorageInfo storage_info; | ||
| 122 | u8 service_access_control[0x20][8]; | ||
| 123 | u8 ex_service_access_control[0x2][8]; | ||
| 124 | u8 reserved[0xf]; | ||
| 125 | u8 resource_limit_category; | ||
| 126 | }; | ||
| 127 | |||
| 128 | struct ExHeader_ARM11_KernelCaps { | ||
| 129 | u32_le descriptors[28]; | ||
| 130 | u8 reserved[0x10]; | ||
| 131 | }; | ||
| 132 | |||
| 133 | struct ExHeader_ARM9_AccessControl { | ||
| 134 | u8 descriptors[15]; | ||
| 135 | u8 descversion; | ||
| 136 | }; | ||
| 137 | |||
| 138 | struct ExHeader_Header { | ||
| 139 | ExHeader_CodeSetInfo codeset_info; | ||
| 140 | ExHeader_DependencyList dependency_list; | ||
| 141 | ExHeader_SystemInfo system_info; | ||
| 142 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 143 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 144 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 145 | struct { | ||
| 146 | u8 signature[0x100]; | ||
| 147 | u8 ncch_public_key_modulus[0x100]; | ||
| 148 | ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; | ||
| 149 | ExHeader_ARM11_KernelCaps arm11_kernel_caps; | ||
| 150 | ExHeader_ARM9_AccessControl arm9_access_control; | ||
| 151 | } access_desc; | ||
| 152 | }; | ||
| 153 | |||
| 154 | static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong"); | ||
| 155 | |||
| 156 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 157 | // Loader namespace | 14 | // Loader namespace |
| 158 | 15 | ||
| 159 | namespace Loader { | 16 | namespace Loader { |
| @@ -162,7 +19,8 @@ namespace Loader { | |||
| 162 | class AppLoader_NCCH final : public AppLoader { | 19 | class AppLoader_NCCH final : public AppLoader { |
| 163 | public: | 20 | public: |
| 164 | AppLoader_NCCH(FileUtil::IOFile&& file, const std::string& filepath) | 21 | AppLoader_NCCH(FileUtil::IOFile&& file, const std::string& filepath) |
| 165 | : AppLoader(std::move(file)), filepath(filepath) {} | 22 | : AppLoader(std::move(file)), filepath(filepath), base_ncch(filepath), |
| 23 | overlay_ncch(&base_ncch) {} | ||
| 166 | 24 | ||
| 167 | /** | 25 | /** |
| 168 | * Returns the type of the file | 26 | * Returns the type of the file |
| @@ -196,48 +54,24 @@ public: | |||
| 196 | ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | 54 | ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, |
| 197 | u64& size) override; | 55 | u64& size) override; |
| 198 | 56 | ||
| 57 | ResultStatus ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, | ||
| 58 | u64& size) override; | ||
| 59 | |||
| 199 | ResultStatus ReadTitle(std::string& title) override; | 60 | ResultStatus ReadTitle(std::string& title) override; |
| 200 | 61 | ||
| 201 | private: | 62 | private: |
| 202 | /** | 63 | /** |
| 203 | * Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.) | ||
| 204 | * @param name Name of section to read out of NCCH file | ||
| 205 | * @param buffer Vector to read data into | ||
| 206 | * @return ResultStatus result of function | ||
| 207 | */ | ||
| 208 | ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer); | ||
| 209 | |||
| 210 | /** | ||
| 211 | * Loads .code section into memory for booting | 64 | * Loads .code section into memory for booting |
| 212 | * @return ResultStatus result of function | 65 | * @return ResultStatus result of function |
| 213 | */ | 66 | */ |
| 214 | ResultStatus LoadExec(); | 67 | ResultStatus LoadExec(); |
| 215 | 68 | ||
| 216 | /** | ||
| 217 | * Ensure ExeFS is loaded and ready for reading sections | ||
| 218 | * @return ResultStatus result of function | ||
| 219 | */ | ||
| 220 | ResultStatus LoadExeFS(); | ||
| 221 | |||
| 222 | /// Reads the region lockout info in the SMDH and send it to CFG service | 69 | /// Reads the region lockout info in the SMDH and send it to CFG service |
| 223 | void ParseRegionLockoutInfo(); | 70 | void ParseRegionLockoutInfo(); |
| 224 | 71 | ||
| 225 | bool is_exefs_loaded = false; | 72 | FileSys::NCCHContainer base_ncch; |
| 226 | bool is_compressed = false; | 73 | FileSys::NCCHContainer update_ncch; |
| 227 | 74 | FileSys::NCCHContainer* overlay_ncch; | |
| 228 | u32 entry_point = 0; | ||
| 229 | u32 code_size = 0; | ||
| 230 | u32 stack_size = 0; | ||
| 231 | u32 bss_size = 0; | ||
| 232 | u32 core_version = 0; | ||
| 233 | u8 priority = 0; | ||
| 234 | u8 resource_limit_category = 0; | ||
| 235 | u32 ncch_offset = 0; // Offset to NCCH header, can be 0 or after NCSD header | ||
| 236 | u32 exefs_offset = 0; | ||
| 237 | |||
| 238 | NCCH_Header ncch_header; | ||
| 239 | ExeFs_Header exefs_header; | ||
| 240 | ExHeader_Header exheader_header; | ||
| 241 | 75 | ||
| 242 | std::string filepath; | 76 | std::string filepath; |
| 243 | }; | 77 | }; |
diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 2f5cdcefe..9b394f84b 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp | |||
| @@ -9,6 +9,8 @@ | |||
| 9 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 10 | #include "common/logging/log.h" | 10 | #include "common/logging/log.h" |
| 11 | #include "common/swap.h" | 11 | #include "common/swap.h" |
| 12 | #include "core/arm/arm_interface.h" | ||
| 13 | #include "core/core.h" | ||
| 12 | #include "core/hle/kernel/memory.h" | 14 | #include "core/hle/kernel/memory.h" |
| 13 | #include "core/hle/kernel/process.h" | 15 | #include "core/hle/kernel/process.h" |
| 14 | #include "core/hle/lock.h" | 16 | #include "core/hle/lock.h" |
| @@ -22,10 +24,17 @@ namespace Memory { | |||
| 22 | static std::array<u8, Memory::VRAM_SIZE> vram; | 24 | static std::array<u8, Memory::VRAM_SIZE> vram; |
| 23 | static std::array<u8, Memory::N3DS_EXTRA_RAM_SIZE> n3ds_extra_ram; | 25 | static std::array<u8, Memory::N3DS_EXTRA_RAM_SIZE> n3ds_extra_ram; |
| 24 | 26 | ||
| 25 | PageTable* current_page_table = nullptr; | 27 | static PageTable* current_page_table = nullptr; |
| 26 | 28 | ||
| 27 | std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers() { | 29 | void SetCurrentPageTable(PageTable* page_table) { |
| 28 | return ¤t_page_table->pointers; | 30 | current_page_table = page_table; |
| 31 | if (Core::System::GetInstance().IsPoweredOn()) { | ||
| 32 | Core::CPU().PageTableChanged(); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | PageTable* GetCurrentPageTable() { | ||
| 37 | return current_page_table; | ||
| 29 | } | 38 | } |
| 30 | 39 | ||
| 31 | static void MapPages(PageTable& page_table, u32 base, u32 size, u8* memory, PageType type) { | 40 | static void MapPages(PageTable& page_table, u32 base, u32 size, u8* memory, PageType type) { |
diff --git a/src/core/memory.h b/src/core/memory.h index b228a48c2..1865bfea0 100644 --- a/src/core/memory.h +++ b/src/core/memory.h | |||
| @@ -182,7 +182,8 @@ enum : VAddr { | |||
| 182 | }; | 182 | }; |
| 183 | 183 | ||
| 184 | /// Currently active page table | 184 | /// Currently active page table |
| 185 | extern PageTable* current_page_table; | 185 | void SetCurrentPageTable(PageTable* page_table); |
| 186 | PageTable* GetCurrentPageTable(); | ||
| 186 | 187 | ||
| 187 | bool IsValidVirtualAddress(const VAddr addr); | 188 | bool IsValidVirtualAddress(const VAddr addr); |
| 188 | bool IsValidPhysicalAddress(const PAddr addr); | 189 | bool IsValidPhysicalAddress(const PAddr addr); |
| @@ -259,10 +260,4 @@ enum class FlushMode { | |||
| 259 | */ | 260 | */ |
| 260 | void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode); | 261 | void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode); |
| 261 | 262 | ||
| 262 | /** | ||
| 263 | * Dynarmic has an optimization to memory accesses when the pointer to the page exists that | ||
| 264 | * can be used by setting up the current page table as a callback. This function is used to | ||
| 265 | * retrieve the current page table for that purpose. | ||
| 266 | */ | ||
| 267 | std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers(); | ||
| 268 | } // namespace Memory | 263 | } // namespace Memory |
diff --git a/src/tests/core/arm/arm_test_common.cpp b/src/tests/core/arm/arm_test_common.cpp index 8384ce744..cfe0d503a 100644 --- a/src/tests/core/arm/arm_test_common.cpp +++ b/src/tests/core/arm/arm_test_common.cpp | |||
| @@ -21,7 +21,7 @@ TestEnvironment::TestEnvironment(bool mutable_memory_) | |||
| 21 | Memory::MapIoRegion(page_table, 0x00000000, 0x80000000, test_memory); | 21 | Memory::MapIoRegion(page_table, 0x00000000, 0x80000000, test_memory); |
| 22 | Memory::MapIoRegion(page_table, 0x80000000, 0x80000000, test_memory); | 22 | Memory::MapIoRegion(page_table, 0x80000000, 0x80000000, test_memory); |
| 23 | 23 | ||
| 24 | Memory::current_page_table = &page_table; | 24 | Memory::SetCurrentPageTable(&page_table); |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | TestEnvironment::~TestEnvironment() { | 27 | TestEnvironment::~TestEnvironment() { |
diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp index fb65a3a0a..3ab4af374 100644 --- a/src/video_core/command_processor.cpp +++ b/src/video_core/command_processor.cpp | |||
| @@ -243,6 +243,15 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) { | |||
| 243 | ASSERT(!g_state.geometry_pipeline.NeedIndexInput()); | 243 | ASSERT(!g_state.geometry_pipeline.NeedIndexInput()); |
| 244 | g_state.geometry_pipeline.Setup(shader_engine); | 244 | g_state.geometry_pipeline.Setup(shader_engine); |
| 245 | g_state.geometry_pipeline.SubmitVertex(output); | 245 | g_state.geometry_pipeline.SubmitVertex(output); |
| 246 | |||
| 247 | // TODO: If drawing after every immediate mode triangle kills performance, | ||
| 248 | // change it to flush triangles whenever a drawing config register changes | ||
| 249 | // See: https://github.com/citra-emu/citra/pull/2866#issuecomment-327011550 | ||
| 250 | VideoCore::g_renderer->Rasterizer()->DrawTriangles(); | ||
| 251 | if (g_debug_context) { | ||
| 252 | g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, | ||
| 253 | nullptr); | ||
| 254 | } | ||
| 246 | } | 255 | } |
| 247 | } | 256 | } |
| 248 | } | 257 | } |
| @@ -250,16 +259,7 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) { | |||
| 250 | } | 259 | } |
| 251 | 260 | ||
| 252 | case PICA_REG_INDEX(pipeline.gpu_mode): | 261 | case PICA_REG_INDEX(pipeline.gpu_mode): |
| 253 | if (regs.pipeline.gpu_mode == PipelineRegs::GPUMode::Configuring) { | 262 | // This register likely just enables vertex processing and doesn't need any special handling |
| 254 | MICROPROFILE_SCOPE(GPU_Drawing); | ||
| 255 | |||
| 256 | // Draw immediate mode triangles when GPU Mode is set to GPUMode::Configuring | ||
| 257 | VideoCore::g_renderer->Rasterizer()->DrawTriangles(); | ||
| 258 | |||
| 259 | if (g_debug_context) { | ||
| 260 | g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | break; | 263 | break; |
| 264 | 264 | ||
| 265 | case PICA_REG_INDEX_WORKAROUND(pipeline.command_buffer.trigger[0], 0x23c): | 265 | case PICA_REG_INDEX_WORKAROUND(pipeline.command_buffer.trigger[0], 0x23c): |
| @@ -398,6 +398,11 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) { | |||
| 398 | range.second, range.first); | 398 | range.second, range.first); |
| 399 | } | 399 | } |
| 400 | 400 | ||
| 401 | VideoCore::g_renderer->Rasterizer()->DrawTriangles(); | ||
| 402 | if (g_debug_context) { | ||
| 403 | g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr); | ||
| 404 | } | ||
| 405 | |||
| 401 | break; | 406 | break; |
| 402 | } | 407 | } |
| 403 | 408 | ||
| @@ -632,6 +637,6 @@ void ProcessCommandList(const u32* list, u32 size) { | |||
| 632 | } | 637 | } |
| 633 | } | 638 | } |
| 634 | 639 | ||
| 635 | } // namespace | 640 | } // namespace CommandProcessor |
| 636 | 641 | ||
| 637 | } // namespace | 642 | } // namespace Pica |
diff --git a/src/video_core/pica_types.h b/src/video_core/pica_types.h index 5d7e10066..2eafa7e9e 100644 --- a/src/video_core/pica_types.h +++ b/src/video_core/pica_types.h | |||
| @@ -58,11 +58,12 @@ public: | |||
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | Float<M, E> operator*(const Float<M, E>& flt) const { | 60 | Float<M, E> operator*(const Float<M, E>& flt) const { |
| 61 | if ((this->value == 0.f && !std::isnan(flt.value)) || | 61 | float result = value * flt.ToFloat32(); |
| 62 | (flt.value == 0.f && !std::isnan(this->value))) | 62 | // PICA gives 0 instead of NaN when multiplying by inf |
| 63 | // PICA gives 0 instead of NaN when multiplying by inf | 63 | if (!std::isnan(value) && !std::isnan(flt.ToFloat32())) |
| 64 | return Zero(); | 64 | if (std::isnan(result)) |
| 65 | return Float<M, E>::FromFloat32(ToFloat32() * flt.ToFloat32()); | 65 | result = 0.f; |
| 66 | return Float<M, E>::FromFloat32(result); | ||
| 66 | } | 67 | } |
| 67 | 68 | ||
| 68 | Float<M, E> operator/(const Float<M, E>& flt) const { | 69 | Float<M, E> operator/(const Float<M, E>& flt) const { |
| @@ -78,12 +79,7 @@ public: | |||
| 78 | } | 79 | } |
| 79 | 80 | ||
| 80 | Float<M, E>& operator*=(const Float<M, E>& flt) { | 81 | Float<M, E>& operator*=(const Float<M, E>& flt) { |
| 81 | if ((this->value == 0.f && !std::isnan(flt.value)) || | 82 | value = operator*(flt).value; |
| 82 | (flt.value == 0.f && !std::isnan(this->value))) | ||
| 83 | // PICA gives 0 instead of NaN when multiplying by inf | ||
| 84 | *this = Zero(); | ||
| 85 | else | ||
| 86 | value *= flt.ToFloat32(); | ||
| 87 | return *this; | 83 | return *this; |
| 88 | } | 84 | } |
| 89 | 85 | ||
diff --git a/src/video_core/utils.h b/src/video_core/utils.h index 7ce83a055..d8567f314 100644 --- a/src/video_core/utils.h +++ b/src/video_core/utils.h | |||
| @@ -8,17 +8,11 @@ | |||
| 8 | 8 | ||
| 9 | namespace VideoCore { | 9 | namespace VideoCore { |
| 10 | 10 | ||
| 11 | /** | 11 | // 8x8 Z-Order coordinate from 2D coordinates |
| 12 | * Interleave the lower 3 bits of each coordinate to get the intra-block offsets, which are | ||
| 13 | * arranged in a Z-order curve. More details on the bit manipulation at: | ||
| 14 | * https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/ | ||
| 15 | */ | ||
| 16 | static inline u32 MortonInterleave(u32 x, u32 y) { | 12 | static inline u32 MortonInterleave(u32 x, u32 y) { |
| 17 | u32 i = (x & 7) | ((y & 7) << 8); // ---- -210 | 13 | static const u32 xlut[] = {0x00, 0x01, 0x04, 0x05, 0x10, 0x11, 0x14, 0x15}; |
| 18 | i = (i ^ (i << 2)) & 0x1313; // ---2 --10 | 14 | static const u32 ylut[] = {0x00, 0x02, 0x08, 0x0a, 0x20, 0x22, 0x28, 0x2a}; |
| 19 | i = (i ^ (i << 1)) & 0x1515; // ---2 -1-0 | 15 | return xlut[x % 8] + ylut[y % 8]; |
| 20 | i = (i | (i >> 7)) & 0x3F; | ||
| 21 | return i; | ||
| 22 | } | 16 | } |
| 23 | 17 | ||
| 24 | /** | 18 | /** |