pl_u.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/common_paths.h"
  5. #include "common/file_util.h"
  6. #include "core/hle/ipc_helpers.h"
  7. #include "core/hle/service/ns/pl_u.h"
  8. namespace Service {
  9. namespace NS {
  10. struct FontRegion {
  11. u32 offset;
  12. u32 size;
  13. };
  14. // The below data is specific to shared font data dumped from Switch on f/w 2.2
  15. // Virtual address and offsets/sizes likely will vary by dump
  16. static constexpr VAddr SHARED_FONT_MEM_VADDR{0x00000009d3016000ULL};
  17. static constexpr u64 SHARED_FONT_MEM_SIZE{0x1100000};
  18. static constexpr std::array<FontRegion, 6> SHARED_FONT_REGIONS{
  19. FontRegion{0x00000008, 0x001fe764}, FontRegion{0x001fe774, 0x00773e58},
  20. FontRegion{0x009725d4, 0x0001aca8}, FontRegion{0x0098d284, 0x00369cec},
  21. FontRegion{0x00cf6f78, 0x0039b858}, FontRegion{0x010927d8, 0x00019e80},
  22. };
  23. enum class LoadState : u32 {
  24. Loading = 0,
  25. Done = 1,
  26. };
  27. PL_U::PL_U() : ServiceFramework("pl:u") {
  28. static const FunctionInfo functions[] = {
  29. {1, &PL_U::GetLoadState, "GetLoadState"},
  30. {2, &PL_U::GetSize, "GetSize"},
  31. {3, &PL_U::GetSharedMemoryAddressOffset, "GetSharedMemoryAddressOffset"},
  32. {4, &PL_U::GetSharedMemoryNativeHandle, "GetSharedMemoryNativeHandle"}};
  33. RegisterHandlers(functions);
  34. // Attempt to load shared font data from disk
  35. const std::string filepath{FileUtil::GetUserPath(D_SYSDATA_IDX) + SHARED_FONT};
  36. FileUtil::CreateFullPath(filepath); // Create path if not already created
  37. FileUtil::IOFile file(filepath, "rb");
  38. if (file.IsOpen()) {
  39. // Read shared font data
  40. ASSERT(file.GetSize() == SHARED_FONT_MEM_SIZE);
  41. shared_font = std::make_shared<std::vector<u8>>(static_cast<size_t>(file.GetSize()));
  42. file.ReadBytes(shared_font->data(), shared_font->size());
  43. } else {
  44. LOG_WARNING(Service_NS, "Unable to load shared font: %s", filepath.c_str());
  45. }
  46. }
  47. void PL_U::GetLoadState(Kernel::HLERequestContext& ctx) {
  48. IPC::RequestParser rp{ctx};
  49. const u32 font_id{rp.Pop<u32>()};
  50. LOG_DEBUG(Service_NS, "called, font_id=%d", font_id);
  51. IPC::ResponseBuilder rb{ctx, 3};
  52. rb.Push(RESULT_SUCCESS);
  53. rb.Push<u32>(static_cast<u32>(LoadState::Done));
  54. }
  55. void PL_U::GetSize(Kernel::HLERequestContext& ctx) {
  56. IPC::RequestParser rp{ctx};
  57. const u32 font_id{rp.Pop<u32>()};
  58. LOG_DEBUG(Service_NS, "called, font_id=%d", font_id);
  59. IPC::ResponseBuilder rb{ctx, 3};
  60. rb.Push(RESULT_SUCCESS);
  61. rb.Push<u32>(SHARED_FONT_REGIONS[font_id].size);
  62. }
  63. void PL_U::GetSharedMemoryAddressOffset(Kernel::HLERequestContext& ctx) {
  64. IPC::RequestParser rp{ctx};
  65. const u32 font_id{rp.Pop<u32>()};
  66. LOG_DEBUG(Service_NS, "called, font_id=%d", font_id);
  67. IPC::ResponseBuilder rb{ctx, 3};
  68. rb.Push(RESULT_SUCCESS);
  69. rb.Push<u32>(SHARED_FONT_REGIONS[font_id].offset);
  70. }
  71. void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) {
  72. if (shared_font != nullptr) {
  73. // TODO(bunnei): This is a less-than-ideal solution to load a RAM dump of the Switch shared
  74. // font data. This (likely) relies on exact address, size, and offsets from the original
  75. // dump. In the future, we need to replace this with a more robust solution.
  76. // Map backing memory for the font data
  77. Kernel::g_current_process->vm_manager.MapMemoryBlock(SHARED_FONT_MEM_VADDR, shared_font, 0,
  78. SHARED_FONT_MEM_SIZE,
  79. Kernel::MemoryState::Shared);
  80. // Create shared font memory object
  81. shared_font_mem = Kernel::SharedMemory::Create(
  82. Kernel::g_current_process, SHARED_FONT_MEM_SIZE, Kernel::MemoryPermission::ReadWrite,
  83. Kernel::MemoryPermission::Read, SHARED_FONT_MEM_VADDR, Kernel::MemoryRegion::BASE,
  84. "PL_U:shared_font_mem");
  85. }
  86. LOG_DEBUG(Service_NS, "called");
  87. IPC::ResponseBuilder rb{ctx, 2, 1};
  88. rb.Push(RESULT_SUCCESS);
  89. rb.PushCopyObjects(shared_font_mem);
  90. }
  91. } // namespace NS
  92. } // namespace Service