Skip to content

Commit 5f15811

Browse files
Rafał Miłeckigregkh
authored andcommitted
nvmem: layouts: add U-Boot env layout
U-Boot environment variables are stored in a specific format. Actual data can be placed in various storage sources (MTD, UBI volume, EEPROM, NVRAM, etc.). Move all generic (NVMEM device independent) code from NVMEM device driver to an NVMEM layout driver. Then add a simple NVMEM layout code on top of it. This allows using NVMEM layout for parsing U-Boot env data stored in any kind of NVMEM device. The old NVMEM glue driver stays in place for handling bindings in the MTD context. To avoid code duplication it uses exported layout parsing function. Please note that handling MTD & NVMEM layout bindings may be refactored in the future. Signed-off-by: Rafał Miłecki <[email protected]> Reviewed-by: Miquel Raynal <[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]>
1 parent 5baeb15 commit 5f15811

File tree

7 files changed

+242
-165
lines changed

7 files changed

+242
-165
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23458,6 +23458,7 @@ U-BOOT ENVIRONMENT VARIABLES
2345823458
M: Rafał Miłecki <[email protected]>
2345923459
S: Maintained
2346023460
F: Documentation/devicetree/bindings/nvmem/u-boot,env.yaml
23461+
F: drivers/nvmem/layouts/u-boot-env.c
2346123462
F: drivers/nvmem/u-boot-env.c
2346223463

2346323464
UACCE ACCELERATOR FRAMEWORK

drivers/nvmem/Kconfig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,7 @@ config NVMEM_SUNXI_SID
363363
config NVMEM_U_BOOT_ENV
364364
tristate "U-Boot environment variables support"
365365
depends on OF && MTD
366-
select CRC32
367-
select GENERIC_NET_UTILS
366+
select NVMEM_LAYOUT_U_BOOT_ENV
368367
help
369368
U-Boot stores its setup as environment variables. This driver adds
370369
support for verifying & exporting such data. It also exposes variables

drivers/nvmem/layouts/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ config NVMEM_LAYOUT_ONIE_TLV
2626

2727
If unsure, say N.
2828

29+
config NVMEM_LAYOUT_U_BOOT_ENV
30+
tristate "U-Boot environment variables layout"
31+
select CRC32
32+
select GENERIC_NET_UTILS
33+
help
34+
U-Boot stores its setup as environment variables. This driver adds
35+
support for verifying & exporting such data. It also exposes variables
36+
as NVMEM cells so they can be referenced by other drivers.
37+
38+
If unsure, say N.
39+
2940
endmenu
3041

3142
endif

drivers/nvmem/layouts/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
obj-$(CONFIG_NVMEM_LAYOUT_SL28_VPD) += sl28vpd.o
77
obj-$(CONFIG_NVMEM_LAYOUT_ONIE_TLV) += onie-tlv.o
8+
obj-$(CONFIG_NVMEM_LAYOUT_U_BOOT_ENV) += u-boot-env.o

