~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to commands/parttool.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
static struct grub_parttool *parts = 0;
35
 
static int curhandle = 0;
36
 
static grub_dl_t mymod;
37
 
static char helpmsg[] =
38
 
  "Perform COMMANDS on partition.\n"
39
 
  "Use \"parttool PARTITION help\" for the list "
40
 
  "of available commands.";
41
 
 
42
 
int
43
 
grub_parttool_register(const char *part_name,
44
 
                       const grub_parttool_function_t func,
45
 
                       const struct grub_parttool_argdesc *args)
46
 
{
47
 
  struct grub_parttool *cur;
48
 
  int nargs = 0;
49
 
 
50
 
  if (! parts)
51
 
    grub_dl_ref (mymod);
52
 
 
53
 
  cur = (struct grub_parttool *) grub_malloc (sizeof (struct grub_parttool));
54
 
  cur->next = parts;
55
 
  cur->name = grub_strdup (part_name);
56
 
  cur->handle = curhandle++;
57
 
  for (nargs = 0; args[nargs].name != 0; nargs++);
58
 
  cur->nargs = nargs;
59
 
  cur->args = (struct grub_parttool_argdesc *)
60
 
    grub_malloc ((nargs + 1) * sizeof (struct grub_parttool_argdesc));
61
 
  grub_memcpy (cur->args, args,
62
 
               (nargs + 1) * sizeof (struct grub_parttool_argdesc));
63
 
 
64
 
  cur->func = func;
65
 
  parts = cur;
66
 
  return cur->handle;
67
 
}
68
 
 
69
 
void
70
 
grub_parttool_unregister (int handle)
71
 
{
72
 
  struct grub_parttool *prev = 0, *cur, *t;
73
 
  for (cur = parts; cur; )
74
 
    if (cur->handle == handle)
75
 
      {
76
 
        grub_free (cur->args);
77
 
        grub_free (cur->name);
78
 
        if (prev)
79
 
          prev->next = cur->next;
80
 
        else
81
 
          parts = cur->next;
82
 
        t = cur;
83
 
        cur = cur->next;
84
 
        grub_free (t);
85
 
      }
86
 
    else
87
 
      {
88
 
        prev = cur;
89
 
        cur = cur->next;
90
 
      }
91
 
  if (! parts)
92
 
    grub_dl_unref (mymod);
93
 
}
94
 
 
95
 
static grub_err_t
96
 
grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
97
 
                   int argc, char **args)
98
 
{
99
 
  grub_device_t dev;
100
 
  struct grub_parttool *cur, *ptool;
101
 
  int *parsed;
102
 
  int i, j;
103
 
  grub_err_t err = GRUB_ERR_NONE;
104
 
 
105
 
  auto grub_err_t show_help (void);
106
 
  grub_err_t show_help (void)
107
 
  {
108
 
    int found = 0;
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_printf ("=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_printf ("%s\n", curarg->desc);
139
 
            }
140
 
        }
141
 
    if (! found)
142
 
      grub_printf ("Sorry no parttool is available for %s\n",
143
 
                   dev->disk->partition->partmap->name);
144
 
    return GRUB_ERR_NONE;
145
 
  }
146
 
 
147
 
  if (argc < 1)
148
 
    {
149
 
      grub_printf ("%s\n", helpmsg);
150
 
      return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
151
 
    }
152
 
 
153
 
  if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
154
 
    {
155
 
      args[0][grub_strlen (args[0]) - 1] = 0;
156
 
      dev = grub_device_open (args[0] + 1);
157
 
      args[0][grub_strlen (args[0]) - 1] = ')';
158
 
    }
159
 
  else
160
 
    dev = grub_device_open (args[0]);
161
 
 
162
 
  if (! dev)
163
 
    return grub_errno;
164
 
 
165
 
  if (! dev->disk)
166
 
    {
167
 
      grub_device_close (dev);
168
 
      return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
169
 
    }
170
 
 
171
 
  if (! dev->disk->partition)
172
 
    {
173
 
      grub_device_close (dev);
174
 
      return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
175
 
    }
176
 
 
177
 
  /* Load modules. */
178
 
