~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to partmap/pc.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

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 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 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 GRUB; if not, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <grub/partition.h>
 
22
#include <grub/pc_partition.h>
 
23
#include <grub/disk.h>
 
24
#include <grub/mm.h>
 
25
#include <grub/misc.h>
 
26
#include <grub/dl.h>
 
27
 
 
28
static struct grub_partition_map grub_pc_partition_map;
 
29
 
 
30
#ifndef GRUB_UTIL
 
31
static grub_dl_t my_mod;
 
32
#endif
 
33
 
 
34
 
 
35
/* Parse the partition representation in STR and return a partition.  */
 
36
static grub_partition_t
 
37
grub_partition_parse (const char *str)
 
38
{
 
39
  grub_partition_t p;
 
40
  struct grub_pc_partition *pcdata;
 
41
  
 
42
  char *s = (char *) str;
 
43
  
 
44
  p = (grub_partition_t) grub_malloc (sizeof (*p));
 
45
  if (! p)
 
46
    return 0;
 
47
  
 
48
  pcdata = (struct grub_pc_partition *) grub_malloc (sizeof (*pcdata));
 
49
  if (! pcdata)
 
50
    goto fail;
 
51
  
 
52
  p->data = pcdata;
 
53
  p->partmap = &grub_pc_partition_map;
 
54
  
 
55
  /* Initialize some of the fields with invalid values.  */
 
56
  pcdata->bsd_part = pcdata->dos_type = pcdata->bsd_type = p->index = -1;
 
57
 
 
58
  /* Get the DOS partition number.  */
 
59
  pcdata->dos_part = grub_strtoul (s, &s, 0);
 
60
  
 
61
  if (grub_errno)
 
62
    {
 
63
      /* Not found. Maybe only a BSD label is specified.  */
 
64
      pcdata->dos_part = -1;
 
65
      grub_errno = GRUB_ERR_NONE;
 
66
    }
 
67
  else if (*s == ',')
 
68
    s++;
 
69
 
 
70
  if (*s)
 
71
    {
 
72
      if (*s >= 'a' && *s <= 'h')
 
73
        {
 
74
          pcdata->bsd_part = *s - 'a';
 
75
          s++;
 
76
        }
 
77
 
 
78
      if (*s)
 
79
        goto fail;
 
80
    }
 
81
 
 
82
  if (pcdata->dos_part == -1 && pcdata->bsd_part == -1)
 
83
    goto fail;
 
84
 
 
85
  return p;
 
86
  
 
87
 fail:
 
88
  grub_free (p);
 
89
  grub_free (pcdata);
 
90
  grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
 
91
  return 0;
 
92
}
 
93
 
 
94
static grub_err_t
 
95
pc_partition_map_iterate (grub_disk_t disk,
 
96
                          int (*hook) (grub_disk_t disk,
 
97
                                       const grub_partition_t partition))
 
98
{
 
99
  struct grub_partition p;
 
100
  struct grub_pc_partition pcdata;
 
101
  struct grub_pc_partition_mbr mbr;
 
102
  struct grub_pc_partition_disk_label label;
 
103
  struct grub_disk raw;
 
104
 
 
105
  /* Enforce raw disk access.  */
 
106
  raw = *disk;
 
107
  raw.partition = 0;
 
108
  
 
109
  p.offset = 0;
 
110
  pcdata.ext_offset = 0;
 
111
  pcdata.dos_part = -1;
 
112
  p.data = &pcdata;
 
113
  p.partmap = &grub_pc_partition_map;
 
114
  
 
115
  while (1)
 
116
    {
 
117
      int i;
 
118
      struct grub_pc_partition_entry *e;
 
119
      
 
120
      /* Read the MBR.  */
 
121
      if (grub_disk_read (&raw, p.offset, 0, sizeof (mbr), (char *) &mbr))
 
122
        goto finish;
 
123
 
 
124
      /* Check if it is valid.  */
 
125
      if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
 
126
        return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
 
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%lx, len 0x%lx\n",
 
141
                        p.index, e->flag, pcdata.dos_type, p.start, p.len);
 
142
 
 
143
          /* If this is a GPT partition, this MBR is just a dummy.  */
 
144
          if (e->type == GRUB_PC_PARTITION_TYPE_GPT_DISK && p.index == 0)
 
145
            return grub_error (GRUB_ERR_BAD_PART_TABLE, "dummy mbr");
 
146
 
 
147
          /* If this partition is a normal one, call the hook.  */
 
148
          if (! grub_pc_partition_is_empty (e->type)
 
149
              && ! grub_pc_partition_is_extended (e->type))
 
150
            {
 
151
              pcdata.dos_part++;
 
152
              
 
153
              if (hook (disk, &p))
 
154
                goto finish;
 
155
 
 
156
              /* Check if this is a BSD partition.  */
 
157
              if (grub_pc_partition_is_bsd (e->type))
 
158
                {
 
159
                  /* Check if the BSD label is within the DOS partition.  */
 
160
                  if (p.len <= GRUB_PC_PARTITION_BSD_LABEL_SECTOR)
 
161
                    return grub_error (GRUB_ERR_BAD_PART_TABLE,
 
162
                                       "no space for disk label");
 
163
 
 
164
                  /* Read the BSD label.  */
 
165
                  if (grub_disk_read (&raw,
 
166
                                      (p.start
 
167
                                       + GRUB_PC_PARTITION_BSD_LABEL_SECTOR),
 
168
                                      0,
 
169
                                      sizeof (label),
 
170
                                      (char *) &label))
 
171
                    goto finish;
 
172
 
 
173
                  /* Check if it is valid.  */
 
174
                  if (label.magic
 
175
                      != grub_cpu_to_le32 (GRUB_PC_PARTITION_BSD_LABEL_MAGIC))
 
176
                    return grub_error (GRUB_ERR_BAD_PART_TABLE,
 
177
                                       "invalid disk label magic 0x%x",
 
178
                                       label.magic);
 
179
 
 
180
                  for (pcdata.bsd_part = 0;
 
181
                       pcdata.bsd_part < grub_cpu_to_le16 (label.num_partitions);
 
182
                       pcdata.bsd_part++)
 
183
                    {
 
184
                      struct grub_pc_partition_bsd_entry *be
 
185
                        = label.entries + pcdata.bsd_part;
 
186
 
 
187
                      p.start = grub_le_to_cpu32 (be->offset);
 
188
                      p.len = grub_le_to_cpu32 (be->size);
 
189
                      pcdata.bsd_type = be->fs_type;
 
190
                      
 
191
                      if (be->fs_type != GRUB_PC_PARTITION_BSD_TYPE_UNUSED)
 
192
                        if (hook (disk, &p))
 
193
                          goto finish;
 
194
                    }
 
195
                }
 
196
            }
 
197
          else if (pcdata.dos_part < 4)
 
198
            /* If this partition is a logical one, shouldn't increase the
 
199
               partition number.  */
 
200
            pcdata.dos_part++;
 
201
        }
 
