[Concept,04/24] panic: Provide a way to poweroff on panic

Message ID 20251031065439.3251464-5-sjg@u-boot.org
State New
Headers
Series luks: Provide basic support for unlocking a LUKS1 partition |

Commit Message

Simon Glass Oct. 31, 2025, 6:54 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

For sandbox it normally doesn't make sense to reset when a panic occurs,
since presumably it will just happen again. Add an option to power off
instead.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 lib/Kconfig | 8 ++++++++
 lib/panic.c | 8 ++++++++
 2 files changed, 16 insertions(+)
  

Patch

diff --git a/lib/Kconfig b/lib/Kconfig
index c45a98313aa..9de4667731e 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -263,6 +263,14 @@  config PANIC_HANG
 	  development since you can try to debug the conditions that lead to
 	  the situation.
 
+config PANIC_POWEROFF
+	bool "Power off the system on fatal error"
+	help
+	  Define this option to power off the system in case of a fatal error,
+	  instead of resetting. This is useful for development and testing to
+	  avoid infinite reset loops when debugging issues like stack smashing.
+	  The system will power off using sysreset.
+
 config REGEX
 	bool "Enable regular expression support"
 	default y if NET
diff --git a/lib/panic.c b/lib/panic.c
index 0f578b5b513..adc338860a5 100644
--- a/lib/panic.c
+++ b/lib/panic.c
@@ -13,6 +13,9 @@ 
 #if !defined(CONFIG_PANIC_HANG)
 #include <command.h>
 #endif
+#if defined(CONFIG_PANIC_POWEROFF)
+#include <sysreset.h>
+#endif
 #include <linux/delay.h>
 #include <stdio.h>
 
@@ -23,6 +26,11 @@  static void panic_finish(void)
 	putc('\n');
 #if defined(CONFIG_PANIC_HANG)
 	hang();
+#elif defined(CONFIG_PANIC_POWEROFF)
+	flush();  /* flush the panic message before power off */
+
+	sysreset_walk(SYSRESET_POWER_OFF);
+	hang();  /* hang if power off fails */
 #else
 	flush();  /* flush the panic message before reset */