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>
33 static struct grub_parttool
*parts
= 0;
34 static int curhandle
= 0;
35 static grub_dl_t mymod
;
36 static char helpmsg
[] =
37 "perform COMMANDS on partition.\n"
38 "Use \"parttool PARTITION help\" for the list "
39 "of available commands";
42 grub_parttool_register(const char *part_name
,
43 const grub_parttool_function_t func
,
44 const struct grub_parttool_argdesc
*args
)
46 struct grub_parttool
*cur
;
52 cur
= (struct grub_parttool
*) grub_malloc (sizeof (struct grub_parttool
));
54 cur
->name
= grub_strdup (part_name
);
55 cur
->handle
= curhandle
++;
56 for (nargs
= 0; args
[nargs
].name
!= 0; nargs
++);
58 cur
->args
= (struct grub_parttool_argdesc
*)
59 grub_malloc ((nargs
+ 1) * sizeof (struct grub_parttool_argdesc
));
60 grub_memcpy (cur
->args
, args
,
61 (nargs
+ 1) * sizeof (struct grub_parttool_argdesc
));
69 grub_parttool_unregister (int handle
)
71 struct grub_parttool
*prev
= 0, *cur
, *t
;
72 for (cur
= parts
; cur
; )
73 if (cur
->handle
== handle
)
75 grub_free (cur
->args
);
76 grub_free (cur
->name
);
78 prev
->next
= cur
->next
;
91 grub_dl_unref (mymod
);
95 grub_cmd_parttool (grub_command_t cmd
__attribute__ ((unused
)),
96 int argc
, char **args
)
99 struct grub_parttool
*cur
, *ptool
;
102 grub_err_t err
= GRUB_ERR_NONE
;
104 auto grub_err_t
show_help (void);
105 grub_err_t
show_help (void)
108 for (cur
= parts
; cur
; cur
= cur
->next
)
109 if (grub_strcmp (dev
->disk
->partition
->partmap
->name
, cur
->name
) == 0)
111 struct grub_parttool_argdesc
*curarg
;
113 for (curarg
= cur
->args
; curarg
->name
; curarg
++)
117 spacing
-= grub_strlen (curarg
->name
);
118 grub_printf ("%s", curarg
->name
);
120 switch (curarg
->type
)
122 case GRUB_PARTTOOL_ARG_BOOL
:
127 case GRUB_PARTTOOL_ARG_VAL
:
128 grub_printf ("=VAL");
132 case GRUB_PARTTOOL_ARG_END
:
135 while (spacing
-- > 0)
137 grub_printf ("%s\n", curarg
->desc
);
141 grub_printf ("Sorry no parttool is available for %s\n",
142 dev
->disk
->partition
->partmap
->name
);
143 return GRUB_ERR_NONE
;
148 grub_printf ("%s\n", helpmsg
);
149 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "too few arguments");
152 if (args
[0][0] == '(' && args
[0][grub_strlen (args
[0]) - 1] == ')')
154 args
[0][grub_strlen (args
[0]) - 1] = 0;
155 dev
= grub_device_open (args
[0] + 1);
156 args
[0][grub_strlen (args
[0]) - 1] = ')';
159 dev
= grub_device_open (args
[0]);
166 grub_device_close (dev
);
167 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "not a disk");
170 if (! dev
->disk
->partition
)
172 grub_device_close (dev
);
173 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "not a partition");
180 prefix
= grub_env_get ("prefix");
185 filename
= grub_malloc (grub_strlen (prefix
) + sizeof ("/parttool.lst"));
190 grub_sprintf (filename
, "%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_malloc (argc
* sizeof (int));
246 grub_memset (parsed
, 0, argc
* sizeof (int));
248 for (i
= 1; i
< argc
; i
++)
251 struct grub_parttool_argdesc
*curarg
;
252 struct grub_parttool_args
*pargs
;
253 for (cur
= parts
; cur
; cur
= cur
->next
)
254 if (grub_strcmp (dev
->disk
->partition
->partmap
->name
, cur
->name
) == 0)
256 for (curarg
= cur
->args
; curarg
->name
; curarg
++)
257 if (grub_strncmp (curarg
->name
, args
[i
],
258 grub_strlen (curarg
->name
)) == 0
259 && ((curarg
->type
== GRUB_PARTTOOL_ARG_BOOL
260 && (args
[i
][grub_strlen (curarg
->name
)] == '+'
261 || args
[i
][grub_strlen (curarg
->name
)] == '-'
262 || args
[i
][grub_strlen (curarg
->name
)] == 0))
263 || (curarg
->type
== GRUB_PARTTOOL_ARG_VAL
264 && args
[i
][grub_strlen (curarg
->name
)] == '=')))
271 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "unrecognised argument %s",
274 pargs
= (struct grub_parttool_args
*)
275 grub_malloc (ptool
->nargs
* sizeof (struct grub_parttool_args
));
276 grub_memset (pargs
, 0,
277 ptool
->nargs
* sizeof (struct grub_parttool_args
));
278 for (j
= i
; j
< argc
; j
++)
281 for (curarg
= ptool
->args
; curarg
->name
; curarg
++)
282 if (grub_strncmp (curarg
->name
, args
[i
],
283 grub_strlen (curarg
->name
)) == 0
284 && ((curarg
->type
== GRUB_PARTTOOL_ARG_BOOL
285 && (args
[j
][grub_strlen (curarg
->name
)] == '+'
286 || args
[j
][grub_strlen (curarg
->name
)] == '-'
287 || args
[j
][grub_strlen (curarg
->name
)] == 0))
288 || (curarg
->type
== GRUB_PARTTOOL_ARG_VAL
289 && args
[j
][grub_strlen (curarg
->name
)] == '=')))
292 pargs
[curarg
- ptool
->args
].set
= 1;
293 switch (curarg
->type
)
295 case GRUB_PARTTOOL_ARG_BOOL
:
296 pargs
[curarg
- ptool
->args
].bool
297 = (args
[j
][grub_strlen (curarg
->name
)] != '-');
300 case GRUB_PARTTOOL_ARG_VAL
:
301 pargs
[curarg
- ptool
->args
].str
302 = (args
[j
] + grub_strlen (curarg
->name
) + 1);
305 case GRUB_PARTTOOL_ARG_END
:
311 err
= ptool
->func (dev
, pargs
);
318 grub_device_close (dev
);
322 static grub_command_t cmd
;
324 GRUB_MOD_INIT(parttool
)
327 cmd
= grub_register_command ("parttool", grub_cmd_parttool
,
328 "parttool PARTITION COMMANDS",
332 GRUB_MOD_FINI(parttool
)
334 grub_unregister_command (cmd
);