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

« back to all changes in this revision

Viewing changes to partmap/msdos.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
 
/* pc.c - Read PC style partition tables.  */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2002,2004,2005,2006,2007,2008,2009  Free Software Foundation, Inc.
5
 
 *
6
 
 *  GRUB 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 3 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include <grub/partition.h>
21
 
#include <grub/msdos_partition.h>
22
 
#include <grub/disk.h>
23
 
#include <grub/mm.h>
24
 
#include <grub/misc.h>
25
 
#include <grub/dl.h>
26
 
 
27
 
static struct grub_partition_map grub_msdos_partition_map;
28
 
 
29
 
 
30
 
/* Parse the partition representation in STR and return a partition.  */
31
 
static grub_partition_t
32
 
grub_partition_parse (const char *str)
33
 
{
34
 
  grub_partition_t p;
35
 
  struct grub_msdos_partition *pcdata;
36
 
 
37
 
  char *s = (char *) str;
38
 
 
39
 
  p = (grub_partition_t) grub_malloc (sizeof (*p));
40
 
  if (! p)
41
 
    return 0;
42
 
 
43
 
  pcdata = (struct grub_msdos_partition *) grub_malloc (sizeof (*pcdata));
44
 
  if (! pcdata)
45
 
    goto fail;
46
 
 
47
 
  p->data = pcdata;
48
 
  p->partmap = &grub_msdos_partition_map;
49
 
 
50
 
  /* Initialize some of the fields with invalid values.  */
51
 
  pcdata->bsd_part = pcdata->dos_type = pcdata->bsd_type = p->index = -1;
52
 
 
53
 
  /* Get the DOS partition number. The number is counted from one for
54
 
     the user interface, and from zero internally.  */
55
 
  pcdata->dos_part = grub_strtoul (s, &s, 0) - 1;
56
 
 
57
 
  if (grub_errno)
58
 
    {
59
 
      /* Not found. Maybe only a BSD label is specified.  */
60
 
      pcdata->dos_part = -1;
61
 
      grub_errno = GRUB_ERR_NONE;
62
 
    }
63
 
  else if (*s == ',')
64
 
    s++;
65
 
 
66
 
  if (*s)
67
 
    {
68
 
      if (*s >= 'a' && *s <= 'h')
69
 
        {
70
 
          pcdata->bsd_part = *s - 'a';
71
 
          s++;
72
 
        }
73
 
 
74
 
      if (*s)
75
 
        goto fail;
76
 
    }
77
 
 
78
 
  if (pcdata->dos_part == -1 && pcdata->bsd_part == -1)
79
 
    goto fail;
80
 
 
81
 
  return p;
82
 
 
83
 
 fail:
84
 
  grub_free (p);
85
 
  grub_free (pcdata);
86
 
  grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
87
 
  return 0;
88
 
}
89
 
 
90
 
static grub_err_t
91
 
pc_partition_map_iterate (grub_disk_t disk,
92
 
                          int (*hook) (grub_disk_t disk,
93
 
                                       const grub_partition_t partition))
94
 
