18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * efibc: control EFI bootloaders which obey LoaderEntryOneShot var
48c2ecf20Sopenharmony_ci * Copyright (c) 2013-2016, Intel Corporation.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "efibc: " fmt
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/efi.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/reboot.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic void efibc_str_to_str16(const char *str, efi_char16_t *str16)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	size_t i;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	for (i = 0; i < strlen(str); i++)
198c2ecf20Sopenharmony_ci		str16[i] = str[i];
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	str16[i] = '\0';
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic int efibc_set_variable(const char *name, const char *value)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	int ret;
278c2ecf20Sopenharmony_ci	efi_guid_t guid = LINUX_EFI_LOADER_ENTRY_GUID;
288c2ecf20Sopenharmony_ci	struct efivar_entry *entry;
298c2ecf20Sopenharmony_ci	size_t size = (strlen(value) + 1) * sizeof(efi_char16_t);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	if (size > sizeof(entry->var.Data)) {
328c2ecf20Sopenharmony_ci		pr_err("value is too large (%zu bytes) for '%s' EFI variable\n", size, name);
338c2ecf20Sopenharmony_ci		return -EINVAL;
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
378c2ecf20Sopenharmony_ci	if (!entry) {
388c2ecf20Sopenharmony_ci		pr_err("failed to allocate efivar entry for '%s' EFI variable\n", name);
398c2ecf20Sopenharmony_ci		return -ENOMEM;
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	efibc_str_to_str16(name, entry->var.VariableName);
438c2ecf20Sopenharmony_ci	efibc_str_to_str16(value, (efi_char16_t *)entry->var.Data);
448c2ecf20Sopenharmony_ci	memcpy(&entry->var.VendorGuid, &guid, sizeof(guid));
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	ret = efivar_entry_set_safe(entry->var.VariableName,
478c2ecf20Sopenharmony_ci				    entry->var.VendorGuid,
488c2ecf20Sopenharmony_ci				    EFI_VARIABLE_NON_VOLATILE
498c2ecf20Sopenharmony_ci				    | EFI_VARIABLE_BOOTSERVICE_ACCESS
508c2ecf20Sopenharmony_ci				    | EFI_VARIABLE_RUNTIME_ACCESS,
518c2ecf20Sopenharmony_ci				    false, size, entry->var.Data);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (ret)
548c2ecf20Sopenharmony_ci		pr_err("failed to set %s EFI variable: 0x%x\n",
558c2ecf20Sopenharmony_ci		       name, ret);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	kfree(entry);
588c2ecf20Sopenharmony_ci	return ret;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int efibc_reboot_notifier_call(struct notifier_block *notifier,
628c2ecf20Sopenharmony_ci				      unsigned long event, void *data)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	const char *reason = "shutdown";
658c2ecf20Sopenharmony_ci	int ret;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	if (event == SYS_RESTART)
688c2ecf20Sopenharmony_ci		reason = "reboot";
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	ret = efibc_set_variable("LoaderEntryRebootReason", reason);
718c2ecf20Sopenharmony_ci	if (ret || !data)
728c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	efibc_set_variable("LoaderEntryOneShot", (char *)data);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic struct notifier_block efibc_reboot_notifier = {
808c2ecf20Sopenharmony_ci	.notifier_call = efibc_reboot_notifier_call,
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic int __init efibc_init(void)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	int ret;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	if (!efivars_kobject() || !efivar_supports_writes())
888c2ecf20Sopenharmony_ci		return -ENODEV;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	ret = register_reboot_notifier(&efibc_reboot_notifier);
918c2ecf20Sopenharmony_ci	if (ret)
928c2ecf20Sopenharmony_ci		pr_err("unable to register reboot notifier\n");
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return ret;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_cimodule_init(efibc_init);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void __exit efibc_exit(void)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	unregister_reboot_notifier(&efibc_reboot_notifier);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_cimodule_exit(efibc_exit);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jeremy Compostella <jeremy.compostella@intel.com>");
1058c2ecf20Sopenharmony_ciMODULE_AUTHOR("Matt Gumbel <matthew.k.gumbel@intel.com");
1068c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("EFI Bootloader Control");
1078c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
108