202
 
 
203
      /* Find an extended partition.  */
 
204
      for (i = 0; i < 4; i++)
 
205
        {
 
206
          e = mbr.entries + i;
 
207
          
 
208
          if (grub_pc_partition_is_extended (e->type))
 
209
            {
 
210
              p.offset = pcdata.ext_offset + grub_le_to_cpu32 (e->start);
 
211
              if (! pcdata.ext_offset)
 
212
                pcdata.ext_offset = p.offset;
 
213
 
 
214
              break;
 
215
            }
 
216
        }
 
217
 
 
218
      /* If no extended partition, the end.  */
 
219
      if (i == 4)
 
220
        break;
 
221
    }
 
222
 
 
223
 finish:
 
224
  return grub_errno;
 
225
}
 
226
 
 
227
 
 
228
static grub_partition_t
 
229
pc_partition_map_probe (grub_disk_t disk, const char *str)
 
230
{
 
231
  grub_partition_t p;
 
232
  struct grub_pc_partition *pcdata;
 
233
  
 
234
  auto int find_func (grub_disk_t d, const grub_partition_t partition);
 
235
 
 
236
  int find_func (grub_disk_t d __attribute__ ((unused)),
 
237
                 const grub_partition_t partition)
 
238
    {
 
239
      struct grub_pc_partition *partdata = partition->data;
 
240
      if ((pcdata->dos_part == partdata->dos_part || pcdata->dos_part == -1)
 
241
          && pcdata->bsd_part == partdata->bsd_part)
 
242
        {
 
243
          grub_memcpy (p, partition, sizeof (*p));
 
244
          p->data = pcdata;
 
245
          grub_memcpy (pcdata, partdata, sizeof (*pcdata));
 
246
          return 1;
 
247
        }
 
248
      
 
249
      return 0;
 
250
    }
 
251
  
 
252
  p = grub_partition_parse (str);
 
253
  if (! p)
 
254
    return 0;
 
255
  
 
256
  pcdata = p->data;
 
257
  if (pc_partition_map_iterate (disk, find_func))
 
258
    goto fail;
 
259
 
 
260
  if (p->index < 0)
 
261
    {
 
262
      grub_error (GRUB_ERR_BAD_DEVICE, "no such partition");
 
263
      goto fail;
 
264
    }
 
265
 
 
266
  return p;
 
267
 
 
268
 fail:
 
269
  grub_free (p);
 
270
  grub_free (pcdata);
 
271
  return 0;
 
272
}
 
273
 
 
274
 
 
275
static char *
 
276
pc_partition_map_get_name (const grub_partition_t p)
 
277
{
 
278
  char *name;
 
279
  struct grub_pc_partition *pcdata = p->data;
 
280
  
 
281
  name = grub_malloc (13);
 
282
  if (! name)
 
283
    return 0;
 
284
 
 
285
  if (pcdata->bsd_part < 0)
 
286
    grub_sprintf (name, "%d", pcdata->dos_part);
 
287
  else
 
288
    grub_sprintf (name, "%d,%c", pcdata->dos_part, pcdata->bsd_part + 'a');
 
289
 
 
290
  return name;
 
291
}
 
292
 
 
293
 
 
294
/* Partition map type.  */
 
295
static struct grub_partition_map grub_pc_partition_map =
 
296
  {
 
297
    .name = "pc_partition_map",
 
298
    .iterate = pc_partition_map_iterate,
 
299
    .probe = pc_partition_map_probe,
 
300
    .get_name = pc_partition_map_get_name
 
301
  };
 
302
 
 
303
GRUB_MOD_INIT(pc_partition_map)
 
304
{
 
305
  grub_partition_map_register (&grub_pc_partition_map);
 
306
#ifndef GRUB_UTIL
 
307
  my_mod = mod;
 
308
#endif
 
309
}
 
310
 
 
311
GRUB_MOD_FINI(pc_partition_map)
 
312
{
 
313
  grub_partition_map_unregister (&grub_pc_partition_map);
 
314
}