From 01b59c41b8f4c38d6e2205a8ebc08dabf701640f Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Fri, 16 Sep 2016 08:14:28 -0400 Subject: Revised ks_flash. Compiles, not yet tested. --- projects/cli-test/mgmt-masterkey.c | 3 --- projects/hsm/mgmt-masterkey.c | 3 --- spiflash_n25q128.c | 13 ++++++++++--- spiflash_n25q128.h | 6 +++++- stm-fpgacfg.c | 2 +- stm-keystore.c | 2 +- stm-keystore.h | 2 ++ 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/projects/cli-test/mgmt-masterkey.c b/projects/cli-test/mgmt-masterkey.c index 9103612..623d19b 100644 --- a/projects/cli-test/mgmt-masterkey.c +++ b/projects/cli-test/mgmt-masterkey.c @@ -48,9 +48,6 @@ #include -#define KEK_LENGTH (256 / 8) - - static char * _status2str(const hal_error_t status) { switch (status) { diff --git a/projects/hsm/mgmt-masterkey.c b/projects/hsm/mgmt-masterkey.c index f154f92..9f5e4d0 100644 --- a/projects/hsm/mgmt-masterkey.c +++ b/projects/hsm/mgmt-masterkey.c @@ -48,9 +48,6 @@ #include -#define KEK_LENGTH (256 / 8) - - static char * _status2str(const hal_error_t status) { switch (status) { diff --git a/spiflash_n25q128.c b/spiflash_n25q128.c index 6e35a41..03273a8 100644 --- a/spiflash_n25q128.c +++ b/spiflash_n25q128.c @@ -298,7 +298,7 @@ inline int _wait_while_wip(struct spiflash_ctx *ctx, uint32_t timeout) } /* This function performs erasure if needed, and then writing of a number of pages to the flash memory */ -int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t *buf, const uint32_t len) +int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t *buf, const uint32_t len, const int auto_erase) { uint32_t page; @@ -306,8 +306,15 @@ int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t if ((offset % N25Q128_PAGE_SIZE) != 0) return -1; if ((len % N25Q128_PAGE_SIZE) != 0) return -2; - if ((offset % N25Q128_SECTOR_SIZE) == 0) { - /* first page in sector, need to erase sector */ + if (auto_erase && (offset % N25Q128_SECTOR_SIZE) == 0) { + /* + * first page in sector, need to erase sector + * + * So why do we only do this when the buffer starts on the + * sector, as opposed to performing this check for every page? + * Also, might be better to do this by subsectors rather than + * sectors. + */ if (! _wait_while_wip(ctx, 1000)) return -3; diff --git a/spiflash_n25q128.h b/spiflash_n25q128.h index fefcb0d..d4c82d5 100644 --- a/spiflash_n25q128.h +++ b/spiflash_n25q128.h @@ -45,6 +45,7 @@ #define N25Q128_COMMAND_READ_STATUS 0x05 #define N25Q128_COMMAND_WRITE_ENABLE 0x06 #define N25Q128_COMMAND_ERASE_SECTOR 0xD8 +#define N25Q128_COMMAND_ERASE_SUBSECTOR 0x20 #define N25Q128_COMMAND_PAGE_PROGRAM 0x02 #define N25Q128_PAGE_SIZE 0x100 // 256 @@ -53,6 +54,9 @@ #define N25Q128_SECTOR_SIZE 0x10000 // 65536 #define N25Q128_NUM_SECTORS 0x100 // 256 +#define N25Q128_SUBSECTOR_SIZE 0x1000 // 4096 +#define N25Q128_NUM_SUBSECTORS 0x1000 // 4096 + #define N25Q128_SPI_TIMEOUT 1000 #define N25Q128_ID_MANUFACTURER 0x20 @@ -71,6 +75,6 @@ extern int n25q128_read_page(struct spiflash_ctx *ctx, uint32_t page_offset, uin 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_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t *buf, const uint32_t len); +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); #endif /* __STM32_SPIFLASH_N25Q128_H */ diff --git a/stm-fpgacfg.c b/stm-fpgacfg.c index 6f6114d..1f15f88 100644 --- a/stm-fpgacfg.c +++ b/stm-fpgacfg.c @@ -48,7 +48,7 @@ int fpgacfg_check_id() int fpgacfg_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len) { - return n25q128_write_data(&fpgacfg_ctx, offset, buf, len); + return n25q128_write_data(&fpgacfg_ctx, offset, buf, len, 1); } void fpgacfg_access_control(enum fpgacfg_access_ctrl access) diff --git a/stm-keystore.c b/stm-keystore.c index 43e22fa..57827cf 100644 --- a/stm-keystore.c +++ b/stm-keystore.c @@ -52,7 +52,7 @@ int keystore_read_data(uint32_t offset, uint8_t *buf, const uint32_t len) int keystore_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len) { - return n25q128_write_data(&keystore_ctx, offset, buf, len); + return n25q128_write_data(&keystore_ctx, offset, buf, len, 0); } int keystore_erase_sectors(uint32_t start, uint32_t stop) diff --git a/stm-keystore.h b/stm-keystore.h index 0c04481..457dc05 100644 --- a/stm-keystore.h +++ b/stm-keystore.h @@ -41,6 +41,8 @@ #define KEYSTORE_PAGE_SIZE N25Q128_PAGE_SIZE #define KEYSTORE_SECTOR_SIZE N25Q128_SECTOR_SIZE #define KEYSTORE_NUM_SECTORS N25Q128_NUM_SECTORS +#define KEYSTORE_SUBSECTOR_SIZE N25Q128_SUBSECTOR_SIZE +#define KEYSTORE_NUM_SUBSECTORS N25Q128_NUM_SUBSECTORS /* Pins connected to the FPGA config memory (SPI flash) */ #define KSM_PROM_CS_N_Pin GPIO_PIN_0 -- cgit v1.2.3