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>
42 #include <grub/ventoy.h>
43 #include "ventoy_def.h"
45 GRUB_MOD_LICENSE ("GPLv3+");
47 int g_ventoy_debug
= 0;
48 static int g_efi_os
= 0xFF;
49 grub_uint32_t g_ventoy_plat_data
;
51 void ventoy_debug(const char *fmt
, ...)
56 grub_vprintf (fmt
, args
);
60 void ventoy_str_tolower(char *str
)
64 *str
= grub_tolower(*str
);
69 void ventoy_str_toupper(char *str
)
73 *str
= grub_toupper(*str
);
80 int ventoy_strcmp(const char *pattern
, const char *str
)
82 while (*pattern
&& *str
)
84 if ((*pattern
!= *str
) && (*pattern
!= '*'))
91 return (int)(grub_uint8_t
)*pattern
- (int)(grub_uint8_t
)*str
;
94 int ventoy_strncmp (const char *pattern
, const char *str
, grub_size_t n
)
99 while (*pattern
&& *str
&& --n
)
101 if ((*pattern
!= *str
) && (*pattern
!= '*'))
108 return (int)(grub_uint8_t
)*pattern
- (int)(grub_uint8_t
)*str
;
111 void ventoy_debug_dump_guid(const char *prefix
, grub_uint8_t
*guid
)
121 for (i
= 0; i
< 16; i
++)
123 grub_printf("%02x ", guid
[i
]);
128 int ventoy_is_efi_os(void)
132 g_efi_os
= (grub_strstr(GRUB_PLATFORM
, "efi")) ? 1 : 0;
138 static int ventoy_arch_mode_init(void)
140 #ifdef GRUB_MACHINE_EFI
141 if (grub_strcmp(GRUB_TARGET_CPU
, "i386") == 0)
143 g_ventoy_plat_data
= VTOY_PLAT_I386_UEFI
;
144 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "ia32");
146 else if (grub_strcmp(GRUB_TARGET_CPU
, "arm64") == 0)
148 g_ventoy_plat_data
= VTOY_PLAT_ARM64_UEFI
;
149 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "aa64");
151 else if (grub_strcmp(GRUB_TARGET_CPU
, "mips64el") == 0)
153 g_ventoy_plat_data
= VTOY_PLAT_MIPS_UEFI
;
154 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "mips");
158 g_ventoy_plat_data
= VTOY_PLAT_X86_64_UEFI
;
159 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "uefi");
162 g_ventoy_plat_data
= VTOY_PLAT_X86_LEGACY
;
163 grub_snprintf(g_arch_mode_suffix
, sizeof(g_arch_mode_suffix
), "%s", "legacy");
169 #ifdef GRUB_MACHINE_EFI
170 static void ventoy_get_uefi_version(char *str
, grub_size_t len
)
172 grub_efi_uint8_t uefi_minor_1
, uefi_minor_2
;
174 uefi_minor_1
= (grub_efi_system_table
->hdr
.revision
& 0xffff) / 10;
175 uefi_minor_2
= (grub_efi_system_table
->hdr
.revision
& 0xffff) % 10;
176 grub_snprintf(str
, len
, "%d.%d", (grub_efi_system_table
->hdr
.revision
>> 16), uefi_minor_1
);
178 grub_snprintf(str
, len
, "%s.%d", str
, uefi_minor_2
);
182 static int ventoy_calc_totalmem(grub_uint64_t addr
, grub_uint64_t size
, grub_memory_type_t type
, void *data
)
184 grub_uint64_t
*total_mem
= (grub_uint64_t
*)data
;
194 static int ventoy_hwinfo_init(void)
197 grub_uint64_t total_mem
= 0;
199 grub_machine_mmap_iterate(ventoy_calc_totalmem
, &total_mem
);
201 grub_snprintf(str
, sizeof(str
), "%ld", (long)(total_mem
/ VTOY_SIZE_1MB
));
202 ventoy_env_export("grub_total_ram", str
);
204 #ifdef GRUB_MACHINE_EFI
205 ventoy_get_uefi_version(str
, sizeof(str
));
206 ventoy_env_export("grub_uefi_version", str
);
212 GRUB_MOD_INIT(ventoy
)
214 ventoy_hwinfo_init();
216 ventoy_arch_mode_init();
217 ventoy_register_all_cmd();
220 GRUB_MOD_FINI(ventoy
)
222 ventoy_unregister_all_cmd();