]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
1. Fix buffer overflow bugs in SNP, MNP and IP6 driver.
[mirror_edk2.git] / MdeModulePkg / Library / PiDxeS3BootScriptLib / BootScriptSave.c
CommitLineData
64d14edf 1/** @file\r
2 Save the S3 data to S3 boot script. \r
3 \r
ce68d3bc 4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
64d14edf 5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions\r
8 of the BSD License which accompanies this distribution. The\r
9 full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16#include "InternalBootScriptLib.h"\r
17\r
18/**\r
19\r
20 Data structure usage:\r
21\r
22 +------------------------------+<-- PcdS3BootScriptTablePrivateDataPtr\r
23 | SCRIPT_TABLE_PRIVATE_DATA |\r
24 | TableBase |---\r
25 | TableLength |--|--\r
26 | AtRuntime | | |\r
27 | InSmm | | |\r
28 +------------------------------+ | |\r
29 | |\r
30 +------------------------------+<-- |\r
31 | EFI_BOOT_SCRIPT_TABLE_HEADER | |\r
32 | TableLength |----|--\r
33 +------------------------------+ | |\r
34 | ...... | | |\r
35 +------------------------------+<---- |\r
36 | EFI_BOOT_SCRIPT_TERMINATE | |\r
37 +------------------------------+<------\r
38\r
39**/\r
40\r
41SCRIPT_TABLE_PRIVATE_DATA *mS3BootScriptTablePtr;\r
42EFI_EVENT mEnterRuntimeEvent;\r
43//\r
3aa764ed 44// Allocate SMM copy because we can not use mS3BootScriptTablePtr when we AtRuntime in InSmm.\r
64d14edf 45//\r
3aa764ed 46SCRIPT_TABLE_PRIVATE_DATA *mS3BootScriptTableSmmPtr;\r
64d14edf 47UINTN mLockBoxLength;\r
48\r
49EFI_GUID mBootScriptDataGuid = {\r
ce68d3bc 50 0xaea6b965, 0xdcf5, 0x4311, { 0xb4, 0xb8, 0xf, 0x12, 0x46, 0x44, 0x94, 0xd2 }\r
64d14edf 51};\r
52\r
02f49fc2
SZ
53EFI_GUID mBootScriptDataOrgGuid = {\r
54 0xb5af1d7a, 0xb8cf, 0x4eb3, { 0x89, 0x25, 0xa8, 0x20, 0xe1, 0x6b, 0x68, 0x7d }\r
55};\r
56\r
64d14edf 57EFI_GUID mBootScriptHeaderDataGuid = {\r
ce68d3bc 58 0x1810ab4a, 0x2314, 0x4df6, { 0x81, 0xeb, 0x67, 0xc6, 0xec, 0x5, 0x85, 0x91 }\r
64d14edf 59};\r
60\r
61/**\r
62 This is an internal function to add a terminate node the entry, recalculate the table \r
63 length and fill into the table. \r
64 \r
65 @return the base address of the boot script tble. \r
66 **/\r
67UINT8*\r
68S3BootScriptInternalCloseTable (\r
69 VOID\r
70 )\r
71{\r
72 UINT8 *S3TableBase;\r
73 EFI_BOOT_SCRIPT_TERMINATE ScriptTerminate;\r
74 EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;\r
75 S3TableBase = mS3BootScriptTablePtr->TableBase;\r
76 \r
77 if (S3TableBase == NULL) {\r
78 //\r
79 // the table is not exist\r
80 //\r
81 return S3TableBase;\r
82 }\r
83 //\r
84 // Append the termination entry.\r
85 //\r
86 ScriptTerminate.OpCode = S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE;\r
87 ScriptTerminate.Length = (UINT8) sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
88 CopyMem (mS3BootScriptTablePtr->TableBase + mS3BootScriptTablePtr->TableLength, &ScriptTerminate, sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
89 //\r
90 // fill the table length\r
91 //\r
92 ScriptTableInfo = (EFI_BOOT_SCRIPT_TABLE_HEADER*)(mS3BootScriptTablePtr->TableBase);\r
93 ScriptTableInfo->TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
94 \r
95 \r
96 \r
97 return S3TableBase;\r
98 //\r
99 // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to \r
100 // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE). Because \r
101 // maybe in runtime, we still need add entries into the table, and the runtime entry should be\r
102 // added start before this TERMINATE node.\r
103 //\r
104} \r
105\r
106/**\r
107 This function save boot script data to LockBox.\r
108 1. BootSriptPrivate data, BootScript data - Image and DispatchContext are handled by platform.\r
109 2. BootScriptExecutor, BootScriptExecutor context\r
110 - ACPI variable - (PI version) sould be handled by SMM driver. S3 Page table is handled here.\r
111 - ACPI variable - framework version is already handled by Framework CPU driver.\r
112**/\r
113VOID\r
114SaveBootScriptDataToLockBox (\r
115 VOID\r
116 )\r
117{\r
f6ec0c77 118 EFI_STATUS Status;\r
f6ec0c77 119\r
64d14edf 120 //\r
121 // mS3BootScriptTablePtr->TableLength does not include EFI_BOOT_SCRIPT_TERMINATE, because we need add entry at runtime.\r
122 // Save all info here, just in case that no one will add boot script entry in SMM.\r
123 //\r
124 Status = SaveLockBox (\r
125 &mBootScriptDataGuid,\r
126 (VOID *)mS3BootScriptTablePtr->TableBase,\r
127 mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE)\r
128 );\r
129 ASSERT_EFI_ERROR (Status);\r
130\r
131 Status = SetLockBoxAttributes (&mBootScriptDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
132 ASSERT_EFI_ERROR (Status);\r
133\r
02f49fc2
SZ
134 //\r
135 // We need duplicate the original copy, because it may have INSERT boot script at runtime in SMM.\r
136 // If so, we should use original copy to restore data after OS rewrites the ACPINvs region.\r
137 // Or the data inserted may cause some original boot script data lost.\r
138 //\r
139 Status = SaveLockBox (\r
140 &mBootScriptDataOrgGuid,\r
141 (VOID *)mS3BootScriptTablePtr->TableBase,\r
142 mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE)\r
143 );\r
144 ASSERT_EFI_ERROR (Status);\r
145\r
64d14edf 146 //\r
147 // Just need save TableBase.\r
148 // Do not update other field because they will NOT be used in S3.\r
149 //\r
150 Status = SaveLockBox (\r
151 &mBootScriptHeaderDataGuid,\r
152 (VOID *)&mS3BootScriptTablePtr->TableBase,\r
153 sizeof(mS3BootScriptTablePtr->TableBase)\r
154 );\r
155 ASSERT_EFI_ERROR (Status);\r
156\r
157 Status = SetLockBoxAttributes (&mBootScriptHeaderDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
158 ASSERT_EFI_ERROR (Status);\r
159}\r
160\r
161/**\r
162 This is the Event call back function to notify the Library the system is entering\r
163 run time phase.\r
164 \r
165 @param Event Pointer to this event\r
166 @param Context Event hanlder private data \r
167 **/\r
168VOID\r
169EFIAPI\r
170S3BootScriptEventCallBack (\r
171 IN EFI_EVENT Event,\r
172 IN VOID *Context\r
173 )\r
174{\r
175 EFI_STATUS Status;\r
176 VOID *Interface;\r
177\r
178 //\r
179 // Try to locate it because EfiCreateProtocolNotifyEvent will trigger it once when registration.\r
180 // Just return if it is not found.\r
181 //\r
182 Status = gBS->LocateProtocol (\r
183 &gEfiDxeSmmReadyToLockProtocolGuid,\r
184 NULL,\r
185 &Interface\r
186 );\r
187 if (EFI_ERROR (Status)) {\r
188 return ;\r
189 }\r
190\r
191 //\r
192 // Here we should tell the library that we are enter into runtime phase. and \r
193 // the memory page number occupied by the table should not grow anymore.\r
194 //\r
195 if (!mS3BootScriptTablePtr->AtRuntime) {\r
196 //\r
197 // In boot time, we need not write the terminate node when adding a node to boot scipt table\r
198 // or else, that will impact the performance. However, in runtime, we should append terminate\r
199 // node on every add to boot script table\r
200 //\r
201 S3BootScriptInternalCloseTable ();\r
202 mS3BootScriptTablePtr->AtRuntime = TRUE;\r
203\r
204 //\r
205 // Save BootScript data to lockbox\r
206 //\r
207 SaveBootScriptDataToLockBox ();\r
208 }\r
209} \r
210/**\r
211 This is the Event call back function is triggered in SMM to notify the Library the system is entering\r
212 run time phase and set InSmm flag.\r
213 \r
214 @param Protocol Points to the protocol's unique identifier\r
215 @param Interface Points to the interface instance\r
216 @param Handle The handle on which the interface was installed\r
217\r
218 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
219 **/\r
220EFI_STATUS\r
221EFIAPI\r
222S3BootScriptSmmEventCallBack (\r
223 IN CONST EFI_GUID *Protocol,\r
224 IN VOID *Interface,\r
225 IN EFI_HANDLE Handle\r
226 )\r
227{\r
228 //\r
229 // Check if it is already done\r
230 //\r
3aa764ed 231 if (mS3BootScriptTablePtr == mS3BootScriptTableSmmPtr) {\r
64d14edf 232 return EFI_SUCCESS;\r
233 }\r
234\r
235 //\r
236 // Last chance to call-out, just make sure AtRuntime is set\r
237 //\r
238 S3BootScriptEventCallBack (NULL, NULL);\r
239\r
240 //\r
3aa764ed 241 // Save a SMM copy. If TableBase is NOT null, it means SMM copy has been ready, skip copy mem.\r
64d14edf 242 //\r
3aa764ed
SZ
243 if (mS3BootScriptTableSmmPtr->TableBase == NULL) {\r
244 CopyMem (mS3BootScriptTableSmmPtr, mS3BootScriptTablePtr, sizeof(*mS3BootScriptTablePtr));\r
245 }\r
64d14edf 246 //\r
247 // We should not use ACPINvs copy, because it is not safe.\r
248 //\r
3aa764ed 249 mS3BootScriptTablePtr = mS3BootScriptTableSmmPtr;\r
64d14edf 250\r
251 //\r
252 // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.\r
253 // InSmm will only be checked if AtRuntime is TRUE.\r
254 //\r
255 mS3BootScriptTablePtr->InSmm = TRUE;\r
256\r
257 //\r
258 // Record LockBoxLength\r
259 //\r
3aa764ed 260 mLockBoxLength = mS3BootScriptTableSmmPtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE);\r
64d14edf 261\r
262 return EFI_SUCCESS;\r
263}\r
264\r
265/**\r
266 Library Constructor.\r
267 this function just identify it is a smm driver or non-smm driver linked against \r
268 with the library \r
269\r
270 @param ImageHandle The firmware allocated handle for the EFI image.\r
271 @param SystemTable A pointer to the EFI System Table.\r
272\r
273 @retval RETURN_SUCCESS Allocate the global memory space to store S3 boot script table private data\r
274 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.\r
275**/\r
276RETURN_STATUS\r
277EFIAPI\r
278S3BootScriptLibInitialize (\r
279 IN EFI_HANDLE ImageHandle,\r
280 IN EFI_SYSTEM_TABLE *SystemTable\r
281 )\r
282{\r
283 EFI_STATUS Status;\r
284 SCRIPT_TABLE_PRIVATE_DATA *S3TablePtr;\r
3aa764ed 285 SCRIPT_TABLE_PRIVATE_DATA *S3TableSmmPtr;\r
64d14edf 286 VOID *Registration;\r
287 EFI_SMM_BASE2_PROTOCOL *SmmBase2;\r
288 BOOLEAN InSmm;\r
289 EFI_SMM_SYSTEM_TABLE2 *Smst;\r
290 EFI_PHYSICAL_ADDRESS Buffer;\r
291\r
292 S3TablePtr = (SCRIPT_TABLE_PRIVATE_DATA*)(UINTN)PcdGet64(PcdS3BootScriptTablePrivateDataPtr);\r
293 //\r
294 // The Boot script private data is not be initialized. create it\r
295 //\r
296 if (S3TablePtr == 0) {\r
297 Buffer = SIZE_4GB - 1;\r
298 Status = gBS->AllocatePages (\r
299 AllocateMaxAddress,\r
300 EfiACPIMemoryNVS,\r
301 EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA)),\r
302 &Buffer\r
303 );\r
304 if (EFI_ERROR (Status)) {\r
305 return RETURN_OUT_OF_RESOURCES;\r
306 }\r
307 S3TablePtr = (VOID *) (UINTN) Buffer;\r
308\r
309 PcdSet64 (PcdS3BootScriptTablePrivateDataPtr, (UINT64) (UINTN)S3TablePtr); \r
310 ZeroMem (S3TablePtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA)); \r
311 //\r
312 // create event to notify the library system enter the runtime phase\r
313 //\r
314 mEnterRuntimeEvent = EfiCreateProtocolNotifyEvent (\r
315 &gEfiDxeSmmReadyToLockProtocolGuid,\r
316 TPL_CALLBACK,\r
317 S3BootScriptEventCallBack,\r
318 NULL,\r
319 &Registration\r
320 );\r
321 ASSERT (mEnterRuntimeEvent != NULL);\r
322 } \r
323 mS3BootScriptTablePtr = S3TablePtr;\r
324\r
325 //\r
326 // Get InSmm, we need to register SmmReadyToLock if this library is linked to SMM driver.\r
327 //\r
328 Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID**) &SmmBase2);\r
329 if (EFI_ERROR (Status)) {\r
330 return RETURN_SUCCESS;\r
331 }\r
332 Status = SmmBase2->InSmm (SmmBase2, &InSmm);\r
333 if (EFI_ERROR (Status)) {\r
334 return RETURN_SUCCESS;\r
335 }\r
336 if (!InSmm) {\r
337 return RETURN_SUCCESS;\r
338 }\r
339 //\r
340 // Good, we are in SMM\r
341 //\r
342 Status = SmmBase2->GetSmstLocation (SmmBase2, &Smst);\r
343 if (EFI_ERROR (Status)) {\r
344 return RETURN_SUCCESS;\r
345 }\r
346\r
3aa764ed
SZ
347 S3TableSmmPtr = (SCRIPT_TABLE_PRIVATE_DATA*)(UINTN)PcdGet64(PcdS3BootScriptTablePrivateSmmDataPtr);\r
348 //\r
349 // The Boot script private data in SMM is not be initialized. create it\r
350 //\r
351 if (S3TableSmmPtr == 0) {\r
352 Status = Smst->SmmAllocatePool (\r
353 EfiRuntimeServicesData,\r
354 sizeof(SCRIPT_TABLE_PRIVATE_DATA),\r
355 (VOID **) &S3TableSmmPtr\r
356 );\r
357 if (EFI_ERROR (Status)) {\r
358 return RETURN_OUT_OF_RESOURCES;\r
359 }\r
360\r
361 PcdSet64 (PcdS3BootScriptTablePrivateSmmDataPtr, (UINT64) (UINTN)S3TableSmmPtr);\r
362 ZeroMem (S3TableSmmPtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));\r
363 }\r
364 mS3BootScriptTableSmmPtr = S3TableSmmPtr;\r
365\r
64d14edf 366 //\r
367 // Then register event after lock\r
368 //\r
369 Registration = NULL;\r
370 Status = Smst->SmmRegisterProtocolNotify (\r
371 &gEfiSmmReadyToLockProtocolGuid,\r
372 S3BootScriptSmmEventCallBack,\r
373 &Registration\r
374 );\r
375 ASSERT_EFI_ERROR (Status);\r
376\r
377 return RETURN_SUCCESS;\r
378}\r
379/**\r
380 To get the start address from which a new boot time s3 boot script entry will write into.\r
381 If the table is not exist, the functio will first allocate a buffer for the table\r
382 If the table buffer is not enough for the new entry, in non-smm mode, the funtion will \r
383 invoke reallocate to enlarge buffer.\r
384 \r
385 @param EntryLength the new entry length.\r
386 \r
387 @retval the address from which the a new s3 boot script entry will write into \r
388 **/\r
389UINT8*\r
390S3BootScriptGetBootTimeEntryAddAddress (\r
391 UINT8 EntryLength\r
392 )\r
393{\r
394 EFI_PHYSICAL_ADDRESS S3TableBase;\r
395 EFI_PHYSICAL_ADDRESS NewS3TableBase;\r
396 UINT8 *NewEntryPtr;\r
397 UINT32 TableLength;\r
398 UINT16 PageNumber;\r
399 EFI_STATUS Status;\r
400 EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;\r
401 \r
402 S3TableBase = (EFI_PHYSICAL_ADDRESS)(UINTN)(mS3BootScriptTablePtr->TableBase);\r
403 if (S3TableBase == 0) {\r
404 // The table is not exist. This is the first to add entry. \r
405 // Allocate ACPI script table space under 4G memory. We need it to save\r
406 // some settings done by CSM, which runs after normal script table closed\r
407 //\r
408 S3TableBase = 0xffffffff;\r
409 Status = gBS->AllocatePages (\r
410 AllocateMaxAddress,\r
411 EfiACPIMemoryNVS,\r
412 2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),\r
413 (EFI_PHYSICAL_ADDRESS*)&S3TableBase\r
414 );\r
415 \r
416 if (EFI_ERROR(Status)) {\r
417 ASSERT_EFI_ERROR (Status);\r
418 return 0;\r
419 }\r
420 //\r
421 // Fill Table Header\r
422 //\r
423 ScriptTableInfo = (EFI_BOOT_SCRIPT_TABLE_HEADER*)(UINTN)S3TableBase;\r
424 ScriptTableInfo->OpCode = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;\r
425 ScriptTableInfo->Length = (UINT8) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
426 ScriptTableInfo->TableLength = 0; // will be calculate at CloseTable\r
427 mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
428 mS3BootScriptTablePtr->TableBase = (UINT8*)(UINTN)S3TableBase;\r
429 mS3BootScriptTablePtr->TableMemoryPageNumber = (UINT16)(2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));\r
430 }\r
431 \r
432 // Here we do not count the reserved memory for runtime script table.\r
433 PageNumber = (UINT16)(mS3BootScriptTablePtr->TableMemoryPageNumber - PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber)); \r
434 TableLength = mS3BootScriptTablePtr->TableLength;\r
d03b3130 435 if ((UINT32)(PageNumber * EFI_PAGE_SIZE) < (TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE))) {\r
64d14edf 436 // \r
437 // The buffer is too small to hold the table, Reallocate the buffer\r
438 //\r
439 NewS3TableBase = 0xffffffff;\r
440 Status = gBS->AllocatePages (\r
441 AllocateMaxAddress,\r
442 EfiACPIMemoryNVS,\r
443 2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),\r
444 (EFI_PHYSICAL_ADDRESS*)&NewS3TableBase\r
445 );\r
446 \r
447 if (EFI_ERROR(Status)) {\r
448 ASSERT_EFI_ERROR (Status);\r
449 return 0;\r
450 }\r
451 \r
452 CopyMem ((VOID*)(UINTN)NewS3TableBase, (VOID*)(UINTN)S3TableBase, TableLength);\r
453 gBS->FreePages (S3TableBase, mS3BootScriptTablePtr->TableMemoryPageNumber);\r
454 \r
455 mS3BootScriptTablePtr->TableBase = (UINT8*)(UINTN)NewS3TableBase;\r
456 mS3BootScriptTablePtr->TableMemoryPageNumber = (UINT16) (2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber)); \r
457 }\r
458 //\r
459 // calculate the the start address for the new entry. \r
460 //\r
461 NewEntryPtr = mS3BootScriptTablePtr->TableBase + TableLength;\r
462 \r
463 //\r
464 // update the table lenghth\r
465 //\r
466 mS3BootScriptTablePtr->TableLength = TableLength + EntryLength;\r
467 \r
468 //\r
469 // In the boot time, we will not append the termination entry to the boot script\r
470 // table until the callers think there is no boot time data that should be added and \r
471 // it is caller's responsibility to explicit call the CloseTable. \r
472 //\r
473 //\r
474 \r
475 return NewEntryPtr; \r
476}\r
477/**\r
478 To get the start address from which a new runtime s3 boot script entry will write into.\r
479 In this case, it should be ensured that there is enough buffer to hold the entry.\r
480 \r
481 @param EntryLength the new entry length.\r
482 \r
483 @retval the address from which the a new s3 runtime script entry will write into\r
484 **/\r
485UINT8*\r
486S3BootScriptGetRuntimeEntryAddAddress (\r
487 UINT8 EntryLength\r
488 )\r
489{\r
490 UINT8 *NewEntryPtr;\r
491 \r
492 NewEntryPtr = NULL; \r
493 //\r
494 // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node. \r
495 //\r
496 if (mS3BootScriptTablePtr->TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE) <= EFI_PAGES_TO_SIZE((UINT32)(mS3BootScriptTablePtr->TableMemoryPageNumber))) {\r
497 NewEntryPtr = mS3BootScriptTablePtr->TableBase + mS3BootScriptTablePtr->TableLength; \r
498 mS3BootScriptTablePtr->TableLength = mS3BootScriptTablePtr->TableLength + EntryLength;\r
499 //\r
500 // Append a terminate node on every insert\r
501 //\r
502 S3BootScriptInternalCloseTable ();\r
503 }\r
504 return (UINT8*)NewEntryPtr; \r
505}\r
506/**\r
507 To get the start address from which a new s3 boot script entry will write into.\r
508 \r
509 @param EntryLength the new entry length.\r
510 \r
511 @retval the address from which the a new s3 runtime script entry will write into \r
512 **/ \r
513UINT8* \r
514S3BootScriptGetEntryAddAddress (\r
515 UINT8 EntryLength\r
516 )\r
517{\r
518 UINT8* NewEntryPtr;\r
519 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader;\r
520 EFI_STATUS Status;\r
02f49fc2 521 UINTN OrgLockBoxLength;\r
64d14edf 522\r
523 if (mS3BootScriptTablePtr->AtRuntime) {\r
524 //\r
525 // We need check InSmm when AtRuntime, because after SmmReadyToLock, only SMM driver is allowed to write boot script.\r
526 //\r
527 if (!mS3BootScriptTablePtr->InSmm) {\r
528 //\r
529 // Add DEBUG ERROR, so that we can find it at boot time.\r
530 // Do not use ASSERT, because we may have test invoke this interface.\r
531 //\r
532 DEBUG ((EFI_D_ERROR, "FATAL ERROR: Set boot script after ReadyToLock!!!\n"));\r
533 return NULL;\r
534 }\r
535\r
536 //\r
3aa764ed 537 // NOTE: OS will restore ACPINvs data. After S3, the table length in mS3BootScriptTableSmmPtr (SMM) is different with\r
64d14edf 538 // table length in BootScriptTable header (ACPINvs).\r
539 // So here we need sync them. We choose ACPINvs table length, because we want to override the boot script saved\r
540 // in SMM every time.\r
541 //\r
3aa764ed 542 ASSERT (mS3BootScriptTablePtr == mS3BootScriptTableSmmPtr);\r
64d14edf 543 CopyMem ((VOID*)&TableHeader, (VOID*)mS3BootScriptTablePtr->TableBase, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
544 if (mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE) != TableHeader.TableLength) {\r
545 //\r
546 // Restore it to use original value\r
547 //\r
02f49fc2
SZ
548 OrgLockBoxLength = mLockBoxLength;\r
549 Status = RestoreLockBox (\r
550 &mBootScriptDataOrgGuid,\r
551 (VOID *)mS3BootScriptTablePtr->TableBase,\r
552 &OrgLockBoxLength\r
553 );\r
554 ASSERT_EFI_ERROR (Status);\r
555 ASSERT (OrgLockBoxLength == mLockBoxLength);\r
556\r
557 //\r
558 // Update the current BootScriptData into LockBox as well\r
559 //\r
560 Status = UpdateLockBox (\r
561 &mBootScriptDataGuid,\r
562 0,\r
563 (VOID *)mS3BootScriptTablePtr->TableBase,\r
564 OrgLockBoxLength\r
565 );\r
566 ASSERT_EFI_ERROR (Status);\r
567\r
64d14edf 568 //\r
64d14edf 569 // NOTE: We should NOT use TableHeader.TableLength, because it is already updated to be whole length.\r
570 //\r
571 mS3BootScriptTablePtr->TableLength = (UINT32)(mLockBoxLength - sizeof(EFI_BOOT_SCRIPT_TERMINATE));\r
572 }\r
573\r
574 NewEntryPtr = S3BootScriptGetRuntimeEntryAddAddress (EntryLength);\r
02f49fc2
SZ
575\r
576 if (EntryLength != 0) {\r
577 //\r
578 // Now the length field is updated, need sync to lockbox.\r
579 // So in S3 resume, the data can be restored correctly.\r
580 //\r
581 CopyMem ((VOID*)&TableHeader, (VOID*)mS3BootScriptTablePtr->TableBase, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
582 Status = UpdateLockBox (\r
583 &mBootScriptDataGuid,\r
584 OFFSET_OF(EFI_BOOT_SCRIPT_TABLE_HEADER, TableLength),\r
585 &TableHeader.TableLength,\r
586 sizeof(TableHeader.TableLength)\r
587 );\r
588 ASSERT_EFI_ERROR (Status);\r
589 }\r
64d14edf 590 } else { \r
591 NewEntryPtr = S3BootScriptGetBootTimeEntryAddAddress (EntryLength);\r
592 } \r
593 return NewEntryPtr;\r
594 \r
595} \r
596\r
597/**\r
598 Sync BootScript LockBox data.\r
02f49fc2
SZ
599\r
600 @param Script The address from where the boot script has been added or updated.\r
601\r
64d14edf 602**/\r
603VOID\r
604SyncBootScript (\r
02f49fc2 605 IN UINT8 *Script\r
64d14edf 606 )\r
607{\r
608 EFI_STATUS Status;\r
02f49fc2
SZ
609 UINTN ScriptOffset;\r
610\r
611 ScriptOffset = (UINTN) (Script - mS3BootScriptTablePtr->TableBase);\r
64d14edf 612\r
02f49fc2
SZ
613 if (!mS3BootScriptTablePtr->AtRuntime || !mS3BootScriptTablePtr->InSmm || ScriptOffset >= mLockBoxLength) {\r
614 //\r
615 // If it is not at runtime in SMM or in the range that needs to be synced in LockBox, just return.\r
616 //\r
64d14edf 617 return ;\r
618 }\r
02f49fc2 619\r
64d14edf 620 //\r
02f49fc2 621 // Update BootScriptData\r
64d14edf 622 // So in S3 resume, the data can be restored correctly.\r
623 //\r
624 Status = UpdateLockBox (\r
625 &mBootScriptDataGuid,\r
02f49fc2
SZ
626 ScriptOffset,\r
627 (VOID *)((UINTN)mS3BootScriptTablePtr->TableBase + ScriptOffset),\r
628 mLockBoxLength - ScriptOffset\r
64d14edf 629 );\r
630 ASSERT_EFI_ERROR (Status);\r
631}\r
632\r
633/** \r
634 This is an function to close the S3 boot script table. The function could only be called in \r
635 BOOT time phase. To comply with the Framework spec definition on \r
636 EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable(), this function will fulfill following things:\r
637 1. Closes the specified boot script table\r
638 2. It allocates a new memory pool to duplicate all the boot scripts in the specified table. \r
639 Once this function is called, the table maintained by the library will be destroyed \r
640 after it is copied into the allocated pool.\r
641 3. Any attempts to add a script record after calling this function will cause a new table \r
642 to be created by the library.\r
643 4. The base address of the allocated pool will be returned in Address. Note that after \r
644 using the boot script table, the CALLER is responsible for freeing the pool that is allocated\r
645 by this function. \r
646\r
647 In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is \r
648 for Framework Spec compatibility.\r
649 \r
650 If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out \r
651 how to get the script to run on an S3 resume because the boot script maintained by the lib will be \r
652 destroyed.\r
653 \r
654 @return the base address of the new copy of the boot script tble. \r
655 @note this function could only called in boot time phase\r
656\r
657**/\r
658UINT8*\r
659EFIAPI\r
660S3BootScriptCloseTable (\r
661 VOID\r
662 )\r
663{\r
664 UINT8 *S3TableBase;\r
665 UINT32 TableLength;\r
666 UINT8 *Buffer;\r
667 EFI_STATUS Status;\r
668 EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;\r
669 \r
670 S3TableBase = mS3BootScriptTablePtr->TableBase; \r
671 if (S3TableBase == 0) {\r
672 return 0; \r
673 }\r
674 //\r
675 // Append the termination record the S3 boot script table\r
676 //\r
677 S3BootScriptInternalCloseTable();\r
678 TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
679 //\r
680 // Allocate the buffer and copy the boot script to the buffer. \r
681 //\r
682 Status = gBS->AllocatePool (\r
683 EfiBootServicesData,\r
684 (UINTN)TableLength,\r
685 (VOID **) &Buffer\r
686 );\r
687 if (EFI_ERROR (Status)) {\r
688 return 0; \r
689 }\r
690 CopyMem (Buffer, S3TableBase, TableLength);\r
691 \r
692 //\r
693 // Destroy the table maintained by the library so that the next write operation \r
694 // will write the record to the first entry of the table.\r
695 //\r
696 // Fill the table header.\r
697 ScriptTableInfo = (EFI_BOOT_SCRIPT_TABLE_HEADER*)S3TableBase;\r
698 ScriptTableInfo->OpCode = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;\r
699 ScriptTableInfo->Length = (UINT8) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
700 ScriptTableInfo->TableLength = 0; // will be calculate at close the table\r
701 \r
702 mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
703 return Buffer;\r
704}\r
705/**\r
706 Save I/O write to boot script \r
707\r
708 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
709 @param Address The base address of the I/O operations.\r
710 @param Count The number of I/O operations to perform.\r
711 @param Buffer The source buffer from which to write data.\r
712\r
713 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
714 @retval RETURN_SUCCESS Opcode is added.\r
715**/\r
716RETURN_STATUS\r
717EFIAPI\r
718S3BootScriptSaveIoWrite (\r
719 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
720 IN UINT64 Address,\r
721 IN UINTN Count,\r
722 IN VOID *Buffer\r
723 )\r
724\r
725{\r
726 UINT8 Length;\r
727 UINT8 *Script;\r
728 UINT8 WidthInByte;\r
729 EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite;\r
730\r
731 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
732 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE) + (WidthInByte * Count));\r
733 \r
734 Script = S3BootScriptGetEntryAddAddress (Length);\r
735 if (Script == NULL) {\r
736 return RETURN_OUT_OF_RESOURCES;\r
737 }\r
738 //\r
739 // save script data\r
740 //\r
741 ScriptIoWrite.OpCode = EFI_BOOT_SCRIPT_IO_WRITE_OPCODE;\r
742 ScriptIoWrite.Length = Length;\r
743 ScriptIoWrite.Width = Width;\r
744 ScriptIoWrite.Address = Address;\r
745 ScriptIoWrite.Count = (UINT32) Count;\r
746 CopyMem ((VOID*)Script, (VOID*)&ScriptIoWrite, sizeof(EFI_BOOT_SCRIPT_IO_WRITE));\r
747 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_WRITE)), Buffer, WidthInByte * Count);\r
748\r
02f49fc2 749 SyncBootScript (Script);\r
64d14edf 750\r
751 return RETURN_SUCCESS;\r
752}\r
753\r
754/**\r
755 Adds a record for an I/O modify operation into a S3 boot script table\r
756\r
757 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
758 @param Address The base address of the I/O operations.\r
759 @param Data A pointer to the data to be OR-ed.\r
760 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register\r
761\r
762 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
763 @retval RETURN_SUCCESS Opcode is added.\r
764**/\r
765RETURN_STATUS\r
766EFIAPI\r
767S3BootScriptSaveIoReadWrite (\r
768 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
769 IN UINT64 Address,\r
770 IN VOID *Data,\r
771 IN VOID *DataMask\r
772 )\r
773{\r
774 UINT8 Length;\r
775 UINT8 *Script;\r
776 UINT8 WidthInByte;\r
777 EFI_BOOT_SCRIPT_IO_READ_WRITE ScriptIoReadWrite;\r
778\r
779 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
780 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + (WidthInByte * 2));\r
781 \r
782 Script = S3BootScriptGetEntryAddAddress (Length);\r
783 if (Script == NULL) {\r
784 return RETURN_OUT_OF_RESOURCES;\r
785 }\r
786 //\r
787 // Build script data\r
788 //\r
789 ScriptIoReadWrite.OpCode = EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE;\r
790 ScriptIoReadWrite.Length = Length;\r
791 ScriptIoReadWrite.Width = Width;\r
792 ScriptIoReadWrite.Address = Address;\r
793 \r
794 CopyMem ((VOID*)Script, (VOID*)&ScriptIoReadWrite, sizeof(EFI_BOOT_SCRIPT_IO_READ_WRITE));\r
795 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE)), Data, WidthInByte);\r
796 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + WidthInByte), DataMask, WidthInByte);\r
797\r
02f49fc2 798 SyncBootScript (Script);\r
64d14edf 799\r
800 return RETURN_SUCCESS;\r
801}\r
802/**\r
803 Adds a record for a memory write operation into a specified boot script table.\r
804\r
805 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
806 @param Address The base address of the memory operations\r
807 @param Count The number of memory operations to perform.\r
808 @param Buffer The source buffer from which to write the data.\r
809\r
810 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
811 @retval RETURN_SUCCESS Opcode is added.\r
812**/\r
813RETURN_STATUS\r
814EFIAPI\r
815S3BootScriptSaveMemWrite (\r
816 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
817 IN UINT64 Address,\r
818 IN UINTN Count,\r
819 IN VOID *Buffer\r
820 )\r
821{\r
822 UINT8 Length;\r
823 UINT8 *Script;\r
824 UINT8 WidthInByte;\r
825 EFI_BOOT_SCRIPT_MEM_WRITE ScriptMemWrite;\r
826 \r
827 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
828 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE) + (WidthInByte * Count));\r
829 \r
830 Script = S3BootScriptGetEntryAddAddress (Length);\r
831 if (Script == NULL) {\r
832 return RETURN_OUT_OF_RESOURCES;\r
833 } \r
834 //\r
835 // Build script data\r
836 //\r
837 ScriptMemWrite.OpCode = EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE;\r
838 ScriptMemWrite.Length = Length;\r
839 ScriptMemWrite.Width = Width;\r
840 ScriptMemWrite.Address = Address;\r
841 ScriptMemWrite.Count = (UINT32) Count;\r
842 \r
843 CopyMem ((VOID*)Script, (VOID*)&ScriptMemWrite, sizeof(EFI_BOOT_SCRIPT_MEM_WRITE));\r
844 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_WRITE)), Buffer, WidthInByte * Count);\r
845 \r
02f49fc2 846 SyncBootScript (Script);\r
64d14edf 847\r
848 return RETURN_SUCCESS;\r
849}\r
850/**\r
851 Adds a record for a memory modify operation into a specified boot script table.\r
852\r
853 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
854 @param Address The base address of the memory operations. Address needs alignment if required\r
855 @param Data A pointer to the data to be OR-ed.\r
856 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register.\r
857\r
858 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
859 @retval RETURN_SUCCESS Opcode is added.\r
860**/\r
861RETURN_STATUS\r
862EFIAPI\r
863S3BootScriptSaveMemReadWrite (\r
864 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
865 IN UINT64 Address,\r
866 IN VOID *Data,\r
867 IN VOID *DataMask\r
868 )\r
869{\r
870 UINT8 Length;\r
871 UINT8 *Script;\r
872 UINT8 WidthInByte;\r
873 EFI_BOOT_SCRIPT_MEM_READ_WRITE ScriptMemReadWrite;\r
874 \r
875 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
876 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + (WidthInByte * 2));\r
877 \r
878 Script = S3BootScriptGetEntryAddAddress (Length);\r
879 if (Script == NULL) {\r
880 return RETURN_OUT_OF_RESOURCES;\r
881 } \r
882 //\r
883 // Build script data\r
884 // \r
885 ScriptMemReadWrite.OpCode = EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE;\r
886 ScriptMemReadWrite.Length = Length;\r
887 ScriptMemReadWrite.Width = Width;\r
888 ScriptMemReadWrite.Address = Address;\r
889 \r
890 CopyMem ((VOID*)Script, (VOID*)&ScriptMemReadWrite , sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE));\r
891 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE)), Data, WidthInByte);\r
892 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + WidthInByte), DataMask, WidthInByte);\r
893\r
02f49fc2 894 SyncBootScript (Script);\r
64d14edf 895\r
896 return RETURN_SUCCESS;\r
897}\r
898/**\r
899 Adds a record for a PCI configuration space write operation into a specified boot script table.\r
900\r
901 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
902 @param Address The address within the PCI configuration space.\r
903 @param Count The number of PCI operations to perform.\r
904 @param Buffer The source buffer from which to write the data.\r
905\r
906 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
907 @retval RETURN_SUCCESS Opcode is added.\r
908**/\r
909RETURN_STATUS\r
910EFIAPI\r
911S3BootScriptSavePciCfgWrite (\r
912 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
913 IN UINT64 Address,\r
914 IN UINTN Count,\r
915 IN VOID *Buffer\r
916 )\r
917{\r
918 UINT8 Length;\r
919 UINT8 *Script;\r
920 UINT8 WidthInByte;\r
921 EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE ScriptPciWrite;\r
922\r
923 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
924 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE) + (WidthInByte * Count));\r
925 \r
926 Script = S3BootScriptGetEntryAddAddress (Length);\r
927 if (Script == NULL) {\r
928 return RETURN_OUT_OF_RESOURCES;\r
929 } \r
930 //\r
931 // Build script data\r
932 //\r
933 ScriptPciWrite.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE;\r
934 ScriptPciWrite.Length = Length;\r
935 ScriptPciWrite.Width = Width;\r
936 ScriptPciWrite.Address = Address;\r
937 ScriptPciWrite.Count = (UINT32) Count;\r
938 \r
939 CopyMem ((VOID*)Script, (VOID*)&ScriptPciWrite, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE));\r
940 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE)), Buffer, WidthInByte * Count);\r
941 \r
02f49fc2 942 SyncBootScript (Script);\r
64d14edf 943\r
944 return RETURN_SUCCESS;\r
945}\r
946/**\r
947 Adds a record for a PCI configuration space modify operation into a specified boot script table.\r
948\r
949 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
950 @param Address The address within the PCI configuration space.\r
951 @param Data A pointer to the data to be OR-ed.The size depends on Width.\r
952 @param DataMask A pointer to the data mask to be AND-ed.\r
953\r
954 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
955 @retval RETURN__SUCCESS Opcode is added.\r
956**/\r
957RETURN_STATUS\r
958EFIAPI\r
959S3BootScriptSavePciCfgReadWrite (\r
960 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
961 IN UINT64 Address,\r
962 IN VOID *Data,\r
963 IN VOID *DataMask\r
964 )\r
965{\r
966 UINT8 Length;\r
967 UINT8 *Script;\r
968 UINT8 WidthInByte;\r
969 EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE ScriptPciReadWrite;\r
970\r
971 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
972 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + (WidthInByte * 2));\r
973 \r
974 Script = S3BootScriptGetEntryAddAddress (Length);\r
975 if (Script == NULL) {\r
976 return RETURN_OUT_OF_RESOURCES;\r
977 } \r
978 //\r
979 // Build script data\r
980 // \r
981 ScriptPciReadWrite.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE;\r
982 ScriptPciReadWrite.Length = Length;\r
983 ScriptPciReadWrite.Width = Width;\r
984 ScriptPciReadWrite.Address = Address;\r
985 \r
986 CopyMem ((VOID*)Script, (VOID*)&ScriptPciReadWrite, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE));\r
987 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE)), Data, WidthInByte);\r
988 CopyMem (\r
989 (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + WidthInByte),\r
990 DataMask,\r
991 WidthInByte\r
992 );\r
993\r
02f49fc2 994 SyncBootScript (Script);\r
64d14edf 995\r
996 return RETURN_SUCCESS;\r
997}\r
998/**\r
02f49fc2 999 Adds a record for a PCI configuration 2 space write operation into a specified boot script table.\r
64d14edf 1000\r
1001 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
1002 @param Segment The PCI segment number for Address.\r
1003 @param Address The address within the PCI configuration space.\r
1004 @param Count The number of PCI operations to perform.\r
1005 @param Buffer The source buffer from which to write the data.\r
1006\r
1007 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1008 @retval RETURN_SUCCESS Opcode is added.\r
1009**/\r
1010RETURN_STATUS\r
1011EFIAPI\r
1012S3BootScriptSavePciCfg2Write (\r
1013 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1014 IN UINT16 Segment,\r
1015 IN UINT64 Address,\r
1016 IN UINTN Count,\r
1017 IN VOID *Buffer\r
1018 )\r
1019{\r
1020 UINT8 Length;\r
1021 UINT8 *Script;\r
1022 UINT8 WidthInByte;\r
1023 EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE ScriptPciWrite2;\r
1024 \r
1025 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
1026 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE) + (WidthInByte * Count));\r
1027 \r
1028 Script = S3BootScriptGetEntryAddAddress (Length);\r
1029 if (Script == NULL) {\r
1030 return RETURN_OUT_OF_RESOURCES;\r
1031 } \r
1032 //\r
1033 // Build script data\r
1034 //\r
1035 ScriptPciWrite2.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE;\r
1036 ScriptPciWrite2.Length = Length;\r
1037 ScriptPciWrite2.Width = Width;\r
1038 ScriptPciWrite2.Address = Address;\r
1039 ScriptPciWrite2.Segment = Segment;\r
1040 ScriptPciWrite2.Count = (UINT32)Count;\r
1041 \r
1042 CopyMem ((VOID*)Script, (VOID*)&ScriptPciWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE));\r
1043 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE)), Buffer, WidthInByte * Count);\r
1044\r
02f49fc2 1045 SyncBootScript (Script);\r
64d14edf 1046\r
1047 return RETURN_SUCCESS;\r
1048}\r
1049/**\r
02f49fc2 1050 Adds a record for a PCI configuration 2 space modify operation into a specified boot script table.\r
64d14edf 1051\r
1052 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
1053 @param Segment The PCI segment number for Address.\r
1054 @param Address The address within the PCI configuration space.\r
1055 @param Data A pointer to the data to be OR-ed. The size depends on Width.\r
1056 @param DataMask A pointer to the data mask to be AND-ed.\r
1057\r
1058 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1059 @retval RETURN_SUCCESS Opcode is added.\r
1060**/\r
1061RETURN_STATUS\r
1062EFIAPI\r
1063S3BootScriptSavePciCfg2ReadWrite (\r
1064 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1065 IN UINT16 Segment,\r
1066 IN UINT64 Address,\r
1067 IN VOID *Data,\r
1068 IN VOID *DataMask\r
1069 )\r
1070{\r
1071 UINT8 Length;\r
1072 UINT8 *Script;\r
1073 UINT8 WidthInByte;\r
1074 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE ScriptPciReadWrite2;\r
1075 \r
1076 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
1077 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + (WidthInByte * 2));\r
1078 \r
1079 Script = S3BootScriptGetEntryAddAddress (Length);\r
1080 if (Script == NULL) {\r
1081 return RETURN_OUT_OF_RESOURCES;\r
1082 } \r
1083 //\r
1084 // Build script data\r
1085 //\r
1086 ScriptPciReadWrite2.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE;\r
1087 ScriptPciReadWrite2.Length = Length;\r
1088 ScriptPciReadWrite2.Width = Width;\r
1089 ScriptPciReadWrite2.Segment = Segment;\r
1090 ScriptPciReadWrite2.Address = Address;\r
1091 \r
1092 CopyMem ((VOID*)Script, (VOID*)&ScriptPciReadWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE));\r
1093 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE)), Data, WidthInByte);\r
1094 CopyMem (\r
1095 (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + WidthInByte),\r
1096 DataMask,\r
1097 WidthInByte\r
1098 );\r
1099 \r
02f49fc2 1100 SyncBootScript (Script);\r
64d14edf 1101\r
1102 return RETURN_SUCCESS;\r
1103}\r
1104/**\r
1105 Adds a record for an SMBus command execution into a specified boot script table.\r
1106\r
1107 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length, and PEC.\r
1108 @param Operation Indicates which particular SMBus protocol it will use to execute the SMBus\r
1109 transactions.\r
1110 @param Length A pointer to signify the number of bytes that this operation will do.\r
1111 @param Buffer Contains the value of data to execute to the SMBUS slave device.\r
1112 \r
1113 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1114 @retval RETURN_SUCCESS Opcode is added.\r
1115**/\r
1116RETURN_STATUS\r
1117EFIAPI\r
1118S3BootScriptSaveSmbusExecute (\r
1119 IN UINTN SmBusAddress, \r
1120 IN EFI_SMBUS_OPERATION Operation,\r
1121 IN UINTN *Length,\r
1122 IN VOID *Buffer\r
1123 )\r
1124{\r
1125 UINT8 DataSize;\r
1126 UINT8 *Script;\r
1127 EFI_BOOT_SCRIPT_SMBUS_EXECUTE ScriptSmbusExecute;\r
1128\r
1129 DataSize = (UINT8)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE) + (*Length));\r
1130 \r
1131 Script = S3BootScriptGetEntryAddAddress (DataSize);\r
1132 if (Script == NULL) {\r
1133 return RETURN_OUT_OF_RESOURCES;\r
1134 }\r
1135 //\r
1136 // Build script data\r
1137 //\r
1138 ScriptSmbusExecute.OpCode = EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE;\r
1139 ScriptSmbusExecute.Length = DataSize;\r
1140 ScriptSmbusExecute.SmBusAddress = (UINT64) SmBusAddress;\r
1141 ScriptSmbusExecute.Operation = Operation;\r
1142 ScriptSmbusExecute.DataSize = (UINT32) *Length;\r
1143\r
1144 CopyMem ((VOID*)Script, (VOID*)&ScriptSmbusExecute, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE));\r
1145 CopyMem (\r
1146 (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)),\r
1147 Buffer,\r
1148 (*Length)\r
1149 );\r
1150\r
02f49fc2 1151 SyncBootScript (Script);\r
64d14edf 1152\r
1153 return RETURN_SUCCESS;\r
1154}\r
1155/**\r
1156 Adds a record for an execution stall on the processor into a specified boot script table.\r
1157\r
1158 @param Duration Duration in microseconds of the stall\r
1159 \r
1160 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1161 @retval RETURN_SUCCESS Opcode is added.\r
1162**/\r
1163RETURN_STATUS\r
1164EFIAPI\r
1165S3BootScriptSaveStall (\r
1166 IN UINTN Duration\r
1167 )\r
1168{\r
1169 UINT8 Length;\r
1170 UINT8 *Script;\r
1171 EFI_BOOT_SCRIPT_STALL ScriptStall;\r
1172\r
1173 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_STALL));\r
1174 \r
1175 Script = S3BootScriptGetEntryAddAddress (Length);\r
1176 if (Script == NULL) {\r
1177 return RETURN_OUT_OF_RESOURCES;\r
1178 } \r
1179 //\r
1180 // Build script data\r
1181 //\r
1182 ScriptStall.OpCode = EFI_BOOT_SCRIPT_STALL_OPCODE;\r
1183 ScriptStall.Length = Length;\r
1184 ScriptStall.Duration = Duration;\r
1185 \r
1186 CopyMem ((VOID*)Script, (VOID*)&ScriptStall, sizeof (EFI_BOOT_SCRIPT_STALL));\r
1187 \r
02f49fc2 1188 SyncBootScript (Script);\r
64d14edf 1189\r
1190 return RETURN_SUCCESS;\r
1191}\r
1192/**\r
02f49fc2 1193 Adds a record for dispatching specified arbitrary code into a specified boot script table.\r
64d14edf 1194\r
1195 @param EntryPoint Entry point of the code to be dispatched.\r
1196 @param Context Argument to be passed into the EntryPoint of the code to be dispatched.\r
1197 \r
1198 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1199 @retval RETURN_SUCCESS Opcode is added.\r
1200**/\r
1201RETURN_STATUS\r
1202EFIAPI\r
1203S3BootScriptSaveDispatch2 (\r
1204 IN VOID *EntryPoint,\r
1205 IN VOID *Context\r
1206 )\r
1207{\r
1208 UINT8 Length;\r
1209 UINT8 *Script;\r
1210 EFI_BOOT_SCRIPT_DISPATCH_2 ScriptDispatch2;\r
1211 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));\r
1212 \r
1213 Script = S3BootScriptGetEntryAddAddress (Length);\r
1214 if (Script == NULL) {\r
1215 return RETURN_OUT_OF_RESOURCES;\r
1216 } \r
1217 //\r
1218 // Build script data\r
1219 //\r
1220 ScriptDispatch2.OpCode = EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE;\r
1221 ScriptDispatch2.Length = Length;\r
1222 ScriptDispatch2.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;\r
1223 ScriptDispatch2.Context = (EFI_PHYSICAL_ADDRESS)(UINTN)Context;\r
1224 \r
1225 CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch2, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));\r
1226 \r
02f49fc2 1227 SyncBootScript (Script);\r
64d14edf 1228\r
1229 return RETURN_SUCCESS;\r
1230\r
1231}\r
1232/**\r
1233 Adds a record for memory reads of the memory location and continues when the exit criteria is\r
1234 satisfied or after a defined duration.\r
1235 \r
1236 @param Width The width of the memory operations.\r
1237 @param Address The base address of the memory operations.\r
1238 @param BitMask A pointer to the bit mask to be AND-ed with the data read from the register.\r
1239 @param BitValue A pointer to the data value after to be Masked.\r
1240 @param Duration Duration in microseconds of the stall.\r
1241 @param LoopTimes The times of the register polling.\r
1242\r
1243 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1244 @retval RETURN_SUCCESS Opcode is added.\r
1245\r
1246**/\r
1247RETURN_STATUS\r
1248EFIAPI\r
1249S3BootScriptSaveMemPoll (\r
1250 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1251 IN UINT64 Address,\r
1252 IN VOID *BitMask,\r
1253 IN VOID *BitValue,\r
1254 IN UINTN Duration,\r
1255 IN UINTN LoopTimes\r
1256 )\r
1257{\r
1258 UINT8 Length;\r
1259 UINT8 *Script;\r
1260 UINT8 WidthInByte; \r
1261 EFI_BOOT_SCRIPT_MEM_POLL ScriptMemPoll; \r
1262\r
1263 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
1264 \r
1265 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + (WidthInByte * 2));\r
1266 \r
1267 Script = S3BootScriptGetEntryAddAddress (Length);\r
1268 if (Script == NULL) {\r
1269 return RETURN_OUT_OF_RESOURCES;\r
1270 }\r
1271 //\r
1272 // Build script data\r
1273 //\r
1274 ScriptMemPoll.OpCode = EFI_BOOT_SCRIPT_MEM_POLL_OPCODE;\r
1275 ScriptMemPoll.Length = Length;\r
1276 ScriptMemPoll.Width = Width; \r
1277 ScriptMemPoll.Address = Address;\r
1278 ScriptMemPoll.Duration = Duration;\r
1279 ScriptMemPoll.LoopTimes = LoopTimes;\r
1280\r
1281 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL)), BitValue, WidthInByte);\r
1282 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + WidthInByte), BitMask, WidthInByte);\r
1283 CopyMem ((VOID*)Script, (VOID*)&ScriptMemPoll, sizeof (EFI_BOOT_SCRIPT_MEM_POLL)); \r
1284\r
02f49fc2 1285 SyncBootScript (Script);\r
64d14edf 1286\r
1287 return RETURN_SUCCESS;\r
1288}\r
1289/**\r
1290 Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only\r
1291 used for debugging script issues.\r
1292 \r
1293 @param InformationLength Length of the data in bytes\r
1294 @param Information Information to be logged in the boot scrpit\r
1295 \r
64d14edf 1296 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1297 @retval RETURN_SUCCESS Opcode is added.\r
1298\r
1299**/\r
1300RETURN_STATUS\r
1301EFIAPI\r
1302S3BootScriptSaveInformation (\r
1303 IN UINT32 InformationLength, \r
1304 IN VOID *Information\r
1305 )\r
1306{\r
64d14edf 1307 UINT8 Length;\r
1308 UINT8 *Script;\r
64d14edf 1309 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;\r
1310\r
93b21ade
SZ
1311 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);\r
1312\r
64d14edf 1313 Script = S3BootScriptGetEntryAddAddress (Length);\r
1314 if (Script == NULL) {\r
1315 return RETURN_OUT_OF_RESOURCES;\r
1316 }\r
1317 //\r
1318 // Build script data\r
1319 //\r
1320 ScriptInformation.OpCode = EFI_BOOT_SCRIPT_INFORMATION_OPCODE;\r
1321 ScriptInformation.Length = Length;\r
1322\r
1323\r
1324 ScriptInformation.InformationLength = InformationLength; \r
1325\r
93b21ade
SZ
1326 CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
1327 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);\r
1328\r
02f49fc2 1329 SyncBootScript (Script);\r
93b21ade 1330\r
64d14edf 1331 return RETURN_SUCCESS;\r
1332\r
1333}\r
1334/**\r
1335 Store a string in the boot script table. This opcode is a no-op on dispatch and is only\r
1336 used for debugging script issues.\r
1337 \r
1338 @param String The string to save to boot script table\r
1339 \r
1340 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1341 @retval RETURN_SUCCESS Opcode is added.\r
1342\r
1343**/\r
1344RETURN_STATUS\r
1345EFIAPI\r
1346S3BootScriptSaveInformationAsciiString (\r
1347 IN CONST CHAR8 *String\r
1348 )\r
1349{\r
1350 return S3BootScriptSaveInformation ( \r
1351 (UINT32) AsciiStrLen (String) + 1, \r
1352 (VOID*) String\r
1353 );\r
1354}\r
1355/**\r
1356 Adds a record for dispatching specified arbitrary code into a specified boot script table.\r
1357\r
1358 @param EntryPoint Entry point of the code to be dispatched.\r
1359 \r
1360 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1361 @retval RETURN_SUCCESS Opcode is added.\r
1362**/\r
1363RETURN_STATUS\r
1364EFIAPI\r
1365S3BootScriptSaveDispatch (\r
1366 IN VOID *EntryPoint\r
1367 )\r
1368{\r
1369 UINT8 Length;\r
1370 UINT8 *Script;\r
1371 EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch;\r
1372 \r
1373 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH));\r
1374 \r
1375 Script = S3BootScriptGetEntryAddAddress (Length);\r
1376 if (Script == NULL) {\r
1377 return RETURN_OUT_OF_RESOURCES;\r
1378 } \r
1379 //\r
1380 // Build script data\r
1381 //\r
1382 ScriptDispatch.OpCode = EFI_BOOT_SCRIPT_DISPATCH_OPCODE;\r
1383 ScriptDispatch.Length = Length;\r
1384 ScriptDispatch.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;\r
1385 \r
1386 CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH)); \r
1387 \r
02f49fc2 1388 SyncBootScript (Script);\r
64d14edf 1389\r
1390 return RETURN_SUCCESS;\r
1391\r
1392}\r
1393/**\r
1394 Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a\r
1395 defined duration.\r
1396 \r
1397 @param Width The width of the I/O operations. \r
1398 @param Address The base address of the I/O operations.\r
1399 @param Data The comparison value used for the polling exit criteria.\r
1400 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
1401 in Data are ignored when polling the memory address.\r
1402 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer\r
1403 granularity so the delay may be longer.\r
1404\r
1405 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1406 @retval RETURN_SUCCESS Opcode is added.\r
1407\r
1408**/\r
1409RETURN_STATUS\r
1410EFIAPI\r
1411S3BootScriptSaveIoPoll (\r
1412 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1413 IN UINT64 Address,\r
1414 IN VOID *Data,\r
1415 IN VOID *DataMask, \r
1416 IN UINT64 Delay \r
1417 )\r
1418{\r
1419 UINT8 WidthInByte; \r
1420 UINT8 *Script;\r
1421 UINT8 Length;\r
1422 EFI_BOOT_SCRIPT_IO_POLL ScriptIoPoll;\r
1423 \r
1424\r
1425 WidthInByte = (UINT8) (0x01 << (Width & 0x03)); \r
1426 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));\r
1427 \r
1428 Script = S3BootScriptGetEntryAddAddress (Length);\r
1429 if (Script == NULL) {\r
1430 return RETURN_OUT_OF_RESOURCES;\r
1431 } \r
1432 //\r
1433 // Build script data\r
1434 //\r
1435 ScriptIoPoll.OpCode = EFI_BOOT_SCRIPT_IO_POLL_OPCODE;\r
1436 ScriptIoPoll.Length = (UINT8) (sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));\r
1437 ScriptIoPoll.Width = Width; \r
1438 ScriptIoPoll.Address = Address;\r
1439 ScriptIoPoll.Delay = Delay;\r
1440\r
1441 CopyMem ((VOID*)Script, (VOID*)&ScriptIoPoll, sizeof (EFI_BOOT_SCRIPT_IO_POLL)); \r
1442 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL)), Data, WidthInByte);\r
1443 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL) + WidthInByte), DataMask, WidthInByte);\r
1444 \r
02f49fc2 1445 SyncBootScript (Script);\r
64d14edf 1446\r
1447 return RETURN_SUCCESS;\r
1448}\r
1449\r
1450/**\r
1451 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or\r
1452 after a defined duration.\r
1453\r
1454 @param Width The width of the I/O operations. \r
1455 @param Address The address within the PCI configuration space.\r
1456 @param Data The comparison value used for the polling exit criteria.\r
1457 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
1458 in Data are ignored when polling the memory address\r
1459 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer\r
1460 granularity so the delay may be longer.\r
1461\r
1462 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1463 @retval RETURN_SUCCESS Opcode is added.\r
1464\r
1465**/\r
1466RETURN_STATUS\r
1467EFIAPI\r
1468S3BootScriptSavePciPoll (\r
1469 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1470 IN UINT64 Address,\r
1471 IN VOID *Data,\r
1472 IN VOID *DataMask,\r
1473 IN UINT64 Delay\r
1474)\r
1475{\r
1476 UINT8 *Script;\r
1477 UINT8 WidthInByte; \r
1478 UINT8 Length;\r
1479 EFI_BOOT_SCRIPT_PCI_CONFIG_POLL ScriptPciPoll;\r
1480\r
1481 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
1482 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));\r
1483 \r
1484 Script = S3BootScriptGetEntryAddAddress (Length);\r
1485 if (Script == NULL) {\r
1486 return RETURN_OUT_OF_RESOURCES;\r
1487 }\r
1488 //\r
1489 // Build script data\r
1490 //\r
1491 ScriptPciPoll.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE;\r
1492 ScriptPciPoll.Length = (UINT8) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));\r
1493 ScriptPciPoll.Width = Width; \r
1494 ScriptPciPoll.Address = Address;\r
1495 ScriptPciPoll.Delay = Delay;\r
1496\r
1497 CopyMem ((VOID*)Script, (VOID*)&ScriptPciPoll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL));\r
1498 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL)), Data, WidthInByte);\r
1499 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + WidthInByte), DataMask, WidthInByte);\r
1500 \r
02f49fc2 1501 SyncBootScript (Script);\r
64d14edf 1502\r
1503 return RETURN_SUCCESS;\r
1504}\r
1505/**\r
1506 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or\r
1507 after a defined duration.\r
1508\r
1509 @param Width The width of the I/O operations. \r
1510 @param Segment The PCI segment number for Address.\r
1511 @param Address The address within the PCI configuration space.\r
1512 @param Data The comparison value used for the polling exit criteria.\r
1513 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
1514 in Data are ignored when polling the memory address\r
1515 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer\r
1516 granularity so the delay may be longer.\r
1517\r
1518 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1519 @retval RETURN_SUCCESS Opcode is added.\r
1520 @note A known Limitations in the implementation: When interpreting the opcode EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE\r
1521 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE and EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE, the 'Segment' parameter is assumed as \r
1522 Zero, or else, assert.\r
1523**/\r
1524RETURN_STATUS\r
1525EFIAPI\r
1526S3BootScriptSavePci2Poll (\r
1527 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1528 IN UINT16 Segment,\r
1529 IN UINT64 Address,\r
1530 IN VOID *Data,\r
1531 IN VOID *DataMask,\r
1532 IN UINT64 Delay\r
1533)\r
1534{\r
1535 UINT8 WidthInByte; \r
1536 UINT8 *Script;\r
1537 UINT8 Length;\r
1538 EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL ScriptPci2Poll;\r
1539 \r
1540 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
1541 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));\r
1542 \r
1543 Script = S3BootScriptGetEntryAddAddress (Length);\r
1544 if (Script == NULL) {\r
1545 return RETURN_OUT_OF_RESOURCES;\r
1546 } \r
1547 //\r
1548 // Build script data\r
1549 //\r
1550 ScriptPci2Poll.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE;\r
1551 ScriptPci2Poll.Length = (UINT8) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));\r
1552 ScriptPci2Poll.Width = Width; \r
1553 ScriptPci2Poll.Segment = Segment;\r
1554 ScriptPci2Poll.Address = Address;\r
1555 ScriptPci2Poll.Delay = Delay;\r
1556\r
1557 CopyMem ((VOID*)Script, (VOID*)&ScriptPci2Poll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL));\r
1558 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL)), Data, WidthInByte);\r
1559 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + WidthInByte), DataMask, WidthInByte);\r
1560 \r
02f49fc2 1561 SyncBootScript (Script);\r
64d14edf 1562\r
1563 return RETURN_SUCCESS;\r
1564}\r
1565/**\r
1566 Do the calculation of start address from which a new s3 boot script entry will write into.\r
1567 \r
1568 @param EntryLength The new entry length.\r
1569 @param Position specifies the position in the boot script table where the opcode will be\r
1570 inserted, either before or after, depending on BeforeOrAfter. \r
1571 @param BeforeOrAfter The flag to indicate to insert the nod before or after the position.\r
1572 This parameter is effective when InsertFlag is TRUE\r
1573 @param Script return out the position from which the a new s3 boot script entry will write into\r
1574**/\r
1575VOID\r
1576S3BootScriptCalculateInsertAddress (\r
1577 IN UINT8 EntryLength,\r
1578 IN VOID *Position OPTIONAL,\r
1579 IN BOOLEAN BeforeOrAfter OPTIONAL,\r
1580 OUT UINT8 **Script \r
1581 )\r
1582{\r
1583 UINTN TableLength;\r
1584 UINT8 *S3TableBase;\r
1585 UINTN PositionOffset; \r
1586 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;\r
1587 //\r
1588 // The entry inserting to table is already added to the end of the table\r
1589 //\r
1590 TableLength = mS3BootScriptTablePtr->TableLength - EntryLength;\r
1591 S3TableBase = mS3BootScriptTablePtr->TableBase ;\r
1592 // \r
1593 // calculate the Position offset\r
1594 //\r
1595 if (Position != NULL) {\r
1596 PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);\r
1597 \r
1598 //\r
1599 // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.\r
1600 //\r
1601 if (!BeforeOrAfter) {\r
1602 CopyMem ((VOID*)&ScriptHeader, Position, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
1603 PositionOffset += (ScriptHeader.Length);\r
1604 }\r
1605 // \r
1606 // Insert the node before the adjusted Position\r
1607 //\r
1608 CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset); \r
1609 //\r
1610 // calculate the the start address for the new entry. \r
1611 //\r
1612 *Script = S3TableBase + PositionOffset;\r
1613 \r
1614 } else {\r
1615 if (!BeforeOrAfter) {\r
1616 //\r
1617 // Insert the node to the end of the table\r
1618 //\r
1619 *Script = S3TableBase + TableLength; \r
1620 } else {\r
1621 // \r
1622 // Insert the node to the beginning of the table\r
1623 //\r
1624 PositionOffset = (UINTN) sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);\r
1625 CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset); \r
1626 *Script = S3TableBase + PositionOffset; \r
1627 }\r
1628 } \r
1629}\r
1630/**\r
1631 Move the last boot script entry to the position \r
1632\r
1633 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position\r
1634 in the boot script table specified by Position. If Position is NULL or points to\r
1635 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end\r
1636 of the table (if FALSE).\r
1637 @param Position On entry, specifies the position in the boot script table where the opcode will be\r
1638 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies\r
1639 the position of the inserted opcode in the boot script table.\r
1640\r
1641 @retval RETURN_OUT_OF_RESOURCES The table is not available.\r
1642 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.\r
1643 @retval RETURN_SUCCESS Opcode is inserted.\r
1644**/\r
1645RETURN_STATUS\r
1646EFIAPI\r
1647S3BootScriptMoveLastOpcode (\r
1648 IN BOOLEAN BeforeOrAfter,\r
1649 IN OUT VOID **Position OPTIONAL\r
1650)\r
1651{\r
1652 UINT8* Script;\r
1653 VOID *TempPosition; \r
1654 UINTN StartAddress;\r
1655 UINT32 TableLength;\r
1656 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;\r
1657 BOOLEAN ValidatePosition;\r
1658 UINT8* LastOpcode;\r
1659 UINT8 TempBootScriptEntry[BOOT_SCRIPT_NODE_MAX_LENGTH];\r
1660 \r
1661 ValidatePosition = FALSE;\r
1662 TempPosition = (Position == NULL) ? NULL:(*Position);\r
02f49fc2
SZ
1663\r
1664 //\r
1665 // Check that the script is initialized and synced without adding an entry to the script.\r
1666 //\r
1667 Script = S3BootScriptGetEntryAddAddress (0);\r
1668 if (Script == NULL) {\r
1669 return RETURN_OUT_OF_RESOURCES;\r
64d14edf 1670 }\r
02f49fc2
SZ
1671 Script = mS3BootScriptTablePtr->TableBase;\r
1672\r
64d14edf 1673 StartAddress = (UINTN) Script;\r
1674 TableLength = mS3BootScriptTablePtr->TableLength;\r
1675 Script = Script + sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);\r
1676 LastOpcode = Script;\r
1677 //\r
1678 // Find the last boot Script Entry which is not the terminate node\r
1679 //\r
1680 while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) { \r
1681 CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
1682 if (TempPosition != NULL && TempPosition == Script) {\r
1683 //\r
1684 // If the position is specified, the position must be pointed to a boot script entry start address. \r
1685 //\r
1686 ValidatePosition = TRUE;\r
1687 }\r
1688 if (ScriptHeader.OpCode != S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE) {\r
1689 LastOpcode = Script;\r
1690 } \r
1691 Script = Script + ScriptHeader.Length;\r
1692 }\r
1693 //\r
1694 // If the position is specified, but not the start of a boot script entry, it is a invalid input\r
1695 //\r
1696 if (TempPosition != NULL && !ValidatePosition) {\r
1697 return RETURN_INVALID_PARAMETER;\r
1698 }\r
1699 \r
1700 CopyMem ((VOID*)&ScriptHeader, LastOpcode, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
1701 \r
1702 CopyMem((VOID*)TempBootScriptEntry, LastOpcode, ScriptHeader.Length); \r
1703 //\r
1704 // Find the right position to write the node in\r
1705 //\r
1706 S3BootScriptCalculateInsertAddress (\r
1707 ScriptHeader.Length,\r
1708 TempPosition,\r
1709 BeforeOrAfter,\r
1710 &Script \r
1711 );\r
1712 //\r
1713 // Copy the node to Boot script table\r
1714 //\r
02f49fc2
SZ
1715 CopyMem((VOID*)Script, (VOID*)TempBootScriptEntry, ScriptHeader.Length);\r
1716\r
1717 SyncBootScript (Script);\r
1718\r
64d14edf 1719 //\r
1720 // return out the Position\r
1721 //\r
1722 if (Position != NULL) {\r
1723 *Position = Script;\r
1724 }\r
1725 return RETURN_SUCCESS;\r
1726}\r
1727/**\r
1728 Create a Label node in the boot script table. \r
1729 \r
1730 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position\r
1731 in the boot script table specified by Position. If Position is NULL or points to\r
1732 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end\r
1733 of the table (if FALSE).\r
1734 @param Position On entry, specifies the position in the boot script table where the opcode will be\r
1735 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies\r
1736 the position of the inserted opcode in the boot script table. \r
1737 @param InformationLength Length of the label in bytes\r
1738 @param Information Label to be logged in the boot scrpit\r
1739 \r
1740 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.\r
1741 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1742 @retval RETURN_SUCCESS Opcode is added.\r
1743\r
1744**/\r
1745RETURN_STATUS\r
1746EFIAPI\r
1747S3BootScriptLabelInternal (\r
1748 IN BOOLEAN BeforeOrAfter,\r
1749 IN OUT VOID **Position OPTIONAL, \r
1750 IN UINT32 InformationLength, \r
1751 IN CONST CHAR8 *Information\r
1752 )\r
1753{\r
1754 UINT8 Length;\r
1755 UINT8 *Script;\r
64d14edf 1756 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;\r
1757 \r
1758 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);\r
1759 \r
1760 Script = S3BootScriptGetEntryAddAddress (Length);\r
1761 if (Script == NULL) {\r
1762 return RETURN_OUT_OF_RESOURCES;\r
1763 }\r
64d14edf 1764 //\r
1765 // Build script data\r
1766 //\r
1767 ScriptInformation.OpCode = S3_BOOT_SCRIPT_LIB_LABEL_OPCODE;\r
1768 ScriptInformation.Length = Length;\r
1769\r
1770\r
1771 ScriptInformation.InformationLength = InformationLength; \r
1772\r
93b21ade
SZ
1773 CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
1774 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);\r
1775\r
02f49fc2
SZ
1776 SyncBootScript (Script);\r
1777\r
64d14edf 1778 return S3BootScriptMoveLastOpcode (BeforeOrAfter, Position);\r
1779\r
1780}\r
1781/**\r
1782 Find a label within the boot script table and, if not present, optionally create it.\r
1783\r
1784 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE)\r
1785 or after (FALSE) the position in the boot script table \r
1786 specified by Position.\r
1787 @param CreateIfNotFound Specifies whether the label will be created if the label \r
1788 does not exists (TRUE) or not (FALSE).\r
1789 @param Position On entry, specifies the position in the boot script table\r
1790 where the opcode will be inserted, either before or after,\r
1791 depending on BeforeOrAfter. On exit, specifies the position\r
1792 of the inserted opcode in the boot script table.\r
1793 @param Label Points to the label which will be inserted in the boot script table.\r
1794\r
1795 @retval EFI_SUCCESS The operation succeeded. A record was added into the\r
1796 specified script table.\r
1797 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.\r
1798 If the opcode is unknow or not supported because of the PCD \r
1799 Feature Flags.\r
1800 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.\r
1801\r
1802**/\r
1803RETURN_STATUS\r
1804EFIAPI \r
1805S3BootScriptLabel (\r
1806 IN BOOLEAN BeforeOrAfter,\r
1807 IN BOOLEAN CreateIfNotFound,\r
1808 IN OUT VOID **Position OPTIONAL,\r
1809 IN CONST CHAR8 *Label\r
1810 )\r
1811{\r
1812 UINT8* Script;\r
1813 UINTN StartAddress;\r
1814 UINT32 TableLength;\r
1815 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;\r
1816 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader;\r
1817 UINT32 LabelLength;\r
1818 //\r
96072947 1819 // Check NULL Label\r
64d14edf 1820 //\r
96072947
JY
1821 if (Label == NULL) {\r
1822 return EFI_INVALID_PARAMETER;\r
1823 }\r
1824 //\r
1825 // Check empty Label\r
1826 //\r
1827 if (Label[0] == '\0') {\r
64d14edf 1828 return EFI_INVALID_PARAMETER;\r
1829 }\r
1830 \r
1831 //\r
02f49fc2
SZ
1832 // Check that the script is initialized and synced without adding an entry to the script.\r
1833 // The code must search for the label first before it knows if a new entry needs\r
64d14edf 1834 // to be added.\r
1835 //\r
1836 Script = S3BootScriptGetEntryAddAddress (0);\r
1837 if (Script == NULL) {\r
1838 return RETURN_OUT_OF_RESOURCES;\r
1839 }\r
1840 \r
1841 //\r
1842 // Check the header and search for existing label.\r
1843 // \r
1844 Script = mS3BootScriptTablePtr->TableBase;\r
1845 CopyMem ((VOID*)&TableHeader, Script, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
1846 if (TableHeader.OpCode != S3_BOOT_SCRIPT_LIB_TABLE_OPCODE) {\r
1847 return EFI_INVALID_PARAMETER;\r
1848 }\r
1849 StartAddress = (UINTN) Script;\r
1850 TableLength = mS3BootScriptTablePtr->TableLength;\r
1851 Script = Script + TableHeader.Length;\r
1852 while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {\r
1853 \r
1854 CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
1855 if (ScriptHeader.OpCode == S3_BOOT_SCRIPT_LIB_LABEL_OPCODE) {\r
1856 if (AsciiStrCmp ((CHAR8 *)(UINTN)(Script+sizeof(EFI_BOOT_SCRIPT_INFORMATION)), Label) == 0) {\r
1857 (*Position) = Script; \r
1858 return EFI_SUCCESS;\r
1859 }\r
1860 } \r
1861 Script = Script + ScriptHeader.Length;\r
1862 }\r
1863 if (CreateIfNotFound) {\r
1864 LabelLength = (UINT32)AsciiStrSize(Label);\r
1865 return S3BootScriptLabelInternal (BeforeOrAfter,Position, LabelLength, Label); \r
1866 } else {\r
1867 return EFI_NOT_FOUND;\r
1868 } \r
1869}\r
1870\r
1871/**\r
1872 Compare two positions in the boot script table and return their relative position.\r
1873 @param Position1 The positions in the boot script table to compare\r
1874 @param Position2 The positions in the boot script table to compare\r
1875 @param RelativePosition On return, points to the result of the comparison\r
1876\r
1877 @retval EFI_SUCCESS The operation succeeded. A record was added into the\r
1878 specified script table.\r
1879 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.\r
1880 If the opcode is unknow or not supported because of the PCD \r
1881 Feature Flags.\r
1882 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.\r
1883\r
1884**/\r
1885RETURN_STATUS\r
1886EFIAPI \r
1887S3BootScriptCompare (\r
1888 IN UINT8 *Position1,\r
1889 IN UINT8 *Position2,\r
1890 OUT UINTN *RelativePosition\r
1891 )\r
1892{\r
1893 UINT8* Script;\r
1894 UINT32 TableLength; \r
1895\r
64d14edf 1896 if (RelativePosition == NULL) {\r
1897 return EFI_INVALID_PARAMETER;\r
1898 }\r
02f49fc2
SZ
1899\r
1900 //\r
1901 // Check that the script is initialized and synced without adding an entry to the script.\r
1902 //\r
1903 Script = S3BootScriptGetEntryAddAddress (0);\r
1904 if (Script == NULL) {\r
1905 return RETURN_OUT_OF_RESOURCES;\r
1906 }\r
1907 Script = mS3BootScriptTablePtr->TableBase;\r
1908\r
64d14edf 1909 //\r
879dbe18 1910 // mS3BootScriptTablePtr->TableLength does not include the termination node, so add it up\r
64d14edf 1911 //\r
879dbe18 1912 TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
64d14edf 1913 if (Position1 < Script || Position1 > Script+TableLength) {\r
1914 return EFI_INVALID_PARAMETER;\r
1915 }\r
1916 if (Position2 < Script || Position2 > Script+TableLength) {\r
1917 return EFI_INVALID_PARAMETER;\r
1918 }\r
1919 *RelativePosition = (Position1 < Position2)?-1:((Position1 == Position2)?0:1);\r
1920 \r
1921 return EFI_SUCCESS;\r
1922}\r
1923\r