trivial: convert pci-psp plugin to create and use a device

In general this is a cleanup, means less vfuncs that get called when
devices are added. Beyon the optimization functionally should be a
no-op for fwupd.  It will also potentially storing the version
to the newly created secure processor device in the future.
This commit is contained in:
Mario Limonciello 2022-09-12 22:35:47 -05:00 committed by Mario Limonciello
parent bfebede490
commit 561c8661f8
4 changed files with 303 additions and 264 deletions

View File

@ -0,0 +1,287 @@
/*
* Copyright (C) 2022 Advanced Micro Devices Inc.
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#include "config.h"
#include <fwupdplugin.h>
#include "fu-pci-psp-device.h"
struct _FuPciPspDevice {
FuUdevDevice parent_instance;
};
G_DEFINE_TYPE(FuPciPspDevice, fu_pci_psp_device, FU_TYPE_UDEV_DEVICE)
static gboolean
fu_pci_psp_device_get_attr(FwupdSecurityAttr *attr,
const gchar *path,
const gchar *file,
gboolean *out,
GError **error)
{
guint64 val = 0;
g_autofree gchar *fn = g_build_filename(path, file, NULL);
g_autofree gchar *buf = NULL;
gsize bufsz = 0;
if (!g_file_get_contents(fn, &buf, &bufsz, error)) {
g_prefix_error(error, "could not open %s: ", fn);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_MISSING_DATA);
return FALSE;
}
if (!fu_strtoull(buf, &val, 0, G_MAXUINT32, error))
return FALSE;
*out = val ? TRUE : FALSE;
return TRUE;
}
static void
fu_pci_psp_device_add_security_attrs_tsme(FuDevice *device,
const gchar *path,
FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
attr = fu_device_security_attr_new(device, FWUPD_SECURITY_ATTR_ID_ENCRYPTED_RAM);
fu_security_attrs_append(attrs, attr);
if (!fu_pci_psp_device_get_attr(attr, path, "tsme_status", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
/* BIOS knob used on Lenovo systems */
fu_security_attr_add_bios_target_value(attr, "com.thinklmi.TSME", "enable");
if (!val) {
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_ENCRYPTED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONFIG_FW);
return;
}
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_ENCRYPTED);
fwupd_security_attr_add_obsolete(attr, "msr");
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_pci_psp_device_add_security_attrs_fused_part(FuDevice *device,
const gchar *path,
FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
attr = fu_device_security_attr_new(device, FWUPD_SECURITY_ATTR_ID_PLATFORM_FUSED);
fu_security_attrs_append(attrs, attr);
if (!fu_pci_psp_device_get_attr(attr, path, "fused_part", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("part is not fused");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_LOCKED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
return;
}
/* success */
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_LOCKED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_pci_psp_device_add_security_attrs_debug_locked_part(FuDevice *device,
const gchar *path,
FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
attr = fu_device_security_attr_new(device, FWUPD_SECURITY_ATTR_ID_PLATFORM_DEBUG_LOCKED);
fu_security_attrs_append(attrs, attr);
if (!fu_pci_psp_device_get_attr(attr, path, "debug_lock_on", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("debug lock disabled");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_LOCKED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
return;
}
/* success */
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_LOCKED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_pci_psp_device_add_security_attrs_rollback_protection(FuDevice *device,
const gchar *path,
FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
attr = fu_device_security_attr_new(device, FWUPD_SECURITY_ATTR_ID_AMD_ROLLBACK_PROTECTION);
fu_security_attrs_append(attrs, attr);
if (!fu_pci_psp_device_get_attr(attr, path, "anti_rollback_status", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("rollback protection not enforced");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
return;
}
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_pci_psp_device_add_security_attrs_rom_armor(FuDevice *device,
const gchar *path,
FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
/* create attr */
attr = fu_device_security_attr_new(device, FWUPD_SECURITY_ATTR_ID_AMD_SPI_WRITE_PROTECTION);
fu_security_attrs_append(attrs, attr);
if (!fu_pci_psp_device_get_attr(attr, path, "rom_armor_enforced", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("ROM armor not enforced");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
return;
}
/* success */
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_pci_psp_device_add_security_attrs_rpmc(FuDevice *device,
const gchar *path,
FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
/* create attr */
attr =
fu_device_security_attr_new(device, FWUPD_SECURITY_ATTR_ID_AMD_SPI_REPLAY_PROTECTION);
fu_security_attrs_append(attrs, attr);
if (!fu_pci_psp_device_get_attr(attr, path, "rpmc_spirom_available", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("no RPMC compatible SPI rom present");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_SUPPORTED);
return;
}
if (!fu_pci_psp_device_get_attr(attr,
path,
"rpmc_production_enabled",
&val,
&error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("no RPMC compatible SPI rom present");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_ENABLED);
return;
}
/* success */
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_pci_psp_device_set_missing_data(FuDevice *device, FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
attr = fu_device_security_attr_new(device, FWUPD_SECURITY_ATTR_ID_SUPPORTED_CPU);
fwupd_security_attr_add_obsolete(attr, "cpu");
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_MISSING_DATA);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
fu_security_attrs_append(attrs, attr);
}
static void
fu_pci_psp_device_add_security_attrs(FuDevice *device, FuSecurityAttrs *attrs)
{
const gchar *sysfs_path = NULL;
g_autofree gchar *test_file = NULL;
/* ccp not loaded */
if (device != NULL) {
sysfs_path = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(device));
test_file = g_build_filename(sysfs_path, "tsme_status", NULL);
}
if (sysfs_path == NULL || !g_file_test(test_file, G_FILE_TEST_EXISTS)) {
fu_pci_psp_device_set_missing_data(device, attrs);
return;
}
fu_pci_psp_device_add_security_attrs_tsme(device, sysfs_path, attrs);
fu_pci_psp_device_add_security_attrs_fused_part(device, sysfs_path, attrs);
fu_pci_psp_device_add_security_attrs_debug_locked_part(device, sysfs_path, attrs);
fu_pci_psp_device_add_security_attrs_rollback_protection(device, sysfs_path, attrs);
fu_pci_psp_device_add_security_attrs_rpmc(device, sysfs_path, attrs);
fu_pci_psp_device_add_security_attrs_rom_armor(device, sysfs_path, attrs);
}
static void
fu_pci_psp_device_init(FuPciPspDevice *self)
{
fu_device_set_name(FU_DEVICE(self), "Secure Processor");
fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_INTERNAL);
fu_device_add_icon(FU_DEVICE(self), "computer");
fu_device_add_parent_guid(FU_DEVICE(self), "cpu");
fu_device_set_vendor(FU_DEVICE(self), "AMD");
fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_QUAD);
fu_device_set_physical_id(FU_DEVICE(self), "pci-psp");
}
static void
fu_pci_psp_device_class_init(FuPciPspDeviceClass *klass)
{
FuDeviceClass *klass_device = FU_DEVICE_CLASS(klass);
klass_device->add_security_attrs = fu_pci_psp_device_add_security_attrs;
}

View File

@ -0,0 +1,12 @@
/*
* Copyright (C) 2022 Advanced Micro Devices Inc.
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#pragma once
#include <fwupdplugin.h>
#define FU_TYPE_PCI_PSP_DEVICE (fu_pci_psp_device_get_type())
G_DECLARE_FINAL_TYPE(FuPciPspDevice, fu_pci_psp_device, FU, PCI_PSP_DEVICE, FuUdevDevice)

View File

@ -8,272 +8,13 @@
#include <fwupdplugin.h> #include <fwupdplugin.h>
#include "fu-pci-psp-device.h"
static void static void
fu_plugin_pci_psp_init(FuPlugin *plugin) fu_plugin_pci_psp_init(FuPlugin *plugin)
{ {
fu_plugin_add_udev_subsystem(plugin, "pci"); fu_plugin_add_udev_subsystem(plugin, "pci");
} fu_plugin_add_device_gtype(plugin, FU_TYPE_PCI_PSP_DEVICE);
static gboolean
fu_plugin_pci_psp_backend_device_added(FuPlugin *plugin, FuDevice *device, GError **error)
{
/* interesting device? */
if (!FU_IS_UDEV_DEVICE(device))
return TRUE;
if (g_strcmp0(fu_udev_device_get_subsystem(FU_UDEV_DEVICE(device)), "pci") != 0)
return TRUE;
fu_plugin_cache_add(plugin, "pci-psp-device", device);
return TRUE;
}
static gboolean
fu_plugin_pci_psp_get_attr(FwupdSecurityAttr *attr,
const gchar *path,
const gchar *file,
gboolean *out,
GError **error)
{
guint64 val = 0;
g_autofree gchar *fn = g_build_filename(path, file, NULL);
g_autofree gchar *buf = NULL;
gsize bufsz = 0;
if (!g_file_get_contents(fn, &buf, &bufsz, error)) {
g_prefix_error(error, "could not open %s: ", fn);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_MISSING_DATA);
return FALSE;
}
if (!fu_strtoull(buf, &val, 0, G_MAXUINT32, error))
return FALSE;
*out = val ? TRUE : FALSE;
return TRUE;
}
static void
fu_plugin_add_security_attrs_tsme(FuPlugin *plugin, const gchar *path, FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
attr = fu_plugin_security_attr_new(plugin, FWUPD_SECURITY_ATTR_ID_ENCRYPTED_RAM);
fu_security_attrs_append(attrs, attr);
if (!fu_plugin_pci_psp_get_attr(attr, path, "tsme_status", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
/* BIOS knob used on Lenovo systems */
fu_security_attr_add_bios_target_value(attr, "com.thinklmi.TSME", "enable");
if (!val) {
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_ENCRYPTED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONFIG_FW);
return;
}
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_ENCRYPTED);
fwupd_security_attr_add_obsolete(attr, "msr");
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_plugin_add_security_attrs_fused_part(FuPlugin *plugin, const gchar *path, FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
attr = fu_plugin_security_attr_new(plugin, FWUPD_SECURITY_ATTR_ID_PLATFORM_FUSED);
fu_security_attrs_append(attrs, attr);
if (!fu_plugin_pci_psp_get_attr(attr, path, "fused_part", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("part is not fused");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_LOCKED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
return;
}
/* success */
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_LOCKED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_plugin_add_security_attrs_debug_locked_part(FuPlugin *plugin,
const gchar *path,
FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
attr = fu_plugin_security_attr_new(plugin, FWUPD_SECURITY_ATTR_ID_PLATFORM_DEBUG_LOCKED);
fu_security_attrs_append(attrs, attr);
if (!fu_plugin_pci_psp_get_attr(attr, path, "debug_lock_on", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("debug lock disabled");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_LOCKED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
return;
}
/* success */
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_LOCKED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_plugin_add_security_attrs_rollback_protection(FuPlugin *plugin,
const gchar *path,
FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
attr = fu_plugin_security_attr_new(plugin, FWUPD_SECURITY_ATTR_ID_AMD_ROLLBACK_PROTECTION);
fu_security_attrs_append(attrs, attr);
if (!fu_plugin_pci_psp_get_attr(attr, path, "anti_rollback_status", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("rollback protection not enforced");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
return;
}
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_plugin_add_security_attrs_rom_armor(FuPlugin *plugin, const gchar *path, FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
/* create attr */
attr = fu_plugin_security_attr_new(plugin, FWUPD_SECURITY_ATTR_ID_AMD_SPI_WRITE_PROTECTION);
fu_security_attrs_append(attrs, attr);
if (!fu_plugin_pci_psp_get_attr(attr, path, "rom_armor_enforced", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("ROM armor not enforced");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
return;
}
/* success */
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_plugin_add_security_attrs_rpmc(FuPlugin *plugin, const gchar *path, FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
g_autoptr(GError) error_local = NULL;
gboolean val;
/* create attr */
attr =
fu_plugin_security_attr_new(plugin, FWUPD_SECURITY_ATTR_ID_AMD_SPI_REPLAY_PROTECTION);
fu_security_attrs_append(attrs, attr);
if (!fu_plugin_pci_psp_get_attr(attr, path, "rpmc_spirom_available", &val, &error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("no RPMC compatible SPI rom present");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_SUPPORTED);
return;
}
if (!fu_plugin_pci_psp_get_attr(attr,
path,
"rpmc_production_enabled",
&val,
&error_local)) {
g_debug("%s", error_local->message);
return;
}
if (!val) {
g_debug("no RPMC compatible SPI rom present");
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_NOT_ENABLED);
return;
}
/* success */
fwupd_security_attr_set_result(attr, FWUPD_SECURITY_ATTR_RESULT_ENABLED);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS);
}
static void
fu_plugin_pci_psp_set_missing_data(FuPlugin *plugin, FuSecurityAttrs *attrs)
{
g_autoptr(FwupdSecurityAttr) attr = NULL;
attr = fu_plugin_security_attr_new(plugin, FWUPD_SECURITY_ATTR_ID_SUPPORTED_CPU);
fwupd_security_attr_add_obsolete(attr, "cpu");
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_MISSING_DATA);
fwupd_security_attr_add_flag(attr, FWUPD_SECURITY_ATTR_FLAG_ACTION_CONTACT_OEM);
fu_security_attrs_append(attrs, attr);
}
static void
fu_plugin_pci_psp_add_security_attrs(FuPlugin *plugin, FuSecurityAttrs *attrs)
{
FuDevice *psp_device = fu_plugin_cache_lookup(plugin, "pci-psp-device");
const gchar *sysfs_path = NULL;
g_autofree gchar *test_file = NULL;
/* only AMD */
if (fu_cpu_get_vendor() != FU_CPU_VENDOR_AMD)
return;
/* ccp not loaded */
if (psp_device) {
sysfs_path = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(psp_device));
test_file = g_build_filename(sysfs_path, "tsme_status", NULL);
}
if (sysfs_path == NULL || !g_file_test(test_file, G_FILE_TEST_EXISTS)) {
fu_plugin_pci_psp_set_missing_data(plugin, attrs);
return;
}
fu_plugin_add_security_attrs_tsme(plugin, sysfs_path, attrs);
fu_plugin_add_security_attrs_fused_part(plugin, sysfs_path, attrs);
fu_plugin_add_security_attrs_debug_locked_part(plugin, sysfs_path, attrs);
fu_plugin_add_security_attrs_rollback_protection(plugin, sysfs_path, attrs);
fu_plugin_add_security_attrs_rpmc(plugin, sysfs_path, attrs);
fu_plugin_add_security_attrs_rom_armor(plugin, sysfs_path, attrs);
} }
void void
@ -281,6 +22,4 @@ fu_plugin_init_vfuncs(FuPluginVfuncs *vfuncs)
{ {
vfuncs->build_hash = FU_BUILD_HASH; vfuncs->build_hash = FU_BUILD_HASH;
vfuncs->init = fu_plugin_pci_psp_init; vfuncs->init = fu_plugin_pci_psp_init;
vfuncs->add_security_attrs = fu_plugin_pci_psp_add_security_attrs;
vfuncs->backend_device_added = fu_plugin_pci_psp_backend_device_added;
} }

View File

@ -7,6 +7,7 @@ shared_module('fu_plugin_pci_psp',
fu_hash, fu_hash,
sources: [ sources: [
'fu-plugin-pci-psp.c', 'fu-plugin-pci-psp.c',
'fu-pci-psp-device.c',
], ],
include_directories: [ include_directories: [
root_incdir, root_incdir,