ISystemSettingsServer.cs 7.1 KB

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