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
;
38 grub_parttool_register(const char *part_name
,
39 const grub_parttool_function_t func
,
40 const struct grub_parttool_argdesc
*args
)
42 struct grub_parttool
*cur
;
50 cur
= (struct grub_parttool
*) grub_malloc (sizeof (struct grub_parttool
));
52 cur
->name
= grub_strdup (part_name
);
53 cur
->handle
= curhandle
++;
54 for (nargs
= 0; args
[nargs
].name
!= 0; nargs
++);
56 cur
->args
= (struct grub_parttool_argdesc
*)
57 grub_malloc ((nargs
+ 1) * sizeof (struct grub_parttool_argdesc
));
58 grub_memcpy (cur
->args
, args
,
59 (nargs
+ 1) * sizeof (struct grub_parttool_argdesc
));
67 grub_parttool_unregister (int handle
)
69 struct grub_parttool
*prev
= 0, *cur
, *t
;
70 for (cur
= parts
; cur
; )
71 if (cur
->handle
== handle
)
73 grub_free (cur
->args
);
74 grub_free (cur
->name
);
76 prev
->next
= cur
->next
;
90 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
;
105 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "too few arguments");
107 if (args
[0][0] == '(' && args
[0][grub_strlen (args
[0]) - 1] == ')')
109 args
[0][grub_strlen (args
[0]) - 1] = 0;
110 dev
= grub_device_open (args
[0] + 1);
111 args
[0][grub_strlen (args
[0]) - 1] = ')';
114 dev
= grub_device_open (args
[0]);
121 grub_device_close (dev
);
122 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "not a disk");
125 if (! dev
->disk
->partition
)
127 grub_device_close (dev
);
128 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "not a partition");
131 parsed
= (int *) grub_malloc (argc
* sizeof (int));
132 grub_memset (parsed
, 0, argc
* sizeof (int));
134 for (i
= 1; i
< argc
; i
++)
135 if (!grub_strcmp (args
[i
], "help"))
138 for (cur
= parts
; cur
; cur
= cur
->next
)
139 if (! grub_strcmp (dev
->disk
->partition
->partmap
->name
, cur
->name
))
141 struct grub_parttool_argdesc
*curarg
;
143 for (curarg
= cur
->args
; curarg
->name
; curarg
++)
147 spacing
-= grub_strlen (curarg
->name
);
148 grub_printf ("%s", curarg
->name
);
150 switch (curarg
->type
)
152 case GRUB_PARTTOOL_ARG_BOOL
:
157 case GRUB_PARTTOOL_ARG_VAL
:
158 grub_printf ("=VAL");
162 case GRUB_PARTTOOL_ARG_END
:
165 while (spacing
-- > 0)
167 grub_printf ("%s\n", curarg
->desc
);
171 grub_printf ("Sorry no parttool is available for %s\n",
172 dev
->disk
->partition
->partmap
->name
);
173 return GRUB_ERR_NONE
;
176 for (i
= 1; i
< argc
; i
++)
179 struct grub_parttool_argdesc
*curarg
;
180 struct grub_parttool_args
*pargs
;
181 for (cur
= parts
; cur
; cur
= cur
->next
)
182 if (! grub_strcmp (dev
->disk
->partition
->partmap
->name
, cur
->name
))
184 for (curarg
= cur
->args
; curarg
->name
; curarg
++)
185 if (!grub_strncmp (curarg
->name
, args
[i
],
186 grub_strlen (curarg
->name
))
187 && ((curarg
->type
== GRUB_PARTTOOL_ARG_BOOL
188 && (args
[i
][grub_strlen (curarg
->name
)] == '+'
189 || args
[i
][grub_strlen (curarg
->name
)] == '-'))
190 || (curarg
->type
== GRUB_PARTTOOL_ARG_VAL
191 && args
[i
][grub_strlen (curarg
->name
)] == '=')))
198 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "unrecognised argument %s",
201 pargs
= (struct grub_parttool_args
*)
202 grub_malloc (ptool
->nargs
* sizeof (struct grub_parttool_args
));
203 grub_memset (pargs
, 0,
204 ptool
->nargs
* sizeof (struct grub_parttool_args
));
205 for (j
= i
; j
< argc
; j
++)
208 for (curarg
= ptool
->args
; curarg
->name
; curarg
++)
209 if (!grub_strncmp (curarg
->name
, args
[i
],
210 grub_strlen (curarg
->name
))
211 && ((curarg
->type
== GRUB_PARTTOOL_ARG_BOOL
212 && (args
[j
][grub_strlen (curarg
->name
)] == '+'
213 || args
[j
][grub_strlen (curarg
->name
)] == '-'))
214 || (curarg
->type
== GRUB_PARTTOOL_ARG_VAL
215 && args
[j
][grub_strlen (curarg
->name
)] == '=')))
218 pargs
[curarg
- ptool
->args
].set
= 1;
219 switch (curarg
->type
)
221 case GRUB_PARTTOOL_ARG_BOOL
:
222 pargs
[curarg
- ptool
->args
].bool
223 = (args
[j
][grub_strlen (curarg
->name
)] != '-');
226 case GRUB_PARTTOOL_ARG_VAL
:
227 pargs
[curarg
- ptool
->args
].str
228 = (args
[j
] + grub_strlen (curarg
->name
) + 1);
231 case GRUB_PARTTOOL_ARG_END
:
237 err
= ptool
->func (dev
, pargs
);
243 grub_device_close (dev
);
247 static grub_command_t cmd
;
249 GRUB_MOD_INIT(parttool
)
251 (void)mod
; /* To stop warning. */
253 cmd
= grub_register_command ("parttool", grub_cmd_parttool
,
254 "parttool PARTITION COMMANDS",
255 "perform COMMANDS on partition."
256 " use parttool PARTITION help for the list "
257 " of available commands");
260 GRUB_MOD_FINI(parttool
)
262 grub_unregister_command (cmd
);