]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_vhd.c
41452b9f77f5d1118623167afcabbe4b275ea720
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / ventoy / ventoy_vhd.c
1 /******************************************************************************
2 * ventoy_vhd.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/charset.h>
39 #ifdef GRUB_MACHINE_EFI
40 #include <grub/efi/efi.h>
41 #endif
42 #include <grub/ventoy.h>
43 #include "ventoy_def.h"
44
45 GRUB_MOD_LICENSE ("GPLv3+");
46
47 static int g_vhdboot_isolen = 0;
48 static char *g_vhdboot_totbuf = NULL;
49 static char *g_vhdboot_isobuf = NULL;
50 static grub_uint64_t g_img_trim_head_secnum = 0;
51
52 static int ventoy_vhd_find_bcd(int *bcdoffset, int *bcdlen, const char *path)
53 {
54 grub_uint32_t offset;
55 grub_file_t file;
56 char cmdbuf[128];
57
58 grub_snprintf(cmdbuf, sizeof(cmdbuf), "loopback vhdiso mem:0x%lx:size:%d", (ulong)g_vhdboot_isobuf, g_vhdboot_isolen);
59
60 grub_script_execute_sourcecode(cmdbuf);
61
62 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "(vhdiso)%s", path);
63 if (!file)
64 {
65 return 1;
66 }
67
68 grub_file_read(file, &offset, 4);
69 offset = (grub_uint32_t)grub_iso9660_get_last_read_pos(file);
70
71 *bcdoffset = (int)offset;
72 *bcdlen = (int)file->size;
73
74 debug("vhdiso bcd file offset:%d len:%d\n", *bcdoffset, *bcdlen);
75
76 grub_file_close(file);
77
78 grub_script_execute_sourcecode("loopback -d vhdiso");
79
80 return 0;
81 }
82
83 static int ventoy_vhd_patch_path(char *vhdpath, ventoy_patch_vhd *patch1, ventoy_patch_vhd *patch2,
84 int bcdoffset, int bcdlen)
85 {
86 int i;
87 int cnt = 0;
88 char *pos;
89 grub_size_t pathlen;
90 const char *plat;
91 char *newpath = NULL;
92 grub_uint16_t *unicode_path;
93 const grub_uint8_t winloadexe[] =
94 {
95 0x77, 0x00, 0x69, 0x00, 0x6E, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x61, 0x00, 0x64, 0x00, 0x2E, 0x00,
96 0x65, 0x00, 0x78, 0x00, 0x65, 0x00
97 };
98
99 while ((*vhdpath) != '/')
100 {
101 vhdpath++;
102 }
103
104 pathlen = sizeof(grub_uint16_t) * (grub_strlen(vhdpath) + 1);
105 debug("unicode path for <%s> len:%d\n", vhdpath, (int)pathlen);
106
107 unicode_path = grub_zalloc(pathlen);
108 if (!unicode_path)
109 {
110 return 0;
111 }
112
113 plat = grub_env_get("grub_platform");
114
115 if (plat && (plat[0] == 'e')) /* UEFI */
116 {
117 pos = g_vhdboot_isobuf + bcdoffset;
118
119 /* winload.exe ==> winload.efi */
120 for (i = 0; i + (int)sizeof(winloadexe) < bcdlen; i++)
121 {
122 if (*((grub_uint32_t *)(pos + i)) == 0x00690077 &&
123 grub_memcmp(pos + i, winloadexe, sizeof(winloadexe)) == 0)
124 {
125 pos[i + sizeof(winloadexe) - 4] = 0x66;
126 pos[i + sizeof(winloadexe) - 2] = 0x69;
127 cnt++;
128 }
129 }
130
131 debug("winload patch %d times\n", cnt);
132 }
133
134 newpath = grub_strdup(vhdpath);
135 for (pos = newpath; *pos; pos++)
136 {
137 if (*pos == '/')
138 {
139 *pos = '\\';
140 }
141 }
142
143 grub_utf8_to_utf16(unicode_path, pathlen, (grub_uint8_t *)newpath, -1, NULL);
144 grub_memcpy(patch1->vhd_file_path, unicode_path, pathlen);
145 grub_memcpy(patch2->vhd_file_path, unicode_path, pathlen);
146
147 grub_free(newpath);
148 return 0;
149 }
150
151 static int ventoy_vhd_read_parttbl(const char *filename, ventoy_gpt_info *gpt, int *index, grub_uint64_t *poffset)
152 {
153 int i;
154 int find = 0;
155 int ret = 1;
156 grub_uint64_t start;
157 grub_file_t file = NULL;
158 grub_disk_t disk = NULL;
159 grub_uint8_t zeroguid[16] = {0};
160
161 file = grub_file_open(filename, VENTOY_FILE_TYPE);
162 if (!file)
163 {
164 goto end;
165 }
166
167 disk = grub_disk_open(file->device->disk->name);
168 if (!disk)
169 {
170 goto end;
171 }
172
173 grub_disk_read(disk, 0, 0, sizeof(ventoy_gpt_info), gpt);
174
175 start = file->device->disk->partition->start;
176
177 if (grub_memcmp(gpt->Head.Signature, "EFI PART", 8) == 0)
178 {
179 debug("GPT part start: %llu\n", (ulonglong)start);
180 for (i = 0; i < 128; i++)
181 {
182 if (grub_memcmp(gpt->PartTbl[i].PartGuid, zeroguid, 16))
183 {
184 if (start == gpt->PartTbl[i].StartLBA)
185 {
186 *index = i;
187 find = 1;
188 break;
189 }
190 }
191 }
192 }
193 else
194 {
195 debug("MBR part start: %llu\n", (ulonglong)start);
196 for (i = 0; i < 4; i++)
197 {
198 if ((grub_uint32_t)start == gpt->MBR.PartTbl[i].StartSectorId)
199 {
200 *index = i;
201 find = 1;
202 break;
203 }
204 }
205 }
206
207 if (find == 0) // MBR Logical partition
208 {
209 if (file->device->disk->partition->number > 0)
210 {
211 *index = file->device->disk->partition->number;
212 debug("Fall back part number: %d\n", *index);
213 }
214 }
215
216 *poffset = start;
217 ret = 0;
218
219 end:
220 check_free(file, grub_file_close);
221 check_free(disk, grub_disk_close);
222
223 return ret;
224 }
225
226 static int ventoy_vhd_patch_disk(const char *vhdpath, ventoy_patch_vhd *patch1, ventoy_patch_vhd *patch2)
227 {
228 int partIndex = 0;
229 grub_uint64_t offset = 0;
230 char efipart[16] = {0};
231 ventoy_gpt_info *gpt = NULL;
232
233 if (vhdpath[0] == '/')
234 {
235 gpt = g_ventoy_part_info;
236 partIndex = 0;
237 debug("This is Ventoy ISO partIndex %d %s\n", partIndex, vhdpath);
238 }
239 else
240 {
241 gpt = grub_zalloc(sizeof(ventoy_gpt_info));
242 ventoy_vhd_read_parttbl(vhdpath, gpt, &partIndex, &offset);
243 debug("This is HDD partIndex %d %s\n", partIndex, vhdpath);
244 }
245
246 grub_memcpy(efipart, gpt->Head.Signature, sizeof(gpt->Head.Signature));
247
248 grub_memset(patch1, 0, OFFSET_OF(ventoy_patch_vhd, vhd_file_path));
249 grub_memset(patch2, 0, OFFSET_OF(ventoy_patch_vhd, vhd_file_path));
250
251 if (grub_strncmp(efipart, "EFI PART", 8) == 0)
252 {
253 ventoy_debug_dump_guid("GPT disk GUID: ", gpt->Head.DiskGuid);
254 ventoy_debug_dump_guid("GPT partIndex GUID: ", gpt->PartTbl[partIndex].PartGuid);
255
256 grub_memcpy(patch1->disk_signature_or_guid, gpt->Head.DiskGuid, 16);
257 grub_memcpy(patch1->part_offset_or_guid, gpt->PartTbl[partIndex].PartGuid, 16);
258 grub_memcpy(patch2->disk_signature_or_guid, gpt->Head.DiskGuid, 16);
259 grub_memcpy(patch2->part_offset_or_guid, gpt->PartTbl[partIndex].PartGuid, 16);
260
261 patch1->part_type = patch2->part_type = 0;
262 }
263 else
264 {
265 offset *= 512;
266 debug("MBR disk signature: %02x%02x%02x%02x Part(%d) offset:%llu\n",
267 gpt->MBR.BootCode[0x1b8 + 0], gpt->MBR.BootCode[0x1b8 + 1],
268 gpt->MBR.BootCode[0x1b8 + 2], gpt->MBR.BootCode[0x1b8 + 3],
269 partIndex + 1, offset);
270
271 grub_memcpy(patch1->part_offset_or_guid, &offset, 8);
272 grub_memcpy(patch2->part_offset_or_guid, &offset, 8);
273
274 grub_memcpy(patch1->disk_signature_or_guid, gpt->MBR.BootCode + 0x1b8, 4);
275 grub_memcpy(patch2->disk_signature_or_guid, gpt->MBR.BootCode + 0x1b8, 4);
276
277 patch1->part_type = patch2->part_type = 1;
278 }
279
280 if (gpt != g_ventoy_part_info)
281 {
282 grub_free(gpt);
283 }
284
285 return 0;
286 }
287
288 static int ventoy_find_vhdpatch_offset(int bcdoffset, int bcdlen, int *offset)
289 {
290 int i;
291 int cnt = 0;
292 grub_uint8_t *buf = (grub_uint8_t *)(g_vhdboot_isobuf + bcdoffset);
293 grub_uint8_t magic[16] = {
294 0x5C, 0x00, 0x58, 0x00, 0x58, 0x00, 0x58, 0x00, 0x58, 0x00, 0x58, 0x00, 0x58, 0x00, 0x58, 0x00
295 };
296
297 for (i = 0; i < bcdlen - 16 && cnt < 2; i++)
298 {
299 if (*(grub_uint32_t *)(buf + i) == 0x0058005C)
300 {
301 if (grub_memcmp(magic, buf + i, 16) == 0)
302 {
303 *offset++ = i - (int)OFFSET_OF(ventoy_patch_vhd, vhd_file_path);
304 cnt++;
305 }
306 }
307 }
308
309 return 0;
310 }
311
312 grub_err_t ventoy_cmd_patch_vhdboot(grub_extcmd_context_t ctxt, int argc, char **args)
313 {
314 int rc;
315 int bcdoffset, bcdlen;
316 int patchoffset[2];
317 ventoy_patch_vhd *patch1;
318 ventoy_patch_vhd *patch2;
319
320 (void)ctxt;
321 (void)argc;
322
323 grub_env_unset("vtoy_vhd_buf_addr");
324
325 debug("patch vhd <%s>\n", args[0]);
326
327 if ((!g_vhdboot_enable) || (!g_vhdboot_totbuf))
328 {
329 debug("vhd boot not ready %d %p\n", g_vhdboot_enable, g_vhdboot_totbuf);
330 return 0;
331 }
332
333 rc = ventoy_vhd_find_bcd(&bcdoffset, &bcdlen, "/boot/bcd");
334 if (rc)
335 {
336 debug("failed to get bcd location %d\n", rc);
337 }
338 else
339 {
340 ventoy_find_vhdpatch_offset(bcdoffset, bcdlen, patchoffset);
341 patch1 = (ventoy_patch_vhd *)(g_vhdboot_isobuf + bcdoffset + patchoffset[0]);
342 patch2 = (ventoy_patch_vhd *)(g_vhdboot_isobuf + bcdoffset + patchoffset[1]);
343
344 debug("Find /boot/bcd (%d %d) now patch it (offset: 0x%x 0x%x) ...\n",
345 bcdoffset, bcdlen, patchoffset[0], patchoffset[1]);
346 ventoy_vhd_patch_disk(args[0], patch1, patch2);
347 ventoy_vhd_patch_path(args[0], patch1, patch2, bcdoffset, bcdlen);
348 }
349
350 rc = ventoy_vhd_find_bcd(&bcdoffset, &bcdlen, "/boot/BCD");
351 if (rc)
352 {
353 debug("No file /boot/BCD \n");
354 }
355 else
356 {
357 ventoy_find_vhdpatch_offset(bcdoffset, bcdlen, patchoffset);
358 patch1 = (ventoy_patch_vhd *)(g_vhdboot_isobuf + bcdoffset + patchoffset[0]);
359 patch2 = (ventoy_patch_vhd *)(g_vhdboot_isobuf + bcdoffset + patchoffset[1]);
360
361 debug("Find /boot/BCD (%d %d) now patch it (offset: 0x%x 0x%x) ...\n",
362 bcdoffset, bcdlen, patchoffset[0], patchoffset[1]);
363 ventoy_vhd_patch_disk(args[0], patch1, patch2);
364 ventoy_vhd_patch_path(args[0], patch1, patch2, bcdoffset, bcdlen);
365 }
366
367 /* set buffer and size */
368 #ifdef GRUB_MACHINE_EFI
369 ventoy_memfile_env_set("vtoy_vhd_buf", g_vhdboot_totbuf, (ulonglong)(g_vhdboot_isolen + sizeof(ventoy_chain_head)));
370 #else
371 ventoy_memfile_env_set("vtoy_vhd_buf", g_vhdboot_isobuf, (ulonglong)g_vhdboot_isolen);
372 #endif
373
374 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
375 }
376
377 grub_err_t ventoy_cmd_load_vhdboot(grub_extcmd_context_t ctxt, int argc, char **args)
378 {
379 int buflen;
380 grub_file_t file;
381
382 (void)ctxt;
383 (void)argc;
384
385 g_vhdboot_enable = 0;
386 grub_check_free(g_vhdboot_totbuf);
387
388 file = grub_file_open(args[0], VENTOY_FILE_TYPE);
389 if (!file)
390 {
391 return 0;
392 }
393
394 debug("load vhd boot: <%s> <%lu>\n", args[0], (ulong)file->size);
395
396 if (file->size < VTOY_SIZE_1KB * 32)
397 {
398 grub_file_close(file);
399 return 0;
400 }
401
402 g_vhdboot_isolen = (int)file->size;
403
404 buflen = (int)(file->size + sizeof(ventoy_chain_head));
405
406 #ifdef GRUB_MACHINE_EFI
407 g_vhdboot_totbuf = (char *)grub_efi_allocate_iso_buf(buflen);
408 #else
409 g_vhdboot_totbuf = (char *)grub_malloc(buflen);
410 #endif
411
412 if (!g_vhdboot_totbuf)
413 {
414 grub_file_close(file);
415 return 0;
416 }
417
418 g_vhdboot_isobuf = g_vhdboot_totbuf + sizeof(ventoy_chain_head);
419
420 grub_file_read(file, g_vhdboot_isobuf, file->size);
421 grub_file_close(file);
422
423 g_vhdboot_enable = 1;
424
425 return 0;
426 }
427
428 static int ventoy_raw_trim_head(grub_uint64_t offset)
429 {
430 grub_uint32_t i;
431 grub_uint32_t memsize;
432 grub_uint32_t imgstart = 0;
433 grub_uint32_t imgsecs = 0;
434 grub_uint64_t sectors = 0;
435 grub_uint64_t cursecs = 0;
436 grub_uint64_t delta = 0;
437
438 if ((!g_img_chunk_list.chunk) || (!offset))
439 {
440 debug("image chunk not ready %p %lu\n", g_img_chunk_list.chunk, (ulong)offset);
441 return 0;
442 }
443
444 debug("image trim head %lu\n", (ulong)offset);
445
446 for (i = 0; i < g_img_chunk_list.cur_chunk; i++)
447 {
448 cursecs = g_img_chunk_list.chunk[i].disk_end_sector + 1 - g_img_chunk_list.chunk[i].disk_start_sector;
449 sectors += cursecs;
450 if (sectors >= offset)
451 {
452 delta = cursecs - (sectors - offset);
453 break;
454 }
455 }
456
457 if (sectors < offset || i >= g_img_chunk_list.cur_chunk)
458 {
459 debug("Invalid size %lu %lu\n", (ulong)sectors, (ulong)offset);
460 return 0;
461 }
462
463 if (sectors == offset)
464 {
465 memsize = (g_img_chunk_list.cur_chunk - (i + 1)) * sizeof(ventoy_img_chunk);
466 grub_memmove(g_img_chunk_list.chunk, g_img_chunk_list.chunk + i + 1, memsize);
467 g_img_chunk_list.cur_chunk -= (i + 1);
468 }
469 else
470 {
471 g_img_chunk_list.chunk[i].disk_start_sector += delta;
472 g_img_chunk_list.chunk[i].img_start_sector += (grub_uint32_t)(delta / 4);
473
474 if (i > 0)
475 {
476 memsize = (g_img_chunk_list.cur_chunk - i) * sizeof(ventoy_img_chunk);
477 grub_memmove(g_img_chunk_list.chunk, g_img_chunk_list.chunk + i, memsize);
478 g_img_chunk_list.cur_chunk -= i;
479 }
480 }
481
482 for (i = 0; i < g_img_chunk_list.cur_chunk; i++)
483 {
484 imgsecs = g_img_chunk_list.chunk[i].img_end_sector + 1 - g_img_chunk_list.chunk[i].img_start_sector;
485 g_img_chunk_list.chunk[i].img_start_sector = imgstart;
486 g_img_chunk_list.chunk[i].img_end_sector = imgstart + (imgsecs - 1);
487 imgstart += imgsecs;
488 }
489
490 return 0;
491 }
492
493 grub_err_t ventoy_cmd_get_vtoy_type(grub_extcmd_context_t ctxt, int argc, char **args)
494 {
495 int i;
496 int altboot = 0;
497 int offset = -1;
498 grub_file_t file;
499 grub_uint8_t data = 0;
500 vhd_footer_t vhdfoot;
501 VDIPREHEADER vdihdr;
502 char type[16] = {0};
503 ventoy_gpt_info *gpt;
504
505 (void)ctxt;
506
507 g_img_trim_head_secnum = 0;
508
509 if (argc != 4)
510 {
511 return 0;
512 }
513
514 file = grub_file_open(args[0], VENTOY_FILE_TYPE);
515 if (!file)
516 {
517 debug("Failed to open file %s\n", args[0]);
518 return 0;
519 }
520
521 grub_snprintf(type, sizeof(type), "unknown");
522
523 grub_file_seek(file, file->size - 512);
524 grub_file_read(file, &vhdfoot, sizeof(vhdfoot));
525
526 if (grub_strncmp(vhdfoot.cookie, "conectix", 8) == 0)
527 {
528 offset = 0;
529 grub_snprintf(type, sizeof(type), "vhd%u", grub_swap_bytes32(vhdfoot.disktype));
530 }
531 else
532 {
533 grub_file_seek(file, 0);
534 grub_file_read(file, &vdihdr, sizeof(vdihdr));
535 if (vdihdr.u32Signature == VDI_IMAGE_SIGNATURE &&
536 grub_strncmp(vdihdr.szFileInfo, VDI_IMAGE_FILE_INFO, grub_strlen(VDI_IMAGE_FILE_INFO)) == 0)
537 {
538 offset = 2 * 1048576;
539 g_img_trim_head_secnum = offset / 512;
540 grub_snprintf(type, sizeof(type), "vdi");
541 }
542 else
543 {
544 offset = 0;
545 grub_snprintf(type, sizeof(type), "raw");
546 }
547 }
548
549 grub_env_set(args[1], type);
550 debug("<%s> vtoy type: <%s> offset:%d\n", args[0], type, offset);
551
552 if (offset >= 0)
553 {
554 gpt = grub_zalloc(sizeof(ventoy_gpt_info));
555 if (!gpt)
556 {
557 grub_env_set(args[1], "unknown");
558 goto end;
559 }
560
561 grub_file_seek(file, offset);
562 grub_file_read(file, gpt, sizeof(ventoy_gpt_info));
563
564 if (gpt->MBR.Byte55 != 0x55 || gpt->MBR.ByteAA != 0xAA)
565 {
566 grub_env_set(args[1], "unknown");
567 debug("invalid mbr signature: 0x%x 0x%x\n", gpt->MBR.Byte55, gpt->MBR.ByteAA);
568 goto end;
569 }
570
571 if (grub_memcmp(gpt->Head.Signature, "EFI PART", 8) == 0)
572 {
573 grub_env_set(args[2], "gpt");
574 debug("part type: %s\n", "GPT");
575
576 if (gpt->MBR.PartTbl[0].FsFlag == 0xEE)
577 {
578 for (i = 0; i < 128; i++)
579 {
580 if (grub_memcmp(gpt->PartTbl[i].PartType, "Hah!IdontNeedEFI", 16) == 0)
581 {
582 debug("part %d is grub_bios part\n", i);
583 altboot = 1;
584 grub_env_set(args[3], "1");
585 break;
586 }
587 else if (gpt->PartTbl[i].LastLBA == 0)
588 {
589 break;
590 }
591 }
592 }
593
594 if (!altboot)
595 {
596 if (gpt->MBR.BootCode[92] == 0x22)
597 {
598 grub_file_seek(file, offset + 17908);
599 grub_file_read(file, &data, 1);
600 if (data == 0x23)
601 {
602 altboot = 1;
603 grub_env_set(args[3], "1");
604 }
605 else
606 {
607 debug("offset data=0x%x\n", data);
608 }
609 }
610 else
611 {
612 debug("BootCode: 0x%x\n", gpt->MBR.BootCode[92]);
613 }
614 }
615 }
616 else
617 {
618 grub_env_set(args[2], "mbr");
619 debug("part type: %s\n", "MBR");
620
621 for (i = 0; i < 4; i++)
622 {
623 if (gpt->MBR.PartTbl[i].FsFlag == 0xEF)
624 {
625 debug("part %d is esp part in MBR mode\n", i);
626 altboot = 1;
627 grub_env_set(args[3], "1");
628 break;
629 }
630 }
631 }
632 }
633 else
634 {
635 debug("part type: %s\n", "xxx");
636 }
637
638 end:
639 grub_check_free(gpt);
640 grub_file_close(file);
641 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
642 }
643
644 grub_err_t ventoy_cmd_raw_chain_data(grub_extcmd_context_t ctxt, int argc, char **args)
645 {
646 grub_uint32_t size = 0;
647 grub_uint32_t img_chunk_size = 0;
648 grub_file_t file;
649 grub_disk_t disk;
650 const char *pLastChain = NULL;
651 ventoy_chain_head *chain;
652
653 (void)ctxt;
654 (void)argc;
655
656 if (NULL == g_img_chunk_list.chunk)
657 {
658 grub_printf("ventoy not ready\n");
659 return 1;
660 }
661
662 if (g_img_trim_head_secnum > 0)
663 {
664 ventoy_raw_trim_head(g_img_trim_head_secnum);
665 }
666
667 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", args[0]);
668 if (!file)
669 {
670 return 1;
671 }
672
673 if (grub_strncmp(args[0], g_iso_path, grub_strlen(g_iso_path)))
674 {
675 file->vlnk = 1;
676 }
677
678 img_chunk_size = g_img_chunk_list.cur_chunk * sizeof(ventoy_img_chunk);
679
680 size = sizeof(ventoy_chain_head) + img_chunk_size;
681
682 pLastChain = grub_env_get("vtoy_chain_mem_addr");
683 if (pLastChain)
684 {
685 chain = (ventoy_chain_head *)grub_strtoul(pLastChain, NULL, 16);
686 if (chain)
687 {
688 debug("free last chain memory %p\n", chain);
689 grub_free(chain);
690 }
691 }
692
693 chain = ventoy_alloc_chain(size);
694 if (!chain)
695 {
696 grub_printf("Failed to alloc chain raw memory size %u\n", size);
697 grub_file_close(file);
698 return 1;
699 }
700
701 ventoy_memfile_env_set("vtoy_chain_mem", chain, (ulonglong)size);
702
703 grub_env_export("vtoy_chain_mem_addr");
704 grub_env_export("vtoy_chain_mem_size");
705
706 grub_memset(chain, 0, sizeof(ventoy_chain_head));
707
708 /* part 1: os parameter */
709 g_ventoy_chain_type = ventoy_chain_linux;
710 ventoy_fill_os_param(file, &(chain->os_param));
711
712 /* part 2: chain head */
713 disk = file->device->disk;
714 chain->disk_drive = disk->id;
715 chain->disk_sector_size = (1 << disk->log_sector_size);
716
717 chain->real_img_size_in_bytes = file->size;
718 if (g_img_trim_head_secnum > 0)
719 {
720 chain->real_img_size_in_bytes -= g_img_trim_head_secnum * 512;
721 }
722
723 chain->virt_img_size_in_bytes = chain->real_img_size_in_bytes;
724 chain->boot_catalog = 0;
725
726 /* part 3: image chunk */
727 chain->img_chunk_offset = sizeof(ventoy_chain_head);
728 chain->img_chunk_num = g_img_chunk_list.cur_chunk;
729 grub_memcpy((char *)chain + chain->img_chunk_offset, g_img_chunk_list.chunk, img_chunk_size);
730
731 grub_file_seek(file, g_img_trim_head_secnum * 512);
732 grub_file_read(file, chain->boot_catalog_sector, 512);
733
734 grub_file_close(file);
735
736 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
737 }