Add PoC of Arm DRTM specification version Beta-0

Change-Id: I26e6f2d4b2299edc246f5e8504d5d15b1399f640
diff --git a/include/lib/psci/psci.h b/include/lib/psci/psci.h
index b56e98b..da2efcc 100644
--- a/include/lib/psci/psci.h
+++ b/include/lib/psci/psci.h
@@ -196,17 +196,6 @@
 }
 
 /*
- * These are the states reported by the PSCI_AFFINITY_INFO API for the specified
- * CPU. The definitions of these states can be found in Section 5.7.1 in the
- * PSCI specification (ARM DEN 0022C).
- */
-typedef enum {
-	AFF_STATE_ON = U(0),
-	AFF_STATE_OFF = U(1),
-	AFF_STATE_ON_PENDING = U(2)
-} aff_info_state_t;
-
-/*
  * These are the power states reported by PSCI_NODE_HW_STATE API for the
  * specified CPU. The definitions of these states can be found in Section 5.15.3
  * of PSCI specification (ARM DEN 0022C).
diff --git a/include/lib/psci/psci_lib.h b/include/lib/psci/psci_lib.h
index 1ac45ad..d135a51 100644
--- a/include/lib/psci/psci_lib.h
+++ b/include/lib/psci/psci_lib.h
@@ -38,6 +38,17 @@
  */
 typedef void (*mailbox_entrypoint_t)(void);
 
+/*
+ * These are the states reported by the PSCI_AFFINITY_INFO API for the specified
+ * CPU. The definitions of these states can be found in Section 5.7.1 in the
+ * PSCI specification (ARM DEN 0022C).  Available for psci_lib clients.
+ */
+typedef enum {
+	AFF_STATE_ON = U(0),
+	AFF_STATE_OFF = U(1),
+	AFF_STATE_ON_PENDING = U(2)
+} aff_info_state_t;
+
 /******************************************************************************
  * Structure to pass PSCI Library arguments.
  *****************************************************************************/
@@ -91,6 +102,8 @@
 			  entry_point_info_t *next_image_info);
 int psci_stop_other_cores(unsigned int wait_ms,
 			  void (*stop_func)(u_register_t mpidr));
+unsigned int psci_is_last_on_core_safe(void);
+
 #endif /* __ASSEMBLER__ */
 
 #endif /* PSCI_LIB_H */
