]> git.proxmox.com Git - grub2.git/blame - grub-core/commands/parttool.c
probe: Support probing for partition UUID with --part-uuid
[grub2.git] / grub-core / commands / parttool.c
CommitLineData
ea761d40 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>
77a79592 32#include <grub/i18n.h>
ea761d40 33
e745cf0c
VS
34GRUB_MOD_LICENSE ("GPLv2+");
35
ea761d40 36static struct grub_parttool *parts = 0;
37static int curhandle = 0;
38static grub_dl_t mymod;
e4343593 39static char helpmsg[] =
6e0632e2 40 N_("Perform COMMANDS on partition.\n"
805a8dcc 41 "Use `parttool PARTITION help' for the list "
6e0632e2 42 "of available commands.");
ea761d40 43
b39f9d20 44int
45grub_parttool_register(const char *part_name,
ea761d40 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
ea761d40 52 if (! parts)
53 grub_dl_ref (mymod);
ea761d40 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;
b39f9d20 61 cur->args = (struct grub_parttool_argdesc *)
ea761d40 62 grub_malloc ((nargs + 1) * sizeof (struct grub_parttool_argdesc));
b39f9d20 63 grub_memcpy (cur->args, args,
ea761d40 64 (nargs + 1) * sizeof (struct grub_parttool_argdesc));
b39f9d20 65
ea761d40 66 cur->func = func;
67 parts = cur;
68 return cur->handle;
69}
70
71void
72grub_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 }
ea761d40 93 if (! parts)
94 grub_dl_unref (mymod);
ea761d40 95}
96
5aec2afe
VS
97static grub_err_t
98show_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)
16f7455b 136 grub_printf_ (N_("Sorry, no parttool is available for %s\n"),
5aec2afe
VS
137 dev->disk->partition->partmap->name);
138 return GRUB_ERR_NONE;
139}
140
ea761d40 141static grub_err_t
142grub_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
e4343593 151 if (argc < 1)
152 {
6e0632e2 153 grub_puts_ (helpmsg);
e4343593 154 return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
155 }
ea761d40 156
157 if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
158 {
159 args[0][grub_strlen (args[0]) - 1] = 0;
b39f9d20 160 dev = grub_device_open (args[0] + 1);
ea761d40 161 args[0][grub_strlen (args[0]) - 1] = ')';
162 }
163 else
b39f9d20 164 dev = grub_device_open (args[0]);
ea761d40 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
e4343593 181 /* Load modules. */
a6393224 182 if (! grub_no_modules)
e4343593 183 {
184 const char *prefix;
185 prefix = grub_env_get ("prefix");
186 if (prefix)
ea761d40 187 {
e4343593 188 char *filename;
ea761d40 189
92cd0f6e
VS
190 filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
191 "/parttool.lst", prefix);
e4343593 192 if (filename)
193 {
194 grub_file_t file;
b39f9d20 195
ca0a4f68 196 file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE_LIST);
e4343593 197 if (file)
198 {
199 char *buf = 0;
200 for (;; grub_free(buf))
201 {
202 char *p, *name;
ea761d40 203
e4343593 204 buf = grub_file_getline (file);
b39f9d20 205
e4343593 206 if (! buf)
ea761d40 207 break;
b39f9d20 208
e4343593 209 name = buf;
9fdb2d7b
VS
210 while (grub_isspace (name[0]))
211 name++;
ea761d40 212
e4343593 213 if (! grub_isgraph (name[0]))
214 continue;
b39f9d20 215
e4343593 216 p = grub_strchr (name, ':');
217 if (! p)
218 continue;
b39f9d20 219
e4343593 220 *p = '\0';
9fdb2d7b
VS
221 p++;
222 while (*p == ' ' || *p == '\t')
223 p++;
e4343593 224
225 if (! grub_isgraph (*p))
226 continue;
b39f9d20 227
e4343593 228 if (grub_strcmp (name, dev->disk->partition->partmap->name)
229 != 0)
230 continue;
b39f9d20 231
e4343593 232 grub_dl_load (p);
233 }
b39f9d20 234
e4343593 235 grub_file_close (file);
236 }
b39f9d20 237
e4343593 238 grub_free (filename);
239 }
ea761d40 240 }
e4343593 241 /* Ignore errors. */
242 grub_errno = GRUB_ERR_NONE;
243 }
e4343593 244
245 if (argc == 1)
4947f11b
VS
246 {
247 err = show_help (dev);
248 grub_device_close (dev);
249 return err;
250 }
e4343593 251
252 for (i = 1; i < argc; i++)
253 if (grub_strcmp (args[i], "help") == 0)
4947f11b
VS
254 {
255 err = show_help (dev);
256 grub_device_close (dev);
257 return err;
258 }
e4343593 259
eab58da2 260 parsed = (int *) grub_zalloc (argc * sizeof (int));
ea761d40 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)
e4343593 268 if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
ea761d40 269 {
270 for (curarg = cur->args; curarg->name; curarg++)
b39f9d20 271 if (grub_strncmp (curarg->name, args[i],
e4343593 272 grub_strlen (curarg->name)) == 0
b39f9d20 273 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
274 && (args[i][grub_strlen (curarg->name)] == '+'
e4343593 275 || args[i][grub_strlen (curarg->name)] == '-'
276 || args[i][grub_strlen (curarg->name)] == 0))
ea761d40 277 || (curarg->type == GRUB_PARTTOOL_ARG_VAL
278 && args[i][grub_strlen (curarg->name)] == '=')))
b39f9d20 279
ea761d40 280 break;
281 if (curarg->name)
282 break;
283 }
284 if (! cur)
3db4f05a 285 {
5db2190f 286 grub_free (parsed);
3db4f05a
AB
287 grub_device_close (dev);
288 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unknown argument `%s'"),
ea761d40 289 args[i]);
3db4f05a 290 }
ea761d40 291 ptool = cur;
b39f9d20 292 pargs = (struct grub_parttool_args *)
eab58da2 293 grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
ea761d40 294 for (j = i; j < argc; j++)
e4343593 295 if (! parsed[j])
ea761d40 296 {
297 for (curarg = ptool->args; curarg->name; curarg++)
35139e8a 298 if (grub_strncmp (curarg->name, args[j],
e4343593 299 grub_strlen (curarg->name)) == 0
b39f9d20 300 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
301 && (args[j][grub_strlen (curarg->name)] == '+'
e4343593 302 || args[j][grub_strlen (curarg->name)] == '-'
303 || args[j][grub_strlen (curarg->name)] == 0))
ea761d40 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:
b39f9d20 312 pargs[curarg - ptool->args].bool
ea761d40 313 = (args[j][grub_strlen (curarg->name)] != '-');
314 break;
315
316 case GRUB_PARTTOOL_ARG_VAL:
b39f9d20 317 pargs[curarg - ptool->args].str
ea761d40 318 = (args[j] + grub_strlen (curarg->name) + 1);
319 break;
b39f9d20 320
ea761d40 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
e4343593 333 grub_free (parsed);
ea761d40 334 grub_device_close (dev);
335 return err;
336}
337
338static grub_command_t cmd;
339
340GRUB_MOD_INIT(parttool)
341{
ea761d40 342 mymod = mod;
b39f9d20 343 cmd = grub_register_command ("parttool", grub_cmd_parttool,
77a79592 344 N_("PARTITION COMMANDS"),
e4343593 345 helpmsg);
ea761d40 346}
347
348GRUB_MOD_FINI(parttool)
349{
350 grub_unregister_command (cmd);
351}