123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- From d5542923f200f95bddf524f36fd495f78aa28e3c Mon Sep 17 00:00:00 2001
- From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <[email protected]>
- Date: Fri, 16 Sep 2022 13:20:48 +0100
- Subject: [PATCH] nvmem: add driver handling U-Boot environment variables
- MIME-Version: 1.0
- Content-Type: text/plain; charset=UTF-8
- Content-Transfer-Encoding: 8bit
- U-Boot stores its setup as environment variables. It's a list of
- key-value pairs stored on flash device with a custom header.
- This commit adds an NVMEM driver that:
- 1. Provides NVMEM access to environment vars binary data
- 2. Extracts variables as NVMEM cells
- Current Linux's NVMEM sysfs API allows reading whole NVMEM data block.
- It can be used by user-space tools for reading U-Boot env vars block
- without the hassle of finding its location. Parsing will still need to
- be re-done there.
- Kernel-parsed NVMEM cells can be read however by Linux drivers. This may
- be useful for Ethernet drivers for reading device MAC address which is
- often stored as U-Boot env variable.
- Reviewed-by: Ahmad Fatoum <[email protected]>
- Signed-off-by: Rafał Miłecki <[email protected]>
- Signed-off-by: Srinivas Kandagatla <[email protected]>
- Link: https://lore.kernel.org/r/[email protected]
- Signed-off-by: Greg Kroah-Hartman <[email protected]>
- ---
- MAINTAINERS | 1 +
- drivers/nvmem/Kconfig | 13 +++
- drivers/nvmem/Makefile | 2 +
- drivers/nvmem/u-boot-env.c | 218 +++++++++++++++++++++++++++++++++++++
- 4 files changed, 234 insertions(+)
- create mode 100644 drivers/nvmem/u-boot-env.c
- --- a/drivers/nvmem/Kconfig
- +++ b/drivers/nvmem/Kconfig
- @@ -344,4 +344,17 @@ config NVMEM_APPLE_EFUSES
- This driver can also be built as a module. If so, the module will
- be called nvmem-apple-efuses.
-
- +config NVMEM_U_BOOT_ENV
- + tristate "U-Boot environment variables support"
- + depends on OF && MTD
- + select CRC32
- + help
- + U-Boot stores its setup as environment variables. This driver adds
- + support for verifying & exporting such data. It also exposes variables
- + as NVMEM cells so they can be referenced by other drivers.
- +
- + Currently this drivers works only with env variables on top of MTD.
- +
- + If compiled as module it will be called nvmem_u-boot-env.
- +
- endif
- --- a/drivers/nvmem/Makefile
- +++ b/drivers/nvmem/Makefile
- @@ -69,3 +69,5 @@ obj-$(CONFIG_NVMEM_APPLE_EFUSES) += nvme
- nvmem-apple-efuses-y := apple-efuses.o
- obj-$(CONFIG_MICROCHIP_OTPC) += nvmem-microchip-otpc.o
- nvmem-microchip-otpc-y := microchip-otpc.o
- +obj-$(CONFIG_NVMEM_U_BOOT_ENV) += nvmem_u-boot-env.o
- +nvmem_u-boot-env-y := u-boot-env.o
- --- /dev/null
- +++ b/drivers/nvmem/u-boot-env.c
- @@ -0,0 +1,218 @@
- +// SPDX-License-Identifier: GPL-2.0-only
- +/*
- + * Copyright (C) 2022 Rafał Miłecki <[email protected]>
- + */
- +
- +#include <linux/crc32.h>
- +#include <linux/mod_devicetable.h>
- +#include <linux/module.h>
- +#include <linux/mtd/mtd.h>
- +#include <linux/nvmem-consumer.h>
- +#include <linux/nvmem-provider.h>
- +#include <linux/of_device.h>
- +#include <linux/platform_device.h>
- +#include <linux/slab.h>
- +
- +enum u_boot_env_format {
- + U_BOOT_FORMAT_SINGLE,
- + U_BOOT_FORMAT_REDUNDANT,
- +};
- +
- +struct u_boot_env {
- + struct device *dev;
- + enum u_boot_env_format format;
- +
- + struct mtd_info *mtd;
- +
- + /* Cells */
- + struct nvmem_cell_info *cells;
- + int ncells;
- +};
- +
- +struct u_boot_env_image_single {
- + __le32 crc32;
- + uint8_t data[];
- +} __packed;
- +
- +struct u_boot_env_image_redundant {
- + __le32 crc32;
- + u8 mark;
- + uint8_t data[];
- +} __packed;
- +
- +static int u_boot_env_read(void *context, unsigned int offset, void *val,
- + size_t bytes)
- +{
- + struct u_boot_env *priv = context;
- + struct device *dev = priv->dev;
- + size_t bytes_read;
- + int err;
- +
- + err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val);
- + if (err && !mtd_is_bitflip(err)) {
- + dev_err(dev, "Failed to read from mtd: %d\n", err);
- + return err;
- + }
- +
- + if (bytes_read != bytes) {
- + dev_err(dev, "Failed to read %zu bytes\n", bytes);
- + return -EIO;
- + }
- +
- + return 0;
- +}
- +
- +static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,
- + size_t data_offset, size_t data_len)
- +{
- + struct device *dev = priv->dev;
- + char *data = buf + data_offset;
- + char *var, *value, *eq;
- + int idx;
- +
- + priv->ncells = 0;
- + for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
- + priv->ncells++;
- +
- + priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
- + if (!priv->cells)
- + return -ENOMEM;
- +
- + for (var = data, idx = 0;
- + var < data + data_len && *var;
- + var = value + strlen(value) + 1, idx++) {
- + eq = strchr(var, '=');
- + if (!eq)
- + break;
- + *eq = '\0';
- + value = eq + 1;
- +
- + priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
- + if (!priv->cells[idx].name)
- + return -ENOMEM;
- + priv->cells[idx].offset = data_offset + value - data;
- + priv->cells[idx].bytes = strlen(value);
- + }
- +
- + if (WARN_ON(idx != priv->ncells))
- + priv->ncells = idx;
- +
- + return 0;
- +}
- +
- +static int u_boot_env_parse(struct u_boot_env *priv)
- +{
- + struct device *dev = priv->dev;
- + size_t crc32_data_offset;
- + size_t crc32_data_len;
- + size_t crc32_offset;
- + size_t data_offset;
- + size_t data_len;
- + uint32_t crc32;
- + uint32_t calc;
- + size_t bytes;
- + uint8_t *buf;
- + int err;
- +
- + buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
- + if (!buf) {
- + err = -ENOMEM;
- + goto err_out;
- + }
- +
- + err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf);
- + if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) {
- + dev_err(dev, "Failed to read from mtd: %d\n", err);
- + goto err_kfree;
- + }
- +
- + switch (priv->format) {
- + case U_BOOT_FORMAT_SINGLE:
- + crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
- + crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
- + data_offset = offsetof(struct u_boot_env_image_single, data);
- + break;
- + case U_BOOT_FORMAT_REDUNDANT:
- + crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
- + crc32_data_offset = offsetof(struct u_boot_env_image_redundant, mark);
- + data_offset = offsetof(struct u_boot_env_image_redundant, data);
- + break;
- + }
- + crc32 = le32_to_cpu(*(uint32_t *)(buf + crc32_offset));
- + crc32_data_len = priv->mtd->size - crc32_data_offset;
- + data_len = priv->mtd->size - data_offset;
- +
- + calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
- + if (calc != crc32) {
- + dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
- + err = -EINVAL;
- + goto err_kfree;
- + }
- +
- + buf[priv->mtd->size - 1] = '\0';
- + err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
- + if (err)
- + dev_err(dev, "Failed to add cells: %d\n", err);
- +
- +err_kfree:
- + kfree(buf);
- +err_out:
- + return err;
- +}
- +
- +static int u_boot_env_probe(struct platform_device *pdev)
- +{
- + struct nvmem_config config = {
- + .name = "u-boot-env",
- + .reg_read = u_boot_env_read,
- + };
- + struct device *dev = &pdev->dev;
- + struct device_node *np = dev->of_node;
- + struct u_boot_env *priv;
- + int err;
- +
- + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
- + if (!priv)
- + return -ENOMEM;
- + priv->dev = dev;
- +
- + priv->format = (uintptr_t)of_device_get_match_data(dev);
- +
- + priv->mtd = of_get_mtd_device_by_node(np);
- + if (IS_ERR(priv->mtd)) {
- + dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
- + return PTR_ERR(priv->mtd);
- + }
- +
- + err = u_boot_env_parse(priv);
- + if (err)
- + return err;
- +
- + config.dev = dev;
- + config.cells = priv->cells;
- + config.ncells = priv->ncells;
- + config.priv = priv;
- + config.size = priv->mtd->size;
- +
- + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
- +}
- +
- +static const struct of_device_id u_boot_env_of_match_table[] = {
- + { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
- + { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
- + { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
- + {},
- +};
- +
- +static struct platform_driver u_boot_env_driver = {
- + .probe = u_boot_env_probe,
- + .driver = {
- + .name = "u_boot_env",
- + .of_match_table = u_boot_env_of_match_table,
- + },
- +};
- +module_platform_driver(u_boot_env_driver);
- +
- +MODULE_AUTHOR("Rafał Miłecki");
- +MODULE_LICENSE("GPL");
- +MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
|