]>
Commit | Line | Data |
---|---|---|
6a85ce79 | 1 | /* search.c - search devices based on a file or a filesystem label */ |
2 | /* | |
3 | * GRUB -- GRand Unified Bootloader | |
2bf5885a | 4 | * Copyright (C) 2005,2007,2008,2009 Free Software Foundation, Inc. |
6a85ce79 | 5 | * |
5a79f472 | 6 | * GRUB is free software: you can redistribute it and/or modify |
6a85ce79 | 7 | * it under the terms of the GNU General Public License as published by |
5a79f472 | 8 | * the Free Software Foundation, either version 3 of the License, or |
6a85ce79 | 9 | * (at your option) any later version. |
10 | * | |
5a79f472 | 11 | * GRUB is distributed in the hope that it will be useful, |
6a85ce79 | 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 | |
5a79f472 | 17 | * along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
6a85ce79 | 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> | |
6a85ce79 | 25 | #include <grub/device.h> |
26 | #include <grub/file.h> | |
27 | #include <grub/env.h> | |
937d332d | 28 | #include <grub/command.h> |
49968932 | 29 | #include <grub/search.h> |
ec5f98ab | 30 | #include <grub/i18n.h> |
6a85ce79 | 31 | |
063e925f | 32 | void |
33 | FUNC_NAME (const char *key, const char *var, int no_floppy) | |
6a85ce79 | 34 | { |
35 | int count = 0; | |
e9a925da | 36 | grub_fs_autoload_hook_t saved_autoload; |
62191274 | 37 | |
6a85ce79 | 38 | auto int iterate_device (const char *name); |
6a85ce79 | 39 | int iterate_device (const char *name) |
7d8a52d3 | 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 | ||
063e925f | 48 | #ifdef DO_SEARCH_FILE |
7d8a52d3 | 49 | { |
8b442f3f | 50 | char *buf; |
7d8a52d3 | 51 | grub_file_t file; |
52 | ||
61eb45ee | 53 | buf = grub_xasprintf ("(%s)%s", name, key); |
8b442f3f | 54 | if (! buf) |
7d8a52d3 | 55 | return 1; |
56 | ||
7d8a52d3 | 57 | file = grub_file_open (buf); |
58 | if (file) | |
59 | { | |
60 | found = 1; | |
61 | grub_file_close (file); | |
62 | } | |
8b442f3f | 63 | grub_free (buf); |
7d8a52d3 | 64 | } |
063e925f | 65 | #else |
7d8a52d3 | 66 | { |
063e925f | 67 | /* SEARCH_FS_UUID or SEARCH_LABEL */ |
7d8a52d3 | 68 | grub_device_t dev; |
69 | grub_fs_t fs; | |
7d8a52d3 | 70 | char *quid; |
71 | ||
72 | dev = grub_device_open (name); | |
73 | if (dev) | |
74 | { | |
75 | fs = grub_fs_probe (dev); | |
7d8a52d3 | 76 | |
063e925f | 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) | |
7d8a52d3 | 86 | { |
063e925f | 87 | fs->read_fn (dev, &quid); |
7d8a52d3 | 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 | } | |
063e925f | 101 | #endif |
7d8a52d3 | 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 | } | |
b39f9d20 | 115 | |
e9a925da | 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); | |
b39f9d20 | 132 | |
6a85ce79 | 133 | if (grub_errno == GRUB_ERR_NONE && count == 0) |
7d8a52d3 | 134 | grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key); |
6a85ce79 | 135 | } |
136 | ||
137 | static grub_err_t | |
063e925f | 138 | grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc, |
139 | char **args) | |
6a85ce79 | 140 | { |
6a85ce79 | 141 | if (argc == 0) |
063e925f | 142 | return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified"); |
6a85ce79 | 143 | |
063e925f | 144 | FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0); |
6a85ce79 | 145 | |
146 | return grub_errno; | |
147 | } | |
148 | ||
063e925f | 149 | static grub_command_t cmd; |
b1b797cb | 150 | |
49968932 | 151 | #ifdef DO_SEARCH_FILE |
19a9fb83 | 152 | GRUB_MOD_INIT(search_fs_file) |
49968932 | 153 | #elif defined (DO_SEARCH_FS_UUID) |
063e925f | 154 | GRUB_MOD_INIT(search_fs_uuid) |
155 | #else | |
19a9fb83 | 156 | GRUB_MOD_INIT(search_label) |
063e925f | 157 | #endif |
6a85ce79 | 158 | { |
b1b797cb | 159 | cmd = |
063e925f | 160 | grub_register_command (COMMAND_NAME, grub_cmd_do_search, |
77a79592 | 161 | N_("NAME [VARIABLE]"), |
40e3a41f | 162 | HELP_MESSAGE); |
6a85ce79 | 163 | } |
164 | ||
49968932 | 165 | #ifdef DO_SEARCH_FILE |
19a9fb83 | 166 | GRUB_MOD_FINI(search_fs_file) |
49968932 | 167 | #elif defined (DO_SEARCH_FS_UUID) |
063e925f | 168 | GRUB_MOD_FINI(search_fs_uuid) |
169 | #else | |
19a9fb83 | 170 | GRUB_MOD_FINI(search_label) |
063e925f | 171 | #endif |
6a85ce79 | 172 | { |
063e925f | 173 | grub_unregister_command (cmd); |
6a85ce79 | 174 | } |