xref: /linux/drivers/firmware/google/coreboot_table.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * coreboot_table.c
4  *
5  * Module providing coreboot table access.
6  *
7  * Copyright 2017 Google Inc.
8  * Copyright 2017 Samuel Holland <samuel@sholland.org>
9  */
10 
11 #include <linux/acpi.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/io.h>
16 #include <linux/kernel.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 
23 #include "coreboot_table.h"
24 
25 /* Coreboot table header structure */
26 struct coreboot_table_header {
27 	char signature[4];
28 	u32 header_bytes;
29 	u32 header_checksum;
30 	u32 table_bytes;
31 	u32 table_checksum;
32 	u32 table_entries;
33 };
34 
35 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
36 #define CB_DRV(d) container_of_const(d, struct coreboot_driver, drv)
37 
38 static int coreboot_bus_match(struct device *dev, const struct device_driver *drv)
39 {
40 	struct coreboot_device *device = CB_DEV(dev);
41 	const struct coreboot_driver *driver = CB_DRV(drv);
42 	const struct coreboot_device_id *id;
43 
44 	if (!driver->id_table)
45 		return 0;
46 
47 	for (id = driver->id_table; id->tag; id++) {
48 		if (device->entry.tag == id->tag)
49 			return 1;
50 	}
51 
52 	return 0;
53 }
54 
55 static int coreboot_bus_probe(struct device *dev)
56 {
57 	int ret = -ENODEV;
58 	struct coreboot_device *device = CB_DEV(dev);
59 	struct coreboot_driver *driver = CB_DRV(dev->driver);
60 
61 	if (driver->probe)
62 		ret = driver->probe(device);
63 
64 	return ret;
65 }
66 
67 static void coreboot_bus_remove(struct device *dev)
68 {
69 	struct coreboot_device *device = CB_DEV(dev);
70 	struct coreboot_driver *driver = CB_DRV(dev->driver);
71 
72 	if (driver->remove)
73 		driver->remove(device);
74 }
75 
76 static int coreboot_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
77 {
78 	struct coreboot_device *device = CB_DEV(dev);
79 	u32 tag = device->entry.tag;
80 
81 	return add_uevent_var(env, "MODALIAS=coreboot:t%08X", tag);
82 }
83 
84 static const struct bus_type coreboot_bus_type = {
85 	.name		= "coreboot",
86 	.match		= coreboot_bus_match,
87 	.probe		= coreboot_bus_probe,
88 	.remove		= coreboot_bus_remove,
89 	.uevent		= coreboot_bus_uevent,
90 };
91 
92 static void coreboot_device_release(struct device *dev)
93 {
94 	struct coreboot_device *device = CB_DEV(dev);
95 
96 	kfree(device);
97 }
98 
99 int __coreboot_driver_register(struct coreboot_driver *driver,
100 			       struct module *owner)
101 {
102 	driver->drv.bus = &coreboot_bus_type;
103 	driver->drv.owner = owner;
104 
105 	return driver_register(&driver->drv);
106 }
107 EXPORT_SYMBOL(__coreboot_driver_register);
108 
109 void coreboot_driver_unregister(struct coreboot_driver *driver)
110 {
111 	driver_unregister(&driver->drv);
112 }
113 EXPORT_SYMBOL(coreboot_driver_unregister);
114 
115 static int coreboot_table_populate(struct device *dev, void *ptr, resource_size_t len)
116 {
117 	int i, ret;
118 	void *ptr_entry;
119 	struct coreboot_device *device;
120 	struct coreboot_table_entry *entry;
121 	struct coreboot_table_header *header = ptr;
122 	void *ptr_end;
123 
124 	ptr_end = ptr + len;
125 	ptr_entry = ptr + header->header_bytes;
126 	for (i = 0; i < header->table_entries; i++) {
127 		if (ptr_entry + sizeof(*entry) > ptr_end)
128 			return -EINVAL;
129 		entry = ptr_entry;
130 
131 		if (entry->size < sizeof(*entry)) {
132 			dev_warn(dev, "coreboot table entry too small!\n");
133 			return -EINVAL;
134 		}
135 
136 		if (ptr_entry + entry->size > ptr_end)
137 			return -EINVAL;
138 
139 		device = kzalloc(sizeof(device->dev) + entry->size, GFP_KERNEL);
140 		if (!device)
141 			return -ENOMEM;
142 
143 		device->dev.parent = dev;
144 		device->dev.bus = &coreboot_bus_type;
145 		device->dev.release = coreboot_device_release;
146 		memcpy(device->raw, ptr_entry, entry->size);
147 
148 		switch (device->entry.tag) {
149 		case LB_TAG_CBMEM_ENTRY:
150 			dev_set_name(&device->dev, "cbmem-%08x",
151 				     device->cbmem_entry.id);
152 			break;
153 		default:
154 			dev_set_name(&device->dev, "coreboot%d", i);
155 			break;
156 		}
157 
158 		ptr_entry += entry->size;
159 
160 		ret = device_register(&device->dev);
161 		if (ret) {
162 			dev_warn(dev, "failed to register coreboot device: %d\n", ret);
163 			put_device(&device->dev);
164 		}
165 	}
166 
167 	return 0;
168 }
169 
170 static int coreboot_table_probe(struct platform_device *pdev)
171 {
172 	resource_size_t len;
173 	struct coreboot_table_header *header;
174 	struct resource *res;
175 	struct device *dev = &pdev->dev;
176 	void *ptr;
177 	int ret;
178 
179 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
180 	if (!res)
181 		return -EINVAL;
182 
183 	len = resource_size(res);
184 	if (!res->start || !len)
185 		return -EINVAL;
186 
187 	/* Check just the header first to make sure things are sane */
188 	header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
189 	if (!header)
190 		return -ENOMEM;
191 
192 	len = header->header_bytes + header->table_bytes;
193 	ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
194 	memunmap(header);
195 	if (ret) {
196 		dev_warn(dev, "coreboot table missing or corrupt!\n");
197 		return -ENODEV;
198 	}
199 
200 	ptr = memremap(res->start, len, MEMREMAP_WB);
201 	if (!ptr)
202 		return -ENOMEM;
203 
204 	ret = coreboot_table_populate(dev, ptr, len);
205 
206 	memunmap(ptr);
207 
208 	return ret;
209 }
210 
211 static int __cb_dev_unregister(struct device *dev, void *dummy)
212 {
213 	device_unregister(dev);
214 	return 0;
215 }
216 
217 static void coreboot_table_remove(struct platform_device *pdev)
218 {
219 	bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);
220 }
221 
222 #ifdef CONFIG_ACPI
223 static const struct acpi_device_id cros_coreboot_acpi_match[] = {
224 	{ "GOOGCB00", 0 },
225 	{ "BOOT0000", 0 },
226 	{ }
227 };
228 MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
229 #endif
230 
231 #ifdef CONFIG_OF
232 static const struct of_device_id coreboot_of_match[] = {
233 	{ .compatible = "coreboot" },
234 	{}
235 };
236 MODULE_DEVICE_TABLE(of, coreboot_of_match);
237 #endif
238 
239 static struct platform_driver coreboot_table_driver = {
240 	.probe = coreboot_table_probe,
241 	.remove = coreboot_table_remove,
242 	.driver = {
243 		.name = "coreboot_table",
244 		.acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
245 		.of_match_table = of_match_ptr(coreboot_of_match),
246 	},
247 };
248 
249 static int __init coreboot_table_driver_init(void)
250 {
251 	int ret;
252 
253 	ret = bus_register(&coreboot_bus_type);
254 	if (ret)
255 		return ret;
256 
257 	ret = platform_driver_register(&coreboot_table_driver);
258 	if (ret) {
259 		bus_unregister(&coreboot_bus_type);
260 		return ret;
261 	}
262 
263 	return 0;
264 }
265 
266 static void __exit coreboot_table_driver_exit(void)
267 {
268 	platform_driver_unregister(&coreboot_table_driver);
269 	bus_unregister(&coreboot_bus_type);
270 }
271 
272 subsys_initcall(coreboot_table_driver_init);
273 module_exit(coreboot_table_driver_exit);
274 
275 MODULE_AUTHOR("Google, Inc.");
276 MODULE_DESCRIPTION("Module providing coreboot table access");
277 MODULE_LICENSE("GPL");
278