Debian Codegraph @

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

raw
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/hex.h>
10 #include <linux/if_ether.h>
11 #include <linux/nvmem-consumer.h>
12 #include <linux/nvmem-provider.h>
13 #include <linux/of.h>
14 #include <linux/slab.h>
15
16 #include "u-boot-env.h"
17
18 struct u_boot_env_image_single {
19 __le32 crc32;
20 uint8_t data[];
21 } __packed;
22
23 struct u_boot_env_image_redundant {
24 __le32 crc32;
25 u8 mark;
26 uint8_t data[];
27 } __packed;
28
29 struct u_boot_env_image_broadcom {
30 __le32 magic;
31 __le32 len;
32 __le32 crc32;
33 DECLARE_FLEX_ARRAY(uint8_t, data);
34 } __packed;
35
36 static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, int index,
37 unsigned int offset, void *buf, size_t bytes)
38 {
39 u8 mac[ETH_ALEN];
40
41 if (bytes != MAC_ADDR_STR_LEN)
42 return -EINVAL;
43
44 if (!mac_pton(buf, mac))
45 return -EINVAL;
46
47 if (index)
48 eth_addr_add(mac, index);
49
50 ether_addr_copy(buf, mac);
51
52 return 0;
53 }
54
55 static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem, uint8_t *buf,
56 size_t data_offset, size_t data_len)
57 {
58 char *data = buf + data_offset;
59 char *var, *value, *eq;
60
61 for (var = data;
62 var < data + data_len && *var;
63 var = value + strlen(value) + 1) {
64 struct nvmem_cell_info info = {};
65
66 eq = strchr(var, '=');
67 if (!eq)
68 break;
69 *eq = '\0';
70 value = eq + 1;
71
72 info.name = devm_kstrdup(dev, var, GFP_KERNEL);
73 if (!info.name)
74 return -ENOMEM;
75 info.offset = data_offset + value - data;
76 info.bytes = strlen(value);
77 info.np = of_get_child_by_name(dev->of_node, info.name);
78 if (!strcmp(var, "ethaddr")) {
79 info.raw_len = strlen(value);
80 info.bytes = ETH_ALEN;
81 info.read_post_process = u_boot_env_read_post_process_ethaddr;
82 }
83
84 nvmem_add_one_cell(nvmem, &info);
85 }
86
87 return 0;
88 }
89
90 int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
91 enum u_boot_env_format format)
92 {
93 size_t crc32_data_offset;
94 size_t crc32_data_len;
95 size_t crc32_offset;
96 uint32_t *crc32_addr;
97 size_t data_offset;
98 size_t data_len;
99 size_t dev_size;
100 uint32_t crc32;
101 uint32_t calc;
102 uint8_t *buf;
103 u32 env_size;
104 int bytes;
105 int err;
106
107 dev_size = device_property_read_u32(dev, "env-size", &env_size) ?
108 nvmem_dev_size(nvmem) : (size_t)env_size;
109
110 buf = kzalloc(dev_size, GFP_KERNEL);
111 if (!buf) {
112 err = -ENOMEM;
113 goto err_out;
114 }
115
116 bytes = nvmem_device_read(nvmem, 0, dev_size, buf);
117 if (bytes < 0) {
118 err = bytes;
119 goto err_kfree;
120 } else if (bytes != dev_size) {
121 err = -EIO;
122 goto err_kfree;
123 }
124
125 switch (format) {
126 case U_BOOT_FORMAT_SINGLE:
127 crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
128 crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
129 data_offset = offsetof(struct u_boot_env_image_single, data);
130 break;
131 case U_BOOT_FORMAT_REDUNDANT:
132 crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
133 crc32_data_offset = offsetof(struct u_boot_env_image_redundant, data);
134 data_offset = offsetof(struct u_boot_env_image_redundant, data);
135 break;
136 case U_BOOT_FORMAT_BROADCOM:
137 crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32);
138 crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data);
139 data_offset = offsetof(struct u_boot_env_image_broadcom, data);
140 break;
141 }
142
143 if (dev_size < data_offset) {
144 dev_err(dev, "Device too small for u-boot-env\n");
145 err = -EIO;
146 goto err_kfree;
147 }
148
149 crc32_addr = (uint32_t *)(buf + crc32_offset);
150 crc32 = *crc32_addr;
151 crc32_data_len = dev_size - crc32_data_offset;
152 data_len = dev_size - data_offset;
153
154 calc = crc32_le(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
155 if (calc != crc32) {
156 dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
157 err = -EINVAL;
158 goto err_kfree;
159 }
160
161 buf[dev_size - 1] = '\0';
162 err = u_boot_env_parse_cells(dev, nvmem, buf, data_offset, data_len);
163
164 err_kfree:
165 kfree(buf);
166 err_out:
167 return err;
168 }
169 EXPORT_SYMBOL_GPL(u_boot_env_parse);
170
171 static int u_boot_env_add_cells(struct nvmem_layout *layout)
172 {
173 struct device *dev = &layout->dev;
174 enum u_boot_env_format format;
175
176 format = (uintptr_t)device_get_match_data(dev);
177
178 return u_boot_env_parse(dev, layout->nvmem, format);
179 }
180
181 static int u_boot_env_probe(struct nvmem_layout *layout)
182 {
183 layout->add_cells = u_boot_env_add_cells;
184
185 return nvmem_layout_register(layout);
186 }
187
188 static void u_boot_env_remove(struct nvmem_layout *layout)
189 {
190 nvmem_layout_unregister(layout);
191 }
192
193 static const struct of_device_id u_boot_env_of_match_table[] = {
194 { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
195 { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
196 { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
197 { .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
198 {},
199 };
200
201 static struct nvmem_layout_driver u_boot_env_layout = {
202 .driver = {
203 .name = "u-boot-env-layout",
204 .of_match_table = u_boot_env_of_match_table,
205 },
206 .probe = u_boot_env_probe,
207 .remove = u_boot_env_remove,
208 };
209 module_nvmem_layout_driver(u_boot_env_layout);
210
211 MODULE_AUTHOR("Rafał Miłecki");
212 MODULE_LICENSE("GPL");
213 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
214 MODULE_DESCRIPTION("NVMEM layout driver for U-Boot environment variables");