Skip to contentSkip to conversation

firmware_loader: Add debug message with checksum for FW file

PATCH Superseded Amadeusz Sławiński 2 participants 5 messages Feb 24, 2023 – Feb 27, 2023

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware_loader: Add debug message with checksum for FW file

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
---
 drivers/base/firmware_loader/main.c | 45 ++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
drivers/base/firmware_loader/main.c +44 -1
index 017c4cdb219e..a6e1fb10763d 100644
@@ -791,6 +791,47 @@ static void fw_abort_batch_reqs(struct firmware *fw)
 	mutex_unlock(&fw_lock);
 }
 
+#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
+#include <crypto/hash.h>
+#include <crypto/sha2.h>
+#define SHA256_STRING_SIZE (SHA256_DIGEST_SIZE * 2)
+static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
+{
+	char outbuf[SHA256_STRING_SIZE + 1];
+	u8 sha256buf[SHA256_DIGEST_SIZE];
+	struct shash_desc *shash;
+	struct crypto_shash *alg;
+
+	alg = crypto_alloc_shash("sha256", 0, 0);
+	if (!alg)
+		return;
+
+	shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(alg), GFP_KERNEL);
+	if (!shash)
+		goto out_alg;
+
+	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_STRING_SIZE] = 0;
+	dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
+
+out_shash:
+	kfree(shash);
+out_alg:
+	crypto_free_shash(alg);
+
+}
+#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 +902,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;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 23 lines · Amadeusz Sławiński

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
---
 drivers/base/firmware_loader/main.c | 45 ++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
drivers/base/firmware_loader/main.c +0 -0
index 017c4cdb219e..a6e1fb10763d 100644
@@ -791,6 +791,47 @@ static void fw_abort_batch_reqs(struct firmware *fw)
 	mutex_unlock(&fw_lock);

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware *fw) · line 801

Quoted 10 lines
drivers/base/firmware_loader/main.c +8 -0
 }
 
+#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
+#include <crypto/hash.h>
+#include <crypto/sha2.h>
+#define SHA256_STRING_SIZE (SHA256_DIGEST_SIZE * 2)
+static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
+{
+	char outbuf[SHA256_STRING_SIZE + 1];
+	u8 sha256buf[SHA256_DIGEST_SIZE];

Nit, these are big, are you _SURE_ you can put them on the stack ok? Why not dynamically allocate them?

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware *fw) · line 805

Quoted 4 lines
drivers/base/firmware_loader/main.c +4 -0
+	struct shash_desc *shash;
+	struct crypto_shash *alg;
+
+	alg = crypto_alloc_shash("sha256", 0, 0);

Do we need to select this in the .config as well?

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware *fw) · line 809

Quoted 4 lines
drivers/base/firmware_loader/main.c +4 -0
+	if (!alg)
+		return;
+
+	shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(alg), GFP_KERNEL);

kmalloc_array()?

Quoted 7 lines
drivers/base/firmware_loader/main.c +7 -0
+	if (!shash)
+		goto out_alg;
+
+	shash->tfm = alg;
+
+	if (crypto_shash_digest(shash, fw->data, fw->size, sha256buf) < 0)
+		goto out_shash;

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware *fw) · line 826

Quoted 10 lines
drivers/base/firmware_loader/main.c +10 -0
+
+	for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
+		sprintf(&outbuf[i * 2], "%02x", sha256buf[i]);
+	outbuf[SHA256_STRING_SIZE] = 0;
+	dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
+
+out_shash:
+	kfree(shash);
+out_alg:
+	crypto_free_shash(alg);

Otherwise, just tiny comments, overall this looks nice, thanks for doing this.

greg k-h

#2 lore raw
Quoted 26 lines · Greg Kroah-Hartman
Quoted 25 lines · Amadeusz Sławiński

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
---
  drivers/base/firmware_loader/main.c | 45 ++++++++++++++++++++++++++++-
  1 file changed, 44 insertions(+), 1 deletion(-)
drivers/base/firmware_loader/main.c +0 -0
index 017c4cdb219e..a6e1fb10763d 100644
@@ -791,6 +791,47 @@ static void fw_abort_batch_reqs(struct firmware *fw)
  	mutex_unlock(&fw_lock);
  }
 
Quoted 12 lines

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware *fw) · line 801

Quoted 8 lines
drivers/base/firmware_loader/main.c +8 -0
+#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
+#include <crypto/hash.h>
+#include <crypto/sha2.h>
+#define SHA256_STRING_SIZE (SHA256_DIGEST_SIZE * 2)
+static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
+{
+	char outbuf[SHA256_STRING_SIZE + 1];
+	u8 sha256buf[SHA256_DIGEST_SIZE];

Nit, these are big, are you _SURE_ you can put them on the stack ok? Why not dynamically allocate them?

Well, those arrays are not that big? First one is 65 bytes and other one 32. Although now that I looked again at the header, there is SHA256_BLOCK_SIZE define for string size, so I will change SHA256_STRING_SIZE to that instead.

Quoted 7 lines

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware *fw) · line 805

Quoted 4 lines
drivers/base/firmware_loader/main.c +4 -0
+	struct shash_desc *shash;
+	struct crypto_shash *alg;
+
+	alg = crypto_alloc_shash("sha256", 0, 0);

Do we need to select this in the .config as well?

Most likely.

Quoted 7 lines

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware *fw) · line 809

Quoted 4 lines
drivers/base/firmware_loader/main.c +4 -0
+	if (!alg)
+		return;
+
+	shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(alg), GFP_KERNEL);

kmalloc_array()?

Yes.

