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