File: FileSystemGCWii.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 28,976 kB
  • ctags: 35,666
  • sloc: cpp: 213,139; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (357 lines) | stat: -rw-r--r-- 9,203 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <algorithm>
#include <cinttypes>
#include <cstddef>
#include <cstring>
#include <string>
#include <vector>

#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/MsgHandler.h"
#include "Common/StringUtil.h"
#include "Common/Logging/Log.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/FileSystemGCWii.h"
#include "DiscIO/Volume.h"

namespace DiscIO
{
CFileSystemGCWii::CFileSystemGCWii(const IVolume *_rVolume)
	: IFileSystem(_rVolume)
	, m_Initialized(false)
	, m_Valid(false)
	, m_Wii(false)
{
	m_Valid = DetectFileSystem();
}

CFileSystemGCWii::~CFileSystemGCWii()
{
	m_FileInfoVector.clear();
}

u64 CFileSystemGCWii::GetFileSize(const std::string& _rFullPath)
{
	if (!m_Initialized)
		InitFileSystem();

	const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);

	if (pFileInfo != nullptr && !pFileInfo->IsDirectory())
		return pFileInfo->m_FileSize;

	return 0;
}

const std::string CFileSystemGCWii::GetFileName(u64 _Address)
{
	if (!m_Initialized)
		InitFileSystem();

	for (auto& fileInfo : m_FileInfoVector)
	{
		if ((fileInfo.m_Offset <= _Address) &&
		    ((fileInfo.m_Offset + fileInfo.m_FileSize) > _Address))
		{
			return fileInfo.m_FullPath;
		}
	}

	return "";
}

u64 CFileSystemGCWii::ReadFile(const std::string& _rFullPath, u8* _pBuffer, u64 _MaxBufferSize, u64 _OffsetInFile)
{
	if (!m_Initialized)
		InitFileSystem();

	const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
	if (pFileInfo == nullptr)
		return 0;

	if (_OffsetInFile >= pFileInfo->m_FileSize)
		return 0;

	u64 read_length = std::min(_MaxBufferSize, pFileInfo->m_FileSize - _OffsetInFile);

	DEBUG_LOG(DISCIO, "Reading %" PRIx64 " bytes at %" PRIx64 " from file %s. Offset: %" PRIx64 " Size: %" PRIx64,
	          read_length, _OffsetInFile, _rFullPath.c_str(), pFileInfo->m_Offset, pFileInfo->m_FileSize);

	m_rVolume->Read(pFileInfo->m_Offset + _OffsetInFile, read_length, _pBuffer, m_Wii);
	return read_length;
}

bool CFileSystemGCWii::ExportFile(const std::string& _rFullPath, const std::string& _rExportFilename)
{
	if (!m_Initialized)
		InitFileSystem();

	const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);

	if (!pFileInfo)
		return false;

	u64 remainingSize = pFileInfo->m_FileSize;
	u64 fileOffset = pFileInfo->m_Offset;

	File::IOFile f(_rExportFilename, "wb");
	if (!f)
		return false;

	bool result = true;

	while (remainingSize)
	{
		// Limit read size to 128 MB
		size_t readSize = (size_t)std::min(remainingSize, (u64)0x08000000);

		std::vector<u8> buffer(readSize);

		result = m_rVolume->Read(fileOffset, readSize, &buffer[0], m_Wii);

		if (!result)
			break;

		f.WriteBytes(&buffer[0], readSize);

		remainingSize -= readSize;
		fileOffset += readSize;
	}

	return result;
}

