From 0f444376bf811c6b1ca17be44c55021f47749bb5 Mon Sep 17 00:00:00 2001
From: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Date: Thu, 10 Feb 2022 20:46:30 +0300
Subject: [PATCH] XXX: reboot: Add reboot_lava_test= kernel cmdline option

Add reboot_lava_test= kernel cmdline option which skips power-off/reboot
made by drivers, allowing us to test whether basics are working properly
in the LAVA environment.

Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
---
 kernel/reboot.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/kernel/reboot.c b/kernel/reboot.c
index ceca51aed998d..ef90b7cf10560 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -48,6 +48,8 @@ int reboot_cpu;
 enum reboot_type reboot_type = BOOT_ACPI;
 int reboot_force;
 
+static bool reboot_lava_test_mode;
+
 /**
  *	emergency_restart - reboot the system
  *
@@ -235,6 +237,9 @@ void migrate_to_reboot_cpu(void)
  */
 void kernel_restart(char *cmd)
 {
+	if (reboot_lava_test_mode)
+		return do_kernel_restart(cmd);
+
 	kernel_restart_prepare(cmd);
 	migrate_to_reboot_cpu();
 	syscore_shutdown();
@@ -354,6 +359,11 @@ static int sys_off_handler_power_off(struct notifier_block *nb,
 	struct sys_off_handler *h;
 	int ret = NOTIFY_DONE;
 
+	if (reboot_lava_test_mode) {
+		pr_emerg("power-off test: okay\n");
+		return NOTIFY_DONE;
+	}
+
 	priv = container_of(nb, struct sys_off_handler_private_data, power_off_nb);
 	h = container_of(priv, struct sys_off_handler, priv);
 	prep_data.cb_data = h->cb_data;
@@ -395,6 +405,11 @@ static int sys_off_handler_restart(struct notifier_block *nb,
 	struct restart_data data = {};
 	struct sys_off_handler *h;
 
+	if (reboot_lava_test_mode) {
+		pr_emerg("restart test: okay\n");
+		return NOTIFY_DONE;
+	}
+
 	priv = container_of(nb, struct sys_off_handler_private_data, restart_nb);
 	h = container_of(priv, struct sys_off_handler, priv);
 
@@ -415,6 +430,11 @@ static int sys_off_handler_reboot(struct notifier_block *nb,
 	struct reboot_prep_data data = {};
 	struct sys_off_handler *h;
 
+	if (reboot_lava_test_mode) {
+		pr_emerg("reboot test: okay\n");
+		return NOTIFY_DONE;
+	}
+
 	priv = container_of(nb, struct sys_off_handler_private_data, reboot_nb);
 	h = container_of(priv, struct sys_off_handler, priv);
 
@@ -805,6 +825,9 @@ static void do_kernel_power_off_prepare(void)
  */
 void kernel_power_off(void)
 {
+	if (reboot_lava_test_mode)
+		return do_kernel_power_off();
+
 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
 	do_kernel_power_off_prepare();
 	migrate_to_reboot_cpu();
@@ -1231,6 +1254,17 @@ static int __init reboot_setup(char *str)
 }
 __setup("reboot=", reboot_setup);
 
+static int __init reboot_lava_test_setup(char *str)
+{
+	if (!strcmp(str, "1")) {
+		pr_info("enabled reboot_lava_test_mode\n");
+		reboot_lava_test_mode = 1;
+	}
+
+	return 1;
+}
+__setup("reboot_lava_test=", reboot_lava_test_setup);
+
 #ifdef CONFIG_SYSFS
 
 #define REBOOT_COLD_STR		"cold"
-- 
GitLab