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

« back to all changes in this revision

Viewing changes to partmap/msdos.c

ImportĀ upstreamĀ versionĀ 1.97~beta3

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
 
 
101
  /* Enforce raw disk access.  */
 
102
  raw = *disk;
 
103
  raw.partition = 0;
 
104
 
 
105
  p.offset = 0;
 
106
  pcdata.ext_offset = 0;
 
107
  pcdata.dos_part = -1;
 
108
  p.data = &pcdata;
 
109
  p.partmap = &grub_msdos_partition_map;
 
110
 
 
111
  while (1)
 
112
    {
 
113
      int i;
 
114
      struct grub_msdos_partition_entry *e;
 
115
 
 
116
      /* Read the MBR.  */
 
117
      if (grub_disk_read (&raw, p.offset, 0, sizeof (mbr), &mbr))
 
118
        goto finish;
 
119
 
 
120
      /* Check if it is valid.  */
 
121
      if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
 
122
        return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
 
123
 
 
124
      for (i = 0; i < 4; i++)
 
125
        if (mbr.entries[i].flag & 0x7f)
 
126
          return grub_error (GRUB_ERR_BAD_PART_TABLE, "bad boot flag");
 
127
 
 
128
      /* Analyze DOS partitions.  */
 
129
      for (p.index = 0; p.index < 4; p.index++)
 
130
        {
 
131
          e = mbr.entries + p.index;
 
132
 
 
133
          p.start = p.offset + grub_le_to_cpu32 (e->start);
 
134
          p.len = grub_le_to_cpu32 (e->length);
 
135
          pcdata.bsd_part = -1;
 
136
          pcdata.dos_type = e->type;
 
137
          pcdata.bsd_type = -1;
 
138
 
 
139
          grub_dprintf ("partition",
 
140
                        "partition %d: flag 0x%x, type 0x%x, start 0x%llx, len 0x%llx\n",
 
141
                        p.index, e->flag, pcdata.dos_type,
 
142
                        (unsigned long long) p.start,
 
143
                        (unsigned long long) p.len);
 
144
 
 
145
          /* If this is a GPT partition, this MBR is just a dummy.  */
 
146
          if (e->type == GRUB_PC_PARTITION_TYPE_GPT_DISK && p.index == 0)
 
147
            return grub_error (GRUB_ERR_BAD_PART_TABLE, "dummy mbr");
 
148
 
 
149
          /* If this partition is a normal one, call the hook.  */
 
150
          if (! grub_msdos_partition_is_empty (e->type)
 
151
              && ! grub_msdos_partition_is_extended (e->type))
 
152
            {
 
153
              pcdata.dos_part++;
 
154
 
 
155
              if (hook (disk, &p))
 
156
                return 1;
 
157
 
 
158
              /* Check if this is a BSD partition.  */
 
159
              if (grub_msdos_partition_is_bsd (e->type))
 
160
                {
 
161
                  /* Check if the BSD label is within the DOS partition.  */
 
162
                  if (p.len <= GRUB_PC_PARTITION_BSD_LABEL_SECTOR)
 
163
                    {
 
164
                      grub_dprintf ("partition", "no space for disk label\n");
 
165
                      continue;
 
166
                    }
 
167
                  /* Read the BSD label.  */
 
168
                  if (grub_disk_read (&raw,
 
169
                                      (p.start
 
170
                                       + GRUB_PC_PARTITION_BSD_LABEL_SECTOR),
 
171
                                      0,
 
172
                                      sizeof (label),
 
173
                                      &label))
 
174
                    goto finish;
 
175
 
 
176
                  /* Check if it is valid.  */
 
177
                  if (label.magic
 
178
                      != grub_cpu_to_le32 (GRUB_PC_PARTITION_BSD_LABEL_MAGIC))
 
179
                    {
 
180
                      grub_dprintf ("partition",
 
181
                                    "invalid disk label magic 0x%x on partition %d\n",
 
182
                                    label.magic, p.index);
 
183
                      continue;
 
184
                    }
 
185
                  for (pcdata.bsd_part = 0;
 
186
                       pcdata.bsd_part < grub_cpu_to_le16 (label.num_partitions);
 
187
                       pcdata.bsd_part++)
 
188
                    {
 
189
                      struct grub_msdos_partition_bsd_entry *be
 
190
                        = label.entries + pcdata.bsd_part;
 
191
 
 
192
                      p.start = grub_le_to_cpu32 (be->offset);
 
193
                      p.len = grub_le_to_cpu32 (be->size);
 
194
                      pcdata.bsd_type = be->fs_type;
 
195
 
 
196
                      if (be->fs_type != GRUB_PC_PARTITION_BSD_TYPE_UNUSED)
 
197
                        if (hook (disk, &p))
 
198
                          return 1;
 
199
                    }
 
200
                }
 
201
            }
 
202
          else if (pcdata.dos_part < 4)
 
203
            /* If this partition is a logical one, shouldn't increase the
 
204
               partition number.  */
 
205
            pcdata.dos_part++;
 
206
        }
 
207
 
 
208
      /* Find an extended partition.  */
 
209
      for (i = 0; i < 4; i++)
 
210
        {
 
211
          e = mbr.entries + i;
 
212
 
 
213
          if (grub_msdos_partition_is_extended (e->type))
 
214
            {
 
215
              p.offset = pcdata.ext_offset + grub_le_to_cpu32 (e->start);
 
216
              if (! pcdata.ext_offset)
 
217
                pcdata.ext_offset = p.offset;
 
218
 
 
219
              break;
 
220
            }
 
221
        }
 
222
 
 
223
      /* If no extended partition, the end.  */
 
224
      if (i == 4)
 
225
        break;
 
226
    }
 
