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