/
coreinit_Misc.cpp
590 lines (544 loc) · 17.5 KB
/
coreinit_Misc.cpp
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include "Cafe/OS/common/OSCommon.h"
#include "Cafe/OS/libs/coreinit/coreinit_Misc.h"
#include "Cafe/OS/libs/coreinit/coreinit_MessageQueue.h"
#include "Cafe/CafeSystem.h"
#include "Cafe/Filesystem/fsc.h"
#include <pugixml.hpp>
namespace coreinit
{
/* coreinit logging and string format */
sint32 ppcSprintf(const char* formatStr, char* strOut, sint32 maxLength, PPCInterpreter_t* hCPU, sint32 initialParamIndex)
{
char tempStr[4096];
sint32 integerParamIndex = initialParamIndex;
sint32 floatParamIndex = 0;
sint32 writeIndex = 0;
while (*formatStr)
{
char c = *formatStr;
if (c == '%')
{
const char* formatStart = formatStr;
formatStr++;
if (*formatStr == '%')
{
// percent sign
if (writeIndex >= maxLength)
break;
strOut[writeIndex] = '%';
writeIndex++;
formatStr++;
continue;
}
// flags
bool flag_leftJustify = false;
bool flag_zeroPadding = false;
if (*formatStr == '-')
{
flag_leftJustify = true;
formatStr++;
}
if (*formatStr == '+')
{
// todo
formatStr++;
}
if (*formatStr == ' ')
{
// todo
formatStr++;
}
if (*formatStr == '#')
{
// todo
formatStr++;
}
if (*formatStr == '0')
{
flag_zeroPadding = true;
formatStr++;
}
// width
if (*formatStr == '*')
{
cemu_assert_debug(false);
formatStr++;
}
bool widthIsSpecified = false;
sint32 width = 0;
while (*formatStr >= '0' && *formatStr <= '9')
{
width *= 10;
width += (*formatStr - '0');
formatStr++;
widthIsSpecified = true;
}
// precision
if (*formatStr == '.')
{
formatStr++;
if (*formatStr == '*')
{
cemu_assert_debug(false);
}
while (*formatStr >= '0' && *formatStr <= '9')
{
formatStr++;
}
}
// length + specifier
char tempFormat[64];
if (*formatStr == 'X' || *formatStr == 'x' || *formatStr == 'u' || *formatStr == 'd' || *formatStr == 'p' || *formatStr == 'i' ||
(formatStr[0] == 'l' && formatStr[1] == 'd'))
{
// number
formatStr++;
strncpy(tempFormat, formatStart, std::min((std::ptrdiff_t)sizeof(tempFormat) - 1, formatStr - formatStart));
if ((formatStr - formatStart) < sizeof(tempFormat))
tempFormat[(formatStr - formatStart)] = '\0';
else
tempFormat[sizeof(tempFormat) - 1] = '\0';
sint32 tempLen = sprintf(tempStr, tempFormat, PPCInterpreter_getCallParamU32(hCPU, integerParamIndex));
integerParamIndex++;
for (sint32 i = 0; i < tempLen; i++)
{
if (writeIndex >= maxLength)
break;
strOut[writeIndex] = tempStr[i];
writeIndex++;
}
}
else if (*formatStr == 's')
{
// string
formatStr++;
strncpy(tempFormat, formatStart, std::min((std::ptrdiff_t)sizeof(tempFormat) - 1, formatStr - formatStart));
if ((formatStr - formatStart) < sizeof(tempFormat))
tempFormat[(formatStr - formatStart)] = '\0';
else
tempFormat[sizeof(tempFormat) - 1] = '\0';
MPTR strOffset = PPCInterpreter_getCallParamU32(hCPU, integerParamIndex);
sint32 tempLen = 0;
if (strOffset == MPTR_NULL)
tempLen = sprintf(tempStr, "NULL");
else
tempLen = sprintf(tempStr, tempFormat, memory_getPointerFromVirtualOffset(strOffset));
integerParamIndex++;
for (sint32 i = 0; i < tempLen; i++)
{
if (writeIndex >= maxLength)
break;
strOut[writeIndex] = tempStr[i];
writeIndex++;
}
strOut[std::min(maxLength - 1, writeIndex)] = '\0';
}
else if (*formatStr == 'f')
{
// float
formatStr++;
strncpy(tempFormat, formatStart, std::min((std::ptrdiff_t)sizeof(tempFormat) - 1, formatStr - formatStart));
if ((formatStr - formatStart) < sizeof(tempFormat))
tempFormat[(formatStr - formatStart)] = '\0';
else
tempFormat[sizeof(tempFormat) - 1] = '\0';
sint32 tempLen = sprintf(tempStr, tempFormat, (float)hCPU->fpr[1 + floatParamIndex].fp0);
floatParamIndex++;
for (sint32 i = 0; i < tempLen; i++)
{
if (writeIndex >= maxLength)
break;
strOut[writeIndex] = tempStr[i];
writeIndex++;
}
}
else if (*formatStr == 'c')
{
// character
formatStr++;
strncpy(tempFormat, formatStart, std::min((std::ptrdiff_t)sizeof(tempFormat) - 1, formatStr - formatStart));
if ((formatStr - formatStart) < sizeof(tempFormat))
tempFormat[(formatStr - formatStart)] = '\0';
else
tempFormat[sizeof(tempFormat) - 1] = '\0';
sint32 tempLen = sprintf(tempStr, tempFormat, PPCInterpreter_getCallParamU32(hCPU, integerParamIndex));
integerParamIndex++;
for (sint32 i = 0; i < tempLen; i++)
{
if (writeIndex >= maxLength)
break;
strOut[writeIndex] = tempStr[i];
writeIndex++;
}
}
else if (formatStr[0] == 'l' && formatStr[1] == 'f')
{
// double
formatStr += 2;
strncpy(tempFormat, formatStart, std::min((std::ptrdiff_t)sizeof(tempFormat) - 1, formatStr - formatStart));
if ((formatStr - formatStart) < sizeof(tempFormat))
tempFormat[(formatStr - formatStart)] = '\0';
else
tempFormat[sizeof(tempFormat) - 1] = '\0';
sint32 tempLen = sprintf(tempStr, tempFormat, (double)hCPU->fpr[1 + floatParamIndex].fp0);
floatParamIndex++;
for (sint32 i = 0; i < tempLen; i++)
{
if (writeIndex >= maxLength)
break;
strOut[writeIndex] = tempStr[i];
writeIndex++;
}
}
else if ((formatStr[0] == 'l' && formatStr[1] == 'l' && (formatStr[2] == 'x' || formatStr[2] == 'X')))
{
formatStr += 3;
// double (64bit)
strncpy(tempFormat, formatStart, std::min((std::ptrdiff_t)sizeof(tempFormat) - 1, formatStr - formatStart));
if ((formatStr - formatStart) < sizeof(tempFormat))
tempFormat[(formatStr - formatStart)] = '\0';
else
tempFormat[sizeof(tempFormat) - 1] = '\0';
if (integerParamIndex & 1)
integerParamIndex++;
sint32 tempLen = sprintf(tempStr, tempFormat, PPCInterpreter_getCallParamU64(hCPU, integerParamIndex));
integerParamIndex += 2;
for (sint32 i = 0; i < tempLen; i++)
{
if (writeIndex >= maxLength)
break;
strOut[writeIndex] = tempStr[i];
writeIndex++;
}
}
else if ((formatStr[0] == 'l' && formatStr[1] == 'l' && formatStr[2] == 'd'))
{
formatStr += 3;
// signed integer (64bit)
strncpy(tempFormat, formatStart, std::min((std::ptrdiff_t)sizeof(tempFormat) - 1, formatStr - formatStart));
if ((formatStr - formatStart) < sizeof(tempFormat))
tempFormat[(formatStr - formatStart)] = '\0';
else
tempFormat[sizeof(tempFormat) - 1] = '\0';
if (integerParamIndex & 1)
integerParamIndex++;
sint32 tempLen = sprintf(tempStr, tempFormat, PPCInterpreter_getCallParamU64(hCPU, integerParamIndex));
integerParamIndex += 2;
for (sint32 i = 0; i < tempLen; i++)
{
if (writeIndex >= maxLength)
break;
strOut[writeIndex] = tempStr[i];
writeIndex++;
}
}
else
{
// unsupported / unknown specifier
cemu_assert_debug(false);
break;
}
}
else
{
if (writeIndex >= maxLength)
break;
strOut[writeIndex] = c;
writeIndex++;
formatStr++;
}
}
strOut[std::min(writeIndex, maxLength - 1)] = '\0';
return std::min(writeIndex, maxLength - 1);
}
sint32 __os_snprintf(char* outputStr, sint32 maxLength, const char* formatStr)
{
sint32 r = ppcSprintf(formatStr, outputStr, maxLength, PPCInterpreter_getCurrentInstance(), 3);
return r;
}
enum class CafeLogType
{
OSCONSOLE = 0,
};
struct CafeLogBuffer
{
std::array<char, 270> lineBuffer;
size_t lineLength{};
};
CafeLogBuffer g_logBuffer_OSReport;
CafeLogBuffer& getLogBuffer(CafeLogType cafeLogType)
{
if (cafeLogType == CafeLogType::OSCONSOLE)
return g_logBuffer_OSReport;
// default to OSReport
return g_logBuffer_OSReport;
}
std::string_view getLogBufferName(CafeLogType cafeLogType)
{
if (cafeLogType == CafeLogType::OSCONSOLE)
return "OSConsole";
return "Unknown";
}
std::mutex sCafeConsoleMutex;
void WriteCafeConsole(CafeLogType cafeLogType, const char* msg, sint32 len)
{
std::unique_lock _l(sCafeConsoleMutex);
CafeLogBuffer& logBuffer = getLogBuffer(cafeLogType);
// once a line is full or \n is written it will be posted to log
auto flushLine = [](CafeLogBuffer& cafeLogBuffer, std::string_view cafeLogName) -> void
{
cemuLog_log(LogType::CoreinitLogging, "[{0}] {1}", cafeLogName, std::basic_string_view(cafeLogBuffer.lineBuffer.data(), cafeLogBuffer.lineLength));
cafeLogBuffer.lineLength = 0;
};
while (len)
{
char c = *msg;
msg++;
len--;
if (c == '\r')
continue;
if (c == '\n')
{
// flush line immediately
flushLine(logBuffer, getLogBufferName(cafeLogType));
continue;
}
logBuffer.lineBuffer[logBuffer.lineLength] = c;
logBuffer.lineLength++;
if (logBuffer.lineLength >= logBuffer.lineBuffer.size())
flushLine(logBuffer, getLogBufferName(cafeLogType));
}
}
void OSReport(const char* format)
{
char buffer[1024 * 2];
sint32 len = ppcSprintf(format, buffer, sizeof(buffer), PPCInterpreter_getCurrentInstance(), 1);
WriteCafeConsole(CafeLogType::OSCONSOLE, buffer, len);
}
void OSVReport(const char* format, MPTR vaArgs)
{
cemu_assert_unimplemented();
}
void COSWarn(int moduleId, const char* format)
{
char buffer[1024 * 2];
int prefixLen = sprintf(buffer, "[COSWarn-%d] ", moduleId);
sint32 len = ppcSprintf(format, buffer + prefixLen, sizeof(buffer) - prefixLen, PPCInterpreter_getCurrentInstance(), 2);
WriteCafeConsole(CafeLogType::OSCONSOLE, buffer, len + prefixLen);
}
void OSLogPrintf(int ukn1, int ukn2, int ukn3, const char* format)
{
char buffer[1024 * 2];
int prefixLen = sprintf(buffer, "[OSLogPrintf-%d-%d-%d] ", ukn1, ukn2, ukn3);
sint32 len = ppcSprintf(format, buffer + prefixLen, sizeof(buffer) - prefixLen, PPCInterpreter_getCurrentInstance(), 4);
WriteCafeConsole(CafeLogType::OSCONSOLE, buffer, len + prefixLen);
}
void OSConsoleWrite(const char* strPtr, sint32 length)
{
if (length < 0)
return;
WriteCafeConsole(CafeLogType::OSCONSOLE, strPtr, length);
}
/* home button menu */
bool g_homeButtonMenuEnabled = false;
bool OSIsHomeButtonMenuEnabled()
{
return g_homeButtonMenuEnabled;
}
bool OSEnableHomeButtonMenu(bool enable)
{
g_homeButtonMenuEnabled = enable;
return true;
}
uint32 OSGetPFID()
{
return 15; // hardcoded as game
}
uint32 OSGetUPID()
{
return OSGetPFID();
}
uint64 s_currentTitleId;
uint64 OSGetTitleID()
{
return s_currentTitleId;
}
uint32 s_sdkVersion;
uint32 __OSGetProcessSDKVersion()
{
return s_sdkVersion;
}
// move this to CafeSystem.cpp?
void OSLauncherThread(uint64 titleId)
{
CafeSystem::ShutdownTitle();
CafeSystem::PrepareForegroundTitle(titleId);
CafeSystem::RequestRecreateCanvas();
CafeSystem::LaunchForegroundTitle();
}
uint32 __LaunchByTitleId(uint64 titleId, uint32 argc, MEMPTR<char>* argv)
{
// prepare argument buffer
#if 0
char argumentBuffer[4096];
uint32 argumentBufferLength = 0;
char* argWriter = argumentBuffer;
for(uint32 i=0; i<argc; i++)
{
const char* arg = argv[i];
uint32 argLength = strlen(arg);
if((argumentBufferLength + argLength + 1) >= sizeof(argumentBuffer))
{
// argument buffer full
cemuLog_logDebug(LogType::Force, "LaunchByTitleId: argument buffer full");
return 0x80000000;
}
memcpy(argWriter, arg, argLength);
argWriter[argLength] = '\0';
argWriter += argLength + 1;
argumentBufferLength += argLength + 1;
}
#endif
// normally the above buffer is passed to the PPC kernel via syscall 0x2B and then
// the kernel forwards it to IOSU MCP when requesting a title launch
// but for now we HLE most of the launching code and can just set the argument array directly
std::vector<std::string> argArray;
for(uint32 i=0; i<argc; i++)
argArray.emplace_back(argv[i]);
CafeSystem::SetOverrideArgs(argArray);
// spawn launcher thread (this current thread will be destroyed during relaunch)
std::thread launcherThread(OSLauncherThread, titleId);
launcherThread.detach();
// suspend this thread
coreinit::OSSuspendThread(coreinit::OSGetCurrentThread());
return 0;
}
uint32 OSLaunchTitleByPathl(const char* path, uint32 pathLength, uint32 argc)
{
char appXmlPath[1024];
cemu_assert_debug(argc == 0); // custom args not supported yet
if(pathLength >= (sizeof(appXmlPath) - 32))
{
// path too long
cemuLog_logDebug(LogType::Force, "OSLaunchTitleByPathl: path too long");
return 0x80000000;
}
// read app.xml to get the titleId
memcpy(appXmlPath, path, pathLength);
appXmlPath[pathLength] = '\0';
strcat(appXmlPath, "/code/app.xml");
sint32 status;
auto fscfile = fsc_open(appXmlPath, FSC_ACCESS_FLAG::OPEN_FILE | FSC_ACCESS_FLAG::READ_PERMISSION, &status);
if (!fscfile)
{
cemuLog_logDebug(LogType::Force, "OSLaunchTitleByPathl: failed to open target app.xml");
return 0x80000000;
}
uint32 size = fsc_getFileSize(fscfile);
std::vector<uint8> tmpData(size);
fsc_readFile(fscfile, tmpData.data(), size);
fsc_close(fscfile);
// parse app.xml to get the titleId
pugi::xml_document app_doc;
if (!app_doc.load_buffer_inplace(tmpData.data(), tmpData.size()))
return false;
uint64 titleId = std::stoull(app_doc.child("app").child("title_id").child_value(), nullptr, 16);
if(titleId == 0)
{
cemuLog_logDebug(LogType::Force, "OSLaunchTitleByPathl: failed to parse titleId from app.xml");
return 0x80000000;
}
__LaunchByTitleId(titleId, 0, nullptr);
return 0;
}
uint32 OSRestartGame(uint32 argc, MEMPTR<char>* argv)
{
__LaunchByTitleId(CafeSystem::GetForegroundTitleId(), argc, argv);
return 0;
}
void OSReleaseForeground()
{
cemuLog_logDebug(LogType::Force, "OSReleaseForeground not implemented");
}
bool s_transitionToBackground = false;
bool s_transitionToForeground = false;
void StartBackgroundForegroundTransition()
{
s_transitionToBackground = true;
s_transitionToForeground = true;
}
// called at the beginning of OSReceiveMessage if the queue is the system message queue
void UpdateSystemMessageQueue()
{
if(!OSIsInterruptEnabled())
return;
cemu_assert_debug(!__OSHasSchedulerLock());
// normally syscall 0x2E is used to get the next message
// for now we just have some preliminary logic here to allow a fake transition to background & foreground
if(s_transitionToBackground)
{
// add transition to background message
OSMessage msg{};
msg.data0 = stdx::to_underlying(SysMessageId::MsgReleaseForeground);
msg.data1 = 0; // 1 -> System is shutting down 0 -> Begin transitioning to background
OSMessageQueue* systemMessageQueue = coreinit::OSGetSystemMessageQueue();
if(OSSendMessage(systemMessageQueue, &msg, 0))
s_transitionToBackground = false;
return;
}
if(s_transitionToForeground)
{
// add transition to foreground message
OSMessage msg{};
msg.data0 = stdx::to_underlying(SysMessageId::MsgAcquireForeground);
msg.data1 = 1; // ?
msg.data2 = 1; // ?
OSMessageQueue* systemMessageQueue = coreinit::OSGetSystemMessageQueue();
if(OSSendMessage(systemMessageQueue, &msg, 0))
s_transitionToForeground = false;
return;
}
}
// called when OSReceiveMessage returns a message from the system message queue
void HandleReceivedSystemMessage(OSMessage* msg)
{
cemu_assert_debug(!__OSHasSchedulerLock());
cemuLog_log(LogType::Force, "Receiving message: {:08x}", (uint32)msg->data0);
}
uint32 OSDriver_Register(uint32 moduleHandle, sint32 priority, OSDriverInterface* driverCallbacks, sint32 driverId, uint32be* outUkn1, uint32be* outUkn2, uint32be* outUkn3)
{
cemuLog_logDebug(LogType::Force, "OSDriver_Register stubbed");
return 0;
}
uint32 OSDriver_Deregister(uint32 moduleHandle, sint32 driverId)
{
cemuLog_logDebug(LogType::Force, "OSDriver_Deregister stubbed");
return 0;
}
void miscInit()
{
s_currentTitleId = CafeSystem::GetForegroundTitleId();
s_sdkVersion = CafeSystem::GetForegroundTitleSDKVersion();
s_transitionToBackground = false;
s_transitionToForeground = false;
cafeExportRegister("coreinit", __os_snprintf, LogType::Placeholder);
cafeExportRegister("coreinit", OSReport, LogType::Placeholder);
cafeExportRegister("coreinit", OSVReport, LogType::Placeholder);
cafeExportRegister("coreinit", COSWarn, LogType::Placeholder);
cafeExportRegister("coreinit", OSLogPrintf, LogType::Placeholder);
cafeExportRegister("coreinit", OSConsoleWrite, LogType::Placeholder);
cafeExportRegister("coreinit", OSGetPFID, LogType::Placeholder);
cafeExportRegister("coreinit", OSGetUPID, LogType::Placeholder);
cafeExportRegister("coreinit", OSGetTitleID, LogType::Placeholder);
cafeExportRegister("coreinit", __OSGetProcessSDKVersion, LogType::Placeholder);
g_homeButtonMenuEnabled = true; // enabled by default
// Disney Infinity 2.0 actually relies on home button menu being enabled by default. If it's false it will crash due to calling erreula->IsAppearHomeNixSign() before initializing erreula
cafeExportRegister("coreinit", OSIsHomeButtonMenuEnabled, LogType::CoreinitThread);
cafeExportRegister("coreinit", OSEnableHomeButtonMenu, LogType::CoreinitThread);
cafeExportRegister("coreinit", OSLaunchTitleByPathl, LogType::Placeholder);
cafeExportRegister("coreinit", OSRestartGame, LogType::Placeholder);
cafeExportRegister("coreinit", OSReleaseForeground, LogType::Placeholder);
cafeExportRegister("coreinit", OSDriver_Register, LogType::Placeholder);
cafeExportRegister("coreinit", OSDriver_Deregister, LogType::Placeholder);
}
};