1
/* pc.c - Read PC style partition tables. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2002,2004,2005,2006,2007,2008,2009 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/partition.h>
21
#include <grub/msdos_partition.h>
22
#include <grub/disk.h>
24
#include <grub/misc.h>
27
static struct grub_partition_map grub_msdos_partition_map;
31
pc_partition_map_iterate (grub_disk_t disk,
32
int (*hook) (grub_disk_t disk,
33
const grub_partition_t partition))
35
struct grub_partition p;
36
struct grub_msdos_partition_mbr mbr;
38
grub_disk_addr_t lastaddr;
39
grub_disk_addr_t ext_offset;
44
p.partmap = &grub_msdos_partition_map;
46
/* Any value different than `p.offset' will satisfy the check during
53
struct grub_msdos_partition_entry *e;
56
if (grub_disk_read (disk, p.offset, 0, sizeof (mbr), &mbr))
59
/* This is our loop-detection algorithm. It works the following way:
60
It saves last position which was a power of two. Then it compares the
61
saved value with a current one. This way it's guaranteed that the loop
62
will be broken by at most third walk.
64
if (labeln && lastaddr == p.offset)
65
return grub_error (GRUB_ERR_BAD_PART_TABLE, "loop detected");
68
if ((labeln & (labeln - 1)) == 0)
71
/* Check if it is valid. */
72
if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
73
return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
75
for (i = 0; i < 4; i++)
76
if (mbr.entries[i].flag & 0x7f)
77
return grub_error (GRUB_ERR_BAD_PART_TABLE, "bad boot flag");
79
/* Analyze DOS partitions. */
80
for (p.index = 0; p.index < 4; p.index++)
82
e = mbr.entries + p.index;
84
p.start = p.offset + grub_le_to_cpu32 (e->start);
85
p.len = grub_le_to_cpu32 (e->length);
87
grub_dprintf ("partition",
88
"partition %d: flag 0x%x, type 0x%x, start 0x%llx, len 0x%llx\n",
89
p.index, e->flag, e->type,
90
(unsigned long long) p.start,
91
(unsigned long long) p.len);
93
/* If this is a GPT partition, this MBR is just a dummy. */
94
if (e->type == GRUB_PC_PARTITION_TYPE_GPT_DISK && p.index == 0)
95
return grub_error (GRUB_ERR_BAD_PART_TABLE, "dummy mbr");
97
/* If this partition is a normal one, call the hook. */
98
if (! grub_msdos_partition_is_empty (e->type)
99
&& ! grub_msdos_partition_is_extended (e->type))
106
else if (p.number < 4)
107
/* If this partition is a logical one, shouldn't increase the
112
/* Find an extended partition. */
113
for (i = 0; i < 4; i++)
117
if (grub_msdos_partition_is_extended (e->type))
119
p.offset = ext_offset + grub_le_to_cpu32 (e->start);
121
ext_offset = p.offset;
127
/* If no extended partition, the end. */
137
/* Partition map type. */
138
static struct grub_partition_map grub_msdos_partition_map =
141
.iterate = pc_partition_map_iterate,
144
GRUB_MOD_INIT(part_msdos)
146
grub_partition_map_register (&grub_msdos_partition_map);
149
GRUB_MOD_FINI(part_msdos)
151
grub_partition_map_unregister (&grub_msdos_partition_map);