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 #include <grub/types.h>
22 #include <grub/misc.h>
26 #include <grub/disk.h>
27 #include <grub/device.h>
28 #include <grub/term.h>
29 #include <grub/partition.h>
30 #include <grub/file.h>
31 #include <grub/normal.h>
32 #include <grub/extcmd.h>
33 #include <grub/datetime.h>
34 #include <grub/i18n.h>
36 #include <grub/time.h>
37 #include <grub/crypto.h>
38 #include <grub/ventoy.h>
39 #include "ventoy_def.h"
41 GRUB_MOD_LICENSE ("GPLv3+");
43 static int g_iso_fs_type
= 0;
44 static int g_wim_total_patch_count
= 0;
45 static int g_wim_valid_patch_count
= 0;
46 static wim_patch
*g_wim_patch_head
= NULL
;
48 static grub_uint64_t g_suppress_wincd_override_offset
= 0;
49 static grub_uint32_t g_suppress_wincd_override_data
= 0;
51 grub_uint8_t g_temp_buf
[512];
53 grub_ssize_t
lzx_decompress ( const void *data
, grub_size_t len
, void *buf
);
54 grub_ssize_t
xca_decompress ( const void *data
, grub_size_t len
, void *buf
);
56 static wim_patch
*ventoy_find_wim_patch(const char *path
)
58 int len
= (int)grub_strlen(path
);
59 wim_patch
*node
= g_wim_patch_head
;
63 if (len
== node
->pathlen
&& 0 == grub_strcmp(path
, node
->path
))
73 static int ventoy_collect_wim_patch(const char *bcdfile
)
78 grub_file_t file
= NULL
;
80 wim_patch
*node
= NULL
;
86 g_ventoy_case_insensitive
= 1;
87 file
= grub_file_open(bcdfile
, VENTOY_FILE_TYPE
);
88 g_ventoy_case_insensitive
= 0;
91 debug("Failed to open file %s\n", bcdfile
);
96 buf
= grub_malloc(file
->size
+ 8);
102 grub_file_read(file
, buf
, file
->size
);
104 for (i
= 0; i
< (int)file
->size
- 8; i
++)
111 magic
= *(grub_uint64_t
*)(buf
+ i
);
114 if ((magic
== 0x006D00690077002EULL
) ||
115 (magic
== 0x004D00490057002EULL
) ||
116 (magic
== 0x006D00690057002EULL
))
118 for (j
= i
; j
> 0; j
-= 2)
120 if (*(grub_uint16_t
*)(buf
+ j
) == 0)
128 byte
= (grub_uint8_t
)(*(grub_uint16_t
*)(buf
+ j
+ 2));
129 if (byte
!= '/' && byte
!= '\\')
135 for (k
= 0, j
+= 2; k
< (int)sizeof(path
) - 1 && j
< i
+ 8; j
+= 2)
137 byte
= (grub_uint8_t
)(*(grub_uint16_t
*)(buf
+ j
));
139 if (byte
> '~' || byte
< ' ') /* not printable */
153 debug("@@@@ Find wim flag:<%s>\n", path
);
157 debug("Invalid wim file %d\n", k
);
159 else if (NULL
== ventoy_find_wim_patch(path
))
161 node
= grub_zalloc(sizeof(wim_patch
));
164 node
->pathlen
= grub_snprintf(node
->path
, sizeof(node
->path
), "%s", path
);
166 debug("add patch <%s>\n", path
);
168 if (g_wim_patch_head
)
170 node
->next
= g_wim_patch_head
;
172 g_wim_patch_head
= node
;
174 g_wim_total_patch_count
++;
179 debug("wim <%s> already exist\n", path
);
186 check_free(file
, grub_file_close
);
187 grub_check_free(buf
);
191 grub_err_t
ventoy_cmd_wim_patch_count(grub_extcmd_context_t ctxt
, int argc
, char **args
)
201 grub_snprintf(buf
, sizeof(buf
), "%d", g_wim_total_patch_count
);
202 ventoy_set_env(args
[0], buf
);
208 grub_err_t
ventoy_cmd_collect_wim_patch(grub_extcmd_context_t ctxt
, int argc
, char **args
)
210 wim_patch
*node
= NULL
;
221 debug("ventoy_cmd_collect_wim_patch %s %s\n", args
[0], args
[1]);
223 if (grub_strcmp(args
[0], "bcd") == 0)
225 ventoy_collect_wim_patch(args
[1]);
229 if (NULL
== ventoy_find_wim_patch(args
[1]))
231 node
= grub_zalloc(sizeof(wim_patch
));
234 node
->pathlen
= grub_snprintf(node
->path
, sizeof(node
->path
), "%s", args
[1]);
236 debug("add patch <%s>\n", args
[1]);
238 if (g_wim_patch_head
)
240 node
->next
= g_wim_patch_head
;
242 g_wim_patch_head
= node
;
244 g_wim_total_patch_count
++;
252 grub_err_t
ventoy_cmd_dump_wim_patch(grub_extcmd_context_t ctxt
, int argc
, char **args
)
255 wim_patch
*node
= NULL
;
261 for (node
= g_wim_patch_head
; node
; node
= node
->next
)
263 grub_printf("%d %s [%s]\n", i
++, node
->path
, node
->valid
? "SUCCESS" : "FAIL");
270 static int wim_name_cmp(const char *search
, grub_uint16_t
*name
, grub_uint16_t namelen
)
272 char c1
= vtoy_to_upper(*search
);
273 char c2
= vtoy_to_upper(*name
);
275 while (namelen
> 0 && (c1
== c2
))
281 c1
= vtoy_to_upper(*search
);
282 c2
= vtoy_to_upper(*name
);
285 if (namelen
== 0 && *search
== 0)
293 static int ventoy_is_pe64(grub_uint8_t
*buffer
)
295 grub_uint32_t pe_off
;
297 if (buffer
[0] != 'M' || buffer
[1] != 'Z')
302 pe_off
= *(grub_uint32_t
*)(buffer
+ 60);
304 if (buffer
[pe_off
] != 'P' || buffer
[pe_off
+ 1] != 'E')
309 if (*(grub_uint16_t
*)(buffer
+ pe_off
+ 24) == 0x020b)
317 grub_err_t
ventoy_cmd_is_pe64(grub_extcmd_context_t ctxt
, int argc
, char **args
)
321 grub_uint8_t buf
[512];
326 file
= grub_file_open(args
[0], VENTOY_FILE_TYPE
);
332 grub_memset(buf
, 0, 512);
333 grub_file_read(file
, buf
, 512);
334 if (ventoy_is_pe64(buf
))
336 debug("%s is PE64\n", args
[0]);
341 debug("%s is PE32\n", args
[0]);
343 grub_file_close(file
);
348 grub_err_t
ventoy_cmd_sel_wimboot(grub_extcmd_context_t ctxt
, int argc
, char **args
)
352 char configfile
[128];
358 debug("select wimboot argc:%d\n", argc
);
360 buf
= (char *)grub_malloc(8192);
366 size
= (int)grub_snprintf(buf
, 8192,
367 "menuentry \"Windows Setup (32-bit)\" {\n"
368 " set vtoy_wimboot_sel=32\n"
370 "menuentry \"Windows Setup (64-bit)\" {\n"
371 " set vtoy_wimboot_sel=64\n"
376 g_ventoy_menu_esc
= 1;
377 g_ventoy_suppress_esc
= 1;
378 g_ventoy_suppress_esc_default
= 1;
380 grub_snprintf(configfile
, sizeof(configfile
), "configfile mem:0x%llx:size:%d", (ulonglong
)(ulong
)buf
, size
);
381 grub_script_execute_sourcecode(configfile
);
383 g_ventoy_menu_esc
= 0;
384 g_ventoy_suppress_esc
= 0;
388 if (g_ventoy_last_entry
== 0)
390 debug("last entry=%d %s=32\n", g_ventoy_last_entry
, args
[0]);
391 grub_env_set(args
[0], "32");
395 debug("last entry=%d %s=64\n", g_ventoy_last_entry
, args
[0]);
396 grub_env_set(args
[0], "64");
399 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
402 grub_err_t
ventoy_cmd_wimdows_reset(grub_extcmd_context_t ctxt
, int argc
, char **args
)
404 wim_patch
*next
= NULL
;
405 wim_patch
*node
= g_wim_patch_head
;
418 g_wim_patch_head
= NULL
;
419 g_wim_total_patch_count
= 0;
420 g_wim_valid_patch_count
= 0;
425 static int ventoy_load_jump_exe(const char *path
, grub_uint8_t
**data
, grub_uint32_t
*size
, wim_hash
*hash
)
431 debug("windows load jump %s\n", path
);
433 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s", path
);
436 debug("Can't open file %s\n", path
);
440 align
= ventoy_align((int)file
->size
, 2048);
442 debug("file %s size:%d align:%u\n", path
, (int)file
->size
, align
);
444 *size
= (grub_uint32_t
)file
->size
;
445 *data
= (grub_uint8_t
*)grub_malloc(align
);
448 debug("Failed to alloc memory size %u\n", align
);
452 grub_file_read(file
, (*data
), file
->size
);
456 grub_crypto_hash(GRUB_MD_SHA1
, hash
->sha1
, (*data
), file
->size
);
460 debug("%s", "jump bin 64 hash: ");
461 for (i
= 0; i
< sizeof(hash
->sha1
); i
++)
463 ventoy_debug("%02x ", hash
->sha1
[i
]);
471 grub_file_close(file
);
475 static int ventoy_get_override_info(grub_file_t file
, wim_tail
*wim_data
)
477 grub_uint32_t start_block
;
478 grub_uint64_t file_offset
;
479 grub_uint64_t override_offset
;
480 grub_uint32_t override_len
;
481 grub_uint64_t fe_entry_size_offset
;
483 if (grub_strcmp(file
->fs
->name
, "iso9660") == 0)
485 g_iso_fs_type
= wim_data
->iso_type
= 0;
486 override_len
= sizeof(ventoy_iso9660_override
);
487 override_offset
= grub_iso9660_get_last_file_dirent_pos(file
) + 2;
489 grub_file_read(file
, &start_block
, 1); // just read for hook trigger
490 file_offset
= grub_iso9660_get_last_read_pos(file
);
492 debug("iso9660 wim size:%llu override_offset:%llu file_offset:%llu\n",
493 (ulonglong
)file
->size
, (ulonglong
)override_offset
, (ulonglong
)file_offset
);
497 g_iso_fs_type
= wim_data
->iso_type
= 1;
498 override_len
= sizeof(ventoy_udf_override
);
499 override_offset
= grub_udf_get_last_file_attr_offset(file
, &start_block
, &fe_entry_size_offset
);
501 file_offset
= grub_udf_get_file_offset(file
);
503 debug("UDF wim size:%llu override_offset:%llu file_offset:%llu start_block=%u\n",
504 (ulonglong
)file
->size
, (ulonglong
)override_offset
, (ulonglong
)file_offset
, start_block
);
507 wim_data
->file_offset
= file_offset
;
508 wim_data
->udf_start_block
= start_block
;
509 wim_data
->fe_entry_size_offset
= fe_entry_size_offset
;
510 wim_data
->override_offset
= override_offset
;
511 wim_data
->override_len
= override_len
;
516 static int ventoy_read_resource(grub_file_t fp
, wim_header
*wimhdr
, wim_resource_header
*head
, void **buffer
)
518 int decompress_len
= 0;
519 int total_decompress
= 0;
521 grub_uint32_t chunk_num
= 0;
522 grub_uint32_t chunk_size
= 0;
523 grub_uint32_t last_chunk_size
= 0;
524 grub_uint32_t last_decompress_size
= 0;
525 grub_uint32_t cur_offset
= 0;
526 grub_uint8_t
*cur_dst
= NULL
;
527 grub_uint8_t
*buffer_compress
= NULL
;
528 grub_uint8_t
*buffer_decompress
= NULL
;
529 grub_uint32_t
*chunk_offset
= NULL
;
531 buffer_decompress
= (grub_uint8_t
*)grub_malloc(head
->raw_size
+ head
->size_in_wim
);
532 if (NULL
== buffer_decompress
)
537 grub_file_seek(fp
, head
->offset
);
539 if (head
->size_in_wim
== head
->raw_size
)
541 grub_file_read(fp
, buffer_decompress
, head
->size_in_wim
);
542 *buffer
= buffer_decompress
;
546 buffer_compress
= buffer_decompress
+ head
->raw_size
;
547 grub_file_read(fp
, buffer_compress
, head
->size_in_wim
);
549 chunk_num
= (head
->raw_size
+ WIM_CHUNK_LEN
- 1) / WIM_CHUNK_LEN
;
550 cur_offset
= (chunk_num
- 1) * 4;
551 chunk_offset
= (grub_uint32_t
*)buffer_compress
;
553 //debug("%llu %llu chunk_num=%lu", (ulonglong)head->size_in_wim, (ulonglong)head->raw_size, chunk_num);
555 cur_dst
= buffer_decompress
;
557 for (i
= 0; i
< chunk_num
- 1; i
++)
559 chunk_size
= (i
== 0) ? chunk_offset
[i
] : chunk_offset
[i
] - chunk_offset
[i
- 1];
561 if (WIM_CHUNK_LEN
== chunk_size
)
563 grub_memcpy(cur_dst
, buffer_compress
+ cur_offset
, chunk_size
);
564 decompress_len
= (int)chunk_size
;
568 if (wimhdr
->flags
& FLAG_HEADER_COMPRESS_XPRESS
)
570 decompress_len
= (int)xca_decompress(buffer_compress
+ cur_offset
, chunk_size
, cur_dst
);
574 decompress_len
= (int)lzx_decompress(buffer_compress
+ cur_offset
, chunk_size
, cur_dst
);
578 //debug("chunk_size:%u decompresslen:%d\n", chunk_size, decompress_len);
580 total_decompress
+= decompress_len
;
581 cur_dst
+= decompress_len
;
582 cur_offset
+= chunk_size
;
586 last_chunk_size
= (grub_uint32_t
)(head
->size_in_wim
- cur_offset
);
587 last_decompress_size
= head
->raw_size
- total_decompress
;
589 if (last_chunk_size
< WIM_CHUNK_LEN
&& last_chunk_size
== last_decompress_size
)
591 debug("Last chunk %u uncompressed\n", last_chunk_size
);
592 grub_memcpy(cur_dst
, buffer_compress
+ cur_offset
, last_chunk_size
);
593 decompress_len
= (int)last_chunk_size
;
597 if (wimhdr
->flags
& FLAG_HEADER_COMPRESS_XPRESS
)
599 decompress_len
= (int)xca_decompress(buffer_compress
+ cur_offset
, head
->size_in_wim
- cur_offset
, cur_dst
);
603 decompress_len
= (int)lzx_decompress(buffer_compress
+ cur_offset
, head
->size_in_wim
- cur_offset
, cur_dst
);
607 cur_dst
+= decompress_len
;
608 total_decompress
+= decompress_len
;
610 //debug("last chunk_size:%u decompresslen:%d tot:%d\n", last_chunk_size, decompress_len, total_decompress);
612 if (cur_dst
!= buffer_decompress
+ head
->raw_size
)
614 debug("head->size_in_wim:%llu head->raw_size:%llu cur_dst:%p buffer_decompress:%p total_decompress:%d\n",
615 (ulonglong
)head
->size_in_wim
, (ulonglong
)head
->raw_size
, cur_dst
, buffer_decompress
, total_decompress
);
616 grub_free(buffer_decompress
);
620 *buffer
= buffer_decompress
;
625 static wim_directory_entry
* search_wim_dirent(wim_directory_entry
*dir
, const char *search_name
)
629 if (dir
->len
&& dir
->name_len
)
631 if (wim_name_cmp(search_name
, (grub_uint16_t
*)(dir
+ 1), dir
->name_len
/ 2) == 0)
636 dir
= (wim_directory_entry
*)((grub_uint8_t
*)dir
+ dir
->len
);
642 static wim_directory_entry
* search_full_wim_dirent
645 wim_directory_entry
*dir
,
649 wim_directory_entry
*subdir
= NULL
;
650 wim_directory_entry
*search
= dir
;
654 subdir
= (wim_directory_entry
*)((char *)meta_data
+ search
->subdir
);
655 search
= search_wim_dirent(subdir
, *path
);
664 static wim_lookup_entry
* ventoy_find_look_entry(wim_header
*header
, wim_lookup_entry
*lookup
, wim_hash
*hash
)
668 for (i
= 0; i
< (grub_uint32_t
)header
->lookup
.raw_size
/ sizeof(wim_lookup_entry
); i
++)
670 if (grub_memcmp(&lookup
[i
].hash
, hash
, sizeof(wim_hash
)) == 0)
679 static int parse_registry_setup_cmdline
683 wim_lookup_entry
*lookup
,
685 wim_directory_entry
*dir
,
693 grub_uint32_t reglen
= 0;
695 reg_vk
*regvk
= NULL
;
696 wim_lookup_entry
*look
= NULL
;
697 wim_directory_entry
*wim_dirent
= NULL
;
698 char *decompress_data
= NULL
;
699 const char *reg_path
[] = { "Windows", "System32", "config", "SYSTEM", NULL
};
701 wim_dirent
= search_full_wim_dirent(meta_data
, dir
, reg_path
);
702 debug("search reg SYSTEM %p\n", wim_dirent
);
708 grub_memset(&zerohash
, 0, sizeof(zerohash
));
709 if (grub_memcmp(&zerohash
, wim_dirent
->hash
.sha1
, sizeof(wim_hash
)) == 0)
714 look
= ventoy_find_look_entry(head
, lookup
, &wim_dirent
->hash
);
720 reglen
= (grub_uint32_t
)look
->resource
.raw_size
;
721 debug("find system lookup entry_id:%ld raw_size:%u\n",
722 ((long)look
- (long)lookup
) / sizeof(wim_lookup_entry
), reglen
);
724 if (0 != ventoy_read_resource(file
, head
, &(look
->resource
), (void **)&(decompress_data
)))
729 if (grub_strncmp(decompress_data
+ 0x1000, "hbin", 4))
734 for (i
= 0x1000; i
+ sizeof(reg_vk
) < reglen
; i
+= 8)
736 regvk
= (reg_vk
*)(decompress_data
+ i
);
737 if (regvk
->sig
== 0x6B76 && regvk
->namesize
== 7 &&
738 regvk
->datatype
== 1 && regvk
->flag
== 1)
740 if (grub_strncasecmp((char *)(regvk
+ 1), "cmdline", 7) == 0)
742 debug("find registry cmdline i:%u offset:(0x%x)%u size:(0x%x)%u\n",
743 i
, regvk
->dataoffset
, regvk
->dataoffset
, regvk
->datasize
, regvk
->datasize
);
749 if (i
+ sizeof(reg_vk
) >= reglen
|| regvk
== NULL
)
754 if (regvk
->datasize
== 0 || (regvk
->datasize
& 0x80000000) > 0 ||
755 regvk
->dataoffset
== 0 || regvk
->dataoffset
== 0xFFFFFFFF)
760 if (regvk
->datasize
/ 2 >= buflen
)
765 debug("start offset is 0x%x(%u)\n", 0x1000 + regvk
->dataoffset
+ 4, 0x1000 + regvk
->dataoffset
+ 4);
767 for (i
= 0; i
< regvk
->datasize
; i
+=2)
769 c
= (char)(*(grub_uint16_t
*)(decompress_data
+ 0x1000 + regvk
->dataoffset
+ 4 + i
));
776 grub_check_free(decompress_data
);
780 static int parse_custom_setup_path(char *cmdline
, const char **path
, char *exefile
)
787 if ((cmdline
[0] == 'x' || cmdline
[0] == 'X') && cmdline
[1] == ':')
789 pos1
= pos2
= cmdline
+ 3;
791 while (i
< VTOY_MAX_DIR_DEPTH
&& *pos2
)
793 while (*pos2
&& *pos2
!= '\\' && *pos2
!= '/')
810 if (i
== 0 || i
>= VTOY_MAX_DIR_DEPTH
)
817 path
[i
++] = "Windows";
818 path
[i
++] = "System32";
822 pos1
= (char *)path
[i
- 1];
823 while (*pos1
!= ' ' && *pos1
!= '\t' && *pos1
)
829 len
= (int)grub_strlen(path
[i
- 1]);
830 if (len
< 4 || grub_strcasecmp(path
[i
- 1] + len
- 4, ".exe") != 0)
832 grub_snprintf(exefile
, 256, "%s.exe", path
[i
- 1]);
833 path
[i
- 1] = exefile
;
837 debug("custom setup: %d <%s>\n", i
, path
[i
- 1]);
841 static wim_directory_entry
* search_replace_wim_dirent
845 wim_lookup_entry
*lookup
,
847 wim_directory_entry
*dir
851 char exefile
[256] = {0};
852 char cmdline
[256] = {0};
853 wim_directory_entry
*wim_dirent
= NULL
;
854 wim_directory_entry
*pecmd_dirent
= NULL
;
855 const char *peset_path
[] = { "Windows", "System32", "peset.exe", NULL
};
856 const char *pecmd_path
[] = { "Windows", "System32", "pecmd.exe", NULL
};
857 const char *winpeshl_path
[] = { "Windows", "System32", "winpeshl.exe", NULL
};
858 const char *custom_path
[VTOY_MAX_DIR_DEPTH
+ 1] = { NULL
};
860 pecmd_dirent
= search_full_wim_dirent(meta_data
, dir
, pecmd_path
);
861 debug("search pecmd.exe %p\n", pecmd_dirent
);
865 ret
= parse_registry_setup_cmdline(file
, head
, lookup
, meta_data
, dir
, cmdline
, sizeof(cmdline
) - 1);
868 debug("registry setup cmdline:<%s>\n", cmdline
);
870 if (grub_strncasecmp(cmdline
, "PECMD", 5) == 0)
872 wim_dirent
= pecmd_dirent
;
874 else if (grub_strncasecmp(cmdline
, "PESET", 5) == 0)
876 wim_dirent
= search_full_wim_dirent(meta_data
, dir
, peset_path
);
877 debug("search peset.exe %p\n", wim_dirent
);
879 else if (grub_strncasecmp(cmdline
, "WINPESHL", 8) == 0)
881 wim_dirent
= search_full_wim_dirent(meta_data
, dir
, winpeshl_path
);
882 debug("search winpeshl.exe %p\n", wim_dirent
);
884 else if (0 == parse_custom_setup_path(cmdline
, custom_path
, exefile
))
886 wim_dirent
= search_full_wim_dirent(meta_data
, dir
, custom_path
);
887 debug("search custom path %p\n", wim_dirent
);
897 debug("registry setup cmdline failed : %d\n", ret
);
901 wim_dirent
= pecmd_dirent
;
907 wim_dirent
= search_full_wim_dirent(meta_data
, dir
, winpeshl_path
);
908 debug("search winpeshl.exe %p\n", wim_dirent
);
918 static wim_lookup_entry
* ventoy_find_meta_entry(wim_header
*header
, wim_lookup_entry
*lookup
)
921 grub_uint32_t index
= 0;;
923 if ((header
== NULL
) || (lookup
== NULL
))
928 for (i
= 0; i
< (grub_uint32_t
)header
->lookup
.raw_size
/ sizeof(wim_lookup_entry
); i
++)
930 if (lookup
[i
].resource
.flags
& RESHDR_FLAG_METADATA
)
933 if (index
== header
->boot_index
)
943 static grub_uint64_t
ventoy_get_stream_len(wim_directory_entry
*dir
)
946 grub_uint64_t offset
= 0;
947 wim_stream_entry
*stream
= (wim_stream_entry
*)((char *)dir
+ dir
->len
);
949 for (i
= 0; i
< dir
->streams
; i
++)
951 offset
+= stream
->len
;
952 stream
= (wim_stream_entry
*)((char *)stream
+ stream
->len
);
958 static int ventoy_update_stream_hash(wim_patch
*patch
, wim_directory_entry
*dir
)
961 grub_uint64_t offset
= 0;
962 wim_stream_entry
*stream
= (wim_stream_entry
*)((char *)dir
+ dir
->len
);
964 for (i
= 0; i
< dir
->streams
; i
++)
966 if (grub_memcmp(stream
->hash
.sha1
, patch
->old_hash
.sha1
, sizeof(wim_hash
)) == 0)
968 debug("find target stream %u, name_len:%u upadte hash\n", i
, stream
->name_len
);
969 grub_memcpy(stream
->hash
.sha1
, &(patch
->wim_data
.bin_hash
), sizeof(wim_hash
));
972 offset
+= stream
->len
;
973 stream
= (wim_stream_entry
*)((char *)stream
+ stream
->len
);
979 static int ventoy_update_all_hash(wim_patch
*patch
, void *meta_data
, wim_directory_entry
*dir
)
981 if ((meta_data
== NULL
) || (dir
== NULL
))
986 if (dir
->len
< sizeof(wim_directory_entry
))
993 if (dir
->subdir
== 0 && grub_memcmp(dir
->hash
.sha1
, patch
->old_hash
.sha1
, sizeof(wim_hash
)) == 0)
995 debug("find target file, name_len:%u upadte hash\n", dir
->name_len
);
996 grub_memcpy(dir
->hash
.sha1
, &(patch
->wim_data
.bin_hash
), sizeof(wim_hash
));
1001 ventoy_update_all_hash(patch
, meta_data
, (wim_directory_entry
*)((char *)meta_data
+ dir
->subdir
));
1006 ventoy_update_stream_hash(patch
, dir
);
1007 dir
= (wim_directory_entry
*)((char *)dir
+ dir
->len
+ ventoy_get_stream_len(dir
));
1011 dir
= (wim_directory_entry
*)((char *)dir
+ dir
->len
);
1013 } while (dir
->len
>= sizeof(wim_directory_entry
));
1018 static int ventoy_cat_exe_file_data(wim_tail
*wim_data
, grub_uint32_t exe_len
, grub_uint8_t
*exe_data
, int windatalen
)
1022 grub_uint32_t jump_len
= 0;
1023 grub_uint32_t jump_align
= 0;
1024 grub_uint8_t
*jump_data
= NULL
;
1026 pe64
= ventoy_is_pe64(exe_data
);
1028 grub_snprintf(file
, sizeof(file
), "%s/vtoyjump%d.exe", grub_env_get("vtoy_path"), pe64
? 64 : 32);
1029 ventoy_load_jump_exe(file
, &jump_data
, &jump_len
, NULL
);
1030 jump_align
= ventoy_align(jump_len
, 16);
1032 wim_data
->jump_exe_len
= jump_len
;
1033 wim_data
->bin_raw_len
= jump_align
+ sizeof(ventoy_os_param
) + windatalen
+ exe_len
;
1034 wim_data
->bin_align_len
= ventoy_align(wim_data
->bin_raw_len
, 2048);
1036 wim_data
->jump_bin_data
= grub_malloc(wim_data
->bin_align_len
);
1037 if (wim_data
->jump_bin_data
)
1039 grub_memcpy(wim_data
->jump_bin_data
, jump_data
, jump_len
);
1040 grub_memcpy(wim_data
->jump_bin_data
+ jump_align
+ sizeof(ventoy_os_param
) + windatalen
, exe_data
, exe_len
);
1043 debug("jump_exe_len:%u bin_raw_len:%u bin_align_len:%u\n",
1044 wim_data
->jump_exe_len
, wim_data
->bin_raw_len
, wim_data
->bin_align_len
);
1049 static int ventoy_get_windows_rtdata_len(const char *iso
, int *flag
)
1052 int template_file_len
= 0;
1054 char *script
= NULL
;
1055 install_template
*template_node
= NULL
;
1058 size
= (int)sizeof(ventoy_windows_data
);
1060 pos
= grub_strstr(iso
, "/");
1066 script
= ventoy_plugin_get_cur_install_template(pos
, &template_node
);
1069 (*flag
) |= WINDATA_FLAG_TEMPLATE
;
1070 template_file_len
= template_node
->filelen
;
1073 return size
+ template_file_len
;
1076 static int ventoy_fill_windows_rtdata(void *buf
, char *isopath
, int dataflag
)
1078 int template_len
= 0;
1081 char *script
= NULL
;
1082 const char *env
= NULL
;
1083 install_template
*template_node
= NULL
;
1084 ventoy_windows_data
*data
= (ventoy_windows_data
*)buf
;
1086 grub_memset(data
, 0, sizeof(ventoy_windows_data
));
1088 env
= grub_env_get("VTOY_WIN11_BYPASS_CHECK");
1089 if (env
&& env
[0] == '1' && env
[1] == 0)
1091 data
->windows11_bypass_check
= 1;
1094 env
= grub_env_get("VTOY_WIN11_BYPASS_NRO");
1095 if (env
&& env
[0] == '1' && env
[1] == 0)
1097 data
->windows11_bypass_nro
= 1;
1100 pos
= grub_strstr(isopath
, "/");
1106 if (dataflag
& WINDATA_FLAG_TEMPLATE
)
1108 script
= ventoy_plugin_get_cur_install_template(pos
, &template_node
);
1111 data
->auto_install_len
= template_len
= template_node
->filelen
;
1112 debug("auto install script OK <%s> <len:%d>\n", script
, template_len
);
1113 end
= ventoy_str_last(script
, '/');
1114 grub_snprintf(data
->auto_install_script
, sizeof(data
->auto_install_script
) - 1, "%s", end
? end
+ 1 : script
);
1115 grub_memcpy(data
+ 1, template_node
->filebuf
, template_len
);
1120 debug("auto install script skipped or not configed %s\n", pos
);
1123 script
= (char *)ventoy_plugin_get_injection(pos
);
1126 if (ventoy_check_file_exist("%s%s", ventoy_get_env("vtoy_iso_part"), script
))
1128 debug("injection archive <%s> OK\n", script
);
1129 grub_snprintf(data
->injection_archive
, sizeof(data
->injection_archive
) - 1, "%s", script
);
1133 debug("injection archive <%s> NOT exist\n", script
);
1138 debug("injection archive not configed %s\n", pos
);
1144 static int ventoy_update_before_chain(ventoy_os_param
*param
, char *isopath
)
1146 grub_uint32_t jump_align
= 0;
1147 wim_lookup_entry
*meta_look
= NULL
;
1148 wim_security_header
*security
= NULL
;
1149 wim_directory_entry
*rootdir
= NULL
;
1150 wim_header
*head
= NULL
;
1151 wim_lookup_entry
*lookup
= NULL
;
1152 wim_patch
*node
= NULL
;
1153 wim_tail
*wim_data
= NULL
;
1155 for (node
= g_wim_patch_head
; node
; node
= node
->next
)
1157 if (0 == node
->valid
)
1162 wim_data
= &node
->wim_data
;
1163 head
= &wim_data
->wim_header
;
1164 lookup
= (wim_lookup_entry
*)wim_data
->new_lookup_data
;
1166 jump_align
= ventoy_align(wim_data
->jump_exe_len
, 16);
1167 if (wim_data
->jump_bin_data
)
1169 grub_memcpy(wim_data
->jump_bin_data
+ jump_align
, param
, sizeof(ventoy_os_param
));
1170 ventoy_fill_windows_rtdata(wim_data
->jump_bin_data
+ jump_align
+ sizeof(ventoy_os_param
), isopath
, wim_data
->windata_flag
);
1173 grub_crypto_hash(GRUB_MD_SHA1
, wim_data
->bin_hash
.sha1
, wim_data
->jump_bin_data
, wim_data
->bin_raw_len
);
1175 security
= (wim_security_header
*)wim_data
->new_meta_data
;
1176 if (security
->len
> 0)
1178 rootdir
= (wim_directory_entry
*)(wim_data
->new_meta_data
+ ((security
->len
+ 7) & 0xFFFFFFF8U
));
1182 rootdir
= (wim_directory_entry
*)(wim_data
->new_meta_data
+ 8);
1185 /* update all winpeshl.exe dirent entry's hash */
1186 ventoy_update_all_hash(node
, wim_data
->new_meta_data
, rootdir
);
1188 /* update winpeshl.exe lookup entry data (hash/offset/length) */
1189 if (node
->replace_look
)
1191 debug("update replace lookup entry_id:%ld\n", ((long)node
->replace_look
- (long)lookup
) / sizeof(wim_lookup_entry
));
1192 node
->replace_look
->resource
.raw_size
= wim_data
->bin_raw_len
;
1193 node
->replace_look
->resource
.size_in_wim
= wim_data
->bin_raw_len
;
1194 node
->replace_look
->resource
.flags
= 0;
1195 node
->replace_look
->resource
.offset
= wim_data
->wim_align_size
;
1197 grub_memcpy(node
->replace_look
->hash
.sha1
, wim_data
->bin_hash
.sha1
, sizeof(wim_hash
));
1200 /* update metadata's hash */
1201 meta_look
= ventoy_find_meta_entry(head
, lookup
);
1204 debug("find meta lookup entry_id:%ld\n", ((long)meta_look
- (long)lookup
) / sizeof(wim_lookup_entry
));
1205 grub_memcpy(&meta_look
->resource
, &head
->metadata
, sizeof(wim_resource_header
));
1206 grub_crypto_hash(GRUB_MD_SHA1
, meta_look
->hash
.sha1
, wim_data
->new_meta_data
, wim_data
->new_meta_len
);
1213 static int ventoy_wimdows_locate_wim(const char *disk
, wim_patch
*patch
, int windatalen
)
1218 grub_uint32_t exe_len
;
1219 grub_uint8_t
*exe_data
= NULL
;
1220 grub_uint8_t
*decompress_data
= NULL
;
1221 wim_lookup_entry
*lookup
= NULL
;
1222 wim_security_header
*security
= NULL
;
1223 wim_directory_entry
*rootdir
= NULL
;
1224 wim_directory_entry
*search
= NULL
;
1225 wim_stream_entry
*stream
= NULL
;
1226 wim_header
*head
= &(patch
->wim_data
.wim_header
);
1227 wim_tail
*wim_data
= &patch
->wim_data
;
1229 debug("windows locate wim start %s\n", patch
->path
);
1231 g_ventoy_case_insensitive
= 1;
1232 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", disk
, patch
->path
);
1233 g_ventoy_case_insensitive
= 0;
1237 debug("File %s%s NOT exist\n", disk
, patch
->path
);
1241 ventoy_get_override_info(file
, &patch
->wim_data
);
1243 grub_file_seek(file
, 0);
1244 grub_file_read(file
, head
, sizeof(wim_header
));
1246 if (grub_memcmp(head
->signature
, WIM_HEAD_SIGNATURE
, sizeof(head
->signature
)))
1248 debug("Not a valid wim file %s\n", (char *)head
->signature
);
1249 grub_file_close(file
);
1253 if (head
->flags
& FLAG_HEADER_COMPRESS_LZMS
)
1255 debug("LZMS compress is not supported 0x%x\n", head
->flags
);
1256 grub_file_close(file
);
1260 rc
= ventoy_read_resource(file
, head
, &head
->metadata
, (void **)&decompress_data
);
1263 grub_printf("failed to read meta data %d\n", rc
);
1264 grub_file_close(file
);
1268 security
= (wim_security_header
*)decompress_data
;
1269 if (security
->len
> 0)
1271 rootdir
= (wim_directory_entry
*)(decompress_data
+ ((security
->len
+ 7) & 0xFFFFFFF8U
));
1275 rootdir
= (wim_directory_entry
*)(decompress_data
+ 8);
1279 debug("read lookup offset:%llu size:%llu\n", (ulonglong
)head
->lookup
.offset
, (ulonglong
)head
->lookup
.raw_size
);
1280 lookup
= grub_malloc(head
->lookup
.raw_size
);
1281 grub_file_seek(file
, head
->lookup
.offset
);
1282 grub_file_read(file
, lookup
, head
->lookup
.raw_size
);
1286 /* search winpeshl.exe dirent entry */
1287 search
= search_replace_wim_dirent(file
, head
, lookup
, decompress_data
, rootdir
);
1290 debug("Failed to find replace file %p\n", search
);
1291 grub_file_close(file
);
1295 debug("find replace file at %p\n", search
);
1297 grub_memset(&patch
->old_hash
, 0, sizeof(wim_hash
));
1298 if (grub_memcmp(&patch
->old_hash
, search
->hash
.sha1
, sizeof(wim_hash
)) == 0)
1300 debug("search hash all 0, now do deep search\n");
1301 stream
= (wim_stream_entry
*)((char *)search
+ search
->len
);
1302 for (i
= 0; i
< search
->streams
; i
++)
1304 if (stream
->name_len
== 0)
1306 grub_memcpy(&patch
->old_hash
, stream
->hash
.sha1
, sizeof(wim_hash
));
1307 debug("new search hash: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1308 ventoy_varg_8(patch
->old_hash
.sha1
));
1311 stream
= (wim_stream_entry
*)((char *)stream
+ stream
->len
);
1316 grub_memcpy(&patch
->old_hash
, search
->hash
.sha1
, sizeof(wim_hash
));
1320 /* find and extact winpeshl.exe */
1321 patch
->replace_look
= ventoy_find_look_entry(head
, lookup
, &patch
->old_hash
);
1322 if (patch
->replace_look
)
1324 exe_len
= (grub_uint32_t
)patch
->replace_look
->resource
.raw_size
;
1325 debug("find replace lookup entry_id:%ld raw_size:%u\n",
1326 ((long)patch
->replace_look
- (long)lookup
) / sizeof(wim_lookup_entry
), exe_len
);
1328 if (0 == ventoy_read_resource(file
, head
, &(patch
->replace_look
->resource
), (void **)&(exe_data
)))
1330 ventoy_cat_exe_file_data(wim_data
, exe_len
, exe_data
, windatalen
);
1331 grub_free(exe_data
);
1335 debug("failed to read replace file meta data %u\n", exe_len
);
1340 debug("failed to find lookup entry for replace file %02x %02x %02x %02x\n",
1341 ventoy_varg_4(patch
->old_hash
.sha1
));
1344 wim_data
->wim_raw_size
= (grub_uint32_t
)file
->size
;
1345 wim_data
->wim_align_size
= ventoy_align(wim_data
->wim_raw_size
, 2048);
1347 grub_check_free(wim_data
->new_meta_data
);
1348 wim_data
->new_meta_data
= decompress_data
;
1349 wim_data
->new_meta_len
= head
->metadata
.raw_size
;
1350 wim_data
->new_meta_align_len
= ventoy_align(wim_data
->new_meta_len
, 2048);
1352 grub_check_free(wim_data
->new_lookup_data
);
1353 wim_data
->new_lookup_data
= (grub_uint8_t
*)lookup
;
1354 wim_data
->new_lookup_len
= (grub_uint32_t
)head
->lookup
.raw_size
;
1355 wim_data
->new_lookup_align_len
= ventoy_align(wim_data
->new_lookup_len
, 2048);
1357 head
->metadata
.flags
= RESHDR_FLAG_METADATA
;
1358 head
->metadata
.offset
= wim_data
->wim_align_size
+ wim_data
->bin_align_len
;
1359 head
->metadata
.size_in_wim
= wim_data
->new_meta_len
;
1360 head
->metadata
.raw_size
= wim_data
->new_meta_len
;
1362 head
->lookup
.flags
= 0;
1363 head
->lookup
.offset
= head
->metadata
.offset
+ wim_data
->new_meta_align_len
;
1364 head
->lookup
.size_in_wim
= wim_data
->new_lookup_len
;
1365 head
->lookup
.raw_size
= wim_data
->new_lookup_len
;
1367 grub_file_close(file
);
1369 debug("%s", "windows locate wim finish\n");
1373 grub_err_t
ventoy_cmd_sel_winpe_wim(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1380 wim_patch
*node
= NULL
;
1381 wim_patch
*tmp
= NULL
;
1382 grub_file_t file
= NULL
;
1383 wim_header
*head
= NULL
;
1389 len
= 8 * VTOY_SIZE_1KB
;
1390 cmd
= (char *)grub_malloc(len
+ sizeof(wim_header
));
1396 head
= (wim_header
*)(cmd
+ len
);
1397 grub_env_unset("vtoy_pe_wim_path");
1399 for (node
= g_wim_patch_head
; node
; node
= node
->next
)
1402 for (tmp
= g_wim_patch_head
; tmp
!= node
; tmp
= tmp
->next
)
1404 if (tmp
->valid
&& grub_strcasecmp(tmp
->path
, node
->path
) == 0)
1416 g_ventoy_case_insensitive
= 1;
1417 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", args
[0], node
->path
);
1418 g_ventoy_case_insensitive
= 0;
1421 debug("File %s%s NOT exist\n", args
[0], node
->path
);
1425 grub_file_read(file
, head
, sizeof(wim_header
));
1426 if (grub_memcmp(head
->signature
, WIM_HEAD_SIGNATURE
, sizeof(head
->signature
)))
1428 debug("Not a valid wim file %s\n", (char *)head
->signature
);
1429 grub_file_close(file
);
1433 if (head
->flags
& FLAG_HEADER_COMPRESS_LZMS
)
1435 debug("LZMS compress is not supported 0x%x\n", head
->flags
);
1436 grub_file_close(file
);
1440 grub_file_close(file
);
1443 vtoy_len_ssprintf(cmd
, pos
, len
, "menuentry \"%s\" --class=\"sel_wim\" {\n echo \"\"\n}\n", node
->path
);
1448 g_ventoy_menu_esc
= 1;
1449 g_ventoy_suppress_esc
= 1;
1450 g_ventoy_suppress_esc_default
= 0;
1452 grub_snprintf(cfgfile
, sizeof(cfgfile
), "configfile mem:0x%llx:size:%d", (ulonglong
)(ulong
)cmd
, pos
);
1453 grub_script_execute_sourcecode(cfgfile
);
1455 g_ventoy_menu_esc
= 0;
1456 g_ventoy_suppress_esc
= 0;
1457 g_ventoy_suppress_esc_default
= 1;
1459 for (node
= g_wim_patch_head
; node
; node
= node
->next
)
1463 if (i
== g_ventoy_last_entry
)
1465 grub_env_set("vtoy_pe_wim_path", node
->path
);
1477 grub_err_t
ventoy_cmd_locate_wim_patch(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1481 wim_patch
*node
= g_wim_patch_head
;
1487 datalen
= ventoy_get_windows_rtdata_len(args
[1], &dataflag
);
1491 node
->wim_data
.windata_flag
= dataflag
;
1492 if (0 == ventoy_wimdows_locate_wim(args
[0], node
, datalen
))
1495 g_wim_valid_patch_count
++;
1504 static grub_uint32_t
ventoy_get_override_chunk_num(void)
1506 grub_uint32_t chunk_num
= 0;
1508 if (g_iso_fs_type
== 0)
1512 /* 1: file_size and file_offset */
1513 /* 2: new wim file header */
1514 chunk_num
= g_wim_valid_patch_count
* 2;
1520 /* 1: block count in Partition Descriptor */
1523 /* 1: file_size in file_entry or extend_file_entry */
1524 /* 2: data_size and position in extend data short ad */
1525 /* 3: new wim file header */
1526 chunk_num
= g_wim_valid_patch_count
* 3 + 1;
1529 if (g_suppress_wincd_override_offset
> 0)
1537 static void ventoy_fill_suppress_wincd_override_data(void *override
)
1539 ventoy_override_chunk
*cur
= (ventoy_override_chunk
*)override
;
1541 cur
->override_size
= 4;
1542 cur
->img_offset
= g_suppress_wincd_override_offset
;
1543 grub_memcpy(cur
->override_data
, &g_suppress_wincd_override_data
, cur
->override_size
);
1546 static void ventoy_windows_fill_override_data_iso9660( grub_uint64_t isosize
, void *override
)
1548 grub_uint64_t sector
;
1549 grub_uint32_t new_wim_size
;
1550 ventoy_override_chunk
*cur
;
1551 wim_patch
*node
= NULL
;
1552 wim_tail
*wim_data
= NULL
;
1553 ventoy_iso9660_override
*dirent
= NULL
;
1555 sector
= (isosize
+ 2047) / 2048;
1557 cur
= (ventoy_override_chunk
*)override
;
1559 if (g_suppress_wincd_override_offset
> 0)
1561 ventoy_fill_suppress_wincd_override_data(cur
);
1565 debug("ventoy_windows_fill_override_data_iso9660 %lu\n", (ulong
)isosize
);
1567 for (node
= g_wim_patch_head
; node
; node
= node
->next
)
1569 wim_data
= &node
->wim_data
;
1570 if (0 == node
->valid
)
1575 new_wim_size
= wim_data
->wim_align_size
+ wim_data
->bin_align_len
+
1576 wim_data
->new_meta_align_len
+ wim_data
->new_lookup_align_len
;
1578 dirent
= (ventoy_iso9660_override
*)wim_data
->override_data
;
1580 dirent
->first_sector
= (grub_uint32_t
)sector
;
1581 dirent
->size
= new_wim_size
;
1582 dirent
->first_sector_be
= grub_swap_bytes32(dirent
->first_sector
);
1583 dirent
->size_be
= grub_swap_bytes32(dirent
->size
);
1585 sector
+= (new_wim_size
/ 2048);
1587 /* override 1: position and length in dirent */
1588 cur
->img_offset
= wim_data
->override_offset
;
1589 cur
->override_size
= wim_data
->override_len
;
1590 grub_memcpy(cur
->override_data
, wim_data
->override_data
, cur
->override_size
);
1593 /* override 2: new wim file header */
1594 cur
->img_offset
= wim_data
->file_offset
;
1595 cur
->override_size
= sizeof(wim_header
);
1596 grub_memcpy(cur
->override_data
, &(wim_data
->wim_header
), cur
->override_size
);
1603 static int ventoy_windows_fill_udf_short_ad(grub_file_t isofile
, grub_uint32_t curpos
,
1604 wim_tail
*wim_data
, grub_uint32_t new_wim_size
)
1607 grub_uint32_t total
= 0;
1608 grub_uint32_t left_size
= 0;
1609 ventoy_udf_override
*udf
= NULL
;
1610 ventoy_udf_override tmp
[4];
1612 grub_memset(tmp
, 0, sizeof(tmp
));
1613 grub_file_seek(isofile
, wim_data
->override_offset
);
1614 grub_file_read(isofile
, tmp
, sizeof(tmp
));
1616 left_size
= new_wim_size
;
1617 udf
= (ventoy_udf_override
*)wim_data
->override_data
;
1619 for (i
= 0; i
< 4; i
++)
1621 total
+= tmp
[i
].length
;
1622 if (total
>= wim_data
->wim_raw_size
)
1624 udf
->length
= left_size
;
1625 udf
->position
= curpos
;
1630 udf
->length
= tmp
[i
].length
;
1631 udf
->position
= curpos
;
1634 left_size
-= tmp
[i
].length
;
1635 curpos
+= udf
->length
/ 2048;
1637 wim_data
->override_len
+= sizeof(ventoy_udf_override
);
1640 debug("######## Too many udf ad ######\n");
1644 static void ventoy_windows_fill_override_data_udf(grub_file_t isofile
, void *override
)
1646 grub_uint32_t data32
;
1647 grub_uint64_t data64
;
1648 grub_uint64_t sector
;
1649 grub_uint32_t new_wim_size
;
1650 grub_uint64_t total_wim_size
= 0;
1651 grub_uint32_t udf_start_block
= 0;
1652 ventoy_override_chunk
*cur
;
1653 wim_patch
*node
= NULL
;
1654 wim_tail
*wim_data
= NULL
;
1656 sector
= (isofile
->size
+ 2047) / 2048;
1658 cur
= (ventoy_override_chunk
*)override
;
1660 if (g_suppress_wincd_override_offset
> 0)
1662 ventoy_fill_suppress_wincd_override_data(cur
);
1666 debug("ventoy_windows_fill_override_data_udf %lu\n", (ulong
)isofile
->size
);
1668 for (node
= g_wim_patch_head
; node
; node
= node
->next
)
1670 wim_data
= &node
->wim_data
;
1673 if (udf_start_block
== 0)
1675 udf_start_block
= wim_data
->udf_start_block
;
1677 new_wim_size
= wim_data
->wim_align_size
+ wim_data
->bin_align_len
+
1678 wim_data
->new_meta_align_len
+ wim_data
->new_lookup_align_len
;
1679 total_wim_size
+= new_wim_size
;
1683 //override 1: sector number in pd data
1684 cur
->img_offset
= grub_udf_get_last_pd_size_offset();
1685 cur
->override_size
= 4;
1686 data32
= sector
- udf_start_block
+ (total_wim_size
/ 2048);
1687 grub_memcpy(cur
->override_data
, &(data32
), 4);
1689 for (node
= g_wim_patch_head
; node
; node
= node
->next
)
1691 wim_data
= &node
->wim_data
;
1692 if (0 == node
->valid
)
1697 new_wim_size
= wim_data
->wim_align_size
+ wim_data
->bin_align_len
+
1698 wim_data
->new_meta_align_len
+ wim_data
->new_lookup_align_len
;
1700 //override 2: filesize in file_entry
1702 cur
->img_offset
= wim_data
->fe_entry_size_offset
;
1703 cur
->override_size
= 8;
1704 data64
= new_wim_size
;
1705 grub_memcpy(cur
->override_data
, &(data64
), 8);
1707 /* override 3: position and length in extend data */
1708 ventoy_windows_fill_udf_short_ad(isofile
, (grub_uint32_t
)sector
- udf_start_block
, wim_data
, new_wim_size
);
1710 sector
+= (new_wim_size
/ 2048);
1713 cur
->img_offset
= wim_data
->override_offset
;
1714 cur
->override_size
= wim_data
->override_len
;
1715 grub_memcpy(cur
->override_data
, wim_data
->override_data
, cur
->override_size
);
1717 /* override 4: new wim file header */
1719 cur
->img_offset
= wim_data
->file_offset
;
1720 cur
->override_size
= sizeof(wim_header
);
1721 grub_memcpy(cur
->override_data
, &(wim_data
->wim_header
), cur
->override_size
);
1727 static grub_uint32_t
ventoy_windows_get_virt_data_size(void)
1729 grub_uint32_t size
= 0;
1730 wim_tail
*wim_data
= NULL
;
1731 wim_patch
*node
= g_wim_patch_head
;
1737 wim_data
= &node
->wim_data
;
1738 size
+= sizeof(ventoy_virt_chunk
) + wim_data
->bin_align_len
+
1739 wim_data
->new_meta_align_len
+ wim_data
->new_lookup_align_len
;
1747 static void ventoy_windows_fill_virt_data( grub_uint64_t isosize
, ventoy_chain_head
*chain
)
1749 grub_uint64_t sector
;
1750 grub_uint32_t offset
;
1751 grub_uint32_t wim_secs
;
1752 grub_uint32_t mem_secs
;
1753 char *override
= NULL
;
1754 ventoy_virt_chunk
*cur
= NULL
;
1755 wim_tail
*wim_data
= NULL
;
1756 wim_patch
*node
= NULL
;
1758 sector
= (isosize
+ 2047) / 2048;
1759 offset
= sizeof(ventoy_virt_chunk
) * g_wim_valid_patch_count
;
1761 override
= (char *)chain
+ chain
->virt_chunk_offset
;
1762 cur
= (ventoy_virt_chunk
*)override
;
1764 for (node
= g_wim_patch_head
; node
; node
= node
->next
)
1766 if (0 == node
->valid
)
1771 wim_data
= &node
->wim_data
;
1773 wim_secs
= wim_data
->wim_align_size
/ 2048;
1774 mem_secs
= (wim_data
->bin_align_len
+ wim_data
->new_meta_align_len
+ wim_data
->new_lookup_align_len
) / 2048;
1776 cur
->remap_sector_start
= sector
;
1777 cur
->remap_sector_end
= cur
->remap_sector_start
+ wim_secs
;
1778 cur
->org_sector_start
= (grub_uint32_t
)(wim_data
->file_offset
/ 2048);
1780 cur
->mem_sector_start
= cur
->remap_sector_end
;
1781 cur
->mem_sector_end
= cur
->mem_sector_start
+ mem_secs
;
1782 cur
->mem_sector_offset
= offset
;
1784 sector
+= wim_secs
+ mem_secs
;
1787 grub_memcpy(override
+ offset
, wim_data
->jump_bin_data
, wim_data
->bin_raw_len
);
1788 offset
+= wim_data
->bin_align_len
;
1790 grub_memcpy(override
+ offset
, wim_data
->new_meta_data
, wim_data
->new_meta_len
);
1791 offset
+= wim_data
->new_meta_align_len
;
1793 grub_memcpy(override
+ offset
, wim_data
->new_lookup_data
, wim_data
->new_lookup_len
);
1794 offset
+= wim_data
->new_lookup_align_len
;
1796 chain
->virt_img_size_in_bytes
+= wim_data
->wim_align_size
+
1797 wim_data
->bin_align_len
+
1798 wim_data
->new_meta_align_len
+
1799 wim_data
->new_lookup_align_len
;
1805 static int ventoy_windows_drive_map(ventoy_chain_head
*chain
, int vlnk
)
1810 debug("drive map begin <%p> <%d> ...\n", chain
, vlnk
);
1812 disk
= grub_disk_open("hd1");
1815 grub_disk_close(disk
);
1817 debug("BIOS hd1 exist\n");
1821 debug("failed to open disk %s\n", "hd1");
1826 if (g_ventoy_disk_bios_id
== 0x80 && hd1
)
1828 debug("drive map needed vlnk %p\n", disk
);
1829 chain
->drive_map
= 0x81;
1832 else if (chain
->disk_drive
== 0x80)
1836 debug("drive map needed normal %p\n", disk
);
1837 chain
->drive_map
= 0x81;
1842 debug("no need to map 0x%x\n", chain
->disk_drive
);
1848 static int ventoy_suppress_windows_cd_prompt(void)
1851 const char *cdprompt
= NULL
;
1852 grub_uint64_t readpos
= 0;
1853 grub_file_t file
= NULL
;
1854 grub_uint8_t data
[32];
1856 cdprompt
= ventoy_get_env("VTOY_WINDOWS_CD_PROMPT");
1857 if (cdprompt
&& cdprompt
[0] == '1' && cdprompt
[1] == 0)
1859 debug("VTOY_WINDOWS_CD_PROMPT:<%s>\n", cdprompt
);
1863 g_ventoy_case_insensitive
= 1;
1864 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/boot/bootfix.bin", "(loop)");
1865 g_ventoy_case_insensitive
= 0;
1869 debug("Failed to open %s\n", "bootfix.bin");
1873 grub_file_read(file
, data
, 32);
1875 if (file
->fs
&& file
->fs
->name
&& grub_strcmp(file
->fs
->name
, "udf") == 0)
1877 readpos
= grub_udf_get_file_offset(file
);
1881 readpos
= grub_iso9660_get_last_read_pos(file
);
1884 debug("bootfix.bin readpos:%lu (sector:%lu) data: %02x %02x %02x %02x\n",
1885 (ulong
)readpos
, (ulong
)readpos
/ 2048, data
[24], data
[25], data
[26], data
[27]);
1887 if (*(grub_uint32_t
*)(data
+ 24) == 0x13cd0080)
1889 g_suppress_wincd_override_offset
= readpos
+ 24;
1890 g_suppress_wincd_override_data
= 0x13cd00fd;
1895 debug("g_suppress_wincd_override_offset:%lu\n", (ulong
)g_suppress_wincd_override_offset
);
1898 check_free(file
, grub_file_close
);
1903 static int ventoy_extract_init_exe(char *wimfile
, grub_uint8_t
**pexe_data
, grub_uint32_t
*pexe_len
, char *exe_name
)
1908 grub_file_t file
= NULL
;
1909 grub_uint32_t exe_len
= 0;
1910 wim_header
*head
= NULL
;
1911 grub_uint16_t
*uname
= NULL
;
1912 grub_uint8_t
*exe_data
= NULL
;
1913 grub_uint8_t
*decompress_data
= NULL
;
1914 wim_lookup_entry
*lookup
= NULL
;
1915 wim_security_header
*security
= NULL
;
1916 wim_directory_entry
*rootdir
= NULL
;
1917 wim_directory_entry
*search
= NULL
;
1918 wim_stream_entry
*stream
= NULL
;
1919 wim_lookup_entry
*replace_look
= NULL
;
1925 file
= grub_file_open(wimfile
, VENTOY_FILE_TYPE
);
1931 grub_file_read(file
, head
, sizeof(wim_header
));
1932 rc
= ventoy_read_resource(file
, head
, &head
->metadata
, (void **)&decompress_data
);
1935 grub_printf("failed to read meta data %d\n", rc
);
1939 security
= (wim_security_header
*)decompress_data
;
1940 if (security
->len
> 0)
1942 rootdir
= (wim_directory_entry
*)(decompress_data
+ ((security
->len
+ 7) & 0xFFFFFFF8U
));
1946 rootdir
= (wim_directory_entry
*)(decompress_data
+ 8);
1949 debug("read lookup offset:%llu size:%llu\n", (ulonglong
)head
->lookup
.offset
, (ulonglong
)head
->lookup
.raw_size
);
1950 lookup
= grub_malloc(head
->lookup
.raw_size
);
1951 grub_file_seek(file
, head
->lookup
.offset
);
1952 grub_file_read(file
, lookup
, head
->lookup
.raw_size
);
1954 /* search winpeshl.exe dirent entry */
1955 search
= search_replace_wim_dirent(file
, head
, lookup
, decompress_data
, rootdir
);
1958 debug("Failed to find replace file %p\n", search
);
1962 uname
= (grub_uint16_t
*)(search
+ 1);
1963 for (i
= 0; i
< search
->name_len
/ 2 && i
< 200; i
++)
1965 exe_name
[i
] = (char)uname
[i
];
1968 debug("find replace file at %p <%s>\n", search
, exe_name
);
1970 grub_memset(&hashdata
, 0, sizeof(wim_hash
));
1971 if (grub_memcmp(&hashdata
, search
->hash
.sha1
, sizeof(wim_hash
)) == 0)
1973 debug("search hash all 0, now do deep search\n");
1974 stream
= (wim_stream_entry
*)((char *)search
+ search
->len
);
1975 for (i
= 0; i
< search
->streams
; i
++)
1977 if (stream
->name_len
== 0)
1979 grub_memcpy(&hashdata
, stream
->hash
.sha1
, sizeof(wim_hash
));
1980 debug("new search hash: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1981 ventoy_varg_8(hashdata
.sha1
));
1984 stream
= (wim_stream_entry
*)((char *)stream
+ stream
->len
);
1989 grub_memcpy(&hashdata
, search
->hash
.sha1
, sizeof(wim_hash
));
1992 /* find and extact winpeshl.exe */
1993 replace_look
= ventoy_find_look_entry(head
, lookup
, &hashdata
);
1996 exe_len
= (grub_uint32_t
)replace_look
->resource
.raw_size
;
1997 debug("find replace lookup entry_id:%ld raw_size:%u\n",
1998 ((long)replace_look
- (long)lookup
) / sizeof(wim_lookup_entry
), exe_len
);
2000 if (0 != ventoy_read_resource(file
, head
, &(replace_look
->resource
), (void **)&(exe_data
)))
2004 debug("failed to read replace file meta data %u\n", exe_len
);
2009 debug("failed to find lookup entry for replace file %02x %02x %02x %02x\n",
2010 ventoy_varg_4(hashdata
.sha1
));
2016 *pexe_data
= exe_data
;
2017 *pexe_len
= exe_len
;
2022 grub_check_free(lookup
);
2023 grub_check_free(decompress_data
);
2024 check_free(file
, grub_file_close
);
2029 grub_err_t
ventoy_cmd_windows_wimboot_data(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2035 grub_uint32_t exe_len
= 0;
2036 grub_uint32_t jump_align
= 0;
2037 const char *addr
= NULL
;
2038 ventoy_chain_head
*chain
= NULL
;
2039 grub_uint8_t
*param
= NULL
;
2040 grub_uint8_t
*exe_data
= NULL
;
2041 ventoy_windows_data
*rtdata
= NULL
;
2042 char exename
[128] = {0};
2048 addr
= grub_env_get("vtoy_chain_mem_addr");
2051 debug("Failed to find vtoy_chain_mem_addr\n");
2055 chain
= (ventoy_chain_head
*)(void *)grub_strtoul(addr
, NULL
, 16);
2057 if (grub_memcmp(&g_ventoy_guid
, &chain
->os_param
.guid
, 16) != 0)
2059 debug("os_param.guid not match\n");
2063 datalen
= ventoy_get_windows_rtdata_len(chain
->os_param
.vtoy_img_path
, &dataflag
);
2065 rc
= ventoy_extract_init_exe(args
[0], &exe_data
, &exe_len
, exename
);
2070 wim64
= ventoy_is_pe64(exe_data
);
2072 grub_memset(&wim_data
, 0, sizeof(wim_data
));
2073 ventoy_cat_exe_file_data(&wim_data
, exe_len
, exe_data
, datalen
);
2074 grub_check_free(exe_data
);
2076 jump_align
= ventoy_align(wim_data
.jump_exe_len
, 16);
2077 param
= wim_data
.jump_bin_data
;
2079 grub_memcpy(param
+ jump_align
, &chain
->os_param
, sizeof(ventoy_os_param
));
2081 rtdata
= (ventoy_windows_data
*)(param
+ jump_align
+ sizeof(ventoy_os_param
));
2082 ventoy_fill_windows_rtdata(rtdata
, chain
->os_param
.vtoy_img_path
, dataflag
);
2084 ventoy_memfile_env_set("vtoy_wimboot_mem", param
, (ulonglong
)(wim_data
.bin_align_len
));
2086 grub_env_set(args
[1], exename
);
2087 grub_env_set(args
[2], wim64
? "64" : "32");
2089 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
2092 grub_err_t
ventoy_cmd_windows_chain_data(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2094 int unknown_image
= 0;
2095 int ventoy_compatible
= 0;
2096 grub_uint32_t size
= 0;
2097 grub_uint64_t isosize
= 0;
2098 grub_uint32_t boot_catlog
= 0;
2099 grub_uint32_t img_chunk_size
= 0;
2100 grub_uint32_t override_size
= 0;
2101 grub_uint32_t virt_chunk_size
= 0;
2104 const char *pLastChain
= NULL
;
2105 const char *compatible
;
2106 ventoy_chain_head
*chain
;
2111 debug("chain data begin <%s> ...\n", args
[0]);
2113 compatible
= grub_env_get("ventoy_compatible");
2114 if (compatible
&& compatible
[0] == 'Y')
2116 ventoy_compatible
= 1;
2119 if (NULL
== g_img_chunk_list
.chunk
)
2121 grub_printf("ventoy not ready\n");
2125 if (0 == ventoy_compatible
&& g_wim_valid_patch_count
== 0)
2128 if (!g_ventoy_wimboot_mode
)
2130 debug("Warning: %s was not recognized by Ventoy\n", args
[0]);
2134 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s", args
[0]);
2140 isosize
= file
->size
;
2142 boot_catlog
= ventoy_get_iso_boot_catlog(file
);
2145 if (ventoy_is_efi_os() && (!ventoy_has_efi_eltorito(file
, boot_catlog
)))
2147 grub_env_set("LoadIsoEfiDriver", "on");
2152 if (ventoy_is_efi_os())
2154 grub_env_set("LoadIsoEfiDriver", "on");
2158 return grub_error(GRUB_ERR_BAD_ARGUMENT
, "File %s is not bootable", args
[0]);
2162 g_suppress_wincd_override_offset
= 0;
2163 if (!ventoy_is_efi_os()) /* legacy mode */
2165 ventoy_suppress_windows_cd_prompt();
2168 img_chunk_size
= g_img_chunk_list
.cur_chunk
* sizeof(ventoy_img_chunk
);
2170 if (ventoy_compatible
|| unknown_image
)
2172 override_size
= g_suppress_wincd_override_offset
> 0 ? sizeof(ventoy_override_chunk
) : 0;
2173 size
= sizeof(ventoy_chain_head
) + img_chunk_size
+ override_size
;
2177 override_size
= ventoy_get_override_chunk_num() * sizeof(ventoy_override_chunk
);
2178 virt_chunk_size
= ventoy_windows_get_virt_data_size();
2179 size
= sizeof(ventoy_chain_head
) + img_chunk_size
+ override_size
+ virt_chunk_size
;
2182 pLastChain
= grub_env_get("vtoy_chain_mem_addr");
2185 chain
= (ventoy_chain_head
*)grub_strtoul(pLastChain
, NULL
, 16);
2188 debug("free last chain memory %p\n", chain
);
2193 chain
= ventoy_alloc_chain(size
);
2196 grub_printf("Failed to alloc chain win1 memory size %u\n", size
);
2197 grub_file_close(file
);
2201 ventoy_memfile_env_set("vtoy_chain_mem", chain
, (ulonglong
)size
);
2203 grub_memset(chain
, 0, sizeof(ventoy_chain_head
));
2205 /* part 1: os parameter */
2206 g_ventoy_chain_type
= ventoy_chain_windows
;
2207 ventoy_fill_os_param(file
, &(chain
->os_param
));
2209 if (0 == unknown_image
)
2211 ventoy_update_before_chain(&(chain
->os_param
), args
[0]);
2214 /* part 2: chain head */
2215 disk
= file
->device
->disk
;
2216 chain
->disk_drive
= disk
->id
;
2217 chain
->disk_sector_size
= (1 << disk
->log_sector_size
);
2218 chain
->real_img_size_in_bytes
= file
->size
;
2219 chain
->virt_img_size_in_bytes
= (file
->size
+ 2047) / 2048 * 2048;
2220 chain
->boot_catalog
= boot_catlog
;
2222 if (!ventoy_is_efi_os())
2224 grub_file_seek(file
, boot_catlog
* 2048);
2225 grub_file_read(file
, chain
->boot_catalog_sector
, sizeof(chain
->boot_catalog_sector
));
2228 /* part 3: image chunk */
2229 chain
->img_chunk_offset
= sizeof(ventoy_chain_head
);
2230 chain
->img_chunk_num
= g_img_chunk_list
.cur_chunk
;
2231 grub_memcpy((char *)chain
+ chain
->img_chunk_offset
, g_img_chunk_list
.chunk
, img_chunk_size
);
2233 if (ventoy_compatible
|| unknown_image
)
2235 if (g_suppress_wincd_override_offset
> 0)
2237 chain
->override_chunk_offset
= chain
->img_chunk_offset
+ img_chunk_size
;
2238 chain
->override_chunk_num
= 1;
2239 ventoy_fill_suppress_wincd_override_data((char *)chain
+ chain
->override_chunk_offset
);
2245 if (0 == g_wim_valid_patch_count
)
2250 /* part 4: override chunk */
2251 chain
->override_chunk_offset
= chain
->img_chunk_offset
+ img_chunk_size
;
2252 chain
->override_chunk_num
= ventoy_get_override_chunk_num();
2254 if (g_iso_fs_type
== 0)
2256 ventoy_windows_fill_override_data_iso9660(isosize
, (char *)chain
+ chain
->override_chunk_offset
);
2260 ventoy_windows_fill_override_data_udf(file
, (char *)chain
+ chain
->override_chunk_offset
);
2263 /* part 5: virt chunk */
2264 chain
->virt_chunk_offset
= chain
->override_chunk_offset
+ override_size
;
2265 chain
->virt_chunk_num
= g_wim_valid_patch_count
;
2266 ventoy_windows_fill_virt_data(isosize
, chain
);
2268 if (ventoy_is_efi_os() == 0)
2270 ventoy_windows_drive_map(chain
, file
->vlnk
);
2273 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
2276 static grub_uint32_t
ventoy_get_wim_iso_offset(const char *filepath
)
2278 grub_uint32_t imgoffset
;
2282 grub_snprintf(cmdbuf
, sizeof(cmdbuf
), "loopback wimiso \"%s\"", filepath
);
2283 grub_script_execute_sourcecode(cmdbuf
);
2285 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s", "(wimiso)/boot/boot.wim");
2288 grub_printf("Failed to open boot.wim file in the image file\n");
2292 imgoffset
= (grub_uint32_t
)grub_iso9660_get_last_file_dirent_pos(file
) + 2;
2294 debug("wimiso wim direct offset: %u\n", imgoffset
);
2296 grub_file_close(file
);
2298 grub_script_execute_sourcecode("loopback -d wimiso");
2303 static int ventoy_get_wim_chunklist(grub_file_t wimfile
, ventoy_img_chunk_list
*wimchunk
)
2305 grub_memset(wimchunk
, 0, sizeof(ventoy_img_chunk_list
));
2306 wimchunk
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
2307 if (NULL
== wimchunk
->chunk
)
2309 return grub_error(GRUB_ERR_OUT_OF_MEMORY
, "Can't allocate image chunk memoty\n");
2312 wimchunk
->max_chunk
= DEFAULT_CHUNK_NUM
;
2313 wimchunk
->cur_chunk
= 0;
2315 ventoy_get_block_list(wimfile
, wimchunk
, wimfile
->device
->disk
->partition
->start
);
2320 grub_err_t
ventoy_cmd_is_standard_winiso(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2324 char prefix
[32] = {0};
2325 const char *chkfile
[] =
2327 "boot/bcd", "boot/boot.sdi", NULL
2333 if (ventoy_check_file_exist("%s/sources/boot.wim", args
[0]))
2337 else if (ventoy_check_file_exist("%s/x86/sources/boot.wim", args
[0]))
2339 grub_snprintf(prefix
, sizeof(prefix
), "/x86");
2341 else if (ventoy_check_file_exist("%s/x64/sources/boot.wim", args
[0]))
2343 grub_snprintf(prefix
, sizeof(prefix
), "/x64");
2347 debug("No boot.wim found.\n");
2351 for (i
= 0; chkfile
[i
]; i
++)
2353 if (!ventoy_check_file_exist("%s%s/%s", args
[0], prefix
, chkfile
[i
]))
2355 debug("%s not found.\n", chkfile
[i
]);
2360 if ((!ventoy_check_file_exist("%s%s/sources/install.wim", args
[0], prefix
)) &&
2361 (!ventoy_check_file_exist("%s%s/sources/install.esd", args
[0], prefix
)))
2363 debug("No install.wim(esd) found.\n");
2367 if (!ventoy_check_file_exist("%s/setup.exe", args
[0]))
2369 debug("No setup.exe found.\n");
2374 debug("This is standard Windows ISO.\n");
2381 grub_err_t
ventoy_cmd_wim_check_bootable(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2383 grub_uint32_t boot_index
;
2384 grub_file_t file
= NULL
;
2385 wim_header
*wimhdr
= NULL
;
2390 wimhdr
= grub_zalloc(sizeof(wim_header
));
2396 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s", args
[0]);
2403 grub_file_read(file
, wimhdr
, sizeof(wim_header
));
2404 grub_file_close(file
);
2405 boot_index
= wimhdr
->boot_index
;
2408 if (boot_index
== 0)
2413 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
2416 static grub_err_t
ventoy_vlnk_wim_chain_data(grub_file_t wimfile
)
2418 grub_uint32_t i
= 0;
2419 grub_uint32_t imgoffset
= 0;
2420 grub_uint32_t size
= 0;
2421 grub_uint32_t isosector
= 0;
2422 grub_uint64_t wimsize
= 0;
2423 grub_uint32_t boot_catlog
= 0;
2424 grub_uint32_t img_chunk1_size
= 0;
2425 grub_uint32_t img_chunk2_size
= 0;
2426 grub_uint32_t override_size
= 0;
2429 const char *pLastChain
= NULL
;
2430 ventoy_chain_head
*chain
;
2431 ventoy_iso9660_override
*dirent
;
2432 ventoy_img_chunk
*chunknode
;
2433 ventoy_override_chunk
*override
;
2434 ventoy_img_chunk_list wimchunk
;
2436 debug("vlnk wim chain data begin <%s> ...\n", wimfile
->name
);
2438 if (NULL
== g_wimiso_chunk_list
.chunk
|| NULL
== g_wimiso_path
)
2440 grub_printf("ventoy not ready\n");
2444 imgoffset
= ventoy_get_wim_iso_offset(g_wimiso_path
);
2447 grub_printf("image offset not found\n");
2451 if (0 != ventoy_get_wim_chunklist(wimfile
, &wimchunk
))
2453 grub_printf("Failed to get wim chunklist\n");
2456 wimsize
= wimfile
->size
;
2458 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s", g_wimiso_path
);
2464 boot_catlog
= ventoy_get_iso_boot_catlog(file
);
2466 img_chunk1_size
= g_wimiso_chunk_list
.cur_chunk
* sizeof(ventoy_img_chunk
);
2467 img_chunk2_size
= wimchunk
.cur_chunk
* sizeof(ventoy_img_chunk
);
2468 override_size
= sizeof(ventoy_override_chunk
) + g_wimiso_size
;
2470 size
= sizeof(ventoy_chain_head
) + img_chunk1_size
+ img_chunk2_size
+ override_size
;
2472 pLastChain
= grub_env_get("vtoy_chain_mem_addr");
2475 chain
= (ventoy_chain_head
*)grub_strtoul(pLastChain
, NULL
, 16);
2478 debug("free last chain memory %p\n", chain
);
2483 chain
= ventoy_alloc_chain(size
);
2486 grub_printf("Failed to alloc chain win2 memory size %u\n", size
);
2487 grub_file_close(file
);
2491 ventoy_memfile_env_set("vtoy_chain_mem", chain
, (ulonglong
)size
);
2493 grub_memset(chain
, 0, sizeof(ventoy_chain_head
));
2495 /* part 1: os parameter */
2496 g_ventoy_chain_type
= ventoy_chain_wim
;
2497 ventoy_fill_os_param(wimfile
, &(chain
->os_param
));
2499 /* part 2: chain head */
2500 disk
= wimfile
->device
->disk
;
2501 chain
->disk_drive
= disk
->id
;
2502 chain
->disk_sector_size
= (1 << disk
->log_sector_size
);
2503 chain
->real_img_size_in_bytes
= ventoy_align_2k(file
->size
) + ventoy_align_2k(wimsize
);
2504 chain
->virt_img_size_in_bytes
= chain
->real_img_size_in_bytes
;
2505 chain
->boot_catalog
= boot_catlog
;
2507 if (!ventoy_is_efi_os())
2509 grub_file_seek(file
, boot_catlog
* 2048);
2510 grub_file_read(file
, chain
->boot_catalog_sector
, sizeof(chain
->boot_catalog_sector
));
2513 /* part 3: image chunk */
2514 chain
->img_chunk_offset
= sizeof(ventoy_chain_head
);
2515 chain
->img_chunk_num
= g_wimiso_chunk_list
.cur_chunk
+ wimchunk
.cur_chunk
;
2516 grub_memcpy((char *)chain
+ chain
->img_chunk_offset
, g_wimiso_chunk_list
.chunk
, img_chunk1_size
);
2518 chunknode
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
2519 for (i
= 0; i
< g_wimiso_chunk_list
.cur_chunk
; i
++)
2521 chunknode
->disk_end_sector
= chunknode
->disk_end_sector
- chunknode
->disk_start_sector
;
2522 chunknode
->disk_start_sector
= 0;
2526 /* fs cluster size >= 2048, so don't need to proc align */
2529 chunknode
= wimchunk
.chunk
+ wimchunk
.cur_chunk
- 1;
2530 i
= (chunknode
->disk_end_sector
+ 1 - chunknode
->disk_start_sector
) % 4;
2533 chunknode
->disk_end_sector
+= 4 - i
;
2536 isosector
= (grub_uint32_t
)((file
->size
+ 2047) / 2048);
2537 for (i
= 0; i
< wimchunk
.cur_chunk
; i
++)
2539 chunknode
= wimchunk
.chunk
+ i
;
2540 chunknode
->img_start_sector
= isosector
;
2541 chunknode
->img_end_sector
= chunknode
->img_start_sector
+
2542 ((chunknode
->disk_end_sector
+ 1 - chunknode
->disk_start_sector
) / 4) - 1;
2543 isosector
= chunknode
->img_end_sector
+ 1;
2546 grub_memcpy((char *)chain
+ chain
->img_chunk_offset
+ img_chunk1_size
, wimchunk
.chunk
, img_chunk2_size
);
2548 /* part 4: override chunk */
2549 chain
->override_chunk_offset
= chain
->img_chunk_offset
+ img_chunk1_size
+ img_chunk2_size
;
2550 chain
->override_chunk_num
= 1;
2552 override
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
2553 override
->img_offset
= 0;
2554 override
->override_size
= g_wimiso_size
;
2556 grub_file_seek(file
, 0);
2557 grub_file_read(file
, override
->override_data
, file
->size
);
2559 dirent
= (ventoy_iso9660_override
*)(override
->override_data
+ imgoffset
);
2560 dirent
->first_sector
= (grub_uint32_t
)((file
->size
+ 2047) / 2048);
2561 dirent
->size
= (grub_uint32_t
)(wimsize
);
2562 dirent
->first_sector_be
= grub_swap_bytes32(dirent
->first_sector
);
2563 dirent
->size_be
= grub_swap_bytes32(dirent
->size
);
2565 debug("imgoffset=%u first_sector=0x%x size=0x%x\n", imgoffset
, dirent
->first_sector
, dirent
->size
);
2567 if (ventoy_is_efi_os() == 0)
2569 ventoy_windows_drive_map(chain
, 0);
2572 grub_file_close(file
);
2574 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
2577 static grub_err_t
ventoy_normal_wim_chain_data(grub_file_t wimfile
)
2579 grub_uint32_t i
= 0;
2580 grub_uint32_t imgoffset
= 0;
2581 grub_uint32_t size
= 0;
2582 grub_uint32_t isosector
= 0;
2583 grub_uint64_t wimsize
= 0;
2584 grub_uint32_t boot_catlog
= 0;
2585 grub_uint32_t img_chunk1_size
= 0;
2586 grub_uint32_t img_chunk2_size
= 0;
2587 grub_uint32_t override_size
= 0;
2590 const char *pLastChain
= NULL
;
2591 ventoy_chain_head
*chain
;
2592 ventoy_iso9660_override
*dirent
;
2593 ventoy_img_chunk
*chunknode
;
2594 ventoy_override_chunk
*override
;
2595 ventoy_img_chunk_list wimchunk
;
2597 debug("normal wim chain data begin <%s> ...\n", wimfile
->name
);
2599 if (NULL
== g_wimiso_chunk_list
.chunk
|| NULL
== g_wimiso_path
)
2601 grub_printf("ventoy not ready\n");
2605 imgoffset
= ventoy_get_wim_iso_offset(g_wimiso_path
);
2608 grub_printf("image offset not found\n");
2612 if (0 != ventoy_get_wim_chunklist(wimfile
, &wimchunk
))
2614 grub_printf("Failed to get wim chunklist\n");
2617 wimsize
= wimfile
->size
;
2619 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s", g_wimiso_path
);
2625 boot_catlog
= ventoy_get_iso_boot_catlog(file
);
2627 img_chunk1_size
= g_wimiso_chunk_list
.cur_chunk
* sizeof(ventoy_img_chunk
);
2628 img_chunk2_size
= wimchunk
.cur_chunk
* sizeof(ventoy_img_chunk
);
2629 override_size
= sizeof(ventoy_override_chunk
);
2631 size
= sizeof(ventoy_chain_head
) + img_chunk1_size
+ img_chunk2_size
+ override_size
;
2633 pLastChain
= grub_env_get("vtoy_chain_mem_addr");
2636 chain
= (ventoy_chain_head
*)grub_strtoul(pLastChain
, NULL
, 16);
2639 debug("free last chain memory %p\n", chain
);
2644 chain
= ventoy_alloc_chain(size
);
2647 grub_printf("Failed to alloc chain win3 memory size %u\n", size
);
2648 grub_file_close(file
);
2652 ventoy_memfile_env_set("vtoy_chain_mem", chain
, (ulonglong
)size
);
2654 grub_memset(chain
, 0, sizeof(ventoy_chain_head
));
2656 /* part 1: os parameter */
2657 g_ventoy_chain_type
= ventoy_chain_wim
;
2658 ventoy_fill_os_param(file
, &(chain
->os_param
));
2660 /* part 2: chain head */
2661 disk
= file
->device
->disk
;
2662 chain
->disk_drive
= disk
->id
;
2663 chain
->disk_sector_size
= (1 << disk
->log_sector_size
);
2664 chain
->real_img_size_in_bytes
= ventoy_align_2k(file
->size
) + ventoy_align_2k(wimsize
);
2665 chain
->virt_img_size_in_bytes
= chain
->real_img_size_in_bytes
;
2666 chain
->boot_catalog
= boot_catlog
;
2668 if (!ventoy_is_efi_os())
2670 grub_file_seek(file
, boot_catlog
* 2048);
2671 grub_file_read(file
, chain
->boot_catalog_sector
, sizeof(chain
->boot_catalog_sector
));
2674 /* part 3: image chunk */
2675 chain
->img_chunk_offset
= sizeof(ventoy_chain_head
);
2676 chain
->img_chunk_num
= g_wimiso_chunk_list
.cur_chunk
+ wimchunk
.cur_chunk
;
2677 grub_memcpy((char *)chain
+ chain
->img_chunk_offset
, g_wimiso_chunk_list
.chunk
, img_chunk1_size
);
2679 /* fs cluster size >= 2048, so don't need to proc align */
2682 chunknode
= wimchunk
.chunk
+ wimchunk
.cur_chunk
- 1;
2683 i
= (chunknode
->disk_end_sector
+ 1 - chunknode
->disk_start_sector
) % 4;
2686 chunknode
->disk_end_sector
+= 4 - i
;
2689 isosector
= (grub_uint32_t
)((file
->size
+ 2047) / 2048);
2690 for (i
= 0; i
< wimchunk
.cur_chunk
; i
++)
2692 chunknode
= wimchunk
.chunk
+ i
;
2693 chunknode
->img_start_sector
= isosector
;
2694 chunknode
->img_end_sector
= chunknode
->img_start_sector
+
2695 ((chunknode
->disk_end_sector
+ 1 - chunknode
->disk_start_sector
) / 4) - 1;
2696 isosector
= chunknode
->img_end_sector
+ 1;
2699 grub_memcpy((char *)chain
+ chain
->img_chunk_offset
+ img_chunk1_size
, wimchunk
.chunk
, img_chunk2_size
);
2701 /* part 4: override chunk */
2702 chain
->override_chunk_offset
= chain
->img_chunk_offset
+ img_chunk1_size
+ img_chunk2_size
;
2703 chain
->override_chunk_num
= 1;
2705 override
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
2706 override
->img_offset
= imgoffset
;
2707 override
->override_size
= sizeof(ventoy_iso9660_override
);
2709 dirent
= (ventoy_iso9660_override
*)(override
->override_data
);
2710 dirent
->first_sector
= (grub_uint32_t
)((file
->size
+ 2047) / 2048);
2711 dirent
->size
= (grub_uint32_t
)(wimsize
);
2712 dirent
->first_sector_be
= grub_swap_bytes32(dirent
->first_sector
);
2713 dirent
->size_be
= grub_swap_bytes32(dirent
->size
);
2715 debug("imgoffset=%u first_sector=0x%x size=0x%x\n", imgoffset
, dirent
->first_sector
, dirent
->size
);
2717 if (ventoy_is_efi_os() == 0)
2719 ventoy_windows_drive_map(chain
, 0);
2722 grub_file_close(file
);
2724 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
2727 grub_err_t
ventoy_cmd_wim_chain_data(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2730 grub_file_t wimfile
;
2735 wimfile
= grub_file_open(args
[0], VENTOY_FILE_TYPE
);
2743 ret
= ventoy_vlnk_wim_chain_data(wimfile
);
2747 ret
= ventoy_normal_wim_chain_data(wimfile
);
2750 grub_file_close(wimfile
);
2754 int ventoy_chain_file_size(const char *path
)
2759 file
= grub_file_open(path
, VENTOY_FILE_TYPE
);
2760 size
= (int)(file
->size
);
2762 grub_file_close(file
);
2767 int ventoy_chain_file_read(const char *path
, int offset
, int len
, void *buf
)
2772 file
= grub_file_open(path
, VENTOY_FILE_TYPE
);
2773 grub_file_seek(file
, offset
);
2774 size
= grub_file_read(file
, buf
, len
);
2775 grub_file_close(file
);