bool CFileSystemGCWii::ExportApploader(const std::string& _rExportFolder) const
{
	u32 apploader_size;
	u32 trailer_size;
	const u32 header_size = 0x20;
	if (!m_rVolume->ReadSwapped(0x2440 + 0x14, &apploader_size, m_Wii) ||
	    !m_rVolume->ReadSwapped(0x2440 + 0x18, &trailer_size, m_Wii))
	    return false;
	apploader_size += trailer_size + header_size;
	DEBUG_LOG(DISCIO, "Apploader size -> %x", apploader_size);

	std::vector<u8> buffer(apploader_size);
	if (m_rVolume->Read(0x2440, apploader_size, buffer.data(), m_Wii))
	{
		std::string exportName(_rExportFolder + "/apploader.img");

		File::IOFile AppFile(exportName, "wb");
		if (AppFile)
		{
			AppFile.WriteBytes(buffer.data(), apploader_size);
			return true;
		}
	}

	return false;
}

u64 CFileSystemGCWii::GetBootDOLOffset() const
{
	u32 offset = 0;
	m_rVolume->ReadSwapped(0x420, &offset, m_Wii);
	return static_cast<u64>(offset) << GetOffsetShift();
}

u32 CFileSystemGCWii::GetBootDOLSize(u64 dol_offset) const
{
	// The dol_offset value is usually obtained by calling GetBootDOLOffset.
	// If GetBootDOLOffset fails by returning 0, GetBootDOLSize should also fail.
	if (dol_offset == 0)
		return 0;

	u32 dol_size = 0;
	u32 offset = 0;
	u32 size = 0;

	// Iterate through the 7 code segments
	for (u8 i = 0; i < 7; i++)
	{
		if (!m_rVolume->ReadSwapped(dol_offset + 0x00 + i * 4, &offset, m_Wii) ||
		    !m_rVolume->ReadSwapped(dol_offset + 0x90 + i * 4, &size, m_Wii))
			return 0;
		dol_size = std::max(offset + size, dol_size);
	}

	// Iterate through the 11 data segments
	for (u8 i = 0; i < 11; i++)
	{
		if (!m_rVolume->ReadSwapped(dol_offset + 0x1c + i * 4, &offset, m_Wii) ||
		    !m_rVolume->ReadSwapped(dol_offset + 0xac + i * 4, &size, m_Wii))
			return 0;
		dol_size = std::max(offset + size, dol_size);
	}

	return dol_size;
}

bool CFileSystemGCWii::ExportDOL(const std::string& _rExportFolder) const
{
	u64 DolOffset = GetBootDOLOffset();
	u32 DolSize = GetBootDOLSize(DolOffset);

	if (DolOffset == 0 || DolSize == 0)
		return false;

	std::vector<u8> buffer(DolSize);
	if (m_rVolume->Read(DolOffset, DolSize, &buffer[0], m_Wii))
	{
		std::string exportName(_rExportFolder + "/boot.dol");

		File::IOFile DolFile(exportName, "wb");
		if (DolFile)
		{
			DolFile.WriteBytes(&buffer[0], DolSize);
			return true;
		}
	}

	return false;
}

std::string CFileSystemGCWii::GetStringFromOffset(u64 _Offset) const
{
	std::string data(255, 0x00);
	m_rVolume->Read(_Offset, data.size(), (u8*)&data[0], m_Wii);
	data.erase(std::find(data.begin(), data.end(), 0x00), data.end());

	// TODO: Should we really always use SHIFT-JIS?
	// It makes some filenames in Pikmin (NTSC-U) sane, but is it correct?
	return SHIFTJISToUTF8(data);
}

const std::vector<SFileInfo>& CFileSystemGCWii::GetFileList()
{
	if (!m_Initialized)
		InitFileSystem();

	return m_FileInfoVector;
}

const SFileInfo* CFileSystemGCWii::FindFileInfo(const std::string& _rFullPath)
{
	if (!m_Initialized)
		InitFileSystem();

	for (auto& fileInfo : m_FileInfoVector)
	{
		if (!strcasecmp(fileInfo.m_FullPath.c_str(), _rFullPath.c_str()))
			return &fileInfo;
	}

	return nullptr;
}

