summaryrefslogtreecommitdiffstats
path: root/libblkid/src/superblocks/jmicron_raid.c
blob: 65e05b624d7c464a27fe3e3f0c7b4b7889b658a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
 *
 * Inspired by libvolume_id by
 *     Kay Sievers <kay.sievers@vrfy.org>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>

#include "superblocks.h"

#define JM_SIGNATURE		"JM"
#define JM_MINOR_VERSION(_x)	(le16_to_cpu((_x)->version) & 0xFF)
#define JM_MAJOR_VERSION(_x)	(le16_to_cpu((_x)->version) >> 8)
#define JM_SPARES		2
#define JM_MEMBERS		8

struct jm_metadata {
	int8_t		signature[2];	/* 0x0 - 0x01 */

	uint16_t	version;	/* 0x03 - 0x04 JMicron version */

	uint16_t	checksum;	/* 0x04 - 0x05 */
	uint8_t		filler[10];

	uint32_t	identity;	/* 0x10 - 0x13 */

	struct {
		uint32_t	base;	/* 0x14 - 0x17 */
		uint32_t	range;	/* 0x18 - 0x1B range */
		uint16_t	range2;	/* 0x1C - 0x1D range2 */
	} segment;

	int8_t		name[16];	/* 0x20 - 0x2F */

	uint8_t		mode;		/* 0x30 RAID level */
	uint8_t		block;		/* 0x31 stride size (2=4K, 3=8K, ...) */
	uint16_t	attribute;	/* 0x32 - 0x33 */
	uint8_t		filler1[4];

	uint32_t	spare[JM_SPARES];	/* 0x38 - 0x3F */
	uint32_t	member[JM_MEMBERS];	/* 0x40 - 0x5F */

	uint8_t		filler2[0x20];
} __attribute__ ((packed));

static int jm_checksum(blkid_probe pr, const struct jm_metadata *jm)
{
	size_t count = sizeof(*jm) / sizeof(uint16_t);
	uint16_t sum = 0;
	unsigned char *ptr = (unsigned char *) jm;

	while (count--) {
		uint16_t val;

		memcpy(&val, ptr, sizeof(uint16_t));
		sum += le16_to_cpu(val);

		ptr += sizeof(uint16_t);
	}

	return blkid_probe_verify_csum(pr, sum == 0 || sum == 1, 1);
}

static int probe_jmraid(blkid_probe pr,
		const struct blkid_idmag *mag __attribute__((__unused__)))
{
	uint64_t off;
	const struct jm_metadata *jm;

	if (pr->size < 0x10000)
		return 1;
	if (!S_ISREG(pr->mode) && !blkid_probe_is_wholedisk(pr))
		return 1;

	off = ((pr->size / 0x200) - 1) * 0x200;
	jm = (struct jm_metadata *)
		blkid_probe_get_buffer(pr,
				off,
				sizeof(struct jm_metadata));
	if (!jm)
		return errno ? -errno : 1;

	if (memcmp(jm->signature, JM_SIGNATURE, sizeof(JM_SIGNATURE) - 1) != 0)
		return 1;

	if (!jm_checksum(pr, jm))
		return 1;

	if (jm->mode > 5)
		return 1;

	if (blkid_probe_sprintf_version(pr, "%u.%u",
			JM_MAJOR_VERSION(jm), JM_MINOR_VERSION(jm)) != 0)
		return 1;
	if (blkid_probe_set_magic(pr, off, sizeof(jm->signature),
				(unsigned char *) jm->signature))
		return 1;
	return 0;
}

const struct blkid_idinfo jmraid_idinfo = {
	.name		= "jmicron_raid_member",
	.usage		= BLKID_USAGE_RAID,
	.probefunc	= probe_jmraid,
	.magics		= BLKID_NONE_MAGIC
};