Add EBC, FTW, Crc32SectionExtract, NullMemoryTest modules.
[mirror_edk2.git] / MdeModulePkg / Universal / Capsule / RuntimeDxe / CapsuleService.c
1 /*++
2
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 CapsuleService.c
15
16 Abstract:
17
18 Capsule Runtime Service.
19
20 --*/
21
22 #include "CapsuleService.h"
23
24 EFI_STATUS
25 EFIAPI
26 UpdateCapsule (
27 IN UEFI_CAPSULE_HEADER **CapsuleHeaderArray,
28 IN UINTN CapsuleCount,
29 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL
30 )
31 /*++
32
33 Routine Description:
34
35 This code finds whether the capsules need reset to update, if not, update immediately.
36
37 Arguments:
38
39 CapsuleHeaderArray A array of pointers to capsule headers passed in
40 CapsuleCount The number of capsule
41 ScatterGatherList Physical address of datablock list points to capsule
42
43 Returns:
44
45 EFI STATUS
46 EFI_SUCCESS Valid capsule was passed.If CAPSULE_FLAG_PERSIT_ACROSS_RESET is
47 not set, the capsule has been successfully processed by the firmware.
48 If it set, the ScattlerGatherList is successfully to be set.
49 EFI_INVALID_PARAMETER CapsuleCount is less than 1,CapsuleGuid is not supported.
50 EFI_DEVICE_ERROR Failed to SetVariable or AllocatePool or ProcessFirmwareVolume.
51
52 --*/
53 {
54 UINTN CapsuleSize;
55 UINTN ArrayNumber;
56 VOID *BufferPtr;
57 EFI_STATUS Status;
58 EFI_HANDLE FvHandle;
59 UEFI_CAPSULE_HEADER *CapsuleHeader;
60
61 if (CapsuleCount < 1) {
62 return EFI_INVALID_PARAMETER;
63 }
64
65 BufferPtr = NULL;
66 CapsuleHeader = NULL;
67
68 //
69 //Compare GUIDs with EFI_CAPSULE_GUID, if capsule header contains CAPSULE_FLAGS_PERSIST_ACROSS_RESET
70 //and CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flags,whatever the GUID is ,the service supports.
71 //
72 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
73 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
74 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
75 return EFI_INVALID_PARAMETER;
76 }
77 if (!CompareGuid (&CapsuleHeader->CapsuleGuid, &gEfiCapsuleGuid)) {
78 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {
79 return EFI_UNSUPPORTED;
80 }
81 }
82 }
83
84 //
85 //Assume that capsules have the same flags on reseting or not.
86 //
87 CapsuleHeader = CapsuleHeaderArray[0];
88
89 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
90 //
91 //Check if the platform supports update capsule across a system reset
92 //
93 if (!FeaturePcdGet(PcdSupportUpdateCapsuleRest)) {
94 return EFI_UNSUPPORTED;
95 }
96
97 if (ScatterGatherList == 0) {
98 return EFI_INVALID_PARAMETER;
99 } else {
100 Status = EfiSetVariable (
101 EFI_CAPSULE_VARIABLE_NAME,
102 &gEfiCapsuleVendorGuid,
103 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
104 sizeof (UINTN),
105 (VOID *) &ScatterGatherList
106 );
107 if (Status != EFI_SUCCESS) {
108 return EFI_DEVICE_ERROR;
109 }
110 }
111 return EFI_SUCCESS;
112 }
113
114 //
115 //The rest occurs in the condition of non-reset mode
116 //
117 if (EfiAtRuntime ()) {
118 return EFI_INVALID_PARAMETER;
119 }
120
121 //
122 //Here should be in the boot-time
123 //
124 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {
125 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
126 CapsuleSize = CapsuleHeader->CapsuleImageSize - CapsuleHeader->HeaderSize;
127
128 BufferPtr = AllocatePool (CapsuleSize);
129 if (BufferPtr == NULL) {
130 return EFI_DEVICE_ERROR;
131 }
132
133 CopyMem (BufferPtr, (UINT8*)CapsuleHeader+ CapsuleHeader->HeaderSize, CapsuleSize);
134
135 //
136 //Call DXE service ProcessFirmwareVolume to process immediatelly
137 //
138 Status = gDS->ProcessFirmwareVolume (BufferPtr, CapsuleSize, &FvHandle);
139 if (Status != EFI_SUCCESS) {
140 FreePool (BufferPtr);
141 return EFI_DEVICE_ERROR;
142 }
143 gDS->Dispatch ();
144 FreePool (BufferPtr);
145 }
146
147 return EFI_SUCCESS;
148 }
149
150
151
152 EFI_STATUS
153 EFIAPI
154 QueryCapsuleCapabilities (
155 IN UEFI_CAPSULE_HEADER **CapsuleHeaderArray,
156 IN UINTN CapsuleCount,
157 OUT UINT64 *MaxiumCapsuleSize,
158 OUT EFI_RESET_TYPE *ResetType
159 )
160 /*++
161
162 Routine Description:
163
164 This code is to query about capsule capability.
165
166 Arguments:
167
168 CapsuleHeaderArray A array of pointers to capsule headers passed in
169 CapsuleCount The number of capsule
170 MaxiumCapsuleSize Max capsule size is supported
171 ResetType Reset type the capsule indicates, if reset is not needed,return EfiResetCold.
172 If reset is needed, return EfiResetWarm.
173
174 Returns:
175
176 EFI STATUS
177 EFI_SUCCESS Valid answer returned
178 EFI_INVALID_PARAMETER MaxiumCapsuleSize is NULL,ResetType is NULL.CapsuleCount is less than 1,CapsuleGuid is not supported.
179 EFI_UNSUPPORTED The capsule type is not supported.
180
181 --*/
182 {
183 UINTN ArrayNumber;
184 UEFI_CAPSULE_HEADER *CapsuleHeader;
185
186 if (CapsuleCount < 1) {
187 return EFI_INVALID_PARAMETER;
188 }
189
190 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {
191 return EFI_INVALID_PARAMETER;
192 }
193
194 CapsuleHeader = NULL;
195
196 //
197 //Compare GUIDs with EFI_CAPSULE_GUID, if capsule header contains CAPSULE_FLAGS_PERSIST_ACROSS_RESET
198 //and CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flags,whatever the GUID is ,the service supports.
199 //
200 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
201 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
202 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
203 return EFI_INVALID_PARAMETER;
204 }
205 if (!CompareGuid (&CapsuleHeader->CapsuleGuid, &gEfiCapsuleGuid)) {
206 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {
207 return EFI_UNSUPPORTED;
208 }
209 }
210 }
211
212 //
213 //Assume that capsules have the same flags on reseting or not.
214 //
215 CapsuleHeader = CapsuleHeaderArray[0];
216 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
217 //
218 //Check if the platform supports update capsule across a system reset
219 //
220 if (!FeaturePcdGet(PcdSupportUpdateCapsuleRest)) {
221 return EFI_UNSUPPORTED;
222 }
223 *ResetType = EfiResetWarm;
224 *MaxiumCapsuleSize = FixedPcdGet32(PcdMaxSizePopulateCapsule);
225 } else {
226 *ResetType = EfiResetCold;
227 *MaxiumCapsuleSize = FixedPcdGet32(PcdMaxSizeNonPopulateCapsule);
228 }
229 return EFI_SUCCESS;
230 }
231