]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/commands/search.c
update languages.ini (#829 #834)
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / commands / search.c
1 /* search.c - search devices based on a file or a filesystem label */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2008,2009 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <grub/types.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/dl.h>
25 #include <grub/device.h>
26 #include <grub/file.h>
27 #include <grub/env.h>
28 #include <grub/command.h>
29 #include <grub/search.h>
30 #include <grub/i18n.h>
31 #include <grub/disk.h>
32 #include <grub/partition.h>
33
34 GRUB_MOD_LICENSE ("GPLv3+");
35
36 static int g_no_vtoyefi_part = 0;
37 static char g_vtoyefi_dosname[64];
38 static char g_vtoyefi_gptname[64];
39
40 struct cache_entry
41 {
42 struct cache_entry *next;
43 char *key;
44 char *value;
45 };
46
47 static struct cache_entry *cache;
48
49 /* Context for FUNC_NAME. */
50 struct search_ctx
51 {
52 const char *key;
53 const char *var;
54 int no_floppy;
55 char **hints;
56 unsigned nhints;
57 int count;
58 int is_cache;
59 };
60
61 /* Helper for FUNC_NAME. */
62 static int
63 iterate_device (const char *name, void *data)
64 {
65 struct search_ctx *ctx = data;
66 int found = 0;
67
68 /* Skip floppy drives when requested. */
69 if (ctx->no_floppy &&
70 name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
71 return 1;
72
73 if (g_no_vtoyefi_part && (grub_strcmp(name, g_vtoyefi_dosname) == 0 || grub_strcmp(name, g_vtoyefi_gptname) == 0)) {
74 return 0;
75 }
76
77 #ifdef DO_SEARCH_FS_UUID
78 #define compare_fn grub_strcasecmp
79 #else
80 #define compare_fn grub_strcmp
81 #endif
82
83 #ifdef DO_SEARCH_FILE
84 {
85 char *buf;
86 grub_file_t file;
87
88 buf = grub_xasprintf ("(%s)%s", name, ctx->key);
89 if (! buf)
90 return 1;
91
92 file = grub_file_open (buf, GRUB_FILE_TYPE_FS_SEARCH
93 | GRUB_FILE_TYPE_NO_DECOMPRESS);
94 if (file)
95 {
96 found = 1;
97 grub_file_close (file);
98 }
99 grub_free (buf);
100 }
101 #else
102 {
103 /* SEARCH_FS_UUID or SEARCH_LABEL */
104 grub_device_t dev;
105 grub_fs_t fs;
106 char *quid;
107
108 dev = grub_device_open (name);
109 if (dev)
110 {
111 fs = grub_fs_probe (dev);
112
113 #ifdef DO_SEARCH_FS_UUID
114 #define read_fn fs_uuid
115 #else
116 #define read_fn fs_label
117 #endif
118
119 if (fs && fs->read_fn)
120 {
121 fs->read_fn (dev, &quid);
122
123 if (grub_errno == GRUB_ERR_NONE && quid)
124 {
125 if (compare_fn (quid, ctx->key) == 0)
126 found = 1;
127
128 grub_free (quid);
129 }
130 }
131
132 grub_device_close (dev);
133 }
134 }
135 #endif
136
137 if (!ctx->is_cache && found && ctx->count == 0)
138 {
139 struct cache_entry *cache_ent;
140 cache_ent = grub_malloc (sizeof (*cache_ent));
141 if (cache_ent)
142 {
143 cache_ent->key = grub_strdup (ctx->key);
144 cache_ent->value = grub_strdup (name);
145 if (cache_ent->value && cache_ent->key)
146 {
147 cache_ent->next = cache;
148 cache = cache_ent;
149 }
150 else
151 {
152 grub_free (cache_ent->value);
153 grub_free (cache_ent->key);
154 grub_free (cache_ent);
155 grub_errno = GRUB_ERR_NONE;
156 }
157 }
158 else
159 grub_errno = GRUB_ERR_NONE;
160 }
161
162 if (found)
163 {
164 ctx->count++;
165 if (ctx->var)
166 grub_env_set (ctx->var, name);
167 else
168 grub_printf (" %s", name);
169 }
170
171 grub_errno = GRUB_ERR_NONE;
172 return (found && ctx->var);
173 }
174
175 /* Helper for FUNC_NAME. */
176 static int
177 part_hook (grub_disk_t disk, const grub_partition_t partition, void *data)
178 {
179 struct search_ctx *ctx = data;
180 char *partition_name, *devname;
181 int ret;
182
183 partition_name = grub_partition_get_name (partition);
184 if (! partition_name)
185 return 1;
186
187 devname = grub_xasprintf ("%s,%s", disk->name, partition_name);
188 grub_free (partition_name);
189 if (!devname)
190 return 1;
191 ret = iterate_device (devname, ctx);
192 grub_free (devname);
193
194 return ret;
195 }
196
197 /* Helper for FUNC_NAME. */
198 static void
199 try (struct search_ctx *ctx)
200 {
201 unsigned i;
202 struct cache_entry **prev;
203 struct cache_entry *cache_ent;
204
205 for (prev = &cache, cache_ent = *prev; cache_ent;
206 prev = &cache_ent->next, cache_ent = *prev)
207 if (compare_fn (cache_ent->key, ctx->key) == 0)
208 break;
209 if (cache_ent)
210 {
211 ctx->is_cache = 1;
212 if (iterate_device (cache_ent->value, ctx))
213 {
214 ctx->is_cache = 0;
215 return;
216 }
217 ctx->is_cache = 0;
218 /* Cache entry was outdated. Remove it. */
219 if (!ctx->count)
220 {
221 *prev = cache_ent->next;
222 grub_free (cache_ent->key);
223 grub_free (cache_ent->value);
224 grub_free (cache_ent);
225 }
226 }
227
228 for (i = 0; i < ctx->nhints; i++)
229 {
230 char *end;
231 if (!ctx->hints[i][0])
232 continue;
233 end = ctx->hints[i] + grub_strlen (ctx->hints[i]) - 1;
234 if (*end == ',')
235 *end = 0;
236 if (iterate_device (ctx->hints[i], ctx))
237 {
238 if (!*end)
239 *end = ',';
240 return;
241 }
242 if (!*end)
243 {
244 grub_device_t dev;
245 int ret;
246 dev = grub_device_open (ctx->hints[i]);
247 if (!dev)
248 {
249 if (!*end)
250 *end = ',';
251 continue;
252 }
253 if (!dev->disk)
254 {
255 grub_device_close (dev);
256 if (!*end)
257 *end = ',';
258 continue;
259 }
260 ret = grub_partition_iterate (dev->disk, part_hook, ctx);
261 if (!*end)
262 *end = ',';
263 grub_device_close (dev);
264 if (ret)
265 return;
266 }
267 }
268 grub_device_iterate (iterate_device, ctx);
269 }
270
271 void
272 FUNC_NAME (const char *key, const char *var, int no_floppy,
273 char **hints, unsigned nhints)
274 {
275 struct search_ctx ctx = {
276 .key = key,
277 .var = var,
278 .no_floppy = no_floppy,
279 .hints = hints,
280 .nhints = nhints,
281 .count = 0,
282 .is_cache = 0
283 };
284 grub_fs_autoload_hook_t saved_autoload;
285
286 g_no_vtoyefi_part = 0;
287 if (grub_env_get("VTOY_SEARCH_NO_VTOYEFI"))
288 {
289 grub_snprintf(g_vtoyefi_dosname, sizeof(g_vtoyefi_dosname), "%s,msdos2", grub_env_get("vtoydev"));
290 grub_snprintf(g_vtoyefi_gptname, sizeof(g_vtoyefi_gptname), "%s,gpt2", grub_env_get("vtoydev"));
291 g_no_vtoyefi_part = 1;
292 }
293
294 /* First try without autoloading if we're setting variable. */
295 if (var)
296 {
297 saved_autoload = grub_fs_autoload_hook;
298 grub_fs_autoload_hook = 0;
299 try (&ctx);
300
301 /* Restore autoload hook. */
302 grub_fs_autoload_hook = saved_autoload;
303
304 /* Retry with autoload if nothing found. */
305 if (grub_errno == GRUB_ERR_NONE && ctx.count == 0)
306 try (&ctx);
307 }
308 else
309 try (&ctx);
310
311 if (grub_errno == GRUB_ERR_NONE && ctx.count == 0)
312 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
313 }
314
315 static grub_err_t
316 grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
317 char **args)
318 {
319 if (argc == 0)
320 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
321
322 FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2),
323 argc > 2 ? argc - 2 : 0);
324
325 return grub_errno;
326 }
327
328 static grub_command_t cmd;
329
330 #ifdef DO_SEARCH_FILE
331 GRUB_MOD_INIT(search_fs_file)
332 #elif defined (DO_SEARCH_FS_UUID)
333 GRUB_MOD_INIT(search_fs_uuid)
334 #else
335 GRUB_MOD_INIT(search_label)
336 #endif
337 {
338 cmd =
339 grub_register_command (COMMAND_NAME, grub_cmd_do_search,
340 N_("NAME [VARIABLE] [HINTS]"),
341 HELP_MESSAGE);
342 }
343
344 #ifdef DO_SEARCH_FILE
345 GRUB_MOD_FINI(search_fs_file)
346 #elif defined (DO_SEARCH_FS_UUID)
347 GRUB_MOD_FINI(search_fs_uuid)
348 #else
349 GRUB_MOD_FINI(search_label)
350 #endif
351 {
352 grub_unregister_command (cmd);
353 }