]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
1.1.07 release
[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_str_all_alnum(const char *str)
119 {
120 if (NULL == str || 0 == *str)
121 {
122 return 0;
123 }
124
125 while (*str)
126 {
127 if (!grub_isalnum(*str))
128 {
129 return 0;
130 }
131 }
132
133 return 1;
134 }
135
136 int ventoy_str_len_alnum(const char *str, int len)
137 {
138 int i;
139 int slen;
140
141 if (NULL == str || 0 == *str)
142 {
143 return 0;
144 }
145
146 slen = grub_strlen(str);
147 if (slen <= len)
148 {
149 return 0;
150 }
151
152 for (i = 0; i < len; i++)
153 {
154 if (!grub_isalnum(str[i]))
155 {
156 return 0;
157 }
158 }
159
160 if (str[len] == 0 || grub_isspace(str[len]))
161 {
162 return 1;
163 }
164
165 return 0;
166 }
167
168 char * ventoy_str_basename(char *path)
169 {
170 char *pos = NULL;
171
172 pos = grub_strrchr(path, '/');
173 if (pos)
174 {
175 pos++;
176 }
177 else
178 {
179 pos = path;
180 }
181
182 return pos;
183 }
184
185 int ventoy_str_chrcnt(const char *str, char c)
186 {
187 int n = 0;
188
189 if (str)
190 {
191 while (*str)
192 {
193 if (*str == c)
194 {
195 n++;
196 }
197 str++;
198 }
199 }
200
201 return n;
202 }
203
204 int ventoy_strcmp(const char *pattern, const char *str)
205 {
206 while (*pattern && *str)
207 {
208 if ((*pattern != *str) && (*pattern != '*'))
209 break;
210
211 pattern++;
212 str++;
213 }
214
215 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
216 }
217
218 int ventoy_strncmp (const char *pattern, const char *str, grub_size_t n)
219 {
220 if (n == 0)
221 return 0;
222
223 while (*pattern && *str && --n)
224 {
225 if ((*pattern != *str) && (*pattern != '*'))
226 break;
227
228 pattern++;
229 str++;
230 }
231
232 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
233 }
234
235 grub_err_t ventoy_env_int_set(const char *name, int value)
236 {
237 char buf[16];
238
239 grub_snprintf(buf, sizeof(buf), "%d", value);
240 return grub_env_set(name, buf);
241 }
242
243 void ventoy_debug_dump_guid(const char *prefix, grub_uint8_t *guid)
244 {
245 int i;
246
247 if (!g_ventoy_debug)
248 {
249 return;
250 }
251
252 debug("%s", prefix);
253 for (i = 0; i < 16; i++)
254 {
255 grub_printf("%02x ", guid[i]);
256 }
257 grub_printf("\n");
258 }
259
260 int ventoy_is_efi_os(void)
261 {
262 if (g_efi_os > 1)
263 {
264 g_efi_os = (grub_strstr(GRUB_PLATFORM, "efi")) ? 1 : 0;
265 }
266
267 return g_efi_os;
268 }
269
270 void * ventoy_alloc_chain(grub_size_t size)
271 {
272 void *p = NULL;
273
274 p = grub_malloc(size);
275 #ifdef GRUB_MACHINE_EFI
276 if (!p)
277 {
278 p = grub_efi_allocate_any_pages(GRUB_EFI_BYTES_TO_PAGES(size));
279 }
280 #endif
281
282 return p;
283 }
284
285 void ventoy_memfile_env_set(const char *prefix, const void *buf, unsigned long long len)
286 {
287 char name[128];
288 char val[64];
289
290 grub_snprintf(name, sizeof(name), "%s_addr", prefix);
291 grub_snprintf(val, sizeof(val), "0x%llx", (ulonglong)(ulong)buf);
292 grub_env_set(name, val);
293
294 grub_snprintf(name, sizeof(name), "%s_size", prefix);
295 grub_snprintf(val, sizeof(val), "%llu", len);
296 grub_env_set(name, val);
297
298 return;
299 }
300
301 static int ventoy_arch_mode_init(void)
302 {
303 #ifdef GRUB_MACHINE_EFI
304 if (grub_strcmp(GRUB_TARGET_CPU, "i386") == 0)
305 {
306 g_ventoy_plat_data = VTOY_PLAT_I386_UEFI;
307 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "ia32");
308 }
309 else if (grub_strcmp(GRUB_TARGET_CPU, "arm64") == 0)
310 {
311 g_ventoy_plat_data = VTOY_PLAT_ARM64_UEFI;
312 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "aa64");
313 }
314 else if (grub_strcmp(GRUB_TARGET_CPU, "mips64el") == 0)
315 {
316 g_ventoy_plat_data = VTOY_PLAT_MIPS_UEFI;
317 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "mips");
318 }
319 else
320 {
321 g_ventoy_plat_data = VTOY_PLAT_X86_64_UEFI;
322 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "uefi");
323 }
324 #else
325 g_ventoy_plat_data = VTOY_PLAT_X86_LEGACY;
326 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "legacy");
327 #endif
328
329 return 0;
330 }
331
332 #ifdef GRUB_MACHINE_EFI
333 static void ventoy_get_uefi_version(char *str, grub_size_t len)
334 {
335 grub_efi_uint8_t uefi_minor_1, uefi_minor_2;
336
337 uefi_minor_1 = (grub_efi_system_table->hdr.revision & 0xffff) / 10;
338 uefi_minor_2 = (grub_efi_system_table->hdr.revision & 0xffff) % 10;
339 grub_snprintf(str, len, "%d.%d", (grub_efi_system_table->hdr.revision >> 16), uefi_minor_1);
340 if (uefi_minor_2)
341 grub_snprintf(str, len, "%s.%d", str, uefi_minor_2);
342 }
343 #endif
344
345 static int ventoy_calc_totalmem(grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type, void *data)
346 {
347 grub_uint64_t *total_mem = (grub_uint64_t *)data;
348
349 (void)addr;
350 (void)type;
351
352 *total_mem += size;
353
354 return 0;
355 }
356
357 static int ventoy_hwinfo_init(void)
358 {
359 char str[256];
360 grub_uint64_t total_mem = 0;
361
362 grub_machine_mmap_iterate(ventoy_calc_totalmem, &total_mem);
363
364 grub_snprintf(str, sizeof(str), "%ld", (long)(total_mem / VTOY_SIZE_1MB));
365 ventoy_env_export("grub_total_ram", str);
366
367 #ifdef GRUB_MACHINE_EFI
368 ventoy_get_uefi_version(str, sizeof(str));
369 ventoy_env_export("grub_uefi_version", str);
370 #else
371 ventoy_env_export("grub_uefi_version", "NA");
372 #endif
373
374 return 0;
375 }
376
377 static global_var_cfg g_global_vars[] =
378 {
379 { "gfxmode", "1024x768", NULL },
380 { ventoy_left_key, "5%", NULL },
381 { ventoy_top_key, "95%", NULL },
382 { ventoy_color_key, "#0000ff", NULL },
383 { NULL, NULL, NULL }
384 };
385
386 static const char * ventoy_global_var_read_hook(struct grub_env_var *var, const char *val)
387 {
388 int i;
389
390 for (i = 0; g_global_vars[i].name; i++)
391 {
392 if (grub_strcmp(g_global_vars[i].name, var->name) == 0)
393 {
394 return g_global_vars[i].value;
395 }
396 }
397
398 return val;
399 }
400
401 static char * ventoy_global_var_write_hook(struct grub_env_var *var, const char *val)
402 {
403 int i;
404
405 for (i = 0; g_global_vars[i].name; i++)
406 {
407 if (grub_strcmp(g_global_vars[i].name, var->name) == 0)
408 {
409 grub_check_free(g_global_vars[i].value);
410 g_global_vars[i].value = grub_strdup(val);
411 break;
412 }
413 }
414
415 return grub_strdup(val);
416 }
417
418 int ventoy_global_var_init(void)
419 {
420 int i;
421
422 for (i = 0; g_global_vars[i].name; i++)
423 {
424 g_global_vars[i].value = grub_strdup(g_global_vars[i].defval);
425 ventoy_env_export(g_global_vars[i].name, g_global_vars[i].defval);
426 grub_register_variable_hook(g_global_vars[i].name, ventoy_global_var_read_hook, ventoy_global_var_write_hook);
427 }
428
429 return 0;
430 }
431
432 static ctrl_var_cfg g_ctrl_vars[] =
433 {
434 { "VTOY_WIN11_BYPASS_CHECK", 1 },
435 { "VTOY_WIN11_BYPASS_NRO", 1 },
436 { "VTOY_LINUX_REMOUNT", 0 },
437 { "VTOY_SECONDARY_BOOT_MENU", 1 },
438 { NULL, 0 }
439 };
440
441 static const char * ventoy_ctrl_var_read_hook(struct grub_env_var *var, const char *val)
442 {
443 int i;
444
445 for (i = 0; g_ctrl_vars[i].name; i++)
446 {
447 if (grub_strcmp(g_ctrl_vars[i].name, var->name) == 0)
448 {
449 return g_ctrl_vars[i].value ? "1" : "0";
450 }
451 }
452
453 return val;
454 }
455
456 static char * ventoy_ctrl_var_write_hook(struct grub_env_var *var, const char *val)
457 {
458 int i;
459
460 for (i = 0; g_ctrl_vars[i].name; i++)
461 {
462 if (grub_strcmp(g_ctrl_vars[i].name, var->name) == 0)
463 {
464 if (val && val[0] == '1' && val[1] == 0)
465 {
466 g_ctrl_vars[i].value = 1;
467 return grub_strdup("1");
468 }
469 else
470 {
471 g_ctrl_vars[i].value = 0;
472 return grub_strdup("0");
473 }
474 }
475 }
476
477 return grub_strdup(val);
478 }
479
480 int ventoy_ctrl_var_init(void)
481 {
482 int i;
483
484 for (i = 0; g_ctrl_vars[i].name; i++)
485 {
486 ventoy_env_export(g_ctrl_vars[i].name, g_ctrl_vars[i].value ? "1" : "0");
487 grub_register_variable_hook(g_ctrl_vars[i].name, ventoy_ctrl_var_read_hook, ventoy_ctrl_var_write_hook);
488 }
489
490 return 0;
491 }
492
493 GRUB_MOD_INIT(ventoy)
494 {
495 ventoy_hwinfo_init();
496 ventoy_env_init();
497 ventoy_arch_mode_init();
498 ventoy_register_all_cmd();
499 }
500
501 GRUB_MOD_FINI(ventoy)
502 {
503 ventoy_unregister_all_cmd();
504 }
505