227
 
 
228
 finish:
 
229
  return grub_errno;
 
230
}
 
231
 
 
232
 
 
233
static grub_partition_t
 
234
pc_partition_map_probe (grub_disk_t disk, const char *str)
 
235
{
 
236
  grub_partition_t p;
 
237
  struct grub_msdos_partition *pcdata;
 
238
 
 
239
  auto int find_func (grub_disk_t d, const grub_partition_t partition);
 
240
 
 
241
  int find_func (grub_disk_t d __attribute__ ((unused)),
 
242
                 const grub_partition_t partition)
 
243
    {
 
244
      struct grub_msdos_partition *partdata = partition->data;
 
245
 
 
246
      if ((pcdata->dos_part == partdata->dos_part || pcdata->dos_part == -1)
 
247
          && pcdata->bsd_part == partdata->bsd_part)
 
248
        {
 
249
          grub_memcpy (p, partition, sizeof (*p));
 
250
          p->data = pcdata;
 
251
          grub_memcpy (pcdata, partdata, sizeof (*pcdata));
 
252
          return 1;
 
253
        }
 
254
 
 
255
      return 0;
 
256
    }
 
257
 
 
258
  p = grub_partition_parse (str);
 
259
  if (! p)
 
260
    return 0;
 
261
 
 
262
  pcdata = p->data;
 
263
  pc_partition_map_iterate (disk, find_func);
 
264
  if (grub_errno)
 
265
    goto fail;
 
266
 
 
267
  if (p->index < 0)
 
268
    {
 
269
      grub_error (GRUB_ERR_BAD_DEVICE, "no such partition");
 
270
      goto fail;
 
271
    }
 
272
 
 
273
  return p;
 
274
 
 
275
 fail:
 
276
  grub_free (p);
 
277
  grub_free (pcdata);
 
278
  return 0;
 
279
}
 
280
 
 
281
 
 
282
static char *
 
283
pc_partition_map_get_name (const grub_partition_t p)
 
284
{
 
285
  char *name;
 
286
  struct grub_msdos_partition *pcdata = p->data;
 
287
 
 
288
  name = grub_malloc (13);
 
289
  if (! name)
 
290
    return 0;
 
291
 
 
292
  if (pcdata->bsd_part < 0)
 
293
    grub_sprintf (name, "%d", pcdata->dos_part + 1);
 
294
  else if (pcdata->dos_part < 0)
 
295
    grub_sprintf (name, "%c", pcdata->bsd_part + 'a');
 
296
  else
 
297
    grub_sprintf (name, "%d,%c", pcdata->dos_part + 1, pcdata->bsd_part + 'a');
 
298
 
 
299
  return name;
 
300
}
 
301
 
 
302
 
 
303
/* Partition map type.  */
 
304
static struct grub_partition_map grub_msdos_partition_map =
 
305
  {
 
306
    .name = "part_msdos",
 
307
    .iterate = pc_partition_map_iterate,
 
308
    .probe = pc_partition_map_probe,
 
309
    .get_name = pc_partition_map_get_name
 
310
  };
 
311
 
 
312
GRUB_MOD_INIT(pc_partition_map)
 
313
{
 
314
  grub_partition_map_register (&grub_msdos_partition_map);
 
315
}
 
316
 
 
317
GRUB_MOD_FINI(pc_partition_map)
 
318
{
 
319
  grub_partition_map_unregister (&grub_msdos_partition_map);
 
320
}