2008-06-17 Robert Millan <rmh@aybabtu.com>
[grub2/phcoder/solaris.git] / partmap / gpt.c
blob36eaef21c736819d96c568b0a7cbf20e4921fe23
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;
37 #ifndef GRUB_UTIL
38 static grub_dl_t my_mod;
39 #endif
43 static grub_err_t
44 gpt_partition_map_iterate (grub_disk_t disk,
45 int (*hook) (grub_disk_t disk,
46 const grub_partition_t partition))
48 struct grub_partition part;
49 struct grub_gpt_header gpt;
50 struct grub_gpt_partentry entry;
51 struct grub_disk raw;
52 struct grub_pc_partition_mbr mbr;
53 grub_uint64_t entries;
54 unsigned int i;
55 int last_offset = 0;
57 /* Enforce raw disk access. */
58 raw = *disk;
59 raw.partition = 0;
61 /* Read the protective MBR. */
62 if (grub_disk_read (&raw, 0, 0, sizeof (mbr), (char *) &mbr))
63 return grub_errno;
65 /* Check if it is valid. */
66 if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
67 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
69 /* Make sure the MBR is a protective MBR and not a normal MBR. */
70 if (mbr.entries[0].type != GRUB_PC_PARTITION_TYPE_GPT_DISK)
71 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no GPT partition map found");
73 /* Read the GPT header. */
74 if (grub_disk_read (&raw, 1, 0, sizeof (gpt), (char *) &gpt))
75 return grub_errno;
77 if (grub_memcmp (gpt.magic, grub_gpt_magic, sizeof (grub_gpt_magic)))
78 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no valid GPT header");
80 grub_dprintf ("gpt", "Read a valid GPT header\n");
82 entries = grub_le_to_cpu64 (gpt.partitions);
83 for (i = 0; i < grub_le_to_cpu32 (gpt.maxpart); i++)
85 if (grub_disk_read (&raw, entries, last_offset,
86 sizeof (entry), (char *) &entry))
87 return grub_errno;
89 if (grub_memcmp (&grub_gpt_partition_type_empty, &entry.type,
90 sizeof (grub_gpt_partition_type_empty)))
92 /* Calculate the first block and the size of the partition. */
93 part.start = grub_le_to_cpu64 (entry.start);
94 part.len = (grub_le_to_cpu64 (entry.end)
95 - grub_le_to_cpu64 (entry.start) + 1);
96 part.offset = entries;
97 part.index = i;
98 part.partmap = &grub_gpt_partition_map;
99 part.data = &entry;
101 grub_dprintf ("gpt", "GPT entry %d: start=%lld, length=%lld\n",
102 i, part.start, part.len);
104 if (hook (disk, &part))
105 return grub_errno;
108 last_offset += grub_le_to_cpu32 (gpt.partentry_size);
109 if (last_offset == GRUB_DISK_SECTOR_SIZE)
111 last_offset = 0;
112 entries++;
116 return 0;
120 static grub_partition_t
121 gpt_partition_map_probe (grub_disk_t disk, const char *str)
123 grub_partition_t p = 0;
124 int partnum = 0;
125 char *s = (char *) str;
127 auto int find_func (grub_disk_t d, const grub_partition_t partition);
129 int find_func (grub_disk_t d __attribute__ ((unused)),
130 const grub_partition_t partition)
132 if (partnum == partition->index)
134 p = (grub_partition_t) grub_malloc (sizeof (*p));
135 if (! p)
136 return 1;
138 grub_memcpy (p, partition, sizeof (*p));
139 return 1;
142 return 0;
145 /* Get the partition number. */
146 partnum = grub_strtoul (s, 0, 10) - 1;
147 if (grub_errno)
149 grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
150 return 0;
153 if (gpt_partition_map_iterate (disk, find_func))
154 goto fail;
156 return p;
158 fail:
159 grub_free (p);
160 return 0;
164 static char *
165 gpt_partition_map_get_name (const grub_partition_t p)
167 char *name;
169 name = grub_malloc (13);
170 if (! name)
171 return 0;
173 grub_sprintf (name, "%d", p->index + 1);
174 return name;
178 /* Partition map type. */
179 static struct grub_partition_map grub_gpt_partition_map =
181 .name = "gpt_partition_map",
182 .iterate = gpt_partition_map_iterate,
183 .probe = gpt_partition_map_probe,
184 .get_name = gpt_partition_map_get_name
187 GRUB_MOD_INIT(gpt_partition_map)
189 grub_partition_map_register (&grub_gpt_partition_map);
190 #ifndef GRUB_UTIL
191 my_mod = mod;
192 #endif
195 GRUB_MOD_FINI(gpt_partition_map)
197 grub_partition_map_unregister (&grub_gpt_partition_map);