Coverage Report

Created: 2026-08-30 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/util-linux/libblkid/src/partitions/bsd.c
Line
Count
Source
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
0
#define BLKID_MAG_SECTOR(_mag)  (((_mag)->kboff / 2)  + ((_mag)->sboff >> 9))
21
22
/* Returns 'blkid_idmag' in bytes */
23
0
#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
0
     (BLKID_MAG_OFFSET(_mag) - (BLKID_MAG_SECTOR(_mag) << 9))
28
29
static uint16_t bsd_checksum(const struct bsd_disklabel *l)
30
0
{
31
0
  uint16_t v, csum = 0;
32
0
  const char *end = (const char *) (l + 1);
33
34
0
  for (const char *c = (const char *) l; c < end; c += sizeof(uint16_t)) {
35
0
    memcpy(&v, c, sizeof(v));
36
0
    csum ^= v;
37
0
  }
38
0
  return csum ^ le16_to_cpu(l->d_checksum);
39
0
}
40
41
static int probe_bsd_pt(blkid_probe pr, const struct blkid_idmag *mag)
42
453
{
43
453
  struct bsd_disklabel *l;
44
453
  struct bsd_partition *p;
45
453
  const char *name = "bsd" ;
46
453
  blkid_parttable tab = NULL;
47
453
  blkid_partition parent;
48
453
  blkid_partlist ls;
49
453
  int i, nparts = BSD_MAXPARTITIONS;
50
453
  const unsigned char *data;
51
453
  int rc = BLKID_PROBE_NONE;
52
453
  uint64_t abs_offset = 0;
53
54
453
  if (blkid_partitions_need_typeonly(pr))
55
    /* caller does not ask for details about partitions */
56
453
    return rc;
57
58
0
  data = blkid_probe_get_buffer(pr,
59
0
      (uint64_t) BLKID_MAG_SECTOR(mag) << 9,
60
0
      BLKID_MAG_LASTOFFSET(mag) + sizeof(struct bsd_disklabel));
61
0
  if (!data) {
62
0
    if (errno)
63
0
      rc = -errno;
64
0
    goto nothing;
65
0
  }
66
67
0
  l = (struct bsd_disklabel *) (data + BLKID_MAG_LASTOFFSET(mag));
68
69
0
  if (!blkid_probe_verify_csum(pr, bsd_checksum(l), le16_to_cpu(l->d_checksum))) {
70
0
    rc = BLKID_PROBE_NONE;
71
0
    goto nothing;
72
0
  }
73
74
0
  ls = blkid_probe_get_partlist(pr);
75
0
  if (!ls)
76
0
    goto nothing;
77
78
  /* try to determine the real type of BSD system according to
79
   * (parental) primary partition */
80
0
  parent = blkid_partlist_get_parent(ls);
81
0
  if (parent) {
82
0
    switch(blkid_partition_get_type(parent)) {
83
0
    case MBR_FREEBSD_PARTITION:
84
0
      name = "freebsd";
85
0
      abs_offset = blkid_partition_get_start(parent);
86
0
      break;
87
0
    case MBR_NETBSD_PARTITION:
88
0
      name = "netbsd";
89
0
      break;
90
0
    case MBR_OPENBSD_PARTITION:
91
0
      name = "openbsd";
92
0
      break;
93
0
    default:
94
0
      DBG(LOWPROBE, ul_debug(
95
0
        "WARNING: BSD label detected on unknown (0x%x) "
96
0
        "primary partition",
97
0
        blkid_partition_get_type(parent)));
98
0
      break;
99
0
    }
100
0
  }
101
102
0
  tab = blkid_partlist_new_parttable(ls, name, BLKID_MAG_OFFSET(mag));
103
0
  if (!tab) {
104
0
    rc = -ENOMEM;
105
0
    goto nothing;
106
0
  }
107
108
0
  if (le16_to_cpu(l->d_npartitions) < BSD_MAXPARTITIONS)
109
0
    nparts = le16_to_cpu(l->d_npartitions);
110
111
0
  else if (le16_to_cpu(l->d_npartitions) > BSD_MAXPARTITIONS)
112
0
    DBG(LOWPROBE, ul_debug(
113
0
      "WARNING: ignore %d more BSD partitions",
114
0
      le16_to_cpu(l->d_npartitions) - BSD_MAXPARTITIONS));
115
116
0
  for (i = 0, p = l->d_partitions; i < nparts; i++, p++) {
117
0
    blkid_partition par;
118
0
    uint64_t start, size;
119
120
0
    if (p->p_fstype == BSD_FS_UNUSED)
121
0
      continue;
122
123
0
    start = le32_to_cpu(p->p_offset);
124
0
    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
0
    if (abs_offset && nparts >= 3
131
0
        && le32_to_cpu(l->d_partitions[2].p_offset) == 0)
132
0
      start += abs_offset;
133
134
0
    if (parent && (uint64_t) blkid_partition_get_start(parent) == start
135
0
         && (uint64_t) blkid_partition_get_size(parent) == size) {
136
0
      DBG(LOWPROBE, ul_debug(
137
0
        "WARNING: BSD partition (%d) same like parent, "
138
0
        "ignore", i));
139
0
      continue;
140
0
    }
141
0
    if (parent && !blkid_is_nested_dimension(parent, start, size)) {
142
0
      DBG(LOWPROBE, ul_debug(
143
0
        "WARNING: BSD partition (%d) overflow "
144
0
        "detected, ignore", i));
145
0
      continue;
146
0
    }
147
148
0
    par = blkid_partlist_add_partition(ls, tab, start, size);
149
0
    if (!par) {
150
0
      rc = -ENOMEM;
151
0
      goto nothing;
152
0
    }
153
154
0
    blkid_partition_set_type(par, p->p_fstype);
155
0
  }
156
157
0
  return BLKID_PROBE_OK;
158
159
0
nothing:
160
0
  return rc;
161
0
}
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