[PATCH 7/8] Move watchdog infrastructure into separate source file

0 views
Skip to first unread message

Jan Kiszka

unread,
Apr 21, 2026, 2:13:53 PM (5 days ago) Apr 21
to efibootg...@googlegroups.com
From: Jan Kiszka <jan.k...@siemens.com>

This is in preparation of disabling more code in case there are no
watchdog drivers available for a target. No functional code changes.

Signed-off-by: Jan Kiszka <jan.k...@siemens.com>
---
Makefile.am | 1 +
drivers/watchdog/amdfch_wdt.c | 1 +
drivers/watchdog/atom-quark.c | 1 +
drivers/watchdog/eiois200_wdt.c | 1 +
drivers/watchdog/hpwdt.c | 1 +
drivers/watchdog/i6300esb.c | 1 +
drivers/watchdog/ipc4x7e_wdt.c | 1 +
drivers/watchdog/ipcbx21a.c | 1 +
drivers/watchdog/ipmi_wdt.c | 1 +
drivers/watchdog/itco.c | 1 +
drivers/watchdog/w83627hf_wdt.c | 1 +
drivers/watchdog/wdat.c | 1 +
drivers/watchdog/wdfuncs_end.c | 4 +-
drivers/watchdog/wdfuncs_start.c | 4 +-
include/utils.h | 16 ----
include/{utils.h => watchdog.h} | 28 +------
main.c | 100 +------------------------
watchdog.c | 121 +++++++++++++++++++++++++++++++
18 files changed, 138 insertions(+), 147 deletions(-)
copy include/{utils.h => watchdog.h} (54%)
create mode 100644 watchdog.c

diff --git a/Makefile.am b/Makefile.am
index 7c87244..fe1b07c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -235,6 +235,7 @@ efi_sources = \
drivers/watchdog/wdfuncs_start.c \
$(efi_sources_watchdogs) \
drivers/watchdog/wdfuncs_end.c \
+ watchdog.c \
env/syspart.c \
env/fatvars.c \
print.c \
diff --git a/drivers/watchdog/amdfch_wdt.c b/drivers/watchdog/amdfch_wdt.c
index 15b5bdd..c4e07e6 100644
--- a/drivers/watchdog/amdfch_wdt.c
+++ b/drivers/watchdog/amdfch_wdt.c
@@ -20,6 +20,7 @@
#include "mmio.h"
#include "print.h"
#include "utils.h"
+#include "watchdog.h"

/* #define AMDFCH_WDT_DEBUG */

diff --git a/drivers/watchdog/atom-quark.c b/drivers/watchdog/atom-quark.c
index a4c88e1..1bb4d93 100644
--- a/drivers/watchdog/atom-quark.c
+++ b/drivers/watchdog/atom-quark.c
@@ -19,6 +19,7 @@

#include "print.h"
#include "utils.h"
+#include "watchdog.h"

#define PCI_DEVICE_ID_INTEL_ITC 0x8186
#define PCI_DEVICE_ID_INTEL_CENTERTON 0x0c60
diff --git a/drivers/watchdog/eiois200_wdt.c b/drivers/watchdog/eiois200_wdt.c
index b7be223..f9f3e54 100644
--- a/drivers/watchdog/eiois200_wdt.c
+++ b/drivers/watchdog/eiois200_wdt.c
@@ -22,6 +22,7 @@
#include "print.h"
#include "smbios.h"
#include "utils.h"
+#include "watchdog.h"

/* #define EIO200IS_WDT_DEBUG */

diff --git a/drivers/watchdog/hpwdt.c b/drivers/watchdog/hpwdt.c
index 64af90b..e12bd18 100644
--- a/drivers/watchdog/hpwdt.c
+++ b/drivers/watchdog/hpwdt.c
@@ -17,6 +17,7 @@

#include "print.h"
#include "utils.h"
+#include "watchdog.h"

