| 1 |
/* |
| 2 |
* Copyright (C) 2010 Jeroen Oortwijn <[email protected]> |
| 3 |
* |
| 4 |
* Partly based on the Haiku BFS driver by |
| 5 |
* Axel Dörfler <[email protected]> |
| 6 |
* |
| 7 |
* Also inspired by the Linux BeFS driver by |
| 8 |
* Will Dyson <[email protected]>, et al. |
| 9 |
* |
| 10 |
* This file may be redistributed under the terms of the |
| 11 |
* GNU Lesser General Public License. |
| 12 |
*/ |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <unistd.h> |
| 16 |
#include <string.h> |
| 17 |
#include <inttypes.h> |
| 18 |
|
| 19 |
#include "superblocks.h" |
| 20 |
|
| 21 |
#define B_OS_NAME_LENGTH 0x20 |
| 22 |
#define SUPER_BLOCK_MAGIC1 0x42465331 /* BFS1 */ |
| 23 |
#define SUPER_BLOCK_MAGIC2 0xdd121031 |
| 24 |
#define SUPER_BLOCK_MAGIC3 0x15b6830e |
| 25 |
#define SUPER_BLOCK_FS_ENDIAN 0x42494745 /* BIGE */ |
| 26 |
#define INODE_MAGIC1 0x3bbe0ad9 |
| 27 |
#define BPLUSTREE_MAGIC 0x69f6c2e8 |
| 28 |
#define BPLUSTREE_NULL -1LL |
| 29 |
#define NUM_DIRECT_BLOCKS 12 |
| 30 |
#define B_UINT64_TYPE 0x554c4c47 /* ULLG */ |
| 31 |
#define KEY_NAME "be:volume_id" |
| 32 |
#define KEY_SIZE 8 |
| 33 |
|
| 34 |
#define FS16_TO_CPU(value, fs_is_le) (fs_is_le ? le16_to_cpu(value) \ |
| 35 |
: be16_to_cpu(value)) |
| 36 |
#define FS32_TO_CPU(value, fs_is_le) (fs_is_le ? le32_to_cpu(value) \ |
| 37 |
: be32_to_cpu(value)) |
| 38 |
#define FS64_TO_CPU(value, fs_is_le) (fs_is_le ? le64_to_cpu(value) \ |
| 39 |
: be64_to_cpu(value)) |
| 40 |
|
| 41 |
typedef struct block_run { |
| 42 |
int32_t allocation_group; |
| 43 |
uint16_t start; |
| 44 |
uint16_t len; |
| 45 |
} __attribute__((packed)) block_run, inode_addr; |
| 46 |
|
| 47 |
struct befs_super_block { |
| 48 |
char name[B_OS_NAME_LENGTH]; |
| 49 |
int32_t magic1; |
| 50 |
int32_t fs_byte_order; |
| 51 |
uint32_t block_size; |
| 52 |
uint32_t block_shift; |
| 53 |
int64_t num_blocks; |
| 54 |
int64_t used_blocks; |
| 55 |
int32_t inode_size; |
| 56 |
int32_t magic2; |
| 57 |
int32_t blocks_per_ag; |
| 58 |
int32_t ag_shift; |
| 59 |
int32_t num_ags; |
| 60 |
int32_t flags; |
| 61 |
block_run log_blocks; |
| 62 |
int64_t log_start; |
| 63 |
int64_t log_end; |
| 64 |
int32_t magic3; |
| 65 |
inode_addr root_dir; |
| 66 |
inode_addr indices; |
| 67 |
int32_t pad[8]; |
| 68 |
} __attribute__((packed)); |
| 69 |
|
| 70 |
typedef struct data_stream { |
| 71 |
block_run direct[NUM_DIRECT_BLOCKS]; |
| 72 |
int64_t max_direct_range; |
| 73 |
block_run indirect; |
| 74 |
int64_t max_indirect_range; |
| 75 |
block_run double_indirect; |
| 76 |
int64_t max_double_indirect_range; |
| 77 |
int64_t size; |
| 78 |
} __attribute__((packed)) data_stream; |
| 79 |
|
| 80 |
struct befs_inode { |
| 81 |
int32_t magic1; |
| 82 |
inode_addr inode_num; |
| 83 |
int32_t uid; |
| 84 |
int32_t gid; |
| 85 |
int32_t mode; |
| 86 |
int32_t flags; |
| 87 |
int64_t create_time; |
| 88 |
int64_t last_modified_time; |
| 89 |
inode_addr parent; |
| 90 |
inode_addr attributes; |
| 91 |
uint32_t type; |
| 92 |
int32_t inode_size; |
| 93 |
uint32_t etc; |
| 94 |
data_stream data; |
| 95 |
int32_t pad[4]; |
| 96 |
int32_t small_data[0]; |
| 97 |
} __attribute__((packed)); |
| 98 |
|
| 99 |
struct small_data { |
| 100 |
uint32_t type; |
| 101 |
uint16_t name_size; |
| 102 |
uint16_t data_size; |
| 103 |
char name[0]; |
| 104 |
} __attribute__((packed)); |
| 105 |
|
| 106 |
struct bplustree_header { |
| 107 |
uint32_t magic; |
| 108 |
uint32_t node_size; |
| 109 |
uint32_t max_number_of_levels; |
| 110 |
uint32_t data_type; |
| 111 |
int64_t root_node_pointer; |
| 112 |
int64_t free_node_pointer; |
| 113 |
int64_t maximum_size; |
| 114 |
} __attribute__((packed)); |
| 115 |
|
| 116 |
struct bplustree_node { |
| 117 |
int64_t left_link; |
| 118 |
int64_t right_link; |
| 119 |
int64_t overflow_link; |
| 120 |
uint16_t all_key_count; |
| 121 |
uint16_t all_key_length; |
| 122 |
char name[0]; |
| 123 |
} __attribute__((packed)); |
| 124 |
|
| 125 |
static const unsigned char *get_block_run(blkid_probe pr, const struct befs_super_block *bs, |
| 126 |
const struct block_run *br, int fs_le) |
| 127 |
{ |
| 128 |
return blkid_probe_get_buffer(pr, |
| 129 |
((uint64_t) FS32_TO_CPU(br->allocation_group, fs_le) |
| 130 |
<< FS32_TO_CPU(bs->ag_shift, fs_le) |
| 131 |
<< FS32_TO_CPU(bs->block_shift, fs_le)) |
| 132 |
+ ((uint64_t) FS16_TO_CPU(br->start, fs_le) |
| 133 |
<< FS32_TO_CPU(bs->block_shift, fs_le)), |
| 134 |
(uint64_t) FS16_TO_CPU(br->len, fs_le) |
| 135 |
<< FS32_TO_CPU(bs->block_shift, fs_le)); |
| 136 |
} |
| 137 |
|
| 138 |
static const unsigned char *get_custom_block_run(blkid_probe pr, |
| 139 |
const struct befs_super_block *bs, |
| 140 |
const struct block_run *br, |
| 141 |
int64_t offset, uint32_t length, int fs_le) |
| 142 |
{ |
| 143 |
if (offset + length > (int64_t) FS16_TO_CPU(br->len, fs_le) |
| 144 |
<< FS32_TO_CPU(bs->block_shift, fs_le)) |
| 145 |
return NULL; |
| 146 |
|
| 147 |
return blkid_probe_get_buffer(pr, |
| 148 |
((uint64_t) FS32_TO_CPU(br->allocation_group, fs_le) |
| 149 |
<< FS32_TO_CPU(bs->ag_shift, fs_le) |
| 150 |
<< FS32_TO_CPU(bs->block_shift, fs_le)) |
| 151 |
+ ((uint64_t) FS16_TO_CPU(br->start, fs_le) |
| 152 |
<< FS32_TO_CPU(bs->block_shift, fs_le)) |
| 153 |
+ offset, |
| 154 |
length); |
| 155 |
} |
| 156 |
|
| 157 |
static const unsigned char *get_tree_node(blkid_probe pr, const struct befs_super_block *bs, |
| 158 |
const struct data_stream *ds, |
| 159 |
int64_t start, uint32_t length, int fs_le) |
| 160 |
{ |
| 161 |
if (start < (int64_t) FS64_TO_CPU(ds->max_direct_range, fs_le)) { |
| 162 |
int64_t br_len; |
| 163 |
size_t i; |
| 164 |
|
| 165 |
for (i = 0; i < NUM_DIRECT_BLOCKS; i++) { |
| 166 |
br_len = (int64_t) FS16_TO_CPU(ds->direct[i].len, fs_le) |
| 167 |
<< FS32_TO_CPU(bs->block_shift, fs_le); |
| 168 |
if (start < br_len) |
| 169 |
return get_custom_block_run(pr, bs, |
| 170 |
&ds->direct[i], |
| 171 |
start, length, fs_le); |
| 172 |
start -= br_len; |
| 173 |
if (start < 0) |
| 174 |
return NULL; /* Corrupt? */ |
| 175 |
} |
| 176 |
} else if (start < (int64_t) FS64_TO_CPU(ds->max_indirect_range, fs_le)) { |
| 177 |
struct block_run *br; |
| 178 |
int64_t max_br, br_len, i; |
| 179 |
|
| 180 |
start -= FS64_TO_CPU(ds->max_direct_range, fs_le); |
| 181 |
if (start < 0) |
| 182 |
return NULL; /* Corrupt? */ |
| 183 |
|
| 184 |
max_br = ((int64_t) FS16_TO_CPU(ds->indirect.len, fs_le) |
| 185 |
<< FS32_TO_CPU(bs->block_shift, fs_le)) |
| 186 |
/ sizeof(struct block_run); |
| 187 |
|
| 188 |
br = (struct block_run *) get_block_run(pr, bs, &ds->indirect, |
| 189 |
fs_le); |
| 190 |
if (!br) |
| 191 |
return NULL; |
| 192 |
|
| 193 |
for (i = 0; i < max_br; i++) { |
| 194 |
br_len = (int64_t) FS16_TO_CPU(br[i].len, fs_le) |
| 195 |
<< FS32_TO_CPU(bs->block_shift, fs_le); |
| 196 |
if (start < br_len) |
| 197 |
return get_custom_block_run(pr, bs, &br[i], |
| 198 |
start, length, fs_le); |
| 199 |
start -= br_len; |
| 200 |
} |
| 201 |
} else if (start < (int64_t) FS64_TO_CPU(ds->max_double_indirect_range, fs_le)) { |
| 202 |
struct block_run *br; |
| 203 |
int64_t max_br, di_br_size, br_per_di_br, di_index, i_index; |
| 204 |
|
| 205 |
start -= (int64_t) FS64_TO_CPU(ds->max_indirect_range, fs_le); |
| 206 |
if (start < 0) |
| 207 |
return NULL; /* Corrupt? */ |
| 208 |
|
| 209 |
di_br_size = (int64_t) FS16_TO_CPU(ds->double_indirect.len, |
| 210 |
fs_le) << FS32_TO_CPU(bs->block_shift, fs_le); |
| 211 |
if (di_br_size == 0) |
| 212 |
return NULL; |
| 213 |
|
| 214 |
br_per_di_br = di_br_size / sizeof(struct block_run); |
| 215 |
if (br_per_di_br == 0) |
| 216 |
return NULL; |
| 217 |
|
| 218 |
di_index = start / (br_per_di_br * di_br_size); |
| 219 |
i_index = (start % (br_per_di_br * di_br_size)) / di_br_size; |
| 220 |
start = (start % (br_per_di_br * di_br_size)) % di_br_size; |
| 221 |
|
| 222 |
if (di_index >= br_per_di_br) |
| 223 |
return NULL; /* Corrupt? */ |
| 224 |
|
| 225 |
br = (struct block_run *) get_block_run(pr, bs, |
| 226 |
&ds->double_indirect, fs_le); |
| 227 |
if (!br) |
| 228 |
return NULL; |
| 229 |
|
| 230 |
max_br = ((int64_t)FS16_TO_CPU(br[di_index].len, fs_le) |
| 231 |
<< FS32_TO_CPU(bs->block_shift, fs_le)) |
| 232 |
/ sizeof(struct block_run); |
| 233 |
|
| 234 |
if (i_index >= max_br) |
| 235 |
return NULL; /* Corrupt? */ |
| 236 |
|
| 237 |
br = (struct block_run *) get_block_run(pr, bs, &br[di_index], |
| 238 |
fs_le); |
| 239 |
if (!br) |
| 240 |
return NULL; |
| 241 |
|
| 242 |
return get_custom_block_run(pr, bs, &br[i_index], start, length, |
| 243 |
fs_le); |
| 244 |
} |
| 245 |
return NULL; |
| 246 |
} |
| 247 |
|
| 248 |
#define BAD_KEYS -2 |
| 249 |
|
| 250 |
static int32_t compare_keys(const char keys1[], uint16_t keylengths1[], |
| 251 |
int32_t index, const char *key2, |
| 252 |
uint16_t keylength2, uint16_t all_key_length, |
| 253 |
int fs_le) |
| 254 |
{ |
| 255 |
const char *key1; |
| 256 |
uint16_t keylength1, keystart1; |
| 257 |
int32_t result; |
| 258 |
|
| 259 |
keystart1 = index == 0 ? 0 : FS16_TO_CPU(keylengths1[index - 1], fs_le); |
| 260 |
keylength1 = FS16_TO_CPU(keylengths1[index], fs_le) - keystart1; |
| 261 |
|
| 262 |
if (keystart1 + keylength1 > all_key_length) |
| 263 |
return BAD_KEYS; /* Corrupt? */ |
| 264 |
|
| 265 |
key1 = &keys1[keystart1]; |
| 266 |
|
| 267 |
result = strncmp(key1, key2, min(keylength1, keylength2)); |
| 268 |
|
| 269 |
if (result == 0) |
| 270 |
return keylength1 - keylength2; |
| 271 |
|
| 272 |
if (result < 0) /* Don't clash with BAD_KEYS */ |
| 273 |
result = -1; |
| 274 |
|
| 275 |
return result; |
| 276 |
} |
| 277 |
|
| 278 |
static int64_t get_key_value(blkid_probe pr, const struct befs_super_block *bs, |
| 279 |
const struct befs_inode *bi, const char *key, int fs_le) |
| 280 |
{ |
| 281 |
struct bplustree_header *bh; |
| 282 |
struct bplustree_node *bn; |
| 283 |
uint16_t *keylengths; |
| 284 |
int64_t *values; |
| 285 |
int64_t node_pointer; |
| 286 |
uint32_t bn_size, all_key_count, all_key_length; |
| 287 |
uint32_t keylengths_offset, values_offset; |
| 288 |
int32_t first, last, mid, cmp; |
| 289 |
int loop_detect = 0; |
| 290 |
|
| 291 |
errno = 0; |
| 292 |
bh = (struct bplustree_header *) get_tree_node(pr, bs, &bi->data, 0, |
| 293 |
sizeof(struct bplustree_header), fs_le); |
| 294 |
if (!bh) |
| 295 |
return errno ? -errno : -ENOENT; |
| 296 |
|
| 297 |
if ((int32_t) FS32_TO_CPU(bh->magic, fs_le) != BPLUSTREE_MAGIC) |
| 298 |
return -ENOENT; |
| 299 |
|
| 300 |
node_pointer = FS64_TO_CPU(bh->root_node_pointer, fs_le); |
| 301 |
bn_size = FS32_TO_CPU(bh->node_size, fs_le); |
| 302 |
|
| 303 |
if (bn_size < sizeof(struct bplustree_node)) |
| 304 |
return -ENOENT; /* Corrupt? */ |
| 305 |
|
| 306 |
do { |
| 307 |
errno = 0; |
| 308 |
|
| 309 |
bn = (struct bplustree_node *) get_tree_node(pr, bs, &bi->data, |
| 310 |
node_pointer, bn_size, fs_le); |
| 311 |
if (!bn) |
| 312 |
return errno ? -errno : -ENOENT; |
| 313 |
|
| 314 |
all_key_count = FS16_TO_CPU(bn->all_key_count, fs_le); |
| 315 |
all_key_length = FS16_TO_CPU(bn->all_key_length, fs_le); |
| 316 |
|
| 317 |
if (all_key_count == 0) |
| 318 |
return -ENOENT; /* Corrupt? */ |
| 319 |
|
| 320 |
keylengths_offset = |
| 321 |
(sizeof(struct bplustree_node) + all_key_length |
| 322 |
+ sizeof(int64_t) - 1) & ~(sizeof(int64_t) - 1); |
| 323 |
values_offset = keylengths_offset + |
| 324 |
all_key_count * sizeof(uint16_t); |
| 325 |
|
| 326 |
if (values_offset + all_key_count * sizeof(uint64_t) > bn_size) |
| 327 |
return -ENOENT; /* Corrupt? */ |
| 328 |
|
| 329 |
keylengths = (uint16_t *) ((uint8_t *) bn + keylengths_offset); |
| 330 |
values = (int64_t *) ((uint8_t *) bn + values_offset); |
| 331 |
|
| 332 |
first = 0; |
| 333 |
mid = 0; |
| 334 |
last = all_key_count - 1; |
| 335 |
|
| 336 |
cmp = compare_keys(bn->name, keylengths, last, key, |
| 337 |
strlen(key), all_key_length, fs_le); |
| 338 |
if (cmp == BAD_KEYS) |
| 339 |
return -ENOENT; |
| 340 |
|
| 341 |
if (cmp == 0) { |
| 342 |
if ((int64_t) FS64_TO_CPU(bn->overflow_link, fs_le) |
| 343 |
== BPLUSTREE_NULL) |
| 344 |
return FS64_TO_CPU(values[last], fs_le); |
| 345 |
|
| 346 |
node_pointer = FS64_TO_CPU(values[last], fs_le); |
| 347 |
} else if (cmp < 0) |
| 348 |
node_pointer = FS64_TO_CPU(bn->overflow_link, fs_le); |
| 349 |
else { |
| 350 |
while (first <= last) { |
| 351 |
mid = (first + last) / 2; |
| 352 |
|
| 353 |
cmp = compare_keys(bn->name, keylengths, mid, |
| 354 |
key, strlen(key), |
| 355 |
all_key_length, fs_le); |
| 356 |
if (cmp == BAD_KEYS) |
| 357 |
return -ENOENT; |
| 358 |
|
| 359 |
if (cmp == 0) { |
| 360 |
if ((int64_t) FS64_TO_CPU(bn->overflow_link, |
| 361 |
fs_le) == BPLUSTREE_NULL) |
| 362 |
return FS64_TO_CPU(values[mid], |
| 363 |
fs_le); |
| 364 |
break; |
| 365 |
} |
| 366 |
|
| 367 |
if (cmp < 0) |
| 368 |
first = mid + 1; |
| 369 |
else |
| 370 |
last = mid - 1; |
| 371 |
} |
| 372 |
if (cmp < 0) { |
| 373 |
if (mid + 1U < all_key_count) |
| 374 |
node_pointer = FS64_TO_CPU(values[mid + 1], |
| 375 |
fs_le); |
| 376 |
else |
| 377 |
node_pointer = FS64_TO_CPU(bn->overflow_link, |
| 378 |
fs_le); |
| 379 |
} else |
| 380 |
node_pointer = FS64_TO_CPU(values[mid], fs_le); |
| 381 |
} |
| 382 |
} while (++loop_detect < 100 && |
| 383 |
(int64_t) FS64_TO_CPU(bn->overflow_link, fs_le) |
| 384 |
!= BPLUSTREE_NULL); |
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
static int get_uuid(blkid_probe pr, const struct befs_super_block *bs, |
| 389 |
uint64_t * const uuid, int fs_le) |
| 390 |
{ |
| 391 |
struct befs_inode *bi; |
| 392 |
struct small_data *sd; |
| 393 |
uint64_t bi_size, offset, sd_size, sd_total_size; |
| 394 |
|
| 395 |
bi = (struct befs_inode *) get_block_run(pr, bs, &bs->root_dir, fs_le); |
| 396 |
if (!bi) |
| 397 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 398 |
|
| 399 |
if (FS32_TO_CPU(bi->magic1, fs_le) != INODE_MAGIC1) |
| 400 |
return BLKID_PROBE_NONE; |
| 401 |
|
| 402 |
bi_size = (uint64_t)FS16_TO_CPU(bs->root_dir.len, fs_le) << |
| 403 |
FS32_TO_CPU(bs->block_shift, fs_le); |
| 404 |
sd_total_size = min(bi_size - sizeof(struct befs_inode), |
| 405 |
(uint64_t)FS32_TO_CPU(bi->inode_size, fs_le)); |
| 406 |
|
| 407 |
offset = 0; |
| 408 |
|
| 409 |
while (offset + sizeof(struct small_data) <= sd_total_size) { |
| 410 |
sd = (struct small_data *) ((uint8_t *)bi->small_data + offset); |
| 411 |
sd_size = sizeof(struct small_data) |
| 412 |
+ FS16_TO_CPU(sd->name_size, fs_le) + 3 |
| 413 |
+ FS16_TO_CPU(sd->data_size, fs_le) + 1; |
| 414 |
|
| 415 |
if (offset + sd_size > sd_total_size) |
| 416 |
break; |
| 417 |
|
| 418 |
if (FS32_TO_CPU(sd->type, fs_le) == B_UINT64_TYPE |
| 419 |
&& FS16_TO_CPU(sd->name_size, fs_le) == strlen(KEY_NAME) |
| 420 |
&& FS16_TO_CPU(sd->data_size, fs_le) == KEY_SIZE |
| 421 |
&& strcmp(sd->name, KEY_NAME) == 0) { |
| 422 |
|
| 423 |
memcpy(uuid, |
| 424 |
sd->name + FS16_TO_CPU(sd->name_size, fs_le) + 3, |
| 425 |
sizeof(uint64_t)); |
| 426 |
|
| 427 |
break; |
| 428 |
} |
| 429 |
|
| 430 |
if (FS32_TO_CPU(sd->type, fs_le) == 0 |
| 431 |
&& FS16_TO_CPU(sd->name_size, fs_le) == 0 |
| 432 |
&& FS16_TO_CPU(sd->data_size, fs_le) == 0) |
| 433 |
break; |
| 434 |
|
| 435 |
offset += sd_size; |
| 436 |
} |
| 437 |
|
| 438 |
if (*uuid == 0 |
| 439 |
&& (FS32_TO_CPU(bi->attributes.allocation_group, fs_le) != 0 |
| 440 |
|| FS16_TO_CPU(bi->attributes.start, fs_le) != 0 |
| 441 |
|| FS16_TO_CPU(bi->attributes.len, fs_le) != 0)) { |
| 442 |
int64_t value; |
| 443 |
|
| 444 |
bi = (struct befs_inode *) get_block_run(pr, bs, |
| 445 |
&bi->attributes, fs_le); |
| 446 |
if (!bi) |
| 447 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 448 |
|
| 449 |
if (FS32_TO_CPU(bi->magic1, fs_le) != INODE_MAGIC1) |
| 450 |
return BLKID_PROBE_NONE; |
| 451 |
|
| 452 |
value = get_key_value(pr, bs, bi, KEY_NAME, fs_le); |
| 453 |
if (value < 0) |
| 454 |
return value == -ENOENT ? BLKID_PROBE_NONE : value; |
| 455 |
|
| 456 |
if (value > 0) { |
| 457 |
bi = (struct befs_inode *) blkid_probe_get_buffer(pr, |
| 458 |
value << FS32_TO_CPU(bs->block_shift, fs_le), |
| 459 |
FS32_TO_CPU(bs->block_size, fs_le)); |
| 460 |
if (!bi) |
| 461 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 462 |
|
| 463 |
if (FS32_TO_CPU(bi->magic1, fs_le) != INODE_MAGIC1) |
| 464 |
return 1; |
| 465 |
|
| 466 |
if (FS32_TO_CPU(bi->type, fs_le) == B_UINT64_TYPE |
| 467 |
&& FS64_TO_CPU(bi->data.size, fs_le) == KEY_SIZE |
| 468 |
&& FS16_TO_CPU(bi->data.direct[0].len, fs_le) |
| 469 |
== 1) { |
| 470 |
uint64_t *attr_data; |
| 471 |
|
| 472 |
attr_data = (uint64_t *) get_block_run(pr, bs, |
| 473 |
&bi->data.direct[0], fs_le); |
| 474 |
if (!attr_data) |
| 475 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 476 |
|
| 477 |
*uuid = *attr_data; |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
return 0; |
| 482 |
} |
| 483 |
|
| 484 |
static int probe_befs(blkid_probe pr, const struct blkid_idmag *mag) |
| 485 |
{ |
| 486 |
struct befs_super_block *bs; |
| 487 |
const char *version = NULL; |
| 488 |
uint64_t volume_id = 0; |
| 489 |
uint32_t block_size, block_shift; |
| 490 |
int fs_le, ret; |
| 491 |
|
| 492 |
bs = (struct befs_super_block *) blkid_probe_get_buffer(pr, |
| 493 |
mag->sboff - B_OS_NAME_LENGTH, |
| 494 |
sizeof(struct befs_super_block)); |
| 495 |
if (!bs) |
| 496 |
return errno ? -errno : BLKID_PROBE_NONE; |
| 497 |
|
| 498 |
if (le32_to_cpu(bs->magic1) == SUPER_BLOCK_MAGIC1 |
| 499 |
&& le32_to_cpu(bs->magic2) == SUPER_BLOCK_MAGIC2 |
| 500 |
&& le32_to_cpu(bs->magic3) == SUPER_BLOCK_MAGIC3 |
| 501 |
&& le32_to_cpu(bs->fs_byte_order) == SUPER_BLOCK_FS_ENDIAN) { |
| 502 |
fs_le = 1; |
| 503 |
version = "little-endian"; |
| 504 |
} else if (be32_to_cpu(bs->magic1) == SUPER_BLOCK_MAGIC1 |
| 505 |
&& be32_to_cpu(bs->magic2) == SUPER_BLOCK_MAGIC2 |
| 506 |
&& be32_to_cpu(bs->magic3) == SUPER_BLOCK_MAGIC3 |
| 507 |
&& be32_to_cpu(bs->fs_byte_order) == SUPER_BLOCK_FS_ENDIAN) { |
| 508 |
fs_le = 0; |
| 509 |
version = "big-endian"; |
| 510 |
} else |
| 511 |
return BLKID_PROBE_NONE; |
| 512 |
|
| 513 |
block_size = FS32_TO_CPU(bs->block_size, fs_le); |
| 514 |
block_shift = FS32_TO_CPU(bs->block_shift, fs_le); |
| 515 |
|
| 516 |
if (block_shift < 10 || block_shift > 13 || |
| 517 |
block_size != 1U << block_shift) |
| 518 |
return BLKID_PROBE_NONE; |
| 519 |
|
| 520 |
/* get_block_run() shifts uint64 left by ag_shift + block_shift, |
| 521 |
* so the combined value must stay below 64 to avoid UB */ |
| 522 |
if (FS32_TO_CPU(bs->ag_shift, fs_le) + block_shift >= 64) |
| 523 |
return BLKID_PROBE_NONE; |
| 524 |
|
| 525 |
ret = get_uuid(pr, bs, &volume_id, fs_le); |
| 526 |
|
| 527 |
if (ret != 0) |
| 528 |
return ret; |
| 529 |
|
| 530 |
/* |
| 531 |
* all checks pass, set LABEL, VERSION and UUID |
| 532 |
*/ |
| 533 |
if (*bs->name != '\0') |
| 534 |
blkid_probe_set_label(pr, (unsigned char *) bs->name, |
| 535 |
sizeof(bs->name)); |
| 536 |
if (version) |
| 537 |
blkid_probe_set_version(pr, version); |
| 538 |
|
| 539 |
if (volume_id) |
| 540 |
blkid_probe_sprintf_uuid(pr, (unsigned char *) &volume_id, |
| 541 |
sizeof(volume_id), "%016" PRIx64, |
| 542 |
FS64_TO_CPU(volume_id, fs_le)); |
| 543 |
|
| 544 |
blkid_probe_set_fsblocksize(pr, block_size); |
| 545 |
blkid_probe_set_block_size(pr, block_size); |
| 546 |
blkid_probe_set_fsendianness(pr, |
| 547 |
fs_le ? BLKID_ENDIANNESS_LITTLE : BLKID_ENDIANNESS_BIG); |
| 548 |
|
| 549 |
return BLKID_PROBE_OK; |
| 550 |
} |
| 551 |
|
| 552 |
const struct blkid_idinfo befs_idinfo = |
| 553 |
{ |
| 554 |
.name = "befs", |
| 555 |
.usage = BLKID_USAGE_FILESYSTEM, |
| 556 |
.probefunc = probe_befs, |
| 557 |
.minsz = 1024 * 1440, |
| 558 |
.magics = { |
| 559 |
{ .magic = "BFS1", .len = 4, .sboff = B_OS_NAME_LENGTH }, |
| 560 |
{ .magic = "1SFB", .len = 4, .sboff = B_OS_NAME_LENGTH }, |
| 561 |
{ .magic = "BFS1", .len = 4, .sboff = 0x200 + |
| 562 |
B_OS_NAME_LENGTH }, |
| 563 |
{ .magic = "1SFB", .len = 4, .sboff = 0x200 + |
| 564 |
B_OS_NAME_LENGTH }, |
| 565 |
{ NULL } |
| 566 |
} |
| 567 |
}; |