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