]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/parttool.c
probe: Support probing for partition UUID with --part-uuid
[grub2.git] / grub-core / commands / parttool.c
1 /* parttool.c - common dispatcher and parser for partition operations */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
5 *
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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 this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <grub/types.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/err.h>
25 #include <grub/dl.h>
26 #include <grub/normal.h>
27 #include <grub/device.h>
28 #include <grub/disk.h>
29 #include <grub/partition.h>
30 #include <grub/parttool.h>
31 #include <grub/command.h>
32 #include <grub/i18n.h>
33
34 GRUB_MOD_LICENSE ("GPLv2+");
35
36 static struct grub_parttool *parts = 0;
37 static int curhandle = 0;
38 static grub_dl_t mymod;
39 static char helpmsg[] =
40 N_("Perform COMMANDS on partition.\n"
41 "Use `parttool PARTITION help' for the list "
42 "of available commands.");
43
44 int
45 grub_parttool_register(const char *part_name,
46 const grub_parttool_function_t func,
47 const struct grub_parttool_argdesc *args)
48 {
49 struct grub_parttool *cur;
50 int nargs = 0;
51
52 if (! parts)
53 grub_dl_ref (mymod);
54
55 cur = (struct grub_parttool *) grub_malloc (sizeof (struct grub_parttool));
56 cur->next = parts;
57 cur->name = grub_strdup (part_name);
58 cur->handle = curhandle++;
59 for (nargs = 0; args[nargs].name != 0; nargs++);
60 cur->nargs = nargs;
61 cur->args = (struct grub_parttool_argdesc *)
62 grub_malloc ((nargs + 1) * sizeof (struct grub_parttool_argdesc));
63 grub_memcpy (cur->args, args,
64 (nargs + 1) * sizeof (struct grub_parttool_argdesc));
65
66 cur->func = func;
67 parts = cur;
68 return cur->handle;
69 }
70
71 void
72 grub_parttool_unregister (int handle)
73 {
74 struct grub_parttool *prev = 0, *cur, *t;
75 for (cur = parts; cur; )
76 if (cur->handle == handle)
77 {
78 grub_free (cur->args);
79 grub_free (cur->name);
80 if (prev)
81 prev->next = cur->next;
82 else
83 parts = cur->next;
84 t = cur;
85 cur = cur->next;
86 grub_free (t);
87 }
88 else
89 {
90 prev = cur;
91 cur = cur->next;
92 }
93 if (! parts)
94 grub_dl_unref (mymod);
95 }
96
97 static grub_err_t
98 show_help (grub_device_t dev)
99 {
100 int found = 0;
101 struct grub_parttool *cur;
102
103 for (cur = parts; cur; cur = cur->next)
104 if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
105 {
106 struct grub_parttool_argdesc *curarg;
107 found = 1;
108 for (curarg = cur->args; curarg->name; curarg++)
109 {
110 int spacing = 20;
111
112 spacing -= grub_strlen (curarg->name);
113 grub_printf ("%s", curarg->name);
114
115 switch (curarg->type)
116 {
117 case GRUB_PARTTOOL_ARG_BOOL:
118 grub_printf ("+/-");
119 spacing -= 3;
120 break;
121
122 case GRUB_PARTTOOL_ARG_VAL:
123 grub_xputs (_("=VAL"));
124 spacing -= 4;
125 break;
126
127 case GRUB_PARTTOOL_ARG_END:
128 break;
129 }
130 while (spacing-- > 0)
131 grub_printf (" ");
132 grub_puts_ (curarg->desc);
133 }
134 }
135 if (! found)
136 grub_printf_ (N_("Sorry, no parttool is available for %s\n"),
137 dev->disk->partition->partmap->name);
138 return GRUB_ERR_NONE;
139 }
140
141 static grub_err_t
142 grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
143 int argc, char **args)
144 {
145 grub_device_t dev;
146 struct grub_parttool *cur, *ptool;
147 int *parsed;
148 int i, j;
149 grub_err_t err = GRUB_ERR_NONE;
150
151 if (argc < 1)
152 {
153 grub_puts_ (helpmsg);
154 return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
155 }
156
157 if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
158 {
159 args[0][grub_strlen (args[0]) - 1] = 0;
160 dev = grub_device_open (args[0] + 1);
161 args[0][grub_strlen (args[0]) - 1] = ')';
162 }
163 else
164 dev = grub_device_open (args[0]);
165
166 if (! dev)
167 return grub_errno;
168
169 if (! dev->disk)
170 {
171 grub_device_close (dev);
172 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
173 }
174
175 if (! dev->disk->partition)
176 {
177 grub_device_close (dev);
178 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
179 }
180
181 /* Load modules. */
182 if (! grub_no_modules)
183 {
184 const char *prefix;
185 prefix = grub_env_get ("prefix");
186 if (prefix)
187 {
188 char *filename;
189
190 filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
191 "/parttool.lst", prefix);
192 if (filename)
193 {
194 grub_file_t file;
195
196 file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE_LIST);
197 if (file)
198 {
199 char *buf = 0;
200 for (;; grub_free(buf))
201 {
202 char *p, *name;
203
204 buf = grub_file_getline (file);
205
206 if (! buf)
207 break;
208
209 name = buf;
210 while (grub_isspace (name[0]))
211 name++;
212
213 if (! grub_isgraph (name[0]))
214 continue;
215
216 p = grub_strchr (name, ':');
217 if (! p)
218 continue;
219
220 *p = '\0';
221 p++;
222 while (*p == ' ' || *p == '\t')
223 p++;
224
225 if (! grub_isgraph (*p))
226 continue;
227
228 if (grub_strcmp (name, dev->disk->partition->partmap->name)
229 != 0)
230 continue;
231
232 grub_dl_load (p);
233 }
234
235 grub_file_close (file);
236 }
237
238 grub_free (filename);
239 }
240 }
241 /* Ignore errors. */
242 grub_errno = GRUB_ERR_NONE;
243 }
244
245 if (argc == 1)
246 {
247 err = show_help (dev);
248 grub_device_close (dev);
249 return err;
250 }
251
252 for (i = 1; i < argc; i++)
253 if (grub_strcmp (args[i], "help") == 0)
254 {
255 err = show_help (dev);
256 grub_device_close (dev);
257 return err;
258 }
259
260 parsed = (int *) grub_zalloc (argc * sizeof (int));
261
262 for (i = 1; i < argc; i++)
263 if (! parsed[i])
264 {
265 struct grub_parttool_argdesc *curarg;
266 struct grub_parttool_args *pargs;
267 for (cur = parts; cur; cur = cur->next)
268 if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
269 {
270 for (curarg = cur->args; curarg->name; curarg++)
271 if (grub_strncmp (curarg->name, args[i],
272 grub_strlen (curarg->name)) == 0
273 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
274 && (args[i][grub_strlen (curarg->name)] == '+'
275 || args[i][grub_strlen (curarg->name)] == '-'
276 || args[i][grub_strlen (curarg->name)] == 0))
277 || (curarg->type == GRUB_PARTTOOL_ARG_VAL
278 && args[i][grub_strlen (curarg->name)] == '=')))
279
280 break;
281 if (curarg->name)
282 break;
283 }
284 if (! cur)
285 {
286 grub_free (parsed);
287 grub_device_close (dev);
288 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unknown argument `%s'"),
289 args[i]);
290 }
291 ptool = cur;
292 pargs = (struct grub_parttool_args *)
293 grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
294 for (j = i; j < argc; j++)
295 if (! parsed[j])
296 {
297 for (curarg = ptool->args; curarg->name; curarg++)
298 if (grub_strncmp (curarg->name, args[j],
299 grub_strlen (curarg->name)) == 0
300 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
301 && (args[j][grub_strlen (curarg->name)] == '+'
302 || args[j][grub_strlen (curarg->name)] == '-'
303 || args[j][grub_strlen (curarg->name)] == 0))
304 || (curarg->type == GRUB_PARTTOOL_ARG_VAL
305 && args[j][grub_strlen (curarg->name)] == '=')))
306 {
307 parsed[j] = 1;
308 pargs[curarg - ptool->args].set = 1;
309 switch (curarg->type)
310 {
311 case GRUB_PARTTOOL_ARG_BOOL:
312 pargs[curarg - ptool->args].bool
313 = (args[j][grub_strlen (curarg->name)] != '-');
314 break;
315
316 case GRUB_PARTTOOL_ARG_VAL:
317 pargs[curarg - ptool->args].str
318 = (args[j] + grub_strlen (curarg->name) + 1);
319 break;
320
321 case GRUB_PARTTOOL_ARG_END:
322 break;
323 }
324 }
325 }
326
327 err = ptool->func (dev, pargs);
328 grub_free (pargs);
329 if (err)
330 break;
331 }
332
333 grub_free (parsed);
334 grub_device_close (dev);
335 return err;
336 }
337
338 static grub_command_t cmd;
339
340 GRUB_MOD_INIT(parttool)
341 {
342 mymod = mod;
343 cmd = grub_register_command ("parttool", grub_cmd_parttool,
344 N_("PARTITION COMMANDS"),
345 helpmsg);
346 }
347
348 GRUB_MOD_FINI(parttool)
349 {
350 grub_unregister_command (cmd);
351 }