secureboot.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. enum efi_secureboot_mode efi_get_secureboot(efi_system_table_t *sys_table_arg)
  30. {
  31. u32 attr;
  32. u8 secboot, setupmode, moksbstate;
  33. unsigned long size;
  34. efi_status_t status;
  35. size = sizeof(secboot);
  36. status = get_efi_var(efi_SecureBoot_name, &efi_variable_guid,
  37. NULL, &size, &secboot);
  38. if (status == EFI_NOT_FOUND)
  39. return efi_secureboot_mode_disabled;
  40. if (status != EFI_SUCCESS)
  41. goto out_efi_err;
  42. size = sizeof(setupmode);
  43. status = get_efi_var(efi_SetupMode_name, &efi_variable_guid,
  44. NULL, &size, &setupmode);
  45. if (status != EFI_SUCCESS)
  46. goto out_efi_err;
  47. if (secboot == 0 || setupmode == 1)
  48. return efi_secureboot_mode_disabled;
  49. /*
  50. * See if a user has put the shim into insecure mode. If so, and if the
  51. * variable doesn't have the runtime attribute set, we might as well
  52. * honor that.
  53. */
  54. size = sizeof(moksbstate);
  55. status = get_efi_var(shim_MokSBState_name, &shim_guid,
  56. &attr, &size, &moksbstate);
  57. /* If it fails, we don't care why. Default to secure */
  58. if (status != EFI_SUCCESS)
  59. goto secure_boot_enabled;
  60. if (!(attr & EFI_VARIABLE_RUNTIME_ACCESS) && moksbstate == 1)
  61. return efi_secureboot_mode_disabled;
  62. secure_boot_enabled:
  63. pr_efi(sys_table_arg, "UEFI Secure Boot is enabled.\n");
  64. return efi_secureboot_mode_enabled;
  65. out_efi_err:
  66. pr_efi_err(sys_table_arg, "Could not determine UEFI Secure Boot status.\n");
  67. return efi_secureboot_mode_unknown;
  68. }