mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wendy Liang <wendy.liang@xilinx.com>
To: <bjorn.andersson@linaro.org>
Cc: <linux-remoteproc@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>,
	"Wendy Liang" <wendy.liang@xilinx.com>,
	Wendy Liang <jliang@xilinx.com>,
	Michal Simek <michal.simek@xilinx.com>
Subject: [RFC LINUX PATCH 06/19] remoteproc: elf loader: Add get checksum firmware implementation
Date: Fri, 24 Mar 2017 12:22:22 -0700	[thread overview]
Message-ID: <1490383355-23176-7-git-send-email-jliang@xilinx.com> (raw)
In-Reply-To: <1490383355-23176-1-git-send-email-jliang@xilinx.com>

From: Wendy Liang <wendy.liang@xilinx.com>

Add get_chksum() implementation to calculate the checksum of
the loadable sections of the firmware.

Signed-off-by: Wendy Liang <jliang@xilinx.com>
Signed-off-by: Michal Simek <michal.simek@xilinx.com>
---
 drivers/remoteproc/Kconfig                 |   1 +
 drivers/remoteproc/remoteproc_elf_loader.c | 109 +++++++++++++++++++++++++++++
 drivers/remoteproc/remoteproc_internal.h   |   3 +
 3 files changed, 113 insertions(+)

diff --git a/drivers/remoteproc/Kconfig b/drivers/remoteproc/Kconfig
index 65f86bc..d832f3b 100644
--- a/drivers/remoteproc/Kconfig
+++ b/drivers/remoteproc/Kconfig
@@ -7,6 +7,7 @@ config REMOTEPROC
 	select FW_LOADER
 	select VIRTIO
 	select VIRTUALIZATION
+	select CRYPTO
 	help
 	  Support for remote processors (such as DSP coprocessors). These
 	  are mainly used on embedded systems.
diff --git a/drivers/remoteproc/remoteproc_elf_loader.c b/drivers/remoteproc/remoteproc_elf_loader.c
index c523983..f508083 100644
--- a/drivers/remoteproc/remoteproc_elf_loader.c
+++ b/drivers/remoteproc/remoteproc_elf_loader.c
@@ -29,6 +29,7 @@
 #include <linux/firmware.h>
 #include <linux/remoteproc.h>
 #include <linux/elf.h>
+#include <crypto/hash.h>
 
 #include "remoteproc_internal.h"
 
@@ -328,10 +329,118 @@ u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
 	return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
 }
 
+/**
+ * rproc_elf_get_chksum() - calcuate checksum of the loadable section
+ * @rproc: the rproc handle
+ * @fw: the ELF firmware image
+ * @algo: name of the checksum algorithm
+ * @chksum: checksum
+ * @output_size: size of the checksum
+ *
+ * This function calculate the checksum of the loadable secitons
+ * of the specified firmware.
+ *
+ * Returns 0 for success, negative value for failure.
+ */
+static int
+rproc_elf_get_chksum(struct rproc *rproc, const struct firmware *fw,
+		char *algo, u8 *chksum, int output_size)
+{
+	int ret, i;
+	struct device *dev = &rproc->dev;
+	struct crypto_shash *tfm;
+	struct shash_desc *desc;
+	int algo_len = 0;
+	struct elf32_hdr *ehdr;
+	struct elf32_phdr *phdr;
+	const u8 *elf_data = fw->data;
+
+	memset(chksum, 0, output_size);
+	/* If no algo is specified, default it to "sha256" */
+	if (!strlen(algo))
+		sprintf(algo, "sha256");
+	ret = crypto_has_alg(algo, 0, 0);
+	if (!ret) {
+		dev_err(dev, "failed to find crypto algo: %s.\n", algo);
+		return -EINVAL;
+	}
+	dev_dbg(dev, "firmware checksum algo: %s.\n", algo);
+	tfm =  crypto_alloc_shash(algo, 0, 0);
+	if (!tfm) {
+		dev_err(dev, "failed to allocate shash.\n");
+		return -ENOMEM;
+	}
+	algo_len = crypto_shash_digestsize(tfm);
+	if (algo_len > output_size) {
+		dev_err(dev,
+			"algo digest size %d is larger expected %d.\n",
+			algo_len, output_size);
+		return -EINVAL;
+	}
+	desc = kzalloc(sizeof(*desc) + algo_len, GFP_KERNEL);
+	if (!desc)
+		return -ENOMEM;
+	desc->tfm = tfm;
+	ret = crypto_shash_init(desc);
+	if (ret) {
+		dev_err(dev, "failed crypto %s initialization.\n", algo);
+		return ret;
+	}
+
+	ehdr = (struct elf32_hdr *)elf_data;
+	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
+
+	/* go through the available ELF segments */
+	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
+		u32 memsz = phdr->p_memsz;
+		u32 filesz = phdr->p_filesz;
+		u32 offset = phdr->p_offset;
+
+		if (phdr->p_type != PT_LOAD)
+			continue;
+
+		if (filesz > memsz) {
+			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+				filesz, memsz);
+			ret = -EINVAL;
+			break;
+		}
+
+		if (offset + filesz > fw->size) {
+			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+				offset + filesz, fw->size);
+			ret = -EINVAL;
+			break;
+		}
+
+		/* put the segment where the remote processor expects it */
+		if (phdr->p_filesz) {
+			ret = crypto_shash_update(desc,
+				elf_data + offset, filesz);
+			if (ret) {
+				dev_err(dev,
+				"Failed to update fw crypto digest state at offset 0x%x, size 0x%x.\n",
+				offset, filesz);
+				return ret;
+			}
+		}
+
+	}
+	ret = crypto_shash_final(desc, chksum);
+	crypto_free_shash(tfm);
+	kfree(desc);
+	if (ret) {
+		dev_err(dev, "failed to finalize checksum of firmware.\n");
+		return ret;
+	}
+	return ret;
+}
+
 const struct rproc_fw_ops rproc_elf_fw_ops = {
 	.load = rproc_elf_load_segments,
 	.find_rsc_table = rproc_elf_find_rsc_table,
 	.find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
 	.sanity_check = rproc_elf_sanity_check,
+	.get_chksum = rproc_elf_get_chksum,
 	.get_boot_addr = rproc_elf_get_boot_addr
 };