#define PCI_VENDOR_ID_HP 0x103c
#define PCI_VENDOR_ID_HP_3PAR 0x1590
diff --git a/drivers/watchdog/i6300esb.c b/drivers/watchdog/i6300esb.c
index b60dbbe..a86cc29 100644
--- a/drivers/watchdog/i6300esb.c
+++ b/drivers/watchdog/i6300esb.c
@@ -18,6 +18,7 @@

#include "print.h"
#include "utils.h"
+#include "watchdog.h"

#define PCI_DEVICE_ID_INTEL_ESB_9 0x25ab

diff --git a/drivers/watchdog/ipc4x7e_wdt.c b/drivers/watchdog/ipc4x7e_wdt.c
index 899756e..ddc8595 100644
--- a/drivers/watchdog/ipc4x7e_wdt.c
+++ b/drivers/watchdog/ipc4x7e_wdt.c
@@ -22,6 +22,7 @@
#include "print.h"
#include "simatic.h"
#include "utils.h"
+#include "watchdog.h"

#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_LPC 0xa150

diff --git a/drivers/watchdog/ipcbx21a.c b/drivers/watchdog/ipcbx21a.c
index 92d83f3..5bfece7 100644
--- a/drivers/watchdog/ipcbx21a.c
+++ b/drivers/watchdog/ipcbx21a.c
@@ -20,6 +20,7 @@
#include "print.h"
#include "simatic.h"
#include "utils.h"
+#include "watchdog.h"

#define WDT_CTRL_REG_BX_21A 0x1854
#define TIMEOUT_MIN_BX_21A (1)
diff --git a/drivers/watchdog/ipmi_wdt.c b/drivers/watchdog/ipmi_wdt.c
index b088c61..d326010 100644
--- a/drivers/watchdog/ipmi_wdt.c
+++ b/drivers/watchdog/ipmi_wdt.c
@@ -19,6 +19,7 @@
#include "print.h"
#include "smbios.h"
#include "utils.h"
+#include "watchdog.h"

#define SMBIOS_TYPE_IPMI_KCS 38
#define IPMI_KCS_DEFAULT_IOBASE 0xca2
diff --git a/drivers/watchdog/itco.c b/drivers/watchdog/itco.c
index c6ef030..8c3b355 100644
--- a/drivers/watchdog/itco.c
+++ b/drivers/watchdog/itco.c
@@ -21,6 +21,7 @@
#include "mmio.h"
#include "print.h"
#include "utils.h"
+#include "watchdog.h"

#define SMI_EN_REG 0x30
#define TCO_EN BIT(13)
diff --git a/drivers/watchdog/w83627hf_wdt.c b/drivers/watchdog/w83627hf_wdt.c
index 5e037a4..77f4862 100644
--- a/drivers/watchdog/w83627hf_wdt.c
+++ b/drivers/watchdog/w83627hf_wdt.c
@@ -45,6 +45,7 @@
#include "print.h"
#include "simatic.h"
#include "utils.h"
+#include "watchdog.h"

#define WDT_EFER (wdt_io+0) /* Extended Function Enable Registers */
#define WDT_EFIR (wdt_io+0) /* Extended Function Index Register
diff --git a/drivers/watchdog/wdat.c b/drivers/watchdog/wdat.c
index 0dfc581..71c1047 100644
--- a/drivers/watchdog/wdat.c
+++ b/drivers/watchdog/wdat.c
@@ -22,6 +22,7 @@

#include "print.h"
#include "utils.h"
+#include "watchdog.h"

#define EFI_ACPI_TABLE_GUID \
{ 0xeb9d2d30, 0x2d88, 0x11d3, {0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d }}
diff --git a/drivers/watchdog/wdfuncs_end.c b/drivers/watchdog/wdfuncs_end.c
index 97bd6a1..3934b4f 100644
--- a/drivers/watchdog/wdfuncs_end.c
+++ b/drivers/watchdog/wdfuncs_end.c
@@ -14,9 +14,7 @@

#if GNU_EFI_VERSION < 3000016

