Repository navigation
Expand file tree
/
Copy pathiop_boot.cpp
More file actions
1650 lines (1461 loc) · 61.5 KB
/
Copy pathiop_boot.cpp
File metadata and controls
1650 lines (1461 loc) · 61.5 KB
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
/* ================================================================================================
* File: iop_boot.cpp
* Brief: Boot-time IOP bring-up and game-data location, and every IOP module the game loads
* afterwards (pad, memory card, sound, keyboard), with the sbv patches that loading
* them takes. See iop_boot.h.
*
* After the host: probe, both console paths run on iomanX + fileXio. HDD uses DEV9 + ATAD + APA
* + PFS; USB uses BDM + FatFs + usbd + usbmass_bd. fileXioInit() routes newlib fopen/fread - and
* the whole Quake filesystem - through iomanX, reaching the mounted pfs0: partition or massN:
* volumes transparently.
*
* The IOP is taken as the launcher left it, which in practice is one of two things:
*
* - Freshly reset: the boot ROM's modules, sometimes with the ROM's SIO2MAN, MCMAN and MCSERV
* on top. LoadExecPS2 launches leave this, and so do wLaunchELF since May 2026 (ps2sdk's
* elf-loader) and older wLaunchELF for an ELF on the HDD.
* - The launcher's own drivers, still running: older wLaunchELF and uLaunchELF reset nothing
* for an ELF on USB or a memory card. They leave iomanX, fileXio, a USB stack (BDM, or
* usbhdfsd before 2022), the ROM's SIO2MAN and PADMAN, ps2sdk's memory card drivers, and
* the HDD's drivers if they read it.
*
* Either way, the reset the boot used to start with only threw away what it would load again.
* So each module is now taken from the running ones when a module doing its job is there -
* recognised by the name it registers, see kModuleIdentities - and loaded otherwise. A USB drive
* the launcher already mounted is then read with no IOP reset and no wait for the drive. The
* reset stays for what can't work alongside the game's drivers (FindResidentConflict), and as
* the fallback when a module won't start on top of what was there, when the launcher's drivers
* show no game data, or when too little IOP memory is left for the drivers that load later.
*
* This source code is released under the GNU GPL v2 license.
* ================================================================================================ */
#include "ps2/common.h"
#include "ps2/system/iop_boot.h"
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <dirent.h>
#include <kernel.h>
#include <smod.h>
#include <slib.h>
#include <sifrpc.h>
#include <iopcontrol.h>
#include <iopheap.h>
#include <loadfile.h>
#include <libpad.h>
#include <sbv_patches.h>
#include <hdd-ioctl.h>
// The header refuses direct fio/fileXio use alongside newlib unless told the
// caller knows what it is doing. File reads use the newlib backend installed by
// fileXioInit(); direct calls below enumerate, mount and sync HDD partitions.
#define NEWLIB_PORT_AWARE
#include <fileXio_rpc.h>
// IRX module images embedded by the Makefile's bin2c rule (IRX_FILES).
extern "C" {
extern unsigned char iomanX_irx[];
extern unsigned int size_iomanX_irx;
extern unsigned char fileXio_irx[];
extern unsigned int size_fileXio_irx;
extern unsigned char ps2dev9_irx[];
extern unsigned int size_ps2dev9_irx;
extern unsigned char ps2atad_irx[];
extern unsigned int size_ps2atad_irx;
extern unsigned char ps2hdd_irx[];
extern unsigned int size_ps2hdd_irx;
extern unsigned char ps2fs_irx[];
extern unsigned int size_ps2fs_irx;
extern unsigned char bdm_irx[];
extern unsigned int size_bdm_irx;
extern unsigned char bdmfs_fatfs_irx[];
extern unsigned int size_bdmfs_fatfs_irx;
extern unsigned char usbd_irx[];
extern unsigned int size_usbd_irx;
extern unsigned char usbmass_bd_irx[];
extern unsigned int size_usbmass_bd_irx;
extern unsigned char ps2kbd_irx[];
extern unsigned int size_ps2kbd_irx;
extern unsigned char libsd_irx[];
extern unsigned int size_libsd_irx;
extern unsigned char audsrv_irx[];
extern unsigned int size_audsrv_irx;
}
// 1 resets the IOP on every console boot, as boots did before they took the IOP as launched:
// to compare the two on hardware, or should a launcher ever leave something that gets past
// FindResidentConflict.
#ifndef PS2_QUAKE_ALWAYS_RESET_IOP
#define PS2_QUAKE_ALWAYS_RESET_IOP 0
#endif
namespace ps2::sys {
namespace {
// ------------------------------------------------------------------------------------------------
// The modules the game runs on
// ------------------------------------------------------------------------------------------------
// What a module doing each job registers itself as with the IOP's module manager - the name in
// its IRX_ID - so that one a launcher left running is recognised and used rather than loaded
// again: a second copy would fail to start next to it, finding its export library, RPC server or
// device name taken. The game's own module comes first in each list, then what a launcher may
// run for the same job: Sony's LIBSD, the ps2sdk or X-module memory card manager. Names are from
// ps2sdk's IRX files and the ROM's (SCPH-70012 BIOS 2.00).
//
// A module can also be known by the library it exports, whatever its name: iomanX's builds have
// no IRX_ID at all, and the USB driver wLaunchELF 4.43a left on a console (2026-10-12) didn't go
// by ps2sdk's name - the game's usbd then found the "usbd" library taken and wouldn't start.
struct ModuleIdentity
{
const char * label; // the game's name for it: the IRX's file name, or the rom0: path
const char * names[2]; // what the IOP lists a module doing the job as
const char * library; // the export library any module doing the job registers, or null
u16 minVersion; // the oldest that talks the protocol of our EE library; 0 for any
};
constexpr ModuleIdentity kModuleIdentities[] = {
{ "iomanX", { nullptr, nullptr }, "iomanx", 0 },
{ "fileXio", { "IOX/File_Manager_Rpc", nullptr }, nullptr, 0x0102 },
{ "ps2dev9", { "dev9", nullptr }, nullptr, 0 },
{ "ps2atad", { "atad", nullptr }, nullptr, 0 },
{ "ps2hdd", { "hdd_driver", nullptr }, nullptr, 0 },
{ "ps2fs", { "pfs_driver", nullptr }, nullptr, 0 },
{ "bdm", { "bdm", nullptr }, nullptr, 0 },
{ "bdmfs_fatfs", { "bdmff", nullptr }, nullptr, 0 },
{ "usbd", { "USB_driver", nullptr }, "usbd", 0 },
{ "usbmass_bd", { "usbmass_bd", nullptr }, nullptr, 0 },
{ "ps2kbd", { "PS2 USB keyboard driver", nullptr }, nullptr, 0 },
{ "libsd", { "freesd", "Sound_Device_Library" }, nullptr, 0 },
{ "audsrv", { "audsrv", nullptr }, nullptr, 0x0104 },
{ "rom0:SIO2MAN", { "sio2man", nullptr }, nullptr, 0 },
{ "rom0:PADMAN", { "padman", nullptr }, nullptr, 0 },
{ "rom0:MCMAN", { "mcman", "mcman_cex" }, nullptr, 0 },
{ "rom0:MCSERV", { "mcserv", nullptr }, nullptr, 0 },
};
// ps2sdk's USB mass storage driver before BDM, which older launchers still run. It serves mass:
// by itself, so BDM's FAT driver couldn't register that device next to it.
constexpr const char * kUsbhdfsdName = "usbhdfsd";
// The boot ROM's module loader, whose version says whether it loads modules out of EE memory.
constexpr const char * kLoadfileName = "LoadModuleByEE";
// APA holds an open handle for each PFS mount, plus one while listing partitions.
// PFS defaults to only two open files; pak handles, a music stream, temporary
// asset reads and the log can coexist. Its cache needs at least 2 * open files + 8.
constexpr char kHddArgs[] = "-o\0" "4\0" "-n\0" "20";
constexpr char kPfsArgs[] = "-m\0" "1\0" "-o\0" "16\0" "-n\0" "40";
// An IRX the ELF carries, with the arguments the boot starts it with.
struct IrxModule
{
const char * label; // as in kModuleIdentities
unsigned char * image;
const unsigned int * sizeBytes;
const char * args;
u32 argsBytes;
};
const IrxModule kIomanX = { "iomanX", iomanX_irx, &size_iomanX_irx, nullptr, 0 };
const IrxModule kFileXio = { "fileXio", fileXio_irx, &size_fileXio_irx, nullptr, 0 };
const IrxModule kPs2Dev9 = { "ps2dev9", ps2dev9_irx, &size_ps2dev9_irx, nullptr, 0 };
const IrxModule kPs2Atad = { "ps2atad", ps2atad_irx, &size_ps2atad_irx, nullptr, 0 };
const IrxModule kPs2Hdd = { "ps2hdd", ps2hdd_irx, &size_ps2hdd_irx, kHddArgs, sizeof(kHddArgs) };
const IrxModule kPs2Fs = { "ps2fs", ps2fs_irx, &size_ps2fs_irx, kPfsArgs, sizeof(kPfsArgs) };
const IrxModule kBdm = { "bdm", bdm_irx, &size_bdm_irx, nullptr, 0 };
const IrxModule kBdmFsFatfs = { "bdmfs_fatfs", bdmfs_fatfs_irx, &size_bdmfs_fatfs_irx, nullptr, 0 };
const IrxModule kUsbd = { "usbd", usbd_irx, &size_usbd_irx, nullptr, 0 };
const IrxModule kUsbmassBd = { "usbmass_bd", usbmass_bd_irx, &size_usbmass_bd_irx, nullptr, 0 };
const IrxModule kPs2Kbd = { "ps2kbd", ps2kbd_irx, &size_ps2kbd_irx, nullptr, 0 };
const IrxModule kLibsd = { "libsd", libsd_irx, &size_libsd_irx, nullptr, 0 };
const IrxModule kAudsrv = { "audsrv", audsrv_irx, &size_audsrv_irx, nullptr, 0 };
// What the modules that load after the boot need of the IOP's memory: sound (libsd, audsrv and its
// buffers), the ROM's SIO2MAN, PADMAN, MCMAN and MCSERV, and the keyboard driver. A boot that kept
// the IOP as launched, with less than this in one block, resets it instead, rather than leave a
// later driver nowhere to go. They took 312 KB in PCSX2 (2026-10-12): a USB boot from a reset IOP
// ended with 1220 KB free in one block, and 908 KB were left once all of them were running, with
// the music playing too. This leaves a margin over that, and resetting when it wasn't needed
// only costs the time a reset takes.
constexpr int kLaterModulesIopBytes = 384 * 1024;
const ModuleIdentity * FindIdentity(const char * label)
{
for (const ModuleIdentity & identity : kModuleIdentities)
{
if (std::strcmp(identity.label, label) == 0)
{
return &identity;
}
}
return nullptr;
}
const ModuleIdentity & IdentityOf(const char * label)
{
const ModuleIdentity * identity = FindIdentity(label);
PS2_AssertMsg(identity != nullptr, "An IOP module missing from kModuleIdentities!");
return (identity != nullptr) ? *identity : kModuleIdentities[0];
}
int VersionMajor(const u16 version) { return version >> 8; }
int VersionMinor(const u16 version) { return version & 0xFF; }
// ------------------------------------------------------------------------------------------------
// What is running on the IOP
// ------------------------------------------------------------------------------------------------
// Module names are kept to this length; the ones the boot looks for are all shorter.
constexpr int kMaxModuleNameLen = 31;
constexpr int kMaxResident = 64;
struct ResidentModule
{
char name[kMaxModuleNameLen + 1]; // "" for a module without an IRX_ID
u16 version; // major << 8 | minor; 0 for one the boot started itself
};
// The IOP's module list as last read, plus each module started since.
static ResidentModule s_resident[kMaxResident];
static int s_numResident = 0;
// Per kModuleIdentities entry, whether its export library is registered (see ModuleIdentity).
static bool s_libraryResident[ArrayLength(kModuleIdentities)] = {};
// Copies a module's name out of IOP memory. The buffer is a SIF DMA target: aligned to, and as
// large as, a cache line, so no neighbour's dirty line can be written back over the transfer.
void ReadModuleName(const smod_mod_info_t & info, char (&name)[kMaxModuleNameLen + 1])
{
constexpr int kNameBufferBytes = 64;
alignas(64) static char s_nameBuffer[kNameBufferBytes];
name[0] = '\0';
if (info.name == nullptr)
{
return;
}
SyncDCache(s_nameBuffer, s_nameBuffer + kNameBufferBytes);
SifRpcReceiveData_t rpcData = {};
if (SifRpcGetOtherData(&rpcData, info.name, s_nameBuffer, kNameBufferBytes, 0) >= 0)
{
std::memcpy(name, s_nameBuffer, kMaxModuleNameLen);
name[kMaxModuleNameLen] = '\0';
}
}
// Reads the IOP's module list, and looks for the export libraries kModuleIdentities names.
void TakeInventory()
{
s_numResident = 0;
smod_mod_info_t info = {};
if (smod_get_next_mod(nullptr, &info) != 0)
{
do
{
if (s_numResident == kMaxResident)
{
break;
}
ResidentModule & module = s_resident[s_numResident++];
ReadModuleName(info, module.name);
module.version = info.version;
} while (smod_get_next_mod(&info, &info) != 0);
}
// slib caches where the IOP keeps its library list: read it afresh, the IOP may have been reset.
const bool haveLibraryList = (slib_exp_lib_list() != nullptr);
for (int i = 0; i < ArrayLength(kModuleIdentities); ++i)
{
const char * const library = kModuleIdentities[i].library;
s_libraryResident[i] = haveLibraryList && (library != nullptr) && (slib_get_exp_lib(library, nullptr) > 0);
}
}
int IdentityIndex(const ModuleIdentity & identity)
{
return static_cast<int>(&identity - kModuleIdentities);
}
const ResidentModule * FindResidentByName(const char * name)
{
for (int i = 0; i < s_numResident; ++i)
{
if (std::strcmp(s_resident[i].name, name) == 0)
{
return &s_resident[i];
}
}
return nullptr;
}
// The running module doing a job by its name, or null - also for one known only by its library.
const ResidentModule * FindResident(const ModuleIdentity & identity)
{
for (const char * name : identity.names)
{
if (name != nullptr)
{
if (const ResidentModule * module = FindResidentByName(name))
{
return module;
}
}
}
return nullptr;
}
bool IsResident(const ModuleIdentity & identity)
{
return s_libraryResident[IdentityIndex(identity)] || (FindResident(identity) != nullptr);
}
// Records a module the boot started, so that a later request for the same job finds it running.
void NoteStarted(const ModuleIdentity & identity)
{
if (identity.library != nullptr)
{
s_libraryResident[IdentityIndex(identity)] = true;
}
if (identity.names[0] != nullptr && s_numResident < kMaxResident)
{
ResidentModule & module = s_resident[s_numResident++];
std::snprintf(module.name, sizeof(module.name), "%s", identity.names[0]);
module.version = 0;
}
}
// "x.y", or a note that the boot started it.
const char * DescribeVersion(const ResidentModule & module, char (&text)[24])
{
if (module.version == 0)
{
std::snprintf(text, sizeof(text), "started by the boot");
}
else
{
std::snprintf(text, sizeof(text), "%d.%d", VersionMajor(module.version), VersionMinor(module.version));
}
return text;
}
// Says which running module a driver that starts after the boot is using instead of its own.
void PrintReused(const char * label, const ModuleIdentity & identity)
{
if (const ResidentModule * running = FindResident(identity))
{
char version[24];
Com_Printf("IOP module '%s': using the %s already running (%s).\n", label, running->name, DescribeVersion(*running, version));
}
else
{
Com_Printf("IOP module '%s': using the one already running (its '%s' library is registered).\n", label, identity.library);
}
}
// ------------------------------------------------------------------------------------------------
// The boot report (see PrintIopBootReport)
// ------------------------------------------------------------------------------------------------
constexpr int kReportBytes = 6144;
static char s_report[kReportBytes];
static int s_reportUsed = 0;
// The modules a bring-up step reused, started and failed to start, for a line each in the report.
static char s_reusedModules[256];
static char s_startedModules[512];
static char s_failedModules[256];
// One line of the report, "IOP boot: " and the text, printed at once as well.
void ReportLine(const char * format, ...) Q_PRINTF_FUNC(1, 2);
void ReportLine(const char * format, ...)
{
char line[640];
va_list args;
va_start(args, format);
std::vsnprintf(line, sizeof(line), format, args);
va_end(args);
std::printf("IOP boot: %s\n", line);
const int room = kReportBytes - s_reportUsed;
if (room > 1)
{
const int written = std::snprintf(&s_report[s_reportUsed], static_cast<size_t>(room), "IOP boot: %s\n", line);
if (written > 0)
{
s_reportUsed += (written < room) ? written : (room - 1);
}
}
}
void ListAppend(char * list, const size_t listBytes, const char * format, ...) Q_PRINTF_FUNC(3, 4);
void ListAppend(char * list, const size_t listBytes, const char * format, ...)
{
const size_t used = std::strlen(list);
if (used + 1 < listBytes)
{
va_list args;
va_start(args, format);
std::vsnprintf(list + used, listBytes - used, format, args);
va_end(args);
}
}
// Lists every module running, a few to a line, and the export libraries kModuleIdentities looks
// for that are registered. On a console the report is the only record of what a launcher left.
void ReportResidentModules()
{
constexpr int kLineChars = 100;
char line[kLineChars + 48] = {};
int used = 0;
for (int i = 0; i < s_numResident; ++i)
{
const ResidentModule & module = s_resident[i];
char entry[48];
const int length = std::snprintf(entry, sizeof(entry), "%s %d.%d", (module.name[0] != '\0') ? module.name : "(unnamed)",
VersionMajor(module.version), VersionMinor(module.version));
if (used > 0 && used + 2 + length > kLineChars)
{
ReportLine("running: %s", line);
used = 0;
}
used += std::snprintf(&line[used], sizeof(line) - static_cast<size_t>(used), "%s%s", (used > 0) ? ", " : "", entry);
}
if (used > 0)
{
ReportLine("running: %s", line);
}
char libraries[160] = {};
for (int i = 0; i < ArrayLength(kModuleIdentities); ++i)
{
if (s_libraryResident[i])
{
ListAppend(libraries, sizeof(libraries), " %s", kModuleIdentities[i].library);
}
}
ReportLine("libraries registered:%s.", (libraries[0] != '\0') ? libraries : " none of those looked for");
}
void ReportModules()
{
if (s_reusedModules[0] != '\0')
{
ReportLine("reused%s.", s_reusedModules);
s_reusedModules[0] = '\0';
}
if (s_startedModules[0] != '\0')
{
ReportLine("started%s.", s_startedModules);
s_startedModules[0] = '\0';
}
if (s_failedModules[0] != '\0')
{
ReportLine("didn't start%s.", s_failedModules);
s_failedModules[0] = '\0';
}
}
// ------------------------------------------------------------------------------------------------
// Game data location
// ------------------------------------------------------------------------------------------------
// The files probed for under "<base>/baseq2/" to decide a base path works: the compressed pack
// the game loads in preference (fs/pkz_pack.h), or id's.
constexpr const char * kProbeFiles[] = { "pak0.pkz", "pak0.pak" };
// PCSX2 builds have differed on whether "host:/" is ELF-relative or host-absolute,
// so the explicitly relative form is tried too.
constexpr const char * kHostBasePaths[] = { "host:", "host:." };
constexpr const char * kHddDevices[] = { "hdd0:", "hdd1:" };
constexpr const char * kHddMountPath = "pfs0:";
constexpr int kMountReadWrite = 0; // FIO_MT_RDWR in io_common.h (not a C++-safe include).
constexpr int kMaxHddPartitionPathLen = static_cast<int>(sizeof("hdd0:") - 1) + APA_IDMAX;
// bdmfs_fatfs serves the FAT/exFAT volumes it mounts as mass0: to mass9: (FatFs's
// FF_VOLUMES), numbered in mount order. Every partition is a volume of its own, and
// usbmass_bd drives two USB drives at once, so the data need not be on mass0:. A bare
// "mass:" is no extra candidate: iomanX reads a missing unit number as 0, so it is mass0:.
constexpr const char * kUsbBasePaths[] = {
"mass0:", "mass1:", "mass2:", "mass3:", "mass4:",
"mass5:", "mass6:", "mass7:", "mass8:", "mass9:",
};
// The engine builds every file path in a MAX_OSPATH buffer as "<base>/baseq2/<name>", where a
// name runs up to MAX_QPATH - 1 chars, so a longer base path would cut some of them short.
// That leaves the ELF's folder whatever a "massN:" prefix doesn't take.
constexpr int kMaxBasePathLen = MAX_OSPATH - static_cast<int>(sizeof("/baseq2/") - 1) - MAX_QPATH;
constexpr int kMaxElfFolderLen = kMaxBasePathLen - static_cast<int>(sizeof("mass0:") - 1);
// The folder on the device the ELF was launched from, "/dir/sub" or empty for the root.
// See ElfFolderFromPath().
static char s_elfFolder[kMaxElfFolderLen + 1];
// The USB base path DetectBasePathAndBootIop() settled on: a massN: unit plus a folder.
static char s_usbBasePath[kMaxBasePathLen + 1];
static char s_hddBasePath[kMaxBasePathLen + 1];
static const char * s_hddSyncDevice = nullptr; // Only the selected, surviving HDD mount.
static const char * s_hddStatus = "not checked";
// What the HDD probe cost: the PFS partitions it mounted, and the time spent in them.
static int s_hddMounts = 0;
static int s_hddMountsMsec = 0;
// Set for an ELF launched from USB, whose boot leaves the HDD alone (see UsbLaunchPath).
static bool s_launchedFromUsb = false;
// How long to wait for the USB drive: enumeration + FAT mount happen
// asynchronously after usbmass_bd starts, and a drive that has to spin up
// takes seconds. The probe returns as soon as the data shows up, so only a
// boot that finds none waits all of it.
constexpr int kUsbWaitTotalMsec = 10000;
constexpr int kUsbWaitStepMsec = 100;
// The same wait through a USB stack the launcher left running. A drive it had mounted is there
// at once; one it hadn't by now isn't going to show up through it, and a boot that finds no
// data falls back to the reset, which gives the game's own drivers the whole wait above.
constexpr int kUsbWaitRunningMsec = 2000;
// fileXio serves a read in pieces the size of its IOP buffer, each one a device read and a DMA
// to the EE, waited out before the next (fileXio_iop.c). Its default of 16 KB makes every PFS
// read reach the drive as 16 KB transfers. 64 KB is the most usbmass_bd asks of a drive in one
// SCSI command (128 sectors). FatFs also ends each read at a cluster's end, so on USB a larger
// buffer only pays on a volume with clusters over 16 KB. Pass -DPS2_QUAKE_FILEXIO_BUFFER_KB=16
// for the stock size, to compare.
#ifndef PS2_QUAKE_FILEXIO_BUFFER_KB
#define PS2_QUAKE_FILEXIO_BUFFER_KB 64
#endif
constexpr int kStockFileXioBufferBytes = 16 * 1024;
constexpr int kFileXioBufferBytes = PS2_QUAKE_FILEXIO_BUFFER_KB * 1024;
static_assert(kFileXioBufferBytes >= kStockFileXioBufferBytes && kFileXioBufferBytes <= 256 * 1024,
"PS2_QUAKE_FILEXIO_BUFFER_KB must be 16 to 256");
// See FileXioBufferBytes().
static int s_fileXioBufferBytes = 0;
bool CanOpen(const char * path)
{
std::FILE * file = std::fopen(path, "rb");
if (file != nullptr)
{
std::fclose(file);
return true;
}
return false;
}
// Probes exactly the path the Quake filesystem will build from the base ("<base>/baseq2/...").
bool HasGameData(const char * basePath)
{
for (const char * probeFile : kProbeFiles)
{
char probePath[MAX_OSPATH];
std::snprintf(probePath, sizeof(probePath), "%s/baseq2/%s", basePath, probeFile);
if (CanOpen(probePath))
{
return true;
}
}
return false;
}
// Retain the launch partition before the IOP reset discards the loader's PFS mount.
// Both the ELF-loader form (hdd0:+Q2:pfs:/dir/quake2.elf) and the file-browser
// form (hdd0:/+Q2/dir/quake2.elf) identify it. A bare pfsN: path cannot, so the
// HDD search below falls back to enumerating partitions in that case.
bool HddPartitionFromPath(const char * elfPath, char (&partition)[kMaxHddPartitionPathLen + 1])
{
partition[0] = '\0';
if (elfPath == nullptr ||
(std::strncmp(elfPath, "hdd0:", 5) != 0 && std::strncmp(elfPath, "hdd1:", 5) != 0))
{
return false;
}
const char * start = elfPath + 5;
while (*start == '/' || *start == '\\') { ++start; }
const char * end = start;
while (*end != '\0' && *end != ':' && *end != '/' && *end != '\\') { ++end; }
const int length = static_cast<int>(end - start);
if (length == 0 || length > APA_IDMAX || *end == '\0')
{
return false;
}
if (*end == ':')
{
const char * fileDevice = end + 1;
if (std::strncmp(fileDevice, "pfs:", 4) != 0 &&
!(std::strncmp(fileDevice, "pfs", 3) == 0 && fileDevice[3] >= '0' &&
fileDevice[3] <= '9' && fileDevice[4] == ':'))
{
return false;
}
}
std::memcpy(partition, elfPath, 5);
std::memcpy(partition + 5, start, static_cast<size_t>(length));
partition[5 + length] = '\0';
return true;
}
// The folder the loader launched the ELF from, so the game data can sit next to it rather than
// only at the root of the drive. argv[0] holds the ELF's path, but each loader spells the device
// its own way ("mass:/APPS/Q2/quake2.elf", "mass0:APPS/Q2/quake2.elf", "hdd0:__common:pfs:/...")
// and the IOP reset renumbers the drives anyway, so only what follows the last colon is kept,
// and every massN: unit gets tried with it. Writes "/APPS/Q2", whatever separators the loader
// used, or an empty string for the root or a null path. Returns false, writing an empty
// string, when the folder is too long for the engine's paths.
bool ElfFolderFromPath(const char * elfPath, char (&folder)[kMaxElfFolderLen + 1])
{
folder[0] = '\0';
if (elfPath == nullptr)
{
return true;
}
const char * path = elfPath;
for (const char * p = elfPath; *p != '\0'; ++p)
{
if (*p == ':')
{
path = p + 1;
}
}
// The browser form puts the APA partition in the first path component; it is
// not part of the directory inside PFS. Canonical HDD paths were handled by
// the last-colon rule above, as were massN: and bare pfsN: launch paths.
if ((std::strncmp(elfPath, "hdd0:", 5) == 0 || std::strncmp(elfPath, "hdd1:", 5) == 0) &&
std::strchr(elfPath + 5, ':') == nullptr)
{
path = elfPath + 5;
while (*path == '/' || *path == '\\') { ++path; }
while (*path != '\0' && *path != '/' && *path != '\\') { ++path; }
}
// The folder ends at the last separator; without one the ELF sits at the root.
const char * folderEnd = path;
for (const char * p = path; *p != '\0'; ++p)
{
if (*p == '/' || *p == '\\')
{
folderEnd = p;
}
}
// Copied one name at a time, which drops leading, doubled and trailing separators.
int length = 0;
for (const char * p = path; p < folderEnd;)
{
if (*p == '/' || *p == '\\')
{
++p;
continue;
}
const char * nameEnd = p;
while (nameEnd < folderEnd && *nameEnd != '/' && *nameEnd != '\\')
{
++nameEnd;
}
const int nameLength = static_cast<int>(nameEnd - p);
if (length + 1 + nameLength > kMaxElfFolderLen)
{
folder[0] = '\0';
return false;
}
folder[length++] = '/';
std::memcpy(&folder[length], p, static_cast<size_t>(nameLength));
length += nameLength;
p = nameEnd;
}
folder[length] = '\0';
return true;
}
// Whether the loader launched the ELF from USB: "mass:" or "mass0:" to "mass9:". Such a boot looks
// for the game data on USB only. Searching the HDD first cost a console with its HDD attached
// (2026-10-12, wLaunchELF 4.43a) 24.6 s of mounting PFS partitions, plus 3.4 s to start ps2hdd
// when the launcher hadn't, before the USB drive was even looked at.
bool UsbLaunchPath(const char * elfPath)
{
if (elfPath == nullptr || std::strncmp(elfPath, "mass", 4) != 0)
{
return false;
}
const char next = elfPath[4];
return (next == ':') || (next >= '0' && next <= '9' && elfPath[5] == ':');
}
// Tries a folder ("" for the root) on every USB volume. Returns the base path that has the
// game data, or null.
const char * FindUsbGameData(const char * folder)
{
for (const char * unit : kUsbBasePaths)
{
std::snprintf(s_usbBasePath, sizeof(s_usbBasePath), "%s%s", unit, folder);
if (HasGameData(s_usbBasePath))
{
return s_usbBasePath;
}
}
return nullptr;
}
// Whether a FAT volume is mounted behind a massN: unit: only then does its root directory open.
bool IsUsbVolumeMounted(const char * basePath)
{
char rootPath[32];
std::snprintf(rootPath, sizeof(rootPath), "%s/", basePath);
DIR * dir = opendir(rootPath);
if (dir == nullptr)
{
return false;
}
closedir(dir);
return true;
}
// ------------------------------------------------------------------------------------------------
// Starting modules
// ------------------------------------------------------------------------------------------------
// Set once SIF RPC is up and modules can be loaded out of an EE buffer.
// See EnsureModuleLoaderReady().
static bool s_moduleLoaderReady = false;
// Set while no module loaded out of an EE buffer has been seen running yet, when the patch below
// couldn't say the loader is up to it. A LOADFILE without the RPC answers the call with whatever
// its reply buffer held, which reads as a module started.
static bool s_verifyNextLoad = false;
// See FileIoRemovePatched().
static bool s_fileIoRemovePatched = false;
// The ROM modules LoadRomModuleOnce has been asked for, and how loading them went.
struct RomModule
{
const char * path;
bool loaded;
};
constexpr int kMaxRomModules = 8;
static RomModule s_romModules[kMaxRomModules];
static int s_numRomModules = 0;
// How each driver that starts after the boot came up. See StartOnce().
enum class DriverStart
{
NotTried,
Up,
Failed,
};
static DriverStart s_padDriver = DriverStart::NotTried;
static DriverStart s_cardDriver = DriverStart::NotTried;
static DriverStart s_soundDriver = DriverStart::NotTried;
static DriverStart s_keyboardDriver = DriverStart::NotTried;
// What the last module that wouldn't start answered, for the error that halts the boot.
static int s_lastFailedId = 0;
static int s_lastFailedResult = 0;
// Lets SifExecModuleBuffer load a module out of EE memory. The boot ROM's LOADFILE (1.x) has no
// RPC for that and sbv patches one in; LOADFILE 2.x, which LoadExecPS2 launches leave running,
// has its own. sbv answers 1 both for that one and for a 1.x patched already (by the launcher,
// or earlier in this boot), and also for a LOADFILE it doesn't recognise. So unless it patched
// one just now, the next module loaded is checked to be running.
bool PatchModuleLoader()
{
SifInitRpc(0);
const int lmbPatch = sbv_patch_enable_lmb();
if (lmbPatch < 0 || sbv_patch_disable_prefix_check() != 0)
{
return false;
}
s_verifyNextLoad = (lmbPatch != 0);
s_moduleLoaderReady = true;
return true;
}
// Prerequisites for SifExecModuleBuffer, for drivers started after boot: SIF RPC up, and the
// patches above. Console boots have these already; only the host: fast path reaches here
// without them, once, however many drivers call in.
bool EnsureModuleLoaderReady()
{
if (s_moduleLoaderReady)
{
return true;
}
if (!PatchModuleLoader())
{
Com_Printf("WARNING: the IOP module loader can't be patched - no IOP module can be loaded!\n");
return false;
}
return true;
}
// Loads an IRX out of EE memory and starts it. False when it isn't running afterwards: a negative
// id is a load that failed, and result 1 (NO_RESIDENT_END) a module whose _start bailed out; some
// drivers return a negative init error too. 'identity' may be null for a module the boot doesn't
// track, which goes unrecorded.
bool ExecModuleBuffer(const ModuleIdentity * identity, void * image, const u32 sizeBytes,
const u32 argsBytes, const char * args, int & id, int & moduleResult)
{
moduleResult = 0;
id = SifExecModuleBuffer(image, sizeBytes, argsBytes, args, &moduleResult);
if (id < 0 || moduleResult < 0 || moduleResult == 1)
{
return false;
}
if (s_verifyNextLoad && identity != nullptr)
{
TakeInventory();
if (!IsResident(*identity))
{
return false;
}
s_verifyNextLoad = false;
}
if (identity != nullptr)
{
NoteStarted(*identity);
}
return true;
}
enum class ModuleStart
{
Reused,
Started,
Failed,
};
// One module of the boot chain: the running one doing its job, or this one, started now.
ModuleStart StartModule(const IrxModule & module)
{
const ModuleIdentity & identity = IdentityOf(module.label);
if (IsResident(identity))
{
ListAppend(s_reusedModules, sizeof(s_reusedModules), " %s", module.label);
return ModuleStart::Reused;
}
const int startMsec = Sys_Milliseconds();
int id = 0;
int moduleResult = 0;
if (!ExecModuleBuffer(&identity, module.image, *module.sizeBytes, module.argsBytes, module.args, id, moduleResult))
{
std::printf("IOP boot: module '%s' didn't start (id %d, result %d).\n", module.label, id, moduleResult);
ListAppend(s_failedModules, sizeof(s_failedModules), " %s (id %d, result %d)", module.label, id, moduleResult);
s_lastFailedId = id;
s_lastFailedResult = moduleResult;
return ModuleStart::Failed;
}
const int msec = Sys_Milliseconds() - startMsec;
std::printf("IOP boot: started '%s' (id %d) in %d ms.\n", module.label, id, msec);
ListAppend(s_startedModules, sizeof(s_startedModules), " %s %d ms", module.label, msec);
return ModuleStart::Started;
}
// A module the boot can't go on without. After a reset there is nothing left to fall back on,
// and it halts the boot, as boots always did; before one it says what failed, for the reset.
ModuleStart StartRequired(const IrxModule & module, const bool afterReset, const char ** failure)
{
const ModuleStart start = StartModule(module);
if (start != ModuleStart::Failed)
{
return start;
}
if (afterReset)
{
Sys_Error("IOP boot: module '%s' failed (id %d, result %d)", module.label, s_lastFailedId, s_lastFailedResult);
}
static char s_failure[64];
std::snprintf(s_failure, sizeof(s_failure), "'%s' wouldn't start", module.label);
*failure = s_failure;
return ModuleStart::Failed;
}
// Why the game's drivers can't run alongside what is running, or null when they can.
const char * FindResidentConflict()
{
static char s_reason[160];
// A module too old to talk its EE library's protocol can't be swapped for the game's own
// either, which would find its RPC server or name taken.
for (const ModuleIdentity & identity : kModuleIdentities)
{
const ResidentModule * module = FindResident(identity);
if (module != nullptr && identity.minVersion != 0 && module->version < identity.minVersion)
{
std::snprintf(s_reason, sizeof(s_reason), "a resident %s %d.%d, older than the %d.%d the game talks to",
module->name, VersionMajor(module->version), VersionMinor(module->version),
VersionMajor(identity.minVersion), VersionMinor(identity.minVersion));
return s_reason;
}
}
// The ROM's PADMAN and MCMAN are built against the ROM's SIO2MAN, version 1, so next to an
// X-module or ps2sdk SIO2MAN they wouldn't link. That one can stay only with every pad and
// card driver the game would otherwise load on top of it.
const ResidentModule * sio2man = FindResident(IdentityOf("rom0:SIO2MAN"));
if (sio2man != nullptr && VersionMajor(sio2man->version) != 1 &&
(!IsResident(IdentityOf("rom0:PADMAN")) || !IsResident(IdentityOf("rom0:MCMAN")) ||
!IsResident(IdentityOf("rom0:MCSERV"))))
{
std::snprintf(s_reason, sizeof(s_reason), "a resident sio2man %d.%d without all of its pad and card drivers",
VersionMajor(sio2man->version), VersionMinor(sio2man->version));
return s_reason;
}
return nullptr;
}
// See LargestFreeIopBlockBytes().
int LargestFreeIopBlock()
{
if (SifInitIopHeap() < 0)
{
return 0;
}
constexpr int kIopRamBytes = 2 * 1024 * 1024;
constexpr int kPrecisionBytes = 4 * 1024;
int fits = 0;
int fails = kIopRamBytes;
while (fails - fits > kPrecisionBytes)
{
const int size = ((fits + fails) / 2) & ~255;
void * block = SifAllocIopHeap(size);
if (block != nullptr)
{
SifFreeIopHeap(block);
fits = size;
}
else
{
fails = size;
}
}
return fits;
}
// The boot of old: a fresh IOP with the boot ROM's modules only. Nothing started on the one
// before - by the launcher, or by this boot - survives it.
void ResetIop(const char * reason)
{
ReportLine("resetting the IOP: %s.", reason);
const int startMsec = Sys_Milliseconds();
SifInitRpc(0);
while (!SifIopReset("", 0)) {}
while (!SifIopSync()) {}
SifInitRpc(0);
if (!PatchModuleLoader())
{
Sys_Error("IOP boot: module-loader patches failed");
return; // unreachable; Sys_Error halts
}
// Only the pad driver can have started by now (when the launcher left its PADMAN), but none
// of them is running any more.
TakeInventory();
s_numRomModules = 0;
s_padDriver = DriverStart::NotTried;
s_cardDriver = DriverStart::NotTried;
s_soundDriver = DriverStart::NotTried;
s_keyboardDriver = DriverStart::NotTried;
s_hddSyncDevice = nullptr;
ReportLine("the reset took %d ms, leaving %d modules.", Sys_Milliseconds() - startMsec, s_numResident);
}
// ------------------------------------------------------------------------------------------------
// The drivers that start after the boot
// ------------------------------------------------------------------------------------------------
// Loads a module from the console's ROM the first time any driver asks for it, and returns that
// first result to every later caller: the pad and memory card drivers share the ROM's SIO2MAN,
// and a second copy of it must not be loaded. A module doing the same job that is already
// running counts as loaded: a launcher may leave its own serial, pad or memory card drivers,
// which libpad and libmc can talk to as well.