]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
5242cb0332d0c7834fdcf475b62df8a9aac06a4e
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / ventoy / ventoy.c
1 /******************************************************************************
2 * ventoy.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/net.h>
35 #include <grub/misc.h>
36 #include <grub/kernel.h>
37 #include <grub/time.h>
38 #include <grub/memory.h>
39 #ifdef GRUB_MACHINE_EFI
40 #include <grub/efi/efi.h>
41 #include <grub/efi/memory.h>
42 #endif
43 #include <grub/ventoy.h>
44 #include "ventoy_def.h"
45
46 GRUB_MOD_LICENSE ("GPLv3+");
47
48 int g_ventoy_debug = 0;
49 static int g_efi_os = 0xFF;
50 grub_uint32_t g_ventoy_plat_data;
51
52 void ventoy_debug(const char *fmt, ...)
53 {
54 va_list args;
55
56 va_start (args, fmt);
57 grub_vprintf (fmt, args);
58 va_end (args);
59 }
60
61 void ventoy_str_tolower(char *str)
62 {
63 while (*str)
64 {
65 *str = grub_tolower(*str);
66 str++;
67 }
68 }
69
70 void ventoy_str_toupper(char *str)
71 {
72 while (*str)
73 {
74 *str = grub_toupper(*str);
75 str++;
76 }
77 }
78
79 char *ventoy_str_last(char *str, char ch)
80 {
81 char *pos = NULL;
82 char *last = NULL;
83
84 if (!str)
85 {
86 return NULL;
87 }
88
89 for (pos = str; *pos; pos++)
90 {
91 if (*pos == ch)
92 {
93 last = pos;
94 }
95 }
96
97 return last;
98 }
99
100 int ventoy_str_all_digit(const char *str)
101 {
102 if (NULL == str || 0 == *str)
103 {
104 return 0;
105 }
106
107 while (*str)
108 {
109 if (*str < '0' || *str > '9')
110 {
111 return 0;
112 }
113 }
114
115 return 1;
116 }
117
118 int ventoy_strcmp(const char *pattern, const char *str)
119 {
120 while (*pattern && *str)
121 {
122 if ((*pattern != *str) && (*pattern != '*'))
123 break;
124
125 pattern++;
126 str++;
127 }
128
129 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
130 }
131
132 int ventoy_strncmp (const char *pattern, const char *str, grub_size_t n)
133 {
134 if (n == 0)
135 return 0;
136
137 while (*pattern && *str && --n)
138 {
139 if ((*pattern != *str) && (*pattern != '*'))
140 break;
141
142 pattern++;
143 str++;
144 }
145
146 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
147 }
148
149 void ventoy_debug_dump_guid(const char *prefix, grub_uint8_t *guid)
150 {
151 int i;
152
153 if (!g_ventoy_debug)
154 {
155 return;
156 }
157
158 debug("%s", prefix);
159 for (i = 0; i < 16; i++)
160 {
161 grub_printf("%02x ", guid[i]);
162 }
163 grub_printf("\n");
164 }
165
166 int ventoy_is_efi_os(void)
167 {
168 if (g_efi_os > 1)
169 {
170 g_efi_os = (grub_strstr(GRUB_PLATFORM, "efi")) ? 1 : 0;
171 }
172
173 return g_efi_os;
174 }
175
176 void * ventoy_alloc_chain(grub_size_t size)
177 {
178 void *p = NULL;
179
180 p = grub_malloc(size);
181 #ifdef GRUB_MACHINE_EFI
182 if (!p)
183 {
184 p = grub_efi_allocate_any_pages(GRUB_EFI_BYTES_TO_PAGES(size));
185 }
186 #endif
187
188 return p;
189 }
190
191 void ventoy_memfile_env_set(const char *prefix, const void *buf, unsigned long long len)
192 {
193 char name[128];
194 char val[64];
195
196 grub_snprintf(name, sizeof(name), "%s_addr", prefix);
197 grub_snprintf(val, sizeof(val), "0x%llx", (ulonglong)(ulong)buf);
198 grub_env_set(name, val);
199
200 grub_snprintf(name, sizeof(name), "%s_size", prefix);
201 grub_snprintf(val, sizeof(val), "%llu", len);
202 grub_env_set(name, val);
203
204 return;
205 }
206
207 static int ventoy_arch_mode_init(void)
208 {
209 #ifdef GRUB_MACHINE_EFI
210 if (grub_strcmp(GRUB_TARGET_CPU, "i386") == 0)
211 {
212 g_ventoy_plat_data = VTOY_PLAT_I386_UEFI;
213 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "ia32");
214 }
215 else if (grub_strcmp(GRUB_TARGET_CPU, "arm64") == 0)
216 {
217 g_ventoy_plat_data = VTOY_PLAT_ARM64_UEFI;
218 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "aa64");
219 }
220 else if (grub_strcmp(GRUB_TARGET_CPU, "mips64el") == 0)
221 {
222 g_ventoy_plat_data = VTOY_PLAT_MIPS_UEFI;
223 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "mips");
224 }
225 else
226 {
227 g_ventoy_plat_data = VTOY_PLAT_X86_64_UEFI;
228 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "uefi");
229 }
230 #else
231 g_ventoy_plat_data = VTOY_PLAT_X86_LEGACY;
232 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "legacy");
233 #endif
234
235 return 0;
236 }
237
238 #ifdef GRUB_MACHINE_EFI
239 static void ventoy_get_uefi_version(char *str, grub_size_t len)
240 {
241 grub_efi_uint8_t uefi_minor_1, uefi_minor_2;
242
243 uefi_minor_1 = (grub_efi_system_table->hdr.revision & 0xffff) / 10;
244 uefi_minor_2 = (grub_efi_system_table->hdr.revision & 0xffff) % 10;
245 grub_snprintf(str, len, "%d.%d", (grub_efi_system_table->hdr.revision >> 16), uefi_minor_1);
246 if (uefi_minor_2)
247 grub_snprintf(str, len, "%s.%d", str, uefi_minor_2);
248 }
249 #endif
250
251 static int ventoy_calc_totalmem(grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type, void *data)
252 {
253 grub_uint64_t *total_mem = (grub_uint64_t *)data;
254
255 (void)addr;
256 (void)type;
257
258 *total_mem += size;
259
260 return 0;
261 }
262
263 static int ventoy_hwinfo_init(void)
264 {
265 char str[256];
266 grub_uint64_t total_mem = 0;
267
268 grub_machine_mmap_iterate(ventoy_calc_totalmem, &total_mem);
269
270 grub_snprintf(str, sizeof(str), "%ld", (long)(total_mem / VTOY_SIZE_1MB));
271 ventoy_env_export("grub_total_ram", str);
272
273 #ifdef GRUB_MACHINE_EFI
274 ventoy_get_uefi_version(str, sizeof(str));
275 ventoy_env_export("grub_uefi_version", str);
276 #else
277 ventoy_env_export("grub_uefi_version", "NA");
278 #endif
279
280 return 0;
281 }
282
283 static global_var_cfg g_global_vars[] =
284 {
285 { "gfxmode", "1024x768", NULL },
286 { ventoy_left_key, "5%", NULL },
287 { ventoy_top_key, "95%", NULL },
288 { ventoy_color_key, "#0000ff", NULL },
289 { NULL, NULL, NULL }
290 };
291
292 static const char * ventoy_global_var_read_hook(struct grub_env_var *var, const char *val)
293 {
294 int i;
295
296 for (i = 0; g_global_vars[i].name; i++)
297 {
298 if (grub_strcmp(g_global_vars[i].name, var->name) == 0)
299 {
300 return g_global_vars[i].value;
301 }
302 }
303
304 return val;
305 }
306
307 static char * ventoy_global_var_write_hook(struct grub_env_var *var, const char *val)
308 {
309 int i;
310
311 for (i = 0; g_global_vars[i].name; i++)
312 {
313 if (grub_strcmp(g_global_vars[i].name, var->name) == 0)
314 {
315 grub_check_free(g_global_vars[i].value);
316 g_global_vars[i].value = grub_strdup(val);
317 break;
318 }
319 }
320
321 return grub_strdup(val);
322 }
323
324 int ventoy_global_var_init(void)
325 {
326 int i;
327
328 for (i = 0; g_global_vars[i].name; i++)
329 {
330 g_global_vars[i].value = grub_strdup(g_global_vars[i].defval);
331 ventoy_env_export(g_global_vars[i].name, g_global_vars[i].defval);
332 grub_register_variable_hook(g_global_vars[i].name, ventoy_global_var_read_hook, ventoy_global_var_write_hook);
333 }
334
335 return 0;
336 }
337
338 static ctrl_var_cfg g_ctrl_vars[] =
339 {
340 { "VTOY_WIN11_BYPASS_CHECK", 1 },
341 { "VTOY_WIN11_BYPASS_NRO", 1 },
342 { "VTOY_LINUX_REMOUNT", 0 },
343 { "VTOY_SECONDARY_BOOT_MENU", 1 },
344 { NULL, 0 }
345 };
346
347 static const char * ventoy_ctrl_var_read_hook(struct grub_env_var *var, const char *val)
348 {
349 int i;
350
351 for (i = 0; g_ctrl_vars[i].name; i++)
352 {
353 if (grub_strcmp(g_ctrl_vars[i].name, var->name) == 0)
354 {
355 return g_ctrl_vars[i].value ? "1" : "0";
356 }
357 }
358
359 return val;
360 }
361
362 static char * ventoy_ctrl_var_write_hook(struct grub_env_var *var, const char *val)
363 {
364 int i;
365
366 for (i = 0; g_ctrl_vars[i].name; i++)
367 {
368 if (grub_strcmp(g_ctrl_vars[i].name, var->name) == 0)
369 {
370 if (val && val[0] == '1' && val[1] == 0)
371 {
372 g_ctrl_vars[i].value = 1;
373 return grub_strdup("1");
374 }
375 else
376 {
377 g_ctrl_vars[i].value = 0;
378 return grub_strdup("0");
379 }
380 }
381 }
382
383 return grub_strdup(val);
384 }
385
386 int ventoy_ctrl_var_init(void)
387 {
388 int i;
389
390 for (i = 0; g_ctrl_vars[i].name; i++)
391 {
392 ventoy_env_export(g_ctrl_vars[i].name, g_ctrl_vars[i].value ? "1" : "0");
393 grub_register_variable_hook(g_ctrl_vars[i].name, ventoy_ctrl_var_read_hook, ventoy_ctrl_var_write_hook);
394 }
395
396 return 0;
397 }
398
399 GRUB_MOD_INIT(ventoy)
400 {
401 ventoy_hwinfo_init();
402 ventoy_env_init();
403 ventoy_arch_mode_init();
404 ventoy_register_all_cmd();
405 }
406
407 GRUB_MOD_FINI(ventoy)
408 {
409 ventoy_unregister_all_cmd();
410 }
411