From ecbc49a97941b208fb162e4a6d10ca7277dc9359 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Tue, 15 Nov 2016 02:02:24 -0500 Subject: Allow keystore reinitialization without re-allocating static memory. Wiping the keystore flash requires reinitializing the keystore, but we don't want to allocate new static memory when we do this. --- ks_flash.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'ks_flash.c') diff --git a/ks_flash.c b/ks_flash.c index cc18e9d..5b38ec5 100644 --- a/ks_flash.c +++ b/ks_flash.c @@ -563,28 +563,38 @@ static inline void *gnaw(uint8_t **mem, size_t *len, const size_t size) return ret; } -static hal_error_t ks_init(const hal_ks_driver_t * const driver) +static hal_error_t ks_init(const hal_ks_driver_t * const driver, const int alloc) { /* * Initialize the in-memory database. */ - size_t len = (sizeof(*db.ksi.index) * NUM_FLASH_BLOCKS + - sizeof(*db.ksi.names) * NUM_FLASH_BLOCKS + - sizeof(*db.cache) * KS_FLASH_CACHE_SIZE); + if (alloc) { - uint8_t *mem = hal_allocate_static_memory(len); + size_t len = (sizeof(*db.ksi.index) * NUM_FLASH_BLOCKS + + sizeof(*db.ksi.names) * NUM_FLASH_BLOCKS + + sizeof(*db.cache) * KS_FLASH_CACHE_SIZE); - if (mem == NULL) - return HAL_ERROR_ALLOCATION_FAILURE; + uint8_t *mem = hal_allocate_static_memory(len); - memset(&db, 0, sizeof(db)); - memset(mem, 0, len); + if (mem == NULL) + return HAL_ERROR_ALLOCATION_FAILURE; + + memset(&db, 0, sizeof(db)); + memset(mem, 0, len); + + db.ksi.index = gnaw(&mem, &len, sizeof(*db.ksi.index) * NUM_FLASH_BLOCKS); + db.ksi.names = gnaw(&mem, &len, sizeof(*db.ksi.names) * NUM_FLASH_BLOCKS); + db.cache = gnaw(&mem, &len, sizeof(*db.cache) * KS_FLASH_CACHE_SIZE); + db.ksi.size = NUM_FLASH_BLOCKS; + } + + else { + memset(&db.wheel_pin, 0, sizeof(db.wheel_pin)); + memset(&db.so_pin, 0, sizeof(db.so_pin)); + memset(&db.user_pin, 0, sizeof(db.user_pin)); + } - db.ksi.index = gnaw(&mem, &len, sizeof(*db.ksi.index) * NUM_FLASH_BLOCKS); - db.ksi.names = gnaw(&mem, &len, sizeof(*db.ksi.names) * NUM_FLASH_BLOCKS); - db.cache = gnaw(&mem, &len, sizeof(*db.cache) * KS_FLASH_CACHE_SIZE); - db.ksi.size = NUM_FLASH_BLOCKS; db.ksi.used = 0; if (db.ksi.index == NULL || db.ksi.names == NULL || db.cache == NULL) -- cgit v1.2.3