diff options
Diffstat (limited to '')
| -rw-r--r-- | src/core/file_sys/system_archive/shared_font.cpp | 3 | ||||
| -rw-r--r-- | src/core/hle/service/ns/pl_u.cpp | 157 |
2 files changed, 48 insertions, 112 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 f64535237..bafba3d2f 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp | |||
| @@ -24,6 +24,7 @@ | |||
| 24 | #include "core/file_sys/nca_metadata.h" | 24 | #include "core/file_sys/nca_metadata.h" |
| 25 | #include "core/file_sys/registered_cache.h" | 25 | #include "core/file_sys/registered_cache.h" |
| 26 | #include "core/file_sys/romfs.h" | 26 | #include "core/file_sys/romfs.h" |
| 27 | #include "core/file_sys/system_archive/system_archive.h" | ||
| 27 | #include "core/hle/ipc_helpers.h" | 28 | #include "core/hle/ipc_helpers.h" |
| 28 | #include "core/hle/kernel/shared_memory.h" | 29 | #include "core/hle/kernel/shared_memory.h" |
| 29 | #include "core/hle/service/filesystem/filesystem.h" | 30 | #include "core/hle/service/filesystem/filesystem.h" |
| @@ -94,15 +95,16 @@ static void DecryptSharedFont(const std::vector<u32>& input, Kernel::PhysicalMem | |||
| 94 | offset += transformed_font.size() * sizeof(u32); | 95 | offset += transformed_font.size() * sizeof(u32); |
| 95 | } | 96 | } |
| 96 | 97 | ||
| 97 | static void EncryptSharedFont(const std::vector<u8>& input, Kernel::PhysicalMemory& output, | 98 | static void EncryptSharedFont(const std::vector<u8>& input, Kernel::PhysicalMemory& output) { |
| 98 | std::size_t& offset) { | 99 | 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!"); | 100 | |
| 100 | const u32 KEY = EXPECTED_MAGIC ^ EXPECTED_RESULT; | 101 | const auto key = Common::swap32(EXPECTED_RESULT ^ EXPECTED_MAGIC); |
| 101 | std::memcpy(output.data() + offset, &EXPECTED_RESULT, sizeof(u32)); // Magic header | 102 | std::vector<u32> transformed_font(input.size() + 2); |
| 102 | const u32 ENC_SIZE = static_cast<u32>(input.size()) ^ KEY; | 103 | transformed_font[0] = Common::swap32(EXPECTED_MAGIC); |
| 103 | std::memcpy(output.data() + offset + sizeof(u32), &ENC_SIZE, sizeof(u32)); | 104 | transformed_font[1] = Common::swap32(input.size() * sizeof(u32)) ^ key; |
| 104 | std::memcpy(output.data() + offset + (sizeof(u32) * 2), input.data(), input.size()); | 105 | std::transform(input.begin(), input.end(), transformed_font.begin() + 2, |
| 105 | offset += input.size() + (sizeof(u32) * 2); | 106 | [key](u32 in) { return in ^ key; }); |
| 107 | std::memcpy(output.data(), transformed_font.data(), transformed_font.size() * sizeof(u32)); | ||
| 106 | } | 108 | } |
| 107 | 109 | ||
| 108 | // Helper function to make BuildSharedFontsRawRegions a bit nicer | 110 | // Helper function to make BuildSharedFontsRawRegions a bit nicer |
| @@ -168,114 +170,49 @@ PL_U::PL_U(Core::System& system) | |||
| 168 | // Attempt to load shared font data from disk | 170 | // Attempt to load shared font data from disk |
| 169 | const auto* nand = fsc.GetSystemNANDContents(); | 171 | const auto* nand = fsc.GetSystemNANDContents(); |
| 170 | std::size_t offset = 0; | 172 | std::size_t offset = 0; |
| 171 | // Rebuild shared fonts from data ncas | 173 | // Rebuild shared fonts from data ncas or synthesize |
| 172 | if (nand->HasEntry(static_cast<u64>(FontArchives::Standard), | 174 | |
| 173 | FileSys::ContentRecordType::Data)) { | 175 | impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(SHARED_FONT_MEM_SIZE); |
| 174 | impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(SHARED_FONT_MEM_SIZE); | 176 | for (auto font : SHARED_FONTS) { |
| 175 | for (auto font : SHARED_FONTS) { | 177 | FileSys::VirtualFile romfs; |
| 176 | const auto nca = | 178 | const auto nca = |
| 177 | nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data); | 179 | nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data); |
| 178 | if (!nca) { | 180 | if (nca) { |
| 179 | LOG_ERROR(Service_NS, "Failed to find {:016X}! Skipping", | 181 | romfs = nca->GetRomFS(); |
| 180 | static_cast<u64>(font.first)); | ||
| 181 | continue; | ||
| 182 | } | ||
| 183 | const auto romfs = nca->GetRomFS(); | ||
| 184 | if (!romfs) { | ||
| 185 | LOG_ERROR(Service_NS, "{:016X} has no RomFS! Skipping", | ||
| 186 | static_cast<u64>(font.first)); | ||
| 187 | continue; | ||
| 188 | } | ||
| 189 | const auto extracted_romfs = FileSys::ExtractRomFS(romfs); | ||
| 190 | if (!extracted_romfs) { | ||
| 191 | LOG_ERROR(Service_NS, "Failed to extract RomFS for {:016X}! Skipping", | ||
| 192 | static_cast<u64>(font.first)); | ||
| 193 | continue; | ||
| 194 | } | ||
| 195 | const auto font_fp = extracted_romfs->GetFile(font.second); | ||
| 196 | if (!font_fp) { | ||
| 197 | LOG_ERROR(Service_NS, "{:016X} has no file \"{}\"! Skipping", | ||
| 198 | static_cast<u64>(font.first), font.second); | ||
| 199 | continue; | ||
| 200 | } | ||
| 201 | std::vector<u32> font_data_u32(font_fp->GetSize() / sizeof(u32)); | ||
| 202 | font_fp->ReadBytes<u32>(font_data_u32.data(), font_fp->GetSize()); | ||
| 203 | // We need to be BigEndian as u32s for the xor encryption | ||
| 204 | std::transform(font_data_u32.begin(), font_data_u32.end(), font_data_u32.begin(), | ||
| 205 | Common::swap32); | ||
| 206 | FontRegion region{ | ||
| 207 | static_cast<u32>(offset + 8), | ||
| 208 | static_cast<u32>((font_data_u32.size() * sizeof(u32)) - | ||
| 209 | 8)}; // Font offset and size do not account for the header | ||
| 210 | DecryptSharedFont(font_data_u32, *impl->shared_font, offset); | ||
| 211 | impl->shared_font_regions.push_back(region); | ||
| 212 | } | 182 | } |
| 213 | 183 | ||
| 214 | } else { | 184 | if (!romfs) { |
| 215 | impl->shared_font = std::make_shared<Kernel::PhysicalMemory>( | 185 | romfs = FileSys::SystemArchive::SynthesizeSystemArchive(static_cast<u64>(font.first)); |
| 216 | SHARED_FONT_MEM_SIZE); // Shared memory needs to always be allocated and a fixed size | 186 | } |
| 217 | |||
| 218 | const std::string user_path = FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir); | ||
| 219 | const std::string filepath{user_path + SHARED_FONT}; | ||
| 220 | 187 | ||
| 221 | // Create path if not already created | 188 | if (!romfs) { |
| 222 | if (!FileUtil::CreateFullPath(filepath)) { | 189 | LOG_ERROR(Service_NS, "Failed to find or synthesize {:016X}! Skipping", |
| 223 | LOG_ERROR(Service_NS, "Failed to create sharedfonts path \"{}\"!", filepath); | 190 | static_cast<u64>(font.first)); |
| 224 | return; | 191 | continue; |
| 225 | } | 192 | } |
| 226 | 193 | ||
| 227 | bool using_ttf = false; | 194 | const auto extracted_romfs = FileSys::ExtractRomFS(romfs); |
| 228 | for (const char* font_ttf : SHARED_FONTS_TTF) { | 195 | if (!extracted_romfs) { |
| 229 | if (FileUtil::Exists(user_path + font_ttf)) { | 196 | LOG_ERROR(Service_NS, "Failed to extract RomFS for {:016X}! Skipping", |
| 230 | using_ttf = true; | 197 | static_cast<u64>(font.first)); |
| 231 | FileUtil::IOFile file(user_path + font_ttf, "rb"); | 198 | continue; |
| 232 | if (file.IsOpen()) { | ||
| 233 | std::vector<u8> ttf_bytes(file.GetSize()); | ||
| 234 | file.ReadBytes<u8>(ttf_bytes.data(), ttf_bytes.size()); | ||
| 235 | FontRegion region{ | ||
| 236 | static_cast<u32>(offset + 8), | ||
| 237 | static_cast<u32>(ttf_bytes.size())}; // Font offset and size do not account | ||
| 238 | // for the header | ||
| 239 | EncryptSharedFont(ttf_bytes, *impl->shared_font, offset); | ||
| 240 | impl->shared_font_regions.push_back(region); | ||
| 241 | } else { | ||
| 242 | LOG_WARNING(Service_NS, "Unable to load font: {}", font_ttf); | ||
| 243 | } | ||
| 244 | } else if (using_ttf) { | ||
| 245 | LOG_WARNING(Service_NS, "Unable to find font: {}", font_ttf); | ||
| 246 | } | ||
| 247 | } | 199 | } |
| 248 | if (using_ttf) | 200 | const auto font_fp = extracted_romfs->GetFile(font.second); |
| 249 | return; | 201 | if (!font_fp) { |
| 250 | FileUtil::IOFile file(filepath, "rb"); | 202 | LOG_ERROR(Service_NS, "{:016X} has no file \"{}\"! Skipping", |
| 251 | 203 | static_cast<u64>(font.first), font.second); | |
| 252 | if (file.IsOpen()) { | 204 | continue; |
| 253 | // Read shared font data | ||
| 254 | ASSERT(file.GetSize() == SHARED_FONT_MEM_SIZE); | ||
| 255 | file.ReadBytes(impl->shared_font->data(), impl->shared_font->size()); | ||
| 256 | impl->BuildSharedFontsRawRegions(*impl->shared_font); | ||
| 257 | } else { | ||
| 258 | LOG_WARNING(Service_NS, | ||
| 259 | "Shared Font file missing. Loading open source replacement from memory"); | ||
| 260 | |||
| 261 | // clang-format off | ||
| 262 | const std::vector<std::vector<u8>> open_source_shared_fonts_ttf = { | ||
| 263 | {std::begin(FontChineseSimplified), std::end(FontChineseSimplified)}, | ||
| 264 | {std::begin(FontChineseTraditional), std::end(FontChineseTraditional)}, | ||
| 265 | {std::begin(FontExtendedChineseSimplified), std::end(FontExtendedChineseSimplified)}, | ||
| 266 | {std::begin(FontKorean), std::end(FontKorean)}, | ||
| 267 | {std::begin(FontNintendoExtended), std::end(FontNintendoExtended)}, | ||
| 268 | {std::begin(FontStandard), std::end(FontStandard)}, | ||
| 269 | }; | ||
| 270 | // clang-format on | ||
| 271 | |||
| 272 | for (const std::vector<u8>& font_ttf : open_source_shared_fonts_ttf) { | ||
| 273 | const FontRegion region{static_cast<u32>(offset + 8), | ||
| 274 | static_cast<u32>(font_ttf.size())}; | ||
| 275 | EncryptSharedFont(font_ttf, *impl->shared_font, offset); | ||
| 276 | impl->shared_font_regions.push_back(region); | ||
| 277 | } | ||
| 278 | } | 205 | } |
| 206 | std::vector<u32> font_data_u32(font_fp->GetSize() / sizeof(u32)); | ||
| 207 | font_fp->ReadBytes<u32>(font_data_u32.data(), font_fp->GetSize()); | ||
| 208 | // We need to be BigEndian as u32s for the xor encryption | ||
| 209 | std::transform(font_data_u32.begin(), font_data_u32.end(), font_data_u32.begin(), | ||
| 210 | Common::swap32); | ||
| 211 | // Font offset and size do not account for the header | ||
| 212 | const FontRegion region{static_cast<u32>(offset + 8), | ||
| 213 | static_cast<u32>((font_data_u32.size() * sizeof(u32)) - 8)}; | ||
| 214 | DecryptSharedFont(font_data_u32, *impl->shared_font, offset); | ||
| 215 | impl->shared_font_regions.push_back(region); | ||
| 279 | } | 216 | } |
| 280 | } | 217 | } |
| 281 | 218 | ||