diff mbox series

[1/2] acpi: svkl: add test for SVKL table (mantis 2162)

Message ID 20220914084026.69362-1-ivan.hu@canonical.com
State Accepted
Headers show
Series [1/2] acpi: svkl: add test for SVKL table (mantis 2162) | expand

Commit Message

Ivan Hu Sept. 14, 2022, 8:40 a.m. UTC
Signed-off-by: Ivan Hu <ivan.hu@canonical.com>
---
 src/Makefile.am             |   1 +
 src/acpi/svkl/svkl.c        | 122 ++++++++++++++++++++++++++++++++++++
 src/lib/include/fwts_acpi.h |  17 +++++
 3 files changed, 140 insertions(+)
 create mode 100644 src/acpi/svkl/svkl.c
diff mbox series

Patch

diff --git a/src/Makefile.am b/src/Makefile.am
index a8c60b79..e1a6f094 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -140,6 +140,7 @@  fwts_SOURCES = main.c 				\
 	acpi/spmi/spmi.c 			\
 	acpi/srat/srat.c 			\
 	acpi/stao/stao.c			\
+	acpi/svkl/svkl.c			\
 	acpi/syntaxcheck/syntaxcheck.c 		\
 	acpi/tcpa/tcpa.c 			\
 	acpi/tpm2/tpm2.c 			\
diff --git a/src/acpi/svkl/svkl.c b/src/acpi/svkl/svkl.c
new file mode 100644
index 00000000..4149c541
--- /dev/null
+++ b/src/acpi/svkl/svkl.c
@@ -0,0 +1,122 @@ 
+/*
+ * Copyright (C) 2022 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.
+ *
+ */
+#include "fwts.h"
+
+#if defined(FWTS_HAS_ACPI)
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <stdbool.h>
+
+static fwts_acpi_table_info *table;
+acpi_table_init(SVKL, &table)
+
+static int svkl_test1(fwts_framework *fw)
+{
+	fwts_acpi_table_svkl *svkl = (fwts_acpi_table_svkl *) table->data;
+	fwts_acpi_table_svkl_key_structure *key_structure;
+	uint32_t offset, count, i;
+	bool passed = true;
+
+	if (table->length < (size_t)svkl->header.length) {
+		fwts_failed(fw, LOG_LEVEL_HIGH,
+			"SVKLTooShort",
+			"SVKL table incorrectly sized, SVKL "
+			"header reports it is %" PRIu32 " bytes, "
+			"instead got %zu bytes",
+			svkl->header.length, table->length);
+		return FWTS_OK;
+	}
+
+	if (table->length < sizeof(fwts_acpi_table_svkl)) {
+		fwts_failed(fw, LOG_LEVEL_HIGH,
+			"SVKLTooShort",
+			"SVKL table too short, expecting %zu bytes, "
+			"instead got %zu bytes",
+			sizeof(fwts_acpi_table_svkl), table->length);
+		return FWTS_OK;
+	}
+
+	fwts_log_info_verbatim(fw, "SVKL Storage Volume Key Data Table:");
+	fwts_log_info_simp_int(fw, "  Key Count:         ", svkl->key_count);
+
+	offset = sizeof(fwts_acpi_table_svkl);
+	key_structure = (fwts_acpi_table_svkl_key_structure *) (table->data + offset);
+
+	count = (svkl->header.length - offset) / sizeof(fwts_acpi_table_svkl_key_structure);
+	if (count != svkl->key_count) {
+		passed = false;
+		fwts_failed(fw, LOG_LEVEL_CRITICAL,
+			"SVKLBadStructureCount",
+			"SVKL should have %" PRId32 " key structures, got %" PRId32,
+			svkl->key_count, count);
+		return FWTS_OK;
+	}
+
+	for (i = 0; i < svkl->key_count; i++) {
+		fwts_log_info_verbatim(fw, "  Storage Volume Key Structure %" PRIu8, (i + 1));
+		fwts_log_info_simp_int(fw, "    Key Type:        ", key_structure->key_type);
+		if (key_structure->key_type != 0) {
+			passed = false;
+			fwts_failed(fw, LOG_LEVEL_HIGH,
+				"SVKLBadKeyType",
+				"SVKL key type not zero, 1~0xFFFF reserved.");
+		}
+		fwts_log_info_simp_int(fw, "    Key Format:      ", key_structure->key_format);
+		if (key_structure->key_format != 0) {
+			passed = false;
+			fwts_failed(fw, LOG_LEVEL_HIGH,
+				"SVKLBadKeyFormat",
+				"SVKL key format not zero, 1~0xFFFF reserved.");
+		}
+		fwts_log_info_simp_int(fw, "    Key Size:        ", key_structure->key_size);
+		fwts_log_info_simp_int(fw, "    Key Address:     ", key_structure->key_addr);
+
+		if ((offset += sizeof(fwts_acpi_table_svkl_key_structure)) > table->length) {
+			passed = false;
+			fwts_failed(fw, LOG_LEVEL_CRITICAL,
+				"SVKLBadTableLength",
+				"SVKL has more key structures than its size can handle");
+			break;
+		}
+
+		key_structure = (fwts_acpi_table_svkl_key_structure *) (table->data + offset);
+	}
+
+	fwts_log_nl(fw);
+
+	if (passed)
+		fwts_passed(fw, "No issues found in SVKL table.");
+
+	return FWTS_OK;
+}
+
+static fwts_framework_minor_test svkl_tests[] = {
+	{ svkl_test1, "Validate SVKL table." },
+	{ NULL, NULL }
+};
+
+static fwts_framework_ops svkl_ops = {
+	.description = "SVKL Storage Volume Key Data table test.",
+	.init        = SVKL_init,
+	.minor_tests = svkl_tests
+};
+
+FWTS_REGISTER("svkl", &svkl_ops, FWTS_TEST_ANYTIME, FWTS_FLAG_BATCH | FWTS_FLAG_ACPI)
+
+#endif
diff --git a/src/lib/include/fwts_acpi.h b/src/lib/include/fwts_acpi.h
index 8381c351..d87224a5 100644
--- a/src/lib/include/fwts_acpi.h
+++ b/src/lib/include/fwts_acpi.h
@@ -2219,4 +2219,21 @@  typedef struct {
 	uint32_t	amrt_addr_end;
 } __attribute__ ((packed)) fwts_acpi_table_aspt;
 
+/*
+ * ACPI SVKL (Storage Volume Key Data)
+ *   https://cdrdv2.intel.com/v1/dl/getContent/726790
+ */
+typedef struct {
+	uint16_t	key_type;
+	uint16_t	key_format;
+	uint32_t	key_size;
+	uint64_t	key_addr;
+} __attribute__ ((packed)) fwts_acpi_table_svkl_key_structure;
+
+typedef struct {
+	fwts_acpi_table_header  header;
+	uint32_t	key_count;
+	fwts_acpi_table_svkl_key_structure key_struct[0];
+} __attribute__ ((packed)) fwts_acpi_table_svkl;
+
 #endif