]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
keep up with 1.0.67 (#1464)
[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/ventoy.h>
39 #include "ventoy_def.h"
40
41 GRUB_MOD_LICENSE ("GPLv3+");
42
43 int g_ventoy_debug = 0;
44 static int g_efi_os = 0xFF;
45 grub_uint32_t g_ventoy_plat_data;
46
47 void ventoy_debug(const char *fmt, ...)
48 {
49 va_list args;
50
51 va_start (args, fmt);
52 grub_vprintf (fmt, args);
53 va_end (args);
54 }
55
56 void ventoy_str_tolower(char *str)
57 {
58 while (*str)
59 {
60 *str = grub_tolower(*str);
61 str++;
62 }
63 }
64
65 void ventoy_str_toupper(char *str)
66 {
67 while (*str)
68 {
69 *str = grub_toupper(*str);
70 str++;
71 }
72 }
73
74
75
76 int ventoy_strcmp(const char *pattern, const char *str)
77 {
78 while (*pattern && *str)
79 {
80 if ((*pattern != *str) && (*pattern != '*'))
81 break;
82
83 pattern++;
84 str++;
85 }
86
87 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
88 }
89
90 int ventoy_strncmp (const char *pattern, const char *str, grub_size_t n)
91 {
92 if (n == 0)
93 return 0;
94
95 while (*pattern && *str && --n)
96 {
97 if ((*pattern != *str) && (*pattern != '*'))
98 break;
99
100 pattern++;
101 str++;
102 }
103
104 return (int)(grub_uint8_t)*pattern - (int)(grub_uint8_t)*str;
105 }
106
107 void ventoy_debug_dump_guid(const char *prefix, grub_uint8_t *guid)
108 {
109 int i;
110
111 if (!g_ventoy_debug)
112 {
113 return;
114 }
115
116 debug("%s", prefix);
117 for (i = 0; i < 16; i++)
118 {
119 grub_printf("%02x ", guid[i]);
120 }
121 grub_printf("\n");
122 }
123
124 int ventoy_is_efi_os(void)
125 {
126 if (g_efi_os > 1)
127 {
128 g_efi_os = (grub_strstr(GRUB_PLATFORM, "efi")) ? 1 : 0;
129 }
130
131 return g_efi_os;
132 }
133
134 static int ventoy_arch_mode_init(void)
135 {
136 #ifdef GRUB_MACHINE_EFI
137 if (grub_strcmp(GRUB_TARGET_CPU, "i386") == 0)
138 {
139 g_ventoy_plat_data = VTOY_PLAT_I386_UEFI;
140 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "ia32");
141 }
142 else if (grub_strcmp(GRUB_TARGET_CPU, "arm64") == 0)
143 {
144 g_ventoy_plat_data = VTOY_PLAT_ARM64_UEFI;
145 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "aa64");
146 }
147 else if (grub_strcmp(GRUB_TARGET_CPU, "mips64el") == 0)
148 {
149 g_ventoy_plat_data = VTOY_PLAT_MIPS_UEFI;
150 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "mips");
151 }
152 else
153 {
154 g_ventoy_plat_data = VTOY_PLAT_X86_64_UEFI;
155 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "uefi");
156 }
157 #else
158 g_ventoy_plat_data = VTOY_PLAT_X86_LEGACY;
159 grub_snprintf(g_arch_mode_suffix, sizeof(g_arch_mode_suffix), "%s", "legacy");
160 #endif
161
162 return 0;
163 }
164
165 GRUB_MOD_INIT(ventoy)
166 {
167 ventoy_env_init();
168 ventoy_arch_mode_init();
169 ventoy_register_all_cmd();
170 }
171
172 GRUB_MOD_FINI(ventoy)
173 {
174 ventoy_unregister_all_cmd();
175 }
176