1
/* parttool.c - common dispatcher and parser for partition operations */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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.
21
#include <grub/types.h>
22
#include <grub/misc.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>
34
static struct grub_parttool *parts = 0;
35
static int curhandle = 0;
36
static grub_dl_t mymod;
37
static char helpmsg[] =
38
"Perform COMMANDS on partition.\n"
39
"Use \"parttool PARTITION help\" for the list "
40
"of available commands.";
43
grub_parttool_register(const char *part_name,
44
const grub_parttool_function_t func,
45
const struct grub_parttool_argdesc *args)
47
struct grub_parttool *cur;
53
cur = (struct grub_parttool *) grub_malloc (sizeof (struct grub_parttool));
55
cur->name = grub_strdup (part_name);
56
cur->handle = curhandle++;
57
for (nargs = 0; args[nargs].name != 0; nargs++);
59
cur->args = (struct grub_parttool_argdesc *)
60
grub_malloc ((nargs + 1) * sizeof (struct grub_parttool_argdesc));
61
grub_memcpy (cur->args, args,
62
(nargs + 1) * sizeof (struct grub_parttool_argdesc));
70
grub_parttool_unregister (int handle)
72
struct grub_parttool *prev = 0, *cur, *t;
73
for (cur = parts; cur; )
74
if (cur->handle == handle)
76
grub_free (cur->args);
77
grub_free (cur->name);
79
prev->next = cur->next;
92
grub_dl_unref (mymod);
96
grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
97
int argc, char **args)
100
struct grub_parttool *cur, *ptool;
103
grub_err_t err = GRUB_ERR_NONE;
105
auto grub_err_t show_help (void);
106
grub_err_t show_help (void)
109
for (cur = parts; cur; cur = cur->next)
110
if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
112
struct grub_parttool_argdesc *curarg;
114
for (curarg = cur->args; curarg->name; curarg++)
118
spacing -= grub_strlen (curarg->name);
119
grub_printf ("%s", curarg->name);
121
switch (curarg->type)
123
case GRUB_PARTTOOL_ARG_BOOL:
128
case GRUB_PARTTOOL_ARG_VAL:
129
grub_printf ("=VAL");
133
case GRUB_PARTTOOL_ARG_END:
136
while (spacing-- > 0)
138
grub_printf ("%s\n", curarg->desc);
142
grub_printf ("Sorry no parttool is available for %s\n",
143
dev->disk->partition->partmap->name);
144
return GRUB_ERR_NONE;
149
grub_printf ("%s\n", helpmsg);
150
return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
153
if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
155
args[0][grub_strlen (args[0]) - 1] = 0;
156
dev = grub_device_open (args[0] + 1);
157
args[0][grub_strlen (args[0]) - 1] = ')';
160
dev = grub_device_open (args[0]);
167
grub_device_close (dev);
168
return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
171
if (! dev->disk->partition)
173
grub_device_close (dev);
174
return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
181
prefix = grub_env_get ("prefix");
186
filename = grub_xasprintf ("%s/parttool.lst", prefix);
191
file = grub_file_open (filename);
195
for (;; grub_free(buf))
199
buf = grub_file_getline (file);
206
if (! grub_isgraph (name[0]))
209
p = grub_strchr (name, ':');
217
if (! grub_isgraph (*p))
220
if (grub_strcmp (name, dev->disk->partition->partmap->name)
227
grub_file_close (file);
230
grub_free (filename);
234
grub_errno = GRUB_ERR_NONE;
241
for (i = 1; i < argc; i++)
242
if (grub_strcmp (args[i], "help") == 0)
245
parsed = (int *) grub_zalloc (argc * sizeof (int));
247
for (i = 1; i < argc; i++)
250
struct grub_parttool_argdesc *curarg;
251
struct grub_parttool_args *pargs;
252
for (cur = parts; cur; cur = cur->next)
253
if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
255
for (curarg = cur->args; curarg->name; curarg++)
256
if (grub_strncmp (curarg->name, args[i],
257
grub_strlen (curarg->name)) == 0
258
&& ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
259
&& (args[i][grub_strlen (curarg->name)] == '+'
260
|| args[i][grub_strlen (curarg->name)] == '-'
261
|| args[i][grub_strlen (curarg->name)] == 0))
262
|| (curarg->type == GRUB_PARTTOOL_ARG_VAL
263
&& args[i][grub_strlen (curarg->name)] == '=')))
270
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised argument %s",
273
pargs = (struct grub_parttool_args *)
274
grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
275
for (j = i; j < argc; j++)
278
for (curarg = ptool->args; curarg->name; curarg++)
279
if (grub_strncmp (curarg->name, args[i],
280
grub_strlen (curarg->name)) == 0
281
&& ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
282
&& (args[j][grub_strlen (curarg->name)] == '+'
283
|| args[j][grub_strlen (curarg->name)] == '-'
284
|| args[j][grub_strlen (curarg->name)] == 0))
285
|| (curarg->type == GRUB_PARTTOOL_ARG_VAL
286
&& args[j][grub_strlen (curarg->name)] == '=')))
289
pargs[curarg - ptool->args].set = 1;
290
switch (curarg->type)
292
case GRUB_PARTTOOL_ARG_BOOL:
293
pargs[curarg - ptool->args].bool
294
= (args[j][grub_strlen (curarg->name)] != '-');
297
case GRUB_PARTTOOL_ARG_VAL:
298
pargs[curarg - ptool->args].str
299
= (args[j] + grub_strlen (curarg->name) + 1);
302
case GRUB_PARTTOOL_ARG_END:
308
err = ptool->func (dev, pargs);
315
grub_device_close (dev);
319
static grub_command_t cmd;
321
GRUB_MOD_INIT(parttool)
324
cmd = grub_register_command ("parttool", grub_cmd_parttool,
325
N_("PARTITION COMMANDS"),
329
GRUB_MOD_FINI(parttool)
331
grub_unregister_command (cmd);