1 /*
2  * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
3  *
4  * Inspired by libvolume_id by
5  *     Kay Sievers <kay.sievers@vrfy.org>
6  *
7  * This file may be redistributed under the terms of the
8  * GNU Lesser General Public License.
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <ctype.h>
17 #include <stdint.h>
18 
19 #include "superblocks.h"
20 
21 struct via_metadata {
22 	uint16_t	signature;
23 	uint8_t		version_number;
24 	struct via_array {
25 		uint16_t	disk_bit_mask;
26 		uint8_t		disk_array_ex;
27 		uint32_t	capacity_low;
28 		uint32_t	capacity_high;
29 		uint32_t	serial_checksum;
30 	} __attribute__((packed)) array;
31 	uint32_t	serial_checksum[8];
32 	uint8_t		checksum;
33 } __attribute__((packed));
34 
35 #define VIA_SIGNATURE		0xAA55
36 
37 /* 8 bit checksum on first 50 bytes of metadata. */
via_checksum(struct via_metadata * v)38 static uint8_t via_checksum(struct via_metadata *v)
39 {
40 	uint8_t i = 50, cs = 0;
41 
42 	while (i--)
43 		cs += ((uint8_t*) v)[i];
44 
45 	return cs;
46 }
47 
probe_viaraid(blkid_probe pr,const struct blkid_idmag * mag)48 static int probe_viaraid(blkid_probe pr,
49 		const struct blkid_idmag *mag __attribute__((__unused__)))
50 {
51 	uint64_t off;
52 	struct via_metadata *v;
53 
54 	if (pr->size < 0x10000)
55 		return 1;
56 	if (!S_ISREG(pr->mode) && !blkid_probe_is_wholedisk(pr))
57 		return 1;
58 
59 	off = ((pr->size / 0x200)-1) * 0x200;
60 
61 	v = (struct via_metadata *)
62 			blkid_probe_get_buffer(pr,
63 				off,
64 				sizeof(struct via_metadata));
65 	if (!v)
66 		return errno ? -errno : 1;
67 
68 	if (le16_to_cpu(v->signature) != VIA_SIGNATURE)
69 		return 1;
70 	if (v->version_number > 2)
71 		return 1;
72 	if (!blkid_probe_verify_csum(pr, via_checksum(v), v->checksum))
73 		return 1;
74 
75 	if (blkid_probe_sprintf_version(pr, "%u", v->version_number) != 0)
76 		return 1;
77 	if (blkid_probe_set_magic(pr, off,
78 				sizeof(v->signature),
79 				(unsigned char *) &v->signature))
80 		return 1;
81 	return 0;
82 }
83 
84 const struct blkid_idinfo viaraid_idinfo = {
85 	.name		= "via_raid_member",
86 	.usage		= BLKID_USAGE_RAID,
87 	.probefunc	= probe_viaraid,
88 	.magics		= BLKID_NONE_MAGIC
89 };
90 
91 
92