2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <algorithm>
|
2013-10-26 03:55:41 -06:00
|
|
|
#include <cinttypes>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
2014-08-31 07:34:58 -06:00
|
|
|
#include <memory>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-10-26 03:55:41 -06:00
|
|
|
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "DiscIO/DiscScrubber.h"
|
|
|
|
#include "DiscIO/Filesystem.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Volume.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "DiscIO/VolumeCreator.h"
|
|
|
|
|
2009-05-21 13:19:15 -06:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace DiscScrubber
|
|
|
|
{
|
|
|
|
|
2013-10-18 01:32:56 -06:00
|
|
|
#define CLUSTER_SIZE 0x8000
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2014-07-08 07:58:25 -06:00
|
|
|
static u8* m_FreeTable = nullptr;
|
|
|
|
static u64 m_FileSize;
|
|
|
|
static u64 m_BlockCount;
|
|
|
|
static u32 m_BlockSize;
|
|
|
|
static int m_BlocksPerCluster;
|
|
|
|
static bool m_isScrubbing = false;
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2014-07-08 07:58:25 -06:00
|
|
|
static std::string m_Filename;
|
|
|
|
static IVolume* m_Disc = nullptr;
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
struct SPartitionHeader
|
|
|
|
{
|
|
|
|
u8* Ticket[0x2a4];
|
|
|
|
u32 TMDSize;
|
|
|
|
u64 TMDOffset;
|
|
|
|
u32 CertChainSize;
|
|
|
|
u64 CertChainOffset;
|
2013-10-28 23:23:17 -06:00
|
|
|
// H3Size is always 0x18000
|
2009-05-21 13:19:15 -06:00
|
|
|
u64 H3Offset;
|
|
|
|
u64 DataOffset;
|
|
|
|
u64 DataSize;
|
|
|
|
// TMD would be here
|
|
|
|
u64 DOLOffset;
|
|
|
|
u64 DOLSize;
|
|
|
|
u64 FSTOffset;
|
|
|
|
u64 FSTSize;
|
|
|
|
u32 ApploaderSize;
|
|
|
|
u32 ApploaderTrailerSize;
|
|
|
|
};
|
|
|
|
struct SPartition
|
|
|
|
{
|
2009-05-21 18:36:44 -06:00
|
|
|
u32 GroupNumber;
|
2009-05-21 13:19:15 -06:00
|
|
|
u32 Number;
|
|
|
|
u64 Offset;
|
|
|
|
u32 Type;
|
|
|
|
SPartitionHeader Header;
|
|
|
|
};
|
|
|
|
struct SPartitionGroup
|
|
|
|
{
|
|
|
|
u32 numPartitions;
|
|
|
|
u64 PartitionsOffset;
|
|
|
|
std::vector<SPartition> PartitionsVec;
|
|
|
|
};
|
2014-07-08 07:58:25 -06:00
|
|
|
static SPartitionGroup PartitionGroup[4];
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
|
|
|
|
void MarkAsUsed(u64 _Offset, u64 _Size);
|
|
|
|
void MarkAsUsedE(u64 _PartitionDataOffset, u64 _Offset, u64 _Size);
|
2015-10-17 09:48:24 -06:00
|
|
|
void ReadFromVolume(u64 _Offset, u32& _Buffer, bool _Decrypt);
|
|
|
|
void ReadFromVolume(u64 _Offset, u64& _Buffer, bool _Decrypt);
|
2009-05-22 01:14:34 -06:00
|
|
|
bool ParseDisc();
|
|
|
|
bool ParsePartitionData(SPartition& _rPartition);
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool SetupScrub(const std::string& filename, int block_size)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
|
|
|
bool success = true;
|
2014-03-12 13:33:41 -06:00
|
|
|
m_Filename = filename;
|
2010-02-08 15:46:04 -07:00
|
|
|
m_BlockSize = block_size;
|
|
|
|
|
|
|
|
if (CLUSTER_SIZE % m_BlockSize != 0)
|
|
|
|
{
|
2013-03-31 17:10:21 -06:00
|
|
|
ERROR_LOG(DISCIO, "Block size %i is not a factor of 0x8000, scrubbing not possible", m_BlockSize);
|
2010-02-08 15:46:04 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_BlocksPerCluster = CLUSTER_SIZE / m_BlockSize;
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
m_Disc = CreateVolumeFromFilename(filename);
|
2015-08-10 11:11:29 -06:00
|
|
|
if (!m_Disc)
|
|
|
|
return false;
|
|
|
|
|
2009-05-21 13:19:15 -06:00
|
|
|
m_FileSize = m_Disc->GetSize();
|
|
|
|
|
2009-08-03 16:51:13 -06:00
|
|
|
u32 numClusters = (u32)(m_FileSize / CLUSTER_SIZE);
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
// Warn if not DVD5 or DVD9 size
|
|
|
|
if (numClusters != 0x23048 && numClusters != 0x46090)
|
2014-03-12 13:33:41 -06:00
|
|
|
WARN_LOG(DISCIO, "%s is not a standard sized Wii disc! (%x blocks)", filename.c_str(), numClusters);
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
// Table of free blocks
|
|
|
|
m_FreeTable = new u8[numClusters];
|
|
|
|
std::fill(m_FreeTable, m_FreeTable + numClusters, 1);
|
|
|
|
|
|
|
|
// Fill out table of free blocks
|
2009-11-18 12:16:59 -07:00
|
|
|
success = ParseDisc();
|
2009-05-21 13:19:15 -06:00
|
|
|
// Done with it; need it closed for the next part
|
|
|
|
delete m_Disc;
|
2014-03-09 14:14:26 -06:00
|
|
|
m_Disc = nullptr;
|
2010-02-08 15:46:04 -07:00
|
|
|
m_BlockCount = 0;
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2009-05-22 01:14:34 -06:00
|
|
|
// Let's not touch the file if we've failed up to here :p
|
|
|
|
if (!success)
|
2010-02-08 15:46:04 -07:00
|
|
|
Cleanup();
|
2009-05-22 01:14:34 -06:00
|
|
|
|
2010-02-08 15:46:04 -07:00
|
|
|
m_isScrubbing = success;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2014-11-27 09:57:49 -07:00
|
|
|
size_t GetNextBlock(File::IOFile& in, u8* buffer)
|
2010-02-08 15:46:04 -07:00
|
|
|
{
|
|
|
|
u64 CurrentOffset = m_BlockCount * m_BlockSize;
|
|
|
|
u64 i = CurrentOffset / CLUSTER_SIZE;
|
|
|
|
|
2014-11-27 09:57:49 -07:00
|
|
|
size_t ReadBytes = 0;
|
2010-02-08 15:46:04 -07:00
|
|
|
if (m_isScrubbing && m_FreeTable[i])
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2015-09-07 23:44:37 -06:00
|
|
|
DEBUG_LOG(DISCIO, "Freeing 0x%016" PRIx64, CurrentOffset);
|
2010-02-08 15:46:04 -07:00
|
|
|
std::fill(buffer, buffer + m_BlockSize, 0xFF);
|
2013-01-31 14:29:29 -07:00
|
|
|
in.Seek(m_BlockSize, SEEK_CUR);
|
2014-11-27 09:57:49 -07:00
|
|
|
ReadBytes = m_BlockSize;
|
2010-02-08 15:46:04 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-07 23:44:37 -06:00
|
|
|
DEBUG_LOG(DISCIO, "Used 0x%016" PRIx64, CurrentOffset);
|
2014-11-27 09:57:49 -07:00
|
|
|
in.ReadArray(buffer, m_BlockSize, &ReadBytes);
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
|
2010-02-08 15:46:04 -07:00
|
|
|
m_BlockCount++;
|
2014-11-27 09:57:49 -07:00
|
|
|
return ReadBytes;
|
2010-02-08 15:46:04 -07:00
|
|
|
}
|
2009-05-22 01:14:34 -06:00
|
|
|
|
2010-02-08 15:46:04 -07:00
|
|
|
void Cleanup()
|
|
|
|
{
|
|
|
|
if (m_FreeTable) delete[] m_FreeTable;
|
2014-03-09 14:14:26 -06:00
|
|
|
m_FreeTable = nullptr;
|
2010-02-08 15:46:04 -07:00
|
|
|
m_FileSize = 0;
|
|
|
|
m_BlockCount = 0;
|
|
|
|
m_BlockSize = 0;
|
|
|
|
m_BlocksPerCluster = 0;
|
|
|
|
m_isScrubbing = false;
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void MarkAsUsed(u64 _Offset, u64 _Size)
|
|
|
|
{
|
|
|
|
u64 CurrentOffset = _Offset;
|
|
|
|
u64 EndOffset = CurrentOffset + _Size;
|
|
|
|
|
2015-09-07 23:44:37 -06:00
|
|
|
DEBUG_LOG(DISCIO, "Marking 0x%016" PRIx64 " - 0x%016" PRIx64 " as used", _Offset, EndOffset);
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
while ((CurrentOffset < EndOffset) && (CurrentOffset < m_FileSize))
|
|
|
|
{
|
|
|
|
m_FreeTable[CurrentOffset / CLUSTER_SIZE] = 0;
|
|
|
|
CurrentOffset += CLUSTER_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Compensate for 0x400(SHA-1) per 0x8000(cluster)
|
|
|
|
void MarkAsUsedE(u64 _PartitionDataOffset, u64 _Offset, u64 _Size)
|
|
|
|
{
|
|
|
|
u64 Offset;
|
|
|
|
u64 Size;
|
|
|
|
|
|
|
|
Offset = _Offset / 0x7c00;
|
|
|
|
Offset = Offset * CLUSTER_SIZE;
|
|
|
|
Offset += _PartitionDataOffset;
|
|
|
|
|
|
|
|
Size = _Size / 0x7c00;
|
|
|
|
Size = (Size + 1) * CLUSTER_SIZE;
|
|
|
|
|
|
|
|
// Add on the offset in the first block for the case where data straddles blocks
|
|
|
|
Size += _Offset % 0x7c00;
|
|
|
|
|
|
|
|
MarkAsUsed(Offset, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions for reading the BE volume
|
2015-10-17 09:48:24 -06:00
|
|
|
void ReadFromVolume(u64 _Offset, u32& _Buffer, bool _Decrypt)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2015-10-17 09:48:24 -06:00
|
|
|
m_Disc->Read(_Offset, sizeof(u32), (u8*)&_Buffer, _Decrypt);
|
2009-05-21 13:19:15 -06:00
|
|
|
_Buffer = Common::swap32(_Buffer);
|
|
|
|
}
|
2015-10-17 09:48:24 -06:00
|
|
|
void ReadFromVolume(u64 _Offset, u64& _Buffer, bool _Decrypt)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2015-10-17 09:48:24 -06:00
|
|
|
m_Disc->Read(_Offset, sizeof(u32), (u8*)&_Buffer, _Decrypt);
|
2009-05-21 13:19:15 -06:00
|
|
|
_Buffer = Common::swap32((u32)_Buffer);
|
|
|
|
_Buffer <<= 2;
|
|
|
|
}
|
|
|
|
|
2009-05-22 01:14:34 -06:00
|
|
|
bool ParseDisc()
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
|
|
|
// Mark the header as used - it's mostly 0s anyways
|
|
|
|
MarkAsUsed(0, 0x50000);
|
|
|
|
|
|
|
|
for (int x = 0; x < 4; x++)
|
|
|
|
{
|
2015-10-17 09:48:24 -06:00
|
|
|
ReadFromVolume(0x40000 + (x * 8) + 0, PartitionGroup[x].numPartitions, false);
|
|
|
|
ReadFromVolume(0x40000 + (x * 8) + 4, PartitionGroup[x].PartitionsOffset, false);
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
// Read all partitions
|
|
|
|
for (u32 i = 0; i < PartitionGroup[x].numPartitions; i++)
|
|
|
|
{
|
|
|
|
SPartition Partition;
|
|
|
|
|
2009-05-21 18:36:44 -06:00
|
|
|
Partition.GroupNumber = x;
|
2009-05-21 13:19:15 -06:00
|
|
|
Partition.Number = i;
|
|
|
|
|
2015-10-17 09:48:24 -06:00
|
|
|
ReadFromVolume(PartitionGroup[x].PartitionsOffset + (i * 8) + 0, Partition.Offset, false);
|
|
|
|
ReadFromVolume(PartitionGroup[x].PartitionsOffset + (i * 8) + 4, Partition.Type, false);
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2015-10-17 09:48:24 -06:00
|
|
|
ReadFromVolume(Partition.Offset + 0x2a4, Partition.Header.TMDSize, false);
|
|
|
|
ReadFromVolume(Partition.Offset + 0x2a8, Partition.Header.TMDOffset, false);
|
|
|
|
ReadFromVolume(Partition.Offset + 0x2ac, Partition.Header.CertChainSize, false);
|
|
|
|
ReadFromVolume(Partition.Offset + 0x2b0, Partition.Header.CertChainOffset, false);
|
|
|
|
ReadFromVolume(Partition.Offset + 0x2b4, Partition.Header.H3Offset, false);
|
|
|
|
ReadFromVolume(Partition.Offset + 0x2b8, Partition.Header.DataOffset, false);
|
|
|
|
ReadFromVolume(Partition.Offset + 0x2bc, Partition.Header.DataSize, false);
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
PartitionGroup[x].PartitionsVec.push_back(Partition);
|
|
|
|
}
|
|
|
|
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& rPartition : PartitionGroup[x].PartitionsVec)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2014-02-16 21:51:41 -07:00
|
|
|
const SPartitionHeader& rHeader = rPartition.Header;
|
2009-05-21 13:19:15 -06:00
|
|
|
|
|
|
|
MarkAsUsed(rPartition.Offset, 0x2c0);
|
|
|
|
|
|
|
|
MarkAsUsed(rPartition.Offset + rHeader.TMDOffset, rHeader.TMDSize);
|
|
|
|
MarkAsUsed(rPartition.Offset + rHeader.CertChainOffset, rHeader.CertChainSize);
|
|
|
|
MarkAsUsed(rPartition.Offset + rHeader.H3Offset, 0x18000);
|
|
|
|
// This would mark the whole (encrypted) data area
|
|
|
|
// we need to parse FST and other crap to find what's free within it!
|
|
|
|
//MarkAsUsed(rPartition.Offset + rHeader.DataOffset, rHeader.DataSize);
|
|
|
|
|
|
|
|
// Parse Data! This is where the big gain is
|
2009-05-22 01:14:34 -06:00
|
|
|
if (!ParsePartitionData(rPartition))
|
|
|
|
return false;
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
}
|
2009-05-22 01:14:34 -06:00
|
|
|
|
|
|
|
return true;
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Operations dealing with encrypted space are done here - the volume is swapped to allow this
|
2009-05-22 01:14:34 -06:00
|
|
|
bool ParsePartitionData(SPartition& _rPartition)
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2009-05-22 01:14:34 -06:00
|
|
|
bool ParsedOK = true;
|
|
|
|
|
2009-05-21 13:19:15 -06:00
|
|
|
// Switch out the main volume temporarily
|
|
|
|
IVolume *OldVolume = m_Disc;
|
|
|
|
|
|
|
|
// Ready some stuff
|
2014-02-12 08:00:34 -07:00
|
|
|
m_Disc = CreateVolumeFromFilename(m_Filename, _rPartition.GroupNumber, _rPartition.Number);
|
2015-07-02 00:43:18 -06:00
|
|
|
if (m_Disc == nullptr)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Failed to create volume from file %s", m_Filename.c_str());
|
|
|
|
m_Disc = OldVolume;
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-21 13:19:15 -06:00
|
|
|
|
2015-07-02 00:43:18 -06:00
|
|
|
std::unique_ptr<IFileSystem> filesystem(CreateFileSystem(m_Disc));
|
2014-08-31 07:34:58 -06:00
|
|
|
if (!filesystem)
|
2009-05-22 01:14:34 -06:00
|
|
|
{
|
2012-08-20 05:12:49 -06:00
|
|
|
ERROR_LOG(DISCIO, "Failed to create filesystem for group %d partition %u", _rPartition.GroupNumber, _rPartition.Number);
|
2009-05-22 01:14:34 -06:00
|
|
|
ParsedOK = false;
|
|
|
|
}
|
|
|
|
else
|
2009-05-21 13:19:15 -06:00
|
|
|
{
|
2009-05-22 01:14:34 -06:00
|
|
|
// Mark things as used which are not in the filesystem
|
|
|
|
// Header, Header Information, Apploader
|
2015-10-17 09:48:24 -06:00
|
|
|
ReadFromVolume(0x2440 + 0x14, _rPartition.Header.ApploaderSize, true);
|
|
|
|
ReadFromVolume(0x2440 + 0x18, _rPartition.Header.ApploaderTrailerSize, true);
|
2009-05-22 01:14:34 -06:00
|
|
|
MarkAsUsedE(_rPartition.Offset
|
2009-05-21 13:19:15 -06:00
|
|
|
+ _rPartition.Header.DataOffset
|
2009-05-22 01:14:34 -06:00
|
|
|
, 0
|
|
|
|
, 0x2440
|
|
|
|
+ _rPartition.Header.ApploaderSize
|
|
|
|
+ _rPartition.Header.ApploaderTrailerSize);
|
|
|
|
|
|
|
|
// DOL
|
2015-10-17 09:48:24 -06:00
|
|
|
ReadFromVolume(0x420, _rPartition.Header.DOLOffset, true);
|
2015-10-17 12:52:26 -06:00
|
|
|
_rPartition.Header.DOLSize = filesystem->GetBootDOLSize(_rPartition.Header.DOLOffset);
|
2009-05-22 01:14:34 -06:00
|
|
|
MarkAsUsedE(_rPartition.Offset
|
2009-05-21 13:19:15 -06:00
|
|
|
+ _rPartition.Header.DataOffset
|
2009-05-22 01:14:34 -06:00
|
|
|
, _rPartition.Header.DOLOffset
|
|
|
|
, _rPartition.Header.DOLSize);
|
|
|
|
|
|
|
|
// FST
|
2015-10-17 09:48:24 -06:00
|
|
|
ReadFromVolume(0x424, _rPartition.Header.FSTOffset, true);
|
|
|
|
ReadFromVolume(0x428, _rPartition.Header.FSTSize, true);
|
2009-05-22 01:14:34 -06:00
|
|
|
MarkAsUsedE(_rPartition.Offset
|
|
|
|
+ _rPartition.Header.DataOffset
|
|
|
|
, _rPartition.Header.FSTOffset
|
|
|
|
, _rPartition.Header.FSTSize);
|
|
|
|
|
|
|
|
// Go through the filesystem and mark entries as used
|
2015-04-28 04:48:05 -06:00
|
|
|
for (SFileInfo file : filesystem->GetFileList())
|
2009-05-22 01:14:34 -06:00
|
|
|
{
|
2015-06-14 00:49:35 -06:00
|
|
|
DEBUG_LOG(DISCIO, "%s", file.m_FullPath.empty() ? "/" : file.m_FullPath.c_str());
|
2009-05-22 01:14:34 -06:00
|
|
|
// Just 1byte for directory? - it will end up reserving a cluster this way
|
2015-04-28 04:48:05 -06:00
|
|
|
if (file.m_NameOffset & 0x1000000)
|
|
|
|
MarkAsUsedE(_rPartition.Offset + _rPartition.Header.DataOffset, file.m_Offset, 1);
|
2009-05-22 01:14:34 -06:00
|
|
|
else
|
2015-04-28 04:48:05 -06:00
|
|
|
MarkAsUsedE(_rPartition.Offset + _rPartition.Header.DataOffset, file.m_Offset, file.m_FileSize);
|
2009-05-22 01:14:34 -06:00
|
|
|
}
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Swap back
|
|
|
|
delete m_Disc;
|
|
|
|
m_Disc = OldVolume;
|
2009-05-22 01:14:34 -06:00
|
|
|
|
|
|
|
return ParsedOK;
|
2009-05-21 13:19:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace DiscScrubber
|
|
|
|
|
|
|
|
} // namespace DiscIO
|