3rdparty/fsp: Update submodule to upstream master
[coreboot2.git] / util / cbfstool / fpt_formats / fpt_hdr_20.c
blob166d24cdf06c370929a6df5cd9c68b3d5b7e8839
1 /* CSE FPT header version 0x20 */
2 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <sys/types.h>
6 #include "cse_fpt.h"
8 struct fpt_hdr {
9 uint8_t marker[4]; /* FPT_MARKER */
10 uint32_t num_entries;
11 uint8_t hdr_version; /* FPT_HDR_VERSION_20 */
12 uint8_t entry_version; /* FPT_ENTRY_VERSION */
13 uint8_t hdr_length;
14 uint8_t hdr_checksum;
15 uint8_t rsvd[20];
16 } __packed;
18 static bool match_version(struct buffer *buff)
20 const uint8_t *data = buffer_get(buff);
21 uint8_t version = read_at_le8(data, offsetof(struct fpt_hdr, hdr_version));
23 return version == FPT_HDR_VERSION_20;
26 static bool validate_fpt_hdr(const struct fpt_hdr *h)
28 if (memcmp(h->marker, FPT_MARKER, sizeof(h->marker))) {
29 ERROR("Invalid FPT header marker!\n");
30 return false;
33 if (h->hdr_version != FPT_HDR_VERSION_20) {
34 ERROR("Invalid FPT header version(0x%x)!\n", h->hdr_version);
35 return false;
38 if (h->entry_version != FPT_ENTRY_VERSION) {
39 ERROR("Invalid FPT entry version(0x%x)!\n", h->entry_version);
40 return false;
43 const uint8_t *data = (const uint8_t *)h;
44 uint8_t checksum = 0;
46 for (size_t i = 0; i < sizeof(*h); i++)
47 checksum += data[i];
49 if (checksum != 0) {
50 ERROR("Invalid checksum (0x%x)!\n", h->hdr_checksum);
51 return false;
54 return true;
57 static fpt_hdr_ptr read_fpt_hdr(struct buffer *buff)
59 struct fpt_hdr *h = malloc(sizeof(*h));
60 if (!h)
61 return NULL;
63 READ_MEMBER(buff, h->marker);
64 READ_MEMBER(buff, h->num_entries);
65 READ_MEMBER(buff, h->hdr_version);
66 READ_MEMBER(buff, h->entry_version);
67 READ_MEMBER(buff, h->hdr_length);
68 READ_MEMBER(buff, h->hdr_checksum);
69 READ_MEMBER(buff, h->rsvd);
71 if (!validate_fpt_hdr(h)) {
72 free(h);
73 return NULL;
76 return h;
79 static void print_fpt_hdr(const fpt_hdr_ptr ptr)
81 struct fpt_hdr *h = ptr;
83 printf(" * FPT header\n");
84 printf("%-25s: %.4s\n", "Marker", h->marker);
85 printf("%-25s: %d\n", "Number of entries", h->num_entries);
86 printf("%-25s: 0x%x\n", "Header version", h->hdr_version);
87 printf("%-25s: 0x%x\n", "Entry version", h->entry_version);
88 printf("%-25s: %d\n", "Header length", h->hdr_length);
89 printf("%-25s: 0x%x\n", "Header checksum", h->hdr_checksum);
92 static size_t get_entry_count(const fpt_hdr_ptr ptr)
94 struct fpt_hdr *h = ptr;
96 return h->num_entries;
99 const struct fpt_hdr_ops fpt_hdr_20_ops = {
100 .match_version = match_version,
102 .read = read_fpt_hdr,
103 .print = print_fpt_hdr,
105 .get_entry_count = get_entry_count,