]> git.proxmox.com Git - grub2.git/blob - commands/search.c
* commands/iorw.c: New file.
[grub2.git] / 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
32 void
33 FUNC_NAME (const char *key, const char *var, int no_floppy)
34 {
35 int count = 0;
36 grub_fs_autoload_hook_t saved_autoload;
37
38 auto int iterate_device (const char *name);
39 int iterate_device (const char *name)
40 {
41 int found = 0;
42
43 /* Skip floppy drives when requested. */
44 if (no_floppy &&
45 name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
46 return 0;
47
48 #ifdef DO_SEARCH_FILE
49 {
50 char *buf;
51 grub_file_t file;
52
53 buf = grub_xasprintf ("(%s)%s", name, key);
54 if (! buf)
55 return 1;
56
57 file = grub_file_open (buf);
58 if (file)
59 {
60 found = 1;
61 grub_file_close (file);
62 }
63 grub_free (buf);
64 }
65 #else
66 {
67 /* SEARCH_FS_UUID or SEARCH_LABEL */
68 grub_device_t dev;
69 grub_fs_t fs;
70 char *quid;
71
72 dev = grub_device_open (name);
73 if (dev)
74 {
75 fs = grub_fs_probe (dev);
76
77 #ifdef DO_SEARCH_FS_UUID
78 #define compare_fn grub_strcasecmp
79 #define read_fn uuid
80 #else
81 #define compare_fn grub_strcmp
82 #define read_fn label
83 #endif
84
85 if (fs && fs->read_fn)
86 {
87 fs->read_fn (dev, &quid);
88
89 if (grub_errno == GRUB_ERR_NONE && quid)
90 {
91 if (compare_fn (quid, key) == 0)
92 found = 1;
93
94 grub_free (quid);
95 }
96 }
97
98 grub_device_close (dev);
99 }
100 }
101 #endif
102
103 if (found)
104 {
105 count++;
106 if (var)
107 grub_env_set (var, name);
108 else
109 grub_printf (" %s", name);
110 }
111
112 grub_errno = GRUB_ERR_NONE;
113 return (found && var);
114 }
115
116 /* First try without autoloading if we're setting variable. */
117 if (var)
118 {
119 saved_autoload = grub_fs_autoload_hook;
120 grub_fs_autoload_hook = 0;
121 grub_device_iterate (iterate_device);
122
123 /* Restore autoload hook. */
124 grub_fs_autoload_hook = saved_autoload;
125
126 /* Retry with autoload if nothing found. */
127 if (grub_errno == GRUB_ERR_NONE && count == 0)
128 grub_device_iterate (iterate_device);
129 }
130 else
131 grub_device_iterate (iterate_device);
132
133 if (grub_errno == GRUB_ERR_NONE && count == 0)
134 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
135 }
136
137 static grub_err_t
138 grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
139 char **args)
140 {
141 if (argc == 0)
142 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
143
144 FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0);
145
146 return grub_errno;
147 }
148
149 static grub_command_t cmd;
150
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)
155 #else
156 GRUB_MOD_INIT(search_label)
157 #endif
158 {
159 cmd =
160 grub_register_command (COMMAND_NAME, grub_cmd_do_search,
161 N_("NAME [VARIABLE]"),
162 HELP_MESSAGE);
163 }
164
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)
169 #else
170 GRUB_MOD_FINI(search_label)
171 #endif
172 {
173 grub_unregister_command (cmd);
174 }