]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/grub-2.04/grub-core/ventoy/ventoy_def.h
initial commit
[Ventoy.git] / GRUB2 / grub-2.04 / grub-core / ventoy / ventoy_def.h
1 /******************************************************************************
2 * ventoy_def.h
3 *
4 * Copyright (c) 2020, 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
21 #ifndef __VENTOY_DEF_H__
22 #define __VENTOY_DEF_H__
23
24 #define JSON_SUCCESS 0
25 #define JSON_FAILED 1
26 #define JSON_NOT_FOUND 2
27
28 #define ulonglong unsigned long long
29
30 #define vtoy_to_upper(c) (((char)(c) >= 'a' && (char)(c) <= 'z') ? ((char)(c) - 'a' + 'A') : (char)(c))
31
32 #define VENTOY_CMD_RETURN(err) grub_errno = (err); return (err)
33 #define VENTOY_FILE_TYPE (GRUB_FILE_TYPE_NO_DECOMPRESS | GRUB_FILE_TYPE_LINUX_INITRD)
34
35 #define ventoy_env_op1(op, a) grub_env_##op(a)
36 #define ventoy_env_op2(op, a, b) grub_env_##op((a), (b))
37
38 #define ventoy_get_env(key) ventoy_env_op1(get, key)
39 #define ventoy_set_env(key, val) ventoy_env_op2(set, key, val)
40
41 typedef struct ventoy_initrd_ctx
42 {
43 const char *path_prefix;
44 const char *dir_prefix;
45 }ventoy_initrd_ctx;
46
47 typedef struct cmd_para
48 {
49 const char *name;
50 grub_extcmd_func_t func;
51 grub_command_flags_t flags;
52 const struct grub_arg_option *parser;
53
54 const char *summary;
55 const char *description;
56
57 grub_extcmd_t cmd;
58 }cmd_para;
59
60 #define ventoy_align(value, align) (((value) + ((align) - 1)) & (~((align) - 1)))
61
62 #pragma pack(1)
63 typedef struct cpio_newc_header
64 {
65 char c_magic[6];
66 char c_ino[8];
67 char c_mode[8];
68 char c_uid[8];
69 char c_gid[8];
70 char c_nlink[8];
71 char c_mtime[8];
72 char c_filesize[8];
73 char c_devmajor[8];
74 char c_devminor[8];
75 char c_rdevmajor[8];
76 char c_rdevminor[8];
77 char c_namesize[8];
78 char c_check[8];
79 }cpio_newc_header;
80 #pragma pack()
81
82
83 #define cmd_raw_name ctxt->extcmd->cmd->name
84 #define check_free(p, func) if (p) { func(p); p = NULL; }
85
86 typedef int (*grub_char_check_func)(int c);
87 #define ventoy_is_decimal(str) ventoy_string_check(str, grub_isdigit)
88
89
90 // El Torito Boot Record Volume Descriptor
91 #pragma pack(1)
92 typedef struct eltorito_descriptor
93 {
94 grub_uint8_t type;
95 grub_uint8_t id[5];
96 grub_uint8_t version;
97 grub_uint8_t system_id[32];
98 grub_uint8_t reserved[32];
99 grub_uint32_t sector;
100 }eltorito_descriptor;
101
102 typedef struct ventoy_iso9660_override
103 {
104 grub_uint32_t first_sector;
105 grub_uint32_t first_sector_be;
106 grub_uint32_t size;
107 grub_uint32_t size_be;
108 }ventoy_iso9660_override;
109
110 typedef struct ventoy_udf_override
111 {
112 grub_uint32_t length;
113 grub_uint32_t position;
114 }ventoy_udf_override;
115
116 #pragma pack()
117
118
119 typedef struct img_info
120 {
121 char path[512];
122 char name[256];
123
124 struct img_info *next;
125 struct img_info *prev;
126 }img_info;
127
128 typedef struct img_iterator_node
129 {
130 struct img_iterator_node *next;
131 img_info **tail;
132 char dir[400];
133 }img_iterator_node;
134
135 typedef struct initrd_info
136 {
137 char name[256];
138
139 grub_uint64_t offset;
140 grub_uint64_t size;
141
142 grub_uint8_t iso_type; // 0: iso9660 1:udf
143 grub_uint32_t udf_start_block;
144
145 grub_uint64_t override_offset;
146 grub_uint32_t override_length;
147 char override_data[32];
148
149 struct initrd_info *next;
150 struct initrd_info *prev;
151 }initrd_info;
152
153 extern initrd_info *g_initrd_img_list;
154 extern initrd_info *g_initrd_img_tail;
155 extern int g_initrd_img_count;
156 extern int g_valid_initrd_count;
157
158 extern img_info *g_ventoy_img_list;
159 extern int g_ventoy_img_count;
160
161 extern grub_uint8_t *g_ventoy_cpio_buf;
162 extern grub_uint32_t g_ventoy_cpio_size;
163 extern cpio_newc_header *g_ventoy_initrd_head;
164 extern grub_uint8_t *g_ventoy_runtime_buf;
165
166 extern ventoy_guid g_ventoy_guid;
167
168 extern ventoy_img_chunk_list g_img_chunk_list;
169
170 extern int g_ventoy_debug;
171 void ventoy_debug(const char *fmt, ...);
172 #define debug(fmt, ...) if (g_ventoy_debug) ventoy_debug("[VTOY]: "fmt, __VA_ARGS__)
173
174
175
176 #define FLAG_HEADER_RESERVED 0x00000001
177 #define FLAG_HEADER_COMPRESSION 0x00000002
178 #define FLAG_HEADER_READONLY 0x00000004
179 #define FLAG_HEADER_SPANNED 0x00000008
180 #define FLAG_HEADER_RESOURCE_ONLY 0x00000010
181 #define FLAG_HEADER_METADATA_ONLY 0x00000020
182 #define FLAG_HEADER_WRITE_IN_PROGRESS 0x00000040
183 #define FLAG_HEADER_RP_FIX 0x00000080 // reparse point fixup
184 #define FLAG_HEADER_COMPRESS_RESERVED 0x00010000
185 #define FLAG_HEADER_COMPRESS_XPRESS 0x00020000
186 #define FLAG_HEADER_COMPRESS_LZX 0x00040000
187
188 #define RESHDR_FLAG_FREE 0x01
189 #define RESHDR_FLAG_METADATA 0x02
190 #define RESHDR_FLAG_COMPRESSED 0x04
191 #define RESHDR_FLAG_SPANNED 0x08
192
193 #pragma pack(1)
194
195 /* A WIM resource header */
196 typedef struct wim_resource_header
197 {
198 grub_uint64_t size_in_wim:56; /* Compressed length */
199 grub_uint64_t flags:8; /* flags */
200 grub_uint64_t offset; /* Offset */
201 grub_uint64_t raw_size; /* Uncompressed length */
202 }wim_resource_header;
203
204 /* WIM resource header length mask */
205 #define WIM_RESHDR_ZLEN_MASK 0x00ffffffffffffffULL
206
207 /* WIM resource header flags */
208 typedef enum wim_resource_header_flags
209 {
210 WIM_RESHDR_METADATA = ( 0x02ULL << 56 ), /* Resource contains metadata */
211 WIM_RESHDR_COMPRESSED = ( 0x04ULL << 56 ), /* Resource is compressed */
212 WIM_RESHDR_PACKED_STREAMS = ( 0x10ULL << 56 ), /* Resource is compressed using packed streams */
213 }wim_resource_header_flags;
214
215 #define WIM_HEAD_SIGNATURE "MSWIM\0\0"
216
217 /* WIM header */
218 typedef struct wim_header
219 {
220 grub_uint8_t signature[8]; /* Signature */
221 grub_uint32_t header_len; /* Header length */
222 grub_uint32_t version; /* Verson */
223 grub_uint32_t flags; /* Flags */
224 grub_uint32_t chunk_len; /* Chunk length */
225 grub_uint8_t guid[16]; /* GUID */
226 grub_uint16_t part; /* Part number */
227 grub_uint16_t parts; /* Total number of parts */
228 grub_uint32_t images; /* number of images */
229 wim_resource_header lookup; /* Lookup table */
230 wim_resource_header xml; /* XML data */
231 wim_resource_header metadata; /* Boot metadata */
232 grub_uint32_t boot_index; /* Boot index */
233 wim_resource_header integrity; /* Integrity table */
234 grub_uint8_t reserved[60]; /* Reserved */
235 } wim_header;
236
237 /* WIM header flags */
238 typedef enum wim_header_flags
239 {
240 WIM_HDR_XPRESS = 0x00020000, /* WIM uses Xpress compresson */
241 WIM_HDR_LZX = 0x00040000, /* WIM uses LZX compression */
242 }wim_header_flags;
243
244 /* A WIM file hash */
245 typedef struct wim_hash
246 {
247 /* SHA-1 hash */
248 grub_uint8_t sha1[20];
249 }wim_hash;
250
251 /* A WIM lookup table entry */
252 typedef struct wim_lookup_entry
253 {
254 wim_resource_header resource; /* Resource header */
255 grub_uint16_t part; /* Part number */
256 grub_uint32_t refcnt; /* Reference count */
257 wim_hash hash; /* Hash */
258 }wim_lookup_entry;
259
260 /* WIM chunk length */
261 #define WIM_CHUNK_LEN 32768
262
263 /* A WIM chunk buffer */
264 typedef struct wim_chunk_buffer
265 {
266 grub_uint8_t data[WIM_CHUNK_LEN]; /*Data */
267 }wim_chunk_buffer;
268
269 /* Security data */
270 typedef struct wim_security_header
271 {
272 grub_uint32_t len; /* Length */
273 grub_uint32_t count; /* Number of entries */
274 }wim_security_header;
275
276 /* Directory entry */
277 typedef struct wim_directory_entry
278 {
279 grub_uint64_t len; /* Length */
280 grub_uint32_t attributes; /* Attributes */
281 grub_uint32_t security; /* Security ID */
282 grub_uint64_t subdir; /* Subdirectory offset */
283 grub_uint8_t reserved1[16]; /* Reserved */
284 grub_uint64_t created; /* Creation time */
285 grub_uint64_t accessed; /* Last access time */
286 grub_uint64_t written; /* Last written time */
287 wim_hash hash; /* Hash */
288 grub_uint8_t reserved2[12]; /* Reserved */
289 grub_uint16_t streams; /* Streams */
290 grub_uint16_t short_name_len; /* Short name length */
291 grub_uint16_t name_len; /* Name length */
292 }wim_directory_entry;
293
294 /** Normal file */
295 #define WIM_ATTR_NORMAL 0x00000080UL
296
297 /** No security information exists for this file */
298 #define WIM_NO_SECURITY 0xffffffffUL
299
300 #pragma pack()
301
302
303 typedef struct wim_tail
304 {
305 grub_uint32_t wim_raw_size;
306 grub_uint32_t wim_align_size;
307
308 grub_uint8_t iso_type;
309 grub_uint64_t file_offset;
310 grub_uint32_t udf_start_block;
311 grub_uint64_t fe_entry_size_offset;
312 grub_uint64_t override_offset;
313 grub_uint32_t override_len;
314 grub_uint8_t override_data[32];
315
316 wim_header wim_header;
317
318 wim_hash bin_hash;
319 grub_uint32_t jump_exe_len;
320 grub_uint8_t *jump_bin_data;
321 grub_uint32_t bin_raw_len;
322 grub_uint32_t bin_align_len;
323
324 grub_uint8_t *new_meta_data;
325 grub_uint32_t new_meta_len;
326 grub_uint32_t new_meta_align_len;
327
328 grub_uint8_t *new_lookup_data;
329 grub_uint32_t new_lookup_len;
330 grub_uint32_t new_lookup_align_len;
331 }wim_tail;
332
333
334
335 typedef enum _JSON_TYPE
336 {
337 JSON_TYPE_NUMBER = 0,
338 JSON_TYPE_STRING,
339 JSON_TYPE_BOOL,
340 JSON_TYPE_ARRAY,
341 JSON_TYPE_OBJECT,
342 JSON_TYPE_NULL,
343 JSON_TYPE_BUTT
344 }JSON_TYPE;
345
346
347 typedef struct _VTOY_JSON
348 {
349 struct _VTOY_JSON *pstPrev;
350 struct _VTOY_JSON *pstNext;
351 struct _VTOY_JSON *pstChild;
352
353 JSON_TYPE enDataType;
354 union
355 {
356 char *pcStrVal;
357 int iNumVal;
358 grub_uint64_t lValue;
359 }unData;
360
361 char *pcName;
362 }VTOY_JSON;
363
364 typedef struct _JSON_PARSE
365 {
366 char *pcKey;
367 void *pDataBuf;
368 grub_uint32_t uiBufSize;
369 }JSON_PARSE;
370
371 #define JSON_NEW_ITEM(pstJson, ret) \
372 { \
373 (pstJson) = (VTOY_JSON *)grub_zalloc(sizeof(VTOY_JSON)); \
374 if (NULL == (pstJson)) \
375 { \
376 json_debug("Failed to alloc memory for json.\n"); \
377 return (ret); \
378 } \
379 }
380
381 typedef int (*ventoy_plugin_entry_pf)(VTOY_JSON *json, const char *isodisk);
382
383 typedef struct plugin_entry
384 {
385 const char *key;
386 ventoy_plugin_entry_pf entryfunc;
387 }plugin_entry;
388
389
390 void ventoy_fill_os_param(grub_file_t file, ventoy_os_param *param);
391 grub_err_t ventoy_cmd_isolinux_initrd_collect(grub_extcmd_context_t ctxt, int argc, char **args);
392 grub_err_t ventoy_cmd_grub_initrd_collect(grub_extcmd_context_t ctxt, int argc, char **args);
393 grub_err_t ventoy_cmd_specify_initrd_file(grub_extcmd_context_t ctxt, int argc, char **args);
394 grub_err_t ventoy_cmd_dump_initrd_list(grub_extcmd_context_t ctxt, int argc, char **args);
395 grub_err_t ventoy_cmd_clear_initrd_list(grub_extcmd_context_t ctxt, int argc, char **args);
396 grub_uint32_t ventoy_get_iso_boot_catlog(grub_file_t file);
397 int ventoy_has_efi_eltorito(grub_file_t file, grub_uint32_t sector);
398 grub_err_t ventoy_cmd_linux_chain_data(grub_extcmd_context_t ctxt, int argc, char **args);
399 grub_err_t ventoy_cmd_linux_locate_initrd(grub_extcmd_context_t ctxt, int argc, char **args);
400 grub_err_t ventoy_cmd_initrd_count(grub_extcmd_context_t ctxt, int argc, char **args);
401 grub_err_t ventoy_cmd_load_cpio(grub_extcmd_context_t ctxt, int argc, char **args);
402 int ventoy_cpio_newc_fill_head(void *buf, int filesize, void *filedata, const char *name);
403 grub_file_t ventoy_grub_file_open(enum grub_file_type type, const char *fmt, ...);
404 int ventoy_is_file_exist(const char *fmt, ...);
405 int ventoy_fill_data(grub_uint32_t buflen, char *buffer);
406 grub_err_t ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt, int argc, char **args);
407 grub_err_t ventoy_cmd_wimdows_reset(grub_extcmd_context_t ctxt, int argc, char **args);
408 grub_err_t ventoy_cmd_wimdows_locate_wim(grub_extcmd_context_t ctxt, int argc, char **args);
409 grub_err_t ventoy_cmd_windows_chain_data(grub_extcmd_context_t ctxt, int argc, char **args);
410
411 VTOY_JSON *vtoy_json_find_item
412 (
413 VTOY_JSON *pstJson,
414 JSON_TYPE enDataType,
415 const char *szKey
416 );
417 int vtoy_json_parse_value
418 (
419 char *pcNewStart,
420 char *pcRawStart,
421 VTOY_JSON *pstJson,
422 const char *pcData,
423 const char **ppcEnd
424 );
425 VTOY_JSON * vtoy_json_create(void);
426 int vtoy_json_parse(VTOY_JSON *pstJson, const char *szJsonData);
427
428 int vtoy_json_scan_parse
429 (
430 const VTOY_JSON *pstJson,
431 grub_uint32_t uiParseNum,
432 JSON_PARSE *pstJsonParse
433 );
434
435 int vtoy_json_scan_array
436 (
437 VTOY_JSON *pstJson,
438 const char *szKey,
439 VTOY_JSON **ppstArrayItem
440 );
441
442 int vtoy_json_scan_array_ex
443 (
444 VTOY_JSON *pstJson,
445 const char *szKey,
446 VTOY_JSON **ppstArrayItem
447 );
448 int vtoy_json_scan_object
449 (
450 VTOY_JSON *pstJson,
451 const char *szKey,
452 VTOY_JSON **ppstObjectItem
453 );
454 int vtoy_json_get_int
455 (
456 VTOY_JSON *pstJson,
457 const char *szKey,
458 int *piValue
459 );
460 int vtoy_json_get_uint
461 (
462 VTOY_JSON *pstJson,
463 const char *szKey,
464 grub_uint32_t *puiValue
465 );
466 int vtoy_json_get_uint64
467 (
468 VTOY_JSON *pstJson,
469 const char *szKey,
470 grub_uint64_t *pui64Value
471 );
472 int vtoy_json_get_bool
473 (
474 VTOY_JSON *pstJson,
475 const char *szKey,
476 grub_uint8_t *pbValue
477 );
478 int vtoy_json_get_string
479 (
480 VTOY_JSON *pstJson,
481 const char *szKey,
482 grub_uint32_t uiBufLen,
483 char *pcBuf
484 );
485 const char * vtoy_json_get_string_ex(VTOY_JSON *pstJson, const char *szKey);
486 int vtoy_json_destroy(VTOY_JSON *pstJson);
487
488
489 grub_uint32_t CalculateCrc32
490 (
491 const void *Buffer,
492 grub_uint32_t Length,
493 grub_uint32_t InitValue
494 );
495
496 static inline int ventoy_isspace (int c)
497 {
498 return (c == '\n' || c == '\r' || c == ' ' || c == '\t');
499 }
500
501 static inline int ventoy_is_word_end(int c)
502 {
503 return (c == 0 || c == ',' || ventoy_isspace(c));
504 }
505
506 #endif /* __VENTOY_DEF_H__ */
507