From b97ba86151439a187c047353dc75d3cf2020043d Mon Sep 17 00:00:00 2001 From: Fredrik Thulin Date: Wed, 8 Jun 2016 18:46:06 +0200 Subject: Test code for ks_flash in corresponding branch of libhal. --- Makefile | 4 ++-- projects/cli-test/Makefile | 3 ++- projects/cli-test/mgmt-show.c | 36 +++++++++++++++++++++++++++++++++++- stm-keystore.c | 13 ++++++++----- stm-keystore.h | 3 ++- stm-sdram.c | 2 +- 6 files changed, 50 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index b96ea64..0cf1c4e 100644 --- a/Makefile +++ b/Makefile @@ -115,7 +115,7 @@ $(MBED_DIR)/libstmf4.a: board-test: $(BOARD_OBJS) $(LIBS) $(MAKE) -C projects/board-test -cli-test: $(BOARD_OBJS) $(LIBS) $(LIBCLI_DIR)/libcli.a +cli-test: $(BOARD_OBJS) $(LIBS) $(LIBCLI_DIR)/libcli.a $(LIBHAL_DIR)/libhal.a $(MAKE) -C projects/cli-test $(RTOS_DIR)/librtos.a: @@ -128,7 +128,7 @@ $(LIBTFM_DIR)/libtfm.a: $(MAKE) -C $(LIBTFM_DIR) PREFIX=$(PREFIX) $(LIBHAL_DIR)/libhal.a: $(LIBTFM_DIR)/libtfm.a - $(MAKE) -C $(LIBHAL_DIR) IO_BUS=fmc RPC_SERVER=yes RPC_TRANSPORT=serial KS=volatile libhal.a + $(MAKE) -C $(LIBHAL_DIR) IO_BUS=fmc RPC_SERVER=yes RPC_TRANSPORT=serial KS=flash libhal.a $(LIBCLI_DIR)/libcli.a: $(MAKE) -C $(LIBCLI_DIR) diff --git a/projects/cli-test/Makefile b/projects/cli-test/Makefile index acf2720..16de9f3 100644 --- a/projects/cli-test/Makefile +++ b/projects/cli-test/Makefile @@ -2,8 +2,9 @@ TEST = cli-test OBJS = crc32.o test_sdram.o mgmt-cli.o mgmt-dfu.c mgmt-fpga.c mgmt-misc.c mgmt-show.c mgmt-test.c -CFLAGS += -I$(LIBCLI_DIR) +CFLAGS += -I$(LIBCLI_DIR) -I$(LIBHAL_DIR) LIBS += $(LIBCLI_DIR)/libcli.a +LIBS += $(LIBHAL_DIR)/libhal.a all: $(TEST:=.elf) diff --git a/projects/cli-test/mgmt-show.c b/projects/cli-test/mgmt-show.c index 3ae196e..ba2a327 100644 --- a/projects/cli-test/mgmt-show.c +++ b/projects/cli-test/mgmt-show.c @@ -32,6 +32,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#define HAL_OK CMSIS_HAL_OK + #include "stm-init.h" #include "stm-keystore.h" #include "stm-fpgacfg.h" @@ -40,6 +42,15 @@ #include "mgmt-cli.h" #include "mgmt-show.h" +/* Rename both CMSIS HAL_OK and libhal HAL_OK to disambiguate */ +#undef HAL_OK +#define LIBHAL_OK HAL_OK +#include "hal.h" + +#define HAL_STATIC_PKEY_STATE_BLOCKS 6 +#include "hal_internal.h" +#undef HAL_OK + #include @@ -104,7 +115,7 @@ int cmd_show_keystore_data(struct cli_def *cli, const char *command, char *argv[ } } else { cli_print(cli, "Erasing first sector since all the first 8 bytes are tombstones"); - if ((i = keystore_erase_sectors(1)) != 1) { + if ((i = keystore_erase_sectors(1, 1)) != 1) { cli_print(cli, "Failed erasing the first sector: %li", i); return CLI_ERROR; } @@ -114,6 +125,28 @@ int cmd_show_keystore_data(struct cli_def *cli, const char *command, char *argv[ return CLI_OK; } +int cmd_show_keystore_keys(struct cli_def *cli, const char *command, char *argv[], int argc) +{ + const hal_ks_keydb_t *db; + + cli_print(cli, "Get keydb"); + db = hal_ks_get_keydb(); + + if (db == NULL) { + cli_print(cli, "Could not get a keydb from libhal"); + return CLI_OK; + } + cli_print(cli, "Got keydb"); + + cli_print(cli, "Sizeof db->keys is %i, sizeof one key is %i", sizeof(db->keys), sizeof(*db->keys)); + + for (int i = 0; i < sizeof(db->keys)/sizeof(*db->keys); i++) { + cli_print(cli, "key %i, in use 0x%x, ks_internal %i", i, db->keys[i].in_use, db->keys[i].ks_internal); + } + + return CLI_OK; +} + void configure_cli_show(struct cli_def *cli) { /* show */ @@ -130,4 +163,5 @@ void configure_cli_show(struct cli_def *cli) /* show keystore status*/ cli_command_node(show_keystore, status, "Show status of the keystore memory"); cli_command_node(show_keystore, data, "Show the first page of the keystore memory"); + cli_command_node(show_keystore, keys, "List the keys in the keystore database"); } diff --git a/stm-keystore.c b/stm-keystore.c index 74826d0..63bf4be 100644 --- a/stm-keystore.c +++ b/stm-keystore.c @@ -55,10 +55,14 @@ int keystore_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len) return n25q128_write_data(&keystore_ctx, offset, buf, len); } -int keystore_erase_sectors(int num) +int keystore_erase_sectors(uint32_t start, uint32_t stop) { - if (num > N25Q128_NUM_SECTORS || num < 0) num = N25Q128_NUM_SECTORS; - while (num) { + uint32_t sector; + + if (start > N25Q128_NUM_SECTORS || start < 0) return -2; + if (stop > N25Q128_NUM_SECTORS || stop < 0 || stop > start) return -3; + + for (sector = start; sector <= stop; sector++) { int timeout = 200; /* times 10ms = 2 seconds timeout */ while (timeout--) { int i = n25q128_get_wip_flag(&keystore_ctx); @@ -68,10 +72,9 @@ int keystore_erase_sectors(int num) } if (! timeout) return 0; - if (! n25q128_erase_sector(&keystore_ctx, num - 1)) { + if (! n25q128_erase_sector(&keystore_ctx, sector)) { return -1; } - num--; } return 1; } diff --git a/stm-keystore.h b/stm-keystore.h index 2c493d2..0c04481 100644 --- a/stm-keystore.h +++ b/stm-keystore.h @@ -40,6 +40,7 @@ #define KEYSTORE_PAGE_SIZE N25Q128_PAGE_SIZE #define KEYSTORE_SECTOR_SIZE N25Q128_SECTOR_SIZE +#define KEYSTORE_NUM_SECTORS N25Q128_NUM_SECTORS /* Pins connected to the FPGA config memory (SPI flash) */ #define KSM_PROM_CS_N_Pin GPIO_PIN_0 @@ -56,6 +57,6 @@ extern SPI_HandleTypeDef hspi_keystore; extern int keystore_check_id(void); extern int keystore_read_data(uint32_t offset, uint8_t *buf, const uint32_t len); extern int keystore_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len); -extern int keystore_erase_sectors(int num); +extern int keystore_erase_sectors(uint32_t start, uint32_t stop); #endif /* __STM32_KEYSTORE_H */ diff --git a/stm-sdram.c b/stm-sdram.c index 0ec8065..22fd8ab 100644 --- a/stm-sdram.c +++ b/stm-sdram.c @@ -51,7 +51,7 @@ HAL_StatusTypeDef sdram_init(void) static int initialized = 0; if (initialized) { - return; + return HAL_OK; } initialized = 1; -- cgit v1.2.3