2 #include <twr_config.h>
3 #include <twr_sha256.h>
4 #include <twr_eeprom.h>
6 #define _CONFIG_SIZEOF_HEADER sizeof(config_header_t)
7 #define _CONFIG_SIZEOF_CONFIG sizeof(config_t)
9 #define _CONFIG_ADDRESS_HEADER 0
10 #define _CONFIG_ADDRESS_CONFIG (_CONFIG_ADDRESS_HEADER + _CONFIG_SIZEOF_HEADER)
36 static void _config_eeprom_read(uint32_t address,
void *buffer,
size_t length);
37 static void _config_eeprom_write(uint32_t address,
const void *buffer,
size_t length);
39 void twr_config_init(uint64_t signature,
void *config,
size_t size,
void *init_config)
41 _twr_config.signature = signature;
42 _twr_config.config = config;
43 _twr_config.size = size;
44 _twr_config.init_config = init_config;
55 if (!_twr_config.init_config)
58 memset(_twr_config.config, 0, _twr_config.size);
62 memcpy(_twr_config.config, _twr_config.init_config, _twr_config.size);
70 _config_eeprom_read(_CONFIG_ADDRESS_HEADER, &header, _CONFIG_SIZEOF_HEADER);
72 if (header.signature != _twr_config.signature ||
73 header.length != _twr_config.size)
78 _config_eeprom_read(_CONFIG_ADDRESS_CONFIG, _twr_config.config, _twr_config.size);
80 static twr_sha256_t sha256;
81 static uint8_t hash[32];
87 if (memcmp(header.hash, hash,
sizeof(header.hash)) != 0)
97 static twr_sha256_t sha256;
98 static uint8_t hash[32];
106 header.signature = _twr_config.signature;
107 header.length = _twr_config.size;
109 memcpy(header.hash, hash,
sizeof(header.hash));
111 _config_eeprom_write(_CONFIG_ADDRESS_HEADER, &header, _CONFIG_SIZEOF_HEADER);
112 _config_eeprom_write(_CONFIG_ADDRESS_CONFIG, _twr_config.config, _twr_config.size);
117 static void _config_eeprom_read(uint32_t address,
void *buffer,
size_t length)
121 for (
size_t i = 0; i < length; i++)
125 uint32_t offset_bank_a = 0;
126 uint32_t offset_bank_b = offset_bank_a + _CONFIG_SIZEOF_HEADER + _twr_config.size;
127 uint32_t offset_bank_c = offset_bank_b + _CONFIG_SIZEOF_HEADER + _twr_config.size;
136 *p++ = (a & b) | (a & c) | (b & c);
140 static void _config_eeprom_write(uint32_t address,
const void *buffer,
size_t length)
142 uint32_t offset_bank_a = 0;
143 uint32_t offset_bank_b = offset_bank_a + _CONFIG_SIZEOF_HEADER + _twr_config.size;
144 uint32_t offset_bank_c = offset_bank_b + _CONFIG_SIZEOF_HEADER + _twr_config.size;
void twr_config_init(uint64_t signature, void *config, size_t size, void *init_config)
Initialize and load the config from EEPROM.
bool twr_config_load(void)
Load EEPROM configuration.
bool twr_config_save(void)
Save configuration to EEPROM.
void twr_config_reset(void)
Reset EEPROM configuration to zeros or init_config.
bool twr_eeprom_read(uint32_t address, void *buffer, size_t length)
Read buffer from EEPROM area.
bool twr_eeprom_write(uint32_t address, const void *buffer, size_t length)
Write buffer to EEPROM area and verify it.
void twr_sha256_update(twr_sha256_t *self, const void *buffer, size_t length)
Compute SHA256 from data.
void twr_sha256_final(twr_sha256_t *self, uint8_t *hash, bool little_endian)
Finalize SHA256 computation and read the final hash.
void twr_sha256_init(twr_sha256_t *self)
Initialize SHA256 structure.