1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/kernel/reboot.c
5 * Copyright (C) 2013 Linus Torvalds
8 #define pr_fmt(fmt) "reboot: " fmt
10 #include <linux/atomic.h>
11 #include <linux/ctype.h>
12 #include <linux/export.h>
13 #include <linux/kexec.h>
14 #include <linux/kmod.h>
15 #include <linux/kmsg_dump.h>
16 #include <linux/reboot.h>
17 #include <linux/suspend.h>
18 #include <linux/syscalls.h>
19 #include <linux/syscore_ops.h>
20 #include <linux/uaccess.h>
23 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
28 EXPORT_SYMBOL(cad_pid);
30 #if defined(CONFIG_ARM)
31 #define DEFAULT_REBOOT_MODE = REBOOT_HARD
33 #define DEFAULT_REBOOT_MODE
35 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
36 EXPORT_SYMBOL_GPL(reboot_mode);
37 enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
40 * This variable is used privately to keep track of whether or not
41 * reboot_type is still set to its default value (i.e., reboot= hasn't
42 * been set on the command line). This is needed so that we can
43 * suppress DMI scanning for reboot quirks. Without it, it's
44 * impossible to override a faulty reboot quirk without recompiling.
46 int reboot_default = 1;
48 enum reboot_type reboot_type = BOOT_ACPI;
51 struct sys_off_handler {
52 struct notifier_block nb;
53 int (*sys_off_cb)(struct sys_off_data *data);
55 enum sys_off_mode mode;
61 * Temporary stub that prevents linkage failure while we're in process
62 * of removing all uses of legacy pm_power_off() around the kernel.
64 void __weak (*pm_power_off)(void);
67 * If set, this is used for preparing the system to power off.
70 void (*pm_power_off_prepare)(void);
71 EXPORT_SYMBOL_GPL(pm_power_off_prepare);
74 * emergency_restart - reboot the system
76 * Without shutting down any hardware or taking any locks
77 * reboot the system. This is called when we know we are in
78 * trouble so this is our best effort to reboot. This is
79 * safe to call in interrupt context.
81 void emergency_restart(void)
83 kmsg_dump(KMSG_DUMP_EMERG);
84 machine_emergency_restart();
86 EXPORT_SYMBOL_GPL(emergency_restart);
88 void kernel_restart_prepare(char *cmd)
90 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
91 system_state = SYSTEM_RESTART;
92 usermodehelper_disable();
97 * register_reboot_notifier - Register function to be called at reboot time
98 * @nb: Info about notifier function to be called
100 * Registers a function with the list of functions
101 * to be called at reboot time.
103 * Currently always returns zero, as blocking_notifier_chain_register()
104 * always returns zero.
106 int register_reboot_notifier(struct notifier_block *nb)
108 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
110 EXPORT_SYMBOL(register_reboot_notifier);
113 * unregister_reboot_notifier - Unregister previously registered reboot notifier
114 * @nb: Hook to be unregistered
116 * Unregisters a previously registered reboot
119 * Returns zero on success, or %-ENOENT on failure.
121 int unregister_reboot_notifier(struct notifier_block *nb)
123 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
125 EXPORT_SYMBOL(unregister_reboot_notifier);
127 static void devm_unregister_reboot_notifier(struct device *dev, void *res)
129 WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
132 int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
134 struct notifier_block **rcnb;
137 rcnb = devres_alloc(devm_unregister_reboot_notifier,
138 sizeof(*rcnb), GFP_KERNEL);
142 ret = register_reboot_notifier(nb);
145 devres_add(dev, rcnb);
152 EXPORT_SYMBOL(devm_register_reboot_notifier);
155 * Notifier list for kernel code which wants to be called
156 * to restart the system.
158 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
161 * register_restart_handler - Register function to be called to reset
163 * @nb: Info about handler function to be called
164 * @nb->priority: Handler priority. Handlers should follow the
165 * following guidelines for setting priorities.
166 * 0: Restart handler of last resort,
167 * with limited restart capabilities
168 * 128: Default restart handler; use if no other
169 * restart handler is expected to be available,
170 * and/or if restart functionality is
171 * sufficient to restart the entire system
172 * 255: Highest priority restart handler, will
173 * preempt all other restart handlers
175 * Registers a function with code to be called to restart the
178 * Registered functions will be called from machine_restart as last
179 * step of the restart sequence (if the architecture specific
180 * machine_restart function calls do_kernel_restart - see below
182 * Registered functions are expected to restart the system immediately.
183 * If more than one function is registered, the restart handler priority
184 * selects which function will be called first.
186 * Restart handlers are expected to be registered from non-architecture
187 * code, typically from drivers. A typical use case would be a system
188 * where restart functionality is provided through a watchdog. Multiple
189 * restart handlers may exist; for example, one restart handler might
190 * restart the entire system, while another only restarts the CPU.
191 * In such cases, the restart handler which only restarts part of the
192 * hardware is expected to register with low priority to ensure that
193 * it only runs if no other means to restart the system is available.
195 * Currently always returns zero, as atomic_notifier_chain_register()
196 * always returns zero.
198 int register_restart_handler(struct notifier_block *nb)
200 return atomic_notifier_chain_register(&restart_handler_list, nb);
202 EXPORT_SYMBOL(register_restart_handler);
205 * unregister_restart_handler - Unregister previously registered
207 * @nb: Hook to be unregistered
209 * Unregisters a previously registered restart handler function.
211 * Returns zero on success, or %-ENOENT on failure.
213 int unregister_restart_handler(struct notifier_block *nb)
215 return atomic_notifier_chain_unregister(&restart_handler_list, nb);
217 EXPORT_SYMBOL(unregister_restart_handler);
220 * do_kernel_restart - Execute kernel restart handler call chain
222 * Calls functions registered with register_restart_handler.
224 * Expected to be called from machine_restart as last step of the restart
227 * Restarts the system immediately if a restart handler function has been
228 * registered. Otherwise does nothing.
230 void do_kernel_restart(char *cmd)
232 atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
235 void migrate_to_reboot_cpu(void)
237 /* The boot cpu is always logical cpu 0 */
238 int cpu = reboot_cpu;
240 cpu_hotplug_disable();
242 /* Make certain the cpu I'm about to reboot on is online */
243 if (!cpu_online(cpu))
244 cpu = cpumask_first(cpu_online_mask);
246 /* Prevent races with other tasks migrating this task */
247 current->flags |= PF_NO_SETAFFINITY;
249 /* Make certain I only run on the appropriate processor */
250 set_cpus_allowed_ptr(current, cpumask_of(cpu));
254 * kernel_restart - reboot the system
255 * @cmd: pointer to buffer containing command to execute for restart
258 * Shutdown everything and perform a clean reboot.
259 * This is not safe to call in interrupt context.
261 void kernel_restart(char *cmd)
263 kernel_restart_prepare(cmd);
264 migrate_to_reboot_cpu();
267 pr_emerg("Restarting system\n");
269 pr_emerg("Restarting system with command '%s'\n", cmd);
270 kmsg_dump(KMSG_DUMP_SHUTDOWN);
271 machine_restart(cmd);
273 EXPORT_SYMBOL_GPL(kernel_restart);
275 static void kernel_shutdown_prepare(enum system_states state)
277 blocking_notifier_call_chain(&reboot_notifier_list,
278 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
279 system_state = state;
280 usermodehelper_disable();
284 * kernel_halt - halt the system
286 * Shutdown everything and perform a clean system halt.
288 void kernel_halt(void)
290 kernel_shutdown_prepare(SYSTEM_HALT);
291 migrate_to_reboot_cpu();
293 pr_emerg("System halted\n");
294 kmsg_dump(KMSG_DUMP_SHUTDOWN);
297 EXPORT_SYMBOL_GPL(kernel_halt);
300 * Notifier list for kernel code which wants to be called
301 * to prepare system for power off.
303 static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
306 * Notifier list for kernel code which wants to be called
307 * to power off system.
309 static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
311 static int sys_off_notify(struct notifier_block *nb,
312 unsigned long mode, void *cmd)
314 struct sys_off_handler *handler;
315 struct sys_off_data data = {};
317 handler = container_of(nb, struct sys_off_handler, nb);
318 data.cb_data = handler->cb_data;
322 return handler->sys_off_cb(&data);
326 * register_sys_off_handler - Register sys-off handler
327 * @mode: Sys-off mode
328 * @priority: Handler priority
329 * @callback: Callback function
330 * @cb_data: Callback argument
332 * Registers system power-off or restart handler that will be invoked
333 * at the step corresponding to the given sys-off mode. Handler's callback
334 * should return NOTIFY_DONE to permit execution of the next handler in
335 * the call chain or NOTIFY_STOP to break the chain (in error case for
338 * Multiple handlers can be registered at the default priority level.
340 * Only one handler can be registered at the non-default priority level,
341 * otherwise ERR_PTR(-EBUSY) is returned.
343 * Returns a new instance of struct sys_off_handler on success, or
344 * an ERR_PTR()-encoded error code otherwise.
346 struct sys_off_handler *
347 register_sys_off_handler(enum sys_off_mode mode,
349 int (*callback)(struct sys_off_data *data),
352 struct sys_off_handler *handler;
355 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
357 return ERR_PTR(-ENOMEM);
360 case SYS_OFF_MODE_POWER_OFF_PREPARE:
361 handler->list = &power_off_prep_handler_list;
362 handler->blocking = true;
365 case SYS_OFF_MODE_POWER_OFF:
366 handler->list = &power_off_handler_list;
369 case SYS_OFF_MODE_RESTART:
370 handler->list = &restart_handler_list;
375 return ERR_PTR(-EINVAL);
378 handler->nb.notifier_call = sys_off_notify;
379 handler->nb.priority = priority;
380 handler->sys_off_cb = callback;
381 handler->cb_data = cb_data;
382 handler->mode = mode;
384 if (handler->blocking) {
385 if (priority == SYS_OFF_PRIO_DEFAULT)
386 err = blocking_notifier_chain_register(handler->list,
389 err = blocking_notifier_chain_register_unique_prio(handler->list,
392 if (priority == SYS_OFF_PRIO_DEFAULT)
393 err = atomic_notifier_chain_register(handler->list,
396 err = atomic_notifier_chain_register_unique_prio(handler->list,
407 EXPORT_SYMBOL_GPL(register_sys_off_handler);
410 * unregister_sys_off_handler - Unregister sys-off handler
411 * @handler: Sys-off handler
413 * Unregisters given sys-off handler.
415 void unregister_sys_off_handler(struct sys_off_handler *handler)
422 if (handler->blocking)
423 err = blocking_notifier_chain_unregister(handler->list,
426 err = atomic_notifier_chain_unregister(handler->list,
429 /* sanity check, shall never happen */
434 EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
436 static void devm_unregister_sys_off_handler(void *data)
438 struct sys_off_handler *handler = data;
440 unregister_sys_off_handler(handler);
444 * devm_register_sys_off_handler - Register sys-off handler
445 * @dev: Device that registers handler
446 * @mode: Sys-off mode
447 * @priority: Handler priority
448 * @callback: Callback function
449 * @cb_data: Callback argument
451 * Registers resource-managed sys-off handler.
453 * Returns zero on success, or error code on failure.
455 int devm_register_sys_off_handler(struct device *dev,
456 enum sys_off_mode mode,
458 int (*callback)(struct sys_off_data *data),
461 struct sys_off_handler *handler;
463 handler = register_sys_off_handler(mode, priority, callback, cb_data);
465 return PTR_ERR(handler);
467 return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
470 EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
472 static struct sys_off_handler *platform_power_off_handler;
474 static int platform_power_off_notify(struct sys_off_data *data)
476 void (*platform_power_power_off_cb)(void) = data->cb_data;
478 platform_power_power_off_cb();
484 * register_platform_power_off - Register platform-level power-off callback
485 * @power_off: Power-off callback
487 * Registers power-off callback that will be called as last step
488 * of the power-off sequence. This callback is expected to be invoked
489 * for the last resort. Only one platform power-off callback is allowed
490 * to be registered at a time.
492 * Returns zero on success, or error code on failure.
494 int register_platform_power_off(void (*power_off)(void))
496 struct sys_off_handler *handler;
498 handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
499 SYS_OFF_PRIO_PLATFORM,
500 platform_power_off_notify,
503 return PTR_ERR(handler);
505 platform_power_off_handler = handler;
509 EXPORT_SYMBOL_GPL(register_platform_power_off);
512 * unregister_platform_power_off - Unregister platform-level power-off callback
513 * @power_off: Power-off callback
515 * Unregisters previously registered platform power-off callback.
517 void unregister_platform_power_off(void (*power_off)(void))
519 if (platform_power_off_handler &&
520 platform_power_off_handler->cb_data == power_off) {
521 unregister_sys_off_handler(platform_power_off_handler);
522 platform_power_off_handler = NULL;
525 EXPORT_SYMBOL_GPL(unregister_platform_power_off);
527 static int legacy_pm_power_off_prepare(struct sys_off_data *data)
529 if (pm_power_off_prepare)
530 pm_power_off_prepare();
535 static int legacy_pm_power_off(struct sys_off_data *data)
544 * Register sys-off handlers for legacy PM callbacks. This allows legacy
545 * PM callbacks co-exist with the new sys-off API.
547 * TODO: Remove legacy handlers once all legacy PM users will be switched
548 * to the sys-off based APIs.
550 static int __init legacy_pm_init(void)
552 register_sys_off_handler(SYS_OFF_MODE_POWER_OFF_PREPARE,
553 SYS_OFF_PRIO_DEFAULT,
554 legacy_pm_power_off_prepare, NULL);
556 register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT,
557 legacy_pm_power_off, NULL);
561 core_initcall(legacy_pm_init);
563 static void do_kernel_power_off_prepare(void)
565 blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
569 * do_kernel_power_off - Execute kernel power-off handler call chain
571 * Expected to be called as last step of the power-off sequence.
573 * Powers off the system immediately if a power-off handler function has
574 * been registered. Otherwise does nothing.
576 void do_kernel_power_off(void)
578 atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
582 * kernel_can_power_off - check whether system can be powered off
584 * Returns true if power-off handler is registered and system can be
585 * powered off, false otherwise.
587 bool kernel_can_power_off(void)
589 return !atomic_notifier_call_chain_is_empty(&power_off_handler_list);
591 EXPORT_SYMBOL_GPL(kernel_can_power_off);
594 * kernel_power_off - power_off the system
596 * Shutdown everything and perform a clean system power_off.
598 void kernel_power_off(void)
600 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
601 do_kernel_power_off_prepare();
602 migrate_to_reboot_cpu();
604 pr_emerg("Power down\n");
605 kmsg_dump(KMSG_DUMP_SHUTDOWN);
608 EXPORT_SYMBOL_GPL(kernel_power_off);
610 DEFINE_MUTEX(system_transition_mutex);
613 * Reboot system call: for obvious reasons only root may call it,
614 * and even root needs to set up some magic numbers in the registers
615 * so that some mistake won't make this reboot the whole machine.
616 * You can also set the meaning of the ctrl-alt-del-key here.
618 * reboot doesn't sync: do that yourself before calling this.
620 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
623 struct pid_namespace *pid_ns = task_active_pid_ns(current);
627 /* We only trust the superuser with rebooting the system. */
628 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
631 /* For safety, we require "magic" arguments. */
632 if (magic1 != LINUX_REBOOT_MAGIC1 ||
633 (magic2 != LINUX_REBOOT_MAGIC2 &&
634 magic2 != LINUX_REBOOT_MAGIC2A &&
635 magic2 != LINUX_REBOOT_MAGIC2B &&
636 magic2 != LINUX_REBOOT_MAGIC2C))
640 * If pid namespaces are enabled and the current task is in a child
641 * pid_namespace, the command is handled by reboot_pid_ns() which will
644 ret = reboot_pid_ns(pid_ns, cmd);
648 /* Instead of trying to make the power_off code look like
649 * halt when pm_power_off is not set do it the easy way.
651 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
652 cmd = LINUX_REBOOT_CMD_HALT;
654 mutex_lock(&system_transition_mutex);
656 case LINUX_REBOOT_CMD_RESTART:
657 kernel_restart(NULL);
660 case LINUX_REBOOT_CMD_CAD_ON:
664 case LINUX_REBOOT_CMD_CAD_OFF:
668 case LINUX_REBOOT_CMD_HALT:
672 case LINUX_REBOOT_CMD_POWER_OFF:
677 case LINUX_REBOOT_CMD_RESTART2:
678 ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
683 buffer[sizeof(buffer) - 1] = '\0';
685 kernel_restart(buffer);
688 #ifdef CONFIG_KEXEC_CORE
689 case LINUX_REBOOT_CMD_KEXEC:
690 ret = kernel_kexec();
694 #ifdef CONFIG_HIBERNATION
695 case LINUX_REBOOT_CMD_SW_SUSPEND:
704 mutex_unlock(&system_transition_mutex);
708 static void deferred_cad(struct work_struct *dummy)
710 kernel_restart(NULL);
714 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
715 * As it's called within an interrupt, it may NOT sync: the only choice
716 * is whether to reboot at once, or just ignore the ctrl-alt-del.
718 void ctrl_alt_del(void)
720 static DECLARE_WORK(cad_work, deferred_cad);
723 schedule_work(&cad_work);
725 kill_cad_pid(SIGINT, 1);
728 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
729 static const char reboot_cmd[] = "/sbin/reboot";
731 static int run_cmd(const char *cmd)
734 static char *envp[] = {
736 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
740 argv = argv_split(GFP_KERNEL, cmd, NULL);
742 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
751 static int __orderly_reboot(void)
755 ret = run_cmd(reboot_cmd);
758 pr_warn("Failed to start orderly reboot: forcing the issue\n");
760 kernel_restart(NULL);
766 static int __orderly_poweroff(bool force)
770 ret = run_cmd(poweroff_cmd);
773 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
776 * I guess this should try to kick off some daemon to sync and
777 * poweroff asap. Or not even bother syncing if we're doing an
778 * emergency shutdown?
787 static bool poweroff_force;
789 static void poweroff_work_func(struct work_struct *work)
791 __orderly_poweroff(poweroff_force);
794 static DECLARE_WORK(poweroff_work, poweroff_work_func);
797 * orderly_poweroff - Trigger an orderly system poweroff
798 * @force: force poweroff if command execution fails
800 * This may be called from any context to trigger a system shutdown.
801 * If the orderly shutdown fails, it will force an immediate shutdown.
803 void orderly_poweroff(bool force)
805 if (force) /* do not override the pending "true" */
806 poweroff_force = true;
807 schedule_work(&poweroff_work);
809 EXPORT_SYMBOL_GPL(orderly_poweroff);
811 static void reboot_work_func(struct work_struct *work)
816 static DECLARE_WORK(reboot_work, reboot_work_func);
819 * orderly_reboot - Trigger an orderly system reboot
821 * This may be called from any context to trigger a system reboot.
822 * If the orderly reboot fails, it will force an immediate reboot.
824 void orderly_reboot(void)
826 schedule_work(&reboot_work);
828 EXPORT_SYMBOL_GPL(orderly_reboot);
831 * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
832 * @work: work_struct associated with the emergency poweroff function
834 * This function is called in very critical situations to force
835 * a kernel poweroff after a configurable timeout value.
837 static void hw_failure_emergency_poweroff_func(struct work_struct *work)
840 * We have reached here after the emergency shutdown waiting period has
841 * expired. This means orderly_poweroff has not been able to shut off
842 * the system for some reason.
844 * Try to shut down the system immediately using kernel_power_off
847 pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
851 * Worst of the worst case trigger emergency restart
853 pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
857 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
858 hw_failure_emergency_poweroff_func);
861 * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
863 * This may be called from any critical situation to trigger a system shutdown
864 * after a given period of time. If time is negative this is not scheduled.
866 static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
868 if (poweroff_delay_ms <= 0)
870 schedule_delayed_work(&hw_failure_emergency_poweroff_work,
871 msecs_to_jiffies(poweroff_delay_ms));
875 * hw_protection_shutdown - Trigger an emergency system poweroff
877 * @reason: Reason of emergency shutdown to be printed.
878 * @ms_until_forced: Time to wait for orderly shutdown before tiggering a
879 * forced shudown. Negative value disables the forced
882 * Initiate an emergency system shutdown in order to protect hardware from
883 * further damage. Usage examples include a thermal protection or a voltage or
884 * current regulator failures.
885 * NOTE: The request is ignored if protection shutdown is already pending even
886 * if the previous request has given a large timeout for forced shutdown.
887 * Can be called from any context.
889 void hw_protection_shutdown(const char *reason, int ms_until_forced)
891 static atomic_t allow_proceed = ATOMIC_INIT(1);
893 pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
895 /* Shutdown should be initiated only once. */
896 if (!atomic_dec_and_test(&allow_proceed))
900 * Queue a backup emergency shutdown in the event of
901 * orderly_poweroff failure
903 hw_failure_emergency_poweroff(ms_until_forced);
904 orderly_poweroff(true);
906 EXPORT_SYMBOL_GPL(hw_protection_shutdown);
908 static int __init reboot_setup(char *str)
911 enum reboot_mode *mode;
914 * Having anything passed on the command line via
915 * reboot= will cause us to disable DMI checking
920 if (!strncmp(str, "panic_", 6)) {
921 mode = &panic_reboot_mode;
942 * reboot_cpu is s[mp]#### with #### being the processor
943 * to be used for rebooting. Skip 's' or 'smp' prefix.
945 str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
947 if (isdigit(str[0])) {
948 int cpu = simple_strtoul(str, NULL, 0);
950 if (cpu >= num_possible_cpus()) {
951 pr_err("Ignoring the CPU number in reboot= option. "
952 "CPU %d exceeds possible cpu number %d\n",
953 cpu, num_possible_cpus());
979 str = strchr(str, ',');
987 __setup("reboot=", reboot_setup);
991 #define REBOOT_COLD_STR "cold"
992 #define REBOOT_WARM_STR "warm"
993 #define REBOOT_HARD_STR "hard"
994 #define REBOOT_SOFT_STR "soft"
995 #define REBOOT_GPIO_STR "gpio"
996 #define REBOOT_UNDEFINED_STR "undefined"
998 #define BOOT_TRIPLE_STR "triple"
999 #define BOOT_KBD_STR "kbd"
1000 #define BOOT_BIOS_STR "bios"
1001 #define BOOT_ACPI_STR "acpi"
1002 #define BOOT_EFI_STR "efi"
1003 #define BOOT_PCI_STR "pci"
1005 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1009 switch (reboot_mode) {
1011 val = REBOOT_COLD_STR;
1014 val = REBOOT_WARM_STR;
1017 val = REBOOT_HARD_STR;
1020 val = REBOOT_SOFT_STR;
1023 val = REBOOT_GPIO_STR;
1026 val = REBOOT_UNDEFINED_STR;
1029 return sprintf(buf, "%s\n", val);
1031 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
1032 const char *buf, size_t count)
1034 if (!capable(CAP_SYS_BOOT))
1037 if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
1038 reboot_mode = REBOOT_COLD;
1039 else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
1040 reboot_mode = REBOOT_WARM;
1041 else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
1042 reboot_mode = REBOOT_HARD;
1043 else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
1044 reboot_mode = REBOOT_SOFT;
1045 else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
1046 reboot_mode = REBOOT_GPIO;
1054 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
1057 static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1059 return sprintf(buf, "%d\n", reboot_force);
1061 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
1062 const char *buf, size_t count)
1066 if (!capable(CAP_SYS_BOOT))
1069 if (kstrtobool(buf, &res))
1077 static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
1079 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1083 switch (reboot_type) {
1085 val = BOOT_TRIPLE_STR;
1091 val = BOOT_BIOS_STR;
1094 val = BOOT_ACPI_STR;
1099 case BOOT_CF9_FORCE:
1103 val = REBOOT_UNDEFINED_STR;
1106 return sprintf(buf, "%s\n", val);
1108 static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
1109 const char *buf, size_t count)
1111 if (!capable(CAP_SYS_BOOT))
1114 if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
1115 reboot_type = BOOT_TRIPLE;
1116 else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
1117 reboot_type = BOOT_KBD;
1118 else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
1119 reboot_type = BOOT_BIOS;
1120 else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
1121 reboot_type = BOOT_ACPI;
1122 else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
1123 reboot_type = BOOT_EFI;
1124 else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
1125 reboot_type = BOOT_CF9_FORCE;
1133 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
1137 static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1139 return sprintf(buf, "%d\n", reboot_cpu);
1141 static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
1142 const char *buf, size_t count)
1144 unsigned int cpunum;
1147 if (!capable(CAP_SYS_BOOT))
1150 rc = kstrtouint(buf, 0, &cpunum);
1155 if (cpunum >= num_possible_cpus())
1159 reboot_cpu = cpunum;
1163 static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
1166 static struct attribute *reboot_attrs[] = {
1167 &reboot_mode_attr.attr,
1169 &reboot_force_attr.attr,
1170 &reboot_type_attr.attr,
1173 &reboot_cpu_attr.attr,
1178 static const struct attribute_group reboot_attr_group = {
1179 .attrs = reboot_attrs,
1182 static int __init reboot_ksysfs_init(void)
1184 struct kobject *reboot_kobj;
1187 reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
1191 ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
1193 kobject_put(reboot_kobj);
1199 late_initcall(reboot_ksysfs_init);