diff options
-rw-r--r-- | hal_internal.h | 8 | ||||
-rw-r--r-- | ks_flash.c | 36 | ||||
-rw-r--r-- | ks_volatile.c | 37 | ||||
-rw-r--r-- | rpc_server.c | 6 |
4 files changed, 54 insertions, 33 deletions
diff --git a/hal_internal.h b/hal_internal.h index 20b89af..9685b0c 100644 --- a/hal_internal.h +++ b/hal_internal.h @@ -453,7 +453,8 @@ typedef struct hal_ks hal_ks_t; struct hal_ks_driver { - hal_error_t (*init)(const hal_ks_driver_t * const driver); + hal_error_t (*init)(const hal_ks_driver_t * const driver, + const int alloc); hal_error_t (*shutdown)(const hal_ks_driver_t * const driver); @@ -518,7 +519,8 @@ extern const hal_ks_driver_t hal_ks_volatile_driver[1], hal_ks_token_driver[1]; -static inline hal_error_t hal_ks_init(const hal_ks_driver_t * const driver) +static inline hal_error_t hal_ks_init(const hal_ks_driver_t * const driver, + const int alloc) { if (driver == NULL) return HAL_ERROR_BAD_ARGUMENTS; @@ -526,7 +528,7 @@ static inline hal_error_t hal_ks_init(const hal_ks_driver_t * const driver) if (driver->init == NULL) return HAL_ERROR_NOT_IMPLEMENTED; - return driver->init(driver); + return driver->init(driver, alloc); } static inline hal_error_t hal_ks_shutdown(const hal_ks_driver_t * const driver) @@ -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) diff --git a/ks_volatile.c b/ks_volatile.c index 0ee19c8..e23aefe 100644 --- a/ks_volatile.c +++ b/ks_volatile.c @@ -143,20 +143,19 @@ static hal_error_t ks_init(const hal_ks_driver_t * const driver, uint8_t *mem, size_t len) { - if (ksv == NULL || mem == NULL) + if (ksv == NULL) return HAL_ERROR_IMPOSSIBLE; - memset(ksv, 0, sizeof(*ksv)); - memset(mem, 0, len); + if (mem != NULL) { + memset(ksv, 0, sizeof(*ksv)); + memset(mem, 0, len); - ksv->ks.driver = driver; - ksv->per_session = per_session; - ksv->db = gnaw(&mem, &len, sizeof(*ksv->db)); - ksv->db->ksi.index = gnaw(&mem, &len, sizeof(*ksv->db->ksi.index) * STATIC_KS_VOLATILE_SLOTS); - ksv->db->ksi.names = gnaw(&mem, &len, sizeof(*ksv->db->ksi.names) * STATIC_KS_VOLATILE_SLOTS); - ksv->db->keys = gnaw(&mem, &len, sizeof(*ksv->db->keys) * STATIC_KS_VOLATILE_SLOTS); - ksv->db->ksi.size = STATIC_KS_VOLATILE_SLOTS; - ksv->db->ksi.used = 0; + ksv->db = gnaw(&mem, &len, sizeof(*ksv->db)); + ksv->db->ksi.index = gnaw(&mem, &len, sizeof(*ksv->db->ksi.index) * STATIC_KS_VOLATILE_SLOTS); + ksv->db->ksi.names = gnaw(&mem, &len, sizeof(*ksv->db->ksi.names) * STATIC_KS_VOLATILE_SLOTS); + ksv->db->keys = gnaw(&mem, &len, sizeof(*ksv->db->keys) * STATIC_KS_VOLATILE_SLOTS); + ksv->db->ksi.size = STATIC_KS_VOLATILE_SLOTS; + } if (ksv->db == NULL || ksv->db->ksi.index == NULL || @@ -164,6 +163,16 @@ static hal_error_t ks_init(const hal_ks_driver_t * const driver, ksv->db->keys == NULL) return HAL_ERROR_IMPOSSIBLE; + if (mem == NULL) { + memset(ksv->db->ksi.index, 0, sizeof(*ksv->db->ksi.index) * STATIC_KS_VOLATILE_SLOTS); + memset(ksv->db->ksi.names, 0, sizeof(*ksv->db->ksi.names) * STATIC_KS_VOLATILE_SLOTS); + memset(ksv->db->keys, 0, sizeof(*ksv->db->keys) * STATIC_KS_VOLATILE_SLOTS); + } + + ksv->ks.driver = driver; + ksv->per_session = per_session; + ksv->db->ksi.used = 0; + /* * Set up keystore with empty index and full free list. * Since this driver doesn't care about wear leveling, @@ -176,16 +185,16 @@ static hal_error_t ks_init(const hal_ks_driver_t * const driver, return hal_ks_index_setup(&ksv->db->ksi); } -static hal_error_t ks_volatile_init(const hal_ks_driver_t * const driver) +static hal_error_t ks_volatile_init(const hal_ks_driver_t * const driver, const int alloc) { const size_t len = (sizeof(*volatile_ks.db) + sizeof(*volatile_ks.db->ksi.index) * STATIC_KS_VOLATILE_SLOTS + sizeof(*volatile_ks.db->ksi.names) * STATIC_KS_VOLATILE_SLOTS + sizeof(*volatile_ks.db->keys) * STATIC_KS_VOLATILE_SLOTS); - uint8_t *mem = hal_allocate_static_memory(len); + uint8_t *mem = NULL; - if (mem == NULL) + if (alloc && (mem = hal_allocate_static_memory(len)) == NULL) return HAL_ERROR_ALLOCATION_FAILURE; return ks_init(driver, 1, &volatile_ks, mem, len); diff --git a/rpc_server.c b/rpc_server.c index b6c755e..ed9a8d5 100644 --- a/rpc_server.c +++ b/rpc_server.c @@ -926,9 +926,9 @@ hal_error_t hal_rpc_server_init(void) { hal_error_t err; - if ((err = hal_ks_init(hal_ks_volatile_driver)) != HAL_OK || - (err = hal_ks_init(hal_ks_token_driver)) != HAL_OK || - (err = hal_rpc_server_transport_init()) != HAL_OK) + if ((err = hal_ks_init(hal_ks_volatile_driver, 1)) != HAL_OK || + (err = hal_ks_init(hal_ks_token_driver, 1)) != HAL_OK || + (err = hal_rpc_server_transport_init()) != HAL_OK) return err; return HAL_OK; |