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

« back to all changes in this revision

Viewing changes to grub-core/partmap/gpt.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
/* gpt.c - Read GUID Partition Tables (GPT).  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2002,2005,2006,2007,2008  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/disk.h>
 
21
#include <grub/misc.h>
 
22
#include <grub/mm.h>
 
23
#include <grub/partition.h>
 
24
#include <grub/dl.h>
 
25
#include <grub/msdos_partition.h>
 
26
#include <grub/gpt_partition.h>
 
27
 
 
28
static grub_uint8_t grub_gpt_magic[8] =
 
29
  {
 
30
    0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54
 
31
  };
 
32
 
 
33
static const grub_gpt_part_type_t grub_gpt_partition_type_empty = GRUB_GPT_PARTITION_TYPE_EMPTY;
 
34
 
 
35
#ifdef GRUB_UTIL
 
36
static const grub_gpt_part_type_t grub_gpt_partition_type_bios_boot = GRUB_GPT_PARTITION_TYPE_BIOS_BOOT;
 
37
#endif
 
38
 
 
39
/* 512 << 7 = 65536 byte sectors.  */
 
40
#define MAX_SECTOR_LOG 7
 
41
 
 
42
static struct grub_partition_map grub_gpt_partition_map;
 
43
 
 
44
 
 
45
 
 
46
static grub_err_t
 
47
gpt_partition_map_iterate (grub_disk_t disk,
 
48
                           int (*hook) (grub_disk_t disk,
 
49
                                        const grub_partition_t partition))
 
50
{
 
51
  struct grub_partition part;
 
52
  struct grub_gpt_header gpt;
 
53
  struct grub_gpt_partentry entry;
 
54
  struct grub_msdos_partition_mbr mbr;
 
55
  grub_uint64_t entries;
 
56
  unsigned int i;
 
57
  int last_offset = 0;
 
58
  int sector_log = 0;
 
59
 
 
60
  /* Read the protective MBR.  */
 
61
  if (grub_disk_read (disk, 0, 0, sizeof (mbr), &mbr))
 
62
    return grub_errno;
 
63
 
 
64
  /* Check if it is valid.  */
 
65
  if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
 
66
    return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
 
67
 
 
68
  /* Make sure the MBR is a protective MBR and not a normal MBR.  */
 
69
  if (mbr.entries[0].type != GRUB_PC_PARTITION_TYPE_GPT_DISK)
 
70
    return grub_error (GRUB_ERR_BAD_PART_TABLE, "no GPT partition map found");
 
71
 
 
72
  /* Read the GPT header.  */
 
73
  for (sector_log = 0; sector_log < MAX_SECTOR_LOG; sector_log++)
 
74
    {
 
75
      if (grub_disk_read (disk, 1 << sector_log, 0, sizeof (gpt), &gpt))
 
76
        return grub_errno;
 
77
 
 
78
      if (grub_memcmp (gpt.magic, grub_gpt_magic, sizeof (grub_gpt_magic)) == 0)
 
79
        break;
 
80
    }
 
81
  if (sector_log == MAX_SECTOR_LOG)
 
82
    return grub_error (GRUB_ERR_BAD_PART_TABLE, "no valid GPT header");
 
83
 
 
84
  grub_dprintf ("gpt", "Read a valid GPT header\n");
 
85
 
 
86
  entries = grub_le_to_cpu64 (gpt.partitions) << sector_log;
 
87
  for (i = 0; i < grub_le_to_cpu32 (gpt.maxpart); i++)
 
88
    {
 
89
      if (grub_disk_read (disk, entries, last_offset,
 
90
                          sizeof (entry), &entry))
 
91
        return grub_errno;
 
92
 
 
93
      if (grub_memcmp (&grub_gpt_partition_type_empty, &entry.type,
 
94
                       sizeof (grub_gpt_partition_type_empty)))
 
95
        {
 
96
          /* Calculate the first block and the size of the partition.  */
 
97
          part.start = grub_le_to_cpu64 (entry.start) << sector_log;
 
98
          part.len = (grub_le_to_cpu64 (entry.end)
 
99
                      - grub_le_to_cpu64 (entry.start) + 1)  << sector_log;
 
100
          part.offset = entries;
 
101
          part.number = i;
 
102
          part.index = last_offset;
 
103
          part.partmap = &grub_gpt_partition_map;
 
104
          part.parent = disk->partition;
 
105
 
 
106
          grub_dprintf ("gpt", "GPT entry %d: start=%lld, length=%lld\n", i,
 
107
                        (unsigned long long) part.start,
 
108
                        (unsigned long long) part.len);
 
109
 
 
110
          if (hook (disk, &part))
 
111
            return grub_errno;
 
112
        }
 
113
 
 
114
      last_offset += grub_le_to_cpu32 (gpt.partentry_size);
 
115
      if (last_offset == GRUB_DISK_SECTOR_SIZE)
 
116
        {
 
117
          last_offset = 0;
 
118
          entries++;
 
119
        }
 
120
    }
 
121
 
 
122
  return GRUB_ERR_NONE;
 
123
}
 
