/ util / cbfstool / bpdt_formats / subpart_hdr_1.c
subpart_hdr_1.c
 1  /* Subpart directory header version 1 support */
 2  /* SPDX-License-Identifier: GPL-2.0-only */
 3  
 4  #include <sys/types.h>
 5  
 6  #include "cse_serger.h"
 7  
 8  struct subpart_hdr {
 9  	uint32_t signature;		/* SUBPART_SIGNATURE */
10  	uint32_t count;
11  	uint8_t hdr_version;		/* Header version = 1 */
12  	uint8_t entry_version;		/* Entry version = 1 */
13  	uint8_t length;
14  	uint8_t checksum;
15  	uint8_t name[4];
16  } __packed;
17  
18  static void subpart_hdr_print(const subpart_hdr_ptr ptr)
19  {
20  	const struct subpart_hdr *hdr = ptr;
21  
22  	printf("%-25s %.4s\n", "Signature", (const char *)&hdr->signature);
23  	printf("%-25s %-25d\n", "Count", hdr->count);
24  	printf("%-25s %-25d\n", "Header Version", hdr->hdr_version);
25  	printf("%-25s %-25d\n", "Entry Version", hdr->entry_version);
26  	printf("%-25s 0x%-23x\n", "Header Length", hdr->length);
27  	printf("%-25s 0x%-23x\n", "Checksum", hdr->checksum);
28  	printf("%-25s ", "Name");
29  	for (size_t i = 0; i < sizeof(hdr->name); i++)
30  		printf("%c", hdr->name[i]);
31  	printf("\n");
32  }
33  
34  static subpart_hdr_ptr subpart_hdr_read(struct buffer *buff)
35  {
36  	struct subpart_hdr *hdr = malloc(sizeof(*hdr));
37  
38  	if (!hdr)
39  		return NULL;
40  
41  	READ_MEMBER(buff, hdr->signature);
42  	READ_MEMBER(buff, hdr->count);
43  	READ_MEMBER(buff, hdr->hdr_version);
44  	READ_MEMBER(buff, hdr->entry_version);
45  	READ_MEMBER(buff, hdr->length);
46  	READ_MEMBER(buff, hdr->checksum);
47  	READ_MEMBER(buff, hdr->name);
48  
49  	return hdr;
50  }
51  
52  static size_t subpart_get_count(const subpart_hdr_ptr ptr)
53  {
54  	const struct subpart_hdr *hdr = ptr;
55  
56  	return hdr->count;
57  }
58  
59  static void subpart_hdr_free(subpart_hdr_ptr ptr)
60  {
61  	free(ptr);
62  }
63  
64  const struct subpart_hdr_ops subpart_hdr_1_ops = {
65  	.read = subpart_hdr_read,
66  	.print = subpart_hdr_print,
67  	.get_entry_count = subpart_get_count,
68  	.free = subpart_hdr_free,
69  };