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 GRUB_MOD_LICENSE ("GPLv2+");
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.");
45 grub_parttool_register(const char *part_name
,
46 const grub_parttool_function_t func
,
47 const struct grub_parttool_argdesc
*args
)
49 struct grub_parttool
*cur
;
55 cur
= (struct grub_parttool
*) grub_malloc (sizeof (struct grub_parttool
));
57 cur
->name
= grub_strdup (part_name
);
58 cur
->handle
= curhandle
++;
59 for (nargs
= 0; args
[nargs
].name
!= 0; nargs
++);
61 cur
->args
= (struct grub_parttool_argdesc
*)
62 grub_calloc (nargs
+ 1, sizeof (struct grub_parttool_argdesc
));
69 grub_memcpy (cur
->args
, args
,
70 (nargs
+ 1) * sizeof (struct grub_parttool_argdesc
));
78 grub_parttool_unregister (int handle
)
80 struct grub_parttool
*prev
= 0, *cur
, *t
;
81 for (cur
= parts
; cur
; )
82 if (cur
->handle
== handle
)
84 grub_free (cur
->args
);
85 grub_free (cur
->name
);
87 prev
->next
= cur
->next
;
100 grub_dl_unref (mymod
);
104 show_help (grub_device_t dev
)
107 struct grub_parttool
*cur
;
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_xputs (_("=VAL"));
133 case GRUB_PARTTOOL_ARG_END
:
136 while (spacing
-- > 0)
138 grub_puts_ (curarg
->desc
);
142 grub_printf_ (N_("Sorry, no parttool is available for %s\n"),
143 dev
->disk
->partition
->partmap
->name
);
144 return GRUB_ERR_NONE
;
148 grub_cmd_parttool (grub_command_t cmd
__attribute__ ((unused
)),
149 int argc
, char **args
)
152 struct grub_parttool
*cur
, *ptool
;
155 grub_err_t err
= GRUB_ERR_NONE
;
159 grub_puts_ (helpmsg
);
160 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "too few arguments");
163 if (args
[0][0] == '(' && args
[0][grub_strlen (args
[0]) - 1] == ')')
165 args
[0][grub_strlen (args
[0]) - 1] = 0;
166 dev
= grub_device_open (args
[0] + 1);
167 args
[0][grub_strlen (args
[0]) - 1] = ')';
170 dev
= grub_device_open (args
[0]);
177 grub_device_close (dev
);
178 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "not a disk");
181 if (! dev
->disk
->partition
)
183 grub_device_close (dev
);
184 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "not a partition");
188 if (! grub_no_modules
)
191 prefix
= grub_env_get ("prefix");
196 filename
= grub_xasprintf ("%s/" GRUB_TARGET_CPU
"-" GRUB_PLATFORM
197 "/parttool.lst", prefix
);
202 file
= grub_file_open (filename
, GRUB_FILE_TYPE_GRUB_MODULE_LIST
);
206 for (;; grub_free(buf
))
210 buf
= grub_file_getline (file
);
216 while (grub_isspace (name
[0]))
219 if (! grub_isgraph (name
[0]))
222 p
= grub_strchr (name
, ':');
228 while (*p
== ' ' || *p
== '\t')
231 if (! grub_isgraph (*p
))
234 if (grub_strcmp (name
, dev
->disk
->partition
->partmap
->name
)
241 grub_file_close (file
);
244 grub_free (filename
);
248 grub_errno
= GRUB_ERR_NONE
;
253 err
= show_help (dev
);
254 grub_device_close (dev
);
258 for (i
= 1; i
< argc
; i
++)
259 if (grub_strcmp (args
[i
], "help") == 0)
261 err
= show_help (dev
);
262 grub_device_close (dev
);
266 parsed
= (int *) grub_calloc (argc
, sizeof (int));
268 for (i
= 1; i
< argc
; i
++)
271 struct grub_parttool_argdesc
*curarg
;
272 struct grub_parttool_args
*pargs
;
273 for (cur
= parts
; cur
; cur
= cur
->next
)
274 if (grub_strcmp (dev
->disk
->partition
->partmap
->name
, cur
->name
) == 0)
276 for (curarg
= cur
->args
; curarg
->name
; curarg
++)
277 if (grub_strncmp (curarg
->name
, args
[i
],
278 grub_strlen (curarg
->name
)) == 0
279 && ((curarg
->type
== GRUB_PARTTOOL_ARG_BOOL
280 && (args
[i
][grub_strlen (curarg
->name
)] == '+'
281 || args
[i
][grub_strlen (curarg
->name
)] == '-'
282 || args
[i
][grub_strlen (curarg
->name
)] == 0))
283 || (curarg
->type
== GRUB_PARTTOOL_ARG_VAL
284 && args
[i
][grub_strlen (curarg
->name
)] == '=')))
293 grub_device_close (dev
);
294 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("unknown argument `%s'"),
298 pargs
= (struct grub_parttool_args
*)
299 grub_calloc (ptool
->nargs
, sizeof (struct grub_parttool_args
));
300 for (j
= i
; j
< argc
; j
++)
303 for (curarg
= ptool
->args
; curarg
->name
; curarg
++)
304 if (grub_strncmp (curarg
->name
, args
[j
],
305 grub_strlen (curarg
->name
)) == 0
306 && ((curarg
->type
== GRUB_PARTTOOL_ARG_BOOL
307 && (args
[j
][grub_strlen (curarg
->name
)] == '+'
308 || args
[j
][grub_strlen (curarg
->name
)] == '-'
309 || args
[j
][grub_strlen (curarg
->name
)] == 0))
310 || (curarg
->type
== GRUB_PARTTOOL_ARG_VAL
311 && args
[j
][grub_strlen (curarg
->name
)] == '=')))
314 pargs
[curarg
- ptool
->args
].set
= 1;
315 switch (curarg
->type
)
317 case GRUB_PARTTOOL_ARG_BOOL
:
318 pargs
[curarg
- ptool
->args
].bool
319 = (args
[j
][grub_strlen (curarg
->name
)] != '-');
322 case GRUB_PARTTOOL_ARG_VAL
:
323 pargs
[curarg
- ptool
->args
].str
324 = (args
[j
] + grub_strlen (curarg
->name
) + 1);
327 case GRUB_PARTTOOL_ARG_END
:
333 err
= ptool
->func (dev
, pargs
);
340 grub_device_close (dev
);
344 static grub_command_t cmd
;
346 GRUB_MOD_INIT(parttool
)
349 cmd
= grub_register_command ("parttool", grub_cmd_parttool
,
350 N_("PARTITION COMMANDS"),
354 GRUB_MOD_FINI(parttool
)
356 grub_unregister_command (cmd
);