0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) "efibc: " fmt
0008
0009 #include <linux/efi.h>
0010 #include <linux/module.h>
0011 #include <linux/reboot.h>
0012 #include <linux/slab.h>
0013 #include <linux/ucs2_string.h>
0014
0015 #define MAX_DATA_LEN 512
0016
0017 static int efibc_set_variable(efi_char16_t *name, efi_char16_t *value,
0018 unsigned long len)
0019 {
0020 efi_status_t status;
0021
0022 status = efi.set_variable(name, &LINUX_EFI_LOADER_ENTRY_GUID,
0023 EFI_VARIABLE_NON_VOLATILE
0024 | EFI_VARIABLE_BOOTSERVICE_ACCESS
0025 | EFI_VARIABLE_RUNTIME_ACCESS,
0026 len * sizeof(efi_char16_t), value);
0027
0028 if (status != EFI_SUCCESS) {
0029 pr_err("failed to set EFI variable: 0x%lx\n", status);
0030 return -EIO;
0031 }
0032 return 0;
0033 }
0034
0035 static int efibc_reboot_notifier_call(struct notifier_block *notifier,
0036 unsigned long event, void *data)
0037 {
0038 efi_char16_t *reason = event == SYS_RESTART ? L"reboot"
0039 : L"shutdown";
0040 const u8 *str = data;
0041 efi_char16_t *wdata;
0042 unsigned long l;
0043 int ret;
0044
0045 ret = efibc_set_variable(L"LoaderEntryRebootReason", reason,
0046 ucs2_strlen(reason));
0047 if (ret || !data)
0048 return NOTIFY_DONE;
0049
0050 wdata = kmalloc(MAX_DATA_LEN * sizeof(efi_char16_t), GFP_KERNEL);
0051 if (!wdata)
0052 return NOTIFY_DONE;
0053
0054 for (l = 0; l < MAX_DATA_LEN - 1 && str[l] != '\0'; l++)
0055 wdata[l] = str[l];
0056 wdata[l] = L'\0';
0057
0058 efibc_set_variable(L"LoaderEntryOneShot", wdata, l);
0059
0060 kfree(wdata);
0061 return NOTIFY_DONE;
0062 }
0063
0064 static struct notifier_block efibc_reboot_notifier = {
0065 .notifier_call = efibc_reboot_notifier_call,
0066 };
0067
0068 static int __init efibc_init(void)
0069 {
0070 int ret;
0071
0072 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
0073 return -ENODEV;
0074
0075 ret = register_reboot_notifier(&efibc_reboot_notifier);
0076 if (ret)
0077 pr_err("unable to register reboot notifier\n");
0078
0079 return ret;
0080 }
0081 module_init(efibc_init);
0082
0083 static void __exit efibc_exit(void)
0084 {
0085 unregister_reboot_notifier(&efibc_reboot_notifier);
0086 }
0087 module_exit(efibc_exit);
0088
0089 MODULE_AUTHOR("Jeremy Compostella <jeremy.compostella@intel.com>");
0090 MODULE_AUTHOR("Matt Gumbel <matthew.k.gumbel@intel.com");
0091 MODULE_DESCRIPTION("EFI Bootloader Control");
0092 MODULE_LICENSE("GPL v2");