CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileSystems/BlockDevices.h
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
// Abstractions around read-only blockdevices, such as PSP UMD discs.
21
// CISOFileBlockDevice implements compressed iso images, CISO format.
22
//
23
// The ISOFileSystemReader reads from a BlockDevice, so it automatically works
24
// with CISO images.
25
26
#include <mutex>
27
28
#include "Common/CommonTypes.h"
29
#include "Core/ELF/PBPReader.h"
30
31
class FileLoader;
32
33
class BlockDevice {
34
public:
35
BlockDevice(FileLoader *fileLoader) : fileLoader_(fileLoader) {}
36
virtual ~BlockDevice() {}
37
virtual bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) = 0;
38
virtual bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) {
39
for (int b = 0; b < count; ++b) {
40
if (!ReadBlock(minBlock + b, outPtr)) {
41
return false;
42
}
43
outPtr += GetBlockSize();
44
}
45
return true;
46
}
47
int GetBlockSize() const { return 2048;} // forced, it cannot be changed by subclasses
48
virtual u32 GetNumBlocks() const = 0;
49
virtual u64 GetUncompressedSize() const {
50
return (u64)GetNumBlocks() * (u64)GetBlockSize();
51
}
52
virtual bool IsDisc() const = 0;
53
54
void NotifyReadError();
55
56
protected:
57
FileLoader *fileLoader_;
58
bool reportedError_ = false;
59
};
60
61
class CISOFileBlockDevice : public BlockDevice {
62
public:
63
CISOFileBlockDevice(FileLoader *fileLoader);
64
~CISOFileBlockDevice();
65
bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;
66
bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) override;
67
u32 GetNumBlocks() const override { return numBlocks; }
68
bool IsDisc() const override { return true; }
69
70
private:
71
u32 *index = nullptr;
72
u8 *readBuffer = nullptr;
73
u8 *zlibBuffer = nullptr;
74
u32 zlibBufferFrame = 0;
75
u8 indexShift = 0;
76
u8 blockShift = 0;
77
u32 frameSize = 0;
78
u32 numBlocks = 0;
79
u32 numFrames = 0;
80
int ver_ = 0;
81
};
82
83
84
class FileBlockDevice : public BlockDevice {
85
public:
86
FileBlockDevice(FileLoader *fileLoader);
87
~FileBlockDevice();
88
bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;
89
bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) override;
90
u32 GetNumBlocks() const override {return (u32)(filesize_ / GetBlockSize());}
91
bool IsDisc() const override { return true; }
92
u64 GetUncompressedSize() const override {
93
return filesize_;
94
}
95
private:
96
u64 filesize_;
97
};
98
99
100
// For encrypted ISOs in PBP files.
101
102
struct table_info {
103
u8 mac[16];
104
u32 offset;
105
int size;
106
int flag;
107
int unk_1c;
108
};
109
110
class NPDRMDemoBlockDevice : public BlockDevice {
111
public:
112
NPDRMDemoBlockDevice(FileLoader *fileLoader);
113
~NPDRMDemoBlockDevice();
114
115
bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;
116
u32 GetNumBlocks() const override {return (u32)lbaSize;}
117
bool IsDisc() const override { return false; }
118
119
private:
120
static std::mutex mutex_;
121
u32 lbaSize;
122
123
u32 psarOffset;
124
int blockSize;
125
int blockLBAs;
126
u32 numBlocks;
127
128
u8 vkey[16];
129
u8 hkey[16];
130
struct table_info *table;
131
132
int currentBlock;
133
u8 *blockBuf;
134
u8 *tempBuf;
135
};
136
137
struct CHDImpl;
138
139
struct ExtendedCoreFile;
140
141
class CHDFileBlockDevice : public BlockDevice {
142
public:
143
CHDFileBlockDevice(FileLoader *fileLoader);
144
~CHDFileBlockDevice();
145
bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;
146
bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) override;
147
u32 GetNumBlocks() const override { return numBlocks; }
148
bool IsDisc() const override { return true; }
149
private:
150
struct ExtendedCoreFile *core_file_ = nullptr;
151
std::unique_ptr<CHDImpl> impl_;
152
u8 *readBuffer = nullptr;
153
u32 currentHunk = 0;
154
u32 blocksPerHunk = 0;
155
u32 numBlocks = 0;
156
};
157
158
BlockDevice *constructBlockDevice(FileLoader *fileLoader);
159
160