2 Save the S3 data to S3 boot script.
4 Copyright (c) 2006 - 2017, 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
;
129 This is an internal function to add a terminate node the entry, recalculate the table
130 length and fill into the table.
132 @return the base address of the boot script table.
135 S3BootScriptInternalCloseTable (
140 EFI_BOOT_SCRIPT_TERMINATE ScriptTerminate
;
141 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
142 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
144 if (S3TableBase
== NULL
) {
146 // the table is not exist
151 // Append the termination entry.
153 ScriptTerminate
.OpCode
= S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE
;
154 ScriptTerminate
.Length
= (UINT8
) sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
155 CopyMem (mS3BootScriptTablePtr
->TableBase
+ mS3BootScriptTablePtr
->TableLength
, &ScriptTerminate
, sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
157 // fill the table length
159 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)(mS3BootScriptTablePtr
->TableBase
);
160 ScriptTableInfo
->TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
166 // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to
167 // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE).
168 // Because maybe after SmmReadyToLock, we still need add entries into the table,
169 // and the entry should be added start before this TERMINATE node.
174 This function save boot script data to LockBox.
178 SaveBootScriptDataToLockBox (
185 // Save whole memory copy into LockBox.
186 // It will be used to restore data at S3 resume.
188 Status
= SaveLockBox (
189 &mBootScriptDataGuid
,
190 (VOID
*)mS3BootScriptTablePtr
->TableBase
,
191 EFI_PAGES_TO_SIZE (mS3BootScriptTablePtr
->TableMemoryPageNumber
)
193 ASSERT_EFI_ERROR (Status
);
195 Status
= SetLockBoxAttributes (&mBootScriptDataGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
196 ASSERT_EFI_ERROR (Status
);
199 // Just need save TableBase.
200 // Do not update other field because they will NOT be used in S3.
202 Status
= SaveLockBox (
203 &mBootScriptTableBaseGuid
,
204 (VOID
*)&mS3BootScriptTablePtr
->TableBase
,
205 sizeof(mS3BootScriptTablePtr
->TableBase
)
207 ASSERT_EFI_ERROR (Status
);
209 Status
= SetLockBoxAttributes (&mBootScriptTableBaseGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
210 ASSERT_EFI_ERROR (Status
);
214 This is the Event call back function to notify the Library the system is entering
217 @param Event Pointer to this event
218 @param Context Event handler private data
222 S3BootScriptEventCallBack (
231 // Try to locate it because EfiCreateProtocolNotifyEvent will trigger it once when registration.
232 // Just return if it is not found.
234 Status
= gBS
->LocateProtocol (
235 &gEfiDxeSmmReadyToLockProtocolGuid
,
239 if (EFI_ERROR (Status
)) {
244 // Here we should tell the library that we are entering SmmLocked phase.
245 // and the memory page number occupied by the table should not grow anymore.
247 if (!mS3BootScriptTablePtr
->SmmLocked
) {
249 // Before SmmReadyToLock, we need not write the terminate node when adding a node to boot scipt table
250 // or else, that will impact the performance. However, after SmmReadyToLock, we should append terminate
251 // node on every add to boot script table.
253 S3BootScriptInternalCloseTable ();
254 mS3BootScriptTablePtr
->SmmLocked
= TRUE
;
257 // Save BootScript data to lockbox
259 SaveBootScriptDataToLockBox ();
264 This is the Event call back function is triggered in SMM to notify the Library
265 the system is entering SmmLocked phase and set InSmm flag.
267 @param Protocol Points to the protocol's unique identifier
268 @param Interface Points to the interface instance
269 @param Handle The handle on which the interface was installed
271 @retval EFI_SUCCESS SmmEventCallback runs successfully
275 S3BootScriptSmmEventCallBack (
276 IN CONST EFI_GUID
*Protocol
,
282 // Check if it is already done
284 if (mS3BootScriptTablePtr
== mS3BootScriptTableSmmPtr
) {
289 // Last chance to call-out, just make sure SmmLocked is set.
291 S3BootScriptEventCallBack (NULL
, NULL
);
294 // Save a SMM copy. If TableBase is NOT null, it means SMM copy has been ready, skip copy mem.
296 if (mS3BootScriptTableSmmPtr
->TableBase
== NULL
) {
297 CopyMem (mS3BootScriptTableSmmPtr
, mS3BootScriptTablePtr
, sizeof(*mS3BootScriptTablePtr
));
300 // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.
301 // InSmm will only be checked if SmmLocked is TRUE.
303 mS3BootScriptTableSmmPtr
->InSmm
= TRUE
;
306 // We should not use ACPI Reserved copy, because it is not safe.
308 mS3BootScriptTablePtr
= mS3BootScriptTableSmmPtr
;
314 This function is to save boot time boot script data to LockBox.
316 Because there may be INSERT boot script at runtime in SMM.
317 The boot time copy will be used to restore data after back from S3.
318 Otherwise the data inserted may cause some boot time boot script data lost
319 if only BootScriptData used.
323 SaveBootTimeDataToLockBox (
330 // ACPI Reserved copy is not safe, restore from BootScriptData LockBox first,
331 // and then save the data to BootScriptDataBootTime LockBox.
333 Status
= RestoreLockBox (
334 &mBootScriptDataGuid
,
338 ASSERT_EFI_ERROR (Status
);
341 // Save BootScriptDataBootTime
342 // It will be used to restore data after back from S3.
344 Status
= SaveLockBox (
345 &mBootScriptDataBootTimeGuid
,
346 (VOID
*) mS3BootScriptTablePtr
->TableBase
,
347 mS3BootScriptTablePtr
->BootTimeScriptLength
349 ASSERT_EFI_ERROR (Status
);
353 This function save boot script SMM private data to LockBox with BackFromS3 = TRUE at runtime.
354 S3 resume will help restore it to tell the Library the system is back from S3.
358 SaveSmmPriviateDataToLockBoxAtRuntime (
365 // Save boot script SMM private data with BackFromS3 = TRUE.
367 mS3BootScriptTablePtr
->BackFromS3
= TRUE
;
368 Status
= SaveLockBox (
369 &mBootScriptSmmPrivateDataGuid
,
370 (VOID
*) mS3BootScriptTablePtr
,
371 sizeof (SCRIPT_TABLE_PRIVATE_DATA
)
373 ASSERT_EFI_ERROR (Status
);
375 Status
= SetLockBoxAttributes (&mBootScriptSmmPrivateDataGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
376 ASSERT_EFI_ERROR (Status
);
379 // Set BackFromS3 flag back to FALSE to indicate that now is not back from S3.
381 mS3BootScriptTablePtr
->BackFromS3
= FALSE
;
385 This is the Event call back function is triggered in SMM to notify the Library
386 the system is entering runtime phase.
388 @param[in] Protocol Points to the protocol's unique identifier
389 @param[in] Interface Points to the interface instance
390 @param[in] Handle The handle on which the interface was installed
392 @retval EFI_SUCCESS SmmAtRuntimeCallBack runs successfully
396 S3BootScriptSmmAtRuntimeCallBack (
397 IN CONST EFI_GUID
*Protocol
,
402 if (!mS3BootScriptTablePtr
->AtRuntime
) {
403 mS3BootScriptTablePtr
->BootTimeScriptLength
= (UINT32
) (mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
404 SaveBootTimeDataToLockBox ();
406 mS3BootScriptTablePtr
->AtRuntime
= TRUE
;
407 SaveSmmPriviateDataToLockBoxAtRuntime ();
415 this function just identify it is a smm driver or non-smm driver linked against
418 @param ImageHandle The firmware allocated handle for the EFI image.
419 @param SystemTable A pointer to the EFI System Table.
421 @retval RETURN_SUCCESS The constructor always returns RETURN_SUCCESS.
426 S3BootScriptLibInitialize (
427 IN EFI_HANDLE ImageHandle
,
428 IN EFI_SYSTEM_TABLE
*SystemTable
432 SCRIPT_TABLE_PRIVATE_DATA
*S3TablePtr
;
433 SCRIPT_TABLE_PRIVATE_DATA
*S3TableSmmPtr
;
435 EFI_SMM_BASE2_PROTOCOL
*SmmBase2
;
437 EFI_PHYSICAL_ADDRESS Buffer
;
439 S3TablePtr
= (SCRIPT_TABLE_PRIVATE_DATA
*)(UINTN
)PcdGet64(PcdS3BootScriptTablePrivateDataPtr
);
441 // The Boot script private data is not be initialized. create it
443 if (S3TablePtr
== 0) {
444 Buffer
= SIZE_4GB
- 1;
445 Status
= gBS
->AllocatePages (
447 EfiReservedMemoryType
,
448 EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA
)),
451 ASSERT_EFI_ERROR (Status
);
452 mS3BootScriptTableAllocated
= TRUE
;
453 S3TablePtr
= (VOID
*) (UINTN
) Buffer
;
455 Status
= PcdSet64S (PcdS3BootScriptTablePrivateDataPtr
, (UINT64
) (UINTN
)S3TablePtr
);
456 ASSERT_EFI_ERROR (Status
);
457 ZeroMem (S3TablePtr
, sizeof(SCRIPT_TABLE_PRIVATE_DATA
));
459 // Create event to notify the library system enter the SmmLocked phase.
461 mEventDxeSmmReadyToLock
= EfiCreateProtocolNotifyEvent (
462 &gEfiDxeSmmReadyToLockProtocolGuid
,
464 S3BootScriptEventCallBack
,
468 ASSERT (mEventDxeSmmReadyToLock
!= NULL
);
470 mS3BootScriptTablePtr
= S3TablePtr
;
473 // Get InSmm, we need to register SmmReadyToLock if this library is linked to SMM driver.
475 Status
= gBS
->LocateProtocol (&gEfiSmmBase2ProtocolGuid
, NULL
, (VOID
**) &SmmBase2
);
476 if (EFI_ERROR (Status
)) {
477 return RETURN_SUCCESS
;
479 Status
= SmmBase2
->InSmm (SmmBase2
, &InSmm
);
480 if (EFI_ERROR (Status
)) {
481 return RETURN_SUCCESS
;
484 return RETURN_SUCCESS
;
487 // Good, we are in SMM
489 Status
= SmmBase2
->GetSmstLocation (SmmBase2
, &mBootScriptSmst
);
490 if (EFI_ERROR (Status
)) {
491 return RETURN_SUCCESS
;
494 S3TableSmmPtr
= (SCRIPT_TABLE_PRIVATE_DATA
*)(UINTN
)PcdGet64(PcdS3BootScriptTablePrivateSmmDataPtr
);
496 // The Boot script private data in SMM is not be initialized. create it
498 if (S3TableSmmPtr
== 0) {
499 Status
= mBootScriptSmst
->SmmAllocatePool (
500 EfiRuntimeServicesData
,
501 sizeof(SCRIPT_TABLE_PRIVATE_DATA
),
502 (VOID
**) &S3TableSmmPtr
504 ASSERT_EFI_ERROR (Status
);
505 mS3BootScriptTableSmmAllocated
= TRUE
;
507 Status
= PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr
, (UINT64
) (UINTN
)S3TableSmmPtr
);
508 ASSERT_EFI_ERROR (Status
);
509 ZeroMem (S3TableSmmPtr
, sizeof(SCRIPT_TABLE_PRIVATE_DATA
));
512 // Register SmmExitBootServices and SmmLegacyBoot notification.
514 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
515 &gEdkiiSmmExitBootServicesProtocolGuid
,
516 S3BootScriptSmmAtRuntimeCallBack
,
517 &mRegistrationSmmExitBootServices
519 ASSERT_EFI_ERROR (Status
);
521 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
522 &gEdkiiSmmLegacyBootProtocolGuid
,
523 S3BootScriptSmmAtRuntimeCallBack
,
524 &mRegistrationSmmLegacyBoot
526 ASSERT_EFI_ERROR (Status
);
528 mS3BootScriptTableSmmPtr
= S3TableSmmPtr
;
531 // Register SmmReadyToLock notification.
533 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
534 &gEfiSmmReadyToLockProtocolGuid
,
535 S3BootScriptSmmEventCallBack
,
536 &mRegistrationSmmReadyToLock
538 ASSERT_EFI_ERROR (Status
);
540 return RETURN_SUCCESS
;
544 Library Destructor to free the resources allocated by
545 S3BootScriptLibInitialize() and unregister callbacks.
547 NOTICE: The destructor doesn't support unloading as a separate action, and it
548 only supports unloading if the containing driver's entry point function fails.
550 @param ImageHandle The firmware allocated handle for the EFI image.
551 @param SystemTable A pointer to the EFI System Table.
553 @retval RETURN_SUCCESS The destructor always returns RETURN_SUCCESS.
558 S3BootScriptLibDeinitialize (
559 IN EFI_HANDLE ImageHandle
,
560 IN EFI_SYSTEM_TABLE
*SystemTable
565 DEBUG ((EFI_D_INFO
, "%a() in %a module\n", __FUNCTION__
, gEfiCallerBaseName
));
567 if (mEventDxeSmmReadyToLock
!= NULL
) {
569 // Close the DxeSmmReadyToLock event.
571 Status
= gBS
->CloseEvent (mEventDxeSmmReadyToLock
);
572 ASSERT_EFI_ERROR (Status
);
575 if (mBootScriptSmst
!= NULL
) {
576 if (mRegistrationSmmExitBootServices
!= NULL
) {
578 // Unregister SmmExitBootServices notification.
580 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
581 &gEdkiiSmmExitBootServicesProtocolGuid
,
583 &mRegistrationSmmExitBootServices
585 ASSERT_EFI_ERROR (Status
);
587 if (mRegistrationSmmLegacyBoot
!= NULL
) {
589 // Unregister SmmLegacyBoot notification.
591 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
592 &gEdkiiSmmLegacyBootProtocolGuid
,
594 &mRegistrationSmmLegacyBoot
596 ASSERT_EFI_ERROR (Status
);
598 if (mRegistrationSmmReadyToLock
!= NULL
) {
600 // Unregister SmmReadyToLock notification.
602 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
603 &gEfiSmmReadyToLockProtocolGuid
,
605 &mRegistrationSmmReadyToLock
607 ASSERT_EFI_ERROR (Status
);
612 // Free the resources allocated and set PCDs to 0.
614 if (mS3BootScriptTableAllocated
) {
615 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) mS3BootScriptTablePtr
, EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA
)));
616 ASSERT_EFI_ERROR (Status
);
617 Status
= PcdSet64S (PcdS3BootScriptTablePrivateDataPtr
, 0);
618 ASSERT_EFI_ERROR (Status
);
620 if ((mBootScriptSmst
!= NULL
) && mS3BootScriptTableSmmAllocated
) {
621 Status
= mBootScriptSmst
->SmmFreePool (mS3BootScriptTableSmmPtr
);
622 ASSERT_EFI_ERROR (Status
);
623 Status
= PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr
, 0);
624 ASSERT_EFI_ERROR (Status
);
627 return RETURN_SUCCESS
;
631 To get the start address from which a new boot time s3 boot script entry will write into.
632 If the table is not exist, the functio will first allocate a buffer for the table
633 If the table buffer is not enough for the new entry, in non-smm mode, the funtion will
634 invoke reallocate to enlarge buffer.
636 @param EntryLength the new entry length.
638 @retval the address from which the a new s3 boot script entry will write into
641 S3BootScriptGetBootTimeEntryAddAddress (
645 EFI_PHYSICAL_ADDRESS S3TableBase
;
646 EFI_PHYSICAL_ADDRESS NewS3TableBase
;
651 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
653 S3TableBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(mS3BootScriptTablePtr
->TableBase
);
654 if (S3TableBase
== 0) {
656 // The table is not exist. This is the first to add entry.
657 // Allocate ACPI script table space under 4G memory.
659 S3TableBase
= 0xffffffff;
660 Status
= gBS
->AllocatePages (
662 EfiReservedMemoryType
,
663 2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
),
664 (EFI_PHYSICAL_ADDRESS
*)&S3TableBase
667 if (EFI_ERROR(Status
)) {
668 ASSERT_EFI_ERROR (Status
);
674 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)(UINTN
)S3TableBase
;
675 ScriptTableInfo
->OpCode
= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
;
676 ScriptTableInfo
->Length
= (UINT8
) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
677 ScriptTableInfo
->Version
= BOOT_SCRIPT_TABLE_VERSION
;
678 ScriptTableInfo
->TableLength
= 0; // will be calculate at CloseTable
679 mS3BootScriptTablePtr
->TableLength
= sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
680 mS3BootScriptTablePtr
->TableBase
= (UINT8
*)(UINTN
)S3TableBase
;
681 mS3BootScriptTablePtr
->TableMemoryPageNumber
= (UINT16
)(2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
));
684 // Here we do not count the reserved memory for runtime script table.
685 PageNumber
= (UINT16
) (mS3BootScriptTablePtr
->TableMemoryPageNumber
- PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
));
686 TableLength
= mS3BootScriptTablePtr
->TableLength
;
687 if (EFI_PAGES_TO_SIZE ((UINTN
) PageNumber
) < (TableLength
+ EntryLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
))) {
689 // The buffer is too small to hold the table, Reallocate the buffer
691 NewS3TableBase
= 0xffffffff;
692 Status
= gBS
->AllocatePages (
694 EfiReservedMemoryType
,
695 2 + PageNumber
+ PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
),
696 (EFI_PHYSICAL_ADDRESS
*)&NewS3TableBase
699 if (EFI_ERROR(Status
)) {
700 ASSERT_EFI_ERROR (Status
);
704 CopyMem ((VOID
*)(UINTN
)NewS3TableBase
, (VOID
*)(UINTN
)S3TableBase
, TableLength
);
705 gBS
->FreePages (S3TableBase
, mS3BootScriptTablePtr
->TableMemoryPageNumber
);
707 mS3BootScriptTablePtr
->TableBase
= (UINT8
*)(UINTN
)NewS3TableBase
;
708 mS3BootScriptTablePtr
->TableMemoryPageNumber
= (UINT16
) (2 + PageNumber
+ PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
));
711 // calculate the the start address for the new entry.
713 NewEntryPtr
= mS3BootScriptTablePtr
->TableBase
+ TableLength
;
716 // update the table lenghth
718 mS3BootScriptTablePtr
->TableLength
= TableLength
+ EntryLength
;
721 // In the boot time, we will not append the termination entry to the boot script
722 // table until the callers think there is no boot time data that should be added and
723 // it is caller's responsibility to explicit call the CloseTable.
730 To get the start address from which a new runtime(after SmmReadyToLock) s3 boot script entry will write into.
731 In this case, it should be ensured that there is enough buffer to hold the entry.
733 @param EntryLength the new entry length.
735 @retval the address from which the a new s3 runtime(after SmmReadyToLock) script entry will write into
738 S3BootScriptGetRuntimeEntryAddAddress (
746 // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node.
748 if ((mS3BootScriptTablePtr
->TableLength
+ EntryLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
)) <= EFI_PAGES_TO_SIZE ((UINTN
) (mS3BootScriptTablePtr
->TableMemoryPageNumber
))) {
749 NewEntryPtr
= mS3BootScriptTablePtr
->TableBase
+ mS3BootScriptTablePtr
->TableLength
;
750 mS3BootScriptTablePtr
->TableLength
= mS3BootScriptTablePtr
->TableLength
+ EntryLength
;
752 // Append a terminate node on every insert
754 S3BootScriptInternalCloseTable ();
756 return (UINT8
*)NewEntryPtr
;
760 This function is to restore boot time boot script data from LockBox.
764 RestoreBootTimeDataFromLockBox (
772 // Restore boot time boot script data from LockBox.
774 LockBoxLength
= mS3BootScriptTablePtr
->BootTimeScriptLength
;
775 Status
= RestoreLockBox (
776 &mBootScriptDataBootTimeGuid
,
777 (VOID
*) mS3BootScriptTablePtr
->TableBase
,
780 ASSERT_EFI_ERROR (Status
);
783 // Update the data to BootScriptData LockBox.
785 Status
= UpdateLockBox (
786 &mBootScriptDataGuid
,
788 (VOID
*) mS3BootScriptTablePtr
->TableBase
,
791 ASSERT_EFI_ERROR (Status
);
794 // Update TableLength.
796 mS3BootScriptTablePtr
->TableLength
= (UINT32
) (mS3BootScriptTablePtr
->BootTimeScriptLength
- sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
800 To get the start address from which a new s3 boot script entry will write into.
802 @param EntryLength the new entry length.
804 @retval the address from which the a new s3 boot script entry will write into
807 S3BootScriptGetEntryAddAddress (
813 if (mS3BootScriptTablePtr
->SmmLocked
) {
815 // We need check InSmm, because after SmmReadyToLock, only SMM driver is allowed to write boot script.
817 if (!mS3BootScriptTablePtr
->InSmm
) {
819 // Add DEBUG ERROR, so that we can find it after SmmReadyToLock.
820 // Do not use ASSERT, because we may have test to invoke this interface.
822 DEBUG ((EFI_D_ERROR
, "FATAL ERROR: Set boot script outside SMM after SmmReadyToLock!!!\n"));
826 if (mS3BootScriptTablePtr
->BackFromS3
) {
828 // Back from S3, restore boot time boot script data from LockBox
829 // and set BackFromS3 flag back to FALSE.
831 RestoreBootTimeDataFromLockBox ();
832 mS3BootScriptTablePtr
->BackFromS3
= FALSE
;
835 NewEntryPtr
= S3BootScriptGetRuntimeEntryAddAddress (EntryLength
);
837 NewEntryPtr
= S3BootScriptGetBootTimeEntryAddAddress (EntryLength
);
844 Sync BootScript LockBox data.
846 @param Script The address from where the boot script has been added or updated.
856 UINT32 TotalScriptLength
;
858 if (!mS3BootScriptTablePtr
->SmmLocked
|| !mS3BootScriptTablePtr
->InSmm
) {
860 // If it is not after SmmReadyToLock in SMM,
866 ScriptOffset
= (UINT32
) (Script
- mS3BootScriptTablePtr
->TableBase
);
868 TotalScriptLength
= (UINT32
) (mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
871 // Update BootScriptData
872 // So in S3 resume, the data can be restored correctly.
874 Status
= UpdateLockBox (
875 &mBootScriptDataGuid
,
877 (VOID
*)((UINTN
)mS3BootScriptTablePtr
->TableBase
+ ScriptOffset
),
878 TotalScriptLength
- ScriptOffset
880 ASSERT_EFI_ERROR (Status
);
883 // Now the length field is updated, need sync to lockbox.
884 // So at S3 resume, the data can be restored correctly.
886 Status
= UpdateLockBox (
887 &mBootScriptDataGuid
,
888 OFFSET_OF (EFI_BOOT_SCRIPT_TABLE_HEADER
, TableLength
),
890 sizeof (TotalScriptLength
)
892 ASSERT_EFI_ERROR (Status
);
896 This is an function to close the S3 boot script table. The function could only be called in
897 BOOT time phase. To comply with the Framework spec definition on
898 EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable(), this function will fulfill following things:
899 1. Closes the specified boot script table
900 2. It allocates a new memory pool to duplicate all the boot scripts in the specified table.
901 Once this function is called, the table maintained by the library will be destroyed
902 after it is copied into the allocated pool.
903 3. Any attempts to add a script record after calling this function will cause a new table
904 to be created by the library.
905 4. The base address of the allocated pool will be returned in Address. Note that after
906 using the boot script table, the CALLER is responsible for freeing the pool that is allocated
909 In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is
910 for Framework Spec compatibility.
912 If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out
913 how to get the script to run at S3 resume because the boot script maintained by the lib will be
916 @return the base address of the new copy of the boot script table.
917 @note this function could only called in boot time phase
922 S3BootScriptCloseTable (
930 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
932 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
933 if (S3TableBase
== 0) {
937 // Append the termination record the S3 boot script table
939 S3BootScriptInternalCloseTable();
940 TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
942 // Allocate the buffer and copy the boot script to the buffer.
944 Status
= gBS
->AllocatePool (
949 if (EFI_ERROR (Status
)) {
952 CopyMem (Buffer
, S3TableBase
, TableLength
);
955 // Destroy the table maintained by the library so that the next write operation
956 // will write the record to the first entry of the table.
958 // Fill the table header.
959 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)S3TableBase
;
960 ScriptTableInfo
->OpCode
= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
;
961 ScriptTableInfo
->Length
= (UINT8
) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
962 ScriptTableInfo
->TableLength
= 0; // will be calculate at close the table
964 mS3BootScriptTablePtr
->TableLength
= sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
968 Save I/O write to boot script
970 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
971 @param Address The base address of the I/O operations.
972 @param Count The number of I/O operations to perform.
973 @param Buffer The source buffer from which to write data.
975 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
976 @retval RETURN_SUCCESS Opcode is added.
980 S3BootScriptSaveIoWrite (
981 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
991 EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite
;
993 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
994 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE
) + (WidthInByte
* Count
));
996 Script
= S3BootScriptGetEntryAddAddress (Length
);
997 if (Script
== NULL
) {
998 return RETURN_OUT_OF_RESOURCES
;
1003 ScriptIoWrite
.OpCode
= EFI_BOOT_SCRIPT_IO_WRITE_OPCODE
;
1004 ScriptIoWrite
.Length
= Length
;
1005 ScriptIoWrite
.Width
= Width
;
1006 ScriptIoWrite
.Address
= Address
;
1007 ScriptIoWrite
.Count
= (UINT32
) Count
;
1008 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoWrite
, sizeof(EFI_BOOT_SCRIPT_IO_WRITE
));
1009 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_WRITE
)), Buffer
, WidthInByte
* Count
);
1011 SyncBootScript (Script
);
1013 return RETURN_SUCCESS
;
1017 Adds a record for an I/O modify operation into a S3 boot script table
1019 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1020 @param Address The base address of the I/O operations.
1021 @param Data A pointer to the data to be OR-ed.
1022 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register
1024 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1025 @retval RETURN_SUCCESS Opcode is added.
1029 S3BootScriptSaveIoReadWrite (
1030 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1039 EFI_BOOT_SCRIPT_IO_READ_WRITE ScriptIoReadWrite
;
1041 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1042 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
) + (WidthInByte
* 2));
1044 Script
= S3BootScriptGetEntryAddAddress (Length
);
1045 if (Script
== NULL
) {
1046 return RETURN_OUT_OF_RESOURCES
;
1049 // Build script data
1051 ScriptIoReadWrite
.OpCode
= EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE
;
1052 ScriptIoReadWrite
.Length
= Length
;
1053 ScriptIoReadWrite
.Width
= Width
;
1054 ScriptIoReadWrite
.Address
= Address
;
1056 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoReadWrite
, sizeof(EFI_BOOT_SCRIPT_IO_READ_WRITE
));
1057 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
)), Data
, WidthInByte
);
1058 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
) + WidthInByte
), DataMask
, WidthInByte
);
1060 SyncBootScript (Script
);
1062 return RETURN_SUCCESS
;
1065 Adds a record for a memory write operation into a specified boot script table.
1067 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1068 @param Address The base address of the memory operations
1069 @param Count The number of memory operations to perform.
1070 @param Buffer The source buffer from which to write the data.
1072 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1073 @retval RETURN_SUCCESS Opcode is added.
1077 S3BootScriptSaveMemWrite (
1078 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1087 EFI_BOOT_SCRIPT_MEM_WRITE ScriptMemWrite
;
1089 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1090 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
) + (WidthInByte
* Count
));
1092 Script
= S3BootScriptGetEntryAddAddress (Length
);
1093 if (Script
== NULL
) {
1094 return RETURN_OUT_OF_RESOURCES
;
1097 // Build script data
1099 ScriptMemWrite
.OpCode
= EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE
;
1100 ScriptMemWrite
.Length
= Length
;
1101 ScriptMemWrite
.Width
= Width
;
1102 ScriptMemWrite
.Address
= Address
;
1103 ScriptMemWrite
.Count
= (UINT32
) Count
;
1105 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemWrite
, sizeof(EFI_BOOT_SCRIPT_MEM_WRITE
));
1106 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
)), Buffer
, WidthInByte
* Count
);
1108 SyncBootScript (Script
);
1110 return RETURN_SUCCESS
;
1113 Adds a record for a memory modify operation into a specified boot script table.
1115 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1116 @param Address The base address of the memory operations. Address needs alignment if required
1117 @param Data A pointer to the data to be OR-ed.
1118 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register.
1120 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1121 @retval RETURN_SUCCESS Opcode is added.
1125 S3BootScriptSaveMemReadWrite (
1126 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1135 EFI_BOOT_SCRIPT_MEM_READ_WRITE ScriptMemReadWrite
;
1137 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1138 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
) + (WidthInByte
* 2));
1140 Script
= S3BootScriptGetEntryAddAddress (Length
);
1141 if (Script
== NULL
) {
1142 return RETURN_OUT_OF_RESOURCES
;
1145 // Build script data
1147 ScriptMemReadWrite
.OpCode
= EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE
;
1148 ScriptMemReadWrite
.Length
= Length
;
1149 ScriptMemReadWrite
.Width
= Width
;
1150 ScriptMemReadWrite
.Address
= Address
;
1152 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemReadWrite
, sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
));
1153 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
)), Data
, WidthInByte
);
1154 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
) + WidthInByte
), DataMask
, WidthInByte
);
1156 SyncBootScript (Script
);
1158 return RETURN_SUCCESS
;
1161 Adds a record for a PCI configuration space write operation into a specified boot script table.
1163 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1164 @param Address The address within the PCI configuration space.
1165 @param Count The number of PCI operations to perform.
1166 @param Buffer The source buffer from which to write the data.
1168 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1169 @retval RETURN_SUCCESS Opcode is added.
1170 @note A known Limitations in the implementation which is 64bits operations are not supported.
1175 S3BootScriptSavePciCfgWrite (
1176 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1185 EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE ScriptPciWrite
;
1187 if (Width
== S3BootScriptWidthUint64
||
1188 Width
== S3BootScriptWidthFifoUint64
||
1189 Width
== S3BootScriptWidthFillUint64
) {
1190 return EFI_INVALID_PARAMETER
;
1193 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1194 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
) + (WidthInByte
* Count
));
1196 Script
= S3BootScriptGetEntryAddAddress (Length
);
1197 if (Script
== NULL
) {
1198 return RETURN_OUT_OF_RESOURCES
;
1201 // Build script data
1203 ScriptPciWrite
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE
;
1204 ScriptPciWrite
.Length
= Length
;
1205 ScriptPciWrite
.Width
= Width
;
1206 ScriptPciWrite
.Address
= Address
;
1207 ScriptPciWrite
.Count
= (UINT32
) Count
;
1209 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciWrite
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
));
1210 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
)), Buffer
, WidthInByte
* Count
);
1212 SyncBootScript (Script
);
1214 return RETURN_SUCCESS
;
1217 Adds a record for a PCI configuration space modify operation into a specified boot script table.
1219 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1220 @param Address The address within the PCI configuration space.
1221 @param Data A pointer to the data to be OR-ed.The size depends on Width.
1222 @param DataMask A pointer to the data mask to be AND-ed.
1224 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1225 @retval RETURN__SUCCESS Opcode is added.
1226 @note A known Limitations in the implementation which is 64bits operations are not supported.
1231 S3BootScriptSavePciCfgReadWrite (
1232 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1241 EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE ScriptPciReadWrite
;
1243 if (Width
== S3BootScriptWidthUint64
||
1244 Width
== S3BootScriptWidthFifoUint64
||
1245 Width
== S3BootScriptWidthFillUint64
) {
1246 return EFI_INVALID_PARAMETER
;
1249 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1250 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
) + (WidthInByte
* 2));
1252 Script
= S3BootScriptGetEntryAddAddress (Length
);
1253 if (Script
== NULL
) {
1254 return RETURN_OUT_OF_RESOURCES
;
1257 // Build script data
1259 ScriptPciReadWrite
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE
;
1260 ScriptPciReadWrite
.Length
= Length
;
1261 ScriptPciReadWrite
.Width
= Width
;
1262 ScriptPciReadWrite
.Address
= Address
;
1264 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciReadWrite
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
));
1265 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
)), Data
, WidthInByte
);
1267 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
) + WidthInByte
),
1272 SyncBootScript (Script
);
1274 return RETURN_SUCCESS
;
1277 Adds a record for a PCI configuration 2 space write operation into a specified boot script table.
1279 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1280 @param Segment The PCI segment number for Address.
1281 @param Address The address within the PCI configuration space.
1282 @param Count The number of PCI operations to perform.
1283 @param Buffer The source buffer from which to write the data.
1285 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1286 @retval RETURN_SUCCESS Opcode is added.
1287 @note A known Limitations in the implementation which is 64bits operations are not supported.
1292 S3BootScriptSavePciCfg2Write (
1293 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1303 EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE ScriptPciWrite2
;
1305 if (Width
== S3BootScriptWidthUint64
||
1306 Width
== S3BootScriptWidthFifoUint64
||
1307 Width
== S3BootScriptWidthFillUint64
) {
1308 return EFI_INVALID_PARAMETER
;
1311 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1312 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
) + (WidthInByte
* Count
));
1314 Script
= S3BootScriptGetEntryAddAddress (Length
);
1315 if (Script
== NULL
) {
1316 return RETURN_OUT_OF_RESOURCES
;
1319 // Build script data
1321 ScriptPciWrite2
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE
;
1322 ScriptPciWrite2
.Length
= Length
;
1323 ScriptPciWrite2
.Width
= Width
;
1324 ScriptPciWrite2
.Address
= Address
;
1325 ScriptPciWrite2
.Segment
= Segment
;
1326 ScriptPciWrite2
.Count
= (UINT32
)Count
;
1328 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciWrite2
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
));
1329 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
)), Buffer
, WidthInByte
* Count
);
1331 SyncBootScript (Script
);
1333 return RETURN_SUCCESS
;
1336 Adds a record for a PCI configuration 2 space modify operation into a specified boot script table.
1338 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1339 @param Segment The PCI segment number for Address.
1340 @param Address The address within the PCI configuration space.
1341 @param Data A pointer to the data to be OR-ed. The size depends on Width.
1342 @param DataMask A pointer to the data mask to be AND-ed.
1344 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1345 @retval RETURN_SUCCESS Opcode is added.
1346 @note A known Limitations in the implementation which is 64bits operations are not supported.
1351 S3BootScriptSavePciCfg2ReadWrite (
1352 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1362 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE ScriptPciReadWrite2
;
1364 if (Width
== S3BootScriptWidthUint64
||
1365 Width
== S3BootScriptWidthFifoUint64
||
1366 Width
== S3BootScriptWidthFillUint64
) {
1367 return EFI_INVALID_PARAMETER
;
1370 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1371 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
) + (WidthInByte
* 2));
1373 Script
= S3BootScriptGetEntryAddAddress (Length
);
1374 if (Script
== NULL
) {
1375 return RETURN_OUT_OF_RESOURCES
;
1378 // Build script data
1380 ScriptPciReadWrite2
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE
;
1381 ScriptPciReadWrite2
.Length
= Length
;
1382 ScriptPciReadWrite2
.Width
= Width
;
1383 ScriptPciReadWrite2
.Segment
= Segment
;
1384 ScriptPciReadWrite2
.Address
= Address
;
1386 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciReadWrite2
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
));
1387 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
)), Data
, WidthInByte
);
1389 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
) + WidthInByte
),
1394 SyncBootScript (Script
);
1396 return RETURN_SUCCESS
;
1400 Checks the parameter of S3BootScriptSaveSmbusExecute().
1402 This function checks the input parameters of SmbusExecute(). If the input parameters are valid
1403 for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain
1404 error code based on the input SMBus bus protocol.
1406 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length,
1408 @param Operation Signifies which particular SMBus hardware protocol instance that
1409 it will use to execute the SMBus transactions. This SMBus
1410 hardware protocol is defined by the SMBus Specification and is
1412 @param Length Signifies the number of bytes that this operation will do. The
1413 maximum number of bytes can be revision specific and operation
1414 specific. This field will contain the actual number of bytes that
1415 are executed for this operation. Not all operations require this
1417 @param Buffer Contains the value of data to execute to the SMBus slave device.
1418 Not all operations require this argument. The length of this
1419 buffer is identified by Length.
1421 @retval EFI_SUCCESS All the parameters are valid for the corresponding SMBus bus
1423 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.
1424 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead
1425 and EfiSmbusQuickWrite. Length is outside the range of valid
1427 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.
1428 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.
1433 IN UINTN SmBusAddress
,
1434 IN EFI_SMBUS_OPERATION Operation
,
1435 IN OUT UINTN
*Length
,
1441 EFI_SMBUS_DEVICE_COMMAND Command
;
1444 Command
= SMBUS_LIB_COMMAND (SmBusAddress
);
1445 PecCheck
= SMBUS_LIB_PEC (SmBusAddress
);
1447 // Set default value to be 2:
1448 // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.
1451 Status
= EFI_SUCCESS
;
1452 switch (Operation
) {
1453 case EfiSmbusQuickRead
:
1454 case EfiSmbusQuickWrite
:
1455 if (PecCheck
|| Command
!= 0) {
1456 return EFI_UNSUPPORTED
;
1459 case EfiSmbusReceiveByte
:
1460 case EfiSmbusSendByte
:
1462 return EFI_UNSUPPORTED
;
1465 // Cascade to check length parameter.
1467 case EfiSmbusReadByte
:
1468 case EfiSmbusWriteByte
:
1471 // Cascade to check length parameter.
1473 case EfiSmbusReadWord
:
1474 case EfiSmbusWriteWord
:
1475 case EfiSmbusProcessCall
:
1476 if (Buffer
== NULL
|| Length
== NULL
) {
1477 return EFI_INVALID_PARAMETER
;
1478 } else if (*Length
< RequiredLen
) {
1479 Status
= EFI_BUFFER_TOO_SMALL
;
1481 *Length
= RequiredLen
;
1483 case EfiSmbusReadBlock
:
1484 case EfiSmbusWriteBlock
:
1485 case EfiSmbusBWBRProcessCall
:
1486 if ((Buffer
== NULL
) ||
1488 (*Length
< MIN_SMBUS_BLOCK_LEN
) ||
1489 (*Length
> MAX_SMBUS_BLOCK_LEN
)) {
1490 return EFI_INVALID_PARAMETER
;
1494 return EFI_INVALID_PARAMETER
;
1500 Adds a record for an SMBus command execution into a specified boot script table.
1502 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length, and PEC.
1503 @param Operation Indicates which particular SMBus protocol it will use to execute the SMBus
1505 @param Length A pointer to signify the number of bytes that this operation will do.
1506 @param Buffer Contains the value of data to execute to the SMBUS slave device.
1508 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1509 @retval RETURN_SUCCESS Opcode is added.
1513 S3BootScriptSaveSmbusExecute (
1514 IN UINTN SmBusAddress
,
1515 IN EFI_SMBUS_OPERATION Operation
,
1524 EFI_BOOT_SCRIPT_SMBUS_EXECUTE ScriptSmbusExecute
;
1526 if (Length
== NULL
) {
1529 BufferLength
= *Length
;
1532 Status
= CheckParameters (SmBusAddress
, Operation
, &BufferLength
, Buffer
);
1533 if (EFI_ERROR (Status
)) {
1537 DataSize
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
) + BufferLength
);
1539 Script
= S3BootScriptGetEntryAddAddress (DataSize
);
1540 if (Script
== NULL
) {
1541 return RETURN_OUT_OF_RESOURCES
;
1544 // Build script data
1546 ScriptSmbusExecute
.OpCode
= EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE
;
1547 ScriptSmbusExecute
.Length
= DataSize
;
1548 ScriptSmbusExecute
.SmBusAddress
= (UINT64
) SmBusAddress
;
1549 ScriptSmbusExecute
.Operation
= Operation
;
1550 ScriptSmbusExecute
.DataSize
= (UINT32
) BufferLength
;
1552 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptSmbusExecute
, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
));
1554 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
)),
1559 SyncBootScript (Script
);
1561 return RETURN_SUCCESS
;
1564 Adds a record for an execution stall on the processor into a specified boot script table.
1566 @param Duration Duration in microseconds of the stall
1568 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1569 @retval RETURN_SUCCESS Opcode is added.
1573 S3BootScriptSaveStall (
1579 EFI_BOOT_SCRIPT_STALL ScriptStall
;
1581 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_STALL
));
1583 Script
= S3BootScriptGetEntryAddAddress (Length
);
1584 if (Script
== NULL
) {
1585 return RETURN_OUT_OF_RESOURCES
;
1588 // Build script data
1590 ScriptStall
.OpCode
= EFI_BOOT_SCRIPT_STALL_OPCODE
;
1591 ScriptStall
.Length
= Length
;
1592 ScriptStall
.Duration
= Duration
;
1594 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptStall
, sizeof (EFI_BOOT_SCRIPT_STALL
));
1596 SyncBootScript (Script
);
1598 return RETURN_SUCCESS
;
1601 Adds a record for dispatching specified arbitrary code into a specified boot script table.
1603 @param EntryPoint Entry point of the code to be dispatched.
1604 @param Context Argument to be passed into the EntryPoint of the code to be dispatched.
1606 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1607 @retval RETURN_SUCCESS Opcode is added.
1611 S3BootScriptSaveDispatch2 (
1612 IN VOID
*EntryPoint
,
1618 EFI_BOOT_SCRIPT_DISPATCH_2 ScriptDispatch2
;
1619 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2
));
1621 Script
= S3BootScriptGetEntryAddAddress (Length
);
1622 if (Script
== NULL
) {
1623 return RETURN_OUT_OF_RESOURCES
;
1626 // Build script data
1628 ScriptDispatch2
.OpCode
= EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE
;
1629 ScriptDispatch2
.Length
= Length
;
1630 ScriptDispatch2
.EntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)EntryPoint
;
1631 ScriptDispatch2
.Context
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)Context
;
1633 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptDispatch2
, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2
));
1635 SyncBootScript (Script
);
1637 return RETURN_SUCCESS
;
1641 Adds a record for memory reads of the memory location and continues when the exit criteria is
1642 satisfied or after a defined duration.
1644 Please aware, below interface is different with PI specification, Vol 5:
1645 EFI_S3_SAVE_STATE_PROTOCOL.Write() for EFI_BOOT_SCRIPT_MEM_POLL_OPCODE.
1646 "Duration" below is microseconds, while "Delay" in PI specification means
1647 the number of 100ns units to poll.
1649 @param Width The width of the memory operations.
1650 @param Address The base address of the memory operations.
1651 @param BitMask A pointer to the bit mask to be AND-ed with the data read from the register.
1652 @param BitValue A pointer to the data value after to be Masked.
1653 @param Duration Duration in microseconds of the stall.
1654 @param LoopTimes The times of the register polling.
1656 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1657 @retval RETURN_SUCCESS Opcode is added.
1662 S3BootScriptSaveMemPoll (
1663 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1674 EFI_BOOT_SCRIPT_MEM_POLL ScriptMemPoll
;
1676 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1678 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL
) + (WidthInByte
* 2));
1680 Script
= S3BootScriptGetEntryAddAddress (Length
);
1681 if (Script
== NULL
) {
1682 return RETURN_OUT_OF_RESOURCES
;
1685 // Build script data
1687 ScriptMemPoll
.OpCode
= EFI_BOOT_SCRIPT_MEM_POLL_OPCODE
;
1688 ScriptMemPoll
.Length
= Length
;
1689 ScriptMemPoll
.Width
= Width
;
1690 ScriptMemPoll
.Address
= Address
;
1691 ScriptMemPoll
.Duration
= Duration
;
1692 ScriptMemPoll
.LoopTimes
= LoopTimes
;
1694 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_POLL
)), BitValue
, WidthInByte
);
1695 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_POLL
) + WidthInByte
), BitMask
, WidthInByte
);
1696 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemPoll
, sizeof (EFI_BOOT_SCRIPT_MEM_POLL
));
1698 SyncBootScript (Script
);
1700 return RETURN_SUCCESS
;
1703 Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only
1704 used for debugging script issues.
1706 @param InformationLength Length of the data in bytes
1707 @param Information Information to be logged in the boot scrpit
1709 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1710 @retval RETURN_SUCCESS Opcode is added.
1715 S3BootScriptSaveInformation (
1716 IN UINT32 InformationLength
,
1717 IN VOID
*Information
1722 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation
;
1724 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_INFORMATION
) + InformationLength
);
1726 Script
= S3BootScriptGetEntryAddAddress (Length
);
1727 if (Script
== NULL
) {
1728 return RETURN_OUT_OF_RESOURCES
;
1731 // Build script data
1733 ScriptInformation
.OpCode
= EFI_BOOT_SCRIPT_INFORMATION_OPCODE
;
1734 ScriptInformation
.Length
= Length
;
1737 ScriptInformation
.InformationLength
= InformationLength
;
1739 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptInformation
, sizeof (EFI_BOOT_SCRIPT_INFORMATION
));
1740 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_INFORMATION
)), (VOID
*) Information
, (UINTN
) InformationLength
);
1742 SyncBootScript (Script
);
1744 return RETURN_SUCCESS
;
1748 Store a string in the boot script table. This opcode is a no-op on dispatch and is only
1749 used for debugging script issues.
1751 @param String The string to save to boot script table
1753 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1754 @retval RETURN_SUCCESS Opcode is added.
1759 S3BootScriptSaveInformationAsciiString (
1760 IN CONST CHAR8
*String
1763 return S3BootScriptSaveInformation (
1764 (UINT32
) AsciiStrLen (String
) + 1,
1769 Adds a record for dispatching specified arbitrary code into a specified boot script table.
1771 @param EntryPoint Entry point of the code to be dispatched.
1773 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1774 @retval RETURN_SUCCESS Opcode is added.
1778 S3BootScriptSaveDispatch (
1784 EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch
;
1786 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_DISPATCH
));
1788 Script
= S3BootScriptGetEntryAddAddress (Length
);
1789 if (Script
== NULL
) {
1790 return RETURN_OUT_OF_RESOURCES
;
1793 // Build script data
1795 ScriptDispatch
.OpCode
= EFI_BOOT_SCRIPT_DISPATCH_OPCODE
;
1796 ScriptDispatch
.Length
= Length
;
1797 ScriptDispatch
.EntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)EntryPoint
;
1799 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptDispatch
, sizeof (EFI_BOOT_SCRIPT_DISPATCH
));
1801 SyncBootScript (Script
);
1803 return RETURN_SUCCESS
;
1807 Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a
1810 @param Width The width of the I/O operations.
1811 @param Address The base address of the I/O operations.
1812 @param Data The comparison value used for the polling exit criteria.
1813 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1814 in Data are ignored when polling the memory address.
1815 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1816 granularity so the delay may be longer.
1818 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1819 @retval RETURN_SUCCESS Opcode is added.
1824 S3BootScriptSaveIoPoll (
1825 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1835 EFI_BOOT_SCRIPT_IO_POLL ScriptIoPoll
;
1838 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1839 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + (WidthInByte
* 2));
1841 Script
= S3BootScriptGetEntryAddAddress (Length
);
1842 if (Script
== NULL
) {
1843 return RETURN_OUT_OF_RESOURCES
;
1846 // Build script data
1848 ScriptIoPoll
.OpCode
= EFI_BOOT_SCRIPT_IO_POLL_OPCODE
;
1849 ScriptIoPoll
.Length
= (UINT8
) (sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + (WidthInByte
* 2));
1850 ScriptIoPoll
.Width
= Width
;
1851 ScriptIoPoll
.Address
= Address
;
1852 ScriptIoPoll
.Delay
= Delay
;
1854 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoPoll
, sizeof (EFI_BOOT_SCRIPT_IO_POLL
));
1855 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_IO_POLL
)), Data
, WidthInByte
);
1856 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1858 SyncBootScript (Script
);
1860 return RETURN_SUCCESS
;
1864 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1865 after a defined duration.
1867 @param Width The width of the I/O operations.
1868 @param Address The address within the PCI configuration space.
1869 @param Data The comparison value used for the polling exit criteria.
1870 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1871 in Data are ignored when polling the memory address
1872 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1873 granularity so the delay may be longer.
1875 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1876 @retval RETURN_SUCCESS Opcode is added.
1877 @note A known Limitations in the implementation which is 64bits operations are not supported.
1882 S3BootScriptSavePciPoll (
1883 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1893 EFI_BOOT_SCRIPT_PCI_CONFIG_POLL ScriptPciPoll
;
1895 if (Width
== S3BootScriptWidthUint64
||
1896 Width
== S3BootScriptWidthFifoUint64
||
1897 Width
== S3BootScriptWidthFillUint64
) {
1898 return EFI_INVALID_PARAMETER
;
1901 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1902 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + (WidthInByte
* 2));
1904 Script
= S3BootScriptGetEntryAddAddress (Length
);
1905 if (Script
== NULL
) {
1906 return RETURN_OUT_OF_RESOURCES
;
1909 // Build script data
1911 ScriptPciPoll
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE
;
1912 ScriptPciPoll
.Length
= (UINT8
) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + (WidthInByte
* 2));
1913 ScriptPciPoll
.Width
= Width
;
1914 ScriptPciPoll
.Address
= Address
;
1915 ScriptPciPoll
.Delay
= Delay
;
1917 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciPoll
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
));
1918 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
)), Data
, WidthInByte
);
1919 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1921 SyncBootScript (Script
);
1923 return RETURN_SUCCESS
;
1926 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1927 after a defined duration.
1929 @param Width The width of the I/O operations.
1930 @param Segment The PCI segment number for Address.
1931 @param Address The address within the PCI configuration space.
1932 @param Data The comparison value used for the polling exit criteria.
1933 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1934 in Data are ignored when polling the memory address
1935 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1936 granularity so the delay may be longer.
1938 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1939 @retval RETURN_SUCCESS Opcode is added.
1940 @note A known Limitations in the implementation which is 64bits operations are not supported.
1945 S3BootScriptSavePci2Poll (
1946 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1957 EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL ScriptPci2Poll
;
1959 if (Width
== S3BootScriptWidthUint64
||
1960 Width
== S3BootScriptWidthFifoUint64
||
1961 Width
== S3BootScriptWidthFillUint64
) {
1962 return EFI_INVALID_PARAMETER
;
1965 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1966 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + (WidthInByte
* 2));
1968 Script
= S3BootScriptGetEntryAddAddress (Length
);
1969 if (Script
== NULL
) {
1970 return RETURN_OUT_OF_RESOURCES
;
1973 // Build script data
1975 ScriptPci2Poll
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE
;
1976 ScriptPci2Poll
.Length
= (UINT8
) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + (WidthInByte
* 2));
1977 ScriptPci2Poll
.Width
= Width
;
1978 ScriptPci2Poll
.Segment
= Segment
;
1979 ScriptPci2Poll
.Address
= Address
;
1980 ScriptPci2Poll
.Delay
= Delay
;
1982 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPci2Poll
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
));
1983 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
)), Data
, WidthInByte
);
1984 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1986 SyncBootScript (Script
);
1988 return RETURN_SUCCESS
;
1991 Do the calculation of start address from which a new s3 boot script entry will write into.
1993 @param EntryLength The new entry length.
1994 @param Position specifies the position in the boot script table where the opcode will be
1995 inserted, either before or after, depending on BeforeOrAfter.
1996 @param BeforeOrAfter The flag to indicate to insert the nod before or after the position.
1997 This parameter is effective when InsertFlag is TRUE
1998 @param Script return out the position from which the a new s3 boot script entry will write into
2001 S3BootScriptCalculateInsertAddress (
2002 IN UINT8 EntryLength
,
2003 IN VOID
*Position OPTIONAL
,
2004 IN BOOLEAN BeforeOrAfter OPTIONAL
,
2010 UINTN PositionOffset
;
2011 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
2013 // The entry inserting to table is already added to the end of the table
2015 TableLength
= mS3BootScriptTablePtr
->TableLength
- EntryLength
;
2016 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
2018 // calculate the Position offset
2020 if (Position
!= NULL
) {
2021 PositionOffset
= (UINTN
)Position
- (UINTN
)S3TableBase
;
2024 // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.
2026 if (!BeforeOrAfter
) {
2027 CopyMem ((VOID
*)&ScriptHeader
, Position
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
2028 PositionOffset
+= (ScriptHeader
.Length
);
2031 // Insert the node before the adjusted Position
2033 CopyMem (S3TableBase
+PositionOffset
+EntryLength
, S3TableBase
+PositionOffset
, TableLength
- PositionOffset
);
2035 // calculate the the start address for the new entry.
2037 *Script
= S3TableBase
+ PositionOffset
;
2040 if (!BeforeOrAfter
) {
2042 // Insert the node to the end of the table
2044 *Script
= S3TableBase
+ TableLength
;
2047 // Insert the node to the beginning of the table
2049 PositionOffset
= (UINTN
) sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
);
2050 CopyMem (S3TableBase
+PositionOffset
+EntryLength
, S3TableBase
+PositionOffset
, TableLength
- PositionOffset
);
2051 *Script
= S3TableBase
+ PositionOffset
;
2056 Move the last boot script entry to the position
2058 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
2059 in the boot script table specified by Position. If Position is NULL or points to
2060 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
2061 of the table (if FALSE).
2062 @param Position On entry, specifies the position in the boot script table where the opcode will be
2063 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
2064 the position of the inserted opcode in the boot script table.
2066 @retval RETURN_OUT_OF_RESOURCES The table is not available.
2067 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
2068 @retval RETURN_SUCCESS Opcode is inserted.
2072 S3BootScriptMoveLastOpcode (
2073 IN BOOLEAN BeforeOrAfter
,
2074 IN OUT VOID
**Position OPTIONAL
2081 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
2082 BOOLEAN ValidatePosition
;
2084 UINT8 TempBootScriptEntry
[BOOT_SCRIPT_NODE_MAX_LENGTH
];
2086 ValidatePosition
= FALSE
;
2087 TempPosition
= (Position
== NULL
) ? NULL
:(*Position
);
2090 // Check that the script is initialized and synced without adding an entry to the script.
2092 Script
= S3BootScriptGetEntryAddAddress (0);
2093 if (Script
== NULL
) {
2094 return RETURN_OUT_OF_RESOURCES
;
2096 Script
= mS3BootScriptTablePtr
->TableBase
;
2098 StartAddress
= (UINTN
) Script
;
2099 TableLength
= mS3BootScriptTablePtr
->TableLength
;
2100 Script
= Script
+ sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
);
2101 LastOpcode
= Script
;
2103 // Find the last boot Script Entry which is not the terminate node
2105 while ((UINTN
) Script
< (UINTN
) (StartAddress
+ TableLength
)) {
2106 CopyMem ((VOID
*)&ScriptHeader
, Script
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
2107 if (TempPosition
!= NULL
&& TempPosition
== Script
) {
2109 // If the position is specified, the position must be pointed to a boot script entry start address.
2111 ValidatePosition
= TRUE
;
2113 if (ScriptHeader
.OpCode
!= S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE
) {
2114 LastOpcode
= Script
;
2116 Script
= Script
+ ScriptHeader
.Length
;
2119 // If the position is specified, but not the start of a boot script entry, it is a invalid input
2121 if (TempPosition
!= NULL
&& !ValidatePosition
) {
2122 return RETURN_INVALID_PARAMETER
;
2125 CopyMem ((VOID
*)&ScriptHeader
, LastOpcode
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
2127 CopyMem((VOID
*)TempBootScriptEntry
, LastOpcode
, ScriptHeader
.Length
);
2129 // Find the right position to write the node in
2131 S3BootScriptCalculateInsertAddress (
2132 ScriptHeader
.Length
,
2138 // Copy the node to Boot script table
2140 CopyMem((VOID
*)Script
, (VOID
*)TempBootScriptEntry
, ScriptHeader
.Length
);
2142 SyncBootScript (Script
);
2145 // return out the Position
2147 if (Position
!= NULL
) {
2150 return RETURN_SUCCESS
;
2153 Create a Label node in the boot script table.
2155 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
2156 in the boot script table specified by Position. If Position is NULL or points to
2157 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
2158 of the table (if FALSE).
2159 @param Position On entry, specifies the position in the boot script table where the opcode will be
2160 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
2161 the position of the inserted opcode in the boot script table.
2162 @param InformationLength Length of the label in bytes
2163 @param Information Label to be logged in the boot scrpit
2165 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
2166 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
2167 @retval RETURN_SUCCESS Opcode is added.
2172 S3BootScriptLabelInternal (
2173 IN BOOLEAN BeforeOrAfter
,
2174 IN OUT VOID
**Position OPTIONAL
,
2175 IN UINT32 InformationLength
,
2176 IN CONST CHAR8
*Information
2181 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation
;
2183 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_INFORMATION
) + InformationLength
);
2185 Script
= S3BootScriptGetEntryAddAddress (Length
);
2186 if (Script
== NULL
) {
2187 return RETURN_OUT_OF_RESOURCES
;
2190 // Build script data
2192 ScriptInformation
.OpCode
= S3_BOOT_SCRIPT_LIB_LABEL_OPCODE
;
2193 ScriptInformation
.Length
= Length
;
2196 ScriptInformation
.InformationLength
= InformationLength
;
2198 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptInformation
, sizeof (EFI_BOOT_SCRIPT_INFORMATION
));
2199 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_INFORMATION
)), (VOID
*) Information
, (UINTN
) InformationLength
);
2201 SyncBootScript (Script
);
2203 return S3BootScriptMoveLastOpcode (BeforeOrAfter
, Position
);
2207 Find a label within the boot script table and, if not present, optionally create it.
2209 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE)
2210 or after (FALSE) the position in the boot script table
2211 specified by Position.
2212 @param CreateIfNotFound Specifies whether the label will be created if the label
2213 does not exists (TRUE) or not (FALSE).
2214 @param Position On entry, specifies the position in the boot script table
2215 where the opcode will be inserted, either before or after,
2216 depending on BeforeOrAfter. On exit, specifies the position
2217 of the inserted opcode in the boot script table.
2218 @param Label Points to the label which will be inserted in the boot script table.
2220 @retval EFI_SUCCESS The operation succeeded. A record was added into the
2221 specified script table.
2222 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
2223 If the opcode is unknow or not supported because of the PCD
2225 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
2231 IN BOOLEAN BeforeOrAfter
,
2232 IN BOOLEAN CreateIfNotFound
,
2233 IN OUT VOID
**Position OPTIONAL
,
2234 IN CONST CHAR8
*Label
2240 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
2241 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader
;
2246 if (Label
== NULL
) {
2247 return EFI_INVALID_PARAMETER
;
2250 // Check empty Label
2252 if (Label
[0] == '\0') {
2253 return EFI_INVALID_PARAMETER
;
2257 // Check that the script is initialized and synced without adding an entry to the script.
2258 // The code must search for the label first before it knows if a new entry needs
2261 Script
= S3BootScriptGetEntryAddAddress (0);
2262 if (Script
== NULL
) {
2263 return RETURN_OUT_OF_RESOURCES
;
2267 // Check the header and search for existing label.
2269 Script
= mS3BootScriptTablePtr
->TableBase
;
2270 CopyMem ((VOID
*)&TableHeader
, Script
, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
));
2271 if (TableHeader
.OpCode
!= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
) {
2272 return EFI_INVALID_PARAMETER
;
2274 StartAddress
= (UINTN
) Script
;
2275 TableLength
= mS3BootScriptTablePtr
->TableLength
;
2276 Script
= Script
+ TableHeader
.Length
;
2277 while ((UINTN
) Script
< (UINTN
) (StartAddress
+ TableLength
)) {
2279 CopyMem ((VOID
*)&ScriptHeader
, Script
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
2280 if (ScriptHeader
.OpCode
== S3_BOOT_SCRIPT_LIB_LABEL_OPCODE
) {
2281 if (AsciiStrCmp ((CHAR8
*)(UINTN
)(Script
+sizeof(EFI_BOOT_SCRIPT_INFORMATION
)), Label
) == 0) {
2282 (*Position
) = Script
;
2286 Script
= Script
+ ScriptHeader
.Length
;
2288 if (CreateIfNotFound
) {
2289 LabelLength
= (UINT32
)AsciiStrSize(Label
);
2290 return S3BootScriptLabelInternal (BeforeOrAfter
,Position
, LabelLength
, Label
);
2292 return EFI_NOT_FOUND
;
2297 Compare two positions in the boot script table and return their relative position.
2298 @param Position1 The positions in the boot script table to compare
2299 @param Position2 The positions in the boot script table to compare
2300 @param RelativePosition On return, points to the result of the comparison
2302 @retval EFI_SUCCESS The operation succeeded. A record was added into the
2303 specified script table.
2304 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
2305 If the opcode is unknow or not supported because of the PCD
2307 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
2312 S3BootScriptCompare (
2313 IN UINT8
*Position1
,
2314 IN UINT8
*Position2
,
2315 OUT UINTN
*RelativePosition
2321 if (RelativePosition
== NULL
) {
2322 return EFI_INVALID_PARAMETER
;
2326 // Check that the script is initialized and synced without adding an entry to the script.
2328 Script
= S3BootScriptGetEntryAddAddress (0);
2329 if (Script
== NULL
) {
2330 return RETURN_OUT_OF_RESOURCES
;
2332 Script
= mS3BootScriptTablePtr
->TableBase
;
2335 // mS3BootScriptTablePtr->TableLength does not include the termination node, so add it up
2337 TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
2338 if (Position1
< Script
|| Position1
> Script
+TableLength
) {
2339 return EFI_INVALID_PARAMETER
;
2341 if (Position2
< Script
|| Position2
> Script
+TableLength
) {
2342 return EFI_INVALID_PARAMETER
;
2344 *RelativePosition
= (Position1
< Position2
)?-1:((Position1
== Position2
)?0:1);