diff --git a/drivers/remoteproc/remoteproc_internal.h b/drivers/remoteproc/remoteproc_internal.h
index 1e9e5b3..f03e07a 100644
--- a/drivers/remoteproc/remoteproc_internal.h
+++ b/drivers/remoteproc/remoteproc_internal.h
@@ -33,6 +33,7 @@
  *			expects to find it
  * @sanity_check:	sanity check the fw image
  * @get_boot_addr:	get boot address to entry point specified in firmware
+ * @get_chksum:		get checksum of the loadable sections of the firmware
  */
 struct rproc_fw_ops {
 	struct resource_table *(*find_rsc_table)(struct rproc *rproc,
@@ -43,6 +44,8 @@ struct rproc_fw_ops {
 	int (*load)(struct rproc *rproc, const struct firmware *fw);
 	int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
 	u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
+	int (*get_chksum)(struct rproc *rproc, const struct firmware *fw,
+			char *algo, u8 *chksum, int output_size);
 };
 
 /* from remoteproc_core.c */
-- 
1.9.1

  parent reply	other threads:[~2017-03-24 19:34 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-24 19:22 [RFC LINUX PATCH 00/19] Allow master to restart without restarting slave Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 01/19] remoteproc: Add is_runinng to remoteproc ops Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 02/19] remtoeproc: Add an operation to check if remote is running Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 03/19] remoteproc: Add a running independent state Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 04/19] remoteproc: sysfs: Add " Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 05/19] remoteproc: check if remote is running the fw before reload fw Wendy Liang
2017-03-24 19:22 ` Wendy Liang [this message]
2017-03-24 19:22 ` [RFC LINUX PATCH 07/19] remoteproc: Add firmware checksum resource entry Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 08/19] remoteproc: add a function to set fw checksum rsc Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 09/19] remoteproc: check firmware checksum in rproc_is_running_fw() Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 10/19] remoteproc: Add rproc resource with id struct Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 11/19] remoteproc: Add rproc_idr_alloc/remove wrapper Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 12/19] remoteproc: use rproc_id_rsc for data got from idr_find() Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 13/19] remoteproc: add config_wait_complete to rproc_vdev Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 14/19] remoteproc virtio: handle rproc vdev notification Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 15/19] remoteproc: virtio: rename rproc_virtio_notify to rproc_vq_notify Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 16/19] remoteproc: virtio: use rproc_virtio_notify() to notify vdev changes Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 17/19] remoteproc: virtio: kick in reset virtio Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 18/19] remoteproc: shutdown: set state to OFFLINE after stopping remote Wendy Liang
2017-03-24 19:22 ` [RFC LINUX PATCH 19/19] remoteproc: virtio: idr_alloc for rvdev Wendy Liang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1490383355-23176-7-git-send-email-jliang@xilinx.com \
    --to=wendy.liang@xilinx.com \
    --cc=bjorn.andersson@linaro.org \
    --cc=jliang@xilinx.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-remoteproc@vger.kernel.org \
    --cc=michal.simek@xilinx.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®