-
Notifications
You must be signed in to change notification settings - Fork 502
/
coreinit_FS.cpp
2779 lines (2389 loc) · 102 KB
/
coreinit_FS.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <OS/RPL/rpl.h>
#include "config/ActiveSettings.h"
#include "Cafe/OS/libs/coreinit/coreinit_SystemInfo.h"
#include "Cafe/OS/common/OSCommon.h"
#include "Cafe/OS/libs/coreinit/coreinit_Thread.h"
#include "Cafe/OS/libs/coreinit/coreinit_FS.h"
#include "Cafe/OS/libs/coreinit/coreinit_MessageQueue.h"
#include "util/helpers/Semaphore.h"
#include "Cafe/HW/Espresso/PPCCallback.h"
#include "Cafe/IOSU/iosu_ipc_common.h"
#include "coreinit_IPC.h"
#include "Cafe/Filesystem/fsc.h"
#include "coreinit_IPCBuf.h"
#include "Cafe/CafeSystem.h"
#include "Cafe/TitleList/TitleInfo.h"
#define FS_CB_PLACEHOLDER_FINISHCMD (MPTR)(0xF122330E)
// return false if src+'\0' does not fit into dst
template<std::size_t Size>
bool strcpy_whole(char (&dst)[Size], const char* src)
{
size_t inputLength = strlen(src);
if ((inputLength + 1) > Size)
{
dst[0] = '\0';
return false;
}
strcpy(dst, src);
return true;
}
bool strcpy_whole(char* dst, size_t dstLength, const char* src)
{
size_t inputLength = strlen(src);
if ((inputLength + 1) > dstLength)
{
dst[0] = '\0';
return false;
}
strcpy(dst, src);
return true;
}
namespace coreinit
{
SysAllocator<OSMutex> s_fsGlobalMutex;
inline void FSLockMutex()
{
OSLockMutex(&s_fsGlobalMutex);
}
inline void FSUnlockMutex()
{
OSUnlockMutex(&s_fsGlobalMutex);
}
void _debugVerifyCommand(const char* stage, FSCmdBlockBody_t* fsCmdBlockBody);
bool sFSInitialized = true; // this should be false but it seems like some games rely on FSInit being called before main()? Twilight Princess for example reads files before it calls FSInit
bool sFSShutdown = false;
enum class MOUNT_TYPE : uint32
{
SD = 0,
// 1 = usb?
};
struct FS_MOUNT_SOURCE
{
uint32be sourceType; // ukn values
char path[128]; // todo - determine correct length
};
FS_RESULT FSGetMountSourceNext(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, MOUNT_TYPE mountSourceType, FS_MOUNT_SOURCE* mountSourceInfo, FS_ERROR_MASK errMask)
{
if (mountSourceType == MOUNT_TYPE::SD)
{
// This function is supposed to be called after an initial FSGetMountSource call => always returns FS_RESULT::END_ITERATION because we only have one SD Card
// It *might* causes issues if this function is called for getting the first MountSource (instead of "FSGetMountSource")
cemu_assert_suspicious();
return FS_RESULT::END_ITERATION;
}
else
{
cemu_assert_unimplemented();
}
return FS_RESULT::END_ITERATION;
}
FS_RESULT FSGetMountSource(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, MOUNT_TYPE mountSourceType, FS_MOUNT_SOURCE* mountSourceInfo, FS_ERROR_MASK errMask)
{
// This implementation is simplified A LOT compared to what the Wii U is actually doing. On Cemu we expect to only have one mountable source (SD Card) anyway,
// so we can just hard code it. Other mount types are not (yet) supported.
if (mountSourceType == MOUNT_TYPE::SD)
{
// check for SD card permissions (from cos.xml)
// One Piece relies on failing here, otherwise it will call FSGetMountSource in an infinite loop
CosCapabilityBitsFS perms = static_cast<CosCapabilityBitsFS>(CafeSystem::GetForegroundTitleCosCapabilities(CosCapabilityGroup::FS));
if(!HAS_FLAG(perms, CosCapabilityBitsFS::SDCARD_MOUNT))
{
cemuLog_logOnce(LogType::Force, "Title is trying to access SD card mount info without having SD card permissions. This may not be a bug");
return FS_RESULT::END_ITERATION;
}
mountSourceInfo->sourceType = 0;
strcpy(mountSourceInfo->path, "/sd");
return FS_RESULT::SUCCESS;
}
else
{
cemu_assert_unimplemented();
}
return FS_RESULT::END_ITERATION;
}
bool _sdCard01Mounted = false;
bool _mlc01Mounted = false;
void mountSDCard()
{
if (_sdCard01Mounted)
return;
std::error_code ec;
const auto path = ActiveSettings::GetUserDataPath("sdcard/");
fs::create_directories(path, ec);
FSCDeviceHostFS_Mount("/vol/external01", _pathToUtf8(path), FSC_PRIORITY_BASE);
_sdCard01Mounted = true;
}
FS_RESULT FSMount(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, FS_MOUNT_SOURCE* mountSourceInfo, char* mountPathOut, sint32 mountPathMaxLength, FS_ERROR_MASK errMask)
{
if (mountSourceInfo->sourceType != 0)
{
return FS_RESULT::ERR_PLACEHOLDER;
}
if (!strcmp(mountSourceInfo->path, "/sd"))
{
const char* sdMountPath = "/vol/external01";
// note: mount path should not end with a slash
if (!strcpy_whole(mountPathOut, mountPathMaxLength, sdMountPath))
return FS_RESULT::ERR_PLACEHOLDER; // string does not fit
mountSDCard();
}
return FS_RESULT::SUCCESS;
}
FS_RESULT FSBindMount(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, char* mountPathSrc, char* mountPathOut, FS_ERROR_MASK errMask)
{
if (strcmp(mountPathSrc, "/dev/sdcard01") == 0)
{
if (_sdCard01Mounted)
return FS_RESULT::ERR_PLACEHOLDER;
std::error_code ec;
const auto path = ActiveSettings::GetUserDataPath("sdcard/");
fs::create_directories(path, ec);
if (!FSCDeviceHostFS_Mount(mountPathOut, _pathToUtf8(path), FSC_PRIORITY_BASE))
return FS_RESULT::ERR_PLACEHOLDER;
_sdCard01Mounted = true;
}
else if (strcmp(mountPathSrc, "/dev/mlc01") == 0)
{
if (_mlc01Mounted)
return FS_RESULT::ERR_PLACEHOLDER;
if (!FSCDeviceHostFS_Mount(mountPathOut, _pathToUtf8(ActiveSettings::GetMlcPath()), FSC_PRIORITY_BASE))
return FS_RESULT::ERR_PLACEHOLDER;
_mlc01Mounted = true;
}
else
{
return FS_RESULT::ERR_PLACEHOLDER;
}
return FS_RESULT::SUCCESS;
}
// return the aligned FSClientBody struct inside a FSClient struct
FSClientBody_t* __FSGetClientBody(FSClient_t* fsClient)
{
// align pointer to 64 bytes
if (fsClient == nullptr)
return nullptr;
FSClientBody_t* fsClientBody = (FSClientBody_t*)(((uintptr_t)fsClient + 0x3F) & ~0x3F);
fsClientBody->selfClient = fsClient;
return fsClientBody;
}
// return the aligned FSClientBody struct inside a FSClient struct
FSCmdBlockBody_t* __FSGetCmdBlockBody(FSCmdBlock_t* fsCmdBlock)
{
// align pointer to 64 bytes
if (fsCmdBlock == nullptr)
return nullptr;
FSCmdBlockBody_t* fsCmdBlockBody = (FSCmdBlockBody_t*)(((uintptr_t)fsCmdBlock + 0x3F) & ~0x3F);
fsCmdBlockBody->selfCmdBlock = fsCmdBlock;
return fsCmdBlockBody;
}
void __FSErrorAndBlock(std::string_view msg)
{
cemuLog_log(LogType::Force, "Critical error in FS: {}", msg);
while (true)
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
/* FS client management */
FSClientBody_t* g_fsRegisteredClientBodies = nullptr;
sint32 FSGetClientNum()
{
sint32 clientNum = 0;
FSClientBody_t* fsBodyItr = g_fsRegisteredClientBodies;
while (fsBodyItr)
{
clientNum++;
fsBodyItr = fsBodyItr->fsClientBodyNext.GetPtr();
}
return clientNum;
}
bool __FSIsClientRegistered(FSClientBody_t* fsClientBody)
{
FSLockMutex();
FSClientBody_t* fsClientBodyFirst = g_fsRegisteredClientBodies;
FSClientBody_t* fsClientBodyItr = fsClientBodyFirst;
if (fsClientBodyItr == NULL)
{
FSUnlockMutex();
return false;
}
while (true)
{
if (fsClientBody == fsClientBodyItr)
{
FSUnlockMutex();
return true;
}
// next
fsClientBodyItr = fsClientBodyItr->fsClientBodyNext.GetPtr();
if (fsClientBodyItr == MPTR_NULL)
break;
if (fsClientBodyItr == fsClientBodyFirst)
break;
}
FSUnlockMutex();
return false;
}
void __FSInitCmdQueue(FSCmdQueue* fsCmdQueueBE, MPTR dequeueHandlerFuncMPTR, uint32 numMaxCommandsInFlight)
{
cemu_assert(numMaxCommandsInFlight > 0);
fsCmdQueueBE->dequeueHandlerFuncMPTR = _swapEndianU32(dequeueHandlerFuncMPTR);
fsCmdQueueBE->numCommandsInFlight = 0;
fsCmdQueueBE->numMaxCommandsInFlight = numMaxCommandsInFlight;
coreinit::OSFastMutex_Init(&fsCmdQueueBE->fastMutex, nullptr);
fsCmdQueueBE->firstMPTR = _swapEndianU32(0);
fsCmdQueueBE->lastMPTR = _swapEndianU32(0);
}
void FSInit()
{
sFSInitialized = true;
}
void FSShutdown()
{
cemuLog_log(LogType::Force, "FSShutdown called");
}
FS_RESULT FSAddClientEx(FSClient_t* fsClient, uint32 uknR4, uint32 errHandling)
{
if (!sFSInitialized || sFSShutdown || !fsClient)
{
__FSErrorAndBlock("Called FSAddClient(Ex) with invalid parameters or while FS is not initialized");
return FS_RESULT::FATAL_ERROR;
}
FSLockMutex();
if (uknR4 != 0)
{
uint32 uknValue = memory_readU32(uknR4 + 0x00);
if (uknValue == 0)
{
FSUnlockMutex();
__FSErrorAndBlock("FSAddClientEx - unknown error");
return FS_RESULT::FATAL_ERROR;
}
}
if (__FSIsClientRegistered(__FSGetClientBody(fsClient)))
{
FSUnlockMutex();
__FSErrorAndBlock("Called FSAddClient(Ex) on client that was already added");
return FS_RESULT::FATAL_ERROR;
}
FSClientBody_t* fsClientBody = __FSGetClientBody(fsClient);
__FSInitCmdQueue(&fsClientBody->fsCmdQueue, MPTR_NULL, 1);
IOSDevHandle devHandle = IOS_Open("/dev/fsa", 0);
if (IOS_ResultIsError((IOS_ERROR)devHandle))
{
cemuLog_log(LogType::Force, "FSAddClientEx(): Exhausted device handles");
cemu_assert(IOS_ResultIsError((IOS_ERROR)devHandle));
FSUnlockMutex();
return FS_RESULT::FATAL_ERROR;
}
fsClientBody->iosuFSAHandle = devHandle;
// add to list of registered clients
if (g_fsRegisteredClientBodies != MPTR_NULL)
{
fsClientBody->fsClientBodyNext = g_fsRegisteredClientBodies;
g_fsRegisteredClientBodies = fsClientBody;
}
else
{
g_fsRegisteredClientBodies = fsClientBody;
fsClientBody->fsClientBodyNext = nullptr;
}
FSUnlockMutex();
return FS_RESULT::SUCCESS;
}
FS_RESULT FSAddClient(FSClient_t* fsClient, uint32 errHandling)
{
return FSAddClientEx(fsClient, 0, errHandling);
}
FS_RESULT FSDelClient(FSClient_t* fsClient, uint32 errHandling)
{
if (sFSInitialized == false || sFSShutdown == true || !fsClient)
{
__FSErrorAndBlock("Called FSDelClient with invalid parameters or while FS is not initialized");
return FS_RESULT::FATAL_ERROR;
}
FSClientBody_t* fsClientBody = __FSGetClientBody(fsClient);
FSLockMutex();
IOS_Close(fsClientBody->iosuFSAHandle);
// todo: wait till in-flight transactions are done
// remove from list
if (g_fsRegisteredClientBodies == fsClientBody)
{
// first entry in list
g_fsRegisteredClientBodies = fsClientBody->fsClientBodyNext.GetPtr();
}
else
{
// scan for entry in list
FSClientBody_t* fsBodyItr = g_fsRegisteredClientBodies;
FSClientBody_t* fsBodyPrev = g_fsRegisteredClientBodies;
bool entryFound = false;
while (fsBodyItr)
{
if (fsBodyItr == fsClientBody)
{
fsBodyPrev->fsClientBodyNext = fsClientBody->fsClientBodyNext;
entryFound = true;
break;
}
// next
fsBodyPrev = fsBodyItr;
fsBodyItr = fsBodyItr->fsClientBodyNext.GetPtr();
}
if (entryFound == false)
{
#ifdef CEMU_DEBUG_ASSERT
assert_dbg();
#endif
}
}
FSUnlockMutex();
return FS_RESULT::SUCCESS;
}
/* FS command queue */
Semaphore g_semaphoreQueuedCmds;
void __FSQueueCmdByPriority(FSCmdQueue* fsCmdQueueBE, FSCmdBlockBody_t* fsCmdBlockBody, bool stopAtEqualPriority)
{
MPTR fsCmdBlockBodyMPTR = memory_getVirtualOffsetFromPointer(fsCmdBlockBody);
if (_swapEndianU32(fsCmdQueueBE->firstMPTR) == MPTR_NULL)
{
// queue is currently empty
cemu_assert(fsCmdQueueBE->lastMPTR == MPTR_NULL);
fsCmdQueueBE->firstMPTR = _swapEndianU32(fsCmdBlockBodyMPTR);
fsCmdQueueBE->lastMPTR = _swapEndianU32(fsCmdBlockBodyMPTR);
fsCmdBlockBody->nextMPTR = _swapEndianU32(MPTR_NULL);
fsCmdBlockBody->previousMPTR = _swapEndianU32(MPTR_NULL);
return;
}
// iterate from last to first element as long as iterated priority is lower
FSCmdBlockBody_t* fsCmdBlockBodyItrPrev = NULL;
FSCmdBlockBody_t* fsCmdBlockBodyItr = (FSCmdBlockBody_t*)memory_getPointerFromVirtualOffsetAllowNull(_swapEndianU32(fsCmdQueueBE->lastMPTR));
while (true)
{
if (fsCmdBlockBodyItr == NULL)
{
// insert at the head of the list
fsCmdQueueBE->firstMPTR = _swapEndianU32(fsCmdBlockBodyMPTR);
fsCmdBlockBody->nextMPTR = _swapEndianU32(memory_getVirtualOffsetFromPointer(fsCmdBlockBodyItrPrev));
fsCmdBlockBody->previousMPTR = _swapEndianU32(MPTR_NULL);
fsCmdBlockBodyItrPrev->previousMPTR = _swapEndianU32(memory_getVirtualOffsetFromPointer(fsCmdBlockBody));
return;
}
// compare priority
if ((stopAtEqualPriority && fsCmdBlockBodyItr->priority >= fsCmdBlockBody->priority) || (stopAtEqualPriority == false && fsCmdBlockBodyItr->priority > fsCmdBlockBody->priority))
{
// insert cmd here
if (fsCmdBlockBodyItrPrev != NULL)
{
fsCmdBlockBody->nextMPTR = _swapEndianU32(memory_getVirtualOffsetFromPointer(fsCmdBlockBodyItrPrev));
}
else
{
fsCmdBlockBody->nextMPTR = _swapEndianU32(MPTR_NULL);
fsCmdQueueBE->lastMPTR = _swapEndianU32(fsCmdBlockBodyMPTR);
}
fsCmdBlockBody->previousMPTR = _swapEndianU32(memory_getVirtualOffsetFromPointer(fsCmdBlockBodyItr));
if (fsCmdBlockBodyItrPrev)
fsCmdBlockBodyItrPrev->previousMPTR = _swapEndianU32(memory_getVirtualOffsetFromPointer(fsCmdBlockBody));
fsCmdBlockBodyItr->nextMPTR = _swapEndianU32(memory_getVirtualOffsetFromPointer(fsCmdBlockBody));
return;
}
// next
fsCmdBlockBodyItrPrev = fsCmdBlockBodyItr;
fsCmdBlockBodyItr = (FSCmdBlockBody_t*)memory_getPointerFromVirtualOffsetAllowNull(_swapEndianU32(fsCmdBlockBodyItr->previousMPTR));
}
}
FSCmdBlockBody_t* __FSTakeCommandFromQueue(FSCmdQueue* cmdQueue)
{
FSCmdBlockBody_t* dequeuedCmd = nullptr;
if (_swapEndianU32(cmdQueue->firstMPTR) != MPTR_NULL)
{
dequeuedCmd = (FSCmdBlockBody_t*)memory_getPointerFromVirtualOffset(_swapEndianU32(cmdQueue->firstMPTR));
// dequeue cmd
if (cmdQueue->firstMPTR == cmdQueue->lastMPTR)
cmdQueue->lastMPTR = _swapEndianU32(MPTR_NULL);
cmdQueue->firstMPTR = dequeuedCmd->nextMPTR;
dequeuedCmd->nextMPTR = _swapEndianU32(MPTR_NULL);
if (_swapEndianU32(dequeuedCmd->nextMPTR) != MPTR_NULL)
{
FSCmdBlockBody_t* fsCmdBodyNext = (FSCmdBlockBody_t*)memory_getPointerFromVirtualOffset(_swapEndianU32(dequeuedCmd->nextMPTR));
fsCmdBodyNext->previousMPTR = _swapEndianU32(MPTR_NULL);
}
}
return dequeuedCmd;
}
void __FSAIoctlResponseCallback(PPCInterpreter_t* hCPU);
void __FSAIPCSubmitCommandAsync(iosu::fsa::FSAShimBuffer* shimBuffer, const MEMPTR<void>& callback, void* context)
{
if (shimBuffer->ipcReqType == 0)
{
IOS_ERROR r = IOS_IoctlAsync(shimBuffer->fsaDevHandle, shimBuffer->operationType, &shimBuffer->request, sizeof(shimBuffer->request), &shimBuffer->response, sizeof(shimBuffer->response), callback, context);
cemu_assert(!IOS_ResultIsError(r));
}
else if (shimBuffer->ipcReqType == 1)
{
IOS_ERROR r = IOS_IoctlvAsync(shimBuffer->fsaDevHandle, shimBuffer->operationType, shimBuffer->ioctlvVecIn, shimBuffer->ioctlvVecOut, shimBuffer->ioctlvVec, callback, context);
cemu_assert(!IOS_ResultIsError(r));
}
else
{
cemu_assert_error();
}
}
FSA_RESULT __FSADecodeIOSErrorToFSA(IOS_ERROR result)
{
return (FSA_RESULT)result;
}
FSA_RESULT __FSAIPCSubmitCommand(iosu::fsa::FSAShimBuffer* shimBuffer)
{
if (shimBuffer->ipcReqType == 0)
{
IOS_ERROR result = IOS_Ioctl(shimBuffer->fsaDevHandle, shimBuffer->operationType, &shimBuffer->request, sizeof(shimBuffer->request), &shimBuffer->response, sizeof(shimBuffer->response));
return __FSADecodeIOSErrorToFSA(result);
}
else if (shimBuffer->ipcReqType == 1)
{
IOS_ERROR result = IOS_Ioctlv(shimBuffer->fsaDevHandle, shimBuffer->operationType, shimBuffer->ioctlvVecIn, shimBuffer->ioctlvVecOut, shimBuffer->ioctlvVec);
return __FSADecodeIOSErrorToFSA(result);
}
return FSA_RESULT::FATAL_ERROR;
}
void __FSUpdateQueue(FSCmdQueue* cmdQueue)
{
FSLockMutex();
if (cmdQueue->numCommandsInFlight < cmdQueue->numMaxCommandsInFlight)
{
FSCmdBlockBody_t* dequeuedCommand = __FSTakeCommandFromQueue(cmdQueue);
if (dequeuedCommand)
{
cmdQueue->numCommandsInFlight += 1;
if (cmdQueue->numCommandsInFlight >= cmdQueue->numMaxCommandsInFlight)
cmdQueue->queueFlags = cmdQueue->queueFlags | FSCmdQueue::QUEUE_FLAG::IS_FULL;
cemu_assert_debug(cmdQueue->dequeueHandlerFuncMPTR == 0); // not supported. We HLE call the handler here
__FSAIPCSubmitCommandAsync(&dequeuedCommand->fsaShimBuffer, MEMPTR<void>(PPCInterpreter_makeCallableExportDepr(__FSAIoctlResponseCallback)), &dequeuedCommand->fsaShimBuffer);
}
}
FSUnlockMutex();
}
void __FSQueueDefaultFinishFunc(FSCmdBlockBody_t* fsCmdBlockBody, FS_RESULT result)
{
switch ((FSA_CMD_OPERATION_TYPE)fsCmdBlockBody->fsaShimBuffer.operationType.value())
{
case FSA_CMD_OPERATION_TYPE::OPENFILE:
{
*fsCmdBlockBody->returnValues.cmdOpenFile.handlePtr = fsCmdBlockBody->fsaShimBuffer.response.cmdOpenFile.fileHandleOutput;
break;
}
case FSA_CMD_OPERATION_TYPE::GETCWD:
{
auto transferSize = fsCmdBlockBody->returnValues.cmdGetCwd.transferSize;
if (transferSize < 0 && transferSize > sizeof(fsCmdBlockBody->fsaShimBuffer.response.cmdGetCWD.path))
{
cemu_assert_error();
}
memcpy(fsCmdBlockBody->returnValues.cmdGetCwd.pathPtr, fsCmdBlockBody->fsaShimBuffer.response.cmdGetCWD.path, transferSize);
break;
}
case FSA_CMD_OPERATION_TYPE::OPENDIR:
{
*fsCmdBlockBody->returnValues.cmdOpenDir.handlePtr = fsCmdBlockBody->fsaShimBuffer.response.cmdOpenDir.dirHandleOutput;
break;
}
case FSA_CMD_OPERATION_TYPE::READDIR:
{
*fsCmdBlockBody->returnValues.cmdReadDir.dirEntryPtr = fsCmdBlockBody->fsaShimBuffer.response.cmdReadDir.dirEntry;
break;
}
case FSA_CMD_OPERATION_TYPE::GETPOS:
{
*fsCmdBlockBody->returnValues.cmdGetPosFile.filePosPtr = fsCmdBlockBody->fsaShimBuffer.response.cmdGetPosFile.filePos;
break;
}
case FSA_CMD_OPERATION_TYPE::GETSTATFILE:
{
*((FSStat_t*)fsCmdBlockBody->returnValues.cmdStatFile.resultPtr.GetPtr()) = fsCmdBlockBody->fsaShimBuffer.response.cmdStatFile.statOut;
break;
}
case FSA_CMD_OPERATION_TYPE::QUERYINFO:
{
if (fsCmdBlockBody->fsaShimBuffer.request.cmdQueryInfo.queryType == FSA_QUERY_TYPE_FREESPACE)
{
*((uint64be*)fsCmdBlockBody->returnValues.cmdQueryInfo.queryResultPtr.GetPtr()) = fsCmdBlockBody->fsaShimBuffer.response.cmdQueryInfo.queryFreeSpace.freespace;
}
else if (fsCmdBlockBody->fsaShimBuffer.request.cmdQueryInfo.queryType == FSA_QUERY_TYPE_STAT)
{
*((FSStat_t*)fsCmdBlockBody->returnValues.cmdQueryInfo.queryResultPtr.GetPtr()) = fsCmdBlockBody->fsaShimBuffer.response.cmdQueryInfo.queryStat.stat;
}
else
{
cemu_assert_unimplemented();
}
break;
}
case FSA_CMD_OPERATION_TYPE::CHANGEDIR:
case FSA_CMD_OPERATION_TYPE::MAKEDIR:
case FSA_CMD_OPERATION_TYPE::REMOVE:
case FSA_CMD_OPERATION_TYPE::RENAME:
case FSA_CMD_OPERATION_TYPE::CLOSEDIR:
case FSA_CMD_OPERATION_TYPE::READ:
case FSA_CMD_OPERATION_TYPE::WRITE:
case FSA_CMD_OPERATION_TYPE::SETPOS:
case FSA_CMD_OPERATION_TYPE::ISEOF:
case FSA_CMD_OPERATION_TYPE::CLOSEFILE:
case FSA_CMD_OPERATION_TYPE::APPENDFILE:
case FSA_CMD_OPERATION_TYPE::TRUNCATEFILE:
case FSA_CMD_OPERATION_TYPE::FLUSHQUOTA:
{
break;
}
default:
{
cemu_assert_unimplemented();
}
}
}
void export___FSQueueDefaultFinishFunc(PPCInterpreter_t* hCPU)
{
ppcDefineParamPtr(cmd, FSCmdBlockBody_t, 0);
FS_RESULT result = (FS_RESULT)PPCInterpreter_getCallParamU32(hCPU, 1);
__FSQueueDefaultFinishFunc(cmd, static_cast<FS_RESULT>(result));
osLib_returnFromFunction(hCPU, 0);
}
void __FSQueueCmd(FSCmdQueue* cmdQueue, FSCmdBlockBody_t* fsCmdBlockBody, MPTR finishCmdFunc)
{
fsCmdBlockBody->cmdFinishFuncMPTR = finishCmdFunc;
FSLockMutex();
fsCmdBlockBody->statusCode = _swapEndianU32(FSA_CMD_STATUS_CODE_D900A22);
__FSQueueCmdByPriority(cmdQueue, fsCmdBlockBody, true);
FSUnlockMutex();
__FSUpdateQueue(cmdQueue);
}
FSA_RESULT _FSIosErrorToFSAStatus(IOS_ERROR iosError)
{
return (FSA_RESULT)iosError;
}
FS_RESULT _FSAStatusToFSStatus(FSA_RESULT err)
{
if ((int)err > 0)
{
return (FS_RESULT)err;
}
switch (err)
{
case FSA_RESULT::OK:
{
return FS_RESULT::SUCCESS;
}
case FSA_RESULT::END_OF_DIRECTORY:
case FSA_RESULT::END_OF_FILE:
{
return FS_RESULT::END_ITERATION;
}
case FSA_RESULT::ALREADY_EXISTS:
{
return FS_RESULT::ALREADY_EXISTS;
}
case FSA_RESULT::NOT_FOUND:
{
return FS_RESULT::NOT_FOUND;
}
case FSA_RESULT::PERMISSION_ERROR:
{
return FS_RESULT::PERMISSION_ERROR;
}
case FSA_RESULT::NOT_FILE:
{
return FS_RESULT::NOT_FILE;
}
case FSA_RESULT::NOT_DIR:
{
return FS_RESULT::NOT_DIR;
}
case FSA_RESULT::MAX_FILES:
case FSA_RESULT::MAX_DIRS:
{
return FS_RESULT::MAX_HANDLES;
}
case FSA_RESULT::INVALID_CLIENT_HANDLE:
case FSA_RESULT::INVALID_FILE_HANDLE:
case FSA_RESULT::INVALID_DIR_HANDLE:
case FSA_RESULT::INVALID_PARAM:
case FSA_RESULT::INVALID_PATH:
case FSA_RESULT::INVALID_BUFFER:
case FSA_RESULT::INVALID_ALIGNMENT:
case FSA_RESULT::NOT_INIT:
case FSA_RESULT::MAX_CLIENTS:
case FSA_RESULT::OUT_OF_RESOURCES:
case FSA_RESULT::FATAL_ERROR:
{
return FS_RESULT::FATAL_ERROR;
}
}
cemu_assert_unimplemented();
return FS_RESULT::FATAL_ERROR;
}
void __FSCmdSubmitResult(FSCmdBlockBody_t* fsCmdBlockBody, FS_RESULT result)
{
_debugVerifyCommand("FSCmdSubmitResult", fsCmdBlockBody);
FSClientBody_t* fsClientBody = fsCmdBlockBody->fsClientBody.GetPtr();
OSFastMutex_Lock(&fsClientBody->fsCmdQueue.fastMutex);
fsCmdBlockBody->cancelState &= ~(1 << 0); // clear cancel bit
if (fsClientBody->currentCmdBlockBody.GetPtr() == fsCmdBlockBody)
fsClientBody->currentCmdBlockBody = nullptr;
fsCmdBlockBody->statusCode = _swapEndianU32(FSA_CMD_STATUS_CODE_D900A24);
OSFastMutex_Unlock(&fsClientBody->fsCmdQueue.fastMutex);
// send result via msg queue or callback
cemu_assert_debug(!fsCmdBlockBody->asyncResult.fsAsyncParamsNew.ioMsgQueue != !fsCmdBlockBody->asyncResult.fsAsyncParamsNew.userCallback); // either must be set
fsCmdBlockBody->ukn09EA = 0;
fsCmdBlockBody->ukn9F4_lastErrorRelated = 0;
if (fsCmdBlockBody->asyncResult.fsAsyncParamsNew.ioMsgQueue)
{
// msg queue
_debugVerifyCommand("SubmitResultQueue", fsCmdBlockBody);
coreinit::OSMessageQueue* ioMsgQueue = fsCmdBlockBody->asyncResult.fsAsyncParamsNew.ioMsgQueue.GetPtr();
fsCmdBlockBody->asyncResult.fsCmdBlock = fsCmdBlockBody->selfCmdBlock;
fsCmdBlockBody->asyncResult.fsStatusNew = (uint32)result;
while (OSSendMessage(ioMsgQueue, &fsCmdBlockBody->asyncResult.msgUnion.osMsg, 0) == 0)
{
cemuLog_log(LogType::Force, "FS driver: Failed to add message to result queue. Retrying...");
if (PPCInterpreter_getCurrentInstance())
PPCCore_switchToScheduler();
else
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
else
{
// callback
_debugVerifyCommand("SubmitResultCallback", fsCmdBlockBody);
FSClient_t* fsClient = fsCmdBlockBody->asyncResult.fsClient.GetPtr();
FSCmdBlock_t* fsCmdBlock = fsCmdBlockBody->asyncResult.fsCmdBlock.GetPtr();
PPCCoreCallback(fsCmdBlockBody->asyncResult.fsAsyncParamsNew.userCallback, fsClient, fsCmdBlock, (sint32)result, fsCmdBlockBody->asyncResult.fsAsyncParamsNew.userContext);
}
}
void __FSAIoctlResponseCallback(PPCInterpreter_t* hCPU)
{
ppcDefineParamU32(iosResult, 0);
ppcDefineParamPtr(cmd, FSCmdBlockBody_t, 1);
FSA_RESULT fsaStatus = _FSIosErrorToFSAStatus((IOS_ERROR)iosResult);
cmd->statusCode = _swapEndianU32(FSA_CMD_STATUS_CODE_D900A26);
cmd->lastFSAStatus = fsaStatus;
// translate error code to FSStatus
FS_RESULT fsStatus = _FSAStatusToFSStatus(fsaStatus);
// On actual hardware this delegates the processing to the AppIO threads, but for now we just run it directly from the IPC thread
FSClientBody_t* client = cmd->fsClientBody;
FSCmdQueue& cmdQueue = client->fsCmdQueue;
FSLockMutex();
cmdQueue.numCommandsInFlight -= 1;
cmdQueue.queueFlags = cmdQueue.queueFlags & ~FSCmdQueue::QUEUE_FLAG::IS_FULL;
FSUnlockMutex();
if (cmd->cmdFinishFuncMPTR)
{
if (cmd->cmdFinishFuncMPTR != FS_CB_PLACEHOLDER_FINISHCMD)
PPCCoreCallback(MEMPTR<void>(cmd->cmdFinishFuncMPTR), cmd, fsStatus);
}
__FSCmdSubmitResult(cmd, fsStatus);
__FSUpdateQueue(&cmd->fsClientBody->fsCmdQueue);
osLib_returnFromFunction(hCPU, 0);
}
/* FS commands */
void FSInitCmdBlock(FSCmdBlock_t* fsCmdBlock)
{
memset(fsCmdBlock, 0x00, sizeof(FSCmdBlock_t));
FSCmdBlockBody_t* fsCmdBlockBody = __FSGetCmdBlockBody(fsCmdBlock);
fsCmdBlockBody->statusCode = _swapEndianU32(FSA_CMD_STATUS_CODE_D900A21);
fsCmdBlockBody->priority = 0x10;
}
void __FSAsyncToSyncInit(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, FSAsyncParamsNew_t* asyncParams)
{
if (fsClient == nullptr || fsCmdBlock == nullptr || asyncParams == nullptr)
assert_dbg();
FSCmdBlockBody_t* fsCmdBlockBody = __FSGetCmdBlockBody(fsCmdBlock);
coreinit::OSInitMessageQueue(&fsCmdBlockBody->syncTaskMsgQueue, fsCmdBlockBody->_syncTaskMsg, 1);
asyncParams->userCallback = nullptr;
asyncParams->userContext = nullptr;
asyncParams->ioMsgQueue = &fsCmdBlockBody->syncTaskMsgQueue;
}
void __FSPrepareCmdAsyncResult(FSClientBody_t* fsClientBody, FSCmdBlockBody_t* fsCmdBlockBody, FSAsyncResult* fsCmdBlockAsyncResult, FSAsyncParamsNew_t* fsAsyncParams)
{
memcpy(&fsCmdBlockAsyncResult->fsAsyncParamsNew, fsAsyncParams, sizeof(FSAsyncParamsNew_t));
fsCmdBlockAsyncResult->fsClient = fsClientBody->selfClient;
fsCmdBlockAsyncResult->fsCmdBlock = fsCmdBlockBody->selfCmdBlock;
fsCmdBlockAsyncResult->msgUnion.fsMsg.fsAsyncResult = fsCmdBlockAsyncResult;
fsCmdBlockAsyncResult->msgUnion.fsMsg.commandType = _swapEndianU32(8);
}
sint32 __FSPrepareCmd(FSClientBody_t* fsClientBody, FSCmdBlockBody_t* fsCmdBlockBody, uint32 errHandling, FSAsyncParamsNew_t* fsAsyncParams)
{
if (sFSInitialized == false || sFSShutdown == true)
return -0x400;
if (!fsClientBody || !fsCmdBlockBody)
{
cemu_assert(false);
return -0x400;
}
if (_swapEndianU32(fsCmdBlockBody->statusCode) != FSA_CMD_STATUS_CODE_D900A21 && _swapEndianU32(fsCmdBlockBody->statusCode) != FSA_CMD_STATUS_CODE_D900A24)
{
cemu_assert(false);
return -0x400;
}
if ((fsAsyncParams->userCallback == nullptr && fsAsyncParams->ioMsgQueue == nullptr) ||
(fsAsyncParams->userCallback != nullptr && fsAsyncParams->ioMsgQueue != nullptr))
{
// userCallback and ioMsgQueue both set or none set
cemu_assert(false);
return -0x400;
}
fsCmdBlockBody->fsClientBody = fsClientBody;
fsCmdBlockBody->errHandling = _swapEndianU32(errHandling);
fsCmdBlockBody->uknStatusGuessed09E9 = 0;
fsCmdBlockBody->cancelState &= ~(1 << 0); // clear cancel bit
fsCmdBlockBody->fsaShimBuffer.fsaDevHandle = fsClientBody->iosuFSAHandle;
__FSPrepareCmdAsyncResult(fsClientBody, fsCmdBlockBody, &fsCmdBlockBody->asyncResult, fsAsyncParams);
return 0;
}
#define _FSCmdIntro() \
FSClientBody_t* fsClientBody = __FSGetClientBody(fsClient); \
FSCmdBlockBody_t* fsCmdBlockBody = __FSGetCmdBlockBody(fsCmdBlock); \
sint32 fsError = __FSPrepareCmd(fsClientBody, fsCmdBlockBody, errorMask, fsAsyncParams); \
if (fsError != 0) \
return fsError;
void _debugVerifyCommand(const char* stage, FSCmdBlockBody_t* fsCmdBlockBody)
{
if (fsCmdBlockBody->asyncResult.msgUnion.fsMsg.commandType != _swapEndianU32(8))
{
cemuLog_log(LogType::Force, "Corrupted FS command detected in stage {}", stage);
cemuLog_log(LogType::Force, "Printing CMD block: ");
for (uint32 i = 0; i < (sizeof(FSCmdBlockBody_t) + 31) / 32; i++)
{
uint8* p = ((uint8*)fsCmdBlockBody) + i * 32;
cemuLog_log(LogType::Force, "{:04x}: {:02x} {:02x} {:02x} {:02x} - {:02x} {:02x} {:02x} {:02x} - {:02x} {:02x} {:02x} {:02x} - {:02x} {:02x} {:02x} {:02x} | {:02x} {:02x} {:02x} {:02x} - {:02x} {:02x} {:02x} {:02x} - {:02x} {:02x} {:02x} {:02x} - {:02x} {:02x} {:02x} {:02x}",
i * 32,
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15],
p[16], p[17], p[18], p[19], p[20], p[21], p[22], p[23], p[24], p[25], p[26], p[27], p[28], p[29], p[30], p[31]);
}
}
}
FSAsyncResult* FSGetAsyncResult(OSMessage* msg)
{
return (FSAsyncResult*)memory_getPointerFromVirtualOffset(_swapEndianU32(msg->message));
}
sint32 __FSProcessAsyncResult(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, sint32 fsStatus, uint32 errHandling)
{
// a positive result (or zero) means success. Most operations return zero in case of success. Read and write operations return the number of transferred units
if (fsStatus >= 0)
{
FSCmdBlockBody_t* fsCmdBlockBody = __FSGetCmdBlockBody(fsCmdBlock);
OSMessage msg;
OSReceiveMessage(&fsCmdBlockBody->syncTaskMsgQueue, &msg, OS_MESSAGE_BLOCK);
_debugVerifyCommand("handleAsyncResult", fsCmdBlockBody);
FSAsyncResult* asyncResult = FSGetAsyncResult(&msg);
return asyncResult->fsStatusNew;
}
else
{
// todo - error handling
cemuLog_log(LogType::Force, "FS handleAsyncResult(): unexpected error {:08x}", errHandling);
cemu_assert_debug(false);
return 0;
}
}
FSA_RESULT __FSPrepareCmd_OpenFile(iosu::fsa::FSAShimBuffer* fsaShimBuffer, IOSDevHandle fsaHandle, char* path, char* mode, uint32 createMode, uint32 openFlags, uint32 preallocSize)
{
if (fsaShimBuffer == NULL)
return FSA_RESULT::INVALID_BUFFER;
if (path == NULL)
return FSA_RESULT::INVALID_PATH;
if (mode == NULL)
return FSA_RESULT::INVALID_PARAM;
fsaShimBuffer->fsaDevHandle = fsaHandle;
fsaShimBuffer->ipcReqType = 0;
fsaShimBuffer->operationType = (uint32)FSA_CMD_OPERATION_TYPE::OPENFILE;
// path
size_t pathLen = strlen((char*)path);
if (pathLen >= FSA_CMD_PATH_MAX_LENGTH)
{
cemu_assert_debug(false);
pathLen = FSA_CMD_PATH_MAX_LENGTH - 1;
}
for (sint32 i = 0; i < pathLen; i++)
fsaShimBuffer->request.cmdOpenFile.path[i] = path[i];
for (size_t i = pathLen; i < FSA_CMD_PATH_MAX_LENGTH; i++)
fsaShimBuffer->request.cmdOpenFile.path[i] = '\0';
// mode
size_t modeLen = strlen((char*)mode);
if (modeLen >= 12)
{
cemu_assert_debug(false);
modeLen = 12 - 1;
}
for (sint32 i = 0; i < modeLen; i++)
fsaShimBuffer->request.cmdOpenFile.mode[i] = mode[i];
for (size_t i = modeLen; i < 12; i++)
fsaShimBuffer->request.cmdOpenFile.mode[i] = '\0';
// createMode
fsaShimBuffer->request.cmdOpenFile.createMode = createMode;
// openFlags
fsaShimBuffer->request.cmdOpenFile.openFlags = openFlags;
// preallocSize
fsaShimBuffer->request.cmdOpenFile.preallocSize = preallocSize;
fsaShimBuffer->response.cmdOpenFile.fileHandleOutput = 0xFFFFFFFF;
return FSA_RESULT::OK;
}
sint32 FSOpenFileAsync(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, char* path, char* mode, FSFileHandleDepr_t* outFileHandle, uint32 errorMask, FSAsyncParamsNew_t* fsAsyncParams)
{
_FSCmdIntro();
if (outFileHandle == nullptr || path == nullptr || mode == nullptr)
return -0x400;
fsCmdBlockBody->returnValues.cmdOpenFile.handlePtr = &outFileHandle->fileHandle;
fsError = (FSStatus)__FSPrepareCmd_OpenFile(&fsCmdBlockBody->fsaShimBuffer, fsClientBody->iosuFSAHandle, path, mode, 0x660, 0, 0);
if (fsError != (FSStatus)FS_RESULT::SUCCESS)
return fsError;
__FSQueueCmd(&fsClientBody->fsCmdQueue, fsCmdBlockBody, RPLLoader_MakePPCCallable(export___FSQueueDefaultFinishFunc));
return (FSStatus)FS_RESULT::SUCCESS;
}
sint32 FSOpenFile(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, char* path, char* mode, FSFileHandleDepr_t* fileHandle, uint32 errHandling)
{
StackAllocator<FSAsyncParamsNew_t, 1> asyncParams;
__FSAsyncToSyncInit(fsClient, fsCmdBlock, &asyncParams);
sint32 fsAsyncRet = FSOpenFileAsync(fsClient, fsCmdBlock, path, mode, fileHandle, errHandling, &asyncParams);
return __FSProcessAsyncResult(fsClient, fsCmdBlock, fsAsyncRet, errHandling);
}
sint32 FSOpenFileExAsync(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, char* path, char* mode, uint32 createMode, uint32 openFlag, uint32 preallocSize, FSFileHandleDepr_t* outFileHandle, uint32 errorMask, FSAsyncParamsNew_t* fsAsyncParams)
{
if (openFlag != 0)
{
cemuLog_log(LogType::Force, "FSOpenFileEx called with unsupported flags!");
cemu_assert_debug(false);
}
_FSCmdIntro();
if (outFileHandle == nullptr || path == nullptr || mode == nullptr)
return -0x400;
fsCmdBlockBody->returnValues.cmdOpenFile.handlePtr = &outFileHandle->fileHandle;
FSA_RESULT prepareResult = __FSPrepareCmd_OpenFile(&fsCmdBlockBody->fsaShimBuffer, fsClientBody->iosuFSAHandle, path, mode, createMode, openFlag, preallocSize);
if (prepareResult != FSA_RESULT::OK)
return (FSStatus)_FSAStatusToFSStatus(prepareResult);
__FSQueueCmd(&fsClientBody->fsCmdQueue, fsCmdBlockBody, RPLLoader_MakePPCCallable(export___FSQueueDefaultFinishFunc));
return (FSStatus)FS_RESULT::SUCCESS;
}
sint32 FSOpenFileEx(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, char* path, char* mode, uint32 createMode, uint32 openFlag, uint32 preallocSize, FSFileHandleDepr_t* fileHandle, uint32 errHandling)
{
StackAllocator<FSAsyncParamsNew_t, 1> asyncParams;
__FSAsyncToSyncInit(fsClient, fsCmdBlock, &asyncParams);
sint32 fsAsyncRet = FSOpenFileExAsync(fsClient, fsCmdBlock, path, mode, createMode, openFlag, preallocSize, fileHandle, errHandling, &asyncParams);
return __FSProcessAsyncResult(fsClient, fsCmdBlock, fsAsyncRet, errHandling);
}
FSA_RESULT __FSPrepareCmd_CloseFile(iosu::fsa::FSAShimBuffer* fsaShimBuffer, IOSDevHandle fsaHandle, uint32 fileHandle)
{
if (fsaShimBuffer == nullptr)
return FSA_RESULT::INVALID_BUFFER;
fsaShimBuffer->fsaDevHandle = fsaHandle;
fsaShimBuffer->ipcReqType = 0;
fsaShimBuffer->operationType = (uint32)FSA_CMD_OPERATION_TYPE::CLOSEFILE;
fsaShimBuffer->request.cmdCloseFile.fileHandle = fileHandle;
return FSA_RESULT::OK;
}
sint32 FSCloseFileAsync(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, uint32 fileHandle, uint32 errorMask, FSAsyncParamsNew_t* fsAsyncParams)
{
_FSCmdIntro();
FSA_RESULT prepareResult = __FSPrepareCmd_CloseFile(&fsCmdBlockBody->fsaShimBuffer, fsClientBody->iosuFSAHandle, fileHandle);
if (prepareResult != FSA_RESULT::OK)
return (FSStatus)_FSAStatusToFSStatus(prepareResult);
__FSQueueCmd(&fsClientBody->fsCmdQueue, fsCmdBlockBody, RPLLoader_MakePPCCallable(export___FSQueueDefaultFinishFunc));
return (FSStatus)FS_RESULT::SUCCESS;
}
sint32 FSCloseFile(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, uint32 fileHandle, uint32 errHandling)
{
StackAllocator<FSAsyncParamsNew_t, 1> asyncParams;
__FSAsyncToSyncInit(fsClient, fsCmdBlock, &asyncParams);
sint32 fsAsyncRet = FSCloseFileAsync(fsClient, fsCmdBlock, fileHandle, errHandling, &asyncParams);
return __FSProcessAsyncResult(fsClient, fsCmdBlock, fsAsyncRet, errHandling);
}
FSA_RESULT __FSPrepareCmd_FlushFile(iosu::fsa::FSAShimBuffer* fsaShimBuffer, IOSDevHandle fsaHandle, uint32 fileHandle)
{
if (fsaShimBuffer == nullptr)
return FSA_RESULT::INVALID_BUFFER;
fsaShimBuffer->fsaDevHandle = fsaHandle;
fsaShimBuffer->ipcReqType = 0;
fsaShimBuffer->operationType = (uint32)FSA_CMD_OPERATION_TYPE::FLUSHFILE;