Debian Codegraph @

libblkid/src/superblocks/nvidia_raid.c

raw
1 /*
2 * Copyright (C) 2008 Karel Zak <[email protected]>
3 * Copyright (C) 2005 Kay Sievers <[email protected]>
4 *
5 * Inspired by libvolume_id by
6 * Kay Sievers <[email protected]>
7 *
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
10 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdint.h>
16
17 #include "superblocks.h"
18
19 struct nv_metadata {
20 uint8_t vendor[8];
21 uint32_t size;
22 uint32_t chksum;
23 uint16_t version;
24 } __attribute__((packed));
25
26 #define NVIDIA_SIGNATURE "NVIDIA "
27 #define NVIDIA_SUPERBLOCK_SIZE 120
28
29
30 static int nvraid_verify_checksum(blkid_probe pr, const struct nv_metadata *nv,
31 size_t bufsiz)
32 {
33 uint32_t csum = le32_to_cpu(nv->chksum);
34 uint32_t count = le32_to_cpu(nv->size);
35
36 if (count > bufsiz / sizeof(uint32_t))
37 return 0;
38
39 for (size_t i = 0; i < count; i++)
40 csum += le32_to_cpu(((uint32_t *) nv)[i]);
41 return blkid_probe_verify_csum(pr, csum, le32_to_cpu(nv->chksum));
42 }
43
44 static int probe_nvraid(blkid_probe pr,
45 const struct blkid_idmag *mag __attribute__((__unused__)))
46 {
47 uint64_t off;
48 struct nv_metadata *nv;
49
50 if (!S_ISREG(pr->mode) && !blkid_probe_is_wholedisk(pr))
51 return 1;
52
53 off = ((pr->size / 0x200) - 2) * 0x200;
54 nv = (struct nv_metadata *)
55 blkid_probe_get_buffer(pr,
56 off,
57 NVIDIA_SUPERBLOCK_SIZE);
58 if (!nv)
59 return errno ? -errno : 1;
60
61 if (memcmp(nv->vendor, NVIDIA_SIGNATURE, sizeof(NVIDIA_SIGNATURE)-1) != 0)
62 return 1;
63 if (le32_to_cpu(nv->size) != NVIDIA_SUPERBLOCK_SIZE / 4)
64 return 1;
65 if (!nvraid_verify_checksum(pr, nv, NVIDIA_SUPERBLOCK_SIZE))
66 return 1;
67 if (blkid_probe_sprintf_version(pr, "%u", le16_to_cpu(nv->version)) != 0)
68 return 1;
69 if (blkid_probe_set_magic(pr, off, sizeof(nv->vendor),
70 (unsigned char *) nv->vendor))
71 return 1;
72 return 0;
73 }
74
75 const struct blkid_idinfo nvraid_idinfo = {
76 .name = "nvidia_raid_member",
77 .usage = BLKID_USAGE_RAID,
78 .minsz = 0x10000,
79 .probefunc = probe_nvraid,
80 .magics = BLKID_NONE_MAGIC
81 };
82
83