Skip to content

Commit

Permalink
xtensa/esp32: write encrypt func implementation
Browse files Browse the repository at this point in the history
based on spec, 16 bytes alignment is checked.
  • Loading branch information
sdc-g committed Feb 24, 2025
1 parent 425ddc7 commit 8b395f9
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion arch/xtensa/src/esp32/esp32_spiflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ static ssize_t esp32_bread_decrypt(struct mtd_dev_s *dev,
#ifdef CONFIG_MTD_BYTE_WRITE
static ssize_t esp32_write(struct mtd_dev_s *dev, off_t offset,
size_t nbytes, const uint8_t *buffer);
static ssize_t esp32_write_encrypt(FAR struct mtd_dev_s *dev, off_t offset,
size_t nbytes, FAR const uint8_t *buffer);
#endif
static ssize_t esp32_bwrite(struct mtd_dev_s *dev, off_t startblock,
size_t nblocks, const uint8_t *buffer);
Expand Down Expand Up @@ -334,7 +336,7 @@ static struct esp32_spiflash_s g_esp32_spiflash1_encrypt =
.read = esp32_read_decrypt,
.ioctl = esp32_ioctl_encrypt,
#ifdef CONFIG_MTD_BYTE_WRITE
.write = NULL,
.write = esp32_write_encrypt,
#endif
.name = "esp32_mainflash_encrypt"
},
Expand Down Expand Up @@ -2091,6 +2093,74 @@ static ssize_t esp32_write(struct mtd_dev_s *dev, off_t offset,

return ret;
}

/****************************************************************************
* Name: esp32_write_encrypt
*
* Description:
* write data to SPI Flash at designated address by SPI Flash hardware
* encryption.
*
* Input Parameters:
* dev - ESP32 MTD device data
* offset - target address offset
* nbytes - data number
* buffer - data buffer pointer
*
* Returned Value:
* Writen bytes if success or a negative value if fail.
*
****************************************************************************/

static ssize_t esp32_write_encrypt(FAR struct mtd_dev_s *dev, off_t offset,
size_t nbytes, FAR const uint8_t *buffer)
{
ssize_t ret;
struct esp32_spiflash_s *priv = MTD2PRIV(dev);

ASSERT(buffer);

if ((offset % SPI_FLASH_ENCRYPT_MIN_SIZE) ||
(nbytes % SPI_FLASH_ENCRYPT_MIN_SIZE))
{
return -EINVAL;
}

#ifdef CONFIG_ESP32_SPIFLASH_DEBUG
finfo("esp32_write_encrypt(%p, 0x%x, %zu, %p)\n", dev, offset,
nbytes, buffer);
#endif

/* Acquire the mutex. */

ret = nxmutex_lock(&g_lock);
if (ret < 0)
{
return ret;
}

#ifdef CONFIG_ESP32_SPI_FLASH_SUPPORT_PSRAM_STACK
if (stack_is_psram())
{
ret = esp32_async_op(SPIFLASH_OP_CODE_ENCRYPT_WRITE, priv,
offset, buffer, nbytes, NULL, NULL, 0);
}
else
{
ret = esp32_writeblk_encrypted(priv, offset, buffer, nbytes);
}
#else
ret = esp32_writeblk_encrypted(priv, offset, buffer, nbytes);
#endif

nxmutex_unlock(&g_lock);

#ifdef CONFIG_ESP32_SPIFLASH_DEBUG
finfo("esp32_write_encrypt()=%d\n", ret);
#endif

return ret;
}
#endif

/****************************************************************************
Expand Down

0 comments on commit 8b395f9

Please sign in to comment.