bis_factory.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <fmt/format.h>
  5. #include "common/file_util.h"
  6. #include "core/core.h"
  7. #include "core/file_sys/bis_factory.h"
  8. #include "core/file_sys/mode.h"
  9. #include "core/file_sys/registered_cache.h"
  10. #include "core/settings.h"
  11. namespace FileSys {
  12. BISFactory::BISFactory(VirtualDir nand_root_, VirtualDir load_root_, VirtualDir dump_root_)
  13. : nand_root(std::move(nand_root_)), load_root(std::move(load_root_)),
  14. dump_root(std::move(dump_root_)),
  15. sysnand_cache(std::make_unique<RegisteredCache>(
  16. GetOrCreateDirectoryRelative(nand_root, "/system/Contents/registered"))),
  17. usrnand_cache(std::make_unique<RegisteredCache>(
  18. GetOrCreateDirectoryRelative(nand_root, "/user/Contents/registered"))),
  19. sysnand_placeholder(std::make_unique<PlaceholderCache>(
  20. GetOrCreateDirectoryRelative(nand_root, "/system/Contents/placehld"))),
  21. usrnand_placeholder(std::make_unique<PlaceholderCache>(
  22. GetOrCreateDirectoryRelative(nand_root, "/user/Contents/placehld"))) {}
  23. BISFactory::~BISFactory() = default;
  24. VirtualDir BISFactory::GetSystemNANDContentDirectory() const {
  25. return GetOrCreateDirectoryRelative(nand_root, "/system/Contents");
  26. }
  27. VirtualDir BISFactory::GetUserNANDContentDirectory() const {
  28. return GetOrCreateDirectoryRelative(nand_root, "/user/Contents");
  29. }
  30. RegisteredCache* BISFactory::GetSystemNANDContents() const {
  31. return sysnand_cache.get();
  32. }
  33. RegisteredCache* BISFactory::GetUserNANDContents() const {
  34. return usrnand_cache.get();
  35. }
  36. PlaceholderCache* BISFactory::GetSystemNANDPlaceholder() const {
  37. return sysnand_placeholder.get();
  38. }
  39. PlaceholderCache* BISFactory::GetUserNANDPlaceholder() const {
  40. return usrnand_placeholder.get();
  41. }
  42. VirtualDir BISFactory::GetModificationLoadRoot(u64 title_id) const {
  43. // LayeredFS doesn't work on updates and title id-less homebrew
  44. if (title_id == 0 || (title_id & 0xFFF) == 0x800)
  45. return nullptr;
  46. return GetOrCreateDirectoryRelative(load_root, fmt::format("/{:016X}", title_id));
  47. }
  48. VirtualDir BISFactory::GetModificationDumpRoot(u64 title_id) const {
  49. if (title_id == 0)
  50. return nullptr;
  51. return GetOrCreateDirectoryRelative(dump_root, fmt::format("/{:016X}", title_id));
  52. }
  53. VirtualDir BISFactory::OpenPartition(BisPartitionId id) const {
  54. switch (id) {
  55. case BisPartitionId::CalibrationFile:
  56. return GetOrCreateDirectoryRelative(nand_root, "/prodinfof");
  57. case BisPartitionId::SafeMode:
  58. return GetOrCreateDirectoryRelative(nand_root, "/safe");
  59. case BisPartitionId::System:
  60. return GetOrCreateDirectoryRelative(nand_root, "/system");
  61. case BisPartitionId::User:
  62. return GetOrCreateDirectoryRelative(nand_root, "/user");
  63. default:
  64. return nullptr;
  65. }
  66. }
  67. VirtualFile BISFactory::OpenPartitionStorage(BisPartitionId id) const {
  68. Core::Crypto::KeyManager keys;
  69. Core::Crypto::PartitionDataManager pdm{
  70. Core::System::GetInstance().GetFilesystem()->OpenDirectory(
  71. FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir), Mode::Read)};
  72. keys.PopulateFromPartitionData(pdm);
  73. switch (id) {
  74. case BisPartitionId::CalibrationBinary:
  75. return pdm.GetDecryptedProdInfo();
  76. case BisPartitionId::BootConfigAndPackage2Part1:
  77. case BisPartitionId::BootConfigAndPackage2Part2:
  78. case BisPartitionId::BootConfigAndPackage2Part3:
  79. case BisPartitionId::BootConfigAndPackage2Part4:
  80. case BisPartitionId::BootConfigAndPackage2Part5:
  81. case BisPartitionId::BootConfigAndPackage2Part6: {
  82. const auto new_id = static_cast<u8>(id) -
  83. static_cast<u8>(BisPartitionId::BootConfigAndPackage2Part1) +
  84. static_cast<u8>(Core::Crypto::Package2Type::NormalMain);
  85. return pdm.GetPackage2Raw(static_cast<Core::Crypto::Package2Type>(new_id));
  86. }
  87. default:
  88. return nullptr;
  89. }
  90. }
  91. VirtualDir BISFactory::GetImageDirectory() const {
  92. return GetOrCreateDirectoryRelative(nand_root, "/user/Album");
  93. }
  94. u64 BISFactory::GetSystemNANDFreeSpace() const {
  95. const auto sys_dir = GetOrCreateDirectoryRelative(nand_root, "/system");
  96. if (sys_dir == nullptr)
  97. return 0;
  98. return GetSystemNANDTotalSpace() - sys_dir->GetSize();
  99. }
  100. u64 BISFactory::GetSystemNANDTotalSpace() const {
  101. return static_cast<u64>(Settings::values.nand_system_size);
  102. }
  103. u64 BISFactory::GetUserNANDFreeSpace() const {
  104. const auto usr_dir = GetOrCreateDirectoryRelative(nand_root, "/user");
  105. if (usr_dir == nullptr)
  106. return 0;
  107. return GetUserNANDTotalSpace() - usr_dir->GetSize();
  108. }
  109. u64 BISFactory::GetUserNANDTotalSpace() const {
  110. return static_cast<u64>(Settings::values.nand_user_size);
  111. }
  112. u64 BISFactory::GetFullNANDTotalSpace() const {
  113. return static_cast<u64>(Settings::values.nand_total_size);
  114. }
  115. VirtualDir BISFactory::GetBCATDirectory(u64 title_id) const {
  116. return GetOrCreateDirectoryRelative(nand_root,
  117. fmt::format("/system/save/bcat/{:016X}", title_id));
  118. }
  119. } // namespace FileSys