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_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 "parttool PARTITION COMMANDS",
329 GRUB_MOD_FINI(parttool
)
331 grub_unregister_command (cmd
);