removed accidental commit
[grub2/phcoder.git] / partmap / gpt.c
blob8a690cc637d69989301bd66d9661c312878aedf5
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.
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.
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.
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/>.
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/pc_partition.h>
26 #include <grub/gpt_partition.h>
28 static grub_uint8_t grub_gpt_magic[8] =
30 0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54
33 static const grub_gpt_part_type_t grub_gpt_partition_type_empty = GRUB_GPT_PARTITION_TYPE_EMPTY;
35 static struct grub_partition_map grub_gpt_partition_map;
39 static grub_err_t
40 gpt_partition_map_iterate (grub_disk_t disk,
41 int (*hook) (grub_disk_t disk,
42 const grub_partition_t partition))
44 struct grub_partition part;
45 struct grub_gpt_header gpt;
46 struct grub_gpt_partentry entry;
47 struct grub_pc_partition_mbr mbr;
48 grub_uint64_t entries;
49 unsigned int i;
50 int last_offset = 0;
52 /* Read the protective MBR. */
53 if (grub_disk_read (disk, 0, 0, sizeof (mbr), &mbr))
54 return grub_errno;
56 /* Check if it is valid. */
57 if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
58 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
60 /* Make sure the MBR is a protective MBR and not a normal MBR. */
61 if (mbr.entries[0].type != GRUB_PC_PARTITION_TYPE_GPT_DISK)
62 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no GPT partition map found");
64 /* Read the GPT header. */
65 if (grub_disk_read (disk, 1, 0, sizeof (gpt), &gpt))
66 return grub_errno;
68 if (grub_memcmp (gpt.magic, grub_gpt_magic, sizeof (grub_gpt_magic)))
69 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no valid GPT header");
71 grub_dprintf ("gpt", "Read a valid GPT header\n");
73 entries = grub_le_to_cpu64 (gpt.partitions);
74 part.data = grub_malloc (sizeof (entry));
75 for (i = 0; i < grub_le_to_cpu32 (gpt.maxpart); i++)
77 if (grub_disk_read (disk, entries, last_offset,
78 sizeof (entry), &entry))
80 grub_free (part.data);
81 return grub_errno;
84 if (grub_memcmp (&grub_gpt_partition_type_empty, &entry.type,
85 sizeof (grub_gpt_partition_type_empty)))
87 /* Calculate the first block and the size of the partition. */
88 part.start = grub_le_to_cpu64 (entry.start);
89 part.len = (grub_le_to_cpu64 (entry.end)
90 - grub_le_to_cpu64 (entry.start) + 1);
91 part.offset = entries;
92 part.number = i;
93 part.partmap = &grub_gpt_partition_map;
94 grub_memcpy (part.data, &entry, sizeof (entry));
96 grub_dprintf ("gpt", "GPT entry %d: start=%lld, length=%lld\n", i,
97 (unsigned long long) part.start,
98 (unsigned long long) part.len);
100 if (hook (disk, &part))
101 return 1;
104 last_offset += grub_le_to_cpu32 (gpt.partentry_size);
105 if (last_offset == GRUB_DISK_SECTOR_SIZE)
107 last_offset = 0;
108 entries++;
111 grub_free (part.data);
113 return 0;
117 /* Partition map type. */
118 static struct grub_partition_map grub_gpt_partition_map =
120 .name = "gpt_partition_map",
121 .iterate = gpt_partition_map_iterate,
124 GRUB_MOD_INIT(gpt_partition_map)
126 grub_partition_map_register (&grub_gpt_partition_map);
129 GRUB_MOD_FINI(gpt_partition_map)
131 grub_partition_map_unregister (&grub_gpt_partition_map);