124
 
 
125
#ifdef GRUB_UTIL
 
126
static grub_err_t
 
127
gpt_partition_map_embed (struct grub_disk *disk, unsigned int *nsectors,
 
128
                         grub_embed_type_t embed_type,
 
129
                         grub_disk_addr_t **sectors)
 
130
{
 
131
  grub_disk_addr_t start = 0, len = 0;
 
132
  unsigned i;
 
133
  grub_err_t err;
 
134
 
 
135
  auto int NESTED_FUNC_ATTR find_usable_region (grub_disk_t disk,
 
136
                                                const grub_partition_t p);
 
137
  int NESTED_FUNC_ATTR find_usable_region (grub_disk_t disk __attribute__ ((unused)),
 
138
                                           const grub_partition_t p)
 
139
  {
 
140
    struct grub_gpt_partentry gptdata;
 
141
 
 
142
    disk->partition = p->parent;
 
143
    if (grub_disk_read (disk, p->offset, p->index,
 
144
                        sizeof (gptdata), &gptdata))
 
145
      return 0;
 
146
 
 
147
    /* If there's an embed region, it is in a dedicated partition.  */
 
148
    if (! grub_memcmp (&gptdata.type, &grub_gpt_partition_type_bios_boot, 16))
 
149
      {
 
150
        start = p->start;
 
151
        len = p->len;
 
152
        return 1;
 
153
      }
 
154
 
 
155
      return 0;
 
156
    }
 
157
 
 
158
  if (embed_type != GRUB_EMBED_PCBIOS)
 
159
    return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 
160
                       "GPT curently supports only PC-BIOS embedding");
 
161
 
 
162
  err = gpt_partition_map_iterate (disk, find_usable_region);
 
163
  if (err)
 
164
    return err;
 
165
 
 
166
  if (len == 0)
 
167
    return grub_error (GRUB_ERR_FILE_NOT_FOUND,
 
168
                       "This GPT partition label has no BIOS Boot Partition;"
 
169
                       " embedding won't be possible!");
 
170
 
 
171
  if (len < *nsectors)
 
172
    return grub_error (GRUB_ERR_OUT_OF_RANGE,
 
173
                       "Your BIOS Boot Partition is too small;"
 
174
                       " embedding won't be possible!");
 
175
 
 
176
  *nsectors = len;
 
177
  *sectors = grub_malloc (*nsectors * sizeof (**sectors));
 
178
  if (!*sectors)
 
179
    return grub_errno;
 
180
  for (i = 0; i < *nsectors; i++)
 
181
    (*sectors)[i] = start + i;
 
182
 
 
183
  return GRUB_ERR_NONE;
 
184
}
 
185
#endif
 
186
 
 
187
 
 
188
/* Partition map type.  */
 
189
static struct grub_partition_map grub_gpt_partition_map =
 
190
  {
 
191
    .name = "gpt",
 
192
    .iterate = gpt_partition_map_iterate,
 
193
#ifdef GRUB_UTIL
 
194
    .embed = gpt_partition_map_embed
 
195
#endif
 
196
  };
 
197
 
 
198
GRUB_MOD_INIT(part_gpt)
 
199
{
 
200
  grub_partition_map_register (&grub_gpt_partition_map);
 
201
}
 
202
 
 
203
GRUB_MOD_FINI(part_gpt)
 
204
{
 
205
  grub_partition_map_unregister (&grub_gpt_partition_map);
 
206
}