diff options
| author | 2019-09-22 16:09:22 +1000 | |
|---|---|---|
| committer | 2019-09-22 16:09:22 +1000 | |
| commit | fa1c60c33ef88c6cd0b72da46842dc9098db712d (patch) | |
| tree | 0e0ac1e013ea4003d116b30ee968eaf26005d210 /src/core/hle | |
| parent | Merge pull request #2886 from MysticExile/dynarmic (diff) | |
| parent | pl_u: Use kernel physical memory (diff) | |
| download | yuzu-fa1c60c33ef88c6cd0b72da46842dc9098db712d.tar.gz yuzu-fa1c60c33ef88c6cd0b72da46842dc9098db712d.tar.xz yuzu-fa1c60c33ef88c6cd0b72da46842dc9098db712d.zip | |
Merge pull request #2709 from DarkLordZach/oss-ext-fonts-1
system_archive: Move shared font data to system_archive and fix extended font data
Diffstat (limited to 'src/core/hle')
| -rw-r--r-- | src/core/hle/service/ns/pl_u.cpp | 164 | ||||
| -rw-r--r-- | src/core/hle/service/ns/pl_u.h | 3 |
2 files changed, 50 insertions, 117 deletions
diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp index 9d49f36e8..8f0c6bc07 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp | |||
| @@ -6,13 +6,6 @@ | |||
| 6 | #include <cstring> | 6 | #include <cstring> |
| 7 | #include <vector> | 7 | #include <vector> |
| 8 | 8 | ||
| 9 | #include <FontChineseSimplified.h> | ||
| 10 | #include <FontChineseTraditional.h> | ||
| 11 | #include <FontExtendedChineseSimplified.h> | ||
| 12 | #include <FontKorean.h> | ||
| 13 | #include <FontNintendoExtended.h> | ||
| 14 | #include <FontStandard.h> | ||
| 15 | |||
| 16 | #include "common/assert.h" | 9 | #include "common/assert.h" |
| 17 | #include "common/common_paths.h" | 10 | #include "common/common_paths.h" |
| 18 | #include "common/common_types.h" | 11 | #include "common/common_types.h" |
| @@ -24,6 +17,7 @@ | |||
| 24 | #include "core/file_sys/nca_metadata.h" | 17 | #include "core/file_sys/nca_metadata.h" |
| 25 | #include "core/file_sys/registered_cache.h" | 18 | #include "core/file_sys/registered_cache.h" |
| 26 | #include "core/file_sys/romfs.h" | 19 | #include "core/file_sys/romfs.h" |
| 20 | #include "core/file_sys/system_archive/system_archive.h" | ||
| 27 | #include "core/hle/ipc_helpers.h" | 21 | #include "core/hle/ipc_helpers.h" |
| 28 | #include "core/hle/kernel/shared_memory.h" | 22 | #include "core/hle/kernel/shared_memory.h" |
| 29 | #include "core/hle/service/filesystem/filesystem.h" | 23 | #include "core/hle/service/filesystem/filesystem.h" |
| @@ -94,15 +88,16 @@ static void DecryptSharedFont(const std::vector<u32>& input, Kernel::PhysicalMem | |||
| 94 | offset += transformed_font.size() * sizeof(u32); | 88 | offset += transformed_font.size() * sizeof(u32); |
| 95 | } | 89 | } |
| 96 | 90 | ||
| 97 | static void EncryptSharedFont(const std::vector<u8>& input, Kernel::PhysicalMemory& output, | 91 | void EncryptSharedFont(const std::vector<u8>& input, Kernel::PhysicalMemory& output) { |
| 98 | std::size_t& offset) { | 92 | ASSERT_MSG(input.size() * sizeof(u32) < SHARED_FONT_MEM_SIZE, "Shared fonts exceeds 17mb!"); |
| 99 | ASSERT_MSG(offset + input.size() + 8 < SHARED_FONT_MEM_SIZE, "Shared fonts exceeds 17mb!"); | 93 | |
| 100 | const u32 KEY = EXPECTED_MAGIC ^ EXPECTED_RESULT; | 94 | const auto key = Common::swap32(EXPECTED_RESULT ^ EXPECTED_MAGIC); |
| 101 | std::memcpy(output.data() + offset, &EXPECTED_RESULT, sizeof(u32)); // Magic header | 95 | std::vector<u32> transformed_font(input.size() + 2); |
| 102 | const u32 ENC_SIZE = static_cast<u32>(input.size()) ^ KEY; | 96 | transformed_font[0] = Common::swap32(EXPECTED_MAGIC); |
| 103 | std::memcpy(output.data() + offset + sizeof(u32), &ENC_SIZE, sizeof(u32)); | 97 | transformed_font[1] = Common::swap32(input.size() * sizeof(u32)) ^ key; |
| 104 | std::memcpy(output.data() + offset + (sizeof(u32) * 2), input.data(), input.size()); | 98 | std::transform(input.begin(), input.end(), transformed_font.begin() + 2, |
| 105 | offset += input.size() + (sizeof(u32) * 2); | 99 | [key](u32 in) { return in ^ key; }); |
| 100 | std::memcpy(output.data(), transformed_font.data(), transformed_font.size() * sizeof(u32)); | ||
| 106 | } | 101 | } |
| 107 | 102 | ||
| 108 | // Helper function to make BuildSharedFontsRawRegions a bit nicer | 103 | // Helper function to make BuildSharedFontsRawRegions a bit nicer |
| @@ -164,114 +159,49 @@ PL_U::PL_U(FileSystem::FileSystemController& fsc) | |||
| 164 | // Attempt to load shared font data from disk | 159 | // Attempt to load shared font data from disk |
| 165 | const auto* nand = fsc.GetSystemNANDContents(); | 160 | const auto* nand = fsc.GetSystemNANDContents(); |
| 166 | std::size_t offset = 0; | 161 | std::size_t offset = 0; |
| 167 | // Rebuild shared fonts from data ncas | 162 | // Rebuild shared fonts from data ncas or synthesize |
| 168 | if (nand->HasEntry(static_cast<u64>(FontArchives::Standard), | 163 | |
| 169 | FileSys::ContentRecordType::Data)) { | 164 | impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(SHARED_FONT_MEM_SIZE); |
| 170 | impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(SHARED_FONT_MEM_SIZE); | 165 | for (auto font : SHARED_FONTS) { |
| 171 | for (auto font : SHARED_FONTS) { | 166 | FileSys::VirtualFile romfs; |
| 172 | const auto nca = | 167 | const auto nca = |
| 173 | nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data); | 168 | nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data); |
| 174 | if (!nca) { | 169 | if (nca) { |
| 175 | LOG_ERROR(Service_NS, "Failed to find {:016X}! Skipping", | 170 | romfs = nca->GetRomFS(); |
| 176 | static_cast<u64>(font.first)); | ||
| 177 | continue; | ||
| 178 | } | ||
| 179 | const auto romfs = nca->GetRomFS(); | ||
| 180 | if (!romfs) { | ||
| 181 | LOG_ERROR(Service_NS, "{:016X} has no RomFS! Skipping", | ||
| 182 | static_cast<u64>(font.first)); | ||
| 183 | continue; | ||
| 184 | } | ||
| 185 | const auto extracted_romfs = FileSys::ExtractRomFS(romfs); | ||
| 186 | if (!extracted_romfs) { | ||
| 187 | LOG_ERROR(Service_NS, "Failed to extract RomFS for {:016X}! Skipping", | ||
| 188 | static_cast<u64>(font.first)); | ||
| 189 | continue; | ||
| 190 | } | ||
| 191 | const auto font_fp = extracted_romfs->GetFile(font.second); | ||
| 192 | if (!font_fp) { | ||
| 193 | LOG_ERROR(Service_NS, "{:016X} has no file \"{}\"! Skipping", | ||
| 194 | static_cast<u64>(font.first), font.second); | ||
| 195 | continue; | ||
| 196 | } | ||
| 197 | std::vector<u32> font_data_u32(font_fp->GetSize() / sizeof(u32)); | ||
| 198 | font_fp->ReadBytes<u32>(font_data_u32.data(), font_fp->GetSize()); | ||
| 199 | // We need to be BigEndian as u32s for the xor encryption | ||
| 200 | std::transform(font_data_u32.begin(), font_data_u32.end(), font_data_u32.begin(), | ||
| 201 | Common::swap32); | ||
| 202 | FontRegion region{ | ||
| 203 | static_cast<u32>(offset + 8), | ||
| 204 | static_cast<u32>((font_data_u32.size() * sizeof(u32)) - | ||
| 205 | 8)}; // Font offset and size do not account for the header | ||
| 206 | DecryptSharedFont(font_data_u32, *impl->shared_font, offset); | ||
| 207 | impl->shared_font_regions.push_back(region); | ||
| 208 | } | 171 | } |
| 209 | 172 | ||
| 210 | } else { | 173 | if (!romfs) { |
| 211 | impl->shared_font = std::make_shared<Kernel::PhysicalMemory>( | 174 | romfs = FileSys::SystemArchive::SynthesizeSystemArchive(static_cast<u64>(font.first)); |
| 212 | SHARED_FONT_MEM_SIZE); // Shared memory needs to always be allocated and a fixed size | 175 | } |
| 213 | |||
| 214 | const std::string user_path = FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir); | ||
| 215 | const std::string filepath{user_path + SHARED_FONT}; | ||
| 216 | 176 | ||
| 217 | // Create path if not already created | 177 | if (!romfs) { |
| 218 | if (!FileUtil::CreateFullPath(filepath)) { | 178 | LOG_ERROR(Service_NS, "Failed to find or synthesize {:016X}! Skipping", |
| 219 | LOG_ERROR(Service_NS, "Failed to create sharedfonts path \"{}\"!", filepath); | 179 | static_cast<u64>(font.first)); |
| 220 | return; | 180 | continue; |
| 221 | } | 181 | } |
| 222 | 182 | ||
| 223 | bool using_ttf = false; | 183 | const auto extracted_romfs = FileSys::ExtractRomFS(romfs); |
| 224 | for (const char* font_ttf : SHARED_FONTS_TTF) { | 184 | if (!extracted_romfs) { |
| 225 | if (FileUtil::Exists(user_path + font_ttf)) { | 185 | LOG_ERROR(Service_NS, "Failed to extract RomFS for {:016X}! Skipping", |
| 226 | using_ttf = true; | 186 | static_cast<u64>(font.first)); |
| 227 | FileUtil::IOFile file(user_path + font_ttf, "rb"); | 187 | continue; |
| 228 | if (file.IsOpen()) { | ||
| 229 | std::vector<u8> ttf_bytes(file.GetSize()); | ||
| 230 | file.ReadBytes<u8>(ttf_bytes.data(), ttf_bytes.size()); | ||
| 231 | FontRegion region{ | ||
| 232 | static_cast<u32>(offset + 8), | ||
| 233 | static_cast<u32>(ttf_bytes.size())}; // Font offset and size do not account | ||
| 234 | // for the header | ||
| 235 | EncryptSharedFont(ttf_bytes, *impl->shared_font, offset); | ||
| 236 | impl->shared_font_regions.push_back(region); | ||
| 237 | } else { | ||
| 238 | LOG_WARNING(Service_NS, "Unable to load font: {}", font_ttf); | ||
| 239 | } | ||
| 240 | } else if (using_ttf) { | ||
| 241 | LOG_WARNING(Service_NS, "Unable to find font: {}", font_ttf); | ||
| 242 | } | ||
| 243 | } | 188 | } |
| 244 | if (using_ttf) | 189 | const auto font_fp = extracted_romfs->GetFile(font.second); |
| 245 | return; | 190 | if (!font_fp) { |
| 246 | FileUtil::IOFile file(filepath, "rb"); | 191 | LOG_ERROR(Service_NS, "{:016X} has no file \"{}\"! Skipping", |
| 247 | 192 | static_cast<u64>(font.first), font.second); | |
| 248 | if (file.IsOpen()) { | 193 | continue; |
| 249 | // Read shared font data | ||
| 250 | ASSERT(file.GetSize() == SHARED_FONT_MEM_SIZE); | ||
| 251 | file.ReadBytes(impl->shared_font->data(), impl->shared_font->size()); | ||
| 252 | impl->BuildSharedFontsRawRegions(*impl->shared_font); | ||
| 253 | } else { | ||
| 254 | LOG_WARNING(Service_NS, | ||
| 255 | "Shared Font file missing. Loading open source replacement from memory"); | ||
| 256 | |||
| 257 | // clang-format off | ||
| 258 | const std::vector<std::vector<u8>> open_source_shared_fonts_ttf = { | ||
| 259 | {std::begin(FontChineseSimplified), std::end(FontChineseSimplified)}, | ||
| 260 | {std::begin(FontChineseTraditional), std::end(FontChineseTraditional)}, | ||
| 261 | {std::begin(FontExtendedChineseSimplified), std::end(FontExtendedChineseSimplified)}, | ||
| 262 | {std::begin(FontKorean), std::end(FontKorean)}, | ||
| 263 | {std::begin(FontNintendoExtended), std::end(FontNintendoExtended)}, | ||
| 264 | {std::begin(FontStandard), std::end(FontStandard)}, | ||
| 265 | }; | ||
| 266 | // clang-format on | ||
| 267 | |||
| 268 | for (const std::vector<u8>& font_ttf : open_source_shared_fonts_ttf) { | ||
| 269 | const FontRegion region{static_cast<u32>(offset + 8), | ||
| 270 | static_cast<u32>(font_ttf.size())}; | ||
| 271 | EncryptSharedFont(font_ttf, *impl->shared_font, offset); | ||
| 272 | impl->shared_font_regions.push_back(region); | ||
| 273 | } | ||
| 274 | } | 194 | } |
| 195 | std::vector<u32> font_data_u32(font_fp->GetSize() / sizeof(u32)); | ||
| 196 | font_fp->ReadBytes<u32>(font_data_u32.data(), font_fp->GetSize()); | ||
| 197 | // We need to be BigEndian as u32s for the xor encryption | ||
| 198 | std::transform(font_data_u32.begin(), font_data_u32.end(), font_data_u32.begin(), | ||
| 199 | Common::swap32); | ||
| 200 | // Font offset and size do not account for the header | ||
| 201 | const FontRegion region{static_cast<u32>(offset + 8), | ||
| 202 | static_cast<u32>((font_data_u32.size() * sizeof(u32)) - 8)}; | ||
| 203 | DecryptSharedFont(font_data_u32, *impl->shared_font, offset); | ||
| 204 | impl->shared_font_regions.push_back(region); | ||
| 275 | } | 205 | } |
| 276 | } | 206 | } |
| 277 | 207 | ||
diff --git a/src/core/hle/service/ns/pl_u.h b/src/core/hle/service/ns/pl_u.h index 35ca424d2..7e9fe6220 100644 --- a/src/core/hle/service/ns/pl_u.h +++ b/src/core/hle/service/ns/pl_u.h | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <memory> | 7 | #include <memory> |
| 8 | #include "core/hle/kernel/physical_memory.h" | ||
| 8 | #include "core/hle/service/service.h" | 9 | #include "core/hle/service/service.h" |
| 9 | 10 | ||
| 10 | namespace Service { | 11 | namespace Service { |
| @@ -15,6 +16,8 @@ class FileSystemController; | |||
| 15 | 16 | ||
| 16 | namespace NS { | 17 | namespace NS { |
| 17 | 18 | ||
| 19 | void EncryptSharedFont(const std::vector<u8>& input, Kernel::PhysicalMemory& output); | ||
| 20 | |||
| 18 | class PL_U final : public ServiceFramework<PL_U> { | 21 | class PL_U final : public ServiceFramework<PL_U> { |
| 19 | public: | 22 | public: |
| 20 | PL_U(FileSystem::FileSystemController& fsc); | 23 | PL_U(FileSystem::FileSystemController& fsc); |