2 Save the S3 data to S3 boot script.
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "InternalBootScriptLib.h"
22 +------------------------------+<------- PcdS3BootScriptTablePrivateDataPtr
23 | SCRIPT_TABLE_PRIVATE_DATA | (mS3BootScriptTablePtr, Before SmmReadyToLock)
24 | TableBase |--- PcdS3BootScriptTablePrivateSmmDataPtr
25 | TableLength |--|-- (mS3BootScriptTablePtr = mS3BootScriptTableSmmPtr, After SmmReadyToLock InSmm)
26 | TableMemoryPageNumber |--|-|----
29 | BootTimeScriptLength |--|-|---|---
31 | BackFromS3 | | | | |
32 +------------------------------+ | | | |
34 +------------------------------+<-- | | |
35 | EFI_BOOT_SCRIPT_TABLE_HEADER | | | |
36 | TableLength |----|-- | |
37 +------------------------------+ | | | |
39 +------------------------------+<---- | | |
40 | EFI_BOOT_SCRIPT_TERMINATE | | | |
41 +------------------------------+<------ | |
44 mBootScriptDataBootTimeGuid LockBox: | |
45 Used to restore data after back from S3| |
46 to handle potential INSERT boot script | |
48 +------------------------------+ | |
49 | Boot Time Boot Script | | |
50 | Before SmmReadyToLock | | |
53 +------------------------------+ | |
54 | Boot Time Boot Script | | |
55 | After SmmReadyToLock InSmm | | |
57 +------------------------------+<-------|--|
60 mBootScriptDataGuid LockBox: (IN_PLACE) | |
61 Used to restore data at S3 resume. | |
62 +------------------------------+ | |
63 | Boot Time Boot Script | | |
64 | Before SmmReadyToLock | | |
67 +------------------------------+ | |
68 | Boot Time Boot Script | | |
69 | After SmmReadyToLock InSmm | | |
71 +------------------------------+<-------|---
72 | Runtime Boot Script | |
73 | After SmmReadyToLock InSmm | |
74 +------------------------------+ |
76 +------------------------------+<--------
79 mBootScriptTableBaseGuid LockBox: (IN_PLACE)
80 +------------------------------+
81 | mS3BootScriptTablePtr-> |
83 +------------------------------+
86 mBootScriptSmmPrivateDataGuid LockBox: (IN_PLACE)
87 SMM private data with BackFromS3 = TRUE
88 at runtime. S3 will help restore it to
89 tell the Library the system is back from S3.
90 +------------------------------+
91 | SCRIPT_TABLE_PRIVATE_DATA |
94 | TableMemoryPageNumber |
97 | BootTimeScriptLength |
100 +------------------------------+
104 SCRIPT_TABLE_PRIVATE_DATA
*mS3BootScriptTablePtr
;
107 // Allocate SMM copy because we can not use mS3BootScriptTablePtr after SmmReadyToLock in InSmm.
109 SCRIPT_TABLE_PRIVATE_DATA
*mS3BootScriptTableSmmPtr
;
111 EFI_GUID mBootScriptDataGuid
= {
112 0xaea6b965, 0xdcf5, 0x4311, { 0xb4, 0xb8, 0xf, 0x12, 0x46, 0x44, 0x94, 0xd2 }
115 EFI_GUID mBootScriptDataBootTimeGuid
= {
116 0xb5af1d7a, 0xb8cf, 0x4eb3, { 0x89, 0x25, 0xa8, 0x20, 0xe1, 0x6b, 0x68, 0x7d }
119 EFI_GUID mBootScriptTableBaseGuid
= {
120 0x1810ab4a, 0x2314, 0x4df6, { 0x81, 0xeb, 0x67, 0xc6, 0xec, 0x5, 0x85, 0x91 }
123 EFI_GUID mBootScriptSmmPrivateDataGuid
= {
124 0x627ee2da, 0x3bf9, 0x439b, { 0x92, 0x9f, 0x2e, 0xe, 0x6e, 0x9d, 0xba, 0x62 }
127 EFI_EVENT mEventDxeSmmReadyToLock
= NULL
;
128 VOID
*mRegistrationSmmExitBootServices
= NULL
;
129 VOID
*mRegistrationSmmLegacyBoot
= NULL
;
130 VOID
*mRegistrationSmmReadyToLock
= NULL
;
131 BOOLEAN mS3BootScriptTableAllocated
= FALSE
;
132 BOOLEAN mS3BootScriptTableSmmAllocated
= FALSE
;
133 EFI_SMM_SYSTEM_TABLE2
*mBootScriptSmst
= NULL
;
136 This is an internal function to add a terminate node the entry, recalculate the table
137 length and fill into the table.
139 @return the base address of the boot script table.
142 S3BootScriptInternalCloseTable (
147 EFI_BOOT_SCRIPT_TERMINATE ScriptTerminate
;
148 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
149 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
151 if (S3TableBase
== NULL
) {
153 // the table is not exist
158 // Append the termination entry.
160 ScriptTerminate
.OpCode
= S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE
;
161 ScriptTerminate
.Length
= (UINT8
) sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
162 CopyMem (mS3BootScriptTablePtr
->TableBase
+ mS3BootScriptTablePtr
->TableLength
, &ScriptTerminate
, sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
164 // fill the table length
166 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)(mS3BootScriptTablePtr
->TableBase
);
167 ScriptTableInfo
->TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
173 // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to
174 // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE).
175 // Because maybe after SmmReadyToLock, we still need add entries into the table,
176 // and the entry should be added start before this TERMINATE node.
181 This function save boot script data to LockBox.
185 SaveBootScriptDataToLockBox (
192 // Save whole memory copy into LockBox.
193 // It will be used to restore data at S3 resume.
195 Status
= SaveLockBox (
196 &mBootScriptDataGuid
,
197 (VOID
*)mS3BootScriptTablePtr
->TableBase
,
198 EFI_PAGES_TO_SIZE (mS3BootScriptTablePtr
->TableMemoryPageNumber
)
200 ASSERT_EFI_ERROR (Status
);
202 Status
= SetLockBoxAttributes (&mBootScriptDataGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
203 ASSERT_EFI_ERROR (Status
);
206 // Just need save TableBase.
207 // Do not update other field because they will NOT be used in S3.
209 Status
= SaveLockBox (
210 &mBootScriptTableBaseGuid
,
211 (VOID
*)&mS3BootScriptTablePtr
->TableBase
,
212 sizeof(mS3BootScriptTablePtr
->TableBase
)
214 ASSERT_EFI_ERROR (Status
);
216 Status
= SetLockBoxAttributes (&mBootScriptTableBaseGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
217 ASSERT_EFI_ERROR (Status
);
221 This is the Event call back function to notify the Library the system is entering
224 @param Event Pointer to this event
225 @param Context Event handler private data
229 S3BootScriptEventCallBack (
238 // Try to locate it because EfiCreateProtocolNotifyEvent will trigger it once when registration.
239 // Just return if it is not found.
241 Status
= gBS
->LocateProtocol (
242 &gEfiDxeSmmReadyToLockProtocolGuid
,
246 if (EFI_ERROR (Status
)) {
251 // Here we should tell the library that we are entering SmmLocked phase.
252 // and the memory page number occupied by the table should not grow anymore.
254 if (!mS3BootScriptTablePtr
->SmmLocked
) {
256 // Before SmmReadyToLock, we need not write the terminate node when adding a node to boot scipt table
257 // or else, that will impact the performance. However, after SmmReadyToLock, we should append terminate
258 // node on every add to boot script table.
260 S3BootScriptInternalCloseTable ();
261 mS3BootScriptTablePtr
->SmmLocked
= TRUE
;
264 // Save BootScript data to lockbox
266 SaveBootScriptDataToLockBox ();
271 This is the Event call back function is triggered in SMM to notify the Library
272 the system is entering SmmLocked phase and set InSmm flag.
274 @param Protocol Points to the protocol's unique identifier
275 @param Interface Points to the interface instance
276 @param Handle The handle on which the interface was installed
278 @retval EFI_SUCCESS SmmEventCallback runs successfully
282 S3BootScriptSmmEventCallBack (
283 IN CONST EFI_GUID
*Protocol
,
289 // Check if it is already done
291 if (mS3BootScriptTablePtr
== mS3BootScriptTableSmmPtr
) {
296 // Last chance to call-out, just make sure SmmLocked is set.
298 S3BootScriptEventCallBack (NULL
, NULL
);
301 // Save a SMM copy. If TableBase is NOT null, it means SMM copy has been ready, skip copy mem.
303 if (mS3BootScriptTableSmmPtr
->TableBase
== NULL
) {
304 CopyMem (mS3BootScriptTableSmmPtr
, mS3BootScriptTablePtr
, sizeof(*mS3BootScriptTablePtr
));
307 // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.
308 // InSmm will only be checked if SmmLocked is TRUE.
310 mS3BootScriptTableSmmPtr
->InSmm
= TRUE
;
313 // We should not use ACPI Reserved copy, because it is not safe.
315 mS3BootScriptTablePtr
= mS3BootScriptTableSmmPtr
;
321 This function is to save boot time boot script data to LockBox.
323 Because there may be INSERT boot script at runtime in SMM.
324 The boot time copy will be used to restore data after back from S3.
325 Otherwise the data inserted may cause some boot time boot script data lost
326 if only BootScriptData used.
330 SaveBootTimeDataToLockBox (
337 // ACPI Reserved copy is not safe, restore from BootScriptData LockBox first,
338 // and then save the data to BootScriptDataBootTime LockBox.
340 Status
= RestoreLockBox (
341 &mBootScriptDataGuid
,
345 ASSERT_EFI_ERROR (Status
);
348 // Save BootScriptDataBootTime
349 // It will be used to restore data after back from S3.
351 Status
= SaveLockBox (
352 &mBootScriptDataBootTimeGuid
,
353 (VOID
*) mS3BootScriptTablePtr
->TableBase
,
354 mS3BootScriptTablePtr
->BootTimeScriptLength
356 ASSERT_EFI_ERROR (Status
);
360 This function save boot script SMM private data to LockBox with BackFromS3 = TRUE at runtime.
361 S3 resume will help restore it to tell the Library the system is back from S3.
365 SaveSmmPriviateDataToLockBoxAtRuntime (
372 // Save boot script SMM private data with BackFromS3 = TRUE.
374 mS3BootScriptTablePtr
->BackFromS3
= TRUE
;
375 Status
= SaveLockBox (
376 &mBootScriptSmmPrivateDataGuid
,
377 (VOID
*) mS3BootScriptTablePtr
,
378 sizeof (SCRIPT_TABLE_PRIVATE_DATA
)
380 ASSERT_EFI_ERROR (Status
);
382 Status
= SetLockBoxAttributes (&mBootScriptSmmPrivateDataGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
383 ASSERT_EFI_ERROR (Status
);
386 // Set BackFromS3 flag back to FALSE to indicate that now is not back from S3.
388 mS3BootScriptTablePtr
->BackFromS3
= FALSE
;
392 This is the Event call back function is triggered in SMM to notify the Library
393 the system is entering runtime phase.
395 @param[in] Protocol Points to the protocol's unique identifier
396 @param[in] Interface Points to the interface instance
397 @param[in] Handle The handle on which the interface was installed
399 @retval EFI_SUCCESS SmmAtRuntimeCallBack runs successfully
403 S3BootScriptSmmAtRuntimeCallBack (
404 IN CONST EFI_GUID
*Protocol
,
409 if (!mS3BootScriptTablePtr
->AtRuntime
) {
410 mS3BootScriptTablePtr
->BootTimeScriptLength
= (UINT32
) (mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
411 SaveBootTimeDataToLockBox ();
413 mS3BootScriptTablePtr
->AtRuntime
= TRUE
;
414 SaveSmmPriviateDataToLockBoxAtRuntime ();
422 this function just identify it is a smm driver or non-smm driver linked against
425 @param ImageHandle The firmware allocated handle for the EFI image.
426 @param SystemTable A pointer to the EFI System Table.
428 @retval RETURN_SUCCESS The constructor always returns RETURN_SUCCESS.
433 S3BootScriptLibInitialize (
434 IN EFI_HANDLE ImageHandle
,
435 IN EFI_SYSTEM_TABLE
*SystemTable
439 SCRIPT_TABLE_PRIVATE_DATA
*S3TablePtr
;
440 SCRIPT_TABLE_PRIVATE_DATA
*S3TableSmmPtr
;
442 EFI_SMM_BASE2_PROTOCOL
*SmmBase2
;
444 EFI_PHYSICAL_ADDRESS Buffer
;
446 S3TablePtr
= (SCRIPT_TABLE_PRIVATE_DATA
*)(UINTN
)PcdGet64(PcdS3BootScriptTablePrivateDataPtr
);
448 // The Boot script private data is not be initialized. create it
450 if (S3TablePtr
== 0) {
451 Buffer
= SIZE_4GB
- 1;
452 Status
= gBS
->AllocatePages (
454 EfiReservedMemoryType
,
455 EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA
)),
458 ASSERT_EFI_ERROR (Status
);
459 mS3BootScriptTableAllocated
= TRUE
;
460 S3TablePtr
= (VOID
*) (UINTN
) Buffer
;
462 Status
= PcdSet64S (PcdS3BootScriptTablePrivateDataPtr
, (UINT64
) (UINTN
)S3TablePtr
);
463 ASSERT_EFI_ERROR (Status
);
464 ZeroMem (S3TablePtr
, sizeof(SCRIPT_TABLE_PRIVATE_DATA
));
466 // Create event to notify the library system enter the SmmLocked phase.
468 mEventDxeSmmReadyToLock
= EfiCreateProtocolNotifyEvent (
469 &gEfiDxeSmmReadyToLockProtocolGuid
,
471 S3BootScriptEventCallBack
,
475 ASSERT (mEventDxeSmmReadyToLock
!= NULL
);
477 mS3BootScriptTablePtr
= S3TablePtr
;
480 // Get InSmm, we need to register SmmReadyToLock if this library is linked to SMM driver.
482 Status
= gBS
->LocateProtocol (&gEfiSmmBase2ProtocolGuid
, NULL
, (VOID
**) &SmmBase2
);
483 if (EFI_ERROR (Status
)) {
484 return RETURN_SUCCESS
;
486 Status
= SmmBase2
->InSmm (SmmBase2
, &InSmm
);
487 if (EFI_ERROR (Status
)) {
488 return RETURN_SUCCESS
;
491 return RETURN_SUCCESS
;
494 // Good, we are in SMM
496 Status
= SmmBase2
->GetSmstLocation (SmmBase2
, &mBootScriptSmst
);
497 if (EFI_ERROR (Status
)) {
498 return RETURN_SUCCESS
;
501 S3TableSmmPtr
= (SCRIPT_TABLE_PRIVATE_DATA
*)(UINTN
)PcdGet64(PcdS3BootScriptTablePrivateSmmDataPtr
);
503 // The Boot script private data in SMM is not be initialized. create it
505 if (S3TableSmmPtr
== 0) {
506 Status
= mBootScriptSmst
->SmmAllocatePool (
507 EfiRuntimeServicesData
,
508 sizeof(SCRIPT_TABLE_PRIVATE_DATA
),
509 (VOID
**) &S3TableSmmPtr
511 ASSERT_EFI_ERROR (Status
);
512 mS3BootScriptTableSmmAllocated
= TRUE
;
514 Status
= PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr
, (UINT64
) (UINTN
)S3TableSmmPtr
);
515 ASSERT_EFI_ERROR (Status
);
516 ZeroMem (S3TableSmmPtr
, sizeof(SCRIPT_TABLE_PRIVATE_DATA
));
519 // Register SmmExitBootServices and SmmLegacyBoot notification.
521 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
522 &gEdkiiSmmExitBootServicesProtocolGuid
,
523 S3BootScriptSmmAtRuntimeCallBack
,
524 &mRegistrationSmmExitBootServices
526 ASSERT_EFI_ERROR (Status
);
528 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
529 &gEdkiiSmmLegacyBootProtocolGuid
,
530 S3BootScriptSmmAtRuntimeCallBack
,
531 &mRegistrationSmmLegacyBoot
533 ASSERT_EFI_ERROR (Status
);
535 mS3BootScriptTableSmmPtr
= S3TableSmmPtr
;
538 // Register SmmReadyToLock notification.
540 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
541 &gEfiSmmReadyToLockProtocolGuid
,
542 S3BootScriptSmmEventCallBack
,
543 &mRegistrationSmmReadyToLock
545 ASSERT_EFI_ERROR (Status
);
547 return RETURN_SUCCESS
;
551 Library Destructor to free the resources allocated by
552 S3BootScriptLibInitialize() and unregister callbacks.
554 NOTICE: The destructor doesn't support unloading as a separate action, and it
555 only supports unloading if the containing driver's entry point function fails.
557 @param ImageHandle The firmware allocated handle for the EFI image.
558 @param SystemTable A pointer to the EFI System Table.
560 @retval RETURN_SUCCESS The destructor always returns RETURN_SUCCESS.
565 S3BootScriptLibDeinitialize (
566 IN EFI_HANDLE ImageHandle
,
567 IN EFI_SYSTEM_TABLE
*SystemTable
572 DEBUG ((EFI_D_INFO
, "%a() in %a module\n", __FUNCTION__
, gEfiCallerBaseName
));
574 if (mEventDxeSmmReadyToLock
!= NULL
) {
576 // Close the DxeSmmReadyToLock event.
578 Status
= gBS
->CloseEvent (mEventDxeSmmReadyToLock
);
579 ASSERT_EFI_ERROR (Status
);
582 if (mBootScriptSmst
!= NULL
) {
583 if (mRegistrationSmmExitBootServices
!= NULL
) {
585 // Unregister SmmExitBootServices notification.
587 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
588 &gEdkiiSmmExitBootServicesProtocolGuid
,
590 &mRegistrationSmmExitBootServices
592 ASSERT_EFI_ERROR (Status
);
594 if (mRegistrationSmmLegacyBoot
!= NULL
) {
596 // Unregister SmmLegacyBoot notification.
598 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
599 &gEdkiiSmmLegacyBootProtocolGuid
,
601 &mRegistrationSmmLegacyBoot
603 ASSERT_EFI_ERROR (Status
);
605 if (mRegistrationSmmReadyToLock
!= NULL
) {
607 // Unregister SmmReadyToLock notification.
609 Status
= mBootScriptSmst
->SmmRegisterProtocolNotify (
610 &gEfiSmmReadyToLockProtocolGuid
,
612 &mRegistrationSmmReadyToLock
614 ASSERT_EFI_ERROR (Status
);
619 // Free the resources allocated and set PCDs to 0.
621 if (mS3BootScriptTableAllocated
) {
622 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) mS3BootScriptTablePtr
, EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA
)));
623 ASSERT_EFI_ERROR (Status
);
624 Status
= PcdSet64S (PcdS3BootScriptTablePrivateDataPtr
, 0);
625 ASSERT_EFI_ERROR (Status
);
627 if ((mBootScriptSmst
!= NULL
) && mS3BootScriptTableSmmAllocated
) {
628 Status
= mBootScriptSmst
->SmmFreePool (mS3BootScriptTableSmmPtr
);
629 ASSERT_EFI_ERROR (Status
);
630 Status
= PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr
, 0);
631 ASSERT_EFI_ERROR (Status
);
634 return RETURN_SUCCESS
;
638 To get the start address from which a new boot time s3 boot script entry will write into.
639 If the table is not exist, the functio will first allocate a buffer for the table
640 If the table buffer is not enough for the new entry, in non-smm mode, the funtion will
641 invoke reallocate to enlarge buffer.
643 @param EntryLength the new entry length.
645 @retval the address from which the a new s3 boot script entry will write into
648 S3BootScriptGetBootTimeEntryAddAddress (
652 EFI_PHYSICAL_ADDRESS S3TableBase
;
653 EFI_PHYSICAL_ADDRESS NewS3TableBase
;
658 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
660 S3TableBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(mS3BootScriptTablePtr
->TableBase
);
661 if (S3TableBase
== 0) {
663 // The table is not exist. This is the first to add entry.
664 // Allocate ACPI script table space under 4G memory.
666 S3TableBase
= 0xffffffff;
667 Status
= gBS
->AllocatePages (
669 EfiReservedMemoryType
,
670 2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
),
671 (EFI_PHYSICAL_ADDRESS
*)&S3TableBase
674 if (EFI_ERROR(Status
)) {
675 ASSERT_EFI_ERROR (Status
);
681 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)(UINTN
)S3TableBase
;
682 ScriptTableInfo
->OpCode
= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
;
683 ScriptTableInfo
->Length
= (UINT8
) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
684 ScriptTableInfo
->Version
= BOOT_SCRIPT_TABLE_VERSION
;
685 ScriptTableInfo
->TableLength
= 0; // will be calculate at CloseTable
686 mS3BootScriptTablePtr
->TableLength
= sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
687 mS3BootScriptTablePtr
->TableBase
= (UINT8
*)(UINTN
)S3TableBase
;
688 mS3BootScriptTablePtr
->TableMemoryPageNumber
= (UINT16
)(2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
));
691 // Here we do not count the reserved memory for runtime script table.
692 PageNumber
= (UINT16
) (mS3BootScriptTablePtr
->TableMemoryPageNumber
- PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
));
693 TableLength
= mS3BootScriptTablePtr
->TableLength
;
694 if ((UINTN
) EFI_PAGES_TO_SIZE ((UINTN
) PageNumber
) < (UINTN
) (TableLength
+ EntryLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
))) {
696 // The buffer is too small to hold the table, Reallocate the buffer
698 NewS3TableBase
= 0xffffffff;
699 Status
= gBS
->AllocatePages (
701 EfiReservedMemoryType
,
702 2 + PageNumber
+ PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
),
703 (EFI_PHYSICAL_ADDRESS
*)&NewS3TableBase
706 if (EFI_ERROR(Status
)) {
707 ASSERT_EFI_ERROR (Status
);
711 CopyMem ((VOID
*)(UINTN
)NewS3TableBase
, (VOID
*)(UINTN
)S3TableBase
, TableLength
);
712 gBS
->FreePages (S3TableBase
, mS3BootScriptTablePtr
->TableMemoryPageNumber
);
714 mS3BootScriptTablePtr
->TableBase
= (UINT8
*)(UINTN
)NewS3TableBase
;
715 mS3BootScriptTablePtr
->TableMemoryPageNumber
= (UINT16
) (2 + PageNumber
+ PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
));
718 // calculate the the start address for the new entry.
720 NewEntryPtr
= mS3BootScriptTablePtr
->TableBase
+ TableLength
;
723 // update the table lenghth
725 mS3BootScriptTablePtr
->TableLength
= TableLength
+ EntryLength
;
728 // In the boot time, we will not append the termination entry to the boot script
729 // table until the callers think there is no boot time data that should be added and
730 // it is caller's responsibility to explicit call the CloseTable.
737 To get the start address from which a new runtime(after SmmReadyToLock) s3 boot script entry will write into.
738 In this case, it should be ensured that there is enough buffer to hold the entry.
740 @param EntryLength the new entry length.
742 @retval the address from which the a new s3 runtime(after SmmReadyToLock) script entry will write into
745 S3BootScriptGetRuntimeEntryAddAddress (
753 // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node.
755 if ((UINTN
) (mS3BootScriptTablePtr
->TableLength
+ EntryLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
)) <= (UINTN
) EFI_PAGES_TO_SIZE ((UINTN
) (mS3BootScriptTablePtr
->TableMemoryPageNumber
))) {
756 NewEntryPtr
= mS3BootScriptTablePtr
->TableBase
+ mS3BootScriptTablePtr
->TableLength
;
757 mS3BootScriptTablePtr
->TableLength
= mS3BootScriptTablePtr
->TableLength
+ EntryLength
;
759 // Append a terminate node on every insert
761 S3BootScriptInternalCloseTable ();
763 return (UINT8
*)NewEntryPtr
;
767 This function is to restore boot time boot script data from LockBox.
771 RestoreBootTimeDataFromLockBox (
779 // Restore boot time boot script data from LockBox.
781 LockBoxLength
= mS3BootScriptTablePtr
->BootTimeScriptLength
;
782 Status
= RestoreLockBox (
783 &mBootScriptDataBootTimeGuid
,
784 (VOID
*) mS3BootScriptTablePtr
->TableBase
,
787 ASSERT_EFI_ERROR (Status
);
790 // Update the data to BootScriptData LockBox.
792 Status
= UpdateLockBox (
793 &mBootScriptDataGuid
,
795 (VOID
*) mS3BootScriptTablePtr
->TableBase
,
798 ASSERT_EFI_ERROR (Status
);
801 // Update TableLength.
803 mS3BootScriptTablePtr
->TableLength
= (UINT32
) (mS3BootScriptTablePtr
->BootTimeScriptLength
- sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
807 To get the start address from which a new s3 boot script entry will write into.
809 @param EntryLength the new entry length.
811 @retval the address from which the a new s3 boot script entry will write into
814 S3BootScriptGetEntryAddAddress (
820 if (mS3BootScriptTablePtr
->SmmLocked
) {
822 // We need check InSmm, because after SmmReadyToLock, only SMM driver is allowed to write boot script.
824 if (!mS3BootScriptTablePtr
->InSmm
) {
826 // Add DEBUG ERROR, so that we can find it after SmmReadyToLock.
827 // Do not use ASSERT, because we may have test to invoke this interface.
829 DEBUG ((EFI_D_ERROR
, "FATAL ERROR: Set boot script outside SMM after SmmReadyToLock!!!\n"));
833 if (mS3BootScriptTablePtr
->BackFromS3
) {
835 // Back from S3, restore boot time boot script data from LockBox
836 // and set BackFromS3 flag back to FALSE.
838 RestoreBootTimeDataFromLockBox ();
839 mS3BootScriptTablePtr
->BackFromS3
= FALSE
;
842 NewEntryPtr
= S3BootScriptGetRuntimeEntryAddAddress (EntryLength
);
844 NewEntryPtr
= S3BootScriptGetBootTimeEntryAddAddress (EntryLength
);
851 Sync BootScript LockBox data.
853 @param Script The address from where the boot script has been added or updated.
863 UINT32 TotalScriptLength
;
865 if (!mS3BootScriptTablePtr
->SmmLocked
|| !mS3BootScriptTablePtr
->InSmm
) {
867 // If it is not after SmmReadyToLock in SMM,
873 ScriptOffset
= (UINT32
) (Script
- mS3BootScriptTablePtr
->TableBase
);
875 TotalScriptLength
= (UINT32
) (mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
878 // Update BootScriptData
879 // So in S3 resume, the data can be restored correctly.
881 Status
= UpdateLockBox (
882 &mBootScriptDataGuid
,
884 (VOID
*)((UINTN
)mS3BootScriptTablePtr
->TableBase
+ ScriptOffset
),
885 TotalScriptLength
- ScriptOffset
887 ASSERT_EFI_ERROR (Status
);
890 // Now the length field is updated, need sync to lockbox.
891 // So at S3 resume, the data can be restored correctly.
893 Status
= UpdateLockBox (
894 &mBootScriptDataGuid
,
895 OFFSET_OF (EFI_BOOT_SCRIPT_TABLE_HEADER
, TableLength
),
897 sizeof (TotalScriptLength
)
899 ASSERT_EFI_ERROR (Status
);
903 This is an function to close the S3 boot script table. The function could only be called in
904 BOOT time phase. To comply with the Framework spec definition on
905 EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable(), this function will fulfill following things:
906 1. Closes the specified boot script table
907 2. It allocates a new memory pool to duplicate all the boot scripts in the specified table.
908 Once this function is called, the table maintained by the library will be destroyed
909 after it is copied into the allocated pool.
910 3. Any attempts to add a script record after calling this function will cause a new table
911 to be created by the library.
912 4. The base address of the allocated pool will be returned in Address. Note that after
913 using the boot script table, the CALLER is responsible for freeing the pool that is allocated
916 In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is
917 for Framework Spec compatibility.
919 If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out
920 how to get the script to run at S3 resume because the boot script maintained by the lib will be
923 @return the base address of the new copy of the boot script table.
924 @note this function could only called in boot time phase
929 S3BootScriptCloseTable (
937 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
939 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
940 if (S3TableBase
== 0) {
944 // Append the termination record the S3 boot script table
946 S3BootScriptInternalCloseTable();
947 TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
949 // Allocate the buffer and copy the boot script to the buffer.
951 Status
= gBS
->AllocatePool (
956 if (EFI_ERROR (Status
)) {
959 CopyMem (Buffer
, S3TableBase
, TableLength
);
962 // Destroy the table maintained by the library so that the next write operation
963 // will write the record to the first entry of the table.
965 // Fill the table header.
966 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)S3TableBase
;
967 ScriptTableInfo
->OpCode
= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
;
968 ScriptTableInfo
->Length
= (UINT8
) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
969 ScriptTableInfo
->TableLength
= 0; // will be calculate at close the table
971 mS3BootScriptTablePtr
->TableLength
= sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
975 Save I/O write to boot script
977 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
978 @param Address The base address of the I/O operations.
979 @param Count The number of I/O operations to perform.
980 @param Buffer The source buffer from which to write data.
982 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
983 @retval RETURN_SUCCESS Opcode is added.
987 S3BootScriptSaveIoWrite (
988 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
998 EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite
;
1000 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1001 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE
) + (WidthInByte
* Count
));
1003 Script
= S3BootScriptGetEntryAddAddress (Length
);
1004 if (Script
== NULL
) {
1005 return RETURN_OUT_OF_RESOURCES
;
1010 ScriptIoWrite
.OpCode
= EFI_BOOT_SCRIPT_IO_WRITE_OPCODE
;
1011 ScriptIoWrite
.Length
= Length
;
1012 ScriptIoWrite
.Width
= Width
;
1013 ScriptIoWrite
.Address
= Address
;
1014 ScriptIoWrite
.Count
= (UINT32
) Count
;
1015 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoWrite
, sizeof(EFI_BOOT_SCRIPT_IO_WRITE
));
1016 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_WRITE
)), Buffer
, WidthInByte
* Count
);
1018 SyncBootScript (Script
);
1020 return RETURN_SUCCESS
;
1024 Adds a record for an I/O modify operation into a S3 boot script table
1026 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1027 @param Address The base address of the I/O operations.
1028 @param Data A pointer to the data to be OR-ed.
1029 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register
1031 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1032 @retval RETURN_SUCCESS Opcode is added.
1036 S3BootScriptSaveIoReadWrite (
1037 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1046 EFI_BOOT_SCRIPT_IO_READ_WRITE ScriptIoReadWrite
;
1048 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1049 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
) + (WidthInByte
* 2));
1051 Script
= S3BootScriptGetEntryAddAddress (Length
);
1052 if (Script
== NULL
) {
1053 return RETURN_OUT_OF_RESOURCES
;
1056 // Build script data
1058 ScriptIoReadWrite
.OpCode
= EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE
;
1059 ScriptIoReadWrite
.Length
= Length
;
1060 ScriptIoReadWrite
.Width
= Width
;
1061 ScriptIoReadWrite
.Address
= Address
;
1063 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoReadWrite
, sizeof(EFI_BOOT_SCRIPT_IO_READ_WRITE
));
1064 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
)), Data
, WidthInByte
);
1065 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
) + WidthInByte
), DataMask
, WidthInByte
);
1067 SyncBootScript (Script
);
1069 return RETURN_SUCCESS
;
1072 Adds a record for a memory write operation into a specified boot script table.
1074 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1075 @param Address The base address of the memory operations
1076 @param Count The number of memory operations to perform.
1077 @param Buffer The source buffer from which to write the data.
1079 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1080 @retval RETURN_SUCCESS Opcode is added.
1084 S3BootScriptSaveMemWrite (
1085 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1094 EFI_BOOT_SCRIPT_MEM_WRITE ScriptMemWrite
;
1096 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1097 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
) + (WidthInByte
* Count
));
1099 Script
= S3BootScriptGetEntryAddAddress (Length
);
1100 if (Script
== NULL
) {
1101 return RETURN_OUT_OF_RESOURCES
;
1104 // Build script data
1106 ScriptMemWrite
.OpCode
= EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE
;
1107 ScriptMemWrite
.Length
= Length
;
1108 ScriptMemWrite
.Width
= Width
;
1109 ScriptMemWrite
.Address
= Address
;
1110 ScriptMemWrite
.Count
= (UINT32
) Count
;
1112 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemWrite
, sizeof(EFI_BOOT_SCRIPT_MEM_WRITE
));
1113 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
)), Buffer
, WidthInByte
* Count
);
1115 SyncBootScript (Script
);
1117 return RETURN_SUCCESS
;
1120 Adds a record for a memory modify operation into a specified boot script table.
1122 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1123 @param Address The base address of the memory operations. Address needs alignment if required
1124 @param Data A pointer to the data to be OR-ed.
1125 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register.
1127 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1128 @retval RETURN_SUCCESS Opcode is added.
1132 S3BootScriptSaveMemReadWrite (
1133 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1142 EFI_BOOT_SCRIPT_MEM_READ_WRITE ScriptMemReadWrite
;
1144 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1145 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
) + (WidthInByte
* 2));
1147 Script
= S3BootScriptGetEntryAddAddress (Length
);
1148 if (Script
== NULL
) {
1149 return RETURN_OUT_OF_RESOURCES
;
1152 // Build script data
1154 ScriptMemReadWrite
.OpCode
= EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE
;
1155 ScriptMemReadWrite
.Length
= Length
;
1156 ScriptMemReadWrite
.Width
= Width
;
1157 ScriptMemReadWrite
.Address
= Address
;
1159 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemReadWrite
, sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
));
1160 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
)), Data
, WidthInByte
);
1161 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
) + WidthInByte
), DataMask
, WidthInByte
);
1163 SyncBootScript (Script
);
1165 return RETURN_SUCCESS
;
1168 Adds a record for a PCI configuration space write operation into a specified boot script table.
1170 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1171 @param Address The address within the PCI configuration space.
1172 @param Count The number of PCI operations to perform.
1173 @param Buffer The source buffer from which to write the data.
1175 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1176 @retval RETURN_SUCCESS Opcode is added.
1177 @note A known Limitations in the implementation which is 64bits operations are not supported.
1182 S3BootScriptSavePciCfgWrite (
1183 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1192 EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE ScriptPciWrite
;
1194 if (Width
== S3BootScriptWidthUint64
||
1195 Width
== S3BootScriptWidthFifoUint64
||
1196 Width
== S3BootScriptWidthFillUint64
) {
1197 return EFI_INVALID_PARAMETER
;
1200 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1201 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
) + (WidthInByte
* Count
));
1203 Script
= S3BootScriptGetEntryAddAddress (Length
);
1204 if (Script
== NULL
) {
1205 return RETURN_OUT_OF_RESOURCES
;
1208 // Build script data
1210 ScriptPciWrite
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE
;
1211 ScriptPciWrite
.Length
= Length
;
1212 ScriptPciWrite
.Width
= Width
;
1213 ScriptPciWrite
.Address
= Address
;
1214 ScriptPciWrite
.Count
= (UINT32
) Count
;
1216 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciWrite
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
));
1217 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
)), Buffer
, WidthInByte
* Count
);
1219 SyncBootScript (Script
);
1221 return RETURN_SUCCESS
;
1224 Adds a record for a PCI configuration space modify operation into a specified boot script table.
1226 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1227 @param Address The address within the PCI configuration space.
1228 @param Data A pointer to the data to be OR-ed.The size depends on Width.
1229 @param DataMask A pointer to the data mask to be AND-ed.
1231 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1232 @retval RETURN__SUCCESS Opcode is added.
1233 @note A known Limitations in the implementation which is 64bits operations are not supported.
1238 S3BootScriptSavePciCfgReadWrite (
1239 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1248 EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE ScriptPciReadWrite
;
1250 if (Width
== S3BootScriptWidthUint64
||
1251 Width
== S3BootScriptWidthFifoUint64
||
1252 Width
== S3BootScriptWidthFillUint64
) {
1253 return EFI_INVALID_PARAMETER
;
1256 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1257 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
) + (WidthInByte
* 2));
1259 Script
= S3BootScriptGetEntryAddAddress (Length
);
1260 if (Script
== NULL
) {
1261 return RETURN_OUT_OF_RESOURCES
;
1264 // Build script data
1266 ScriptPciReadWrite
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE
;
1267 ScriptPciReadWrite
.Length
= Length
;
1268 ScriptPciReadWrite
.Width
= Width
;
1269 ScriptPciReadWrite
.Address
= Address
;
1271 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciReadWrite
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
));
1272 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
)), Data
, WidthInByte
);
1274 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
) + WidthInByte
),
1279 SyncBootScript (Script
);
1281 return RETURN_SUCCESS
;
1284 Adds a record for a PCI configuration 2 space write operation into a specified boot script table.
1286 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1287 @param Segment The PCI segment number for Address.
1288 @param Address The address within the PCI configuration space.
1289 @param Count The number of PCI operations to perform.
1290 @param Buffer The source buffer from which to write the data.
1292 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1293 @retval RETURN_SUCCESS Opcode is added.
1294 @note A known Limitations in the implementation which is 64bits operations are not supported.
1299 S3BootScriptSavePciCfg2Write (
1300 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1310 EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE ScriptPciWrite2
;
1312 if (Width
== S3BootScriptWidthUint64
||
1313 Width
== S3BootScriptWidthFifoUint64
||
1314 Width
== S3BootScriptWidthFillUint64
) {
1315 return EFI_INVALID_PARAMETER
;
1318 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1319 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
) + (WidthInByte
* Count
));
1321 Script
= S3BootScriptGetEntryAddAddress (Length
);
1322 if (Script
== NULL
) {
1323 return RETURN_OUT_OF_RESOURCES
;
1326 // Build script data
1328 ScriptPciWrite2
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE
;
1329 ScriptPciWrite2
.Length
= Length
;
1330 ScriptPciWrite2
.Width
= Width
;
1331 ScriptPciWrite2
.Address
= Address
;
1332 ScriptPciWrite2
.Segment
= Segment
;
1333 ScriptPciWrite2
.Count
= (UINT32
)Count
;
1335 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciWrite2
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
));
1336 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
)), Buffer
, WidthInByte
* Count
);
1338 SyncBootScript (Script
);
1340 return RETURN_SUCCESS
;
1343 Adds a record for a PCI configuration 2 space modify operation into a specified boot script table.
1345 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
1346 @param Segment The PCI segment number for Address.
1347 @param Address The address within the PCI configuration space.
1348 @param Data A pointer to the data to be OR-ed. The size depends on Width.
1349 @param DataMask A pointer to the data mask to be AND-ed.
1351 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1352 @retval RETURN_SUCCESS Opcode is added.
1353 @note A known Limitations in the implementation which is 64bits operations are not supported.
1358 S3BootScriptSavePciCfg2ReadWrite (
1359 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1369 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE ScriptPciReadWrite2
;
1371 if (Width
== S3BootScriptWidthUint64
||
1372 Width
== S3BootScriptWidthFifoUint64
||
1373 Width
== S3BootScriptWidthFillUint64
) {
1374 return EFI_INVALID_PARAMETER
;
1377 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1378 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
) + (WidthInByte
* 2));
1380 Script
= S3BootScriptGetEntryAddAddress (Length
);
1381 if (Script
== NULL
) {
1382 return RETURN_OUT_OF_RESOURCES
;
1385 // Build script data
1387 ScriptPciReadWrite2
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE
;
1388 ScriptPciReadWrite2
.Length
= Length
;
1389 ScriptPciReadWrite2
.Width
= Width
;
1390 ScriptPciReadWrite2
.Segment
= Segment
;
1391 ScriptPciReadWrite2
.Address
= Address
;
1393 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciReadWrite2
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
));
1394 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
)), Data
, WidthInByte
);
1396 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
) + WidthInByte
),
1401 SyncBootScript (Script
);
1403 return RETURN_SUCCESS
;
1407 Checks the parameter of S3BootScriptSaveSmbusExecute().
1409 This function checks the input parameters of SmbusExecute(). If the input parameters are valid
1410 for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain
1411 error code based on the input SMBus bus protocol.
1413 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length,
1415 @param Operation Signifies which particular SMBus hardware protocol instance that
1416 it will use to execute the SMBus transactions. This SMBus
1417 hardware protocol is defined by the SMBus Specification and is
1419 @param Length Signifies the number of bytes that this operation will do. The
1420 maximum number of bytes can be revision specific and operation
1421 specific. This field will contain the actual number of bytes that
1422 are executed for this operation. Not all operations require this
1424 @param Buffer Contains the value of data to execute to the SMBus slave device.
1425 Not all operations require this argument. The length of this
1426 buffer is identified by Length.
1428 @retval EFI_SUCCESS All the parameters are valid for the corresponding SMBus bus
1430 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.
1431 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead
1432 and EfiSmbusQuickWrite. Length is outside the range of valid
1434 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.
1435 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.
1440 IN UINTN SmBusAddress
,
1441 IN EFI_SMBUS_OPERATION Operation
,
1442 IN OUT UINTN
*Length
,
1448 EFI_SMBUS_DEVICE_COMMAND Command
;
1451 Command
= SMBUS_LIB_COMMAND (SmBusAddress
);
1452 PecCheck
= SMBUS_LIB_PEC (SmBusAddress
);
1454 // Set default value to be 2:
1455 // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.
1458 Status
= EFI_SUCCESS
;
1459 switch (Operation
) {
1460 case EfiSmbusQuickRead
:
1461 case EfiSmbusQuickWrite
:
1462 if (PecCheck
|| Command
!= 0) {
1463 return EFI_UNSUPPORTED
;
1466 case EfiSmbusReceiveByte
:
1467 case EfiSmbusSendByte
:
1469 return EFI_UNSUPPORTED
;
1472 // Cascade to check length parameter.
1474 case EfiSmbusReadByte
:
1475 case EfiSmbusWriteByte
:
1478 // Cascade to check length parameter.
1480 case EfiSmbusReadWord
:
1481 case EfiSmbusWriteWord
:
1482 case EfiSmbusProcessCall
:
1483 if (Buffer
== NULL
|| Length
== NULL
) {
1484 return EFI_INVALID_PARAMETER
;
1485 } else if (*Length
< RequiredLen
) {
1486 Status
= EFI_BUFFER_TOO_SMALL
;
1488 *Length
= RequiredLen
;
1490 case EfiSmbusReadBlock
:
1491 case EfiSmbusWriteBlock
:
1492 case EfiSmbusBWBRProcessCall
:
1493 if ((Buffer
== NULL
) ||
1495 (*Length
< MIN_SMBUS_BLOCK_LEN
) ||
1496 (*Length
> MAX_SMBUS_BLOCK_LEN
)) {
1497 return EFI_INVALID_PARAMETER
;
1501 return EFI_INVALID_PARAMETER
;
1507 Adds a record for an SMBus command execution into a specified boot script table.
1509 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length, and PEC.
1510 @param Operation Indicates which particular SMBus protocol it will use to execute the SMBus
1512 @param Length A pointer to signify the number of bytes that this operation will do.
1513 @param Buffer Contains the value of data to execute to the SMBUS slave device.
1515 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1516 @retval RETURN_SUCCESS Opcode is added.
1520 S3BootScriptSaveSmbusExecute (
1521 IN UINTN SmBusAddress
,
1522 IN EFI_SMBUS_OPERATION Operation
,
1531 EFI_BOOT_SCRIPT_SMBUS_EXECUTE ScriptSmbusExecute
;
1533 if (Length
== NULL
) {
1536 BufferLength
= *Length
;
1539 Status
= CheckParameters (SmBusAddress
, Operation
, &BufferLength
, Buffer
);
1540 if (EFI_ERROR (Status
)) {
1544 DataSize
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
) + BufferLength
);
1546 Script
= S3BootScriptGetEntryAddAddress (DataSize
);
1547 if (Script
== NULL
) {
1548 return RETURN_OUT_OF_RESOURCES
;
1551 // Build script data
1553 ScriptSmbusExecute
.OpCode
= EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE
;
1554 ScriptSmbusExecute
.Length
= DataSize
;
1555 ScriptSmbusExecute
.SmBusAddress
= (UINT64
) SmBusAddress
;
1556 ScriptSmbusExecute
.Operation
= Operation
;
1557 ScriptSmbusExecute
.DataSize
= (UINT32
) BufferLength
;
1559 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptSmbusExecute
, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
));
1561 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
)),
1566 SyncBootScript (Script
);
1568 return RETURN_SUCCESS
;
1571 Adds a record for an execution stall on the processor into a specified boot script table.
1573 @param Duration Duration in microseconds of the stall
1575 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1576 @retval RETURN_SUCCESS Opcode is added.
1580 S3BootScriptSaveStall (
1586 EFI_BOOT_SCRIPT_STALL ScriptStall
;
1588 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_STALL
));
1590 Script
= S3BootScriptGetEntryAddAddress (Length
);
1591 if (Script
== NULL
) {
1592 return RETURN_OUT_OF_RESOURCES
;
1595 // Build script data
1597 ScriptStall
.OpCode
= EFI_BOOT_SCRIPT_STALL_OPCODE
;
1598 ScriptStall
.Length
= Length
;
1599 ScriptStall
.Duration
= Duration
;
1601 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptStall
, sizeof (EFI_BOOT_SCRIPT_STALL
));
1603 SyncBootScript (Script
);
1605 return RETURN_SUCCESS
;
1608 Adds a record for dispatching specified arbitrary code into a specified boot script table.
1610 @param EntryPoint Entry point of the code to be dispatched.
1611 @param Context Argument to be passed into the EntryPoint of the code to be dispatched.
1613 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1614 @retval RETURN_SUCCESS Opcode is added.
1618 S3BootScriptSaveDispatch2 (
1619 IN VOID
*EntryPoint
,
1625 EFI_BOOT_SCRIPT_DISPATCH_2 ScriptDispatch2
;
1626 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2
));
1628 Script
= S3BootScriptGetEntryAddAddress (Length
);
1629 if (Script
== NULL
) {
1630 return RETURN_OUT_OF_RESOURCES
;
1633 // Build script data
1635 ScriptDispatch2
.OpCode
= EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE
;
1636 ScriptDispatch2
.Length
= Length
;
1637 ScriptDispatch2
.EntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)EntryPoint
;
1638 ScriptDispatch2
.Context
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)Context
;
1640 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptDispatch2
, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2
));
1642 SyncBootScript (Script
);
1644 return RETURN_SUCCESS
;
1648 Adds a record for memory reads of the memory location and continues when the exit criteria is
1649 satisfied or after a defined duration.
1651 Please aware, below interface is different with PI specification, Vol 5:
1652 EFI_S3_SAVE_STATE_PROTOCOL.Write() for EFI_BOOT_SCRIPT_MEM_POLL_OPCODE.
1653 "Duration" below is microseconds, while "Delay" in PI specification means
1654 the number of 100ns units to poll.
1656 @param Width The width of the memory operations.
1657 @param Address The base address of the memory operations.
1658 @param BitMask A pointer to the bit mask to be AND-ed with the data read from the register.
1659 @param BitValue A pointer to the data value after to be Masked.
1660 @param Duration Duration in microseconds of the stall.
1661 @param LoopTimes The times of the register polling.
1663 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1664 @retval RETURN_SUCCESS Opcode is added.
1669 S3BootScriptSaveMemPoll (
1670 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1681 EFI_BOOT_SCRIPT_MEM_POLL ScriptMemPoll
;
1683 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1685 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL
) + (WidthInByte
* 2));
1687 Script
= S3BootScriptGetEntryAddAddress (Length
);
1688 if (Script
== NULL
) {
1689 return RETURN_OUT_OF_RESOURCES
;
1692 // Build script data
1694 ScriptMemPoll
.OpCode
= EFI_BOOT_SCRIPT_MEM_POLL_OPCODE
;
1695 ScriptMemPoll
.Length
= Length
;
1696 ScriptMemPoll
.Width
= Width
;
1697 ScriptMemPoll
.Address
= Address
;
1698 ScriptMemPoll
.Duration
= Duration
;
1699 ScriptMemPoll
.LoopTimes
= LoopTimes
;
1701 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_POLL
)), BitValue
, WidthInByte
);
1702 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_POLL
) + WidthInByte
), BitMask
, WidthInByte
);
1703 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemPoll
, sizeof (EFI_BOOT_SCRIPT_MEM_POLL
));
1705 SyncBootScript (Script
);
1707 return RETURN_SUCCESS
;
1710 Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only
1711 used for debugging script issues.
1713 @param InformationLength Length of the data in bytes
1714 @param Information Information to be logged in the boot scrpit
1716 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1717 @retval RETURN_SUCCESS Opcode is added.
1722 S3BootScriptSaveInformation (
1723 IN UINT32 InformationLength
,
1724 IN VOID
*Information
1729 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation
;
1731 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_INFORMATION
) + InformationLength
);
1733 Script
= S3BootScriptGetEntryAddAddress (Length
);
1734 if (Script
== NULL
) {
1735 return RETURN_OUT_OF_RESOURCES
;
1738 // Build script data
1740 ScriptInformation
.OpCode
= EFI_BOOT_SCRIPT_INFORMATION_OPCODE
;
1741 ScriptInformation
.Length
= Length
;
1744 ScriptInformation
.InformationLength
= InformationLength
;
1746 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptInformation
, sizeof (EFI_BOOT_SCRIPT_INFORMATION
));
1747 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_INFORMATION
)), (VOID
*) Information
, (UINTN
) InformationLength
);
1749 SyncBootScript (Script
);
1751 return RETURN_SUCCESS
;
1755 Store a string in the boot script table. This opcode is a no-op on dispatch and is only
1756 used for debugging script issues.
1758 @param String The string to save to boot script table
1760 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1761 @retval RETURN_SUCCESS Opcode is added.
1766 S3BootScriptSaveInformationAsciiString (
1767 IN CONST CHAR8
*String
1770 return S3BootScriptSaveInformation (
1771 (UINT32
) AsciiStrLen (String
) + 1,
1776 Adds a record for dispatching specified arbitrary code into a specified boot script table.
1778 @param EntryPoint Entry point of the code to be dispatched.
1780 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1781 @retval RETURN_SUCCESS Opcode is added.
1785 S3BootScriptSaveDispatch (
1791 EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch
;
1793 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_DISPATCH
));
1795 Script
= S3BootScriptGetEntryAddAddress (Length
);
1796 if (Script
== NULL
) {
1797 return RETURN_OUT_OF_RESOURCES
;
1800 // Build script data
1802 ScriptDispatch
.OpCode
= EFI_BOOT_SCRIPT_DISPATCH_OPCODE
;
1803 ScriptDispatch
.Length
= Length
;
1804 ScriptDispatch
.EntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)EntryPoint
;
1806 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptDispatch
, sizeof (EFI_BOOT_SCRIPT_DISPATCH
));
1808 SyncBootScript (Script
);
1810 return RETURN_SUCCESS
;
1814 Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a
1817 @param Width The width of the I/O operations.
1818 @param Address The base address of the I/O operations.
1819 @param Data The comparison value used for the polling exit criteria.
1820 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1821 in Data are ignored when polling the memory address.
1822 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1823 granularity so the delay may be longer.
1825 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1826 @retval RETURN_SUCCESS Opcode is added.
1831 S3BootScriptSaveIoPoll (
1832 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1842 EFI_BOOT_SCRIPT_IO_POLL ScriptIoPoll
;
1845 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1846 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + (WidthInByte
* 2));
1848 Script
= S3BootScriptGetEntryAddAddress (Length
);
1849 if (Script
== NULL
) {
1850 return RETURN_OUT_OF_RESOURCES
;
1853 // Build script data
1855 ScriptIoPoll
.OpCode
= EFI_BOOT_SCRIPT_IO_POLL_OPCODE
;
1856 ScriptIoPoll
.Length
= (UINT8
) (sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + (WidthInByte
* 2));
1857 ScriptIoPoll
.Width
= Width
;
1858 ScriptIoPoll
.Address
= Address
;
1859 ScriptIoPoll
.Delay
= Delay
;
1861 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoPoll
, sizeof (EFI_BOOT_SCRIPT_IO_POLL
));
1862 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_IO_POLL
)), Data
, WidthInByte
);
1863 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1865 SyncBootScript (Script
);
1867 return RETURN_SUCCESS
;
1871 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1872 after a defined duration.
1874 @param Width The width of the I/O operations.
1875 @param Address The address within the PCI configuration space.
1876 @param Data The comparison value used for the polling exit criteria.
1877 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1878 in Data are ignored when polling the memory address
1879 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1880 granularity so the delay may be longer.
1882 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1883 @retval RETURN_SUCCESS Opcode is added.
1884 @note A known Limitations in the implementation which is 64bits operations are not supported.
1889 S3BootScriptSavePciPoll (
1890 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1900 EFI_BOOT_SCRIPT_PCI_CONFIG_POLL ScriptPciPoll
;
1902 if (Width
== S3BootScriptWidthUint64
||
1903 Width
== S3BootScriptWidthFifoUint64
||
1904 Width
== S3BootScriptWidthFillUint64
) {
1905 return EFI_INVALID_PARAMETER
;
1908 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1909 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + (WidthInByte
* 2));
1911 Script
= S3BootScriptGetEntryAddAddress (Length
);
1912 if (Script
== NULL
) {
1913 return RETURN_OUT_OF_RESOURCES
;
1916 // Build script data
1918 ScriptPciPoll
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE
;
1919 ScriptPciPoll
.Length
= (UINT8
) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + (WidthInByte
* 2));
1920 ScriptPciPoll
.Width
= Width
;
1921 ScriptPciPoll
.Address
= Address
;
1922 ScriptPciPoll
.Delay
= Delay
;
1924 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciPoll
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
));
1925 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
)), Data
, WidthInByte
);
1926 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1928 SyncBootScript (Script
);
1930 return RETURN_SUCCESS
;
1933 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1934 after a defined duration.
1936 @param Width The width of the I/O operations.
1937 @param Segment The PCI segment number for Address.
1938 @param Address The address within the PCI configuration space.
1939 @param Data The comparison value used for the polling exit criteria.
1940 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1941 in Data are ignored when polling the memory address
1942 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1943 granularity so the delay may be longer.
1945 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1946 @retval RETURN_SUCCESS Opcode is added.
1947 @note A known Limitations in the implementation which is 64bits operations are not supported.
1952 S3BootScriptSavePci2Poll (
1953 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1964 EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL ScriptPci2Poll
;
1966 if (Width
== S3BootScriptWidthUint64
||
1967 Width
== S3BootScriptWidthFifoUint64
||
1968 Width
== S3BootScriptWidthFillUint64
) {
1969 return EFI_INVALID_PARAMETER
;
1972 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1973 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + (WidthInByte
* 2));
1975 Script
= S3BootScriptGetEntryAddAddress (Length
);
1976 if (Script
== NULL
) {
1977 return RETURN_OUT_OF_RESOURCES
;
1980 // Build script data
1982 ScriptPci2Poll
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE
;
1983 ScriptPci2Poll
.Length
= (UINT8
) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + (WidthInByte
* 2));
1984 ScriptPci2Poll
.Width
= Width
;
1985 ScriptPci2Poll
.Segment
= Segment
;
1986 ScriptPci2Poll
.Address
= Address
;
1987 ScriptPci2Poll
.Delay
= Delay
;
1989 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPci2Poll
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
));
1990 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
)), Data
, WidthInByte
);
1991 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1993 SyncBootScript (Script
);
1995 return RETURN_SUCCESS
;
1998 Do the calculation of start address from which a new s3 boot script entry will write into.
2000 @param EntryLength The new entry length.
2001 @param Position specifies the position in the boot script table where the opcode will be
2002 inserted, either before or after, depending on BeforeOrAfter.
2003 @param BeforeOrAfter The flag to indicate to insert the nod before or after the position.
2004 This parameter is effective when InsertFlag is TRUE
2005 @param Script return out the position from which the a new s3 boot script entry will write into
2008 S3BootScriptCalculateInsertAddress (
2009 IN UINT8 EntryLength
,
2010 IN VOID
*Position OPTIONAL
,
2011 IN BOOLEAN BeforeOrAfter OPTIONAL
,
2017 UINTN PositionOffset
;
2018 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
2020 // The entry inserting to table is already added to the end of the table
2022 TableLength
= mS3BootScriptTablePtr
->TableLength
- EntryLength
;
2023 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
2025 // calculate the Position offset
2027 if (Position
!= NULL
) {
2028 PositionOffset
= (UINTN
) ((UINT8
*)Position
- S3TableBase
);
2031 // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.
2033 if (!BeforeOrAfter
) {
2034 CopyMem ((VOID
*)&ScriptHeader
, Position
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
2035 PositionOffset
+= (ScriptHeader
.Length
);
2038 // Insert the node before the adjusted Position
2040 CopyMem (S3TableBase
+PositionOffset
+EntryLength
, S3TableBase
+PositionOffset
, TableLength
- PositionOffset
);
2042 // calculate the the start address for the new entry.
2044 *Script
= S3TableBase
+ PositionOffset
;
2047 if (!BeforeOrAfter
) {
2049 // Insert the node to the end of the table
2051 *Script
= S3TableBase
+ TableLength
;
2054 // Insert the node to the beginning of the table
2056 PositionOffset
= (UINTN
) sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
);
2057 CopyMem (S3TableBase
+PositionOffset
+EntryLength
, S3TableBase
+PositionOffset
, TableLength
- PositionOffset
);
2058 *Script
= S3TableBase
+ PositionOffset
;
2063 Move the last boot script entry to the position
2065 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
2066 in the boot script table specified by Position. If Position is NULL or points to
2067 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
2068 of the table (if FALSE).
2069 @param Position On entry, specifies the position in the boot script table where the opcode will be
2070 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
2071 the position of the inserted opcode in the boot script table.
2073 @retval RETURN_OUT_OF_RESOURCES The table is not available.
2074 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
2075 @retval RETURN_SUCCESS Opcode is inserted.
2079 S3BootScriptMoveLastOpcode (
2080 IN BOOLEAN BeforeOrAfter
,
2081 IN OUT VOID
**Position OPTIONAL
2088 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
2089 BOOLEAN ValidatePosition
;
2091 UINT8 TempBootScriptEntry
[BOOT_SCRIPT_NODE_MAX_LENGTH
];
2093 ValidatePosition
= FALSE
;
2094 TempPosition
= (Position
== NULL
) ? NULL
:(*Position
);
2097 // Check that the script is initialized and synced without adding an entry to the script.
2099 Script
= S3BootScriptGetEntryAddAddress (0);
2100 if (Script
== NULL
) {
2101 return RETURN_OUT_OF_RESOURCES
;
2103 Script
= mS3BootScriptTablePtr
->TableBase
;
2105 StartAddress
= (UINTN
) Script
;
2106 TableLength
= mS3BootScriptTablePtr
->TableLength
;
2107 Script
= Script
+ sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
);
2108 LastOpcode
= Script
;
2110 // Find the last boot Script Entry which is not the terminate node
2112 while ((UINTN
) Script
< (UINTN
) (StartAddress
+ TableLength
)) {
2113 CopyMem ((VOID
*)&ScriptHeader
, Script
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
2114 if (TempPosition
!= NULL
&& TempPosition
== Script
) {
2116 // If the position is specified, the position must be pointed to a boot script entry start address.
2118 ValidatePosition
= TRUE
;
2120 if (ScriptHeader
.OpCode
!= S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE
) {
2121 LastOpcode
= Script
;
2123 Script
= Script
+ ScriptHeader
.Length
;
2126 // If the position is specified, but not the start of a boot script entry, it is a invalid input
2128 if (TempPosition
!= NULL
&& !ValidatePosition
) {
2129 return RETURN_INVALID_PARAMETER
;
2132 CopyMem ((VOID
*)&ScriptHeader
, LastOpcode
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
2134 CopyMem((VOID
*)TempBootScriptEntry
, LastOpcode
, ScriptHeader
.Length
);
2136 // Find the right position to write the node in
2138 S3BootScriptCalculateInsertAddress (
2139 ScriptHeader
.Length
,
2145 // Copy the node to Boot script table
2147 CopyMem((VOID
*)Script
, (VOID
*)TempBootScriptEntry
, ScriptHeader
.Length
);
2149 SyncBootScript (Script
);
2152 // return out the Position
2154 if (Position
!= NULL
) {
2157 return RETURN_SUCCESS
;
2160 Create a Label node in the boot script table.
2162 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
2163 in the boot script table specified by Position. If Position is NULL or points to
2164 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
2165 of the table (if FALSE).
2166 @param Position On entry, specifies the position in the boot script table where the opcode will be
2167 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
2168 the position of the inserted opcode in the boot script table.
2169 @param InformationLength Length of the label in bytes
2170 @param Information Label to be logged in the boot scrpit
2172 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
2173 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
2174 @retval RETURN_SUCCESS Opcode is added.
2179 S3BootScriptLabelInternal (
2180 IN BOOLEAN BeforeOrAfter
,
2181 IN OUT VOID
**Position OPTIONAL
,
2182 IN UINT32 InformationLength
,
2183 IN CONST CHAR8
*Information
2188 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation
;
2190 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_INFORMATION
) + InformationLength
);
2192 Script
= S3BootScriptGetEntryAddAddress (Length
);
2193 if (Script
== NULL
) {
2194 return RETURN_OUT_OF_RESOURCES
;
2197 // Build script data
2199 ScriptInformation
.OpCode
= S3_BOOT_SCRIPT_LIB_LABEL_OPCODE
;
2200 ScriptInformation
.Length
= Length
;
2203 ScriptInformation
.InformationLength
= InformationLength
;
2205 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptInformation
, sizeof (EFI_BOOT_SCRIPT_INFORMATION
));
2206 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_INFORMATION
)), (VOID
*) Information
, (UINTN
) InformationLength
);
2208 SyncBootScript (Script
);
2210 return S3BootScriptMoveLastOpcode (BeforeOrAfter
, Position
);
2214 Find a label within the boot script table and, if not present, optionally create it.
2216 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE)
2217 or after (FALSE) the position in the boot script table
2218 specified by Position.
2219 @param CreateIfNotFound Specifies whether the label will be created if the label
2220 does not exists (TRUE) or not (FALSE).
2221 @param Position On entry, specifies the position in the boot script table
2222 where the opcode will be inserted, either before or after,
2223 depending on BeforeOrAfter. On exit, specifies the position
2224 of the inserted opcode in the boot script table.
2225 @param Label Points to the label which will be inserted in the boot script table.
2227 @retval EFI_SUCCESS The operation succeeded. A record was added into the
2228 specified script table.
2229 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
2230 If the opcode is unknow or not supported because of the PCD
2232 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
2238 IN BOOLEAN BeforeOrAfter
,
2239 IN BOOLEAN CreateIfNotFound
,
2240 IN OUT VOID
**Position OPTIONAL
,
2241 IN CONST CHAR8
*Label
2247 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
2248 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader
;
2253 if (Label
== NULL
) {
2254 return EFI_INVALID_PARAMETER
;
2257 // Check empty Label
2259 if (Label
[0] == '\0') {
2260 return EFI_INVALID_PARAMETER
;
2264 // Check that the script is initialized and synced without adding an entry to the script.
2265 // The code must search for the label first before it knows if a new entry needs
2268 Script
= S3BootScriptGetEntryAddAddress (0);
2269 if (Script
== NULL
) {
2270 return RETURN_OUT_OF_RESOURCES
;
2274 // Check the header and search for existing label.
2276 Script
= mS3BootScriptTablePtr
->TableBase
;
2277 CopyMem ((VOID
*)&TableHeader
, Script
, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
));
2278 if (TableHeader
.OpCode
!= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
) {
2279 return EFI_INVALID_PARAMETER
;
2281 StartAddress
= (UINTN
) Script
;
2282 TableLength
= mS3BootScriptTablePtr
->TableLength
;
2283 Script
= Script
+ TableHeader
.Length
;
2284 while ((UINTN
) Script
< (UINTN
) (StartAddress
+ TableLength
)) {
2286 CopyMem ((VOID
*)&ScriptHeader
, Script
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
2287 if (ScriptHeader
.OpCode
== S3_BOOT_SCRIPT_LIB_LABEL_OPCODE
) {
2288 if (AsciiStrCmp ((CHAR8
*)(UINTN
)(Script
+sizeof(EFI_BOOT_SCRIPT_INFORMATION
)), Label
) == 0) {
2289 (*Position
) = Script
;
2293 Script
= Script
+ ScriptHeader
.Length
;
2295 if (CreateIfNotFound
) {
2296 LabelLength
= (UINT32
)AsciiStrSize(Label
);
2297 return S3BootScriptLabelInternal (BeforeOrAfter
,Position
, LabelLength
, Label
);
2299 return EFI_NOT_FOUND
;
2304 Compare two positions in the boot script table and return their relative position.
2305 @param Position1 The positions in the boot script table to compare
2306 @param Position2 The positions in the boot script table to compare
2307 @param RelativePosition On return, points to the result of the comparison
2309 @retval EFI_SUCCESS The operation succeeded. A record was added into the
2310 specified script table.
2311 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
2312 If the opcode is unknow or not supported because of the PCD
2314 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
2319 S3BootScriptCompare (
2320 IN UINT8
*Position1
,
2321 IN UINT8
*Position2
,
2322 OUT UINTN
*RelativePosition
2328 if (RelativePosition
== NULL
) {
2329 return EFI_INVALID_PARAMETER
;
2333 // Check that the script is initialized and synced without adding an entry to the script.
2335 Script
= S3BootScriptGetEntryAddAddress (0);
2336 if (Script
== NULL
) {
2337 return RETURN_OUT_OF_RESOURCES
;
2339 Script
= mS3BootScriptTablePtr
->TableBase
;
2342 // mS3BootScriptTablePtr->TableLength does not include the termination node, so add it up
2344 TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
2345 if (Position1
< Script
|| Position1
> Script
+TableLength
) {
2346 return EFI_INVALID_PARAMETER
;
2348 if (Position2
< Script
|| Position2
> Script
+TableLength
) {
2349 return EFI_INVALID_PARAMETER
;
2351 *RelativePosition
= (Position1
< Position2
)?-1:((Position1
== Position2
)?0:1);