/
BlockDevices.h
152 lines (122 loc) · 3.88 KB
/
BlockDevices.h
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
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#pragma once
// Abstractions around read-only blockdevices, such as PSP UMD discs.
// CISOFileBlockDevice implements compressed iso images, CISO format.
//
// The ISOFileSystemReader reads from a BlockDevice, so it automatically works
// with CISO images.
#include <mutex>
#include "Common/CommonTypes.h"
#include "Core/ELF/PBPReader.h"
class FileLoader;
class BlockDevice {
public:
BlockDevice(FileLoader *fileLoader) : fileLoader_(fileLoader) {}
virtual ~BlockDevice() {}
virtual bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) = 0;
virtual bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) {
for (int b = 0; b < count; ++b) {
if (!ReadBlock(minBlock + b, outPtr)) {
return false;
}
outPtr += GetBlockSize();
}
return true;
}
int GetBlockSize() const { return 2048;} // forced, it cannot be changed by subclasses
virtual u32 GetNumBlocks() = 0;
virtual bool IsDisc() = 0;
u32 CalculateCRC(volatile bool *cancel = nullptr);
void NotifyReadError();
protected:
FileLoader *fileLoader_;
bool reportedError_ = false;
};
class CISOFileBlockDevice : public BlockDevice {
public:
CISOFileBlockDevice(FileLoader *fileLoader);
~CISOFileBlockDevice();
bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;
bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) override;
u32 GetNumBlocks() override { return numBlocks; }
bool IsDisc() override { return true; }
private:
u32 *index;
u8 *readBuffer;
u8 *zlibBuffer;
u32 zlibBufferFrame;
u8 indexShift;
u8 blockShift;
u32 frameSize;
u32 numBlocks;
u32 numFrames;
int ver_;
};
class FileBlockDevice : public BlockDevice {
public:
FileBlockDevice(FileLoader *fileLoader);
~FileBlockDevice();
bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;
bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) override;
u32 GetNumBlocks() override {return (u32)(filesize_ / GetBlockSize());}
bool IsDisc() override { return true; }
private:
u64 filesize_;
};
// For encrypted ISOs in PBP files.
struct table_info {
u8 mac[16];
u32 offset;
int size;
int flag;
int unk_1c;
};
class NPDRMDemoBlockDevice : public BlockDevice {
public:
NPDRMDemoBlockDevice(FileLoader *fileLoader);
~NPDRMDemoBlockDevice();
bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;
u32 GetNumBlocks() override {return (u32)lbaSize;}
bool IsDisc() override { return false; }
private:
static std::mutex mutex_;
u32 lbaSize;
u32 psarOffset;
int blockSize;
int blockLBAs;
u32 numBlocks;
u8 vkey[16];
u8 hkey[16];
struct table_info *table;
int currentBlock;
u8 *blockBuf;
u8 *tempBuf;
};
struct CHDImpl;
class CHDFileBlockDevice : public BlockDevice {
public:
CHDFileBlockDevice(FileLoader *fileLoader);
~CHDFileBlockDevice();
bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;
bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) override;
u32 GetNumBlocks() override { return numBlocks; }
bool IsDisc() override { return true; }
private:
std::unique_ptr<CHDImpl> impl_;
u8 *readBuffer;
u32 currentHunk;
u32 blocksPerHunk;
u32 numBlocks;
};
BlockDevice *constructBlockDevice(FileLoader *fileLoader);