2024-09-02 15:29:47 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 - 2023 Rafał Miłecki <rafal@milecki.pl>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/crc32.h>
|
|
|
|
#include <linux/etherdevice.h>
|
|
|
|
#include <linux/export.h>
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <linux/nvmem-consumer.h>
|
|
|
|
#include <linux/nvmem-provider.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
#include "u-boot-env.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
struct u_boot_env_image_broadcom {
|
|
|
|
__le32 magic;
|
|
|
|
__le32 len;
|
|
|
|
__le32 crc32;
|
|
|
|
DECLARE_FLEX_ARRAY(uint8_t, data);
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, int index,
|
|
|
|
unsigned int offset, void *buf, size_t bytes)
|
|
|
|
{
|
|
|
|
u8 mac[ETH_ALEN];
|
|
|
|
|
2025-03-12 13:51:46 -06:00
|
|
|
if (bytes != MAC_ADDR_STR_LEN)
|
2024-09-02 15:29:47 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!mac_pton(buf, mac))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (index)
|
|
|
|
eth_addr_add(mac, index);
|
|
|
|
|
|
|
|
ether_addr_copy(buf, mac);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem, uint8_t *buf,
|
|
|
|
size_t data_offset, size_t data_len)
|
|
|
|
{
|
|
|
|
char *data = buf + data_offset;
|
|
|
|
char *var, *value, *eq;
|
|
|
|
|
|
|
|
for (var = data;
|
|
|
|
var < data + data_len && *var;
|
|
|
|
var = value + strlen(value) + 1) {
|
|
|
|
struct nvmem_cell_info info = {};
|
|
|
|
|
|
|
|
eq = strchr(var, '=');
|
|
|
|
if (!eq)
|
|
|
|
break;
|
|
|
|
*eq = '\0';
|
|
|
|
value = eq + 1;
|
|
|
|
|
|
|
|
info.name = devm_kstrdup(dev, var, GFP_KERNEL);
|
|
|
|
if (!info.name)
|
|
|
|
return -ENOMEM;
|
|
|
|
info.offset = data_offset + value - data;
|
|
|
|
info.bytes = strlen(value);
|
|
|
|
info.np = of_get_child_by_name(dev->of_node, info.name);
|
|
|
|
if (!strcmp(var, "ethaddr")) {
|
|
|
|
info.raw_len = strlen(value);
|
|
|
|
info.bytes = ETH_ALEN;
|
|
|
|
info.read_post_process = u_boot_env_read_post_process_ethaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nvmem_add_one_cell(nvmem, &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
|
|
|
|
enum u_boot_env_format format)
|
|
|
|
{
|
|
|
|
size_t crc32_data_offset;
|
|
|
|
size_t crc32_data_len;
|
|
|
|
size_t crc32_offset;
|
nvmem: layouts: u-boot-env: remove crc32 endianness conversion
On 11 Oct 2022, it was reported that the crc32 verification
of the u-boot environment failed only on big-endian systems
for the u-boot-env nvmem layout driver with the following error.
Invalid calculated CRC32: 0x88cd6f09 (expected: 0x096fcd88)
This problem has been present since the driver was introduced,
and before it was made into a layout driver.
The suggested fix at the time was to use further endianness
conversion macros in order to have both the stored and calculated
crc32 values to compare always represented in the system's endianness.
This was not accepted due to sparse warnings
and some disagreement on how to handle the situation.
Later on in a newer revision of the patch, it was proposed to use
cpu_to_le32() for both values to compare instead of le32_to_cpu()
and store the values as __le32 type to remove compilation errors.
The necessity of this is based on the assumption that the use of crc32()
requires endianness conversion because the algorithm uses little-endian,
however, this does not prove to be the case and the issue is unrelated.
Upon inspecting the current kernel code,
there already is an existing use of le32_to_cpu() in this driver,
which suggests there already is special handling for big-endian systems,
however, it is big-endian systems that have the problem.
This, being the only functional difference between architectures
in the driver combined with the fact that the suggested fix
was to use the exact same endianness conversion for the values
brings up the possibility that it was not necessary to begin with,
as the same endianness conversion for two values expected to be the same
is expected to be equivalent to no conversion at all.
After inspecting the u-boot environment of devices of both endianness
and trying to remove the existing endianness conversion,
the problem is resolved in an equivalent way as the other suggested fixes.
Ultimately, it seems that u-boot is agnostic to endianness
at least for the purpose of environment variables.
In other words, u-boot reads and writes the stored crc32 value
with the same endianness that the crc32 value is calculated with
in whichever endianness a certain architecture runs on.
Therefore, the u-boot-env driver does not need to convert endianness.
Remove the usage of endianness macros in the u-boot-env driver,
and change the type of local variables to maintain the same return type.
If there is a special situation in the case of endianness,
it would be a corner case and should be handled by a unique "compatible".
Even though it is not necessary to use endianness conversion macros here,
it may be useful to use them in the future for consistent error printing.
Fixes: d5542923f200 ("nvmem: add driver handling U-Boot environment variables")
Reported-by: INAGAKI Hiroshi <musashino.open@gmail.com>
Link: https://lore.kernel.org/all/20221011024928.1807-1-musashino.open@gmail.com
Cc: stable@vger.kernel.org
Signed-off-by: "Michael C. Pratt" <mcpratt@pm.me>
Signed-off-by: Srinivas Kandagatla <srini@kernel.org>
Link: https://lore.kernel.org/r/20250716144210.4804-1-srini@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2025-07-16 15:42:10 +01:00
|
|
|
uint32_t *crc32_addr;
|
2024-09-02 15:29:47 +01:00
|
|
|
size_t data_offset;
|
|
|
|
size_t data_len;
|
|
|
|
size_t dev_size;
|
|
|
|
uint32_t crc32;
|
|
|
|
uint32_t calc;
|
|
|
|
uint8_t *buf;
|
|
|
|
int bytes;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
dev_size = nvmem_dev_size(nvmem);
|
|
|
|
|
|
|
|
buf = kzalloc(dev_size, GFP_KERNEL);
|
|
|
|
if (!buf) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = nvmem_device_read(nvmem, 0, dev_size, buf);
|
|
|
|
if (bytes < 0) {
|
|
|
|
err = bytes;
|
|
|
|
goto err_kfree;
|
|
|
|
} else if (bytes != dev_size) {
|
|
|
|
err = -EIO;
|
|
|
|
goto err_kfree;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (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, data);
|
|
|
|
data_offset = offsetof(struct u_boot_env_image_redundant, data);
|
|
|
|
break;
|
|
|
|
case U_BOOT_FORMAT_BROADCOM:
|
|
|
|
crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32);
|
|
|
|
crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data);
|
|
|
|
data_offset = offsetof(struct u_boot_env_image_broadcom, data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev_size < data_offset) {
|
|
|
|
dev_err(dev, "Device too small for u-boot-env\n");
|
|
|
|
err = -EIO;
|
|
|
|
goto err_kfree;
|
|
|
|
}
|
|
|
|
|
nvmem: layouts: u-boot-env: remove crc32 endianness conversion
On 11 Oct 2022, it was reported that the crc32 verification
of the u-boot environment failed only on big-endian systems
for the u-boot-env nvmem layout driver with the following error.
Invalid calculated CRC32: 0x88cd6f09 (expected: 0x096fcd88)
This problem has been present since the driver was introduced,
and before it was made into a layout driver.
The suggested fix at the time was to use further endianness
conversion macros in order to have both the stored and calculated
crc32 values to compare always represented in the system's endianness.
This was not accepted due to sparse warnings
and some disagreement on how to handle the situation.
Later on in a newer revision of the patch, it was proposed to use
cpu_to_le32() for both values to compare instead of le32_to_cpu()
and store the values as __le32 type to remove compilation errors.
The necessity of this is based on the assumption that the use of crc32()
requires endianness conversion because the algorithm uses little-endian,
however, this does not prove to be the case and the issue is unrelated.
Upon inspecting the current kernel code,
there already is an existing use of le32_to_cpu() in this driver,
which suggests there already is special handling for big-endian systems,
however, it is big-endian systems that have the problem.
This, being the only functional difference between architectures
in the driver combined with the fact that the suggested fix
was to use the exact same endianness conversion for the values
brings up the possibility that it was not necessary to begin with,
as the same endianness conversion for two values expected to be the same
is expected to be equivalent to no conversion at all.
After inspecting the u-boot environment of devices of both endianness
and trying to remove the existing endianness conversion,
the problem is resolved in an equivalent way as the other suggested fixes.
Ultimately, it seems that u-boot is agnostic to endianness
at least for the purpose of environment variables.
In other words, u-boot reads and writes the stored crc32 value
with the same endianness that the crc32 value is calculated with
in whichever endianness a certain architecture runs on.
Therefore, the u-boot-env driver does not need to convert endianness.
Remove the usage of endianness macros in the u-boot-env driver,
and change the type of local variables to maintain the same return type.
If there is a special situation in the case of endianness,
it would be a corner case and should be handled by a unique "compatible".
Even though it is not necessary to use endianness conversion macros here,
it may be useful to use them in the future for consistent error printing.
Fixes: d5542923f200 ("nvmem: add driver handling U-Boot environment variables")
Reported-by: INAGAKI Hiroshi <musashino.open@gmail.com>
Link: https://lore.kernel.org/all/20221011024928.1807-1-musashino.open@gmail.com
Cc: stable@vger.kernel.org
Signed-off-by: "Michael C. Pratt" <mcpratt@pm.me>
Signed-off-by: Srinivas Kandagatla <srini@kernel.org>
Link: https://lore.kernel.org/r/20250716144210.4804-1-srini@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2025-07-16 15:42:10 +01:00
|
|
|
crc32_addr = (uint32_t *)(buf + crc32_offset);
|
|
|
|
crc32 = *crc32_addr;
|
2024-09-02 15:29:47 +01:00
|
|
|
crc32_data_len = dev_size - crc32_data_offset;
|
|
|
|
data_len = dev_size - data_offset;
|
|
|
|
|
2025-06-20 13:50:27 -07:00
|
|
|
calc = crc32_le(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
|
2024-09-02 15:29:47 +01:00
|
|
|
if (calc != crc32) {
|
|
|
|
dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
|
|
|
|
err = -EINVAL;
|
|
|
|
goto err_kfree;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[dev_size - 1] = '\0';
|
|
|
|
err = u_boot_env_parse_cells(dev, nvmem, buf, data_offset, data_len);
|
|
|
|
|
|
|
|
err_kfree:
|
|
|
|
kfree(buf);
|
|
|
|
err_out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(u_boot_env_parse);
|
|
|
|
|
|
|
|
static int u_boot_env_add_cells(struct nvmem_layout *layout)
|
|
|
|
{
|
|
|
|
struct device *dev = &layout->dev;
|
|
|
|
enum u_boot_env_format format;
|
|
|
|
|
|
|
|
format = (uintptr_t)device_get_match_data(dev);
|
|
|
|
|
|
|
|
return u_boot_env_parse(dev, layout->nvmem, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int u_boot_env_probe(struct nvmem_layout *layout)
|
|
|
|
{
|
|
|
|
layout->add_cells = u_boot_env_add_cells;
|
|
|
|
|
|
|
|
return nvmem_layout_register(layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void u_boot_env_remove(struct nvmem_layout *layout)
|
|
|
|
{
|
|
|
|
nvmem_layout_unregister(layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
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, },
|
|
|
|
{ .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct nvmem_layout_driver u_boot_env_layout = {
|
|
|
|
.driver = {
|
|
|
|
.name = "u-boot-env-layout",
|
|
|
|
.of_match_table = u_boot_env_of_match_table,
|
|
|
|
},
|
|
|
|
.probe = u_boot_env_probe,
|
|
|
|
.remove = u_boot_env_remove,
|
|
|
|
};
|
|
|
|
module_nvmem_layout_driver(u_boot_env_layout);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Rafał Miłecki");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
|
|
|
|
MODULE_DESCRIPTION("NVMEM layout driver for U-Boot environment variables");
|