| 1 |
/* |
| 2 |
* Copyright (C) 1999, 2001 by Andries Brouwer |
| 3 |
* Copyright (C) 1999, 2000, 2003 by Theodore Ts'o |
| 4 |
* Copyright (C) 2008 Karel Zak <[email protected]> |
| 5 |
* |
| 6 |
* This file may be redistributed under the terms of the |
| 7 |
* GNU Lesser General Public License. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <string.h> |
| 14 |
#include <errno.h> |
| 15 |
#include <ctype.h> |
| 16 |
#include <stdint.h> |
| 17 |
|
| 18 |
#include "superblocks.h" |
| 19 |
|
| 20 |
struct romfs_super_block { |
| 21 |
unsigned char ros_magic[8]; |
| 22 |
uint32_t ros_full_size; |
| 23 |
uint32_t ros_checksum; |
| 24 |
unsigned char ros_volume[16]; |
| 25 |
} __attribute__((packed)); |
| 26 |
|
| 27 |
static int romfs_verify_csum(blkid_probe pr, const struct blkid_idmag *mag, |
| 28 |
const struct romfs_super_block *ros) |
| 29 |
{ |
| 30 |
uint32_t csummed_size = min((uint32_t) 512, |
| 31 |
be32_to_cpu(ros->ros_full_size)); |
| 32 |
const unsigned char *csummed; |
| 33 |
uint32_t csum; |
| 34 |
|
| 35 |
if (csummed_size % sizeof(uint32_t) != 0) |
| 36 |
return 0; |
| 37 |
|
| 38 |
csummed = blkid_probe_get_sb_buffer(pr, mag, csummed_size); |
| 39 |
if (!csummed) |
| 40 |
return 0; |
| 41 |
|
| 42 |
csum = 0; |
| 43 |
while (csummed_size) { |
| 44 |
csum += be32_to_cpu(*(uint32_t *) csummed); |
| 45 |
csummed_size -= sizeof(uint32_t); |
| 46 |
csummed += sizeof(uint32_t); |
| 47 |
} |
| 48 |
return blkid_probe_verify_csum(pr, csum, 0); |
| 49 |
} |
| 50 |
|
| 51 |
static int probe_romfs(blkid_probe pr, const struct blkid_idmag *mag) |
| 52 |
{ |
| 53 |
const struct romfs_super_block *ros; |
| 54 |
|
| 55 |
ros = blkid_probe_get_sb(pr, mag, struct romfs_super_block); |
| 56 |
if (!ros) |
| 57 |
return errno ? -errno : 1; |
| 58 |
|
| 59 |
if (!romfs_verify_csum(pr, mag, ros)) |
| 60 |
return 1; |
| 61 |
|
| 62 |
if (*((char *) ros->ros_volume) != '\0') |
| 63 |
blkid_probe_set_label(pr, ros->ros_volume, |
| 64 |
sizeof(ros->ros_volume)); |
| 65 |
|
| 66 |
blkid_probe_set_fsblocksize(pr, 1024); |
| 67 |
blkid_probe_set_fssize(pr, be32_to_cpu(ros->ros_full_size)); |
| 68 |
blkid_probe_set_block_size(pr, 1024); |
| 69 |
|
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
const struct blkid_idinfo romfs_idinfo = |
| 74 |
{ |
| 75 |
.name = "romfs", |
| 76 |
.usage = BLKID_USAGE_FILESYSTEM, |
| 77 |
.probefunc = probe_romfs, |
| 78 |
.magics = |
| 79 |
{ |
| 80 |
{ .magic = "-rom1fs-", .len = 8 }, |
| 81 |
{ NULL } |
| 82 |
} |
| 83 |
}; |
| 84 |
|