1 | /** @file
|
---|
2 | Capsule library runtime support.
|
---|
3 |
|
---|
4 | Copyright (c) 2016 - 2024, Intel Corporation. All rights reserved.<BR>
|
---|
5 | Copyright (c) 2024, Ampere Computing LLC. All rights reserved.<BR>
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 |
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #include <PiDxe.h>
|
---|
11 |
|
---|
12 | #include <Guid/FmpCapsule.h>
|
---|
13 | #include <Guid/SystemResourceTable.h>
|
---|
14 | #include <Guid/EventGroup.h>
|
---|
15 |
|
---|
16 | #include <Library/BaseLib.h>
|
---|
17 | #include <Library/DebugLib.h>
|
---|
18 | #include <Library/BaseMemoryLib.h>
|
---|
19 | #include <Library/DxeServicesTableLib.h>
|
---|
20 | #include <Library/UefiBootServicesTableLib.h>
|
---|
21 | #include <Library/UefiRuntimeServicesTableLib.h>
|
---|
22 | #include <Library/MemoryAllocationLib.h>
|
---|
23 |
|
---|
24 | extern EFI_SYSTEM_RESOURCE_TABLE *mEsrtTable;
|
---|
25 | extern BOOLEAN mDxeCapsuleLibIsExitBootService;
|
---|
26 | EFI_EVENT mDxeRuntimeCapsuleLibVirtualAddressChangeEvent = NULL;
|
---|
27 | EFI_EVENT mDxeRuntimeCapsuleLibSystemResourceTableEvent = NULL;
|
---|
28 | EFI_EVENT mDxeRuntimeCapsuleLibExitBootServiceEvent = NULL;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | Convert EsrtTable physical address to virtual address.
|
---|
32 |
|
---|
33 | @param[in] Event Event whose notification function is being invoked.
|
---|
34 | @param[in] Context The pointer to the notification function's context, which
|
---|
35 | is implementation-dependent.
|
---|
36 | **/
|
---|
37 | VOID
|
---|
38 | EFIAPI
|
---|
39 | DxeCapsuleLibVirtualAddressChangeEvent (
|
---|
40 | IN EFI_EVENT Event,
|
---|
41 | IN VOID *Context
|
---|
42 | )
|
---|
43 | {
|
---|
44 | gRT->ConvertPointer (EFI_OPTIONAL_PTR, (VOID **)&mEsrtTable);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | Notify function for event of system resource table installation.
|
---|
49 |
|
---|
50 | @param[in] Event The Event that is being processed.
|
---|
51 | @param[in] Context The Event Context.
|
---|
52 |
|
---|
53 | **/
|
---|
54 | STATIC
|
---|
55 | VOID
|
---|
56 | EFIAPI
|
---|
57 | DxeCapsuleLibSystemResourceTableInstallEventNotify (
|
---|
58 | IN EFI_EVENT Event,
|
---|
59 | IN VOID *Context
|
---|
60 | )
|
---|
61 | {
|
---|
62 | UINTN Index;
|
---|
63 | EFI_CONFIGURATION_TABLE *ConfigEntry;
|
---|
64 | EFI_SYSTEM_RESOURCE_TABLE *EsrtTable;
|
---|
65 |
|
---|
66 | //
|
---|
67 | // Get Esrt table first
|
---|
68 | //
|
---|
69 | ConfigEntry = gST->ConfigurationTable;
|
---|
70 | for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {
|
---|
71 | if (CompareGuid (&gEfiSystemResourceTableGuid, &ConfigEntry->VendorGuid)) {
|
---|
72 | break;
|
---|
73 | }
|
---|
74 |
|
---|
75 | ConfigEntry++;
|
---|
76 | }
|
---|
77 |
|
---|
78 | //
|
---|
79 | // If no Esrt table installed in Configure Table
|
---|
80 | //
|
---|
81 | if (Index < gST->NumberOfTableEntries) {
|
---|
82 | //
|
---|
83 | // Free the pool to remove the cached ESRT table.
|
---|
84 | //
|
---|
85 | if (mEsrtTable != NULL) {
|
---|
86 | FreePool ((VOID *)mEsrtTable);
|
---|
87 | mEsrtTable = NULL;
|
---|
88 | }
|
---|
89 |
|
---|
90 | //
|
---|
91 | // Search Esrt to check given capsule is qualified
|
---|
92 | //
|
---|
93 | EsrtTable = (EFI_SYSTEM_RESOURCE_TABLE *)ConfigEntry->VendorTable;
|
---|
94 |
|
---|
95 | mEsrtTable = AllocateRuntimeCopyPool (
|
---|
96 | sizeof (EFI_SYSTEM_RESOURCE_TABLE) +
|
---|
97 | EsrtTable->FwResourceCount * sizeof (EFI_SYSTEM_RESOURCE_ENTRY),
|
---|
98 | EsrtTable
|
---|
99 | );
|
---|
100 | ASSERT (mEsrtTable != NULL);
|
---|
101 |
|
---|
102 | //
|
---|
103 | // Set FwResourceCountMax to a sane value.
|
---|
104 | //
|
---|
105 | mEsrtTable->FwResourceCountMax = mEsrtTable->FwResourceCount;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | Notify function for event of exit boot service.
|
---|
111 |
|
---|
112 | @param[in] Event The Event that is being processed.
|
---|
113 | @param[in] Context The Event Context.
|
---|
114 |
|
---|
115 | **/
|
---|
116 | STATIC
|
---|
117 | VOID
|
---|
118 | EFIAPI
|
---|
119 | DxeCapsuleLibExitBootServiceEventNotify (
|
---|
120 | IN EFI_EVENT Event,
|
---|
121 | IN VOID *Context
|
---|
122 | )
|
---|
123 | {
|
---|
124 | mDxeCapsuleLibIsExitBootService = TRUE;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | The constructor function for the file of DxeCapsuleRuntime.
|
---|
129 |
|
---|
130 | @param ImageHandle The firmware allocated handle for the EFI image.
|
---|
131 | @param SystemTable A pointer to the EFI System Table.
|
---|
132 |
|
---|
133 | @retval EFI_SUCCESS The constructor successfully .
|
---|
134 | **/
|
---|
135 | EFI_STATUS
|
---|
136 | EFIAPI
|
---|
137 | DxeRuntimeCapsuleLibConstructor (
|
---|
138 | IN EFI_HANDLE ImageHandle,
|
---|
139 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
140 | )
|
---|
141 | {
|
---|
142 | EFI_STATUS Status;
|
---|
143 |
|
---|
144 | //
|
---|
145 | // Make sure we can handle virtual address changes.
|
---|
146 | //
|
---|
147 | Status = gBS->CreateEventEx (
|
---|
148 | EVT_NOTIFY_SIGNAL,
|
---|
149 | TPL_NOTIFY,
|
---|
150 | DxeCapsuleLibVirtualAddressChangeEvent,
|
---|
151 | NULL,
|
---|
152 | &gEfiEventVirtualAddressChangeGuid,
|
---|
153 | &mDxeRuntimeCapsuleLibVirtualAddressChangeEvent
|
---|
154 | );
|
---|
155 | ASSERT_EFI_ERROR (Status);
|
---|
156 |
|
---|
157 | //
|
---|
158 | // Register notify function to cache the FMP capsule GUIDs when system resource table installed.
|
---|
159 | //
|
---|
160 | Status = gBS->CreateEventEx (
|
---|
161 | EVT_NOTIFY_SIGNAL,
|
---|
162 | TPL_CALLBACK,
|
---|
163 | DxeCapsuleLibSystemResourceTableInstallEventNotify,
|
---|
164 | NULL,
|
---|
165 | &gEfiSystemResourceTableGuid,
|
---|
166 | &mDxeRuntimeCapsuleLibSystemResourceTableEvent
|
---|
167 | );
|
---|
168 | ASSERT_EFI_ERROR (Status);
|
---|
169 |
|
---|
170 | //
|
---|
171 | // Register notify function to indicate the event is signaled at ExitBootService.
|
---|
172 | //
|
---|
173 | Status = gBS->CreateEventEx (
|
---|
174 | EVT_NOTIFY_SIGNAL,
|
---|
175 | TPL_CALLBACK,
|
---|
176 | DxeCapsuleLibExitBootServiceEventNotify,
|
---|
177 | NULL,
|
---|
178 | &gEfiEventExitBootServicesGuid,
|
---|
179 | &mDxeRuntimeCapsuleLibExitBootServiceEvent
|
---|
180 | );
|
---|
181 | ASSERT_EFI_ERROR (Status);
|
---|
182 |
|
---|
183 | return EFI_SUCCESS;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | The destructor function for the file of DxeCapsuleRuntime.
|
---|
188 |
|
---|
189 | @param ImageHandle The firmware allocated handle for the EFI image.
|
---|
190 | @param SystemTable A pointer to the EFI System Table.
|
---|
191 |
|
---|
192 | @retval EFI_SUCCESS The destructor completed successfully.
|
---|
193 | **/
|
---|
194 | EFI_STATUS
|
---|
195 | EFIAPI
|
---|
196 | DxeRuntimeCapsuleLibDestructor (
|
---|
197 | IN EFI_HANDLE ImageHandle,
|
---|
198 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
199 | )
|
---|
200 | {
|
---|
201 | EFI_STATUS Status;
|
---|
202 |
|
---|
203 | //
|
---|
204 | // Close the VirtualAddressChange event.
|
---|
205 | //
|
---|
206 | Status = gBS->CloseEvent (mDxeRuntimeCapsuleLibVirtualAddressChangeEvent);
|
---|
207 | ASSERT_EFI_ERROR (Status);
|
---|
208 |
|
---|
209 | //
|
---|
210 | // Close the system resource table installed event.
|
---|
211 | //
|
---|
212 | Status = gBS->CloseEvent (mDxeRuntimeCapsuleLibSystemResourceTableEvent);
|
---|
213 | ASSERT_EFI_ERROR (Status);
|
---|
214 |
|
---|
215 | //
|
---|
216 | // Close the ExitBootService event.
|
---|
217 | //
|
---|
218 | Status = gBS->CloseEvent (mDxeRuntimeCapsuleLibExitBootServiceEvent);
|
---|
219 | ASSERT_EFI_ERROR (Status);
|
---|
220 |
|
---|
221 | return EFI_SUCCESS;
|
---|
222 | }
|
---|