From: Rafał Miłecki Date: Wed, 17 Aug 2022 18:44:52 +0000 (+0200) Subject: kernel: backport U-Boot environment data NVMEM driver X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=34cf31043504473df3174a22d163b71b75e46542;p=openwrt%2Fstaging%2Fwigyori.git kernel: backport U-Boot environment data NVMEM driver It parses U-Boot env data into NVMEM cells. Signed-off-by: Rafał Miłecki --- diff --git a/target/linux/bcm53xx/patches-5.10/080-v5.13-0002-nvmem-brcm_nvram-new-driver-exposing-Broadcom-s-NVRA.patch b/target/linux/bcm53xx/patches-5.10/080-v5.13-0002-nvmem-brcm_nvram-new-driver-exposing-Broadcom-s-NVRA.patch index ac89ea4bc2..3a8940b36a 100644 --- a/target/linux/bcm53xx/patches-5.10/080-v5.13-0002-nvmem-brcm_nvram-new-driver-exposing-Broadcom-s-NVRA.patch +++ b/target/linux/bcm53xx/patches-5.10/080-v5.13-0002-nvmem-brcm_nvram-new-driver-exposing-Broadcom-s-NVRA.patch @@ -19,9 +19,9 @@ Signed-off-by: Srinivas Kandagatla --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig -@@ -270,4 +270,13 @@ config SPRD_EFUSE - This driver can also be built as a module. If so, the module - will be called nvmem-sprd-efuse. +@@ -283,4 +283,13 @@ config NVMEM_U_BOOT_ENV + + If compiled as module it will be called nvmem_u-boot-env. + +config NVMEM_BRCM_NVRAM @@ -35,10 +35,10 @@ Signed-off-by: Srinivas Kandagatla endif --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile -@@ -55,3 +55,5 @@ obj-$(CONFIG_NVMEM_ZYNQMP) += nvmem_zynq - nvmem_zynqmp_nvmem-y := zynqmp_nvmem.o - obj-$(CONFIG_SPRD_EFUSE) += nvmem_sprd_efuse.o +@@ -57,3 +57,5 @@ obj-$(CONFIG_SPRD_EFUSE) += nvmem_sprd_e nvmem_sprd_efuse-y := sprd-efuse.o + obj-$(CONFIG_NVMEM_U_BOOT_ENV) += nvmem_u-boot-env.o + nvmem_u-boot-env-y := u-boot-env.o +obj-$(CONFIG_NVMEM_BRCM_NVRAM) += nvmem_brcm_nvram.o +nvmem_brcm_nvram-y := brcm_nvram.o --- /dev/null diff --git a/target/linux/generic/backport-5.10/414-v6.1-mtd-allow-getting-MTD-device-associated-with-a-speci.patch b/target/linux/generic/backport-5.10/414-v6.1-mtd-allow-getting-MTD-device-associated-with-a-speci.patch new file mode 100644 index 0000000000..0e8708e5bb --- /dev/null +++ b/target/linux/generic/backport-5.10/414-v6.1-mtd-allow-getting-MTD-device-associated-with-a-speci.patch @@ -0,0 +1,72 @@ +From b0321721be50b80c03a51866a94fde4f94690e18 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= +Date: Wed, 15 Jun 2022 21:42:59 +0200 +Subject: [PATCH] mtd: allow getting MTD device associated with a specific DT + node +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +MTD subsystem API allows interacting with MTD devices (e.g. reading, +writing, handling bad blocks). So far a random driver could get MTD +device only by its name (get_mtd_device_nm()). This change allows +getting them also by a DT node. + +This API is required for drivers handling DT defined MTD partitions in a +specific way (e.g. U-Boot (sub)partition with environment variables). + +Signed-off-by: Rafał Miłecki +Acked-by: Miquel Raynal +Signed-off-by: Srinivas Kandagatla +--- + drivers/mtd/mtdcore.c | 28 ++++++++++++++++++++++++++++ + include/linux/mtd/mtd.h | 1 + + 2 files changed, 29 insertions(+) + +--- a/drivers/mtd/mtdcore.c ++++ b/drivers/mtd/mtdcore.c +@@ -1070,6 +1070,34 @@ int __get_mtd_device(struct mtd_info *mt + EXPORT_SYMBOL_GPL(__get_mtd_device); + + /** ++ * of_get_mtd_device_by_node - obtain an MTD device associated with a given node ++ * ++ * @np: device tree node ++ */ ++struct mtd_info *of_get_mtd_device_by_node(struct device_node *np) ++{ ++ struct mtd_info *mtd = NULL; ++ struct mtd_info *tmp; ++ int err; ++ ++ mutex_lock(&mtd_table_mutex); ++ ++ err = -EPROBE_DEFER; ++ mtd_for_each_device(tmp) { ++ if (mtd_get_of_node(tmp) == np) { ++ mtd = tmp; ++ err = __get_mtd_device(mtd); ++ break; ++ } ++ } ++ ++ mutex_unlock(&mtd_table_mutex); ++ ++ return err ? ERR_PTR(err) : mtd; ++} ++EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node); ++ ++/** + * get_mtd_device_nm - obtain a validated handle for an MTD device by + * device name + * @name: MTD device name to open +--- a/include/linux/mtd/mtd.h ++++ b/include/linux/mtd/mtd.h +@@ -675,6 +675,7 @@ extern int mtd_device_unregister(struct + extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num); + extern int __get_mtd_device(struct mtd_info *mtd); + extern void __put_mtd_device(struct mtd_info *mtd); ++extern struct mtd_info *of_get_mtd_device_by_node(struct device_node *np); + extern struct mtd_info *get_mtd_device_nm(const char *name); + extern void put_mtd_device(struct mtd_info *mtd); + diff --git a/target/linux/generic/backport-5.10/801-v6.1-nvmem-add-driver-handling-U-Boot-environment-variabl.patch b/target/linux/generic/backport-5.10/801-v6.1-nvmem-add-driver-handling-U-Boot-environment-variabl.patch new file mode 100644 index 0000000000..1459d1184e --- /dev/null +++ b/target/linux/generic/backport-5.10/801-v6.1-nvmem-add-driver-handling-U-Boot-environment-variabl.patch @@ -0,0 +1,278 @@ +From f955dc14450695564926711cf9fa8e1d5d854302 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= +Date: Wed, 15 Jun 2022 21:43:00 +0200 +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. + +Signed-off-by: Rafał Miłecki +Reviewed-by: Ahmad Fatoum +Signed-off-by: Srinivas Kandagatla +--- + +--- a/drivers/nvmem/Kconfig ++++ b/drivers/nvmem/Kconfig +@@ -270,4 +270,17 @@ config SPRD_EFUSE + This driver can also be built as a module. If so, the module + will be called nvmem-sprd-efuse. + ++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 +@@ -55,3 +55,5 @@ obj-$(CONFIG_NVMEM_ZYNQMP) += nvmem_zynq + nvmem_zynqmp_nvmem-y := zynqmp_nvmem.o + obj-$(CONFIG_SPRD_EFUSE) += nvmem_sprd_efuse.o + nvmem_sprd_efuse-y := sprd-efuse.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 ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++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); diff --git a/target/linux/generic/backport-5.15/403-v6.1-mtd-allow-getting-MTD-device-associated-with-a-speci.patch b/target/linux/generic/backport-5.15/403-v6.1-mtd-allow-getting-MTD-device-associated-with-a-speci.patch new file mode 100644 index 0000000000..be1aa9cfb8 --- /dev/null +++ b/target/linux/generic/backport-5.15/403-v6.1-mtd-allow-getting-MTD-device-associated-with-a-speci.patch @@ -0,0 +1,72 @@ +From b0321721be50b80c03a51866a94fde4f94690e18 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= +Date: Wed, 15 Jun 2022 21:42:59 +0200 +Subject: [PATCH] mtd: allow getting MTD device associated with a specific DT + node +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +MTD subsystem API allows interacting with MTD devices (e.g. reading, +writing, handling bad blocks). So far a random driver could get MTD +device only by its name (get_mtd_device_nm()). This change allows +getting them also by a DT node. + +This API is required for drivers handling DT defined MTD partitions in a +specific way (e.g. U-Boot (sub)partition with environment variables). + +Signed-off-by: Rafał Miłecki +Acked-by: Miquel Raynal +Signed-off-by: Srinivas Kandagatla +--- + drivers/mtd/mtdcore.c | 28 ++++++++++++++++++++++++++++ + include/linux/mtd/mtd.h | 1 + + 2 files changed, 29 insertions(+) + +--- a/drivers/mtd/mtdcore.c ++++ b/drivers/mtd/mtdcore.c +@@ -1227,6 +1227,34 @@ int __get_mtd_device(struct mtd_info *mt + EXPORT_SYMBOL_GPL(__get_mtd_device); + + /** ++ * of_get_mtd_device_by_node - obtain an MTD device associated with a given node ++ * ++ * @np: device tree node ++ */ ++struct mtd_info *of_get_mtd_device_by_node(struct device_node *np) ++{ ++ struct mtd_info *mtd = NULL; ++ struct mtd_info *tmp; ++ int err; ++ ++ mutex_lock(&mtd_table_mutex); ++ ++ err = -EPROBE_DEFER; ++ mtd_for_each_device(tmp) { ++ if (mtd_get_of_node(tmp) == np) { ++ mtd = tmp; ++ err = __get_mtd_device(mtd); ++ break; ++ } ++ } ++ ++ mutex_unlock(&mtd_table_mutex); ++ ++ return err ? ERR_PTR(err) : mtd; ++} ++EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node); ++ ++/** + * get_mtd_device_nm - obtain a validated handle for an MTD device by + * device name + * @name: MTD device name to open +--- a/include/linux/mtd/mtd.h ++++ b/include/linux/mtd/mtd.h +@@ -682,6 +682,7 @@ extern int mtd_device_unregister(struct + extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num); + extern int __get_mtd_device(struct mtd_info *mtd); + extern void __put_mtd_device(struct mtd_info *mtd); ++extern struct mtd_info *of_get_mtd_device_by_node(struct device_node *np); + extern struct mtd_info *get_mtd_device_nm(const char *name); + extern void put_mtd_device(struct mtd_info *mtd); + diff --git a/target/linux/generic/backport-5.15/802-v6.1-nvmem-add-driver-handling-U-Boot-environment-variabl.patch b/target/linux/generic/backport-5.15/802-v6.1-nvmem-add-driver-handling-U-Boot-environment-variabl.patch new file mode 100644 index 0000000000..a40c61f929 --- /dev/null +++ b/target/linux/generic/backport-5.15/802-v6.1-nvmem-add-driver-handling-U-Boot-environment-variabl.patch @@ -0,0 +1,278 @@ +From f955dc14450695564926711cf9fa8e1d5d854302 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= +Date: Wed, 15 Jun 2022 21:43:00 +0200 +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. + +Signed-off-by: Rafał Miłecki +Reviewed-by: Ahmad Fatoum +Signed-off-by: Srinivas Kandagatla +--- + +--- a/drivers/nvmem/Kconfig ++++ b/drivers/nvmem/Kconfig +@@ -300,4 +300,17 @@ config NVMEM_BRCM_NVRAM + This driver provides support for Broadcom's NVRAM that can be accessed + using I/O mapping. + ++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 +@@ -61,3 +61,5 @@ obj-$(CONFIG_NVMEM_RMEM) += nvmem-rmem. + nvmem-rmem-y := rmem.o + obj-$(CONFIG_NVMEM_BRCM_NVRAM) += nvmem_brcm_nvram.o + nvmem_brcm_nvram-y := brcm_nvram.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 ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++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); diff --git a/target/linux/generic/pending-5.10/400-mtd-mtdsplit-support.patch b/target/linux/generic/pending-5.10/400-mtd-mtdsplit-support.patch index f37df6d164..ce4743879d 100644 --- a/target/linux/generic/pending-5.10/400-mtd-mtdsplit-support.patch +++ b/target/linux/generic/pending-5.10/400-mtd-mtdsplit-support.patch @@ -298,7 +298,7 @@ static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd) { if (mtd->writesize_shift) -@@ -678,6 +696,13 @@ extern void __put_mtd_device(struct mtd_ +@@ -679,6 +697,13 @@ extern struct mtd_info *of_get_mtd_devic extern struct mtd_info *get_mtd_device_nm(const char *name); extern void put_mtd_device(struct mtd_info *mtd); diff --git a/target/linux/generic/pending-5.10/495-mtd-core-add-get_mtd_device_by_node.patch b/target/linux/generic/pending-5.10/495-mtd-core-add-get_mtd_device_by_node.patch index cc5946ac05..66017e1648 100644 --- a/target/linux/generic/pending-5.10/495-mtd-core-add-get_mtd_device_by_node.patch +++ b/target/linux/generic/pending-5.10/495-mtd-core-add-get_mtd_device_by_node.patch @@ -17,7 +17,7 @@ Reviewed-by: Miquel Raynal --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c -@@ -1117,6 +1117,44 @@ out_unlock: +@@ -1145,6 +1145,44 @@ out_unlock: } EXPORT_SYMBOL_GPL(get_mtd_device_nm); @@ -64,9 +64,9 @@ Reviewed-by: Miquel Raynal mutex_lock(&mtd_table_mutex); --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h -@@ -696,6 +696,8 @@ extern struct mtd_info *get_mtd_device(s - extern int __get_mtd_device(struct mtd_info *mtd); +@@ -697,6 +697,8 @@ extern int __get_mtd_device(struct mtd_i extern void __put_mtd_device(struct mtd_info *mtd); + extern struct mtd_info *of_get_mtd_device_by_node(struct device_node *np); extern struct mtd_info *get_mtd_device_nm(const char *name); +extern struct mtd_info *get_mtd_device_by_node( + const struct device_node *of_node); diff --git a/target/linux/generic/pending-5.15/400-mtd-mtdsplit-support.patch b/target/linux/generic/pending-5.15/400-mtd-mtdsplit-support.patch index 8b34f4f467..d07afd70ee 100644 --- a/target/linux/generic/pending-5.15/400-mtd-mtdsplit-support.patch +++ b/target/linux/generic/pending-5.15/400-mtd-mtdsplit-support.patch @@ -288,7 +288,7 @@ Subject: [PATCH] mtd: mtdsplit support static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd) { if (mtd->writesize_shift) -@@ -685,6 +703,13 @@ extern void __put_mtd_device(struct mtd_ +@@ -686,6 +704,13 @@ extern struct mtd_info *of_get_mtd_devic extern struct mtd_info *get_mtd_device_nm(const char *name); extern void put_mtd_device(struct mtd_info *mtd); diff --git a/target/linux/generic/pending-5.15/495-mtd-core-add-get_mtd_device_by_node.patch b/target/linux/generic/pending-5.15/495-mtd-core-add-get_mtd_device_by_node.patch index 431c80795e..3e14a1dd31 100644 --- a/target/linux/generic/pending-5.15/495-mtd-core-add-get_mtd_device_by_node.patch +++ b/target/linux/generic/pending-5.15/495-mtd-core-add-get_mtd_device_by_node.patch @@ -17,7 +17,7 @@ Reviewed-by: Miquel Raynal --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c -@@ -1274,6 +1274,44 @@ out_unlock: +@@ -1302,6 +1302,44 @@ out_unlock: } EXPORT_SYMBOL_GPL(get_mtd_device_nm); @@ -64,9 +64,9 @@ Reviewed-by: Miquel Raynal mutex_lock(&mtd_table_mutex); --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h -@@ -703,6 +703,8 @@ extern struct mtd_info *get_mtd_device(s - extern int __get_mtd_device(struct mtd_info *mtd); +@@ -704,6 +704,8 @@ extern int __get_mtd_device(struct mtd_i extern void __put_mtd_device(struct mtd_info *mtd); + extern struct mtd_info *of_get_mtd_device_by_node(struct device_node *np); extern struct mtd_info *get_mtd_device_nm(const char *name); +extern struct mtd_info *get_mtd_device_by_node( + const struct device_node *of_node);