gpt.c 6.29 KB
Newer Older
1 2 3
/* gpt.c - Read GUID Partition Tables (GPT).  */
/*
 *  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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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 21 22 23 24
 */

#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/partition.h>
#include <grub/dl.h>
25
#include <grub/msdos_partition.h>
26
#include <grub/gpt_partition.h>
27
#include <grub/i18n.h>
28

29 30
GRUB_MOD_LICENSE ("GPLv3+");

31 32
static grub_uint8_t grub_gpt_magic[8] =
  {
33
    0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54
34 35
  };

36
static const grub_gpt_part_type_t grub_gpt_partition_type_empty = GRUB_GPT_PARTITION_TYPE_EMPTY;
37

38
#ifdef GRUB_UTIL
39
static const grub_gpt_part_type_t grub_gpt_partition_type_bios_boot = GRUB_GPT_PARTITION_TYPE_BIOS_BOOT;
40 41
#endif

42 43 44
/* 512 << 7 = 65536 byte sectors.  */
#define MAX_SECTOR_LOG 7

45 46 47 48
static struct grub_partition_map grub_gpt_partition_map;



49 50
grub_err_t
grub_gpt_partition_map_iterate (grub_disk_t disk,
51 52
				grub_partition_iterate_hook_t hook,
				void *hook_data)
53 54 55 56
{
  struct grub_partition part;
  struct grub_gpt_header gpt;
  struct grub_gpt_partentry entry;
57
  struct grub_msdos_partition_mbr mbr;
58 59 60
  grub_uint64_t entries;
  unsigned int i;
  int last_offset = 0;
61
  int sector_log = 0;
62 63

  /* Read the protective MBR.  */
64
  if (grub_disk_read (disk, 0, 0, sizeof (mbr), &mbr))
65 66 67
    return grub_errno;

  /* Check if it is valid.  */
68
  if (mbr.signature != grub_cpu_to_le16_compile_time (GRUB_PC_PARTITION_SIGNATURE))
69 70 71
    return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");

  /* Make sure the MBR is a protective MBR and not a normal MBR.  */
72 73 74 75
  for (i = 0; i < 4; i++)
    if (mbr.entries[i].type == GRUB_PC_PARTITION_TYPE_GPT_DISK)
      break;
  if (i == 4)
76 77 78
    return grub_error (GRUB_ERR_BAD_PART_TABLE, "no GPT partition map found");

  /* Read the GPT header.  */
79 80 81 82
  for (sector_log = 0; sector_log < MAX_SECTOR_LOG; sector_log++)
    {
      if (grub_disk_read (disk, 1 << sector_log, 0, sizeof (gpt), &gpt))
	return grub_errno;
83

84 85 86 87
      if (grub_memcmp (gpt.magic, grub_gpt_magic, sizeof (grub_gpt_magic)) == 0)
	break;
    }
  if (sector_log == MAX_SECTOR_LOG)
88 89 90 91
    return grub_error (GRUB_ERR_BAD_PART_TABLE, "no valid GPT header");

  grub_dprintf ("gpt", "Read a valid GPT header\n");

92
  entries = grub_le_to_cpu64 (gpt.partitions) << sector_log;
93 94
  for (i = 0; i < grub_le_to_cpu32 (gpt.maxpart); i++)
    {
95
      if (grub_disk_read (disk, entries, last_offset,
96
			  sizeof (entry), &entry))
97
	return grub_errno;
98

99
      if (grub_memcmp (&grub_gpt_partition_type_empty, &entry.type,
100 101 102
		       sizeof (grub_gpt_partition_type_empty)))
	{
	  /* Calculate the first block and the size of the partition.  */
103
	  part.start = grub_le_to_cpu64 (entry.start) << sector_log;
104
	  part.len = (grub_le_to_cpu64 (entry.end)
105
		      - grub_le_to_cpu64 (entry.start) + 1)  << sector_log;
106
	  part.offset = entries;
107 108
	  part.number = i;
	  part.index = last_offset;
109
	  part.partmap = &grub_gpt_partition_map;
110
	  part.parent = disk->partition;
111

112 113 114
	  grub_dprintf ("gpt", "GPT entry %d: start=%lld, length=%lld\n", i,
			(unsigned long long) part.start,
			(unsigned long long) part.len);
115

116
	  if (hook (disk, &part, hook_data))
117
	    return grub_errno;
118 119 120 121 122 123 124 125 126 127
	}

      last_offset += grub_le_to_cpu32 (gpt.partentry_size);
      if (last_offset == GRUB_DISK_SECTOR_SIZE)
	{
	  last_offset = 0;
	  entries++;
	}
    }

128
  return GRUB_ERR_NONE;
129 130
}

