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