2 Save the S3 data to S3 boot script.
4 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalBootScriptLib.h"
15 +------------------------------+<------- PcdS3BootScriptTablePrivateDataPtr
16 | SCRIPT_TABLE_PRIVATE_DATA | (mS3BootScriptTablePtr, Before SmmReadyToLock)
17 | TableBase |--- PcdS3BootScriptTablePrivateSmmDataPtr
18 | TableLength |--|-- (mS3BootScriptTablePtr = mS3BootScriptTableSmmPtr, After SmmReadyToLock InSmm)
19 | TableMemoryPageNumber |--|-|----
22 | BootTimeScriptLength |--|-|---|---
24 | BackFromS3 | | | | |
25 +------------------------------+ | | | |
27 +------------------------------+<-- | | |
28 | EFI_BOOT_SCRIPT_TABLE_HEADER | | | |
29 | TableLength |----|-- | |
30 +------------------------------+ | | | |
32 +------------------------------+<---- | | |
33 | EFI_BOOT_SCRIPT_TERMINATE | | | |
34 +------------------------------+<------ | |
37 mBootScriptDataBootTimeGuid LockBox: | |
38 Used to restore data after back from S3| |
39 to handle potential INSERT boot script | |
41 +------------------------------+ | |
42 | Boot Time Boot Script | | |
43 | Before SmmReadyToLock | | |
46 +------------------------------+ | |
47 | Boot Time Boot Script | | |
48 | After SmmReadyToLock InSmm | | |
50 +------------------------------+<-------|--|
53 mBootScriptDataGuid LockBox: (IN_PLACE) | |
54 Used to restore data at S3 resume. | |
55 +------------------------------+ | |
56 | Boot Time Boot Script | | |
57 | Before SmmReadyToLock | | |
60 +------------------------------+ | |
61 | Boot Time Boot Script | | |
62 | After SmmReadyToLock InSmm | | |
64 +------------------------------+<-------|---
65 | Runtime Boot Script | |
66 | After SmmReadyToLock InSmm | |
67 +------------------------------+ |
69 +------------------------------+<--------
72 mBootScriptTableBaseGuid LockBox: (IN_PLACE)
73 +------------------------------+
74 | mS3BootScriptTablePtr-> |
76 +------------------------------+
79 mBootScriptSmmPrivateDataGuid LockBox: (IN_PLACE)
80 SMM private data with BackFromS3 = TRUE
81 at runtime. S3 will help restore it to
82 tell the Library the system is back from S3.
83 +------------------------------+
84 | SCRIPT_TABLE_PRIVATE_DATA |
87 | TableMemoryPageNumber |
90 | BootTimeScriptLength |
93 +------------------------------+
97 SCRIPT_TABLE_PRIVATE_DATA
*mS3BootScriptTablePtr
;
100 // Allocate SMM copy because we can not use mS3BootScriptTablePtr after SmmReadyToLock in InSmm.
102 SCRIPT_TABLE_PRIVATE_DATA
*mS3BootScriptTableSmmPtr
;
104 EFI_GUID mBootScriptDataGuid
= {
105 0xaea6b965, 0xdcf5, 0x4311, { 0xb4, 0xb8, 0xf, 0x12, 0x46, 0x44, 0x94, 0xd2 }
108 EFI_GUID mBootScriptDataBootTimeGuid
= {
109 0xb5af1d7a, 0xb8cf, 0x4eb3, { 0x89, 0x25, 0xa8, 0x20, 0xe1, 0x6b, 0x68, 0x7d }
112 EFI_GUID mBootScriptTableBaseGuid
= {
113 0x1810ab4a, 0x2314, 0x4df6, { 0x81, 0xeb, 0x67, 0xc6, 0xec, 0x5, 0x85, 0x91 }
116 EFI_GUID mBootScriptSmmPrivateDataGuid
= {
117 0x627ee2da, 0x3bf9, 0x439b, { 0x92, 0x9f, 0x2e, 0xe, 0x6e, 0x9d, 0xba, 0x62 }
120 EFI_EVENT mEventDxeSmmReadyToLock
= NULL
;
121 VOID
*mRegistrationSmmExitBootServices
= NULL
;
122 VOID
*mRegistrationSmmLegacyBoot
= NULL
;
123 VOID
*mRegistrationSmmReadyToLock
= NULL
;
124 BOOLEAN mS3BootScriptTableAllocated
= FALSE
;
125 BOOLEAN mS3BootScriptTableSmmAllocated
= FALSE
;
126 EFI_SMM_SYSTEM_TABLE2
*mBootScriptSmst
= NULL
;
127 BOOLEAN mS3BootScriptAcpiS3Enable
= TRUE
;
130 This is an internal function to add a terminate node the entry, recalculate the table
131 length and fill into the table.
133 @return the base address of the boot script table.
136 S3BootScriptInternalCloseTable (
141 EFI_BOOT_SCRIPT_TERMINATE ScriptTerminate
;
142 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
144 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
146 if (S3TableBase
== NULL
) {
148 // the table is not exist
154 // Append the termination entry.
156 ScriptTerminate
.OpCode
= S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE
;
157 ScriptTerminate
.Length
= (UINT8
)sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
158 CopyMem (mS3BootScriptTablePtr
->TableBase
+ mS3BootScriptTablePtr
->TableLength
, &ScriptTerminate
, sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
160 // fill the table length
162 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)(mS3BootScriptTablePtr
->TableBase
);
163 ScriptTableInfo
->TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
167 // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to
168 // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE).
169 // Because maybe after SmmReadyToLock, we still need add entries into the table,
170 // and the entry should be added start before this TERMINATE node.
175 This function save boot script data to LockBox.
179 SaveBootScriptDataToLockBox (
186 // Save whole memory copy into LockBox.
187 // It will be used to restore data at S3 resume.
189 Status
= SaveLockBox (
190 &mBootScriptDataGuid
,
191 (VOID
*)mS3BootScriptTablePtr
->TableBase
,
192 EFI_PAGES_TO_SIZE (mS3BootScriptTablePtr
->TableMemoryPageNumber
)
194 ASSERT_EFI_ERROR (Status
);
196 Status
= SetLockBoxAttributes (&mBootScriptDataGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
197 ASSERT_EFI_ERROR (Status
);
200 // Just need save TableBase.
201 // Do not update other field because they will NOT be used in S3.
203 Status
= SaveLockBox (
204 &mBootScriptTableBaseGuid
,
205 (VOID
*)&mS3BootScriptTablePtr
->TableBase
,
206 sizeof (mS3BootScriptTablePtr
->TableBase
)
208 ASSERT_EFI_ERROR (Status
);
210 Status
= SetLockBoxAttributes (&mBootScriptTableBaseGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
211 ASSERT_EFI_ERROR (Status
);
215 This is the Event call back function to notify the Library the system is entering
218 @param Event Pointer to this event
219 @param Context Event handler private data
223 S3BootScriptEventCallBack (
232 // Try to locate it because EfiCreateProtocolNotifyEvent will trigger it once when registration.
233 // Just return if it is not found.
235 Status
= gBS
->LocateProtocol (
236 &gEfiDxeSmmReadyToLockProtocolGuid
,
240 if (EFI_ERROR (Status
)) {
245 // Here we should tell the library that we are entering SmmLocked phase.
246 // and the memory page number occupied by the table should not grow anymore.
248 if (!mS3BootScriptTablePtr
->SmmLocked
) {
250 // Before SmmReadyToLock, we need not write the terminate node when adding a node to boot scipt table
251 // or else, that will impact the performance. However, after SmmReadyToLock, we should append terminate
252 // node on every add to boot script table.
254 S3BootScriptInternalCloseTable ();
255 mS3BootScriptTablePtr
->SmmLocked
= TRUE
;
258 // Save BootScript data to lockbox
260 SaveBootScriptDataToLockBox ();
265 This is the Event call back function is triggered in SMM to notify the Library
266 the system is entering SmmLocked phase and set InSmm flag.
268 @param Protocol Points to the protocol's unique identifier
269 @param Interface Points to the interface instance
270 @param Handle The handle on which the interface was installed
272 @retval EFI_SUCCESS SmmEventCallback runs successfully
276 S3BootScriptSmmEventCallBack (
277 IN CONST EFI_GUID
*Protocol
,
283 // Check if it is already done
285 if (mS3BootScriptTablePtr
== mS3BootScriptTableSmmPtr
) {
290 // Last chance to call-out, just make sure SmmLocked is set.
292 S3BootScriptEventCallBack (NULL
, NULL
);
295 // Save a SMM copy. If TableBase is NOT null, it means SMM copy has been ready, skip copy mem.
297 if (mS3BootScriptTableSmmPtr
->TableBase
== NULL
) {
298 CopyMem (mS3BootScriptTableSmmPtr
, mS3BootScriptTablePtr
, sizeof (*mS3BootScriptTablePtr
));
301 // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.
302 // InSmm will only be checked if SmmLocked is TRUE.
304 mS3BootScriptTableSmmPtr
->InSmm
= TRUE
;
308 // We should not use ACPI Reserved copy, because it is not safe.
310 mS3BootScriptTablePtr
= mS3BootScriptTableSmmPtr
;
316 This function is to save boot time boot script data to LockBox.
318 Because there may be INSERT boot script at runtime in SMM.
319 The boot time copy will be used to restore data after back from S3.
320 Otherwise the data inserted may cause some boot time boot script data lost
321 if only BootScriptData used.
325 SaveBootTimeDataToLockBox (
332 // ACPI Reserved copy is not safe, restore from BootScriptData LockBox first,
333 // and then save the data to BootScriptDataBootTime LockBox.
335 Status
= RestoreLockBox (
336 &mBootScriptDataGuid
,
340 ASSERT_EFI_ERROR (Status
);
343 // Save BootScriptDataBootTime
344 // It will be used to restore data after back from S3.
346 Status
= SaveLockBox (
347 &mBootScriptDataBootTimeGuid
,
348 (VOID
*)mS3BootScriptTablePtr
->TableBase
,
349 mS3BootScriptTablePtr
->BootTimeScriptLength
351 ASSERT_EFI_ERROR (Status
);
355 This function save boot script SMM private data to LockBox with BackFromS3 = TRUE at runtime.
356 S3 resume will help restore it to tell the Library the system is back from S3.
360 SaveSmmPriviateDataToLockBoxAtRuntime (
367 // Save boot script SMM private data with BackFromS3 = TRUE.
369 mS3BootScriptTablePtr
->BackFromS3
= TRUE
;
370 Status
= SaveLockBox (
371 &mBootScriptSmmPrivateDataGuid
,
372 (VOID
*)mS3BootScriptTablePtr
,
373 sizeof (SCRIPT_TABLE_PRIVATE_DATA
)
375 ASSERT_EFI_ERROR (Status
);
377 Status
= SetLockBoxAttributes (&mBootScriptSmmPrivateDataGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
378 ASSERT_EFI_ERROR (Status
);
381 // Set BackFromS3 flag back to FALSE to indicate that now is not back from S3.
383 mS3BootScriptTablePtr
->BackFromS3
= FALSE
;
387 This is the Event call back function is triggered in SMM to notify the Library
388 the system is entering runtime phase.
390 @param[in] Protocol Points to the protocol's unique identifier
391 @param[in] Interface Points to the interface instance
392 @param[in] Handle The handle on which the interface was installed
394 @retval EFI_SUCCESS SmmAtRuntimeCallBack runs successfully
398 S3BootScriptSmmAtRuntimeCallBack (
399 IN CONST EFI_GUID
*Protocol
,
404 if (!mS3BootScriptTablePtr
->AtRuntime
) {
405 mS3BootScriptTablePtr
->BootTimeScriptLength
= (UINT32
)(mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
406 SaveBootTimeDataToLockBox ();
408 mS3BootScriptTablePtr
->AtRuntime
= TRUE
;
409 SaveSmmPriviateDataToLockBoxAtRuntime ();
417 this function just identify it is a smm driver or non-smm driver linked against
420 @param ImageHandle The firmware allocated handle for the EFI image.
421 @param SystemTable A pointer to the EFI System Table.
423 @retval RETURN_SUCCESS The constructor always returns RETURN_SUCCESS.
428 S3BootScriptLibInitialize (
429 IN EFI_HANDLE ImageHandle
,
430 IN EFI_SYSTEM_TABLE
*SystemTable
434 SCRIPT_TABLE_PRIVATE_DATA
*S3TablePtr
;
435 SCRIPT_TABLE_PRIVATE_DATA
*S3TableSmmPtr
;
437 EFI_SMM_BASE2_PROTOCOL
*SmmBase2
;
439 EFI_PHYSICAL_ADDRESS Buffer
;
441 if (!PcdGetBool (PcdAcpiS3Enable
)) {
442 mS3BootScriptAcpiS3Enable
= FALSE
;
443 DEBUG ((DEBUG_INFO
, "%a: Skip S3BootScript because ACPI S3 disabled.\n", gEfiCallerBaseName
));
444 return RETURN_SUCCESS
;
447 S3TablePtr
= (SCRIPT_TABLE_PRIVATE_DATA
*)(UINTN
)PcdGet64 (PcdS3BootScriptTablePrivateDataPtr
);
449 // The Boot script private data is not be initialized. create it
451 if (S3TablePtr
== 0) {
452 Buffer
= SIZE_4GB
- 1;
453 Status
= gBS
->AllocatePages (
455 EfiReservedMemoryType
,
456 EFI_SIZE_TO_PAGES (sizeof (SCRIPT_TABLE_PRIVATE_DATA
)),
459 ASSERT_EFI_ERROR (Status
);
460 mS3BootScriptTableAllocated
= TRUE
;
461 S3TablePtr
= (VOID
*)(UINTN
)Buffer
;
463 Status
= PcdSet64S (PcdS3BootScriptTablePrivateDataPtr
, (UINT64
)(UINTN
)S3TablePtr
);
464 ASSERT_EFI_ERROR (Status
);
465 ZeroMem (S3TablePtr
, sizeof (SCRIPT_TABLE_PRIVATE_DATA
));
467 // Create event to notify the library system enter the SmmLocked phase.
469 mEventDxeSmmReadyToLock
= EfiCreateProtocolNotifyEvent (
470 &gEfiDxeSmmReadyToLockProtocolGuid
,
472 S3BootScriptEventCallBack
,
476 ASSERT (mEventDxeSmmReadyToLock
!= NULL
);
479 mS3BootScriptTablePtr
= S3TablePtr
;
482 // Get InSmm, we need to register SmmReadyToLock if this library is linked to SMM driver.
484 Status
= gBS
->LocateProtocol (&gEfiSmmBase2ProtocolGuid
, NULL
, (VOID
**)&SmmBase2
);
485 if (EFI_ERROR (Status
)) {
486 return RETURN_SUCCESS
;
489 Status
= SmmBase2
->InSmm (SmmBase2
, &InSmm
);
490 if (EFI_ERROR (Status
)) {
491 return RETURN_SUCCESS
;
495 return RETURN_SUCCESS
;
499 // Good, we are in SMM
501 Status
= SmmBase2
->GetSmstLocation (SmmBase2
, &mBootScriptSmst
);
502 if (EFI_ERROR (Status
)) {
503 return RETURN_SUCCESS
;
506 S3TableSmmPtr
= (SCRIPT_TABLE_PRIVATE_DATA
*)(UINTN
)PcdGet64 (PcdS3BootScriptTablePrivateSmmDataPtr
);
508 // The Boot script private data in SMM is not be initialized. create it
510 if (S3TableSmmPtr
== 0) {
511 Status
= mBootScriptSmst
->SmmAllocatePool (
512 EfiRuntimeServicesData
,
513 sizeof (SCRIPT_TABLE_PRIVATE_DATA
),
514 (VOID
**)&S3TableSmmPtr
516 ASSERT_EFI_ERROR (Status
);
517 mS3BootScriptTableSmmAllocated
= TRUE
;
519 Status
= PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr
, (UINT64
)(UINTN
)S3TableSmmPtr
);
520 ASSERT_EFI_ERROR (Status
);
521 ZeroMem (S3TableSmmPtr
, sizeof (SCRIPT_TABLE_PRIVATE_DATA
));
524 // Register SmmExitBootServices and SmmLegacyBoot notification.
526 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
527 &gEdkiiSmmExitBootServicesProtocolGuid
,
528 S3BootScriptSmmAtRuntimeCallBack
,
529 &mRegistrationSmmExitBootServices
531 ASSERT_EFI_ERROR (Status
);
533 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
534 &gEdkiiSmmLegacyBootProtocolGuid
,
535 S3BootScriptSmmAtRuntimeCallBack
,
536 &mRegistrationSmmLegacyBoot
538 ASSERT_EFI_ERROR (Status
);
541 mS3BootScriptTableSmmPtr
= S3TableSmmPtr
;
544 // Register SmmReadyToLock notification.
546 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
547 &gEfiSmmReadyToLockProtocolGuid
,
548 S3BootScriptSmmEventCallBack
,
549 &mRegistrationSmmReadyToLock
551 ASSERT_EFI_ERROR (Status
);
553 return RETURN_SUCCESS
;
557 Library Destructor to free the resources allocated by
558 S3BootScriptLibInitialize() and unregister callbacks.
560 NOTICE: The destructor doesn't support unloading as a separate action, and it
561 only supports unloading if the containing driver's entry point function fails.
563 @param ImageHandle The firmware allocated handle for the EFI image.
564 @param SystemTable A pointer to the EFI System Table.
566 @retval RETURN_SUCCESS The destructor always returns RETURN_SUCCESS.
571 S3BootScriptLibDeinitialize (
572 IN EFI_HANDLE ImageHandle
,
573 IN EFI_SYSTEM_TABLE
*SystemTable
578 if (!mS3BootScriptAcpiS3Enable
) {
579 return RETURN_SUCCESS
;
582 DEBUG ((DEBUG_INFO
, "%a() in %a module\n", __FUNCTION__
, gEfiCallerBaseName
));
584 if (mEventDxeSmmReadyToLock
!= NULL
) {
586 // Close the DxeSmmReadyToLock event.
588 Status
= gBS
->CloseEvent (mEventDxeSmmReadyToLock
);
589 ASSERT_EFI_ERROR (Status
);
592 if (mBootScriptSmst
!= NULL
) {
593 if (mRegistrationSmmExitBootServices
!= NULL
) {
595 // Unregister SmmExitBootServices notification.
597 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
598 &gEdkiiSmmExitBootServicesProtocolGuid
,
600 &mRegistrationSmmExitBootServices
602 ASSERT_EFI_ERROR (Status
);
605 if (mRegistrationSmmLegacyBoot
!= NULL
) {
607 // Unregister SmmLegacyBoot notification.
609 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
610 &gEdkiiSmmLegacyBootProtocolGuid
,
612 &mRegistrationSmmLegacyBoot
614 ASSERT_EFI_ERROR (Status
);
617 if (mRegistrationSmmReadyToLock
!= NULL
) {
619 // Unregister SmmReadyToLock notification.
621 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
622 &gEfiSmmReadyToLockProtocolGuid
,
624 &mRegistrationSmmReadyToLock
626 ASSERT_EFI_ERROR (Status
);
631 // Free the resources allocated and set PCDs to 0.
633 if (mS3BootScriptTableAllocated
) {
634 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)mS3BootScriptTablePtr
, EFI_SIZE_TO_PAGES (sizeof (SCRIPT_TABLE_PRIVATE_DATA
)));
635 ASSERT_EFI_ERROR (Status
);
636 Status
= PcdSet64S (PcdS3BootScriptTablePrivateDataPtr
, 0);
637 ASSERT_EFI_ERROR (Status
);
640 if ((mBootScriptSmst
!= NULL
) && mS3BootScriptTableSmmAllocated
) {
641 Status
= mBootScriptSmst
->SmmFreePool (mS3BootScriptTableSmmPtr
);
642 ASSERT_EFI_ERROR (Status
);
643 Status
= PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr
, 0);
644 ASSERT_EFI_ERROR (Status
);
647 return RETURN_SUCCESS
;
651 To get the start address from which a new boot time s3 boot script entry will write into.
652 If the table is not exist, the functio will first allocate a buffer for the table
653 If the table buffer is not enough for the new entry, in non-smm mode, the funtion will
654 invoke reallocate to enlarge buffer.
656 @param EntryLength the new entry length.
658 @retval the address from which the a new s3 boot script entry will write into
661 S3BootScriptGetBootTimeEntryAddAddress (
665 EFI_PHYSICAL_ADDRESS S3TableBase
;
666 EFI_PHYSICAL_ADDRESS NewS3TableBase
;
671 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
673 S3TableBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(mS3BootScriptTablePtr
->TableBase
);
674 if (S3TableBase
== 0) {
676 // The table is not exist. This is the first to add entry.
677 // Allocate ACPI script table space under 4G memory.
679 S3TableBase
= 0xffffffff;
680 Status
= gBS
->AllocatePages (
682 EfiReservedMemoryType
,
683 2 + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber
),
684 (EFI_PHYSICAL_ADDRESS
*)&S3TableBase
687 if (EFI_ERROR (Status
)) {
688 ASSERT_EFI_ERROR (Status
);
695 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)(UINTN
)S3TableBase
;
696 ScriptTableInfo
->OpCode
= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
;
697 ScriptTableInfo
->Length
= (UINT8
)sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
698 ScriptTableInfo
->Version
= BOOT_SCRIPT_TABLE_VERSION
;
699 ScriptTableInfo
->TableLength
= 0; // will be calculate at CloseTable
700 mS3BootScriptTablePtr
->TableLength
= sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
701 mS3BootScriptTablePtr
->TableBase
= (UINT8
*)(UINTN
)S3TableBase
;
702 mS3BootScriptTablePtr
->TableMemoryPageNumber
= (UINT16
)(2 + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber
));
705 // Here we do not count the reserved memory for runtime script table.
706 PageNumber
= (UINT16
)(mS3BootScriptTablePtr
->TableMemoryPageNumber
- PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber
));
707 TableLength
= mS3BootScriptTablePtr
->TableLength
;
708 if (EFI_PAGES_TO_SIZE ((UINTN
)PageNumber
) < (TableLength
+ EntryLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
))) {
710 // The buffer is too small to hold the table, Reallocate the buffer
712 NewS3TableBase
= 0xffffffff;
713 Status
= gBS
->AllocatePages (
715 EfiReservedMemoryType
,
716 2 + PageNumber
+ PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber
),
717 (EFI_PHYSICAL_ADDRESS
*)&NewS3TableBase
720 if (EFI_ERROR (Status
)) {
721 ASSERT_EFI_ERROR (Status
);
725 CopyMem ((VOID
*)(UINTN
)NewS3TableBase
, (VOID
*)(UINTN
)S3TableBase
, TableLength
);
726 gBS
->FreePages (S3TableBase
, mS3BootScriptTablePtr
->TableMemoryPageNumber
);
728 mS3BootScriptTablePtr
->TableBase
= (UINT8
*)(UINTN
)NewS3TableBase
;
729 mS3BootScriptTablePtr
->TableMemoryPageNumber
= (UINT16
)(2 + PageNumber
+ PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber
));
733 // calculate the the start address for the new entry.
735 NewEntryPtr
= mS3BootScriptTablePtr
->TableBase
+ TableLength
;
738 // update the table lenghth
740 mS3BootScriptTablePtr
->TableLength
= TableLength
+ EntryLength
;
743 // In the boot time, we will not append the termination entry to the boot script
744 // table until the callers think there is no boot time data that should be added and
745 // it is caller's responsibility to explicit call the CloseTable.
753 To get the start address from which a new runtime(after SmmReadyToLock) s3 boot script entry will write into.
754 In this case, it should be ensured that there is enough buffer to hold the entry.
756 @param EntryLength the new entry length.
758 @retval the address from which the a new s3 runtime(after SmmReadyToLock) script entry will write into
761 S3BootScriptGetRuntimeEntryAddAddress (
769 // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node.
771 if ((mS3BootScriptTablePtr
->TableLength
+ EntryLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
)) <= EFI_PAGES_TO_SIZE ((UINTN
)(mS3BootScriptTablePtr
->TableMemoryPageNumber
))) {
772 NewEntryPtr
= mS3BootScriptTablePtr
->TableBase
+ mS3BootScriptTablePtr
->TableLength
;
773 mS3BootScriptTablePtr
->TableLength
= mS3BootScriptTablePtr
->TableLength
+ EntryLength
;
775 // Append a terminate node on every insert
777 S3BootScriptInternalCloseTable ();
780 return (UINT8
*)NewEntryPtr
;
784 This function is to restore boot time boot script data from LockBox.
788 RestoreBootTimeDataFromLockBox (
796 // Restore boot time boot script data from LockBox.
798 LockBoxLength
= mS3BootScriptTablePtr
->BootTimeScriptLength
;
799 Status
= RestoreLockBox (
800 &mBootScriptDataBootTimeGuid
,
801 (VOID
*)mS3BootScriptTablePtr
->TableBase
,
804 ASSERT_EFI_ERROR (Status
);
807 // Update the data to BootScriptData LockBox.
809 Status
= UpdateLockBox (
810 &mBootScriptDataGuid
,
812 (VOID
*)mS3BootScriptTablePtr
->TableBase
,
815 ASSERT_EFI_ERROR (Status
);
818 // Update TableLength.
820 mS3BootScriptTablePtr
->TableLength
= (UINT32
)(mS3BootScriptTablePtr
->BootTimeScriptLength
- sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
824 To get the start address from which a new s3 boot script entry will write into.
826 @param EntryLength the new entry length.
828 @retval the address from which the a new s3 boot script entry will write into
831 S3BootScriptGetEntryAddAddress (
837 if (!mS3BootScriptAcpiS3Enable
) {
841 if (mS3BootScriptTablePtr
->SmmLocked
) {
843 // We need check InSmm, because after SmmReadyToLock, only SMM driver is allowed to write boot script.
845 if (!mS3BootScriptTablePtr
->InSmm
) {
847 // Add DEBUG ERROR, so that we can find it after SmmReadyToLock.
848 // Do not use ASSERT, because we may have test to invoke this interface.
850 DEBUG ((DEBUG_ERROR
, "FATAL ERROR: Set boot script outside SMM after SmmReadyToLock!!!\n"));
854 if (mS3BootScriptTablePtr
->BackFromS3
) {
856 // Back from S3, restore boot time boot script data from LockBox
857 // and set BackFromS3 flag back to FALSE.
859 RestoreBootTimeDataFromLockBox ();
860 mS3BootScriptTablePtr
->BackFromS3
= FALSE
;
863 NewEntryPtr
= S3BootScriptGetRuntimeEntryAddAddress (EntryLength
);
865 NewEntryPtr
= S3BootScriptGetBootTimeEntryAddAddress (EntryLength
);
872 Sync BootScript LockBox data.
874 @param Script The address from where the boot script has been added or updated.
884 UINT32 TotalScriptLength
;
886 if (!mS3BootScriptTablePtr
->SmmLocked
|| !mS3BootScriptTablePtr
->InSmm
) {
888 // If it is not after SmmReadyToLock in SMM,
894 ScriptOffset
= (UINT32
)(Script
- mS3BootScriptTablePtr
->TableBase
);
896 TotalScriptLength
= (UINT32
)(mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
899 // Update BootScriptData
900 // So in S3 resume, the data can be restored correctly.
902 Status
= UpdateLockBox (
903 &mBootScriptDataGuid
,
905 (VOID
*)((UINTN
)mS3BootScriptTablePtr
->TableBase
+ ScriptOffset
),
906 TotalScriptLength
- ScriptOffset
908 ASSERT_EFI_ERROR (Status
);
911 // Now the length field is updated, need sync to lockbox.
912 // So at S3 resume, the data can be restored correctly.
914 Status
= UpdateLockBox (
915 &mBootScriptDataGuid
,
916 OFFSET_OF (EFI_BOOT_SCRIPT_TABLE_HEADER
, TableLength
),
918 sizeof (TotalScriptLength
)
920 ASSERT_EFI_ERROR (Status
);
924 This is an function to close the S3 boot script table. The function could only be called in
925 BOOT time phase. To comply with the Framework spec definition on
926 EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable(), this function will fulfill following things:
927 1. Closes the specified boot script table
928 2. It allocates a new memory pool to duplicate all the boot scripts in the specified table.
929 Once this function is called, the table maintained by the library will be destroyed
930 after it is copied into the allocated pool.
931 3. Any attempts to add a script record after calling this function will cause a new table
932 to be created by the library.
933 4. The base address of the allocated pool will be returned in Address. Note that after
934 using the boot script table, the CALLER is responsible for freeing the pool that is allocated
937 In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is
938 for Framework Spec compatibility.
940 If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out
941 how to get the script to run at S3 resume because the boot script maintained by the lib will be
944 @return the base address of the new copy of the boot script table.
945 @note this function could only called in boot time phase
950 S3BootScriptCloseTable (
958 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
960 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
961 if (S3TableBase
== 0) {
966 // Append the termination record the S3 boot script table
968 S3BootScriptInternalCloseTable ();
969 TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
971 // Allocate the buffer and copy the boot script to the buffer.
973 Status
= gBS
->AllocatePool (
978 if (EFI_ERROR (Status
)) {
982 CopyMem (Buffer
, S3TableBase
, TableLength
);
985 // Destroy the table maintained by the library so that the next write operation
986 // will write the record to the first entry of the table.
988 // Fill the table header.
989 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)S3TableBase
;
990 ScriptTableInfo
->OpCode
= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
;
991 ScriptTableInfo
->Length
= (UINT8
)sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
992 ScriptTableInfo
->TableLength
= 0; // will be calculate at close the table
994 mS3BootScriptTablePtr
->TableLength
= sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
999 Save I/O write to boot script
1001 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1002 @param Address The base address of the I/O operations.
1003 @param Count The number of I/O operations to perform.
1004 @param Buffer The source buffer from which to write data.
1006 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1007 @retval RETURN_SUCCESS Opcode is added.
1011 S3BootScriptSaveIoWrite (
1012 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1022 EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite
;
1024 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1029 if ((Count
> MAX_UINT8
) ||
1030 (WidthInByte
* Count
> MAX_UINT8
- sizeof (EFI_BOOT_SCRIPT_IO_WRITE
)))
1032 return RETURN_OUT_OF_RESOURCES
;
1035 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE
) + (WidthInByte
* Count
));
1037 Script
= S3BootScriptGetEntryAddAddress (Length
);
1038 if (Script
== NULL
) {
1039 return RETURN_OUT_OF_RESOURCES
;
1045 ScriptIoWrite
.OpCode
= EFI_BOOT_SCRIPT_IO_WRITE_OPCODE
;
1046 ScriptIoWrite
.Length
= Length
;
1047 ScriptIoWrite
.Width
= Width
;
1048 ScriptIoWrite
.Address
= Address
;
1049 ScriptIoWrite
.Count
= (UINT32
)Count
;
1050 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoWrite
, sizeof (EFI_BOOT_SCRIPT_IO_WRITE
));
1051 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_WRITE
)), Buffer
, WidthInByte
* Count
);
1053 SyncBootScript (Script
);
1055 return RETURN_SUCCESS
;
1059 Adds a record for an I/O modify operation into a S3 boot script table
1061 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1062 @param Address The base address of the I/O operations.
1063 @param Data A pointer to the data to be OR-ed.
1064 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register
1066 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1067 @retval RETURN_SUCCESS Opcode is added.
1071 S3BootScriptSaveIoReadWrite (
1072 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1081 EFI_BOOT_SCRIPT_IO_READ_WRITE ScriptIoReadWrite
;
1083 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1084 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
) + (WidthInByte
* 2));
1086 Script
= S3BootScriptGetEntryAddAddress (Length
);
1087 if (Script
== NULL
) {
1088 return RETURN_OUT_OF_RESOURCES
;
1092 // Build script data
1094 ScriptIoReadWrite
.OpCode
= EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE
;
1095 ScriptIoReadWrite
.Length
= Length
;
1096 ScriptIoReadWrite
.Width
= Width
;
1097 ScriptIoReadWrite
.Address
= Address
;
1099 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoReadWrite
, sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
));
1100 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
)), Data
, WidthInByte
);
1101 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
) + WidthInByte
), DataMask
, WidthInByte
);
1103 SyncBootScript (Script
);
1105 return RETURN_SUCCESS
;
1109 Adds a record for a memory write operation into a specified boot script table.
1111 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1112 @param Address The base address of the memory operations
1113 @param Count The number of memory operations to perform.
1114 @param Buffer The source buffer from which to write the data.
1116 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1117 @retval RETURN_SUCCESS Opcode is added.
1121 S3BootScriptSaveMemWrite (
1122 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1131 EFI_BOOT_SCRIPT_MEM_WRITE ScriptMemWrite
;
1133 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1138 if ((Count
> MAX_UINT8
) ||
1139 (WidthInByte
* Count
> MAX_UINT8
- sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
)))
1141 return RETURN_OUT_OF_RESOURCES
;
1144 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
) + (WidthInByte
* Count
));
1146 Script
= S3BootScriptGetEntryAddAddress (Length
);
1147 if (Script
== NULL
) {
1148 return RETURN_OUT_OF_RESOURCES
;
1152 // Build script data
1154 ScriptMemWrite
.OpCode
= EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE
;
1155 ScriptMemWrite
.Length
= Length
;
1156 ScriptMemWrite
.Width
= Width
;
1157 ScriptMemWrite
.Address
= Address
;
1158 ScriptMemWrite
.Count
= (UINT32
)Count
;
1160 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemWrite
, sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
));
1161 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
)), Buffer
, WidthInByte
* Count
);
1163 SyncBootScript (Script
);
1165 return RETURN_SUCCESS
;
1169 Adds a record for a memory modify operation into a specified boot script table.
1171 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1172 @param Address The base address of the memory operations. Address needs alignment if required
1173 @param Data A pointer to the data to be OR-ed.
1174 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register.
1176 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1177 @retval RETURN_SUCCESS Opcode is added.
1181 S3BootScriptSaveMemReadWrite (
1182 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1191 EFI_BOOT_SCRIPT_MEM_READ_WRITE ScriptMemReadWrite
;
1193 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1194 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
) + (WidthInByte
* 2));
1196 Script
= S3BootScriptGetEntryAddAddress (Length
);
1197 if (Script
== NULL
) {
1198 return RETURN_OUT_OF_RESOURCES
;
1202 // Build script data
1204 ScriptMemReadWrite
.OpCode
= EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE
;
1205 ScriptMemReadWrite
.Length
= Length
;
1206 ScriptMemReadWrite
.Width
= Width
;
1207 ScriptMemReadWrite
.Address
= Address
;
1209 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemReadWrite
, sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
));
1210 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
)), Data
, WidthInByte
);
1211 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
) + WidthInByte
), DataMask
, WidthInByte
);
1213 SyncBootScript (Script
);
1215 return RETURN_SUCCESS
;
1219 Adds a record for a PCI configuration space write operation into a specified boot script table.
1221 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1222 @param Address The address within the PCI configuration space.
1223 @param Count The number of PCI operations to perform.
1224 @param Buffer The source buffer from which to write the data.
1226 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1227 @retval RETURN_SUCCESS Opcode is added.
1228 @note A known Limitations in the implementation which is 64bits operations are not supported.
1233 S3BootScriptSavePciCfgWrite (
1234 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1243 EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE ScriptPciWrite
;
1245 if ((Width
== S3BootScriptWidthUint64
) ||
1246 (Width
== S3BootScriptWidthFifoUint64
) ||
1247 (Width
== S3BootScriptWidthFillUint64
))
1249 return EFI_INVALID_PARAMETER
;
1252 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1257 if ((Count
> MAX_UINT8
) ||
1258 (WidthInByte
* Count
> MAX_UINT8
- sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
)))
1260 return RETURN_OUT_OF_RESOURCES
;
1263 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
) + (WidthInByte
* Count
));
1265 Script
= S3BootScriptGetEntryAddAddress (Length
);
1266 if (Script
== NULL
) {
1267 return RETURN_OUT_OF_RESOURCES
;
1271 // Build script data
1273 ScriptPciWrite
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE
;
1274 ScriptPciWrite
.Length
= Length
;
1275 ScriptPciWrite
.Width
= Width
;
1276 ScriptPciWrite
.Address
= Address
;
1277 ScriptPciWrite
.Count
= (UINT32
)Count
;
1279 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciWrite
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
));
1280 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
)), Buffer
, WidthInByte
* Count
);
1282 SyncBootScript (Script
);
1284 return RETURN_SUCCESS
;
1288 Adds a record for a PCI configuration space modify operation into a specified boot script table.
1290 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1291 @param Address The address within the PCI configuration space.
1292 @param Data A pointer to the data to be OR-ed.The size depends on Width.
1293 @param DataMask A pointer to the data mask to be AND-ed.
1295 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1296 @retval RETURN__SUCCESS Opcode is added.
1297 @note A known Limitations in the implementation which is 64bits operations are not supported.
1302 S3BootScriptSavePciCfgReadWrite (
1303 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1312 EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE ScriptPciReadWrite
;
1314 if ((Width
== S3BootScriptWidthUint64
) ||
1315 (Width
== S3BootScriptWidthFifoUint64
) ||
1316 (Width
== S3BootScriptWidthFillUint64
))
1318 return EFI_INVALID_PARAMETER
;
1321 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1322 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
) + (WidthInByte
* 2));
1324 Script
= S3BootScriptGetEntryAddAddress (Length
);
1325 if (Script
== NULL
) {
1326 return RETURN_OUT_OF_RESOURCES
;
1330 // Build script data
1332 ScriptPciReadWrite
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE
;
1333 ScriptPciReadWrite
.Length
= Length
;
1334 ScriptPciReadWrite
.Width
= Width
;
1335 ScriptPciReadWrite
.Address
= Address
;
1337 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciReadWrite
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
));
1338 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
)), Data
, WidthInByte
);
1340 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
) + WidthInByte
),
1345 SyncBootScript (Script
);
1347 return RETURN_SUCCESS
;
1351 Adds a record for a PCI configuration 2 space write operation into a specified boot script table.
1353 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1354 @param Segment The PCI segment number for Address.
1355 @param Address The address within the PCI configuration space.
1356 @param Count The number of PCI operations to perform.
1357 @param Buffer The source buffer from which to write the data.
1359 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1360 @retval RETURN_SUCCESS Opcode is added.
1361 @note A known Limitations in the implementation which is 64bits operations are not supported.
1366 S3BootScriptSavePciCfg2Write (
1367 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1377 EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE ScriptPciWrite2
;
1379 if ((Width
== S3BootScriptWidthUint64
) ||
1380 (Width
== S3BootScriptWidthFifoUint64
) ||
1381 (Width
== S3BootScriptWidthFillUint64
))
1383 return EFI_INVALID_PARAMETER
;
1386 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1391 if ((Count
> MAX_UINT8
) ||
1392 (WidthInByte
* Count
> MAX_UINT8
- sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
)))
1394 return RETURN_OUT_OF_RESOURCES
;
1397 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
) + (WidthInByte
* Count
));
1399 Script
= S3BootScriptGetEntryAddAddress (Length
);
1400 if (Script
== NULL
) {
1401 return RETURN_OUT_OF_RESOURCES
;
1405 // Build script data
1407 ScriptPciWrite2
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE
;
1408 ScriptPciWrite2
.Length
= Length
;
1409 ScriptPciWrite2
.Width
= Width
;
1410 ScriptPciWrite2
.Address
= Address
;
1411 ScriptPciWrite2
.Segment
= Segment
;
1412 ScriptPciWrite2
.Count
= (UINT32
)Count
;
1414 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciWrite2
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
));
1415 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
)), Buffer
, WidthInByte
* Count
);
1417 SyncBootScript (Script
);
1419 return RETURN_SUCCESS
;
1423 Adds a record for a PCI configuration 2 space modify operation into a specified boot script table.
1425 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1426 @param Segment The PCI segment number for Address.
1427 @param Address The address within the PCI configuration space.
1428 @param Data A pointer to the data to be OR-ed. The size depends on Width.
1429 @param DataMask A pointer to the data mask to be AND-ed.
1431 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1432 @retval RETURN_SUCCESS Opcode is added.
1433 @note A known Limitations in the implementation which is 64bits operations are not supported.
1438 S3BootScriptSavePciCfg2ReadWrite (
1439 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1449 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE ScriptPciReadWrite2
;
1451 if ((Width
== S3BootScriptWidthUint64
) ||
1452 (Width
== S3BootScriptWidthFifoUint64
) ||
1453 (Width
== S3BootScriptWidthFillUint64
))
1455 return EFI_INVALID_PARAMETER
;
1458 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1459 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
) + (WidthInByte
* 2));
1461 Script
= S3BootScriptGetEntryAddAddress (Length
);
1462 if (Script
== NULL
) {
1463 return RETURN_OUT_OF_RESOURCES
;
1467 // Build script data
1469 ScriptPciReadWrite2
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE
;
1470 ScriptPciReadWrite2
.Length
= Length
;
1471 ScriptPciReadWrite2
.Width
= Width
;
1472 ScriptPciReadWrite2
.Segment
= Segment
;
1473 ScriptPciReadWrite2
.Address
= Address
;
1475 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciReadWrite2
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
));
1476 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
)), Data
, WidthInByte
);
1478 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
) + WidthInByte
),
1483 SyncBootScript (Script
);
1485 return RETURN_SUCCESS
;
1489 Checks the parameter of S3BootScriptSaveSmbusExecute().
1491 This function checks the input parameters of SmbusExecute(). If the input parameters are valid
1492 for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain
1493 error code based on the input SMBus bus protocol.
1495 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length,
1497 @param Operation Signifies which particular SMBus hardware protocol instance that
1498 it will use to execute the SMBus transactions. This SMBus
1499 hardware protocol is defined by the SMBus Specification and is
1501 @param Length Signifies the number of bytes that this operation will do. The
1502 maximum number of bytes can be revision specific and operation
1503 specific. This field will contain the actual number of bytes that
1504 are executed for this operation. Not all operations require this
1506 @param Buffer Contains the value of data to execute to the SMBus slave device.
1507 Not all operations require this argument. The length of this
1508 buffer is identified by Length.
1510 @retval EFI_SUCCESS All the parameters are valid for the corresponding SMBus bus
1512 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.
1513 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead
1514 and EfiSmbusQuickWrite. Length is outside the range of valid
1516 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.
1517 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.
1522 IN UINTN SmBusAddress
,
1523 IN EFI_SMBUS_OPERATION Operation
,
1524 IN OUT UINTN
*Length
,
1530 EFI_SMBUS_DEVICE_COMMAND Command
;
1533 Command
= SMBUS_LIB_COMMAND (SmBusAddress
);
1534 PecCheck
= SMBUS_LIB_PEC (SmBusAddress
);
1536 // Set default value to be 2:
1537 // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.
1540 Status
= EFI_SUCCESS
;
1541 switch (Operation
) {
1542 case EfiSmbusQuickRead
:
1543 case EfiSmbusQuickWrite
:
1544 if (PecCheck
|| (Command
!= 0)) {
1545 return EFI_UNSUPPORTED
;
1549 case EfiSmbusReceiveByte
:
1550 case EfiSmbusSendByte
:
1552 return EFI_UNSUPPORTED
;
1556 // Cascade to check length parameter.
1558 case EfiSmbusReadByte
:
1559 case EfiSmbusWriteByte
:
1562 // Cascade to check length parameter.
1564 case EfiSmbusReadWord
:
1565 case EfiSmbusWriteWord
:
1566 case EfiSmbusProcessCall
:
1567 if ((Buffer
== NULL
) || (Length
== NULL
)) {
1568 return EFI_INVALID_PARAMETER
;
1569 } else if (*Length
< RequiredLen
) {
1570 Status
= EFI_BUFFER_TOO_SMALL
;
1573 *Length
= RequiredLen
;
1575 case EfiSmbusReadBlock
:
1576 case EfiSmbusWriteBlock
:
1577 case EfiSmbusBWBRProcessCall
:
1578 if ((Buffer
== NULL
) ||
1580 (*Length
< MIN_SMBUS_BLOCK_LEN
) ||
1581 (*Length
> MAX_SMBUS_BLOCK_LEN
))
1583 return EFI_INVALID_PARAMETER
;
1588 return EFI_INVALID_PARAMETER
;
1595 Adds a record for an SMBus command execution into a specified boot script table.
1597 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length, and PEC.
1598 @param Operation Indicates which particular SMBus protocol it will use to execute the SMBus
1600 @param Length A pointer to signify the number of bytes that this operation will do.
1601 @param Buffer Contains the value of data to execute to the SMBUS slave device.
1603 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1604 @retval RETURN_SUCCESS Opcode is added.
1608 S3BootScriptSaveSmbusExecute (
1609 IN UINTN SmBusAddress
,
1610 IN EFI_SMBUS_OPERATION Operation
,
1619 EFI_BOOT_SCRIPT_SMBUS_EXECUTE ScriptSmbusExecute
;
1621 if (Length
== NULL
) {
1624 BufferLength
= *Length
;
1627 Status
= CheckParameters (SmBusAddress
, Operation
, &BufferLength
, Buffer
);
1628 if (EFI_ERROR (Status
)) {
1635 if (BufferLength
> MAX_UINT8
- sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
)) {
1636 return RETURN_OUT_OF_RESOURCES
;
1639 DataSize
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
) + BufferLength
);
1641 Script
= S3BootScriptGetEntryAddAddress (DataSize
);
1642 if (Script
== NULL
) {
1643 return RETURN_OUT_OF_RESOURCES
;
1647 // Build script data
1649 ScriptSmbusExecute
.OpCode
= EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE
;
1650 ScriptSmbusExecute
.Length
= DataSize
;
1651 ScriptSmbusExecute
.SmBusAddress
= (UINT64
)SmBusAddress
;
1652 ScriptSmbusExecute
.Operation
= Operation
;
1653 ScriptSmbusExecute
.DataSize
= (UINT32
)BufferLength
;
1655 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptSmbusExecute
, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
));
1657 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
)),
1662 SyncBootScript (Script
);
1664 return RETURN_SUCCESS
;
1668 Adds a record for an execution stall on the processor into a specified boot script table.
1670 @param Duration Duration in microseconds of the stall
1672 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1673 @retval RETURN_SUCCESS Opcode is added.
1677 S3BootScriptSaveStall (
1683 EFI_BOOT_SCRIPT_STALL ScriptStall
;
1685 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_STALL
));
1687 Script
= S3BootScriptGetEntryAddAddress (Length
);
1688 if (Script
== NULL
) {
1689 return RETURN_OUT_OF_RESOURCES
;
1693 // Build script data
1695 ScriptStall
.OpCode
= EFI_BOOT_SCRIPT_STALL_OPCODE
;
1696 ScriptStall
.Length
= Length
;
1697 ScriptStall
.Duration
= Duration
;
1699 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptStall
, sizeof (EFI_BOOT_SCRIPT_STALL
));
1701 SyncBootScript (Script
);
1703 return RETURN_SUCCESS
;
1707 Adds a record for dispatching specified arbitrary code into a specified boot script table.
1709 @param EntryPoint Entry point of the code to be dispatched.
1710 @param Context Argument to be passed into the EntryPoint of the code to be dispatched.
1712 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1713 @retval RETURN_SUCCESS Opcode is added.
1717 S3BootScriptSaveDispatch2 (
1718 IN VOID
*EntryPoint
,
1724 EFI_BOOT_SCRIPT_DISPATCH_2 ScriptDispatch2
;
1726 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2
));
1728 Script
= S3BootScriptGetEntryAddAddress (Length
);
1729 if (Script
== NULL
) {
1730 return RETURN_OUT_OF_RESOURCES
;
1734 // Build script data
1736 ScriptDispatch2
.OpCode
= EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE
;
1737 ScriptDispatch2
.Length
= Length
;
1738 ScriptDispatch2
.EntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)EntryPoint
;
1739 ScriptDispatch2
.Context
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)Context
;
1741 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptDispatch2
, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2
));
1743 SyncBootScript (Script
);
1745 return RETURN_SUCCESS
;
1749 Adds a record for memory reads of the memory location and continues when the exit criteria is
1750 satisfied or after a defined duration.
1752 Please aware, below interface is different with PI specification, Vol 5:
1753 EFI_S3_SAVE_STATE_PROTOCOL.Write() for EFI_BOOT_SCRIPT_MEM_POLL_OPCODE.
1754 "Duration" below is microseconds, while "Delay" in PI specification means
1755 the number of 100ns units to poll.
1757 @param Width The width of the memory operations.
1758 @param Address The base address of the memory operations.
1759 @param BitMask A pointer to the bit mask to be AND-ed with the data read from the register.
1760 @param BitValue A pointer to the data value after to be Masked.
1761 @param Duration Duration in microseconds of the stall.
1762 @param LoopTimes The times of the register polling.
1764 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1765 @retval RETURN_SUCCESS Opcode is added.
1770 S3BootScriptSaveMemPoll (
1771 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1782 EFI_BOOT_SCRIPT_MEM_POLL ScriptMemPoll
;
1784 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1786 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL
) + (WidthInByte
* 2));
1788 Script
= S3BootScriptGetEntryAddAddress (Length
);
1789 if (Script
== NULL
) {
1790 return RETURN_OUT_OF_RESOURCES
;
1794 // Build script data
1796 ScriptMemPoll
.OpCode
= EFI_BOOT_SCRIPT_MEM_POLL_OPCODE
;
1797 ScriptMemPoll
.Length
= Length
;
1798 ScriptMemPoll
.Width
= Width
;
1799 ScriptMemPoll
.Address
= Address
;
1800 ScriptMemPoll
.Duration
= Duration
;
1801 ScriptMemPoll
.LoopTimes
= LoopTimes
;
1803 CopyMem ((UINT8
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_POLL
)), BitValue
, WidthInByte
);
1804 CopyMem ((UINT8
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_POLL
) + WidthInByte
), BitMask
, WidthInByte
);
1805 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemPoll
, sizeof (EFI_BOOT_SCRIPT_MEM_POLL
));
1807 SyncBootScript (Script
);
1809 return RETURN_SUCCESS
;
1813 Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only
1814 used for debugging script issues.
1816 @param InformationLength Length of the data in bytes
1817 @param Information Information to be logged in the boot scrpit
1819 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1820 @retval RETURN_SUCCESS Opcode is added.
1825 S3BootScriptSaveInformation (
1826 IN UINT32 InformationLength
,
1827 IN VOID
*Information
1832 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation
;
1837 if (InformationLength
> MAX_UINT8
- sizeof (EFI_BOOT_SCRIPT_INFORMATION
)) {
1838 return RETURN_OUT_OF_RESOURCES
;
1841 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_INFORMATION
) + InformationLength
);
1843 Script
= S3BootScriptGetEntryAddAddress (Length
);
1844 if (Script
== NULL
) {
1845 return RETURN_OUT_OF_RESOURCES
;
1849 // Build script data
1851 ScriptInformation
.OpCode
= EFI_BOOT_SCRIPT_INFORMATION_OPCODE
;
1852 ScriptInformation
.Length
= Length
;
1854 ScriptInformation
.InformationLength
= InformationLength
;
1856 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptInformation
, sizeof (EFI_BOOT_SCRIPT_INFORMATION
));
1857 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_INFORMATION
)), (VOID
*)Information
, (UINTN
)InformationLength
);
1859 SyncBootScript (Script
);
1861 return RETURN_SUCCESS
;
1865 Store a string in the boot script table. This opcode is a no-op on dispatch and is only
1866 used for debugging script issues.
1868 @param String The string to save to boot script table
1870 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1871 @retval RETURN_SUCCESS Opcode is added.
1876 S3BootScriptSaveInformationAsciiString (
1877 IN CONST CHAR8
*String
1880 return S3BootScriptSaveInformation (
1881 (UINT32
)AsciiStrLen (String
) + 1,
1887 Adds a record for dispatching specified arbitrary code into a specified boot script table.
1889 @param EntryPoint Entry point of the code to be dispatched.
1891 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1892 @retval RETURN_SUCCESS Opcode is added.
1896 S3BootScriptSaveDispatch (
1902 EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch
;
1904 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_DISPATCH
));
1906 Script
= S3BootScriptGetEntryAddAddress (Length
);
1907 if (Script
== NULL
) {
1908 return RETURN_OUT_OF_RESOURCES
;
1912 // Build script data
1914 ScriptDispatch
.OpCode
= EFI_BOOT_SCRIPT_DISPATCH_OPCODE
;
1915 ScriptDispatch
.Length
= Length
;
1916 ScriptDispatch
.EntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)EntryPoint
;
1918 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptDispatch
, sizeof (EFI_BOOT_SCRIPT_DISPATCH
));
1920 SyncBootScript (Script
);
1922 return RETURN_SUCCESS
;
1926 Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a
1929 @param Width The width of the I/O operations.
1930 @param Address The base address of the I/O operations.
1931 @param Data The comparison value used for the polling exit criteria.
1932 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1933 in Data are ignored when polling the memory address.
1934 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1935 granularity so the delay may be longer.
1937 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1938 @retval RETURN_SUCCESS Opcode is added.
1943 S3BootScriptSaveIoPoll (
1944 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1954 EFI_BOOT_SCRIPT_IO_POLL ScriptIoPoll
;
1956 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
1957 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + (WidthInByte
* 2));
1959 Script
= S3BootScriptGetEntryAddAddress (Length
);
1960 if (Script
== NULL
) {
1961 return RETURN_OUT_OF_RESOURCES
;
1965 // Build script data
1967 ScriptIoPoll
.OpCode
= EFI_BOOT_SCRIPT_IO_POLL_OPCODE
;
1968 ScriptIoPoll
.Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + (WidthInByte
* 2));
1969 ScriptIoPoll
.Width
= Width
;
1970 ScriptIoPoll
.Address
= Address
;
1971 ScriptIoPoll
.Delay
= Delay
;
1973 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoPoll
, sizeof (EFI_BOOT_SCRIPT_IO_POLL
));
1974 CopyMem ((UINT8
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_POLL
)), Data
, WidthInByte
);
1975 CopyMem ((UINT8
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1977 SyncBootScript (Script
);
1979 return RETURN_SUCCESS
;
1983 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1984 after a defined duration.
1986 @param Width The width of the I/O operations.
1987 @param Address The address within the PCI configuration space.
1988 @param Data The comparison value used for the polling exit criteria.
1989 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1990 in Data are ignored when polling the memory address
1991 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1992 granularity so the delay may be longer.
1994 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1995 @retval RETURN_SUCCESS Opcode is added.
1996 @note A known Limitations in the implementation which is 64bits operations are not supported.
2001 S3BootScriptSavePciPoll (
2002 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
2012 EFI_BOOT_SCRIPT_PCI_CONFIG_POLL ScriptPciPoll
;
2014 if ((Width
== S3BootScriptWidthUint64
) ||
2015 (Width
== S3BootScriptWidthFifoUint64
) ||
2016 (Width
== S3BootScriptWidthFillUint64
))
2018 return EFI_INVALID_PARAMETER
;
2021 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
2022 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + (WidthInByte
* 2));
2024 Script
= S3BootScriptGetEntryAddAddress (Length
);
2025 if (Script
== NULL
) {
2026 return RETURN_OUT_OF_RESOURCES
;
2030 // Build script data
2032 ScriptPciPoll
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE
;
2033 ScriptPciPoll
.Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + (WidthInByte
* 2));
2034 ScriptPciPoll
.Width
= Width
;
2035 ScriptPciPoll
.Address
= Address
;
2036 ScriptPciPoll
.Delay
= Delay
;
2038 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciPoll
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
));
2039 CopyMem ((UINT8
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
)), Data
, WidthInByte
);
2040 CopyMem ((UINT8
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
2042 SyncBootScript (Script
);
2044 return RETURN_SUCCESS
;
2048 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
2049 after a defined duration.
2051 @param Width The width of the I/O operations.
2052 @param Segment The PCI segment number for Address.
2053 @param Address The address within the PCI configuration space.
2054 @param Data The comparison value used for the polling exit criteria.
2055 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
2056 in Data are ignored when polling the memory address
2057 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
2058 granularity so the delay may be longer.
2060 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
2061 @retval RETURN_SUCCESS Opcode is added.
2062 @note A known Limitations in the implementation which is 64bits operations are not supported.
2067 S3BootScriptSavePci2Poll (
2068 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
2079 EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL ScriptPci2Poll
;
2081 if ((Width
== S3BootScriptWidthUint64
) ||
2082 (Width
== S3BootScriptWidthFifoUint64
) ||
2083 (Width
== S3BootScriptWidthFillUint64
))
2085 return EFI_INVALID_PARAMETER
;
2088 WidthInByte
= (UINT8
)(0x01 << (Width
& 0x03));
2089 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + (WidthInByte
* 2));
2091 Script
= S3BootScriptGetEntryAddAddress (Length
);
2092 if (Script
== NULL
) {
2093 return RETURN_OUT_OF_RESOURCES
;
2097 // Build script data
2099 ScriptPci2Poll
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE
;
2100 ScriptPci2Poll
.Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + (WidthInByte
* 2));
2101 ScriptPci2Poll
.Width
= Width
;
2102 ScriptPci2Poll
.Segment
= Segment
;
2103 ScriptPci2Poll
.Address
= Address
;
2104 ScriptPci2Poll
.Delay
= Delay
;
2106 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPci2Poll
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
));
2107 CopyMem ((UINT8
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
)), Data
, WidthInByte
);
2108 CopyMem ((UINT8
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
2110 SyncBootScript (Script
);
2112 return RETURN_SUCCESS
;
2116 Do the calculation of start address from which a new s3 boot script entry will write into.
2118 @param EntryLength The new entry length.
2119 @param Position specifies the position in the boot script table where the opcode will be
2120 inserted, either before or after, depending on BeforeOrAfter.
2121 @param BeforeOrAfter The flag to indicate to insert the nod before or after the position.
2122 This parameter is effective when InsertFlag is TRUE
2123 @param Script return out the position from which the a new s3 boot script entry will write into
2126 S3BootScriptCalculateInsertAddress (
2127 IN UINT8 EntryLength
,
2128 IN VOID
*Position OPTIONAL
,
2129 IN BOOLEAN BeforeOrAfter OPTIONAL
,
2135 UINTN PositionOffset
;
2136 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
2139 // The entry inserting to table is already added to the end of the table
2141 TableLength
= mS3BootScriptTablePtr
->TableLength
- EntryLength
;
2142 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
2144 // calculate the Position offset
2146 if (Position
!= NULL
) {
2147 PositionOffset
= (UINTN
)Position
- (UINTN
)S3TableBase
;
2150 // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.
2152 if (!BeforeOrAfter
) {
2153 CopyMem ((VOID
*)&ScriptHeader
, Position
, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER
));
2154 PositionOffset
+= (ScriptHeader
.Length
);
2158 // Insert the node before the adjusted Position
2160 CopyMem (S3TableBase
+PositionOffset
+EntryLength
, S3TableBase
+PositionOffset
, TableLength
- PositionOffset
);
2162 // calculate the the start address for the new entry.
2164 *Script
= S3TableBase
+ PositionOffset
;
2166 if (!BeforeOrAfter
) {
2168 // Insert the node to the end of the table
2170 *Script
= S3TableBase
+ TableLength
;
2173 // Insert the node to the beginning of the table
2175 PositionOffset
= (UINTN
)sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
2176 CopyMem (S3TableBase
+PositionOffset
+EntryLength
, S3TableBase
+PositionOffset
, TableLength
- PositionOffset
);
2177 *Script
= S3TableBase
+ PositionOffset
;
2183 Move the last boot script entry to the position
2185 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
2186 in the boot script table specified by Position. If Position is NULL or points to
2187 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
2188 of the table (if FALSE).
2189 @param Position On entry, specifies the position in the boot script table where the opcode will be
2190 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
2191 the position of the inserted opcode in the boot script table.
2193 @retval RETURN_OUT_OF_RESOURCES The table is not available.
2194 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
2195 @retval RETURN_SUCCESS Opcode is inserted.
2199 S3BootScriptMoveLastOpcode (
2200 IN BOOLEAN BeforeOrAfter
,
2201 IN OUT VOID
**Position OPTIONAL
2208 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
2209 BOOLEAN ValidatePosition
;
2211 UINT8 TempBootScriptEntry
[BOOT_SCRIPT_NODE_MAX_LENGTH
];
2213 ValidatePosition
= FALSE
;
2214 TempPosition
= (Position
== NULL
) ? NULL
: (*Position
);
2217 // Check that the script is initialized and synced without adding an entry to the script.
2219 Script
= S3BootScriptGetEntryAddAddress (0);
2220 if (Script
== NULL
) {
2221 return RETURN_OUT_OF_RESOURCES
;
2224 Script
= mS3BootScriptTablePtr
->TableBase
;
2226 StartAddress
= (UINTN
)Script
;
2227 TableLength
= mS3BootScriptTablePtr
->TableLength
;
2228 Script
= Script
+ sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
2229 LastOpcode
= Script
;
2231 // Find the last boot Script Entry which is not the terminate node
2233 while ((UINTN
)Script
< (UINTN
)(StartAddress
+ TableLength
)) {
2234 CopyMem ((VOID
*)&ScriptHeader
, Script
, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER
));
2235 if ((TempPosition
!= NULL
) && (TempPosition
== Script
)) {
2237 // If the position is specified, the position must be pointed to a boot script entry start address.
2239 ValidatePosition
= TRUE
;
2242 if (ScriptHeader
.OpCode
!= S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE
) {
2243 LastOpcode
= Script
;
2246 Script
= Script
+ ScriptHeader
.Length
;
2250 // If the position is specified, but not the start of a boot script entry, it is a invalid input
2252 if ((TempPosition
!= NULL
) && !ValidatePosition
) {
2253 return RETURN_INVALID_PARAMETER
;
2256 CopyMem ((VOID
*)&ScriptHeader
, LastOpcode
, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER
));
2258 CopyMem ((VOID
*)TempBootScriptEntry
, LastOpcode
, ScriptHeader
.Length
);
2260 // Find the right position to write the node in
2262 S3BootScriptCalculateInsertAddress (
2263 ScriptHeader
.Length
,
2269 // Copy the node to Boot script table
2271 CopyMem ((VOID
*)Script
, (VOID
*)TempBootScriptEntry
, ScriptHeader
.Length
);
2273 SyncBootScript (Script
);
2276 // return out the Position
2278 if (Position
!= NULL
) {
2282 return RETURN_SUCCESS
;
2286 Create a Label node in the boot script table.
2288 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
2289 in the boot script table specified by Position. If Position is NULL or points to
2290 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
2291 of the table (if FALSE).
2292 @param Position On entry, specifies the position in the boot script table where the opcode will be
2293 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
2294 the position of the inserted opcode in the boot script table.
2295 @param InformationLength Length of the label in bytes
2296 @param Information Label to be logged in the boot scrpit
2298 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
2299 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
2300 @retval RETURN_SUCCESS Opcode is added.
2305 S3BootScriptLabelInternal (
2306 IN BOOLEAN BeforeOrAfter
,
2307 IN OUT VOID
**Position OPTIONAL
,
2308 IN UINT32 InformationLength
,
2309 IN CONST CHAR8
*Information
2314 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation
;
2319 if (InformationLength
> MAX_UINT8
- sizeof (EFI_BOOT_SCRIPT_INFORMATION
)) {
2320 return RETURN_OUT_OF_RESOURCES
;
2323 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_INFORMATION
) + InformationLength
);
2325 Script
= S3BootScriptGetEntryAddAddress (Length
);
2326 if (Script
== NULL
) {
2327 return RETURN_OUT_OF_RESOURCES
;
2331 // Build script data
2333 ScriptInformation
.OpCode
= S3_BOOT_SCRIPT_LIB_LABEL_OPCODE
;
2334 ScriptInformation
.Length
= Length
;
2336 ScriptInformation
.InformationLength
= InformationLength
;
2338 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptInformation
, sizeof (EFI_BOOT_SCRIPT_INFORMATION
));
2339 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_INFORMATION
)), (VOID
*)Information
, (UINTN
)InformationLength
);
2341 SyncBootScript (Script
);
2343 return S3BootScriptMoveLastOpcode (BeforeOrAfter
, Position
);
2347 Find a label within the boot script table and, if not present, optionally create it.
2349 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE)
2350 or after (FALSE) the position in the boot script table
2351 specified by Position.
2352 @param CreateIfNotFound Specifies whether the label will be created if the label
2353 does not exists (TRUE) or not (FALSE).
2354 @param Position On entry, specifies the position in the boot script table
2355 where the opcode will be inserted, either before or after,
2356 depending on BeforeOrAfter. On exit, specifies the position
2357 of the inserted opcode in the boot script table.
2358 @param Label Points to the label which will be inserted in the boot script table.
2360 @retval EFI_SUCCESS The operation succeeded. A record was added into the
2361 specified script table.
2362 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
2363 If the opcode is unknow or not supported because of the PCD
2365 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
2371 IN BOOLEAN BeforeOrAfter
,
2372 IN BOOLEAN CreateIfNotFound
,
2373 IN OUT VOID
**Position OPTIONAL
,
2374 IN CONST CHAR8
*Label
2380 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
2381 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader
;
2387 if (Label
== NULL
) {
2388 return EFI_INVALID_PARAMETER
;
2392 // Check empty Label
2394 if (Label
[0] == '\0') {
2395 return EFI_INVALID_PARAMETER
;
2399 // Check that the script is initialized and synced without adding an entry to the script.
2400 // The code must search for the label first before it knows if a new entry needs
2403 Script
= S3BootScriptGetEntryAddAddress (0);
2404 if (Script
== NULL
) {
2405 return RETURN_OUT_OF_RESOURCES
;
2409 // Check the header and search for existing label.
2411 Script
= mS3BootScriptTablePtr
->TableBase
;
2412 CopyMem ((VOID
*)&TableHeader
, Script
, sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
));
2413 if (TableHeader
.OpCode
!= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
) {
2414 return EFI_INVALID_PARAMETER
;
2417 StartAddress
= (UINTN
)Script
;
2418 TableLength
= mS3BootScriptTablePtr
->TableLength
;
2419 Script
= Script
+ TableHeader
.Length
;
2420 while ((UINTN
)Script
< (UINTN
)(StartAddress
+ TableLength
)) {
2421 CopyMem ((VOID
*)&ScriptHeader
, Script
, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER
));
2422 if (ScriptHeader
.OpCode
== S3_BOOT_SCRIPT_LIB_LABEL_OPCODE
) {
2423 if (AsciiStrCmp ((CHAR8
*)(UINTN
)(Script
+sizeof (EFI_BOOT_SCRIPT_INFORMATION
)), Label
) == 0) {
2424 (*Position
) = Script
;
2429 Script
= Script
+ ScriptHeader
.Length
;
2432 if (CreateIfNotFound
) {
2433 LabelLength
= (UINT32
)AsciiStrSize (Label
);
2434 return S3BootScriptLabelInternal (BeforeOrAfter
, Position
, LabelLength
, Label
);
2436 return EFI_NOT_FOUND
;
2441 Compare two positions in the boot script table and return their relative position.
2442 @param Position1 The positions in the boot script table to compare
2443 @param Position2 The positions in the boot script table to compare
2444 @param RelativePosition On return, points to the result of the comparison
2446 @retval EFI_SUCCESS The operation succeeded. A record was added into the
2447 specified script table.
2448 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
2449 If the opcode is unknow or not supported because of the PCD
2451 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
2456 S3BootScriptCompare (
2457 IN UINT8
*Position1
,
2458 IN UINT8
*Position2
,
2459 OUT UINTN
*RelativePosition
2465 if (RelativePosition
== NULL
) {
2466 return EFI_INVALID_PARAMETER
;
2470 // Check that the script is initialized and synced without adding an entry to the script.
2472 Script
= S3BootScriptGetEntryAddAddress (0);
2473 if (Script
== NULL
) {
2474 return RETURN_OUT_OF_RESOURCES
;
2477 Script
= mS3BootScriptTablePtr
->TableBase
;
2480 // mS3BootScriptTablePtr->TableLength does not include the termination node, so add it up
2482 TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
2483 if ((Position1
< Script
) || (Position1
> Script
+TableLength
)) {
2484 return EFI_INVALID_PARAMETER
;
2487 if ((Position2
< Script
) || (Position2
> Script
+TableLength
)) {
2488 return EFI_INVALID_PARAMETER
;
2491 *RelativePosition
= (Position1
< Position2
) ? -1 : ((Position1
== Position2
) ? 0 : 1);