[gbl] Implement GBL EFI Debug protocol

Implement GblEfiDebugProtocol to satisfy GBL requirements.
Prints fatal error tag and resets the system.

TAG=agy
CONV=236c2e4e-69e4-48b6-a8ea-8986bc68bb04

Change-Id: I75c05e6996f76476d1c87dfeb7666f70c26288a1
Reviewed-on: https://turquoise-internal-review.googlesource.com/c/third_party/u-boot/+/1370273
Reviewed-by: David Pursell <dpursell@google.com>
Reviewed-by: Dov Shlachter <dovs@google.com>
Commit-Queue: Sergii Parubochyi <sergiip@google.com>
GitOrigin-RevId: 83f0c0265de61d4a5c560bebb4600eca3debe4fb
diff --git a/include/efi_loader.h b/include/efi_loader.h
index e2894c9..69c4754 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -583,6 +583,7 @@
 efi_status_t efi_rng_sw_register(void);
 efi_status_t efi_gbl_fastboot_register(void);
 efi_status_t efi_gbl_boot_memory_register(void);
+efi_status_t efi_gbl_debug_register(void);
 /* Called by efi_init_obj_list() to do initial measurement */
 efi_status_t efi_tcg2_do_initial_measurement(void);
 /* measure the pe-coff image, extend PCR and add Event Log */
diff --git a/include/gbl_efi_debug_protocol.h b/include/gbl_efi_debug_protocol.h
new file mode 100644
index 0000000..bab382b
--- /dev/null
+++ b/include/gbl_efi_debug_protocol.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, embodiment or
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause-Patent
+ *
+ * This file is dual-licensed under Apache 2.0 and BSD-2-Clause-Patent.
+ * You may choose to use this file under the terms of either:
+ *  (a) the Apache License, Version 2.0, or
+ *  (b) the BSD 2-Clause Patent License.
+ *
+ * Unless you expressly elect the BSD-2-Clause-Patent terms, the Apache-2.0
+ * license terms apply by default.
+ */
+
+#ifndef __GBL_EFI_DEBUG_PROTOCOL_H__
+#define __GBL_EFI_DEBUG_PROTOCOL_H__
+
+#include <gbl_efi_common.h>
+
+static const uint64_t GBL_EFI_DEBUG_PROTOCOL_REVISION = GBL_PROTOCOL_REVISION(1, 0);
+
+EFI_ENUM(GblEfiDebugErrorTag, uint64_t, GBL_EFI_DEBUG_ERROR_TAG_ASSERTION_ERROR,
+         GBL_EFI_DEBUG_ERROR_TAG_PARTITION, GBL_EFI_DEBUG_ERROR_TAG_LOAD_IMAGE,
+         GBL_EFI_DEBUG_ERROR_TAG_BOOT_ERROR);
+
+typedef struct GblEfiDebugProtocol {
+  uint64_t revision;
+
+  efi_status_t (*fatal_error)(struct GblEfiDebugProtocol* self,
+                           /* in */ const void* frame_ptr,
+                           /* in */ GblEfiDebugErrorTag tag);
+} GblEfiDebugProtocol;
+
+#endif  // __GBL_EFI_DEBUG_PROTOCOL_H__ */
diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
index e860185..b74d553 100644
--- a/lib/efi_loader/Makefile
+++ b/lib/efi_loader/Makefile
@@ -95,6 +95,7 @@
 obj-y += gbl_efi_fastboot_transport.o
 obj-y += gbl_efi_fastboot.o
 obj-y += gbl_efi_boot_memory.o
+obj-y += gbl_efi_debug.o
 obj-y += efi_aml_sd_emmc.o
 obj-y += efi_timestamp.o
 
diff --git a/lib/efi_loader/efi_setup.c b/lib/efi_loader/efi_setup.c
index 779d8e6..1541e82 100644
--- a/lib/efi_loader/efi_setup.c
+++ b/lib/efi_loader/efi_setup.c
@@ -326,6 +326,10 @@
 	if (ret != EFI_SUCCESS)
 		goto out;
 
+	ret = efi_gbl_debug_register();
+	if (ret != EFI_SUCCESS)
+		goto out;
+
 	/* Secure boot */
 	ret = efi_init_secure_boot();
 	if (ret != EFI_SUCCESS)
diff --git a/lib/efi_loader/gbl_efi_debug.c b/lib/efi_loader/gbl_efi_debug.c
new file mode 100644
index 0000000..7fa6381
--- /dev/null
+++ b/lib/efi_loader/gbl_efi_debug.c
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2026 The Fuchsia Authors
+ *
+ * SPDX-License-Identifier:	BSD-3-Clause
+ */
+
+#include <common.h>
+#include <efi_loader.h>
+#include <gbl_efi_debug_protocol.h>
+
+static efi_status_t EFIAPI fatal_error(struct GblEfiDebugProtocol *self,
+				       const void *frame_ptr,
+				       GblEfiDebugErrorTag tag)
+{
+	EFI_ENTRY("%p, %p, %llu", self, frame_ptr, tag);
+	printf("GBL FATAL ERROR: tag %llu, frame_ptr %p\n", tag, frame_ptr);
+	return EFI_EXIT(EFI_SUCCESS);
+}
+
+static const efi_guid_t guid = EFI_GUID(0x98ca3da1, 0xc1ac, 0x4402, 0x9c, 0x16,
+					0x75, 0x58, 0xd3, 0xed, 0x57, 0x05);
+
+static GblEfiDebugProtocol protocol = {
+	.revision = GBL_EFI_DEBUG_PROTOCOL_REVISION,
+	.fatal_error = fatal_error,
+};
+
+efi_status_t efi_gbl_debug_register(void)
+{
+	efi_status_t ret = efi_add_protocol(efi_root, &guid, (void *)&protocol);
+	if (ret != EFI_SUCCESS)
+		printf("Cannot install GBL_EFI_DEBUG_PROTOCOL\n");
+	return ret;
+}