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_malloc ((nargs
+ 1) * sizeof (struct grub_parttool_argdesc
));
63 grub_memcpy (cur
->args
, args
,
64 (nargs
+ 1) * sizeof (struct grub_parttool_argdesc
));
72 grub_parttool_unregister (int handle
)
74 struct grub_parttool
*prev
= 0, *cur
, *t
;
75 for (cur
= parts
; cur
; )
76 if (cur
->handle
== handle
)
78 grub_free (cur
->args
);
79 grub_free (cur
->name
);
81 prev
->next
= cur
->next
;
94 grub_dl_unref (mymod
);
98 show_help (grub_device_t dev
)
101 struct grub_parttool
*cur
;
103 for (cur
= parts
; cur
; cur
= cur
->next
)
104 if (grub_strcmp (dev
->disk
->partition
->partmap
->name
, cur
->name
) == 0)
106 struct grub_parttool_argdesc
*curarg
;
108 for (curarg
= cur
->args
; curarg
->name
; curarg
++)
112 spacing
-= grub_strlen (curarg
->name
);
113 grub_printf ("%s", curarg
->name
);
115 switch (curarg
->type
)
117 case GRUB_PARTTOOL_ARG_BOOL
:
122 case GRUB_PARTTOOL_ARG_VAL
:
123 grub_xputs (_("=VAL"));
127 case GRUB_PARTTOOL_ARG_END
:
130 while (spacing
-- > 0)
132 grub_puts_ (curarg
->desc
);
136 grub_printf_ (N_("Sorry, no parttool is available for %s\n"),
137 dev
->disk
->partition
->partmap
->name
);
138 return GRUB_ERR_NONE
;
142 grub_cmd_parttool (grub_command_t cmd
__attribute__ ((unused
)),
143 int argc
, char **args
)
146 struct grub_parttool
*cur
, *ptool
;
149 grub_err_t err
= GRUB_ERR_NONE
;
153 grub_puts_ (helpmsg
);
154 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "too few arguments");
157 if (args
[0][0] == '(' && args
[0][grub_strlen (args
[0]) - 1] == ')')
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] = ')';
164 dev
= grub_device_open (args
[0]);
171 grub_device_close (dev
);
172 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "not a disk");
175 if (! dev
->disk
->partition
)
177 grub_device_close (dev
);
178 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "not a partition");
182 if (! grub_no_modules
)
185 prefix
= grub_env_get ("prefix");
190 filename
= grub_xasprintf ("%s/" GRUB_TARGET_CPU
"-" GRUB_PLATFORM
191 "/parttool.lst", prefix
);
196 file
= grub_file_open (filename
);
200 for (;; grub_free(buf
))
204 buf
= grub_file_getline (file
);
210 while (grub_isspace (name
[0]))
213 if (! grub_isgraph (name
[0]))
216 p
= grub_strchr (name
, ':');
222 while (*p
== ' ' || *p
== '\t')
225 if (! grub_isgraph (*p
))
228 if (grub_strcmp (name
, dev
->disk
->partition
->partmap
->name
)
235 grub_file_close (file
);
238 grub_free (filename
);
242 grub_errno
= GRUB_ERR_NONE
;
247 err
= show_help (dev
);
248 grub_device_close (dev
);
252 for (i
= 1; i
< argc
; i
++)
253 if (grub_strcmp (args
[i
], "help") == 0)
255 err
= show_help (dev
);
256 grub_device_close (dev
);
260 parsed
= (int *) grub_zalloc (argc
* sizeof (int));
262 for (i
= 1; i
< argc
; i
++)
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)
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
)] == '=')))
286 grub_device_close (dev
);
287 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("unknown argument `%s'"),
291 pargs
= (struct grub_parttool_args
*)
292 grub_zalloc (ptool
->nargs
* sizeof (struct grub_parttool_args
));
293 for (j
= i
; j
< argc
; j
++)
296 for (curarg
= ptool
->args
; curarg
->name
; curarg
++)
297 if (grub_strncmp (curarg
->name
, args
[j
],
298 grub_strlen (curarg
->name
)) == 0
299 && ((curarg
->type
== GRUB_PARTTOOL_ARG_BOOL
300 && (args
[j
][grub_strlen (curarg
->name
)] == '+'
301 || args
[j
][grub_strlen (curarg
->name
)] == '-'
302 || args
[j
][grub_strlen (curarg
->name
)] == 0))
303 || (curarg
->type
== GRUB_PARTTOOL_ARG_VAL
304 && args
[j
][grub_strlen (curarg
->name
)] == '=')))
307 pargs
[curarg
- ptool
->args
].set
= 1;
308 switch (curarg
->type
)
310 case GRUB_PARTTOOL_ARG_BOOL
:
311 pargs
[curarg
- ptool
->args
].bool
312 = (args
[j
][grub_strlen (curarg
->name
)] != '-');
315 case GRUB_PARTTOOL_ARG_VAL
:
316 pargs
[curarg
- ptool
->args
].str
317 = (args
[j
] + grub_strlen (curarg
->name
) + 1);
320 case GRUB_PARTTOOL_ARG_END
:
326 err
= ptool
->func (dev
, pargs
);
333 grub_device_close (dev
);
337 static grub_command_t cmd
;
339 GRUB_MOD_INIT(parttool
)
342 cmd
= grub_register_command ("parttool", grub_cmd_parttool
,
343 N_("PARTITION COMMANDS"),
347 GRUB_MOD_FINI(parttool
)
349 grub_unregister_command (cmd
);