5 #define VERSION_MAJOR 0
9 #define VERSION_MINOR 0
13 #define VERSION_PATCH 0
16 static uint32_t signature
17 __attribute__((section(
".pib_signature")));
19 static uint8_t version
20 __attribute__((section(
".pib_version")));
23 __attribute__((section(
".pib_size")));
25 static uint32_t serial_number
26 __attribute__((section(
".pib_serial_number")));
28 static uint16_t hw_revision
29 __attribute__((section(
".pib_hw_revision")));
31 static uint32_t hw_variant
32 __attribute__((section(
".pib_hw_variant")));
34 static char vendor_name[32]
35 __attribute__((section(
".pib_vendor_name")));
37 static char product_name[32]
38 __attribute__((section(
".pib_product_name")));
40 static int16_t rf_offset
41 __attribute__((section(
".pib_rf_offset")));
44 __attribute__((section(
".pib_crc")));
46 static bool pib_valid;
49 calc_crc(uint32_t *crc,
const uint8_t *buf,
size_t len)
51 for (
size_t i = 0; i < len; i++)
55 for (
int j = 0; j < 8; j++)
57 *crc = (*crc >> 1) ^ (0xedb88320 & ~((*crc & 1) - 1));
64 void twr_info_init(
void)
66 twr_info_pib_t res = twr_info_pib_check();
72 case TWR_INFO_PIB_VALID:
74 case TWR_INFO_PIB_ERR_SIGNATURE:
77 case TWR_INFO_PIB_ERR_VERSION:
80 case TWR_INFO_PIB_ERR_SIZE:
83 case TWR_INFO_PIB_ERR_CRC:
95 twr_log_error(
"APP: Integrity check for PIB failed %s", type);
99 twr_info_pib_check(
void)
103 if (signature != 0xbabecafe)
104 return TWR_INFO_PIB_ERR_SIGNATURE;
107 return TWR_INFO_PIB_ERR_VERSION;
109 if (size != 88 + 4 + 4)
110 return TWR_INFO_PIB_ERR_SIZE;
112 uint32_t crc_ = 0xffffffff;
114 calc_crc(&crc_, (
const uint8_t *)&signature,
sizeof(signature));
115 calc_crc(&crc_, (
const uint8_t *)&version,
sizeof(version));
116 calc_crc(&crc_, (
const uint8_t *)&size,
sizeof(size));
117 calc_crc(&crc_, (
const uint8_t *)&serial_number,
sizeof(serial_number));
118 calc_crc(&crc_, (
const uint8_t *)&hw_revision,
sizeof(hw_revision));
119 calc_crc(&crc_, (
const uint8_t *)&hw_variant,
sizeof(hw_variant));
120 calc_crc(&crc_, (
const uint8_t *)&vendor_name,
sizeof(vendor_name));
121 calc_crc(&crc_, (
const uint8_t *)&product_name,
sizeof(product_name));
122 calc_crc(&crc_, (
const uint8_t *)&rf_offset,
sizeof(rf_offset));
127 return TWR_INFO_PIB_ERR_CRC;
132 return TWR_INFO_PIB_VALID;
136 twr_info_serial_number(
void)
138 return pib_valid ? serial_number : 0;
142 twr_info_hw_revision(
void)
144 return pib_valid ? hw_revision : 0;
148 twr_info_hw_variant(
void)
150 return pib_valid ? hw_variant : 0;
154 twr_info_fw_version(
void)
158 return VERSION_MAJOR << 24 | VERSION_MINOR << 16 | VERSION_PATCH << 8;
167 twr_info_vendor_name(
void)
180 twr_info_product_name(
void)
void void void void void twr_log_error(const char *format,...) __attribute__((format(printf
Log ERROR message (annotated in log as <E>)