[PATCH 1/2] acpi: sdev: add ACPI SDEV test (mantis 1632)

Alex Hung alex.hung at canonical.com
Fri Oct 20 08:23:45 UTC 2017


Signed-off-by: Alex Hung <alex.hung at canonical.com>
---
 src/Makefile.am                    |   1 +
 src/acpi/sdev/sdev.c               | 146 +++++++++++++++++++++++++++++++++++++
 src/lib/include/fwts_acpi.h        |  37 ++++++++++
 src/lib/include/fwts_acpi_tables.h |   2 +
 src/lib/src/fwts_acpi_tables.c     |  50 +++++++++++++
 5 files changed, 236 insertions(+)
 create mode 100644 src/acpi/sdev/sdev.c

diff --git a/src/Makefile.am b/src/Makefile.am
index b6b925b..0f47b43 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -120,6 +120,7 @@ fwts_SOURCES = main.c 				\
 	acpi/s4/s4.c 				\
 	acpi/sbst/sbst.c			\
 	acpi/sdei/sdei.c			\
+	acpi/sdev/sdev.c			\
 	acpi/slic/slic.c 			\
 	acpi/slit/slit.c 			\
 	acpi/spcr/spcr.c 			\
diff --git a/src/acpi/sdev/sdev.c b/src/acpi/sdev/sdev.c
new file mode 100644
index 0000000..1d49bcb
--- /dev/null
+++ b/src/acpi/sdev/sdev.c
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2017 Canonical
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+#include "fwts.h"
+
+#if defined(FWTS_HAS_ACPI)
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+
+static fwts_acpi_table_info *table;
+
+static int sdev_init(fwts_framework *fw)
+{
+	if (fwts_acpi_find_table(fw, "SDEV", 0, &table) != FWTS_OK) {
+		fwts_log_error(fw, "Cannot read ACPI tables");
+		return FWTS_ERROR;
+	}
+	if (table == NULL || (table && table->length == 0)) {
+		fwts_log_error(fw, "ACPI SDEV table does not exist, skipping test");
+		return FWTS_SKIP;
+	}
+
+	return FWTS_OK;
+}
+
+static void sdev_acpi_namespace_device_test(fwts_framework *fw, const fwts_acpi_table_sdev_acpi *entry, bool *passed)
+{
+	fwts_log_info_verbatim(fw, "  ACPI Integrated Device (Type 0):");
+	fwts_log_info_verbatim(fw, "    Type:                           0x%2.2" PRIx8, entry->header.type);
+	fwts_log_info_verbatim(fw, "    Flags:                          0x%2.2" PRIx8, entry->header.flags);
+	fwts_log_info_verbatim(fw, "    Length:                         0x%4.4" PRIx16, entry->header.length);
+	fwts_log_info_verbatim(fw, "    Device Id Offset:               0x%4.4" PRIx16, entry->device_id_offset);
+	fwts_log_info_verbatim(fw, "    Device Id Length:               0x%4.4" PRIx16, entry->device_id_length);
+	fwts_log_info_verbatim(fw, "    Vendor Specific Data Offset:    0x%4.4" PRIx16, entry->vendor_offset);
+	fwts_log_info_verbatim(fw, "    Vendor Specific Data Length:    0x%4.4" PRIx16, entry->vendor_length);
+
+	fwts_acpi_reserved_bits_check(fw, "SDEV", "Flags", entry->header.flags, sizeof(entry->header.flags), 1, 15, passed);
+}
+
+static void sdev_pcie_endpoint_device_test(fwts_framework *fw, const fwts_acpi_table_sdev_pcie *entry, bool *passed)
+{
+	fwts_log_info_verbatim(fw, "  PCIe Endpoint Device (Type 1):");
+	fwts_log_info_verbatim(fw, "    Type:                           0x%2.2" PRIx8, entry->header.type);
+	fwts_log_info_verbatim(fw, "    Flags:                          0x%2.2" PRIx8, entry->header.flags);
+	fwts_log_info_verbatim(fw, "    Length:                         0x%4.4" PRIx16, entry->header.length);
+	fwts_log_info_verbatim(fw, "    PCI Segment Number:             0x%4.4" PRIx16, entry->segment);
+	fwts_log_info_verbatim(fw, "    Start Bus Number:               0x%4.4" PRIx16, entry->start_bus);
+	fwts_log_info_verbatim(fw, "    PCI Path Offset:                0x%4.4" PRIx16, entry->path_offset);
+	fwts_log_info_verbatim(fw, "    PCI Path Length:                0x%4.4" PRIx16, entry->path_length);
+	fwts_log_info_verbatim(fw, "    Vendor Specific Data Offset:    0x%4.4" PRIx16, entry->vendor_offset);
+	fwts_log_info_verbatim(fw, "    Vendor Specific Data Length:    0x%4.4" PRIx16, entry->vendor_length);
+
+	fwts_acpi_reserved_bits_check(fw, "SDEV", "Flags", entry->header.flags, sizeof(entry->header.flags), 1, 15, passed);
+}
+
+static int sdev_test1(fwts_framework *fw)
+{
+	fwts_acpi_table_sdev_header *entry;
+	bool passed = true;
+	uint32_t offset;
+
+	fwts_log_info_verbatim(fw, "SDEV Secure Devices Table:");
+
+	entry = (fwts_acpi_table_sdev_header *) (table->data + sizeof(fwts_acpi_table_sdev));
+	offset = sizeof(fwts_acpi_table_sdev);
+	while (offset < table->length) {
+		uint32_t type_length;
+
+		if (entry->length == 0) {
+			passed = false;
+			fwts_failed(fw, LOG_LEVEL_HIGH, "SDEVStructLengthZero",
+				    "SDEV structure (offset 0x%4.4" PRIx32 ") "
+				    "length cannot be 0", offset);
+			break;
+		}
+
+		if (entry->type == FWTS_ACPI_SDEV_TYPE_ACPI_NAMESPACE) {
+			fwts_acpi_table_sdev_acpi *acpi = (fwts_acpi_table_sdev_acpi *) entry;
+			sdev_acpi_namespace_device_test(fw, acpi, &passed);
+			type_length = sizeof(fwts_acpi_table_sdev_acpi) + acpi->device_id_length + acpi->vendor_length;
+		} else if (entry->type == FWTS_ACPI_SDEV_TYPE_PCIE_ENDPOINT) {
+			fwts_acpi_table_sdev_pcie *pcie = (fwts_acpi_table_sdev_pcie *) entry;
+			sdev_pcie_endpoint_device_test(fw, pcie, &passed);
+			type_length = sizeof(fwts_acpi_table_sdev_pcie) + pcie->path_length + pcie->vendor_length;
+		} else {
+			fwts_acpi_reserved_type_check(fw, "SDEV", entry->type, 0, FWTS_ACPI_SDEV_TYPE_RESERVED, &passed);
+			break;
+		}
+
+		if (!fwts_acpi_subtable_length_check(fw, "SDEV", entry->type, entry->length, type_length)) {
+			passed = false;
+			break;
+		}
+
+		if ((offset += entry->length) > table->length) {
+			passed = false;
+			fwts_failed(fw, LOG_LEVEL_CRITICAL,
+				"SDEVBadTableLength",
+				"SDEV has more subtypes than its size can handle");
+			break;
+		}
+
+		entry = (fwts_acpi_table_sdev_header *) (table->data + offset);
+		fwts_log_nl(fw);
+	}
+
+	fwts_log_nl(fw);
+	if (passed)
+		fwts_passed(fw, "No issues found in SDEV table.");
+
+	return FWTS_OK;
+}
+
+static fwts_framework_minor_test sdev_tests[] = {
+	{ sdev_test1, "Validate SDEV table." },
+	{ NULL, NULL }
+};
+
+static fwts_framework_ops sdev_ops = {
+	.description = "SDEV Secure Devices Table test",
+	.init        = sdev_init,
+	.minor_tests = sdev_tests
+};
+
+FWTS_REGISTER("sdev", &sdev_ops, FWTS_TEST_ANYTIME, FWTS_FLAG_BATCH | FWTS_FLAG_TEST_ACPI)
+
+#endif
diff --git a/src/lib/include/fwts_acpi.h b/src/lib/include/fwts_acpi.h
index e22e1af..4e29816 100644
--- a/src/lib/include/fwts_acpi.h
+++ b/src/lib/include/fwts_acpi.h
@@ -1265,6 +1265,43 @@ typedef struct {
 } __attribute__ ((packed)) fwts_acpi_table_nfit_platform_cap;
 
 /*
+ * ACPI SDEV (Secure Devices Table), 5.2.26
+ */
+typedef struct {
+	fwts_acpi_table_header	header;
+} __attribute__ ((packed)) fwts_acpi_table_sdev;
+
+typedef enum {
+	FWTS_ACPI_SDEV_TYPE_ACPI_NAMESPACE	= 0,
+	FWTS_ACPI_SDEV_TYPE_PCIE_ENDPOINT	= 1,
+	FWTS_ACPI_SDEV_TYPE_RESERVED		= 2,
+} fwts_acpi_sdev_type;
+
+typedef struct {
+	uint8_t		type;
+	uint8_t		flags;
+	uint16_t	length;
+} __attribute__ ((packed)) fwts_acpi_table_sdev_header;
+
+typedef struct {
+	fwts_acpi_table_sdev_header	header;
+	uint16_t	device_id_offset;
+	uint16_t	device_id_length;
+	uint16_t	vendor_offset;
+	uint16_t	vendor_length;
+} fwts_acpi_table_sdev_acpi;
+
+typedef struct {
+	fwts_acpi_table_sdev_header	header;
+	uint16_t	segment;
+	uint16_t	start_bus;
+	uint16_t	path_offset;
+	uint16_t	path_length;
+	uint16_t	vendor_offset;
+	uint16_t	vendor_length;
+} fwts_acpi_table_sdev_pcie;
+
+/*
  * ACPI HMAT (Heterogeneous Memory Attribute Table), 5.2.27
  */
 typedef struct {
diff --git a/src/lib/include/fwts_acpi_tables.h b/src/lib/include/fwts_acpi_tables.h
index 27fbce4..445a35b 100644
--- a/src/lib/include/fwts_acpi_tables.h
+++ b/src/lib/include/fwts_acpi_tables.h
@@ -55,6 +55,8 @@ fwts_bool fwts_acpi_is_reduced_hardware(const fwts_acpi_table_fadt *fadt);
 
 void fwts_acpi_reserved_zero_check(fwts_framework *fw, const char *table, const char *field, uint64_t value, uint8_t size, bool *passed);
 void fwts_acpi_reserved_bits_check(fwts_framework *fw, const char *table, const char *field, uint64_t value, uint8_t size, uint8_t min, uint8_t max, bool *passed);
+void fwts_acpi_reserved_type_check(fwts_framework *fw, const char *table, uint8_t value, uint8_t min, uint8_t reserved, bool *passed);
+bool fwts_acpi_subtable_length_check(fwts_framework *fw, const char *table, uint8_t subtable_type, uint32_t subtable_length, uint32_t size);
 
 #endif
 
diff --git a/src/lib/src/fwts_acpi_tables.c b/src/lib/src/fwts_acpi_tables.c
index c4b9377..0db67f6 100644
--- a/src/lib/src/fwts_acpi_tables.c
+++ b/src/lib/src/fwts_acpi_tables.c
@@ -1482,4 +1482,54 @@ void fwts_acpi_reserved_bits_check(
 	}
 }
 
