Skip to contentSkip to conversation

efi/tpcm: Add Trusted Platform Control

RFC PATCH New Khalid Ali 2 participants 4 messages Jun 28, 2025 – Jul 2, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20250627074229.31458-1-[email protected]

Khalid Ali

author
#1 lore raw
Quoted 13 lines

+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 Free Software Foundation, Inc.
+ *

+ *  GRUB 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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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.
Quoted 6 lines

+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * EFI TPCM support code.
+ */

Please fix here, the copyright should be the year you submitted the patch (i.e this year 2025).

Other than that standard and patch seems solid.

Regards
Khaalid

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw

v3 cover efi/tpcm: Add Trusted Platform Control Module

Hi all,

This v3 addresses comments from Khaalid and Sudhakar on the previous v2 series.

Changes v2 -> v3
* By Khaalid:
- Modify the year information in the copyright to 2025
* By Sudhakar:
- Standardize the patch format

Thanks again for your reviews!

Regards
Hao

chench246 (2):
efi/tpcm: Add UEFI interface for TPCM module
efi/tpcm: Add complete support of TPCM module

 grub-core/Makefile.core.def   |   7 ++
 grub-core/commands/efi/tpcm.c | 163 ++++++++++++++++++++++++++++++++++
 grub-core/commands/tpcm.c     | 100 +++++++++++++++++++++
 include/grub/efi/tpcm.h       |  61 +++++++++++++
 include/grub/err.h            |   3 +-
 5 files changed, 333 insertions(+), 1 deletion(-)
 create mode 100755 grub-core/commands/efi/tpcm.c
 create mode 100755 grub-core/commands/tpcm.c
 create mode 100644 include/grub/efi/tpcm.h
Signature · 7 lines
2.17.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

PATCH v3 1/2 efi/tpcm: Add UEFI interface for TPCM module

TPCM(Trusted Platform Control Module) is a Chinese standard, and the interface implementation complies with UEFI specification. If tpcm related protocol is not implemented in UEFI, then tpcm module directly returns NONE.

---
 grub-core/commands/efi/tpcm.c | 163 ++++++++++++++++++++++++++++++++++
 include/grub/efi/tpcm.h       |  61 +++++++++++++
 include/grub/err.h            |   3 +-
 3 files changed, 226 insertions(+), 1 deletion(-)
 create mode 100755 grub-core/commands/efi/tpcm.c
 create mode 100644 include/grub/efi/tpcm.h
