]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/parttool.c
051e31320e9ecb52fc1eaac48f089ddb7b6c6737
[grub2.git] / grub-core / commands / parttool.c
1 /* parttool.c - common dispatcher and parser for partition operations */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include <grub/types.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/err.h>
25 #include <grub/dl.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>
33
34 GRUB_MOD_LICENSE ("GPLv2+");
35
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.");
43
44 int
45 grub_parttool_register(const char *part_name,
46 const grub_parttool_function_t func,
47 const struct grub_parttool_argdesc *args)
48 {
49 struct grub_parttool *cur;
50 int nargs = 0;
51
52 if (! parts)
53 grub_dl_ref (mymod);
54
55 cur = (struct grub_parttool *) grub_malloc (sizeof (struct grub_parttool));
56 cur->next = parts;
57 cur->name = grub_strdup (part_name);
58 cur->handle = curhandle++;
59 for (nargs = 0; args[nargs].name != 0; nargs++);
60 cur->nargs = nargs;
61 cur->args = (struct grub_parttool_argdesc *)
62 grub_calloc (nargs + 1, sizeof (struct grub_parttool_argdesc));
63 if (!cur->args)
64 {
65 grub_free (cur);
66 curhandle--;
67 return -1;
68 }
69 grub_memcpy (cur->args, args,
70 (nargs + 1) * sizeof (struct grub_parttool_argdesc));
71
72 cur->func = func;
73 parts = cur;
74 return cur->handle;
75 }
76
77 void
78 grub_parttool_unregister (int handle)
79 {
80 struct grub_parttool *prev = 0, *cur, *t;
81 for (cur = parts; cur; )
82 if (cur->handle == handle)
83 {
84 grub_free (cur->args);
85 grub_free (cur->name);
86 if (prev)
87 prev->next = cur->next;
88 else
89 parts = cur->next;
90 t = cur;
91 cur = cur->next;
92 grub_free (t);
93 }
94 else
95 {
96 prev = cur;
97 cur = cur->next;
98 }
99 if (! parts)
100 grub_dl_unref (mymod);
101 }
102
103 static grub_err_t
104 show_help (grub_device_t dev)
105 {
106 int found = 0;
107 struct grub_parttool *cur;
108
109 for (cur = parts; cur; cur = cur->next)
110 if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
111 {
112 struct grub_parttool_argdesc *curarg;
113 found = 1;
114 for (curarg = cur->args; curarg->name; curarg++)
115 {
116 int spacing = 20;
117
118 spacing -= grub_strlen (curarg->name);
119 grub_printf ("%s", curarg->name);
120
121 switch (curarg->type)
122 {
123 case GRUB_PARTTOOL_ARG_BOOL:
124 grub_printf ("+/-");
125 spacing -= 3;
126 break;
127
128 case GRUB_PARTTOOL_ARG_VAL:
129 grub_xputs (_("=VAL"));
130 spacing -= 4;
131 break;
132
133 case GRUB_PARTTOOL_ARG_END:
134 break;
135 }
136 while (spacing-- > 0)
137 grub_printf (" ");
138 grub_puts_ (curarg->desc);
139 }
140 }
141 if (! found)
142 grub_printf_ (N_("Sorry, no parttool is available for %s\n"),
143 dev->disk->partition->partmap->name);
144 return GRUB_ERR_NONE;
145 }
146
147 static grub_err_t
148 grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
149 int argc, char **args)
150 {
151 grub_device_t dev;
152 struct grub_parttool *cur, *ptool;
153 int *parsed;
154 int i, j;
155 grub_err_t err = GRUB_ERR_NONE;
156
157 if (argc < 1)
158 {
159 grub_puts_ (helpmsg);
160 return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
161 }
162
163 if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
164 {
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] = ')';
168 }
169 else
170 dev = grub_device_open (args[0]);
171
172 if (! dev)
173 return grub_errno;
174
175 if (! dev->disk)
176 {
177 grub_device_close (dev);
178 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
179 }
180
181 if (! dev->disk->partition)
182 {
183 grub_device_close (dev);
184 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
185 }
186
187 /* Load modules. */
188 if (! grub_no_modules)
189 {
190 const char *prefix;
191 prefix = grub_env_get ("prefix");
192 if (prefix)
193 {
194 char *filename;
195
196 filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
197 "/parttool.lst", prefix);
198 if (filename)
199 {
200 grub_file_t file;
201
202 file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE_LIST);
203 if (file)
204 {
205 char *buf = 0;
206 for (;; grub_free(buf))
207 {
208 char *p, *name;
209
210 buf = grub_file_getline (file);
211
212 if (! buf)
213 break;
214
215 name = buf;
216 while (grub_isspace (name[0]))
217 name++;
218
219 if (! grub_isgraph (name[0]))
220 continue;
221
222 p = grub_strchr (name, ':');
223 if (! p)
224 continue;
225
226 *p = '\0';
227 p++;
228 while (*p == ' ' || *p == '\t')
229 p++;
230
231 if (! grub_isgraph (*p))
232 continue;
233
234 if (grub_strcmp (name, dev->disk->partition->partmap->name)
235 != 0)
236 continue;
237
238 grub_dl_load (p);
239 }
240
241 grub_file_close (file);
242 }
243
244 grub_free (filename);
245 }
246 }
247 /* Ignore errors. */
248 grub_errno = GRUB_ERR_NONE;
249 }
250
251 if (argc == 1)
252 {
253 err = show_help (dev);
254 grub_device_close (dev);
255 return err;
256 }
257
258 for (i = 1; i < argc; i++)
259 if (grub_strcmp (args[i], "help") == 0)
260 {
261 err = show_help (dev);
262 grub_device_close (dev);
263 return err;
264 }
265
266 parsed = (int *) grub_calloc (argc, sizeof (int));
267
268 for (i = 1; i < argc; i++)
269 if (! parsed[i])
270 {
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)
275 {
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)] == '=')))
285
286 break;
287 if (curarg->name)
288 break;
289 }
290 if (! cur)
291 {
292 grub_free (parsed);
293 grub_device_close (dev);
294 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unknown argument `%s'"),
295 args[i]);
296 }
297 ptool = cur;
298 pargs = (struct grub_parttool_args *)
299 grub_calloc (ptool->nargs, sizeof (struct grub_parttool_args));
300 for (j = i; j < argc; j++)
301 if (! parsed[j])
302 {
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)] == '=')))
312 {
313 parsed[j] = 1;
314 pargs[curarg - ptool->args].set = 1;
315 switch (curarg->type)
316 {
317 case GRUB_PARTTOOL_ARG_BOOL:
318 pargs[curarg - ptool->args].bool
319 = (args[j][grub_strlen (curarg->name)] != '-');
320 break;
321
322 case GRUB_PARTTOOL_ARG_VAL:
323 pargs[curarg - ptool->args].str
324 = (args[j] + grub_strlen (curarg->name) + 1);
325 break;
326
327 case GRUB_PARTTOOL_ARG_END:
328 break;
329 }
330 }
331 }
332
333 err = ptool->func (dev, pargs);
334 grub_free (pargs);
335 if (err)
336 break;
337 }
338
339 grub_free (parsed);
340 grub_device_close (dev);
341 return err;
342 }
343
344 static grub_command_t cmd;
345
346 GRUB_MOD_INIT(parttool)
347 {
348 mymod = mod;
349 cmd = grub_register_command ("parttool", grub_cmd_parttool,
350 N_("PARTITION COMMANDS"),
351 helpmsg);
352 }
353
354 GRUB_MOD_FINI(parttool)
355 {
356 grub_unregister_command (cmd);
357 }