ISystemSettingsServer.cs 8.9 KB

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