2021-04-21 22:12:21 +02:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "system_utils.hpp"
|
|
|
|
|
#include "system_config.h"
|
2021-09-23 20:12:06 +02:00
|
|
|
#include "vfs_config.h"
|
2021-08-10 21:45:26 +02:00
|
|
|
#include "Emu/Io/pad_config.h"
|
2021-04-21 22:12:21 +02:00
|
|
|
#include "util/sysinfo.hpp"
|
|
|
|
|
#include "Utilities/File.h"
|
|
|
|
|
#include "Utilities/StrUtil.h"
|
|
|
|
|
#include "Utilities/Thread.h"
|
|
|
|
|
#include "Crypto/unpkg.h"
|
2021-07-18 21:19:58 +02:00
|
|
|
#include "Crypto/unself.h"
|
|
|
|
|
#include "Crypto/unedat.h"
|
2021-04-21 22:12:21 +02:00
|
|
|
|
|
|
|
|
#include <charconv>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
LOG_CHANNEL(sys_log, "SYS");
|
|
|
|
|
|
|
|
|
|
namespace rpcs3::utils
|
|
|
|
|
{
|
|
|
|
|
u32 get_max_threads()
|
|
|
|
|
{
|
|
|
|
|
const u32 max_threads = static_cast<u32>(g_cfg.core.llvm_threads);
|
|
|
|
|
const u32 hw_threads = ::utils::get_thread_count();
|
|
|
|
|
const u32 thread_count = max_threads > 0 ? std::min(max_threads, hw_threads) : hw_threads;
|
|
|
|
|
return thread_count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-06 22:14:25 +02:00
|
|
|
void configure_logs(bool force_enable)
|
2021-04-21 22:12:21 +02:00
|
|
|
{
|
|
|
|
|
static bool was_silenced = false;
|
|
|
|
|
|
2023-07-06 22:14:25 +02:00
|
|
|
const bool silenced = g_cfg.misc.silence_all_logs.get() && !force_enable;
|
2021-04-21 22:12:21 +02:00
|
|
|
|
|
|
|
|
if (silenced)
|
|
|
|
|
{
|
|
|
|
|
if (!was_silenced)
|
|
|
|
|
{
|
2023-07-06 22:14:25 +02:00
|
|
|
sys_log.always()("Disabling logging! Do not create issues on GitHub or on the forums while logging is disabled.");
|
2021-04-21 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logs::silence();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logs::reset();
|
|
|
|
|
logs::set_channel_levels(g_cfg.log.get_map());
|
|
|
|
|
|
|
|
|
|
if (was_silenced)
|
|
|
|
|
{
|
2021-08-28 18:21:13 +02:00
|
|
|
sys_log.success("Logging enabled");
|
2021-04-21 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
was_silenced = silenced;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 check_user(const std::string& user)
|
|
|
|
|
{
|
|
|
|
|
u32 id = 0;
|
2022-03-31 22:01:02 +02:00
|
|
|
|
2021-04-21 22:12:21 +02:00
|
|
|
if (user.size() == 8)
|
|
|
|
|
{
|
|
|
|
|
std::from_chars(&user.front(), &user.back() + 1, id);
|
|
|
|
|
}
|
2022-03-31 22:01:02 +02:00
|
|
|
|
2021-04-21 22:12:21 +02:00
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool install_pkg(const std::string& path)
|
|
|
|
|
{
|
|
|
|
|
sys_log.success("Installing package: %s", path);
|
|
|
|
|
|
|
|
|
|
int int_progress = 0;
|
|
|
|
|
|
2023-01-03 13:31:39 +01:00
|
|
|
std::deque<package_reader> reader;
|
|
|
|
|
reader.emplace_back(path);
|
|
|
|
|
|
2021-04-21 22:12:21 +02:00
|
|
|
// Run PKG unpacking asynchronously
|
|
|
|
|
named_thread worker("PKG Installer", [&]
|
|
|
|
|
{
|
2023-01-03 13:31:39 +01:00
|
|
|
std::deque<std::string> bootables;
|
2023-01-11 03:06:52 +01:00
|
|
|
const package_error error = package_reader::extract_data(reader, bootables);
|
|
|
|
|
return error == package_error::no_error;
|
2021-04-21 22:12:21 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Wait for the completion
|
|
|
|
|
while (std::this_thread::sleep_for(5ms), worker <= thread_state::aborting)
|
|
|
|
|
{
|
|
|
|
|
// TODO: update unified progress dialog
|
2023-01-03 13:31:39 +01:00
|
|
|
const int pval = reader[0].get_progress(100);
|
2021-04-21 22:12:21 +02:00
|
|
|
|
2023-01-03 13:31:39 +01:00
|
|
|
if (pval > int_progress)
|
2021-04-21 22:12:21 +02:00
|
|
|
{
|
2023-01-03 13:31:39 +01:00
|
|
|
int_progress = pval;
|
2021-04-21 22:12:21 +02:00
|
|
|
sys_log.success("... %u%%", int_progress);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return worker();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string get_emu_dir()
|
|
|
|
|
{
|
2021-09-23 20:12:06 +02:00
|
|
|
const std::string& emu_dir_ = g_cfg_vfs.emulator_dir;
|
2021-04-21 22:12:21 +02:00
|
|
|
return emu_dir_.empty() ? fs::get_config_dir() : emu_dir_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string get_hdd0_dir()
|
|
|
|
|
{
|
2021-09-23 20:12:06 +02:00
|
|
|
return g_cfg_vfs.get(g_cfg_vfs.dev_hdd0, get_emu_dir());
|
2021-04-21 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string get_hdd1_dir()
|
|
|
|
|
{
|
2021-09-23 20:12:06 +02:00
|
|
|
return g_cfg_vfs.get(g_cfg_vfs.dev_hdd1, get_emu_dir());
|
2021-04-21 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string get_cache_dir()
|
|
|
|
|
{
|
|
|
|
|
return fs::get_cache_dir() + "cache/";
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 21:19:58 +02:00
|
|
|
std::string get_rap_file_path(const std::string_view& rap)
|
2021-04-21 22:12:21 +02:00
|
|
|
{
|
2021-07-18 21:19:58 +02:00
|
|
|
const std::string home_dir = get_hdd0_dir() + "home";
|
2021-04-21 22:12:21 +02:00
|
|
|
|
2021-06-04 20:06:04 +02:00
|
|
|
std::string rap_path;
|
|
|
|
|
|
2021-04-21 22:12:21 +02:00
|
|
|
for (auto&& entry : fs::dir(home_dir))
|
|
|
|
|
{
|
|
|
|
|
if (entry.is_directory && check_user(entry.name))
|
|
|
|
|
{
|
2021-06-04 20:06:04 +02:00
|
|
|
rap_path = fmt::format("%s/%s/exdata/%s.rap", home_dir, entry.name, rap);
|
2021-04-21 22:12:21 +02:00
|
|
|
if (fs::is_file(rap_path))
|
|
|
|
|
{
|
|
|
|
|
return rap_path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 20:06:04 +02:00
|
|
|
// Return a sample path tested for logging purposes
|
|
|
|
|
return rap_path;
|
2021-04-21 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-18 18:42:53 +02:00
|
|
|
std::string get_c00_unlock_edat_path(const std::string_view& content_id)
|
2021-07-18 21:19:58 +02:00
|
|
|
{
|
|
|
|
|
const std::string home_dir = get_hdd0_dir() + "home";
|
|
|
|
|
|
|
|
|
|
std::string edat_path;
|
|
|
|
|
|
|
|
|
|
for (auto&& entry : fs::dir(home_dir))
|
|
|
|
|
{
|
|
|
|
|
if (entry.is_directory && check_user(entry.name))
|
|
|
|
|
{
|
2021-09-18 18:42:53 +02:00
|
|
|
edat_path = fmt::format("%s/%s/exdata/%s.edat", home_dir, entry.name, content_id);
|
2021-07-18 21:19:58 +02:00
|
|
|
if (fs::is_file(edat_path))
|
|
|
|
|
{
|
|
|
|
|
return edat_path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return a sample path tested for logging purposes
|
|
|
|
|
return edat_path;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 10:24:28 +01:00
|
|
|
bool verify_c00_unlock_edat(const std::string_view& content_id, bool fast)
|
2021-07-18 21:19:58 +02:00
|
|
|
{
|
2021-09-18 18:42:53 +02:00
|
|
|
const std::string edat_path = rpcs3::utils::get_c00_unlock_edat_path(content_id);
|
2021-07-18 21:19:58 +02:00
|
|
|
|
|
|
|
|
// Check if user has unlock EDAT installed
|
2021-09-26 19:45:24 +02:00
|
|
|
fs::file enc_file(edat_path);
|
|
|
|
|
|
|
|
|
|
if (!enc_file)
|
2021-07-18 21:19:58 +02:00
|
|
|
{
|
|
|
|
|
sys_log.notice("verify_c00_unlock_edat(): '%s' not found", edat_path);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 10:24:28 +01:00
|
|
|
// Use simple check for GUI
|
|
|
|
|
if (fast)
|
|
|
|
|
return true;
|
|
|
|
|
|
2021-07-18 21:19:58 +02:00
|
|
|
u128 k_licensee = get_default_self_klic();
|
2022-03-20 13:13:59 +01:00
|
|
|
NPD_HEADER npd;
|
2021-07-18 21:19:58 +02:00
|
|
|
|
2022-03-20 13:13:59 +01:00
|
|
|
if (!VerifyEDATHeaderWithKLicense(enc_file, edat_path, reinterpret_cast<u8*>(&k_licensee), &npd))
|
2021-07-18 21:19:58 +02:00
|
|
|
{
|
|
|
|
|
sys_log.error("verify_c00_unlock_edat(): Failed to verify npd file '%s'", edat_path);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 13:13:59 +01:00
|
|
|
std::string edat_content_id = npd.content_id;
|
|
|
|
|
|
2021-07-18 21:19:58 +02:00
|
|
|
if (edat_content_id != content_id)
|
|
|
|
|
{
|
|
|
|
|
sys_log.error("verify_c00_unlock_edat(): Content ID mismatch in npd header of '%s'", edat_path);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decrypt EDAT and verify its contents
|
2021-09-26 19:45:24 +02:00
|
|
|
fs::file dec_file = DecryptEDAT(enc_file, edat_path, 8, reinterpret_cast<u8*>(&k_licensee), false);
|
2021-07-18 21:19:58 +02:00
|
|
|
if (!dec_file)
|
|
|
|
|
{
|
|
|
|
|
sys_log.error("verify_c00_unlock_edat(): Failed to decrypt '%s'", edat_path);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 magic{};
|
|
|
|
|
dec_file.read<u32>(magic);
|
|
|
|
|
if (magic != "GOMA"_u32)
|
|
|
|
|
{
|
|
|
|
|
sys_log.error("verify_c00_unlock_edat(): Bad header magic in unlock EDAT '%s'", edat_path);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read null-terminated string
|
|
|
|
|
dec_file.seek(0x10);
|
2021-12-01 17:09:07 +01:00
|
|
|
dec_file.read(edat_content_id, 0x30);
|
2021-07-18 21:19:58 +02:00
|
|
|
edat_content_id.resize(std::min<usz>(0x30, edat_content_id.find_first_of('\0')));
|
|
|
|
|
if (edat_content_id != content_id)
|
|
|
|
|
{
|
|
|
|
|
sys_log.error("verify_c00_unlock_edat(): Content ID mismatch in unlock EDAT '%s'", edat_path);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Game has been purchased and EDAT is verified
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 22:12:21 +02:00
|
|
|
std::string get_sfo_dir_from_game_path(const std::string& game_path, const std::string& title_id)
|
|
|
|
|
{
|
|
|
|
|
if (fs::is_file(game_path + "/PS3_DISC.SFB"))
|
|
|
|
|
{
|
|
|
|
|
// This is a disc game.
|
|
|
|
|
if (!title_id.empty())
|
|
|
|
|
{
|
|
|
|
|
for (auto&& entry : fs::dir{game_path})
|
|
|
|
|
{
|
|
|
|
|
if (entry.name == "." || entry.name == "..")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string sfo_path = game_path + "/" + entry.name + "/PARAM.SFO";
|
|
|
|
|
|
|
|
|
|
if (entry.is_directory && fs::is_file(sfo_path))
|
|
|
|
|
{
|
2022-11-27 08:59:57 +01:00
|
|
|
const auto psf = psf::load_object(sfo_path);
|
2021-04-21 22:12:21 +02:00
|
|
|
const auto serial = psf::get_string(psf, "TITLE_ID");
|
|
|
|
|
if (serial == title_id)
|
|
|
|
|
{
|
|
|
|
|
return game_path + "/" + entry.name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return game_path + "/PS3_GAME";
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 08:59:57 +01:00
|
|
|
const auto psf = psf::load_object(game_path + "/PARAM.SFO");
|
2021-04-21 22:12:21 +02:00
|
|
|
|
|
|
|
|
const auto category = psf::get_string(psf, "CATEGORY");
|
2021-07-18 21:19:58 +02:00
|
|
|
const auto content_id = psf::get_string(psf, "CONTENT_ID");
|
2021-04-21 22:12:21 +02:00
|
|
|
|
|
|
|
|
if (category == "HG" && !content_id.empty())
|
|
|
|
|
{
|
2021-07-18 21:19:58 +02:00
|
|
|
// This is a trial game. Check if the user has EDAT file to unlock it.
|
|
|
|
|
const auto c00_title_id = psf::get_string(psf, "TITLE_ID");
|
|
|
|
|
|
2022-03-07 10:24:28 +01:00
|
|
|
if (fs::is_file(game_path + "/C00/PARAM.SFO") && verify_c00_unlock_edat(content_id, true))
|
2021-04-21 22:12:21 +02:00
|
|
|
{
|
|
|
|
|
// Load full game data.
|
2022-03-07 10:24:28 +01:00
|
|
|
sys_log.notice("Found EDAT file %s.edat for trial game %s", content_id, c00_title_id);
|
2021-04-21 22:12:21 +02:00
|
|
|
return game_path + "/C00";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return game_path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string get_custom_config_dir()
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
return fs::get_config_dir() + "config/custom_configs/";
|
|
|
|
|
#else
|
|
|
|
|
return fs::get_config_dir() + "custom_configs/";
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 13:00:24 +01:00
|
|
|
std::string get_custom_config_path(const std::string& identifier)
|
2021-04-21 22:12:21 +02:00
|
|
|
{
|
2023-01-06 13:00:24 +01:00
|
|
|
if (identifier.empty())
|
|
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return get_custom_config_dir() + "config_" + identifier + ".yml";
|
2021-04-21 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-10 21:45:26 +02:00
|
|
|
std::string get_input_config_root()
|
2021-04-21 22:12:21 +02:00
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
2021-08-10 21:45:26 +02:00
|
|
|
return fs::get_config_dir() + "config/input_configs/";
|
2021-04-21 22:12:21 +02:00
|
|
|
#else
|
2021-08-10 21:45:26 +02:00
|
|
|
return fs::get_config_dir() + "input_configs/";
|
2021-04-21 22:12:21 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 21:45:26 +02:00
|
|
|
std::string get_input_config_dir(const std::string& title_id)
|
|
|
|
|
{
|
|
|
|
|
return get_input_config_root() + (title_id.empty() ? "global" : title_id) + "/";
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 22:12:21 +02:00
|
|
|
std::string get_custom_input_config_path(const std::string& title_id)
|
|
|
|
|
{
|
2021-08-10 21:45:26 +02:00
|
|
|
if (title_id.empty()) return "";
|
2023-08-29 21:05:04 +02:00
|
|
|
return get_input_config_dir(title_id) + g_cfg_input_configs.default_config + ".yml";
|
2021-04-21 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
}
|