[v2] firmware_loader: Add debug message with checksum for FW file

Message ID 20230228185507.1729059-1-amadeuszx.slawinski@linux.intel.com
State New
Headers
Series [v2] firmware_loader: Add debug message with checksum for FW file |

Commit Message

Amadeusz Sławiński Feb. 28, 2023, 6:55 p.m. UTC
  Enable dynamic-debug logging of firmware filenames and SHA256 checksums
to clearly identify the firmware files that are loaded by the system.

Example output:
[   34.944619] firmware_class:_request_firmware: i915 0000:00:02.0: Loaded FW: i915/kbl_dmc_ver1_04.bin, sha256: 2cde41c3e5ad181423bcc3e98ff9c49f743c88f18646af4d0b3c3a9664b831a1
[   48.155884] firmware_class:_request_firmware: snd_soc_avs 0000:00:1f.3: Loaded FW: intel/avs/cnl/dsp_basefw.bin, sha256: 43f6ac1b066e9bd0423d914960fbbdccb391af27d2b1da1085eee3ea8df0f357
[   49.579540] firmware_class:_request_firmware: snd_soc_avs 0000:00:1f.3: Loaded FW: intel/avs/rt274-tplg.bin, sha256: 4b3580da96dc3d2c443ba20c6728d8b665fceb3ed57223c3a57582bbad8e2413
[   49.798196] firmware_class:_request_firmware: snd_soc_avs 0000:00:1f.3: Loaded FW: intel/avs/hda-8086280c-tplg.bin, sha256: 5653172579b2be1b51fd69f5cf46e2bac8d63f2a1327924311c13b2f1fe6e601
[   49.859627] firmware_class:_request_firmware: snd_soc_avs 0000:00:1f.3: Loaded FW: intel/avs/dmic-tplg.bin, sha256: 00fb7fbdb74683333400d7e46925dae60db448b88638efcca0b30215db9df63f

Reviewed-by: Cezary Rojewski <cezary.rojewski@intel.com>
Signed-off-by: Amadeusz Sławiński <amadeuszx.slawinski@linux.intel.com>
---

Changes in v2:
 * allocate buffers (Greg)
 * introduce CONFIG_ option to allow for CONFIG_CRYPTO and CONFIG_CRYPTO_SHA256
dependencies without introducing circular dependency (Greg)
 * add new line between includes and function name (Cezary)

---
 drivers/base/firmware_loader/Kconfig | 10 ++++++
 drivers/base/firmware_loader/main.c  | 48 +++++++++++++++++++++++++++-
 2 files changed, 57 insertions(+), 1 deletion(-)
  

Comments

kernel test robot Feb. 28, 2023, 2:59 p.m. UTC | #1
Hi Amadeusz,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on driver-core/driver-core-testing]
[also build test ERROR on driver-core/driver-core-next driver-core/driver-core-linus linus/master v6.2 next-20230228]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Amadeusz-S-awi-ski/firmware_loader-Add-debug-message-with-checksum-for-FW-file/20230228-185558
patch link:    https://lore.kernel.org/r/20230228185507.1729059-1-amadeuszx.slawinski%40linux.intel.com
patch subject: [PATCH v2] firmware_loader: Add debug message with checksum for FW file
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20230228/202302282231.g7ZUHJUm-lkp@intel.com/config)
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/229a0f94cfda465aff751f6a0395b8d6c6688acd
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Amadeusz-S-awi-ski/firmware_loader-Add-debug-message-with-checksum-for-FW-file/20230228-185558
        git checkout 229a0f94cfda465aff751f6a0395b8d6c6688acd
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=x86_64 olddefconfig
        make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Link: https://lore.kernel.org/oe-kbuild-all/202302282231.g7ZUHJUm-lkp@intel.com/

All errors (new ones prefixed by >>):

   ld: drivers/base/firmware_loader/main.o: in function `fw_log_firmware_info':
>> drivers/base/firmware_loader/main.c:805: undefined reference to `crypto_alloc_shash'
>> ld: drivers/base/firmware_loader/main.c:817: undefined reference to `crypto_shash_digest'
   ld: drivers/base/firmware_loader/main.o: in function `crypto_free_shash':
