1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2022 - 2023 Rafał Miłecki <rafal@milecki.pl>
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
u_boot_env_read_post_process_ethaddr(void * context,const char * id,int index,unsigned int offset,void * buf,size_t bytes)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 != MAC_ADDR_STR_LEN)
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
u_boot_env_parse_cells(struct device * dev,struct nvmem_device * nvmem,uint8_t * buf,size_t data_offset,size_t data_len)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
u_boot_env_parse(struct device * dev,struct nvmem_device * nvmem,enum u_boot_env_format format)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 uint32_t *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 u32 env_size;
103 int bytes;
104 int err;
105
106 dev_size = device_property_read_u32(dev, "env-size", &env_size) ?
107 nvmem_dev_size(nvmem) : (size_t)env_size;
108
109 buf = kzalloc(dev_size, GFP_KERNEL);
110 if (!buf) {
111 err = -ENOMEM;
112 goto err_out;
113 }
114
115 bytes = nvmem_device_read(nvmem, 0, dev_size, buf);
116 if (bytes < 0) {
117 err = bytes;
118 goto err_kfree;
119 } else if (bytes != dev_size) {
120 err = -EIO;
121 goto err_kfree;
122 }
123
124 switch (format) {
125 case U_BOOT_FORMAT_SINGLE:
126 crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
127 crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
128 data_offset = offsetof(struct u_boot_env_image_single, data);
129 break;
130 case U_BOOT_FORMAT_REDUNDANT:
131 crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
132 crc32_data_offset = offsetof(struct u_boot_env_image_redundant, data);
133 data_offset = offsetof(struct u_boot_env_image_redundant, data);
134 break;
135 case U_BOOT_FORMAT_BROADCOM:
136 crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32);
137 crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data);
138 data_offset = offsetof(struct u_boot_env_image_broadcom, data);
139 break;
140 }
141
142 if (dev_size < data_offset) {
143 dev_err(dev, "Device too small for u-boot-env\n");
144 err = -EIO;
145 goto err_kfree;
146 }
147
148 crc32_addr = (uint32_t *)(buf + crc32_offset);
149 crc32 = *crc32_addr;
150 crc32_data_len = dev_size - crc32_data_offset;
151 data_len = dev_size - data_offset;
152
153 calc = crc32_le(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
154 if (calc != crc32) {
155 dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
156 err = -EINVAL;
157 goto err_kfree;
158 }
159
160 buf[dev_size - 1] = '\0';
161 err = u_boot_env_parse_cells(dev, nvmem, buf, data_offset, data_len);
162
163 err_kfree:
164 kfree(buf);
165 err_out:
166 return err;
167 }
168 EXPORT_SYMBOL_GPL(u_boot_env_parse);
169
u_boot_env_add_cells(struct nvmem_layout * layout)170 static int u_boot_env_add_cells(struct nvmem_layout *layout)
171 {
172 struct device *dev = &layout->dev;
173 enum u_boot_env_format format;
174
175 format = (uintptr_t)device_get_match_data(dev);
176
177 return u_boot_env_parse(dev, layout->nvmem, format);
178 }
179
u_boot_env_probe(struct nvmem_layout * layout)180 static int u_boot_env_probe(struct nvmem_layout *layout)
181 {
182 layout->add_cells = u_boot_env_add_cells;
183
184 return nvmem_layout_register(layout);
185 }
186
u_boot_env_remove(struct nvmem_layout * layout)187 static void u_boot_env_remove(struct nvmem_layout *layout)
188 {
189 nvmem_layout_unregister(layout);
190 }
191
192 static const struct of_device_id u_boot_env_of_match_table[] = {
193 { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
194 { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
195 { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
196 { .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
197 {},
198 };
199
200 static struct nvmem_layout_driver u_boot_env_layout = {
201 .driver = {
202 .name = "u-boot-env-layout",
203 .of_match_table = u_boot_env_of_match_table,
204 },
205 .probe = u_boot_env_probe,
206 .remove = u_boot_env_remove,
207 };
208 module_nvmem_layout_driver(u_boot_env_layout);
209
210 MODULE_AUTHOR("Rafał Miłecki");
211 MODULE_LICENSE("GPL");
212 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
213 MODULE_DESCRIPTION("NVMEM layout driver for U-Boot environment variables");
214