2e0c7ccd9d9ec506baca32d51b5291e6d3fc467e
[linux-2.6-block.git] / drivers / firmware / efi / efibc.c
1 /*
2  * efibc: control EFI bootloaders which obey LoaderEntryOneShot var
3  * Copyright (c) 2013-2016, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14
15 #define pr_fmt(fmt) "efibc: " fmt
16
17 #include <linux/efi.h>
18 #include <linux/module.h>
19 #include <linux/reboot.h>
20
21 static void efibc_str_to_str16(const char *str, efi_char16_t *str16)
22 {
23         size_t i;
24
25         for (i = 0; i < strlen(str); i++)
26                 str16[i] = str[i];
27
28         str16[i] = '\0';
29 }
30
31 static void efibc_set_variable(const char *name, const char *value)
32 {
33         int ret;
34         efi_guid_t guid = LINUX_EFI_LOADER_ENTRY_GUID;
35         struct efivar_entry entry;
36         size_t size = (strlen(value) + 1) * sizeof(efi_char16_t);
37
38         if (size > sizeof(entry.var.Data))
39                 pr_err("value is too large");
40
41         efibc_str_to_str16(name, entry.var.VariableName);
42         efibc_str_to_str16(value, (efi_char16_t *)entry.var.Data);
43         memcpy(&entry.var.VendorGuid, &guid, sizeof(guid));
44
45         ret = efivar_entry_set(&entry,
46                                EFI_VARIABLE_NON_VOLATILE
47                                | EFI_VARIABLE_BOOTSERVICE_ACCESS
48                                | EFI_VARIABLE_RUNTIME_ACCESS,
49                                size, entry.var.Data, NULL);
50         if (ret)
51                 pr_err("failed to set %s EFI variable: 0x%x\n",
52                        name, ret);
53 }
54
55 static int efibc_reboot_notifier_call(struct notifier_block *notifier,
56                                       unsigned long event, void *data)
57 {
58         const char *reason = "shutdown";
59
60         if (event == SYS_RESTART)
61                 reason = "reboot";
62
63         efibc_set_variable("LoaderEntryRebootReason", reason);
64
65         if (!data)
66                 return NOTIFY_DONE;
67
68         efibc_set_variable("LoaderEntryOneShot", (char *)data);
69
70         return NOTIFY_DONE;
71 }
72
73 static struct notifier_block efibc_reboot_notifier = {
74         .notifier_call = efibc_reboot_notifier_call,
75 };
76
77 static int __init efibc_init(void)
78 {
79         int ret;
80
81         if (!efi_enabled(EFI_RUNTIME_SERVICES))
82                 return -ENODEV;
83
84         ret = register_reboot_notifier(&efibc_reboot_notifier);
85         if (ret)
86                 pr_err("unable to register reboot notifier\n");
87
88         return ret;
89 }
90 module_init(efibc_init);
91
92 static void __exit efibc_exit(void)
93 {
94         unregister_reboot_notifier(&efibc_reboot_notifier);
95 }
96 module_exit(efibc_exit);
97
98 MODULE_AUTHOR("Jeremy Compostella <jeremy.compostella@intel.com>");
99 MODULE_AUTHOR("Matt Gumbel <matthew.k.gumbel@intel.com");
100 MODULE_DESCRIPTION("EFI Bootloader Control");
101 MODULE_LICENSE("GPL v2");