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_SIZE_1GB 1073741824
28 #define JSON_SUCCESS 0
30 #define JSON_NOT_FOUND 2
32 #define ulong unsigned long
33 #define ulonglong unsigned long long
35 #define vtoy_to_upper(c) (((char)(c) >= 'a' && (char)(c) <= 'z') ? ((char)(c) - 'a' + 'A') : (char)(c))
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)
40 #define ventoy_env_op1(op, a) grub_env_##op(a)
41 #define ventoy_env_op2(op, a, b) grub_env_##op((a), (b))
43 #define ventoy_get_env(key) ventoy_env_op1(get, key)
44 #define ventoy_set_env(key, val) ventoy_env_op2(set, key, val)
46 typedef struct ventoy_initrd_ctx
48 const char *path_prefix
;
49 const char *dir_prefix
;
52 typedef struct cmd_para
55 grub_extcmd_func_t func
;
56 grub_command_flags_t flags
;
57 const struct grub_arg_option
*parser
;
60 const char *description
;
65 #define ventoy_align(value, align) (((value) + ((align) - 1)) & (~((align) - 1)))
68 typedef struct cpio_newc_header
88 #define cmd_raw_name ctxt->extcmd->cmd->name
89 #define check_free(p, func) if (p) { func(p); p = NULL; }
91 typedef int (*grub_char_check_func
)(int c
);
92 #define ventoy_is_decimal(str) ventoy_string_check(str, grub_isdigit)
95 // El Torito Boot Record Volume Descriptor
97 typedef struct eltorito_descriptor
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
;
107 typedef struct ventoy_iso9660_override
109 grub_uint32_t first_sector
;
110 grub_uint32_t first_sector_be
;
112 grub_uint32_t size_be
;
113 }ventoy_iso9660_override
;
115 typedef struct ventoy_udf_override
117 grub_uint32_t length
;
118 grub_uint32_t position
;
119 }ventoy_udf_override
;
123 typedef struct img_info
133 struct img_info
*next
;
134 struct img_info
*prev
;
137 typedef struct img_iterator_node
139 struct img_iterator_node
*next
;
147 struct img_iterator_node
*parent
;
148 struct img_iterator_node
*firstchild
;
155 typedef struct initrd_info
159 grub_uint64_t offset
;
162 grub_uint8_t iso_type
; // 0: iso9660 1:udf
163 grub_uint32_t udf_start_block
;
165 grub_uint64_t override_offset
;
166 grub_uint32_t override_length
;
167 char override_data
[32];
169 struct initrd_info
*next
;
170 struct initrd_info
*prev
;
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
;
178 extern img_info
*g_ventoy_img_list
;
179 extern int g_ventoy_img_count
;
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
;
186 extern ventoy_guid g_ventoy_guid
;
188 extern ventoy_img_chunk_list g_img_chunk_list
;
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__)
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
208 #define RESHDR_FLAG_FREE 0x01
209 #define RESHDR_FLAG_METADATA 0x02
210 #define RESHDR_FLAG_COMPRESSED 0x04
211 #define RESHDR_FLAG_SPANNED 0x08
215 /* A WIM resource header */
216 typedef struct wim_resource_header
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
;
224 /* WIM resource header length mask */
225 #define WIM_RESHDR_ZLEN_MASK 0x00ffffffffffffffULL
227 /* WIM resource header flags */
228 typedef enum wim_resource_header_flags
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
;
235 #define WIM_HEAD_SIGNATURE "MSWIM\0\0"
238 typedef struct wim_header
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 */
257 /* WIM header flags */
258 typedef enum wim_header_flags
260 WIM_HDR_XPRESS
= 0x00020000, /* WIM uses Xpress compresson */
261 WIM_HDR_LZX
= 0x00040000, /* WIM uses LZX compression */
264 /* A WIM file hash */
265 typedef struct wim_hash
268 grub_uint8_t sha1
[20];
271 /* A WIM lookup table entry */
272 typedef struct wim_lookup_entry
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 */
280 /* WIM chunk length */
281 #define WIM_CHUNK_LEN 32768
283 /* A WIM chunk buffer */
284 typedef struct wim_chunk_buffer
286 grub_uint8_t data
[WIM_CHUNK_LEN
]; /*Data */
290 typedef struct wim_security_header
292 grub_uint32_t len
; /* Length */
293 grub_uint32_t count
; /* Number of entries */
294 }wim_security_header
;
296 /* Directory entry */
297 typedef struct wim_directory_entry
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
;
315 #define WIM_ATTR_NORMAL 0x00000080UL
317 /** No security information exists for this file */
318 #define WIM_NO_SECURITY 0xffffffffUL
323 typedef struct wim_tail
325 grub_uint32_t wim_raw_size
;
326 grub_uint32_t wim_align_size
;
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];
336 wim_header wim_header
;
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
;
344 grub_uint8_t
*new_meta_data
;
345 grub_uint32_t new_meta_len
;
346 grub_uint32_t new_meta_align_len
;
348 grub_uint8_t
*new_lookup_data
;
349 grub_uint32_t new_lookup_len
;
350 grub_uint32_t new_lookup_align_len
;
355 typedef enum _JSON_TYPE
357 JSON_TYPE_NUMBER
= 0,
367 typedef struct _VTOY_JSON
369 struct _VTOY_JSON
*pstPrev
;
370 struct _VTOY_JSON
*pstNext
;
371 struct _VTOY_JSON
*pstChild
;
373 JSON_TYPE enDataType
;
378 grub_uint64_t lValue
;
384 typedef struct _JSON_PARSE
388 grub_uint32_t uiBufSize
;
391 #define JSON_NEW_ITEM(pstJson, ret) \
393 (pstJson) = (VTOY_JSON *)grub_zalloc(sizeof(VTOY_JSON)); \
394 if (NULL == (pstJson)) \
396 json_debug("Failed to alloc memory for json.\n"); \
401 typedef int (*ventoy_plugin_entry_pf
)(VTOY_JSON
*json
, const char *isodisk
);
403 typedef struct plugin_entry
406 ventoy_plugin_entry_pf entryfunc
;
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
);
432 VTOY_JSON
*vtoy_json_find_item
435 JSON_TYPE enDataType
,
438 int vtoy_json_parse_value
446 VTOY_JSON
* vtoy_json_create(void);
447 int vtoy_json_parse(VTOY_JSON
*pstJson
, const char *szJsonData
);
449 int vtoy_json_scan_parse
451 const VTOY_JSON
*pstJson
,
452 grub_uint32_t uiParseNum
,
453 JSON_PARSE
*pstJsonParse
456 int vtoy_json_scan_array
460 VTOY_JSON
**ppstArrayItem
463 int vtoy_json_scan_array_ex
467 VTOY_JSON
**ppstArrayItem
469 int vtoy_json_scan_object
473 VTOY_JSON
**ppstObjectItem
475 int vtoy_json_get_int
481 int vtoy_json_get_uint
485 grub_uint32_t
*puiValue
487 int vtoy_json_get_uint64
491 grub_uint64_t
*pui64Value
493 int vtoy_json_get_bool
497 grub_uint8_t
*pbValue
499 int vtoy_json_get_string
503 grub_uint32_t uiBufLen
,
506 const char * vtoy_json_get_string_ex(VTOY_JSON
*pstJson
, const char *szKey
);
507 int vtoy_json_destroy(VTOY_JSON
*pstJson
);
510 grub_uint32_t CalculateCrc32
513 grub_uint32_t Length
,
514 grub_uint32_t InitValue
517 static inline int ventoy_isspace (int c
)
519 return (c
== '\n' || c
== '\r' || c
== ' ' || c
== '\t');
522 static inline int ventoy_is_word_end(int c
)
524 return (c
== 0 || c
== ',' || ventoy_isspace(c
));
528 typedef struct ventoy_part_table
530 grub_uint8_t Active
; // 0x00 0x80
532 grub_uint8_t StartHead
;
533 grub_uint16_t StartSector
: 6;
534 grub_uint16_t StartCylinder
: 10;
538 grub_uint8_t EndHead
;
539 grub_uint16_t EndSector
: 6;
540 grub_uint16_t EndCylinder
: 10;
542 grub_uint32_t StartSectorId
;
543 grub_uint32_t SectorCount
;
546 typedef struct ventoy_mbr_head
548 grub_uint8_t BootCode
[446];
549 ventoy_part_table PartTbl
[4];
556 typedef struct install_template
559 char templatepath
[256];
561 struct install_template
*next
;
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
;
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
);
576 #endif /* __VENTOY_DEF_H__ */