{
95
 
  struct grub_partition p;
96
 
  struct grub_msdos_partition pcdata;
97
 
  struct grub_msdos_partition_mbr mbr;
98
 
  struct grub_msdos_partition_disk_label label;
99
 
  struct grub_disk raw;
100
 
  int labeln = 0;
101
 
  grub_disk_addr_t lastaddr;
102
 
 
103
 
  /* Enforce raw disk access.  */
104
 
  raw = *disk;
105
 
  raw.partition = 0;
106
 
 
107
 
  p.offset = 0;
108
 
  pcdata.ext_offset = 0;
109
 
  pcdata.dos_part = -1;
110
 
  p.data = &pcdata;
111
 
  p.partmap = &grub_msdos_partition_map;
112
 
 
113
 
  /* Any value different than `p.offset' will satisfy the check during
114
 
     first loop.  */
115
 
  lastaddr = !p.offset;
116
 
 
117
 
  while (1)
118
 
    {
119
 
      int i;
120
 
      struct grub_msdos_partition_entry *e;
121
 
 
122
 
      /* Read the MBR.  */
123
 
      if (grub_disk_read (&raw, p.offset, 0, sizeof (mbr), &mbr))
124
 
        goto finish;
125
 
 
126
 
      /* This is our loop-detection algorithm. It works the following way:
127
 
         It saves last position which was a power of two. Then it compares the
128
 
         saved value with a current one. This way it's guaranteed that the loop
129
 
         will be broken by at most third walk.
130
 
       */
131
 
      if (labeln && lastaddr == p.offset)
132
 
        return grub_error (GRUB_ERR_BAD_PART_TABLE, "loop detected");
133
 
 
134
 
      labeln++;
135
 
      if ((labeln & (labeln - 1)) == 0)
136
 
        lastaddr = p.offset;
137
 
 
138
 
      /* Check if it is valid.  */
139
 
      if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
140
 
        return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
141
 
 
142
 
      for (i = 0; i < 4; i++)
143
 
        if (mbr.entries[i].flag & 0x7f)
144
 
          return grub_error (GRUB_ERR_BAD_PART_TABLE, "bad boot flag");
145
 
 
146
 
      /* Analyze DOS partitions.  */
147
 
      for (p.index = 0; p.index < 4; p.index++)
148
 
        {
149
 
          e = mbr.entries + p.index;
150
 
 
151
 
          p.start = p.offset + grub_le_to_cpu32 (e->start);
152
 
          p.len = grub_le_to_cpu32 (e->length);
153
 
          pcdata.bsd_part = -1;
154
 
          pcdata.dos_type = e->type;
155
 
          pcdata.bsd_type = -1;
156
 
 
157
 
          grub_dprintf ("partition",
158
 
                        "partition %d: flag 0x%x, type 0x%x, start 0x%llx, len 0x%llx\n",
159
 
                        p.index, e->flag, pcdata.dos_type,
160
 
                        (unsigned long long) p.start,
161
 
                        (unsigned long long) p.len);
162
 
 
163
 
          /* If this is a GPT partition, this MBR is just a dummy.  */
164
 
          if (e->type == GRUB_PC_PARTITION_TYPE_GPT_DISK && p.index == 0)
165
 
            return grub_error (GRUB_ERR_BAD_PART_TABLE, "dummy mbr");
166
 
 
167
 
          /* If this partition is a normal one, call the hook.  */
168
 
          if (! grub_msdos_partition_is_empty (e->type)
169
 
              && ! grub_msdos_partition_is_extended (e->type))
170
 
            {
171
 
              pcdata.dos_part++;
172
 
 
173
 
              if (hook (disk, &p))
174
 
                return 1;
175
 
 
176
 
              /* Check if this is a BSD partition.  */
177
 
              if (grub_msdos_partition_is_bsd (e->type))
178
 
                {
179
 
                  /* Check if the BSD label is within the DOS partition.  */
180
 
                  if (p.len <= GRUB_PC_PARTITION_BSD_LABEL_SECTOR)
181
 
                    {
182
 
                      grub_dprintf ("partition", "no space for disk label\n");
183
 
                      continue;
184
 
                    }
185
 
                  /* Read the BSD label.  */
186
 
                  if (grub_disk_read (&raw,
187
 
                                      (p.start
188
 
                                       + GRUB_PC_PARTITION_BSD_LABEL_SECTOR),
189
 
                                      0,
190
 
                                      sizeof (label),
191
 
                                      &label))
192
 
                    goto finish;
193
 
 
194
 
                  /* Check if it is valid.  */
195
 
                  if (label.magic
196
 
                      != grub_cpu_to_le32 (GRUB_PC_PARTITION_BSD_LABEL_MAGIC))
197
 
                    {
198
 
                      grub_dprintf ("partition",
199
 
                                    "invalid disk label magic 0x%x on partition %d\n",
200
 
                                    label.magic, p.index);
201
 
                      continue;
202
 
                    }
203
 
                  for (pcdata.bsd_part = 0;
204
 
                       pcdata.bsd_part < grub_cpu_to_le16 (label.num_partitions);
205
 
                       pcdata.bsd_part++)
206
 
                    {
207
 
                      struct grub_msdos_partition_bsd_entry *be
208
 
                        = label.entries + pcdata.bsd_part;
209
 
 
210
 
                      p.start = grub_le_to_cpu32 (be->offset);
211
 
                      p.len = grub_le_to_cpu32 (be->size);
212
 
                      pcdata.bsd_type = be->fs_type;
213
 
 
214
 
                      if (be->fs_type != GRUB_PC_PARTITION_BSD_TYPE_UNUSED)
215
 
                        if (hook (disk, &p))
216
 
                          return 1;
217
 
                    }
218
 
                }
219
 
            }
220
 
          else if (pcdata.dos_part < 4)
221
 
            /* If this partition is a logical one, shouldn't increase the
222
 
               partition number.  */
223
 
            pcdata.dos_part++;
224
 
        }
225
 
 
226
 
      /* Find an extended partition.  */
227
 
      for (i = 0; i < 4; i++)
228
 
        {
229
 
          e = mbr.entries + i;
230
 
 
231
 
          if (grub_msdos_partition_is_extended (e->type))
232
 
            {
233
 
              p.offset = pcdata.ext_offset + grub_le_to_cpu32 (e->start);
234
 
              if (! pcdata.ext_offset)
235
 
                pcdata.ext_offset = p.offset;
236
 
 
237
 
              break;
238
 
            }
239
 
        }
240
 
 
241
 
      /* If no extended partition, the end.  */
242
 
      if (i == 4)
243
 
        break;
244
 
    }
245
 
 
246
 
 finish:
247
 
  return grub_errno;
248
 
}
249
 
 
250
 
 
251
 