Quoted 22 lines
Quoted 7 lines
drivers/base/firmware_loader/main.c +7 -0
+	if (!shash)
+		goto out_alg;
+
+	shash->tfm = alg;
+
+	if (crypto_shash_digest(shash, fw->data, fw->size, sha256buf) < 0)
+		goto out_shash;

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware *fw) · line 826

Quoted 10 lines
drivers/base/firmware_loader/main.c +10 -0
+
+	for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
+		sprintf(&outbuf[i * 2], "%02x", sha256buf[i]);
+	outbuf[SHA256_STRING_SIZE] = 0;
+	dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
+
+out_shash:
+	kfree(shash);
+out_alg:
+	crypto_free_shash(alg);

Otherwise, just tiny comments, overall this looks nice, thanks for doing this.

greg k-h

#3 lore raw
Quoted 39 lines · Amadeusz Sławiński
Quoted 28 lines · Greg Kroah-Hartman
Quoted 27 lines · Amadeusz Sławiński

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
---
  drivers/base/firmware_loader/main.c | 45 ++++++++++++++++++++++++++++-
  1 file changed, 44 insertions(+), 1 deletion(-)
drivers/base/firmware_loader/main.c +3 -0
index 017c4cdb219e..a6e1fb10763d 100644
@@ -791,6 +791,47 @@ static void fw_abort_batch_reqs(struct firmware *fw)
  	mutex_unlock(&fw_lock);
  }
+#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
+#include <crypto/hash.h>
+#include <crypto/sha2.h>
Quoted 9 lines

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware *fw) · line 801

Quoted 5 lines
drivers/base/firmware_loader/main.c +5 -0
+#define SHA256_STRING_SIZE (SHA256_DIGEST_SIZE * 2)
+static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
+{
+	char outbuf[SHA256_STRING_SIZE + 1];
+	u8 sha256buf[SHA256_DIGEST_SIZE];

Nit, these are big, are you _SURE_ you can put them on the stack ok? Why not dynamically allocate them?

Quoted 1 line

Well, those arrays are not that big? First one is 65 bytes and other one 32.

And how far down in the stack are you when a driver is requesting firmware to be loaded? Usually pretty deep :(

Quoted 2 lines

Although now that I looked again at the header, there is SHA256_BLOCK_SIZE define for string size, so I will change SHA256_STRING_SIZE to that instead.

You are already dynamically allocating memory, just do it for all of these please so we don't have to go fix this in a few years when some codepath from a driver is found to have blown up the stack for this debug information.

thanks,

greg k-h

#4 lore raw
In reply to Amadeusz Sławiński
Quoted 48 lines · Amadeusz Sławiński
Quoted 30 lines · Greg Kroah-Hartman
Quoted 29 lines · Amadeusz Sławiński

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

---
  drivers/base/firmware_loader/main.c | 45 ++++++++++++++++++++++++++++-

  1 file changed, 44 insertions(+), 1 deletion(-)
drivers/base/firmware_loader/main.c +4 -0
index 017c4cdb219e..a6e1fb10763d 100644
@@ -791,6 +791,47 @@ static void fw_abort_batch_reqs(struct firmware 
 *fw)
       mutex_unlock(&fw_lock);
   }
+#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
+#include <crypto/hash.h>
+#include <crypto/sha2.h>
+#define SHA256_STRING_SIZE (SHA256_DIGEST_SIZE * 2)
Quoted 9 lines

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware · line 802

Quoted 5 lines
drivers/base/firmware_loader/main.c +4 -0
+static void fw_log_firmware_info(const struct firmware *fw, const 
 char *name, struct device *device)
+{
+    char outbuf[SHA256_STRING_SIZE + 1];
+    u8 sha256buf[SHA256_DIGEST_SIZE];

Nit, these are big, are you _SURE_ you can put them on the stack ok? Why not dynamically allocate them?

Well, those arrays are not that big? First one is 65 bytes and other one 32. Although now that I looked again at the header, there is SHA256_BLOCK_SIZE define for string size, so I will change SHA256_STRING_SIZE to that instead.

Quoted 5 lines
Quoted 4 lines
drivers/base/firmware_loader/main.c +4 -0
+    struct shash_desc *shash;
+    struct crypto_shash *alg;
+
+    alg = crypto_alloc_shash("sha256", 0, 0);
Quoted 5 lines
Quoted 2 lines

Do we need to select this in the .config as well?

Most likely.

So I'm having a bit of problem here, as something like:

drivers/base/firmware_loader/Kconfig +2 -0
index 5166b323a0f8..95cf2d8af5c4 100644
@@ -3,6 +3,8 @@ menu "Firmware loader"
 
 config FW_LOADER
        tristate "Firmware loading facility" if EXPERT
+       select CRYPTO_SHA256 if DYNAMIC_DEBUG
+       select CRYPTO if DYNAMIC_DEBUG
        default y
        help
          This enables the firmware loading facility in the kernel. The 

kernel
being the most simple potential fix doesn't seem to work due to circular dependencies. Seems like quite a few cryptography accelerators require FW_LOADER and it causes problems.

I tried few more things, but none of them seem to work. Any advice on what I can do here?

Quoted 11 lines
Quoted 8 lines

drivers/base/firmware_loader/main.c static void fw_abort_batch_reqs(struct firmware · line 811

Quoted 5 lines
drivers/base/firmware_loader/main.c +4 -0
+    if (!alg)
+        return;
+
+    shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(alg), 
 GFP_KERNEL);

kmalloc_array()?

Yes.

And taking one more look, it isn't array allocation but struct followed by VLA used to store additional data, so it will stay as is.