Linux: Support keys in config file (#1093)

- Signing and encryption keys are set through the configuration file.
- SoftEeprom update.
This commit is contained in:
Marcelo Aquino
2018-04-04 20:43:13 +02:00
committed by Mikael Falkvidd
parent 4d90c00f98
commit 3c0b2727a5
6 changed files with 230 additions and 127 deletions
+20 -17
View File
@@ -60,9 +60,6 @@ int SoftEeprom::init(const char *fileName, size_t length)
_length = length;
_values = new uint8_t[_length];
for (size_t i = 0; i < _length; ++i) {
_values[i] = 0xFF;
}
if (stat(_fileName, &fileInfo) != 0) {
//File does not exist. Create it.
@@ -72,11 +69,16 @@ int SoftEeprom::init(const char *fileName, size_t length)
logError("Unable to create config file %s.\n", _fileName);
return -1;
}
// Fill the eeprom with 1s
for (size_t i = 0; i < _length; ++i) {
_values[i] = 0xFF;
}
myFile.write((const char*)_values, _length);
myFile.close();
} else if (fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) {
logError("EEPROM file %s is not the correct size of %zu. Please remove the file and a new one will be created.\n",
_fileName, _length);
destroy();
return -1;
} else {
//Read config into local memory.
@@ -100,27 +102,19 @@ void SoftEeprom::destroy()
if (_fileName) {
free(_fileName);
}
_length = 0;
}
void SoftEeprom::readBlock(void* buf, void* addr, size_t length)
{
static bool config_to_mem = false;
unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
if (!config_to_mem && length) {
//Read config into local memory.
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
if (!myFile) {
logError("Unable to open config to file %s for reading.\n", _fileName);
exit(1);
}
myFile.read((char*)_values, _length);
myFile.close();
config_to_mem = true;
if (!length) {
logError("EEPROM being read without being initialized!\n");
return;
}
if (length && offs + length <= _length) {
if (offs + length <= _length) {
memcpy(buf, _values+offs, length);
}
}
@@ -129,7 +123,16 @@ void SoftEeprom::writeBlock(void* buf, void* addr, size_t length)
{
unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
if (length && offs + length <= _length) {
if (!length) {
logError("EEPROM being written without being initialized!\n");
return;
}
if (offs + length <= _length) {
if (memcmp(_values+offs, buf, length) == 0) {
return;
}
memcpy(_values+offs, buf, length);
std::ofstream myFile(_fileName, std::ios::out | std::ios::in | std::ios::binary);