[RFC PATCH 3/4] drivers: fpga: Add user-key encrypted FPGA Image loading support

From: Nava kishore Manne
Date: Tue May 04 2021 - 06:23:20 EST


This patch adds user-key encrypted FPGA Image loading support
to the framework.

Signed-off-by: Nava kishore Manne <nava.manne@xxxxxxxxxx>
---
drivers/fpga/fpga-mgr.c | 15 +++++++++++++++
drivers/fpga/of-fpga-region.c | 13 +++++++++++++
include/linux/fpga/fpga-mgr.h | 7 +++++++
3 files changed, 35 insertions(+)

diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
index b85bc47c91a9..3e79ab8cc86f 100644
--- a/drivers/fpga/fpga-mgr.c
+++ b/drivers/fpga/fpga-mgr.c
@@ -325,6 +325,7 @@ static int fpga_mgr_firmware_load(struct fpga_manager *mgr,
const char *image_name)
{
struct device *dev = &mgr->dev;
+ const struct firmware *enc_fw;
const struct firmware *fw;
int ret;

@@ -339,8 +340,22 @@ static int fpga_mgr_firmware_load(struct fpga_manager *mgr,
return ret;
}

+ if (info->encrypted_key_name) {
+ ret = request_firmware(&enc_fw, info->encrypted_key_name, dev);
+ if (ret) {
+ mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
+ dev_err(dev, "Error requesting firmware %s\n",
+ info->encrypted_key_name);
+ return ret;
+ }
+ info->enc_key_buf = enc_fw->data;
+ info->enc_key_buf_size = enc_fw->size;
+ }
+
ret = fpga_mgr_buf_load(mgr, info, fw->data, fw->size);

+ if (info->encrypted_key_name)
+ release_firmware(enc_fw);
release_firmware(fw);

return ret;
diff --git a/drivers/fpga/of-fpga-region.c b/drivers/fpga/of-fpga-region.c
index e405309baadc..19faa463d96e 100644
--- a/drivers/fpga/of-fpga-region.c
+++ b/drivers/fpga/of-fpga-region.c
@@ -195,6 +195,7 @@ static struct fpga_image_info *of_fpga_region_parse_ov(
{
struct device *dev = &region->dev;
struct fpga_image_info *info;
+ const char *encrypted_key_name;
const char *firmware_name;
int ret;

@@ -228,6 +229,18 @@ static struct fpga_image_info *of_fpga_region_parse_ov(
if (of_property_read_bool(overlay, "encrypted-fpga-config"))
info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;

+ if (of_property_read_bool(overlay, "encrypted-user-key-fpga-config")) {
+ if (!of_property_read_string(overlay, "encrypted-key-name",
+ &encrypted_key_name)) {
+ info->encrypted_key_name =
+ devm_kstrdup(dev, encrypted_key_name, GFP_KERNEL);
+ if (!info->encrypted_key_name)
+ return ERR_PTR(-ENOMEM);
+ }
+
+ info->flags |= FPGA_MGR_ENCRYPTED_USER_KEY_BITSTREAM;
+ }
+
if (!of_property_read_string(overlay, "firmware-name",
&firmware_name)) {
info->firmware_name = devm_kstrdup(dev, firmware_name,
diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
index 2bc3030a69e5..ac86f4398c3c 100644
--- a/include/linux/fpga/fpga-mgr.h
+++ b/include/linux/fpga/fpga-mgr.h
@@ -67,12 +67,15 @@ enum fpga_mgr_states {
* %FPGA_MGR_BITSTREAM_LSB_FIRST: SPI bitstream bit order is LSB first
*
* %FPGA_MGR_COMPRESSED_BITSTREAM: FPGA bitstream is compressed
+ * %FPGA_MGR_ENCRYPTED_USER_KEY_BITSTREAM: indicates bitstream is encrypted
+ * with user-key
*/
#define FPGA_MGR_PARTIAL_RECONFIG BIT(0)
#define FPGA_MGR_EXTERNAL_CONFIG BIT(1)
#define FPGA_MGR_ENCRYPTED_BITSTREAM BIT(2)
#define FPGA_MGR_BITSTREAM_LSB_FIRST BIT(3)
#define FPGA_MGR_COMPRESSED_BITSTREAM BIT(4)
+#define FPGA_MGR_ENCRYPTED_USER_KEY_BITSTREAM BIT(5)

/**
* struct fpga_image_info - information specific to a FPGA image
@@ -82,6 +85,7 @@ enum fpga_mgr_states {
* @config_complete_timeout_us: maximum time for FPGA to switch to operating
* status in the write_complete op.
* @firmware_name: name of FPGA image firmware file
+ * @encrypted_key_name: name of the FPGA image encrypted user-key file
* @sgt: scatter/gather table containing FPGA image
* @buf: contiguous buffer containing FPGA image
* @count: size of buf
@@ -95,8 +99,11 @@ struct fpga_image_info {
u32 disable_timeout_us;
u32 config_complete_timeout_us;
char *firmware_name;
+ char *encrypted_key_name;
struct sg_table *sgt;
+ const char *enc_key_buf;
const char *buf;
+ size_t enc_key_buf_size;
size_t count;
int region_id;
struct device *dev;
--
2.17.1