]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_windows.c
a35680aafaf740424610e39945b8e058ade515f2
[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
662
663 static wim_lookup_entry * ventoy_find_look_entry(wim_header *header, wim_lookup_entry *lookup, wim_hash *hash)
664 {
665 grub_uint32_t i = 0;
666
667 for (i = 0; i < (grub_uint32_t)header->lookup.raw_size / sizeof(wim_lookup_entry); i++)
668 {
669 if (grub_memcmp(&lookup[i].hash, hash, sizeof(wim_hash)) == 0)
670 {
671 return lookup + i;
672 }
673 }
674
675 return NULL;
676 }
677
678 static int parse_registry_setup_cmdline
679 (
680 grub_file_t file,
681 wim_header *head,
682 wim_lookup_entry *lookup,
683 void *meta_data,
684 wim_directory_entry *dir,
685 char *buf,
686 grub_uint32_t buflen
687 )
688 {
689 char c;
690 int ret = 0;
691 grub_uint32_t i = 0;
692 grub_uint32_t reglen = 0;
693 wim_hash zerohash;
694 reg_vk *regvk = NULL;
695 wim_lookup_entry *look = NULL;
696 wim_directory_entry *wim_dirent = NULL;
697 char *decompress_data = NULL;
698 const char *reg_path[] = { "Windows", "System32", "config", "SYSTEM", NULL };
699
700 wim_dirent = search_full_wim_dirent(meta_data, dir, reg_path);
701 debug("search reg SYSTEM %p\n", wim_dirent);
702 if (!wim_dirent)
703 {
704 return 1;
705 }
706
707 grub_memset(&zerohash, 0, sizeof(zerohash));
708 if (grub_memcmp(&zerohash, wim_dirent->hash.sha1, sizeof(wim_hash)) == 0)
709 {
710 return 2;
711 }
712
713 look = ventoy_find_look_entry(head, lookup, &wim_dirent->hash);
714 if (!look)
715 {
716 return 3;
717 }
718
719 reglen = (grub_uint32_t)look->resource.raw_size;
720 debug("find system lookup entry_id:%ld raw_size:%u\n",
721 ((long)look - (long)lookup) / sizeof(wim_lookup_entry), reglen);
722
723 if (0 != ventoy_read_resource(file, head, &(look->resource), (void **)&(decompress_data)))
724 {
725 return 4;
726 }
727
728 if (grub_strncmp(decompress_data + 0x1000, "hbin", 4))
729 {
730 ret_goto_end(5);
731 }
732
733 for (i = 0x1000; i + sizeof(reg_vk) < reglen; i += 8)
734 {
735 regvk = (reg_vk *)(decompress_data + i);
736 if (regvk->sig == 0x6B76 && regvk->namesize == 7 &&
737 regvk->datatype == 1 && regvk->flag == 1)
738 {
739 if (grub_strncasecmp((char *)(regvk + 1), "cmdline", 7) == 0)
740 {
741 debug("find registry cmdline i:%u offset:(0x%x)%u size:(0x%x)%u\n",
742 i, regvk->dataoffset, regvk->dataoffset, regvk->datasize, regvk->datasize);
743 break;
744 }
745 }
746 }
747
748 if (i + sizeof(reg_vk) >= reglen || regvk == NULL)
749 {
750 ret_goto_end(6);
751 }
752
753 if (regvk->datasize == 0 || (regvk->datasize & 0x80000000) > 0 ||
754 regvk->dataoffset == 0 || regvk->dataoffset == 0xFFFFFFFF)
755 {
756 ret_goto_end(7);
757 }
758
759 if (regvk->datasize / 2 >= buflen)
760 {
761 ret_goto_end(8);
762 }
763
764 debug("start offset is 0x%x(%u)\n", 0x1000 + regvk->dataoffset + 4, 0x1000 + regvk->dataoffset + 4);
765
766 for (i = 0; i < regvk->datasize; i+=2)
767 {
768 c = (char)(*(grub_uint16_t *)(decompress_data + 0x1000 + regvk->dataoffset + 4 + i));
769 *buf++ = c;
770 }
771
772 ret = 0;
773
774 end:
775 grub_check_free(decompress_data);
776 return ret;
777 }
778
779 static wim_directory_entry * search_replace_wim_dirent
780 (
781 grub_file_t file,
782 wim_header *head,
783 wim_lookup_entry *lookup,
784 void *meta_data,
785 wim_directory_entry *dir
786 )
787 {
788 int ret;
789 char cmdline[256] = {0};
790 wim_directory_entry *wim_dirent = NULL;
791 wim_directory_entry *pecmd_dirent = NULL;
792 const char *peset_path[] = { "Windows", "System32", "peset.exe", NULL };
793 const char *pecmd_path[] = { "Windows", "System32", "pecmd.exe", NULL };
794 const char *winpeshl_path[] = { "Windows", "System32", "winpeshl.exe", NULL };
795
796 pecmd_dirent = search_full_wim_dirent(meta_data, dir, pecmd_path);
797 debug("search pecmd.exe %p\n", pecmd_dirent);
798
799 if (pecmd_dirent)
800 {
801 ret = parse_registry_setup_cmdline(file, head, lookup, meta_data, dir, cmdline, sizeof(cmdline));
802 if (0 == ret)
803 {
804 debug("registry setup cmdline:<%s>\n", cmdline);
805 ventoy_str_toupper(cmdline);
806
807 if (grub_strncmp(cmdline, "PECMD", 5) == 0)
808 {
809 wim_dirent = pecmd_dirent;
810 }
811 else if (grub_strncmp(cmdline, "PESET", 5) == 0)
812 {
813 wim_dirent = search_full_wim_dirent(meta_data, dir, peset_path);
814 debug("search peset.exe %p\n", wim_dirent);
815 }
816 else if (grub_strncmp(cmdline, "WINPESHL", 8) == 0)
817 {
818 wim_dirent = search_full_wim_dirent(meta_data, dir, winpeshl_path);
819 debug("search winpeshl.exe %p\n", wim_dirent);
820 }
821
822 if (wim_dirent)
823 {
824 return wim_dirent;
825 }
826 }
827 else
828 {
829 debug("registry setup cmdline failed : %d\n", ret);
830 }
831 }
832
833 wim_dirent = pecmd_dirent;
834 if (wim_dirent)
835 {
836 return wim_dirent;
837 }
838
839 wim_dirent = search_full_wim_dirent(meta_data, dir, winpeshl_path);
840 debug("search winpeshl.exe %p\n", wim_dirent);
841 if (wim_dirent)
842 {
843 return wim_dirent;
844 }
845
846 return NULL;
847 }
848
849
850 static wim_lookup_entry * ventoy_find_meta_entry(wim_header *header, wim_lookup_entry *lookup)
851 {
852 grub_uint32_t i = 0;
853 grub_uint32_t index = 0;;
854
855 if ((header == NULL) || (lookup == NULL))
856 {
857 return NULL;
858 }
859
860 for (i = 0; i < (grub_uint32_t)header->lookup.raw_size / sizeof(wim_lookup_entry); i++)
861 {
862 if (lookup[i].resource.flags & RESHDR_FLAG_METADATA)
863 {
864 index++;
865 if (index == header->boot_index)
866 {
867 return lookup + i;
868 }
869 }
870 }
871
872 return NULL;
873 }
874
875 static grub_uint64_t ventoy_get_stream_len(wim_directory_entry *dir)
876 {
877 grub_uint16_t i;
878 grub_uint64_t offset = 0;
879 wim_stream_entry *stream = (wim_stream_entry *)((char *)dir + dir->len);
880
881 for (i = 0; i < dir->streams; i++)
882 {
883 offset += stream->len;
884 stream = (wim_stream_entry *)((char *)stream + stream->len);
885 }
886
887 return offset;
888 }
889
890 static int ventoy_update_stream_hash(wim_patch *patch, wim_directory_entry *dir)
891 {
892 grub_uint16_t i;
893 grub_uint64_t offset = 0;
894 wim_stream_entry *stream = (wim_stream_entry *)((char *)dir + dir->len);
895
896 for (i = 0; i < dir->streams; i++)
897 {
898 if (grub_memcmp(stream->hash.sha1, patch->old_hash.sha1, sizeof(wim_hash)) == 0)
899 {
900 debug("find target stream %u, name_len:%u upadte hash\n", i, stream->name_len);
901 grub_memcpy(stream->hash.sha1, &(patch->wim_data.bin_hash), sizeof(wim_hash));
902 }
903
904 offset += stream->len;
905 stream = (wim_stream_entry *)((char *)stream + stream->len);
906 }
907
908 return offset;
909 }
910
911 static int ventoy_update_all_hash(wim_patch *patch, void *meta_data, wim_directory_entry *dir)
912 {
913 if ((meta_data == NULL) || (dir == NULL))
914 {
915 return 0;
916 }
917
918 if (dir->len < sizeof(wim_directory_entry))
919 {
920 return 0;
921 }
922
923 do
924 {
925 if (dir->subdir == 0 && grub_memcmp(dir->hash.sha1, patch->old_hash.sha1, sizeof(wim_hash)) == 0)
926 {
927 debug("find target file, name_len:%u upadte hash\n", dir->name_len);
928 grub_memcpy(dir->hash.sha1, &(patch->wim_data.bin_hash), sizeof(wim_hash));
929 }
930
931 if (dir->subdir)
932 {
933 ventoy_update_all_hash(patch, meta_data, (wim_directory_entry *)((char *)meta_data + dir->subdir));
934 }
935
936 if (dir->streams)
937 {
938 ventoy_update_stream_hash(patch, dir);
939 dir = (wim_directory_entry *)((char *)dir + dir->len + ventoy_get_stream_len(dir));
940 }
941 else
942 {
943 dir = (wim_directory_entry *)((char *)dir + dir->len);
944 }
945 } while (dir->len >= sizeof(wim_directory_entry));
946
947 return 0;
948 }
949
950 static int ventoy_cat_exe_file_data(wim_tail *wim_data, grub_uint32_t exe_len, grub_uint8_t *exe_data)
951 {
952 int pe64 = 0;
953 char file[256];
954 grub_uint32_t jump_len = 0;
955 grub_uint32_t jump_align = 0;
956 grub_uint8_t *jump_data = NULL;
957
958 pe64 = ventoy_is_pe64(exe_data);
959
960 grub_snprintf(file, sizeof(file), "%s/vtoyjump%d.exe", grub_env_get("vtoy_path"), pe64 ? 64 : 32);
961 ventoy_load_jump_exe(file, &jump_data, &jump_len, NULL);
962 jump_align = ventoy_align(jump_len, 16);
963
964 wim_data->jump_exe_len = jump_len;
965 wim_data->bin_raw_len = jump_align + sizeof(ventoy_os_param) + sizeof(ventoy_windows_data) + exe_len;
966 wim_data->bin_align_len = ventoy_align(wim_data->bin_raw_len, 2048);
967
968 wim_data->jump_bin_data = grub_malloc(wim_data->bin_align_len);
969 if (wim_data->jump_bin_data)
970 {
971 grub_memcpy(wim_data->jump_bin_data, jump_data, jump_len);
972 grub_memcpy(wim_data->jump_bin_data + jump_align + sizeof(ventoy_os_param) + sizeof(ventoy_windows_data), exe_data, exe_len);
973 }
974
975 debug("jump_exe_len:%u bin_raw_len:%u bin_align_len:%u\n",
976 wim_data->jump_exe_len, wim_data->bin_raw_len, wim_data->bin_align_len);
977
978 return 0;
979 }
980
981 int ventoy_fill_windows_rtdata(void *buf, char *isopath)
982 {
983 char *pos = NULL;
984 char *script = NULL;
985 ventoy_windows_data *data = (ventoy_windows_data *)buf;
986
987 grub_memset(data, 0, sizeof(ventoy_windows_data));
988
989 pos = grub_strstr(isopath, "/");
990 if (!pos)
991 {
992 return 1;
993 }
994
995 script = ventoy_plugin_get_cur_install_template(pos);
996 if (script)
997 {
998 debug("auto install script <%s>\n", script);
999 grub_snprintf(data->auto_install_script, sizeof(data->auto_install_script) - 1, "%s", script);
1000 }
1001 else
1002 {
1003 debug("auto install script skipped or not configed %s\n", pos);
1004 }
1005
1006 script = (char *)ventoy_plugin_get_injection(pos);
1007 if (script)
1008 {
1009 if (ventoy_check_file_exist("%s%s", ventoy_get_env("vtoy_iso_part"), script))
1010 {
1011 debug("injection archive <%s> OK\n", script);
1012 grub_snprintf(data->injection_archive, sizeof(data->injection_archive) - 1, "%s", script);
1013 }
1014 else
1015 {
1016 debug("injection archive <%s> NOT exist\n", script);
1017 }
1018 }
1019 else
1020 {
1021 debug("injection archive not configed %s\n", pos);
1022 }
1023
1024 return 0;
1025 }
1026
1027 static int ventoy_update_before_chain(ventoy_os_param *param, char *isopath)
1028 {
1029 grub_uint32_t jump_align = 0;
1030 wim_lookup_entry *meta_look = NULL;
1031 wim_security_header *security = NULL;
1032 wim_directory_entry *rootdir = NULL;
1033 wim_header *head = NULL;
1034 wim_lookup_entry *lookup = NULL;
1035 wim_patch *node = NULL;
1036 wim_tail *wim_data = NULL;
1037
1038 for (node = g_wim_patch_head; node; node = node->next)
1039 {
1040 if (0 == node->valid)
1041 {
1042 continue;
1043 }
1044
1045 wim_data = &node->wim_data;
1046 head = &wim_data->wim_header;
1047 lookup = (wim_lookup_entry *)wim_data->new_lookup_data;
1048
1049 jump_align = ventoy_align(wim_data->jump_exe_len, 16);
1050 if (wim_data->jump_bin_data)
1051 {
1052 grub_memcpy(wim_data->jump_bin_data + jump_align, param, sizeof(ventoy_os_param));
1053 ventoy_fill_windows_rtdata(wim_data->jump_bin_data + jump_align + sizeof(ventoy_os_param), isopath);
1054 }
1055
1056 grub_crypto_hash(GRUB_MD_SHA1, wim_data->bin_hash.sha1, wim_data->jump_bin_data, wim_data->bin_raw_len);
1057
1058 security = (wim_security_header *)wim_data->new_meta_data;
1059 if (security->len > 0)
1060 {
1061 rootdir = (wim_directory_entry *)(wim_data->new_meta_data + ((security->len + 7) & 0xFFFFFFF8U));
1062 }
1063 else
1064 {
1065 rootdir = (wim_directory_entry *)(wim_data->new_meta_data + 8);
1066 }
1067
1068 /* update all winpeshl.exe dirent entry's hash */
1069 ventoy_update_all_hash(node, wim_data->new_meta_data, rootdir);
1070
1071 /* update winpeshl.exe lookup entry data (hash/offset/length) */
1072 if (node->replace_look)
1073 {
1074 debug("update replace lookup entry_id:%ld\n", ((long)node->replace_look - (long)lookup) / sizeof(wim_lookup_entry));
1075 node->replace_look->resource.raw_size = wim_data->bin_raw_len;
1076 node->replace_look->resource.size_in_wim = wim_data->bin_raw_len;
1077 node->replace_look->resource.flags = 0;
1078 node->replace_look->resource.offset = wim_data->wim_align_size;
1079
1080 grub_memcpy(node->replace_look->hash.sha1, wim_data->bin_hash.sha1, sizeof(wim_hash));
1081 }
1082
1083 /* update metadata's hash */
1084 meta_look = ventoy_find_meta_entry(head, lookup);
1085 if (meta_look)
1086 {
1087 debug("find meta lookup entry_id:%ld\n", ((long)meta_look - (long)lookup) / sizeof(wim_lookup_entry));
1088 grub_memcpy(&meta_look->resource, &head->metadata, sizeof(wim_resource_header));
1089 grub_crypto_hash(GRUB_MD_SHA1, meta_look->hash.sha1, wim_data->new_meta_data, wim_data->new_meta_len);
1090 }
1091 }
1092
1093 return 0;
1094 }
1095
1096 static int ventoy_wimdows_locate_wim(const char *disk, wim_patch *patch)
1097 {
1098 int rc;
1099 grub_uint16_t i;
1100 grub_file_t file;
1101 grub_uint32_t exe_len;
1102 grub_uint8_t *exe_data = NULL;
1103 grub_uint8_t *decompress_data = NULL;
1104 wim_lookup_entry *lookup = NULL;
1105 wim_security_header *security = NULL;
1106 wim_directory_entry *rootdir = NULL;
1107 wim_directory_entry *search = NULL;
1108 wim_stream_entry *stream = NULL;
1109 wim_header *head = &(patch->wim_data.wim_header);
1110 wim_tail *wim_data = &patch->wim_data;
1111
1112 debug("windows locate wim start %s\n", patch->path);
1113
1114 g_ventoy_case_insensitive = 1;
1115 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s%s", disk, patch->path);
1116 g_ventoy_case_insensitive = 0;
1117
1118 if (!file)
1119 {
1120 debug("File %s%s NOT exist\n", disk, patch->path);
1121 return 1;
1122 }
1123
1124 ventoy_get_override_info(file, &patch->wim_data);
1125
1126 grub_file_seek(file, 0);
1127 grub_file_read(file, head, sizeof(wim_header));
1128
1129 if (grub_memcmp(head->signature, WIM_HEAD_SIGNATURE, sizeof(head->signature)))
1130 {
1131 debug("Not a valid wim file %s\n", (char *)head->signature);
1132 grub_file_close(file);
1133 return 1;
1134 }
1135
1136 if (head->flags & FLAG_HEADER_COMPRESS_LZMS)
1137 {
1138 debug("LZMS compress is not supported 0x%x\n", head->flags);
1139 grub_file_close(file);
1140 return 1;
1141 }
1142
1143 rc = ventoy_read_resource(file, head, &head->metadata, (void **)&decompress_data);
1144 if (rc)
1145 {
1146 grub_printf("failed to read meta data %d\n", rc);
1147 grub_file_close(file);
1148 return 1;
1149 }
1150
1151 security = (wim_security_header *)decompress_data;
1152 if (security->len > 0)
1153 {
1154 rootdir = (wim_directory_entry *)(decompress_data + ((security->len + 7) & 0xFFFFFFF8U));
1155 }
1156 else
1157 {
1158 rootdir = (wim_directory_entry *)(decompress_data + 8);
1159 }
1160
1161
1162 debug("read lookup offset:%llu size:%llu\n", (ulonglong)head->lookup.offset, (ulonglong)head->lookup.raw_size);
1163 lookup = grub_malloc(head->lookup.raw_size);
1164 grub_file_seek(file, head->lookup.offset);
1165 grub_file_read(file, lookup, head->lookup.raw_size);
1166
1167
1168
1169 /* search winpeshl.exe dirent entry */
1170 search = search_replace_wim_dirent(file, head, lookup, decompress_data, rootdir);
1171 if (!search)
1172 {
1173 debug("Failed to find replace file %p\n", search);
1174 grub_file_close(file);
1175 return 1;
1176 }
1177
1178 debug("find replace file at %p\n", search);
1179
1180 grub_memset(&patch->old_hash, 0, sizeof(wim_hash));
1181 if (grub_memcmp(&patch->old_hash, search->hash.sha1, sizeof(wim_hash)) == 0)
1182 {
1183 debug("search hash all 0, now do deep search\n");
1184 stream = (wim_stream_entry *)((char *)search + search->len);
1185 for (i = 0; i < search->streams; i++)
1186 {
1187 if (stream->name_len == 0)
1188 {
1189 grub_memcpy(&patch->old_hash, stream->hash.sha1, sizeof(wim_hash));
1190 debug("new search hash: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1191 ventoy_varg_8(patch->old_hash.sha1));
1192 break;
1193 }
1194 stream = (wim_stream_entry *)((char *)stream + stream->len);
1195 }
1196 }
1197 else
1198 {
1199 grub_memcpy(&patch->old_hash, search->hash.sha1, sizeof(wim_hash));
1200 }
1201
1202
1203 /* find and extact winpeshl.exe */
1204 patch->replace_look = ventoy_find_look_entry(head, lookup, &patch->old_hash);
1205 if (patch->replace_look)
1206 {
1207 exe_len = (grub_uint32_t)patch->replace_look->resource.raw_size;
1208 debug("find replace lookup entry_id:%ld raw_size:%u\n",
1209 ((long)patch->replace_look - (long)lookup) / sizeof(wim_lookup_entry), exe_len);
1210
1211 if (0 == ventoy_read_resource(file, head, &(patch->replace_look->resource), (void **)&(exe_data)))
1212 {
1213 ventoy_cat_exe_file_data(wim_data, exe_len, exe_data);
1214 grub_free(exe_data);
1215 }
1216 else
1217 {
1218 debug("failed to read replace file meta data %u\n", exe_len);
1219 }
1220 }
1221 else
1222 {
1223 debug("failed to find lookup entry for replace file %02x %02x %02x %02x\n",
1224 ventoy_varg_4(patch->old_hash.sha1));
1225 }
1226
1227 wim_data->wim_raw_size = (grub_uint32_t)file->size;
1228 wim_data->wim_align_size = ventoy_align(wim_data->wim_raw_size, 2048);
1229
1230 grub_check_free(wim_data->new_meta_data);
1231 wim_data->new_meta_data = decompress_data;
1232 wim_data->new_meta_len = head->metadata.raw_size;
1233 wim_data->new_meta_align_len = ventoy_align(wim_data->new_meta_len, 2048);
1234
1235 grub_check_free(wim_data->new_lookup_data);
1236 wim_data->new_lookup_data = (grub_uint8_t *)lookup;
1237 wim_data->new_lookup_len = (grub_uint32_t)head->lookup.raw_size;
1238 wim_data->new_lookup_align_len = ventoy_align(wim_data->new_lookup_len, 2048);
1239
1240 head->metadata.flags = RESHDR_FLAG_METADATA;
1241 head->metadata.offset = wim_data->wim_align_size + wim_data->bin_align_len;
1242 head->metadata.size_in_wim = wim_data->new_meta_len;
1243 head->metadata.raw_size = wim_data->new_meta_len;
1244
1245 head->lookup.flags = 0;
1246 head->lookup.offset = head->metadata.offset + wim_data->new_meta_align_len;
1247 head->lookup.size_in_wim = wim_data->new_lookup_len;
1248 head->lookup.raw_size = wim_data->new_lookup_len;
1249
1250 grub_file_close(file);
1251
1252 debug("%s", "windows locate wim finish\n");
1253 return 0;
1254 }
1255
1256 grub_err_t ventoy_cmd_locate_wim_patch(grub_extcmd_context_t ctxt, int argc, char **args)
1257 {
1258 wim_patch *node = g_wim_patch_head;
1259
1260 (void)ctxt;
1261 (void)argc;
1262 (void)args;
1263
1264 while (node)
1265 {
1266 if (0 == ventoy_wimdows_locate_wim(args[0], node))
1267 {
1268 node->valid = 1;
1269 g_wim_valid_patch_count++;
1270 }
1271
1272 node = node->next;
1273 }
1274
1275 return 0;
1276 }
1277
1278 static grub_uint32_t ventoy_get_override_chunk_num(void)
1279 {
1280 grub_uint32_t chunk_num = 0;
1281
1282 if (g_iso_fs_type == 0)
1283 {
1284 /* ISO9660: */
1285 /* per wim */
1286 /* 1: file_size and file_offset */
1287 /* 2: new wim file header */
1288 chunk_num = g_wim_valid_patch_count * 2;
1289 }
1290 else
1291 {
1292 /* UDF: */
1293 /* global: */
1294 /* 1: block count in Partition Descriptor */
1295
1296 /* per wim */
1297 /* 1: file_size in file_entry or extend_file_entry */
1298 /* 2: data_size and position in extend data short ad */
1299 /* 3: new wim file header */
1300 chunk_num = g_wim_valid_patch_count * 3 + 1;
1301 }
1302
1303 if (g_suppress_wincd_override_offset > 0)
1304 {
1305 chunk_num++;
1306 }
1307
1308 return chunk_num;
1309 }
1310
1311 static void ventoy_fill_suppress_wincd_override_data(void *override)
1312 {
1313 ventoy_override_chunk *cur = (ventoy_override_chunk *)override;
1314
1315 cur->override_size = 4;
1316 cur->img_offset = g_suppress_wincd_override_offset;
1317 grub_memcpy(cur->override_data, &g_suppress_wincd_override_data, cur->override_size);
1318 }
1319
1320 static void ventoy_windows_fill_override_data_iso9660( grub_uint64_t isosize, void *override)
1321 {
1322 grub_uint64_t sector;
1323 grub_uint32_t new_wim_size;
1324 ventoy_override_chunk *cur;
1325 wim_patch *node = NULL;
1326 wim_tail *wim_data = NULL;
1327 ventoy_iso9660_override *dirent = NULL;
1328
1329 sector = (isosize + 2047) / 2048;
1330
1331 cur = (ventoy_override_chunk *)override;
1332
1333 if (g_suppress_wincd_override_offset > 0)
1334 {
1335 ventoy_fill_suppress_wincd_override_data(cur);
1336 cur++;
1337 }
1338
1339 debug("ventoy_windows_fill_override_data_iso9660 %lu\n", (ulong)isosize);
1340
1341 for (node = g_wim_patch_head; node; node = node->next)
1342 {
1343 wim_data = &node->wim_data;
1344 if (0 == node->valid)
1345 {
1346 continue;
1347 }
1348
1349 new_wim_size = wim_data->wim_align_size + wim_data->bin_align_len +
1350 wim_data->new_meta_align_len + wim_data->new_lookup_align_len;
1351
1352 dirent = (ventoy_iso9660_override *)wim_data->override_data;
1353
1354 dirent->first_sector = (grub_uint32_t)sector;
1355 dirent->size = new_wim_size;
1356 dirent->first_sector_be = grub_swap_bytes32(dirent->first_sector);
1357 dirent->size_be = grub_swap_bytes32(dirent->size);
1358
1359 sector += (new_wim_size / 2048);
1360
1361 /* override 1: position and length in dirent */
1362 cur->img_offset = wim_data->override_offset;
1363 cur->override_size = wim_data->override_len;
1364 grub_memcpy(cur->override_data, wim_data->override_data, cur->override_size);
1365 cur++;
1366
1367 /* override 2: new wim file header */
1368 cur->img_offset = wim_data->file_offset;
1369 cur->override_size = sizeof(wim_header);
1370 grub_memcpy(cur->override_data, &(wim_data->wim_header), cur->override_size);
1371 cur++;
1372 }
1373
1374 return;
1375 }
1376
1377 static int ventoy_windows_fill_udf_short_ad(grub_file_t isofile, grub_uint32_t curpos,
1378 wim_tail *wim_data, grub_uint32_t new_wim_size)
1379 {
1380 int i;
1381 grub_uint32_t total = 0;
1382 grub_uint32_t left_size = 0;
1383 ventoy_udf_override *udf = NULL;
1384 ventoy_udf_override tmp[4];
1385
1386 grub_memset(tmp, 0, sizeof(tmp));
1387 grub_file_seek(isofile, wim_data->override_offset);
1388 grub_file_read(isofile, tmp, sizeof(tmp));
1389
1390 left_size = new_wim_size;
1391 udf = (ventoy_udf_override *)wim_data->override_data;
1392
1393 for (i = 0; i < 4; i++)
1394 {
1395 total += tmp[i].length;
1396 if (total >= wim_data->wim_raw_size)
1397 {
1398 udf->length = left_size;
1399 udf->position = curpos;
1400 return 0;
1401 }
1402 else
1403 {
1404 udf->length = tmp[i].length;
1405 udf->position = curpos;
1406 }
1407
1408 left_size -= tmp[i].length;
1409 curpos += udf->length / 2048;
1410 udf++;
1411 wim_data->override_len += sizeof(ventoy_udf_override);
1412 }
1413
1414 debug("######## Too many udf ad ######\n");
1415 return 1;
1416 }
1417
1418 static void ventoy_windows_fill_override_data_udf(grub_file_t isofile, void *override)
1419 {
1420 grub_uint32_t data32;
1421 grub_uint64_t data64;
1422 grub_uint64_t sector;
1423 grub_uint32_t new_wim_size;
1424 grub_uint64_t total_wim_size = 0;
1425 grub_uint32_t udf_start_block = 0;
1426 ventoy_override_chunk *cur;
1427 wim_patch *node = NULL;
1428 wim_tail *wim_data = NULL;
1429
1430 sector = (isofile->size + 2047) / 2048;
1431
1432 cur = (ventoy_override_chunk *)override;
1433
1434 if (g_suppress_wincd_override_offset > 0)
1435 {
1436 ventoy_fill_suppress_wincd_override_data(cur);
1437 cur++;
1438 }
1439
1440 debug("ventoy_windows_fill_override_data_udf %lu\n", (ulong)isofile->size);
1441
1442 for (node = g_wim_patch_head; node; node = node->next)
1443 {
1444 wim_data = &node->wim_data;
1445 if (node->valid)
1446 {
1447 if (udf_start_block == 0)
1448 {
1449 udf_start_block = wim_data->udf_start_block;
1450 }
1451 new_wim_size = wim_data->wim_align_size + wim_data->bin_align_len +
1452 wim_data->new_meta_align_len + wim_data->new_lookup_align_len;
1453 total_wim_size += new_wim_size;
1454 }
1455 }
1456
1457 //override 1: sector number in pd data
1458 cur->img_offset = grub_udf_get_last_pd_size_offset();
1459 cur->override_size = 4;
1460 data32 = sector - udf_start_block + (total_wim_size / 2048);
1461 grub_memcpy(cur->override_data, &(data32), 4);
1462
1463 for (node = g_wim_patch_head; node; node = node->next)
1464 {
1465 wim_data = &node->wim_data;
1466 if (0 == node->valid)
1467 {
1468 continue;
1469 }
1470
1471 new_wim_size = wim_data->wim_align_size + wim_data->bin_align_len +
1472 wim_data->new_meta_align_len + wim_data->new_lookup_align_len;
1473
1474 //override 2: filesize in file_entry
1475 cur++;
1476 cur->img_offset = wim_data->fe_entry_size_offset;
1477 cur->override_size = 8;
1478 data64 = new_wim_size;
1479 grub_memcpy(cur->override_data, &(data64), 8);
1480
1481 /* override 3: position and length in extend data */
1482 ventoy_windows_fill_udf_short_ad(isofile, (grub_uint32_t)sector - udf_start_block, wim_data, new_wim_size);
1483
1484 sector += (new_wim_size / 2048);
1485
1486 cur++;
1487 cur->img_offset = wim_data->override_offset;
1488 cur->override_size = wim_data->override_len;
1489 grub_memcpy(cur->override_data, wim_data->override_data, cur->override_size);
1490
1491 /* override 4: new wim file header */
1492 cur++;
1493 cur->img_offset = wim_data->file_offset;
1494 cur->override_size = sizeof(wim_header);
1495 grub_memcpy(cur->override_data, &(wim_data->wim_header), cur->override_size);
1496 }
1497
1498 return;
1499 }
1500
1501 static grub_uint32_t ventoy_windows_get_virt_data_size(void)
1502 {
1503 grub_uint32_t size = 0;
1504 wim_tail *wim_data = NULL;
1505 wim_patch *node = g_wim_patch_head;
1506
1507 while (node)
1508 {
1509 if (node->valid)
1510 {
1511 wim_data = &node->wim_data;
1512 size += sizeof(ventoy_virt_chunk) + wim_data->bin_align_len +
1513 wim_data->new_meta_align_len + wim_data->new_lookup_align_len;
1514 }
1515 node = node->next;
1516 }
1517
1518 return size;
1519 }
1520
1521 static void ventoy_windows_fill_virt_data( grub_uint64_t isosize, ventoy_chain_head *chain)
1522 {
1523 grub_uint64_t sector;
1524 grub_uint32_t offset;
1525 grub_uint32_t wim_secs;
1526 grub_uint32_t mem_secs;
1527 char *override = NULL;
1528 ventoy_virt_chunk *cur = NULL;
1529 wim_tail *wim_data = NULL;
1530 wim_patch *node = NULL;
1531
1532 sector = (isosize + 2047) / 2048;
1533 offset = sizeof(ventoy_virt_chunk) * g_wim_valid_patch_count;
1534
1535 override = (char *)chain + chain->virt_chunk_offset;
1536 cur = (ventoy_virt_chunk *)override;
1537
1538 for (node = g_wim_patch_head; node; node = node->next)
1539 {
1540 if (0 == node->valid)
1541 {
1542 continue;
1543 }
1544
1545 wim_data = &node->wim_data;
1546
1547 wim_secs = wim_data->wim_align_size / 2048;
1548 mem_secs = (wim_data->bin_align_len + wim_data->new_meta_align_len + wim_data->new_lookup_align_len) / 2048;
1549
1550 cur->remap_sector_start = sector;
1551 cur->remap_sector_end = cur->remap_sector_start + wim_secs;
1552 cur->org_sector_start = (grub_uint32_t)(wim_data->file_offset / 2048);
1553
1554 cur->mem_sector_start = cur->remap_sector_end;
1555 cur->mem_sector_end = cur->mem_sector_start + mem_secs;
1556 cur->mem_sector_offset = offset;
1557
1558 sector += wim_secs + mem_secs;
1559 cur++;
1560
1561 grub_memcpy(override + offset, wim_data->jump_bin_data, wim_data->bin_raw_len);
1562 offset += wim_data->bin_align_len;
1563
1564 grub_memcpy(override + offset, wim_data->new_meta_data, wim_data->new_meta_len);
1565 offset += wim_data->new_meta_align_len;
1566
1567 grub_memcpy(override + offset, wim_data->new_lookup_data, wim_data->new_lookup_len);
1568 offset += wim_data->new_lookup_align_len;
1569
1570 chain->virt_img_size_in_bytes += wim_data->wim_align_size +
1571 wim_data->bin_align_len +
1572 wim_data->new_meta_align_len +
1573 wim_data->new_lookup_align_len;
1574 }
1575
1576 return;
1577 }
1578
1579 static int ventoy_windows_drive_map(ventoy_chain_head *chain)
1580 {
1581 grub_disk_t disk;
1582
1583 debug("drive map begin <%p> ...\n", chain);
1584
1585 if (chain->disk_drive == 0x80)
1586 {
1587 disk = grub_disk_open("hd1");
1588 if (disk)
1589 {
1590 grub_disk_close(disk);
1591 debug("drive map needed %p\n", disk);
1592 chain->drive_map = 0x81;
1593 }
1594 else
1595 {
1596 debug("failed to open disk %s\n", "hd1");
1597 }
1598 }
1599 else
1600 {
1601 debug("no need to map 0x%x\n", chain->disk_drive);
1602 }
1603
1604 return 0;
1605 }
1606
1607 static int ventoy_suppress_windows_cd_prompt(void)
1608 {
1609 int rc = 1;
1610 const char *cdprompt = NULL;
1611 grub_uint64_t readpos = 0;
1612 grub_file_t file = NULL;
1613 grub_uint8_t data[32];
1614
1615 cdprompt = ventoy_get_env("VTOY_WINDOWS_CD_PROMPT");
1616 if (cdprompt && cdprompt[0] == '1' && cdprompt[1] == 0)
1617 {
1618 debug("VTOY_WINDOWS_CD_PROMPT:<%s>\n", cdprompt);
1619 return 0;
1620 }
1621
1622 g_ventoy_case_insensitive = 1;
1623 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s/boot/bootfix.bin", "(loop)");
1624 g_ventoy_case_insensitive = 0;
1625
1626 if (!file)
1627 {
1628 debug("Failed to open %s\n", "bootfix.bin");
1629 goto end;
1630 }
1631
1632 grub_file_read(file, data, 32);
1633
1634 if (file->fs && file->fs->name && grub_strcmp(file->fs->name, "udf") == 0)
1635 {
1636 readpos = grub_udf_get_file_offset(file);
1637 }
1638 else
1639 {
1640 readpos = grub_iso9660_get_last_read_pos(file);
1641 }
1642
1643 debug("bootfix.bin readpos:%lu (sector:%lu) data: %02x %02x %02x %02x\n",
1644 (ulong)readpos, (ulong)readpos / 2048, data[24], data[25], data[26], data[27]);
1645
1646 if (*(grub_uint32_t *)(data + 24) == 0x13cd0080)
1647 {
1648 g_suppress_wincd_override_offset = readpos + 24;
1649 g_suppress_wincd_override_data = 0x13cd00fd;
1650
1651 rc = 0;
1652 }
1653
1654 debug("g_suppress_wincd_override_offset:%lu\n", (ulong)g_suppress_wincd_override_offset);
1655
1656 end:
1657 check_free(file, grub_file_close);
1658
1659 return rc;
1660 }
1661
1662 grub_err_t ventoy_cmd_windows_wimboot_data(grub_extcmd_context_t ctxt, int argc, char **args)
1663 {
1664 grub_uint32_t size = 0;
1665 const char *addr = NULL;
1666 ventoy_chain_head *chain = NULL;
1667 ventoy_os_param *param = NULL;
1668 char envbuf[64];
1669
1670 (void)ctxt;
1671 (void)argc;
1672 (void)args;
1673
1674 addr = grub_env_get("vtoy_chain_mem_addr");
1675 if (!addr)
1676 {
1677 debug("Failed to find vtoy_chain_mem_addr\n");
1678 return 1;
1679 }
1680
1681 chain = (ventoy_chain_head *)(void *)grub_strtoul(addr, NULL, 16);
1682
1683 if (grub_memcmp(&g_ventoy_guid, &chain->os_param.guid, 16) != 0)
1684 {
1685 debug("os_param.guid not match\n");
1686 return 1;
1687 }
1688
1689 size = sizeof(ventoy_os_param) + sizeof(ventoy_windows_data);
1690 param = (ventoy_os_param *)grub_zalloc(size);
1691 if (!param)
1692 {
1693 return 1;
1694 }
1695
1696 grub_memcpy(param, &chain->os_param, sizeof(ventoy_os_param));
1697 ventoy_fill_windows_rtdata(param + 1, param->vtoy_img_path);
1698
1699 grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)param);
1700 grub_env_set("vtoy_wimboot_mem_addr", envbuf);
1701 debug("vtoy_wimboot_mem_addr: %s\n", envbuf);
1702
1703 grub_snprintf(envbuf, sizeof(envbuf), "%u", size);
1704 grub_env_set("vtoy_wimboot_mem_size", envbuf);
1705 debug("vtoy_wimboot_mem_size: %s\n", envbuf);
1706
1707 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
1708 }
1709
1710 grub_err_t ventoy_cmd_windows_chain_data(grub_extcmd_context_t ctxt, int argc, char **args)
1711 {
1712 int unknown_image = 0;
1713 int ventoy_compatible = 0;
1714 grub_uint32_t size = 0;
1715 grub_uint64_t isosize = 0;
1716 grub_uint32_t boot_catlog = 0;
1717 grub_uint32_t img_chunk_size = 0;
1718 grub_uint32_t override_size = 0;
1719 grub_uint32_t virt_chunk_size = 0;
1720 grub_file_t file;
1721 grub_disk_t disk;
1722 const char *pLastChain = NULL;
1723 const char *compatible;
1724 ventoy_chain_head *chain;
1725 char envbuf[64];
1726
1727 (void)ctxt;
1728 (void)argc;
1729
1730 debug("chain data begin <%s> ...\n", args[0]);
1731
1732 compatible = grub_env_get("ventoy_compatible");
1733 if (compatible && compatible[0] == 'Y')
1734 {
1735 ventoy_compatible = 1;
1736 }
1737
1738 if (NULL == g_img_chunk_list.chunk)
1739 {
1740 grub_printf("ventoy not ready\n");
1741 return 1;
1742 }
1743
1744 if (0 == ventoy_compatible && g_wim_valid_patch_count == 0)
1745 {
1746 unknown_image = 1;
1747 debug("Warning: %s was not recognized by Ventoy\n", args[0]);
1748 }
1749
1750 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", args[0]);
1751 if (!file)
1752 {
1753 return 1;
1754 }
1755
1756 isosize = file->size;
1757
1758 boot_catlog = ventoy_get_iso_boot_catlog(file);
1759 if (boot_catlog)
1760 {
1761 if (ventoy_is_efi_os() && (!ventoy_has_efi_eltorito(file, boot_catlog)))
1762 {
1763 grub_env_set("LoadIsoEfiDriver", "on");
1764 }
1765 }
1766 else
1767 {
1768 if (ventoy_is_efi_os())
1769 {
1770 grub_env_set("LoadIsoEfiDriver", "on");
1771 }
1772 else
1773 {
1774 return grub_error(GRUB_ERR_BAD_ARGUMENT, "File %s is not bootable", args[0]);
1775 }
1776 }
1777
1778 g_suppress_wincd_override_offset = 0;
1779 if (!ventoy_is_efi_os()) /* legacy mode */
1780 {
1781 ventoy_suppress_windows_cd_prompt();
1782 }
1783
1784 img_chunk_size = g_img_chunk_list.cur_chunk * sizeof(ventoy_img_chunk);
1785
1786 if (ventoy_compatible || unknown_image)
1787 {
1788 override_size = g_suppress_wincd_override_offset > 0 ? sizeof(ventoy_override_chunk) : 0;
1789 size = sizeof(ventoy_chain_head) + img_chunk_size + override_size;
1790 }
1791 else
1792 {
1793 override_size = ventoy_get_override_chunk_num() * sizeof(ventoy_override_chunk);
1794 virt_chunk_size = ventoy_windows_get_virt_data_size();
1795 size = sizeof(ventoy_chain_head) + img_chunk_size + override_size + virt_chunk_size;
1796 }
1797
1798 pLastChain = grub_env_get("vtoy_chain_mem_addr");
1799 if (pLastChain)
1800 {
1801 chain = (ventoy_chain_head *)grub_strtoul(pLastChain, NULL, 16);
1802 if (chain)
1803 {
1804 debug("free last chain memory %p\n", chain);
1805 grub_free(chain);
1806 }
1807 }
1808
1809 chain = grub_malloc(size);
1810 if (!chain)
1811 {
1812 grub_printf("Failed to alloc chain memory size %u\n", size);
1813 grub_file_close(file);
1814 return 1;
1815 }
1816
1817 grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain);
1818 grub_env_set("vtoy_chain_mem_addr", envbuf);
1819 grub_snprintf(envbuf, sizeof(envbuf), "%u", size);
1820 grub_env_set("vtoy_chain_mem_size", envbuf);
1821
1822 grub_memset(chain, 0, sizeof(ventoy_chain_head));
1823
1824 /* part 1: os parameter */
1825 g_ventoy_chain_type = ventoy_chain_windows;
1826 ventoy_fill_os_param(file, &(chain->os_param));
1827
1828 if (0 == unknown_image)
1829 {
1830 ventoy_update_before_chain(&(chain->os_param), args[0]);
1831 }
1832
1833 /* part 2: chain head */
1834 disk = file->device->disk;
1835 chain->disk_drive = disk->id;
1836 chain->disk_sector_size = (1 << disk->log_sector_size);
1837 chain->real_img_size_in_bytes = file->size;
1838 chain->virt_img_size_in_bytes = (file->size + 2047) / 2048 * 2048;
1839 chain->boot_catalog = boot_catlog;
1840
1841 if (!ventoy_is_efi_os())
1842 {
1843 grub_file_seek(file, boot_catlog * 2048);
1844 grub_file_read(file, chain->boot_catalog_sector, sizeof(chain->boot_catalog_sector));
1845 }
1846
1847 /* part 3: image chunk */
1848 chain->img_chunk_offset = sizeof(ventoy_chain_head);
1849 chain->img_chunk_num = g_img_chunk_list.cur_chunk;
1850 grub_memcpy((char *)chain + chain->img_chunk_offset, g_img_chunk_list.chunk, img_chunk_size);
1851
1852 if (ventoy_compatible || unknown_image)
1853 {
1854 if (g_suppress_wincd_override_offset > 0)
1855 {
1856 chain->override_chunk_offset = chain->img_chunk_offset + img_chunk_size;
1857 chain->override_chunk_num = 1;
1858 ventoy_fill_suppress_wincd_override_data((char *)chain + chain->override_chunk_offset);
1859 }
1860
1861 return 0;
1862 }
1863
1864 if (0 == g_wim_valid_patch_count)
1865 {
1866 return 0;
1867 }
1868
1869 /* part 4: override chunk */
1870 chain->override_chunk_offset = chain->img_chunk_offset + img_chunk_size;
1871 chain->override_chunk_num = ventoy_get_override_chunk_num();
1872
1873 if (g_iso_fs_type == 0)
1874 {
1875 ventoy_windows_fill_override_data_iso9660(isosize, (char *)chain + chain->override_chunk_offset);
1876 }
1877 else
1878 {
1879 ventoy_windows_fill_override_data_udf(file, (char *)chain + chain->override_chunk_offset);
1880 }
1881
1882 /* part 5: virt chunk */
1883 chain->virt_chunk_offset = chain->override_chunk_offset + override_size;
1884 chain->virt_chunk_num = g_wim_valid_patch_count;
1885 ventoy_windows_fill_virt_data(isosize, chain);
1886
1887 if (ventoy_is_efi_os() == 0)
1888 {
1889 ventoy_windows_drive_map(chain);
1890 }
1891
1892 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
1893 }
1894
1895 static grub_uint32_t ventoy_get_wim_iso_offset(const char *filepath)
1896 {
1897 grub_uint32_t imgoffset;
1898 grub_file_t file;
1899 char cmdbuf[128];
1900
1901 grub_snprintf(cmdbuf, sizeof(cmdbuf), "loopback wimiso \"%s\"", filepath);
1902 grub_script_execute_sourcecode(cmdbuf);
1903
1904 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", "(wimiso)/boot/boot.wim");
1905 if (!file)
1906 {
1907 grub_printf("Failed to open boot.wim file in the image file\n");
1908 return 0;
1909 }
1910
1911 imgoffset = (grub_uint32_t)grub_iso9660_get_last_file_dirent_pos(file) + 2;
1912
1913 debug("wimiso wim direct offset: %u\n", imgoffset);
1914
1915 grub_file_close(file);
1916
1917 grub_script_execute_sourcecode("loopback -d wimiso");
1918
1919 return imgoffset;
1920 }
1921
1922 static int ventoy_get_wim_chunklist(const char *filename, ventoy_img_chunk_list *wimchunk, grub_uint64_t *wimsize)
1923 {
1924 grub_file_t wimfile;
1925
1926 wimfile = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", filename);
1927 if (!wimfile)
1928 {
1929 return 1;
1930 }
1931
1932 grub_memset(wimchunk, 0, sizeof(ventoy_img_chunk_list));
1933 wimchunk->chunk = grub_malloc(sizeof(ventoy_img_chunk) * DEFAULT_CHUNK_NUM);
1934 if (NULL == wimchunk->chunk)
1935 {
1936 grub_file_close(wimfile);
1937 return grub_error(GRUB_ERR_OUT_OF_MEMORY, "Can't allocate image chunk memoty\n");
1938 }
1939
1940 wimchunk->max_chunk = DEFAULT_CHUNK_NUM;
1941 wimchunk->cur_chunk = 0;
1942
1943 ventoy_get_block_list(wimfile, wimchunk, wimfile->device->disk->partition->start);
1944
1945 *wimsize = wimfile->size;
1946 grub_file_close(wimfile);
1947
1948 return 0;
1949 }
1950
1951 grub_err_t ventoy_cmd_wim_check_bootable(grub_extcmd_context_t ctxt, int argc, char **args)
1952 {
1953 grub_uint32_t boot_index;
1954 grub_file_t file = NULL;
1955 wim_header *wimhdr = NULL;
1956
1957 (void)ctxt;
1958 (void)argc;
1959
1960 wimhdr = grub_zalloc(sizeof(wim_header));
1961 if (!wimhdr)
1962 {
1963 return 1;
1964 }
1965
1966 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", args[0]);
1967 if (!file)
1968 {
1969 grub_free(wimhdr);
1970 return 1;
1971 }
1972
1973 grub_file_read(file, wimhdr, sizeof(wim_header));
1974 grub_file_close(file);
1975 boot_index = wimhdr->boot_index;
1976 grub_free(wimhdr);
1977
1978 if (boot_index == 0)
1979 {
1980 return 1;
1981 }
1982
1983 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
1984 }
1985
1986 grub_err_t ventoy_cmd_wim_chain_data(grub_extcmd_context_t ctxt, int argc, char **args)
1987 {
1988 grub_uint32_t i = 0;
1989 grub_uint32_t imgoffset = 0;
1990 grub_uint32_t size = 0;
1991 grub_uint32_t isosector = 0;
1992 grub_uint64_t wimsize = 0;
1993 grub_uint32_t boot_catlog = 0;
1994 grub_uint32_t img_chunk1_size = 0;
1995 grub_uint32_t img_chunk2_size = 0;
1996 grub_uint32_t override_size = 0;
1997 grub_file_t file;
1998 grub_disk_t disk;
1999 const char *pLastChain = NULL;
2000 ventoy_chain_head *chain;
2001 ventoy_iso9660_override *dirent;
2002 ventoy_img_chunk *chunknode;
2003 ventoy_override_chunk *override;
2004 ventoy_img_chunk_list wimchunk;
2005 char envbuf[128];
2006
2007 (void)ctxt;
2008 (void)argc;
2009
2010 debug("wim chain data begin <%s> ...\n", args[0]);
2011
2012 if (NULL == g_wimiso_chunk_list.chunk || NULL == g_wimiso_path)
2013 {
2014 grub_printf("ventoy not ready\n");
2015 return 1;
2016 }
2017
2018 imgoffset = ventoy_get_wim_iso_offset(g_wimiso_path);
2019 if (imgoffset == 0)
2020 {
2021 grub_printf("image offset not found\n");
2022 return 1;
2023 }
2024
2025 if (0 != ventoy_get_wim_chunklist(args[0], &wimchunk, &wimsize))
2026 {
2027 grub_printf("Failed to get wim chunklist\n");
2028 return 1;
2029 }
2030
2031 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", g_wimiso_path);
2032 if (!file)
2033 {
2034 return 1;
2035 }
2036
2037 boot_catlog = ventoy_get_iso_boot_catlog(file);
2038
2039 img_chunk1_size = g_wimiso_chunk_list.cur_chunk * sizeof(ventoy_img_chunk);
2040 img_chunk2_size = wimchunk.cur_chunk * sizeof(ventoy_img_chunk);
2041 override_size = sizeof(ventoy_override_chunk);
2042
2043 size = sizeof(ventoy_chain_head) + img_chunk1_size + img_chunk2_size + override_size;
2044
2045 pLastChain = grub_env_get("vtoy_chain_mem_addr");
2046 if (pLastChain)
2047 {
2048 chain = (ventoy_chain_head *)grub_strtoul(pLastChain, NULL, 16);
2049 if (chain)
2050 {
2051 debug("free last chain memory %p\n", chain);
2052 grub_free(chain);
2053 }
2054 }
2055
2056 chain = grub_malloc(size);
2057 if (!chain)
2058 {
2059 grub_printf("Failed to alloc chain memory size %u\n", size);
2060 grub_file_close(file);
2061 return 1;
2062 }
2063
2064 grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain);
2065 grub_env_set("vtoy_chain_mem_addr", envbuf);
2066 grub_snprintf(envbuf, sizeof(envbuf), "%u", size);
2067 grub_env_set("vtoy_chain_mem_size", envbuf);
2068
2069 grub_memset(chain, 0, sizeof(ventoy_chain_head));
2070
2071 /* part 1: os parameter */
2072 g_ventoy_chain_type = ventoy_chain_wim;
2073 ventoy_fill_os_param(file, &(chain->os_param));
2074
2075 /* part 2: chain head */
2076 disk = file->device->disk;
2077 chain->disk_drive = disk->id;
2078 chain->disk_sector_size = (1 << disk->log_sector_size);
2079 chain->real_img_size_in_bytes = ventoy_align_2k(file->size) + ventoy_align_2k(wimsize);
2080 chain->virt_img_size_in_bytes = chain->real_img_size_in_bytes;
2081 chain->boot_catalog = boot_catlog;
2082
2083 if (!ventoy_is_efi_os())
2084 {
2085 grub_file_seek(file, boot_catlog * 2048);
2086 grub_file_read(file, chain->boot_catalog_sector, sizeof(chain->boot_catalog_sector));
2087 }
2088
2089 /* part 3: image chunk */
2090 chain->img_chunk_offset = sizeof(ventoy_chain_head);
2091 chain->img_chunk_num = g_wimiso_chunk_list.cur_chunk + wimchunk.cur_chunk;
2092 grub_memcpy((char *)chain + chain->img_chunk_offset, g_wimiso_chunk_list.chunk, img_chunk1_size);
2093
2094 /* fs cluster size >= 2048, so don't need to proc align */
2095
2096 /* align by 2048 */
2097 chunknode = wimchunk.chunk + wimchunk.cur_chunk - 1;
2098 i = (chunknode->disk_end_sector + 1 - chunknode->disk_start_sector) % 4;
2099 if (i)
2100 {
2101 chunknode->disk_end_sector += 4 - i;
2102 }
2103
2104 isosector = (grub_uint32_t)((file->size + 2047) / 2048);
2105 for (i = 0; i < wimchunk.cur_chunk; i++)
2106 {
2107 chunknode = wimchunk.chunk + i;
2108 chunknode->img_start_sector = isosector;
2109 chunknode->img_end_sector = chunknode->img_start_sector +
2110 ((chunknode->disk_end_sector + 1 - chunknode->disk_start_sector) / 4) - 1;
2111 isosector = chunknode->img_end_sector + 1;
2112 }
2113
2114 grub_memcpy((char *)chain + chain->img_chunk_offset + img_chunk1_size, wimchunk.chunk, img_chunk2_size);
2115
2116 /* part 4: override chunk */
2117 chain->override_chunk_offset = chain->img_chunk_offset + img_chunk1_size + img_chunk2_size;
2118 chain->override_chunk_num = 1;
2119
2120 override = (ventoy_override_chunk *)((char *)chain + chain->override_chunk_offset);
2121 override->img_offset = imgoffset;
2122 override->override_size = sizeof(ventoy_iso9660_override);
2123
2124 dirent = (ventoy_iso9660_override *)(override->override_data);
2125 dirent->first_sector = (grub_uint32_t)((file->size + 2047) / 2048);
2126 dirent->size = (grub_uint32_t)(wimsize);
2127 dirent->first_sector_be = grub_swap_bytes32(dirent->first_sector);
2128 dirent->size_be = grub_swap_bytes32(dirent->size);
2129
2130 debug("imgoffset=%u first_sector=0x%x size=0x%x\n", imgoffset, dirent->first_sector, dirent->size);
2131
2132 if (ventoy_is_efi_os() == 0)
2133 {
2134 ventoy_windows_drive_map(chain);
2135 }
2136
2137 grub_file_close(file);
2138
2139 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
2140 }
2141
2142 int ventoy_chain_file_size(const char *path)
2143 {
2144 int size;
2145 grub_file_t file;
2146
2147 file = grub_file_open(path, VENTOY_FILE_TYPE);
2148 size = (int)(file->size);
2149
2150 grub_file_close(file);
2151
2152 return size;
2153 }
2154
2155 int ventoy_chain_file_read(const char *path, int offset, int len, void *buf)
2156 {
2157 int size;
2158 grub_file_t file;
2159
2160 file = grub_file_open(path, VENTOY_FILE_TYPE);
2161 grub_file_seek(file, offset);
2162 size = grub_file_read(file, buf, len);
2163 grub_file_close(file);
2164
2165 return size;
2166 }
2167