1 | /** @file
|
---|
2 | Enroll default PK, KEK, db, dbx.
|
---|
3 |
|
---|
4 | Copyright (c) 2021, ARM Ltd. All rights reserved.<BR>
|
---|
5 | Copyright (c) 2021, Semihalf All rights reserved.<BR>
|
---|
6 |
|
---|
7 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #include <Guid/AuthenticatedVariableFormat.h> // gEfiCustomModeEnableGuid
|
---|
11 | #include <Guid/GlobalVariable.h> // EFI_SETUP_MODE_NAME
|
---|
12 | #include <Guid/ImageAuthentication.h> // EFI_IMAGE_SECURITY_DATABASE
|
---|
13 | #include <Library/BaseLib.h> // GUID_STRING_LENGTH
|
---|
14 | #include <Library/BaseMemoryLib.h> // CopyGuid()
|
---|
15 | #include <Library/DebugLib.h> // ASSERT()
|
---|
16 | #include <Library/MemoryAllocationLib.h> // FreePool()
|
---|
17 | #include <Library/PrintLib.h> // AsciiSPrint()
|
---|
18 | #include <Library/UefiBootServicesTableLib.h> // gBS
|
---|
19 | #include <Library/UefiLib.h> // AsciiPrint()
|
---|
20 | #include <Library/UefiRuntimeServicesTableLib.h> // gRT
|
---|
21 | #include <Uefi/UefiMultiPhase.h>
|
---|
22 | #include <UefiSecureBoot.h>
|
---|
23 | #include <Library/SecureBootVariableLib.h>
|
---|
24 | #include <Library/SecureBootVariableProvisionLib.h>
|
---|
25 |
|
---|
26 | /**
|
---|
27 | Entry point function of this shell application.
|
---|
28 | @param[in] ImageHandle The firmware allocated handle for the EFI image.
|
---|
29 | @param[in] SystemTable A pointer to the EFI System Table.
|
---|
30 |
|
---|
31 | @retval 0 The entry point is executed successfully.
|
---|
32 | @retval other Some error occurs when executing this entry point.
|
---|
33 | **/
|
---|
34 | EFI_STATUS
|
---|
35 | EFIAPI
|
---|
36 | UefiMain (
|
---|
37 | IN EFI_HANDLE ImageHandle,
|
---|
38 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
39 | )
|
---|
40 | {
|
---|
41 | EFI_STATUS Status;
|
---|
42 | UINT8 SetupMode;
|
---|
43 |
|
---|
44 | Status = GetSetupMode (&SetupMode);
|
---|
45 | if (EFI_ERROR (Status)) {
|
---|
46 | AsciiPrint ("EnrollFromDefaultKeysApp: Cannot get SetupMode variable: %r\n", Status);
|
---|
47 | return 1;
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (SetupMode == USER_MODE) {
|
---|
51 | AsciiPrint ("EnrollFromDefaultKeysApp: Skipped - USER_MODE\n");
|
---|
52 | return 1;
|
---|
53 | }
|
---|
54 |
|
---|
55 | Status = SetSecureBootMode (CUSTOM_SECURE_BOOT_MODE);
|
---|
56 | if (EFI_ERROR (Status)) {
|
---|
57 | AsciiPrint ("EnrollFromDefaultKeysApp: Cannot set CUSTOM_SECURE_BOOT_MODE: %r\n", Status);
|
---|
58 | return 1;
|
---|
59 | }
|
---|
60 |
|
---|
61 | Status = EnrollDbFromDefault ();
|
---|
62 | if (EFI_ERROR (Status)) {
|
---|
63 | AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll db: %r\n", Status);
|
---|
64 | goto error;
|
---|
65 | }
|
---|
66 |
|
---|
67 | Status = EnrollDbxFromDefault ();
|
---|
68 | if (EFI_ERROR (Status)) {
|
---|
69 | AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll dbt: %r\n", Status);
|
---|
70 | }
|
---|
71 |
|
---|
72 | Status = EnrollDbtFromDefault ();
|
---|
73 | if (EFI_ERROR (Status)) {
|
---|
74 | AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll dbx: %r\n", Status);
|
---|
75 | }
|
---|
76 |
|
---|
77 | Status = EnrollKEKFromDefault ();
|
---|
78 | if (EFI_ERROR (Status)) {
|
---|
79 | AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll KEK: %r\n", Status);
|
---|
80 | goto cleardbs;
|
---|
81 | }
|
---|
82 |
|
---|
83 | Status = EnrollPKFromDefault ();
|
---|
84 | if (EFI_ERROR (Status)) {
|
---|
85 | AsciiPrint ("EnrollFromDefaultKeysApp: Cannot enroll PK: %r\n", Status);
|
---|
86 | goto clearKEK;
|
---|
87 | }
|
---|
88 |
|
---|
89 | Status = SetSecureBootMode (STANDARD_SECURE_BOOT_MODE);
|
---|
90 | if (EFI_ERROR (Status)) {
|
---|
91 | AsciiPrint (
|
---|
92 | "EnrollFromDefaultKeysApp: Cannot set CustomMode to STANDARD_SECURE_BOOT_MODE\n"
|
---|
93 | "Please do it manually, otherwise system can be easily compromised\n"
|
---|
94 | );
|
---|
95 | }
|
---|
96 |
|
---|
97 | return 0;
|
---|
98 |
|
---|
99 | clearKEK:
|
---|
100 | DeleteKEK ();
|
---|
101 |
|
---|
102 | cleardbs:
|
---|
103 | DeleteDbt ();
|
---|
104 | DeleteDbx ();
|
---|
105 | DeleteDb ();
|
---|
106 |
|
---|
107 | error:
|
---|
108 | Status = SetSecureBootMode (STANDARD_SECURE_BOOT_MODE);
|
---|
109 | if (EFI_ERROR (Status)) {
|
---|
110 | AsciiPrint (
|
---|
111 | "EnrollFromDefaultKeysApp: Cannot set CustomMode to STANDARD_SECURE_BOOT_MODE\n"
|
---|
112 | "Please do it manually, otherwise system can be easily compromised\n"
|
---|
113 | );
|
---|
114 | }
|
---|
115 |
|
---|
116 | return 1;
|
---|
117 | }
|
---|