grub-core/commands/efi/tpcm.c new file +163 -0
new file mode 100755
index 000000000..57cd147dc
@@ -0,0 +1,163 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2025  Free Software Foundation, Inc.
+ *
+ *  GRUB 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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  EFI TPCM support code.
+ */
+
+#include <grub/err.h>
+#include <grub/efi/tpcm.h>
+
+
+static grub_uint32_t g_measured_id = STAGE_START;
+
+/*
+ * get_tpcm_stage:
+ * TPCM does not make a distinction with the type of
+ * measured target, so we use g_measured_id directly
+ * for the stage.
+ */
+static grub_uint32_t
+get_tpcm_stage (void)
+{
+    grub_uint32_t stage = STAGE_INVALID;
+
+    stage = g_measured_id;
+
+    if (stage < STAGE_START || stage > STAGE_END)
+      stage = STAGE_INVALID;
+
+    return stage;
+}
+
+/*
+ * update_measured_id:
+ * update g_measured_id +1 every time measured, and g_measured_id
+ * will never be decreased.
+ */
+static void
+update_measured_id (void)
+{
+    g_measured_id++;
+}
+
+/*
+ * measure_memory:
+ * measure the memery region--(addr, size) through the TPCM protocol.
+ * if TPCM protocol is not exist in BIOS, it will return SUCC to keep
+ * compatible with non-measurement-support bios; if TPCM protocol is
+ * exist but not enabled, it will also return SUCC.
+ */
+static grub_err_t
+measure_memory (enum grub_file_type type __attribute__((unused)),
+                char *desc, grub_addr_t addr, grub_size_t size)
+{
+    grub_efi_handle_t   *handles = NULL;
+    grub_efi_uintn_t    num_handles;
+    grub_efi_handle_t   grub_c2p_handle = 0;
+    grub_err_t  test_c2p_err = GRUB_ERR_BAD_OS;
+    grub_guid_t         c2p_guid = C2PGUID;
+    grub_uint32_t       measure_result = 0;
+    grub_uint32_t       control_result = 0;
+    grub_efi_boolean_t    verify_enable = false;
+    grub_size_t         desc_len = 0;
+
+    handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &c2p_guid, NULL, &num_handles);
+    if (handles != NULL && num_handles > 0)
+    {
+        struct c2p_protocol *c2p;
+
+        grub_c2p_handle = handles[0];
+        grub_dprintf ("tpcm", "measue memory addr 0x%lx size 0x%lx  \n", addr, size);
+        c2p = grub_efi_open_protocol (grub_c2p_handle, &c2p_guid,
+                                      GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+        if (c2p != NULL)
+        {
+            verify_enable = c2p->verify_is_enabled (c2p);
+            if (verify_enable == true)
+            {
+                struct addr_range range;
+                grub_efi_status_t status = 0;
+                grub_uint32_t stage = STAGE_INVALID;
+
+                range.start  = addr;
+                range.length = size;
+
+                stage = get_tpcm_stage ();
+                if (stage != STAGE_INVALID)
+                {
+                    desc_len = grub_strlen (desc) + 1;
+                    status = c2p->verify_raw (c2p, stage, (grub_uint64_t)desc, desc_len, 1, &range, &measure_result, &control_result);
+                    if (status == 0 && (control_result & MEASURE_ACTION_MASK) == 0)
+                    {
+                        grub_dprintf ("tpcm", "verify_raw success. stage[%d]desc:[%s]\n", stage, desc);
+                        test_c2p_err = GRUB_ERR_NONE;
+                    }
+                    else
+                    {
+                        grub_dprintf ("tpcm", "verify_raw error\n");
+                        while (1)
+                        {
+                            grub_error (GRUB_ERR_TPCM_VERIFY, "tpcm verify error. stage[%d]desc[%s]\n", stage, desc);
+                            asm volatile ("hlt");
+                        }
+                    }
+                }
+                else
+                  grub_dprintf ("tpcm", "invalid stage\n");
+
+                update_measured_id ();
+
+            }
+            else
+            {
+                grub_dprintf ("tpcm", "image verify not enabled\n");
+                test_c2p_err = GRUB_ERR_NONE;
+            }
+        }
+        else
+            grub_dprintf ("tpcm", "open c2p protocol failed\n");
+    }
+    else
+    {
+        /* keep compatible with non-measurement-support bios. */
+        grub_dprintf ("tpcm", "not found C2P protocol\n");
+        test_c2p_err = GRUB_ERR_NONE;
+    }
+
+    return test_c2p_err;
+}
+
+/* grub_tpcm_measure_memory */
+grub_err_t
+grub_tpcm_measure_memory (void *context, grub_addr_t buf, grub_size_t size)
+{
+    char *p_context = (char *) context;
+    char *p, *p_desc;
+    char tmp[TPCM_MAX_BUF_SIZE] = {'0'};
+    enum grub_file_type type;
+
+    if (p_context == NULL)
+        return GRUB_ERR_BUG;
+
+    p = grub_strchr (p_context, '|');
+    p_desc = p + 1;
+    grub_memcpy (tmp, p_context, (p - p_context));
+    type = grub_strtoul (tmp, 0, 10);
+
+    return measure_memory (type, p_desc, buf, size);
+}
+
include/grub/efi/tpcm.h new file +61 -0

61 lines not on this page · raw message

include/grub/err.h +2 -1
index 202fa8a7a..4d2756097 100644
@@ -75,7 +75,8 @@ typedef enum
     GRUB_ERR_BAD_SIGNATURE,
     GRUB_ERR_BAD_FIRMWARE,
     GRUB_ERR_STILL_REFERENCED,
-    GRUB_ERR_RECURSION_DEPTH
+    GRUB_ERR_RECURSION_DEPTH,
+    GRUB_ERR_TPCM_VERIFY
   }
 grub_err_t;
 
Signature · 7 lines
2.17.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#4 lore raw

PATCH v3 2/2 efi/tpcm: Add complete support of TPCM module

In reply to Khalid Ali

TPCM(Trusted Platform Control Module) is a Chinese standard and has similar function to tpm, but tpcm adds the function of active monitoring and control to the system. It can realize active startup measurement when the system starts,as well as dynamic measurement and monitoring when the program is running, further enhance the security of the system.

---
 grub-core/Makefile.core.def |   7 +++
 grub-core/commands/tpcm.c   | 100 ++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)
 create mode 100755 grub-core/commands/tpcm.c
grub-core/Makefile.core.def +7 -0
index 24e8c8437..fc31dbca4 100644
@@ -2600,6 +2600,13 @@ module = {
   cppflags = '-I$(srcdir)/lib/tss2 -I$(srcdir)/lib/libtasn1-grub';
 };
 
+module = {
+  name = tpcm;
+  common = commands/tpcm.c;
+  efi = commands/efi/tpcm.c;
+  enable = x86_64_efi;
+};
+
 module = {
   name = tr;
   common = commands/tr.c;
grub-core/commands/tpcm.c new file +100 -0

100 lines not on this page · raw message

Signature · 7 lines
2.17.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel