eedump_apparam.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Simple tool to dump the AP_Param contents from an EEPROM dump
  3. * Andrew Tridgell February 2012
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. uint8_t eeprom[0x1000];
  9. struct EEPROM_header {
  10. uint8_t magic[2];
  11. uint8_t revision;
  12. uint8_t spare;
  13. };
  14. static const uint16_t k_EEPROM_magic0 = 0x50;
  15. static const uint16_t k_EEPROM_magic1 = 0x41;
  16. static const uint16_t k_EEPROM_revision = 6;
  17. enum ap_var_type {
  18. AP_PARAM_NONE = 0,
  19. AP_PARAM_INT8,
  20. AP_PARAM_INT16,
  21. AP_PARAM_INT32,
  22. AP_PARAM_FLOAT,
  23. AP_PARAM_VECTOR3F,
  24. AP_PARAM_VECTOR6F,
  25. AP_PARAM_MATRIX3F,
  26. AP_PARAM_GROUP
  27. };
  28. static const char *type_names[8] = {
  29. "NONE", "INT8", "INT16", "INT32", "FLOAT", "VECTOR3F", "MATRIX6F", "GROUP"
  30. };
  31. struct Param_header {
  32. uint32_t key : 8;
  33. uint32_t type : 6;
  34. uint32_t group_element : 18;
  35. };
  36. static const uint8_t _sentinal_key = 0xFF;
  37. static const uint8_t _sentinal_type = 0xFF;
  38. static const uint8_t _sentinal_group = 0xFF;
  39. static uint8_t type_size(enum ap_var_type type)
  40. {
  41. switch (type) {
  42. case AP_PARAM_NONE:
  43. case AP_PARAM_GROUP:
  44. return 0;
  45. case AP_PARAM_INT8:
  46. return 1;
  47. case AP_PARAM_INT16:
  48. return 2;
  49. case AP_PARAM_INT32:
  50. return 4;
  51. case AP_PARAM_FLOAT:
  52. return 4;
  53. case AP_PARAM_VECTOR3F:
  54. return 3*4;
  55. case AP_PARAM_VECTOR6F:
  56. return 6*4;
  57. case AP_PARAM_MATRIX3F:
  58. return 3*3*4;
  59. }
  60. printf("unknown type %u\n", (unsigned int)type);
  61. return 0;
  62. }
  63. static void
  64. fail(const char *why)
  65. {
  66. fprintf(stderr, "ERROR: %s\n", why);
  67. exit(1);
  68. }
  69. int
  70. main(int argc, char *argv[])
  71. {
  72. FILE *fp;
  73. struct EEPROM_header *header;
  74. struct Param_header *var;
  75. unsigned index;
  76. unsigned i;
  77. if (argc != 2) {
  78. fail("missing EEPROM file name");
  79. }
  80. if (NULL == (fp = fopen(argv[1], "rb"))) {
  81. fail("can't open EEPROM file");
  82. }
  83. if (1 != fread(eeprom, sizeof(eeprom), 1, fp)) {
  84. fail("can't read EEPROM file");
  85. }
  86. fclose(fp);
  87. header = (struct EEPROM_header *)&eeprom[0];
  88. if (header->magic[0] != k_EEPROM_magic0 ||
  89. header->magic[1] != k_EEPROM_magic1) {
  90. fail("bad magic in EEPROM file");
  91. }
  92. if (header->revision != k_EEPROM_revision) {
  93. fail("unsupported EEPROM format revision");
  94. }
  95. printf("Header OK\n");
  96. index = sizeof(*header);
  97. for (;; ) {
  98. uint8_t size;
  99. var = (struct Param_header *)&eeprom[index];
  100. if (var->key == _sentinal_key ||
  101. var->group_element == _sentinal_group ||
  102. var->type == _sentinal_type) {
  103. printf("end sentinel at %u\n", index);
  104. break;
  105. }
  106. size = type_size(var->type);
  107. printf("%04x: type %u (%s) key %u group_element %u size %d value ",
  108. index, var->type, type_names[var->type], var->key, var->group_element, size);
  109. index += sizeof(*var);
  110. switch (var->type) {
  111. case AP_PARAM_INT8:
  112. printf("%d\n", (int)*(int8_t *)&eeprom[index]);
  113. break;
  114. case AP_PARAM_INT16:
  115. printf("%d\n", (int)*(int16_t *)&eeprom[index]);
  116. break;
  117. case AP_PARAM_INT32:
  118. printf("%d\n", (int)*(int32_t *)&eeprom[index]);
  119. break;
  120. case AP_PARAM_FLOAT:
  121. printf("%f\n", *(float *)&eeprom[index]);
  122. break;
  123. case AP_PARAM_VECTOR3F:
  124. printf("%f %f %f\n",
  125. *(float *)&eeprom[index],
  126. *(float *)&eeprom[index+4],
  127. *(float *)&eeprom[index+8]);
  128. break;
  129. case AP_PARAM_VECTOR6F:
  130. printf("%f %f %f %f %f %f\n",
  131. *(float *)&eeprom[index],
  132. *(float *)&eeprom[index+4],
  133. *(float *)&eeprom[index+8],
  134. *(float *)&eeprom[index+12],
  135. *(float *)&eeprom[index+16],
  136. *(float *)&eeprom[index+20]);
  137. break;
  138. case AP_PARAM_MATRIX3F:
  139. printf("%f %f %f %f %f %f %f %f %f\n",
  140. *(float *)&eeprom[index],
  141. *(float *)&eeprom[index+4],
  142. *(float *)&eeprom[index+8],
  143. *(float *)&eeprom[index+12],
  144. *(float *)&eeprom[index+16],
  145. *(float *)&eeprom[index+20],
  146. *(float *)&eeprom[index+24],
  147. *(float *)&eeprom[index+28],
  148. *(float *)&eeprom[index+32]);
  149. break;
  150. default:
  151. printf("NONE\n");
  152. break;
  153. }
  154. for (i = 0; i < size; i++) {
  155. printf(" %02x", eeprom[index + i]);
  156. }
  157. printf("\n");
  158. index += size;
  159. if (index >= sizeof(eeprom)) {
  160. fflush(stdout);
  161. fail("missing end sentinel");
  162. }
  163. }
  164. return 0;
  165. }