efibc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #define pr_fmt(fmt) "efibc: " fmt
  15. #include <linux/efi.h>
  16. #include <linux/module.h>
  17. #include <linux/reboot.h>
  18. #include <linux/slab.h>
  19. static void efibc_str_to_str16(const char *str, efi_char16_t *str16)
  20. {
  21. size_t i;
  22. for (i = 0; i < strlen(str); i++)
  23. str16[i] = str[i];
  24. str16[i] = '\0';
  25. }
  26. static int efibc_set_variable(const char *name, const char *value)
  27. {
  28. int ret;
  29. efi_guid_t guid = LINUX_EFI_LOADER_ENTRY_GUID;
  30. struct efivar_entry *entry;
  31. size_t size = (strlen(value) + 1) * sizeof(efi_char16_t);
  32. if (size > sizeof(entry->var.Data)) {
  33. pr_err("value is too large (%zu bytes) for '%s' EFI variable\n", size, name);
  34. return -EINVAL;
  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. 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. ret = efivar_entry_set(entry,
  45. EFI_VARIABLE_NON_VOLATILE
  46. | EFI_VARIABLE_BOOTSERVICE_ACCESS
  47. | EFI_VARIABLE_RUNTIME_ACCESS,
  48. size, entry->var.Data, NULL);
  49. if (ret)
  50. pr_err("failed to set %s EFI variable: 0x%x\n",
  51. name, ret);
  52. kfree(entry);
  53. return ret;
  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. int ret;
  60. if (event == SYS_RESTART)
  61. reason = "reboot";
  62. ret = efibc_set_variable("LoaderEntryRebootReason", reason);
  63. if (ret || !data)
  64. return NOTIFY_DONE;
  65. efibc_set_variable("LoaderEntryOneShot", (char *)data);
  66. return NOTIFY_DONE;
  67. }
  68. static struct notifier_block efibc_reboot_notifier = {
  69. .notifier_call = efibc_reboot_notifier_call,
  70. };
  71. static int __init efibc_init(void)
  72. {
  73. int ret;
  74. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  75. return -ENODEV;
  76. ret = register_reboot_notifier(&efibc_reboot_notifier);
  77. if (ret)
  78. pr_err("unable to register reboot notifier\n");
  79. return ret;
  80. }
  81. module_init(efibc_init);
  82. static void __exit efibc_exit(void)
  83. {
  84. unregister_reboot_notifier(&efibc_reboot_notifier);
  85. }
  86. module_exit(efibc_exit);
  87. MODULE_AUTHOR("Jeremy Compostella <jeremy.compostella@intel.com>");
  88. MODULE_AUTHOR("Matt Gumbel <matthew.k.gumbel@intel.com");
  89. MODULE_DESCRIPTION("EFI Bootloader Control");
  90. MODULE_LICENSE("GPL v2");