Debian Codegraph @

Source/Core/DiscIO/DiscScrubber.cpp

raw symbols SCIP
1 // Copyright 2009 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DiscIO/DiscScrubber.h"
5
6 #include <cstddef>
7 #include <optional>
8 #include <vector>
9
10 #include "Common/Align.h"
11 #include "Common/CommonTypes.h"
12 #include "Common/Logging/Log.h"
13
14 #include "DiscIO/DiscUtils.h"
15 #include "DiscIO/Filesystem.h"
16 #include "DiscIO/Volume.h"
17
18 namespace DiscIO
19 {
20 DiscScrubber::DiscScrubber() = default;
21
22 bool DiscScrubber::SetupScrub(const Volume& disc)
23 {
24 m_file_size = disc.GetDataSize();
25 m_has_wii_hashes = disc.HasWiiHashes();
26
27 // Round up when diving by CLUSTER_SIZE, otherwise MarkAsUsed might write out of bounds
28 const size_t num_clusters = static_cast<size_t>((m_file_size + CLUSTER_SIZE - 1) / CLUSTER_SIZE);
29
30 // Table of free blocks
31 m_free_table.resize(num_clusters, 1);
32
33 // Fill out table of free blocks
34 const bool success = ParseDisc(disc);
35
36 m_is_scrubbing = success;
37 return success;
38 }
39
40 bool DiscScrubber::CanBlockBeScrubbed(u64 offset) const
41 {
42 if (!m_is_scrubbing)
43 return false;
44
45 const u64 cluster_index = offset / CLUSTER_SIZE;
46 return cluster_index >= m_free_table.size() || m_free_table[cluster_index];
47 }
48
49 void DiscScrubber::MarkAsUsed(u64 offset, u64 size)
50 {
51 u64 current_offset = Common::AlignDown(offset, CLUSTER_SIZE);
52 const u64 end_offset = offset + size;
53
54 DEBUG_LOG_FMT(DISCIO, "Marking {:#018x} - {:#018x} as used", offset, end_offset);
55
56 while (current_offset < end_offset && current_offset < m_file_size)
57 {
58 m_free_table[current_offset / CLUSTER_SIZE] = 0;
59 current_offset += CLUSTER_SIZE;
60 }
61 }
62
63 void DiscScrubber::MarkAsUsedE(u64 partition_data_offset, u64 offset, u64 size)
64 {
65 if (partition_data_offset == 0)
66 {
67 MarkAsUsed(offset, size);
68 }
69 else
70 {
71 u64 first_cluster_start = ToClusterOffset(offset) + partition_data_offset;
72
73 u64 last_cluster_end;
74 if (size == 0)
75 {
76 // Without this special case, a size of 0 can be rounded to 1 cluster instead of 0
77 last_cluster_end = first_cluster_start;
78 }
79 else
80 {
81 last_cluster_end = ToClusterOffset(offset + size - 1) + CLUSTER_SIZE + partition_data_offset;
82 }
83
84 MarkAsUsed(first_cluster_start, last_cluster_end - first_cluster_start);
85 }
86 }
87
88 // Compensate for 0x400 (SHA-1) per 0x8000 (cluster), and round to whole clusters
89 u64 DiscScrubber::ToClusterOffset(u64 offset) const
90 {
91 if (m_has_wii_hashes)
92 return offset / 0x7c00 * CLUSTER_SIZE;
93 else
94 return Common::AlignDown(offset, CLUSTER_SIZE);
95 }
96
97 // Helper functions for reading the BE volume
98 bool DiscScrubber::ReadFromVolume(const Volume& disc, u64 offset, u32& buffer,
99 const Partition& partition)
100 {
101 std::optional<u32> value = disc.ReadSwapped<u32>(offset, partition);
102 if (value)
103 buffer = *value;
104 return value.has_value();
105 }
106
107 bool DiscScrubber::ReadFromVolume(const Volume& disc, u64 offset, u64& buffer,
108 const Partition& partition)
109 {
110 std::optional<u64> value = disc.ReadSwappedAndShifted(offset, partition);
111 if (value)
112 buffer = *value;
113 return value.has_value();
114 }
115
116 bool DiscScrubber::ParseDisc(const Volume& disc)
117 {
118 if (disc.GetPartitions().empty())
119 return ParsePartitionData(disc, PARTITION_NONE);
120
121 // Mark the header as used - it's mostly 0s anyways
122 MarkAsUsed(0, 0x50000);
123
124 for (const Partition& partition : disc.GetPartitions())
125 {
126 u32 tmd_size;
127 u64 tmd_offset;
128 u32 cert_chain_size;
129 u64 cert_chain_offset;
130 u64 h3_offset;
131 // The H3 size is always 0x18000
132
133 if (!ReadFromVolume(disc, partition.offset + WII_PARTITION_TMD_SIZE_ADDRESS, tmd_size,
134 PARTITION_NONE) ||
135 !ReadFromVolume(disc, partition.offset + WII_PARTITION_TMD_OFFSET_ADDRESS, tmd_offset,
136 PARTITION_NONE) ||
137 !ReadFromVolume(disc, partition.offset + WII_PARTITION_CERT_CHAIN_SIZE_ADDRESS,
138 cert_chain_size, PARTITION_NONE) ||
139 !ReadFromVolume(disc, partition.offset + WII_PARTITION_CERT_CHAIN_OFFSET_ADDRESS,
140 cert_chain_offset, PARTITION_NONE) ||
141 !ReadFromVolume(disc, partition.offset + WII_PARTITION_H3_OFFSET_ADDRESS, h3_offset,
142 PARTITION_NONE))
143 {
144 return false;
145 }
146
147 MarkAsUsed(partition.offset, 0x2c0);
148
149 MarkAsUsed(partition.offset + tmd_offset, tmd_size);
150 MarkAsUsed(partition.offset + cert_chain_offset, cert_chain_size);
151 MarkAsUsed(partition.offset + h3_offset, WII_PARTITION_H3_SIZE);
152
153 // Parse Data! This is where the big gain is
154 if (!ParsePartitionData(disc, partition))
155 return false;
156 }
157
158 return true;
159 }
160
161 // Operations dealing with encrypted space are done here
162 bool DiscScrubber::ParsePartitionData(const Volume& disc, const Partition& partition)
163 {
164 const FileSystem* filesystem = disc.GetFileSystem(partition);
165 if (!filesystem)
166 {
167 ERROR_LOG_FMT(DISCIO, "Failed to read file system for the partition at {:#x}",
168 partition.offset);
169 return false;
170 }
171
172 u64 partition_data_offset;
173 if (partition == PARTITION_NONE)
174 {
175 partition_data_offset = 0;
176 }
177 else
178 {
179 u64 data_offset;
180 if (!ReadFromVolume(disc, partition.offset + 0x2b8, data_offset, PARTITION_NONE))
181 return false;
182
183 partition_data_offset = partition.offset + data_offset;
184 }
185
186 // Mark things as used which are not in the filesystem
187 // Header, Header Information, Apploader
188 u32 apploader_size;
189 u32 apploader_trailer_size;
190 if (!ReadFromVolume(disc, 0x2440 + 0x14, apploader_size, partition) ||
191 !ReadFromVolume(disc, 0x2440 + 0x18, apploader_trailer_size, partition))
192 {
193 return false;
194 }
195 MarkAsUsedE(partition_data_offset, 0, 0x2440 + apploader_size + apploader_trailer_size);
196
197 // DOL
198 const std::optional<u64> dol_offset = GetBootDOLOffset(disc, partition);
199 if (!dol_offset)
200 return false;
201 const std::optional<u64> dol_size = GetBootDOLSize(disc, partition, *dol_offset);
202 if (!dol_size)
203 return false;
204 MarkAsUsedE(partition_data_offset, *dol_offset, *dol_size);
205
206 // FST
207 const std::optional<u64> fst_offset = GetFSTOffset(disc, partition);
208 const std::optional<u64> fst_size = GetFSTSize(disc, partition);
209 if (!fst_offset || !fst_size)
210 return false;
211 MarkAsUsedE(partition_data_offset, *fst_offset, *fst_size);
212
213 // Go through the filesystem and mark entries as used
214 ParseFileSystemData(partition_data_offset, filesystem->GetRoot());
215
216 return true;
217 }
218
219 void DiscScrubber::ParseFileSystemData(u64 partition_data_offset, const FileInfo& directory)
220 {
221 for (const FileInfo& file_info : directory)
222 {
223 DEBUG_LOG_FMT(DISCIO, "Scrubbing {}", file_info.GetPath());
224 if (file_info.IsDirectory())
225 ParseFileSystemData(partition_data_offset, file_info);
226 else
227 MarkAsUsedE(partition_data_offset, file_info.GetOffset(), file_info.GetSize());
228 }
229 }
230
231 } // namespace DiscIO