]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Core/ventoy_define.h
switch to en_US when use text mode.
[Ventoy.git] / LinuxGUI / Ventoy2Disk / Core / ventoy_define.h
1 /******************************************************************************
2 * ventoy_define.h
3 *
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 #ifndef __VENTOY_DEFINE_H__
21 #define __VENTOY_DEFINE_H__
22
23 #define MAX_DISK_NUM 256
24
25 #define SIZE_1MB 1048576
26 #define SIZE_1GB 1073741824
27
28 #define VTOYIMG_PART_START_BYTES (1024 * 1024)
29 #define VTOYIMG_PART_START_SECTOR 2048
30
31 #define VTOYEFI_PART_BYTES (32 * 1024 * 1024)
32 #define VTOYEFI_PART_SECTORS 65536
33
34 #pragma pack(1)
35
36 typedef struct vtoy_guid
37 {
38 uint32_t data1;
39 uint16_t data2;
40 uint16_t data3;
41 uint8_t data4[8];
42 }vtoy_guid;
43
44 typedef struct PART_TABLE
45 {
46 uint8_t Active; // 0x00 0x80
47
48 uint8_t StartHead;
49 uint16_t StartSector : 6;
50 uint16_t StartCylinder : 10;
51
52 uint8_t FsFlag;
53
54 uint8_t EndHead;
55 uint16_t EndSector : 6;
56 uint16_t EndCylinder : 10;
57
58 uint32_t StartSectorId;
59 uint32_t SectorCount;
60 }PART_TABLE;
61
62 typedef struct MBR_HEAD
63 {
64 uint8_t BootCode[446];
65 PART_TABLE PartTbl[4];
66 uint8_t Byte55;
67 uint8_t ByteAA;
68 }MBR_HEAD;
69
70 typedef struct VTOY_GPT_HDR
71 {
72 char Signature[8]; /* EFI PART */
73 uint8_t Version[4];
74 uint32_t Length;
75 uint32_t Crc;
76 uint8_t Reserved1[4];
77 uint64_t EfiStartLBA;
78 uint64_t EfiBackupLBA;
79 uint64_t PartAreaStartLBA;
80 uint64_t PartAreaEndLBA;
81 vtoy_guid DiskGuid;
82 uint64_t PartTblStartLBA;
83 uint32_t PartTblTotNum;
84 uint32_t PartTblEntryLen;
85 uint32_t PartTblCrc;
86 uint8_t Reserved2[420];
87 }VTOY_GPT_HDR;
88
89 typedef struct VTOY_GPT_PART_TBL
90 {
91 vtoy_guid PartType;
92 vtoy_guid PartGuid;
93 uint64_t StartLBA;
94 uint64_t LastLBA;
95 uint64_t Attr;
96 uint16_t Name[36];
97 }VTOY_GPT_PART_TBL;
98
99 typedef struct VTOY_GPT_INFO
100 {
101 MBR_HEAD MBR;
102 VTOY_GPT_HDR Head;
103 VTOY_GPT_PART_TBL PartTbl[128];
104 }VTOY_GPT_INFO;
105 #pragma pack()
106
107
108 #define MBR_PART_STYLE 0
109 #define GPT_PART_STYLE 1
110
111 typedef struct disk_ventoy_data
112 {
113 int ventoy_valid;
114
115 char ventoy_ver[32]; // 1.0.33 ...
116 int secure_boot_flag;
117 uint64_t preserved_space;
118
119 uint64_t part2_start_sector;
120
121 int partition_style; // MBR_PART_STYLE/GPT_PART_STYLE
122 VTOY_GPT_INFO gptinfo;
123 uint8_t rsvdata[4096];
124 }disk_ventoy_data;
125
126
127 typedef struct ventoy_disk
128 {
129 char disk_name[32]; // sda
130 char disk_path[64]; // /dev/sda
131
132 char part1_name[32]; // sda1
133 char part1_path[64]; // /dev/sda1
134 char part2_name[32]; // sda2
135 char part2_path[64]; // /dev/sda2
136
137 char disk_model[256]; // Sandisk/Kingston ...
138 char human_readable_size[32];
139
140 int major;
141 int minor;
142 int type;
143 int partstyle;
144 uint64_t size_in_byte;
145
146 disk_ventoy_data vtoydata;
147 }ventoy_disk;
148
149 #pragma pack(1)
150 typedef struct ventoy_guid
151 {
152 uint32_t data1;
153 uint16_t data2;
154 uint16_t data3;
155 uint8_t data4[8];
156 }ventoy_guid;
157 #pragma pack()
158
159 #ifndef O_BINARY
160 #define O_BINARY 0
161 #endif
162
163 #define VLOG_LOG 1
164 #define VLOG_DEBUG 2
165
166 #define ulong unsigned long
167 #define _ll long long
168 #define _ull unsigned long long
169 #define strlcpy(dst, src) strncpy(dst, src, sizeof(dst) - 1)
170 #define scnprintf(dst, fmt, args...) snprintf(dst, sizeof(dst) - 1, fmt, ##args)
171
172 #define vlog(fmt, args...) ventoy_syslog(VLOG_LOG, fmt, ##args)
173 #define vdebug(fmt, args...) ventoy_syslog(VLOG_DEBUG, fmt, ##args)
174
175 void ventoy_syslog(int level, const char *Fmt, ...);
176 void ventoy_set_loglevel(int level);
177 uint32_t ventoy_crc32(void *Buffer, uint32_t Length);
178
179
180 static inline void * zalloc(size_t n)
181 {
182 void *p = malloc(n);
183 if (p) memset(p, 0, n);
184 return p;
185 }
186
187
188 #endif /* __VENTOY_DEFINE_H__ */
189