2018-08-10 02:49:45 +02:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
#include <fmt/format.h>
|
2019-04-16 20:27:34 +02:00
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "core/core.h"
|
2018-08-10 02:49:45 +02:00
|
|
|
#include "core/file_sys/bis_factory.h"
|
2019-04-16 20:27:34 +02:00
|
|
|
#include "core/file_sys/mode.h"
|
2018-09-04 03:58:19 +02:00
|
|
|
#include "core/file_sys/registered_cache.h"
|
2018-08-10 02:49:45 +02:00
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2020-07-09 18:29:12 +02:00
|
|
|
constexpr u64 NAND_USER_SIZE = 0x680000000; // 26624 MiB
|
|
|
|
constexpr u64 NAND_SYSTEM_SIZE = 0xA0000000; // 2560 MiB
|
|
|
|
constexpr u64 NAND_TOTAL_SIZE = 0x747C00000; // 29820 MiB
|
|
|
|
|
2018-10-29 21:08:03 +01:00
|
|
|
BISFactory::BISFactory(VirtualDir nand_root_, VirtualDir load_root_, VirtualDir dump_root_)
|
2018-09-20 04:03:36 +02:00
|
|
|
: nand_root(std::move(nand_root_)), load_root(std::move(load_root_)),
|
2018-10-29 21:08:03 +01:00
|
|
|
dump_root(std::move(dump_root_)),
|
2018-10-16 15:05:47 +02:00
|
|
|
sysnand_cache(std::make_unique<RegisteredCache>(
|
2018-08-16 23:05:30 +02:00
|
|
|
GetOrCreateDirectoryRelative(nand_root, "/system/Contents/registered"))),
|
2018-10-16 15:05:47 +02:00
|
|
|
usrnand_cache(std::make_unique<RegisteredCache>(
|
2019-04-10 18:10:38 +02:00
|
|
|
GetOrCreateDirectoryRelative(nand_root, "/user/Contents/registered"))),
|
|
|
|
sysnand_placeholder(std::make_unique<PlaceholderCache>(
|
|
|
|
GetOrCreateDirectoryRelative(nand_root, "/system/Contents/placehld"))),
|
|
|
|
usrnand_placeholder(std::make_unique<PlaceholderCache>(
|
|
|
|
GetOrCreateDirectoryRelative(nand_root, "/user/Contents/placehld"))) {}
|
2018-08-10 02:49:45 +02:00
|
|
|
|
2018-09-04 03:58:19 +02:00
|
|
|
BISFactory::~BISFactory() = default;
|
|
|
|
|
2019-04-10 18:08:39 +02:00
|
|
|
VirtualDir BISFactory::GetSystemNANDContentDirectory() const {
|
|
|
|
return GetOrCreateDirectoryRelative(nand_root, "/system/Contents");
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir BISFactory::GetUserNANDContentDirectory() const {
|
|
|
|
return GetOrCreateDirectoryRelative(nand_root, "/user/Contents");
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:05:47 +02:00
|
|
|
RegisteredCache* BISFactory::GetSystemNANDContents() const {
|
|
|
|
return sysnand_cache.get();
|
2018-08-10 02:49:45 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:05:47 +02:00
|
|
|
RegisteredCache* BISFactory::GetUserNANDContents() const {
|
|
|
|
return usrnand_cache.get();
|
2018-08-10 02:49:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 18:10:38 +02:00
|
|
|
PlaceholderCache* BISFactory::GetSystemNANDPlaceholder() const {
|
|
|
|
return sysnand_placeholder.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
PlaceholderCache* BISFactory::GetUserNANDPlaceholder() const {
|
|
|
|
return usrnand_placeholder.get();
|
|
|
|
}
|
|
|
|
|
2018-09-20 04:03:36 +02:00
|
|
|
VirtualDir BISFactory::GetModificationLoadRoot(u64 title_id) const {
|
|
|
|
// LayeredFS doesn't work on updates and title id-less homebrew
|
2019-04-10 18:11:50 +02:00
|
|
|
if (title_id == 0 || (title_id & 0xFFF) == 0x800)
|
2018-09-20 04:03:36 +02:00
|
|
|
return nullptr;
|
|
|
|
return GetOrCreateDirectoryRelative(load_root, fmt::format("/{:016X}", title_id));
|
|
|
|
}
|
|
|
|
|
2018-10-29 21:08:03 +01:00
|
|
|
VirtualDir BISFactory::GetModificationDumpRoot(u64 title_id) const {
|
|
|
|
if (title_id == 0)
|
|
|
|
return nullptr;
|
|
|
|
return GetOrCreateDirectoryRelative(dump_root, fmt::format("/{:016X}", title_id));
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:07:29 +02:00
|
|
|
VirtualDir BISFactory::OpenPartition(BisPartitionId id) const {
|
|
|
|
switch (id) {
|
|
|
|
case BisPartitionId::CalibrationFile:
|
|
|
|
return GetOrCreateDirectoryRelative(nand_root, "/prodinfof");
|
|
|
|
case BisPartitionId::SafeMode:
|
|
|
|
return GetOrCreateDirectoryRelative(nand_root, "/safe");
|
|
|
|
case BisPartitionId::System:
|
|
|
|
return GetOrCreateDirectoryRelative(nand_root, "/system");
|
|
|
|
case BisPartitionId::User:
|
|
|
|
return GetOrCreateDirectoryRelative(nand_root, "/user");
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile BISFactory::OpenPartitionStorage(BisPartitionId id) const {
|
2020-07-01 06:28:49 +02:00
|
|
|
auto& keys = Core::Crypto::KeyManager::Instance();
|
2019-04-10 18:07:29 +02:00
|
|
|
Core::Crypto::PartitionDataManager pdm{
|
|
|
|
Core::System::GetInstance().GetFilesystem()->OpenDirectory(
|
2020-08-15 14:33:16 +02:00
|
|
|
Common::FS::GetUserPath(Common::FS::UserPath::SysDataDir), Mode::Read)};
|
2019-04-10 18:07:29 +02:00
|
|
|
keys.PopulateFromPartitionData(pdm);
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case BisPartitionId::CalibrationBinary:
|
|
|
|
return pdm.GetDecryptedProdInfo();
|
|
|
|
case BisPartitionId::BootConfigAndPackage2Part1:
|
|
|
|
case BisPartitionId::BootConfigAndPackage2Part2:
|
|
|
|
case BisPartitionId::BootConfigAndPackage2Part3:
|
|
|
|
case BisPartitionId::BootConfigAndPackage2Part4:
|
|
|
|
case BisPartitionId::BootConfigAndPackage2Part5:
|
|
|
|
case BisPartitionId::BootConfigAndPackage2Part6: {
|
|
|
|
const auto new_id = static_cast<u8>(id) -
|
|
|
|
static_cast<u8>(BisPartitionId::BootConfigAndPackage2Part1) +
|
|
|
|
static_cast<u8>(Core::Crypto::Package2Type::NormalMain);
|
|
|
|
return pdm.GetPackage2Raw(static_cast<Core::Crypto::Package2Type>(new_id));
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:25 +02:00
|
|
|
VirtualDir BISFactory::GetImageDirectory() const {
|
|
|
|
return GetOrCreateDirectoryRelative(nand_root, "/user/Album");
|
|
|
|
}
|
|
|
|
|
2019-04-16 20:27:34 +02:00
|
|
|
u64 BISFactory::GetSystemNANDFreeSpace() const {
|
|
|
|
const auto sys_dir = GetOrCreateDirectoryRelative(nand_root, "/system");
|
2020-07-09 18:29:12 +02:00
|
|
|
if (sys_dir == nullptr) {
|
|
|
|
return GetSystemNANDTotalSpace();
|
|
|
|
}
|
2019-04-16 20:27:34 +02:00
|
|
|
|
|
|
|
return GetSystemNANDTotalSpace() - sys_dir->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 BISFactory::GetSystemNANDTotalSpace() const {
|
2020-07-09 18:29:12 +02:00
|
|
|
return NAND_SYSTEM_SIZE;
|
2019-04-16 20:27:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 BISFactory::GetUserNANDFreeSpace() const {
|
2020-07-09 20:38:28 +02:00
|
|
|
// For some reason games such as BioShock 1 checks whether this is exactly 0x680000000 bytes.
|
|
|
|
// Set the free space to be 1 MiB less than the total as a workaround to this issue.
|
|
|
|
return GetUserNANDTotalSpace() - 0x100000;
|
2019-04-16 20:27:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 BISFactory::GetUserNANDTotalSpace() const {
|
2020-07-09 18:29:12 +02:00
|
|
|
return NAND_USER_SIZE;
|
2019-04-16 20:27:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 BISFactory::GetFullNANDTotalSpace() const {
|
2020-07-09 18:29:12 +02:00
|
|
|
return NAND_TOTAL_SIZE;
|
2019-04-16 20:27:34 +02:00
|
|
|
}
|
|
|
|
|
2019-04-29 00:42:36 +02:00
|
|
|
VirtualDir BISFactory::GetBCATDirectory(u64 title_id) const {
|
|
|
|
return GetOrCreateDirectoryRelative(nand_root,
|
|
|
|
fmt::format("/system/save/bcat/{:016X}", title_id));
|
|
|
|
}
|
|
|
|
|
2018-08-10 02:49:45 +02:00
|
|
|
} // namespace FileSys
|