[PATCH 2/2] firmware: google: Expose coreboot tables over sysfs

From: patrick . rudolph
Date: Fri Nov 15 2019 - 11:16:26 EST


From: Patrick Rudolph <patrick.rudolph@xxxxxxxxxxxxx>

Make all coreboot table entries available to userland. This is useful for
tools that are currently using /dev/mem.

Besides the tag and size also expose the raw table data itself.

Tools can easily scan for the right coreboot table by reading
/sys/bus/coreboot/devices/coreboot*/attributes/id
The binary table data can then be read from
/sys/bus/coreboot/devices/coreboot*/attributes/data

Signed-off-by: Patrick Rudolph <patrick.rudolph@xxxxxxxxxxxxx>
---
drivers/firmware/google/coreboot_table.c | 60 ++++++++++++++++++++++++
1 file changed, 60 insertions(+)

diff --git a/drivers/firmware/google/coreboot_table.c b/drivers/firmware/google/coreboot_table.c
index 8d132e4f008a..baddf28a2103 100644
--- a/drivers/firmware/google/coreboot_table.c
+++ b/drivers/firmware/google/coreboot_table.c
@@ -6,6 +6,7 @@
*
* Copyright 2017 Google Inc.
* Copyright 2017 Samuel Holland <samuel@xxxxxxxxxxxx>
+ * Copyright 2019 9elements Agency GmbH
*/

#include <linux/acpi.h>
@@ -84,6 +85,63 @@ void coreboot_driver_unregister(struct coreboot_driver *driver)
}
EXPORT_SYMBOL(coreboot_driver_unregister);

+static ssize_t id_show(struct device *dev,
+ struct device_attribute *attr, char *buffer)
+{
+ struct coreboot_device *device = CB_DEV(dev);
+
+ return sprintf(buffer, "%08x\n", device->entry.tag);
+}
+
+static ssize_t size_show(struct device *dev,
+ struct device_attribute *attr, char *buffer)
+{
+ struct coreboot_device *device = CB_DEV(dev);
+
+ return sprintf(buffer, "%u\n", device->entry.size);
+}
+
+static DEVICE_ATTR_RO(id);
+static DEVICE_ATTR_RO(size);
+
+static struct attribute *cb_dev_attrs[] = {
+ &dev_attr_id.attr,
+ &dev_attr_size.attr,
+ NULL
+};
+
+static ssize_t table_data_read(struct file *filp, struct kobject *kobj,
+ struct bin_attribute *bin_attr,
+ char *buffer, loff_t offset, size_t count)
+{
+ struct device *dev = kobj_to_dev(kobj);
+ struct coreboot_device *device = CB_DEV(dev);
+
+ return memory_read_from_buffer(buffer, count, &offset,
+ &device->entry, device->entry.size);
+}
+
+static struct bin_attribute coreboot_attr_data = {
+ .attr = { .name = "data", .mode = 0444 },
+ .read = table_data_read,
+};
+
+static struct bin_attribute *cb_dev_bin_attrs[] = {
+ &coreboot_attr_data,
+ NULL
+};
+
+static const struct attribute_group cb_dev_attr_group = {
+ .name = "attributes",
+ .attrs = cb_dev_attrs,
+ .bin_attrs = cb_dev_bin_attrs,
+};
+
+static const struct attribute_group *cb_dev_attr_groups[] = {
+ &cb_dev_attr_group,
+ NULL
+};
+
static int coreboot_table_populate(struct device *dev, void *ptr)
{
int i, ret;
@@ -104,6 +162,8 @@ static int coreboot_table_populate(struct device *dev, void *ptr)
device->dev.parent = dev;
device->dev.bus = &coreboot_bus_type;
device->dev.release = coreboot_device_release;
+ device->dev.groups = cb_dev_attr_groups;
+
memcpy(&device->entry, ptr_entry, entry->size);

ret = device_register(&device->dev);
--
2.21.0