1 // SPDX-License-Identifier: GPL-2.0-only
6 #include <linux/crc32.h>
7 #include <linux/etherdevice.h>
8 #include <linux/export.h>
9 #include <linux/if_ether.h>
10 #include <linux/nvmem-consumer.h>
11 #include <linux/nvmem-provider.h>
13 #include <linux/slab.h>
15 #include "u-boot-env.h"
17 struct u_boot_env_image_single {
22 struct u_boot_env_image_redundant {
28 struct u_boot_env_image_broadcom {
32 DECLARE_FLEX_ARRAY(uint8_t, data);
35 static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, int index,
36 unsigned int offset, void *buf, size_t bytes)
40 if (bytes != 3 * ETH_ALEN - 1)
43 if (!mac_pton(buf, mac))
47 eth_addr_add(mac, index);
49 ether_addr_copy(buf, mac);
54 static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem, uint8_t *buf,
55 size_t data_offset, size_t data_len)
57 char *data = buf + data_offset;
58 char *var, *value, *eq;
61 var < data + data_len && *var;
62 var = value + strlen(value) + 1) {
63 struct nvmem_cell_info info = {};
65 eq = strchr(var, '=');
71 info.name = devm_kstrdup(dev, var, GFP_KERNEL);
74 info.offset = data_offset + value - data;
75 info.bytes = strlen(value);
76 info.np = of_get_child_by_name(dev->of_node, info.name);
77 if (!strcmp(var, "ethaddr")) {
78 info.raw_len = strlen(value);
79 info.bytes = ETH_ALEN;
80 info.read_post_process = u_boot_env_read_post_process_ethaddr;
83 nvmem_add_one_cell(nvmem, &info);
89 int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
90 enum u_boot_env_format format)
92 size_t crc32_data_offset;
93 size_t crc32_data_len;
105 dev_size = nvmem_dev_size(nvmem);
107 buf = kzalloc(dev_size, GFP_KERNEL);
113 bytes = nvmem_device_read(nvmem, 0, dev_size, buf);
117 } else if (bytes != dev_size) {
123 case U_BOOT_FORMAT_SINGLE:
124 crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
125 crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
126 data_offset = offsetof(struct u_boot_env_image_single, data);
128 case U_BOOT_FORMAT_REDUNDANT:
129 crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
130 crc32_data_offset = offsetof(struct u_boot_env_image_redundant, data);
131 data_offset = offsetof(struct u_boot_env_image_redundant, data);
133 case U_BOOT_FORMAT_BROADCOM:
134 crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32);
135 crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data);
136 data_offset = offsetof(struct u_boot_env_image_broadcom, data);
140 if (dev_size < data_offset) {
141 dev_err(dev, "Device too small for u-boot-env\n");
146 crc32_addr = (__le32 *)(buf + crc32_offset);
147 crc32 = le32_to_cpu(*crc32_addr);
148 crc32_data_len = dev_size - crc32_data_offset;
149 data_len = dev_size - data_offset;
151 calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
153 dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
158 buf[dev_size - 1] = '\0';
159 err = u_boot_env_parse_cells(dev, nvmem, buf, data_offset, data_len);
166 EXPORT_SYMBOL_GPL(u_boot_env_parse);
168 static int u_boot_env_add_cells(struct nvmem_layout *layout)
170 struct device *dev = &layout->dev;
171 enum u_boot_env_format format;
173 format = (uintptr_t)device_get_match_data(dev);
175 return u_boot_env_parse(dev, layout->nvmem, format);
178 static int u_boot_env_probe(struct nvmem_layout *layout)
180 layout->add_cells = u_boot_env_add_cells;
182 return nvmem_layout_register(layout);
185 static void u_boot_env_remove(struct nvmem_layout *layout)
187 nvmem_layout_unregister(layout);
190 static const struct of_device_id u_boot_env_of_match_table[] = {
191 { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
192 { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
193 { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
194 { .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
198 static struct nvmem_layout_driver u_boot_env_layout = {
200 .name = "u-boot-env-layout",
201 .of_match_table = u_boot_env_of_match_table,
203 .probe = u_boot_env_probe,
204 .remove = u_boot_env_remove,
206 module_nvmem_layout_driver(u_boot_env_layout);
208 MODULE_AUTHOR("Rafał Miłecki");
209 MODULE_LICENSE("GPL");
210 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
211 MODULE_DESCRIPTION("NVMEM layout driver for U-Boot environment variables");