1 /******************************************************************************
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
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.
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.
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/>.
21 #include <grub/types.h>
22 #include <grub/misc.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>
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>
43 #include <grub/ventoy.h>
44 #include "ventoy_def.h"
46 GRUB_MOD_LICENSE ("GPLv3+");
48 int g_ventoy_debug
= 0;
49 static int g_efi_os
= 0xFF;
50 grub_uint32_t g_ventoy_plat_data
;
52 void ventoy_debug(const char *fmt
, ...)
57 grub_vprintf (fmt
, args
);
61 void ventoy_str_tolower(char *str
)
65 *str
= grub_tolower(*str
);
70 void ventoy_str_toupper(char *str
)
74 *str
= grub_toupper(*str
);
79 char *ventoy_str_last(char *str
, char ch
)
89 for (pos
= str
; *pos
; pos
++)
100 int ventoy_str_all_digit(const char *str
)
102 if (NULL
== str
|| 0 == *str
)
109 if (*str
< '0' || *str
> '9')
118 int ventoy_str_all_alnum(const char *str
)
120 if (NULL
== str
|| 0 == *str
)
127 if (!grub_isalnum(*str
))
136 int ventoy_str_len_alnum(const char *str
, int len
)
141 if (NULL
== str
|| 0 == *str
)
146 slen
= grub_strlen(str
);
152 for (i
= 0; i
< len
; i
++)
154 if (!grub_isalnum(str
[i
]))
160 if (str
[len
] == 0 || grub_isspace(str
[len
]))
168 char * ventoy_str_basename(char *path
)
172 pos
= grub_strrchr(path
, '/');
185 int ventoy_str_chrcnt(const char *str
, char c
)
204 int ventoy_strcmp(const char *pattern
, const char *str
)
206 while (*pattern
&& *str
)
208 if ((*pattern
!= *str
) && (*pattern
!= '*'))
215 return (int)(grub_uint8_t
)*pattern
- (int)(grub_uint8_t
)*str
;
218 int ventoy_strncmp (const char *pattern
, const char *str
, grub_size_t n
)
223 while (*pattern
&& *str
&& --n
)
225 if ((*pattern
!= *str
) && (*pattern
!= '*'))
232 return (int)(grub_uint8_t
)*pattern
- (int)(grub_uint8_t
)*str
;
235 grub_err_t
ventoy_env_int_set(const char *name
, int value
)
239 grub_snprintf(buf
, sizeof(buf
), "%d", value
);
240 return grub_env_set(name
, buf
);
243 void ventoy_debug_dump_guid(const char *prefix
, grub_uint8_t
*guid
)
253 for (i
= 0; i
< 16; i
++)
255 grub_printf("%02x ", guid
[i
]);
260 int ventoy_is_efi_os(void)
264 g_efi_os
= (grub_strstr(GRUB_PLATFORM
, "efi")) ? 1 : 0;
270 void * ventoy_alloc_chain(grub_size_t size
)
274 p
= grub_malloc(size
);
275 #ifdef GRUB_MACHINE_EFI
278 p
= grub_efi_allocate_any_pages(GRUB_EFI_BYTES_TO_PAGES(size
));
285 void ventoy_memfile_env_set(const char *prefix
, const void *buf
, unsigned long long len
)
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
);
294 grub_snprintf(name
, sizeof(name
), "%s_size", prefix
);
295 grub_snprintf(val
, sizeof(val
), "%llu", len
);
296 grub_env_set(name
, val
);
301 static int ventoy_arch_mode_init(void)
303 #ifdef GRUB_MACHINE_EFI
304 if (grub_strcmp(GRUB_TARGET_CPU
, "i386") == 0)
306 g_ventoy_plat_data
= VTOY_PLAT_I386_UEFI
;
307 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "ia32");
309 else if (grub_strcmp(GRUB_TARGET_CPU
, "arm64") == 0)
311 g_ventoy_plat_data
= VTOY_PLAT_ARM64_UEFI
;
312 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "aa64");
314 else if (grub_strcmp(GRUB_TARGET_CPU
, "mips64el") == 0)
316 g_ventoy_plat_data
= VTOY_PLAT_MIPS_UEFI
;
317 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "mips");
321 g_ventoy_plat_data
= VTOY_PLAT_X86_64_UEFI
;
322 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "uefi");
325 g_ventoy_plat_data
= VTOY_PLAT_X86_LEGACY
;
326 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "legacy");
332 #ifdef GRUB_MACHINE_EFI
333 static void ventoy_get_uefi_version(char *str
, grub_size_t len
)
335 grub_efi_uint8_t uefi_minor_1
, uefi_minor_2
;
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
);
341 grub_snprintf(str
, len
, "%s.%d", str
, uefi_minor_2
);
345 static int ventoy_calc_totalmem(grub_uint64_t addr
, grub_uint64_t size
, grub_memory_type_t type
, void *data
)
347 grub_uint64_t
*total_mem
= (grub_uint64_t
*)data
;
357 static int ventoy_hwinfo_init(void)
360 grub_uint64_t total_mem
= 0;
362 grub_machine_mmap_iterate(ventoy_calc_totalmem
, &total_mem
);
364 grub_snprintf(str
, sizeof(str
), "%ld", (long)(total_mem
/ VTOY_SIZE_1MB
));
365 ventoy_env_export("grub_total_ram", str
);
367 #ifdef GRUB_MACHINE_EFI
368 ventoy_get_uefi_version(str
, sizeof(str
));
369 ventoy_env_export("grub_uefi_version", str
);
371 ventoy_env_export("grub_uefi_version", "NA");
377 static global_var_cfg g_global_vars
[] =
379 { "gfxmode", "1024x768", NULL
},
380 { ventoy_left_key
, "5%", NULL
},
381 { ventoy_top_key
, "95%", NULL
},
382 { ventoy_color_key
, "#0000ff", NULL
},
386 static const char * ventoy_global_var_read_hook(struct grub_env_var
*var
, const char *val
)
390 for (i
= 0; g_global_vars
[i
].name
; i
++)
392 if (grub_strcmp(g_global_vars
[i
].name
, var
->name
) == 0)
394 return g_global_vars
[i
].value
;
401 static char * ventoy_global_var_write_hook(struct grub_env_var
*var
, const char *val
)
405 for (i
= 0; g_global_vars
[i
].name
; i
++)
407 if (grub_strcmp(g_global_vars
[i
].name
, var
->name
) == 0)
409 grub_check_free(g_global_vars
[i
].value
);
410 g_global_vars
[i
].value
= grub_strdup(val
);
415 return grub_strdup(val
);
418 int ventoy_global_var_init(void)
422 for (i
= 0; g_global_vars
[i
].name
; i
++)
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
);
432 static ctrl_var_cfg g_ctrl_vars
[] =
434 { "VTOY_WIN11_BYPASS_CHECK", 1 },
435 { "VTOY_WIN11_BYPASS_NRO", 1 },
436 { "VTOY_LINUX_REMOUNT", 0 },
437 { "VTOY_SECONDARY_BOOT_MENU", 1 },
441 static const char * ventoy_ctrl_var_read_hook(struct grub_env_var
*var
, const char *val
)
445 for (i
= 0; g_ctrl_vars
[i
].name
; i
++)
447 if (grub_strcmp(g_ctrl_vars
[i
].name
, var
->name
) == 0)
449 return g_ctrl_vars
[i
].value
? "1" : "0";
456 static char * ventoy_ctrl_var_write_hook(struct grub_env_var
*var
, const char *val
)
460 for (i
= 0; g_ctrl_vars
[i
].name
; i
++)
462 if (grub_strcmp(g_ctrl_vars
[i
].name
, var
->name
) == 0)
464 if (val
&& val
[0] == '1' && val
[1] == 0)
466 g_ctrl_vars
[i
].value
= 1;
467 return grub_strdup("1");
471 g_ctrl_vars
[i
].value
= 0;
472 return grub_strdup("0");
477 return grub_strdup(val
);
480 int ventoy_ctrl_var_init(void)
484 for (i
= 0; g_ctrl_vars
[i
].name
; i
++)
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
);
493 GRUB_MOD_INIT(ventoy
)
495 ventoy_hwinfo_init();
497 ventoy_arch_mode_init();
498 ventoy_register_all_cmd();
501 GRUB_MOD_FINI(ventoy
)
503 ventoy_unregister_all_cmd();