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_strcmp(const char *pattern
, const char *str
)
120 while (*pattern
&& *str
)
122 if ((*pattern
!= *str
) && (*pattern
!= '*'))
129 return (int)(grub_uint8_t
)*pattern
- (int)(grub_uint8_t
)*str
;
132 int ventoy_strncmp (const char *pattern
, const char *str
, grub_size_t n
)
137 while (*pattern
&& *str
&& --n
)
139 if ((*pattern
!= *str
) && (*pattern
!= '*'))
146 return (int)(grub_uint8_t
)*pattern
- (int)(grub_uint8_t
)*str
;
149 void ventoy_debug_dump_guid(const char *prefix
, grub_uint8_t
*guid
)
159 for (i
= 0; i
< 16; i
++)
161 grub_printf("%02x ", guid
[i
]);
166 int ventoy_is_efi_os(void)
170 g_efi_os
= (grub_strstr(GRUB_PLATFORM
, "efi")) ? 1 : 0;
176 void * ventoy_alloc_chain(grub_size_t size
)
180 p
= grub_malloc(size
);
181 #ifdef GRUB_MACHINE_EFI
184 p
= grub_efi_allocate_any_pages(GRUB_EFI_BYTES_TO_PAGES(size
));
191 void ventoy_memfile_env_set(const char *prefix
, const void *buf
, unsigned long long len
)
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
);
200 grub_snprintf(name
, sizeof(name
), "%s_size", prefix
);
201 grub_snprintf(val
, sizeof(val
), "%llu", len
);
202 grub_env_set(name
, val
);
207 static int ventoy_arch_mode_init(void)
209 #ifdef GRUB_MACHINE_EFI
210 if (grub_strcmp(GRUB_TARGET_CPU
, "i386") == 0)
212 g_ventoy_plat_data
= VTOY_PLAT_I386_UEFI
;
213 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "ia32");
215 else if (grub_strcmp(GRUB_TARGET_CPU
, "arm64") == 0)
217 g_ventoy_plat_data
= VTOY_PLAT_ARM64_UEFI
;
218 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "aa64");
220 else if (grub_strcmp(GRUB_TARGET_CPU
, "mips64el") == 0)
222 g_ventoy_plat_data
= VTOY_PLAT_MIPS_UEFI
;
223 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "mips");
227 g_ventoy_plat_data
= VTOY_PLAT_X86_64_UEFI
;
228 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "uefi");
231 g_ventoy_plat_data
= VTOY_PLAT_X86_LEGACY
;
232 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "legacy");
238 #ifdef GRUB_MACHINE_EFI
239 static void ventoy_get_uefi_version(char *str
, grub_size_t len
)
241 grub_efi_uint8_t uefi_minor_1
, uefi_minor_2
;
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
);
247 grub_snprintf(str
, len
, "%s.%d", str
, uefi_minor_2
);
251 static int ventoy_calc_totalmem(grub_uint64_t addr
, grub_uint64_t size
, grub_memory_type_t type
, void *data
)
253 grub_uint64_t
*total_mem
= (grub_uint64_t
*)data
;
263 static int ventoy_hwinfo_init(void)
266 grub_uint64_t total_mem
= 0;
268 grub_machine_mmap_iterate(ventoy_calc_totalmem
, &total_mem
);
270 grub_snprintf(str
, sizeof(str
), "%ld", (long)(total_mem
/ VTOY_SIZE_1MB
));
271 ventoy_env_export("grub_total_ram", str
);
273 #ifdef GRUB_MACHINE_EFI
274 ventoy_get_uefi_version(str
, sizeof(str
));
275 ventoy_env_export("grub_uefi_version", str
);
277 ventoy_env_export("grub_uefi_version", "NA");
283 static global_var_cfg g_global_vars
[] =
285 { "gfxmode", "1024x768", NULL
},
286 { ventoy_left_key
, "5%", NULL
},
287 { ventoy_top_key
, "95%", NULL
},
288 { ventoy_color_key
, "#0000ff", NULL
},
292 static const char * ventoy_global_var_read_hook(struct grub_env_var
*var
, const char *val
)
296 for (i
= 0; g_global_vars
[i
].name
; i
++)
298 if (grub_strcmp(g_global_vars
[i
].name
, var
->name
) == 0)
300 return g_global_vars
[i
].value
;
307 static char * ventoy_global_var_write_hook(struct grub_env_var
*var
, const char *val
)
311 for (i
= 0; g_global_vars
[i
].name
; i
++)
313 if (grub_strcmp(g_global_vars
[i
].name
, var
->name
) == 0)
315 grub_check_free(g_global_vars
[i
].value
);
316 g_global_vars
[i
].value
= grub_strdup(val
);
321 return grub_strdup(val
);
324 int ventoy_global_var_init(void)
328 for (i
= 0; g_global_vars
[i
].name
; i
++)
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
);
338 static ctrl_var_cfg g_ctrl_vars
[] =
340 { "VTOY_WIN11_BYPASS_CHECK", 1 },
341 { "VTOY_WIN11_BYPASS_NRO", 1 },
342 { "VTOY_LINUX_REMOUNT", 0 },
343 { "VTOY_SECONDARY_BOOT_MENU", 1 },
347 static const char * ventoy_ctrl_var_read_hook(struct grub_env_var
*var
, const char *val
)
351 for (i
= 0; g_ctrl_vars
[i
].name
; i
++)
353 if (grub_strcmp(g_ctrl_vars
[i
].name
, var
->name
) == 0)
355 return g_ctrl_vars
[i
].value
? "1" : "0";
362 static char * ventoy_ctrl_var_write_hook(struct grub_env_var
*var
, const char *val
)
366 for (i
= 0; g_ctrl_vars
[i
].name
; i
++)
368 if (grub_strcmp(g_ctrl_vars
[i
].name
, var
->name
) == 0)
370 if (val
&& val
[0] == '1' && val
[1] == 0)
372 g_ctrl_vars
[i
].value
= 1;
373 return grub_strdup("1");
377 g_ctrl_vars
[i
].value
= 0;
378 return grub_strdup("0");
383 return grub_strdup(val
);
386 int ventoy_ctrl_var_init(void)
390 for (i
= 0; g_ctrl_vars
[i
].name
; i
++)
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
);
399 GRUB_MOD_INIT(ventoy
)
401 ventoy_hwinfo_init();
403 ventoy_arch_mode_init();
404 ventoy_register_all_cmd();
407 GRUB_MOD_FINI(ventoy
)
409 ventoy_unregister_all_cmd();