Debian Codegraph @

libblkid/src/partitions/bsd.c

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