secureboot.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Secure boot handling.
  4. *
  5. * Copyright (C) 2013,2014 Linaro Limited
  6. * Roy Franz <[email protected]
  7. * Copyright (C) 2013 Red Hat, Inc.
  8. * Mark Salter <[email protected]>
  9. */
  10. #include <linux/efi.h>
  11. #include <asm/efi.h>
  12. #include "efistub.h"
  13. /* SHIM variables */
  14. static const efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
  15. static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";
  16. static efi_status_t get_var(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
  17. unsigned long *data_size, void *data)
  18. {
  19. return get_efi_var(name, vendor, attr, data_size, data);
  20. }
  21. /*
  22. * Determine whether we're in secure boot mode.
  23. */
  24. enum efi_secureboot_mode efi_get_secureboot(void)
  25. {
  26. u32 attr;
  27. unsigned long size;
  28. enum efi_secureboot_mode mode;
  29. efi_status_t status;
  30. u8 moksbstate;
  31. mode = efi_get_secureboot_mode(get_var);
  32. if (mode == efi_secureboot_mode_unknown) {
  33. efi_err("Could not determine UEFI Secure Boot status.\n");
  34. return efi_secureboot_mode_unknown;
  35. }
  36. if (mode != efi_secureboot_mode_enabled)
  37. return mode;
  38. /*
  39. * See if a user has put the shim into insecure mode. If so, and if the
  40. * variable doesn't have the non-volatile attribute set, we might as
  41. * well honor that.
  42. */
  43. size = sizeof(moksbstate);
  44. status = get_efi_var(shim_MokSBState_name, &shim_guid,
  45. &attr, &size, &moksbstate);
  46. /* If it fails, we don't care why. Default to secure */
  47. if (status != EFI_SUCCESS)
  48. goto secure_boot_enabled;
  49. if (!(attr & EFI_VARIABLE_NON_VOLATILE) && moksbstate == 1)
  50. return efi_secureboot_mode_disabled;
  51. secure_boot_enabled:
  52. efi_info("UEFI Secure Boot is enabled.\n");
  53. return efi_secureboot_mode_enabled;
  54. }