secureboot.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Secure boot handling.
  3. *
  4. * Copyright (C) 2013,2014 Linaro Limited
  5. * Roy Franz <roy.franz@linaro.org
  6. * Copyright (C) 2013 Red Hat, Inc.
  7. * Mark Salter <msalter@redhat.com>
  8. *
  9. * This file is part of the Linux kernel, and is made available under the
  10. * terms of the GNU General Public License version 2.
  11. */
  12. #include <linux/efi.h>
  13. #include <asm/efi.h>
  14. /* BIOS variables */
  15. static const efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
  16. static const efi_char16_t const efi_SecureBoot_name[] = {
  17. 'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', 0
  18. };
  19. static const efi_char16_t const efi_SetupMode_name[] = {
  20. 'S', 'e', 't', 'u', 'p', 'M', 'o', 'd', 'e', 0
  21. };
  22. /* SHIM variables */
  23. static const efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
  24. static efi_char16_t const shim_MokSBState_name[] = {
  25. 'M', 'o', 'k', 'S', 'B', 'S', 't', 'a', 't', 'e', 0
  26. };
  27. #define get_efi_var(name, vendor, ...) \
  28. efi_call_runtime(get_variable, \
  29. (efi_char16_t *)(name), (efi_guid_t *)(vendor), \
  30. __VA_ARGS__);
  31. /*
  32. * Determine whether we're in secure boot mode.
  33. */
  34. enum efi_secureboot_mode efi_get_secureboot(efi_system_table_t *sys_table_arg)
  35. {
  36. u32 attr;
  37. u8 secboot, setupmode, moksbstate;
  38. unsigned long size;
  39. efi_status_t status;
  40. size = sizeof(secboot);
  41. status = get_efi_var(efi_SecureBoot_name, &efi_variable_guid,
  42. NULL, &size, &secboot);
  43. if (status == EFI_NOT_FOUND)
  44. return efi_secureboot_mode_disabled;
  45. if (status != EFI_SUCCESS)
  46. goto out_efi_err;
  47. size = sizeof(setupmode);
  48. status = get_efi_var(efi_SetupMode_name, &efi_variable_guid,
  49. NULL, &size, &setupmode);
  50. if (status != EFI_SUCCESS)
  51. goto out_efi_err;
  52. if (secboot == 0 || setupmode == 1)
  53. return efi_secureboot_mode_disabled;
  54. /*
  55. * See if a user has put the shim into insecure mode. If so, and if the
  56. * variable doesn't have the runtime attribute set, we might as well
  57. * honor that.
  58. */
  59. size = sizeof(moksbstate);
  60. status = get_efi_var(shim_MokSBState_name, &shim_guid,
  61. &attr, &size, &moksbstate);
  62. /* If it fails, we don't care why. Default to secure */
  63. if (status != EFI_SUCCESS)
  64. goto secure_boot_enabled;
  65. if (!(attr & EFI_VARIABLE_RUNTIME_ACCESS) && moksbstate == 1)
  66. return efi_secureboot_mode_disabled;
  67. secure_boot_enabled:
  68. pr_efi(sys_table_arg, "UEFI Secure Boot is enabled.\n");
  69. return efi_secureboot_mode_enabled;
  70. out_efi_err:
  71. pr_efi_err(sys_table_arg, "Could not determine UEFI Secure Boot status.\n");
  72. return efi_secureboot_mode_unknown;
  73. }