xref: /openbmc/linux/drivers/firmware/efi/efibc.c (revision 7da5b13d)
14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
206f7d4a1SCompostella, Jeremy /*
306f7d4a1SCompostella, Jeremy  * efibc: control EFI bootloaders which obey LoaderEntryOneShot var
406f7d4a1SCompostella, Jeremy  * Copyright (c) 2013-2016, Intel Corporation.
506f7d4a1SCompostella, Jeremy  */
606f7d4a1SCompostella, Jeremy 
706f7d4a1SCompostella, Jeremy #define pr_fmt(fmt) "efibc: " fmt
806f7d4a1SCompostella, Jeremy 
906f7d4a1SCompostella, Jeremy #include <linux/efi.h>
1006f7d4a1SCompostella, Jeremy #include <linux/module.h>
1106f7d4a1SCompostella, Jeremy #include <linux/reboot.h>
122e121d71SJeremy Compostella #include <linux/slab.h>
13416581e4SArd Biesheuvel #include <linux/ucs2_string.h>
1406f7d4a1SCompostella, Jeremy 
15416581e4SArd Biesheuvel #define MAX_DATA_LEN	512
16416581e4SArd Biesheuvel 
efibc_set_variable(efi_char16_t * name,efi_char16_t * value,unsigned long len)17416581e4SArd Biesheuvel static int efibc_set_variable(efi_char16_t *name, efi_char16_t *value,
18416581e4SArd Biesheuvel 			      unsigned long len)
1906f7d4a1SCompostella, Jeremy {
20416581e4SArd Biesheuvel 	efi_status_t status;
2106f7d4a1SCompostella, Jeremy 
22416581e4SArd Biesheuvel 	status = efi.set_variable(name, &LINUX_EFI_LOADER_ENTRY_GUID,
2306f7d4a1SCompostella, Jeremy 				  EFI_VARIABLE_NON_VOLATILE
2406f7d4a1SCompostella, Jeremy 				  | EFI_VARIABLE_BOOTSERVICE_ACCESS
2506f7d4a1SCompostella, Jeremy 				  | EFI_VARIABLE_RUNTIME_ACCESS,
26416581e4SArd Biesheuvel 				  len * sizeof(efi_char16_t), value);
27975a6166STian Baofeng 
28416581e4SArd Biesheuvel 	if (status != EFI_SUCCESS) {
29416581e4SArd Biesheuvel 		pr_err("failed to set EFI variable: 0x%lx\n", status);
30416581e4SArd Biesheuvel 		return -EIO;
31416581e4SArd Biesheuvel 	}
32416581e4SArd Biesheuvel 	return 0;
3306f7d4a1SCompostella, Jeremy }
3406f7d4a1SCompostella, Jeremy 
efibc_reboot_notifier_call(struct notifier_block * notifier,unsigned long event,void * data)3506f7d4a1SCompostella, Jeremy static int efibc_reboot_notifier_call(struct notifier_block *notifier,
3606f7d4a1SCompostella, Jeremy 				      unsigned long event, void *data)
3706f7d4a1SCompostella, Jeremy {
38416581e4SArd Biesheuvel 	efi_char16_t *reason = event == SYS_RESTART ? L"reboot"
39416581e4SArd Biesheuvel 						    : L"shutdown";
40416581e4SArd Biesheuvel 	const u8 *str = data;
41416581e4SArd Biesheuvel 	efi_char16_t *wdata;
42416581e4SArd Biesheuvel 	unsigned long l;
432e121d71SJeremy Compostella 	int ret;
4406f7d4a1SCompostella, Jeremy 
45416581e4SArd Biesheuvel 	ret = efibc_set_variable(L"LoaderEntryRebootReason", reason,
46416581e4SArd Biesheuvel 				 ucs2_strlen(reason));
472e121d71SJeremy Compostella 	if (ret || !data)
4806f7d4a1SCompostella, Jeremy 		return NOTIFY_DONE;
4906f7d4a1SCompostella, Jeremy 
50416581e4SArd Biesheuvel 	wdata = kmalloc(MAX_DATA_LEN * sizeof(efi_char16_t), GFP_KERNEL);
51*7da5b13dSGuilherme G. Piccoli 	if (!wdata)
52*7da5b13dSGuilherme G. Piccoli 		return NOTIFY_DONE;
53*7da5b13dSGuilherme G. Piccoli 
54416581e4SArd Biesheuvel 	for (l = 0; l < MAX_DATA_LEN - 1 && str[l] != '\0'; l++)
55416581e4SArd Biesheuvel 		wdata[l] = str[l];
56416581e4SArd Biesheuvel 	wdata[l] = L'\0';
5706f7d4a1SCompostella, Jeremy 
58416581e4SArd Biesheuvel 	efibc_set_variable(L"LoaderEntryOneShot", wdata, l);
59416581e4SArd Biesheuvel 
60416581e4SArd Biesheuvel 	kfree(wdata);
6106f7d4a1SCompostella, Jeremy 	return NOTIFY_DONE;
6206f7d4a1SCompostella, Jeremy }
6306f7d4a1SCompostella, Jeremy 
6406f7d4a1SCompostella, Jeremy static struct notifier_block efibc_reboot_notifier = {
6506f7d4a1SCompostella, Jeremy 	.notifier_call = efibc_reboot_notifier_call,
6606f7d4a1SCompostella, Jeremy };
6706f7d4a1SCompostella, Jeremy 
efibc_init(void)6806f7d4a1SCompostella, Jeremy static int __init efibc_init(void)
6906f7d4a1SCompostella, Jeremy {
7006f7d4a1SCompostella, Jeremy 	int ret;
7106f7d4a1SCompostella, Jeremy 
72416581e4SArd Biesheuvel 	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
7306f7d4a1SCompostella, Jeremy 		return -ENODEV;
7406f7d4a1SCompostella, Jeremy 
7506f7d4a1SCompostella, Jeremy 	ret = register_reboot_notifier(&efibc_reboot_notifier);
7606f7d4a1SCompostella, Jeremy 	if (ret)
7706f7d4a1SCompostella, Jeremy 		pr_err("unable to register reboot notifier\n");
7806f7d4a1SCompostella, Jeremy 
7906f7d4a1SCompostella, Jeremy 	return ret;
8006f7d4a1SCompostella, Jeremy }
8106f7d4a1SCompostella, Jeremy module_init(efibc_init);
8206f7d4a1SCompostella, Jeremy 
efibc_exit(void)8306f7d4a1SCompostella, Jeremy static void __exit efibc_exit(void)
8406f7d4a1SCompostella, Jeremy {
8506f7d4a1SCompostella, Jeremy 	unregister_reboot_notifier(&efibc_reboot_notifier);
8606f7d4a1SCompostella, Jeremy }
8706f7d4a1SCompostella, Jeremy module_exit(efibc_exit);
8806f7d4a1SCompostella, Jeremy 
8906f7d4a1SCompostella, Jeremy MODULE_AUTHOR("Jeremy Compostella <jeremy.compostella@intel.com>");
9006f7d4a1SCompostella, Jeremy MODULE_AUTHOR("Matt Gumbel <matthew.k.gumbel@intel.com");
9106f7d4a1SCompostella, Jeremy MODULE_DESCRIPTION("EFI Bootloader Control");
9206f7d4a1SCompostella, Jeremy MODULE_LICENSE("GPL v2");
93