efibc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #define pr_fmt(fmt) "efibc: " fmt
  7. #include <linux/efi.h>
  8. #include <linux/module.h>
  9. #include <linux/reboot.h>
  10. #include <linux/slab.h>
  11. static void efibc_str_to_str16(const char *str, efi_char16_t *str16)
  12. {
  13. size_t i;
  14. for (i = 0; i < strlen(str); i++)
  15. str16[i] = str[i];
  16. str16[i] = '\0';
  17. }
  18. static int efibc_set_variable(const char *name, const char *value)
  19. {
  20. int ret;
  21. efi_guid_t guid = LINUX_EFI_LOADER_ENTRY_GUID;
  22. struct efivar_entry *entry;
  23. size_t size = (strlen(value) + 1) * sizeof(efi_char16_t);
  24. if (size > sizeof(entry->var.Data)) {
  25. pr_err("value is too large (%zu bytes) for '%s' EFI variable\n", size, name);
  26. return -EINVAL;
  27. }
  28. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  29. if (!entry) {
  30. pr_err("failed to allocate efivar entry for '%s' EFI variable\n", name);
  31. return -ENOMEM;
  32. }
  33. efibc_str_to_str16(name, entry->var.VariableName);
  34. efibc_str_to_str16(value, (efi_char16_t *)entry->var.Data);
  35. memcpy(&entry->var.VendorGuid, &guid, sizeof(guid));
  36. ret = efivar_entry_set_safe(entry->var.VariableName,
  37. entry->var.VendorGuid,
  38. EFI_VARIABLE_NON_VOLATILE
  39. | EFI_VARIABLE_BOOTSERVICE_ACCESS
  40. | EFI_VARIABLE_RUNTIME_ACCESS,
  41. false, size, entry->var.Data);
  42. if (ret)
  43. pr_err("failed to set %s EFI variable: 0x%x\n",
  44. name, ret);
  45. kfree(entry);
  46. return ret;
  47. }
  48. static int efibc_reboot_notifier_call(struct notifier_block *notifier,
  49. unsigned long event, void *data)
  50. {
  51. const char *reason = "shutdown";
  52. int ret;
  53. if (event == SYS_RESTART)
  54. reason = "reboot";
  55. ret = efibc_set_variable("LoaderEntryRebootReason", reason);
  56. if (ret || !data)
  57. return NOTIFY_DONE;
  58. efibc_set_variable("LoaderEntryOneShot", (char *)data);
  59. return NOTIFY_DONE;
  60. }
  61. static struct notifier_block efibc_reboot_notifier = {
  62. .notifier_call = efibc_reboot_notifier_call,
  63. };
  64. static int __init efibc_init(void)
  65. {
  66. int ret;
  67. if (!efivars_kobject() || !efivar_supports_writes())
  68. return -ENODEV;
  69. ret = register_reboot_notifier(&efibc_reboot_notifier);
  70. if (ret)
  71. pr_err("unable to register reboot notifier\n");
  72. return ret;
  73. }
  74. module_init(efibc_init);
  75. static void __exit efibc_exit(void)
  76. {
  77. unregister_reboot_notifier(&efibc_reboot_notifier);
  78. }
  79. module_exit(efibc_exit);
  80. MODULE_AUTHOR("Jeremy Compostella <jeremy.compostella@intel.com>");
  81. MODULE_AUTHOR("Matt Gumbel <matthew.k.gumbel@intel.com");
  82. MODULE_DESCRIPTION("EFI Bootloader Control");
  83. MODULE_LICENSE("GPL v2");