reboot.c (f9a90501faac55ddbea93c1f73497857f1997227) | reboot.c (2c622ed0eaa38b68d7440bedb8c6cdd138b5a860) |
---|---|
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 --- 586 unchanged lines hidden (view full) --- 595 if (str) 596 str++; 597 else 598 break; 599 } 600 return 1; 601} 602__setup("reboot=", reboot_setup); | 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 --- 586 unchanged lines hidden (view full) --- 595 if (str) 596 str++; 597 else 598 break; 599 } 600 return 1; 601} 602__setup("reboot=", reboot_setup); |
603 604#ifdef CONFIG_SYSFS 605 606#define REBOOT_COLD_STR "cold" 607#define REBOOT_WARM_STR "warm" 608#define REBOOT_HARD_STR "hard" 609#define REBOOT_SOFT_STR "soft" 610#define REBOOT_GPIO_STR "gpio" 611#define REBOOT_UNDEFINED_STR "undefined" 612 613#define BOOT_TRIPLE_STR "triple" 614#define BOOT_KBD_STR "kbd" 615#define BOOT_BIOS_STR "bios" 616#define BOOT_ACPI_STR "acpi" 617#define BOOT_EFI_STR "efi" 618#define BOOT_CF9_FORCE_STR "cf9_force" 619#define BOOT_CF9_SAFE_STR "cf9_safe" 620 621static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 622{ 623 const char *val; 624 625 switch (reboot_mode) { 626 case REBOOT_COLD: 627 val = REBOOT_COLD_STR; 628 break; 629 case REBOOT_WARM: 630 val = REBOOT_WARM_STR; 631 break; 632 case REBOOT_HARD: 633 val = REBOOT_HARD_STR; 634 break; 635 case REBOOT_SOFT: 636 val = REBOOT_SOFT_STR; 637 break; 638 case REBOOT_GPIO: 639 val = REBOOT_GPIO_STR; 640 break; 641 default: 642 val = REBOOT_UNDEFINED_STR; 643 } 644 645 return sprintf(buf, "%s\n", val); 646} 647static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr, 648 const char *buf, size_t count) 649{ 650 if (!capable(CAP_SYS_BOOT)) 651 return -EPERM; 652 653 if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR))) 654 reboot_mode = REBOOT_COLD; 655 else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR))) 656 reboot_mode = REBOOT_WARM; 657 else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR))) 658 reboot_mode = REBOOT_HARD; 659 else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR))) 660 reboot_mode = REBOOT_SOFT; 661 else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR))) 662 reboot_mode = REBOOT_GPIO; 663 else 664 return -EINVAL; 665 666 return count; 667} 668static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode); 669 670static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 671{ 672 const char *val; 673 674 switch (reboot_type) { 675 case BOOT_TRIPLE: 676 val = BOOT_TRIPLE_STR; 677 break; 678 case BOOT_KBD: 679 val = BOOT_KBD_STR; 680 break; 681 case BOOT_BIOS: 682 val = BOOT_BIOS_STR; 683 break; 684 case BOOT_ACPI: 685 val = BOOT_ACPI_STR; 686 break; 687 case BOOT_EFI: 688 val = BOOT_EFI_STR; 689 break; 690 case BOOT_CF9_FORCE: 691 val = BOOT_CF9_FORCE_STR; 692 break; 693 case BOOT_CF9_SAFE: 694 val = BOOT_CF9_SAFE_STR; 695 break; 696 default: 697 val = REBOOT_UNDEFINED_STR; 698 } 699 700 return sprintf(buf, "%s\n", val); 701} 702static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr, 703 const char *buf, size_t count) 704{ 705 if (!capable(CAP_SYS_BOOT)) 706 return -EPERM; 707 708 if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR))) 709 reboot_type = BOOT_TRIPLE; 710 else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR))) 711 reboot_type = BOOT_KBD; 712 else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR))) 713 reboot_type = BOOT_BIOS; 714 else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR))) 715 reboot_type = BOOT_ACPI; 716 else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR))) 717 reboot_type = BOOT_EFI; 718 else if (!strncmp(buf, BOOT_CF9_FORCE_STR, strlen(BOOT_CF9_FORCE_STR))) 719 reboot_type = BOOT_CF9_FORCE; 720 else if (!strncmp(buf, BOOT_CF9_SAFE_STR, strlen(BOOT_CF9_SAFE_STR))) 721 reboot_type = BOOT_CF9_SAFE; 722 else 723 return -EINVAL; 724 725 return count; 726} 727static struct kobj_attribute reboot_type_attr = __ATTR_RW(type); 728 729static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 730{ 731 return sprintf(buf, "%d\n", reboot_cpu); 732} 733static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr, 734 const char *buf, size_t count) 735{ 736 unsigned int cpunum; 737 int rc; 738 739 if (!capable(CAP_SYS_BOOT)) 740 return -EPERM; 741 742 rc = kstrtouint(buf, 0, &cpunum); 743 744 if (rc) 745 return rc; 746 747 if (cpunum >= num_possible_cpus()) 748 return -ERANGE; 749 750 reboot_cpu = cpunum; 751 752 return count; 753} 754static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu); 755 756static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 757{ 758 return sprintf(buf, "%d\n", reboot_force); 759} 760static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, 761 const char *buf, size_t count) 762{ 763 bool res; 764 765 if (!capable(CAP_SYS_BOOT)) 766 return -EPERM; 767 768 if (kstrtobool(buf, &res)) 769 return -EINVAL; 770 771 reboot_force = res; 772 773 return count; 774} 775static struct kobj_attribute reboot_force_attr = __ATTR_RW(force); 776 777static struct attribute *reboot_attrs[] = { 778 &reboot_mode_attr.attr, 779 &reboot_type_attr.attr, 780 &reboot_cpu_attr.attr, 781 &reboot_force_attr.attr, 782 NULL, 783}; 784 785static const struct attribute_group reboot_attr_group = { 786 .attrs = reboot_attrs, 787}; 788 789static int __init reboot_ksysfs_init(void) 790{ 791 struct kobject *reboot_kobj; 792 int ret; 793 794 reboot_kobj = kobject_create_and_add("reboot", kernel_kobj); 795 if (!reboot_kobj) 796 return -ENOMEM; 797 798 ret = sysfs_create_group(reboot_kobj, &reboot_attr_group); 799 if (ret) { 800 kobject_put(reboot_kobj); 801 return ret; 802 } 803 804 return 0; 805} 806late_initcall(reboot_ksysfs_init); 807 808#endif |
|