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+");
38 struct cache_entry
*next
;
43 static struct cache_entry
*cache
;
45 /* Context for FUNC_NAME. */
57 /* Helper for FUNC_NAME. */
59 iterate_device (const char *name
, void *data
)
61 struct search_ctx
*ctx
= data
;
64 /* Skip floppy drives when requested. */
66 name
[0] == 'f' && name
[1] == 'd' && name
[2] >= '0' && name
[2] <= '9')
69 #ifdef DO_SEARCH_FS_UUID
70 #define compare_fn grub_strcasecmp
72 #define compare_fn grub_strcmp
80 buf
= grub_xasprintf ("(%s)%s", name
, ctx
->key
);
84 grub_file_filter_disable_compression ();
85 file
= grub_file_open (buf
);
89 grub_file_close (file
);
95 /* SEARCH_FS_UUID or SEARCH_LABEL */
100 dev
= grub_device_open (name
);
103 fs
= grub_fs_probe (dev
);
105 #ifdef DO_SEARCH_FS_UUID
108 #define read_fn label
111 if (fs
&& fs
->read_fn
)
113 fs
->read_fn (dev
, &quid
);
115 if (grub_errno
== GRUB_ERR_NONE
&& quid
)
117 if (compare_fn (quid
, ctx
->key
) == 0)
124 grub_device_close (dev
);
129 if (!ctx
->is_cache
&& found
&& ctx
->count
== 0)
131 struct cache_entry
*cache_ent
;
132 cache_ent
= grub_malloc (sizeof (*cache_ent
));
135 cache_ent
->key
= grub_strdup (ctx
->key
);
136 cache_ent
->value
= grub_strdup (name
);
137 if (cache_ent
->value
&& cache_ent
->key
)
139 cache_ent
->next
= cache
;
144 grub_free (cache_ent
->value
);
145 grub_free (cache_ent
->key
);
146 grub_free (cache_ent
);
147 grub_errno
= GRUB_ERR_NONE
;
151 grub_errno
= GRUB_ERR_NONE
;
158 grub_env_set (ctx
->var
, name
);
160 grub_printf (" %s", name
);
163 grub_errno
= GRUB_ERR_NONE
;
164 return (found
&& ctx
->var
);
167 /* Helper for FUNC_NAME. */
169 part_hook (grub_disk_t disk
, const grub_partition_t partition
, void *data
)
171 struct search_ctx
*ctx
= data
;
172 char *partition_name
, *devname
;
175 partition_name
= grub_partition_get_name (partition
);
176 if (! partition_name
)
179 devname
= grub_xasprintf ("%s,%s", disk
->name
, partition_name
);
180 grub_free (partition_name
);
183 ret
= iterate_device (devname
, ctx
);
189 /* Helper for FUNC_NAME. */
191 try (struct search_ctx
*ctx
)
194 struct cache_entry
**prev
;
195 struct cache_entry
*cache_ent
;
197 for (prev
= &cache
, cache_ent
= *prev
; cache_ent
;
198 prev
= &cache_ent
->next
, cache_ent
= *prev
)
199 if (compare_fn (cache_ent
->key
, ctx
->key
) == 0)
204 if (iterate_device (cache_ent
->value
, ctx
))
210 /* Cache entry was outdated. Remove it. */
213 grub_free (cache_ent
->key
);
214 grub_free (cache_ent
->value
);
215 grub_free (cache_ent
);
216 *prev
= cache_ent
->next
;
220 for (i
= 0; i
< ctx
->nhints
; i
++)
223 if (!ctx
->hints
[i
][0])
225 end
= ctx
->hints
[i
] + grub_strlen (ctx
->hints
[i
]) - 1;
228 if (iterate_device (ctx
->hints
[i
], ctx
))
238 dev
= grub_device_open (ctx
->hints
[i
]);
247 grub_device_close (dev
);
252 ret
= grub_partition_iterate (dev
->disk
, part_hook
, ctx
);
255 grub_device_close (dev
);
260 grub_device_iterate (iterate_device
, ctx
);
264 FUNC_NAME (const char *key
, const char *var
, int no_floppy
,
265 char **hints
, unsigned nhints
)
267 struct search_ctx ctx
= {
270 .no_floppy
= no_floppy
,
276 grub_fs_autoload_hook_t saved_autoload
;
278 /* First try without autoloading if we're setting variable. */
281 saved_autoload
= grub_fs_autoload_hook
;
282 grub_fs_autoload_hook
= 0;
285 /* Restore autoload hook. */
286 grub_fs_autoload_hook
= saved_autoload
;
288 /* Retry with autoload if nothing found. */
289 if (grub_errno
== GRUB_ERR_NONE
&& ctx
.count
== 0)
295 if (grub_errno
== GRUB_ERR_NONE
&& ctx
.count
== 0)
296 grub_error (GRUB_ERR_FILE_NOT_FOUND
, "no such device: %s", key
);
300 grub_cmd_do_search (grub_command_t cmd
__attribute__ ((unused
)), int argc
,
304 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("one argument expected"));
306 FUNC_NAME (args
[0], argc
== 1 ? 0 : args
[1], 0, (args
+ 2),
307 argc
> 2 ? argc
- 2 : 0);
312 static grub_command_t cmd
;
314 #ifdef DO_SEARCH_FILE
315 GRUB_MOD_INIT(search_fs_file
)
316 #elif defined (DO_SEARCH_FS_UUID)
317 GRUB_MOD_INIT(search_fs_uuid
)
319 GRUB_MOD_INIT(search_label
)
323 grub_register_command (COMMAND_NAME
, grub_cmd_do_search
,
324 N_("NAME [VARIABLE] [HINTS]"),
328 #ifdef DO_SEARCH_FILE
329 GRUB_MOD_FINI(search_fs_file
)
330 #elif defined (DO_SEARCH_FS_UUID)
331 GRUB_MOD_FINI(search_fs_uuid
)
333 GRUB_MOD_FINI(search_label
)
336 grub_unregister_command (cmd
);