1 /*
2 * BSD/OSF partition parsing code
3 *
4 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 *
9 * Inspired by fdisk, partx, Linux kernel, libparted and openbsd header files.
10 */
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdint.h>
15
16 #include "partitions.h"
17 #include "pt-bsd.h"
18
19 /* Returns 'blkid_idmag' in 512-sectors */
20 #define BLKID_MAG_SECTOR(_mag) (((_mag)->kboff / 2) + ((_mag)->sboff >> 9))
21
22 /* Returns 'blkid_idmag' in bytes */
23 #define BLKID_MAG_OFFSET(_mag) ((_mag)->kboff << 10) + ((_mag)->sboff)
24
25 /* Returns 'blkid_idmag' offset in bytes within the last sector */
26 #define BLKID_MAG_LASTOFFSET(_mag) \
27 (BLKID_MAG_OFFSET(_mag) - (BLKID_MAG_SECTOR(_mag) << 9))
28
probe_bsd_pt(blkid_probe pr,const struct blkid_idmag * mag)29 static int probe_bsd_pt(blkid_probe pr, const struct blkid_idmag *mag)
30 {
31 struct bsd_disklabel *l;
32 struct bsd_partition *p;
33 const char *name = "bsd" ;
34 blkid_parttable tab = NULL;
35 blkid_partition parent;
36 blkid_partlist ls;
37 int i, nparts = BSD_MAXPARTITIONS;
38 unsigned char *data;
39 int rc = BLKID_PROBE_NONE;
40 uint32_t abs_offset = 0;
41
42 if (blkid_partitions_need_typeonly(pr))
43 /* caller does not ask for details about partitions */
44 return rc;
45
46 data = blkid_probe_get_sector(pr, BLKID_MAG_SECTOR(mag));
47 if (!data) {
48 if (errno)
49 rc = -errno;
50 goto nothing;
51 }
52
53 l = (struct bsd_disklabel *) data + BLKID_MAG_LASTOFFSET(mag);
54
55 ls = blkid_probe_get_partlist(pr);
56 if (!ls)
57 goto nothing;
58
59 /* try to determine the real type of BSD system according to
60 * (parental) primary partition */
61 parent = blkid_partlist_get_parent(ls);
62 if (parent) {
63 switch(blkid_partition_get_type(parent)) {
64 case MBR_FREEBSD_PARTITION:
65 name = "freebsd";
66 abs_offset = blkid_partition_get_start(parent);
67 break;
68 case MBR_NETBSD_PARTITION:
69 name = "netbsd";
70 break;
71 case MBR_OPENBSD_PARTITION:
72 name = "openbsd";
73 break;
74 default:
75 DBG(LOWPROBE, ul_debug(
76 "WARNING: BSD label detected on unknown (0x%x) "
77 "primary partition",
78 blkid_partition_get_type(parent)));
79 break;
80 }
81 }
82
83 tab = blkid_partlist_new_parttable(ls, name, BLKID_MAG_OFFSET(mag));
84 if (!tab) {
85 rc = -ENOMEM;
86 goto nothing;
87 }
88
89 if (le16_to_cpu(l->d_npartitions) < BSD_MAXPARTITIONS)
90 nparts = le16_to_cpu(l->d_npartitions);
91
92 else if (le16_to_cpu(l->d_npartitions) > BSD_MAXPARTITIONS)
93 DBG(LOWPROBE, ul_debug(
94 "WARNING: ignore %d more BSD partitions",
95 le16_to_cpu(l->d_npartitions) - BSD_MAXPARTITIONS));
96
97 for (i = 0, p = l->d_partitions; i < nparts; i++, p++) {
98 blkid_partition par;
99 uint32_t start, size;
100
101 if (p->p_fstype == BSD_FS_UNUSED)
102 continue;
103
104 start = le32_to_cpu(p->p_offset);
105 size = le32_to_cpu(p->p_size);
106
107 /* FreeBSD since version 10 uses relative offsets. We can use
108 * 3rd partition (special wholedisk partition) to detect this
109 * situation.
110 */
111 if (abs_offset && nparts >= 3
112 && le32_to_cpu(l->d_partitions[2].p_offset) == 0)
113 start += abs_offset;
114
115 if (parent && blkid_partition_get_start(parent) == start
116 && blkid_partition_get_size(parent) == size) {
117 DBG(LOWPROBE, ul_debug(
118 "WARNING: BSD partition (%d) same like parent, "
119 "ignore", i));
120 continue;
121 }
122 if (parent && !blkid_is_nested_dimension(parent, start, size)) {
123 DBG(LOWPROBE, ul_debug(
124 "WARNING: BSD partition (%d) overflow "
125 "detected, ignore", i));
126 continue;
127 }
128
129 par = blkid_partlist_add_partition(ls, tab, start, size);
130 if (!par) {
131 rc = -ENOMEM;
132 goto nothing;
133 }
134
135 blkid_partition_set_type(par, p->p_fstype);
136 }
137
138 return BLKID_PROBE_OK;
139
140 nothing:
141 return rc;
142 }
143
144
145 /*
146 * All BSD variants use the same magic string (little-endian),
147 * and the same disklabel.
148 *
149 * The difference between {Free,Open,...}BSD is in the (parental)
150 * primary partition type.
151 *
152 * See also: http://en.wikipedia.org/wiki/BSD_disklabel
153 *
154 * The location of BSD disk label is architecture specific and in defined by
155 * LABELSECTOR and LABELOFFSET macros in the disklabel.h file. The location
156 * also depends on BSD variant, FreeBSD uses only one location, NetBSD and
157 * OpenBSD are more creative...
158 *
159 * The basic overview:
160 *
161 * arch | LABELSECTOR | LABELOFFSET
162 * ------------------------+-------------+------------
163 * amd64 arm hppa hppa64 | |
164 * i386, macppc, mvmeppc | 1 | 0
165 * sgi, aviion, sh, socppc | |
166 * ------------------------+-------------+------------
167 * alpha luna88k mac68k | 0 | 64
168 * sparc(OpenBSD) vax | |
169 * ------------------------+-------------+------------
170 * sparc64 sparc(NetBSD) | 0 | 128
171 * ------------------------+-------------+------------
172 *
173 * ...and more (see http://fxr.watson.org/fxr/ident?v=NETBSD;i=LABELSECTOR)
174 *
175 */
176 const struct blkid_idinfo bsd_pt_idinfo =
177 {
178 .name = "bsd",
179 .probefunc = probe_bsd_pt,
180 .magics =
181 {
182 { .magic = "\x57\x45\x56\x82", .len = 4, .sboff = 512 },
183 { .magic = "\x57\x45\x56\x82", .len = 4, .sboff = 64 },
184 { .magic = "\x57\x45\x56\x82", .len = 4, .sboff = 128 },
185 { NULL }
186 }
187 };
188
189