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>
27 static struct grub_partition_map grub_bsdlabel_partition_map
;
31 bsdlabel_partition_map_iterate (grub_disk_t disk
,
32 int (*hook
) (grub_disk_t disk
,
33 const grub_partition_t partition
))
35 struct grub_pc_partition_disk_label label
;
36 struct grub_partition p
;
37 grub_partition_t part
;
38 grub_disk_addr_t delta
= 0;
40 /* BSDLabel offsets are absolute even when it's embed inside partition. */
41 for (part
= disk
->partition
; part
; part
= part
->parent
)
42 delta
+= grub_partition_get_start (part
);
44 /* Read the BSD label. */
45 if (grub_disk_read (disk
, GRUB_PC_PARTITION_BSD_LABEL_SECTOR
,
46 0, sizeof (label
), &label
))
49 /* Check if it is valid. */
50 if (label
.magic
!= grub_cpu_to_le32 (GRUB_PC_PARTITION_BSD_LABEL_MAGIC
))
51 return grub_error (GRUB_ERR_BAD_PART_TABLE
, "no signature");
53 p
.number
< grub_cpu_to_le16 (label
.num_partitions
);
56 struct grub_pc_partition_bsd_entry
*be
57 = label
.entries
+ p
.number
;
59 p
.start
= grub_le_to_cpu32 (be
->offset
) - delta
;
60 p
.len
= grub_le_to_cpu32 (be
->size
);
61 p
.offset
= GRUB_PC_PARTITION_BSD_LABEL_SECTOR
;
62 p
.partmap
= &grub_bsdlabel_partition_map
;
65 if (be
->fs_type
!= GRUB_PC_PARTITION_BSD_TYPE_UNUSED
)
74 /* Partition map type. */
75 static struct grub_partition_map grub_bsdlabel_partition_map
=
77 .name
= "bsdlabel_partition_map",
78 .iterate
= bsdlabel_partition_map_iterate
,
81 GRUB_MOD_INIT(bsdlabel_partition_map
)
83 grub_partition_map_register (&grub_bsdlabel_partition_map
);
86 GRUB_MOD_FINI(bsdlabel_partition_map
)
88 grub_partition_map_unregister (&grub_bsdlabel_partition_map
);