IpcHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using ChocolArm64.Memory;
  2. using Ryujinx.OsHle.Handles;
  3. using Ryujinx.OsHle.Objects;
  4. using Ryujinx.OsHle.Services;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. namespace Ryujinx.OsHle.Ipc
  9. {
  10. static class IpcHandler
  11. {
  12. private static Dictionary<(string, int), ServiceProcessRequest> ServiceCmds =
  13. new Dictionary<(string, int), ServiceProcessRequest>()
  14. {
  15. { ( "acc:u0", 3), Service.AccU0ListOpenUsers },
  16. { ( "acc:u0", 5), Service.AccU0GetProfile },
  17. { ( "acc:u0", 100), Service.AccU0InitializeApplicationInfo },
  18. { ( "acc:u0", 101), Service.AccU0GetBaasAccountManagerForApplication },
  19. { ( "apm", 0), Service.ApmOpenSession },
  20. { ( "apm:p", 0), Service.ApmOpenSession },
  21. { ( "appletOE", 0), Service.AppletOpenApplicationProxy },
  22. { ( "audout:u", 0), Service.AudOutListAudioOuts },
  23. { ( "audout:u", 1), Service.AudOutOpenAudioOut },
  24. { ( "audren:u", 0), Service.AudRenOpenAudioRenderer },
  25. { ( "audren:u", 1), Service.AudRenGetAudioRendererWorkBufferSize },
  26. { ( "friend:a", 0), Service.FriendCreateFriendService },
  27. { ( "fsp-srv", 1), Service.FspSrvInitialize },
  28. { ( "fsp-srv", 18), Service.FspSrvMountSdCard },
  29. { ( "fsp-srv", 51), Service.FspSrvMountSaveData },
  30. { ( "fsp-srv", 200), Service.FspSrvOpenDataStorageByCurrentProcess },
  31. { ( "fsp-srv", 203), Service.FspSrvOpenRomStorage },
  32. { ( "fsp-srv", 1005), Service.FspSrvGetGlobalAccessLogMode },
  33. { ( "hid", 0), Service.HidCreateAppletResource },
  34. { ( "hid", 11), Service.HidActivateTouchScreen },
  35. { ( "hid", 100), Service.HidSetSupportedNpadStyleSet },
  36. { ( "hid", 102), Service.HidSetSupportedNpadIdType },
  37. { ( "hid", 103), Service.HidActivateNpad },
  38. { ( "hid", 120), Service.HidSetNpadJoyHoldType },
  39. { ( "lm", 0), Service.LmInitialize },
  40. { ( "nvdrv", 0), Service.NvDrvOpen },
  41. { ( "nvdrv", 1), Service.NvDrvIoctl },
  42. { ( "nvdrv", 2), Service.NvDrvClose },
  43. { ( "nvdrv", 3), Service.NvDrvInitialize },
  44. { ( "nvdrv", 4), Service.NvDrvQueryEvent },
  45. { ( "nvdrv", 8), Service.NvDrvSetClientPid },
  46. { ( "nvdrv:a", 0), Service.NvDrvOpen },
  47. { ( "nvdrv:a", 1), Service.NvDrvIoctl },
  48. { ( "nvdrv:a", 2), Service.NvDrvClose },
  49. { ( "nvdrv:a", 3), Service.NvDrvInitialize },
  50. { ( "nvdrv:a", 4), Service.NvDrvQueryEvent },
  51. { ( "nvdrv:a", 8), Service.NvDrvSetClientPid },
  52. { ( "pctl:a", 0), Service.PctlCreateService },
  53. { ( "pl:u", 1), Service.PlGetLoadState },
  54. { ( "pl:u", 2), Service.PlGetFontSize },
  55. { ( "pl:u", 3), Service.PlGetSharedMemoryAddressOffset },
  56. { ( "pl:u", 4), Service.PlGetSharedMemoryNativeHandle },
  57. { ( "set", 1), Service.SetGetAvailableLanguageCodes },
  58. { ( "sm:", 0), Service.SmInitialize },
  59. { ( "sm:", 1), Service.SmGetService },
  60. { ( "time:u", 0), Service.TimeGetStandardUserSystemClock },
  61. { ( "time:u", 1), Service.TimeGetStandardNetworkSystemClock },
  62. { ( "time:u", 2), Service.TimeGetStandardSteadyClock },
  63. { ( "time:u", 3), Service.TimeGetTimeZoneService },
  64. { ( "time:u", 4), Service.TimeGetStandardLocalSystemClock },
  65. { ( "time:s", 0), Service.TimeGetStandardUserSystemClock },
  66. { ( "time:s", 1), Service.TimeGetStandardNetworkSystemClock },
  67. { ( "time:s", 2), Service.TimeGetStandardSteadyClock },
  68. { ( "time:s", 3), Service.TimeGetTimeZoneService },
  69. { ( "time:s", 4), Service.TimeGetStandardLocalSystemClock },
  70. { ( "vi:m", 2), Service.ViGetDisplayService },
  71. };
  72. private const long SfciMagic = 'S' << 0 | 'F' << 8 | 'C' << 16 | 'I' << 24;
  73. private const long SfcoMagic = 'S' << 0 | 'F' << 8 | 'C' << 16 | 'O' << 24;
  74. public static void IpcCall(
  75. Switch Ns,
  76. AMemory Memory,
  77. HSession Session,
  78. IpcMessage Request,
  79. long CmdPtr,
  80. int HndId)
  81. {
  82. IpcMessage Response = new IpcMessage(Request.IsDomain);
  83. using (MemoryStream Raw = new MemoryStream(Request.RawData))
  84. {
  85. BinaryReader ReqReader = new BinaryReader(Raw);
  86. if (Request.Type == IpcMessageType.Request)
  87. {
  88. string ServiceName = Session.ServiceName;
  89. ServiceProcessRequest ProcReq = null;
  90. bool IgnoreNullPR = false;
  91. string DbgServiceName = string.Empty;
  92. if (Session is HDomain Dom)
  93. {
  94. if (Request.DomCmd == IpcDomCmd.SendMsg)
  95. {
  96. long Magic = ReqReader.ReadInt64();
  97. int CmdId = (int)ReqReader.ReadInt64();
  98. object Obj = Dom.GetObject(Request.DomObjId);
  99. if (Obj is HDomain)
  100. {
  101. ServiceCmds.TryGetValue((ServiceName, CmdId), out ProcReq);
  102. DbgServiceName = $"{ServiceName} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  103. }
  104. else if (Obj != null)
  105. {
  106. ((IIpcInterface)Obj).Commands.TryGetValue(CmdId, out ProcReq);
  107. DbgServiceName = $"{ServiceName} {Obj.GetType().Name} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  108. }
  109. }
  110. else if (Request.DomCmd == IpcDomCmd.DeleteObj)
  111. {
  112. Dom.DeleteObject(Request.DomObjId);
  113. Response = FillResponse(Response, 0);
  114. IgnoreNullPR = true;
  115. }
  116. }
  117. else
  118. {
  119. long Magic = ReqReader.ReadInt64();
  120. int CmdId = (int)ReqReader.ReadInt64();
  121. if (Session is HSessionObj)
  122. {
  123. object Obj = ((HSessionObj)Session).Obj;
  124. ((IIpcInterface)Obj).Commands.TryGetValue(CmdId, out ProcReq);
  125. DbgServiceName = $"{ServiceName} {Obj.GetType().Name} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  126. }
  127. else
  128. {
  129. ServiceCmds.TryGetValue((ServiceName, CmdId), out ProcReq);
  130. DbgServiceName = $"{ServiceName} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  131. }
  132. }
  133. Logging.Debug($"IpcMessage: {DbgServiceName}");
  134. if (ProcReq != null)
  135. {
  136. using (MemoryStream ResMS = new MemoryStream())
  137. {
  138. BinaryWriter ResWriter = new BinaryWriter(ResMS);
  139. ServiceCtx Context = new ServiceCtx(
  140. Ns,
  141. Memory,
  142. Session,
  143. Request,
  144. Response,
  145. ReqReader,
  146. ResWriter);
  147. long Result = ProcReq(Context);
  148. Response = FillResponse(Response, Result, ResMS.ToArray());
  149. }
  150. }
  151. else if (!IgnoreNullPR)
  152. {
  153. throw new NotImplementedException(DbgServiceName);
  154. }
  155. }
  156. else if (Request.Type == IpcMessageType.Control)
  157. {
  158. long Magic = ReqReader.ReadInt64();
  159. long CmdId = ReqReader.ReadInt64();
  160. switch (CmdId)
  161. {
  162. case 0: Request = IpcConvertSessionToDomain(Ns, Session, Response, HndId); break;
  163. case 3: Request = IpcQueryBufferPointerSize(Response); break;
  164. case 4: Request = IpcDuplicateSessionEx(Ns, Session, Response, ReqReader); break;
  165. default: throw new NotImplementedException(CmdId.ToString());
  166. }
  167. }
  168. else if (Request.Type == IpcMessageType.Unknown2)
  169. {
  170. //TODO
  171. }
  172. else
  173. {
  174. throw new NotImplementedException(Request.Type.ToString());
  175. }
  176. AMemoryHelper.WriteBytes(Memory, CmdPtr, Response.GetBytes(CmdPtr));
  177. }
  178. }
  179. private static IpcMessage IpcConvertSessionToDomain(
  180. Switch Ns,
  181. HSession Session,
  182. IpcMessage Response,
  183. int HndId)
  184. {
  185. HDomain Dom = new HDomain(Session);
  186. Ns.Os.Handles.ReplaceData(HndId, Dom);
  187. return FillResponse(Response, 0, Dom.GenerateObjectId(Dom));
  188. }
  189. private static IpcMessage IpcDuplicateSessionEx(
  190. Switch Ns,
  191. HSession Session,
  192. IpcMessage Response,
  193. BinaryReader ReqReader)
  194. {
  195. int Unknown = ReqReader.ReadInt32();
  196. int Handle = Ns.Os.Handles.GenerateId(Session);
  197. Response.HandleDesc = IpcHandleDesc.MakeMove(Handle);
  198. return FillResponse(Response, 0);
  199. }
  200. private static IpcMessage IpcQueryBufferPointerSize(IpcMessage Response)
  201. {
  202. return FillResponse(Response, 0, 0x500);
  203. }
  204. private static IpcMessage FillResponse(IpcMessage Response, long Result, params int[] Values)
  205. {
  206. using (MemoryStream MS = new MemoryStream())
  207. {
  208. BinaryWriter Writer = new BinaryWriter(MS);
  209. foreach (int Value in Values)
  210. {
  211. Writer.Write(Value);
  212. }
  213. return FillResponse(Response, Result, MS.ToArray());
  214. }
  215. }
  216. private static IpcMessage FillResponse(IpcMessage Response, long Result, byte[] Data = null)
  217. {
  218. Response.Type = IpcMessageType.Response;
  219. using (MemoryStream MS = new MemoryStream())
  220. {
  221. BinaryWriter Writer = new BinaryWriter(MS);
  222. Writer.Write(SfcoMagic);
  223. Writer.Write(Result);
  224. if (Data != null)
  225. {
  226. Writer.Write(Data);
  227. }
  228. Response.RawData = MS.ToArray();
  229. }
  230. return Response;
  231. }
  232. }
  233. }