ISystemSettingsServer.cs 9.0 KB

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