Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * efibc: control EFI bootloaders which obey LoaderEntryOneShot var
  4 * Copyright (c) 2013-2016, Intel Corporation.
  5 */
  6
  7#define pr_fmt(fmt) "efibc: " fmt
  8
  9#include <linux/efi.h>
 10#include <linux/module.h>
 11#include <linux/reboot.h>
 12#include <linux/slab.h>
 
 13
 14static void efibc_str_to_str16(const char *str, efi_char16_t *str16)
 15{
 16	size_t i;
 17
 18	for (i = 0; i < strlen(str); i++)
 19		str16[i] = str[i];
 20
 21	str16[i] = '\0';
 22}
 23
 24static int efibc_set_variable(const char *name, const char *value)
 
 25{
 26	int ret;
 27	efi_guid_t guid = LINUX_EFI_LOADER_ENTRY_GUID;
 28	struct efivar_entry *entry;
 29	size_t size = (strlen(value) + 1) * sizeof(efi_char16_t);
 30
 31	if (size > sizeof(entry->var.Data)) {
 32		pr_err("value is too large (%zu bytes) for '%s' EFI variable\n", size, name);
 33		return -EINVAL;
 34	}
 35
 36	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
 37	if (!entry) {
 38		pr_err("failed to allocate efivar entry for '%s' EFI variable\n", name);
 39		return -ENOMEM;
 
 
 
 
 
 40	}
 41
 42	efibc_str_to_str16(name, entry->var.VariableName);
 43	efibc_str_to_str16(value, (efi_char16_t *)entry->var.Data);
 44	memcpy(&entry->var.VendorGuid, &guid, sizeof(guid));
 45
 46	ret = efivar_entry_set_safe(entry->var.VariableName,
 47				    entry->var.VendorGuid,
 48				    EFI_VARIABLE_NON_VOLATILE
 49				    | EFI_VARIABLE_BOOTSERVICE_ACCESS
 50				    | EFI_VARIABLE_RUNTIME_ACCESS,
 51				    false, size, entry->var.Data);
 52
 53	if (ret)
 54		pr_err("failed to set %s EFI variable: 0x%x\n",
 55		       name, ret);
 56
 57	kfree(entry);
 58	return ret;
 59}
 60
 61static int efibc_reboot_notifier_call(struct notifier_block *notifier,
 62				      unsigned long event, void *data)
 63{
 64	const char *reason = "shutdown";
 
 
 
 
 65	int ret;
 66
 67	if (event == SYS_RESTART)
 68		reason = "reboot";
 69
 70	ret = efibc_set_variable("LoaderEntryRebootReason", reason);
 71	if (ret || !data)
 72		return NOTIFY_DONE;
 73
 74	efibc_set_variable("LoaderEntryOneShot", (char *)data);
 
 
 
 
 
 
 
 
 75
 
 76	return NOTIFY_DONE;
 77}
 78
 79static struct notifier_block efibc_reboot_notifier = {
 80	.notifier_call = efibc_reboot_notifier_call,
 81};
 82
 83static int __init efibc_init(void)
 84{
 85	int ret;
 86
 87	if (!efivars_kobject() || !efivar_supports_writes())
 88		return -ENODEV;
 89
 90	ret = register_reboot_notifier(&efibc_reboot_notifier);
 91	if (ret)
 92		pr_err("unable to register reboot notifier\n");
 93
 94	return ret;
 95}
 96module_init(efibc_init);
 97
 98static void __exit efibc_exit(void)
 99{
100	unregister_reboot_notifier(&efibc_reboot_notifier);
101}
102module_exit(efibc_exit);
103
104MODULE_AUTHOR("Jeremy Compostella <jeremy.compostella@intel.com>");
105MODULE_AUTHOR("Matt Gumbel <matthew.k.gumbel@intel.com");
106MODULE_DESCRIPTION("EFI Bootloader Control");
107MODULE_LICENSE("GPL v2");
v6.13.7
 1// SPDX-License-Identifier: GPL-2.0
 2/*
 3 * efibc: control EFI bootloaders which obey LoaderEntryOneShot var
 4 * Copyright (c) 2013-2016, Intel Corporation.
 5 */
 6
 7#define pr_fmt(fmt) "efibc: " fmt
 8
 9#include <linux/efi.h>
10#include <linux/module.h>
11#include <linux/reboot.h>
12#include <linux/slab.h>
13#include <linux/ucs2_string.h>
14
15#define MAX_DATA_LEN	512
 
 
 
 
 
 
 
 
16
17static int efibc_set_variable(efi_char16_t *name, efi_char16_t *value,
18			      unsigned long len)
19{
20	efi_status_t status;
 
 
 
 
 
 
 
 
21
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);
27
28	if (status != EFI_SUCCESS) {
29		pr_err("failed to set EFI variable: 0x%lx\n", status);
30		return -EIO;
31	}
32	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33}
34
35static int efibc_reboot_notifier_call(struct notifier_block *notifier,
36				      unsigned long event, void *data)
37{
38	efi_char16_t *reason = event == SYS_RESTART ? L"reboot"
39						    : L"shutdown";
40	const u8 *str = data;
41	efi_char16_t *wdata;
42	unsigned long l;
43	int ret;
44
45	ret = efibc_set_variable(L"LoaderEntryRebootReason", reason,
46				 ucs2_strlen(reason));
 
 
47	if (ret || !data)
48		return NOTIFY_DONE;
49
50	wdata = kmalloc(MAX_DATA_LEN * sizeof(efi_char16_t), GFP_KERNEL);
51	if (!wdata)
52		return NOTIFY_DONE;
53
54	for (l = 0; l < MAX_DATA_LEN - 1 && str[l] != '\0'; l++)
55		wdata[l] = str[l];
56	wdata[l] = L'\0';
57
58	efibc_set_variable(L"LoaderEntryOneShot", wdata, l);
59
60	kfree(wdata);
61	return NOTIFY_DONE;
62}
63
64static struct notifier_block efibc_reboot_notifier = {
65	.notifier_call = efibc_reboot_notifier_call,
66};
67
68static int __init efibc_init(void)
69{
70	int ret;
71
72	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
73		return -ENODEV;
74
75	ret = register_reboot_notifier(&efibc_reboot_notifier);
76	if (ret)
77		pr_err("unable to register reboot notifier\n");
78
79	return ret;
80}
81module_init(efibc_init);
82
83static void __exit efibc_exit(void)
84{
85	unregister_reboot_notifier(&efibc_reboot_notifier);
86}
87module_exit(efibc_exit);
88
89MODULE_AUTHOR("Jeremy Compostella <jeremy.compostella@intel.com>");
90MODULE_AUTHOR("Matt Gumbel <matthew.k.gumbel@intel.com");
91MODULE_DESCRIPTION("EFI Bootloader Control");
92MODULE_LICENSE("GPL v2");