summaryrefslogtreecommitdiffstats
path: root/util/cbfstool/bpdt_formats/subpart_hdr_1.c
blob: 5335c7a844e766cc6f090aa7b71fb539e6682756 (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
/* Subpart directory header version 1 support */
/* SPDX-License-Identifier: GPL-2.0-only */

#include <sys/types.h>

#include "cse_serger.h"

struct subpart_hdr {
	uint32_t signature;		/* SUBPART_SIGNATURE */
	uint32_t count;
	uint8_t hdr_version;		/* Header version = 1 */
	uint8_t entry_version;		/* Entry version = 1 */
	uint8_t length;
	uint8_t checksum;
	uint8_t name[4];
} __packed;

static void subpart_hdr_print(const subpart_hdr_ptr ptr)
{
	const struct subpart_hdr *hdr = ptr;

	printf("%-25s %.4s\n", "Signature", (const char *)&hdr->signature);
	printf("%-25s %-25d\n", "Count", hdr->count);
	printf("%-25s %-25d\n", "Header Version", hdr->hdr_version);
	printf("%-25s %-25d\n", "Entry Version", hdr->entry_version);
	printf("%-25s 0x%-23x\n", "Header Length", hdr->length);
	printf("%-25s 0x%-23x\n", "Checksum", hdr->checksum);
	printf("%-25s ", "Name");
	for (size_t i = 0; i < sizeof(hdr->name); i++)
		printf("%c", hdr->name[i]);
	printf("\n");
}

static subpart_hdr_ptr subpart_hdr_read(struct buffer *buff)
{
	struct subpart_hdr *hdr = malloc(sizeof(*hdr));

	if (!hdr)
		return NULL;

	READ_MEMBER(buff, hdr->signature);
	READ_MEMBER(buff, hdr->count);
	READ_MEMBER(buff, hdr->hdr_version);
	READ_MEMBER(buff, hdr->entry_version);
	READ_MEMBER(buff, hdr->length);
	READ_MEMBER(buff, hdr->checksum);
	READ_MEMBER(buff, hdr->name);

	return hdr;
}

static size_t subpart_get_count(const subpart_hdr_ptr ptr)
{
	const struct subpart_hdr *hdr = ptr;

	return hdr->count;
}

static void subpart_hdr_free(subpart_hdr_ptr ptr)
{
	free(ptr);
}

const struct subpart_hdr_ops subpart_hdr_1_ops = {
	.read = subpart_hdr_read,
	.print = subpart_hdr_print,
	.get_entry_count = subpart_get_count,
	.free = subpart_hdr_free,
};