summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/DiscScrubber.h
blob: 152e16f1235b8af28836069e47381d8ea82513c3 (plain)
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
// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

// DiscScrubber removes the garbage data from discs (currently Wii only) which
// is on the disc due to encryption

// It could be adapted to GameCube discs, but the gain is most likely negligible,
// and having 1:1 backups of discs is always nice when they are reasonably sized

// Note: the technique is inspired by Wiiscrubber, but much simpler - intentionally :)

#pragma once

#include <array>
#include <memory>
#include <string>
#include <vector>
#include "Common/CommonTypes.h"

namespace File
{
class IOFile;
}

namespace DiscIO
{
class IVolume;

class DiscScrubber final
{
public:
  DiscScrubber();
  ~DiscScrubber();

  bool SetupScrub(const std::string& filename, int block_size);
  size_t GetNextBlock(File::IOFile& in, u8* buffer);

private:
  struct PartitionHeader final
  {
    u8* ticket[0x2a4];
    u32 tmd_size;
    u64 tmd_offset;
    u32 cert_chain_size;
    u64 cert_chain_offset;
    // H3Size is always 0x18000
    u64 h3_offset;
    u64 data_offset;
    u64 data_size;
    // TMD would be here
    u64 dol_offset;
    u64 dol_size;
    u64 fst_offset;
    u64 fst_size;
    u32 apploader_size;
    u32 apploader_trailer_size;
  };

  struct Partition final
  {
    u32 group_number;
    u32 number;
    u64 offset;
    u32 type;
    PartitionHeader header;
  };

  struct PartitionGroup final
  {
    u32 num_partitions;
    u64 partitions_offset;
    std::vector<Partition> partitions;
  };

  void MarkAsUsed(u64 offset, u64 size);
  void MarkAsUsedE(u64 partition_data_offset, u64 offset, u64 size);
  bool ReadFromVolume(u64 offset, u32& buffer, bool decrypt);
  bool ReadFromVolume(u64 offset, u64& buffer, bool decrypt);
  bool ParseDisc();
  bool ParsePartitionData(Partition& partition);

  std::string m_filename;
  std::unique_ptr<IVolume> m_disc;

  std::array<PartitionGroup, 4> m_partition_group{};

  std::vector<u8> m_free_table;
  u64 m_file_size = 0;
  u64 m_block_count = 0;
  u32 m_block_size = 0;
  bool m_is_scrubbing = false;
};

}  // namespace DiscIO