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> _commands;
  15. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  16. public ISystemSettingsServer()
  17. {
  18. _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. }