]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
Merge branch 'master' of https://github.com/ventoy/Ventoy
[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 #endif
42 #include <grub/ventoy.h>
43 #include "ventoy_def.h"
44
45 GRUB_MOD_LICENSE ("GPLv3+");
46
47 int g_ventoy_debug = 0;
48 static int g_efi_os = 0xFF;
49 grub_uint32_t g_ventoy_plat_data;
50
51 void ventoy_debug(const char *fmt, ...)
52 {
53 va_list args;
54
55 va_start (args, fmt);
56 grub_vprintf (fmt, args);
57 va_end (args);
58 }
59
60 void ventoy_str_tolower(char *str)
61 {
62 while (*str)
63 {
64 *str = grub_tolower(*str);
65 str++;
66 }
67 }
68
69 void ventoy_str_toupper(char *str)
70 {
71 while (*str)
72 {
73 *str = grub_toupper(*str);
74 str++;
75 }
76 }
77
78
79
80 int ventoy_strcmp(const char *pattern, const char *str)
81 {
82 while (*pattern && *str)
83 {
84 if ((*pattern != *str) && (*pattern != '*'))
85 break;
86
87 pattern++;
88 str++;
89 }
90
91 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
92 }
93
94 int ventoy_strncmp (const char *pattern, const char *str, grub_size_t n)
95 {
96 if (n == 0)
97 return 0;
98
99 while (*pattern && *str && --n)
100 {
101 if ((*pattern != *str) && (*pattern != '*'))
102 break;
103
104 pattern++;
105 str++;
106 }
107
108 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
109 }
110
111 void ventoy_debug_dump_guid(const char *prefix, grub_uint8_t *guid)
112 {
113 int i;
114
115 if (!g_ventoy_debug)
116 {
117 return;
118 }
119
120 debug("%s", prefix);
121 for (i = 0; i < 16; i++)
122 {
123 grub_printf("%02x ", guid[i]);
124 }
125 grub_printf("\n");
126 }
127
128 int ventoy_is_efi_os(void)
129 {
130 if (g_efi_os > 1)
131 {
132 g_efi_os = (grub_strstr(GRUB_PLATFORM, "efi")) ? 1 : 0;
133 }
134
135 return g_efi_os;
136 }
137
138 static int ventoy_arch_mode_init(void)
139 {
140 #ifdef GRUB_MACHINE_EFI
141 if (grub_strcmp(GRUB_TARGET_CPU, "i386") == 0)
142 {
143 g_ventoy_plat_data = VTOY_PLAT_I386_UEFI;
144 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "ia32");
145 }
146 else if (grub_strcmp(GRUB_TARGET_CPU, "arm64") == 0)
147 {
148 g_ventoy_plat_data = VTOY_PLAT_ARM64_UEFI;
149 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "aa64");
150 }
151 else if (grub_strcmp(GRUB_TARGET_CPU, "mips64el") == 0)
152 {
153 g_ventoy_plat_data = VTOY_PLAT_MIPS_UEFI;
154 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "mips");
155 }
156 else
157 {
158 g_ventoy_plat_data = VTOY_PLAT_X86_64_UEFI;
159 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "uefi");
160 }
161 #else
162 g_ventoy_plat_data = VTOY_PLAT_X86_LEGACY;
163 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "legacy");
164 #endif
165
166 return 0;
167 }
168
169 #ifdef GRUB_MACHINE_EFI
170 static void ventoy_get_uefi_version(char *str, grub_size_t len)
171 {
172 grub_efi_uint8_t uefi_minor_1, uefi_minor_2;
173
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);
177 if (uefi_minor_2)
178 grub_snprintf(str, len, "%s.%d", str, uefi_minor_2);
179 }
180 #endif
181
182 static int ventoy_calc_totalmem(grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type, void *data)
183 {
184 grub_uint64_t *total_mem = (grub_uint64_t *)data;
185
186 (void)addr;
187 (void)type;
188
189 *total_mem += size;
190
191 return 0;
192 }
193
194 static int ventoy_hwinfo_init(void)
195 {
196 char str[256];
197 grub_uint64_t total_mem = 0;
198
199 grub_machine_mmap_iterate(ventoy_calc_totalmem, &total_mem);
200
201 grub_snprintf(str, sizeof(str), "%ld", (long)(total_mem / VTOY_SIZE_1MB));
202 ventoy_env_export("grub_total_ram", str);
203
204 #ifdef GRUB_MACHINE_EFI
205 ventoy_get_uefi_version(str, sizeof(str));
206 ventoy_env_export("grub_uefi_version", str);
207 #endif
208
209 return 0;
210 }
211
212 GRUB_MOD_INIT(ventoy)
213 {
214 ventoy_hwinfo_init();
215 ventoy_env_init();
216 ventoy_arch_mode_init();
217 ventoy_register_all_cmd();
218 }
219
220 GRUB_MOD_FINI(ventoy)
221 {
222 ventoy_unregister_all_cmd();
223 }
224