]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
00ebba5253a2ae442e32f3d61a6b9e1a8c73e002
[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_strcmp(const char *pattern, const char *str)
101 {
102 while (*pattern && *str)
103 {
104 if ((*pattern != *str) && (*pattern != '*'))
105 break;
106
107 pattern++;
108 str++;
109 }
110
111 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
112 }
113
114 int ventoy_strncmp (const char *pattern, const char *str, grub_size_t n)
115 {
116 if (n == 0)
117 return 0;
118
119 while (*pattern && *str && --n)
120 {
121 if ((*pattern != *str) && (*pattern != '*'))
122 break;
123
124 pattern++;
125 str++;
126 }
127
128 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
129 }
130
131 void ventoy_debug_dump_guid(const char *prefix, grub_uint8_t *guid)
132 {
133 int i;
134
135 if (!g_ventoy_debug)
136 {
137 return;
138 }
139
140 debug("%s", prefix);
141 for (i = 0; i < 16; i++)
142 {
143 grub_printf("%02x ", guid[i]);
144 }
145 grub_printf("\n");
146 }
147
148 int ventoy_is_efi_os(void)
149 {
150 if (g_efi_os > 1)
151 {
152 g_efi_os = (grub_strstr(GRUB_PLATFORM, "efi")) ? 1 : 0;
153 }
154
155 return g_efi_os;
156 }
157
158 void * ventoy_alloc_chain(grub_size_t size)
159 {
160 void *p = NULL;
161
162 p = grub_malloc(size);
163 #ifdef GRUB_MACHINE_EFI
164 if (!p)
165 {
166 p = grub_efi_allocate_any_pages(GRUB_EFI_BYTES_TO_PAGES(size));
167 }
168 #endif
169
170 return p;
171 }
172
173
174 static int ventoy_arch_mode_init(void)
175 {
176 #ifdef GRUB_MACHINE_EFI
177 if (grub_strcmp(GRUB_TARGET_CPU, "i386") == 0)
178 {
179 g_ventoy_plat_data = VTOY_PLAT_I386_UEFI;
180 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "ia32");
181 }
182 else if (grub_strcmp(GRUB_TARGET_CPU, "arm64") == 0)
183 {
184 g_ventoy_plat_data = VTOY_PLAT_ARM64_UEFI;
185 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "aa64");
186 }
187 else if (grub_strcmp(GRUB_TARGET_CPU, "mips64el") == 0)
188 {
189 g_ventoy_plat_data = VTOY_PLAT_MIPS_UEFI;
190 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "mips");
191 }
192 else
193 {
194 g_ventoy_plat_data = VTOY_PLAT_X86_64_UEFI;
195 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "uefi");
196 }
197 #else
198 g_ventoy_plat_data = VTOY_PLAT_X86_LEGACY;
199 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "legacy");
200 #endif
201
202 return 0;
203 }
204
205 #ifdef GRUB_MACHINE_EFI
206 static void ventoy_get_uefi_version(char *str, grub_size_t len)
207 {
208 grub_efi_uint8_t uefi_minor_1, uefi_minor_2;
209
210 uefi_minor_1 = (grub_efi_system_table->hdr.revision & 0xffff) / 10;
211 uefi_minor_2 = (grub_efi_system_table->hdr.revision & 0xffff) % 10;
212 grub_snprintf(str, len, "%d.%d", (grub_efi_system_table->hdr.revision >> 16), uefi_minor_1);
213 if (uefi_minor_2)
214 grub_snprintf(str, len, "%s.%d", str, uefi_minor_2);
215 }
216 #endif
217
218 static int ventoy_calc_totalmem(grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type, void *data)
219 {
220 grub_uint64_t *total_mem = (grub_uint64_t *)data;
221
222 (void)addr;
223 (void)type;
224
225 *total_mem += size;
226
227 return 0;
228 }
229
230 static int ventoy_hwinfo_init(void)
231 {
232 char str[256];
233 grub_uint64_t total_mem = 0;
234
235 grub_machine_mmap_iterate(ventoy_calc_totalmem, &total_mem);
236
237 grub_snprintf(str, sizeof(str), "%ld", (long)(total_mem / VTOY_SIZE_1MB));
238 ventoy_env_export("grub_total_ram", str);
239
240 #ifdef GRUB_MACHINE_EFI
241 ventoy_get_uefi_version(str, sizeof(str));
242 ventoy_env_export("grub_uefi_version", str);
243 #endif
244
245 return 0;
246 }
247
248 GRUB_MOD_INIT(ventoy)
249 {
250 ventoy_hwinfo_init();
251 ventoy_env_init();
252 ventoy_arch_mode_init();
253 ventoy_register_all_cmd();
254 }
255
256 GRUB_MOD_FINI(ventoy)
257 {
258 ventoy_unregister_all_cmd();
259 }
260