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>
33 FUNC_NAME (const char *key
, const char *var
, int no_floppy
)
36 grub_fs_autoload_hook_t saved_autoload
;
38 auto int iterate_device (const char *name
);
39 int iterate_device (const char *name
)
43 /* Skip floppy drives when requested. */
45 name
[0] == 'f' && name
[1] == 'd' && name
[2] >= '0' && name
[2] <= '9')
53 buf
= grub_xasprintf ("(%s)%s", name
, key
);
57 file
= grub_file_open (buf
);
61 grub_file_close (file
);
67 /* SEARCH_FS_UUID or SEARCH_LABEL */
72 dev
= grub_device_open (name
);
75 fs
= grub_fs_probe (dev
);
77 #ifdef DO_SEARCH_FS_UUID
78 #define compare_fn grub_strcasecmp
81 #define compare_fn grub_strcmp
85 if (fs
&& fs
->read_fn
)
87 fs
->read_fn (dev
, &quid
);
89 if (grub_errno
== GRUB_ERR_NONE
&& quid
)
91 if (compare_fn (quid
, key
) == 0)
98 grub_device_close (dev
);
107 grub_env_set (var
, name
);
109 grub_printf (" %s", name
);
112 grub_errno
= GRUB_ERR_NONE
;
113 return (found
&& var
);
116 /* First try without autoloading if we're setting variable. */
119 saved_autoload
= grub_fs_autoload_hook
;
120 grub_fs_autoload_hook
= 0;
121 grub_device_iterate (iterate_device
);
123 /* Restore autoload hook. */
124 grub_fs_autoload_hook
= saved_autoload
;
126 /* Retry with autoload if nothing found. */
127 if (grub_errno
== GRUB_ERR_NONE
&& count
== 0)
128 grub_device_iterate (iterate_device
);
131 grub_device_iterate (iterate_device
);
133 if (grub_errno
== GRUB_ERR_NONE
&& count
== 0)
134 grub_error (GRUB_ERR_FILE_NOT_FOUND
, "no such device: %s", key
);
138 grub_cmd_do_search (grub_command_t cmd
__attribute__ ((unused
)), int argc
,
142 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no argument specified");
144 FUNC_NAME (args
[0], argc
== 1 ? 0 : args
[1], 0);
149 static grub_command_t cmd
;
151 #ifdef DO_SEARCH_FILE
152 GRUB_MOD_INIT(search_fs_file
)
153 #elif defined (DO_SEARCH_FS_UUID)
154 GRUB_MOD_INIT(search_fs_uuid
)
156 GRUB_MOD_INIT(search_label
)
160 grub_register_command (COMMAND_NAME
, grub_cmd_do_search
,
161 N_("NAME [VARIABLE]"),
165 #ifdef DO_SEARCH_FILE
166 GRUB_MOD_FINI(search_fs_file
)
167 #elif defined (DO_SEARCH_FS_UUID)
168 GRUB_MOD_FINI(search_fs_uuid
)
170 GRUB_MOD_FINI(search_label
)
173 grub_unregister_command (cmd
);