2
* BSD/OSF partition parsing code
4
* Copyright (C) 2009 Karel Zak <kzak@redhat.com>
6
* This file may be redistributed under the terms of the
7
* GNU Lesser General Public License.
9
* Inspired by fdisk, partx, Linux kernel, libparted and openbsd header files.
16
#include "partitions.h"
18
#define BSD_MAXPARTITIONS 16
19
#define BSD_FS_UNUSED 0
21
struct bsd_disklabel {
22
uint32_t d_magic; /* the magic number */
23
int16_t d_type; /* drive type */
24
int16_t d_subtype; /* controller/d_type specific */
25
char d_typename[16]; /* type name, e.g. "eagle" */
26
char d_packname[16]; /* pack identifier */
29
uint32_t d_secsize; /* # of bytes per sector */
30
uint32_t d_nsectors; /* # of data sectors per track */
31
uint32_t d_ntracks; /* # of tracks per cylinder */
32
uint32_t d_ncylinders; /* # of data cylinders per unit */
33
uint32_t d_secpercyl; /* # of data sectors per cylinder */
34
uint32_t d_secperunit; /* # of data sectors per unit */
37
* Spares (bad sector replacements) below
38
* are not counted in d_nsectors or d_secpercyl.
39
* Spare sectors are assumed to be physical sectors
40
* which occupy space at the end of each track and/or cylinder.
42
uint16_t d_sparespertrack; /* # of spare sectors per track */
43
uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
46
* Alternate cylinders include maintenance, replacement,
47
* configuration description areas, etc.
49
uint32_t d_acylinders; /* # of alt. cylinders per unit */
51
/* hardware characteristics: */
53
* d_interleave, d_trackskew and d_cylskew describe perturbations
54
* in the media format used to compensate for a slow controller.
55
* Interleave is physical sector interleave, set up by the formatter
56
* or controller when formatting. When interleaving is in use,
57
* logically adjacent sectors are not physically contiguous,
58
* but instead are separated by some number of sectors.
59
* It is specified as the ratio of physical sectors traversed
60
* per logical sector. Thus an interleave of 1:1 implies contiguous
61
* layout, while 2:1 implies that logical sector 0 is separated
62
* by one sector from logical sector 1.
63
* d_trackskew is the offset of sector 0 on track N
64
* relative to sector 0 on track N-1 on the same cylinder.
65
* Finally, d_cylskew is the offset of sector 0 on cylinder N
66
* relative to sector 0 on cylinder N-1.
68
uint16_t d_rpm; /* rotational speed */
69
uint16_t d_interleave; /* hardware sector interleave */
70
uint16_t d_trackskew; /* sector 0 skew, per track */
71
uint16_t d_cylskew; /* sector 0 skew, per cylinder */
72
uint32_t d_headswitch; /* head switch time, usec */
73
uint32_t d_trkseek; /* track-to-track seek, usec */
74
uint32_t d_flags; /* generic flags */
75
uint32_t d_drivedata[5]; /* drive-type specific information */
76
uint32_t d_spare[5]; /* reserved for future use */
77
uint32_t d_magic2; /* the magic number (again) */
78
uint16_t d_checksum; /* xor of data incl. partitions */
80
/* filesystem and partition information: */
81
uint16_t d_npartitions; /* number of partitions in following */
82
uint32_t d_bbsize; /* size of boot area at sn0, bytes */
83
uint32_t d_sbsize; /* max size of fs superblock, bytes */
85
struct bsd_partition { /* the partition table */
86
uint32_t p_size; /* number of sectors in partition */
87
uint32_t p_offset; /* starting sector */
88
uint32_t p_fsize; /* filesystem basic fragment size */
89
uint8_t p_fstype; /* filesystem type, see below */
90
uint8_t p_frag; /* filesystem fragments per block */
91
uint16_t p_cpg; /* filesystem cylinders per group */
92
} __attribute__((packed)) d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
93
} __attribute__((packed));
96
/* Returns 'blkid_idmag' in 512-sectors */
97
#define BLKID_MAG_SECTOR(_mag) (((_mag)->kboff * 2) + ((_mag)->sboff >> 9))
99
/* Returns 'blkid_idmag' in bytes */
100
#define BLKID_MAG_OFFSET(_mag) ((_mag)->kboff >> 10) + ((_mag)->sboff)
102
/* Returns 'blkid_idmag' offset in bytes within the last sector */
103
#define BLKID_MAG_LASTOFFSET(_mag) \
104
(BLKID_MAG_OFFSET(_mag) - (BLKID_MAG_SECTOR(_mag) << 9))
106
static int probe_bsd_pt(blkid_probe pr, const struct blkid_idmag *mag)
108
struct bsd_disklabel *l;
109
struct bsd_partition *p;
110
const char *name = "bsd" ;
111
blkid_parttable tab = NULL;
112
blkid_partition parent;
114
int i, nparts = BSD_MAXPARTITIONS;
117
if (blkid_partitions_need_typeonly(pr))
118
/* caller does not ask for details about partitions */
121
data = blkid_probe_get_sector(pr, BLKID_MAG_SECTOR(mag));
125
l = (struct bsd_disklabel *) data + BLKID_MAG_LASTOFFSET(mag);
127
ls = blkid_probe_get_partlist(pr);
131
/* try to determine the real type of BSD system according to
132
* (parental) primary partition */
133
parent = blkid_partlist_get_parent(ls);
135
switch(blkid_partition_get_type(parent)) {
136
case BLKID_FREEBSD_PARTITION:
139
case BLKID_NETBSD_PARTITION:
142
case BLKID_OPENBSD_PARTITION:
146
DBG(DEBUG_LOWPROBE, printf(
147
"WARNING: BSD label detected on unknown (0x%x) "
148
"primary partition\n",
149
blkid_partition_get_type(parent)));
154
tab = blkid_partlist_new_parttable(ls, name, BLKID_MAG_OFFSET(mag));
158
if (le16_to_cpu(l->d_npartitions) < BSD_MAXPARTITIONS)
159
nparts = le16_to_cpu(l->d_npartitions);
161
else if (le16_to_cpu(l->d_npartitions) > BSD_MAXPARTITIONS)
162
DBG(DEBUG_LOWPROBE, printf(
163
"WARNING: ignore %d more BSD partitions\n",
164
le16_to_cpu(l->d_npartitions) - BSD_MAXPARTITIONS));
166
for (i = 0, p = l->d_partitions; i < nparts; i++, p++) {
168
uint32_t start, size;
170
/* TODO: in fdisk-mode returns all non-zero (p_size) partitions */
171
if (p->p_fstype == BSD_FS_UNUSED)
174
start = le32_to_cpu(p->p_offset);
175
size = le32_to_cpu(p->p_size);
177
if (parent && !blkid_is_nested_dimension(parent, start, size)) {
178
DBG(DEBUG_LOWPROBE, printf(
179
"WARNING: BSD partition (%d) overflow "
180
"detected, ignore\n", i));
184
par = blkid_partlist_add_partition(ls, tab, start, size);
188
blkid_partition_set_type(par, p->p_fstype);
201
* All BSD variants use the same magic string (little-endian),
202
* and the same disklabel.
204
* The difference between {Free,Open,...}BSD is in the (parental)
205
* primary partition type.
207
* See also: http://en.wikipedia.org/wiki/BSD_disklabel
209
* The location of BSD disk label is architecture specific and in defined by
210
* LABELSECTOR and LABELOFFSET macros in the disklabel.h file. The location
211
* also depends on BSD variant, FreeBSD uses only one location, NetBSD and
212
* OpenBSD are more creative...
214
* The basic overview:
216
* arch | LABELSECTOR | LABELOFFSET
217
* ------------------------+-------------+------------
218
* amd64 arm hppa hppa64 | |
219
* i386, macppc, mvmeppc | 1 | 0
220
* sgi, aviion, sh, socppc | |
221
* ------------------------+-------------+------------
222
* alpha luna88k mac68k | 0 | 64
223
* sparc(OpenBSD) vax | |
224
* ------------------------+-------------+------------
225
* spark64 sparc(NetBSD) | 0 | 128
226
* ------------------------+-------------+------------
228
* ...and more (see http://fxr.watson.org/fxr/ident?v=NETBSD;i=LABELSECTOR)
231
const struct blkid_idinfo bsd_pt_idinfo =
234
.probefunc = probe_bsd_pt,
237
{ .magic = "\x57\x45\x56\x82", .len = 4, .sboff = 512 },
238
{ .magic = "\x57\x45\x56\x82", .len = 4, .sboff = 64 },
239
{ .magic = "\x57\x45\x56\x82", .len = 4, .sboff = 128 },