-#include <efi.h>
-
-#include "utils.h"
+#include "watchdog.h"

/* Section .init_array's end address for watchdog probing function pointers
* preceding this marker, if any. */
diff --git a/drivers/watchdog/wdfuncs_start.c b/drivers/watchdog/wdfuncs_start.c
index b564fff..c353eab 100644
--- a/drivers/watchdog/wdfuncs_start.c
+++ b/drivers/watchdog/wdfuncs_start.c
@@ -14,9 +14,7 @@

#if GNU_EFI_VERSION < 3000016

-#include <efi.h>
-
-#include "utils.h"
+#include "watchdog.h"

/* Section .init_array's sentinel value and start address for watchdog probing
* function pointers following this marker, if any. */
diff --git a/include/utils.h b/include/utils.h
index 261581c..a46767e 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -40,20 +40,4 @@ EFI_DEVICE_PATH *FileDevicePathFromConfig(EFI_HANDLE device,
CHAR16 *payloadpath);
CHAR16 *GetMediumPath(const CHAR16 *input);

-typedef EFI_STATUS (*WATCHDOG_PROBE)(EFI_PCI_IO *, UINT16, UINT16, UINTN);
-
-typedef struct _WATCHDOG_DRIVER {
- WATCHDOG_PROBE probe;
- struct _WATCHDOG_DRIVER *next;
-} WATCHDOG_DRIVER;
-
-VOID register_watchdog(WATCHDOG_DRIVER *driver);
-
-#define WATCHDOG_REGISTER(_func) \
- static WATCHDOG_DRIVER this_driver = {.probe = _func}; \
- static void __attribute__((constructor)) register_driver(void) \
- { \
- register_watchdog(&this_driver); \
- }
-
#define BIT(x) (1UL << (x))
diff --git a/include/utils.h b/include/watchdog.h
similarity index 54%
copy from include/utils.h
copy to include/watchdog.h
index 261581c..beac592 100644
--- a/include/utils.h
+++ b/include/watchdog.h
@@ -1,11 +1,10 @@
/*
* EFI Boot Guard
*
- * Copyright (c) Siemens AG, 2017-2025
+ * Copyright (c) Siemens AG, 2026
*
* Authors:
* Jan Kiszka <jan.k...@siemens.com>
- * Andreas Reichel <andreas.r...@siemens.com>
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
@@ -16,29 +15,6 @@
#pragma once

#include <efi.h>
-#include <efilib.h>
-
-#define MAX_INFO_SIZE 1024
-
-typedef struct _VOLUME_DESC {
- EFI_DEVICE_PATH *devpath;
- BOOLEAN onbootmedium;
- CHAR16 *fslabel;
- CHAR16 *fscustomlabel;
- EFI_FILE_HANDLE root;
-} VOLUME_DESC;
-
-extern VOLUME_DESC *volumes;
-extern UINTN volume_count;
-extern CHAR16 *boot_medium_path;
-
-typedef enum { DOSFSLABEL, CUSTOMLABEL, NOLABEL } LABELMODE;
-
-EFI_STATUS get_volumes(VOLUME_DESC **volumes, UINTN *count);
-EFI_STATUS close_volumes(VOLUME_DESC *volumes, UINTN count);
-EFI_DEVICE_PATH *FileDevicePathFromConfig(EFI_HANDLE device,
- CHAR16 *payloadpath);
-CHAR16 *GetMediumPath(const CHAR16 *input);

typedef EFI_STATUS (*WATCHDOG_PROBE)(EFI_PCI_IO *, UINT16, UINT16, UINTN);

@@ -56,4 +32,4 @@ VOID register_watchdog(WATCHDOG_DRIVER *driver);
register_watchdog(&this_driver); \
}

-#define BIT(x) (1UL << (x))
+EFI_STATUS probe_watchdogs(UINTN timeout);
diff --git a/main.c b/main.c
index 5e9142c..988efa5 100644
--- a/main.c
+++ b/main.c
@@ -25,105 +25,7 @@
#include "print.h"
#include "utils.h"
#include "version.h"
-
-extern const unsigned long wdfuncs_start[];
-extern const unsigned long wdfuncs_end[];
-
-#define PCI_GET_VENDOR_ID(id) (UINT16)(id)
-#define PCI_GET_PRODUCT_ID(id) (UINT16)((id) >> 16)
-
-static WATCHDOG_DRIVER *watchdog_drivers;
-static WATCHDOG_DRIVER *last_watchdog_driver;
-
-VOID register_watchdog(WATCHDOG_DRIVER *driver)
-{
- if (last_watchdog_driver != NULL)
- last_watchdog_driver->next = driver;
- else
- watchdog_drivers = driver;
- last_watchdog_driver = driver;
-}
-
-static EFI_STATUS probe_watchdogs(UINTN timeout)
-{
-#if GNU_EFI_VERSION < 3000016
- const unsigned long *entry = wdfuncs_start;
- for (entry++; entry < wdfuncs_end; entry++) {
- ((void (*)(void))*entry)();
- }
-#endif
- if (watchdog_drivers == NULL) {
- if (timeout > 0) {
- ERROR(L"No watchdog drivers registered, but timeout is non-zero.\n");
- return EFI_UNSUPPORTED;
- }
- return EFI_SUCCESS;
- }
- if (timeout == 0) {
- WARNING(L"Watchdog is disabled.\n");
- return EFI_SUCCESS;
- }
-
- UINTN handle_count = 0;
- EFI_HANDLE *handle_buffer = NULL;
- EFI_STATUS status = BS->LocateHandleBuffer(ByProtocol, &PciIoProtocol,
- NULL, &handle_count,
- &handle_buffer);
- if (EFI_ERROR(status) || (handle_count == 0)) {
- ERROR(L"No PCI I/O Protocol handles found.\n");
- if (handle_buffer) {
- FreePool(handle_buffer);
- }
- return EFI_UNSUPPORTED;
- }
-
- EFI_PCI_IO_PROTOCOL *pci_io;
- UINT32 value;
- for (UINTN index = 0; index < handle_count; index++) {
- status = BS->OpenProtocol(handle_buffer[index], &PciIoProtocol,
- (VOID **)&pci_io, this_image, NULL,
- EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
- if (EFI_ERROR(status)) {
- ERROR(L"Cannot not open PciIoProtocol: %r\n", status);
- FreePool(handle_buffer);
- return status;
- }
-
- status = pci_io->Pci.Read(pci_io, EfiPciIoWidthUint32,
- PCI_VENDOR_ID, 1, &value);
- if (EFI_ERROR(status)) {
- WARNING(
- L"Cannot not read from PCI device, skipping: %r\n",
- status);
- (VOID) BS->CloseProtocol(handle_buffer[index],
- &PciIoProtocol, this_image,
- NULL);
- continue;
- }
-
- WATCHDOG_DRIVER *driver = watchdog_drivers;
- while (driver) {
- status = driver->probe(pci_io,
- PCI_GET_VENDOR_ID(value),
- PCI_GET_PRODUCT_ID(value),
- timeout);
- if (status == EFI_SUCCESS) {
- break;
- }
- driver = driver->next;
- }
-
- (VOID) BS->CloseProtocol(handle_buffer[index], &PciIoProtocol,
- this_image, NULL);
-
- if (status == EFI_SUCCESS) {
- break;
- }
- }
- FreePool(handle_buffer);
-
- return status;
-}
+#include "watchdog.h"

EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
{
diff --git a/watchdog.c b/watchdog.c
new file mode 100644
index 0000000..7a714fd
--- /dev/null
+++ b/watchdog.c
@@ -0,0 +1,121 @@
+/*
+ * EFI Boot Guard
+ *
+ * Copyright (c) Siemens AG, 2017-2025
+ *
+ * Authors:
+ * Jan Kiszka <jan.k...@siemens.com>
+ * Andreas Reichel <andreas.r...@siemens.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+
+#include <efi.h>
+#include <pci/header.h>
+
+#include "bootguard.h"
+#include "print.h"
+#include "utils.h"
+#include "watchdog.h"
+
+extern const unsigned long wdfuncs_start[];
+extern const unsigned long wdfuncs_end[];
+
+#define PCI_GET_VENDOR_ID(id) (UINT16)(id)
+#define PCI_GET_PRODUCT_ID(id) (UINT16)((id) >> 16)
+
+static WATCHDOG_DRIVER *watchdog_drivers;
+static WATCHDOG_DRIVER *last_watchdog_driver;
+
+VOID register_watchdog(WATCHDOG_DRIVER *driver)
+{
+ if (last_watchdog_driver != NULL)
+ last_watchdog_driver->next = driver;
+ else
+ watchdog_drivers = driver;
+ last_watchdog_driver = driver;
+}
+
+EFI_STATUS probe_watchdogs(UINTN timeout)
+{
+#if GNU_EFI_VERSION < 3000016
+ const unsigned long *entry = wdfuncs_start;
+ for (entry++; entry < wdfuncs_end; entry++) {
+ ((void (*)(void))*entry)();
+ }
+#endif
+ if (watchdog_drivers == NULL) {
+ if (timeout > 0) {
+ ERROR(L"No watchdog drivers registered, but timeout is non-zero.\n");
+ return EFI_UNSUPPORTED;
+ }
+ return EFI_SUCCESS;
+ }
+ if (timeout == 0) {
+ WARNING(L"Watchdog is disabled.\n");
+ return EFI_SUCCESS;
+ }
+
+ UINTN handle_count = 0;
+ EFI_HANDLE *handle_buffer = NULL;
+ EFI_STATUS status = BS->LocateHandleBuffer(ByProtocol, &PciIoProtocol,
+ NULL, &handle_count,
+ &handle_buffer);
+ if (EFI_ERROR(status) || (handle_count == 0)) {
+ ERROR(L"No PCI I/O Protocol handles found.\n");
+ if (handle_buffer) {
+ FreePool(handle_buffer);
+ }
+ return EFI_UNSUPPORTED;
+ }
+
+ EFI_PCI_IO_PROTOCOL *pci_io;
+ UINT32 value;
+ for (UINTN index = 0; index < handle_count; index++) {
+ status = BS->OpenProtocol(handle_buffer[index], &PciIoProtocol,
+ (VOID **)&pci_io, this_image, NULL,
+ EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
+ if (EFI_ERROR(status)) {
+ ERROR(L"Cannot not open PciIoProtocol: %r\n", status);
+ FreePool(handle_buffer);
+ return status;
+ }
+
+ status = pci_io->Pci.Read(pci_io, EfiPciIoWidthUint32,
+ PCI_VENDOR_ID, 1, &value);
+ if (EFI_ERROR(status)) {
+ WARNING(
+ L"Cannot not read from PCI device, skipping: %r\n",
+ status);
+ (VOID) BS->CloseProtocol(handle_buffer[index],
+ &PciIoProtocol, this_image,
+ NULL);
+ continue;
+ }
+
+ WATCHDOG_DRIVER *driver = watchdog_drivers;
+ while (driver) {
+ status = driver->probe(pci_io,
+ PCI_GET_VENDOR_ID(value),
+ PCI_GET_PRODUCT_ID(value),
+ timeout);
+ if (status == EFI_SUCCESS) {
+ break;
+ }
+ driver = driver->next;
+ }
+
+ (VOID) BS->CloseProtocol(handle_buffer[index], &PciIoProtocol,
+ this_image, NULL);
+
+ if (status == EFI_SUCCESS) {
+ break;
+ }
+ }
+ FreePool(handle_buffer);
+
+ return status;
+}
--
2.47.3

Reply all
Reply to author
Forward
0 new messages