IpcHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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:s", 0), Service.TimeGetStandardUserSystemClock },
  65. { ( "time:s", 1), Service.TimeGetStandardNetworkSystemClock },
  66. { ( "time:s", 2), Service.TimeGetStandardSteadyClock },
  67. { ( "time:s", 3), Service.TimeGetTimeZoneService },
  68. { ( "vi:m", 2), Service.ViGetDisplayService },
  69. };
  70. private const long SfciMagic = 'S' << 0 | 'F' << 8 | 'C' << 16 | 'I' << 24;
  71. private const long SfcoMagic = 'S' << 0 | 'F' << 8 | 'C' << 16 | 'O' << 24;
  72. public static void IpcCall(
  73. Switch Ns,
  74. AMemory Memory,
  75. HSession Session,
  76. IpcMessage Request,
  77. long CmdPtr,
  78. int HndId)
  79. {
  80. IpcMessage Response = new IpcMessage(Request.IsDomain);
  81. using (MemoryStream Raw = new MemoryStream(Request.RawData))
  82. {
  83. BinaryReader ReqReader = new BinaryReader(Raw);
  84. if (Request.Type == IpcMessageType.Request)
  85. {
  86. string ServiceName = Session.ServiceName;
  87. ServiceProcessRequest ProcReq = null;
  88. bool IgnoreNullPR = false;
  89. string DbgServiceName = string.Empty;
  90. if (Session is HDomain Dom)
  91. {
  92. if (Request.DomCmd == IpcDomCmd.SendMsg)
  93. {
  94. long Magic = ReqReader.ReadInt64();
  95. int CmdId = (int)ReqReader.ReadInt64();
  96. object Obj = Dom.GetObject(Request.DomObjId);
  97. if (Obj is HDomain)
  98. {
  99. ServiceCmds.TryGetValue((ServiceName, CmdId), out ProcReq);
  100. DbgServiceName = $"{ServiceName} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  101. }
  102. else if (Obj != null)
  103. {
  104. ((IIpcInterface)Obj).Commands.TryGetValue(CmdId, out ProcReq);
  105. DbgServiceName = $"{ServiceName} {Obj.GetType().Name} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  106. }
  107. }
  108. else if (Request.DomCmd == IpcDomCmd.DeleteObj)
  109. {
  110. Dom.DeleteObject(Request.DomObjId);
  111. Response = FillResponse(Response, 0);
  112. IgnoreNullPR = true;
  113. }
  114. }
  115. else
  116. {
  117. long Magic = ReqReader.ReadInt64();
  118. int CmdId = (int)ReqReader.ReadInt64();
  119. if (Session is HSessionObj)
  120. {
  121. object Obj = ((HSessionObj)Session).Obj;
  122. ((IIpcInterface)Obj).Commands.TryGetValue(CmdId, out ProcReq);
  123. DbgServiceName = $"{ServiceName} {Obj.GetType().Name} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  124. }
  125. else
  126. {
  127. ServiceCmds.TryGetValue((ServiceName, CmdId), out ProcReq);
  128. DbgServiceName = $"{ServiceName} {ProcReq?.Method.Name ?? CmdId.ToString()}";
  129. }
  130. }
  131. Logging.Debug($"IpcMessage: {DbgServiceName}");
  132. if (ProcReq != null)
  133. {
  134. using (MemoryStream ResMS = new MemoryStream())
  135. {
  136. BinaryWriter ResWriter = new BinaryWriter(ResMS);
  137. ServiceCtx Context = new ServiceCtx(
  138. Ns,
  139. Memory,
  140. Session,
  141. Request,
  142. Response,
  143. ReqReader,
  144. ResWriter);
  145. long Result = ProcReq(Context);
  146. Response = FillResponse(Response, Result, ResMS.ToArray());
  147. }
  148. }
  149. else if (!IgnoreNullPR)
  150. {
  151. throw new NotImplementedException(DbgServiceName);
  152. }
  153. }
  154. else if (Request.Type == IpcMessageType.Control)
  155. {
  156. long Magic = ReqReader.ReadInt64();
  157. long CmdId = ReqReader.ReadInt64();
  158. switch (CmdId)
  159. {
  160. case 0: Request = IpcConvertSessionToDomain(Ns, Session, Response, HndId); break;
  161. case 3: Request = IpcQueryBufferPointerSize(Response); break;
  162. case 4: Request = IpcDuplicateSessionEx(Ns, Session, Response, ReqReader); break;
  163. default: throw new NotImplementedException(CmdId.ToString());
  164. }
  165. }
  166. else if (Request.Type == IpcMessageType.Unknown2)
  167. {
  168. //TODO
  169. }
  170. else
  171. {
  172. throw new NotImplementedException(Request.Type.ToString());
  173. }
  174. AMemoryHelper.WriteBytes(Memory, CmdPtr, Response.GetBytes(CmdPtr));
  175. }
  176. }
  177. private static IpcMessage IpcConvertSessionToDomain(
  178. Switch Ns,
  179. HSession Session,
  180. IpcMessage Response,
  181. int HndId)
  182. {
  183. HDomain Dom = new HDomain(Session);
  184. Ns.Os.Handles.ReplaceData(HndId, Dom);
  185. return FillResponse(Response, 0, Dom.GenertateObjectId(Dom));
  186. }
  187. private static IpcMessage IpcDuplicateSessionEx(
  188. Switch Ns,
  189. HSession Session,
  190. IpcMessage Response,
  191. BinaryReader ReqReader)
  192. {
  193. int Unknown = ReqReader.ReadInt32();
  194. int Handle = Ns.Os.Handles.GenerateId(Session);
  195. Response.HandleDesc = IpcHandleDesc.MakeMove(Handle);
  196. return FillResponse(Response, 0);
  197. }
  198. private static IpcMessage IpcQueryBufferPointerSize(IpcMessage Response)
  199. {
  200. return FillResponse(Response, 0, 0x500);
  201. }
  202. private static IpcMessage FillResponse(IpcMessage Response, long Result, params int[] Values)
  203. {
  204. using (MemoryStream MS = new MemoryStream())
  205. {
  206. BinaryWriter Writer = new BinaryWriter(MS);
  207. foreach (int Value in Values)
  208. {
  209. Writer.Write(Value);
  210. }
  211. return FillResponse(Response, Result, MS.ToArray());
  212. }
  213. }
  214. private static IpcMessage FillResponse(IpcMessage Response, long Result, byte[] Data = null)
  215. {
  216. Response.Type = IpcMessageType.Response;
  217. using (MemoryStream MS = new MemoryStream())
  218. {
  219. BinaryWriter Writer = new BinaryWriter(MS);
  220. Writer.Write(SfcoMagic);
  221. Writer.Write(Result);
  222. if (Data != null)
  223. {
  224. Writer.Write(Data);
  225. }
  226. Response.RawData = MS.ToArray();
  227. }
  228. return Response;
  229. }
  230. }
  231. }