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