>> include/crypto/hash.h:736: undefined reference to `crypto_destroy_tfm'


vim +805 drivers/base/firmware_loader/main.c

   797	
   798	static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
   799	{
   800		struct shash_desc *shash;
   801		struct crypto_shash *alg;
   802		u8 *sha256buf;
   803		char *outbuf;
   804	
 > 805		alg = crypto_alloc_shash("sha256", 0, 0);
   806		if (!alg)
   807			return;
   808	
   809		sha256buf = kmalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
   810		outbuf = kmalloc(SHA256_BLOCK_SIZE + 1, GFP_KERNEL);
   811		shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(alg), GFP_KERNEL);
   812		if (!sha256buf || !outbuf || !shash)
   813			goto out_free;
   814	
   815		shash->tfm = alg;
   816	
 > 817		if (crypto_shash_digest(shash, fw->data, fw->size, sha256buf) < 0)
   818			goto out_shash;
   819	
   820		for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
   821			sprintf(&outbuf[i * 2], "%02x", sha256buf[i]);
   822		outbuf[SHA256_BLOCK_SIZE] = 0;
   823		dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
   824	
   825	out_shash:
   826		crypto_free_shash(alg);
   827	out_free:
   828		kfree(shash);
   829		kfree(outbuf);
   830		kfree(sha256buf);
   831	}
   832	#else
   833	static void fw_log_firmware_info(const struct firmware *fw, const char *name,
   834					 struct device *device)
   835	{}
   836	#endif
   837
  

Patch

diff --git a/drivers/base/firmware_loader/Kconfig b/drivers/base/firmware_loader/Kconfig
index 5166b323a0f8..70524113c9fb 100644
--- a/drivers/base/firmware_loader/Kconfig
+++ b/drivers/base/firmware_loader/Kconfig
@@ -3,6 +3,7 @@  menu "Firmware loader"
 
 config FW_LOADER
 	tristate "Firmware loading facility" if EXPERT
+	select FW_LOADER_DEBUG if DYNAMIC_DEBUG
 	default y
 	help
 	  This enables the firmware loading facility in the kernel. The kernel
@@ -24,6 +25,15 @@  config FW_LOADER
 	  You also want to be sure to enable this built-in if you are going to
 	  enable built-in firmware (CONFIG_EXTRA_FIRMWARE).
 
+config FW_LOADER_DEBUG
+	bool "Additional debug logs"
+	depends on CRYPTO
+	depends on CRYPTO_SHA256
+	default FW_LOADER
+	help
+	  Select this if additional information about loaded firmware file in
+	  form of sha256sum should be dumped.
+
 if FW_LOADER
 
 config FW_LOADER_PAGED_BUF
diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
index 017c4cdb219e..b2c292ca95e8 100644
--- a/drivers/base/firmware_loader/main.c
+++ b/drivers/base/firmware_loader/main.c
@@ -791,6 +791,50 @@  static void fw_abort_batch_reqs(struct firmware *fw)
 	mutex_unlock(&fw_lock);
 }
 
+#if defined(CONFIG_FW_LOADER_DEBUG)
+#include <crypto/hash.h>
+#include <crypto/sha2.h>
+
+static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
+{
+	struct shash_desc *shash;
+	struct crypto_shash *alg;
+	u8 *sha256buf;
+	char *outbuf;
+
+	alg = crypto_alloc_shash("sha256", 0, 0);
+	if (!alg)
+		return;
+
+	sha256buf = kmalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
+	outbuf = kmalloc(SHA256_BLOCK_SIZE + 1, GFP_KERNEL);
+	shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(alg), GFP_KERNEL);
+	if (!sha256buf || !outbuf || !shash)
+		goto out_free;
+
+	shash->tfm = alg;
+
+	if (crypto_shash_digest(shash, fw->data, fw->size, sha256buf) < 0)
+		goto out_shash;
+
+	for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
+		sprintf(&outbuf[i * 2], "%02x", sha256buf[i]);
+	outbuf[SHA256_BLOCK_SIZE] = 0;
+	dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
+
+out_shash:
+	crypto_free_shash(alg);
+out_free:
+	kfree(shash);
+	kfree(outbuf);
+	kfree(sha256buf);
+}
+#else
+static void fw_log_firmware_info(const struct firmware *fw, const char *name,
+				 struct device *device)
+{}
+#endif
+
 /* called from request_firmware() and request_firmware_work_func() */
 static int
 _request_firmware(const struct firmware **firmware_p, const char *name,
@@ -861,11 +905,13 @@  _request_firmware(const struct firmware **firmware_p, const char *name,
 	revert_creds(old_cred);
 	put_cred(kern_cred);
 
- out:
+out:
 	if (ret < 0) {
 		fw_abort_batch_reqs(fw);
 		release_firmware(fw);
 		fw = NULL;
+	} else {
+		fw_log_firmware_info(fw, name, device);
 	}
 
 	*firmware_p = fw;