2 Save the S3 data to S3 boot script.
4 Copyright (c) 2006 - 2012, 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 |
28 +------------------------------+ | |
30 +------------------------------+<-- |
31 | EFI_BOOT_SCRIPT_TABLE_HEADER | |
32 | TableLength |----|--
33 +------------------------------+ | |
35 +------------------------------+<---- |
36 | EFI_BOOT_SCRIPT_TERMINATE | |
37 +------------------------------+<------
41 SCRIPT_TABLE_PRIVATE_DATA
*mS3BootScriptTablePtr
;
42 EFI_EVENT mEnterRuntimeEvent
;
44 // Allocate local copy in SMM because we can not use mS3BootScriptTablePtr when we AtRuntime in InSmm.
46 SCRIPT_TABLE_PRIVATE_DATA mS3BootScriptTable
;
49 EFI_GUID mBootScriptDataGuid
= {
50 0xaea6b965, 0xdcf5, 0x4311, { 0xb4, 0xb8, 0xf, 0x12, 0x46, 0x44, 0x94, 0xd2 }
53 EFI_GUID mBootScriptHeaderDataGuid
= {
54 0x1810ab4a, 0x2314, 0x4df6, { 0x81, 0xeb, 0x67, 0xc6, 0xec, 0x5, 0x85, 0x91 }
58 This is an internal function to add a terminate node the entry, recalculate the table
59 length and fill into the table.
61 @return the base address of the boot script tble.
64 S3BootScriptInternalCloseTable (
69 EFI_BOOT_SCRIPT_TERMINATE ScriptTerminate
;
70 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
71 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
73 if (S3TableBase
== NULL
) {
75 // the table is not exist
80 // Append the termination entry.
82 ScriptTerminate
.OpCode
= S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE
;
83 ScriptTerminate
.Length
= (UINT8
) sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
84 CopyMem (mS3BootScriptTablePtr
->TableBase
+ mS3BootScriptTablePtr
->TableLength
, &ScriptTerminate
, sizeof (EFI_BOOT_SCRIPT_TERMINATE
));
86 // fill the table length
88 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)(mS3BootScriptTablePtr
->TableBase
);
89 ScriptTableInfo
->TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
95 // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to
96 // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE). Because
97 // maybe in runtime, we still need add entries into the table, and the runtime entry should be
98 // added start before this TERMINATE node.
103 This function save boot script data to LockBox.
104 1. BootSriptPrivate data, BootScript data - Image and DispatchContext are handled by platform.
105 2. BootScriptExecutor, BootScriptExecutor context
106 - ACPI variable - (PI version) sould be handled by SMM driver. S3 Page table is handled here.
107 - ACPI variable - framework version is already handled by Framework CPU driver.
110 SaveBootScriptDataToLockBox (
117 // mS3BootScriptTablePtr->TableLength does not include EFI_BOOT_SCRIPT_TERMINATE, because we need add entry at runtime.
118 // Save all info here, just in case that no one will add boot script entry in SMM.
120 Status
= SaveLockBox (
121 &mBootScriptDataGuid
,
122 (VOID
*)mS3BootScriptTablePtr
->TableBase
,
123 mS3BootScriptTablePtr
->TableLength
+ sizeof(EFI_BOOT_SCRIPT_TERMINATE
)
125 ASSERT_EFI_ERROR (Status
);
127 Status
= SetLockBoxAttributes (&mBootScriptDataGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
128 ASSERT_EFI_ERROR (Status
);
131 // Just need save TableBase.
132 // Do not update other field because they will NOT be used in S3.
134 Status
= SaveLockBox (
135 &mBootScriptHeaderDataGuid
,
136 (VOID
*)&mS3BootScriptTablePtr
->TableBase
,
137 sizeof(mS3BootScriptTablePtr
->TableBase
)
139 ASSERT_EFI_ERROR (Status
);
141 Status
= SetLockBoxAttributes (&mBootScriptHeaderDataGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
142 ASSERT_EFI_ERROR (Status
);
146 This is the Event call back function to notify the Library the system is entering
149 @param Event Pointer to this event
150 @param Context Event hanlder private data
154 S3BootScriptEventCallBack (
163 // Try to locate it because EfiCreateProtocolNotifyEvent will trigger it once when registration.
164 // Just return if it is not found.
166 Status
= gBS
->LocateProtocol (
167 &gEfiDxeSmmReadyToLockProtocolGuid
,
171 if (EFI_ERROR (Status
)) {
176 // Here we should tell the library that we are enter into runtime phase. and
177 // the memory page number occupied by the table should not grow anymore.
179 if (!mS3BootScriptTablePtr
->AtRuntime
) {
181 // In boot time, we need not write the terminate node when adding a node to boot scipt table
182 // or else, that will impact the performance. However, in runtime, we should append terminate
183 // node on every add to boot script table
185 S3BootScriptInternalCloseTable ();
186 mS3BootScriptTablePtr
->AtRuntime
= TRUE
;
189 // Save BootScript data to lockbox
191 SaveBootScriptDataToLockBox ();
195 This is the Event call back function is triggered in SMM to notify the Library the system is entering
196 run time phase and set InSmm flag.
198 @param Protocol Points to the protocol's unique identifier
199 @param Interface Points to the interface instance
200 @param Handle The handle on which the interface was installed
202 @retval EFI_SUCCESS SmmEventCallback runs successfully
206 S3BootScriptSmmEventCallBack (
207 IN CONST EFI_GUID
*Protocol
,
213 // Check if it is already done
215 if (mS3BootScriptTablePtr
== &mS3BootScriptTable
) {
220 // Last chance to call-out, just make sure AtRuntime is set
222 S3BootScriptEventCallBack (NULL
, NULL
);
227 CopyMem (&mS3BootScriptTable
, mS3BootScriptTablePtr
, sizeof(*mS3BootScriptTablePtr
));
229 // We should not use ACPINvs copy, because it is not safe.
231 mS3BootScriptTablePtr
= &mS3BootScriptTable
;
234 // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.
235 // InSmm will only be checked if AtRuntime is TRUE.
237 mS3BootScriptTablePtr
->InSmm
= TRUE
;
240 // Record LockBoxLength
242 mLockBoxLength
= mS3BootScriptTable
.TableLength
+ sizeof(EFI_BOOT_SCRIPT_TERMINATE
);
249 this function just identify it is a smm driver or non-smm driver linked against
252 @param ImageHandle The firmware allocated handle for the EFI image.
253 @param SystemTable A pointer to the EFI System Table.
255 @retval RETURN_SUCCESS Allocate the global memory space to store S3 boot script table private data
256 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
260 S3BootScriptLibInitialize (
261 IN EFI_HANDLE ImageHandle
,
262 IN EFI_SYSTEM_TABLE
*SystemTable
266 SCRIPT_TABLE_PRIVATE_DATA
*S3TablePtr
;
268 EFI_SMM_BASE2_PROTOCOL
*SmmBase2
;
270 EFI_SMM_SYSTEM_TABLE2
*Smst
;
271 EFI_PHYSICAL_ADDRESS Buffer
;
273 S3TablePtr
= (SCRIPT_TABLE_PRIVATE_DATA
*)(UINTN
)PcdGet64(PcdS3BootScriptTablePrivateDataPtr
);
275 // The Boot script private data is not be initialized. create it
277 if (S3TablePtr
== 0) {
278 Buffer
= SIZE_4GB
- 1;
279 Status
= gBS
->AllocatePages (
282 EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA
)),
285 if (EFI_ERROR (Status
)) {
286 return RETURN_OUT_OF_RESOURCES
;
288 S3TablePtr
= (VOID
*) (UINTN
) Buffer
;
290 PcdSet64 (PcdS3BootScriptTablePrivateDataPtr
, (UINT64
) (UINTN
)S3TablePtr
);
291 ZeroMem (S3TablePtr
, sizeof(SCRIPT_TABLE_PRIVATE_DATA
));
293 // create event to notify the library system enter the runtime phase
295 mEnterRuntimeEvent
= EfiCreateProtocolNotifyEvent (
296 &gEfiDxeSmmReadyToLockProtocolGuid
,
298 S3BootScriptEventCallBack
,
302 ASSERT (mEnterRuntimeEvent
!= NULL
);
304 mS3BootScriptTablePtr
= S3TablePtr
;
307 // Get InSmm, we need to register SmmReadyToLock if this library is linked to SMM driver.
309 Status
= gBS
->LocateProtocol (&gEfiSmmBase2ProtocolGuid
, NULL
, (VOID
**) &SmmBase2
);
310 if (EFI_ERROR (Status
)) {
311 return RETURN_SUCCESS
;
313 Status
= SmmBase2
->InSmm (SmmBase2
, &InSmm
);
314 if (EFI_ERROR (Status
)) {
315 return RETURN_SUCCESS
;
318 return RETURN_SUCCESS
;
321 // Good, we are in SMM
323 Status
= SmmBase2
->GetSmstLocation (SmmBase2
, &Smst
);
324 if (EFI_ERROR (Status
)) {
325 return RETURN_SUCCESS
;
329 // Then register event after lock
332 Status
= Smst
->SmmRegisterProtocolNotify (
333 &gEfiSmmReadyToLockProtocolGuid
,
334 S3BootScriptSmmEventCallBack
,
337 ASSERT_EFI_ERROR (Status
);
339 return RETURN_SUCCESS
;
342 To get the start address from which a new boot time s3 boot script entry will write into.
343 If the table is not exist, the functio will first allocate a buffer for the table
344 If the table buffer is not enough for the new entry, in non-smm mode, the funtion will
345 invoke reallocate to enlarge buffer.
347 @param EntryLength the new entry length.
349 @retval the address from which the a new s3 boot script entry will write into
352 S3BootScriptGetBootTimeEntryAddAddress (
356 EFI_PHYSICAL_ADDRESS S3TableBase
;
357 EFI_PHYSICAL_ADDRESS NewS3TableBase
;
362 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
364 S3TableBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(mS3BootScriptTablePtr
->TableBase
);
365 if (S3TableBase
== 0) {
366 // The table is not exist. This is the first to add entry.
367 // Allocate ACPI script table space under 4G memory. We need it to save
368 // some settings done by CSM, which runs after normal script table closed
370 S3TableBase
= 0xffffffff;
371 Status
= gBS
->AllocatePages (
374 2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
),
375 (EFI_PHYSICAL_ADDRESS
*)&S3TableBase
378 if (EFI_ERROR(Status
)) {
379 ASSERT_EFI_ERROR (Status
);
385 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)(UINTN
)S3TableBase
;
386 ScriptTableInfo
->OpCode
= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
;
387 ScriptTableInfo
->Length
= (UINT8
) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
388 ScriptTableInfo
->TableLength
= 0; // will be calculate at CloseTable
389 mS3BootScriptTablePtr
->TableLength
= sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
390 mS3BootScriptTablePtr
->TableBase
= (UINT8
*)(UINTN
)S3TableBase
;
391 mS3BootScriptTablePtr
->TableMemoryPageNumber
= (UINT16
)(2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
));
394 // Here we do not count the reserved memory for runtime script table.
395 PageNumber
= (UINT16
)(mS3BootScriptTablePtr
->TableMemoryPageNumber
- PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
));
396 TableLength
= mS3BootScriptTablePtr
->TableLength
;
397 if ((UINT32
)(PageNumber
* EFI_PAGE_SIZE
) < (TableLength
+ EntryLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
))) {
399 // The buffer is too small to hold the table, Reallocate the buffer
401 NewS3TableBase
= 0xffffffff;
402 Status
= gBS
->AllocatePages (
405 2 + PageNumber
+ PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
),
406 (EFI_PHYSICAL_ADDRESS
*)&NewS3TableBase
409 if (EFI_ERROR(Status
)) {
410 ASSERT_EFI_ERROR (Status
);
414 CopyMem ((VOID
*)(UINTN
)NewS3TableBase
, (VOID
*)(UINTN
)S3TableBase
, TableLength
);
415 gBS
->FreePages (S3TableBase
, mS3BootScriptTablePtr
->TableMemoryPageNumber
);
417 mS3BootScriptTablePtr
->TableBase
= (UINT8
*)(UINTN
)NewS3TableBase
;
418 mS3BootScriptTablePtr
->TableMemoryPageNumber
= (UINT16
) (2 + PageNumber
+ PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber
));
421 // calculate the the start address for the new entry.
423 NewEntryPtr
= mS3BootScriptTablePtr
->TableBase
+ TableLength
;
426 // update the table lenghth
428 mS3BootScriptTablePtr
->TableLength
= TableLength
+ EntryLength
;
431 // In the boot time, we will not append the termination entry to the boot script
432 // table until the callers think there is no boot time data that should be added and
433 // it is caller's responsibility to explicit call the CloseTable.
440 To get the start address from which a new runtime s3 boot script entry will write into.
441 In this case, it should be ensured that there is enough buffer to hold the entry.
443 @param EntryLength the new entry length.
445 @retval the address from which the a new s3 runtime script entry will write into
448 S3BootScriptGetRuntimeEntryAddAddress (
456 // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node.
458 if (mS3BootScriptTablePtr
->TableLength
+ EntryLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
) <= EFI_PAGES_TO_SIZE((UINT32
)(mS3BootScriptTablePtr
->TableMemoryPageNumber
))) {
459 NewEntryPtr
= mS3BootScriptTablePtr
->TableBase
+ mS3BootScriptTablePtr
->TableLength
;
460 mS3BootScriptTablePtr
->TableLength
= mS3BootScriptTablePtr
->TableLength
+ EntryLength
;
462 // Append a terminate node on every insert
464 S3BootScriptInternalCloseTable ();
466 return (UINT8
*)NewEntryPtr
;
469 To get the start address from which a new s3 boot script entry will write into.
471 @param EntryLength the new entry length.
473 @retval the address from which the a new s3 runtime script entry will write into
476 S3BootScriptGetEntryAddAddress (
481 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader
;
484 if (mS3BootScriptTablePtr
->AtRuntime
) {
486 // We need check InSmm when AtRuntime, because after SmmReadyToLock, only SMM driver is allowed to write boot script.
488 if (!mS3BootScriptTablePtr
->InSmm
) {
490 // Add DEBUG ERROR, so that we can find it at boot time.
491 // Do not use ASSERT, because we may have test invoke this interface.
493 DEBUG ((EFI_D_ERROR
, "FATAL ERROR: Set boot script after ReadyToLock!!!\n"));
498 // NOTE: OS will restore ACPINvs data. After S3, the table length in mS3BootScriptTable (SMM) is different with
499 // table length in BootScriptTable header (ACPINvs).
500 // So here we need sync them. We choose ACPINvs table length, because we want to override the boot script saved
501 // in SMM every time.
503 ASSERT (mS3BootScriptTablePtr
== &mS3BootScriptTable
);
504 CopyMem ((VOID
*)&TableHeader
, (VOID
*)mS3BootScriptTablePtr
->TableBase
, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
));
505 if (mS3BootScriptTablePtr
->TableLength
+ sizeof(EFI_BOOT_SCRIPT_TERMINATE
) != TableHeader
.TableLength
) {
507 // Restore it to use original value
509 RestoreLockBox (&mBootScriptDataGuid
, NULL
, NULL
);
511 // Copy it again to get original value
512 // NOTE: We should NOT use TableHeader.TableLength, because it is already updated to be whole length.
514 mS3BootScriptTablePtr
->TableLength
= (UINT32
)(mLockBoxLength
- sizeof(EFI_BOOT_SCRIPT_TERMINATE
));
517 NewEntryPtr
= S3BootScriptGetRuntimeEntryAddAddress (EntryLength
);
519 // Now the length field is updated, need sync to lockbox.
520 // So in S3 resume, the data can be restored correctly.
522 CopyMem ((VOID
*)&TableHeader
, (VOID
*)mS3BootScriptTablePtr
->TableBase
, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
));
523 Status
= UpdateLockBox (
524 &mBootScriptDataGuid
,
525 OFFSET_OF(EFI_BOOT_SCRIPT_TABLE_HEADER
, TableLength
),
526 &TableHeader
.TableLength
,
527 sizeof(TableHeader
.TableLength
)
529 ASSERT_EFI_ERROR (Status
);
531 NewEntryPtr
= S3BootScriptGetBootTimeEntryAddAddress (EntryLength
);
538 Sync BootScript LockBox data.
547 if (!mS3BootScriptTablePtr
->AtRuntime
|| !mS3BootScriptTablePtr
->InSmm
) {
552 // So in S3 resume, the data can be restored correctly.
554 Status
= UpdateLockBox (
555 &mBootScriptDataGuid
,
556 mLockBoxLength
- sizeof(EFI_BOOT_SCRIPT_TERMINATE
),
557 (VOID
*)((UINTN
)mS3BootScriptTablePtr
->TableBase
+ mLockBoxLength
- sizeof(EFI_BOOT_SCRIPT_TERMINATE
)),
558 sizeof(EFI_BOOT_SCRIPT_TERMINATE
)
560 ASSERT_EFI_ERROR (Status
);
564 This is an function to close the S3 boot script table. The function could only be called in
565 BOOT time phase. To comply with the Framework spec definition on
566 EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable(), this function will fulfill following things:
567 1. Closes the specified boot script table
568 2. It allocates a new memory pool to duplicate all the boot scripts in the specified table.
569 Once this function is called, the table maintained by the library will be destroyed
570 after it is copied into the allocated pool.
571 3. Any attempts to add a script record after calling this function will cause a new table
572 to be created by the library.
573 4. The base address of the allocated pool will be returned in Address. Note that after
574 using the boot script table, the CALLER is responsible for freeing the pool that is allocated
577 In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is
578 for Framework Spec compatibility.
580 If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out
581 how to get the script to run on an S3 resume because the boot script maintained by the lib will be
584 @return the base address of the new copy of the boot script tble.
585 @note this function could only called in boot time phase
590 S3BootScriptCloseTable (
598 EFI_BOOT_SCRIPT_TABLE_HEADER
*ScriptTableInfo
;
600 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
601 if (S3TableBase
== 0) {
605 // Append the termination record the S3 boot script table
607 S3BootScriptInternalCloseTable();
608 TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
610 // Allocate the buffer and copy the boot script to the buffer.
612 Status
= gBS
->AllocatePool (
617 if (EFI_ERROR (Status
)) {
620 CopyMem (Buffer
, S3TableBase
, TableLength
);
623 // Destroy the table maintained by the library so that the next write operation
624 // will write the record to the first entry of the table.
626 // Fill the table header.
627 ScriptTableInfo
= (EFI_BOOT_SCRIPT_TABLE_HEADER
*)S3TableBase
;
628 ScriptTableInfo
->OpCode
= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
;
629 ScriptTableInfo
->Length
= (UINT8
) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
630 ScriptTableInfo
->TableLength
= 0; // will be calculate at close the table
632 mS3BootScriptTablePtr
->TableLength
= sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER
);
636 Save I/O write to boot script
638 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
639 @param Address The base address of the I/O operations.
640 @param Count The number of I/O operations to perform.
641 @param Buffer The source buffer from which to write data.
643 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
644 @retval RETURN_SUCCESS Opcode is added.
648 S3BootScriptSaveIoWrite (
649 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
659 EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite
;
661 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
662 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE
) + (WidthInByte
* Count
));
664 Script
= S3BootScriptGetEntryAddAddress (Length
);
665 if (Script
== NULL
) {
666 return RETURN_OUT_OF_RESOURCES
;
671 ScriptIoWrite
.OpCode
= EFI_BOOT_SCRIPT_IO_WRITE_OPCODE
;
672 ScriptIoWrite
.Length
= Length
;
673 ScriptIoWrite
.Width
= Width
;
674 ScriptIoWrite
.Address
= Address
;
675 ScriptIoWrite
.Count
= (UINT32
) Count
;
676 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoWrite
, sizeof(EFI_BOOT_SCRIPT_IO_WRITE
));
677 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_WRITE
)), Buffer
, WidthInByte
* Count
);
681 return RETURN_SUCCESS
;
685 Adds a record for an I/O modify operation into a S3 boot script table
687 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
688 @param Address The base address of the I/O operations.
689 @param Data A pointer to the data to be OR-ed.
690 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register
692 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
693 @retval RETURN_SUCCESS Opcode is added.
697 S3BootScriptSaveIoReadWrite (
698 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
707 EFI_BOOT_SCRIPT_IO_READ_WRITE ScriptIoReadWrite
;
709 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
710 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
) + (WidthInByte
* 2));
712 Script
= S3BootScriptGetEntryAddAddress (Length
);
713 if (Script
== NULL
) {
714 return RETURN_OUT_OF_RESOURCES
;
719 ScriptIoReadWrite
.OpCode
= EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE
;
720 ScriptIoReadWrite
.Length
= Length
;
721 ScriptIoReadWrite
.Width
= Width
;
722 ScriptIoReadWrite
.Address
= Address
;
724 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoReadWrite
, sizeof(EFI_BOOT_SCRIPT_IO_READ_WRITE
));
725 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
)), Data
, WidthInByte
);
726 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE
) + WidthInByte
), DataMask
, WidthInByte
);
730 return RETURN_SUCCESS
;
733 Adds a record for a memory write operation into a specified boot script table.
735 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
736 @param Address The base address of the memory operations
737 @param Count The number of memory operations to perform.
738 @param Buffer The source buffer from which to write the data.
740 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
741 @retval RETURN_SUCCESS Opcode is added.
745 S3BootScriptSaveMemWrite (
746 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
755 EFI_BOOT_SCRIPT_MEM_WRITE ScriptMemWrite
;
757 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
758 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
) + (WidthInByte
* Count
));
760 Script
= S3BootScriptGetEntryAddAddress (Length
);
761 if (Script
== NULL
) {
762 return RETURN_OUT_OF_RESOURCES
;
767 ScriptMemWrite
.OpCode
= EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE
;
768 ScriptMemWrite
.Length
= Length
;
769 ScriptMemWrite
.Width
= Width
;
770 ScriptMemWrite
.Address
= Address
;
771 ScriptMemWrite
.Count
= (UINT32
) Count
;
773 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemWrite
, sizeof(EFI_BOOT_SCRIPT_MEM_WRITE
));
774 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_WRITE
)), Buffer
, WidthInByte
* Count
);
778 return RETURN_SUCCESS
;
781 Adds a record for a memory modify operation into a specified boot script table.
783 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
784 @param Address The base address of the memory operations. Address needs alignment if required
785 @param Data A pointer to the data to be OR-ed.
786 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register.
788 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
789 @retval RETURN_SUCCESS Opcode is added.
793 S3BootScriptSaveMemReadWrite (
794 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
803 EFI_BOOT_SCRIPT_MEM_READ_WRITE ScriptMemReadWrite
;
805 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
806 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
) + (WidthInByte
* 2));
808 Script
= S3BootScriptGetEntryAddAddress (Length
);
809 if (Script
== NULL
) {
810 return RETURN_OUT_OF_RESOURCES
;
815 ScriptMemReadWrite
.OpCode
= EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE
;
816 ScriptMemReadWrite
.Length
= Length
;
817 ScriptMemReadWrite
.Width
= Width
;
818 ScriptMemReadWrite
.Address
= Address
;
820 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemReadWrite
, sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
));
821 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
)), Data
, WidthInByte
);
822 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE
) + WidthInByte
), DataMask
, WidthInByte
);
826 return RETURN_SUCCESS
;
829 Adds a record for a PCI configuration space write operation into a specified boot script table.
831 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
832 @param Address The address within the PCI configuration space.
833 @param Count The number of PCI operations to perform.
834 @param Buffer The source buffer from which to write the data.
836 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
837 @retval RETURN_SUCCESS Opcode is added.
841 S3BootScriptSavePciCfgWrite (
842 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
851 EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE ScriptPciWrite
;
853 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
854 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
) + (WidthInByte
* Count
));
856 Script
= S3BootScriptGetEntryAddAddress (Length
);
857 if (Script
== NULL
) {
858 return RETURN_OUT_OF_RESOURCES
;
863 ScriptPciWrite
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE
;
864 ScriptPciWrite
.Length
= Length
;
865 ScriptPciWrite
.Width
= Width
;
866 ScriptPciWrite
.Address
= Address
;
867 ScriptPciWrite
.Count
= (UINT32
) Count
;
869 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciWrite
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
));
870 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE
)), Buffer
, WidthInByte
* Count
);
874 return RETURN_SUCCESS
;
877 Adds a record for a PCI configuration space modify operation into a specified boot script table.
879 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
880 @param Address The address within the PCI configuration space.
881 @param Data A pointer to the data to be OR-ed.The size depends on Width.
882 @param DataMask A pointer to the data mask to be AND-ed.
884 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
885 @retval RETURN__SUCCESS Opcode is added.
889 S3BootScriptSavePciCfgReadWrite (
890 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
899 EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE ScriptPciReadWrite
;
901 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
902 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
) + (WidthInByte
* 2));
904 Script
= S3BootScriptGetEntryAddAddress (Length
);
905 if (Script
== NULL
) {
906 return RETURN_OUT_OF_RESOURCES
;
911 ScriptPciReadWrite
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE
;
912 ScriptPciReadWrite
.Length
= Length
;
913 ScriptPciReadWrite
.Width
= Width
;
914 ScriptPciReadWrite
.Address
= Address
;
916 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciReadWrite
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
));
917 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
)), Data
, WidthInByte
);
919 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE
) + WidthInByte
),
926 return RETURN_SUCCESS
;
929 Adds a record for a PCI configuration space modify operation into a specified boot script table.
931 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
932 @param Segment The PCI segment number for Address.
933 @param Address The address within the PCI configuration space.
934 @param Count The number of PCI operations to perform.
935 @param Buffer The source buffer from which to write the data.
937 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
938 @retval RETURN_SUCCESS Opcode is added.
942 S3BootScriptSavePciCfg2Write (
943 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
953 EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE ScriptPciWrite2
;
955 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
956 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
) + (WidthInByte
* Count
));
958 Script
= S3BootScriptGetEntryAddAddress (Length
);
959 if (Script
== NULL
) {
960 return RETURN_OUT_OF_RESOURCES
;
965 ScriptPciWrite2
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE
;
966 ScriptPciWrite2
.Length
= Length
;
967 ScriptPciWrite2
.Width
= Width
;
968 ScriptPciWrite2
.Address
= Address
;
969 ScriptPciWrite2
.Segment
= Segment
;
970 ScriptPciWrite2
.Count
= (UINT32
)Count
;
972 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciWrite2
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
));
973 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE
)), Buffer
, WidthInByte
* Count
);
977 return RETURN_SUCCESS
;
980 Adds a record for a PCI configuration space modify operation into a specified boot script table.
982 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
983 @param Segment The PCI segment number for Address.
984 @param Address The address within the PCI configuration space.
985 @param Data A pointer to the data to be OR-ed. The size depends on Width.
986 @param DataMask A pointer to the data mask to be AND-ed.
988 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
989 @retval RETURN_SUCCESS Opcode is added.
993 S3BootScriptSavePciCfg2ReadWrite (
994 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1004 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE ScriptPciReadWrite2
;
1006 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1007 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
) + (WidthInByte
* 2));
1009 Script
= S3BootScriptGetEntryAddAddress (Length
);
1010 if (Script
== NULL
) {
1011 return RETURN_OUT_OF_RESOURCES
;
1014 // Build script data
1016 ScriptPciReadWrite2
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE
;
1017 ScriptPciReadWrite2
.Length
= Length
;
1018 ScriptPciReadWrite2
.Width
= Width
;
1019 ScriptPciReadWrite2
.Segment
= Segment
;
1020 ScriptPciReadWrite2
.Address
= Address
;
1022 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciReadWrite2
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
));
1023 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
)), Data
, WidthInByte
);
1025 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE
) + WidthInByte
),
1032 return RETURN_SUCCESS
;
1035 Adds a record for an SMBus command execution into a specified boot script table.
1037 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length, and PEC.
1038 @param Operation Indicates which particular SMBus protocol it will use to execute the SMBus
1040 @param Length A pointer to signify the number of bytes that this operation will do.
1041 @param Buffer Contains the value of data to execute to the SMBUS slave device.
1043 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1044 @retval RETURN_SUCCESS Opcode is added.
1048 S3BootScriptSaveSmbusExecute (
1049 IN UINTN SmBusAddress
,
1050 IN EFI_SMBUS_OPERATION Operation
,
1057 EFI_BOOT_SCRIPT_SMBUS_EXECUTE ScriptSmbusExecute
;
1059 DataSize
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
) + (*Length
));
1061 Script
= S3BootScriptGetEntryAddAddress (DataSize
);
1062 if (Script
== NULL
) {
1063 return RETURN_OUT_OF_RESOURCES
;
1066 // Build script data
1068 ScriptSmbusExecute
.OpCode
= EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE
;
1069 ScriptSmbusExecute
.Length
= DataSize
;
1070 ScriptSmbusExecute
.SmBusAddress
= (UINT64
) SmBusAddress
;
1071 ScriptSmbusExecute
.Operation
= Operation
;
1072 ScriptSmbusExecute
.DataSize
= (UINT32
) *Length
;
1074 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptSmbusExecute
, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
));
1076 (VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE
)),
1083 return RETURN_SUCCESS
;
1086 Adds a record for an execution stall on the processor into a specified boot script table.
1088 @param Duration Duration in microseconds of the stall
1090 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1091 @retval RETURN_SUCCESS Opcode is added.
1095 S3BootScriptSaveStall (
1101 EFI_BOOT_SCRIPT_STALL ScriptStall
;
1103 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_STALL
));
1105 Script
= S3BootScriptGetEntryAddAddress (Length
);
1106 if (Script
== NULL
) {
1107 return RETURN_OUT_OF_RESOURCES
;
1110 // Build script data
1112 ScriptStall
.OpCode
= EFI_BOOT_SCRIPT_STALL_OPCODE
;
1113 ScriptStall
.Length
= Length
;
1114 ScriptStall
.Duration
= Duration
;
1116 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptStall
, sizeof (EFI_BOOT_SCRIPT_STALL
));
1120 return RETURN_SUCCESS
;
1123 Adds a record for an execution stall on the processor into a specified boot script table.
1125 @param EntryPoint Entry point of the code to be dispatched.
1126 @param Context Argument to be passed into the EntryPoint of the code to be dispatched.
1128 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1129 @retval RETURN_SUCCESS Opcode is added.
1133 S3BootScriptSaveDispatch2 (
1134 IN VOID
*EntryPoint
,
1140 EFI_BOOT_SCRIPT_DISPATCH_2 ScriptDispatch2
;
1141 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2
));
1143 Script
= S3BootScriptGetEntryAddAddress (Length
);
1144 if (Script
== NULL
) {
1145 return RETURN_OUT_OF_RESOURCES
;
1148 // Build script data
1150 ScriptDispatch2
.OpCode
= EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE
;
1151 ScriptDispatch2
.Length
= Length
;
1152 ScriptDispatch2
.EntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)EntryPoint
;
1153 ScriptDispatch2
.Context
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)Context
;
1155 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptDispatch2
, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2
));
1159 return RETURN_SUCCESS
;
1163 Adds a record for memory reads of the memory location and continues when the exit criteria is
1164 satisfied or after a defined duration.
1166 @param Width The width of the memory operations.
1167 @param Address The base address of the memory operations.
1168 @param BitMask A pointer to the bit mask to be AND-ed with the data read from the register.
1169 @param BitValue A pointer to the data value after to be Masked.
1170 @param Duration Duration in microseconds of the stall.
1171 @param LoopTimes The times of the register polling.
1173 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1174 @retval RETURN_SUCCESS Opcode is added.
1179 S3BootScriptSaveMemPoll (
1180 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1191 EFI_BOOT_SCRIPT_MEM_POLL ScriptMemPoll
;
1193 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1195 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL
) + (WidthInByte
* 2));
1197 Script
= S3BootScriptGetEntryAddAddress (Length
);
1198 if (Script
== NULL
) {
1199 return RETURN_OUT_OF_RESOURCES
;
1202 // Build script data
1204 ScriptMemPoll
.OpCode
= EFI_BOOT_SCRIPT_MEM_POLL_OPCODE
;
1205 ScriptMemPoll
.Length
= Length
;
1206 ScriptMemPoll
.Width
= Width
;
1207 ScriptMemPoll
.Address
= Address
;
1208 ScriptMemPoll
.Duration
= Duration
;
1209 ScriptMemPoll
.LoopTimes
= LoopTimes
;
1211 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_POLL
)), BitValue
, WidthInByte
);
1212 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_MEM_POLL
) + WidthInByte
), BitMask
, WidthInByte
);
1213 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptMemPoll
, sizeof (EFI_BOOT_SCRIPT_MEM_POLL
));
1217 return RETURN_SUCCESS
;
1220 Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only
1221 used for debugging script issues.
1223 @param InformationLength Length of the data in bytes
1224 @param Information Information to be logged in the boot scrpit
1226 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1227 @retval RETURN_SUCCESS Opcode is added.
1232 S3BootScriptSaveInformation (
1233 IN UINT32 InformationLength
,
1234 IN VOID
*Information
1239 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation
;
1241 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_INFORMATION
) + InformationLength
);
1243 Script
= S3BootScriptGetEntryAddAddress (Length
);
1244 if (Script
== NULL
) {
1245 return RETURN_OUT_OF_RESOURCES
;
1248 // Build script data
1250 ScriptInformation
.OpCode
= EFI_BOOT_SCRIPT_INFORMATION_OPCODE
;
1251 ScriptInformation
.Length
= Length
;
1254 ScriptInformation
.InformationLength
= InformationLength
;
1256 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptInformation
, sizeof (EFI_BOOT_SCRIPT_INFORMATION
));
1257 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_INFORMATION
)), (VOID
*) Information
, (UINTN
) InformationLength
);
1261 return RETURN_SUCCESS
;
1265 Store a string in the boot script table. This opcode is a no-op on dispatch and is only
1266 used for debugging script issues.
1268 @param String The string to save to boot script table
1270 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1271 @retval RETURN_SUCCESS Opcode is added.
1276 S3BootScriptSaveInformationAsciiString (
1277 IN CONST CHAR8
*String
1280 return S3BootScriptSaveInformation (
1281 (UINT32
) AsciiStrLen (String
) + 1,
1286 Adds a record for dispatching specified arbitrary code into a specified boot script table.
1288 @param EntryPoint Entry point of the code to be dispatched.
1290 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1291 @retval RETURN_SUCCESS Opcode is added.
1295 S3BootScriptSaveDispatch (
1301 EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch
;
1303 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_DISPATCH
));
1305 Script
= S3BootScriptGetEntryAddAddress (Length
);
1306 if (Script
== NULL
) {
1307 return RETURN_OUT_OF_RESOURCES
;
1310 // Build script data
1312 ScriptDispatch
.OpCode
= EFI_BOOT_SCRIPT_DISPATCH_OPCODE
;
1313 ScriptDispatch
.Length
= Length
;
1314 ScriptDispatch
.EntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)EntryPoint
;
1316 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptDispatch
, sizeof (EFI_BOOT_SCRIPT_DISPATCH
));
1320 return RETURN_SUCCESS
;
1324 Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a
1327 @param Width The width of the I/O operations.
1328 @param Address The base address of the I/O operations.
1329 @param Data The comparison value used for the polling exit criteria.
1330 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1331 in Data are ignored when polling the memory address.
1332 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1333 granularity so the delay may be longer.
1335 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1336 @retval RETURN_SUCCESS Opcode is added.
1341 S3BootScriptSaveIoPoll (
1342 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1352 EFI_BOOT_SCRIPT_IO_POLL ScriptIoPoll
;
1355 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1356 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + (WidthInByte
* 2));
1358 Script
= S3BootScriptGetEntryAddAddress (Length
);
1359 if (Script
== NULL
) {
1360 return RETURN_OUT_OF_RESOURCES
;
1363 // Build script data
1365 ScriptIoPoll
.OpCode
= EFI_BOOT_SCRIPT_IO_POLL_OPCODE
;
1366 ScriptIoPoll
.Length
= (UINT8
) (sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + (WidthInByte
* 2));
1367 ScriptIoPoll
.Width
= Width
;
1368 ScriptIoPoll
.Address
= Address
;
1369 ScriptIoPoll
.Delay
= Delay
;
1371 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptIoPoll
, sizeof (EFI_BOOT_SCRIPT_IO_POLL
));
1372 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_IO_POLL
)), Data
, WidthInByte
);
1373 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_IO_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1377 return RETURN_SUCCESS
;
1381 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1382 after a defined duration.
1384 @param Width The width of the I/O operations.
1385 @param Address The address within the PCI configuration space.
1386 @param Data The comparison value used for the polling exit criteria.
1387 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1388 in Data are ignored when polling the memory address
1389 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1390 granularity so the delay may be longer.
1392 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1393 @retval RETURN_SUCCESS Opcode is added.
1398 S3BootScriptSavePciPoll (
1399 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1409 EFI_BOOT_SCRIPT_PCI_CONFIG_POLL ScriptPciPoll
;
1411 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1412 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + (WidthInByte
* 2));
1414 Script
= S3BootScriptGetEntryAddAddress (Length
);
1415 if (Script
== NULL
) {
1416 return RETURN_OUT_OF_RESOURCES
;
1419 // Build script data
1421 ScriptPciPoll
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE
;
1422 ScriptPciPoll
.Length
= (UINT8
) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + (WidthInByte
* 2));
1423 ScriptPciPoll
.Width
= Width
;
1424 ScriptPciPoll
.Address
= Address
;
1425 ScriptPciPoll
.Delay
= Delay
;
1427 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPciPoll
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
));
1428 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
)), Data
, WidthInByte
);
1429 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1433 return RETURN_SUCCESS
;
1436 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1437 after a defined duration.
1439 @param Width The width of the I/O operations.
1440 @param Segment The PCI segment number for Address.
1441 @param Address The address within the PCI configuration space.
1442 @param Data The comparison value used for the polling exit criteria.
1443 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1444 in Data are ignored when polling the memory address
1445 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1446 granularity so the delay may be longer.
1448 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1449 @retval RETURN_SUCCESS Opcode is added.
1450 @note A known Limitations in the implementation: When interpreting the opcode EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE
1451 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE and EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE, the 'Segment' parameter is assumed as
1452 Zero, or else, assert.
1456 S3BootScriptSavePci2Poll (
1457 IN S3_BOOT_SCRIPT_LIB_WIDTH Width
,
1468 EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL ScriptPci2Poll
;
1470 WidthInByte
= (UINT8
) (0x01 << (Width
& 0x03));
1471 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + (WidthInByte
* 2));
1473 Script
= S3BootScriptGetEntryAddAddress (Length
);
1474 if (Script
== NULL
) {
1475 return RETURN_OUT_OF_RESOURCES
;
1478 // Build script data
1480 ScriptPci2Poll
.OpCode
= EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE
;
1481 ScriptPci2Poll
.Length
= (UINT8
) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + (WidthInByte
* 2));
1482 ScriptPci2Poll
.Width
= Width
;
1483 ScriptPci2Poll
.Segment
= Segment
;
1484 ScriptPci2Poll
.Address
= Address
;
1485 ScriptPci2Poll
.Delay
= Delay
;
1487 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptPci2Poll
, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
));
1488 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
)), Data
, WidthInByte
);
1489 CopyMem ((UINT8
*) (Script
+ sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL
) + WidthInByte
), DataMask
, WidthInByte
);
1493 return RETURN_SUCCESS
;
1496 Do the calculation of start address from which a new s3 boot script entry will write into.
1498 @param EntryLength The new entry length.
1499 @param Position specifies the position in the boot script table where the opcode will be
1500 inserted, either before or after, depending on BeforeOrAfter.
1501 @param BeforeOrAfter The flag to indicate to insert the nod before or after the position.
1502 This parameter is effective when InsertFlag is TRUE
1503 @param Script return out the position from which the a new s3 boot script entry will write into
1506 S3BootScriptCalculateInsertAddress (
1507 IN UINT8 EntryLength
,
1508 IN VOID
*Position OPTIONAL
,
1509 IN BOOLEAN BeforeOrAfter OPTIONAL
,
1515 UINTN PositionOffset
;
1516 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
1518 // The entry inserting to table is already added to the end of the table
1520 TableLength
= mS3BootScriptTablePtr
->TableLength
- EntryLength
;
1521 S3TableBase
= mS3BootScriptTablePtr
->TableBase
;
1523 // calculate the Position offset
1525 if (Position
!= NULL
) {
1526 PositionOffset
= (UINTN
) ((UINT8
*)Position
- S3TableBase
);
1529 // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.
1531 if (!BeforeOrAfter
) {
1532 CopyMem ((VOID
*)&ScriptHeader
, Position
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
1533 PositionOffset
+= (ScriptHeader
.Length
);
1536 // Insert the node before the adjusted Position
1538 CopyMem (S3TableBase
+PositionOffset
+EntryLength
, S3TableBase
+PositionOffset
, TableLength
- PositionOffset
);
1540 // calculate the the start address for the new entry.
1542 *Script
= S3TableBase
+ PositionOffset
;
1545 if (!BeforeOrAfter
) {
1547 // Insert the node to the end of the table
1549 *Script
= S3TableBase
+ TableLength
;
1552 // Insert the node to the beginning of the table
1554 PositionOffset
= (UINTN
) sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
);
1555 CopyMem (S3TableBase
+PositionOffset
+EntryLength
, S3TableBase
+PositionOffset
, TableLength
- PositionOffset
);
1556 *Script
= S3TableBase
+ PositionOffset
;
1561 Move the last boot script entry to the position
1563 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
1564 in the boot script table specified by Position. If Position is NULL or points to
1565 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
1566 of the table (if FALSE).
1567 @param Position On entry, specifies the position in the boot script table where the opcode will be
1568 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
1569 the position of the inserted opcode in the boot script table.
1571 @retval RETURN_OUT_OF_RESOURCES The table is not available.
1572 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
1573 @retval RETURN_SUCCESS Opcode is inserted.
1577 S3BootScriptMoveLastOpcode (
1578 IN BOOLEAN BeforeOrAfter
,
1579 IN OUT VOID
**Position OPTIONAL
1586 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
1587 BOOLEAN ValidatePosition
;
1589 UINT8 TempBootScriptEntry
[BOOT_SCRIPT_NODE_MAX_LENGTH
];
1591 ValidatePosition
= FALSE
;
1592 TempPosition
= (Position
== NULL
) ? NULL
:(*Position
);
1593 Script
= mS3BootScriptTablePtr
->TableBase
;
1595 return EFI_OUT_OF_RESOURCES
;
1597 StartAddress
= (UINTN
) Script
;
1598 TableLength
= mS3BootScriptTablePtr
->TableLength
;
1599 Script
= Script
+ sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
);
1600 LastOpcode
= Script
;
1602 // Find the last boot Script Entry which is not the terminate node
1604 while ((UINTN
) Script
< (UINTN
) (StartAddress
+ TableLength
)) {
1605 CopyMem ((VOID
*)&ScriptHeader
, Script
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
1606 if (TempPosition
!= NULL
&& TempPosition
== Script
) {
1608 // If the position is specified, the position must be pointed to a boot script entry start address.
1610 ValidatePosition
= TRUE
;
1612 if (ScriptHeader
.OpCode
!= S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE
) {
1613 LastOpcode
= Script
;
1615 Script
= Script
+ ScriptHeader
.Length
;
1618 // If the position is specified, but not the start of a boot script entry, it is a invalid input
1620 if (TempPosition
!= NULL
&& !ValidatePosition
) {
1621 return RETURN_INVALID_PARAMETER
;
1624 CopyMem ((VOID
*)&ScriptHeader
, LastOpcode
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
1626 CopyMem((VOID
*)TempBootScriptEntry
, LastOpcode
, ScriptHeader
.Length
);
1628 // Find the right position to write the node in
1630 S3BootScriptCalculateInsertAddress (
1631 ScriptHeader
.Length
,
1637 // Copy the node to Boot script table
1639 CopyMem((VOID
*)Script
, (VOID
*)TempBootScriptEntry
, ScriptHeader
.Length
);
1641 // return out the Position
1643 if (Position
!= NULL
) {
1646 return RETURN_SUCCESS
;
1649 Create a Label node in the boot script table.
1651 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
1652 in the boot script table specified by Position. If Position is NULL or points to
1653 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
1654 of the table (if FALSE).
1655 @param Position On entry, specifies the position in the boot script table where the opcode will be
1656 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
1657 the position of the inserted opcode in the boot script table.
1658 @param InformationLength Length of the label in bytes
1659 @param Information Label to be logged in the boot scrpit
1661 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
1662 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1663 @retval RETURN_SUCCESS Opcode is added.
1668 S3BootScriptLabelInternal (
1669 IN BOOLEAN BeforeOrAfter
,
1670 IN OUT VOID
**Position OPTIONAL
,
1671 IN UINT32 InformationLength
,
1672 IN CONST CHAR8
*Information
1677 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation
;
1679 Length
= (UINT8
)(sizeof (EFI_BOOT_SCRIPT_INFORMATION
) + InformationLength
);
1681 Script
= S3BootScriptGetEntryAddAddress (Length
);
1682 if (Script
== NULL
) {
1683 return RETURN_OUT_OF_RESOURCES
;
1686 // Build script data
1688 ScriptInformation
.OpCode
= S3_BOOT_SCRIPT_LIB_LABEL_OPCODE
;
1689 ScriptInformation
.Length
= Length
;
1692 ScriptInformation
.InformationLength
= InformationLength
;
1694 CopyMem ((VOID
*)Script
, (VOID
*)&ScriptInformation
, sizeof (EFI_BOOT_SCRIPT_INFORMATION
));
1695 CopyMem ((VOID
*)(Script
+ sizeof (EFI_BOOT_SCRIPT_INFORMATION
)), (VOID
*) Information
, (UINTN
) InformationLength
);
1697 return S3BootScriptMoveLastOpcode (BeforeOrAfter
, Position
);
1701 Find a label within the boot script table and, if not present, optionally create it.
1703 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE)
1704 or after (FALSE) the position in the boot script table
1705 specified by Position.
1706 @param CreateIfNotFound Specifies whether the label will be created if the label
1707 does not exists (TRUE) or not (FALSE).
1708 @param Position On entry, specifies the position in the boot script table
1709 where the opcode will be inserted, either before or after,
1710 depending on BeforeOrAfter. On exit, specifies the position
1711 of the inserted opcode in the boot script table.
1712 @param Label Points to the label which will be inserted in the boot script table.
1714 @retval EFI_SUCCESS The operation succeeded. A record was added into the
1715 specified script table.
1716 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
1717 If the opcode is unknow or not supported because of the PCD
1719 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
1725 IN BOOLEAN BeforeOrAfter
,
1726 IN BOOLEAN CreateIfNotFound
,
1727 IN OUT VOID
**Position OPTIONAL
,
1728 IN CONST CHAR8
*Label
1734 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader
;
1735 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader
;
1740 if (Label
== NULL
) {
1741 return EFI_INVALID_PARAMETER
;
1744 // Check empty Label
1746 if (Label
[0] == '\0') {
1747 return EFI_INVALID_PARAMETER
;
1751 // Check that the script is initialized without adding an entry to the script.
1752 // The code must search for the label first befor it knows if a new entry needs
1755 Script
= S3BootScriptGetEntryAddAddress (0);
1756 if (Script
== NULL
) {
1757 return RETURN_OUT_OF_RESOURCES
;
1761 // Check the header and search for existing label.
1763 Script
= mS3BootScriptTablePtr
->TableBase
;
1764 CopyMem ((VOID
*)&TableHeader
, Script
, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER
));
1765 if (TableHeader
.OpCode
!= S3_BOOT_SCRIPT_LIB_TABLE_OPCODE
) {
1766 return EFI_INVALID_PARAMETER
;
1768 StartAddress
= (UINTN
) Script
;
1769 TableLength
= mS3BootScriptTablePtr
->TableLength
;
1770 Script
= Script
+ TableHeader
.Length
;
1771 while ((UINTN
) Script
< (UINTN
) (StartAddress
+ TableLength
)) {
1773 CopyMem ((VOID
*)&ScriptHeader
, Script
, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER
));
1774 if (ScriptHeader
.OpCode
== S3_BOOT_SCRIPT_LIB_LABEL_OPCODE
) {
1775 if (AsciiStrCmp ((CHAR8
*)(UINTN
)(Script
+sizeof(EFI_BOOT_SCRIPT_INFORMATION
)), Label
) == 0) {
1776 (*Position
) = Script
;
1780 Script
= Script
+ ScriptHeader
.Length
;
1782 if (CreateIfNotFound
) {
1783 LabelLength
= (UINT32
)AsciiStrSize(Label
);
1784 return S3BootScriptLabelInternal (BeforeOrAfter
,Position
, LabelLength
, Label
);
1786 return EFI_NOT_FOUND
;
1791 Compare two positions in the boot script table and return their relative position.
1792 @param Position1 The positions in the boot script table to compare
1793 @param Position2 The positions in the boot script table to compare
1794 @param RelativePosition On return, points to the result of the comparison
1796 @retval EFI_SUCCESS The operation succeeded. A record was added into the
1797 specified script table.
1798 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
1799 If the opcode is unknow or not supported because of the PCD
1801 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
1806 S3BootScriptCompare (
1807 IN UINT8
*Position1
,
1808 IN UINT8
*Position2
,
1809 OUT UINTN
*RelativePosition
1815 Script
= mS3BootScriptTablePtr
->TableBase
;
1816 if (Script
== NULL
) {
1817 return EFI_OUT_OF_RESOURCES
;
1819 if (RelativePosition
== NULL
) {
1820 return EFI_INVALID_PARAMETER
;
1823 // mS3BootScriptTablePtr->TableLength does not include the termination node, so add it up
1825 TableLength
= mS3BootScriptTablePtr
->TableLength
+ sizeof (EFI_BOOT_SCRIPT_TERMINATE
);
1826 if (Position1
< Script
|| Position1
> Script
+TableLength
) {
1827 return EFI_INVALID_PARAMETER
;
1829 if (Position2
< Script
|| Position2
> Script
+TableLength
) {
1830 return EFI_INVALID_PARAMETER
;
1832 *RelativePosition
= (Position1
< Position2
)?-1:((Position1
== Position2
)?0:1);