]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_vhd.c
98e83842aad588e869240e8af38757e50abf74e0
[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_bcd_offset = 0;
48 static int g_vhdboot_bcd_len = 0;
49 static int g_vhdboot_isolen = 0;
50 static char *g_vhdboot_totbuf = NULL;
51 static char *g_vhdboot_isobuf = NULL;
52
53 static int ventoy_vhd_find_bcd(int *bcdoffset, int *bcdlen)
54 {
55 grub_uint32_t offset;
56 grub_file_t file;
57 char cmdbuf[128];
58
59 grub_snprintf(cmdbuf, sizeof(cmdbuf), "loopback vhdiso mem:0x%lx:size:%d", (ulong)g_vhdboot_isobuf, g_vhdboot_isolen);
60
61 grub_script_execute_sourcecode(cmdbuf);
62
63 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", "(vhdiso)/boot/bcd");
64 if (!file)
65 {
66 grub_printf("Failed to open bcd file in the image file\n");
67 return 1;
68 }
69
70 grub_file_read(file, &offset, 4);
71 offset = (grub_uint32_t)grub_iso9660_get_last_read_pos(file);
72
73 *bcdoffset = (int)offset;
74 *bcdlen = (int)file->size;
75
76 debug("vhdiso bcd file offset:%d len:%d\n", *bcdoffset, *bcdlen);
77
78 grub_file_close(file);
79
80 grub_script_execute_sourcecode("loopback -d vhdiso");
81
82 return 0;
83 }
84
85 static int ventoy_vhd_patch_path(char *vhdpath, ventoy_patch_vhd *patch1, ventoy_patch_vhd *patch2)
86 {
87 int i;
88 int cnt = 0;
89 char *pos;
90 grub_size_t pathlen;
91 const char *plat;
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 pathlen = sizeof(grub_uint16_t) * (grub_strlen(vhdpath) + 1);
100 debug("unicode path for <%s> len:%d\n", vhdpath, (int)pathlen);
101
102 unicode_path = grub_zalloc(pathlen);
103 if (!unicode_path)
104 {
105 return 0;
106 }
107
108 plat = grub_env_get("grub_platform");
109
110 if (plat && (plat[0] == 'e')) /* UEFI */
111 {
112 pos = g_vhdboot_isobuf + g_vhdboot_bcd_offset;
113
114 /* winload.exe ==> winload.efi */
115 for (i = 0; i + (int)sizeof(winloadexe) < g_vhdboot_bcd_len; i++)
116 {
117 if (*((grub_uint32_t *)(pos + i)) == 0x00690077 &&
118 grub_memcmp(pos + i, winloadexe, sizeof(winloadexe)) == 0)
119 {
120 pos[i + sizeof(winloadexe) - 4] = 0x66;
121 pos[i + sizeof(winloadexe) - 2] = 0x69;
122 cnt++;
123 }
124 }
125
126 debug("winload patch %d times\n", cnt);
127 }
128
129 for (pos = vhdpath; *pos; pos++)
130 {
131 if (*pos == '/')
132 {
133 *pos = '\\';
134 }
135 }
136
137 grub_utf8_to_utf16(unicode_path, pathlen, (grub_uint8_t *)vhdpath, -1, NULL);
138 grub_memcpy(patch1->vhd_file_path, unicode_path, pathlen);
139 grub_memcpy(patch2->vhd_file_path, unicode_path, pathlen);
140
141 return 0;
142 }
143
144 static int ventoy_vhd_patch_disk(ventoy_patch_vhd *patch1, ventoy_patch_vhd *patch2)
145 {
146 char efipart[16] = {0};
147
148 grub_memcpy(efipart, g_ventoy_part_info->Head.Signature, sizeof(g_ventoy_part_info->Head.Signature));
149
150 debug("part1 type: 0x%x <%s>\n", g_ventoy_part_info->MBR.PartTbl[0].FsFlag, efipart);
151
152 if (grub_strncmp(efipart, "EFI PART", 8) == 0)
153 {
154 ventoy_debug_dump_guid("GPT disk GUID: ", g_ventoy_part_info->Head.DiskGuid);
155 ventoy_debug_dump_guid("GPT part GUID: ", g_ventoy_part_info->PartTbl[0].PartGuid);
156
157 grub_memcpy(patch1->disk_signature_or_guid, g_ventoy_part_info->Head.DiskGuid, 16);
158 grub_memcpy(patch1->part_offset_or_guid, g_ventoy_part_info->PartTbl[0].PartGuid, 16);
159 grub_memcpy(patch2->disk_signature_or_guid, g_ventoy_part_info->Head.DiskGuid, 16);
160 grub_memcpy(patch2->part_offset_or_guid, g_ventoy_part_info->PartTbl[0].PartGuid, 16);
161
162 patch1->part_type = patch2->part_type = 0;
163 }
164 else
165 {
166 debug("MBR disk signature: %02x%02x%02x%02x\n",
167 g_ventoy_part_info->MBR.BootCode[0x1b8 + 0], g_ventoy_part_info->MBR.BootCode[0x1b8 + 1],
168 g_ventoy_part_info->MBR.BootCode[0x1b8 + 2], g_ventoy_part_info->MBR.BootCode[0x1b8 + 3]);
169 grub_memcpy(patch1->disk_signature_or_guid, g_ventoy_part_info->MBR.BootCode + 0x1b8, 4);
170 grub_memcpy(patch2->disk_signature_or_guid, g_ventoy_part_info->MBR.BootCode + 0x1b8, 4);
171 }
172
173 return 0;
174 }
175
176 grub_err_t ventoy_cmd_patch_vhdboot(grub_extcmd_context_t ctxt, int argc, char **args)
177 {
178 int rc;
179 ventoy_patch_vhd *patch1;
180 ventoy_patch_vhd *patch2;
181 char envbuf[64];
182
183 (void)ctxt;
184 (void)argc;
185
186 grub_env_unset("vtoy_vhd_buf_addr");
187
188 debug("patch vhd <%s>\n", args[0]);
189
190 if ((!g_vhdboot_enable) || (!g_vhdboot_totbuf))
191 {
192 debug("vhd boot not ready %d %p\n", g_vhdboot_enable, g_vhdboot_totbuf);
193 return 0;
194 }
195
196 rc = ventoy_vhd_find_bcd(&g_vhdboot_bcd_offset, &g_vhdboot_bcd_len);
197 if (rc)
198 {
199 debug("failed to get bcd location %d\n", rc);
200 return 0;
201 }
202
203 patch1 = (ventoy_patch_vhd *)(g_vhdboot_isobuf + g_vhdboot_bcd_offset + 0x495a);
204 patch2 = (ventoy_patch_vhd *)(g_vhdboot_isobuf + g_vhdboot_bcd_offset + 0x50aa);
205
206 ventoy_vhd_patch_disk(patch1, patch2);
207 ventoy_vhd_patch_path(args[0], patch1, patch2);
208
209 /* set buffer and size */
210 #ifdef GRUB_MACHINE_EFI
211 grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (ulong)g_vhdboot_totbuf);
212 grub_env_set("vtoy_vhd_buf_addr", envbuf);
213 grub_snprintf(envbuf, sizeof(envbuf), "%d", (int)(g_vhdboot_isolen + sizeof(ventoy_chain_head)));
214 grub_env_set("vtoy_vhd_buf_size", envbuf);
215 #else
216 grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (ulong)g_vhdboot_isobuf);
217 grub_env_set("vtoy_vhd_buf_addr", envbuf);
218 grub_snprintf(envbuf, sizeof(envbuf), "%d", g_vhdboot_isolen);
219 grub_env_set("vtoy_vhd_buf_size", envbuf);
220 #endif
221
222 return 0;
223 }
224
225 grub_err_t ventoy_cmd_load_vhdboot(grub_extcmd_context_t ctxt, int argc, char **args)
226 {
227 int buflen;
228 grub_file_t file;
229
230 (void)ctxt;
231 (void)argc;
232
233 g_vhdboot_enable = 0;
234 grub_check_free(g_vhdboot_totbuf);
235
236 file = grub_file_open(args[0], VENTOY_FILE_TYPE);
237 if (!file)
238 {
239 return 0;
240 }
241
242 debug("load vhd boot: <%s> <%lu>\n", args[0], (ulong)file->size);
243
244 if (file->size < VTOY_SIZE_1KB * 32)
245 {
246 grub_file_close(file);
247 return 0;
248 }
249
250 g_vhdboot_isolen = (int)file->size;
251
252 buflen = (int)(file->size + sizeof(ventoy_chain_head));
253
254 #ifdef GRUB_MACHINE_EFI
255 g_vhdboot_totbuf = (char *)grub_efi_allocate_iso_buf(buflen);
256 #else
257 g_vhdboot_totbuf = (char *)grub_malloc(buflen);
258 #endif
259
260 if (!g_vhdboot_totbuf)
261 {
262 grub_file_close(file);
263 return 0;
264 }
265
266 g_vhdboot_isobuf = g_vhdboot_totbuf + sizeof(ventoy_chain_head);
267
268 grub_file_read(file, g_vhdboot_isobuf, file->size);
269 grub_file_close(file);
270
271 g_vhdboot_enable = 1;
272
273 return 0;
274 }
275