secureboot.c 2.4 KB

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