Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
Enforce minimum Llama EEPROM size for CRC32 check
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewlloyd committed Jul 11, 2021
1 parent 0548012 commit e582d34
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/common/llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@ static uint16_t eeprom_llama_var_offset(uint8_t id) {
static int eeprom_llama_check_crc32() {
uint16_t datasize = _eeprom_llama_cache.DATASIZE;
if (_eeprom_llama_cache.VERSION == 1)
datasize = 28; // before datasize field was added
if (datasize < 0 || datasize > EEPROM_LLAMA_DATASIZE) return 0;
datasize = EEPROM_LLAMA_MIN_DATASIZE; // before datasize field was added
if (datasize < EEPROM_LLAMA_MIN_DATASIZE || datasize > EEPROM_LLAMA_DATASIZE) return 0;
uint32_t crc2 = crc32_eeprom((uint32_t *)&_eeprom_llama_cache, (datasize - 4) / 4);
uint32_t eeprom_crc2 = *((uint32_t*)(((uint8_t*)&_eeprom_llama_cache) + datasize - 4));
return eeprom_crc2 == crc2 ? 1 : 0;
}

static void eeprom_llama_update_crc32() {
uint16_t datasize = _eeprom_llama_cache.DATASIZE;
if (datasize < 0 || datasize > EEPROM_LLAMA_DATASIZE) return;
if (datasize < EEPROM_LLAMA_MIN_DATASIZE || datasize > EEPROM_LLAMA_DATASIZE) return;
// calculate crc32
_eeprom_llama_cache.CRC32 = crc32_eeprom((uint32_t *)&_eeprom_llama_cache, (datasize - 4) / 4);
// write crc to eeprom
Expand Down
2 changes: 2 additions & 0 deletions src/common/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ typedef struct __attribute__((__packed__)) _eeprom_llama_vars_t {
uint32_t CRC32;
} eeprom_llama_vars_t;

#define EEPROM_LLAMA_MIN_DATASIZE (28)
static_assert(sizeof(eeprom_llama_vars_t) % 4 == 0, "EEPROM_LLAMA__PADDING needs to be adjusted so CRC32 could work.");
static const constexpr uint32_t EEPROM_LLAMA_VARCOUNT = sizeof(eeprom_llama_map) / sizeof(eeprom_entry_t);
static const constexpr uint32_t EEPROM_LLAMA_DATASIZE = sizeof(eeprom_llama_vars_t);
static_assert(EEPROM_LLAMA_DATASIZE >= EEPROM_LLAMA_MIN_DATASIZE, "EEPROM_LLAMA_DATASIZE too small. Only add fields.");


// Llama eeprom variable defaults
Expand Down

0 comments on commit e582d34

Please sign in to comment.