firmware: google: Implement vboot workbuf in sysfs
PATCH 1/1 firmware: google: Implement vboot workbuf in sysfs
The vboot workbuf is a large data structure exposed by coreboot for payloads and the operating system to use. Existing tools like crossystem re-create this structure in userspace by reading the deprecated vboot v1 VBSD structure from FDT/ACPI, and translating it back to a vboot v2 workbuf. Since not all data required for vboot v2 is available in the VBSD structure, crossystem also has to shell out to tools like flashrom to fill in the missing bits.
Since coreboot commit a8bda43, this workbuf is also available in CBMEM, and since workbuf struct version 2 (vboot commit ecdca93), this data also contains the vboot context, making it useful. Exposing it via sysfs enables tools like crossystem significantly easier access to read and manipulate vboot variables.
--- drivers/firmware/google/Kconfig | 8 ++ drivers/firmware/google/Makefile | 1 + drivers/firmware/google/vboot.c | 173 +++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 drivers/firmware/google/vboot.c
drivers/firmware/google/Kconfig +8 -0
@@ -67,6 +67,14 @@ config GOOGLE_MEMCONSOLE_COREBOOT the coreboot table. If found, this log is exported to userland in the file /sys/firmware/log. +config GOOGLE_VBOOT_SYSFS + tristate "Verified Boot in sysfs" + depends on GOOGLE_COREBOOT_TABLE + help + This option enables the kernel to search for the Verified Boot + workbuf coreboot table. If found, the workbuf is exported + to userland in /sys/firmware/vboot. + config GOOGLE_VPD tristate "Vital Product Data" depends on GOOGLE_COREBOOT_TABLE
drivers/firmware/google/Makefile +1 -0
@@ -6,6 +6,7 @@ obj-$(CONFIG_GOOGLE_FRAMEBUFFER_COREBOOT) += framebuffer-coreboot.o obj-$(CONFIG_GOOGLE_MEMCONSOLE) += memconsole.o obj-$(CONFIG_GOOGLE_MEMCONSOLE_COREBOOT) += memconsole-coreboot.o obj-$(CONFIG_GOOGLE_MEMCONSOLE_X86_LEGACY) += memconsole-x86-legacy.o +obj-$(CONFIG_GOOGLE_VBOOT_SYSFS) += vboot.o vpd-sysfs-y := vpd.o vpd_decode.o obj-$(CONFIG_GOOGLE_VPD) += vpd-sysfs.o
drivers/firmware/google/vboot.c new file +173 -0
173 lines not on this page · raw message
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
Sorry, this wasn't quite what I meant. I think we should definitely not hardcode any details about the vboot data structure layout here. It's already enough of a pain to keep crossystem in sync with different data structure versions, we absolutely don't want to have to keep updating that in the kernel as well.
I assume that the reason you went that route seems to have been mostly that the lb_cbmem_ref coreboot table entry has no size field, so you had to infer the size from within the data structure. Thankfully, we don't need to use lb_cbmem_ref for this, that's somewhat of a legacy entry type that we're trying to phase out where it's no longer needed for backwards-compatibility anyway (and in fact I think we should be okay to remove the vboot entry there nowadays). We also have lb_cbmem_entry (see
https://review.coreboot.org/plugins/gitiles/coreboot/+/refs/heads/master/src/lib/imd_cbmem.c#222)
that exports info about every area in CBMEM, with address, size and CBMEM ID. The vboot workbuffer is CBMEM ID 0x78007343.
I think we should just add a general driver for lb_cbmem_entry tags here, that uses the "id" as (part of) the device name and contains node to read/write the raw bytes of the buffer. Then crossystem can easily find the right one for vboot, and the infrastructure may also come in handy for other uses (or debugging) in the future.
Quoting Julius Werner (2022-07-26 17:26:41)
Quoted 18 lines
Sorry, this wasn't quite what I meant. I think we should definitely not hardcode any details about the vboot data structure layout here. It's already enough of a pain to keep crossystem in sync with different data structure versions, we absolutely don't want to have to keep updating that in the kernel as well.
I assume that the reason you went that route seems to have been mostly that the
lb_cbmem_refcoreboot table entry has no size field, so you had to infer the size from within the data structure. Thankfully, we don't need to uselb_cbmem_reffor this, that's somewhat of a legacy entry type that we're trying to phase out where it's no longer needed for backwards-compatibility anyway (and in fact I think we should be okay to remove the vboot entry there nowadays). We also havelb_cbmem_entry(see
https://review.coreboot.org/plugins/gitiles/coreboot/+/refs/heads/master/src/lib/imd_cbmem.c#222)
that exports info about every area in CBMEM, with address, size and CBMEM ID. The vboot workbuffer is CBMEM ID0x78007343.
Quoted 5 lines
I think we should just add a general driver for
lb_cbmem_entrytags here, that uses the "id" as (part of) the device name and contains node to read/write the raw bytes of the buffer. Then crossystem can easily find the right one for vboot, and the infrastructure may also come in handy for other uses (or debugging) in the future.
If there's nothing to really "drive" then a driver is overkill. Would exposing some raw bytes in /sys/firmware/coreboot be sufficient here, possibly under some sort of /sys/firmware/coreboot/cbmem_entries/ path?
I honestly have no idea what vboot workbuffer is though so maybe I'm just talking nonsense. If this ends up in sysfs please document the files in Documentation/ABI/ and include it in the patch so we know what is being exposed in the kernel ABI.
Thanks for the feedback. I will re-work this to be a more generic cbmem sysfs interface.
Jack
PATCH v3 1/1 firmware: google: Implement cbmem in sysfs driver
cbmem entries can be read from coreboot table0x31 (LB_TAG_CBMEM_ENTRY). This commit exports access to cbmem entries in sysfs under /sys/firmware/coreboot/cbmem-*.
--- .../ABI/testing/sysfs-firmware-coreboot | 16 + drivers/firmware/google/Kconfig | 8 + drivers/firmware/google/Makefile | 3 + drivers/firmware/google/cbmem.c | 408 ++++++++++++++++++ drivers/firmware/google/coreboot_table.h | 11 + 5 files changed, 446 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-firmware-coreboot create mode 100644 drivers/firmware/google/cbmem.c
Documentation/ABI/testing/sysfs-firmware-coreboot new file +16 -0
@@ -0,0 +1,16 @@ +What: /sys/firmware/coreboot/ +Date: July 2022 +Contact: Jack Rosenthal <[email protected]> +Description: + Coreboot-based BIOS firmware provides a variety of information + in CBMEM. Each CBMEM entry can be found via Coreboot tables. + For each CBMEM entry, the following are exposed: + + address: A hexidecimal value of the memory address the data for + the entry begins at. + size: The size of the data stored. + id: The id corresponding to the entry. A list of ids known to + coreboot can be found in the coreboot tree at + src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h + alias: A common name for the id of the entry. + mem: A file exposing the raw memory for the entry.
drivers/firmware/google/Kconfig +8 -0
@@ -19,6 +19,14 @@ config GOOGLE_SMI driver provides an interface for reading and writing NVRAM variables. +config GOOGLE_CBMEM + tristate "CBMEM entries in sysfs" + depends on GOOGLE_COREBOOT_TABLE + help + This option enables the kernel to search for Coreboot CBMEM + entries, and expose the memory for each entry in sysfs under + /sys/firmware/coreboot. + config GOOGLE_COREBOOT_TABLE tristate "Coreboot Table Access" depends on HAS_IOMEM && (ACPI || OF)
drivers/firmware/google/Makefile +3 -0
@@ -7,5 +7,8 @@ obj-$(CONFIG_GOOGLE_MEMCONSOLE) += memconsole.o obj-$(CONFIG_GOOGLE_MEMCONSOLE_COREBOOT) += memconsole-coreboot.o obj-$(CONFIG_GOOGLE_MEMCONSOLE_X86_LEGACY) += memconsole-x86-legacy.o +# Must come after coreboot_table.o, as this driver depends on that bus type. +obj-$(CONFIG_GOOGLE_CBMEM) += cbmem.o + vpd-sysfs-y := vpd.o vpd_decode.o obj-$(CONFIG_GOOGLE_VPD) += vpd-sysfs.o
drivers/firmware/google/cbmem.c new file +408 -0
408 lines not on this page · raw message
drivers/firmware/google/coreboot_table.h +11 -0
@@ -39,6 +39,16 @@ struct lb_cbmem_ref {u64 cbmem_addr; }; +/* Corresponds to LB_TAG_CBMEM_ENTRY. */ +struct lb_cbmem_entry { +u32 tag; +u32 size; + +u64 address; +u32 entry_size; +u32 id; +}; +/* Describes framebuffer setup by coreboot */ struct lb_framebuffer {u32 tag;
@@ -65,6 +75,7 @@ struct coreboot_device {union {struct coreboot_table_entry entry;struct lb_cbmem_ref cbmem_ref; +struct lb_cbmem_entry cbmem_entry;struct lb_framebuffer framebuffer; }; };
Signature · 1 line
2.37.1.455.g008518b4e5-goog
Re: [PATCH v3] firmware: google: Implement cbmem in sysfs driver
Thanks, I think this looks great in general!
Quoted 1 line
+ * Copyright 2022 Google Inc.
LLC
drivers/firmware/google/cbmem.c line 35
Quoted 11 lines
drivers/firmware/google/cbmem.c new file +11
-0+/* +* This list should be kept in sync with +* src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h from coreboot. +*/ +static const struct { +const char *alias; +u32 id; +} cbmem_aliases[] = { + {"acpi" ,0x41435049 }, + {"acpi-bert" ,0x42455254 }, + {"acpi-cnvs" ,0x434e5653 },
I don't think we really want to keep this in the kernel? New entries get added here all the time, I think it would really be way more effort than benefit to keep this in sync. It also doesn't really mesh with the idea that this is a thin interface in the kernel that doesn't do any heavyweight interpretation. I'd say keep out the alias stuff and just have the numeric nodes only.
drivers/firmware/google/cbmem.c line 168
Quoted 8 lines
drivers/firmware/google/cbmem.c new file +8
-0+static ssize_t cbmem_entry_mem_write (struct file *filp,struct kobject *kobp, +struct bin_attribute *bin_attr,char *buf, +loff_t pos,size_t count) +{ +struct cbmem_entry *entry = bin_attr->private; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ;
Not sure what the current standard kernel policy on these things is, but we already have file permission bits to control this so generally I'd leave it at that and let userspace decide how to manage access. (Forcing everything that could possibly be security-sensitive to be "root only" just means more things are forced to run as root, which is also not a good thing.)
drivers/firmware/google/cbmem.c line 261
Quoted 9 lines
drivers/firmware/google/cbmem.c new file +9
-0+static int cbmem_entry_setup (struct cbmem_entry *entry) +{ +int ret; + + entry->mem_file_buf =memremap (entry->dev->cbmem_entry.address, + entry->dev->cbmem_entry.entry_size, +MEMREMAP_WB ); +if (!entry->mem_file_buf) +return -ENOMEM ;
Can this use devm_memremap() for automatic cleanup?
Quoted 1 line
+MODULE_AUTHOR("Google, Inc.");
LLC
PATCH v4 1/1 firmware: google: Implement cbmem in sysfs driver
cbmem entries can be read from coreboot table0x31 (LB_TAG_CBMEM_ENTRY). This commit exports access to cbmem entries in sysfs under /sys/firmware/coreboot/cbmem-*.
--- .../ABI/testing/sysfs-firmware-coreboot | 15 ++ drivers/firmware/google/Kconfig | 8 + drivers/firmware/google/Makefile | 3 + drivers/firmware/google/cbmem.c | 232 ++++++++++++++++++ drivers/firmware/google/coreboot_table.h | 11 + 5 files changed, 269 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-firmware-coreboot create mode 100644 drivers/firmware/google/cbmem.c
Documentation/ABI/testing/sysfs-firmware-coreboot new file +15 -0
@@ -0,0 +1,15 @@ +What: /sys/firmware/coreboot/ +Date: July 2022 +Contact: Jack Rosenthal <[email protected]> +Description: + Coreboot-based BIOS firmware provides a variety of information + in CBMEM. Each CBMEM entry can be found via Coreboot tables. + For each CBMEM entry, the following are exposed: + + address: A hexidecimal value of the memory address the data for + the entry begins at. + size: The size of the data stored. + id: The id corresponding to the entry. A list of ids known to + coreboot can be found in the coreboot tree at + src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h + mem: A file exposing the raw memory for the entry.
drivers/firmware/google/Kconfig +8 -0
@@ -19,6 +19,14 @@ config GOOGLE_SMI driver provides an interface for reading and writing NVRAM variables. +config GOOGLE_CBMEM + tristate "CBMEM entries in sysfs" + depends on GOOGLE_COREBOOT_TABLE + help + This option enables the kernel to search for Coreboot CBMEM + entries, and expose the memory for each entry in sysfs under + /sys/firmware/coreboot. + config GOOGLE_COREBOOT_TABLE tristate "Coreboot Table Access" depends on HAS_IOMEM && (ACPI || OF)
drivers/firmware/google/Makefile +3 -0
@@ -7,5 +7,8 @@ obj-$(CONFIG_GOOGLE_MEMCONSOLE) += memconsole.o obj-$(CONFIG_GOOGLE_MEMCONSOLE_COREBOOT) += memconsole-coreboot.o obj-$(CONFIG_GOOGLE_MEMCONSOLE_X86_LEGACY) += memconsole-x86-legacy.o +# Must come after coreboot_table.o, as this driver depends on that bus type. +obj-$(CONFIG_GOOGLE_CBMEM) += cbmem.o + vpd-sysfs-y := vpd.o vpd_decode.o obj-$(CONFIG_GOOGLE_VPD) += vpd-sysfs.o
drivers/firmware/google/cbmem.c new file +232 -0
232 lines not on this page · raw message
drivers/firmware/google/coreboot_table.h +11 -0
@@ -39,6 +39,16 @@ struct lb_cbmem_ref {u64 cbmem_addr; }; +/* Corresponds to LB_TAG_CBMEM_ENTRY. */ +struct lb_cbmem_entry { +u32 tag; +u32 size; + +u64 address; +u32 entry_size; +u32 id; +}; +/* Describes framebuffer setup by coreboot */ struct lb_framebuffer {u32 tag;
@@ -65,6 +75,7 @@ struct coreboot_device {union {struct coreboot_table_entry entry;struct lb_cbmem_ref cbmem_ref; +struct lb_cbmem_entry cbmem_entry;struct lb_framebuffer framebuffer; }; };
Signature · 1 line
2.37.1.455.g008518b4e5-goog