| 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 redundancy; |
| 15 |
uint8_t reserved[8]; |
| 16 |
uint32_t checksum; |
| 17 |
struct { |
| 18 |
uint16_t major; |
| 19 |
uint16_t minor; |
| 20 |
uint16_t build; |
| 21 |
uint16_t hotfix; |
| 22 |
} fit_tool_version; |
| 23 |
} __packed; |
| 24 |
|
| 25 |
static bool match_version(struct buffer *buff) |
| 26 |
{ |
| 27 |
const uint8_t *data = buffer_get(buff); |
| 28 |
uint8_t version = read_at_le8(data, offsetof(struct fpt_hdr, hdr_version)); |
| 29 |
|
| 30 |
return version == FPT_HDR_VERSION_21; |
| 31 |
} |
| 32 |
|
| 33 |
static bool validate_fpt_hdr(const struct fpt_hdr *h) |
| 34 |
{ |
| 35 |
if (memcmp(h->marker, FPT_MARKER, sizeof(h->marker))) { |
| 36 |
ERROR("Invalid FPT header marker!\n"); |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
if (h->hdr_version != FPT_HDR_VERSION_21) { |
| 41 |
ERROR("Invalid FPT header version(0x%x)!\n", h->hdr_version); |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
if (h->entry_version != FPT_ENTRY_VERSION) { |
| 46 |
ERROR("Invalid FPT entry version(0x%x)!\n", h->entry_version); |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
static fpt_hdr_ptr read_fpt_hdr(struct buffer *buff) |
| 54 |
{ |
| 55 |
struct fpt_hdr *h = malloc(sizeof(*h)); |
| 56 |
if (!h) |
| 57 |
return NULL; |
| 58 |
|
| 59 |
READ_MEMBER(buff, h->marker); |
| 60 |
READ_MEMBER(buff, h->num_entries); |
| 61 |
READ_MEMBER(buff, h->hdr_version); |
| 62 |
READ_MEMBER(buff, h->entry_version); |
| 63 |
READ_MEMBER(buff, h->hdr_length); |
| 64 |
READ_MEMBER(buff, h->redundancy); |
| 65 |
READ_MEMBER(buff, h->reserved); |
| 66 |
READ_MEMBER(buff, h->checksum); |
| 67 |
READ_MEMBER(buff, h->fit_tool_version); |
| 68 |
|
| 69 |
if (!validate_fpt_hdr(h)) { |
| 70 |
free(h); |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
|
| 74 |
return h; |
| 75 |
} |
| 76 |
|
| 77 |
static void print_fpt_hdr(const fpt_hdr_ptr ptr) |
| 78 |
{ |
| 79 |
struct fpt_hdr *h = ptr; |
| 80 |
|
| 81 |
printf(" * FPT header\n"); |
| 82 |
printf("%-25s: %.4s\n", "Marker", h->marker); |
| 83 |
printf("%-25s: %d\n", "Number of entries", h->num_entries); |
| 84 |
printf("%-25s: 0x%x\n", "Header version", h->hdr_version); |
| 85 |
printf("%-25s: 0x%x\n", "Entry version", h->entry_version); |
| 86 |
printf("%-25s: %d\n", "Header length", h->hdr_length); |
| 87 |
printf("%-25s: 0x%x\n", "Redundancy", h->redundancy); |
| 88 |
printf("%-25s: ", "Reserved"); |
| 89 |
for (size_t i = 0; i < ARRAY_SIZE(h->reserved); i++) |
| 90 |
printf("0x%x ", h->reserved[i]); |
| 91 |
printf("\n"); |
| 92 |
printf("%-25s: 0x%x\n", "Checksum", h->checksum); |
| 93 |
printf("%-25s: %d.%d.%d.%d(%.2x.%.2x.%.2x.%.2x)\n", "FIT Tool Version", |
| 94 |
h->fit_tool_version.major, h->fit_tool_version.minor, |
| 95 |
h->fit_tool_version.build, h->fit_tool_version.hotfix, |
| 96 |
h->fit_tool_version.major, h->fit_tool_version.minor, |
| 97 |
h->fit_tool_version.build, h->fit_tool_version.hotfix); |
| 98 |
} |
| 99 |
|
| 100 |
static size_t get_entry_count(const fpt_hdr_ptr ptr) |
| 101 |
{ |
| 102 |
struct fpt_hdr *h = ptr; |
| 103 |
|
| 104 |
return h->num_entries; |
| 105 |
} |
| 106 |
|
| 107 |
const struct fpt_hdr_ops fpt_hdr_21_ops = { |
| 108 |
.match_version = match_version, |
| 109 |
|
| 110 |
.read = read_fpt_hdr, |
| 111 |
.print = print_fpt_hdr, |
| 112 |
|
| 113 |
.get_entry_count = get_entry_count, |
| 114 |
}; |