]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
7d4c6257196d423b92ad68526b3fd26fa12cf8e9
[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_strcmp(const char *pattern, const char *str)
119 {
120 while (*pattern && *str)
121 {
122 if ((*pattern != *str) && (*pattern != '*'))
123 break;
124
125 pattern++;
126 str++;
127 }
128
129 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
130 }
131
132 int ventoy_strncmp (const char *pattern, const char *str, grub_size_t n)
133 {
134 if (n == 0)
135 return 0;
136
137 while (*pattern && *str && --n)
138 {
139 if ((*pattern != *str) && (*pattern != '*'))
140 break;
141
142 pattern++;
143 str++;
144 }
145
146 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
147 }
148
149 void ventoy_debug_dump_guid(const char *prefix, grub_uint8_t *guid)
150 {
151 int i;
152
153 if (!g_ventoy_debug)
154 {
155 return;
156 }
157
158 debug("%s", prefix);
159 for (i = 0; i < 16; i++)
160 {
161 grub_printf("%02x ", guid[i]);
162 }
163 grub_printf("\n");
164 }
165
166 int ventoy_is_efi_os(void)
167 {
168 if (g_efi_os > 1)
169 {
170 g_efi_os = (grub_strstr(GRUB_PLATFORM, "efi")) ? 1 : 0;
171 }
172
173 return g_efi_os;
174 }
175
176 void * ventoy_alloc_chain(grub_size_t size)
177 {
178 void *p = NULL;
179
180 p = grub_malloc(size);
181 #ifdef GRUB_MACHINE_EFI
182 if (!p)
183 {
184 p = grub_efi_allocate_any_pages(GRUB_EFI_BYTES_TO_PAGES(size));
185 }
186 #endif
187
188 return p;
189 }
190
191 void ventoy_memfile_env_set(const char *prefix, const void *buf, unsigned long long len)
192 {
193 char name[128];
194 char val[64];
195
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);
199
200 grub_snprintf(name, sizeof(name), "%s_size", prefix);
201 grub_snprintf(val, sizeof(val), "%llu", len);
202 grub_env_set(name, val);
203
204 return;
205 }
206
207 static int ventoy_arch_mode_init(void)
208 {
209 #ifdef GRUB_MACHINE_EFI
210 if (grub_strcmp(GRUB_TARGET_CPU, "i386") == 0)
211 {
212 g_ventoy_plat_data = VTOY_PLAT_I386_UEFI;
213 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "ia32");
214 }
215 else if (grub_strcmp(GRUB_TARGET_CPU, "arm64") == 0)
216 {
217 g_ventoy_plat_data = VTOY_PLAT_ARM64_UEFI;
218 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "aa64");
219 }
220 else if (grub_strcmp(GRUB_TARGET_CPU, "mips64el") == 0)
221 {
222 g_ventoy_plat_data = VTOY_PLAT_MIPS_UEFI;
223 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "mips");
224 }
225 else
226 {
227 g_ventoy_plat_data = VTOY_PLAT_X86_64_UEFI;
228 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "uefi");
229 }
230 #else
231 g_ventoy_plat_data = VTOY_PLAT_X86_LEGACY;
232 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "legacy");
233 #endif
234
235 return 0;
236 }
237
238 #ifdef GRUB_MACHINE_EFI
239 static void ventoy_get_uefi_version(char *str, grub_size_t len)
240 {
241 grub_efi_uint8_t uefi_minor_1, uefi_minor_2;
242
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);
246 if (uefi_minor_2)
247 grub_snprintf(str, len, "%s.%d", str, uefi_minor_2);
248 }
249 #endif
250
251 static int ventoy_calc_totalmem(grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type, void *data)
252 {
253 grub_uint64_t *total_mem = (grub_uint64_t *)data;
254
255 (void)addr;
256 (void)type;
257
258 *total_mem += size;
259
260 return 0;
261 }
262
263 static int ventoy_hwinfo_init(void)
264 {
265 char str[256];
266 grub_uint64_t total_mem = 0;
267
268 grub_machine_mmap_iterate(ventoy_calc_totalmem, &total_mem);
269
270 grub_snprintf(str, sizeof(str), "%ld", (long)(total_mem / VTOY_SIZE_1MB));
271 ventoy_env_export("grub_total_ram", str);
272
273 #ifdef GRUB_MACHINE_EFI
274 ventoy_get_uefi_version(str, sizeof(str));
275 ventoy_env_export("grub_uefi_version", str);
276 #else
277 ventoy_env_export("grub_uefi_version", "NA");
278 #endif
279
280 return 0;
281 }
282
283 GRUB_MOD_INIT(ventoy)
284 {
285 ventoy_hwinfo_init();
286 ventoy_env_init();
287 ventoy_arch_mode_init();
288 ventoy_register_all_cmd();
289 }
290
291 GRUB_MOD_FINI(ventoy)
292 {
293 ventoy_unregister_all_cmd();
294 }
295