forked from ehem/lg-v20-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpt.h
142 lines (116 loc) · 4.03 KB
/
gpt.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
/* **********************************************************************
* Copyright (C) 2016-2018 Elliott Mitchell <[email protected]> *
* *
* 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, either version 3 of *
* the License, or (at your option) any later version. *
* *
* 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 for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
************************************************************************/
#ifndef __GPT_H__
#define __GPT_H__
#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS==64
#define GPT_OFF_T off_t
#elif defined(_LARGEFILE64_SOURCE)
#define GPT_OFF_T off64_t
#else
#error "gpt.h needs either _FILE_OFFSET_BITS=64 or _LARGEFILE64_SOURCE defined!"
/* suppress other warnings/errors from compiler */
#define GPT_OFF_T long int
#endif
#include <stdint.h>
#include <uuid/uuid.h>
#ifdef ANDROID
typedef unsigned short char16_t;
#else
#include <uchar.h>
#endif
#include <stdbool.h>
/* Values from the GPT header */
/* NOTE: All of these values are little-endian on storage media! */
struct gpt_header {
uint64_t magic; // "EFI PART"
uint16_t minor, major; // major=1, minor=0 at this time
uint32_t headerSize; // 0x5C is minimum
uint32_t headerCRC32;
uint32_t reserved;
uint64_t myLBA;
uint64_t altLBA;
uint64_t dataStartLBA;
uint64_t dataEndLBA;
uuid_t diskUuid;
uint64_t entryStart;
uint32_t entryCount;
uint32_t entrySize;
uint32_t entryCRC32;
// uint8_t reserved2[]; // headerSize - 0x5C bytes
};
/* Entries for the storage slices */
struct gpt_entry {
uuid_t type;
uuid_t id;
uint64_t startLBA;
uint64_t endLBA;
uint64_t flags;
char name[109]; /* UTF-8 uses at most 3 bytes for every 2 of UTF-16 */
};
/* structure holding all the information from the GPT */
struct gpt_data {
struct gpt_header head;
GPT_OFF_T blocksz;
struct gpt_entry entry[];
};
/* NOTE: All of these values are little-endian */
struct _gpt_entry {
uuid_t type;
uuid_t id;
uint64_t startLBA;
uint64_t endLBA;
uint64_t flags;
char16_t name[36];
};
/* temporary structure which can overlay gpt_data */
struct _gpt_data {
struct gpt_header head;
GPT_OFF_T blocksz;
struct _gpt_entry entry[];
};
/* magic number in the GPT header */
extern const union _gpt_magic {
char ch[8];
uint64_t num;
} gpt_magic;
/* This is without the tail padding inserted by most compilers... */
#define GPT_SIZE 0x5C
/* used for telling loadgpt() which type to look for */
enum gpt_type {GPT_NONE, GPT_ANY, GPT_PRIMARY, GPT_BACKUP};
/* load data from a GPT */
extern struct gpt_data *readgpt(int fd, enum gpt_type);
/* load GPT data from somewhere */
/* NOTE: gptpreadfunc() MUST interpret negative numbers as being relative
* to the end of file/device/buffer */
typedef ssize_t (*gptpreadfunc)(void *opaque, void *buf, size_t count, GPT_OFF_T offset);
extern struct gpt_data *readgptb(gptpreadfunc, void *opaque, uint32_t blocksz,
enum gpt_type);
/* convert the entries into storage-media format */
extern bool gpt_entries2raw(struct _gpt_data *dst, const struct gpt_data *gpt);
/* compare two in-memory GPTs (ignores data modified during write) */
extern bool comparegpt(const struct gpt_data *, const struct gpt_data *);
/* write the given host format GPT to storage media */
extern bool writegpt(int fd, const struct gpt_data *gpt);
/* write the boot area */
extern bool writegptboot(int fd, const struct gpt_data *gpt);
/* WARNING: _writegpt() and __writegpt WILL modify the data */
/* write the given GPT to storage media, where header is still in host fmt */
extern bool _writegpt(int fd, struct _gpt_data *gpt);
/* don't contaiminate others' namespace */
#undef GPT_OFF_T
#endif