diff --git a/include/lib/tpm/tpm.h b/include/lib/tpm/tpm.h
new file mode 100644
index 0000000..ac17c0b
--- /dev/null
+++ b/include/lib/tpm/tpm.h
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier:    BSD-3-Clause
+ *
+ */
+#ifndef TPM_H
+#define TPM_H
+
+#include <lib/utils_def.h>
+
+/*
+ * TPM_ALG_ID constants.
+ * Ref. Table 9 - Definition of (UINT16) TPM_ALG_ID Constants
+ * Trusted Platform Module Library. Part 2: Structures,
+ * Family "2.0", Level 00 Revision 01.38, September 29 2016.
+ */
+enum tpm_hash_alg {
+	TPM_ALG_NONE   = 0x0,
+	TPM_ALG_SHA256 = 0x000B,
+	TPM_ALG_SHA384 = 0x000C,
+	TPM_ALG_SHA512 = 0x000D,
+};
+static inline bool tpm_alg_is_valid(enum tpm_hash_alg alg)
+{
+	switch (alg) {
+	case TPM_ALG_SHA256:
+	case TPM_ALG_SHA384:
+	case TPM_ALG_SHA512:
+		return true;
+
+	default:
+		return false;
+	}
+}
+
+enum tpm_hash_alg_dsize {
+	TPM_ALG_SHA256_DSIZE = 32,
+	TPM_ALG_SHA384_DSIZE = 48,
+	TPM_ALG_SHA512_DSIZE = 64,
+
+	TPM_ALG_MAX_DSIZE = TPM_ALG_SHA512_DSIZE
+};
+static inline size_t tpm_alg_dsize(enum tpm_hash_alg alg)
+{
+	switch (alg) {
+	case TPM_ALG_SHA256:
+		return TPM_ALG_SHA256_DSIZE;
+
+	case TPM_ALG_SHA384:
+		return TPM_ALG_SHA384_DSIZE;
+
+	case TPM_ALG_SHA512:
+		return TPM_ALG_SHA512_DSIZE;
+
+	default:
+		return 0;
+	}
+}
+
+enum tpm_pcr_idx {
+	/*
+	 * SRTM, BIOS, Host Platform Extensions, Embedded
+	 * Option ROMs and PI Drivers
+	 */
+	TPM_PCR_0 = 0,
+	/* Host Platform Configuration */
+	TPM_PCR_1,
+	/* UEFI driver and application Code */
+	TPM_PCR_2,
+	/* UEFI driver and application Configuration and Data */
+	TPM_PCR_3,
+	/* UEFI Boot Manager Code (usually the MBR) and Boot Attempts */
+	TPM_PCR_4,
+	/*
+	 * Boot Manager Code Configuration and Data (for use
+	 * by the Boot Manager Code) and GPT/Partition Table
+	 */
+	TPM_PCR_5,
+	/* Host Platform Manufacturer Specific */
+	TPM_PCR_6,
+	/* Secure Boot Policy */
+	TPM_PCR_7,
+	/* 8-15: Defined for use by the Static OS */
+	TPM_PCR_8,
+	/* Debug */
+	TPM_PCR_16 = 16,
+
+	/* DRTM (1) */
+	TPM_PCR_17 = 17,
+	/* DRTM (2) */
+	TPM_PCR_18 = 18,
+};
+static bool inline tpm_pcr_idx_is_valid(enum tpm_pcr_idx pcr_idx)
+{
+	switch (pcr_idx) {
+	case TPM_PCR_0:
+	case TPM_PCR_1:
+	case TPM_PCR_2:
+	case TPM_PCR_3:
+	case TPM_PCR_4:
+	case TPM_PCR_5:
+	case TPM_PCR_6:
+	case TPM_PCR_7:
+	case TPM_PCR_8:
+	case TPM_PCR_16:
+	case TPM_PCR_17:
+	case TPM_PCR_18:
+		return true;
+
+	default:
+		return false;
+	}
+}
+
+#endif /* TPM_H */
diff --git a/include/lib/tpm/tpm_log.h b/include/lib/tpm/tpm_log.h
new file mode 100644
index 0000000..a9f8d3b
--- /dev/null
+++ b/include/lib/tpm/tpm_log.h
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier:    BSD-3-Clause
+ *
+ */
+#ifndef TPM_LOG_H
+#define TPM_LOG_H
+
+#include <stddef.h>
+
+#include <lib/tpm/tpm.h>
+#include <export/lib/utils_def_exp.h>
+
+/*
+ * Event types
+ * Ref. Table 9 Events
+ * TCG PC Client Platform Firmware Profile Specification,
+ * Family "2.0", Level 00 Revision 1.04, June 3 2019.
+ */
+#define TPM_LOG_EV_PREBOOT_CERT				U(0x00000000)
+#define TPM_LOG_EV_POST_CODE				U(0x00000001)
+#define TPM_LOG_EV_UNUSED				U(0x00000002)
+#define TPM_LOG_EV_NO_ACTION				U(0x00000003)
+#define TPM_LOG_EV_SEPARATOR				U(0x00000004)
+#define TPM_LOG_EV_ACTION				U(0x00000005)
+#define TPM_LOG_EV_EVENT_TAG				U(0x00000006)
+#define TPM_LOG_EV_S_CRTM_CONTENTS			U(0x00000007)
+#define TPM_LOG_EV_S_CRTM_VERSION			U(0x00000008)
+#define TPM_LOG_EV_CPU_MICROCODE			U(0x00000009)
+#define TPM_LOG_EV_PLATFORM_CONFIG_FLAGS		U(0x0000000A)
+#define TPM_LOG_EV_TABLE_OF_DEVICES			U(0x0000000B)
+#define TPM_LOG_EV_COMPACT_HASH				U(0x0000000C)
+#define TPM_LOG_EV_IPL					U(0x0000000D)
+#define TPM_LOG_EV_IPL_PARTITION_DATA			U(0x0000000E)
+#define TPM_LOG_EV_NONHOST_CODE				U(0x0000000F)
+#define TPM_LOG_EV_NONHOST_CONFIG			U(0x00000010)
+#define TPM_LOG_EV_NONHOST_INFO				U(0x00000011)
+#define TPM_LOG_EV_OMIT_BOOT_DEVICE_EVENTS		U(0x00000012)
+#define TPM_LOG_EV_EFI_EVENT_BASE			U(0x80000000)
+#define TPM_LOG_EV_EFI_VARIABLE_DRIVER_CONFIG		U(0x80000001)
+#define TPM_LOG_EV_EFI_VARIABLE_BOOT			U(0x80000002)
+#define TPM_LOG_EV_EFI_BOOT_SERVICES_APPLICATION	U(0x80000003)
+#define TPM_LOG_EV_EFI_BOOT_SERVICES_DRIVER		U(0x80000004)
+#define TPM_LOG_EV_EFI_RUNTIME_SERVICES_DRIVER		U(0x80000005)
+#define TPM_LOG_EV_EFI_GPT_EVENT			U(0x80000006)
+#define TPM_LOG_EV_EFI_ACTION				U(0x80000007)
+#define TPM_LOG_EV_EFI_PLATFORM_FIRMWARE_BLOB		U(0x80000008)
+#define TPM_LOG_EV_EFI_HANDOFF_TABLES			U(0x80000009)
+#define TPM_LOG_EV_EFI_HCRTM_EVENT			U(0x80000010)
+#define TPM_LOG_EV_EFI_VARIABLE_AUTHORITY		U(0x800000E0)
+
+
+struct tpm_log_digest {
+	enum tpm_hash_alg h_alg;
+	size_t buf_bytes;
+	char buf[];
+};
+
+struct tpm_log_digests {
+	size_t count;
+	struct tpm_log_digest d[];
+};
+
+struct tpm_log_info {
+	char *buf;
+	size_t buf_bytes;
+
+	/* Running cursor, into the buffer. */
+	char *cursor;
+
+	/* */
+	char *startup_locality_event_data;
+};
+/* Opaque / encapsulated type */
+typedef struct tpm_log_info tpm_log_info_t;
+
+
+int tpm_log_init(uint32_t *const tpm_log_buf, size_t tpm_log_buf_bytes,
+                 enum tpm_hash_alg alg[], size_t num_algs,
+                 tpm_log_info_t *log_info_out);
+int tpm_log_add_event(tpm_log_info_t *tpm_log_info,
+                      uint32_t event_type, enum tpm_pcr_idx pcr,
+                      struct tpm_log_digests *digests,
+                      const unsigned char *event_data, size_t event_data_bytes);
+void tpm_log_serialise(char *dst, const tpm_log_info_t *tpm_log,
+                       size_t *tpm_log_size_out);
+
+
+#endif /* TPM_LOG_H */
diff --git a/include/lib/utils.h b/include/lib/utils.h
index 17ee9369..cd402c1 100644
--- a/include/lib/utils.h
+++ b/include/lib/utils.h
@@ -21,6 +21,11 @@
 	size_t nbytes;
 } mem_region_t;
 
+typedef struct p_mem_region {
+	unsigned long long base;
+	unsigned long long nbytes;
+} p_mem_region_t;
+
 /*
  * zero_normalmem all the regions defined in tbl.
  */