eedump.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. uint8_t eeprom[0x1000];
  8. struct PACKED EEPROM_header {
  9. uint16_t magic;
  10. uint8_t revision;
  11. uint8_t spare;
  12. };
  13. static const uint16_t k_EEPROM_magic = 0x5041;
  14. static const uint16_t k_EEPROM_revision = 2;
  15. struct PACKED Var_header {
  16. uint8_t size : 6;
  17. uint8_t spare : 2;
  18. uint8_t key;
  19. };
  20. static const uint8_t k_key_sentinel = 0xff;
  21. void
  22. fail(const char *why)
  23. {
  24. fprintf(stderr, "ERROR: %s\n", why);
  25. exit(1);
  26. }
  27. int
  28. main(int argc, char *argv[])
  29. {
  30. FILE *fp;
  31. struct EEPROM_header *header;
  32. struct Var_header *var;
  33. unsigned index;
  34. unsigned i;
  35. if (argc != 2) {
  36. fail("missing EEPROM file name");
  37. }
  38. if (NULL == (fp = fopen(argv[1], "rb"))) {
  39. fail("can't open EEPROM file");
  40. }
  41. if (1 != fread(eeprom, sizeof(eeprom), 1, fp)) {
  42. fail("can't read EEPROM file");
  43. }
  44. fclose(fp);
  45. header = (struct EEPROM_header *)&eeprom[0];
  46. if (header->magic != k_EEPROM_magic) {
  47. fail("bad magic in EEPROM file");
  48. }
  49. if (header->revision != 2) {
  50. fail("unsupported EEPROM format revision");
  51. }
  52. printf("Header OK\n");
  53. index = sizeof(*header);
  54. for (;; ) {
  55. var = (struct Var_header *)&eeprom[index];
  56. if (var->key == k_key_sentinel) {
  57. printf("end sentinel at %u\n", index);
  58. break;
  59. }
  60. printf("%04x: key %u size %d\n ", index, var->key, var->size + 1);
  61. index += sizeof(*var);
  62. for (i = 0; i <= var->size; i++) {
  63. printf(" %02x", eeprom[index + i]);
  64. }
  65. printf("\n");
  66. index += var->size + 1;
  67. if (index >= sizeof(eeprom)) {
  68. fflush(stdout);
  69. fail("missing end sentinel");
  70. }
  71. }
  72. }