bool CFileSystemGCWii::DetectFileSystem()
{
	u32 magic_bytes;
	if (m_rVolume->ReadSwapped(0x18, &magic_bytes, false) && magic_bytes == 0x5D1C9EA3)
	{
		m_Wii = true;
		return true;
	}
	else if (m_rVolume->ReadSwapped(0x1c, &magic_bytes, false) && magic_bytes == 0xC2339F3D)
	{
		m_Wii = false;
		return true;
	}

	return false;
}

void CFileSystemGCWii::InitFileSystem()
{
	m_Initialized = true;
	u32 const shift = GetOffsetShift();

	// read the whole FST
	u32 fst_offset_unshifted;
	if (!m_rVolume->ReadSwapped(0x424, &fst_offset_unshifted, m_Wii))
		return;
	u64 FSTOffset = static_cast<u64>(fst_offset_unshifted) << shift;

	// read all fileinfos
	u32 name_offset, offset, size;
	if (!m_rVolume->ReadSwapped(FSTOffset + 0x0, &name_offset, m_Wii) ||
	    !m_rVolume->ReadSwapped(FSTOffset + 0x4, &offset, m_Wii) ||
	    !m_rVolume->ReadSwapped(FSTOffset + 0x8, &size, m_Wii))
		return;
	SFileInfo root = { name_offset, static_cast<u64>(offset) << shift, size };

	if (!root.IsDirectory())
		return;

	// 12 bytes (the size of a file entry) times 10 * 1024 * 1024 is 120 MiB,
	// more than total RAM in a Wii. No file system should use anywhere near that much.
	static const u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 10 * 1024 * 1024;
	if (root.m_FileSize > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT)
	{
		// Without this check, Dolphin can crash by trying to allocate too much
		// memory when loading the file systems of certain malformed disc images.

		ERROR_LOG(DISCIO, "File system is abnormally large! Aborting loading");
		return;
	}

	if (m_FileInfoVector.size())
		PanicAlert("Wtf?");
	u64 NameTableOffset = FSTOffset;

	m_FileInfoVector.reserve((size_t)root.m_FileSize);
	for (u32 i = 0; i < root.m_FileSize; i++)
	{
		const u64 read_offset = FSTOffset + (i * 0xC);
		name_offset = 0;
		m_rVolume->ReadSwapped(read_offset + 0x0, &name_offset, m_Wii);
		offset = 0;
		m_rVolume->ReadSwapped(read_offset + 0x4, &offset, m_Wii);
		size = 0;
		m_rVolume->ReadSwapped(read_offset + 0x8, &size, m_Wii);
		m_FileInfoVector.emplace_back(name_offset, static_cast<u64>(offset) << shift, size);
		NameTableOffset += 0xC;
	}

	BuildFilenames(1, m_FileInfoVector.size(), "", NameTableOffset);
}

size_t CFileSystemGCWii::BuildFilenames(const size_t _FirstIndex, const size_t _LastIndex, const std::string& _szDirectory, u64 _NameTableOffset)
{
	size_t CurrentIndex = _FirstIndex;

	while (CurrentIndex < _LastIndex)
	{
		SFileInfo& rFileInfo = m_FileInfoVector[CurrentIndex];
		u64 const uOffset = _NameTableOffset + (rFileInfo.m_NameOffset & 0xFFFFFF);
		std::string const offset_str { GetStringFromOffset(uOffset) };
		bool const is_dir = rFileInfo.IsDirectory();
		rFileInfo.m_FullPath.reserve(_szDirectory.size() + offset_str.size());

		rFileInfo.m_FullPath.append(_szDirectory.data(), _szDirectory.size())
		                    .append(offset_str.data(), offset_str.size())
		                    .append("/", size_t(is_dir));

		if (!is_dir)
		{
			++CurrentIndex;
			continue;
		}

		// check next index
		CurrentIndex = BuildFilenames(CurrentIndex + 1, (size_t) rFileInfo.m_FileSize, rFileInfo.m_FullPath, _NameTableOffset);
	}

	return CurrentIndex;
}

u32 CFileSystemGCWii::GetOffsetShift() const
{
	return m_Wii ? 2 : 0;
}

}  // namespace