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