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