]> git.proxmox.com Git - grub2.git/blob - commands/parttool.c
Really remove lib/datetime.c and include/grub/lib/datetime.h
[grub2.git] / 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
33 static struct grub_parttool *parts = 0;
34 static int curhandle = 0;
35 static grub_dl_t mymod;
36
37 int
38 grub_parttool_register(const char *part_name,
39 const grub_parttool_function_t func,
40 const struct grub_parttool_argdesc *args)
41 {
42 struct grub_parttool *cur;
43 int nargs = 0;
44
45 #ifndef GRUB_UTIL
46 if (! parts)
47 grub_dl_ref (mymod);
48 #endif
49
50 cur = (struct grub_parttool *) grub_malloc (sizeof (struct grub_parttool));
51 cur->next = parts;
52 cur->name = grub_strdup (part_name);
53 cur->handle = curhandle++;
54 for (nargs = 0; args[nargs].name != 0; nargs++);
55 cur->nargs = 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));
60
61 cur->func = func;
62 parts = cur;
63 return cur->handle;
64 }
65
66 void
67 grub_parttool_unregister (int handle)
68 {
69 struct grub_parttool *prev = 0, *cur, *t;
70 for (cur = parts; cur; )
71 if (cur->handle == handle)
72 {
73 grub_free (cur->args);
74 grub_free (cur->name);
75 if (prev)
76 prev->next = cur->next;
77 else
78 parts = cur->next;
79 t = cur;
80 cur = cur->next;
81 grub_free (t);
82 }
83 else
84 {
85 prev = cur;
86 cur = cur->next;
87 }
88 #ifndef GRUB_UTIL
89 if (! parts)
90 grub_dl_unref (mymod);
91 #endif
92 }
93
94 static grub_err_t
95 grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
96 int argc, char **args)
97 {
98 grub_device_t dev;
99 struct grub_parttool *cur, *ptool;
100 int *parsed;
101 int i, j;
102 grub_err_t err = GRUB_ERR_NONE;
103
104 if (argc < 2)
105 return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
106
107 if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
108 {
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] = ')';
112 }
113 else
114 dev = grub_device_open (args[0]);
115
116 if (! dev)
117 return grub_errno;
118
119 if (! dev->disk)
120 {
121 grub_device_close (dev);
122 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
123 }
124
125 if (! dev->disk->partition)
126 {
127 grub_device_close (dev);
128 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
129 }
130
131 parsed = (int *) grub_malloc (argc * sizeof (int));
132 grub_memset (parsed, 0, argc * sizeof (int));
133
134 for (i = 1; i < argc; i++)
135 if (!grub_strcmp (args[i], "help"))
136 {
137 int found = 0;
138 for (cur = parts; cur; cur = cur->next)
139 if (! grub_strcmp (dev->disk->partition->partmap->name, cur->name))
140 {
141 struct grub_parttool_argdesc *curarg;
142 found = 1;
143 for (curarg = cur->args; curarg->name; curarg++)
144 {
145 int spacing = 20;
146
147 spacing -= grub_strlen (curarg->name);
148 grub_printf ("%s", curarg->name);
149
150 switch (curarg->type)
151 {
152 case GRUB_PARTTOOL_ARG_BOOL:
153 grub_printf ("+/-");
154 spacing -= 3;
155 break;
156
157 case GRUB_PARTTOOL_ARG_VAL:
158 grub_printf ("=VAL");
159 spacing -= 4;
160 break;
161
162 case GRUB_PARTTOOL_ARG_END:
163 break;
164 }
165 while (spacing-- > 0)
166 grub_printf (" ");
167 grub_printf ("%s\n", curarg->desc);
168 }
169 }
170 if (! found)
171 grub_printf ("Sorry no parttool is available for %s\n",
172 dev->disk->partition->partmap->name);
173 return GRUB_ERR_NONE;
174 }
175
176 for (i = 1; i < argc; i++)
177 if (! parsed[i])
178 {
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))
183 {
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)] == '=')))
192
193 break;
194 if (curarg->name)
195 break;
196 }
197 if (! cur)
198 return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised argument %s",
199 args[i]);
200 ptool = cur;
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++)
206 if (!parsed[j])
207 {
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)] == '=')))
216 {
217 parsed[j] = 1;
218 pargs[curarg - ptool->args].set = 1;
219 switch (curarg->type)
220 {
221 case GRUB_PARTTOOL_ARG_BOOL:
222 pargs[curarg - ptool->args].bool
223 = (args[j][grub_strlen (curarg->name)] != '-');
224 break;
225
226 case GRUB_PARTTOOL_ARG_VAL:
227 pargs[curarg - ptool->args].str
228 = (args[j] + grub_strlen (curarg->name) + 1);
229 break;
230
231 case GRUB_PARTTOOL_ARG_END:
232 break;
233 }
234 }
235 }
236
237 err = ptool->func (dev, pargs);
238 grub_free (pargs);
239 if (err)
240 break;
241 }
242
243 grub_device_close (dev);
244 return err;
245 }
246
247 static grub_command_t cmd;
248
249 GRUB_MOD_INIT(parttool)
250 {
251 (void)mod; /* To stop warning. */
252 mymod = mod;
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");
258 }
259
260 GRUB_MOD_FINI(parttool)
261 {
262 grub_unregister_command (cmd);
263 }