From f7c3a151c7b0a3f040ab9aa8d81c562684d3f2e8 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Tue, 1 Nov 2016 12:09:41 -0400 Subject: hal_ks_index_fsck() and a pile of debugging code. The debugging code was for tracking down what turned out to be a race condition in the Alpha's flash driver code (see sw/stm32); much of this was temporary, and will be removed in a (near) future commit, but some of the techniques were useful and belong in the repository in case we need to pull them back for something similar in the future. hal_ks_index_fsck() attempts to diagnose all the things I found wrong in the ks_flash index after one long series of errors. As presently written, it doesn't attempt to fix anything, just diagnose errors: the intent is that we can call this, before and after every modification if necessary, to poinpoint exactly which calls introduce errors. Once things stablize a bit, we may want to crank down the number of calls to this (it's a bit expensive, since it checks the entire index), and perhaps add the ability to clean up whatever errors it might find; the latter might be a good candidate for a CLI command. --- ks_volatile.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'ks_volatile.c') diff --git a/ks_volatile.c b/ks_volatile.c index 0f53c11..2e6ea3e 100644 --- a/ks_volatile.c +++ b/ks_volatile.c @@ -44,16 +44,12 @@ #define KEK_LENGTH (bitsToBytes(256)) -#ifndef HAL_STATIC_PKEY_STATE_BLOCKS -#define HAL_STATIC_PKEY_STATE_BLOCKS 0 +#ifndef STATIC_KS_VOLATILE_SLOTS +#define STATIC_KS_VOLATILE_SLOTS HAL_STATIC_PKEY_STATE_BLOCKS #endif -#ifndef HAL_KS_VOLATILE_SLOTS -#define HAL_KS_VOLATILE_SLOTS HAL_STATIC_PKEY_STATE_BLOCKS -#endif - -#ifndef HAL_KS_VOLATILE_ATTRIBUTE_SPACE -#define HAL_KS_VOLATILE_ATTRIBUTE_SPACE 4096 +#ifndef STATIC_KS_VOLATILE_ATTRIBUTE_SPACE +#define STATIC_KS_VOLATILE_ATTRIBUTE_SPACE 4096 #endif /* @@ -70,7 +66,7 @@ typedef struct { hal_session_handle_t session; size_t der_len; unsigned attributes_len; - uint8_t der[HAL_KS_WRAPPED_KEYSIZE + HAL_KS_VOLATILE_ATTRIBUTE_SPACE]; + uint8_t der[HAL_KS_WRAPPED_KEYSIZE + STATIC_KS_VOLATILE_ATTRIBUTE_SPACE]; } ks_key_t; typedef struct { @@ -97,7 +93,7 @@ typedef struct { * conditional testing whether either HAL_KS_*_SLOTS were nonzero. */ -#if HAL_KS_VOLATILE_SLOTS > 0 +#if STATIC_KS_VOLATILE_SLOTS > 0 static ks_t volatile_ks; @@ -156,10 +152,10 @@ static hal_error_t ks_init(const hal_ks_driver_t * const driver, 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) * HAL_KS_VOLATILE_SLOTS); - ksv->db->ksi.names = gnaw(&mem, &len, sizeof(*ksv->db->ksi.names) * HAL_KS_VOLATILE_SLOTS); - ksv->db->keys = gnaw(&mem, &len, sizeof(*ksv->db->keys) * HAL_KS_VOLATILE_SLOTS); - ksv->db->ksi.size = HAL_KS_VOLATILE_SLOTS; + 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; if (ksv->db == NULL || @@ -174,7 +170,7 @@ static hal_error_t ks_init(const hal_ks_driver_t * const driver, * just populate the free list in block numerical order. */ - for (int i = 0; i < HAL_KS_VOLATILE_SLOTS; i++) + for (int i = 0; i < STATIC_KS_VOLATILE_SLOTS; i++) ksv->db->ksi.index[i] = i; return hal_ks_index_setup(&ksv->db->ksi); @@ -183,9 +179,9 @@ static hal_error_t ks_init(const hal_ks_driver_t * const driver, static hal_error_t ks_volatile_init(const hal_ks_driver_t * const driver) { const size_t len = (sizeof(*volatile_ks.db) + - sizeof(*volatile_ks.db->ksi.index) * HAL_KS_VOLATILE_SLOTS + - sizeof(*volatile_ks.db->ksi.names) * HAL_KS_VOLATILE_SLOTS + - sizeof(*volatile_ks.db->keys) * HAL_KS_VOLATILE_SLOTS); + 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); @@ -613,7 +609,7 @@ const hal_ks_driver_t hal_ks_volatile_driver[1] = {{ ks_delete_attribute }}; -#endif /* HAL_KS_VOLATILE_SLOTS > 0 */ +#endif /* STATIC_KS_VOLATILE_SLOTS > 0 */ /* * Local variables: -- cgit v1.2.3