131
#ifdef GRUB_UTIL
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/* Context for gpt_partition_map_embed.  */
struct gpt_partition_map_embed_ctx
{
  grub_disk_addr_t start, len;
};

/* Helper for gpt_partition_map_embed.  */
static int
find_usable_region (grub_disk_t disk __attribute__ ((unused)),
		    const grub_partition_t p, void *data)
{
  struct gpt_partition_map_embed_ctx *ctx = data;
  struct grub_gpt_partentry gptdata;
  grub_partition_t p2;

  p2 = disk->partition;
  disk->partition = p->parent;
  if (grub_disk_read (disk, p->offset, p->index,
		      sizeof (gptdata), &gptdata))
    {
      disk->partition = p2;
      return 0;
    }
  disk->partition = p2;

  /* If there's an embed region, it is in a dedicated partition.  */
  if (! grub_memcmp (&gptdata.type, &grub_gpt_partition_type_bios_boot, 16))
    {
      ctx->start = p->start;
      ctx->len = p->len;
      return 1;
    }

  return 0;
}

168
static grub_err_t
169
gpt_partition_map_embed (struct grub_disk *disk, unsigned int *nsectors,
170
			 unsigned int max_nsectors,
171
			 grub_embed_type_t embed_type,
172
			 grub_disk_addr_t **sectors)
173
{
174 175 176 177
  struct gpt_partition_map_embed_ctx ctx = {
    .start = 0,
    .len = 0
  };
178 179 180 181 182
  unsigned i;
  grub_err_t err;

  if (embed_type != GRUB_EMBED_PCBIOS)
    return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
183
		       "GPT currently supports only PC-BIOS embedding");
184

185
  err = grub_gpt_partition_map_iterate (disk, find_usable_region, &ctx);
186 187 188
  if (err)
    return err;

189
  if (ctx.len == 0)
190
    return grub_error (GRUB_ERR_FILE_NOT_FOUND,
191
		       N_("this GPT partition label contains no BIOS Boot Partition;"
192
			  " embedding won't be possible"));
193

194
  if (ctx.len < *nsectors)
195
    return grub_error (GRUB_ERR_OUT_OF_RANGE,
196 197
		       N_("your BIOS Boot Partition is too small;"
			  " embedding won't be possible"));
198

199
  *nsectors = ctx.len;
200 201
  if (*nsectors > max_nsectors)
    *nsectors = max_nsectors;
202 203 204 205
  *sectors = grub_malloc (*nsectors * sizeof (**sectors));
  if (!*sectors)
    return grub_errno;
  for (i = 0; i < *nsectors; i++)
206
    (*sectors)[i] = ctx.start + i;
207 208 209 210 211

  return GRUB_ERR_NONE;
}
#endif

212 213 214 215

/* Partition map type.  */
static struct grub_partition_map grub_gpt_partition_map =
  {
216
    .name = "gpt",
217
    .iterate = grub_gpt_partition_map_iterate,
218 219 220
#ifdef GRUB_UTIL
    .embed = gpt_partition_map_embed
#endif
221 222
  };

223
GRUB_MOD_INIT(part_gpt)
224 225 226 227
{
  grub_partition_map_register (&grub_gpt_partition_map);
}

228
GRUB_MOD_FINI(part_gpt)
229 230 231
{
  grub_partition_map_unregister (&grub_gpt_partition_map);
}