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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
#include <revolution/os.h>
#include <revolution/os/OSResetSW.h>
#include <revolution/vi.h>
#include <revolution/ipc.h>
#include <revolution/private/iosrestypes.h>
static u32 StmImInBuf[8] ATTRIBUTE_ALIGN(32);
static u32 StmImOutBuf[8] ATTRIBUTE_ALIGN(32);
static u32 StmVdInBuf[8] ATTRIBUTE_ALIGN(32);
static u32 StmVdOutBuf[8] ATTRIBUTE_ALIGN(32);
static u32 StmEhInBuf[8] ATTRIBUTE_ALIGN(32);
static u32 StmEhOutBuf[8] ATTRIBUTE_ALIGN(32);
static OSResetCallback ResetCallback;
static OSPowerCallback PowerCallback;
static BOOL ResetDown = 0;
static int StmReady = 0;
static int StmImDesc = 0;
static int StmEhDesc = 0;
static int StmEhRegistered = 0;
static int StmVdInUse = 0;
static BOOL __OSGetResetButtonStateRaw(void);
static s32 __OSStateEventHandler(s32, void *);
static s32 __OSVIDimReplyHandler(s32, void *);
static void __OSDefaultResetCallback(void);
static void __OSDefaultPowerCallback(void);
static void __OSRegisterStateEvent(void);
static int AccessVIDimRegs(void);
static void LockUp(void);
OSPowerCallback OSSetPowerCallback(OSPowerCallback callback) {
BOOL enabled;
OSPowerCallback prevCallback;
enabled = OSDisableInterrupts();
prevCallback = PowerCallback;
if (callback) {
PowerCallback = callback;
} else {
PowerCallback = __OSDefaultPowerCallback;
}
if (!StmEhRegistered) {
__OSRegisterStateEvent();
}
OSRestoreInterrupts(enabled);
if (prevCallback == __OSDefaultPowerCallback) {
return NULL;
} else {
return prevCallback;
}
}
// NONMATCHING - regswap
BOOL OSGetResetButtonState(void) {
BOOL enabled = OSDisableInterrupts();
int state = ResetDown;
ResetDown = FALSE;
OSRestoreInterrupts(enabled);
if (!StmEhRegistered) {
__OSRegisterStateEvent();
}
return state;
}
BOOL OSGetResetSwitchState(void) {
return OSGetResetButtonState();
}
int __OSInitSTM(void) {
PowerCallback = __OSDefaultPowerCallback;
ResetCallback = __OSDefaultResetCallback;
ResetDown = 0;
if (StmReady) {
return 1;
}
StmVdInUse = 0;
StmImDesc = IOS_Open("/dev/stm/immediate", 0);
if (StmImDesc < 0) {
OSReport("[OS] Note: STM is not included in the firmware.\n");
StmReady = 0;
return 0;
}
StmEhDesc = IOS_Open("/dev/stm/eventhook", 0);
if (StmEhDesc < 0) {
OSReport("[OS] Note: STM is not included in the firmware.\n");
StmReady = 0;
return 0;
}
__OSRegisterStateEvent();
StmReady = 1;
return 1;
}
// unknown function of origin
void dummyStrings() {
OSReport("Error: Failed to close STM immediate control driver. %d\n");
OSReport("Error: Failed to close STM event hook driver. %d\n");
}
void __OSShutdownToSBY(void) {
int res;
__VIRegs[1] = 0;
if (!StmReady) {
OSPanic(__FILE__, 348, "Error: The firmware doesn\'t support shutdown feature.\n");
}
StmImInBuf[0] = 0;
res = IOS_Ioctl(StmImDesc, 0x2003, StmImInBuf, 0x20, StmImOutBuf, 0x20);
LockUp();
}
void __OSHotReset(void) {
int result;
__VIRegs[1] = 0;
if (!StmReady) {
OSPanic(__FILE__, 412, "Error: The firmware doesn't support reboot feature.\n");
}
result = IOS_Ioctl(StmImDesc, 0x2001, StmImInBuf, sizeof(StmImInBuf), StmImOutBuf, sizeof(StmImOutBuf));
LockUp();
}
BOOL __OSGetResetButtonStateRaw(void) {
u32 reg = __PIRegs[0];
if (!(reg & 0x10000)) {
return TRUE;
} else {
return FALSE;
}
}
int __OSSetVIForceDimming(BOOL isEnabled, u32 yShift, u32 xShift) {
BOOL en;
int var_r31;
if (!StmReady) {
return -10;
}
en = OSDisableInterrupts();
if (StmVdInUse) {
OSRestoreInterrupts(en);
return 0;
}
StmVdInUse = 1;
OSRestoreInterrupts(en);
StmVdInBuf[0] = (xShift | yShift << 3) | isEnabled << 7;
StmVdInBuf[1] = 0;
StmVdInBuf[2] = 0;
StmVdInBuf[3] = 0;
StmVdInBuf[4] = 0;
StmVdInBuf[5] = 0xFFFFFFFF;
StmVdInBuf[6] = 0xFFFF0000;
StmVdInBuf[7] = 0;
var_r31 = AccessVIDimRegs();
return var_r31;
}
s32 __OSSetIdleLEDMode(u32 led_mode) {
s32 ret;
if (StmReady == 0) {
return -6;
}
StmImInBuf[0] = led_mode;
ret = IOS_Ioctl(StmImDesc, 0x6002, StmImInBuf, 0x20, StmImOutBuf, 0x20);
return ret;
}
s32 __OSUnRegisterStateEvent(void) {
s32 ret;
if (StmEhRegistered == 0) {
return 0;
}
if (StmReady == 0) {
return -6;
}
ret = IOS_Ioctl(StmImDesc, 0x3002, StmImInBuf, 0x20, StmImOutBuf, 0x20);
if (ret == 0) {
StmEhRegistered = 0;
}
return ret;
}
// NONMATCHING - extra branch
static int AccessVIDimRegs(void) {
int res;
res = IOS_IoctlAsync(StmImDesc, 0x5001, StmVdInBuf, 0x20, StmVdOutBuf, 0x20, __OSVIDimReplyHandler, 0);
switch(res) {
default:
return res;
case 0:
return 1;
}
}
s32 __OSVIDimReplyHandler(s32 ret, void *pUnused) {
StmVdInUse = 0;
return 0;
}
static void __OSRegisterStateEvent(void) {
int err, enabled;
enabled = OSDisableInterrupts();
err = IOS_IoctlAsync(StmEhDesc, 0x1000, StmEhInBuf, 0x20, StmEhOutBuf, 0x20, __OSStateEventHandler, (void*)0);
if (err == IOS_ERROR_OK) {
StmEhRegistered = 1;
}
else {
StmEhRegistered = 0;
}
OSRestoreInterrupts(enabled);
}
void __OSDefaultResetCallback(void) {
}
void __OSDefaultPowerCallback(void) {
}
// unknown origin function
void dummyStrings2() {
OSReport("Error: The firmware doesn't support hot reset feature.\n");
}
// NONMATCHING
static s32 __OSStateEventHandler(s32 ret, void* pUnused) {
int en;
OSResetCallback cb;
if (ret != 0) {
OSPanic(__FILE__, 820, "Error on STM state event handler\n");
}
StmEhRegistered = 0;
if (StmEhOutBuf[0] == 0x20000) {
if (__OSGetResetButtonStateRaw()) {
en = OSDisableInterrupts();
ResetDown = TRUE;
cb = ResetCallback;
ResetCallback = __OSDefaultResetCallback;
cb();
OSRestoreInterrupts(en);
VIResetDimmingCount();
}
__OSRegisterStateEvent();
}
if (StmEhOutBuf[0] == 0x800) {
en = OSDisableInterrupts();
cb = PowerCallback;
PowerCallback = __OSDefaultPowerCallback;
cb();
OSRestoreInterrupts(en);
}
if (StmEhOutBuf[0] == 0) {}
return 0;
}
static void LockUp(void) {
BOOL en = OSDisableInterrupts();
ICFlashInvalidate();
while (1) {
}
}
|