1 /* search.c - search devices based on a file or a filesystem label */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/types.h>
21 #include <grub/misc.h>
25 #include <grub/device.h>
26 #include <grub/file.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>
34 GRUB_MOD_LICENSE ("GPLv3+");
36 static int g_no_vtoyefi_part
= 0;
37 static char g_vtoyefi_dosname
[64];
38 static char g_vtoyefi_gptname
[64];
42 struct cache_entry
*next
;
47 static struct cache_entry
*cache
;
49 /* Context for FUNC_NAME. */
61 /* Helper for FUNC_NAME. */
63 iterate_device (const char *name
, void *data
)
65 struct search_ctx
*ctx
= data
;
68 /* Skip floppy drives when requested. */
70 name
[0] == 'f' && name
[1] == 'd' && name
[2] >= '0' && name
[2] <= '9')
73 if (g_no_vtoyefi_part
&& (grub_strcmp(name
, g_vtoyefi_dosname
) == 0 || grub_strcmp(name
, g_vtoyefi_gptname
) == 0)) {
77 #ifdef DO_SEARCH_FS_UUID
78 #define compare_fn grub_strcasecmp
80 #define compare_fn grub_strcmp
88 buf
= grub_xasprintf ("(%s)%s", name
, ctx
->key
);
92 file
= grub_file_open (buf
, GRUB_FILE_TYPE_FS_SEARCH
93 | GRUB_FILE_TYPE_NO_DECOMPRESS
);
97 grub_file_close (file
);
103 /* SEARCH_FS_UUID or SEARCH_LABEL */
108 dev
= grub_device_open (name
);
111 fs
= grub_fs_probe (dev
);
113 #ifdef DO_SEARCH_FS_UUID
114 #define read_fn fs_uuid
116 #define read_fn fs_label
119 if (fs
&& fs
->read_fn
)
121 fs
->read_fn (dev
, &quid
);
123 if (grub_errno
== GRUB_ERR_NONE
&& quid
)
125 if (compare_fn (quid
, ctx
->key
) == 0)
132 grub_device_close (dev
);
137 if (!ctx
->is_cache
&& found
&& ctx
->count
== 0)
139 struct cache_entry
*cache_ent
;
140 cache_ent
= grub_malloc (sizeof (*cache_ent
));
143 cache_ent
->key
= grub_strdup (ctx
->key
);
144 cache_ent
->value
= grub_strdup (name
);
145 if (cache_ent
->value
&& cache_ent
->key
)
147 cache_ent
->next
= cache
;
152 grub_free (cache_ent
->value
);
153 grub_free (cache_ent
->key
);
154 grub_free (cache_ent
);
155 grub_errno
= GRUB_ERR_NONE
;
159 grub_errno
= GRUB_ERR_NONE
;
166 grub_env_set (ctx
->var
, name
);
168 grub_printf (" %s", name
);
171 grub_errno
= GRUB_ERR_NONE
;
172 return (found
&& ctx
->var
);
175 /* Helper for FUNC_NAME. */
177 part_hook (grub_disk_t disk
, const grub_partition_t partition
, void *data
)
179 struct search_ctx
*ctx
= data
;
180 char *partition_name
, *devname
;
183 partition_name
= grub_partition_get_name (partition
);
184 if (! partition_name
)
187 devname
= grub_xasprintf ("%s,%s", disk
->name
, partition_name
);
188 grub_free (partition_name
);
191 ret
= iterate_device (devname
, ctx
);
197 /* Helper for FUNC_NAME. */
199 try (struct search_ctx
*ctx
)
202 struct cache_entry
**prev
;
203 struct cache_entry
*cache_ent
;
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)
212 if (iterate_device (cache_ent
->value
, ctx
))
218 /* Cache entry was outdated. Remove it. */
221 *prev
= cache_ent
->next
;
222 grub_free (cache_ent
->key
);
223 grub_free (cache_ent
->value
);
224 grub_free (cache_ent
);
228 for (i
= 0; i
< ctx
->nhints
; i
++)
231 if (!ctx
->hints
[i
][0])
233 end
= ctx
->hints
[i
] + grub_strlen (ctx
->hints
[i
]) - 1;
236 if (iterate_device (ctx
->hints
[i
], ctx
))
246 dev
= grub_device_open (ctx
->hints
[i
]);
255 grub_device_close (dev
);
260 ret
= grub_partition_iterate (dev
->disk
, part_hook
, ctx
);
263 grub_device_close (dev
);
268 grub_device_iterate (iterate_device
, ctx
);
272 FUNC_NAME (const char *key
, const char *var
, int no_floppy
,
273 char **hints
, unsigned nhints
)
275 struct search_ctx ctx
= {
278 .no_floppy
= no_floppy
,
284 grub_fs_autoload_hook_t saved_autoload
;
286 g_no_vtoyefi_part
= 0;
287 if (grub_env_get("VTOY_SEARCH_NO_VTOYEFI"))
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;
294 /* First try without autoloading if we're setting variable. */
297 saved_autoload
= grub_fs_autoload_hook
;
298 grub_fs_autoload_hook
= 0;
301 /* Restore autoload hook. */
302 grub_fs_autoload_hook
= saved_autoload
;
304 /* Retry with autoload if nothing found. */
305 if (grub_errno
== GRUB_ERR_NONE
&& ctx
.count
== 0)
311 if (grub_errno
== GRUB_ERR_NONE
&& ctx
.count
== 0)
312 grub_error (GRUB_ERR_FILE_NOT_FOUND
, "no such device: %s", key
);
316 grub_cmd_do_search (grub_command_t cmd
__attribute__ ((unused
)), int argc
,
320 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("one argument expected"));
322 FUNC_NAME (args
[0], argc
== 1 ? 0 : args
[1], 0, (args
+ 2),
323 argc
> 2 ? argc
- 2 : 0);
328 static grub_command_t cmd
;
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
)
335 GRUB_MOD_INIT(search_label
)
339 grub_register_command (COMMAND_NAME
, grub_cmd_do_search
,
340 N_("NAME [VARIABLE] [HINTS]"),
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
)
349 GRUB_MOD_FINI(search_label
)
352 grub_unregister_command (cmd
);