+/*
+ *  fwts_acpi_reserved_type_check()
+ *  verify whether the reserved types are used
+ */
+void fwts_acpi_reserved_type_check(
+	fwts_framework *fw,
+	const char *table,
+	uint8_t value,
+	uint8_t min,
+	uint8_t reserved,
+	bool *passed)
+{
+	if (value < min || value >= reserved) {
+		char label[20];
+		strncpy(label, table, 4);	/* ACPI name is 4 char long */
+		strncpy(label + 4, "BadSubTableType", sizeof(label) - 4);
+
+		fwts_failed(fw, LOG_LEVEL_HIGH, label,
+			"%4.4s must have subtable with Type %" PRId8
+			"..%" PRId8 ", got %" PRId8 " instead",
+			table, min, reserved, value);
+
+		*passed = false;
+	}
+}
+
+/*
+ *  fwts_acpi_subtable_length_check()
+ *  verify whether sub-table length is sane
+ */
+bool fwts_acpi_subtable_length_check(
+	fwts_framework *fw,
+	const char *table,
+	uint8_t subtable_type,
+	uint32_t subtable_length,
+	uint32_t size)
+{
+	if (subtable_length != size) {
+		char label[30];
+		strncpy(label, table, 4);	/* ACPI name is 4 char long */
+		strncpy(label + 4, "BadSubTableLength", sizeof(label) - 4);
+		fwts_failed(fw, LOG_LEVEL_CRITICAL, label,
+			"%4.4s subtable Type 0x%2.2" PRIx8 " should have "
+			"length 0x%2.2" PRIx8 ", got 0x%2.2" PRIx8,
+			table, subtable_type, subtable_length, size);
+		return false;
+	}
+	return true;
+}
+
 #endif
-- 
2.7.4




More information about the fwts-devel mailing list