summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/bis_factory.cpp
blob: 8f758d6d9db941c094b8c92d38637f615a2613e6 (plain) (blame)
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
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <fmt/format.h>
#include "common/file_util.h"
#include "core/core.h"
#include "core/file_sys/bis_factory.h"
#include "core/file_sys/mode.h"
#include "core/file_sys/registered_cache.h"
#include "core/settings.h"

namespace FileSys {

BISFactory::BISFactory(VirtualDir nand_root_, VirtualDir load_root_, VirtualDir dump_root_)
    : nand_root(std::move(nand_root_)), load_root(std::move(load_root_)),
      dump_root(std::move(dump_root_)),
      sysnand_cache(std::make_unique<RegisteredCache>(
          GetOrCreateDirectoryRelative(nand_root, "/system/Contents/registered"))),
      usrnand_cache(std::make_unique<RegisteredCache>(
          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"))) {}

BISFactory::~BISFactory() = default;

VirtualDir BISFactory::GetSystemNANDContentDirectory() const {
    return GetOrCreateDirectoryRelative(nand_root, "/system/Contents");
}

VirtualDir BISFactory::GetUserNANDContentDirectory() const {
    return GetOrCreateDirectoryRelative(nand_root, "/user/Contents");
}

RegisteredCache* BISFactory::GetSystemNANDContents() const {
    return sysnand_cache.get();
}

RegisteredCache* BISFactory::GetUserNANDContents() const {
    return usrnand_cache.get();
}

PlaceholderCache* BISFactory::GetSystemNANDPlaceholder() const {
    return sysnand_placeholder.get();
}

PlaceholderCache* BISFactory::GetUserNANDPlaceholder() const {
    return usrnand_placeholder.get();
}

VirtualDir BISFactory::GetModificationLoadRoot(u64 title_id) const {
    // LayeredFS doesn't work on updates and title id-less homebrew
    if (title_id == 0 || (title_id & 0xFFF) == 0x800)
        return nullptr;
    return GetOrCreateDirectoryRelative(load_root, fmt::format("/{:016X}", title_id));
}

VirtualDir BISFactory::GetModificationDumpRoot(u64 title_id) const {
    if (title_id == 0)
        return nullptr;
    return GetOrCreateDirectoryRelative(dump_root, fmt::format("/{:016X}", title_id));
}

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 {
    Core::Crypto::KeyManager keys;
    Core::Crypto::PartitionDataManager pdm{
        Core::System::GetInstance().GetFilesystem()->OpenDirectory(
            FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir), Mode::Read)};
    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;
    }
}

VirtualDir BISFactory::GetImageDirectory() const {
    return GetOrCreateDirectoryRelative(nand_root, "/user/Album");
}

u64 BISFactory::GetSystemNANDFreeSpace() const {
    const auto sys_dir = GetOrCreateDirectoryRelative(nand_root, "/system");
    if (sys_dir == nullptr)
        return 0;

    return GetSystemNANDTotalSpace() - sys_dir->GetSize();
}

u64 BISFactory::GetSystemNANDTotalSpace() const {
    return static_cast<u64>(Settings::values.nand_system_size);
}

u64 BISFactory::GetUserNANDFreeSpace() const {
    const auto usr_dir = GetOrCreateDirectoryRelative(nand_root, "/user");
    if (usr_dir == nullptr)
        return 0;

    return GetUserNANDTotalSpace() - usr_dir->GetSize();
}

u64 BISFactory::GetUserNANDTotalSpace() const {
    return static_cast<u64>(Settings::values.nand_user_size);
}

u64 BISFactory::GetFullNANDTotalSpace() const {
    return static_cast<u64>(Settings::values.nand_total_size);
}

} // namespace FileSys