secureboot.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "efistub.h"
  15. /* BIOS variables */
  16. static const efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
  17. static const efi_char16_t efi_SecureBoot_name[] = {
  18. 'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', 0
  19. };
  20. static const efi_char16_t efi_SetupMode_name[] = {
  21. 'S', 'e', 't', 'u', 'p', 'M', 'o', 'd', 'e', 0
  22. };
  23. /* SHIM variables */
  24. static const efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
  25. static efi_char16_t const shim_MokSBState_name[] = {
  26. 'M', 'o', 'k', 'S', 'B', 'S', 't', 'a', 't', 'e', 0
  27. };
  28. #define get_efi_var(name, vendor, ...) \
  29. efi_call_runtime(get_variable, \
  30. (efi_char16_t *)(name), (efi_guid_t *)(vendor), \
  31. __VA_ARGS__);
  32. /*
  33. * Determine whether we're in secure boot mode.
  34. */
  35. enum efi_secureboot_mode efi_get_secureboot(efi_system_table_t *sys_table_arg)
  36. {
  37. u32 attr;
  38. u8 secboot, setupmode, moksbstate;
  39. unsigned long size;
  40. efi_status_t status;
  41. size = sizeof(secboot);
  42. status = get_efi_var(efi_SecureBoot_name, &efi_variable_guid,
  43. NULL, &size, &secboot);
  44. if (status == EFI_NOT_FOUND)
  45. return efi_secureboot_mode_disabled;
  46. if (status != EFI_SUCCESS)
  47. goto out_efi_err;
  48. size = sizeof(setupmode);
  49. status = get_efi_var(efi_SetupMode_name, &efi_variable_guid,
  50. NULL, &size, &setupmode);
  51. if (status != EFI_SUCCESS)
  52. goto out_efi_err;
  53. if (secboot == 0 || setupmode == 1)
  54. return efi_secureboot_mode_disabled;
  55. /*
  56. * See if a user has put the shim into insecure mode. If so, and if the
  57. * variable doesn't have the runtime attribute set, we might as well
  58. * honor that.
  59. */
  60. size = sizeof(moksbstate);
  61. status = get_efi_var(shim_MokSBState_name, &shim_guid,
  62. &attr, &size, &moksbstate);
  63. /* If it fails, we don't care why. Default to secure */
  64. if (status != EFI_SUCCESS)
  65. goto secure_boot_enabled;
  66. if (!(attr & EFI_VARIABLE_RUNTIME_ACCESS) && moksbstate == 1)
  67. return efi_secureboot_mode_disabled;
  68. secure_boot_enabled:
  69. pr_efi(sys_table_arg, "UEFI Secure Boot is enabled.\n");
  70. return efi_secureboot_mode_enabled;
  71. out_efi_err:
  72. pr_efi_err(sys_table_arg, "Could not determine UEFI Secure Boot status.\n");
  73. return efi_secureboot_mode_unknown;
  74. }