]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h
beb9530f799d50a0dd84c7f75605a6e048c4be7a
[Ventoy.git] / GRUB2 / MOD_SRC / 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 VTOY_MAX_SCRIPT_BUF (4 * 1024 * 1024)
25
26 #define VTOY_PART_BUF_LEN (128 * 1024)
27
28 #define VTOY_FILT_MIN_FILE_SIZE 32768
29
30 #define VTOY_SIZE_1GB 1073741824
31 #define VTOY_SIZE_1MB (1024 * 1024)
32 #define VTOY_SIZE_512KB (512 * 1024)
33 #define VTOY_SIZE_1KB 1024
34
35 #define JSON_SUCCESS 0
36 #define JSON_FAILED 1
37 #define JSON_NOT_FOUND 2
38
39 #define ulong unsigned long
40 #define ulonglong unsigned long long
41
42 #define vtoy_to_upper(c) (((char)(c) >= 'a' && (char)(c) <= 'z') ? ((char)(c) - 'a' + 'A') : (char)(c))
43
44 #define VENTOY_CMD_RETURN(err) grub_errno = (err); return (err)
45 #define VENTOY_FILE_TYPE (GRUB_FILE_TYPE_NO_DECOMPRESS | GRUB_FILE_TYPE_LINUX_INITRD)
46
47 #define ventoy_env_op1(op, a) grub_env_##op(a)
48 #define ventoy_env_op2(op, a, b) grub_env_##op((a), (b))
49
50 #define ventoy_get_env(key) ventoy_env_op1(get, key)
51 #define ventoy_set_env(key, val) ventoy_env_op2(set, key, val)
52
53 #define VTOY_WARNING "!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!"
54
55 #define VTOY_PLAT_I386_UEFI 0x49413332
56 #define VTOY_PLAT_ARM64_UEFI 0x41413634
57 #define VTOY_PLAT_X86_64_UEFI 0x55454649
58 #define VTOY_PLAT_X86_LEGACY 0x42494f53
59 #define VTOY_PLAT_MIPS_UEFI 0x4D495053
60
61 #define VTOY_COMM_CPIO "ventoy.cpio"
62 #if defined(__arm__) || defined(__aarch64__)
63 #define VTOY_ARCH_CPIO "ventoy_arm64.cpio"
64 #elif defined(__mips__)
65 #define VTOY_ARCH_CPIO "ventoy_mips64.cpio"
66 #else
67 #define VTOY_ARCH_CPIO "ventoy_x86.cpio"
68 #endif
69
70 #define VTOY_PWD_CORRUPTED(err) \
71 {\
72 grub_printf("\n\n Password corrupted, will reboot after 5 seconds.\n\n"); \
73 grub_refresh(); \
74 grub_sleep(5); \
75 grub_exit(); \
76 return (err);\
77 }
78
79 typedef enum VTOY_FILE_FLT
80 {
81 VTOY_FILE_FLT_ISO = 0, /* .iso */
82 VTOY_FILE_FLT_WIM, /* .wim */
83 VTOY_FILE_FLT_EFI, /* .efi */
84 VTOY_FILE_FLT_IMG, /* .img */
85 VTOY_FILE_FLT_VHD, /* .vhd(x) */
86 VTOY_FILE_FLT_VTOY, /* .vtoy */
87
88 VTOY_FILE_FLT_BUTT
89 }VTOY_FILE_FLT;
90
91 #define FILE_FLT(type) (0 == g_vtoy_file_flt[VTOY_FILE_FLT_##type])
92
93 typedef struct ventoy_initrd_ctx
94 {
95 const char *path_prefix;
96 const char *dir_prefix;
97 }ventoy_initrd_ctx;
98
99 typedef struct cmd_para
100 {
101 const char *name;
102 grub_extcmd_func_t func;
103 grub_command_flags_t flags;
104 const struct grub_arg_option *parser;
105
106 const char *summary;
107 const char *description;
108
109 grub_extcmd_t cmd;
110 }cmd_para;
111
112 #define ventoy_align_2k(value) ((value + 2047) / 2048 * 2048)
113 #define ventoy_align(value, align) (((value) + ((align) - 1)) & (~((align) - 1)))
114
115 #pragma pack(1)
116 typedef struct cpio_newc_header
117 {
118 char c_magic[6];
119 char c_ino[8];
120 char c_mode[8];
121 char c_uid[8];
122 char c_gid[8];
123 char c_nlink[8];
124 char c_mtime[8];
125 char c_filesize[8];
126 char c_devmajor[8];
127 char c_devminor[8];
128 char c_rdevmajor[8];
129 char c_rdevminor[8];
130 char c_namesize[8];
131 char c_check[8];
132 }cpio_newc_header;
133 #pragma pack()
134
135
136 #define cmd_raw_name ctxt->extcmd->cmd->name
137 #define check_free(p, func) if (p) { func(p); p = NULL; }
138 #define grub_check_free(p) if (p) { grub_free(p); p = NULL; }
139
140 typedef int (*grub_char_check_func)(int c);
141 #define ventoy_is_decimal(str) ventoy_string_check(str, grub_isdigit)
142
143
144 #pragma pack(1)
145 typedef struct ventoy_patch_vhd
146 {
147 grub_uint8_t part_offset_or_guid[16];
148 grub_uint32_t reserved1;
149 grub_uint32_t part_type;
150 grub_uint8_t disk_signature_or_guid[16];
151 grub_uint8_t reserved2[16];
152 grub_uint8_t vhd_file_path[1];
153 }ventoy_patch_vhd;
154 #pragma pack()
155
156 // El Torito Boot Record Volume Descriptor
157 #pragma pack(1)
158 typedef struct eltorito_descriptor
159 {
160 grub_uint8_t type;
161 grub_uint8_t id[5];
162 grub_uint8_t version;
163 grub_uint8_t system_id[32];
164 grub_uint8_t reserved[32];
165 grub_uint32_t sector;
166 }eltorito_descriptor;
167
168 typedef struct ventoy_iso9660_override
169 {
170 grub_uint32_t first_sector;
171 grub_uint32_t first_sector_be;
172 grub_uint32_t size;
173 grub_uint32_t size_be;
174 }ventoy_iso9660_override;
175
176 typedef struct ventoy_udf_override
177 {
178 grub_uint32_t length;
179 grub_uint32_t position;
180 }ventoy_udf_override;
181
182 typedef struct ventoy_iso9660_vd
183 {
184 grub_uint8_t type;
185 grub_uint8_t id[5];
186 grub_uint8_t ver;
187 grub_uint8_t res;
188 char sys[32];
189 char vol[32];
190 grub_uint8_t res2[8];
191 grub_uint32_t space;
192 }ventoy_iso9660_vd;
193
194 #pragma pack()
195
196 #define img_type_iso 0
197 #define img_type_wim 1
198 #define img_type_efi 2
199 #define img_type_img 3
200 #define img_type_vhd 4
201 #define img_type_vtoy 5
202
203 typedef struct img_info
204 {
205 int pathlen;
206 char path[512];
207 char name[256];
208
209 const char *alias;
210 const char *class;
211 const char *menu_prefix;
212
213 int id;
214 int type;
215 int plugin_list_index;
216 grub_uint64_t size;
217 int select;
218 int unsupport;
219
220 void *parent;
221
222 struct img_info *next;
223 struct img_info *prev;
224 }img_info;
225
226 typedef struct img_iterator_node
227 {
228 struct img_iterator_node *next;
229 img_info **tail;
230 char dir[400];
231 int dirlen;
232 int level;
233 int isocnt;
234 int done;
235 int select;
236
237 int plugin_list_index;
238
239 struct img_iterator_node *parent;
240 struct img_iterator_node *firstchild;
241
242 void *firstiso;
243 }img_iterator_node;
244
245
246
247 typedef struct initrd_info
248 {
249 char name[256];
250
251 grub_uint64_t offset;
252 grub_uint64_t size;
253
254 grub_uint8_t iso_type; // 0: iso9660 1:udf
255 grub_uint32_t udf_start_block;
256
257 grub_uint64_t override_offset;
258 grub_uint32_t override_length;
259 char override_data[32];
260
261 struct initrd_info *next;
262 struct initrd_info *prev;
263 }initrd_info;
264
265 extern initrd_info *g_initrd_img_list;
266 extern initrd_info *g_initrd_img_tail;
267 extern int g_initrd_img_count;
268 extern int g_valid_initrd_count;
269
270 extern img_info *g_ventoy_img_list;
271 extern int g_ventoy_img_count;
272
273 extern grub_uint8_t *g_ventoy_cpio_buf;
274 extern grub_uint32_t g_ventoy_cpio_size;
275 extern cpio_newc_header *g_ventoy_initrd_head;
276 extern grub_uint8_t *g_ventoy_runtime_buf;
277
278 extern ventoy_guid g_ventoy_guid;
279
280 extern ventoy_img_chunk_list g_img_chunk_list;
281 extern ventoy_img_chunk_list g_wimiso_chunk_list;
282 extern char *g_wimiso_path;
283 extern char g_arch_mode_suffix[64];
284
285 extern int g_ventoy_debug;
286 void ventoy_debug(const char *fmt, ...);
287 #define debug(fmt, args...) if (g_ventoy_debug) ventoy_debug("[VTOY]: "fmt, ##args)
288
289 #define vtoy_ssprintf(buf, pos, fmt, ...) \
290 pos += grub_snprintf(buf + pos, VTOY_MAX_SCRIPT_BUF - pos, fmt, __VA_ARGS__)
291
292 #define FLAG_HEADER_RESERVED 0x00000001
293 #define FLAG_HEADER_COMPRESSION 0x00000002
294 #define FLAG_HEADER_READONLY 0x00000004
295 #define FLAG_HEADER_SPANNED 0x00000008
296 #define FLAG_HEADER_RESOURCE_ONLY 0x00000010
297 #define FLAG_HEADER_METADATA_ONLY 0x00000020
298 #define FLAG_HEADER_WRITE_IN_PROGRESS 0x00000040
299 #define FLAG_HEADER_RP_FIX 0x00000080 // reparse point fixup
300 #define FLAG_HEADER_COMPRESS_RESERVED 0x00010000
301 #define FLAG_HEADER_COMPRESS_XPRESS 0x00020000
302 #define FLAG_HEADER_COMPRESS_LZX 0x00040000
303 #define FLAG_HEADER_COMPRESS_LZMS 0x00080000
304
305 #define RESHDR_FLAG_FREE 0x01
306 #define RESHDR_FLAG_METADATA 0x02
307 #define RESHDR_FLAG_COMPRESSED 0x04
308 #define RESHDR_FLAG_SPANNED 0x08
309
310 #pragma pack(1)
311
312 /* A WIM resource header */
313 typedef struct wim_resource_header
314 {
315 grub_uint64_t size_in_wim:56; /* Compressed length */
316 grub_uint64_t flags:8; /* flags */
317 grub_uint64_t offset; /* Offset */
318 grub_uint64_t raw_size; /* Uncompressed length */
319 }wim_resource_header;
320
321 /* WIM resource header length mask */
322 #define WIM_RESHDR_ZLEN_MASK 0x00ffffffffffffffULL
323
324 /* WIM resource header flags */
325 typedef enum wim_resource_header_flags
326 {
327 WIM_RESHDR_METADATA = ( 0x02ULL << 56 ), /* Resource contains metadata */
328 WIM_RESHDR_COMPRESSED = ( 0x04ULL << 56 ), /* Resource is compressed */
329 WIM_RESHDR_PACKED_STREAMS = ( 0x10ULL << 56 ), /* Resource is compressed using packed streams */
330 }wim_resource_header_flags;
331
332 #define WIM_HEAD_SIGNATURE "MSWIM\0\0"
333
334 /* WIM header */
335 typedef struct wim_header
336 {
337 grub_uint8_t signature[8]; /* Signature */
338 grub_uint32_t header_len; /* Header length */
339 grub_uint32_t version; /* Verson */
340 grub_uint32_t flags; /* Flags */
341 grub_uint32_t chunk_len; /* Chunk length */
342 grub_uint8_t guid[16]; /* GUID */
343 grub_uint16_t part; /* Part number */
344 grub_uint16_t parts; /* Total number of parts */
345 grub_uint32_t images; /* number of images */
346 wim_resource_header lookup; /* Lookup table */
347 wim_resource_header xml; /* XML data */
348 wim_resource_header metadata; /* Boot metadata */
349 grub_uint32_t boot_index; /* Boot index */
350 wim_resource_header integrity; /* Integrity table */
351 grub_uint8_t reserved[60]; /* Reserved */
352 } wim_header;
353
354 /* WIM header flags */
355 typedef enum wim_header_flags
356 {
357 WIM_HDR_XPRESS = 0x00020000, /* WIM uses Xpress compresson */
358 WIM_HDR_LZX = 0x00040000, /* WIM uses LZX compression */
359 }wim_header_flags;
360
361 /* A WIM file hash */
362 typedef struct wim_hash
363 {
364 /* SHA-1 hash */
365 grub_uint8_t sha1[20];
366 }wim_hash;
367
368 /* A WIM lookup table entry */
369 typedef struct wim_lookup_entry
370 {
371 wim_resource_header resource; /* Resource header */
372 grub_uint16_t part; /* Part number */
373 grub_uint32_t refcnt; /* Reference count */
374 wim_hash hash; /* Hash */
375 }wim_lookup_entry;
376
377 /* WIM chunk length */
378 #define WIM_CHUNK_LEN 32768
379
380 /* A WIM chunk buffer */
381 typedef struct wim_chunk_buffer
382 {
383 grub_uint8_t data[WIM_CHUNK_LEN]; /*Data */
384 }wim_chunk_buffer;
385
386 /* Security data */
387 typedef struct wim_security_header
388 {
389 grub_uint32_t len; /* Length */
390 grub_uint32_t count; /* Number of entries */
391 }wim_security_header;
392
393 typedef struct wim_stream_entry
394 {
395 grub_uint64_t len;
396 grub_uint64_t unused1;
397 wim_hash hash;
398 grub_uint16_t name_len;
399 /* name */
400 }wim_stream_entry;
401
402 /* Directory entry */
403 typedef struct wim_directory_entry
404 {
405 grub_uint64_t len; /* Length */
406 grub_uint32_t attributes; /* Attributes */
407 grub_uint32_t security; /* Security ID */
408 grub_uint64_t subdir; /* Subdirectory offset */
409 grub_uint8_t reserved1[16]; /* Reserved */
410 grub_uint64_t created; /* Creation time */
411 grub_uint64_t accessed; /* Last access time */
412 grub_uint64_t written; /* Last written time */
413 wim_hash hash; /* Hash */
414 grub_uint8_t reserved2[12]; /* Reserved */
415 grub_uint16_t streams; /* Streams */
416 grub_uint16_t short_name_len; /* Short name length */
417 grub_uint16_t name_len; /* Name length */
418 }wim_directory_entry;
419
420 /** Normal file */
421 #define WIM_ATTR_NORMAL 0x00000080UL
422
423 /** No security information exists for this file */
424 #define WIM_NO_SECURITY 0xffffffffUL
425
426 #pragma pack()
427
428
429 typedef struct wim_tail
430 {
431 grub_uint32_t wim_raw_size;
432 grub_uint32_t wim_align_size;
433
434 grub_uint8_t iso_type;
435 grub_uint64_t file_offset;
436 grub_uint32_t udf_start_block;
437 grub_uint64_t fe_entry_size_offset;
438 grub_uint64_t override_offset;
439 grub_uint32_t override_len;
440 grub_uint8_t override_data[32];
441
442 wim_header wim_header;
443
444 wim_hash bin_hash;
445 grub_uint32_t jump_exe_len;
446 grub_uint8_t *jump_bin_data;
447 grub_uint32_t bin_raw_len;
448 grub_uint32_t bin_align_len;
449
450 grub_uint8_t *new_meta_data;
451 grub_uint32_t new_meta_len;
452 grub_uint32_t new_meta_align_len;
453
454 grub_uint8_t *new_lookup_data;
455 grub_uint32_t new_lookup_len;
456 grub_uint32_t new_lookup_align_len;
457 }wim_tail;
458
459 typedef struct wim_patch
460 {
461 int pathlen;
462 char path[256];
463
464 wim_hash old_hash;
465 wim_tail wim_data;
466 wim_lookup_entry *replace_look;
467
468 int valid;
469
470 struct wim_patch *next;
471 }wim_patch;
472
473
474 typedef enum _JSON_TYPE
475 {
476 JSON_TYPE_NUMBER = 0,
477 JSON_TYPE_STRING,
478 JSON_TYPE_BOOL,
479 JSON_TYPE_ARRAY,
480 JSON_TYPE_OBJECT,
481 JSON_TYPE_NULL,
482 JSON_TYPE_BUTT
483 }JSON_TYPE;
484
485
486 typedef struct _VTOY_JSON
487 {
488 struct _VTOY_JSON *pstPrev;
489 struct _VTOY_JSON *pstNext;
490 struct _VTOY_JSON *pstChild;
491
492 JSON_TYPE enDataType;
493 union
494 {
495 char *pcStrVal;
496 int iNumVal;
497 grub_uint64_t lValue;
498 }unData;
499
500 char *pcName;
501 }VTOY_JSON;
502
503 typedef struct _JSON_PARSE
504 {
505 char *pcKey;
506 void *pDataBuf;
507 grub_uint32_t uiBufSize;
508 }JSON_PARSE;
509
510 #define JSON_NEW_ITEM(pstJson, ret) \
511 { \
512 (pstJson) = (VTOY_JSON *)grub_zalloc(sizeof(VTOY_JSON)); \
513 if (NULL == (pstJson)) \
514 { \
515 json_debug("Failed to alloc memory for json.\n"); \
516 return (ret); \
517 } \
518 }
519
520 typedef int (*ventoy_plugin_entry_pf)(VTOY_JSON *json, const char *isodisk);
521 typedef int (*ventoy_plugin_check_pf)(VTOY_JSON *json, const char *isodisk);
522
523 typedef struct plugin_entry
524 {
525 const char *key;
526 ventoy_plugin_entry_pf entryfunc;
527 ventoy_plugin_check_pf checkfunc;
528 }plugin_entry;
529
530 int ventoy_strcmp(const char *pattern, const char *str);
531 int ventoy_strncmp (const char *pattern, const char *str, grub_size_t n);
532 void ventoy_fill_os_param(grub_file_t file, ventoy_os_param *param);
533 grub_err_t ventoy_cmd_isolinux_initrd_collect(grub_extcmd_context_t ctxt, int argc, char **args);
534 grub_err_t ventoy_cmd_grub_initrd_collect(grub_extcmd_context_t ctxt, int argc, char **args);
535 grub_err_t ventoy_cmd_specify_initrd_file(grub_extcmd_context_t ctxt, int argc, char **args);
536 grub_err_t ventoy_cmd_dump_initrd_list(grub_extcmd_context_t ctxt, int argc, char **args);
537 grub_err_t ventoy_cmd_clear_initrd_list(grub_extcmd_context_t ctxt, int argc, char **args);
538 grub_uint32_t ventoy_get_iso_boot_catlog(grub_file_t file);
539 int ventoy_has_efi_eltorito(grub_file_t file, grub_uint32_t sector);
540 grub_err_t ventoy_cmd_linux_chain_data(grub_extcmd_context_t ctxt, int argc, char **args);
541 grub_err_t ventoy_cmd_linux_locate_initrd(grub_extcmd_context_t ctxt, int argc, char **args);
542 grub_err_t ventoy_cmd_initrd_count(grub_extcmd_context_t ctxt, int argc, char **args);
543 grub_err_t ventoy_cmd_valid_initrd_count(grub_extcmd_context_t ctxt, int argc, char **args);
544 grub_err_t ventoy_cmd_load_cpio(grub_extcmd_context_t ctxt, int argc, char **args);
545 grub_err_t ventoy_cmd_skip_svd(grub_extcmd_context_t ctxt, int argc, char **args);
546 grub_err_t ventoy_cmd_cpio_busybox_64(grub_extcmd_context_t ctxt, int argc, char **args);
547 grub_err_t ventoy_cmd_trailer_cpio(grub_extcmd_context_t ctxt, int argc, char **args);
548 int ventoy_cpio_newc_fill_head(void *buf, int filesize, const void *filedata, const char *name);
549 grub_file_t ventoy_grub_file_open(enum grub_file_type type, const char *fmt, ...);
550 grub_uint64_t ventoy_grub_get_file_size(const char *fmt, ...);
551 int ventoy_is_file_exist(const char *fmt, ...);
552 int ventoy_is_dir_exist(const char *fmt, ...);
553 int ventoy_fill_data(grub_uint32_t buflen, char *buffer);
554 grub_err_t ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt, int argc, char **args);
555 grub_err_t ventoy_cmd_wimdows_reset(grub_extcmd_context_t ctxt, int argc, char **args);
556 grub_err_t ventoy_cmd_windows_chain_data(grub_extcmd_context_t ctxt, int argc, char **args);
557 grub_err_t ventoy_cmd_wim_chain_data(grub_extcmd_context_t ctxt, int argc, char **args);
558 grub_err_t ventoy_cmd_wim_check_bootable(grub_extcmd_context_t ctxt, int argc, char **args);
559 grub_err_t ventoy_cmd_dump_wim_patch(grub_extcmd_context_t ctxt, int argc, char **args);
560
561 VTOY_JSON *vtoy_json_find_item
562 (
563 VTOY_JSON *pstJson,
564 JSON_TYPE enDataType,
565 const char *szKey
566 );
567 int vtoy_json_parse_value
568 (
569 char *pcNewStart,
570 char *pcRawStart,
571 VTOY_JSON *pstJson,
572 const char *pcData,
573 const char **ppcEnd
574 );
575 VTOY_JSON * vtoy_json_create(void);
576 int vtoy_json_parse(VTOY_JSON *pstJson, const char *szJsonData);
577
578 int vtoy_json_scan_parse
579 (
580 const VTOY_JSON *pstJson,
581 grub_uint32_t uiParseNum,
582 JSON_PARSE *pstJsonParse
583 );
584
585 int vtoy_json_scan_array
586 (
587 VTOY_JSON *pstJson,
588 const char *szKey,
589 VTOY_JSON **ppstArrayItem
590 );
591
592 int vtoy_json_scan_array_ex
593 (
594 VTOY_JSON *pstJson,
595 const char *szKey,
596 VTOY_JSON **ppstArrayItem
597 );
598 int vtoy_json_scan_object
599 (
600 VTOY_JSON *pstJson,
601 const char *szKey,
602 VTOY_JSON **ppstObjectItem
603 );
604 int vtoy_json_get_int
605 (
606 VTOY_JSON *pstJson,
607 const char *szKey,
608 int *piValue
609 );
610 int vtoy_json_get_uint
611 (
612 VTOY_JSON *pstJson,
613 const char *szKey,
614 grub_uint32_t *puiValue
615 );
616 int vtoy_json_get_uint64
617 (
618 VTOY_JSON *pstJson,
619 const char *szKey,
620 grub_uint64_t *pui64Value
621 );
622 int vtoy_json_get_bool
623 (
624 VTOY_JSON *pstJson,
625 const char *szKey,
626 grub_uint8_t *pbValue
627 );
628 int vtoy_json_get_string
629 (
630 VTOY_JSON *pstJson,
631 const char *szKey,
632 grub_uint32_t uiBufLen,
633 char *pcBuf
634 );
635 const char * vtoy_json_get_string_ex(VTOY_JSON *pstJson, const char *szKey);
636 int vtoy_json_destroy(VTOY_JSON *pstJson);
637
638
639 grub_uint32_t CalculateCrc32
640 (
641 const void *Buffer,
642 grub_uint32_t Length,
643 grub_uint32_t InitValue
644 );
645
646 static inline int ventoy_isspace (int c)
647 {
648 return (c == '\n' || c == '\r' || c == ' ' || c == '\t');
649 }
650
651 static inline int ventoy_is_word_end(int c)
652 {
653 return (c == 0 || c == ',' || ventoy_isspace(c));
654 }
655
656 #pragma pack(1)
657 typedef struct ventoy_part_table
658 {
659 grub_uint8_t Active; // 0x00 0x80
660
661 grub_uint8_t StartHead;
662 grub_uint16_t StartSector : 6;
663 grub_uint16_t StartCylinder : 10;
664
665 grub_uint8_t FsFlag;
666
667 grub_uint8_t EndHead;
668 grub_uint16_t EndSector : 6;
669 grub_uint16_t EndCylinder : 10;
670
671 grub_uint32_t StartSectorId;
672 grub_uint32_t SectorCount;
673 }ventoy_part_table;
674
675 typedef struct ventoy_mbr_head
676 {
677 grub_uint8_t BootCode[446];
678 ventoy_part_table PartTbl[4];
679 grub_uint8_t Byte55;
680 grub_uint8_t ByteAA;
681 }ventoy_mbr_head;
682
683 typedef struct ventoy_gpt_head
684 {
685 char Signature[8]; /* EFI PART */
686 grub_uint8_t Version[4];
687 grub_uint32_t Length;
688 grub_uint32_t Crc;
689 grub_uint8_t Reserved1[4];
690 grub_uint64_t EfiStartLBA;
691 grub_uint64_t EfiBackupLBA;
692 grub_uint64_t PartAreaStartLBA;
693 grub_uint64_t PartAreaEndLBA;
694 grub_uint8_t DiskGuid[16];
695 grub_uint64_t PartTblStartLBA;
696 grub_uint32_t PartTblTotNum;
697 grub_uint32_t PartTblEntryLen;
698 grub_uint32_t PartTblCrc;
699 grub_uint8_t Reserved2[420];
700 }ventoy_gpt_head;
701
702 typedef struct ventoy_gpt_part_tbl
703 {
704 grub_uint8_t PartType[16];
705 grub_uint8_t PartGuid[16];
706 grub_uint64_t StartLBA;
707 grub_uint64_t LastLBA;
708 grub_uint64_t Attr;
709 grub_uint16_t Name[36];
710 }ventoy_gpt_part_tbl;
711
712 typedef struct ventoy_gpt_info
713 {
714 ventoy_mbr_head MBR;
715 ventoy_gpt_head Head;
716 ventoy_gpt_part_tbl PartTbl[128];
717 }ventoy_gpt_info;
718
719 typedef struct vhd_footer_t
720 {
721 char cookie[8]; // Cookie
722 grub_uint32_t features; // Features
723 grub_uint32_t ffversion; // File format version
724 grub_uint32_t dataoffset; // Data offset
725 grub_uint32_t timestamp; // Timestamp
726 grub_uint32_t creatorapp; // Creator application
727 grub_uint32_t creatorver; // Creator version
728 grub_uint32_t creatorhos; // Creator host OS
729 grub_uint32_t origsize; // Original size
730 grub_uint32_t currsize; // Current size
731 grub_uint32_t diskgeom; // Disk geometry
732 grub_uint32_t disktype; // Disk type
733 grub_uint32_t checksum; // Checksum
734 grub_uint8_t uniqueid[16]; // Unique ID
735 grub_uint8_t savedst; // Saved state
736 }vhd_footer_t;
737
738 #define VDI_IMAGE_FILE_INFO "<<< Oracle VM VirtualBox Disk Image >>>\n"
739
740 /** Image signature. */
741 #define VDI_IMAGE_SIGNATURE (0xbeda107f)
742
743 typedef struct VDIPREHEADER
744 {
745 /** Just text info about image type, for eyes only. */
746 char szFileInfo[64];
747 /** The image signature (VDI_IMAGE_SIGNATURE). */
748 grub_uint32_t u32Signature;
749 /** The image version (VDI_IMAGE_VERSION). */
750 grub_uint32_t u32Version;
751 } VDIPREHEADER, *PVDIPREHEADER;
752
753 #pragma pack()
754
755 typedef struct ventoy_video_mode
756 {
757 grub_uint32_t width;
758 grub_uint32_t height;
759 grub_uint32_t bpp;
760 }ventoy_video_mode;
761
762
763
764 typedef struct file_fullpath
765 {
766 char path[256];
767 }file_fullpath;
768
769 typedef struct install_template
770 {
771 int pathlen;
772 char isopath[256];
773
774 int autosel;
775 int cursel;
776 int templatenum;
777 file_fullpath *templatepath;
778
779 struct install_template *next;
780 }install_template;
781
782 typedef struct dudfile
783 {
784 int size;
785 char *buf;
786 }dudfile;
787
788 typedef struct dud
789 {
790 int pathlen;
791 char isopath[256];
792
793 int dudnum;
794 file_fullpath *dudpath;
795 dudfile *files;
796
797 struct dud *next;
798 }dud;
799
800 typedef struct persistence_config
801 {
802 int pathlen;
803 char isopath[256];
804
805 int autosel;
806 int cursel;
807 int backendnum;
808 file_fullpath *backendpath;
809
810 struct persistence_config *next;
811 }persistence_config;
812
813 #define vtoy_alias_image_file 0
814 #define vtoy_alias_directory 1
815
816 typedef struct menu_alias
817 {
818 int type;
819 int pathlen;
820 char isopath[256];
821 char alias[256];
822
823 struct menu_alias *next;
824 }menu_alias;
825
826 #define vtoy_class_image_file 0
827 #define vtoy_class_directory 1
828
829 typedef struct menu_class
830 {
831 int type;
832 int patlen;
833 char pattern[256];
834 char class[64];
835
836 struct menu_class *next;
837 }menu_class;
838
839 #define vtoy_custom_boot_image_file 0
840 #define vtoy_custom_boot_directory 1
841
842 typedef struct custom_boot
843 {
844 int type;
845 int pathlen;
846 char path[256];
847 char cfg[256];
848
849 struct custom_boot *next;
850 }custom_boot;
851
852 #define vtoy_max_replace_file_size (2 * 1024 * 1024)
853 typedef struct conf_replace
854 {
855 int pathlen;
856 char isopath[256];
857 char orgconf[256];
858 char newconf[256];
859
860 struct conf_replace *next;
861 }conf_replace;
862
863 typedef struct injection_config
864 {
865 int pathlen;
866 char isopath[256];
867 char archive[256];
868
869 struct injection_config *next;
870 }injection_config;
871
872 typedef struct auto_memdisk
873 {
874 int pathlen;
875 char isopath[256];
876
877 struct auto_memdisk *next;
878 }auto_memdisk;
879
880 typedef struct image_list
881 {
882 int pathlen;
883 char isopath[256];
884
885 struct image_list *next;
886 }image_list;
887
888 #define VTOY_PASSWORD_NONE 0
889 #define VTOY_PASSWORD_TXT 1
890 #define VTOY_PASSWORD_MD5 2
891 #define VTOY_PASSWORD_SALT_MD5 3
892
893 typedef struct vtoy_password
894 {
895 int type;
896 char text[128];
897 char salt[64];
898 grub_uint8_t md5[16];
899 }vtoy_password;
900
901 typedef struct menu_password
902 {
903 int pathlen;
904 char isopath[256];
905
906 vtoy_password password;
907
908 struct menu_password *next;
909 }menu_password;
910
911 extern int g_ventoy_menu_esc;
912 extern int g_ventoy_suppress_esc;
913 extern int g_ventoy_last_entry;
914 extern int g_ventoy_memdisk_mode;
915 extern int g_ventoy_iso_raw;
916 extern int g_ventoy_grub2_mode;
917 extern int g_ventoy_iso_uefi_drv;
918 extern int g_ventoy_case_insensitive;
919 extern grub_uint8_t g_ventoy_chain_type;
920 extern int g_vhdboot_enable;
921
922 #define VENTOY_IMG_WHITE_LIST 1
923 #define VENTOY_IMG_BLACK_LIST 2
924 extern int g_plugin_image_list;
925
926 extern ventoy_gpt_info *g_ventoy_part_info;
927 extern grub_uint64_t g_conf_replace_offset;
928 extern grub_uint64_t g_svd_replace_offset;
929 extern conf_replace *g_conf_replace_node;
930 extern grub_uint8_t *g_conf_replace_new_buf;
931 extern int g_conf_replace_new_len;
932 extern int g_conf_replace_new_len_align;
933 extern grub_uint64_t g_ventoy_disk_size;
934
935 #define ventoy_unix_fill_virt(new_data, new_len) \
936 { \
937 data_secs = (new_len + 2047) / 2048; \
938 cur->mem_sector_start = sector; \
939 cur->mem_sector_end = cur->mem_sector_start + data_secs; \
940 cur->mem_sector_offset = offset; \
941 cur->remap_sector_start = 0; \
942 cur->remap_sector_end = 0; \
943 cur->org_sector_start = 0; \
944 grub_memcpy(override + offset, new_data, new_len); \
945 cur++; \
946 sector += data_secs; \
947 offset += new_len; \
948 chain->virt_img_size_in_bytes += data_secs * 2048; \
949 }
950
951 char * ventoy_get_line(char *start);
952 int ventoy_cmp_img(img_info *img1, img_info *img2);
953 void ventoy_swap_img(img_info *img1, img_info *img2);
954 char * ventoy_plugin_get_cur_install_template(const char *isopath);
955 install_template * ventoy_plugin_find_install_template(const char *isopath);
956 persistence_config * ventoy_plugin_find_persistent(const char *isopath);
957 void ventoy_plugin_dump_injection(void);
958 void ventoy_plugin_dump_auto_install(void);
959 int ventoy_fill_windows_rtdata(void *buf, char *isopath);
960 int ventoy_plugin_get_persistent_chunklist(const char *isopath, int index, ventoy_img_chunk_list *chunk_list);
961 const char * ventoy_plugin_get_injection(const char *isopath);
962 const char * ventoy_plugin_get_menu_alias(int type, const char *isopath);
963 const char * ventoy_plugin_get_menu_class(int type, const char *name);
964 int ventoy_plugin_check_memdisk(const char *isopath);
965 int ventoy_plugin_get_image_list_index(int type, const char *name);
966 conf_replace * ventoy_plugin_find_conf_replace(const char *iso);
967 dud * ventoy_plugin_find_dud(const char *iso);
968 int ventoy_plugin_load_dud(dud *node, const char *isopart);
969 int ventoy_get_block_list(grub_file_t file, ventoy_img_chunk_list *chunklist, grub_disk_addr_t start);
970 int ventoy_check_block_list(grub_file_t file, ventoy_img_chunk_list *chunklist, grub_disk_addr_t start);
971 void ventoy_plugin_dump_persistence(void);
972 grub_err_t ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt, int argc, char **args);
973 grub_err_t ventoy_cmd_check_password(grub_extcmd_context_t ctxt, int argc, char **args);
974 grub_err_t ventoy_cmd_linux_get_main_initrd_index(grub_extcmd_context_t ctxt, int argc, char **args);
975 grub_err_t ventoy_cmd_collect_wim_patch(grub_extcmd_context_t ctxt, int argc, char **args);
976 grub_err_t ventoy_cmd_wim_patch_count(grub_extcmd_context_t ctxt, int argc, char **args);
977 grub_err_t ventoy_cmd_locate_wim_patch(grub_extcmd_context_t ctxt, int argc, char **args);
978 grub_err_t ventoy_cmd_unix_chain_data(grub_extcmd_context_t ctxt, int argc, char **args);
979 int ventoy_get_disk_guid(const char *filename, grub_uint8_t *guid, grub_uint8_t *signature);
980 grub_err_t ventoy_cmd_unix_reset(grub_extcmd_context_t ctxt, int argc, char **args);
981 grub_err_t ventoy_cmd_unix_replace_conf(grub_extcmd_context_t ctxt, int argc, char **args);
982 grub_err_t ventoy_cmd_unix_replace_ko(grub_extcmd_context_t ctxt, int argc, char **args);
983 grub_err_t ventoy_cmd_unix_fill_image_desc(grub_extcmd_context_t ctxt, int argc, char **args);
984 grub_err_t ventoy_cmd_unix_gzip_newko(grub_extcmd_context_t ctxt, int argc, char **args);
985 grub_err_t ventoy_cmd_unix_freebsd_ver(grub_extcmd_context_t ctxt, int argc, char **args);
986 grub_err_t ventoy_cmd_parse_freenas_ver(grub_extcmd_context_t ctxt, int argc, char **args);
987 grub_err_t ventoy_cmd_unix_freebsd_ver_elf(grub_extcmd_context_t ctxt, int argc, char **args);
988 int ventoy_check_device_result(int ret);
989 int ventoy_check_device(grub_device_t dev);
990 void ventoy_debug_dump_guid(const char *prefix, grub_uint8_t *guid);
991 grub_err_t ventoy_cmd_load_vhdboot(grub_extcmd_context_t ctxt, int argc, char **args);
992 grub_err_t ventoy_cmd_patch_vhdboot(grub_extcmd_context_t ctxt, int argc, char **args);
993 grub_err_t ventoy_cmd_raw_chain_data(grub_extcmd_context_t ctxt, int argc, char **args);
994 grub_err_t ventoy_cmd_get_vtoy_type(grub_extcmd_context_t ctxt, int argc, char **args);
995 int ventoy_check_password(const vtoy_password *pwd, int retry);
996 int ventoy_gzip_compress(void *mem_in, int mem_in_len, void *mem_out, int mem_out_len);
997 grub_uint64_t ventoy_get_part1_size(ventoy_gpt_info *gpt);
998 int ventoy_plugin_add_custom_boot(const char *vcfgpath);
999 const char * ventoy_plugin_get_custom_boot(const char *isopath);
1000 grub_err_t ventoy_cmd_dump_custom_boot(grub_extcmd_context_t ctxt, int argc, char **args);
1001
1002 #endif /* __VENTOY_DEF_H__ */
1003