summaryrefslogtreecommitdiffstats
path: root/libblkid/src/superblocks/ubi.c
blob: b15199bd4981632aa088c23dae99573d9cc5b7fb (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
/*
 * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
 *
 * 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"
#include "crc32.h"

struct ubi_ec_hdr {
	uint32_t	magic;
	uint8_t		version;
	uint8_t		padding1[3];
	uint64_t	ec;
	uint32_t	vid_hdr_offset;
	uint32_t	data_offset;
	uint32_t	image_seq;
	uint8_t		padding2[32];
	uint32_t	hdr_crc;
} __attribute__((packed));

static int ubi_verify_csum(blkid_probe pr, const struct ubi_ec_hdr *hdr)
{
	return blkid_probe_verify_csum(pr,
			ul_crc32(~0LL, (unsigned char *) hdr,
				sizeof(*hdr) - sizeof(hdr->hdr_crc)),
			be32_to_cpu(hdr->hdr_crc));
}

static int probe_ubi(blkid_probe pr, const struct blkid_idmag *mag)
{
	const struct ubi_ec_hdr *hdr;

	hdr = blkid_probe_get_sb(pr, mag, struct ubi_ec_hdr);
	if (!hdr)
		return -1;

	if (!ubi_verify_csum(pr, hdr))
		return -1;

	blkid_probe_sprintf_version(pr, "%u", hdr->version);
	blkid_probe_sprintf_uuid(pr, (unsigned char *)&hdr->image_seq, 4, "%u",
				 be32_to_cpu(hdr->image_seq));
	return 0;
}

const struct blkid_idinfo ubi_idinfo =
{
	.name		= "ubi",
	.usage		= BLKID_USAGE_RAID,
	.probefunc	= probe_ubi,
	.magics		=
	{
		{ .magic = "UBI#", .len = 4 },
		{ NULL }
	}
};