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 grub_partition_iterate_hook_t hook
, void *hook_data
)
46 struct grub_partition_bsd_disk_label label
;
47 struct grub_partition p
;
48 grub_disk_addr_t delta
= 0;
51 /* Read the BSD label. */
52 if (grub_disk_read (disk
, sector
, 0, sizeof (label
), &label
))
55 /* Check if it is valid. */
56 if (label
.magic
!= grub_cpu_to_le32_compile_time (GRUB_PC_PARTITION_BSD_LABEL_MAGIC
))
57 return grub_error (GRUB_ERR_BAD_PART_TABLE
, "no signature");
59 /* A kludge to determine a base of be.offset. */
60 if (GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION
61 < grub_cpu_to_le16 (label
.num_partitions
) && freebsd
)
63 struct grub_partition_bsd_entry whole_disk_be
;
65 pos
= sizeof (label
) + sector
* GRUB_DISK_SECTOR_SIZE
66 + sizeof (struct grub_partition_bsd_entry
)
67 * GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION
;
69 if (grub_disk_read (disk
, pos
/ GRUB_DISK_SECTOR_SIZE
,
70 pos
% GRUB_DISK_SECTOR_SIZE
, sizeof (whole_disk_be
),
74 delta
= grub_le_to_cpu32 (whole_disk_be
.offset
);
77 pos
= sizeof (label
) + sector
* GRUB_DISK_SECTOR_SIZE
;
80 p
.number
< grub_cpu_to_le16 (label
.num_partitions
);
81 p
.number
++, pos
+= sizeof (struct grub_partition_bsd_entry
))
83 struct grub_partition_bsd_entry be
;
85 if (p
.number
== GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION
)
88 p
.offset
= pos
/ GRUB_DISK_SECTOR_SIZE
;
89 p
.index
= pos
% GRUB_DISK_SECTOR_SIZE
;
91 if (grub_disk_read (disk
, p
.offset
, p
.index
, sizeof (be
), &be
))
94 p
.start
= grub_le_to_cpu32 (be
.offset
);
95 p
.len
= grub_le_to_cpu32 (be
.size
);
105 /* disk->partition != NULL as 0 < delta */
106 partname
= disk
->partition
? grub_partition_get_name (disk
->partition
)
108 grub_util_warn (_("Discarding improperly nested partition (%s,%s,%s%d)"),
109 disk
->name
, partname
? : "", p
.partmap
->name
,
111 grub_free (partname
);
118 if (hook (disk
, &p
, hook_data
))
121 return GRUB_ERR_NONE
;
125 bsdlabel_partition_map_iterate (grub_disk_t disk
,
126 grub_partition_iterate_hook_t hook
,
130 if (disk
->partition
&& grub_strcmp (disk
->partition
->partmap
->name
, "msdos")
131 == 0 && disk
->partition
->msdostype
== GRUB_PC_PARTITION_TYPE_FREEBSD
)
132 return iterate_real (disk
, GRUB_PC_PARTITION_BSD_LABEL_SECTOR
, 1,
133 &grub_bsdlabel_partition_map
, hook
, hook_data
);
136 && (grub_strcmp (disk
->partition
->partmap
->name
, "msdos") == 0
137 || disk
->partition
->partmap
== &grub_bsdlabel_partition_map
138 || disk
->partition
->partmap
== &grub_netbsdlabel_partition_map
139 || disk
->partition
->partmap
== &grub_openbsdlabel_partition_map
))
140 return grub_error (GRUB_ERR_BAD_PART_TABLE
, "no embedding supported");
142 return iterate_real (disk
, GRUB_PC_PARTITION_BSD_LABEL_SECTOR
, 0,
143 &grub_bsdlabel_partition_map
, hook
, hook_data
);
146 /* Context for netopenbsdlabel_partition_map_iterate. */
147 struct netopenbsdlabel_ctx
150 struct grub_partition_map
*pmap
;
151 grub_partition_iterate_hook_t hook
;
156 /* Helper for netopenbsdlabel_partition_map_iterate. */
158 check_msdos (grub_disk_t dsk
, const grub_partition_t partition
, void *data
)
160 struct netopenbsdlabel_ctx
*ctx
= data
;
163 if (partition
->msdostype
!= ctx
->type
)
166 err
= iterate_real (dsk
, partition
->start
167 + GRUB_PC_PARTITION_BSD_LABEL_SECTOR
, 0, ctx
->pmap
,
168 ctx
->hook
, ctx
->hook_data
);
169 if (err
== GRUB_ERR_NONE
)
174 if (err
== GRUB_ERR_BAD_PART_TABLE
)
176 grub_errno
= GRUB_ERR_NONE
;
183 /* This is a total breakage. Even when net-/openbsd label is inside partition
184 it actually describes the whole disk.
187 netopenbsdlabel_partition_map_iterate (grub_disk_t disk
, grub_uint8_t type
,
188 struct grub_partition_map
*pmap
,
189 grub_partition_iterate_hook_t hook
,
192 if (disk
->partition
&& grub_strcmp (disk
->partition
->partmap
->name
, "msdos")
194 return grub_error (GRUB_ERR_BAD_PART_TABLE
, "no embedding supported");
197 struct netopenbsdlabel_ctx ctx
= {
201 .hook_data
= hook_data
,
206 err
= grub_partition_msdos_iterate (disk
, check_msdos
, &ctx
);
211 return grub_error (GRUB_ERR_BAD_PART_TABLE
, "no bsdlabel found");
213 return GRUB_ERR_NONE
;
217 netbsdlabel_partition_map_iterate (grub_disk_t disk
,
218 grub_partition_iterate_hook_t hook
,
221 return netopenbsdlabel_partition_map_iterate (disk
,
222 GRUB_PC_PARTITION_TYPE_NETBSD
,
223 &grub_netbsdlabel_partition_map
,
228 openbsdlabel_partition_map_iterate (grub_disk_t disk
,
229 grub_partition_iterate_hook_t hook
,
232 return netopenbsdlabel_partition_map_iterate (disk
,
233 GRUB_PC_PARTITION_TYPE_OPENBSD
,
234 &grub_openbsdlabel_partition_map
,
239 static struct grub_partition_map grub_bsdlabel_partition_map
=
242 .iterate
= bsdlabel_partition_map_iterate
,
245 static struct grub_partition_map grub_openbsdlabel_partition_map
=
248 .iterate
= openbsdlabel_partition_map_iterate
,
251 static struct grub_partition_map grub_netbsdlabel_partition_map
=
254 .iterate
= netbsdlabel_partition_map_iterate
,
259 GRUB_MOD_INIT(part_bsd
)
261 grub_partition_map_register (&grub_bsdlabel_partition_map
);
262 grub_partition_map_register (&grub_netbsdlabel_partition_map
);
263 grub_partition_map_register (&grub_openbsdlabel_partition_map
);
266 GRUB_MOD_FINI(part_bsd
)
268 grub_partition_map_unregister (&grub_bsdlabel_partition_map
);
269 grub_partition_map_unregister (&grub_netbsdlabel_partition_map
);
270 grub_partition_map_unregister (&grub_openbsdlabel_partition_map
);