1
/* bsdlabel.c - Read BSD 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/bsdlabel.h>
22
#include <grub/disk.h>
24
#include <grub/misc.h>
26
#include <grub/msdos_partition.h>
29
#include <grub/util/misc.h>
32
static struct grub_partition_map grub_bsdlabel_partition_map;
33
static struct grub_partition_map grub_netbsdlabel_partition_map;
34
static struct grub_partition_map grub_openbsdlabel_partition_map;
39
iterate_real (grub_disk_t disk, grub_disk_addr_t sector, int freebsd,
40
struct grub_partition_map *pmap,
41
int (*hook) (grub_disk_t disk,
42
const grub_partition_t partition))
44
struct grub_partition_bsd_disk_label label;
45
struct grub_partition p;
46
grub_disk_addr_t delta = 0;
49
/* Read the BSD label. */
50
if (grub_disk_read (disk, sector, 0, sizeof (label), &label))
53
/* Check if it is valid. */
54
if (label.magic != grub_cpu_to_le32 (GRUB_PC_PARTITION_BSD_LABEL_MAGIC))
55
return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
57
/* A kludge to determine a base of be.offset. */
58
if (GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION
59
< grub_cpu_to_le16 (label.num_partitions) && freebsd)
61
struct grub_partition_bsd_entry whole_disk_be;
63
pos = sizeof (label) + sector * GRUB_DISK_SECTOR_SIZE
64
+ sizeof (struct grub_partition_bsd_entry)
65
* GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION;
67
if (grub_disk_read (disk, pos / GRUB_DISK_SECTOR_SIZE,
68
pos % GRUB_DISK_SECTOR_SIZE, sizeof (whole_disk_be),
72
delta = grub_le_to_cpu32 (whole_disk_be.offset);
75
pos = sizeof (label) + sector * GRUB_DISK_SECTOR_SIZE;
78
p.number < grub_cpu_to_le16 (label.num_partitions);
79
p.number++, pos += sizeof (struct grub_partition_bsd_entry))
81
struct grub_partition_bsd_entry be;
83
if (p.number == GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION)
86
p.offset = pos / GRUB_DISK_SECTOR_SIZE;
87
p.index = pos % GRUB_DISK_SECTOR_SIZE;
89
if (grub_disk_read (disk, p.offset, p.index, sizeof (be), &be))
92
p.start = grub_le_to_cpu32 (be.offset);
93
p.len = grub_le_to_cpu32 (be.size);
103
/* disk->partition != NULL as 0 < delta */
104
partname = grub_partition_get_name (disk->partition);
105
grub_util_warn ("Discarding improperly nested partition (%s,%s,%s%d)",
106
disk->name, partname, p.partmap->name, p.number + 1);
107
grub_free (partname);
117
return GRUB_ERR_NONE;
121
bsdlabel_partition_map_iterate (grub_disk_t disk,
122
int (*hook) (grub_disk_t disk,
123
const grub_partition_t partition))
126
if (disk->partition && grub_strcmp (disk->partition->partmap->name, "msdos")
127
== 0 && disk->partition->msdostype == GRUB_PC_PARTITION_TYPE_FREEBSD)
128
return iterate_real (disk, GRUB_PC_PARTITION_BSD_LABEL_SECTOR, 1,
129
&grub_bsdlabel_partition_map, hook);
132
&& (grub_strcmp (disk->partition->partmap->name, "msdos") == 0
133
|| disk->partition->partmap == &grub_bsdlabel_partition_map
134
|| disk->partition->partmap == &grub_netbsdlabel_partition_map
135
|| disk->partition->partmap == &grub_openbsdlabel_partition_map))
136
return grub_error (GRUB_ERR_BAD_PART_TABLE, "no embedding supported");
138
return iterate_real (disk, GRUB_PC_PARTITION_BSD_LABEL_SECTOR, 0,
139
&grub_bsdlabel_partition_map, hook);
142
/* This is a total breakage. Even when net-/openbsd label is inside partition
143
it actually describes the whole disk.
146
netopenbsdlabel_partition_map_iterate (grub_disk_t disk, grub_uint8_t type,
147
struct grub_partition_map *pmap,
148
int (*hook) (grub_disk_t disk,
149
const grub_partition_t partition))
153
auto int check_msdos (grub_disk_t dsk,
154
const grub_partition_t partition);
156
int check_msdos (grub_disk_t dsk,
157
const grub_partition_t partition)
161
if (partition->msdostype != type)
164
err = iterate_real (dsk, partition->start
165
+ GRUB_PC_PARTITION_BSD_LABEL_SECTOR, 0, pmap, hook);
166
if (err == GRUB_ERR_NONE)
171
if (err == GRUB_ERR_BAD_PART_TABLE)
173
grub_errno = GRUB_ERR_NONE;
180
if (disk->partition && grub_strcmp (disk->partition->partmap->name, "msdos")
182
return grub_error (GRUB_ERR_BAD_PART_TABLE, "no embedding supported");
186
err = grub_partition_msdos_iterate (disk, check_msdos);
191
return grub_error (GRUB_ERR_BAD_PART_TABLE, "no bsdlabel found");
193
return GRUB_ERR_NONE;
197
netbsdlabel_partition_map_iterate (grub_disk_t disk,
198
int (*hook) (grub_disk_t disk,
199
const grub_partition_t partition))
201
return netopenbsdlabel_partition_map_iterate (disk,
202
GRUB_PC_PARTITION_TYPE_NETBSD,
203
&grub_netbsdlabel_partition_map,
208
openbsdlabel_partition_map_iterate (grub_disk_t disk,
209
int (*hook) (grub_disk_t disk,
210
const grub_partition_t partition))
212
return netopenbsdlabel_partition_map_iterate (disk,
213
GRUB_PC_PARTITION_TYPE_OPENBSD,
214
&grub_openbsdlabel_partition_map,
219
static struct grub_partition_map grub_bsdlabel_partition_map =
222
.iterate = bsdlabel_partition_map_iterate,
225
static struct grub_partition_map grub_openbsdlabel_partition_map =
228
.iterate = openbsdlabel_partition_map_iterate,
231
static struct grub_partition_map grub_netbsdlabel_partition_map =
234
.iterate = netbsdlabel_partition_map_iterate,
239
GRUB_MOD_INIT(part_bsd)
241
grub_partition_map_register (&grub_bsdlabel_partition_map);
242
grub_partition_map_register (&grub_netbsdlabel_partition_map);
243
grub_partition_map_register (&grub_openbsdlabel_partition_map);
246
GRUB_MOD_FINI(part_bsd)
248
grub_partition_map_unregister (&grub_bsdlabel_partition_map);
249
grub_partition_map_unregister (&grub_netbsdlabel_partition_map);
250
grub_partition_map_unregister (&grub_openbsdlabel_partition_map);