-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPT.c
149 lines (124 loc) · 3.69 KB
/
GPT.c
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
// Partition 정보들을 담을 구조체 선언
struct Partition
{
char TypeGUID[16];
char UniqGUID[16];
char FirstLBA[8];
char LastLBA[8];
char attributeFlags[8];
char PartitionName[72];
};
// 주소 계산을 위해 byte 배열을 64bit int로 변환해주는 함수 구현
int64_t bytesToInt64(const char *bytes)
{
int64_t result = 0;
for (int i = 0; i < 8; ++i)
{
result |= ((int64_t)(bytes[i] & 0xFF)) << (8 * i);
}
return result;
}
// 엔디안 변환함수
void swapEndianness(void *data, int size)
{
unsigned char *ptr = (unsigned char *)data;
int i, j;
unsigned char temp;
for (i = 0, j = size - 1; i < j; i++, j--)
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
// GPT 파싱 함수
void extract_gpt_info(const char *image_path)
{
FILE *f = fopen(image_path, "rb");
if (f == NULL)
{
printf("파일을 찾을 수 없습니다.\n");
return;
}
// 파티션으로 분기
fseek(f, 1024, SEEK_SET);
// 최대 파티션 개수만큼 돌기
for (int cnt = 1 ; cnt < 129 ; cnt++)
{
printf("Partition %d\n",cnt);
unsigned char partition[128];
fread(partition, sizeof(unsigned char), 128, f);
struct Partition *ptr = (struct Partition *)(partition);
// 파티션이 더이상 없으면 종료
if(ptr->TypeGUID[0] == 0x00)
{
printf("Partition %d doesn't exists..\n",cnt);
printf("End of Program..\n");
break;
}
// 주소 연산을 위해 64bit 정수 변수 정의
int64_t firstLBA_integer = bytesToInt64(ptr->FirstLBA);
int64_t lastLBA_integer = bytesToInt64(ptr->LastLBA);
// 각각의 정보 출력
printf("TypeGUID\n>> ");
for (int i = 0; i < 16; i++)
{
printf("%02x ", (unsigned char)ptr->TypeGUID[i]);
}
printf("\n");
printf("UniqGUID\n>> ");
for (int i = 0; i < 16; i++)
{
printf("%02x ", (unsigned char)ptr->UniqGUID[i]);
}
printf("\n");
printf("FileType\n>> ");
unsigned char filetype[5];
fseek(f,(unsigned char)firstLBA_integer * 512 + 3 , SEEK_SET);
fread(filetype, sizeof(unsigned char), 4, f);
filetype[4] = '\0';
printf("%s",filetype);
printf("\n");
printf("FirstLBA\n>> ");
for (int i = 0; i < 8; i++)
{
printf("%02x ", (unsigned char)ptr->FirstLBA[i]);
}
printf("\n");
printf("LastLBA\n>> ");
for (int i = 0; i < 8; i++)
{
printf("%02x ", (unsigned char)ptr->LastLBA[i]);
}
printf("\n");
swapEndianness(ptr->FirstLBA,sizeof(ptr->FirstLBA));
swapEndianness(ptr->LastLBA,sizeof(ptr->LastLBA));
printf("FirstLBA\n>> ");
for (int i = 0; i < 8; i++)
{
printf("%02x ", (unsigned char)ptr->FirstLBA[i]);
}
printf("\n");
printf("LastLBA\n>> ");
for (int i = 0; i < 8; i++)
{
printf("%02x ", (unsigned char)ptr->LastLBA[i]);
}
printf("\n");
printf("Size\n>> %ld\n\n", (lastLBA_integer - firstLBA_integer + 1) * 512);
// filetype을 파싱하기 위해 움직였던 파일 읽기 포인터를 다은 파티션을 가리키게
fseek(f, 1024 + 128 * cnt, SEEK_SET);
}
printf("\n");
fclose(f);
}
int main()
{
// 이미지 파일 경로를 지정하여 함수를 호출
const char *image_path = "gpt_128.dd";
extract_gpt_info(image_path);
return 0;
}