2009-06-22 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / commands / search.c
blob5b1c0b03b2b29a2a550a7702b8278d695c8708de
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.
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>
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/extcmd.h>
30 static const struct grub_arg_option options[] =
32 {"file", 'f', 0, "search devices by a file (default)", 0, 0},
33 {"label", 'l', 0, "search devices by a filesystem label", 0, 0},
34 {"fs-uuid", 'u', 0, "search devices by a filesystem UUID", 0, 0},
35 {"set", 's', GRUB_ARG_OPTION_OPTIONAL, "set a variable to the first device found", "VAR", ARG_TYPE_STRING},
36 {"no-floppy", 'n', 0, "do not probe any floppy drive", 0, 0},
37 {0, 0, 0, 0, 0, 0}
40 enum options
42 SEARCH_FILE,
43 SEARCH_LABEL,
44 SEARCH_FS_UUID,
45 SEARCH_SET,
46 SEARCH_NO_FLOPPY,
49 static void
50 search_fs (const char *key, const char *var, int no_floppy, int is_uuid)
52 int count = 0;
53 auto int iterate_device (const char *name);
55 int iterate_device (const char *name)
57 grub_device_t dev;
58 int abort = 0;
60 /* Skip floppy drives when requested. */
61 if (no_floppy &&
62 name[0] == 'f' && name[1] == 'd' &&
63 name[2] >= '0' && name[2] <= '9')
64 return 0;
66 dev = grub_device_open (name);
67 if (dev)
69 grub_fs_t fs;
71 fs = grub_fs_probe (dev);
73 #define QUID(x) (is_uuid ? (x)->uuid : (x)->label)
75 if (fs && QUID(fs))
77 char *quid;
79 (QUID(fs)) (dev, &quid);
80 if (grub_errno == GRUB_ERR_NONE && quid)
82 if (grub_strcmp (quid, key) == 0)
84 /* Found! */
85 count++;
86 if (var)
88 grub_env_set (var, name);
89 abort = 1;
91 else
92 grub_printf (" %s", name);
95 grub_free (quid);
99 grub_device_close (dev);
102 grub_errno = GRUB_ERR_NONE;
103 return abort;
106 grub_device_iterate (iterate_device);
108 if (count == 0)
109 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
112 static void
113 search_file (const char *key, const char *var, int no_floppy)
115 int count = 0;
116 char *buf = 0;
117 auto int iterate_device (const char *name);
119 int iterate_device (const char *name)
121 grub_size_t len;
122 char *p;
123 grub_file_t file;
124 int abort = 0;
126 /* Skip floppy drives when requested. */
127 if (no_floppy &&
128 name[0] == 'f' && name[1] == 'd' &&
129 name[2] >= '0' && name[2] <= '9')
130 return 0;
132 len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
133 p = grub_realloc (buf, len);
134 if (! p)
135 return 1;
137 buf = p;
138 grub_sprintf (buf, "(%s)%s", name, key);
140 file = grub_file_open (buf);
141 if (file)
143 /* Found! */
144 count++;
145 if (var)
147 grub_env_set (var, name);
148 abort = 1;
150 else
151 grub_printf (" %s", name);
153 grub_file_close (file);
156 grub_errno = GRUB_ERR_NONE;
157 return abort;
160 grub_device_iterate (iterate_device);
162 grub_free (buf);
164 if (grub_errno == GRUB_ERR_NONE && count == 0)
165 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device");
168 static grub_err_t
169 grub_cmd_search (grub_extcmd_t cmd, int argc, char **args)
171 struct grub_arg_list *state = cmd->state;
172 const char *var = 0;
174 if (argc == 0)
175 return grub_error (GRUB_ERR_INVALID_COMMAND, "no argument specified");
177 if (state[SEARCH_SET].set)
178 var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
180 if (state[SEARCH_LABEL].set)
181 search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, 0);
182 else if (state[SEARCH_FS_UUID].set)
183 search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, 1);
184 else if (state[SEARCH_FILE].set)
185 search_file (args[0], var, state[SEARCH_NO_FLOPPY].set);
186 else
187 return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
189 return grub_errno;
192 static grub_extcmd_t cmd;
194 GRUB_MOD_INIT(search)
196 cmd =
197 grub_register_extcmd ("search", grub_cmd_search,
198 GRUB_COMMAND_FLAG_BOTH,
199 "search [-f|-l|-u|-s] NAME",
200 "Search devices by file, filesystem label or filesystem UUID."
201 " If --set is specified, the first device found is"
202 " set to a variable. If no variable name is"
203 " specified, \"root\" is used.",
204 options);
207 GRUB_MOD_FINI(search)
209 grub_unregister_extcmd (cmd);