forked from RPCS3/rpcs3
/
System.cpp
2988 lines (2454 loc) · 76 KB
/
System.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 "stdafx.h"
#include "VFS.h"
#include "Utilities/bin_patch.h"
#include "Emu/Memory/vm.h"
#include "Emu/System.h"
#include "Emu/system_progress.hpp"
#include "Emu/system_utils.hpp"
#include "Emu/perf_meter.hpp"
#include "Emu/perf_monitor.hpp"
#include "Emu/vfs_config.h"
#include "Emu/IPC_config.h"
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/PPUThread.h"
#include "Emu/Cell/PPUDisAsm.h"
#include "Emu/Cell/PPUAnalyser.h"
#include "Emu/Cell/SPUThread.h"
#include "Emu/RSX/RSXThread.h"
#include "Emu/Cell/lv2/sys_process.h"
#include "Emu/Cell/lv2/sys_sync.h"
#include "Emu/Cell/lv2/sys_prx.h"
#include "Emu/Cell/lv2/sys_overlay.h"
#include "Emu/Cell/lv2/sys_spu.h"
#include "Emu/Cell/Modules/cellGame.h"
#include "Emu/title.h"
#include "Emu/IdManager.h"
#include "Emu/RSX/Capture/rsx_replay.h"
#include "Loader/PSF.h"
#include "Loader/TAR.h"
#include "Loader/ELF.h"
#include "Loader/disc.h"
#include "Utilities/StrUtil.h"
#include "../Crypto/unself.h"
#include "util/yaml.hpp"
#include "util/logs.hpp"
#include "util/serialization.hpp"
#include <fstream>
#include <memory>
#include <regex>
#include <optional>
#include "Utilities/JIT.h"
#include "display_sleep_control.h"
#include "Emu/IPC_socket.h"
#if defined(HAVE_VULKAN)
#include "Emu/RSX/VK/VulkanAPI.h"
#endif
LOG_CHANNEL(sys_log, "SYS");
// Preallocate 32 MiB
stx::manual_typemap<void, 0x20'00000, 128> g_fixed_typemap;
bool g_log_all_errors = false;
bool g_use_rtm = false;
u64 g_rtm_tx_limit1 = 0;
u64 g_rtm_tx_limit2 = 0;
std::string g_cfg_defaults;
atomic_t<u64> g_watchdog_hold_ctr{0};
extern bool ppu_load_exec(const ppu_exec_object&, utils::serial* = nullptr);
extern void spu_load_exec(const spu_exec_object&);
extern void spu_load_rel_exec(const spu_rel_object&);
extern void ppu_precompile(std::vector<std::string>& dir_queue, std::vector<ppu_module*>* loaded_prx);
extern bool ppu_initialize(const ppu_module&, bool = false);
extern void ppu_finalize(const ppu_module&);
extern void ppu_unload_prx(const lv2_prx&);
extern std::shared_ptr<lv2_prx> ppu_load_prx(const ppu_prx_object&, const std::string&, s64 = 0, utils::serial* = nullptr);
extern std::pair<std::shared_ptr<lv2_overlay>, CellError> ppu_load_overlay(const ppu_exec_object&, const std::string& path, s64 = 0, utils::serial* = nullptr);
extern bool ppu_load_rel_exec(const ppu_rel_object&);
extern bool is_savestate_version_compatible(const std::vector<std::pair<u16, u16>>& data, bool is_boot_check);
extern std::vector<std::pair<u16, u16>> read_used_savestate_versions();
std::string get_savestate_path(std::string_view title_id, std::string_view boot_path);
fs::file g_tty;
atomic_t<s64> g_tty_size{0};
std::array<std::deque<std::string>, 16> g_tty_input;
std::mutex g_tty_mutex;
thread_local std::string_view g_tls_serialize_name;
extern thread_local std::string(*g_tls_log_prefix)();
// Report error and call std::abort(), defined in main.cpp
[[noreturn]] void report_fatal_error(std::string_view);
void initialize_timebased_time(u64 timebased_init, bool reset = false);
namespace atomic_wait
{
extern void parse_hashtable(bool(*cb)(u64 id, u32 refs, u64 ptr, u32 max_coll));
}
template<>
void fmt_class_string<game_boot_result>::format(std::string& out, u64 arg)
{
format_enum(out, arg, [](game_boot_result value)
{
switch (value)
{
case game_boot_result::no_errors: return "No errors";
case game_boot_result::generic_error: return "Generic error";
case game_boot_result::nothing_to_boot: return "Nothing to boot";
case game_boot_result::wrong_disc_location: return "Wrong disc location";
case game_boot_result::invalid_file_or_folder: return "Invalid file or folder";
case game_boot_result::invalid_bdvd_folder: return "Invalid dev_bdvd folder";
case game_boot_result::install_failed: return "Game install failed";
case game_boot_result::decryption_error: return "Failed to decrypt content";
case game_boot_result::file_creation_error: return "Could not create important files";
case game_boot_result::firmware_missing: return "Firmware is missing";
case game_boot_result::unsupported_disc_type: return "This disc type is not supported yet";
case game_boot_result::savestate_corrupted: return "Savestate data is corrupted or it's not an RPCS3 savestate";
case game_boot_result::savestate_version_unsupported: return "Savestate versioning data differes from your RPCS3 build";
}
return unknown;
});
}
template<>
void fmt_class_string<cfg_mode>::format(std::string& out, u64 arg)
{
format_enum(out, arg, [](cfg_mode value)
{
switch (value)
{
case cfg_mode::custom: return "custom config";
case cfg_mode::custom_selection: return "custom config selection";
case cfg_mode::global: return "global config";
case cfg_mode::config_override: return "config override";
case cfg_mode::continuous: return "continuous config";
case cfg_mode::default_config: return "default config";
}
return unknown;
});
}
void Emulator::CallFromMainThread(std::function<void()>&& func, atomic_t<bool>* wake_up, bool track_emu_state, u64 stop_ctr) const
{
if (!track_emu_state)
{
m_cb.call_from_main_thread(std::move(func), wake_up);
return;
}
std::function<void()> final_func = [this, before = IsStopped(), count = (stop_ctr == umax ? +m_stop_ctr : stop_ctr), func = std::move(func)]
{
if (count == m_stop_ctr && before == IsStopped())
{
func();
}
};
m_cb.call_from_main_thread(std::move(final_func), wake_up);
}
void Emulator::BlockingCallFromMainThread(std::function<void()>&& func) const
{
atomic_t<bool> wake_up = false;
CallFromMainThread(std::move(func), &wake_up);
while (!wake_up && !IsStopped())
{
ensure(thread_ctrl::get_current());
thread_ctrl::wait_on(wake_up, false);
}
}
// This function ensures constant initialization order between different compilers and builds
void init_fxo_for_exec(utils::serial* ar, bool full = false)
{
g_fxo->init<ppu_module>();
void init_ppu_functions(utils::serial* ar, bool full);
if (full)
{
init_ppu_functions(ar, true);
}
Emu.ConfigurePPUCache();
g_fxo->init(false, ar);
Emu.GetCallbacks().init_gs_render(ar);
Emu.GetCallbacks().init_pad_handler(Emu.GetTitleID());
Emu.GetCallbacks().init_kb_handler();
Emu.GetCallbacks().init_mouse_handler();
if (ar)
{
Emu.ExecDeserializationRemnants();
ar->pos += 32; // Reserved area
}
}
void Emulator::Init(bool add_only)
{
jit_runtime::initialize();
if (!g_tty)
{
const auto tty_path = fs::get_cache_dir() + "TTY.log";
g_tty.open(tty_path, fs::rewrite + fs::append);
if (!g_tty)
{
sys_log.fatal("Failed to create TTY log: %s (%s)", tty_path, fs::g_tls_error);
}
}
g_fxo->reset();
// Reset defaults, cache them
g_cfg_vfs.from_default();
g_cfg.from_default();
g_cfg.name.clear();
// Not all renderers are known at compile time, so set a provided default if possible
if (m_default_renderer == video_renderer::vulkan && !m_default_graphics_adapter.empty())
{
g_cfg.video.renderer.set(m_default_renderer);
g_cfg.video.vk.adapter.from_string(m_default_graphics_adapter);
}
g_cfg_defaults = g_cfg.to_string();
// Load VFS config
g_cfg_vfs.load();
sys_log.notice("Using VFS config:\n%s", g_cfg_vfs.to_string());
// Mount all devices
const std::string emu_dir = rpcs3::utils::get_emu_dir();
const std::string elf_dir = fs::get_parent_dir(m_path);
const std::string dev_bdvd = g_cfg_vfs.get(g_cfg_vfs.dev_bdvd, emu_dir); // Only used for make_path
const std::string dev_hdd0 = g_cfg_vfs.get(g_cfg_vfs.dev_hdd0, emu_dir);
const std::string dev_hdd1 = g_cfg_vfs.get(g_cfg_vfs.dev_hdd1, emu_dir);
const std::string dev_flsh = g_cfg_vfs.get_dev_flash();
const std::string dev_flsh2 = g_cfg_vfs.get_dev_flash2();
const std::string dev_flsh3 = g_cfg_vfs.get_dev_flash3();
vfs::mount("/dev_hdd0", dev_hdd0);
vfs::mount("/dev_flash", dev_flsh);
vfs::mount("/dev_flash2", dev_flsh2);
vfs::mount("/dev_flash3", dev_flsh3);
vfs::mount("/app_home", g_cfg_vfs.app_home.to_string().empty() ? elf_dir + '/' : g_cfg_vfs.get(g_cfg_vfs.app_home, emu_dir));
std::string dev_usb;
for (const auto& [key, value] : g_cfg_vfs.dev_usb.get_map())
{
const cfg::device_info usb_info = g_cfg_vfs.get_device(g_cfg_vfs.dev_usb, key, emu_dir);
if (key.size() != 11 || !key.starts_with("/dev_usb00"sv) || key.back() < '0' || key.back() > '7')
{
sys_log.error("Trying to mount unsupported usb device: %s", key);
continue;
}
vfs::mount(key, usb_info.path);
if (key == "/dev_usb000"sv)
{
dev_usb = usb_info.path;
}
}
ensure(!dev_usb.empty());
if (!hdd1.empty())
{
vfs::mount("/dev_hdd1", hdd1);
sys_log.notice("Hdd1: %s", vfs::get("/dev_hdd1"));
}
const bool is_exitspawn = m_config_mode == cfg_mode::continuous;
// Load config file
if (m_config_mode == cfg_mode::config_override)
{
if (const fs::file cfg_file{m_config_path, fs::read + fs::create})
{
sys_log.notice("Applying config override: %s", m_config_path);
if (!g_cfg.from_string(cfg_file.to_string()))
{
sys_log.fatal("Failed to apply config: %s. Proceeding with regular configuration.", m_config_path);
m_config_path.clear();
m_config_mode = cfg_mode::custom;
}
else
{
sys_log.success("Applied config override: %s", m_config_path);
g_cfg.name = m_config_path;
}
}
else
{
sys_log.fatal("Failed to access config: %s (%s). Proceeding with regular configuration.", m_config_path, fs::g_tls_error);
m_config_path.clear();
m_config_mode = cfg_mode::custom;
}
}
// Reload global configuration
if (m_config_mode != cfg_mode::config_override && m_config_mode != cfg_mode::default_config)
{
const auto cfg_path = fs::get_config_dir() + "/config.yml";
if (const fs::file cfg_file{cfg_path, fs::read + fs::create})
{
sys_log.notice("Applying global config: %s", cfg_path);
if (!g_cfg.from_string(cfg_file.to_string()))
{
sys_log.fatal("Failed to apply global config: %s", cfg_path);
}
g_cfg.name = cfg_path;
}
else
{
sys_log.fatal("Failed to access global config: %s (%s)", cfg_path, fs::g_tls_error);
}
}
// Create directories (can be disabled if necessary)
auto make_path_verbose = [](const std::string& path)
{
if (!fs::create_path(path))
{
sys_log.fatal("Failed to create path: %s (%s)", path, fs::g_tls_error);
}
};
const std::string save_path = dev_hdd0 + "home/" + m_usr + "/savedata/";
const std::string user_path = dev_hdd0 + "home/" + m_usr + "/localusername";
if (g_cfg.vfs.init_dirs)
{
make_path_verbose(dev_bdvd);
make_path_verbose(dev_hdd0);
make_path_verbose(dev_hdd1);
make_path_verbose(dev_flsh);
make_path_verbose(dev_flsh2);
make_path_verbose(dev_flsh3);
make_path_verbose(dev_usb);
make_path_verbose(dev_hdd0 + "game/");
make_path_verbose(dev_hdd0 + reinterpret_cast<const char*>(u8"game/$locks/"));
make_path_verbose(dev_hdd0 + "home/");
make_path_verbose(dev_hdd0 + "home/" + m_usr + "/");
make_path_verbose(dev_hdd0 + "home/" + m_usr + "/exdata/");
make_path_verbose(save_path);
make_path_verbose(dev_hdd0 + "home/" + m_usr + "/trophy/");
if (!fs::write_file(user_path, fs::create + fs::excl + fs::write, "User"s))
{
if (fs::g_tls_error != fs::error::exist)
{
sys_log.fatal("Failed to create file: %s (%s)", user_path, fs::g_tls_error);
}
}
make_path_verbose(dev_hdd0 + "disc/");
make_path_verbose(dev_hdd0 + "savedata/");
make_path_verbose(dev_hdd0 + "savedata/vmc/");
make_path_verbose(dev_hdd0 + "photo/");
make_path_verbose(dev_hdd1 + "caches/");
}
make_path_verbose(fs::get_cache_dir() + "shaderlog/");
make_path_verbose(fs::get_cache_dir() + "spu_progs/");
make_path_verbose(fs::get_cache_dir() + "/savestates/");
make_path_verbose(fs::get_config_dir() + "captures/");
make_path_verbose(fs::get_config_dir() + "sounds/");
make_path_verbose(patch_engine::get_patches_path());
if (add_only)
{
// We don't need to initialize the rest if we only add games
return;
}
// Log user
if (m_usr.empty())
{
sys_log.fatal("No user configured");
}
else
{
std::string username;
if (const fs::file file = fs::file(user_path))
{
if (const std::string localusername = file.to_string(); !localusername.empty())
{
username = localusername;
}
else
{
sys_log.warning("Empty username in file: '%s'. Consider setting a username for user '%s' in the user manager.", user_path, m_usr);
}
}
else
{
sys_log.error("Could not read file: '%s'", user_path);
}
if (username.empty())
{
sys_log.notice("Logged in as user '%s'", m_usr);
}
else
{
sys_log.notice("Logged in as user '%s' with the username '%s'", m_usr, username);
}
}
if (is_exitspawn)
{
// Actions not taken during exitspawn
return;
}
// Fixup savedata
for (const auto& entry : fs::dir(save_path))
{
if (entry.is_directory && entry.name.starts_with(".backup_"))
{
const std::string desired = entry.name.substr(8);
const std::string pending = save_path + ".working_" + desired;
if (fs::is_dir(pending))
{
// Finalize interrupted saving
if (!fs::rename(pending, save_path + desired, false))
{
sys_log.fatal("Failed to fix save data: %s (%s)", pending, fs::g_tls_error);
continue;
}
sys_log.success("Fixed save data: %s", desired);
}
// Remove pending backup data
if (!fs::remove_all(save_path + entry.name))
{
sys_log.fatal("Failed to remove save data backup: %s%s (%s)", save_path, entry.name, fs::g_tls_error);
}
else
{
sys_log.success("Removed save data backup: %s%s", save_path, entry.name);
}
}
}
// Limit cache size
if (g_cfg.vfs.limit_cache_size)
{
rpcs3::cache::limit_cache_size();
}
// Wipe clean VSH's temporary directory of choice
if (g_cfg.vfs.empty_hdd0_tmp && !fs::remove_all(dev_hdd0 + "tmp/", false, true))
{
sys_log.error("Could not clean /dev_hdd0/tmp/ (%s)", fs::g_tls_error);
}
// Remove temporary game data that would have been removed when cellGame has been properly shut
for (const auto& entry : fs::dir(dev_hdd0 + "game/"))
{
if (entry.name.starts_with("_GDATA_") && fs::is_dir(dev_hdd0 + "game/" + entry.name + "/USRDIR/"))
{
const std::string target = dev_hdd0 + "game/" + entry.name;
if (!fs::remove_all(target, true, true))
{
sys_log.error("Could not clean \"%s\" (%s)", target, fs::g_tls_error);
}
}
}
// Load IPC config
g_cfg_ipc.load();
sys_log.notice("Using IPC config:\n%s", g_cfg_ipc.to_string());
g_fxo->get<IPC_socket::IPC_server_manager>().set_server_enabled(g_cfg_ipc.get_server_enabled());
}
void Emulator::SetUsr(const std::string& user)
{
sys_log.notice("Setting user ID '%s'", user);
const u32 id = rpcs3::utils::check_user(user);
if (id == 0)
{
fmt::throw_exception("Failed to set user ID '%s'", user);
}
m_usrid = id;
m_usr = user;
}
std::string Emulator::GetBackgroundPicturePath() const
{
// Try to find a custom icon first
std::string path = fs::get_config_dir() + "/Icons/game_icons/" + GetTitleID() + "/PIC1.PNG";
if (fs::is_file(path))
{
return path;
}
path = m_sfo_dir + "/PIC1.PNG";
if (!fs::is_file(path))
{
const std::string disc_dir = vfs::get("/dev_bdvd/PS3_GAME");
if (disc_dir.empty())
{
// Fallback to ICON0.PNG
path = m_sfo_dir + "/ICON0.PNG";
}
else
{
// Fallback to PIC1.PNG in disc dir
path = disc_dir + "/PIC1.PNG";
if (!fs::is_file(path))
{
// Fallback to ICON0.PNG in update dir
path = m_sfo_dir + "/ICON0.PNG";
if (!fs::is_file(path))
{
// Fallback to ICON0.PNG in disc dir
path = disc_dir + "/ICON0.PNG";
}
}
}
}
return path;
}
bool Emulator::BootRsxCapture(const std::string& path)
{
fs::file in_file(path);
if (!in_file)
{
return false;
}
std::unique_ptr<rsx::frame_capture_data> frame = std::make_unique<rsx::frame_capture_data>();
utils::serial load;
load.set_reading_state();
in_file.read(load.data, in_file.size());
load.data.shrink_to_fit();
load(*frame);
in_file.close();
if (frame->magic != rsx::c_fc_magic)
{
sys_log.error("Invalid rsx capture file!");
return false;
}
if (frame->version != rsx::c_fc_version)
{
sys_log.error("Rsx capture file version not supported! Expected %d, found %d", +rsx::c_fc_version, frame->version);
return false;
}
if (frame->LE_format != u32{std::endian::little == std::endian::native})
{
static constexpr std::string_view machines[2]{"Big-Endian", "Little-Endian"};
sys_log.error("Rsx capture byte endianness not supported! Expected %s format, found %s format"
, machines[frame->LE_format ^ 1], machines[frame->LE_format]);
return false;
}
Init();
g_cfg.video.disable_on_disk_shader_cache.set(true);
vm::init();
g_fxo->init(false);
// Initialize progress dialog
g_fxo->init<named_thread<progress_dialog_server>>();
// Initialize performance monitor
g_fxo->init<named_thread<perf_monitor>>();
// PS3 'executable'
m_state = system_state::ready;
GetCallbacks().on_ready();
GetCallbacks().init_gs_render(nullptr);
GetCallbacks().init_pad_handler("");
GetCallbacks().on_run(false);
m_state = system_state::starting;
ensure(g_fxo->init<named_thread<rsx::rsx_replay_thread>>("RSX Replay", std::move(frame)));
return true;
}
game_boot_result Emulator::GetElfPathFromDir(std::string& elf_path, const std::string& path)
{
if (!fs::is_dir(path))
{
return game_boot_result::invalid_file_or_folder;
}
static const char* boot_list[] =
{
"/EBOOT.BIN",
"/USRDIR/EBOOT.BIN",
"/USRDIR/ISO.BIN.EDAT",
"/PS3_GAME/USRDIR/EBOOT.BIN",
};
for (std::string elf : boot_list)
{
elf = path + elf;
if (fs::is_file(elf))
{
elf_path = elf;
return game_boot_result::no_errors;
}
}
return game_boot_result::invalid_file_or_folder;
}
game_boot_result Emulator::BootGame(const std::string& path, const std::string& title_id, bool direct, bool add_only, cfg_mode config_mode, const std::string& config_path)
{
if (!fs::exists(path))
{
return game_boot_result::invalid_file_or_folder;
}
m_path_old = m_path;
m_config_mode = config_mode;
m_config_path = config_path;
if (fs::file save{path, fs::isfile + fs::read}; save && save.size() >= 8 && save.read<u64>() == "RPCS3SAV"_u64)
{
m_ar = std::make_shared<utils::serial>();
m_ar->set_reading_state();
save.seek(0);
save.read(m_ar->data, save.size());
m_ar->data.shrink_to_fit();
}
if (direct || m_ar || fs::is_file(path))
{
m_path = path;
auto error = Load(title_id, add_only);
if (g_cfg.savestate.suspend_emu)
{
for (std::string old_path : std::initializer_list<std::string>{m_ar ? path : "", m_title_id.empty() ? "" : get_savestate_path(m_title_id, path)})
{
if (old_path.empty())
{
continue;
}
old_path = old_path.substr(0, old_path.find_last_not_of(fs::delim) + 1);
const usz insert_pos = old_path.find_last_of(fs::delim) + 1;
const auto prefix = "used_"sv;
if (old_path.compare(insert_pos, prefix.size(), prefix) != 0)
{
old_path.insert(insert_pos, prefix);
if (fs::rename(path, old_path, true))
{
sys_log.notice("Savestate has been moved to path='%s'", old_path);
}
}
}
}
return error;
}
game_boot_result result = game_boot_result::nothing_to_boot;
std::string elf;
if (const game_boot_result res = GetElfPathFromDir(elf, path); res == game_boot_result::no_errors)
{
ensure(!elf.empty());
m_path = elf;
result = Load(title_id, add_only);
}
if (add_only)
{
for (auto&& entry : fs::dir{ path })
{
if (entry.name == "." || entry.name == "..")
{
continue;
}
if (entry.is_directory && std::regex_match(entry.name, std::regex("^PS3_GM[[:digit:]]{2}$")))
{
const std::string elf = path + "/" + entry.name + "/USRDIR/EBOOT.BIN";
if (fs::is_file(elf))
{
m_path = elf;
if (const auto err = Load(title_id, add_only); err != game_boot_result::no_errors)
{
result = err;
}
}
}
}
}
return result;
}
void Emulator::SetForceBoot(bool force_boot)
{
m_force_boot = force_boot;
}
game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool is_disc_patch)
{
if (m_config_mode == cfg_mode::continuous)
{
// The program is being booted from another running program
// CELL_GAME_GAMETYPE_GAMEDATA is not used as boot type
if (m_cat == "DG"sv)
{
m_boot_source_type = CELL_GAME_GAMETYPE_DISC;
}
else if (m_cat == "HM"sv)
{
m_boot_source_type = CELL_GAME_GAMETYPE_HOME;
}
else
{
m_boot_source_type = CELL_GAME_GAMETYPE_HDD;
}
}
else
{
m_boot_source_type = CELL_GAME_GAMETYPE_SYS;
}
if (!IsStopped())
{
Kill();
}
if (!title_id.empty())
{
m_title_id = title_id;
}
sys_log.notice("Selected config: mode=%s, path=\"%s\"", m_config_mode, m_config_path);
sys_log.notice("Path: %s", m_path);
struct cleanup_t
{
Emulator* _this;
bool cleanup = true;
~cleanup_t()
{
if (cleanup && _this->IsStopped())
{
_this->Kill(false);
}
}
} cleanup{this};
{
Init(add_only);
// Load game list (maps ABCD12345 IDs to /dev_bdvd/ locations)
YAML::Node games;
if (fs::file f{fs::get_config_dir() + "/games.yml", fs::read + fs::create})
{
auto [result, error] = yaml_load(f.to_string());
if (!error.empty())
{
sys_log.error("Failed to load games.yml: %s", error);
}
games = result;
}
if (!games.IsMap())
{
games.reset();
}
m_state_inspection_savestate = g_cfg.savestate.state_inspection_mode.get();
if (m_ar)
{
struct file_header
{
ENABLE_BITWISE_SERIALIZATION;
nse_t<u64, 1> magic;
bool LE_format;
bool state_inspection_support;
nse_t<u64, 1> offset;
std::array<u8, 32> reserved;
};
const auto header = m_ar->try_read<file_header>().second;
if (header.magic != "RPCS3SAV"_u64)
{
return game_boot_result::savestate_corrupted;
}
if (header.LE_format != (std::endian::native == std::endian::little) || header.offset >= m_ar->data.size())
{
return game_boot_result::savestate_corrupted;
}
g_cfg.savestate.state_inspection_mode.set(header.state_inspection_support);
// Emulate seek operation (please avoid using in other places)
m_ar->pos = header.offset;
if (!is_savestate_version_compatible(m_ar->operator std::vector<std::pair<u16, u16>>(), true))
{
return game_boot_result::savestate_version_unsupported;
}
m_ar->pos = sizeof(file_header); // Restore position
argv.clear();
klic.clear();
std::string disc_info;
(*m_ar)(argv.emplace_back(), disc_info, klic.emplace_back(), m_game_dir, hdd1);
if (!klic[0])
{
klic.clear();
}
if (!disc_info.empty() && disc_info[0] != '/')
{
// Restore disc path for disc games (must exist in games.yml i.e. your game library)
m_title_id = disc_info;
// Load /dev_bdvd/ from game list if available
if (auto node = games[m_title_id])
{
disc = node.Scalar();
}
else if (!g_cfg.savestate.state_inspection_mode)
{
sys_log.fatal("Disc directory not found. Savestate cannot be loaded. ('%s')", m_title_id);
return game_boot_result::invalid_file_or_folder;
}
}
auto load_tar = [&](const std::string& path)
{
const usz size = *m_ar;
if (size)
{
fs::remove_all(path, false);
ensure(tar_object(fs::file(&m_ar->data[m_ar->pos], size)).extract(path));
m_ar->pos += size;
}
};
if (!hdd1.empty())
{
hdd1 = rpcs3::utils::get_hdd1_dir() + "caches/" + hdd1 + "/";
load_tar(hdd1);
}
for (const std::string hdd0_game = rpcs3::utils::get_hdd0_dir() + "game/";;)
{
const std::string game_data = *m_ar;
if (game_data.empty())
{
break;
}
load_tar(hdd0_game + game_data);
}
m_ar->pos += 32; // Reserved area
if (disc_info.starts_with("/"sv))
{
// Restore SFO directory for PSN games
if (disc_info.starts_with("/dev_hdd0"sv))
{
disc = rpcs3::utils::get_hdd0_dir();
disc += std::string_view(disc_info).substr(9);
}
else if (disc_info.starts_with("/host_root"sv))
{
sys_log.error("Host root has been used in savestates!");
disc = disc_info.substr(9);
}
else
{
sys_log.error("Unknown source for game SFO directory: %s", disc_info);
}
m_cat.clear();
}
if (argv[0].starts_with("/dev_hdd0"sv))
{
m_path = rpcs3::utils::get_hdd0_dir();
m_path += std::string_view(argv[0]).substr(9);
}
else if (argv[0].starts_with("/dev_flash"sv))
{
m_path = g_cfg_vfs.get_dev_flash();
m_path += std::string_view(argv[0]).substr(10);
}
else if (argv[0].starts_with("/dev_bdvd"sv))
{
m_path = disc;
m_path += std::string_view(argv[0]).substr(9);
}
else if (argv[0].starts_with("/host_root"sv))
{
sys_log.error("Host root has been used in savestates!");
m_path = argv[0].substr(9);
}
else if (argv[0].starts_with("/dev_hdd1"sv))
{
sys_log.error("HDD1 has been used to store executable in savestates!");
m_path = rpcs3::utils::get_hdd1_dir();
m_path += std::string_view(argv[0]).substr(9);
}
else
{
sys_log.error("Unknown source for savestates: %s", argv[0]);
}
sys_log.notice("Restored executable path: \'%s\'", m_path);
}
const std::string resolved_path = GetCallbacks().resolve_path(m_path);
const std::string elf_dir = fs::get_parent_dir(m_path);
// Mount /app_home again since m_path might have changed due to savestates.
vfs::mount("/app_home", g_cfg_vfs.app_home.to_string().empty() ? elf_dir + '/' : g_cfg_vfs.get(g_cfg_vfs.app_home, rpcs3::utils::get_emu_dir()));
// Load PARAM.SFO (TODO)
psf::registry _psf;
if (fs::file sfov{elf_dir + "/sce_sys/param.sfo"})
{
m_sfo_dir = elf_dir;
_psf = psf::load_object(sfov);
}
else
{
if (fs::is_dir(m_path))
{
// Special case (directory scan)
m_sfo_dir = rpcs3::utils::get_sfo_dir_from_game_path(m_path, m_title_id);
}