1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/kernel/reboot.c
4 *
5 * Copyright (C) 2013 Linus Torvalds
6 */
7
8 #define pr_fmt(fmt) "reboot: " fmt
9
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>
21
22 /*
23 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
24 */
25
26 int C_A_D = 1;
27 struct pid *cad_pid;
28 EXPORT_SYMBOL(cad_pid);
29
30 #if defined(CONFIG_ARM)
31 #define DEFAULT_REBOOT_MODE = REBOOT_HARD
32 #else
33 #define DEFAULT_REBOOT_MODE
34 #endif
35 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
36 EXPORT_SYMBOL_GPL(reboot_mode);
37 enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
38 EXPORT_SYMBOL_GPL(panic_reboot_mode);
39
40 /*
41 * This variable is used privately to keep track of whether or not
42 * reboot_type is still set to its default value (i.e., reboot= hasn't
43 * been set on the command line). This is needed so that we can
44 * suppress DMI scanning for reboot quirks. Without it, it's
45 * impossible to override a faulty reboot quirk without recompiling.
46 */
47 int reboot_default = 1;
48 int reboot_cpu;
49 enum reboot_type reboot_type = BOOT_ACPI;
50 int reboot_force;
51
52 /*
53 * If set, this is used for preparing the system to power off.
54 */
55
56 void (*pm_power_off_prepare)(void);
57 EXPORT_SYMBOL_GPL(pm_power_off_prepare);
58
59 /**
60 * emergency_restart - reboot the system
61 *
62 * Without shutting down any hardware or taking any locks
63 * reboot the system. This is called when we know we are in
64 * trouble so this is our best effort to reboot. This is
65 * safe to call in interrupt context.
66 */
emergency_restart(void)67 void emergency_restart(void)
68 {
69 kmsg_dump(KMSG_DUMP_EMERG);
70 system_state = SYSTEM_RESTART;
71 machine_emergency_restart();
72 }
73 EXPORT_SYMBOL_GPL(emergency_restart);
74
kernel_restart_prepare(char * cmd)75 void kernel_restart_prepare(char *cmd)
76 {
77 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
78 system_state = SYSTEM_RESTART;
79 usermodehelper_disable();
80 device_shutdown();
81 }
82
83 /**
84 * register_reboot_notifier - Register function to be called at reboot time
85 * @nb: Info about notifier function to be called
86 *
87 * Registers a function with the list of functions
88 * to be called at reboot time.
89 *
90 * Currently always returns zero, as blocking_notifier_chain_register()
91 * always returns zero.
92 */
register_reboot_notifier(struct notifier_block * nb)93 int register_reboot_notifier(struct notifier_block *nb)
94 {
95 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
96 }
97 EXPORT_SYMBOL(register_reboot_notifier);
98
99 /**
100 * unregister_reboot_notifier - Unregister previously registered reboot notifier
101 * @nb: Hook to be unregistered
102 *
103 * Unregisters a previously registered reboot
104 * notifier function.
105 *
106 * Returns zero on success, or %-ENOENT on failure.
107 */
unregister_reboot_notifier(struct notifier_block * nb)108 int unregister_reboot_notifier(struct notifier_block *nb)
109 {
110 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
111 }
112 EXPORT_SYMBOL(unregister_reboot_notifier);
113
devm_unregister_reboot_notifier(struct device * dev,void * res)114 static void devm_unregister_reboot_notifier(struct device *dev, void *res)
115 {
116 WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
117 }
118
devm_register_reboot_notifier(struct device * dev,struct notifier_block * nb)119 int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
120 {
121 struct notifier_block **rcnb;
122 int ret;
123
124 rcnb = devres_alloc(devm_unregister_reboot_notifier,
125 sizeof(*rcnb), GFP_KERNEL);
126 if (!rcnb)
127 return -ENOMEM;
128
129 ret = register_reboot_notifier(nb);
130 if (!ret) {
131 *rcnb = nb;
132 devres_add(dev, rcnb);
133 } else {
134 devres_free(rcnb);
135 }
136
137 return ret;
138 }
139 EXPORT_SYMBOL(devm_register_reboot_notifier);
140
141 /*
142 * Notifier list for kernel code which wants to be called
143 * to restart the system.
144 */
145 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
146
147 /**
148 * register_restart_handler - Register function to be called to reset
149 * the system
150 * @nb: Info about handler function to be called
151 * @nb->priority: Handler priority. Handlers should follow the
152 * following guidelines for setting priorities.
153 * 0: Restart handler of last resort,
154 * with limited restart capabilities
155 * 128: Default restart handler; use if no other
156 * restart handler is expected to be available,
157 * and/or if restart functionality is
158 * sufficient to restart the entire system
159 * 255: Highest priority restart handler, will
160 * preempt all other restart handlers
161 *
162 * Registers a function with code to be called to restart the
163 * system.
164 *
165 * Registered functions will be called from machine_restart as last
166 * step of the restart sequence (if the architecture specific
167 * machine_restart function calls do_kernel_restart - see below
168 * for details).
169 * Registered functions are expected to restart the system immediately.
170 * If more than one function is registered, the restart handler priority
171 * selects which function will be called first.
172 *
173 * Restart handlers are expected to be registered from non-architecture
174 * code, typically from drivers. A typical use case would be a system
175 * where restart functionality is provided through a watchdog. Multiple
176 * restart handlers may exist; for example, one restart handler might
177 * restart the entire system, while another only restarts the CPU.
178 * In such cases, the restart handler which only restarts part of the
179 * hardware is expected to register with low priority to ensure that
180 * it only runs if no other means to restart the system is available.
181 *
182 * Currently always returns zero, as atomic_notifier_chain_register()
183 * always returns zero.
184 */
register_restart_handler(struct notifier_block * nb)185 int register_restart_handler(struct notifier_block *nb)
186 {
187 return atomic_notifier_chain_register(&restart_handler_list, nb);
188 }
189 EXPORT_SYMBOL(register_restart_handler);
190
191 /**
192 * unregister_restart_handler - Unregister previously registered
193 * restart handler
194 * @nb: Hook to be unregistered
195 *
196 * Unregisters a previously registered restart handler function.
197 *
198 * Returns zero on success, or %-ENOENT on failure.
199 */
unregister_restart_handler(struct notifier_block * nb)200 int unregister_restart_handler(struct notifier_block *nb)
201 {
202 return atomic_notifier_chain_unregister(&restart_handler_list, nb);
203 }
204 EXPORT_SYMBOL(unregister_restart_handler);
205
206 /**
207 * do_kernel_restart - Execute kernel restart handler call chain
208 *
209 * Calls functions registered with register_restart_handler.
210 *
211 * Expected to be called from machine_restart as last step of the restart
212 * sequence.
213 *
214 * Restarts the system immediately if a restart handler function has been
215 * registered. Otherwise does nothing.
216 */
do_kernel_restart(char * cmd)217 void do_kernel_restart(char *cmd)
218 {
219 atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
220 }
221
migrate_to_reboot_cpu(void)222 void migrate_to_reboot_cpu(void)
223 {
224 /* The boot cpu is always logical cpu 0 */
225 int cpu = reboot_cpu;
226
227 cpu_hotplug_disable();
228
229 /* Make certain the cpu I'm about to reboot on is online */
230 if (!cpu_online(cpu))
231 cpu = cpumask_first(cpu_online_mask);
232
233 /* Prevent races with other tasks migrating this task */
234 current->flags |= PF_NO_SETAFFINITY;
235
236 /* Make certain I only run on the appropriate processor */
237 set_cpus_allowed_ptr(current, cpumask_of(cpu));
238 }
239
240 /**
241 * kernel_restart - reboot the system
242 * @cmd: pointer to buffer containing command to execute for restart
243 * or %NULL
244 *
245 * Shutdown everything and perform a clean reboot.
246 * This is not safe to call in interrupt context.
247 */
kernel_restart(char * cmd)248 void kernel_restart(char *cmd)
249 {
250 kernel_restart_prepare(cmd);
251 migrate_to_reboot_cpu();
252 syscore_shutdown();
253 if (!cmd)
254 pr_emerg("Restarting system\n");
255 else
256 pr_emerg("Restarting system with command '%s'\n", cmd);
257 kmsg_dump(KMSG_DUMP_SHUTDOWN);
258 machine_restart(cmd);
259 }
260 EXPORT_SYMBOL_GPL(kernel_restart);
261
kernel_shutdown_prepare(enum system_states state)262 static void kernel_shutdown_prepare(enum system_states state)
263 {
264 blocking_notifier_call_chain(&reboot_notifier_list,
265 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
266 system_state = state;
267 usermodehelper_disable();
268 device_shutdown();
269 }
270 /**
271 * kernel_halt - halt the system
272 *
273 * Shutdown everything and perform a clean system halt.
274 */
kernel_halt(void)275 void kernel_halt(void)
276 {
277 kernel_shutdown_prepare(SYSTEM_HALT);
278 migrate_to_reboot_cpu();
279 syscore_shutdown();
280 pr_emerg("System halted\n");
281 kmsg_dump(KMSG_DUMP_SHUTDOWN);
282 machine_halt();
283 }
284 EXPORT_SYMBOL_GPL(kernel_halt);
285
286 /**
287 * kernel_power_off - power_off the system
288 *
289 * Shutdown everything and perform a clean system power_off.
290 */
kernel_power_off(void)291 void kernel_power_off(void)
292 {
293 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
294 if (pm_power_off_prepare)
295 pm_power_off_prepare();
296 migrate_to_reboot_cpu();
297 syscore_shutdown();
298 pr_emerg("Power down\n");
299 kmsg_dump(KMSG_DUMP_SHUTDOWN);
300 machine_power_off();
301 }
302 EXPORT_SYMBOL_GPL(kernel_power_off);
303
304 DEFINE_MUTEX(system_transition_mutex);
305
306 /*
307 * Reboot system call: for obvious reasons only root may call it,
308 * and even root needs to set up some magic numbers in the registers
309 * so that some mistake won't make this reboot the whole machine.
310 * You can also set the meaning of the ctrl-alt-del-key here.
311 *
312 * reboot doesn't sync: do that yourself before calling this.
313 */
SYSCALL_DEFINE4(reboot,int,magic1,int,magic2,unsigned int,cmd,void __user *,arg)314 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
315 void __user *, arg)
316 {
317 struct pid_namespace *pid_ns = task_active_pid_ns(current);
318 char buffer[256];
319 int ret = 0;
320
321 /* We only trust the superuser with rebooting the system. */
322 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
323 return -EPERM;
324
325 /* For safety, we require "magic" arguments. */
326 if (magic1 != LINUX_REBOOT_MAGIC1 ||
327 (magic2 != LINUX_REBOOT_MAGIC2 &&
328 magic2 != LINUX_REBOOT_MAGIC2A &&
329 magic2 != LINUX_REBOOT_MAGIC2B &&
330 magic2 != LINUX_REBOOT_MAGIC2C))
331 return -EINVAL;
332
333 /*
334 * If pid namespaces are enabled and the current task is in a child
335 * pid_namespace, the command is handled by reboot_pid_ns() which will
336 * call do_exit().
337 */
338 ret = reboot_pid_ns(pid_ns, cmd);
339 if (ret)
340 return ret;
341
342 /* Instead of trying to make the power_off code look like
343 * halt when pm_power_off is not set do it the easy way.
344 */
345 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
346 cmd = LINUX_REBOOT_CMD_HALT;
347
348 mutex_lock(&system_transition_mutex);
349 switch (cmd) {
350 case LINUX_REBOOT_CMD_RESTART:
351 kernel_restart(NULL);
352 break;
353
354 case LINUX_REBOOT_CMD_CAD_ON:
355 C_A_D = 1;
356 break;
357
358 case LINUX_REBOOT_CMD_CAD_OFF:
359 C_A_D = 0;
360 break;
361
362 case LINUX_REBOOT_CMD_HALT:
363 kernel_halt();
364 do_exit(0);
365 panic("cannot halt");
366
367 case LINUX_REBOOT_CMD_POWER_OFF:
368 kernel_power_off();
369 do_exit(0);
370 break;
371
372 case LINUX_REBOOT_CMD_RESTART2:
373 ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
374 if (ret < 0) {
375 ret = -EFAULT;
376 break;
377 }
378 buffer[sizeof(buffer) - 1] = '\0';
379
380 kernel_restart(buffer);
381 break;
382
383 #ifdef CONFIG_KEXEC_CORE
384 case LINUX_REBOOT_CMD_KEXEC:
385 ret = kernel_kexec();
386 break;
387 #endif
388
389 #ifdef CONFIG_HIBERNATION
390 case LINUX_REBOOT_CMD_SW_SUSPEND:
391 ret = hibernate();
392 break;
393 #endif
394
395 default:
396 ret = -EINVAL;
397 break;
398 }
399 mutex_unlock(&system_transition_mutex);
400 return ret;
401 }
402
deferred_cad(struct work_struct * dummy)403 static void deferred_cad(struct work_struct *dummy)
404 {
405 kernel_restart(NULL);
406 }
407
408 /*
409 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
410 * As it's called within an interrupt, it may NOT sync: the only choice
411 * is whether to reboot at once, or just ignore the ctrl-alt-del.
412 */
ctrl_alt_del(void)413 void ctrl_alt_del(void)
414 {
415 static DECLARE_WORK(cad_work, deferred_cad);
416
417 if (C_A_D)
418 schedule_work(&cad_work);
419 else
420 kill_cad_pid(SIGINT, 1);
421 }
422
423 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
424 static const char reboot_cmd[] = "/sbin/reboot";
425
run_cmd(const char * cmd)426 static int run_cmd(const char *cmd)
427 {
428 char **argv;
429 static char *envp[] = {
430 "HOME=/",
431 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
432 NULL
433 };
434 int ret;
435 argv = argv_split(GFP_KERNEL, cmd, NULL);
436 if (argv) {
437 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
438 argv_free(argv);
439 } else {
440 ret = -ENOMEM;
441 }
442
443 return ret;
444 }
445
__orderly_reboot(void)446 static int __orderly_reboot(void)
447 {
448 int ret;
449
450 ret = run_cmd(reboot_cmd);
451
452 if (ret) {
453 pr_warn("Failed to start orderly reboot: forcing the issue\n");
454 emergency_sync();
455 kernel_restart(NULL);
456 }
457
458 return ret;
459 }
460
__orderly_poweroff(bool force)461 static int __orderly_poweroff(bool force)
462 {
463 int ret;
464
465 ret = run_cmd(poweroff_cmd);
466
467 if (ret && force) {
468 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
469
470 /*
471 * I guess this should try to kick off some daemon to sync and
472 * poweroff asap. Or not even bother syncing if we're doing an
473 * emergency shutdown?
474 */
475 emergency_sync();
476 kernel_power_off();
477 }
478
479 return ret;
480 }
481
482 static bool poweroff_force;
483
poweroff_work_func(struct work_struct * work)484 static void poweroff_work_func(struct work_struct *work)
485 {
486 __orderly_poweroff(poweroff_force);
487 }
488
489 static DECLARE_WORK(poweroff_work, poweroff_work_func);
490
491 /**
492 * orderly_poweroff - Trigger an orderly system poweroff
493 * @force: force poweroff if command execution fails
494 *
495 * This may be called from any context to trigger a system shutdown.
496 * If the orderly shutdown fails, it will force an immediate shutdown.
497 */
orderly_poweroff(bool force)498 void orderly_poweroff(bool force)
499 {
500 if (force) /* do not override the pending "true" */
501 poweroff_force = true;
502 schedule_work(&poweroff_work);
503 }
504 EXPORT_SYMBOL_GPL(orderly_poweroff);
505
reboot_work_func(struct work_struct * work)506 static void reboot_work_func(struct work_struct *work)
507 {
508 __orderly_reboot();
509 }
510
511 static DECLARE_WORK(reboot_work, reboot_work_func);
512
513 /**
514 * orderly_reboot - Trigger an orderly system reboot
515 *
516 * This may be called from any context to trigger a system reboot.
517 * If the orderly reboot fails, it will force an immediate reboot.
518 */
orderly_reboot(void)519 void orderly_reboot(void)
520 {
521 schedule_work(&reboot_work);
522 }
523 EXPORT_SYMBOL_GPL(orderly_reboot);
524
525 /**
526 * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
527 * @work: work_struct associated with the emergency poweroff function
528 *
529 * This function is called in very critical situations to force
530 * a kernel poweroff after a configurable timeout value.
531 */
hw_failure_emergency_poweroff_func(struct work_struct * work)532 static void hw_failure_emergency_poweroff_func(struct work_struct *work)
533 {
534 /*
535 * We have reached here after the emergency shutdown waiting period has
536 * expired. This means orderly_poweroff has not been able to shut off
537 * the system for some reason.
538 *
539 * Try to shut down the system immediately using kernel_power_off
540 * if populated
541 */
542 pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
543 kernel_power_off();
544
545 /*
546 * Worst of the worst case trigger emergency restart
547 */
548 pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
549 emergency_restart();
550 }
551
552 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
553 hw_failure_emergency_poweroff_func);
554
555 /**
556 * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
557 *
558 * This may be called from any critical situation to trigger a system shutdown
559 * after a given period of time. If time is negative this is not scheduled.
560 */
hw_failure_emergency_poweroff(int poweroff_delay_ms)561 static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
562 {
563 if (poweroff_delay_ms <= 0)
564 return;
565 schedule_delayed_work(&hw_failure_emergency_poweroff_work,
566 msecs_to_jiffies(poweroff_delay_ms));
567 }
568
569 /**
570 * hw_protection_shutdown - Trigger an emergency system poweroff
571 *
572 * @reason: Reason of emergency shutdown to be printed.
573 * @ms_until_forced: Time to wait for orderly shutdown before tiggering a
574 * forced shudown. Negative value disables the forced
575 * shutdown.
576 *
577 * Initiate an emergency system shutdown in order to protect hardware from
578 * further damage. Usage examples include a thermal protection or a voltage or
579 * current regulator failures.
580 * NOTE: The request is ignored if protection shutdown is already pending even
581 * if the previous request has given a large timeout for forced shutdown.
582 * Can be called from any context.
583 */
hw_protection_shutdown(const char * reason,int ms_until_forced)584 void hw_protection_shutdown(const char *reason, int ms_until_forced)
585 {
586 static atomic_t allow_proceed = ATOMIC_INIT(1);
587
588 pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
589
590 /* Shutdown should be initiated only once. */
591 if (!atomic_dec_and_test(&allow_proceed))
592 return;
593
594 /*
595 * Queue a backup emergency shutdown in the event of
596 * orderly_poweroff failure
597 */
598 hw_failure_emergency_poweroff(ms_until_forced);
599 orderly_poweroff(true);
600 }
601 EXPORT_SYMBOL_GPL(hw_protection_shutdown);
602
reboot_setup(char * str)603 static int __init reboot_setup(char *str)
604 {
605 for (;;) {
606 enum reboot_mode *mode;
607
608 /*
609 * Having anything passed on the command line via
610 * reboot= will cause us to disable DMI checking
611 * below.
612 */
613 reboot_default = 0;
614
615 if (!strncmp(str, "panic_", 6)) {
616 mode = &panic_reboot_mode;
617 str += 6;
618 } else {
619 mode = &reboot_mode;
620 }
621
622 switch (*str) {
623 case 'w':
624 *mode = REBOOT_WARM;
625 break;
626
627 case 'c':
628 *mode = REBOOT_COLD;
629 break;
630
631 case 'h':
632 *mode = REBOOT_HARD;
633 break;
634
635 case 's':
636 /*
637 * reboot_cpu is s[mp]#### with #### being the processor
638 * to be used for rebooting. Skip 's' or 'smp' prefix.
639 */
640 str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
641
642 if (isdigit(str[0])) {
643 int cpu = simple_strtoul(str, NULL, 0);
644
645 if (cpu >= num_possible_cpus()) {
646 pr_err("Ignoring the CPU number in reboot= option. "
647 "CPU %d exceeds possible cpu number %d\n",
648 cpu, num_possible_cpus());
649 break;
650 }
651 reboot_cpu = cpu;
652 } else
653 *mode = REBOOT_SOFT;
654 break;
655
656 case 'g':
657 *mode = REBOOT_GPIO;
658 break;
659
660 case 'b':
661 case 'a':
662 case 'k':
663 case 't':
664 case 'e':
665 case 'p':
666 reboot_type = *str;
667 break;
668
669 case 'f':
670 reboot_force = 1;
671 break;
672 }
673
674 str = strchr(str, ',');
675 if (str)
676 str++;
677 else
678 break;
679 }
680 return 1;
681 }
682 __setup("reboot=", reboot_setup);
683
684 #ifdef CONFIG_SYSFS
685
686 #define REBOOT_COLD_STR "cold"
687 #define REBOOT_WARM_STR "warm"
688 #define REBOOT_HARD_STR "hard"
689 #define REBOOT_SOFT_STR "soft"
690 #define REBOOT_GPIO_STR "gpio"
691 #define REBOOT_UNDEFINED_STR "undefined"
692
693 #define BOOT_TRIPLE_STR "triple"
694 #define BOOT_KBD_STR "kbd"
695 #define BOOT_BIOS_STR "bios"
696 #define BOOT_ACPI_STR "acpi"
697 #define BOOT_EFI_STR "efi"
698 #define BOOT_PCI_STR "pci"
699
mode_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)700 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
701 {
702 const char *val;
703
704 switch (reboot_mode) {
705 case REBOOT_COLD:
706 val = REBOOT_COLD_STR;
707 break;
708 case REBOOT_WARM:
709 val = REBOOT_WARM_STR;
710 break;
711 case REBOOT_HARD:
712 val = REBOOT_HARD_STR;
713 break;
714 case REBOOT_SOFT:
715 val = REBOOT_SOFT_STR;
716 break;
717 case REBOOT_GPIO:
718 val = REBOOT_GPIO_STR;
719 break;
720 default:
721 val = REBOOT_UNDEFINED_STR;
722 }
723
724 return sprintf(buf, "%s\n", val);
725 }
mode_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)726 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
727 const char *buf, size_t count)
728 {
729 if (!capable(CAP_SYS_BOOT))
730 return -EPERM;
731
732 if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
733 reboot_mode = REBOOT_COLD;
734 else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
735 reboot_mode = REBOOT_WARM;
736 else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
737 reboot_mode = REBOOT_HARD;
738 else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
739 reboot_mode = REBOOT_SOFT;
740 else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
741 reboot_mode = REBOOT_GPIO;
742 else
743 return -EINVAL;
744
745 reboot_default = 0;
746
747 return count;
748 }
749 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
750
751 #ifdef CONFIG_X86
force_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)752 static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
753 {
754 return sprintf(buf, "%d\n", reboot_force);
755 }
force_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)756 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
757 const char *buf, size_t count)
758 {
759 bool res;
760
761 if (!capable(CAP_SYS_BOOT))
762 return -EPERM;
763
764 if (kstrtobool(buf, &res))
765 return -EINVAL;
766
767 reboot_default = 0;
768 reboot_force = res;
769
770 return count;
771 }
772 static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
773
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)774 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
775 {
776 const char *val;
777
778 switch (reboot_type) {
779 case BOOT_TRIPLE:
780 val = BOOT_TRIPLE_STR;
781 break;
782 case BOOT_KBD:
783 val = BOOT_KBD_STR;
784 break;
785 case BOOT_BIOS:
786 val = BOOT_BIOS_STR;
787 break;
788 case BOOT_ACPI:
789 val = BOOT_ACPI_STR;
790 break;
791 case BOOT_EFI:
792 val = BOOT_EFI_STR;
793 break;
794 case BOOT_CF9_FORCE:
795 val = BOOT_PCI_STR;
796 break;
797 default:
798 val = REBOOT_UNDEFINED_STR;
799 }
800
801 return sprintf(buf, "%s\n", val);
802 }
type_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)803 static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
804 const char *buf, size_t count)
805 {
806 if (!capable(CAP_SYS_BOOT))
807 return -EPERM;
808
809 if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
810 reboot_type = BOOT_TRIPLE;
811 else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
812 reboot_type = BOOT_KBD;
813 else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
814 reboot_type = BOOT_BIOS;
815 else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
816 reboot_type = BOOT_ACPI;
817 else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
818 reboot_type = BOOT_EFI;
819 else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
820 reboot_type = BOOT_CF9_FORCE;
821 else
822 return -EINVAL;
823
824 reboot_default = 0;
825
826 return count;
827 }
828 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
829 #endif
830
831 #ifdef CONFIG_SMP
cpu_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)832 static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
833 {
834 return sprintf(buf, "%d\n", reboot_cpu);
835 }
cpu_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)836 static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
837 const char *buf, size_t count)
838 {
839 unsigned int cpunum;
840 int rc;
841
842 if (!capable(CAP_SYS_BOOT))
843 return -EPERM;
844
845 rc = kstrtouint(buf, 0, &cpunum);
846
847 if (rc)
848 return rc;
849
850 if (cpunum >= num_possible_cpus())
851 return -ERANGE;
852
853 reboot_default = 0;
854 reboot_cpu = cpunum;
855
856 return count;
857 }
858 static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
859 #endif
860
861 static struct attribute *reboot_attrs[] = {
862 &reboot_mode_attr.attr,
863 #ifdef CONFIG_X86
864 &reboot_force_attr.attr,
865 &reboot_type_attr.attr,
866 #endif
867 #ifdef CONFIG_SMP
868 &reboot_cpu_attr.attr,
869 #endif
870 NULL,
871 };
872
873 static const struct attribute_group reboot_attr_group = {
874 .attrs = reboot_attrs,
875 };
876
reboot_ksysfs_init(void)877 static int __init reboot_ksysfs_init(void)
878 {
879 struct kobject *reboot_kobj;
880 int ret;
881
882 reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
883 if (!reboot_kobj)
884 return -ENOMEM;
885
886 ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
887 if (ret) {
888 kobject_put(reboot_kobj);
889 return ret;
890 }
891
892 return 0;
893 }
894 late_initcall(reboot_ksysfs_init);
895
896 #endif
897