APM:Libraries
eedump.c
Go to the documentation of this file.
1 /*
2  * Simple tool to dump the AP_Var contents from an EEPROM dump
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7 
8 uint8_t eeprom[0x1000];
9 
11  uint16_t magic;
12  uint8_t revision;
13  uint8_t spare;
14 };
15 
16 static const uint16_t k_EEPROM_magic = 0x5041;
17 static const uint16_t k_EEPROM_revision = 2;
18 
20  uint8_t size : 6;
21  uint8_t spare : 2;
22  uint8_t key;
23 };
24 
25 static const uint8_t k_key_sentinel = 0xff;
26 
27 void
28 fail(const char *why)
29 {
30  fprintf(stderr, "ERROR: %s\n", why);
31  exit(1);
32 }
33 
34 int
35 main(int argc, char *argv[])
36 {
37  FILE *fp;
38  struct EEPROM_header *header;
39  struct Var_header *var;
40  unsigned index;
41  unsigned i;
42 
43  if (argc != 2) {
44  fail("missing EEPROM file name");
45  }
46  if (NULL == (fp = fopen(argv[1], "rb"))) {
47  fail("can't open EEPROM file");
48  }
49  if (1 != fread(eeprom, sizeof(eeprom), 1, fp)) {
50  fail("can't read EEPROM file");
51  }
52  fclose(fp);
53 
54  header = (struct EEPROM_header *)&eeprom[0];
55  if (header->magic != k_EEPROM_magic) {
56  fail("bad magic in EEPROM file");
57  }
58  if (header->revision != 2) {
59  fail("unsupported EEPROM format revision");
60  }
61  printf("Header OK\n");
62 
63  index = sizeof(*header);
64  for (;; ) {
65  var = (struct Var_header *)&eeprom[index];
66  if (var->key == k_key_sentinel) {
67  printf("end sentinel at %u\n", index);
68  break;
69  }
70  printf("%04x: key %u size %d\n ", index, var->key, var->size + 1);
71  index += sizeof(*var);
72  for (i = 0; i <= var->size; i++) {
73  printf(" %02x", eeprom[index + i]);
74  }
75  printf("\n");
76  index += var->size + 1;
77  if (index >= sizeof(eeprom)) {
78  fflush(stdout);
79  fail("missing end sentinel");
80  }
81  }
82 }
int printf(const char *fmt,...)
Definition: stdio.c:113
uint16_t magic
Definition: eedump.c:11
void fail(const char *why)
Definition: eedump.c:28
uint8_t eeprom[0x1000]
Definition: eedump.c:8
static const uint16_t k_EEPROM_magic
Definition: eedump.c:16
uint8_t spare
Definition: eedump.c:21
static const uint8_t k_key_sentinel
Definition: eedump.c:25
#define NULL
Definition: hal_types.h:59
uint8_t spare
Definition: eedump.c:13
static const uint16_t k_EEPROM_revision
Definition: eedump.c:17
uint8_t size
Definition: eedump.c:20
FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
Definition: posix.c:772
int main(int argc, char *argv[])
Definition: eedump.c:35
uint8_t key
Definition: eedump.c:22
int fprintf(FILE *fp, const char *fmt,...)
fprintf character write function
Definition: posix.c:2539
#define PACKED
Definition: AP_Common.h:28
uint8_t revision
Definition: eedump.c:12