apt_u.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/common.h"
  5. #include "common/file_util.h"
  6. #include "core/hle/hle.h"
  7. #include "core/hle/kernel/event.h"
  8. #include "core/hle/kernel/mutex.h"
  9. #include "core/hle/kernel/shared_memory.h"
  10. #include "apt_u.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // Namespace APT_U
  13. namespace APT_U {
  14. // Address used for shared font (as observed on HW)
  15. // TODO(bunnei): This is the hard-coded address where we currently dump the shared font from via
  16. // https://github.com/citra-emu/3dsutils. This is technically a hack, and will not work at any
  17. // address other than 0x18000000 due to internal pointers in the shared font dump that would need to
  18. // be relocated. This might be fixed by dumping the shared font @ address 0x00000000 and then
  19. // correctly mapping it in Citra, however we still do not understand how the mapping is determined.
  20. static const VAddr SHARED_FONT_VADDR = 0x18000000;
  21. // Handle to shared memory region designated to for shared system font
  22. static Handle shared_font_mem = 0;
  23. static Handle lock_handle = 0;
  24. static std::vector<u8> shared_font;
  25. /// Signals used by APT functions
  26. enum class SignalType : u32 {
  27. None = 0x0,
  28. AppJustStarted = 0x1,
  29. ReturningToApp = 0xB,
  30. ExitingApp = 0xC,
  31. };
  32. void Initialize(Service::Interface* self) {
  33. u32* cmd_buff = Kernel::GetCommandBuffer();
  34. cmd_buff[3] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "APT_U:Menu"); // APT menu event handle
  35. cmd_buff[4] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "APT_U:Pause"); // APT pause event handle
  36. Kernel::SetEventLocked(cmd_buff[3], true);
  37. Kernel::SetEventLocked(cmd_buff[4], false); // Fire start event
  38. _assert_msg_(KERNEL, (0 != lock_handle), "Cannot initialize without lock");
  39. Kernel::ReleaseMutex(lock_handle);
  40. cmd_buff[1] = 0; // No error
  41. LOG_DEBUG(Service_APT, "called");
  42. }
  43. void GetLockHandle(Service::Interface* self) {
  44. u32* cmd_buff = Kernel::GetCommandBuffer();
  45. u32 flags = cmd_buff[1]; // TODO(bunnei): Figure out the purpose of the flag field
  46. if (0 == lock_handle) {
  47. // TODO(bunnei): Verify if this is created here or at application boot?
  48. lock_handle = Kernel::CreateMutex(false, "APT_U:Lock");
  49. Kernel::ReleaseMutex(lock_handle);
  50. }
  51. cmd_buff[1] = 0; // No error
  52. // Not sure what these parameters are used for, but retail apps check that they are 0 after
  53. // GetLockHandle has been called.
  54. cmd_buff[2] = 0;
  55. cmd_buff[3] = 0;
  56. cmd_buff[4] = 0;
  57. cmd_buff[5] = lock_handle;
  58. LOG_TRACE(Service_APT, "called handle=0x%08X", cmd_buff[5]);
  59. }
  60. void Enable(Service::Interface* self) {
  61. u32* cmd_buff = Kernel::GetCommandBuffer();
  62. u32 unk = cmd_buff[1]; // TODO(bunnei): What is this field used for?
  63. cmd_buff[1] = 0; // No error
  64. LOG_WARNING(Service_APT, "(STUBBED) called unk=0x%08X", unk);
  65. }
  66. void InquireNotification(Service::Interface* self) {
  67. u32* cmd_buff = Kernel::GetCommandBuffer();
  68. u32 app_id = cmd_buff[2];
  69. cmd_buff[1] = 0; // No error
  70. cmd_buff[2] = static_cast<u32>(SignalType::None); // Signal type
  71. LOG_WARNING(Service_APT, "(STUBBED) called app_id=0x%08X", app_id);
  72. }
  73. /**
  74. * APT_U::ReceiveParameter service function. This returns the current parameter data from NS state,
  75. * from the source process which set the parameters. Once finished, NS will clear a flag in the NS
  76. * state so that this command will return an error if this command is used again if parameters were
  77. * not set again. This is called when the second Initialize event is triggered. It returns a signal
  78. * type indicating why it was triggered.
  79. * Inputs:
  80. * 1 : AppID
  81. * 2 : Parameter buffer size, max size is 0x1000
  82. * Outputs:
  83. * 1 : Result of function, 0 on success, otherwise error code
  84. * 2 : Unknown, for now assume AppID of the process which sent these parameters
  85. * 3 : Unknown, for now assume Signal type
  86. * 4 : Actual parameter buffer size, this is <= to the the input size
  87. * 5 : Value
  88. * 6 : Handle from the source process which set the parameters, likely used for shared memory
  89. * 7 : Size
  90. * 8 : Output parameter buffer ptr
  91. */
  92. void ReceiveParameter(Service::Interface* self) {
  93. u32* cmd_buff = Kernel::GetCommandBuffer();
  94. u32 app_id = cmd_buff[1];
  95. u32 buffer_size = cmd_buff[2];
  96. cmd_buff[1] = 0; // No error
  97. cmd_buff[2] = 0;
  98. cmd_buff[3] = static_cast<u32>(SignalType::AppJustStarted); // Signal type
  99. cmd_buff[4] = 0x10; // Parameter buffer size (16)
  100. cmd_buff[5] = 0;
  101. cmd_buff[6] = 0;
  102. cmd_buff[7] = 0;
  103. LOG_WARNING(Service_APT, "(STUBBED) called app_id=0x%08X, buffer_size=0x%08X", app_id, buffer_size);
  104. }
  105. /**
  106. * APT_U::GlanceParameter service function. This is exactly the same as APT_U::ReceiveParameter
  107. * (except for the word value prior to the output handle), except this will not clear the flag
  108. * (except when responseword[3]==8 || responseword[3]==9) in NS state.
  109. * Inputs:
  110. * 1 : AppID
  111. * 2 : Parameter buffer size, max size is 0x1000
  112. * Outputs:
  113. * 1 : Result of function, 0 on success, otherwise error code
  114. * 2 : Unknown, for now assume AppID of the process which sent these parameters
  115. * 3 : Unknown, for now assume Signal type
  116. * 4 : Actual parameter buffer size, this is <= to the the input size
  117. * 5 : Value
  118. * 6 : Handle from the source process which set the parameters, likely used for shared memory
  119. * 7 : Size
  120. * 8 : Output parameter buffer ptr
  121. */
  122. void GlanceParameter(Service::Interface* self) {
  123. u32* cmd_buff = Kernel::GetCommandBuffer();
  124. u32 app_id = cmd_buff[1];
  125. u32 buffer_size = cmd_buff[2];
  126. cmd_buff[1] = 0; // No error
  127. cmd_buff[2] = 0;
  128. cmd_buff[3] = static_cast<u32>(SignalType::AppJustStarted); // Signal type
  129. cmd_buff[4] = 0x10; // Parameter buffer size (16)
  130. cmd_buff[5] = 0;
  131. cmd_buff[6] = 0;
  132. cmd_buff[7] = 0;
  133. LOG_WARNING(Service_APT, "(STUBBED) called app_id=0x%08X, buffer_size=0x%08X", app_id, buffer_size);
  134. }
  135. /**
  136. * APT_U::AppletUtility service function
  137. * Inputs:
  138. * 1 : Unknown, but clearly used for something
  139. * 2 : Buffer 1 size (purpose is unknown)
  140. * 3 : Buffer 2 size (purpose is unknown)
  141. * 5 : Buffer 1 address (purpose is unknown)
  142. * 65 : Buffer 2 address (purpose is unknown)
  143. * Outputs:
  144. * 1 : Result of function, 0 on success, otherwise error code
  145. */
  146. void AppletUtility(Service::Interface* self) {
  147. u32* cmd_buff = Kernel::GetCommandBuffer();
  148. // These are from 3dbrew - I'm not really sure what they're used for.
  149. u32 unk = cmd_buff[1];
  150. u32 buffer1_size = cmd_buff[2];
  151. u32 buffer2_size = cmd_buff[3];
  152. u32 buffer1_addr = cmd_buff[5];
  153. u32 buffer2_addr = cmd_buff[65];
  154. cmd_buff[1] = 0; // No error
  155. LOG_WARNING(Service_APT, "(STUBBED) called unk=0x%08X, buffer1_size=0x%08x, buffer2_size=0x%08x, "
  156. "buffer1_addr=0x%08x, buffer2_addr=0x%08x", unk, buffer1_size, buffer2_size,
  157. buffer1_addr, buffer2_addr);
  158. }
  159. /**
  160. * APT_U::GetSharedFont service function
  161. * Outputs:
  162. * 1 : Result of function, 0 on success, otherwise error code
  163. * 2 : Virtual address of where shared font will be loaded in memory
  164. * 4 : Handle to shared font memory
  165. */
  166. void GetSharedFont(Service::Interface* self) {
  167. LOG_TRACE(Kernel_SVC, "called");
  168. u32* cmd_buff = Kernel::GetCommandBuffer();
  169. if (!shared_font.empty()) {
  170. // TODO(bunnei): This function shouldn't copy the shared font every time it's called.
  171. // Instead, it should probably map the shared font as RO memory. We don't currently have
  172. // an easy way to do this, but the copy should be sufficient for now.
  173. memcpy(Memory::GetPointer(SHARED_FONT_VADDR), shared_font.data(), shared_font.size());
  174. cmd_buff[0] = 0x00440082;
  175. cmd_buff[1] = 0; // No error
  176. cmd_buff[2] = SHARED_FONT_VADDR;
  177. cmd_buff[4] = shared_font_mem;
  178. } else {
  179. cmd_buff[1] = -1; // Generic error (not really possible to verify this on hardware)
  180. LOG_ERROR(Kernel_SVC, "called, but %s has not been loaded!", SHARED_FONT);
  181. }
  182. }
  183. const Interface::FunctionInfo FunctionTable[] = {
  184. {0x00010040, GetLockHandle, "GetLockHandle"},
  185. {0x00020080, Initialize, "Initialize"},
  186. {0x00030040, Enable, "Enable"},
  187. {0x00040040, nullptr, "Finalize"},
  188. {0x00050040, nullptr, "GetAppletManInfo"},
  189. {0x00060040, nullptr, "GetAppletInfo"},
  190. {0x00070000, nullptr, "GetLastSignaledAppletId"},
  191. {0x00080000, nullptr, "CountRegisteredApplet"},
  192. {0x00090040, nullptr, "IsRegistered"},
  193. {0x000A0040, nullptr, "GetAttribute"},
  194. {0x000B0040, InquireNotification, "InquireNotification"},
  195. {0x000C0104, nullptr, "SendParameter"},
  196. {0x000D0080, ReceiveParameter, "ReceiveParameter"},
  197. {0x000E0080, GlanceParameter, "GlanceParameter"},
  198. {0x000F0100, nullptr, "CancelParameter"},
  199. {0x001000C2, nullptr, "DebugFunc"},
  200. {0x001100C0, nullptr, "MapProgramIdForDebug"},
  201. {0x00120040, nullptr, "SetHomeMenuAppletIdForDebug"},
  202. {0x00130000, nullptr, "GetPreparationState"},
  203. {0x00140040, nullptr, "SetPreparationState"},
  204. {0x00150140, nullptr, "PrepareToStartApplication"},
  205. {0x00160040, nullptr, "PreloadLibraryApplet"},
  206. {0x00170040, nullptr, "FinishPreloadingLibraryApplet"},
  207. {0x00180040, nullptr, "PrepareToStartLibraryApplet"},
  208. {0x00190040, nullptr, "PrepareToStartSystemApplet"},
  209. {0x001A0000, nullptr, "PrepareToStartNewestHomeMenu"},
  210. {0x001B00C4, nullptr, "StartApplication"},
  211. {0x001C0000, nullptr, "WakeupApplication"},
  212. {0x001D0000, nullptr, "CancelApplication"},
  213. {0x001E0084, nullptr, "StartLibraryApplet"},
  214. {0x001F0084, nullptr, "StartSystemApplet"},
  215. {0x00200044, nullptr, "StartNewestHomeMenu"},
  216. {0x00210000, nullptr, "OrderToCloseApplication"},
  217. {0x00220040, nullptr, "PrepareToCloseApplication"},
  218. {0x00230040, nullptr, "PrepareToJumpToApplication"},
  219. {0x00240044, nullptr, "JumpToApplication"},
  220. {0x002500C0, nullptr, "PrepareToCloseLibraryApplet"},
  221. {0x00260000, nullptr, "PrepareToCloseSystemApplet"},
  222. {0x00270044, nullptr, "CloseApplication"},
  223. {0x00280044, nullptr, "CloseLibraryApplet"},
  224. {0x00290044, nullptr, "CloseSystemApplet"},
  225. {0x002A0000, nullptr, "OrderToCloseSystemApplet"},
  226. {0x002B0000, nullptr, "PrepareToJumpToHomeMenu"},
  227. {0x002C0044, nullptr, "JumpToHomeMenu"},
  228. {0x002D0000, nullptr, "PrepareToLeaveHomeMenu"},
  229. {0x002E0044, nullptr, "LeaveHomeMenu"},
  230. {0x002F0040, nullptr, "PrepareToLeaveResidentApplet"},
  231. {0x00300044, nullptr, "LeaveResidentApplet"},
  232. {0x00310100, nullptr, "PrepareToDoApplicationJump"},
  233. {0x00320084, nullptr, "DoApplicationJump"},
  234. {0x00330000, nullptr, "GetProgramIdOnApplicationJump"},
  235. {0x00340084, nullptr, "SendDeliverArg"},
  236. {0x00350080, nullptr, "ReceiveDeliverArg"},
  237. {0x00360040, nullptr, "LoadSysMenuArg"},
  238. {0x00370042, nullptr, "StoreSysMenuArg"},
  239. {0x00380040, nullptr, "PreloadResidentApplet"},
  240. {0x00390040, nullptr, "PrepareToStartResidentApplet"},
  241. {0x003A0044, nullptr, "StartResidentApplet"},
  242. {0x003B0040, nullptr, "CancelLibraryApplet"},
  243. {0x003C0042, nullptr, "SendDspSleep"},
  244. {0x003D0042, nullptr, "SendDspWakeUp"},
  245. {0x003E0080, nullptr, "ReplySleepQuery"},
  246. {0x003F0040, nullptr, "ReplySleepNotificationComplete"},
  247. {0x00400042, nullptr, "SendCaptureBufferInfo"},
  248. {0x00410040, nullptr, "ReceiveCaptureBufferInfo"},
  249. {0x00420080, nullptr, "SleepSystem"},
  250. {0x00430040, nullptr, "NotifyToWait"},
  251. {0x00440000, GetSharedFont, "GetSharedFont"},
  252. {0x00450040, nullptr, "GetWirelessRebootInfo"},
  253. {0x00460104, nullptr, "Wrap"},
  254. {0x00470104, nullptr, "Unwrap"},
  255. {0x00480100, nullptr, "GetProgramInfo"},
  256. {0x00490180, nullptr, "Reboot"},
  257. {0x004A0040, nullptr, "GetCaptureInfo"},
  258. {0x004B00C2, AppletUtility, "AppletUtility"},
  259. {0x004C0000, nullptr, "SetFatalErrDispMode"},
  260. {0x004D0080, nullptr, "GetAppletProgramInfo"},
  261. {0x004E0000, nullptr, "HardwareResetAsync"},
  262. {0x004F0080, nullptr, "SetApplicationCpuTimeLimit"},
  263. {0x00500040, nullptr, "GetApplicationCpuTimeLimit"},
  264. };
  265. ////////////////////////////////////////////////////////////////////////////////////////////////////
  266. // Interface class
  267. Interface::Interface() {
  268. // Load the shared system font (if available).
  269. // The expected format is a decrypted, uncompressed BCFNT file with the 0x80 byte header
  270. // generated by the APT:U service. The best way to get is by dumping it from RAM. We've provided
  271. // a homebrew app to do this: https://github.com/citra-emu/3dsutils. Put the resulting file
  272. // "shared_font.bin" in the Citra "sysdata" directory.
  273. shared_font.clear();
  274. std::string filepath = FileUtil::GetUserPath(D_SYSDATA_IDX) + SHARED_FONT;
  275. FileUtil::CreateFullPath(filepath); // Create path if not already created
  276. FileUtil::IOFile file(filepath, "rb");
  277. if (file.IsOpen()) {
  278. // Read shared font data
  279. shared_font.resize((size_t)file.GetSize());
  280. file.ReadBytes(shared_font.data(), (size_t)file.GetSize());
  281. // Create shared font memory object
  282. shared_font_mem = Kernel::CreateSharedMemory("APT_U:shared_font_mem");
  283. } else {
  284. LOG_WARNING(Service_APT, "Unable to load shared font: %s", filepath.c_str());
  285. shared_font_mem = 0;
  286. }
  287. lock_handle = 0;
  288. Register(FunctionTable, ARRAY_SIZE(FunctionTable));
  289. }
  290. Interface::~Interface() {
  291. }
  292. } // namespace