-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPT.cpp
248 lines (203 loc) · 9.16 KB
/
GPT.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#pragma once
#include "GPT.h"
#include "kfs.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
namespace GPT {
PartionsInfo** AllPartitionsInfo;
uint64_t AllPartitionsInfoNumber;
char* Buffer = new char[0x1000];
GPTHeader* GetGPTHeader() {
memset(Buffer, 0, sizeof(GPTHeader));
std::ifstream infile(filePath);
infile.seekg(SizeofLBASector);
infile.read(Buffer, SizeofLBASector);
GPTHeader* ReturnValue = (GPTHeader*)malloc(sizeof(GPTHeader));
memcpy(ReturnValue, Buffer, sizeof(GPTHeader));
infile.close();
return ReturnValue;
}
GUIDPartitionEntryFormat* GetGUIDPartitionEntryFormat(uint64_t LBAAddress, uint8_t which) {
uint8_t MaxGUIDPartitionEntryFormatPerSectors = SizeofLBASector / sizeof(GUIDPartitionEntryFormat);
memset(Buffer, 0, SizeofLBASector);
void* buffer = malloc(sizeof(GUIDPartitionEntryFormat));
memset(buffer, 0, sizeof(GUIDPartitionEntryFormat));
std::ifstream infile(filePath);
infile.seekg(LBAAddress * SizeofLBASector);
if (infile.read(Buffer, SizeofLBASector)) {
infile.seekg(0);
infile.close();
memcpy(buffer, (Buffer + (which * sizeof(GUIDPartitionEntryFormat))), sizeof(GUIDPartitionEntryFormat));
GUIDPartitionEntryFormat* ReturnValue = (GUIDPartitionEntryFormat*)buffer;
return ReturnValue;
}
else {
infile.close();
free(buffer);
return NULL;
}
}
uint64_t GetFirstFreeLBA() {
GPTHeader* gptHeader = GetGPTHeader();
Partitions* partitions = GetAllPartitions();
uint64_t MaxLastUsedLBA = 0;
if (partitions->NumberPartitionsCreated == 0) {
return gptHeader->FirstUsableLBAPartitions;
}
for (int i = 0; i < partitions->NumberPartitionsCreated; i++) {
if (partitions->AllParitions[i]->LastLBA > MaxLastUsedLBA) {
MaxLastUsedLBA = partitions->AllParitions[i]->LastLBA;
}
}
return MaxLastUsedLBA + 1; //because the last is used so we need to return the free
}
GUIDPartitionEntryFormat* GetPartitionByGUID(GUID* guid) {
Partitions* AllPartitions = GetAllPartitions();
for (int i = 0; i < AllPartitions->NumberPartitionsCreated; i++) {
if (AllPartitions->AllParitions[i]->PartitionTypeGUID.Data1 == guid->Data1 &&
AllPartitions->AllParitions[i]->PartitionTypeGUID.Data2 == guid->Data2 &&
AllPartitions->AllParitions[i]->PartitionTypeGUID.Data3 == guid->Data3 &&
AllPartitions->AllParitions[i]->PartitionTypeGUID.Data4 == guid->Data4) {
return AllPartitions->AllParitions[i];
}
}
return NULL;
}
Partitions* GetAllPartitions() {
GPTHeader* gptHeader = GetGPTHeader();
Partitions* ReturnValue = (Partitions*)malloc(sizeof(Partitions));
memset(ReturnValue, 0, sizeof(Partitions));
ReturnValue->IsPartitionsEntryBitmapFree = BitmapHeap(gptHeader->NumberPartitionEntries);
uint8_t MaxGUIDPartitionEntryFormatPerSectors = SizeofLBASector / sizeof(GUIDPartitionEntryFormat);
uint64_t PartitionEntriesStartingLBA = gptHeader->PartitionEntriesStartingLBA;
GUIDPartitionEntryFormat* CheckEntry = (GUIDPartitionEntryFormat*)malloc(sizeof(GUIDPartitionEntryFormat));;
for (int i = 0; i < gptHeader->NumberPartitionEntries; i++) {
CheckEntry = (GUIDPartitionEntryFormat*)malloc(sizeof(GUIDPartitionEntryFormat));
CheckEntry = GetGUIDPartitionEntryFormat(PartitionEntriesStartingLBA + (i / MaxGUIDPartitionEntryFormatPerSectors), i % MaxGUIDPartitionEntryFormatPerSectors);
if (CheckEntry->FirstLBA != 0) {
ReturnValue->AllParitions[ReturnValue->NumberPartitionsCreated] = CheckEntry;
ReturnValue->NumberPartitionsCreated++;
ReturnValue->IsPartitionsEntryBitmapFree.Set(i, false);
}
else {
free(CheckEntry);
ReturnValue->IsPartitionsEntryBitmapFree.Set(i, true);
}
}
return ReturnValue;
}
uint64_t GetFreeSizePatition() {
GPTHeader* gptHeader = GetGPTHeader();
Partitions* partitions = GetAllPartitions();
uint64_t UsedLBASectors = 0;
uint64_t TotalUsableLBASectors = gptHeader->LastUsableLBAPartitions - gptHeader->FirstUsableLBAPartitions;
for (int i = 0; i < partitions->NumberPartitionsCreated; i++) {
//the problem come from here
UsedLBASectors += partitions->AllParitions[i]->LastLBA - partitions->AllParitions[i]->FirstLBA;
}
uint64_t freeSizeSectors = TotalUsableLBASectors - UsedLBASectors;
uint64_t freeSize = (freeSizeSectors * SizeofLBASector) - 1;
return freeSize;
}
GUID* GetReservedGUIDPartitionType() {
GUID* KotReservedGUID = (GUID*)malloc(sizeof(GUID));
KotReservedGUID->Data1 = 0x47A1ACC0;
KotReservedGUID->Data2 = 0x3B40;
KotReservedGUID->Data3 = 0x2A53;
KotReservedGUID->Data4 = 0xF38D3D321F6D;
return KotReservedGUID;
}
GUID* GetDataGUIDPartitionType() {
GUID* KotReservedGUID = (GUID*)malloc(sizeof(GUID));
KotReservedGUID->Data1 = 0x64617461;
KotReservedGUID->Data2 = 0x3B40;
KotReservedGUID->Data3 = 0x2A53;
KotReservedGUID->Data4 = 0xF38D3D321F6D;
return KotReservedGUID;
}
GUID* GetSystemGUIDPartitionType() {
GUID* KotReservedGUID = (GUID*)malloc(sizeof(GUID));
KotReservedGUID->Data1 = 0xC12A7328;
KotReservedGUID->Data2 = 0xF81F;
KotReservedGUID->Data3 = 0x11D2;
KotReservedGUID->Data4 = 0x00A0C93EC93B;
return KotReservedGUID;
}
void AssignNamePartitonsGUID() {
bool systemPartInit = false;
uint64_t PatitionNumber = 0;
for (int i = 0; i < AllPartitionsInfoNumber; i++) {
if (AllPartitionsInfo[i]->Partition->Flags == 7) { //flags disk system
AllPartitionsInfo[i]->PatitionNumber = 0;
systemPartInit = true;
}
else if (AllPartitionsInfo[i]->Partition->Flags == 5) { //flags to mount the disk
PatitionNumber++;
AllPartitionsInfo[i]->PatitionNumber = PatitionNumber;
}
}
}
/* Partition class */
Partition::Partition(GUIDPartitionEntryFormat* partition) {
this->partition = partition;
}
void Partition::Read(uint64_t firstByte, uint64_t size, void* buffer) {
uint64_t LBAFirstSector = this->partition->FirstLBA + (firstByte / SizeofLBASector);
memset(buffer, 0, size);
uint64_t sizeRead = 0;
uint64_t sizeToRead = 0;
uint64_t sectorsToRead = 0;
uint64_t sectorsRead = 0;
std::ifstream infile(filePath, std::ios::binary | std::ios::out);
for (int i = 0; i < Divide(size, 0x1000); i++) {
sizeToRead = size - sizeRead;
if (sizeToRead > 0x1000) {
sizeToRead = 0x1000;
}
sectorsToRead = Divide(sizeToRead, SizeofLBASector);
infile.seekg((LBAFirstSector + sectorsRead) * SizeofLBASector);
infile.read(Buffer, sectorsToRead * SizeofLBASector);
infile.seekg(0);
if (sizeRead != 0) {
memcpy((void*)((uint64_t)buffer + sizeRead), Buffer, sizeToRead);
}
else {
memcpy((void*)((uint64_t)buffer + sizeRead), (void*)((uint64_t)Buffer + firstByte % SizeofLBASector), sizeToRead); //Get the correct first byte
}
sizeRead += sizeToRead;
sectorsRead += sectorsToRead;
}
infile.close();
}
void Partition::Write(uint64_t firstByte, uint64_t size, void* buffer) {
uint64_t LBAFirstSector = (firstByte / SizeofLBASector) + this->partition->FirstLBA;
uint64_t sizeWrite = 0;
uint64_t sizeToWrite = 0;
uint64_t sectorsToWrite = 0;
uint64_t sectorsWrite = 0;
std::fstream file(filePath, std::ios::binary | std::ios::out | std::ios::in);
for (int i = 0; i < Divide(size, 0x1000); i++) {
sizeToWrite = size - sizeWrite;
if (sizeToWrite > 0x1000) {
sizeToWrite = 0x1000;
}
sectorsToWrite = Divide(sizeToWrite, SizeofLBASector);
file.seekg((LBAFirstSector + sectorsWrite) * SizeofLBASector);
file.read(Buffer, sectorsToWrite * SizeofLBASector);
file.seekg(0);
if (sizeWrite != 0) {
memcpy(Buffer, (void*)((uint64_t)buffer + sizeWrite), sizeToWrite);
}
else {
memcpy((void*)((uint64_t)Buffer + firstByte % SizeofLBASector), (void*)((uint64_t)buffer + sizeWrite), sizeToWrite);
}
file.seekp((LBAFirstSector + sectorsWrite) * SizeofLBASector);
file.write(Buffer, sectorsToWrite * SizeofLBASector);
file.seekp(0);
sizeWrite += sizeToWrite;
sectorsWrite += sectorsToWrite;
}
file.close();
}
}