ISystemSettingsServer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.SystemState;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. using LibHac;
  9. using Ryujinx.HLE.FileSystem;
  10. namespace Ryujinx.HLE.HOS.Services.Set
  11. {
  12. class ISystemSettingsServer : IpcService
  13. {
  14. private Dictionary<int, ServiceProcessRequest> m_Commands;
  15. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  16. public ISystemSettingsServer()
  17. {
  18. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  19. {
  20. { 3, GetFirmwareVersion },
  21. { 4, GetFirmwareVersion2 },
  22. { 23, GetColorSetId },
  23. { 24, SetColorSetId },
  24. { 38, GetSettingsItemValue }
  25. };
  26. }
  27. // GetFirmwareVersion() -> buffer<nn::settings::system::FirmwareVersion, 0x1a, 0x100>
  28. public static long GetFirmwareVersion(ServiceCtx Context)
  29. {
  30. return GetFirmwareVersion2(Context);
  31. }
  32. // GetFirmwareVersion2() -> buffer<nn::settings::system::FirmwareVersion, 0x1a, 0x100>
  33. public static long GetFirmwareVersion2(ServiceCtx Context)
  34. {
  35. long ReplyPos = Context.Request.RecvListBuff[0].Position;
  36. long ReplySize = Context.Request.RecvListBuff[0].Size;
  37. byte[] FirmwareData = GetFirmwareData(Context.Device);
  38. if (FirmwareData != null)
  39. {
  40. Context.Memory.WriteBytes(ReplyPos, FirmwareData);
  41. return 0;
  42. }
  43. const byte MajorFWVersion = 0x03;
  44. const byte MinorFWVersion = 0x00;
  45. const byte MicroFWVersion = 0x00;
  46. const byte Unknown = 0x00; //Build?
  47. const int RevisionNumber = 0x0A;
  48. const string Platform = "NX";
  49. const string UnknownHex = "7fbde2b0bba4d14107bf836e4643043d9f6c8e47";
  50. const string Version = "3.0.0";
  51. const string Build = "NintendoSDK Firmware for NX 3.0.0-10.0";
  52. //http://switchbrew.org/index.php?title=System_Version_Title
  53. using (MemoryStream MS = new MemoryStream(0x100))
  54. {
  55. BinaryWriter Writer = new BinaryWriter(MS);
  56. Writer.Write(MajorFWVersion);
  57. Writer.Write(MinorFWVersion);
  58. Writer.Write(MicroFWVersion);
  59. Writer.Write(Unknown);
  60. Writer.Write(RevisionNumber);
  61. Writer.Write(Encoding.ASCII.GetBytes(Platform));
  62. MS.Seek(0x28, SeekOrigin.Begin);
  63. Writer.Write(Encoding.ASCII.GetBytes(UnknownHex));
  64. MS.Seek(0x68, SeekOrigin.Begin);
  65. Writer.Write(Encoding.ASCII.GetBytes(Version));
  66. MS.Seek(0x80, SeekOrigin.Begin);
  67. Writer.Write(Encoding.ASCII.GetBytes(Build));
  68. Context.Memory.WriteBytes(ReplyPos, MS.ToArray());
  69. }
  70. return 0;
  71. }
  72. // GetColorSetId() -> i32
  73. public static long GetColorSetId(ServiceCtx Context)
  74. {
  75. Context.ResponseData.Write((int)Context.Device.System.State.ThemeColor);
  76. return 0;
  77. }
  78. // GetColorSetId() -> i32
  79. public static long SetColorSetId(ServiceCtx Context)
  80. {
  81. int ColorSetId = Context.RequestData.ReadInt32();
  82. Context.Device.System.State.ThemeColor = (ColorSet)ColorSetId;
  83. return 0;
  84. }
  85. // GetSettingsItemValue(buffer<nn::settings::SettingsName, 0x19, 0x48>, buffer<nn::settings::SettingsItemKey, 0x19, 0x48>) -> (u64, buffer<unknown, 6, 0>)
  86. public static long GetSettingsItemValue(ServiceCtx Context)
  87. {
  88. long ClassPos = Context.Request.PtrBuff[0].Position;
  89. long ClassSize = Context.Request.PtrBuff[0].Size;
  90. long NamePos = Context.Request.PtrBuff[1].Position;
  91. long NameSize = Context.Request.PtrBuff[1].Size;
  92. long ReplyPos = Context.Request.ReceiveBuff[0].Position;
  93. long ReplySize = Context.Request.ReceiveBuff[0].Size;
  94. byte[] Class = Context.Memory.ReadBytes(ClassPos, ClassSize);
  95. byte[] Name = Context.Memory.ReadBytes(NamePos, NameSize);
  96. string AskedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(Name).Trim('\0');
  97. NxSettings.Settings.TryGetValue(AskedSetting, out object NxSetting);
  98. if (NxSetting != null)
  99. {
  100. byte[] SettingBuffer = new byte[ReplySize];
  101. if (NxSetting is string StringValue)
  102. {
  103. if (StringValue.Length + 1 > ReplySize)
  104. {
  105. Logger.PrintError(LogClass.ServiceSet, $"{AskedSetting} String value size is too big!");
  106. }
  107. else
  108. {
  109. SettingBuffer = Encoding.ASCII.GetBytes(StringValue + "\0");
  110. }
  111. }
  112. if (NxSetting is int IntValue)
  113. {
  114. SettingBuffer = BitConverter.GetBytes(IntValue);
  115. }
  116. else if (NxSetting is bool BoolValue)
  117. {
  118. SettingBuffer[0] = BoolValue ? (byte)1 : (byte)0;
  119. }
  120. else
  121. {
  122. throw new NotImplementedException(NxSetting.GetType().Name);
  123. }
  124. Context.Memory.WriteBytes(ReplyPos, SettingBuffer);
  125. Logger.PrintDebug(LogClass.ServiceSet, $"{AskedSetting} set value: {NxSetting} as {NxSetting.GetType()}");
  126. }
  127. else
  128. {
  129. Logger.PrintError(LogClass.ServiceSet, $"{AskedSetting} not found!");
  130. }
  131. return 0;
  132. }
  133. public static byte[] GetFirmwareData(Switch Device)
  134. {
  135. byte[] Data = null;
  136. long TitleId = 0x0100000000000809;
  137. string ContentPath = Device.System.ContentManager.GetInstalledContentPath(TitleId, StorageId.NandSystem, ContentType.Data);
  138. if(string.IsNullOrWhiteSpace(ContentPath))
  139. {
  140. return null;
  141. }
  142. string FirmwareTitlePath = Device.FileSystem.SwitchPathToSystemPath(ContentPath);
  143. FileStream FirmwareStream = File.Open(FirmwareTitlePath, FileMode.Open, FileAccess.Read);
  144. Nca FirmwareContent = new Nca(Device.System.KeySet, FirmwareStream, false);
  145. Stream RomFsStream = FirmwareContent.OpenSection(0, false, Device.System.FsIntegrityCheckLevel);
  146. if(RomFsStream == null)
  147. {
  148. return null;
  149. }
  150. Romfs FirmwareRomFs = new Romfs(RomFsStream);
  151. using(MemoryStream MemoryStream = new MemoryStream())
  152. {
  153. using (Stream FirmwareFile = FirmwareRomFs.OpenFile("/file"))
  154. {
  155. FirmwareFile.CopyTo(MemoryStream);
  156. }
  157. Data = MemoryStream.ToArray();
  158. }
  159. FirmwareContent.Dispose();
  160. FirmwareStream.Dispose();
  161. return Data;
  162. }
  163. }
  164. }