blob: d83f2cf024764ddb28eb4d77e6b536014200015c [file] [log] [blame]
bigbiff7b4c7a62015-01-01 19:44:14 -05001/*
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
29static 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
41 if (blkid_partitions_need_typeonly(pr))
42 /* caller does not ask for details about partitions */
43 return rc;
44
45 data = blkid_probe_get_sector(pr, BLKID_MAG_SECTOR(mag));
46 if (!data) {
47 if (errno)
48 rc = -errno;
49 goto nothing;
50 }
51
52 l = (struct bsd_disklabel *) data + BLKID_MAG_LASTOFFSET(mag);
53
54 ls = blkid_probe_get_partlist(pr);
55 if (!ls)
56 goto nothing;
57
58 /* try to determine the real type of BSD system according to
59 * (parental) primary partition */
60 parent = blkid_partlist_get_parent(ls);
61 if (parent) {
62 switch(blkid_partition_get_type(parent)) {
63 case MBR_FREEBSD_PARTITION:
64 name = "freebsd";
65 break;
66 case MBR_NETBSD_PARTITION:
67 name = "netbsd";
68 break;
69 case MBR_OPENBSD_PARTITION:
70 name = "openbsd";
71 break;
72 default:
73 DBG(LOWPROBE, ul_debug(
74 "WARNING: BSD label detected on unknown (0x%x) "
75 "primary partition",
76 blkid_partition_get_type(parent)));
77 break;
78 }
79 }
80
81 tab = blkid_partlist_new_parttable(ls, name, BLKID_MAG_OFFSET(mag));
82 if (!tab) {
83 rc = -ENOMEM;
84 goto nothing;
85 }
86
87 if (le16_to_cpu(l->d_npartitions) < BSD_MAXPARTITIONS)
88 nparts = le16_to_cpu(l->d_npartitions);
89
90 else if (le16_to_cpu(l->d_npartitions) > BSD_MAXPARTITIONS)
91 DBG(LOWPROBE, ul_debug(
92 "WARNING: ignore %d more BSD partitions",
93 le16_to_cpu(l->d_npartitions) - BSD_MAXPARTITIONS));
94
95 for (i = 0, p = l->d_partitions; i < nparts; i++, p++) {
96 blkid_partition par;
97 uint32_t start, size;
98
99 /* TODO: in fdisk-mode returns all non-zero (p_size) partitions */
100 if (p->p_fstype == BSD_FS_UNUSED)
101 continue;
102
103 start = le32_to_cpu(p->p_offset);
104 size = le32_to_cpu(p->p_size);
105
106 if (parent && blkid_partition_get_start(parent) == start
107 && blkid_partition_get_size(parent) == size) {
108 DBG(LOWPROBE, ul_debug(
109 "WARNING: BSD partition (%d) same like parent, "
110 "ignore", i));
111 continue;
112 }
113 if (parent && !blkid_is_nested_dimension(parent, start, size)) {
114 DBG(LOWPROBE, ul_debug(
115 "WARNING: BSD partition (%d) overflow "
116 "detected, ignore", i));
117 continue;
118 }
119
120 par = blkid_partlist_add_partition(ls, tab, start, size);
121 if (!par) {
122 rc = -ENOMEM;
123 goto nothing;
124 }
125
126 blkid_partition_set_type(par, p->p_fstype);
127 }
128
129 return BLKID_PROBE_OK;
130
131nothing:
132 return rc;
133}
134
135
136/*
137 * All BSD variants use the same magic string (little-endian),
138 * and the same disklabel.
139 *
140 * The difference between {Free,Open,...}BSD is in the (parental)
141 * primary partition type.
142 *
143 * See also: http://en.wikipedia.org/wiki/BSD_disklabel
144 *
145 * The location of BSD disk label is architecture specific and in defined by
146 * LABELSECTOR and LABELOFFSET macros in the disklabel.h file. The location
147 * also depends on BSD variant, FreeBSD uses only one location, NetBSD and
148 * OpenBSD are more creative...
149 *
150 * The basic overview:
151 *
152 * arch | LABELSECTOR | LABELOFFSET
153 * ------------------------+-------------+------------
154 * amd64 arm hppa hppa64 | |
155 * i386, macppc, mvmeppc | 1 | 0
156 * sgi, aviion, sh, socppc | |
157 * ------------------------+-------------+------------
158 * alpha luna88k mac68k | 0 | 64
159 * sparc(OpenBSD) vax | |
160 * ------------------------+-------------+------------
161 * sparc64 sparc(NetBSD) | 0 | 128
162 * ------------------------+-------------+------------
163 *
164 * ...and more (see http://fxr.watson.org/fxr/ident?v=NETBSD;i=LABELSECTOR)
165 *
166 */
167const struct blkid_idinfo bsd_pt_idinfo =
168{
169 .name = "bsd",
170 .probefunc = probe_bsd_pt,
171 .magics =
172 {
173 { .magic = "\x57\x45\x56\x82", .len = 4, .sboff = 512 },
174 { .magic = "\x57\x45\x56\x82", .len = 4, .sboff = 64 },
175 { .magic = "\x57\x45\x56\x82", .len = 4, .sboff = 128 },
176 { NULL }
177 }
178};
179