static grub_partition_t
252
 
pc_partition_map_probe (grub_disk_t disk, const char *str)
253
 
{
254
 
  grub_partition_t p;
255
 
  struct grub_msdos_partition *pcdata;
256
 
 
257
 
  auto int find_func (grub_disk_t d, const grub_partition_t partition);
258
 
 
259
 
  int find_func (grub_disk_t d __attribute__ ((unused)),
260
 
                 const grub_partition_t partition)
261
 
    {
262
 
      struct grub_msdos_partition *partdata = partition->data;
263
 
 
264
 
      if ((pcdata->dos_part == partdata->dos_part || pcdata->dos_part == -1)
265
 
          && pcdata->bsd_part == partdata->bsd_part)
266
 
        {
267
 
          grub_memcpy (p, partition, sizeof (*p));
268
 
          p->data = pcdata;
269
 
          grub_memcpy (pcdata, partdata, sizeof (*pcdata));
270
 
          return 1;
271
 
        }
272
 
 
273
 
      return 0;
274
 
    }
275
 
 
276
 
  p = grub_partition_parse (str);
277
 
  if (! p)
278
 
    return 0;
279
 
 
280
 
  pcdata = p->data;
281
 
  pc_partition_map_iterate (disk, find_func);
282
 
  if (grub_errno)
283
 
    goto fail;
284
 
 
285
 
  if (p->index < 0)
286
 
    {
287
 
      grub_error (GRUB_ERR_BAD_DEVICE, "no such partition");
288
 
      goto fail;
289
 
    }
290
 
 
291
 
  return p;
292
 
 
293
 
 fail:
294
 
  grub_free (p);
295
 
  grub_free (pcdata);
296
 
  return 0;
297
 
}
298
 
 
299
 
 
300
 
static char *
301
 
pc_partition_map_get_name (const grub_partition_t p)
302
 
{
303
 
  struct grub_msdos_partition *pcdata = p->data;
304
 
 
305
 
  if (pcdata->bsd_part < 0)
306
 
    return grub_xasprintf ("%d", pcdata->dos_part + 1);
307
 
  else if (pcdata->dos_part < 0)
308
 
    return grub_xasprintf ("%c", pcdata->bsd_part + 'a');
309
 
  else
310
 
    return grub_xasprintf ("%d,%c", pcdata->dos_part + 1,
311
 
                          pcdata->bsd_part + 'a');
312
 
}
313
 
 
314
 
 
315
 
/* Partition map type.  */
316
 
static struct grub_partition_map grub_msdos_partition_map =
317
 
  {
318
 
    .name = "part_msdos",
319
 
    .iterate = pc_partition_map_iterate,
320
 
    .probe = pc_partition_map_probe,
321
 
    .get_name = pc_partition_map_get_name
322
 
  };
323
 
 
324
 
GRUB_MOD_INIT(pc_partition_map)
325
 
{
326
 
  grub_partition_map_register (&grub_msdos_partition_map);
327
 
}
328
 
 
329
 
GRUB_MOD_FINI(pc_partition_map)
330
 
{
331
 
  grub_partition_map_unregister (&grub_msdos_partition_map);
332
 
}