ISystemSettingsServer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using LibHac;
  2. using LibHac.Fs;
  3. using LibHac.FsSystem;
  4. using LibHac.FsSystem.NcaUtils;
  5. using Ryujinx.Common.Logging;
  6. using Ryujinx.HLE.FileSystem;
  7. using Ryujinx.HLE.HOS.SystemState;
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. namespace Ryujinx.HLE.HOS.Services.Settings
  12. {
  13. [Service("set:sys")]
  14. class ISystemSettingsServer : IpcService
  15. {
  16. public ISystemSettingsServer(ServiceCtx context) { }
  17. [Command(3)]
  18. // GetFirmwareVersion() -> buffer<nn::settings::system::FirmwareVersion, 0x1a, 0x100>
  19. public ResultCode GetFirmwareVersion(ServiceCtx context)
  20. {
  21. return GetFirmwareVersion2(context);
  22. }
  23. [Command(4)]
  24. // GetFirmwareVersion2() -> buffer<nn::settings::system::FirmwareVersion, 0x1a, 0x100>
  25. public ResultCode GetFirmwareVersion2(ServiceCtx context)
  26. {
  27. long replyPos = context.Request.RecvListBuff[0].Position;
  28. long replySize = context.Request.RecvListBuff[0].Size;
  29. byte[] firmwareData = GetFirmwareData(context.Device);
  30. if (firmwareData != null)
  31. {
  32. context.Memory.WriteBytes(replyPos, firmwareData);
  33. return ResultCode.Success;
  34. }
  35. const byte majorFwVersion = 0x03;
  36. const byte minorFwVersion = 0x00;
  37. const byte microFwVersion = 0x00;
  38. const byte unknown = 0x00; //Build?
  39. const int revisionNumber = 0x0A;
  40. const string platform = "NX";
  41. const string unknownHex = "7fbde2b0bba4d14107bf836e4643043d9f6c8e47";
  42. const string version = "3.0.0";
  43. const string build = "NintendoSDK Firmware for NX 3.0.0-10.0";
  44. // http://switchbrew.org/index.php?title=System_Version_Title
  45. using (MemoryStream ms = new MemoryStream(0x100))
  46. {
  47. BinaryWriter writer = new BinaryWriter(ms);
  48. writer.Write(majorFwVersion);
  49. writer.Write(minorFwVersion);
  50. writer.Write(microFwVersion);
  51. writer.Write(unknown);
  52. writer.Write(revisionNumber);
  53. writer.Write(Encoding.ASCII.GetBytes(platform));
  54. ms.Seek(0x28, SeekOrigin.Begin);
  55. writer.Write(Encoding.ASCII.GetBytes(unknownHex));
  56. ms.Seek(0x68, SeekOrigin.Begin);
  57. writer.Write(Encoding.ASCII.GetBytes(version));
  58. ms.Seek(0x80, SeekOrigin.Begin);
  59. writer.Write(Encoding.ASCII.GetBytes(build));
  60. context.Memory.WriteBytes(replyPos, ms.ToArray());
  61. }
  62. return ResultCode.Success;
  63. }
  64. [Command(23)]
  65. // GetColorSetId() -> i32
  66. public ResultCode GetColorSetId(ServiceCtx context)
  67. {
  68. context.ResponseData.Write((int)context.Device.System.State.ThemeColor);
  69. return ResultCode.Success;
  70. }
  71. [Command(24)]
  72. // GetColorSetId() -> i32
  73. public ResultCode SetColorSetId(ServiceCtx context)
  74. {
  75. int colorSetId = context.RequestData.ReadInt32();
  76. context.Device.System.State.ThemeColor = (ColorSet)colorSetId;
  77. return ResultCode.Success;
  78. }
  79. [Command(38)]
  80. // GetSettingsItemValue(buffer<nn::settings::SettingsName, 0x19, 0x48>, buffer<nn::settings::SettingsItemKey, 0x19, 0x48>) -> (u64, buffer<unknown, 6, 0>)
  81. public ResultCode GetSettingsItemValue(ServiceCtx context)
  82. {
  83. long classPos = context.Request.PtrBuff[0].Position;
  84. long classSize = context.Request.PtrBuff[0].Size;
  85. long namePos = context.Request.PtrBuff[1].Position;
  86. long nameSize = context.Request.PtrBuff[1].Size;
  87. long replyPos = context.Request.ReceiveBuff[0].Position;
  88. long replySize = context.Request.ReceiveBuff[0].Size;
  89. byte[] Class = context.Memory.ReadBytes(classPos, classSize);
  90. byte[] name = context.Memory.ReadBytes(namePos, nameSize);
  91. string askedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(name).Trim('\0');
  92. NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting);
  93. if (nxSetting != null)
  94. {
  95. byte[] settingBuffer = new byte[replySize];
  96. if (nxSetting is string stringValue)
  97. {
  98. if (stringValue.Length + 1 > replySize)
  99. {
  100. Logger.PrintError(LogClass.ServiceSet, $"{askedSetting} String value size is too big!");
  101. }
  102. else
  103. {
  104. settingBuffer = Encoding.ASCII.GetBytes(stringValue + "\0");
  105. }
  106. }
  107. if (nxSetting is int intValue)
  108. {
  109. settingBuffer = BitConverter.GetBytes(intValue);
  110. }
  111. else if (nxSetting is bool boolValue)
  112. {
  113. settingBuffer[0] = boolValue ? (byte)1 : (byte)0;
  114. }
  115. else
  116. {
  117. throw new NotImplementedException(nxSetting.GetType().Name);
  118. }
  119. context.Memory.WriteBytes(replyPos, settingBuffer);
  120. Logger.PrintDebug(LogClass.ServiceSet, $"{askedSetting} set value: {nxSetting} as {nxSetting.GetType()}");
  121. }
  122. else
  123. {
  124. Logger.PrintError(LogClass.ServiceSet, $"{askedSetting} not found!");
  125. }
  126. return ResultCode.Success;
  127. }
  128. public byte[] GetFirmwareData(Switch device)
  129. {
  130. long titleId = 0x0100000000000809;
  131. string contentPath = device.System.ContentManager.GetInstalledContentPath(titleId, StorageId.NandSystem, NcaContentType.Data);
  132. if (string.IsNullOrWhiteSpace(contentPath))
  133. {
  134. return null;
  135. }
  136. string firmwareTitlePath = device.FileSystem.SwitchPathToSystemPath(contentPath);
  137. using(IStorage firmwareStorage = new LocalStorage(firmwareTitlePath, FileAccess.Read))
  138. {
  139. Nca firmwareContent = new Nca(device.System.KeySet, firmwareStorage);
  140. if (!firmwareContent.CanOpenSection(NcaSectionType.Data))
  141. {
  142. return null;
  143. }
  144. IFileSystem firmwareRomFs = firmwareContent.OpenFileSystem(NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
  145. Result result = firmwareRomFs.OpenFile(out IFile firmwareFile, "/file", OpenMode.Read);
  146. if (result.IsFailure())
  147. {
  148. return null;
  149. }
  150. result = firmwareFile.GetSize(out long fileSize);
  151. if (result.IsFailure())
  152. {
  153. return null;
  154. }
  155. byte[] data = new byte[fileSize];
  156. result = firmwareFile.Read(out _, 0, data);
  157. if (result.IsFailure())
  158. {
  159. return null;
  160. }
  161. return data;
  162. }
  163. }
  164. }
  165. }