summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Lioncash2018-09-11 21:06:40 -0400
committerGravatar Lioncash2018-09-11 21:24:19 -0400
commitc061e27155a59d4563003e0f4f7364864f9b51d4 (patch)
treec9e0748edd0e9d1487f5a8e32b2490078212dde1 /src
parentMerge pull request #1291 from lioncash/default (diff)
downloadyuzu-c061e27155a59d4563003e0f4f7364864f9b51d4.tar.gz
yuzu-c061e27155a59d4563003e0f4f7364864f9b51d4.tar.xz
yuzu-c061e27155a59d4563003e0f4f7364864f9b51d4.zip
pl_u: Eliminate mutable file-scope state
Converts the PL_U internals to use the PImpl idiom and makes the state part of the Impl struct, eliminating mutable global/file state.
Diffstat (limited to '')
-rw-r--r--src/core/hle/service/ns/pl_u.cpp146
-rw-r--r--src/core/hle/service/ns/pl_u.h8
2 files changed, 88 insertions, 66 deletions
diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp
index da1c46d59..ac0eaaa8f 100644
--- a/src/core/hle/service/ns/pl_u.cpp
+++ b/src/core/hle/service/ns/pl_u.cpp
@@ -2,6 +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>
6#include <cstring>
7#include <vector>
8
5#include <FontChineseSimplified.h> 9#include <FontChineseSimplified.h>
6#include <FontChineseTraditional.h> 10#include <FontChineseTraditional.h>
7#include <FontExtendedChineseSimplified.h> 11#include <FontExtendedChineseSimplified.h>
@@ -9,14 +13,19 @@
9#include <FontNintendoExtended.h> 13#include <FontNintendoExtended.h>
10#include <FontStandard.h> 14#include <FontStandard.h>
11 15
16#include "common/assert.h"
12#include "common/common_paths.h" 17#include "common/common_paths.h"
18#include "common/common_types.h"
13#include "common/file_util.h" 19#include "common/file_util.h"
20#include "common/logging/log.h"
21#include "common/swap.h"
14#include "core/core.h" 22#include "core/core.h"
15#include "core/file_sys/content_archive.h" 23#include "core/file_sys/content_archive.h"
16#include "core/file_sys/nca_metadata.h" 24#include "core/file_sys/nca_metadata.h"
17#include "core/file_sys/registered_cache.h" 25#include "core/file_sys/registered_cache.h"
18#include "core/file_sys/romfs.h" 26#include "core/file_sys/romfs.h"
19#include "core/hle/ipc_helpers.h" 27#include "core/hle/ipc_helpers.h"
28#include "core/hle/kernel/shared_memory.h"
20#include "core/hle/service/filesystem/filesystem.h" 29#include "core/hle/service/filesystem/filesystem.h"
21#include "core/hle/service/ns/pl_u.h" 30#include "core/hle/service/ns/pl_u.h"
22 31
@@ -35,49 +44,41 @@ struct FontRegion {
35 u32 size; 44 u32 size;
36}; 45};
37 46
38static constexpr std::array<std::pair<FontArchives, const char*>, 7> SHARED_FONTS{ 47constexpr std::array<std::pair<FontArchives, const char*>, 7> SHARED_FONTS{
39 std::make_pair(FontArchives::Standard, "nintendo_udsg-r_std_003.bfttf"), 48 std::make_pair(FontArchives::Standard, "nintendo_udsg-r_std_003.bfttf"),
40 std::make_pair(FontArchives::ChineseSimple, "nintendo_udsg-r_org_zh-cn_003.bfttf"), 49 std::make_pair(FontArchives::ChineseSimple, "nintendo_udsg-r_org_zh-cn_003.bfttf"),
41 std::make_pair(FontArchives::ChineseSimple, "nintendo_udsg-r_ext_zh-cn_003.bfttf"), 50 std::make_pair(FontArchives::ChineseSimple, "nintendo_udsg-r_ext_zh-cn_003.bfttf"),
42 std::make_pair(FontArchives::ChineseTraditional, "nintendo_udjxh-db_zh-tw_003.bfttf"), 51 std::make_pair(FontArchives::ChineseTraditional, "nintendo_udjxh-db_zh-tw_003.bfttf"),
43 std::make_pair(FontArchives::Korean, "nintendo_udsg-r_ko_003.bfttf"), 52 std::make_pair(FontArchives::Korean, "nintendo_udsg-r_ko_003.bfttf"),
44 std::make_pair(FontArchives::Extension, "nintendo_ext_003.bfttf"), 53 std::make_pair(FontArchives::Extension, "nintendo_ext_003.bfttf"),
45 std::make_pair(FontArchives::Extension, "nintendo_ext2_003.bfttf")}; 54 std::make_pair(FontArchives::Extension, "nintendo_ext2_003.bfttf"),
55};
46 56
47static constexpr std::array<const char*, 7> SHARED_FONTS_TTF{"FontStandard.ttf", 57constexpr std::array<const char*, 7> SHARED_FONTS_TTF{
48 "FontChineseSimplified.ttf", 58 "FontStandard.ttf",
49 "FontExtendedChineseSimplified.ttf", 59 "FontChineseSimplified.ttf",
50 "FontChineseTraditional.ttf", 60 "FontExtendedChineseSimplified.ttf",
51 "FontKorean.ttf", 61 "FontChineseTraditional.ttf",
52 "FontNintendoExtended.ttf", 62 "FontKorean.ttf",
53 "FontNintendoExtended2.ttf"}; 63 "FontNintendoExtended.ttf",
64 "FontNintendoExtended2.ttf",
65};
54 66
55// The below data is specific to shared font data dumped from Switch on f/w 2.2 67// The below data is specific to shared font data dumped from Switch on f/w 2.2
56// Virtual address and offsets/sizes likely will vary by dump 68// Virtual address and offsets/sizes likely will vary by dump
57static constexpr VAddr SHARED_FONT_MEM_VADDR{0x00000009d3016000ULL}; 69constexpr VAddr SHARED_FONT_MEM_VADDR{0x00000009d3016000ULL};
58static constexpr u32 EXPECTED_RESULT{ 70constexpr u32 EXPECTED_RESULT{0x7f9a0218}; // What we expect the decrypted bfttf first 4 bytes to be
59 0x7f9a0218}; // What we expect the decrypted bfttf first 4 bytes to be 71constexpr u32 EXPECTED_MAGIC{0x36f81a1e}; // What we expect the encrypted bfttf first 4 bytes to be
60static constexpr u32 EXPECTED_MAGIC{ 72constexpr u64 SHARED_FONT_MEM_SIZE{0x1100000};
61 0x36f81a1e}; // What we expect the encrypted bfttf first 4 bytes to be 73constexpr FontRegion EMPTY_REGION{0, 0};
62static constexpr u64 SHARED_FONT_MEM_SIZE{0x1100000};
63static constexpr FontRegion EMPTY_REGION{0, 0};
64std::vector<FontRegion>
65 SHARED_FONT_REGIONS{}; // Automatically populated based on shared_fonts dump or system archives
66
67const FontRegion& GetSharedFontRegion(size_t index) {
68 if (index >= SHARED_FONT_REGIONS.size() || SHARED_FONT_REGIONS.empty()) {
69 // No font fallback
70 return EMPTY_REGION;
71 }
72 return SHARED_FONT_REGIONS.at(index);
73}
74 74
75enum class LoadState : u32 { 75enum class LoadState : u32 {
76 Loading = 0, 76 Loading = 0,
77 Done = 1, 77 Done = 1,
78}; 78};
79 79
80void DecryptSharedFont(const std::vector<u32>& input, std::vector<u8>& output, size_t& offset) { 80static void DecryptSharedFont(const std::vector<u32>& input, std::vector<u8>& output,
81 size_t& offset) {
81 ASSERT_MSG(offset + (input.size() * sizeof(u32)) < SHARED_FONT_MEM_SIZE, 82 ASSERT_MSG(offset + (input.size() * sizeof(u32)) < SHARED_FONT_MEM_SIZE,
82 "Shared fonts exceeds 17mb!"); 83 "Shared fonts exceeds 17mb!");
83 ASSERT_MSG(input[0] == EXPECTED_MAGIC, "Failed to derive key, unexpected magic number"); 84 ASSERT_MSG(input[0] == EXPECTED_MAGIC, "Failed to derive key, unexpected magic number");
@@ -104,28 +105,52 @@ static void EncryptSharedFont(const std::vector<u8>& input, std::vector<u8>& out
104 offset += input.size() + (sizeof(u32) * 2); 105 offset += input.size() + (sizeof(u32) * 2);
105} 106}
106 107
108// Helper function to make BuildSharedFontsRawRegions a bit nicer
107static u32 GetU32Swapped(const u8* data) { 109static u32 GetU32Swapped(const u8* data) {
108 u32 value; 110 u32 value;
109 std::memcpy(&value, data, sizeof(value)); 111 std::memcpy(&value, data, sizeof(value));
110 return Common::swap32(value); // Helper function to make BuildSharedFontsRawRegions a bit nicer 112 return Common::swap32(value);
111} 113}
112 114
113void BuildSharedFontsRawRegions(const std::vector<u8>& input) { 115struct PL_U::Impl {
114 unsigned cur_offset = 0; // As we can derive the xor key we can just populate the offsets based 116 const FontRegion& GetSharedFontRegion(size_t index) const {
115 // on the shared memory dump 117 if (index >= shared_font_regions.size() || shared_font_regions.empty()) {
116 for (size_t i = 0; i < SHARED_FONTS.size(); i++) { 118 // No font fallback
117 // Out of shared fonts/Invalid font 119 return EMPTY_REGION;
118 if (GetU32Swapped(input.data() + cur_offset) != EXPECTED_RESULT) 120 }
119 break; 121 return shared_font_regions.at(index);
120 const u32 KEY = GetU32Swapped(input.data() + cur_offset) ^
121 EXPECTED_MAGIC; // Derive key withing inverse xor
122 const u32 SIZE = GetU32Swapped(input.data() + cur_offset + 4) ^ KEY;
123 SHARED_FONT_REGIONS.push_back(FontRegion{cur_offset + 8, SIZE});
124 cur_offset += SIZE + 8;
125 } 122 }
126}
127 123
128PL_U::PL_U() : ServiceFramework("pl:u") { 124 void BuildSharedFontsRawRegions(const std::vector<u8>& input) {
125 // As we can derive the xor key we can just populate the offsets
126 // based on the shared memory dump
127 unsigned cur_offset = 0;
128
129 for (size_t i = 0; i < SHARED_FONTS.size(); i++) {
130 // Out of shared fonts/invalid font
131 if (GetU32Swapped(input.data() + cur_offset) != EXPECTED_RESULT) {
132 break;
133 }
134
135 // Derive key withing inverse xor
136 const u32 KEY = GetU32Swapped(input.data() + cur_offset) ^ EXPECTED_MAGIC;
137 const u32 SIZE = GetU32Swapped(input.data() + cur_offset + 4) ^ KEY;
138 shared_font_regions.push_back(FontRegion{cur_offset + 8, SIZE});
139 cur_offset += SIZE + 8;
140 }
141 }
142
143 /// Handle to shared memory region designated for a shared font
144 Kernel::SharedPtr<Kernel::SharedMemory> shared_font_mem;
145
146 /// Backing memory for the shared font data
147 std::shared_ptr<std::vector<u8>> shared_font;
148
149 // Automatically populated based on shared_fonts dump or system archives.
150 std::vector<FontRegion> shared_font_regions;
151};
152
153PL_U::PL_U() : ServiceFramework("pl:u"), impl{std::make_unique<Impl>()} {
129 static const FunctionInfo functions[] = { 154 static const FunctionInfo functions[] = {
130 {0, &PL_U::RequestLoad, "RequestLoad"}, 155 {0, &PL_U::RequestLoad, "RequestLoad"},
131 {1, &PL_U::GetLoadState, "GetLoadState"}, 156 {1, &PL_U::GetLoadState, "GetLoadState"},
@@ -141,7 +166,7 @@ PL_U::PL_U() : ServiceFramework("pl:u") {
141 // Rebuild shared fonts from data ncas 166 // Rebuild shared fonts from data ncas
142 if (nand->HasEntry(static_cast<u64>(FontArchives::Standard), 167 if (nand->HasEntry(static_cast<u64>(FontArchives::Standard),
143 FileSys::ContentRecordType::Data)) { 168 FileSys::ContentRecordType::Data)) {
144 shared_font = std::make_shared<std::vector<u8>>(SHARED_FONT_MEM_SIZE); 169 impl->shared_font = std::make_shared<std::vector<u8>>(SHARED_FONT_MEM_SIZE);
145 for (auto font : SHARED_FONTS) { 170 for (auto font : SHARED_FONTS) {
146 const auto nca = 171 const auto nca =
147 nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data); 172 nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data);
@@ -177,12 +202,12 @@ PL_U::PL_U() : ServiceFramework("pl:u") {
177 static_cast<u32>(offset + 8), 202 static_cast<u32>(offset + 8),
178 static_cast<u32>((font_data_u32.size() * sizeof(u32)) - 203 static_cast<u32>((font_data_u32.size() * sizeof(u32)) -
179 8)}; // Font offset and size do not account for the header 204 8)}; // Font offset and size do not account for the header
180 DecryptSharedFont(font_data_u32, *shared_font, offset); 205 DecryptSharedFont(font_data_u32, *impl->shared_font, offset);
181 SHARED_FONT_REGIONS.push_back(region); 206 impl->shared_font_regions.push_back(region);
182 } 207 }
183 208
184 } else { 209 } else {
185 shared_font = std::make_shared<std::vector<u8>>( 210 impl->shared_font = std::make_shared<std::vector<u8>>(
186 SHARED_FONT_MEM_SIZE); // Shared memory needs to always be allocated and a fixed size 211 SHARED_FONT_MEM_SIZE); // Shared memory needs to always be allocated and a fixed size
187 212
188 const std::string user_path = FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir); 213 const std::string user_path = FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir);
@@ -206,8 +231,8 @@ PL_U::PL_U() : ServiceFramework("pl:u") {
206 static_cast<u32>(offset + 8), 231 static_cast<u32>(offset + 8),
207 static_cast<u32>(ttf_bytes.size())}; // Font offset and size do not account 232 static_cast<u32>(ttf_bytes.size())}; // Font offset and size do not account
208 // for the header 233 // for the header
209 EncryptSharedFont(ttf_bytes, *shared_font, offset); 234 EncryptSharedFont(ttf_bytes, *impl->shared_font, offset);
210 SHARED_FONT_REGIONS.push_back(region); 235 impl->shared_font_regions.push_back(region);
211 } else { 236 } else {
212 LOG_WARNING(Service_NS, "Unable to load font: {}", font_ttf); 237 LOG_WARNING(Service_NS, "Unable to load font: {}", font_ttf);
213 } 238 }
@@ -222,8 +247,8 @@ PL_U::PL_U() : ServiceFramework("pl:u") {
222 if (file.IsOpen()) { 247 if (file.IsOpen()) {
223 // Read shared font data 248 // Read shared font data
224 ASSERT(file.GetSize() == SHARED_FONT_MEM_SIZE); 249 ASSERT(file.GetSize() == SHARED_FONT_MEM_SIZE);
225 file.ReadBytes(shared_font->data(), shared_font->size()); 250 file.ReadBytes(impl->shared_font->data(), impl->shared_font->size());
226 BuildSharedFontsRawRegions(*shared_font); 251 impl->BuildSharedFontsRawRegions(*impl->shared_font);
227 } else { 252 } else {
228 LOG_WARNING(Service_NS, 253 LOG_WARNING(Service_NS,
229 "Shared Font file missing. Loading open source replacement from memory"); 254 "Shared Font file missing. Loading open source replacement from memory");
@@ -240,8 +265,8 @@ PL_U::PL_U() : ServiceFramework("pl:u") {
240 for (const std::vector<u8>& font_ttf : open_source_shared_fonts_ttf) { 265 for (const std::vector<u8>& font_ttf : open_source_shared_fonts_ttf) {
241 const FontRegion region{static_cast<u32>(offset + 8), 266 const FontRegion region{static_cast<u32>(offset + 8),
242 static_cast<u32>(font_ttf.size())}; 267 static_cast<u32>(font_ttf.size())};
243 EncryptSharedFont(font_ttf, *shared_font, offset); 268 EncryptSharedFont(font_ttf, *impl->shared_font, offset);
244 SHARED_FONT_REGIONS.push_back(region); 269 impl->shared_font_regions.push_back(region);
245 } 270 }
246 } 271 }
247 } 272 }
@@ -275,7 +300,7 @@ void PL_U::GetSize(Kernel::HLERequestContext& ctx) {
275 LOG_DEBUG(Service_NS, "called, font_id={}", font_id); 300 LOG_DEBUG(Service_NS, "called, font_id={}", font_id);
276 IPC::ResponseBuilder rb{ctx, 3}; 301 IPC::ResponseBuilder rb{ctx, 3};
277 rb.Push(RESULT_SUCCESS); 302 rb.Push(RESULT_SUCCESS);
278 rb.Push<u32>(GetSharedFontRegion(font_id).size); 303 rb.Push<u32>(impl->GetSharedFontRegion(font_id).size);
279} 304}
280 305
281void PL_U::GetSharedMemoryAddressOffset(Kernel::HLERequestContext& ctx) { 306void PL_U::GetSharedMemoryAddressOffset(Kernel::HLERequestContext& ctx) {
@@ -285,17 +310,18 @@ void PL_U::GetSharedMemoryAddressOffset(Kernel::HLERequestContext& ctx) {
285 LOG_DEBUG(Service_NS, "called, font_id={}", font_id); 310 LOG_DEBUG(Service_NS, "called, font_id={}", font_id);
286 IPC::ResponseBuilder rb{ctx, 3}; 311 IPC::ResponseBuilder rb{ctx, 3};
287 rb.Push(RESULT_SUCCESS); 312 rb.Push(RESULT_SUCCESS);
288 rb.Push<u32>(GetSharedFontRegion(font_id).offset); 313 rb.Push<u32>(impl->GetSharedFontRegion(font_id).offset);
289} 314}
290 315
291void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) { 316void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) {
292 // Map backing memory for the font data 317 // Map backing memory for the font data
293 Core::CurrentProcess()->vm_manager.MapMemoryBlock( 318 Core::CurrentProcess()->vm_manager.MapMemoryBlock(SHARED_FONT_MEM_VADDR, impl->shared_font, 0,
294 SHARED_FONT_MEM_VADDR, shared_font, 0, SHARED_FONT_MEM_SIZE, Kernel::MemoryState::Shared); 319 SHARED_FONT_MEM_SIZE,
320 Kernel::MemoryState::Shared);
295 321
296 // Create shared font memory object 322 // Create shared font memory object
297 auto& kernel = Core::System::GetInstance().Kernel(); 323 auto& kernel = Core::System::GetInstance().Kernel();
298 shared_font_mem = Kernel::SharedMemory::Create( 324 impl->shared_font_mem = Kernel::SharedMemory::Create(
299 kernel, Core::CurrentProcess(), SHARED_FONT_MEM_SIZE, Kernel::MemoryPermission::ReadWrite, 325 kernel, Core::CurrentProcess(), SHARED_FONT_MEM_SIZE, Kernel::MemoryPermission::ReadWrite,
300 Kernel::MemoryPermission::Read, SHARED_FONT_MEM_VADDR, Kernel::MemoryRegion::BASE, 326 Kernel::MemoryPermission::Read, SHARED_FONT_MEM_VADDR, Kernel::MemoryRegion::BASE,
301 "PL_U:shared_font_mem"); 327 "PL_U:shared_font_mem");
@@ -303,7 +329,7 @@ void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) {
303 LOG_DEBUG(Service_NS, "called"); 329 LOG_DEBUG(Service_NS, "called");
304 IPC::ResponseBuilder rb{ctx, 2, 1}; 330 IPC::ResponseBuilder rb{ctx, 2, 1};
305 rb.Push(RESULT_SUCCESS); 331 rb.Push(RESULT_SUCCESS);
306 rb.PushCopyObjects(shared_font_mem); 332 rb.PushCopyObjects(impl->shared_font_mem);
307} 333}
308 334
309void PL_U::GetSharedFontInOrderOfPriority(Kernel::HLERequestContext& ctx) { 335void PL_U::GetSharedFontInOrderOfPriority(Kernel::HLERequestContext& ctx) {
@@ -316,9 +342,9 @@ void PL_U::GetSharedFontInOrderOfPriority(Kernel::HLERequestContext& ctx) {
316 std::vector<u32> font_sizes; 342 std::vector<u32> font_sizes;
317 343
318 // TODO(ogniK): Have actual priority order 344 // TODO(ogniK): Have actual priority order
319 for (size_t i = 0; i < SHARED_FONT_REGIONS.size(); i++) { 345 for (size_t i = 0; i < impl->shared_font_regions.size(); i++) {
320 font_codes.push_back(static_cast<u32>(i)); 346 font_codes.push_back(static_cast<u32>(i));
321 auto region = GetSharedFontRegion(i); 347 auto region = impl->GetSharedFontRegion(i);
322 font_offsets.push_back(region.offset); 348 font_offsets.push_back(region.offset);
323 font_sizes.push_back(region.size); 349 font_sizes.push_back(region.size);
324 } 350 }
diff --git a/src/core/hle/service/ns/pl_u.h b/src/core/hle/service/ns/pl_u.h
index 296c3db05..253f26a2a 100644
--- a/src/core/hle/service/ns/pl_u.h
+++ b/src/core/hle/service/ns/pl_u.h
@@ -5,7 +5,6 @@
5#pragma once 5#pragma once
6 6
7#include <memory> 7#include <memory>
8#include "core/hle/kernel/shared_memory.h"
9#include "core/hle/service/service.h" 8#include "core/hle/service/service.h"
10 9
11namespace Service::NS { 10namespace Service::NS {
@@ -23,11 +22,8 @@ private:
23 void GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx); 22 void GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx);
24 void GetSharedFontInOrderOfPriority(Kernel::HLERequestContext& ctx); 23 void GetSharedFontInOrderOfPriority(Kernel::HLERequestContext& ctx);
25 24
26 /// Handle to shared memory region designated for a shared font 25 struct Impl;
27 Kernel::SharedPtr<Kernel::SharedMemory> shared_font_mem; 26 std::unique_ptr<Impl> impl;
28
29 /// Backing memory for the shared font data
30 std::shared_ptr<std::vector<u8>> shared_font;
31}; 27};
32 28
33} // namespace Service::NS 29} // namespace Service::NS