1 // SPDX-License-Identifier: GPL-2.0
3 * efibc: control EFI bootloaders which obey LoaderEntryOneShot var
4 * Copyright (c) 2013-2016, Intel Corporation.
7 #define pr_fmt(fmt) "efibc: " fmt
10 #include <linux/module.h>
11 #include <linux/reboot.h>
12 #include <linux/slab.h>
13 #include <linux/ucs2_string.h>
15 #define MAX_DATA_LEN 512
17 static int efibc_set_variable(efi_char16_t
*name
, efi_char16_t
*value
,
22 status
= efi
.set_variable(name
, &LINUX_EFI_LOADER_ENTRY_GUID
,
23 EFI_VARIABLE_NON_VOLATILE
24 | EFI_VARIABLE_BOOTSERVICE_ACCESS
25 | EFI_VARIABLE_RUNTIME_ACCESS
,
26 len
* sizeof(efi_char16_t
), value
);
28 if (status
!= EFI_SUCCESS
) {
29 pr_err("failed to set EFI variable: 0x%lx\n", status
);
35 static int efibc_reboot_notifier_call(struct notifier_block
*notifier
,
36 unsigned long event
, void *data
)
38 efi_char16_t
*reason
= event
== SYS_RESTART
? L
"reboot"
45 ret
= efibc_set_variable(L
"LoaderEntryRebootReason", reason
,
50 wdata
= kmalloc(MAX_DATA_LEN
* sizeof(efi_char16_t
), GFP_KERNEL
);
54 for (l
= 0; l
< MAX_DATA_LEN
- 1 && str
[l
] != '\0'; l
++)
58 efibc_set_variable(L
"LoaderEntryOneShot", wdata
, l
);
64 static struct notifier_block efibc_reboot_notifier
= {
65 .notifier_call
= efibc_reboot_notifier_call
,
68 static int __init
efibc_init(void)
72 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE
))
75 ret
= register_reboot_notifier(&efibc_reboot_notifier
);
77 pr_err("unable to register reboot notifier\n");
81 module_init(efibc_init
);
83 static void __exit
efibc_exit(void)
85 unregister_reboot_notifier(&efibc_reboot_notifier
);
87 module_exit(efibc_exit
);
89 MODULE_AUTHOR("Jeremy Compostella <jeremy.compostella@intel.com>");
90 MODULE_AUTHOR("Matt Gumbel <matthew.k.gumbel@intel.com");
91 MODULE_DESCRIPTION("EFI Bootloader Control");
92 MODULE_LICENSE("GPL v2");