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