]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_windows.c
3bcadc4d95672ad07f7e035090fae7d085b86ee9
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / ventoy / ventoy_windows.c
1 /******************************************************************************
2 * ventoy_windows.c
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include <grub/types.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/err.h>
25 #include <grub/dl.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>
35 #include <grub/net.h>
36 #include <grub/time.h>
37 #include <grub/crypto.h>
38 #include <grub/ventoy.h>
39 #include "ventoy_def.h"
40
41 GRUB_MOD_LICENSE ("GPLv3+");
42
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;
47
48 static grub_uint64_t g_suppress_wincd_override_offset = 0;
49 static grub_uint32_t g_suppress_wincd_override_data = 0;
50
51 grub_uint8_t g_temp_buf[512];
52
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 );
55
56 static wim_patch *ventoy_find_wim_patch(const char *path)
57 {
58 int len = (int)grub_strlen(path);
59 wim_patch *node = g_wim_patch_head;
60
61 while (node)
62 {
63 if (len == node->pathlen && 0 == grub_strcmp(path, node->path))
64 {
65 return node;
66 }
67 node = node->next;
68 }
69
70 return NULL;
71 }
72
73 static int ventoy_collect_wim_patch(const char *bcdfile)
74 {
75 int i, j, k;
76 int rc = 1;
77 grub_uint64_t magic;
78 grub_file_t file = NULL;
79 char *buf = NULL;
80 wim_patch *node = NULL;
81 char c;
82 grub_uint8_t byte;
83 char valid;
84 char path[256];
85
86 g_ventoy_case_insensitive = 1;
87 file = grub_file_open(bcdfile, VENTOY_FILE_TYPE);
88 g_ventoy_case_insensitive = 0;
89 if (!file)
90 {
91 debug("Failed to open file %s\n", bcdfile);
92 grub_errno = 0;
93 goto end;
94 }
95
96 buf = grub_malloc(file->size + 8);
97 if (!buf)
98 {
99 goto end;
100 }
101
102 grub_file_read(file, buf, file->size);
103
104 for (i = 0; i < (int)file->size - 8; i++)
105 {
106 if (buf[i + 8] != 0)
107 {
108 continue;
109 }
110
111 magic = *(grub_uint64_t *)(buf + i);
112
113 /* .wim .WIM .Wim */
114 if ((magic == 0x006D00690077002EULL) ||
115 (magic == 0x004D00490057002EULL) ||
116 (magic == 0x006D00690057002EULL))
117 {
118 for (j = i; j > 0; j-= 2)
119 {
120 if (*(grub_uint16_t *)(buf + j) == 0)
121 {
122 break;
123 }
124 }
125
126 if (j > 0)
127 {
128 byte = (grub_uint8_t)(*(grub_uint16_t *)(buf + j + 2));
129 if (byte != '/' && byte != '\\')
130 {
131 continue;
132 }
133
134 valid = 1;
135 for (k = 0, j += 2; k < (int)sizeof(path) - 1 && j < i + 8; j += 2)
136 {
137 byte = (grub_uint8_t)(*(grub_uint16_t *)(buf + j));
138 c = (char)byte;
139 if (byte > '~' || byte < ' ') /* not printable */
140 {
141 valid = 0;
142 break;
143 }
144 else if (c == '\\')
145 {
146 c = '/';
147 }
148
149 path[k++] = c;
150 }
151 path[k++] = 0;
152
153 debug("@@@@ Find wim flag:<%s>\n", path);
154
155 if (0 == valid)
156 {
157 debug("Invalid wim file %d\n", k);
158 }
159 else if (NULL == ventoy_find_wim_patch(path))
160 {
161 node = grub_zalloc(sizeof(wim_patch));
162 if (node)
163 {
164 node->pathlen = grub_snprintf(node->path, sizeof(node->path), "%s", path);
165
166 debug("add patch <%s>\n", path);
167
168 if (g_wim_patch_head)
169 {
170 node->next = g_wim_patch_head;
171 }
172 g_wim_patch_head = node;
173
174 g_wim_total_patch_count++;
175 }
176 }
177 else
178 {
179 debug("wim <%s> already exist\n", path);
180 }
181 }
182 }
183 }
184
185 end:
186 check_free(file, grub_file_close);
187 grub_check_free(buf);
188 return rc;
189 }
190
191 grub_err_t ventoy_cmd_wim_patch_count(grub_extcmd_context_t ctxt, int argc, char **args)
192 {
193 char buf[32];
194
195 (void)ctxt;
196 (void)argc;
197 (void)args;
198
199 if (argc == 1)
200 {
201 grub_snprintf(buf, sizeof(buf), "%d", g_wim_total_patch_count);
202 ventoy_set_env(args[0], buf);
203 }
204
205 return 0;
206 }
207
208 grub_err_t ventoy_cmd_collect_wim_patch(grub_extcmd_context_t ctxt, int argc, char **args)
209 {
210 wim_patch *node = NULL;
211
212 (void)ctxt;
213 (void)argc;
214 (void)args;
215
216 if (argc != 2)
217 {
218 return 1;
219 }
220
221 debug("ventoy_cmd_collect_wim_patch %s %s\n", args[0], args[1]);
222
223 if (grub_strcmp(args[0], "bcd") == 0)
224 {
225 ventoy_collect_wim_patch(args[1]);
226 return 0;
227 }
228
229 if (NULL == ventoy_find_wim_patch(args[1]))
230 {
231 node = grub_zalloc(sizeof(wim_patch));
232 if (node)
233 {
234 node->pathlen = grub_snprintf(node->path, sizeof(node->path), "%s", args[1]);
235
236 debug("add patch <%s>\n", args[1]);
237
238 if (g_wim_patch_head)
239 {
240 node->next = g_wim_patch_head;
241 }
242 g_wim_patch_head = node;
243
244 g_wim_total_patch_count++;
245 }
246 }
247
248 return 0;
249 }
250
251
252 grub_err_t ventoy_cmd_dump_wim_patch(grub_extcmd_context_t ctxt, int argc, char **args)
253 {
254 int i = 0;
255 wim_patch *node = NULL;
256
257 (void)ctxt;
258 (void)argc;
259 (void)args;
260
261 for (node = g_wim_patch_head; node; node = node->next)
262 {
263 grub_printf("%d %s [%s]\n", i++, node->path, node->valid ? "SUCCESS" : "FAIL");
264 }
265
266 return 0;
267 }
268
269
270 static int wim_name_cmp(const char *search, grub_uint16_t *name, grub_uint16_t namelen)
271 {
272 char c1 = vtoy_to_upper(*search);
273 char c2 = vtoy_to_upper(*name);
274
275 while (namelen > 0 && (c1 == c2))
276 {
277 search++;
278 name++;
279 namelen--;
280
281 c1 = vtoy_to_upper(*search);
282 c2 = vtoy_to_upper(*name);
283 }
284
285 if (namelen == 0 && *search == 0)
286 {
287 return 0;
288 }
289
290 return 1;
291 }
292
293 static int ventoy_is_pe64(grub_uint8_t *buffer)
294 {
295 grub_uint32_t pe_off;
296
297 if (buffer[0] != 'M' || buffer[1] != 'Z')
298 {
299 return 0;
300 }
301
302 pe_off = *(grub_uint32_t *)(buffer + 60);
303
304 if (buffer[pe_off] != 'P' || buffer[pe_off + 1] != 'E')
305 {
306 return 0;
307 }
308
309 if (*(grub_uint16_t *)(buffer + pe_off + 24) == 0x020b)
310 {
311 return 1;
312 }
313
314 return 0;
315 }
316
317 grub_err_t ventoy_cmd_is_pe64(grub_extcmd_context_t ctxt, int argc, char **args)
318 {
319 int ret = 1;
320 grub_file_t file;
321 grub_uint8_t buf[512];
322
323 (void)ctxt;
324 (void)argc;
325
326 file = grub_file_open(args[0], VENTOY_FILE_TYPE);
327 if (!file)
328 {
329 return 1;
330 }
331
332 grub_memset(buf, 0, 512);
333 grub_file_read(file, buf, 512);
334 if (ventoy_is_pe64(buf))
335 {
336 debug("%s is PE64\n", args[0]);
337 ret = 0;
338 }
339 else
340 {
341 debug("%s is PE32\n", args[0]);
342 }
343 grub_file_close(file);
344
345 return ret;
346 }
347
348 grub_err_t ventoy_cmd_sel_wimboot(grub_extcmd_context_t ctxt, int argc, char **args)
349 {
350 int size;
351 char *buf = NULL;
352 char configfile[128];
353
354 (void)ctxt;
355 (void)argc;
356 (void)args;
357
358 debug("select wimboot argc:%d\n", argc);
359
360 buf = (char *)grub_malloc(8192);
361 if (!buf)
362 {
363 return 0;
364 }
365
366 size = (int)grub_snprintf(buf, 8192,
367 "menuentry \"Windows Setup (32-bit)\" {\n"
368 " set vtoy_wimboot_sel=32\n"
369 "}\n"
370 "menuentry \"Windows Setup (64-bit)\" {\n"
371 " set vtoy_wimboot_sel=64\n"
372 "}\n"
373 );
374 buf[size] = 0;
375
376 g_ventoy_menu_esc = 1;
377 g_ventoy_suppress_esc = 1;
378
379 grub_snprintf(configfile, sizeof(configfile), "configfile mem:0x%llx:size:%d", (ulonglong)(ulong)buf, size);
380 grub_script_execute_sourcecode(configfile);
381
382 g_ventoy_menu_esc = 0;
383 g_ventoy_suppress_esc = 0;
384
385 grub_free(buf);
386
387 if (g_ventoy_last_entry == 0)
388 {
389 debug("last entry=%d %s=32\n", g_ventoy_last_entry, args[0]);
390 grub_env_set(args[0], "32");
391 }
392 else
393 {
394 debug("last entry=%d %s=64\n", g_ventoy_last_entry, args[0]);
395 grub_env_set(args[0], "64");
396 }
397
398 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
399 }
400
401 grub_err_t ventoy_cmd_wimdows_reset(grub_extcmd_context_t ctxt, int argc, char **args)
402 {
403 wim_patch *next = NULL;
404 wim_patch *node = g_wim_patch_head;
405
406 (void)ctxt;
407 (void)argc;
408 (void)args;
409
410 while (node)
411 {
412 next = node->next;
413 grub_free(node);
414 node = next;
415 }
416
417 g_wim_patch_head = NULL;
418 g_wim_total_patch_count = 0;
419 g_wim_valid_patch_count = 0;
420
421 return 0;
422 }
423
424 static int ventoy_load_jump_exe(const char *path, grub_uint8_t **data, grub_uint32_t *size, wim_hash *hash)
425 {
426 grub_uint32_t i;
427 grub_uint32_t align;
428 grub_file_t file;
429
430 debug("windows load jump %s\n", path);
431
432 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", path);
433 if (!file)
434 {
435 debug("Can't open file %s\n", path);
436 return 1;
437 }
438
439 align = ventoy_align((int)file->size, 2048);
440
441 debug("file %s size:%d align:%u\n", path, (int)file->size, align);
442
443 *size = (grub_uint32_t)file->size;
444 *data = (grub_uint8_t *)grub_malloc(align);
445 if ((*data) == NULL)
446 {
447 debug("Failed to alloc memory size %u\n", align);
448 goto end;
449 }
450
451 grub_file_read(file, (*data), file->size);
452
453 if (hash)
454 {
455 grub_crypto_hash(GRUB_MD_SHA1, hash->sha1, (*data), file->size);
456
457 if (g_ventoy_debug)
458 {
459 debug("%s", "jump bin 64 hash: ");
460 for (i = 0; i < sizeof(hash->sha1); i++)
461 {
462 ventoy_debug("%02x ", hash->sha1[i]);
463 }
464 ventoy_debug("\n");
465 }
466 }
467
468 end:
469
470 grub_file_close(file);
471 return 0;
472 }
473
474 static int ventoy_get_override_info(grub_file_t file, wim_tail *wim_data)
475 {
476 grub_uint32_t start_block;
477 grub_uint64_t file_offset;
478 grub_uint64_t override_offset;
479 grub_uint32_t override_len;
480 grub_uint64_t fe_entry_size_offset;
481
482 if (grub_strcmp(file->fs->name, "iso9660") == 0)
483 {
484 g_iso_fs_type = wim_data->iso_type = 0;
485 override_len = sizeof(ventoy_iso9660_override);
486 override_offset = grub_iso9660_get_last_file_dirent_pos(file) + 2;
487
488 grub_file_read(file, &start_block, 1); // just read for hook trigger
489 file_offset = grub_iso9660_get_last_read_pos(file);
490
491 debug("iso9660 wim size:%llu override_offset:%llu file_offset:%llu\n",
492 (ulonglong)file->size, (ulonglong)override_offset, (ulonglong)file_offset);
493 }
494 else
495 {
496 g_iso_fs_type = wim_data->iso_type = 1;
497 override_len = sizeof(ventoy_udf_override);
498 override_offset = grub_udf_get_last_file_attr_offset(file, &start_block, &fe_entry_size_offset);
499
500 file_offset = grub_udf_get_file_offset(file);
501
502 debug("UDF wim size:%llu override_offset:%llu file_offset:%llu start_block=%u\n",
503 (ulonglong)file->size, (ulonglong)override_offset, (ulonglong)file_offset, start_block);
504 }
505
506 wim_data->file_offset = file_offset;
507 wim_data->udf_start_block = start_block;
508 wim_data->fe_entry_size_offset = fe_entry_size_offset;
509 wim_data->override_offset = override_offset;
510 wim_data->override_len = override_len;
511
512 return 0;
513 }
514
515 static int ventoy_read_resource(grub_file_t fp, wim_header *wimhdr, wim_resource_header *head, void **buffer)
516 {
517 int decompress_len = 0;
518 int total_decompress = 0;
519 grub_uint32_t i = 0;
520 grub_uint32_t chunk_num = 0;
521 grub_uint32_t chunk_size = 0;
522 grub_uint32_t last_chunk_size = 0;
523 grub_uint32_t last_decompress_size = 0;
524 grub_uint32_t cur_offset = 0;
525 grub_uint8_t *cur_dst = NULL;
526 grub_uint8_t *buffer_compress = NULL;
527 grub_uint8_t *buffer_decompress = NULL;
528 grub_uint32_t *chunk_offset = NULL;
529
530 buffer_decompress = (grub_uint8_t *)grub_malloc(head->raw_size + head->size_in_wim);
531 if (NULL == buffer_decompress)
532 {
533 return 0;
534 }
535
536 grub_file_seek(fp, head->offset);
537
538 if (head->size_in_wim == head->raw_size)
539 {
540 grub_file_read(fp, buffer_decompress, head->size_in_wim);
541 *buffer = buffer_decompress;
542 return 0;
543 }
544
545 buffer_compress = buffer_decompress + head->raw_size;
546 grub_file_read(fp, buffer_compress, head->size_in_wim);
547
548 chunk_num = (head->raw_size + WIM_CHUNK_LEN - 1) / WIM_CHUNK_LEN;
549 cur_offset = (chunk_num - 1) * 4;
550 chunk_offset = (grub_uint32_t *)buffer_compress;
551
552 //debug("%llu %llu chunk_num=%lu", (ulonglong)head->size_in_wim, (ulonglong)head->raw_size, chunk_num);
553
554 cur_dst = buffer_decompress;
555
556 for (i = 0; i < chunk_num - 1; i++)
557 {
558 chunk_size = (i == 0) ? chunk_offset[i] : chunk_offset[i] - chunk_offset[i - 1];
559
560 if (WIM_CHUNK_LEN == chunk_size)
561 {
562 grub_memcpy(cur_dst, buffer_compress + cur_offset, chunk_size);
563 decompress_len = (int)chunk_size;
564 }
565 else
566 {
567 if (wimhdr->flags & FLAG_HEADER_COMPRESS_XPRESS)
568 {
569 decompress_len = (int)xca_decompress(buffer_compress + cur_offset, chunk_size, cur_dst);
570 }
571 else
572 {
573 decompress_len = (int)lzx_decompress(buffer_compress + cur_offset, chunk_size, cur_dst);
574 }
575 }
576
577 //debug("chunk_size:%u decompresslen:%d\n", chunk_size, decompress_len);
578
579 total_decompress += decompress_len;
580 cur_dst += decompress_len;
581 cur_offset += chunk_size;
582 }
583
584 /* last chunk */
585 last_chunk_size = (grub_uint32_t)(head->size_in_wim - cur_offset);
586 last_decompress_size = head->raw_size - total_decompress;
587
588 if (last_chunk_size < WIM_CHUNK_LEN && last_chunk_size == last_decompress_size)
589 {
590 debug("Last chunk %u uncompressed\n", last_chunk_size);
591 grub_memcpy(cur_dst, buffer_compress + cur_offset, last_chunk_size);
592 decompress_len = (int)last_chunk_size;
593 }
594 else
595 {
596 if (wimhdr->flags & FLAG_HEADER_COMPRESS_XPRESS)
597 {
598 decompress_len = (int)xca_decompress(buffer_compress + cur_offset, head->size_in_wim - cur_offset, cur_dst);
599 }
600 else
601 {
602 decompress_len = (int)lzx_decompress(buffer_compress + cur_offset, head->size_in_wim - cur_offset, cur_dst);
603 }
604 }
605
606 cur_dst += decompress_len;
607 total_decompress += decompress_len;
608
609 //debug("last chunk_size:%u decompresslen:%d tot:%d\n", last_chunk_size, decompress_len, total_decompress);
610
611 if (cur_dst != buffer_decompress + head->raw_size)
612 {
613 debug("head->size_in_wim:%llu head->raw_size:%llu cur_dst:%p buffer_decompress:%p total_decompress:%d\n",
614 (ulonglong)head->size_in_wim, (ulonglong)head->raw_size, cur_dst, buffer_decompress, total_decompress);
615 grub_free(buffer_decompress);
616 return 1;
617 }
618
619 *buffer = buffer_decompress;
620 return 0;
621 }
622
623
624 static wim_directory_entry * search_wim_dirent(wim_directory_entry *dir, const char *search_name)
625 {
626 do
627 {
628 if (dir->len && dir->name_len)
629 {
630 if (wim_name_cmp(search_name, (grub_uint16_t *)(dir + 1), dir->name_len / 2) == 0)
631 {
632 return dir;
633 }
634 }
635 dir = (wim_directory_entry *)((grub_uint8_t *)dir + dir->len);
636 } while(dir->len);
637
638 return NULL;
639 }
640
641 static wim_directory_entry * search_full_wim_dirent
642 (
643 void *meta_data,
644 wim_directory_entry *dir,
645 const char **path
646 )
647 {
648 wim_directory_entry *subdir = NULL;
649 wim_directory_entry *search = dir;
650
651 while (*path)
652 {
653 subdir = (wim_directory_entry *)((char *)meta_data + search->subdir);
654 search = search_wim_dirent(subdir, *path);
655 path++;
656 }
657
658 return search;
659 }
660
661 static wim_directory_entry * search_replace_wim_dirent(void *meta_data, wim_directory_entry *dir)
662 {
663 wim_directory_entry *wim_dirent1 = NULL;
664 wim_directory_entry *wim_dirent2 = NULL;
665 const char *pecmd_path[] = { "Windows", "System32", "pecmd.exe", NULL };
666 const char *wpeinit_path[] = { "Windows", "System32", "wpeinit.exe", NULL };
667 const char *winpeshl_path[] = { "Windows", "System32", "winpeshl.exe", NULL };
668
669 wim_dirent1 = search_full_wim_dirent(meta_data, dir, pecmd_path);
670 debug("search pecmd.exe %p\n", wim_dirent1);
671 if (wim_dirent1)
672 {
673 wim_dirent2 = search_full_wim_dirent(meta_data, dir, wpeinit_path);
674 debug("search wpeinit.exe %p\n", wim_dirent1);
675 if (wim_dirent2)
676 {
677 return wim_dirent2;
678 }
679 return wim_dirent1;
680 }
681
682 wim_dirent1 = search_full_wim_dirent(meta_data, dir, winpeshl_path);
683 debug("search winpeshl.exe %p\n", wim_dirent1);
684 if (wim_dirent1)
685 {
686 return wim_dirent1;
687 }
688
689 return NULL;
690 }
691
692
693 static wim_lookup_entry * ventoy_find_look_entry(wim_header *header, wim_lookup_entry *lookup, wim_hash *hash)
694 {
695 grub_uint32_t i = 0;
696
697 for (i = 0; i < (grub_uint32_t)header->lookup.raw_size / sizeof(wim_lookup_entry); i++)
698 {
699 if (grub_memcmp(&lookup[i].hash, hash, sizeof(wim_hash)) == 0)
700 {
701 return lookup + i;
702 }
703 }
704
705 return NULL;
706 }
707
708 static wim_lookup_entry * ventoy_find_meta_entry(wim_header *header, wim_lookup_entry *lookup)
709 {
710 grub_uint32_t i = 0;
711 grub_uint32_t index = 0;;
712
713 if ((header == NULL) || (lookup == NULL))
714 {
715 return NULL;
716 }
717
718 for (i = 0; i < (grub_uint32_t)header->lookup.raw_size / sizeof(wim_lookup_entry); i++)
719 {
720 if (lookup[i].resource.flags & RESHDR_FLAG_METADATA)
721 {
722 index++;
723 if (index == header->boot_index)
724 {
725 return lookup + i;
726 }
727 }
728 }
729
730 return NULL;
731 }
732
733 static grub_uint64_t ventoy_get_stream_len(wim_directory_entry *dir)
734 {
735 grub_uint16_t i;
736 grub_uint64_t offset = 0;
737 wim_stream_entry *stream = (wim_stream_entry *)((char *)dir + dir->len);
738
739 for (i = 0; i < dir->streams; i++)
740 {
741 offset += stream->len;
742 stream = (wim_stream_entry *)((char *)stream + stream->len);
743 }
744
745 return offset;
746 }
747
748 static int ventoy_update_all_hash(wim_patch *patch, void *meta_data, wim_directory_entry *dir)
749 {
750 if ((meta_data == NULL) || (dir == NULL))
751 {
752 return 0;
753 }
754
755 if (dir->len < sizeof(wim_directory_entry))
756 {
757 return 0;
758 }
759
760 do
761 {
762 if (dir->subdir == 0 && grub_memcmp(dir->hash.sha1, patch->old_hash.sha1, sizeof(wim_hash)) == 0)
763 {
764 debug("find target file, name_len:%u upadte hash\n", dir->name_len);
765 grub_memcpy(dir->hash.sha1, &(patch->wim_data.bin_hash), sizeof(wim_hash));
766 }
767
768 if (dir->subdir)
769 {
770 ventoy_update_all_hash(patch, meta_data, (wim_directory_entry *)((char *)meta_data + dir->subdir));
771 }
772
773 if (dir->streams)
774 {
775 dir = (wim_directory_entry *)((char *)dir + dir->len + ventoy_get_stream_len(dir));
776 }
777 else
778 {
779 dir = (wim_directory_entry *)((char *)dir + dir->len);
780 }
781 } while (dir->len >= sizeof(wim_directory_entry));
782
783 return 0;
784 }
785
786 static int ventoy_cat_exe_file_data(wim_tail *wim_data, grub_uint32_t exe_len, grub_uint8_t *exe_data)
787 {
788 int pe64 = 0;
789 char file[256];
790 grub_uint32_t jump_len = 0;
791 grub_uint32_t jump_align = 0;
792 grub_uint8_t *jump_data = NULL;
793
794 pe64 = ventoy_is_pe64(exe_data);
795
796 grub_snprintf(file, sizeof(file), "%s/vtoyjump%d.exe", grub_env_get("vtoy_path"), pe64 ? 64 : 32);
797 ventoy_load_jump_exe(file, &jump_data, &jump_len, NULL);
798 jump_align = ventoy_align(jump_len, 16);
799
800 wim_data->jump_exe_len = jump_len;
801 wim_data->bin_raw_len = jump_align + sizeof(ventoy_os_param) + sizeof(ventoy_windows_data) + exe_len;
802 wim_data->bin_align_len = ventoy_align(wim_data->bin_raw_len, 2048);
803
804 wim_data->jump_bin_data = grub_malloc(wim_data->bin_align_len);
805 if (wim_data->jump_bin_data)
806 {
807 grub_memcpy(wim_data->jump_bin_data, jump_data, jump_len);
808 grub_memcpy(wim_data->jump_bin_data + jump_align + sizeof(ventoy_os_param) + sizeof(ventoy_windows_data), exe_data, exe_len);
809 }
810
811 debug("jump_exe_len:%u bin_raw_len:%u bin_align_len:%u\n",
812 wim_data->jump_exe_len, wim_data->bin_raw_len, wim_data->bin_align_len);
813
814 return 0;
815 }
816
817 int ventoy_fill_windows_rtdata(void *buf, char *isopath)
818 {
819 char *pos = NULL;
820 char *script = NULL;
821 ventoy_windows_data *data = (ventoy_windows_data *)buf;
822
823 grub_memset(data, 0, sizeof(ventoy_windows_data));
824
825 pos = grub_strstr(isopath, "/");
826 if (!pos)
827 {
828 return 1;
829 }
830
831 script = ventoy_plugin_get_cur_install_template(pos);
832 if (script)
833 {
834 debug("auto install script <%s>\n", script);
835 grub_snprintf(data->auto_install_script, sizeof(data->auto_install_script) - 1, "%s", script);
836 }
837 else
838 {
839 debug("auto install script skipped or not configed %s\n", pos);
840 }
841
842 script = (char *)ventoy_plugin_get_injection(pos);
843 if (script)
844 {
845 if (ventoy_check_file_exist("%s%s", ventoy_get_env("vtoy_iso_part"), script))
846 {
847 debug("injection archive <%s> OK\n", script);
848 grub_snprintf(data->injection_archive, sizeof(data->injection_archive) - 1, "%s", script);
849 }
850 else
851 {
852 debug("injection archive <%s> NOT exist\n", script);
853 }
854 }
855 else
856 {
857 debug("injection archive not configed %s\n", pos);
858 }
859
860 return 0;
861 }
862
863 static int ventoy_update_before_chain(ventoy_os_param *param, char *isopath)
864 {
865 grub_uint32_t jump_align = 0;
866 wim_lookup_entry *meta_look = NULL;
867 wim_security_header *security = NULL;
868 wim_directory_entry *rootdir = NULL;
869 wim_header *head = NULL;
870 wim_lookup_entry *lookup = NULL;
871 wim_patch *node = NULL;
872 wim_tail *wim_data = NULL;
873
874 for (node = g_wim_patch_head; node; node = node->next)
875 {
876 if (0 == node->valid)
877 {
878 continue;
879 }
880
881 wim_data = &node->wim_data;
882 head = &wim_data->wim_header;
883 lookup = (wim_lookup_entry *)wim_data->new_lookup_data;
884
885 jump_align = ventoy_align(wim_data->jump_exe_len, 16);
886 if (wim_data->jump_bin_data)
887 {
888 grub_memcpy(wim_data->jump_bin_data + jump_align, param, sizeof(ventoy_os_param));
889 ventoy_fill_windows_rtdata(wim_data->jump_bin_data + jump_align + sizeof(ventoy_os_param), isopath);
890 }
891
892 grub_crypto_hash(GRUB_MD_SHA1, wim_data->bin_hash.sha1, wim_data->jump_bin_data, wim_data->bin_raw_len);
893
894 security = (wim_security_header *)wim_data->new_meta_data;
895 if (security->len > 0)
896 {
897 rootdir = (wim_directory_entry *)(wim_data->new_meta_data + ((security->len + 7) & 0xFFFFFFF8U));
898 }
899 else
900 {
901 rootdir = (wim_directory_entry *)(wim_data->new_meta_data + 8);
902 }
903
904 /* update all winpeshl.exe dirent entry's hash */
905 ventoy_update_all_hash(node, wim_data->new_meta_data, rootdir);
906
907 /* update winpeshl.exe lookup entry data (hash/offset/length) */
908 if (node->replace_look)
909 {
910 debug("update replace lookup entry_id:%ld\n", ((long)node->replace_look - (long)lookup) / sizeof(wim_lookup_entry));
911 node->replace_look->resource.raw_size = wim_data->bin_raw_len;
912 node->replace_look->resource.size_in_wim = wim_data->bin_raw_len;
913 node->replace_look->resource.flags = 0;
914 node->replace_look->resource.offset = wim_data->wim_align_size;
915
916 grub_memcpy(node->replace_look->hash.sha1, wim_data->bin_hash.sha1, sizeof(wim_hash));
917 }
918
919 /* update metadata's hash */
920 meta_look = ventoy_find_meta_entry(head, lookup);
921 if (meta_look)
922 {
923 debug("find meta lookup entry_id:%ld\n", ((long)meta_look - (long)lookup) / sizeof(wim_lookup_entry));
924 grub_memcpy(&meta_look->resource, &head->metadata, sizeof(wim_resource_header));
925 grub_crypto_hash(GRUB_MD_SHA1, meta_look->hash.sha1, wim_data->new_meta_data, wim_data->new_meta_len);
926 }
927 }
928
929 return 0;
930 }
931
932 static int ventoy_wimdows_locate_wim(const char *disk, wim_patch *patch)
933 {
934 int rc;
935 grub_file_t file;
936 grub_uint32_t exe_len;
937 grub_uint8_t *exe_data = NULL;
938 grub_uint8_t *decompress_data = NULL;
939 wim_lookup_entry *lookup = NULL;
940 wim_security_header *security = NULL;
941 wim_directory_entry *rootdir = NULL;
942 wim_directory_entry *search = NULL;
943 wim_header *head = &(patch->wim_data.wim_header);
944 wim_tail *wim_data = &patch->wim_data;
945
946 debug("windows locate wim start %s\n", patch->path);
947
948 g_ventoy_case_insensitive = 1;
949 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s%s", disk, patch->path);
950 g_ventoy_case_insensitive = 0;
951
952 if (!file)
953 {
954 debug("File %s%s NOT exist\n", disk, patch->path);
955 return 1;
956 }
957
958 ventoy_get_override_info(file, &patch->wim_data);
959
960 grub_file_seek(file, 0);
961 grub_file_read(file, head, sizeof(wim_header));
962
963 if (grub_memcmp(head->signature, WIM_HEAD_SIGNATURE, sizeof(head->signature)))
964 {
965 debug("Not a valid wim file %s\n", (char *)head->signature);
966 grub_file_close(file);
967 return 1;
968 }
969
970 if (head->flags & FLAG_HEADER_COMPRESS_LZMS)
971 {
972 debug("LZMS compress is not supported 0x%x\n", head->flags);
973 grub_file_close(file);
974 return 1;
975 }
976
977 rc = ventoy_read_resource(file, head, &head->metadata, (void **)&decompress_data);
978 if (rc)
979 {
980 grub_printf("failed to read meta data %d\n", rc);
981 grub_file_close(file);
982 return 1;
983 }
984
985 security = (wim_security_header *)decompress_data;
986 if (security->len > 0)
987 {
988 rootdir = (wim_directory_entry *)(decompress_data + ((security->len + 7) & 0xFFFFFFF8U));
989 }
990 else
991 {
992 rootdir = (wim_directory_entry *)(decompress_data + 8);
993 }
994
995 /* search winpeshl.exe dirent entry */
996 search = search_replace_wim_dirent(decompress_data, rootdir);
997 if (!search)
998 {
999 debug("Failed to find replace file %p\n", search);
1000 grub_file_close(file);
1001 return 1;
1002 }
1003
1004 debug("find replace file at %p\n", search);
1005
1006 grub_memcpy(&patch->old_hash, search->hash.sha1, sizeof(wim_hash));
1007
1008 debug("read lookup offset:%llu size:%llu\n", (ulonglong)head->lookup.offset, (ulonglong)head->lookup.raw_size);
1009 lookup = grub_malloc(head->lookup.raw_size);
1010 grub_file_seek(file, head->lookup.offset);
1011 grub_file_read(file, lookup, head->lookup.raw_size);
1012
1013 /* find and extact winpeshl.exe */
1014 patch->replace_look = ventoy_find_look_entry(head, lookup, &patch->old_hash);
1015 if (patch->replace_look)
1016 {
1017 exe_len = (grub_uint32_t)patch->replace_look->resource.raw_size;
1018 debug("find replace lookup entry_id:%ld raw_size:%u\n",
1019 ((long)patch->replace_look - (long)lookup) / sizeof(wim_lookup_entry), exe_len);
1020
1021 if (0 == ventoy_read_resource(file, head, &(patch->replace_look->resource), (void **)&(exe_data)))
1022 {
1023 ventoy_cat_exe_file_data(wim_data, exe_len, exe_data);
1024 grub_free(exe_data);
1025 }
1026 else
1027 {
1028 debug("failed to read replace file meta data %u\n", exe_len);
1029 }
1030 }
1031 else
1032 {
1033 debug("failed to find lookup entry for replace file 0x%02x 0x%02x\n",
1034 patch->old_hash.sha1[0], patch->old_hash.sha1[1]);
1035 }
1036
1037 wim_data->wim_raw_size = (grub_uint32_t)file->size;
1038 wim_data->wim_align_size = ventoy_align(wim_data->wim_raw_size, 2048);
1039
1040 grub_check_free(wim_data->new_meta_data);
1041 wim_data->new_meta_data = decompress_data;
1042 wim_data->new_meta_len = head->metadata.raw_size;
1043 wim_data->new_meta_align_len = ventoy_align(wim_data->new_meta_len, 2048);
1044
1045 grub_check_free(wim_data->new_lookup_data);
1046 wim_data->new_lookup_data = (grub_uint8_t *)lookup;
1047 wim_data->new_lookup_len = (grub_uint32_t)head->lookup.raw_size;
1048 wim_data->new_lookup_align_len = ventoy_align(wim_data->new_lookup_len, 2048);
1049
1050 head->metadata.flags = RESHDR_FLAG_METADATA;
1051 head->metadata.offset = wim_data->wim_align_size + wim_data->bin_align_len;
1052 head->metadata.size_in_wim = wim_data->new_meta_len;
1053 head->metadata.raw_size = wim_data->new_meta_len;
1054
1055 head->lookup.flags = 0;
1056 head->lookup.offset = head->metadata.offset + wim_data->new_meta_align_len;
1057 head->lookup.size_in_wim = wim_data->new_lookup_len;
1058 head->lookup.raw_size = wim_data->new_lookup_len;
1059
1060 grub_file_close(file);
1061
1062 debug("%s", "windows locate wim finish\n");
1063 return 0;
1064 }
1065
1066 grub_err_t ventoy_cmd_locate_wim_patch(grub_extcmd_context_t ctxt, int argc, char **args)
1067 {
1068 wim_patch *node = g_wim_patch_head;
1069
1070 (void)ctxt;
1071 (void)argc;
1072 (void)args;
1073
1074 while (node)
1075 {
1076 if (0 == ventoy_wimdows_locate_wim(args[0], node))
1077 {
1078 node->valid = 1;
1079 g_wim_valid_patch_count++;
1080 }
1081
1082 node = node->next;
1083 }
1084
1085 return 0;
1086 }
1087
1088 static grub_uint32_t ventoy_get_override_chunk_num(void)
1089 {
1090 grub_uint32_t chunk_num = 0;
1091
1092 if (g_iso_fs_type == 0)
1093 {
1094 /* ISO9660: */
1095 /* per wim */
1096 /* 1: file_size and file_offset */
1097 /* 2: new wim file header */
1098 chunk_num = g_wim_valid_patch_count * 2;
1099 }
1100 else
1101 {
1102 /* UDF: */
1103 /* global: */
1104 /* 1: block count in Partition Descriptor */
1105
1106 /* per wim */
1107 /* 1: file_size in file_entry or extend_file_entry */
1108 /* 2: data_size and position in extend data short ad */
1109 /* 3: new wim file header */
1110 chunk_num = g_wim_valid_patch_count * 3 + 1;
1111 }
1112
1113 if (g_suppress_wincd_override_offset > 0)
1114 {
1115 chunk_num++;
1116 }
1117
1118 return chunk_num;
1119 }
1120
1121 static void ventoy_fill_suppress_wincd_override_data(void *override)
1122 {
1123 ventoy_override_chunk *cur = (ventoy_override_chunk *)override;
1124
1125 cur->override_size = 4;
1126 cur->img_offset = g_suppress_wincd_override_offset;
1127 grub_memcpy(cur->override_data, &g_suppress_wincd_override_data, cur->override_size);
1128 }
1129
1130 static void ventoy_windows_fill_override_data_iso9660( grub_uint64_t isosize, void *override)
1131 {
1132 grub_uint64_t sector;
1133 grub_uint32_t new_wim_size;
1134 ventoy_override_chunk *cur;
1135 wim_patch *node = NULL;
1136 wim_tail *wim_data = NULL;
1137 ventoy_iso9660_override *dirent = NULL;
1138
1139 sector = (isosize + 2047) / 2048;
1140
1141 cur = (ventoy_override_chunk *)override;
1142
1143 if (g_suppress_wincd_override_offset > 0)
1144 {
1145 ventoy_fill_suppress_wincd_override_data(cur);
1146 cur++;
1147 }
1148
1149 debug("ventoy_windows_fill_override_data_iso9660 %lu\n", (ulong)isosize);
1150
1151 for (node = g_wim_patch_head; node; node = node->next)
1152 {
1153 wim_data = &node->wim_data;
1154 if (0 == node->valid)
1155 {
1156 continue;
1157 }
1158
1159 new_wim_size = wim_data->wim_align_size + wim_data->bin_align_len +
1160 wim_data->new_meta_align_len + wim_data->new_lookup_align_len;
1161
1162 dirent = (ventoy_iso9660_override *)wim_data->override_data;
1163
1164 dirent->first_sector = (grub_uint32_t)sector;
1165 dirent->size = new_wim_size;
1166 dirent->first_sector_be = grub_swap_bytes32(dirent->first_sector);
1167 dirent->size_be = grub_swap_bytes32(dirent->size);
1168
1169 sector += (new_wim_size / 2048);
1170
1171 /* override 1: position and length in dirent */
1172 cur->img_offset = wim_data->override_offset;
1173 cur->override_size = wim_data->override_len;
1174 grub_memcpy(cur->override_data, wim_data->override_data, cur->override_size);
1175 cur++;
1176
1177 /* override 2: new wim file header */
1178 cur->img_offset = wim_data->file_offset;
1179 cur->override_size = sizeof(wim_header);
1180 grub_memcpy(cur->override_data, &(wim_data->wim_header), cur->override_size);
1181 cur++;
1182 }
1183
1184 return;
1185 }
1186
1187 static int ventoy_windows_fill_udf_short_ad(grub_file_t isofile, grub_uint32_t curpos,
1188 wim_tail *wim_data, grub_uint32_t new_wim_size)
1189 {
1190 int i;
1191 grub_uint32_t total = 0;
1192 grub_uint32_t left_size = 0;
1193 ventoy_udf_override *udf = NULL;
1194 ventoy_udf_override tmp[4];
1195
1196 grub_memset(tmp, 0, sizeof(tmp));
1197 grub_file_seek(isofile, wim_data->override_offset);
1198 grub_file_read(isofile, tmp, sizeof(tmp));
1199
1200 left_size = new_wim_size;
1201 udf = (ventoy_udf_override *)wim_data->override_data;
1202
1203 for (i = 0; i < 4; i++)
1204 {
1205 total += tmp[i].length;
1206 if (total >= wim_data->wim_raw_size)
1207 {
1208 udf->length = left_size;
1209 udf->position = curpos;
1210 return 0;
1211 }
1212 else
1213 {
1214 udf->length = tmp[i].length;
1215 udf->position = curpos;
1216 }
1217
1218 left_size -= tmp[i].length;
1219 curpos += udf->length / 2048;
1220 udf++;
1221 wim_data->override_len += sizeof(ventoy_udf_override);
1222 }
1223
1224 debug("######## Too many udf ad ######\n");
1225 return 1;
1226 }
1227
1228 static void ventoy_windows_fill_override_data_udf(grub_file_t isofile, void *override)
1229 {
1230 grub_uint32_t data32;
1231 grub_uint64_t data64;
1232 grub_uint64_t sector;
1233 grub_uint32_t new_wim_size;
1234 grub_uint64_t total_wim_size = 0;
1235 grub_uint32_t udf_start_block = 0;
1236 ventoy_override_chunk *cur;
1237 wim_patch *node = NULL;
1238 wim_tail *wim_data = NULL;
1239
1240 sector = (isofile->size + 2047) / 2048;
1241
1242 cur = (ventoy_override_chunk *)override;
1243
1244 if (g_suppress_wincd_override_offset > 0)
1245 {
1246 ventoy_fill_suppress_wincd_override_data(cur);
1247 cur++;
1248 }
1249
1250 debug("ventoy_windows_fill_override_data_udf %lu\n", (ulong)isofile->size);
1251
1252 for (node = g_wim_patch_head; node; node = node->next)
1253 {
1254 wim_data = &node->wim_data;
1255 if (node->valid)
1256 {
1257 if (udf_start_block == 0)
1258 {
1259 udf_start_block = wim_data->udf_start_block;
1260 }
1261 new_wim_size = wim_data->wim_align_size + wim_data->bin_align_len +
1262 wim_data->new_meta_align_len + wim_data->new_lookup_align_len;
1263 total_wim_size += new_wim_size;
1264 }
1265 }
1266
1267 //override 1: sector number in pd data
1268 cur->img_offset = grub_udf_get_last_pd_size_offset();
1269 cur->override_size = 4;
1270 data32 = sector - udf_start_block + (total_wim_size / 2048);
1271 grub_memcpy(cur->override_data, &(data32), 4);
1272
1273 for (node = g_wim_patch_head; node; node = node->next)
1274 {
1275 wim_data = &node->wim_data;
1276 if (0 == node->valid)
1277 {
1278 continue;
1279 }
1280
1281 new_wim_size = wim_data->wim_align_size + wim_data->bin_align_len +
1282 wim_data->new_meta_align_len + wim_data->new_lookup_align_len;
1283
1284 //override 2: filesize in file_entry
1285 cur++;
1286 cur->img_offset = wim_data->fe_entry_size_offset;
1287 cur->override_size = 8;
1288 data64 = new_wim_size;
1289 grub_memcpy(cur->override_data, &(data64), 8);
1290
1291 /* override 3: position and length in extend data */
1292 ventoy_windows_fill_udf_short_ad(isofile, (grub_uint32_t)sector - udf_start_block, wim_data, new_wim_size);
1293
1294 sector += (new_wim_size / 2048);
1295
1296 cur++;
1297 cur->img_offset = wim_data->override_offset;
1298 cur->override_size = wim_data->override_len;
1299 grub_memcpy(cur->override_data, wim_data->override_data, cur->override_size);
1300
1301 /* override 4: new wim file header */
1302 cur++;
1303 cur->img_offset = wim_data->file_offset;
1304 cur->override_size = sizeof(wim_header);
1305 grub_memcpy(cur->override_data, &(wim_data->wim_header), cur->override_size);
1306 }
1307
1308 return;
1309 }
1310
1311 static grub_uint32_t ventoy_windows_get_virt_data_size(void)
1312 {
1313 grub_uint32_t size = 0;
1314 wim_tail *wim_data = NULL;
1315 wim_patch *node = g_wim_patch_head;
1316
1317 while (node)
1318 {
1319 if (node->valid)
1320 {
1321 wim_data = &node->wim_data;
1322 size += sizeof(ventoy_virt_chunk) + wim_data->bin_align_len +
1323 wim_data->new_meta_align_len + wim_data->new_lookup_align_len;
1324 }
1325 node = node->next;
1326 }
1327
1328 return size;
1329 }
1330
1331 static void ventoy_windows_fill_virt_data( grub_uint64_t isosize, ventoy_chain_head *chain)
1332 {
1333 grub_uint64_t sector;
1334 grub_uint32_t offset;
1335 grub_uint32_t wim_secs;
1336 grub_uint32_t mem_secs;
1337 char *override = NULL;
1338 ventoy_virt_chunk *cur = NULL;
1339 wim_tail *wim_data = NULL;
1340 wim_patch *node = NULL;
1341
1342 sector = (isosize + 2047) / 2048;
1343 offset = sizeof(ventoy_virt_chunk) * g_wim_valid_patch_count;
1344
1345 override = (char *)chain + chain->virt_chunk_offset;
1346 cur = (ventoy_virt_chunk *)override;
1347
1348 for (node = g_wim_patch_head; node; node = node->next)
1349 {
1350 if (0 == node->valid)
1351 {
1352 continue;
1353 }
1354
1355 wim_data = &node->wim_data;
1356
1357 wim_secs = wim_data->wim_align_size / 2048;
1358 mem_secs = (wim_data->bin_align_len + wim_data->new_meta_align_len + wim_data->new_lookup_align_len) / 2048;
1359
1360 cur->remap_sector_start = sector;
1361 cur->remap_sector_end = cur->remap_sector_start + wim_secs;
1362 cur->org_sector_start = (grub_uint32_t)(wim_data->file_offset / 2048);
1363
1364 cur->mem_sector_start = cur->remap_sector_end;
1365 cur->mem_sector_end = cur->mem_sector_start + mem_secs;
1366 cur->mem_sector_offset = offset;
1367
1368 sector += wim_secs + mem_secs;
1369 cur++;
1370
1371 grub_memcpy(override + offset, wim_data->jump_bin_data, wim_data->bin_raw_len);
1372 offset += wim_data->bin_align_len;
1373
1374 grub_memcpy(override + offset, wim_data->new_meta_data, wim_data->new_meta_len);
1375 offset += wim_data->new_meta_align_len;
1376
1377 grub_memcpy(override + offset, wim_data->new_lookup_data, wim_data->new_lookup_len);
1378 offset += wim_data->new_lookup_align_len;
1379
1380 chain->virt_img_size_in_bytes += wim_data->wim_align_size +
1381 wim_data->bin_align_len +
1382 wim_data->new_meta_align_len +
1383 wim_data->new_lookup_align_len;
1384 }
1385
1386 return;
1387 }
1388
1389 static int ventoy_windows_drive_map(ventoy_chain_head *chain)
1390 {
1391 grub_disk_t disk;
1392
1393 debug("drive map begin <%p> ...\n", chain);
1394
1395 if (chain->disk_drive == 0x80)
1396 {
1397 disk = grub_disk_open("hd1");
1398 if (disk)
1399 {
1400 grub_disk_close(disk);
1401 debug("drive map needed %p\n", disk);
1402 chain->drive_map = 0x81;
1403 }
1404 else
1405 {
1406 debug("failed to open disk %s\n", "hd1");
1407 }
1408 }
1409 else
1410 {
1411 debug("no need to map 0x%x\n", chain->disk_drive);
1412 }
1413
1414 return 0;
1415 }
1416
1417 static int ventoy_suppress_windows_cd_prompt(void)
1418 {
1419 int rc = 1;
1420 const char *cdprompt = NULL;
1421 grub_uint64_t readpos = 0;
1422 grub_file_t file = NULL;
1423 grub_uint8_t data[32];
1424
1425 cdprompt = ventoy_get_env("VTOY_WINDOWS_CD_PROMPT");
1426 if (cdprompt && cdprompt[0] == '1' && cdprompt[1] == 0)
1427 {
1428 debug("VTOY_WINDOWS_CD_PROMPT:<%s>\n", cdprompt);
1429 return 0;
1430 }
1431
1432 g_ventoy_case_insensitive = 1;
1433 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s/boot/bootfix.bin", "(loop)");
1434 g_ventoy_case_insensitive = 0;
1435
1436 if (!file)
1437 {
1438 debug("Failed to open %s\n", "bootfix.bin");
1439 goto end;
1440 }
1441
1442 grub_file_read(file, data, 32);
1443
1444 if (file->fs && file->fs->name && grub_strcmp(file->fs->name, "udf") == 0)
1445 {
1446 readpos = grub_udf_get_file_offset(file);
1447 }
1448 else
1449 {
1450 readpos = grub_iso9660_get_last_read_pos(file);
1451 }
1452
1453 debug("bootfix.bin readpos:%lu (sector:%lu) data: %02x %02x %02x %02x\n",
1454 (ulong)readpos, (ulong)readpos / 2048, data[24], data[25], data[26], data[27]);
1455
1456 if (*(grub_uint32_t *)(data + 24) == 0x13cd0080)
1457 {
1458 g_suppress_wincd_override_offset = readpos + 24;
1459 g_suppress_wincd_override_data = 0x13cd00fd;
1460
1461 rc = 0;
1462 }
1463
1464 debug("g_suppress_wincd_override_offset:%lu\n", (ulong)g_suppress_wincd_override_offset);
1465
1466 end:
1467 check_free(file, grub_file_close);
1468
1469 return rc;
1470 }
1471
1472 grub_err_t ventoy_cmd_windows_wimboot_data(grub_extcmd_context_t ctxt, int argc, char **args)
1473 {
1474 grub_uint32_t size = 0;
1475 const char *addr = NULL;
1476 ventoy_chain_head *chain = NULL;
1477 ventoy_os_param *param = NULL;
1478 char envbuf[64];
1479
1480 (void)ctxt;
1481 (void)argc;
1482 (void)args;
1483
1484 addr = grub_env_get("vtoy_chain_mem_addr");
1485 if (!addr)
1486 {
1487 debug("Failed to find vtoy_chain_mem_addr\n");
1488 return 1;
1489 }
1490
1491 chain = (ventoy_chain_head *)(void *)grub_strtoul(addr, NULL, 16);
1492
1493 if (grub_memcmp(&g_ventoy_guid, &chain->os_param.guid, 16) != 0)
1494 {
1495 debug("os_param.guid not match\n");
1496 return 1;
1497 }
1498
1499 size = sizeof(ventoy_os_param) + sizeof(ventoy_windows_data);
1500 param = (ventoy_os_param *)grub_zalloc(size);
1501 if (!param)
1502 {
1503 return 1;
1504 }
1505
1506 grub_memcpy(param, &chain->os_param, sizeof(ventoy_os_param));
1507 ventoy_fill_windows_rtdata(param + 1, param->vtoy_img_path);
1508
1509 grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)param);
1510 grub_env_set("vtoy_wimboot_mem_addr", envbuf);
1511 debug("vtoy_wimboot_mem_addr: %s\n", envbuf);
1512
1513 grub_snprintf(envbuf, sizeof(envbuf), "%u", size);
1514 grub_env_set("vtoy_wimboot_mem_size", envbuf);
1515 debug("vtoy_wimboot_mem_size: %s\n", envbuf);
1516
1517 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
1518 }
1519
1520 grub_err_t ventoy_cmd_windows_chain_data(grub_extcmd_context_t ctxt, int argc, char **args)
1521 {
1522 int unknown_image = 0;
1523 int ventoy_compatible = 0;
1524 grub_uint32_t size = 0;
1525 grub_uint64_t isosize = 0;
1526 grub_uint32_t boot_catlog = 0;
1527 grub_uint32_t img_chunk_size = 0;
1528 grub_uint32_t override_size = 0;
1529 grub_uint32_t virt_chunk_size = 0;
1530 grub_file_t file;
1531 grub_disk_t disk;
1532 const char *pLastChain = NULL;
1533 const char *compatible;
1534 ventoy_chain_head *chain;
1535 char envbuf[64];
1536
1537 (void)ctxt;
1538 (void)argc;
1539
1540 debug("chain data begin <%s> ...\n", args[0]);
1541
1542 compatible = grub_env_get("ventoy_compatible");
1543 if (compatible && compatible[0] == 'Y')
1544 {
1545 ventoy_compatible = 1;
1546 }
1547
1548 if (NULL == g_img_chunk_list.chunk)
1549 {
1550 grub_printf("ventoy not ready\n");
1551 return 1;
1552 }
1553
1554 if (0 == ventoy_compatible && g_wim_valid_patch_count == 0)
1555 {
1556 unknown_image = 1;
1557 debug("Warning: %s was not recognized by Ventoy\n", args[0]);
1558 }
1559
1560 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", args[0]);
1561 if (!file)
1562 {
1563 return 1;
1564 }
1565
1566 isosize = file->size;
1567
1568 boot_catlog = ventoy_get_iso_boot_catlog(file);
1569 if (boot_catlog)
1570 {
1571 if (ventoy_is_efi_os() && (!ventoy_has_efi_eltorito(file, boot_catlog)))
1572 {
1573 grub_env_set("LoadIsoEfiDriver", "on");
1574 }
1575 }
1576 else
1577 {
1578 if (ventoy_is_efi_os())
1579 {
1580 grub_env_set("LoadIsoEfiDriver", "on");
1581 }
1582 else
1583 {
1584 return grub_error(GRUB_ERR_BAD_ARGUMENT, "File %s is not bootable", args[0]);
1585 }
1586 }
1587
1588 g_suppress_wincd_override_offset = 0;
1589 if (!ventoy_is_efi_os()) /* legacy mode */
1590 {
1591 ventoy_suppress_windows_cd_prompt();
1592 }
1593
1594 img_chunk_size = g_img_chunk_list.cur_chunk * sizeof(ventoy_img_chunk);
1595
1596 if (ventoy_compatible || unknown_image)
1597 {
1598 override_size = g_suppress_wincd_override_offset > 0 ? sizeof(ventoy_override_chunk) : 0;
1599 size = sizeof(ventoy_chain_head) + img_chunk_size + override_size;
1600 }
1601 else
1602 {
1603 override_size = ventoy_get_override_chunk_num() * sizeof(ventoy_override_chunk);
1604 virt_chunk_size = ventoy_windows_get_virt_data_size();
1605 size = sizeof(ventoy_chain_head) + img_chunk_size + override_size + virt_chunk_size;
1606 }
1607
1608 pLastChain = grub_env_get("vtoy_chain_mem_addr");
1609 if (pLastChain)
1610 {
1611 chain = (ventoy_chain_head *)grub_strtoul(pLastChain, NULL, 16);
1612 if (chain)
1613 {
1614 debug("free last chain memory %p\n", chain);
1615 grub_free(chain);
1616 }
1617 }
1618
1619 chain = grub_malloc(size);
1620 if (!chain)
1621 {
1622 grub_printf("Failed to alloc chain memory size %u\n", size);
1623 grub_file_close(file);
1624 return 1;
1625 }
1626
1627 grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain);
1628 grub_env_set("vtoy_chain_mem_addr", envbuf);
1629 grub_snprintf(envbuf, sizeof(envbuf), "%u", size);
1630 grub_env_set("vtoy_chain_mem_size", envbuf);
1631
1632 grub_memset(chain, 0, sizeof(ventoy_chain_head));
1633
1634 /* part 1: os parameter */
1635 g_ventoy_chain_type = ventoy_chain_windows;
1636 ventoy_fill_os_param(file, &(chain->os_param));
1637
1638 if (0 == unknown_image)
1639 {
1640 ventoy_update_before_chain(&(chain->os_param), args[0]);
1641 }
1642
1643 /* part 2: chain head */
1644 disk = file->device->disk;
1645 chain->disk_drive = disk->id;
1646 chain->disk_sector_size = (1 << disk->log_sector_size);
1647 chain->real_img_size_in_bytes = file->size;
1648 chain->virt_img_size_in_bytes = (file->size + 2047) / 2048 * 2048;
1649 chain->boot_catalog = boot_catlog;
1650
1651 if (!ventoy_is_efi_os())
1652 {
1653 grub_file_seek(file, boot_catlog * 2048);
1654 grub_file_read(file, chain->boot_catalog_sector, sizeof(chain->boot_catalog_sector));
1655 }
1656
1657 /* part 3: image chunk */
1658 chain->img_chunk_offset = sizeof(ventoy_chain_head);
1659 chain->img_chunk_num = g_img_chunk_list.cur_chunk;
1660 grub_memcpy((char *)chain + chain->img_chunk_offset, g_img_chunk_list.chunk, img_chunk_size);
1661
1662 if (ventoy_compatible || unknown_image)
1663 {
1664 if (g_suppress_wincd_override_offset > 0)
1665 {
1666 chain->override_chunk_offset = chain->img_chunk_offset + img_chunk_size;
1667 chain->override_chunk_num = 1;
1668 ventoy_fill_suppress_wincd_override_data((char *)chain + chain->override_chunk_offset);
1669 }
1670
1671 return 0;
1672 }
1673
1674 if (0 == g_wim_valid_patch_count)
1675 {
1676 return 0;
1677 }
1678
1679 /* part 4: override chunk */
1680 chain->override_chunk_offset = chain->img_chunk_offset + img_chunk_size;
1681 chain->override_chunk_num = ventoy_get_override_chunk_num();
1682
1683 if (g_iso_fs_type == 0)
1684 {
1685 ventoy_windows_fill_override_data_iso9660(isosize, (char *)chain + chain->override_chunk_offset);
1686 }
1687 else
1688 {
1689 ventoy_windows_fill_override_data_udf(file, (char *)chain + chain->override_chunk_offset);
1690 }
1691
1692 /* part 5: virt chunk */
1693 chain->virt_chunk_offset = chain->override_chunk_offset + override_size;
1694 chain->virt_chunk_num = g_wim_valid_patch_count;
1695 ventoy_windows_fill_virt_data(isosize, chain);
1696
1697 if (ventoy_is_efi_os() == 0)
1698 {
1699 ventoy_windows_drive_map(chain);
1700 }
1701
1702 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
1703 }
1704
1705 static grub_uint32_t ventoy_get_wim_iso_offset(const char *filepath)
1706 {
1707 grub_uint32_t imgoffset;
1708 grub_file_t file;
1709 char cmdbuf[128];
1710
1711 grub_snprintf(cmdbuf, sizeof(cmdbuf), "loopback wimiso \"%s\"", filepath);
1712 grub_script_execute_sourcecode(cmdbuf);
1713
1714 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", "(wimiso)/boot/boot.wim");
1715 if (!file)
1716 {
1717 grub_printf("Failed to open boot.wim file in the image file\n");
1718 return 0;
1719 }
1720
1721 imgoffset = (grub_uint32_t)grub_iso9660_get_last_file_dirent_pos(file) + 2;
1722
1723 debug("wimiso wim direct offset: %u\n", imgoffset);
1724
1725 grub_file_close(file);
1726
1727 grub_script_execute_sourcecode("loopback -d wimiso");
1728
1729 return imgoffset;
1730 }
1731
1732 static int ventoy_get_wim_chunklist(const char *filename, ventoy_img_chunk_list *wimchunk, grub_uint64_t *wimsize)
1733 {
1734 grub_file_t wimfile;
1735
1736 wimfile = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", filename);
1737 if (!wimfile)
1738 {
1739 return 1;
1740 }
1741
1742 grub_memset(wimchunk, 0, sizeof(ventoy_img_chunk_list));
1743 wimchunk->chunk = grub_malloc(sizeof(ventoy_img_chunk) * DEFAULT_CHUNK_NUM);
1744 if (NULL == wimchunk->chunk)
1745 {
1746 grub_file_close(wimfile);
1747 return grub_error(GRUB_ERR_OUT_OF_MEMORY, "Can't allocate image chunk memoty\n");
1748 }
1749
1750 wimchunk->max_chunk = DEFAULT_CHUNK_NUM;
1751 wimchunk->cur_chunk = 0;
1752
1753 ventoy_get_block_list(wimfile, wimchunk, wimfile->device->disk->partition->start);
1754
1755 *wimsize = wimfile->size;
1756 grub_file_close(wimfile);
1757
1758 return 0;
1759 }
1760
1761 grub_err_t ventoy_cmd_wim_check_bootable(grub_extcmd_context_t ctxt, int argc, char **args)
1762 {
1763 grub_uint32_t boot_index;
1764 grub_file_t file = NULL;
1765 wim_header *wimhdr = NULL;
1766
1767 (void)ctxt;
1768 (void)argc;
1769
1770 wimhdr = grub_zalloc(sizeof(wim_header));
1771 if (!wimhdr)
1772 {
1773 return 1;
1774 }
1775
1776 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", args[0]);
1777 if (!file)
1778 {
1779 grub_free(wimhdr);
1780 return 1;
1781 }
1782
1783 grub_file_read(file, wimhdr, sizeof(wim_header));
1784 grub_file_close(file);
1785 boot_index = wimhdr->boot_index;
1786 grub_free(wimhdr);
1787
1788 if (boot_index == 0)
1789 {
1790 return 1;
1791 }
1792
1793 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
1794 }
1795
1796 grub_err_t ventoy_cmd_wim_chain_data(grub_extcmd_context_t ctxt, int argc, char **args)
1797 {
1798 grub_uint32_t i = 0;
1799 grub_uint32_t imgoffset = 0;
1800 grub_uint32_t size = 0;
1801 grub_uint32_t isosector = 0;
1802 grub_uint64_t wimsize = 0;
1803 grub_uint32_t boot_catlog = 0;
1804 grub_uint32_t img_chunk1_size = 0;
1805 grub_uint32_t img_chunk2_size = 0;
1806 grub_uint32_t override_size = 0;
1807 grub_file_t file;
1808 grub_disk_t disk;
1809 const char *pLastChain = NULL;
1810 ventoy_chain_head *chain;
1811 ventoy_iso9660_override *dirent;
1812 ventoy_img_chunk *chunknode;
1813 ventoy_override_chunk *override;
1814 ventoy_img_chunk_list wimchunk;
1815 char envbuf[128];
1816
1817 (void)ctxt;
1818 (void)argc;
1819
1820 debug("wim chain data begin <%s> ...\n", args[0]);
1821
1822 if (NULL == g_wimiso_chunk_list.chunk || NULL == g_wimiso_path)
1823 {
1824 grub_printf("ventoy not ready\n");
1825 return 1;
1826 }
1827
1828 imgoffset = ventoy_get_wim_iso_offset(g_wimiso_path);
1829 if (imgoffset == 0)
1830 {
1831 grub_printf("image offset not found\n");
1832 return 1;
1833 }
1834
1835 if (0 != ventoy_get_wim_chunklist(args[0], &wimchunk, &wimsize))
1836 {
1837 grub_printf("Failed to get wim chunklist\n");
1838 return 1;
1839 }
1840
1841 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", g_wimiso_path);
1842 if (!file)
1843 {
1844 return 1;
1845 }
1846
1847 boot_catlog = ventoy_get_iso_boot_catlog(file);
1848
1849 img_chunk1_size = g_wimiso_chunk_list.cur_chunk * sizeof(ventoy_img_chunk);
1850 img_chunk2_size = wimchunk.cur_chunk * sizeof(ventoy_img_chunk);
1851 override_size = sizeof(ventoy_override_chunk);
1852
1853 size = sizeof(ventoy_chain_head) + img_chunk1_size + img_chunk2_size + override_size;
1854
1855 pLastChain = grub_env_get("vtoy_chain_mem_addr");
1856 if (pLastChain)
1857 {
1858 chain = (ventoy_chain_head *)grub_strtoul(pLastChain, NULL, 16);
1859 if (chain)
1860 {
1861 debug("free last chain memory %p\n", chain);
1862 grub_free(chain);
1863 }
1864 }
1865
1866 chain = grub_malloc(size);
1867 if (!chain)
1868 {
1869 grub_printf("Failed to alloc chain memory size %u\n", size);
1870 grub_file_close(file);
1871 return 1;
1872 }
1873
1874 grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain);
1875 grub_env_set("vtoy_chain_mem_addr", envbuf);
1876 grub_snprintf(envbuf, sizeof(envbuf), "%u", size);
1877 grub_env_set("vtoy_chain_mem_size", envbuf);
1878
1879 grub_memset(chain, 0, sizeof(ventoy_chain_head));
1880
1881 /* part 1: os parameter */
1882 g_ventoy_chain_type = ventoy_chain_wim;
1883 ventoy_fill_os_param(file, &(chain->os_param));
1884
1885 /* part 2: chain head */
1886 disk = file->device->disk;
1887 chain->disk_drive = disk->id;
1888 chain->disk_sector_size = (1 << disk->log_sector_size);
1889 chain->real_img_size_in_bytes = ventoy_align_2k(file->size) + ventoy_align_2k(wimsize);
1890 chain->virt_img_size_in_bytes = chain->real_img_size_in_bytes;
1891 chain->boot_catalog = boot_catlog;
1892
1893 if (!ventoy_is_efi_os())
1894 {
1895 grub_file_seek(file, boot_catlog * 2048);
1896 grub_file_read(file, chain->boot_catalog_sector, sizeof(chain->boot_catalog_sector));
1897 }
1898
1899 /* part 3: image chunk */
1900 chain->img_chunk_offset = sizeof(ventoy_chain_head);
1901 chain->img_chunk_num = g_wimiso_chunk_list.cur_chunk + wimchunk.cur_chunk;
1902 grub_memcpy((char *)chain + chain->img_chunk_offset, g_wimiso_chunk_list.chunk, img_chunk1_size);
1903
1904 /* fs cluster size >= 2048, so don't need to proc align */
1905
1906 /* align by 2048 */
1907 chunknode = wimchunk.chunk + wimchunk.cur_chunk - 1;
1908 i = (chunknode->disk_end_sector + 1 - chunknode->disk_start_sector) % 4;
1909 if (i)
1910 {
1911 chunknode->disk_end_sector += 4 - i;
1912 }
1913
1914 isosector = (grub_uint32_t)((file->size + 2047) / 2048);
1915 for (i = 0; i < wimchunk.cur_chunk; i++)
1916 {
1917 chunknode = wimchunk.chunk + i;
1918 chunknode->img_start_sector = isosector;
1919 chunknode->img_end_sector = chunknode->img_start_sector +
1920 ((chunknode->disk_end_sector + 1 - chunknode->disk_start_sector) / 4) - 1;
1921 isosector = chunknode->img_end_sector + 1;
1922 }
1923
1924 grub_memcpy((char *)chain + chain->img_chunk_offset + img_chunk1_size, wimchunk.chunk, img_chunk2_size);
1925
1926 /* part 4: override chunk */
1927 chain->override_chunk_offset = chain->img_chunk_offset + img_chunk1_size + img_chunk2_size;
1928 chain->override_chunk_num = 1;
1929
1930 override = (ventoy_override_chunk *)((char *)chain + chain->override_chunk_offset);
1931 override->img_offset = imgoffset;
1932 override->override_size = sizeof(ventoy_iso9660_override);
1933
1934 dirent = (ventoy_iso9660_override *)(override->override_data);
1935 dirent->first_sector = (grub_uint32_t)((file->size + 2047) / 2048);
1936 dirent->size = (grub_uint32_t)(wimsize);
1937 dirent->first_sector_be = grub_swap_bytes32(dirent->first_sector);
1938 dirent->size_be = grub_swap_bytes32(dirent->size);
1939
1940 debug("imgoffset=%u first_sector=0x%x size=0x%x\n", imgoffset, dirent->first_sector, dirent->size);
1941
1942 if (ventoy_is_efi_os() == 0)
1943 {
1944 ventoy_windows_drive_map(chain);
1945 }
1946
1947 grub_file_close(file);
1948
1949 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
1950 }
1951
1952 int ventoy_chain_file_size(const char *path)
1953 {
1954 int size;
1955 grub_file_t file;
1956
1957 file = grub_file_open(path, VENTOY_FILE_TYPE);
1958 size = (int)(file->size);
1959
1960 grub_file_close(file);
1961
1962 return size;
1963 }
1964
1965 int ventoy_chain_file_read(const char *path, int offset, int len, void *buf)
1966 {
1967 int size;
1968 grub_file_t file;
1969
1970 file = grub_file_open(path, VENTOY_FILE_TYPE);
1971 grub_file_seek(file, offset);
1972 size = grub_file_read(file, buf, len);
1973 grub_file_close(file);
1974
1975 return size;
1976 }
1977