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