From nobody Sat Apr 11 05:11:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5CBEC00140 for ; Tue, 16 Aug 2022 00:54:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243122AbiHPAyI (ORCPT ); Mon, 15 Aug 2022 20:54:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350120AbiHPAqv (ORCPT ); Mon, 15 Aug 2022 20:46:51 -0400 Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFED4D8E2B for ; Mon, 15 Aug 2022 13:45:40 -0700 (PDT) Received: by mail-io1-xd30.google.com with SMTP id e192so1313534iof.5 for ; Mon, 15 Aug 2022 13:45:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc; bh=tIu5C5uMPp111gSPdwc0Dp76L1hJvvSseLDiJjEoffM=; b=OjOMTHnBF7JQvxSwuS5IlDrkhXB4ASdYWoIV0ZgNsSXXty6xl1f3qwaR7yTDBMX9g8 i+drsi5itN2ofE+HkJIQ9riLu9jDbGJKPBE1/wXV6x3UwV/Z6F8JFK5CST8ReNH2rC5v 5eMAhF4ECwEHQ8BbbSHUJVmxyAKY+WOJi8pK0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=tIu5C5uMPp111gSPdwc0Dp76L1hJvvSseLDiJjEoffM=; b=4eYyHDprxZpG61+NJZzieDqKCwmdJpmc1yke/IxgwOrceTZbisL069wmUKsFbR7Klx OTVQL+BrbkHMjk6Ng5+b/7w5a9iNZ7qZgnEd1ybcRhpjDbNuHH73gb1FGYp21tP1Ma1S BH40tp9uSnJ2qHEJbNOwwgy+l+qtbSHnNYL7Se6AVAJb76rZWuiX07cUCXVSgLbPt3FB BoRV05aBv7llGt2g6JRsvlAG8io34PWRTrdJJdKBWOxBqqelS/EN8gCc8XKc+PaJE/bY tsIhaZI9cPVP7pbuMGFtrjf1fmx0ZHr4mElzJusvvRoPobSssuefnjkDYmqQBRKs5IrA MW8Q== X-Gm-Message-State: ACgBeo3ozh3+OFc3Pa+XtxuCYVPpYY1YxAzfZRa4vvB6b6uCy8bSn03z L6aHCtHMJ5mBhAdv/b+IQAJRB2qOKG8AhX+0 X-Google-Smtp-Source: AA6agR58XhABPFk1wERmMO21JeF8rkLt5CsPxCCU7eEd8uo6G/bKSTlM8uuNQh0Zxkz914v5cMLfgw== X-Received: by 2002:a05:6638:4807:b0:343:5eb7:7f7 with SMTP id cp7-20020a056638480700b003435eb707f7mr8334999jab.29.1660596337411; Mon, 15 Aug 2022 13:45:37 -0700 (PDT) Received: from jrosenth45.corp.google.com ([100.107.108.157]) by smtp.gmail.com with ESMTPSA id w8-20020a023948000000b0034366d9ff06sm3661593jae.155.2022.08.15.13.45.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Aug 2022 13:45:36 -0700 (PDT) From: Jack Rosenthal To: linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev Cc: Jack Rosenthal , Stephen Boyd , Tzung-Bi Shih , Guenter Roeck , Julius Werner Subject: [PATCH v9] firmware: google: Implement cbmem in sysfs driver Date: Mon, 15 Aug 2022 14:45:11 -0600 Message-Id: <20220815204511.621569-1-jrosenth@chromium.org> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The CBMEM area is a downward-growing memory region used by coreboot to dynamically allocate tagged data structures ("CBMEM entries") that remain resident during boot. This implements a driver which exports access to the CBMEM entries via sysfs under /sys/firmware/coreboot/cbmem/. This implementation is quite versatile. Examples of how it could be used are given below: * Tools like util/cbmem from the coreboot tree could use this driver instead of finding CBMEM in /dev/mem directly. Alternatively, firmware developers debugging an issue may find the sysfs interface more ergonomic than the cbmem tool and choose to use it directly. * The crossystem tool, which exposes verified boot variables, can use this driver to read the vboot work buffer. * Tools which read the BIOS SPI flash (e.g., flashrom) can find the flash layout in CBMEM directly, which is significantly faster than searching the flash directly. Link: https://issuetracker.google.com/239604743 Cc: Stephen Boyd Cc: Tzung-Bi Shih Reviewed-by: Guenter Roeck Reviewed-by: Julius Werner Tested-by: Jack Rosenthal Signed-off-by: Jack Rosenthal --- v9: * Removed unnecessary include linux/ctype.h. * Made /sys/firmware/coreboot/cbmem//mem provide read-only access only, and the permissions are now 0400. * Added .owner =3D THIS_MODULE. * Corrected coreboot_kobj -> cbmem_kobj. * Updated Kconfig help message. * Updated commit message to add much mode detail. .../ABI/testing/sysfs-firmware-coreboot | 49 ++++ drivers/firmware/google/Kconfig | 8 + drivers/firmware/google/Makefile | 3 + drivers/firmware/google/cbmem.c | 215 ++++++++++++++++++ drivers/firmware/google/coreboot_table.c | 10 + drivers/firmware/google/coreboot_table.h | 16 ++ 6 files changed, 301 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-firmware-coreboot create mode 100644 drivers/firmware/google/cbmem.c diff --git a/Documentation/ABI/testing/sysfs-firmware-coreboot b/Documentat= ion/ABI/testing/sysfs-firmware-coreboot new file mode 100644 index 000000000000..5432dea2408c --- /dev/null +++ b/Documentation/ABI/testing/sysfs-firmware-coreboot @@ -0,0 +1,49 @@ +What: /sys/firmware/coreboot/ +Date: August 2022 +Contact: Jack Rosenthal +Description: + Kernel objects associated with the Coreboot-based BIOS firmware. + +What: /sys/firmware/coreboot/cbmem/ +Date: August 2022 +Contact: Jack Rosenthal +Description: + Coreboot provides a variety of information in CBMEM. This + directory contains each CBMEM entry, which can be found via + Coreboot tables. + +What: /sys/firmware/coreboot/cbmem// +Date: August 2022 +Contact: Jack Rosenthal +Description: + Each CBMEM entry is given a directory based on the id + corresponding to the entry. A list of ids known to coreboot can + be found in the coreboot source tree at + ``src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h``. + +What: /sys/firmware/coreboot/cbmem//address +Date: August 2022 +Contact: Jack Rosenthal +Description: + The memory address that the CBMEM entry's data begins at. + +What: /sys/firmware/coreboot/cbmem//size +Date: August 2022 +Contact: Jack Rosenthal +Description: + The size of the data being stored. + +What: /sys/firmware/coreboot/cbmem//id +Date: August 2022 +Contact: Jack Rosenthal +Description: + The CBMEM id corresponding to the entry. + +What: /sys/firmware/coreboot/cbmem//mem +Date: August 2022 +Contact: Jack Rosenthal +Description: + A file exposing read-only access to the entry's data. Note + that this file does not support mmap(), and should be used for + basic data access only. Users requiring mmap() should read the + address and size files, and mmap() /dev/mem. diff --git a/drivers/firmware/google/Kconfig b/drivers/firmware/google/Kcon= fig index 983e07dc022e..b0f7a24fd90a 100644 --- a/drivers/firmware/google/Kconfig +++ b/drivers/firmware/google/Kconfig @@ -19,6 +19,14 @@ config GOOGLE_SMI driver provides an interface for reading and writing NVRAM variables. =20 +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/cbmem. + config GOOGLE_COREBOOT_TABLE tristate "Coreboot Table Access" depends on HAS_IOMEM && (ACPI || OF) diff --git a/drivers/firmware/google/Makefile b/drivers/firmware/google/Mak= efile index d17caded5d88..8151e323cc43 100644 --- a/drivers/firmware/google/Makefile +++ b/drivers/firmware/google/Makefile @@ -7,5 +7,8 @@ obj-$(CONFIG_GOOGLE_MEMCONSOLE) +=3D memconsole.o obj-$(CONFIG_GOOGLE_MEMCONSOLE_COREBOOT) +=3D memconsole-coreboot.o obj-$(CONFIG_GOOGLE_MEMCONSOLE_X86_LEGACY) +=3D memconsole-x86-legacy.o =20 +# Must come after coreboot_table.o, as this driver depends on that bus typ= e. +obj-$(CONFIG_GOOGLE_CBMEM) +=3D cbmem.o + vpd-sysfs-y :=3D vpd.o vpd_decode.o obj-$(CONFIG_GOOGLE_VPD) +=3D vpd-sysfs.o diff --git a/drivers/firmware/google/cbmem.c b/drivers/firmware/google/cbme= m.c new file mode 100644 index 000000000000..cbfe8f388097 --- /dev/null +++ b/drivers/firmware/google/cbmem.c @@ -0,0 +1,215 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * cbmem.c + * + * Driver for exporting cbmem entries in sysfs. + * + * Copyright 2022 Google LLC + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "coreboot_table.h" + +#define LB_TAG_CBMEM_ENTRY 0x31 + +static struct kobject *cbmem_kobj; + +struct cbmem_entry; +struct cbmem_entry_attr { + struct kobj_attribute kobj_attr; + struct cbmem_entry *entry; +}; + +struct cbmem_entry { + struct kobject *kobj; + struct coreboot_device *dev; + struct bin_attribute mem_file; + char *mem_file_buf; + struct cbmem_entry_attr address_file; + struct cbmem_entry_attr size_file; + struct cbmem_entry_attr id_file; +}; + +static struct cbmem_entry_attr *to_cbmem_entry_attr(struct kobj_attribute = *a) +{ + return container_of(a, struct cbmem_entry_attr, kobj_attr); +} + +static ssize_t cbmem_entry_mem_read(struct file *filp, struct kobject *kob= p, + struct bin_attribute *bin_attr, char *buf, + loff_t pos, size_t count) +{ + struct cbmem_entry *entry =3D bin_attr->private; + + return memory_read_from_buffer(buf, count, &pos, entry->mem_file_buf, + bin_attr->size); +} + +static ssize_t cbmem_entry_address_show(struct kobject *kobj, + struct kobj_attribute *a, char *buf) +{ + struct cbmem_entry_attr *entry_attr =3D to_cbmem_entry_attr(a); + + return sysfs_emit(buf, "0x%llx\n", + entry_attr->entry->dev->cbmem_entry.address); +} + +static ssize_t cbmem_entry_size_show(struct kobject *kobj, + struct kobj_attribute *a, char *buf) +{ + struct cbmem_entry_attr *entry_attr =3D to_cbmem_entry_attr(a); + + return sysfs_emit(buf, "0x%x\n", + entry_attr->entry->dev->cbmem_entry.entry_size); +} + +static ssize_t cbmem_entry_id_show(struct kobject *kobj, + struct kobj_attribute *a, char *buf) +{ + struct cbmem_entry_attr *entry_attr =3D to_cbmem_entry_attr(a); + + return sysfs_emit(buf, "0x%08x\n", + entry_attr->entry->dev->cbmem_entry.id); +} + +static int cbmem_entry_setup(struct cbmem_entry *entry) +{ + int ret; + char *kobj_name; + + entry->mem_file_buf =3D + devm_memremap(&entry->dev->dev, entry->dev->cbmem_entry.address, + entry->dev->cbmem_entry.entry_size, MEMREMAP_WB); + if (!entry->mem_file_buf) + return -ENOMEM; + + kobj_name =3D devm_kasprintf(&entry->dev->dev, GFP_KERNEL, "%08x", + entry->dev->cbmem_entry.id); + if (!kobj_name) + return -ENOMEM; + + entry->kobj =3D kobject_create_and_add(kobj_name, cbmem_kobj); + if (!entry->kobj) + return -ENOMEM; + + sysfs_bin_attr_init(&entry->mem_file); + entry->mem_file.attr.name =3D "mem"; + entry->mem_file.attr.mode =3D 0400; + entry->mem_file.size =3D entry->dev->cbmem_entry.entry_size; + entry->mem_file.read =3D cbmem_entry_mem_read; + entry->mem_file.private =3D entry; + ret =3D sysfs_create_bin_file(entry->kobj, &entry->mem_file); + if (ret) + goto free_kobj; + + sysfs_attr_init(&entry->address_file.kobj_attr.attr); + entry->address_file.kobj_attr.attr.name =3D "address"; + entry->address_file.kobj_attr.attr.mode =3D 0444; + entry->address_file.kobj_attr.show =3D cbmem_entry_address_show; + entry->address_file.entry =3D entry; + ret =3D sysfs_create_file(entry->kobj, + &entry->address_file.kobj_attr.attr); + if (ret) + goto free_mem_file; + + sysfs_attr_init(&entry->size_file.kobj_attr.attr); + entry->size_file.kobj_attr.attr.name =3D "size"; + entry->size_file.kobj_attr.attr.mode =3D 0444; + entry->size_file.kobj_attr.show =3D cbmem_entry_size_show; + entry->size_file.entry =3D entry; + ret =3D sysfs_create_file(entry->kobj, &entry->size_file.kobj_attr.attr); + if (ret) + goto free_address_file; + + sysfs_attr_init(&entry->id_file.kobj_attr.attr); + entry->id_file.kobj_attr.attr.name =3D "id"; + entry->id_file.kobj_attr.attr.mode =3D 0444; + entry->id_file.kobj_attr.show =3D cbmem_entry_id_show; + entry->id_file.entry =3D entry; + ret =3D sysfs_create_file(entry->kobj, &entry->id_file.kobj_attr.attr); + if (ret) + goto free_size_file; + + return 0; + +free_size_file: + sysfs_remove_file(entry->kobj, &entry->size_file.kobj_attr.attr); +free_address_file: + sysfs_remove_file(entry->kobj, &entry->address_file.kobj_attr.attr); +free_mem_file: + sysfs_remove_bin_file(entry->kobj, &entry->mem_file); +free_kobj: + kobject_put(entry->kobj); + return ret; +} + +static int cbmem_entry_probe(struct coreboot_device *dev) +{ + struct cbmem_entry *entry; + + entry =3D devm_kzalloc(&dev->dev, sizeof(*entry), GFP_KERNEL); + if (!entry) + return -ENOMEM; + + dev_set_drvdata(&dev->dev, entry); + entry->dev =3D dev; + return cbmem_entry_setup(entry); +} + +static void cbmem_entry_remove(struct coreboot_device *dev) +{ + struct cbmem_entry *entry =3D dev_get_drvdata(&dev->dev); + + sysfs_remove_bin_file(entry->kobj, &entry->mem_file); + sysfs_remove_file(entry->kobj, &entry->address_file.kobj_attr.attr); + sysfs_remove_file(entry->kobj, &entry->size_file.kobj_attr.attr); + sysfs_remove_file(entry->kobj, &entry->id_file.kobj_attr.attr); + kobject_put(entry->kobj); +} + +static struct coreboot_driver cbmem_entry_driver =3D { + .probe =3D cbmem_entry_probe, + .remove =3D cbmem_entry_remove, + .drv =3D { + .name =3D "cbmem", + .owner =3D THIS_MODULE, + }, + .tag =3D LB_TAG_CBMEM_ENTRY, +}; + +static int __init cbmem_init(void) +{ + int ret; + + cbmem_kobj =3D kobject_create_and_add("cbmem", coreboot_kobj); + if (!cbmem_kobj) + return -ENOMEM; + + ret =3D coreboot_driver_register(&cbmem_entry_driver); + if (ret) { + kobject_put(cbmem_kobj); + return ret; + } + + return 0; +} +module_init(cbmem_init); + +static void __exit cbmem_exit(void) +{ + kobject_put(cbmem_kobj); + coreboot_driver_unregister(&cbmem_entry_driver); +} +module_exit(cbmem_exit); + +MODULE_AUTHOR("Jack Rosenthal "); +MODULE_LICENSE("GPL"); diff --git a/drivers/firmware/google/coreboot_table.c b/drivers/firmware/go= ogle/coreboot_table.c index c52bcaa9def6..a3e2720e4638 100644 --- a/drivers/firmware/google/coreboot_table.c +++ b/drivers/firmware/google/coreboot_table.c @@ -14,16 +14,21 @@ #include #include #include +#include #include #include #include #include +#include =20 #include "coreboot_table.h" =20 #define CB_DEV(d) container_of(d, struct coreboot_device, dev) #define CB_DRV(d) container_of(d, struct coreboot_driver, drv) =20 +struct kobject *coreboot_kobj; +EXPORT_SYMBOL(coreboot_kobj); + static int coreboot_bus_match(struct device *dev, struct device_driver *dr= v) { struct coreboot_device *device =3D CB_DEV(dev); @@ -157,6 +162,10 @@ static int coreboot_table_probe(struct platform_device= *pdev) } memunmap(ptr); =20 + coreboot_kobj =3D kobject_create_and_add("coreboot", firmware_kobj); + if (!coreboot_kobj) + return -ENOMEM; + return ret; } =20 @@ -170,6 +179,7 @@ static int coreboot_table_remove(struct platform_device= *pdev) { bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister); bus_unregister(&coreboot_bus_type); + kobject_put(coreboot_kobj); return 0; } =20 diff --git a/drivers/firmware/google/coreboot_table.h b/drivers/firmware/go= ogle/coreboot_table.h index beb778674acd..76c31e6e5376 100644 --- a/drivers/firmware/google/coreboot_table.h +++ b/drivers/firmware/google/coreboot_table.h @@ -14,6 +14,11 @@ =20 #include =20 +struct kobject; + +/* This is /sys/firmware/coreboot */ +extern struct kobject *coreboot_kobj; + /* Coreboot table header structure */ struct coreboot_table_header { char signature[4]; @@ -39,6 +44,16 @@ struct lb_cbmem_ref { u64 cbmem_addr; }; =20 +/* 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 +80,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; }; }; --=20 2.37.1.595.g718a3a8f04-goog