Print this page
fixes + mirror
Split |
Close |
Expand all |
Collapse all |
--- old/grub/grub-core/commands/search.c
+++ new/grub/grub-core/commands/search.c
1 1 /* search.c - search devices based on a file or a filesystem label */
2 2 /*
3 3 * GRUB -- GRand Unified Bootloader
4 4 * Copyright (C) 2005,2007,2008,2009 Free Software Foundation, Inc.
5 5 *
6 6 * GRUB is free software: you can redistribute it and/or modify
7 7 * it under the terms of the GNU General Public License as published by
8 8 * the Free Software Foundation, either version 3 of the License, or
9 9 * (at your option) any later version.
10 10 *
11 11 * GRUB is distributed in the hope that it will be useful,
12 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 * GNU General Public License for more details.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 *
16 16 * You should have received a copy of the GNU General Public License
17 17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 18 */
19 19
20 20 #include <grub/types.h>
21 21 #include <grub/misc.h>
22 22 #include <grub/mm.h>
23 23 #include <grub/err.h>
24 24 #include <grub/dl.h>
25 +#include <grub/zfs/zfs.h>
25 26 #include <grub/device.h>
26 27 #include <grub/file.h>
27 28 #include <grub/env.h>
28 29 #include <grub/command.h>
29 30 #include <grub/search.h>
30 31 #include <grub/i18n.h>
31 32 #include <grub/disk.h>
32 33 #include <grub/partition.h>
33 34
34 35 GRUB_MOD_LICENSE ("GPLv3+");
35 36
36 37 struct cache_entry
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
37 38 {
38 39 struct cache_entry *next;
39 40 char *key;
40 41 char *value;
41 42 };
42 43
43 44 static struct cache_entry *cache;
44 45
45 46 void
46 47 FUNC_NAME (const char *key, const char *var, int no_floppy,
47 - char **hints, unsigned nhints)
48 + char **hints, unsigned nhints, int mirror)
48 49 {
49 50 int count = 0;
50 51 int is_cache = 0;
52 + grub_uint64_t txg = 0;
51 53 grub_fs_autoload_hook_t saved_autoload;
52 54
53 55 auto int iterate_device (const char *name);
54 56 int iterate_device (const char *name)
55 57 {
56 58 int found = 0;
57 59
58 60 /* Skip floppy drives when requested. */
59 61 if (no_floppy &&
60 62 name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
61 63 return 0;
62 64
63 65 #ifdef DO_SEARCH_FS_UUID
64 66 #define compare_fn grub_strcasecmp
65 67 #else
66 68 #define compare_fn grub_strcmp
67 69 #endif
68 70
69 71 #ifdef DO_SEARCH_FILE
70 72 {
71 73 char *buf;
72 74 grub_file_t file;
73 75
74 76 buf = grub_xasprintf ("(%s)%s", name, key);
75 77 if (! buf)
76 78 return 1;
77 79
78 80 grub_file_filter_disable_compression ();
79 81 file = grub_file_open (buf);
80 82 if (file)
81 83 {
82 84 found = 1;
83 85 grub_file_close (file);
84 86 }
85 87 grub_free (buf);
86 88 }
87 89 #else
88 90 {
89 91 /* SEARCH_FS_UUID or SEARCH_LABEL */
90 92 grub_device_t dev;
91 93 grub_fs_t fs;
92 94 char *quid;
93 95
94 96 dev = grub_device_open (name);
95 97 if (dev)
96 98 {
97 99 fs = grub_fs_probe (dev);
98 100
99 101 #ifdef DO_SEARCH_FS_UUID
100 102 #define read_fn uuid
101 103 #else
102 104 #define read_fn label
103 105 #endif
104 106
105 107 if (fs && fs->read_fn)
106 108 {
107 109 fs->read_fn (dev, &quid);
108 110
109 111 if (grub_errno == GRUB_ERR_NONE && quid)
110 112 {
111 113 if (compare_fn (quid, key) == 0)
112 114 found = 1;
113 115
114 116 grub_free (quid);
115 117 }
116 118 }
117 119
118 120 grub_device_close (dev);
119 121 }
120 122 }
121 123 #endif
122 124
123 125 if (!is_cache && found && count == 0)
124 126 {
125 127 struct cache_entry *cache_ent;
126 128 cache_ent = grub_malloc (sizeof (*cache_ent));
127 129 if (cache_ent)
128 130 {
129 131 cache_ent->key = grub_strdup (key);
130 132 cache_ent->value = grub_strdup (name);
131 133 if (cache_ent->value && cache_ent->key)
132 134 {
133 135 cache_ent->next = cache;
134 136 cache = cache_ent;
135 137 }
136 138 else
137 139 {
138 140 grub_free (cache_ent->value);
139 141 grub_free (cache_ent->key);
140 142 grub_free (cache_ent);
↓ open down ↓ |
80 lines elided |
↑ open up ↑ |
141 143 grub_errno = GRUB_ERR_NONE;
142 144 }
143 145 }
144 146 else
145 147 grub_errno = GRUB_ERR_NONE;
146 148 }
147 149
148 150 if (found)
149 151 {
150 152 count++;
151 - if (var)
152 - grub_env_set (var, name);
153 - else
153 + if (var) {
154 + if (! mirror) {
155 + grub_env_set (var, name);
156 + } else {
157 + grub_uint64_t tmp_txg = 0;
158 + char * nvlist = NULL;
159 + grub_device_t dev = grub_device_open (name);
160 + if (! dev) {
161 + grub_errno = GRUB_ERR_BAD_DEVICE;
162 + return 0;
163 + }
164 + grub_errno = grub_zfs_fetch_nvlist (dev, &nvlist);
165 + grub_device_close (dev);
166 + if (grub_errno)
167 + return 0;
168 + grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_TXG, &tmp_txg);
169 + if (tmp_txg > txg) {
170 + grub_env_set (var, name);
171 + txg = tmp_txg;
172 + }
173 + grub_free (nvlist);
174 + }
175 + } else {
154 176 grub_printf (" %s", name);
177 + }
155 178 }
156 179
157 180 grub_errno = GRUB_ERR_NONE;
158 - return (found && var);
181 + return (found && var && !mirror);
159 182 }
160 183
161 184 auto int part_hook (grub_disk_t disk, const grub_partition_t partition);
162 185 int part_hook (grub_disk_t disk, const grub_partition_t partition)
163 186 {
164 187 char *partition_name, *devname;
165 188 int ret;
166 189
167 190 partition_name = grub_partition_get_name (partition);
168 191 if (! partition_name)
169 192 return 1;
170 193
171 194 devname = grub_xasprintf ("%s,%s", disk->name, partition_name);
172 195 grub_free (partition_name);
173 196 if (!devname)
174 197 return 1;
175 198 ret = iterate_device (devname);
176 199 grub_free (devname);
177 200
178 201 return ret;
179 202 }
180 203
181 204 auto void try (void);
182 205 void try (void)
183 206 {
184 207 unsigned i;
185 208 struct cache_entry **prev;
186 209 struct cache_entry *cache_ent;
187 210
188 211 for (prev = &cache, cache_ent = *prev; cache_ent;
189 212 prev = &cache_ent->next, cache_ent = *prev)
190 213 if (compare_fn (cache_ent->key, key) == 0)
191 214 break;
192 215 if (cache_ent)
193 216 {
194 217 is_cache = 1;
195 218 if (iterate_device (cache_ent->value))
196 219 {
197 220 is_cache = 0;
198 221 return;
199 222 }
200 223 is_cache = 0;
201 224 /* Cache entry was outdated. Remove it. */
202 225 if (!count)
203 226 {
204 227 grub_free (cache_ent->key);
205 228 grub_free (cache_ent->value);
206 229 grub_free (cache_ent);
207 230 *prev = cache_ent->next;
208 231 }
209 232 }
210 233
211 234 for (i = 0; i < nhints; i++)
212 235 {
213 236 char *end;
214 237 if (!hints[i][0])
215 238 continue;
216 239 end = hints[i] + grub_strlen (hints[i]) - 1;
217 240 if (*end == ',')
218 241 *end = 0;
219 242 if (iterate_device (hints[i]))
220 243 {
221 244 if (!*end)
222 245 *end = ',';
223 246 return;
224 247 }
225 248 if (!*end)
226 249 {
227 250 grub_device_t dev;
228 251 int ret;
229 252 dev = grub_device_open (hints[i]);
230 253 if (!dev)
231 254 {
232 255 if (!*end)
233 256 *end = ',';
234 257 continue;
235 258 }
236 259 if (!dev->disk)
237 260 {
238 261 grub_device_close (dev);
239 262 if (!*end)
240 263 *end = ',';
241 264 continue;
242 265 }
243 266 ret = grub_partition_iterate (dev->disk, part_hook);
244 267 if (!*end)
245 268 *end = ',';
246 269 grub_device_close (dev);
247 270 if (ret)
248 271 return;
249 272 }
250 273 }
251 274 grub_device_iterate (iterate_device);
252 275 }
253 276
254 277 /* First try without autoloading if we're setting variable. */
255 278 if (var)
256 279 {
257 280 saved_autoload = grub_fs_autoload_hook;
258 281 grub_fs_autoload_hook = 0;
259 282 try ();
260 283
261 284 /* Restore autoload hook. */
262 285 grub_fs_autoload_hook = saved_autoload;
263 286
264 287 /* Retry with autoload if nothing found. */
265 288 if (grub_errno == GRUB_ERR_NONE && count == 0)
266 289 try ();
267 290 }
268 291 else
269 292 try ();
270 293
271 294 if (grub_errno == GRUB_ERR_NONE && count == 0)
272 295 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
↓ open down ↓ |
104 lines elided |
↑ open up ↑ |
273 296 }
274 297
275 298 static grub_err_t
276 299 grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
277 300 char **args)
278 301 {
279 302 if (argc == 0)
280 303 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
281 304
282 305 FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2),
283 - argc > 2 ? argc - 2 : 0);
306 + argc > 2 ? argc - 2 : 0, 0);
284 307
285 308 return grub_errno;
286 309 }
287 310
288 311 static grub_command_t cmd;
289 312
290 313 #ifdef DO_SEARCH_FILE
291 314 GRUB_MOD_INIT(search_fs_file)
292 315 #elif defined (DO_SEARCH_FS_UUID)
293 316 GRUB_MOD_INIT(search_fs_uuid)
294 317 #else
295 318 GRUB_MOD_INIT(search_label)
296 319 #endif
297 320 {
298 321 cmd =
299 322 grub_register_command (COMMAND_NAME, grub_cmd_do_search,
300 323 N_("NAME [VARIABLE] [HINTS]"),
301 324 HELP_MESSAGE);
302 325 }
303 326
304 327 #ifdef DO_SEARCH_FILE
305 328 GRUB_MOD_FINI(search_fs_file)
306 329 #elif defined (DO_SEARCH_FS_UUID)
307 330 GRUB_MOD_FINI(search_fs_uuid)
308 331 #else
309 332 GRUB_MOD_FINI(search_label)
310 333 #endif
311 334 {
312 335 grub_unregister_command (cmd);
313 336 }
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX