aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--projects/cli-test/cli-test.c20
-rw-r--r--projects/hsm/hsm.c8
-rw-r--r--spiflash_n25q128.c43
-rw-r--r--spiflash_n25q128.h1
-rw-r--r--stm-keystore.c25
-rw-r--r--stm-keystore.h1
6 files changed, 80 insertions, 18 deletions
diff --git a/projects/cli-test/cli-test.c b/projects/cli-test/cli-test.c
index d04371b..17b85cd 100644
--- a/projects/cli-test/cli-test.c
+++ b/projects/cli-test/cli-test.c
@@ -67,3 +67,23 @@ main()
/* NOT REACHED */
Error_Handler();
}
+
+
+/*
+ * Dummy to solve link problem. Not obvious to me that a program
+ * called "cli-test" should be duplicating all of the HSM keystore
+ * logic, let alone that it should be doing it badly, but, whatever.
+ *
+ * We could just copy the sdram_malloc() code from hsm.c, but since
+ * one of the other commands linked into cli-test goes merrily stomping
+ * all over the entire SDRAM chip, that might not work out so well.
+ *
+ * Issue deferred until somebody cares.
+ */
+
+#warning hal_allocate_static_memory() stubbed out in cli-test, see source code
+
+void *hal_allocate_static_memory(const size_t size)
+{
+ return NULL;
+}
diff --git a/projects/hsm/hsm.c b/projects/hsm/hsm.c
index 862e718..ac0f23a 100644
--- a/projects/hsm/hsm.c
+++ b/projects/hsm/hsm.c
@@ -217,6 +217,14 @@ static uint8_t *sdram_malloc(size_t size)
return p;
}
+/* Implement static memory allocation for libhal over sdram_malloc().
+ * Once again, there's only alloc, not free. */
+
+void *hal_allocate_static_memory(const size_t size)
+{
+ return sdram_malloc(size);
+}
+
#if NUM_RPC_TASK > 1
/* Critical section start/end, currently used just for hal_core_alloc/_free.
*/
diff --git a/spiflash_n25q128.c b/spiflash_n25q128.c
index 03273a8..728db7e 100644
--- a/spiflash_n25q128.c
+++ b/spiflash_n25q128.c
@@ -43,6 +43,14 @@
#define _n25q128_deselect(ctx) HAL_GPIO_WritePin(ctx->cs_n_port, ctx->cs_n_pin, GPIO_PIN_SET);
+#define N25Q128_NUM_BYTES (N25Q128_PAGE_SIZE * N25Q128_NUM_PAGES)
+
+#if N25Q128_SECTOR_SIZE * N25Q128_NUM_SECTORS != N25Q128_NUM_BYTES || \
+ N25Q128_SUBSECTOR_SIZE * N25Q128_NUM_SUBSECTORS != N25Q128_NUM_BYTES
+#error Inconsistant definitions for pages / sectors / subsectors
+#endif
+
+
int _n25q128_get_wel_flag(struct spiflash_ctx *ctx);
@@ -209,17 +217,17 @@ int n25q128_get_wip_flag(struct spiflash_ctx *ctx)
}
-int n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset)
+static int n25q128_erase_something(struct spiflash_ctx *ctx, uint8_t command, uint32_t byte_offset)
{
+ // check offset
+ if (byte_offset >= N25Q128_NUM_BYTES) return 0;
+
// tx buffer
uint8_t spi_tx[4];
// result
HAL_StatusTypeDef ok;
- // check offset
- if (sector_offset >= N25Q128_NUM_SECTORS) return 0;
-
// enable writing
spi_tx[0] = N25Q128_COMMAND_WRITE_ENABLE;
@@ -236,14 +244,11 @@ int n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset)
int wel = _n25q128_get_wel_flag(ctx);
if (wel != 1) return 0;
- // calculate byte address
- sector_offset *= N25Q128_SECTOR_SIZE;
-
- // send ERASE SUBSECTOR command
- spi_tx[0] = N25Q128_COMMAND_ERASE_SECTOR;
- spi_tx[1] = (uint8_t)(sector_offset >> 16);
- spi_tx[2] = (uint8_t)(sector_offset >> 8);
- spi_tx[3] = (uint8_t)(sector_offset >> 0);
+ // send command (ERASE SECTOR or ERASE SUBSECTOR)
+ spi_tx[0] = command;
+ spi_tx[1] = (uint8_t)(byte_offset >> 16);
+ spi_tx[2] = (uint8_t)(byte_offset >> 8);
+ spi_tx[3] = (uint8_t)(byte_offset >> 0);
// activate, send command, deselect
_n25q128_select(ctx);
@@ -259,6 +264,20 @@ int n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset)
}
+int n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset)
+{
+ return n25q128_erase_something(ctx, N25Q128_COMMAND_ERASE_SECTOR,
+ sector_offset * N25Q128_SECTOR_SIZE);
+}
+
+
+int n25q128_erase_subsector(struct spiflash_ctx *ctx, uint32_t subsector_offset)
+{
+ return n25q128_erase_something(ctx, N25Q128_COMMAND_ERASE_SUBSECTOR,
+ subsector_offset * N25Q128_SUBSECTOR_SIZE);
+}
+
+
int _n25q128_get_wel_flag(struct spiflash_ctx *ctx)
{
// tx, rx buffers
diff --git a/spiflash_n25q128.h b/spiflash_n25q128.h
index d4c82d5..c696911 100644
--- a/spiflash_n25q128.h
+++ b/spiflash_n25q128.h
@@ -74,6 +74,7 @@ extern int n25q128_get_wip_flag(struct spiflash_ctx *ctx);
extern int n25q128_read_page(struct spiflash_ctx *ctx, uint32_t page_offset, uint8_t *page_buffer);
extern int n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uint8_t *page_buffer);
extern int n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset);
+extern int n25q128_erase_subsector(struct spiflash_ctx *ctx, uint32_t subsector_offset);
extern int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t *buf, const uint32_t len, const int auto_erase);
extern int n25q128_read_data(struct spiflash_ctx *ctx, uint32_t offset, uint8_t *buf, const uint32_t len);
diff --git a/stm-keystore.c b/stm-keystore.c
index 57827cf..b9900cb 100644
--- a/stm-keystore.c
+++ b/stm-keystore.c
@@ -55,14 +55,15 @@ int keystore_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len)
return n25q128_write_data(&keystore_ctx, offset, buf, len, 0);
}
-int keystore_erase_sectors(uint32_t start, uint32_t stop)
+static int keystore_erase_something(uint32_t start, uint32_t stop, uint32_t limit,
+ int (*eraser)(uint32_t, uint32_t))
{
- uint32_t sector;
+ uint32_t someting;
- if (start > N25Q128_NUM_SECTORS) return -2;
- if (stop > N25Q128_NUM_SECTORS || stop < start) return -3;
+ if (start > limit) return -2;
+ if (stop > limit || stop < start) return -3;
- for (sector = start; sector <= stop; sector++) {
+ for (someting = start; someting <= stop; someting++) {
int timeout = 200; /* times 10ms = 2 seconds timeout */
while (timeout--) {
int i = n25q128_get_wip_flag(&keystore_ctx);
@@ -72,9 +73,21 @@ int keystore_erase_sectors(uint32_t start, uint32_t stop)
}
if (! timeout) return 0;
- if (! n25q128_erase_sector(&keystore_ctx, sector)) {
+ if (! eraser(&keystore_ctx, someting)) {
return -1;
}
}
return 1;
}
+
+int keystore_erase_sectors(uint32_t start, uint32_t stop)
+{
+ return keystore_erase_something(start, stop, N25Q128_NUM_SECTORS,
+ n25q128_erase_sector);
+}
+
+int keystore_erase_subsectors(uint32_t start, uint32_t stop)
+{
+ return keystore_erase_something(start, stop, N25Q128_NUM_SUBSECTORS,
+ n25q128_erase_subsector);
+}
diff --git a/stm-keystore.h b/stm-keystore.h
index 457dc05..9054db5 100644
--- a/stm-keystore.h
+++ b/stm-keystore.h
@@ -60,5 +60,6 @@ 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(uint32_t start, uint32_t stop);
+extern int keystore_erase_subsectors(uint32_t start, uint32_t stop);
#endif /* __STM32_KEYSTORE_H */