| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <sys/types.h> |
| 5 |
|
| 6 |
#include "cse_fpt.h" |
| 7 |
|
| 8 |
struct fpt_hdr { |
| 9 |
uint8_t marker[4]; |
| 10 |
uint32_t num_entries; |
| 11 |
uint8_t hdr_version; |
| 12 |
uint8_t entry_version; |
| 13 |
uint8_t hdr_length; |
| 14 |
uint8_t hdr_checksum; |
| 15 |
uint8_t rsvd[20]; |
| 16 |
} __packed; |
| 17 |
|
| 18 |
static bool match_version(struct buffer *buff) |
| 19 |
{ |
| 20 |
const uint8_t *data = buffer_get(buff); |
| 21 |
uint8_t version = read_at_le8(data, offsetof(struct fpt_hdr, hdr_version)); |
| 22 |
|
| 23 |
return version == FPT_HDR_VERSION_20; |
| 24 |
} |
| 25 |
|
| 26 |
static bool validate_fpt_hdr(const struct fpt_hdr *h) |
| 27 |
{ |
| 28 |
if (memcmp(h->marker, FPT_MARKER, sizeof(h->marker))) { |
| 29 |
ERROR("Invalid FPT header marker!\n"); |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
if (h->hdr_version != FPT_HDR_VERSION_20) { |
| 34 |
ERROR("Invalid FPT header version(0x%x)!\n", h->hdr_version); |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
if (h->entry_version != FPT_ENTRY_VERSION) { |
| 39 |
ERROR("Invalid FPT entry version(0x%x)!\n", h->entry_version); |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
const uint8_t *data = (const uint8_t *)h; |
| 44 |
uint8_t checksum = 0; |
| 45 |
|
| 46 |
for (size_t i = 0; i < sizeof(*h); i++) |
| 47 |
checksum += data[i]; |
| 48 |
|
| 49 |
if (checksum != 0) { |
| 50 |
ERROR("Invalid checksum (0x%x)!\n", h->hdr_checksum); |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
static fpt_hdr_ptr read_fpt_hdr(struct buffer *buff) |
| 58 |
{ |
| 59 |
struct fpt_hdr *h = malloc(sizeof(*h)); |
| 60 |
if (!h) |
| 61 |
return NULL; |
| 62 |
|
| 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); |
| 70 |
|
| 71 |
if (!validate_fpt_hdr(h)) { |
| 72 |
free(h); |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
|
| 76 |
return h; |
| 77 |
} |
| 78 |
|
| 79 |
static void print_fpt_hdr(const fpt_hdr_ptr ptr) |
| 80 |
{ |
| 81 |
struct fpt_hdr *h = ptr; |
| 82 |
|
| 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); |
| 90 |
} |
| 91 |
|
| 92 |
static size_t get_entry_count(const fpt_hdr_ptr ptr) |
| 93 |
{ |
| 94 |
struct fpt_hdr *h = ptr; |
| 95 |
|
| 96 |
return h->num_entries; |
| 97 |
} |
| 98 |
|
| 99 |
const struct fpt_hdr_ops fpt_hdr_20_ops = { |
| 100 |
.match_version = match_version, |
| 101 |
|
| 102 |
.read = read_fpt_hdr, |
| 103 |
.print = print_fpt_hdr, |
| 104 |
|
| 105 |
.get_entry_count = get_entry_count, |
| 106 |
}; |