#ifndef GRUB_UTIL
179
 
  {
180
 
    const char *prefix;
181
 
    prefix = grub_env_get ("prefix");
182
 
    if (prefix)
183
 
      {
184
 
        char *filename;
185
 
 
186
 
        filename = grub_xasprintf ("%s/parttool.lst", prefix);
187
 
        if (filename)
188
 
          {
189
 
            grub_file_t file;
190
 
 
191
 
            file = grub_file_open (filename);
192
 
            if (file)
193
 
              {
194
 
                char *buf = 0;
195
 
                for (;; grub_free(buf))
196
 
                  {
197
 
                    char *p, *name;
198
 
 
199
 
                    buf = grub_file_getline (file);
200
 
 
201
 
                    if (! buf)
202
 
                      break;
203
 
 
204
 
                    name = buf;
205
 
 
206
 
                    if (! grub_isgraph (name[0]))
207
 
                      continue;
208
 
 
209
 
                    p = grub_strchr (name, ':');
210
 
                    if (! p)
211
 
                      continue;
212
 
 
213
 
                    *p = '\0';
214
 
                    while (*++p == ' ')
215
 
                      ;
216
 
 
217
 
                    if (! grub_isgraph (*p))
218
 
                      continue;
219
 
 
220
 
                    if (grub_strcmp (name, dev->disk->partition->partmap->name)
221
 
                        != 0)
222
 
                      continue;
223
 
 
224
 
                    grub_dl_load (p);
225
 
                  }
226
 
 
227
 
                grub_file_close (file);
228
 
              }
229
 
 
230
 
            grub_free (filename);
231
 
          }
232
 
      }
233
 
    /* Ignore errors.  */
234
 
    grub_errno = GRUB_ERR_NONE;
235
 
  }
236
 
#endif
237
 
 
238
 
  if (argc == 1)
239
 
    return show_help ();
240
 
 
241
 
  for (i = 1; i < argc; i++)
242
 
    if (grub_strcmp (args[i], "help") == 0)
243
 
      return show_help ();
244
 
 
245
 
  parsed = (int *) grub_zalloc (argc * sizeof (int));
246
 
 
247
 
  for (i = 1; i < argc; i++)
248
 
    if (! parsed[i])
249
 
      {
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)
254
 
            {
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)] == '=')))
264
 
 
265
 
                  break;
266
 
              if (curarg->name)
267
 
                break;
268
 
            }
269
 
        if (! cur)
270
 
          return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised argument %s",
271
 
                             args[i]);
272
 
        ptool = cur;
273
 
        pargs = (struct grub_parttool_args *)
274
 
          grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
275
 
        for (j = i; j < argc; j++)
276
 
          if (! parsed[j])
277
 
            {
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)] == '=')))
287
 
                  {
288
 
                    parsed[j] = 1;
289
 
                    pargs[curarg - ptool->args].set = 1;
290
 
                    switch (curarg->type)
291
 
                      {
292
 
                      case GRUB_PARTTOOL_ARG_BOOL:
293
 
                        pargs[curarg - ptool->args].bool
294
 
                          = (args[j][grub_strlen (curarg->name)] != '-');
295
 
                        break;
296
 
 
297
 
                      case GRUB_PARTTOOL_ARG_VAL:
298
 
                        pargs[curarg - ptool->args].str
299
 
                          = (args[j] + grub_strlen (curarg->name) + 1);
300
 
                        break;
301
 
 
302
 
                      case GRUB_PARTTOOL_ARG_END:
303
 
                        break;
304
 
                      }
305
 
                  }
306
 
            }
307
 
 
308
 
        err = ptool->func (dev, pargs);
309
 
        grub_free (pargs);
310
 
        if (err)
311
 
          break;
312
 
      }
313
 
 
314
 
  grub_free (parsed);
315
 
  grub_device_close (dev);
316
 
  return err;
317
 
}
318
 
 
319
 
static grub_command_t cmd;
320
 
 
321
 
GRUB_MOD_INIT(parttool)
322
 
{
323
 
  mymod = mod;
324
 
  cmd = grub_register_command ("parttool", grub_cmd_parttool,
325
 
                               N_("PARTITION COMMANDS"),
326
 
                               helpmsg);
327
 
}
328
 
 
329
 
GRUB_MOD_FINI(parttool)
330
 
{
331
 
  grub_unregister_command (cmd);
332
 
}