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>
35
FUNC_NAME (const char *key, const char *var, int no_floppy,
36
char **hints, unsigned nhints)
39
grub_fs_autoload_hook_t saved_autoload;
41
auto int iterate_device (const char *name);
42
int iterate_device (const char *name)
46
/* Skip floppy drives when requested. */
48
name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
56
buf = grub_xasprintf ("(%s)%s", name, key);
60
grub_file_filter_disable_compression ();
61
file = grub_file_open (buf);
65
grub_file_close (file);
71
/* SEARCH_FS_UUID or SEARCH_LABEL */
76
dev = grub_device_open (name);
79
fs = grub_fs_probe (dev);
81
#ifdef DO_SEARCH_FS_UUID
82
#define compare_fn grub_strcasecmp
85
#define compare_fn grub_strcmp
89
if (fs && fs->read_fn)
91
fs->read_fn (dev, &quid);
93
if (grub_errno == GRUB_ERR_NONE && quid)
95
if (compare_fn (quid, key) == 0)
102
grub_device_close (dev);
111
grub_env_set (var, name);
113
grub_printf (" %s", name);
116
grub_errno = GRUB_ERR_NONE;
117
return (found && var);
120
auto int part_hook (grub_disk_t disk, const grub_partition_t partition);
121
int part_hook (grub_disk_t disk, const grub_partition_t partition)
123
char *partition_name, *devname;
126
partition_name = grub_partition_get_name (partition);
127
if (! partition_name)
130
devname = grub_xasprintf ("%s,%s", disk->name, partition_name);
131
grub_free (partition_name);
134
ret = iterate_device (devname);
140
auto void try (void);
144
for (i = 0; i < nhints; i++)
149
end = hints[i] + grub_strlen (hints[i]) - 1;
152
if (iterate_device (hints[i]))
162
dev = grub_device_open (hints[i]);
170
grub_device_close (dev);
174
ret = grub_partition_iterate (dev->disk, part_hook);
176
grub_device_close (dev);
181
grub_device_iterate (iterate_device);
184
/* First try without autoloading if we're setting variable. */
187
saved_autoload = grub_fs_autoload_hook;
188
grub_fs_autoload_hook = 0;
191
/* Restore autoload hook. */
192
grub_fs_autoload_hook = saved_autoload;
194
/* Retry with autoload if nothing found. */
195
if (grub_errno == GRUB_ERR_NONE && count == 0)
201
if (grub_errno == GRUB_ERR_NONE && count == 0)
202
grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
206
grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
210
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
212
FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2),
213
argc > 2 ? argc - 2 : 0);
218
static grub_command_t cmd;
220
#ifdef DO_SEARCH_FILE
221
GRUB_MOD_INIT(search_fs_file)
222
#elif defined (DO_SEARCH_FS_UUID)
223
GRUB_MOD_INIT(search_fs_uuid)
225
GRUB_MOD_INIT(search_label)
229
grub_register_command (COMMAND_NAME, grub_cmd_do_search,
230
N_("NAME [VARIABLE] [HINTS]"),
234
#ifdef DO_SEARCH_FILE
235
GRUB_MOD_FINI(search_fs_file)
236
#elif defined (DO_SEARCH_FS_UUID)
237
GRUB_MOD_FINI(search_fs_uuid)
239
GRUB_MOD_FINI(search_label)
242
grub_unregister_command (cmd);