diff mbox series

[5/6] nvmem: rmem: add CRC validation for Mobileye EyeQ5 NVMEM

Message ID 20241203-rmem-v1-5-24f4970cf14e@bootlin.com (mailing list archive)
State Superseded
Headers show
Series nvmem: rmem: cleanup & add checksumming support for Mobileye EyeQ5 | expand

Commit Message

Théo Lebrun Dec. 3, 2024, 1:55 p.m. UTC
Mobileye EyeQ5 has a non-volatile memory region which
gets used to store MAC addresses. Its format includes
a prefix 12-byte header and a suffix 4-byte CRC.

Add an optional ->checksum() callback inside match data;
it runs CRC32 onto the content.

Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
---
 drivers/nvmem/rmem.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)

Comments

kernel test robot Dec. 4, 2024, 7:58 a.m. UTC | #1
Hi Théo,

kernel test robot noticed the following build errors:

[auto build test ERROR on 40384c840ea1944d7c5a392e8975ed088ecf0b37]

url:    https://github.com/intel-lab-lkp/linux/commits/Th-o-Lebrun/dt-bindings-nvmem-rmem-Add-mobileye-eyeq5-bootloader-config/20241204-103417
base:   40384c840ea1944d7c5a392e8975ed088ecf0b37
patch link:    https://lore.kernel.org/r/20241203-rmem-v1-5-24f4970cf14e%40bootlin.com
patch subject: [PATCH 5/6] nvmem: rmem: add CRC validation for Mobileye EyeQ5 NVMEM
config: arm-randconfig-002 (https://download.01.org/0day-ci/archive/20241204/202412041522.01H5Kj6F-lkp@intel.com/config)
compiler: arm-linux-gnueabi-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241204/202412041522.01H5Kj6F-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202412041522.01H5Kj6F-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/nvmem/rmem.c: In function 'rmem_eyeq5_checksum':
   drivers/nvmem/rmem.c:66:9: error: cleanup argument not a function
      66 |         void *buf __free(kfree) = NULL;
         |         ^~~~
   drivers/nvmem/rmem.c:97:15: error: implicit declaration of function 'kmalloc'; did you mean 'mm_alloc'? [-Wimplicit-function-declaration]
      97 |         buf = kmalloc(header.size, GFP_KERNEL);
         |               ^~~~~~~
         |               mm_alloc
>> drivers/nvmem/rmem.c:97:13: error: assignment to 'void *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
      97 |         buf = kmalloc(header.size, GFP_KERNEL);
         |             ^


vim +97 drivers/nvmem/rmem.c

    62	
    63	static int rmem_eyeq5_checksum(struct rmem *priv)
    64	{
    65		struct rmem_eyeq5_header header;
    66		void *buf __free(kfree) = NULL;
    67		u32 computed_crc, *target_crc;
    68		size_t data_size;
    69		int ret;
    70	
    71		ret = rmem_read(priv, 0, &header, sizeof(header));
    72		if (ret)
    73			return ret;
    74	
    75		if (header.magic != RMEM_EYEQ5_MAGIC)
    76			return -EINVAL;
    77	
    78		/*
    79		 * Avoid massive kmalloc() if header read is invalid;
    80		 * the check would be done by the next rmem_read() anyway.
    81		 */
    82		if (header.size > priv->mem->size)
    83			return -EINVAL;
    84	
    85		/*
    86		 *           0 +-------------------+
    87		 *             | Header (12 bytes) | \
    88		 *             +-------------------+ |
    89		 *             |                   | | data to be CRCed
    90		 *             |        ...        | |
    91		 *             |                   | /
    92		 *   data_size +-------------------+
    93		 *             |   CRC (4 bytes)   |
    94		 * header.size +-------------------+
    95		 */
    96	
  > 97		buf = kmalloc(header.size, GFP_KERNEL);
    98		if (!buf)
    99			return -ENOMEM;
   100	
   101		ret = rmem_read(priv, 0, buf, header.size);
   102		if (ret)
   103			return ret;
   104	
   105		data_size = header.size - sizeof(*target_crc);
   106		target_crc = buf + data_size;
   107		computed_crc = crc32(U32_MAX, buf, data_size) ^ U32_MAX;
   108	
   109		if (computed_crc == *target_crc)
   110			return 0;
   111	
   112		dev_err(priv->dev,
   113			"checksum failed: computed %#x, expected %#x, header (%#x, %#x, %#x)\n",
   114			computed_crc, *target_crc, header.magic, header.version, header.size);
   115		return -EINVAL;
   116	}
   117
kernel test robot Dec. 4, 2024, 8:29 a.m. UTC | #2
Hi Théo,

kernel test robot noticed the following build errors:

[auto build test ERROR on 40384c840ea1944d7c5a392e8975ed088ecf0b37]

url:    https://github.com/intel-lab-lkp/linux/commits/Th-o-Lebrun/dt-bindings-nvmem-rmem-Add-mobileye-eyeq5-bootloader-config/20241204-103417
base:   40384c840ea1944d7c5a392e8975ed088ecf0b37
patch link:    https://lore.kernel.org/r/20241203-rmem-v1-5-24f4970cf14e%40bootlin.com
patch subject: [PATCH 5/6] nvmem: rmem: add CRC validation for Mobileye EyeQ5 NVMEM
config: arc-randconfig-001 (https://download.01.org/0day-ci/archive/20241204/202412041614.WGhDRXyh-lkp@intel.com/config)
compiler: arc-elf-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241204/202412041614.WGhDRXyh-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202412041614.WGhDRXyh-lkp@intel.com/

All error/warnings (new ones prefixed by >>):

   drivers/nvmem/rmem.c: In function 'rmem_eyeq5_checksum':
>> drivers/nvmem/rmem.c:66:9: error: cleanup argument not a function
      66 |         void *buf __free(kfree) = NULL;
         |         ^~~~
>> drivers/nvmem/rmem.c:97:15: error: implicit declaration of function 'kmalloc'; did you mean 'mm_alloc'? [-Werror=implicit-function-declaration]
      97 |         buf = kmalloc(header.size, GFP_KERNEL);
         |               ^~~~~~~
         |               mm_alloc
>> drivers/nvmem/rmem.c:97:13: warning: assignment to 'void *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
      97 |         buf = kmalloc(header.size, GFP_KERNEL);
         |             ^
   cc1: some warnings being treated as errors


vim +66 drivers/nvmem/rmem.c

    62	
    63	static int rmem_eyeq5_checksum(struct rmem *priv)
    64	{
    65		struct rmem_eyeq5_header header;
  > 66		void *buf __free(kfree) = NULL;
    67		u32 computed_crc, *target_crc;
    68		size_t data_size;
    69		int ret;
    70	
    71		ret = rmem_read(priv, 0, &header, sizeof(header));
    72		if (ret)
    73			return ret;
    74	
    75		if (header.magic != RMEM_EYEQ5_MAGIC)
    76			return -EINVAL;
    77	
    78		/*
    79		 * Avoid massive kmalloc() if header read is invalid;
    80		 * the check would be done by the next rmem_read() anyway.
    81		 */
    82		if (header.size > priv->mem->size)
    83			return -EINVAL;
    84	
    85		/*
    86		 *           0 +-------------------+
    87		 *             | Header (12 bytes) | \
    88		 *             +-------------------+ |
    89		 *             |                   | | data to be CRCed
    90		 *             |        ...        | |
    91		 *             |                   | /
    92		 *   data_size +-------------------+
    93		 *             |   CRC (4 bytes)   |
    94		 * header.size +-------------------+
    95		 */
    96	
  > 97		buf = kmalloc(header.size, GFP_KERNEL);
    98		if (!buf)
    99			return -ENOMEM;
   100	
   101		ret = rmem_read(priv, 0, buf, header.size);
   102		if (ret)
   103			return ret;
   104	
   105		data_size = header.size - sizeof(*target_crc);
   106		target_crc = buf + data_size;
   107		computed_crc = crc32(U32_MAX, buf, data_size) ^ U32_MAX;
   108	
   109		if (computed_crc == *target_crc)
   110			return 0;
   111	
   112		dev_err(priv->dev,
   113			"checksum failed: computed %#x, expected %#x, header (%#x, %#x, %#x)\n",
   114			computed_crc, *target_crc, header.magic, header.version, header.size);
   115		return -EINVAL;
   116	}
   117
Théo Lebrun Dec. 4, 2024, 5:04 p.m. UTC | #3
On Wed Dec 4, 2024 at 8:58 AM CET, kernel test robot wrote:
> kernel test robot noticed the following build errors:
>
> [auto build test ERROR on 40384c840ea1944d7c5a392e8975ed088ecf0b37]
>
> url:    https://github.com/intel-lab-lkp/linux/commits/Th-o-Lebrun/dt-bindings-nvmem-rmem-Add-mobileye-eyeq5-bootloader-config/20241204-103417
> base:   40384c840ea1944d7c5a392e8975ed088ecf0b37
> patch link:    https://lore.kernel.org/r/20241203-rmem-v1-5-24f4970cf14e%40bootlin.com
> patch subject: [PATCH 5/6] nvmem: rmem: add CRC validation for Mobileye EyeQ5 NVMEM
> config: arm-randconfig-002 (https://download.01.org/0day-ci/archive/20241204/202412041522.01H5Kj6F-lkp@intel.com/config)
> compiler: arm-linux-gnueabi-gcc (GCC) 14.2.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241204/202412041522.01H5Kj6F-lkp@intel.com/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202412041522.01H5Kj6F-lkp@intel.com/
>
> All errors (new ones prefixed by >>):
>
>    drivers/nvmem/rmem.c: In function 'rmem_eyeq5_checksum':
>    drivers/nvmem/rmem.c:66:9: error: cleanup argument not a function
>       66 |         void *buf __free(kfree) = NULL;
>          |         ^~~~
>    drivers/nvmem/rmem.c:97:15: error: implicit declaration of function 'kmalloc'; did you mean 'mm_alloc'? [-Wimplicit-function-declaration]
>       97 |         buf = kmalloc(header.size, GFP_KERNEL);
>          |               ^~~~~~~
>          |               mm_alloc
> >> drivers/nvmem/rmem.c:97:13: error: assignment to 'void *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
>       97 |         buf = kmalloc(header.size, GFP_KERNEL);
>          |             ^

Will fix with the following.
V2 incoming in a few days to avoid spam.

	diff --git a/drivers/nvmem/rmem.c b/drivers/nvmem/rmem.c
	index 04796f4fa8ae..1f0caf1d2dc1 100644
	--- a/drivers/nvmem/rmem.c
	+++ b/drivers/nvmem/rmem.c
	@@ -9,6 +9,7 @@
	 #include <linux/nvmem-provider.h>
	 #include <linux/of_reserved_mem.h>
	 #include <linux/platform_device.h>
	+#include <linux/slab.h>

	 struct rmem {
	        struct device *dev;

Regards,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
diff mbox series

Patch

diff --git a/drivers/nvmem/rmem.c b/drivers/nvmem/rmem.c
index ca89c2689031534ff316a48e03360aeec823b025..04796f4fa8ae708387013fa260afb901a14e24ff 100644
--- a/drivers/nvmem/rmem.c
+++ b/drivers/nvmem/rmem.c
@@ -3,6 +3,7 @@ 
  * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
  */
 
+#include <linux/crc32.h>
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/nvmem-provider.h>
@@ -15,6 +16,18 @@  struct rmem {
 	struct reserved_mem *mem;
 };
 
+struct rmem_match_data {
+	int (*checksum)(struct rmem *priv);
+};
+
+struct __packed rmem_eyeq5_header {
+	u32 magic;
+	u32 version;
+	u32 size;
+};
+
+#define RMEM_EYEQ5_MAGIC	((u32)0xDABBAD00)
+
 static int rmem_read(void *context, unsigned int offset,
 		     void *val, size_t bytes)
 {
@@ -47,10 +60,66 @@  static int rmem_read(void *context, unsigned int offset,
 	return 0;
 }
 
+static int rmem_eyeq5_checksum(struct rmem *priv)
+{
+	struct rmem_eyeq5_header header;
+	void *buf __free(kfree) = NULL;
+	u32 computed_crc, *target_crc;
+	size_t data_size;
+	int ret;
+
+	ret = rmem_read(priv, 0, &header, sizeof(header));
+	if (ret)
+		return ret;
+
+	if (header.magic != RMEM_EYEQ5_MAGIC)
+		return -EINVAL;
+
+	/*
+	 * Avoid massive kmalloc() if header read is invalid;
+	 * the check would be done by the next rmem_read() anyway.
+	 */
+	if (header.size > priv->mem->size)
+		return -EINVAL;
+
+	/*
+	 *           0 +-------------------+
+	 *             | Header (12 bytes) | \
+	 *             +-------------------+ |
+	 *             |                   | | data to be CRCed
+	 *             |        ...        | |
+	 *             |                   | /
+	 *   data_size +-------------------+
+	 *             |   CRC (4 bytes)   |
+	 * header.size +-------------------+
+	 */
+
+	buf = kmalloc(header.size, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = rmem_read(priv, 0, buf, header.size);
+	if (ret)
+		return ret;
+
+	data_size = header.size - sizeof(*target_crc);
+	target_crc = buf + data_size;
+	computed_crc = crc32(U32_MAX, buf, data_size) ^ U32_MAX;
+
+	if (computed_crc == *target_crc)
+		return 0;
+
+	dev_err(priv->dev,
+		"checksum failed: computed %#x, expected %#x, header (%#x, %#x, %#x)\n",
+		computed_crc, *target_crc, header.magic, header.version, header.size);
+	return -EINVAL;
+}
+
 static int rmem_probe(struct platform_device *pdev)
 {
 	struct nvmem_config config = { };
 	struct device *dev = &pdev->dev;
+	const struct rmem_match_data *match_data = device_get_match_data(dev);
 	struct reserved_mem *mem;
 	struct rmem *priv;
 
@@ -73,10 +142,21 @@  static int rmem_probe(struct platform_device *pdev)
 	config.size = mem->size;
 	config.reg_read = rmem_read;
 
+	if (match_data && match_data->checksum) {
+		int ret = match_data->checksum(priv);
+		if (ret)
+			return ret;
+	}
+
 	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
 }
 
+static const struct rmem_match_data rmem_eyeq5_match_data = {
+	.checksum = rmem_eyeq5_checksum,
+};
+
 static const struct of_device_id rmem_match[] = {
+	{ .compatible = "mobileye,eyeq5-bootloader-config", .data = &rmem_eyeq5_match_data },
 	{ .compatible = "nvmem-rmem", },
 	{ /* sentinel */ },
 };