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>
27 #include <grub/i18n.h>
29 GRUB_MOD_LICENSE ("GPLv3+");
32 #include <grub/emu/misc.h>
35 static struct grub_partition_map grub_bsdlabel_partition_map
;
36 static struct grub_partition_map grub_netbsdlabel_partition_map
;
37 static struct grub_partition_map grub_openbsdlabel_partition_map
;
42 iterate_real (grub_disk_t disk
, grub_disk_addr_t sector
, int freebsd
,
43 struct grub_partition_map
*pmap
,
44 int (*hook
) (grub_disk_t disk
,
45 const grub_partition_t partition
))
47 struct grub_partition_bsd_disk_label label
;
48 struct grub_partition p
;
49 grub_disk_addr_t delta
= 0;
52 /* Read the BSD label. */
53 if (grub_disk_read (disk
, sector
, 0, sizeof (label
), &label
))
56 /* Check if it is valid. */
57 if (label
.magic
!= grub_cpu_to_le32 (GRUB_PC_PARTITION_BSD_LABEL_MAGIC
))
58 return grub_error (GRUB_ERR_BAD_PART_TABLE
, "no signature");
60 /* A kludge to determine a base of be.offset. */
61 if (GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION
62 < grub_cpu_to_le16 (label
.num_partitions
) && freebsd
)
64 struct grub_partition_bsd_entry whole_disk_be
;
66 pos
= sizeof (label
) + sector
* GRUB_DISK_SECTOR_SIZE
67 + sizeof (struct grub_partition_bsd_entry
)
68 * GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION
;
70 if (grub_disk_read (disk
, pos
/ GRUB_DISK_SECTOR_SIZE
,
71 pos
% GRUB_DISK_SECTOR_SIZE
, sizeof (whole_disk_be
),
75 delta
= grub_le_to_cpu32 (whole_disk_be
.offset
);
78 pos
= sizeof (label
) + sector
* GRUB_DISK_SECTOR_SIZE
;
81 p
.number
< grub_cpu_to_le16 (label
.num_partitions
);
82 p
.number
++, pos
+= sizeof (struct grub_partition_bsd_entry
))
84 struct grub_partition_bsd_entry be
;
86 if (p
.number
== GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION
)
89 p
.offset
= pos
/ GRUB_DISK_SECTOR_SIZE
;
90 p
.index
= pos
% GRUB_DISK_SECTOR_SIZE
;
92 if (grub_disk_read (disk
, p
.offset
, p
.index
, sizeof (be
), &be
))
95 p
.start
= grub_le_to_cpu32 (be
.offset
);
96 p
.len
= grub_le_to_cpu32 (be
.size
);
106 /* disk->partition != NULL as 0 < delta */
107 partname
= disk
->partition
? grub_partition_get_name (disk
->partition
)
109 grub_util_warn (_("Discarding improperly nested partition (%s,%s,%s%d)"),
110 disk
->name
, partname
? : "", p
.partmap
->name
,
112 grub_free (partname
);
122 return GRUB_ERR_NONE
;
126 bsdlabel_partition_map_iterate (grub_disk_t disk
,
127 int (*hook
) (grub_disk_t disk
,
128 const grub_partition_t partition
))
131 if (disk
->partition
&& grub_strcmp (disk
->partition
->partmap
->name
, "msdos")
132 == 0 && disk
->partition
->msdostype
== GRUB_PC_PARTITION_TYPE_FREEBSD
)
133 return iterate_real (disk
, GRUB_PC_PARTITION_BSD_LABEL_SECTOR
, 1,
134 &grub_bsdlabel_partition_map
, hook
);
137 && (grub_strcmp (disk
->partition
->partmap
->name
, "msdos") == 0
138 || disk
->partition
->partmap
== &grub_bsdlabel_partition_map
139 || disk
->partition
->partmap
== &grub_netbsdlabel_partition_map
140 || disk
->partition
->partmap
== &grub_openbsdlabel_partition_map
))
141 return grub_error (GRUB_ERR_BAD_PART_TABLE
, "no embedding supported");
143 return iterate_real (disk
, GRUB_PC_PARTITION_BSD_LABEL_SECTOR
, 0,
144 &grub_bsdlabel_partition_map
, hook
);
147 /* This is a total breakage. Even when net-/openbsd label is inside partition
148 it actually describes the whole disk.
151 netopenbsdlabel_partition_map_iterate (grub_disk_t disk
, grub_uint8_t type
,
152 struct grub_partition_map
*pmap
,
153 int (*hook
) (grub_disk_t disk
,
154 const grub_partition_t partition
))
158 auto int check_msdos (grub_disk_t dsk
,
159 const grub_partition_t partition
);
161 int check_msdos (grub_disk_t dsk
,
162 const grub_partition_t partition
)
166 if (partition
->msdostype
!= type
)
169 err
= iterate_real (dsk
, partition
->start
170 + GRUB_PC_PARTITION_BSD_LABEL_SECTOR
, 0, pmap
, hook
);
171 if (err
== GRUB_ERR_NONE
)
176 if (err
== GRUB_ERR_BAD_PART_TABLE
)
178 grub_errno
= GRUB_ERR_NONE
;
185 if (disk
->partition
&& grub_strcmp (disk
->partition
->partmap
->name
, "msdos")
187 return grub_error (GRUB_ERR_BAD_PART_TABLE
, "no embedding supported");
191 err
= grub_partition_msdos_iterate (disk
, check_msdos
);
196 return grub_error (GRUB_ERR_BAD_PART_TABLE
, "no bsdlabel found");
198 return GRUB_ERR_NONE
;
202 netbsdlabel_partition_map_iterate (grub_disk_t disk
,
203 int (*hook
) (grub_disk_t disk
,
204 const grub_partition_t partition
))
206 return netopenbsdlabel_partition_map_iterate (disk
,
207 GRUB_PC_PARTITION_TYPE_NETBSD
,
208 &grub_netbsdlabel_partition_map
,
213 openbsdlabel_partition_map_iterate (grub_disk_t disk
,
214 int (*hook
) (grub_disk_t disk
,
215 const grub_partition_t partition
))
217 return netopenbsdlabel_partition_map_iterate (disk
,
218 GRUB_PC_PARTITION_TYPE_OPENBSD
,
219 &grub_openbsdlabel_partition_map
,
224 static struct grub_partition_map grub_bsdlabel_partition_map
=
227 .iterate
= bsdlabel_partition_map_iterate
,
230 static struct grub_partition_map grub_openbsdlabel_partition_map
=
233 .iterate
= openbsdlabel_partition_map_iterate
,
236 static struct grub_partition_map grub_netbsdlabel_partition_map
=
239 .iterate
= netbsdlabel_partition_map_iterate
,
244 GRUB_MOD_INIT(part_bsd
)
246 grub_partition_map_register (&grub_bsdlabel_partition_map
);
247 grub_partition_map_register (&grub_netbsdlabel_partition_map
);
248 grub_partition_map_register (&grub_openbsdlabel_partition_map
);
251 GRUB_MOD_FINI(part_bsd
)
253 grub_partition_map_unregister (&grub_bsdlabel_partition_map
);
254 grub_partition_map_unregister (&grub_netbsdlabel_partition_map
);
255 grub_partition_map_unregister (&grub_openbsdlabel_partition_map
);