secureboot.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <dragonstub/dragonstub.h>
  2. /* SHIM variables */
  3. static const efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
  4. static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";
  5. static efi_status_t get_var(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
  6. unsigned long *data_size, void *data)
  7. {
  8. return get_efi_var(name, vendor, attr, data_size, data);
  9. }
  10. /*
  11. * Determine whether we're in secure boot mode.
  12. */
  13. enum efi_secureboot_mode efi_get_secureboot(void)
  14. {
  15. u32 attr;
  16. unsigned long size;
  17. enum efi_secureboot_mode mode;
  18. efi_status_t status;
  19. u8 moksbstate;
  20. mode = efi_get_secureboot_mode(get_var);
  21. if (mode == efi_secureboot_mode_unknown) {
  22. efi_err("Could not determine UEFI Secure Boot status.\n");
  23. return efi_secureboot_mode_unknown;
  24. }
  25. if (mode != efi_secureboot_mode_enabled)
  26. return mode;
  27. /*
  28. * See if a user has put the shim into insecure mode. If so, and if the
  29. * variable doesn't have the non-volatile attribute set, we might as
  30. * well honor that.
  31. */
  32. size = sizeof(moksbstate);
  33. status = get_efi_var(shim_MokSBState_name, &shim_guid, &attr, &size,
  34. &moksbstate);
  35. /* If it fails, we don't care why. Default to secure */
  36. if (status != EFI_SUCCESS)
  37. goto secure_boot_enabled;
  38. if (!(attr & EFI_VARIABLE_NON_VOLATILE) && moksbstate == 1)
  39. return efi_secureboot_mode_disabled;
  40. secure_boot_enabled:
  41. efi_info("UEFI Secure Boot is enabled.\n");
  42. return efi_secureboot_mode_enabled;
  43. }
  44. /// @brief 打印efi_secureboot_mode
  45. void print_efi_secureboot_mode(enum efi_secureboot_mode mode)
  46. {
  47. switch (mode) {
  48. case efi_secureboot_mode_unknown:
  49. efi_info("efi_secureboot_mode: efi_secureboot_mode_unknown\n");
  50. break;
  51. case efi_secureboot_mode_disabled:
  52. efi_info("efi_secureboot_mode: efi_secureboot_mode_disabled\n");
  53. break;
  54. case efi_secureboot_mode_enabled:
  55. efi_info("efi_secureboot_mode: efi_secureboot_mode_enabled\n");
  56. break;
  57. case efi_secureboot_mode_unset:
  58. efi_info("efi_secureboot_mode: efi_secureboot_mode_unset\n");
  59. break;
  60. default:
  61. break;
  62. }
  63. }