drivers/nvmem/layouts/u-boot-env.c

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2022 - 2023 Rafał Miłecki <[email protected]>
4+
*/
5+
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>
12+
#include <linux/of.h>
13+
#include <linux/slab.h>
14+
15+
#include "u-boot-env.h"
16+
17+
struct u_boot_env_image_single {
18+
__le32 crc32;
19+
uint8_t data[];
20+
} __packed;
21+
22+
struct u_boot_env_image_redundant {
23+
__le32 crc32;
24+
u8 mark;
25+
uint8_t data[];
26+
} __packed;
27+
28+
struct u_boot_env_image_broadcom {
29+
__le32 magic;
30+
__le32 len;
31+
__le32 crc32;
32+
DECLARE_FLEX_ARRAY(uint8_t, data);
33+
} __packed;
34+
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)
37+
{
38+
u8 mac[ETH_ALEN];
39+
40+
if (bytes != 3 * ETH_ALEN - 1)
41+
return -EINVAL;
42+
43+
if (!mac_pton(buf, mac))
44+
return -EINVAL;
45+
46+
if (index)
47+
eth_addr_add(mac, index);
48+
49+
ether_addr_copy(buf, mac);
50+
51+
return 0;
52+
}
53+
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)
56+
{
57+
char *data = buf + data_offset;
58+
char *var, *value, *eq;
59+
60+
for (var = data;
61+
var < data + data_len && *var;
62+
var = value + strlen(value) + 1) {
63+
struct nvmem_cell_info info = {};
64+
65+
eq = strchr(var, '=');
66+
if (!eq)
67+
break;
68+
*eq = '\0';
69+
value = eq + 1;
70+
71+
info.name = devm_kstrdup(dev, var, GFP_KERNEL);
72+
if (!info.name)
73+
return -ENOMEM;
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;
81+
}
82+
83+
nvmem_add_one_cell(nvmem, &info);
84+
}
85+
86+
return 0;
87+
}
88+
89+
int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
90+
enum u_boot_env_format format)
91+
{
92+
size_t crc32_data_offset;
93+
size_t crc32_data_len;
94+
size_t crc32_offset;
95+
__le32 *crc32_addr;
96+
size_t data_offset;
97+
size_t data_len;
98+
size_t dev_size;
99+
uint32_t crc32;
100+
uint32_t calc;
101+
uint8_t *buf;
102+
int bytes;
103+
int err;
104+
105+
dev_size = nvmem_dev_size(nvmem);
106+
107+
buf = kzalloc(dev_size, GFP_KERNEL);
108+
if (!buf) {
109+
err = -ENOMEM;
110+
goto err_out;
111+
}
112+
113+
bytes = nvmem_device_read(nvmem, 0, dev_size, buf);
114+
if (bytes < 0) {
115+
err = bytes;
116+
goto err_kfree;
117+
} else if (bytes != dev_size) {
118+
err = -EIO;
119+
goto err_kfree;
120+
}
121+
122+
switch (format) {
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);
127+
break;
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);
132+
break;
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);
137+
break;
138+
}
139+
140+
if (dev_size < data_offset) {
141+
dev_err(dev, "Device too small for u-boot-env\n");
142+
err = -EIO;
143+
goto err_kfree;
144+
}
145+
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;
150+
151+
calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
152+
if (calc != crc32) {
153+
dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
154+
err = -EINVAL;
155+
goto err_kfree;
156+
}
157+
158+
buf[dev_size - 1] = '\0';
159+
err = u_boot_env_parse_cells(dev, nvmem, buf, data_offset, data_len);
160+
161+
err_kfree:
162+
kfree(buf);
163+
err_out:
164+
return err;
165+
}
166+
EXPORT_SYMBOL_GPL(u_boot_env_parse);
167+
168+
static int u_boot_env_add_cells(struct nvmem_layout *layout)
169+
{
170+
struct device *dev = &layout->dev;
171+
enum u_boot_env_format format;
172+
173+
format = (uintptr_t)device_get_match_data(dev);
174+
175+
return u_boot_env_parse(dev, layout->nvmem, format);
176+
}
177+
178+
static int u_boot_env_probe(struct nvmem_layout *layout)
179+
{
180+
layout->add_cells = u_boot_env_add_cells;
181+
182+
return nvmem_layout_register(layout);
183+
}
184+
185+
static void u_boot_env_remove(struct nvmem_layout *layout)
186+
{
187+
nvmem_layout_unregister(layout);
188+
}
189+
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, },
195+
{},
196+
};
197+
198+
static struct nvmem_layout_driver u_boot_env_layout = {
199+
.driver = {
200+
.name = "u-boot-env-layout",
201+
.of_match_table = u_boot_env_of_match_table,
202+
},
203+
.probe = u_boot_env_probe,
204+
.remove = u_boot_env_remove,
205+
};
206+
module_nvmem_layout_driver(u_boot_env_layout);
207+
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");

drivers/nvmem/layouts/u-boot-env.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
3+
#ifndef _LINUX_NVMEM_LAYOUTS_U_BOOT_ENV_H
4+
#define _LINUX_NVMEM_LAYOUTS_U_BOOT_ENV_H
5+
6+
enum u_boot_env_format {
7+
U_BOOT_FORMAT_SINGLE,
8+
U_BOOT_FORMAT_REDUNDANT,
9+
U_BOOT_FORMAT_BROADCOM,
10+
};
11+
12+
int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
13+
enum u_boot_env_format format);
14+
15+
#endif /* ifndef _LINUX_NVMEM_LAYOUTS_U_BOOT_ENV_H */

0 commit comments

Comments
 (0)