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 /* 118 * Whether the memory of a CBMEM entry may be mapped: it must be exclusively 119 * System RAM, or exclusively Reserved memory, as CBMEM often is on x86, or 120 * memory that the kernel doesn't know about at all. coreboot leaves CBMEM out 121 * of the /memory node of the device tree that it passes to FIT payloads, e.g. 122 * on arm64, and only lists it in the memory reservation block. 123 * 124 * Anything else, such as no-map regions on arm64, which are IORESOURCE_MEM 125 * (see request_standard_resources() in arch/arm64/kernel/setup.c), must not 126 * be mapped. 127 */ 128 static bool cbmem_entry_is_mappable(const struct lb_cbmem_entry *entry) 129 { 130 resource_size_t start = entry->address; 131 size_t size = entry->entry_size; 132 133 return region_intersects(start, size, IORESOURCE_SYSTEM_RAM, 134 IORES_DESC_NONE) == REGION_INTERSECTS || 135 region_intersects(start, size, IORESOURCE_MEM, 136 IORES_DESC_RESERVED) == REGION_INTERSECTS || 137 region_intersects(start, size, IORESOURCE_MEM, 138 IORES_DESC_NONE) == REGION_DISJOINT; 139 } 140 141 static int coreboot_table_populate(struct device *dev, void *ptr, resource_size_t len) 142 { 143 int i, ret; 144 void *ptr_entry; 145 struct coreboot_device *device; 146 struct coreboot_table_entry *entry; 147 struct coreboot_table_header *header = ptr; 148 void *ptr_end; 149 150 ptr_end = ptr + len; 151 ptr_entry = ptr + header->header_bytes; 152 for (i = 0; i < header->table_entries; i++, ptr_entry += entry->size) { 153 if (ptr_entry + sizeof(*entry) > ptr_end) 154 return -EINVAL; 155 entry = ptr_entry; 156 157 if (entry->size < sizeof(*entry)) { 158 dev_warn(dev, "coreboot table entry too small!\n"); 159 return -EINVAL; 160 } 161 162 if (ptr_entry + entry->size > ptr_end) 163 return -EINVAL; 164 165 device = kzalloc(sizeof(device->dev) + entry->size, GFP_KERNEL); 166 if (!device) 167 return -ENOMEM; 168 169 device->dev.parent = dev; 170 device->dev.bus = &coreboot_bus_type; 171 device->dev.release = coreboot_device_release; 172 memcpy(device->raw, ptr_entry, entry->size); 173 174 switch (device->entry.tag) { 175 case LB_TAG_CBMEM_ENTRY: 176 if (!cbmem_entry_is_mappable(&device->cbmem_entry)) { 177 kfree(device); 178 continue; 179 } 180 dev_set_name(&device->dev, "cbmem-%08x", 181 device->cbmem_entry.id); 182 break; 183 default: 184 dev_set_name(&device->dev, "coreboot%d", i); 185 break; 186 } 187 188 ret = device_register(&device->dev); 189 if (ret) { 190 dev_warn(dev, "failed to register coreboot device: %d\n", ret); 191 put_device(&device->dev); 192 } 193 } 194 195 return 0; 196 } 197 198 static int coreboot_table_probe(struct platform_device *pdev) 199 { 200 resource_size_t len; 201 resource_size_t table_span; 202 struct coreboot_table_header *header; 203 struct resource *res; 204 struct device *dev = &pdev->dev; 205 void *ptr; 206 int ret; 207 208 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 209 if (!res) 210 return -EINVAL; 211 212 len = resource_size(res); 213 if (!res->start || len < sizeof(*header)) 214 return -EINVAL; 215 216 /* Check just the header first to make sure things are sane */ 217 header = memremap(res->start, sizeof(*header), MEMREMAP_WB); 218 if (!header) 219 return -ENOMEM; 220 221 ret = strncmp(header->signature, "LBIO", sizeof(header->signature)); 222 223 if (!ret && 224 (header->header_bytes < sizeof(*header) || 225 check_add_overflow((resource_size_t)header->header_bytes, 226 (resource_size_t)header->table_bytes, 227 &table_span) || 228 table_span > len)) 229 ret = -EINVAL; 230 231 memunmap(header); 232 if (ret) { 233 dev_warn(dev, "coreboot table missing or corrupt!\n"); 234 return -ENODEV; 235 } 236 237 ptr = memremap(res->start, table_span, MEMREMAP_WB); 238 if (!ptr) 239 return -ENOMEM; 240 241 ret = coreboot_table_populate(dev, ptr, table_span); 242 243 memunmap(ptr); 244 245 return ret; 246 } 247 248 static int __cb_dev_unregister(struct device *dev, void *dummy) 249 { 250 device_unregister(dev); 251 return 0; 252 } 253 254 static void coreboot_table_remove(struct platform_device *pdev) 255 { 256 bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister); 257 } 258 259 #ifdef CONFIG_ACPI 260 static const struct acpi_device_id cros_coreboot_acpi_match[] = { 261 { .id = "GOOGCB00" }, 262 { .id = "BOOT0000" }, 263 { } 264 }; 265 MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match); 266 #endif 267 268 #ifdef CONFIG_OF 269 static const struct of_device_id coreboot_of_match[] = { 270 { .compatible = "coreboot" }, 271 {} 272 }; 273 MODULE_DEVICE_TABLE(of, coreboot_of_match); 274 #endif 275 276 static struct platform_driver coreboot_table_driver = { 277 .probe = coreboot_table_probe, 278 .remove = coreboot_table_remove, 279 .driver = { 280 .name = "coreboot_table", 281 .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match), 282 .of_match_table = of_match_ptr(coreboot_of_match), 283 }, 284 }; 285 286 static int __init coreboot_table_driver_init(void) 287 { 288 int ret; 289 290 ret = bus_register(&coreboot_bus_type); 291 if (ret) 292 return ret; 293 294 ret = platform_driver_register(&coreboot_table_driver); 295 if (ret) { 296 bus_unregister(&coreboot_bus_type); 297 return ret; 298 } 299 300 return 0; 301 } 302 303 static void __exit coreboot_table_driver_exit(void) 304 { 305 platform_driver_unregister(&coreboot_table_driver); 306 bus_unregister(&coreboot_bus_type); 307 } 308 309 subsys_initcall(coreboot_table_driver_init); 310 module_exit(coreboot_table_driver_exit); 311 312 MODULE_AUTHOR("Google, Inc."); 313 MODULE_DESCRIPTION("Module providing coreboot table access"); 314 MODULE_LICENSE("GPL"); 315