aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Selkirk <paul@psgd.org>2016-07-12 22:48:53 -0400
committerPaul Selkirk <paul@psgd.org>2016-07-12 22:48:53 -0400
commitd1012863307128061c4285a144c84ae736f3edeb (patch)
tree9139fc73af9fe5dc50845d9487600a18ec9a9248
parent20d94fd816ad1755086501547aaffdda7916235a (diff)
Make probe_cores deal with an unconfigured FPGA (and come back later).
See, reading from an unconfigured FPGA returns all-1, while reading from empty cores on a configured FPGA returns all-0. The consequence of this is that the HSM was probing the FPGA once on startup, filling its core table with 0xff, rendering the FPGA useless. Along the way, I put the FPGA core table in static memory, rather than malloc'ing it, because that's not so good in an embedded environment. But I kept the linked list, because that at least tells us what to do if HAL_STATIC_CORE_STATE_BLOCKS is 0.
-rw-r--r--Makefile2
-rw-r--r--core.c31
2 files changed, 31 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index b6597e1..190466b 100644
--- a/Makefile
+++ b/Makefile
@@ -30,6 +30,7 @@
# Number of static hash and HMAC state blocks to allocate.
# Numbers pulled out of a hat, just testing.
+STATIC_CORE_STATE_BLOCKS = 32
STATIC_HASH_STATE_BLOCKS = 10
STATIC_HMAC_STATE_BLOCKS = 4
STATIC_PKEY_STATE_BLOCKS = 6
@@ -185,6 +186,7 @@ LIBTFM_BLD ?= ${LIBTFM_SRC}
# directory.
CFLAGS += -g3 -Wall -std=c99 -Wno-strict-aliasing
+CFLAGS += -DHAL_STATIC_CORE_STATE_BLOCKS=${STATIC_CORE_STATE_BLOCKS}
CFLAGS += -DHAL_STATIC_HASH_STATE_BLOCKS=${STATIC_HASH_STATE_BLOCKS}
CFLAGS += -DHAL_STATIC_HMAC_STATE_BLOCKS=${STATIC_HMAC_STATE_BLOCKS}
CFLAGS += -DHAL_STATIC_PKEY_STATE_BLOCKS=${STATIC_PKEY_STATE_BLOCKS}
diff --git a/core.c b/core.c
index 0d7ed06..cfda754 100644
--- a/core.c
+++ b/core.c
@@ -52,6 +52,14 @@ struct hal_core {
struct hal_core *next;
};
+#ifndef HAL_STATIC_CORE_STATE_BLOCKS
+#define HAL_STATIC_CORE_STATE_BLOCKS 0
+#endif
+
+#if HAL_STATIC_CORE_STATE_BLOCKS > 0
+static hal_core_t core_table[HAL_STATIC_CORE_STATE_BLOCKS];
+#endif
+
/*
* Check whether a core's name matches a particular string. This is a
* bit nasty due to non-null-terminated fixed-length names.
@@ -91,16 +99,23 @@ static hal_core_t *probe_cores(void)
if (head != NULL)
return head;
- hal_core_t **tail = &head;
hal_core_t *core = NULL;
+ hal_core_t **tail = &head;
hal_error_t err = HAL_OK;
+#if HAL_STATIC_CORE_STATE_BLOCKS > 0
+ int n = 0;
+#endif
for (hal_addr_t addr = CORE_MIN; addr < CORE_MAX; addr += CORE_SIZE) {
+#if HAL_STATIC_CORE_STATE_BLOCKS > 0
+ core = &core_table[n];
+#else
if (core == NULL && (core = malloc(sizeof(hal_core_t))) == NULL) {
err = HAL_ERROR_ALLOCATION_FAILURE;
goto fail;
}
+#endif
memset(core, 0, sizeof(*core));
core->info.base = addr;
@@ -109,7 +124,7 @@ static hal_core_t *probe_cores(void)
(err = hal_io_read(core, ADDR_VERSION, (uint8_t *) core->info.version, 4)) != HAL_OK)
goto fail;
- if (core->info.name[0] == '\0')
+ if (core->info.name[0] == 0x00 || core->info.name[0] == 0xff)
continue;
for (int i = 0; i < sizeof(gaps)/sizeof(*gaps); i++) {
@@ -122,20 +137,32 @@ static hal_core_t *probe_cores(void)
*tail = core;
tail = &core->next;
core = NULL;
+
+#if HAL_STATIC_CORE_STATE_BLOCKS > 0
+ if (++n >= HAL_STATIC_CORE_STATE_BLOCKS)
+ break;
+#endif
}
+#if HAL_STATIC_CORE_STATE_BLOCKS > 0
+#else
if (core != NULL)
free(core);
+#endif
return head;
fail:
+#if HAL_STATIC_CORE_STATE_BLOCKS > 0
+ memset(core_table, 0, sizeof(core_table));
+#else
if (core != NULL)
free(core);
while ((core = head) != NULL) {
head = core->next;
free(core);
}
+#endif
return NULL;
}