1 /******************************************************************************
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
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.
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.
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/>.
21 #ifndef __VENTOY_DEF_H__
22 #define __VENTOY_DEF_H__
24 #define VTOY_MAX_SCRIPT_BUF (4 * 1024 * 1024)
26 #define VTOY_FILT_MIN_FILE_SIZE 32768
28 #define VTOY_SIZE_1GB 1073741824
29 #define VTOY_SIZE_512KB (512 * 1024)
31 #define JSON_SUCCESS 0
33 #define JSON_NOT_FOUND 2
35 #define ulong unsigned long
36 #define ulonglong unsigned long long
38 #define vtoy_to_upper(c) (((char)(c) >= 'a' && (char)(c) <= 'z') ? ((char)(c) - 'a' + 'A') : (char)(c))
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)
43 #define ventoy_env_op1(op, a) grub_env_##op(a)
44 #define ventoy_env_op2(op, a, b) grub_env_##op((a), (b))
46 #define ventoy_get_env(key) ventoy_env_op1(get, key)
47 #define ventoy_set_env(key, val) ventoy_env_op2(set, key, val)
49 typedef struct ventoy_initrd_ctx
51 const char *path_prefix
;
52 const char *dir_prefix
;
55 typedef struct cmd_para
58 grub_extcmd_func_t func
;
59 grub_command_flags_t flags
;
60 const struct grub_arg_option
*parser
;
63 const char *description
;
68 #define ventoy_align_2k(value) ((value + 2047) / 2048 * 2048)
69 #define ventoy_align(value, align) (((value) + ((align) - 1)) & (~((align) - 1)))
72 typedef struct cpio_newc_header
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; }
96 typedef int (*grub_char_check_func
)(int c
);
97 #define ventoy_is_decimal(str) ventoy_string_check(str, grub_isdigit)
100 // El Torito Boot Record Volume Descriptor
102 typedef struct eltorito_descriptor
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
;
112 typedef struct ventoy_iso9660_override
114 grub_uint32_t first_sector
;
115 grub_uint32_t first_sector_be
;
117 grub_uint32_t size_be
;
118 }ventoy_iso9660_override
;
120 typedef struct ventoy_udf_override
122 grub_uint32_t length
;
123 grub_uint32_t position
;
124 }ventoy_udf_override
;
128 #define img_type_iso 0
129 #define img_type_wim 1
131 typedef struct img_info
146 struct img_info
*next
;
147 struct img_info
*prev
;
150 typedef struct img_iterator_node
152 struct img_iterator_node
*next
;
160 struct img_iterator_node
*parent
;
161 struct img_iterator_node
*firstchild
;
168 typedef struct initrd_info
172 grub_uint64_t offset
;
175 grub_uint8_t iso_type
; // 0: iso9660 1:udf
176 grub_uint32_t udf_start_block
;
178 grub_uint64_t override_offset
;
179 grub_uint32_t override_length
;
180 char override_data
[32];
182 struct initrd_info
*next
;
183 struct initrd_info
*prev
;
186 extern initrd_info
*g_initrd_img_list
;
187 extern initrd_info
*g_initrd_img_tail
;
188 extern int g_initrd_img_count
;
189 extern int g_valid_initrd_count
;
191 extern img_info
*g_ventoy_img_list
;
192 extern int g_ventoy_img_count
;
194 extern grub_uint8_t
*g_ventoy_cpio_buf
;
195 extern grub_uint32_t g_ventoy_cpio_size
;
196 extern cpio_newc_header
*g_ventoy_initrd_head
;
197 extern grub_uint8_t
*g_ventoy_runtime_buf
;
199 extern ventoy_guid g_ventoy_guid
;
201 extern ventoy_img_chunk_list g_img_chunk_list
;
202 extern ventoy_img_chunk_list g_wimiso_chunk_list
;
203 extern char *g_wimiso_path
;
205 extern int g_ventoy_debug
;
206 void ventoy_debug(const char *fmt
, ...);
207 #define debug(fmt, ...) if (g_ventoy_debug) ventoy_debug("[VTOY]: "fmt, __VA_ARGS__)
209 #define vtoy_ssprintf(buf, pos, fmt, ...) \
210 pos += grub_snprintf(buf + pos, VTOY_MAX_SCRIPT_BUF - pos, fmt, __VA_ARGS__)
212 #define FLAG_HEADER_RESERVED 0x00000001
213 #define FLAG_HEADER_COMPRESSION 0x00000002
214 #define FLAG_HEADER_READONLY 0x00000004
215 #define FLAG_HEADER_SPANNED 0x00000008
216 #define FLAG_HEADER_RESOURCE_ONLY 0x00000010
217 #define FLAG_HEADER_METADATA_ONLY 0x00000020
218 #define FLAG_HEADER_WRITE_IN_PROGRESS 0x00000040
219 #define FLAG_HEADER_RP_FIX 0x00000080 // reparse point fixup
220 #define FLAG_HEADER_COMPRESS_RESERVED 0x00010000
221 #define FLAG_HEADER_COMPRESS_XPRESS 0x00020000
222 #define FLAG_HEADER_COMPRESS_LZX 0x00040000
224 #define RESHDR_FLAG_FREE 0x01
225 #define RESHDR_FLAG_METADATA 0x02
226 #define RESHDR_FLAG_COMPRESSED 0x04
227 #define RESHDR_FLAG_SPANNED 0x08
231 /* A WIM resource header */
232 typedef struct wim_resource_header
234 grub_uint64_t size_in_wim
:56; /* Compressed length */
235 grub_uint64_t flags
:8; /* flags */
236 grub_uint64_t offset
; /* Offset */
237 grub_uint64_t raw_size
; /* Uncompressed length */
238 }wim_resource_header
;
240 /* WIM resource header length mask */
241 #define WIM_RESHDR_ZLEN_MASK 0x00ffffffffffffffULL
243 /* WIM resource header flags */
244 typedef enum wim_resource_header_flags
246 WIM_RESHDR_METADATA
= ( 0x02ULL
<< 56 ), /* Resource contains metadata */
247 WIM_RESHDR_COMPRESSED
= ( 0x04ULL
<< 56 ), /* Resource is compressed */
248 WIM_RESHDR_PACKED_STREAMS
= ( 0x10ULL
<< 56 ), /* Resource is compressed using packed streams */
249 }wim_resource_header_flags
;
251 #define WIM_HEAD_SIGNATURE "MSWIM\0\0"
254 typedef struct wim_header
256 grub_uint8_t signature
[8]; /* Signature */
257 grub_uint32_t header_len
; /* Header length */
258 grub_uint32_t version
; /* Verson */
259 grub_uint32_t flags
; /* Flags */
260 grub_uint32_t chunk_len
; /* Chunk length */
261 grub_uint8_t guid
[16]; /* GUID */
262 grub_uint16_t part
; /* Part number */
263 grub_uint16_t parts
; /* Total number of parts */
264 grub_uint32_t images
; /* number of images */
265 wim_resource_header lookup
; /* Lookup table */
266 wim_resource_header xml
; /* XML data */
267 wim_resource_header metadata
; /* Boot metadata */
268 grub_uint32_t boot_index
; /* Boot index */
269 wim_resource_header integrity
; /* Integrity table */
270 grub_uint8_t reserved
[60]; /* Reserved */
273 /* WIM header flags */
274 typedef enum wim_header_flags
276 WIM_HDR_XPRESS
= 0x00020000, /* WIM uses Xpress compresson */
277 WIM_HDR_LZX
= 0x00040000, /* WIM uses LZX compression */
280 /* A WIM file hash */
281 typedef struct wim_hash
284 grub_uint8_t sha1
[20];
287 /* A WIM lookup table entry */
288 typedef struct wim_lookup_entry
290 wim_resource_header resource
; /* Resource header */
291 grub_uint16_t part
; /* Part number */
292 grub_uint32_t refcnt
; /* Reference count */
293 wim_hash hash
; /* Hash */
296 /* WIM chunk length */
297 #define WIM_CHUNK_LEN 32768
299 /* A WIM chunk buffer */
300 typedef struct wim_chunk_buffer
302 grub_uint8_t data
[WIM_CHUNK_LEN
]; /*Data */
306 typedef struct wim_security_header
308 grub_uint32_t len
; /* Length */
309 grub_uint32_t count
; /* Number of entries */
310 }wim_security_header
;
312 /* Directory entry */
313 typedef struct wim_directory_entry
315 grub_uint64_t len
; /* Length */
316 grub_uint32_t attributes
; /* Attributes */
317 grub_uint32_t security
; /* Security ID */
318 grub_uint64_t subdir
; /* Subdirectory offset */
319 grub_uint8_t reserved1
[16]; /* Reserved */
320 grub_uint64_t created
; /* Creation time */
321 grub_uint64_t accessed
; /* Last access time */
322 grub_uint64_t written
; /* Last written time */
323 wim_hash hash
; /* Hash */
324 grub_uint8_t reserved2
[12]; /* Reserved */
325 grub_uint16_t streams
; /* Streams */
326 grub_uint16_t short_name_len
; /* Short name length */
327 grub_uint16_t name_len
; /* Name length */
328 }wim_directory_entry
;
331 #define WIM_ATTR_NORMAL 0x00000080UL
333 /** No security information exists for this file */
334 #define WIM_NO_SECURITY 0xffffffffUL
339 typedef struct wim_tail
341 grub_uint32_t wim_raw_size
;
342 grub_uint32_t wim_align_size
;
344 grub_uint8_t iso_type
;
345 grub_uint64_t file_offset
;
346 grub_uint32_t udf_start_block
;
347 grub_uint64_t fe_entry_size_offset
;
348 grub_uint64_t override_offset
;
349 grub_uint32_t override_len
;
350 grub_uint8_t override_data
[32];
352 wim_header wim_header
;
355 grub_uint32_t jump_exe_len
;
356 grub_uint8_t
*jump_bin_data
;
357 grub_uint32_t bin_raw_len
;
358 grub_uint32_t bin_align_len
;
360 grub_uint8_t
*new_meta_data
;
361 grub_uint32_t new_meta_len
;
362 grub_uint32_t new_meta_align_len
;
364 grub_uint8_t
*new_lookup_data
;
365 grub_uint32_t new_lookup_len
;
366 grub_uint32_t new_lookup_align_len
;
369 typedef struct wim_patch
376 wim_lookup_entry
*replace_look
;
380 struct wim_patch
*next
;
384 typedef enum _JSON_TYPE
386 JSON_TYPE_NUMBER
= 0,
396 typedef struct _VTOY_JSON
398 struct _VTOY_JSON
*pstPrev
;
399 struct _VTOY_JSON
*pstNext
;
400 struct _VTOY_JSON
*pstChild
;
402 JSON_TYPE enDataType
;
407 grub_uint64_t lValue
;
413 typedef struct _JSON_PARSE
417 grub_uint32_t uiBufSize
;
420 #define JSON_NEW_ITEM(pstJson, ret) \
422 (pstJson) = (VTOY_JSON *)grub_zalloc(sizeof(VTOY_JSON)); \
423 if (NULL == (pstJson)) \
425 json_debug("Failed to alloc memory for json.\n"); \
430 typedef int (*ventoy_plugin_entry_pf
)(VTOY_JSON
*json
, const char *isodisk
);
431 typedef int (*ventoy_plugin_check_pf
)(VTOY_JSON
*json
, const char *isodisk
);
433 typedef struct plugin_entry
436 ventoy_plugin_entry_pf entryfunc
;
437 ventoy_plugin_check_pf checkfunc
;
441 void ventoy_fill_os_param(grub_file_t file
, ventoy_os_param
*param
);
442 grub_err_t
ventoy_cmd_isolinux_initrd_collect(grub_extcmd_context_t ctxt
, int argc
, char **args
);
443 grub_err_t
ventoy_cmd_grub_initrd_collect(grub_extcmd_context_t ctxt
, int argc
, char **args
);
444 grub_err_t
ventoy_cmd_specify_initrd_file(grub_extcmd_context_t ctxt
, int argc
, char **args
);
445 grub_err_t
ventoy_cmd_dump_initrd_list(grub_extcmd_context_t ctxt
, int argc
, char **args
);
446 grub_err_t
ventoy_cmd_clear_initrd_list(grub_extcmd_context_t ctxt
, int argc
, char **args
);
447 grub_uint32_t
ventoy_get_iso_boot_catlog(grub_file_t file
);
448 int ventoy_has_efi_eltorito(grub_file_t file
, grub_uint32_t sector
);
449 grub_err_t
ventoy_cmd_linux_chain_data(grub_extcmd_context_t ctxt
, int argc
, char **args
);
450 grub_err_t
ventoy_cmd_linux_locate_initrd(grub_extcmd_context_t ctxt
, int argc
, char **args
);
451 grub_err_t
ventoy_cmd_initrd_count(grub_extcmd_context_t ctxt
, int argc
, char **args
);
452 grub_err_t
ventoy_cmd_valid_initrd_count(grub_extcmd_context_t ctxt
, int argc
, char **args
);
453 grub_err_t
ventoy_cmd_load_cpio(grub_extcmd_context_t ctxt
, int argc
, char **args
);
454 int ventoy_cpio_newc_fill_head(void *buf
, int filesize
, const void *filedata
, const char *name
);
455 grub_file_t
ventoy_grub_file_open(enum grub_file_type type
, const char *fmt
, ...);
456 grub_uint64_t
ventoy_grub_get_file_size(const char *fmt
, ...);
457 int ventoy_is_file_exist(const char *fmt
, ...);
458 int ventoy_fill_data(grub_uint32_t buflen
, char *buffer
);
459 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
);
460 grub_err_t
ventoy_cmd_wimdows_reset(grub_extcmd_context_t ctxt
, int argc
, char **args
);
461 grub_err_t
ventoy_cmd_windows_chain_data(grub_extcmd_context_t ctxt
, int argc
, char **args
);
462 grub_err_t
ventoy_cmd_wim_chain_data(grub_extcmd_context_t ctxt
, int argc
, char **args
);
463 grub_err_t
ventoy_cmd_dump_wim_patch(grub_extcmd_context_t ctxt
, int argc
, char **args
);
465 VTOY_JSON
*vtoy_json_find_item
468 JSON_TYPE enDataType
,
471 int vtoy_json_parse_value
479 VTOY_JSON
* vtoy_json_create(void);
480 int vtoy_json_parse(VTOY_JSON
*pstJson
, const char *szJsonData
);
482 int vtoy_json_scan_parse
484 const VTOY_JSON
*pstJson
,
485 grub_uint32_t uiParseNum
,
486 JSON_PARSE
*pstJsonParse
489 int vtoy_json_scan_array
493 VTOY_JSON
**ppstArrayItem
496 int vtoy_json_scan_array_ex
500 VTOY_JSON
**ppstArrayItem
502 int vtoy_json_scan_object
506 VTOY_JSON
**ppstObjectItem
508 int vtoy_json_get_int
514 int vtoy_json_get_uint
518 grub_uint32_t
*puiValue
520 int vtoy_json_get_uint64
524 grub_uint64_t
*pui64Value
526 int vtoy_json_get_bool
530 grub_uint8_t
*pbValue
532 int vtoy_json_get_string
536 grub_uint32_t uiBufLen
,
539 const char * vtoy_json_get_string_ex(VTOY_JSON
*pstJson
, const char *szKey
);
540 int vtoy_json_destroy(VTOY_JSON
*pstJson
);
543 grub_uint32_t CalculateCrc32
546 grub_uint32_t Length
,
547 grub_uint32_t InitValue
550 static inline int ventoy_isspace (int c
)
552 return (c
== '\n' || c
== '\r' || c
== ' ' || c
== '\t');
555 static inline int ventoy_is_word_end(int c
)
557 return (c
== 0 || c
== ',' || ventoy_isspace(c
));
561 typedef struct ventoy_part_table
563 grub_uint8_t Active
; // 0x00 0x80
565 grub_uint8_t StartHead
;
566 grub_uint16_t StartSector
: 6;
567 grub_uint16_t StartCylinder
: 10;
571 grub_uint8_t EndHead
;
572 grub_uint16_t EndSector
: 6;
573 grub_uint16_t EndCylinder
: 10;
575 grub_uint32_t StartSectorId
;
576 grub_uint32_t SectorCount
;
579 typedef struct ventoy_mbr_head
581 grub_uint8_t BootCode
[446];
582 ventoy_part_table PartTbl
[4];
588 typedef struct file_fullpath
593 typedef struct install_template
600 file_fullpath
*templatepath
;
602 struct install_template
*next
;
605 typedef struct persistence_config
612 file_fullpath
*backendpath
;
614 struct persistence_config
*next
;
617 typedef struct menu_alias
623 struct menu_alias
*next
;
626 extern int g_ventoy_menu_esc
;
627 extern int g_ventoy_suppress_esc
;
628 extern int g_ventoy_last_entry
;
629 extern int g_ventoy_memdisk_mode
;
630 extern int g_ventoy_iso_raw
;
631 extern int g_ventoy_iso_uefi_drv
;
632 extern int g_ventoy_case_insensitive
;
633 extern grub_uint8_t g_ventoy_chain_type
;
635 int ventoy_cmp_img(img_info
*img1
, img_info
*img2
);
636 void ventoy_swap_img(img_info
*img1
, img_info
*img2
);
637 char * ventoy_plugin_get_cur_install_template(const char *isopath
);
638 install_template
* ventoy_plugin_find_install_template(const char *isopath
);
639 persistence_config
* ventoy_plugin_find_persistent(const char *isopath
);
640 void ventoy_plugin_dump_auto_install(void);
641 int ventoy_fill_windows_rtdata(void *buf
, char *isopath
);
642 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, int index
, ventoy_img_chunk_list
*chunk_list
);
643 const char * ventoy_plugin_get_menu_alias(const char *isopath
);
644 int ventoy_get_block_list(grub_file_t file
, ventoy_img_chunk_list
*chunklist
, grub_disk_addr_t start
);
645 int ventoy_check_block_list(grub_file_t file
, ventoy_img_chunk_list
*chunklist
, grub_disk_addr_t start
);
646 void ventoy_plugin_dump_persistence(void);
647 grub_err_t
ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt
, int argc
, char **args
);
648 grub_err_t
ventoy_cmd_linux_get_main_initrd_index(grub_extcmd_context_t ctxt
, int argc
, char **args
);
649 grub_err_t
ventoy_cmd_collect_wim_patch(grub_extcmd_context_t ctxt
, int argc
, char **args
);
650 grub_err_t
ventoy_cmd_wim_patch_count(grub_extcmd_context_t ctxt
, int argc
, char **args
);
651 grub_err_t
ventoy_cmd_locate_wim_patch(grub_extcmd_context_t ctxt
, int argc
, char **args
);
653 #endif /* __VENTOY_DEF_H__ */