]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
Fix the issue that S3BootScriptLabel() does not work to insert label when the specifi...
[mirror_edk2.git] / MdeModulePkg / Library / PiDxeS3BootScriptLib / BootScriptSave.c
1 /** @file
2 Save the S3 data to S3 boot script.
3
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5
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
11
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.
14
15 **/
16 #include "InternalBootScriptLib.h"
17
18 /**
19
20 Data structure usage:
21
22 +------------------------------+<-- PcdS3BootScriptTablePrivateDataPtr
23 | SCRIPT_TABLE_PRIVATE_DATA |
24 | TableBase |---
25 | TableLength |--|--
26 | AtRuntime | | |
27 | InSmm | | |
28 +------------------------------+ | |
29 | |
30 +------------------------------+<-- |
31 | EFI_BOOT_SCRIPT_TABLE_HEADER | |
32 | TableLength |----|--
33 +------------------------------+ | |
34 | ...... | | |
35 +------------------------------+<---- |
36 | EFI_BOOT_SCRIPT_TERMINATE | |
37 +------------------------------+<------
38
39 **/
40
41 SCRIPT_TABLE_PRIVATE_DATA *mS3BootScriptTablePtr;
42 EFI_EVENT mEnterRuntimeEvent;
43 //
44 // Allocate local copy in SMM because we can not use mS3BootScriptTablePtr when we AtRuntime in InSmm.
45 //
46 SCRIPT_TABLE_PRIVATE_DATA mS3BootScriptTable;
47 UINTN mLockBoxLength;
48
49 EFI_GUID mBootScriptDataGuid = {
50 0xaea6b965, 0xdcf5, 0x4311, { 0xb4, 0xb8, 0xf, 0x12, 0x46, 0x44, 0x94, 0xd2 }
51 };
52
53 EFI_GUID mBootScriptHeaderDataGuid = {
54 0x1810ab4a, 0x2314, 0x4df6, { 0x81, 0xeb, 0x67, 0xc6, 0xec, 0x5, 0x85, 0x91 }
55 };
56
57 /**
58 This is an internal function to add a terminate node the entry, recalculate the table
59 length and fill into the table.
60
61 @return the base address of the boot script tble.
62 **/
63 UINT8*
64 S3BootScriptInternalCloseTable (
65 VOID
66 )
67 {
68 UINT8 *S3TableBase;
69 EFI_BOOT_SCRIPT_TERMINATE ScriptTerminate;
70 EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;
71 S3TableBase = mS3BootScriptTablePtr->TableBase;
72
73 if (S3TableBase == NULL) {
74 //
75 // the table is not exist
76 //
77 return S3TableBase;
78 }
79 //
80 // Append the termination entry.
81 //
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));
85 //
86 // fill the table length
87 //
88 ScriptTableInfo = (EFI_BOOT_SCRIPT_TABLE_HEADER*)(mS3BootScriptTablePtr->TableBase);
89 ScriptTableInfo->TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);
90
91
92
93 return S3TableBase;
94 //
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.
99 //
100 }
101
102 /**
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.
108 **/
109 VOID
110 SaveBootScriptDataToLockBox (
111 VOID
112 )
113 {
114 EFI_STATUS Status;
115
116 //
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.
119 //
120 Status = SaveLockBox (
121 &mBootScriptDataGuid,
122 (VOID *)mS3BootScriptTablePtr->TableBase,
123 mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE)
124 );
125 ASSERT_EFI_ERROR (Status);
126
127 Status = SetLockBoxAttributes (&mBootScriptDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
128 ASSERT_EFI_ERROR (Status);
129
130 //
131 // Just need save TableBase.
132 // Do not update other field because they will NOT be used in S3.
133 //
134 Status = SaveLockBox (
135 &mBootScriptHeaderDataGuid,
136 (VOID *)&mS3BootScriptTablePtr->TableBase,
137 sizeof(mS3BootScriptTablePtr->TableBase)
138 );
139 ASSERT_EFI_ERROR (Status);
140
141 Status = SetLockBoxAttributes (&mBootScriptHeaderDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
142 ASSERT_EFI_ERROR (Status);
143 }
144
145 /**
146 This is the Event call back function to notify the Library the system is entering
147 run time phase.
148
149 @param Event Pointer to this event
150 @param Context Event hanlder private data
151 **/
152 VOID
153 EFIAPI
154 S3BootScriptEventCallBack (
155 IN EFI_EVENT Event,
156 IN VOID *Context
157 )
158 {
159 EFI_STATUS Status;
160 VOID *Interface;
161
162 //
163 // Try to locate it because EfiCreateProtocolNotifyEvent will trigger it once when registration.
164 // Just return if it is not found.
165 //
166 Status = gBS->LocateProtocol (
167 &gEfiDxeSmmReadyToLockProtocolGuid,
168 NULL,
169 &Interface
170 );
171 if (EFI_ERROR (Status)) {
172 return ;
173 }
174
175 //
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.
178 //
179 if (!mS3BootScriptTablePtr->AtRuntime) {
180 //
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
184 //
185 S3BootScriptInternalCloseTable ();
186 mS3BootScriptTablePtr->AtRuntime = TRUE;
187
188 //
189 // Save BootScript data to lockbox
190 //
191 SaveBootScriptDataToLockBox ();
192 }
193 }
194 /**
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.
197
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
201
202 @retval EFI_SUCCESS SmmEventCallback runs successfully
203 **/
204 EFI_STATUS
205 EFIAPI
206 S3BootScriptSmmEventCallBack (
207 IN CONST EFI_GUID *Protocol,
208 IN VOID *Interface,
209 IN EFI_HANDLE Handle
210 )
211 {
212 //
213 // Check if it is already done
214 //
215 if (mS3BootScriptTablePtr == &mS3BootScriptTable) {
216 return EFI_SUCCESS;
217 }
218
219 //
220 // Last chance to call-out, just make sure AtRuntime is set
221 //
222 S3BootScriptEventCallBack (NULL, NULL);
223
224 //
225 // Save a local copy
226 //
227 CopyMem (&mS3BootScriptTable, mS3BootScriptTablePtr, sizeof(*mS3BootScriptTablePtr));
228 //
229 // We should not use ACPINvs copy, because it is not safe.
230 //
231 mS3BootScriptTablePtr = &mS3BootScriptTable;
232
233 //
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.
236 //
237 mS3BootScriptTablePtr->InSmm = TRUE;
238
239 //
240 // Record LockBoxLength
241 //
242 mLockBoxLength = mS3BootScriptTable.TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE);
243
244 return EFI_SUCCESS;
245 }
246
247 /**
248 Library Constructor.
249 this function just identify it is a smm driver or non-smm driver linked against
250 with the library
251
252 @param ImageHandle The firmware allocated handle for the EFI image.
253 @param SystemTable A pointer to the EFI System Table.
254
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.
257 **/
258 RETURN_STATUS
259 EFIAPI
260 S3BootScriptLibInitialize (
261 IN EFI_HANDLE ImageHandle,
262 IN EFI_SYSTEM_TABLE *SystemTable
263 )
264 {
265 EFI_STATUS Status;
266 SCRIPT_TABLE_PRIVATE_DATA *S3TablePtr;
267 VOID *Registration;
268 EFI_SMM_BASE2_PROTOCOL *SmmBase2;
269 BOOLEAN InSmm;
270 EFI_SMM_SYSTEM_TABLE2 *Smst;
271 EFI_PHYSICAL_ADDRESS Buffer;
272
273 S3TablePtr = (SCRIPT_TABLE_PRIVATE_DATA*)(UINTN)PcdGet64(PcdS3BootScriptTablePrivateDataPtr);
274 //
275 // The Boot script private data is not be initialized. create it
276 //
277 if (S3TablePtr == 0) {
278 Buffer = SIZE_4GB - 1;
279 Status = gBS->AllocatePages (
280 AllocateMaxAddress,
281 EfiACPIMemoryNVS,
282 EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA)),
283 &Buffer
284 );
285 if (EFI_ERROR (Status)) {
286 return RETURN_OUT_OF_RESOURCES;
287 }
288 S3TablePtr = (VOID *) (UINTN) Buffer;
289
290 PcdSet64 (PcdS3BootScriptTablePrivateDataPtr, (UINT64) (UINTN)S3TablePtr);
291 ZeroMem (S3TablePtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));
292 //
293 // create event to notify the library system enter the runtime phase
294 //
295 mEnterRuntimeEvent = EfiCreateProtocolNotifyEvent (
296 &gEfiDxeSmmReadyToLockProtocolGuid,
297 TPL_CALLBACK,
298 S3BootScriptEventCallBack,
299 NULL,
300 &Registration
301 );
302 ASSERT (mEnterRuntimeEvent != NULL);
303 }
304 mS3BootScriptTablePtr = S3TablePtr;
305
306 //
307 // Get InSmm, we need to register SmmReadyToLock if this library is linked to SMM driver.
308 //
309 Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID**) &SmmBase2);
310 if (EFI_ERROR (Status)) {
311 return RETURN_SUCCESS;
312 }
313 Status = SmmBase2->InSmm (SmmBase2, &InSmm);
314 if (EFI_ERROR (Status)) {
315 return RETURN_SUCCESS;
316 }
317 if (!InSmm) {
318 return RETURN_SUCCESS;
319 }
320 //
321 // Good, we are in SMM
322 //
323 Status = SmmBase2->GetSmstLocation (SmmBase2, &Smst);
324 if (EFI_ERROR (Status)) {
325 return RETURN_SUCCESS;
326 }
327
328 //
329 // Then register event after lock
330 //
331 Registration = NULL;
332 Status = Smst->SmmRegisterProtocolNotify (
333 &gEfiSmmReadyToLockProtocolGuid,
334 S3BootScriptSmmEventCallBack,
335 &Registration
336 );
337 ASSERT_EFI_ERROR (Status);
338
339 return RETURN_SUCCESS;
340 }
341 /**
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.
346
347 @param EntryLength the new entry length.
348
349 @retval the address from which the a new s3 boot script entry will write into
350 **/
351 UINT8*
352 S3BootScriptGetBootTimeEntryAddAddress (
353 UINT8 EntryLength
354 )
355 {
356 EFI_PHYSICAL_ADDRESS S3TableBase;
357 EFI_PHYSICAL_ADDRESS NewS3TableBase;
358 UINT8 *NewEntryPtr;
359 UINT32 TableLength;
360 UINT16 PageNumber;
361 EFI_STATUS Status;
362 EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;
363
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
369 //
370 S3TableBase = 0xffffffff;
371 Status = gBS->AllocatePages (
372 AllocateMaxAddress,
373 EfiACPIMemoryNVS,
374 2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),
375 (EFI_PHYSICAL_ADDRESS*)&S3TableBase
376 );
377
378 if (EFI_ERROR(Status)) {
379 ASSERT_EFI_ERROR (Status);
380 return 0;
381 }
382 //
383 // Fill Table Header
384 //
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));
392 }
393
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))) {
398 //
399 // The buffer is too small to hold the table, Reallocate the buffer
400 //
401 NewS3TableBase = 0xffffffff;
402 Status = gBS->AllocatePages (
403 AllocateMaxAddress,
404 EfiACPIMemoryNVS,
405 2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),
406 (EFI_PHYSICAL_ADDRESS*)&NewS3TableBase
407 );
408
409 if (EFI_ERROR(Status)) {
410 ASSERT_EFI_ERROR (Status);
411 return 0;
412 }
413
414 CopyMem ((VOID*)(UINTN)NewS3TableBase, (VOID*)(UINTN)S3TableBase, TableLength);
415 gBS->FreePages (S3TableBase, mS3BootScriptTablePtr->TableMemoryPageNumber);
416
417 mS3BootScriptTablePtr->TableBase = (UINT8*)(UINTN)NewS3TableBase;
418 mS3BootScriptTablePtr->TableMemoryPageNumber = (UINT16) (2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));
419 }
420 //
421 // calculate the the start address for the new entry.
422 //
423 NewEntryPtr = mS3BootScriptTablePtr->TableBase + TableLength;
424
425 //
426 // update the table lenghth
427 //
428 mS3BootScriptTablePtr->TableLength = TableLength + EntryLength;
429
430 //
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.
434 //
435 //
436
437 return NewEntryPtr;
438 }
439 /**
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.
442
443 @param EntryLength the new entry length.
444
445 @retval the address from which the a new s3 runtime script entry will write into
446 **/
447 UINT8*
448 S3BootScriptGetRuntimeEntryAddAddress (
449 UINT8 EntryLength
450 )
451 {
452 UINT8 *NewEntryPtr;
453
454 NewEntryPtr = NULL;
455 //
456 // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node.
457 //
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;
461 //
462 // Append a terminate node on every insert
463 //
464 S3BootScriptInternalCloseTable ();
465 }
466 return (UINT8*)NewEntryPtr;
467 }
468 /**
469 To get the start address from which a new s3 boot script entry will write into.
470
471 @param EntryLength the new entry length.
472
473 @retval the address from which the a new s3 runtime script entry will write into
474 **/
475 UINT8*
476 S3BootScriptGetEntryAddAddress (
477 UINT8 EntryLength
478 )
479 {
480 UINT8* NewEntryPtr;
481 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader;
482 EFI_STATUS Status;
483
484 if (mS3BootScriptTablePtr->AtRuntime) {
485 //
486 // We need check InSmm when AtRuntime, because after SmmReadyToLock, only SMM driver is allowed to write boot script.
487 //
488 if (!mS3BootScriptTablePtr->InSmm) {
489 //
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.
492 //
493 DEBUG ((EFI_D_ERROR, "FATAL ERROR: Set boot script after ReadyToLock!!!\n"));
494 return NULL;
495 }
496
497 //
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.
502 //
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) {
506 //
507 // Restore it to use original value
508 //
509 RestoreLockBox (&mBootScriptDataGuid, NULL, NULL);
510 //
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.
513 //
514 mS3BootScriptTablePtr->TableLength = (UINT32)(mLockBoxLength - sizeof(EFI_BOOT_SCRIPT_TERMINATE));
515 }
516
517 NewEntryPtr = S3BootScriptGetRuntimeEntryAddAddress (EntryLength);
518 //
519 // Now the length field is updated, need sync to lockbox.
520 // So in S3 resume, the data can be restored correctly.
521 //
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)
528 );
529 ASSERT_EFI_ERROR (Status);
530 } else {
531 NewEntryPtr = S3BootScriptGetBootTimeEntryAddAddress (EntryLength);
532 }
533 return NewEntryPtr;
534
535 }
536
537 /**
538 Sync BootScript LockBox data.
539 **/
540 VOID
541 SyncBootScript (
542 VOID
543 )
544 {
545 EFI_STATUS Status;
546
547 if (!mS3BootScriptTablePtr->AtRuntime || !mS3BootScriptTablePtr->InSmm) {
548 return ;
549 }
550 //
551 // Update Terminate
552 // So in S3 resume, the data can be restored correctly.
553 //
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)
559 );
560 ASSERT_EFI_ERROR (Status);
561 }
562
563 /**
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
575 by this function.
576
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.
579
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
582 destroyed.
583
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
586
587 **/
588 UINT8*
589 EFIAPI
590 S3BootScriptCloseTable (
591 VOID
592 )
593 {
594 UINT8 *S3TableBase;
595 UINT32 TableLength;
596 UINT8 *Buffer;
597 EFI_STATUS Status;
598 EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;
599
600 S3TableBase = mS3BootScriptTablePtr->TableBase;
601 if (S3TableBase == 0) {
602 return 0;
603 }
604 //
605 // Append the termination record the S3 boot script table
606 //
607 S3BootScriptInternalCloseTable();
608 TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);
609 //
610 // Allocate the buffer and copy the boot script to the buffer.
611 //
612 Status = gBS->AllocatePool (
613 EfiBootServicesData,
614 (UINTN)TableLength,
615 (VOID **) &Buffer
616 );
617 if (EFI_ERROR (Status)) {
618 return 0;
619 }
620 CopyMem (Buffer, S3TableBase, TableLength);
621
622 //
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.
625 //
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
631
632 mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
633 return Buffer;
634 }
635 /**
636 Save I/O write to boot script
637
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.
642
643 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
644 @retval RETURN_SUCCESS Opcode is added.
645 **/
646 RETURN_STATUS
647 EFIAPI
648 S3BootScriptSaveIoWrite (
649 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
650 IN UINT64 Address,
651 IN UINTN Count,
652 IN VOID *Buffer
653 )
654
655 {
656 UINT8 Length;
657 UINT8 *Script;
658 UINT8 WidthInByte;
659 EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite;
660
661 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
662 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE) + (WidthInByte * Count));
663
664 Script = S3BootScriptGetEntryAddAddress (Length);
665 if (Script == NULL) {
666 return RETURN_OUT_OF_RESOURCES;
667 }
668 //
669 // save script data
670 //
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);
678
679 SyncBootScript ();
680
681 return RETURN_SUCCESS;
682 }
683
684 /**
685 Adds a record for an I/O modify operation into a S3 boot script table
686
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
691
692 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
693 @retval RETURN_SUCCESS Opcode is added.
694 **/
695 RETURN_STATUS
696 EFIAPI
697 S3BootScriptSaveIoReadWrite (
698 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
699 IN UINT64 Address,
700 IN VOID *Data,
701 IN VOID *DataMask
702 )
703 {
704 UINT8 Length;
705 UINT8 *Script;
706 UINT8 WidthInByte;
707 EFI_BOOT_SCRIPT_IO_READ_WRITE ScriptIoReadWrite;
708
709 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
710 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + (WidthInByte * 2));
711
712 Script = S3BootScriptGetEntryAddAddress (Length);
713 if (Script == NULL) {
714 return RETURN_OUT_OF_RESOURCES;
715 }
716 //
717 // Build script data
718 //
719 ScriptIoReadWrite.OpCode = EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE;
720 ScriptIoReadWrite.Length = Length;
721 ScriptIoReadWrite.Width = Width;
722 ScriptIoReadWrite.Address = Address;
723
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);
727
728 SyncBootScript ();
729
730 return RETURN_SUCCESS;
731 }
732 /**
733 Adds a record for a memory write operation into a specified boot script table.
734
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.
739
740 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
741 @retval RETURN_SUCCESS Opcode is added.
742 **/
743 RETURN_STATUS
744 EFIAPI
745 S3BootScriptSaveMemWrite (
746 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
747 IN UINT64 Address,
748 IN UINTN Count,
749 IN VOID *Buffer
750 )
751 {
752 UINT8 Length;
753 UINT8 *Script;
754 UINT8 WidthInByte;
755 EFI_BOOT_SCRIPT_MEM_WRITE ScriptMemWrite;
756
757 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
758 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE) + (WidthInByte * Count));
759
760 Script = S3BootScriptGetEntryAddAddress (Length);
761 if (Script == NULL) {
762 return RETURN_OUT_OF_RESOURCES;
763 }
764 //
765 // Build script data
766 //
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;
772
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);
775
776 SyncBootScript ();
777
778 return RETURN_SUCCESS;
779 }
780 /**
781 Adds a record for a memory modify operation into a specified boot script table.
782
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.
787
788 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
789 @retval RETURN_SUCCESS Opcode is added.
790 **/
791 RETURN_STATUS
792 EFIAPI
793 S3BootScriptSaveMemReadWrite (
794 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
795 IN UINT64 Address,
796 IN VOID *Data,
797 IN VOID *DataMask
798 )
799 {
800 UINT8 Length;
801 UINT8 *Script;
802 UINT8 WidthInByte;
803 EFI_BOOT_SCRIPT_MEM_READ_WRITE ScriptMemReadWrite;
804
805 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
806 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + (WidthInByte * 2));
807
808 Script = S3BootScriptGetEntryAddAddress (Length);
809 if (Script == NULL) {
810 return RETURN_OUT_OF_RESOURCES;
811 }
812 //
813 // Build script data
814 //
815 ScriptMemReadWrite.OpCode = EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE;
816 ScriptMemReadWrite.Length = Length;
817 ScriptMemReadWrite.Width = Width;
818 ScriptMemReadWrite.Address = Address;
819
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);
823
824 SyncBootScript ();
825
826 return RETURN_SUCCESS;
827 }
828 /**
829 Adds a record for a PCI configuration space write operation into a specified boot script table.
830
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.
835
836 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
837 @retval RETURN_SUCCESS Opcode is added.
838 **/
839 RETURN_STATUS
840 EFIAPI
841 S3BootScriptSavePciCfgWrite (
842 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
843 IN UINT64 Address,
844 IN UINTN Count,
845 IN VOID *Buffer
846 )
847 {
848 UINT8 Length;
849 UINT8 *Script;
850 UINT8 WidthInByte;
851 EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE ScriptPciWrite;
852
853 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
854 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE) + (WidthInByte * Count));
855
856 Script = S3BootScriptGetEntryAddAddress (Length);
857 if (Script == NULL) {
858 return RETURN_OUT_OF_RESOURCES;
859 }
860 //
861 // Build script data
862 //
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;
868
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);
871
872 SyncBootScript ();
873
874 return RETURN_SUCCESS;
875 }
876 /**
877 Adds a record for a PCI configuration space modify operation into a specified boot script table.
878
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.
883
884 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
885 @retval RETURN__SUCCESS Opcode is added.
886 **/
887 RETURN_STATUS
888 EFIAPI
889 S3BootScriptSavePciCfgReadWrite (
890 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
891 IN UINT64 Address,
892 IN VOID *Data,
893 IN VOID *DataMask
894 )
895 {
896 UINT8 Length;
897 UINT8 *Script;
898 UINT8 WidthInByte;
899 EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE ScriptPciReadWrite;
900
901 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
902 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + (WidthInByte * 2));
903
904 Script = S3BootScriptGetEntryAddAddress (Length);
905 if (Script == NULL) {
906 return RETURN_OUT_OF_RESOURCES;
907 }
908 //
909 // Build script data
910 //
911 ScriptPciReadWrite.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE;
912 ScriptPciReadWrite.Length = Length;
913 ScriptPciReadWrite.Width = Width;
914 ScriptPciReadWrite.Address = Address;
915
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);
918 CopyMem (
919 (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + WidthInByte),
920 DataMask,
921 WidthInByte
922 );
923
924 SyncBootScript ();
925
926 return RETURN_SUCCESS;
927 }
928 /**
929 Adds a record for a PCI configuration space modify operation into a specified boot script table.
930
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.
936
937 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
938 @retval RETURN_SUCCESS Opcode is added.
939 **/
940 RETURN_STATUS
941 EFIAPI
942 S3BootScriptSavePciCfg2Write (
943 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
944 IN UINT16 Segment,
945 IN UINT64 Address,
946 IN UINTN Count,
947 IN VOID *Buffer
948 )
949 {
950 UINT8 Length;
951 UINT8 *Script;
952 UINT8 WidthInByte;
953 EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE ScriptPciWrite2;
954
955 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
956 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE) + (WidthInByte * Count));
957
958 Script = S3BootScriptGetEntryAddAddress (Length);
959 if (Script == NULL) {
960 return RETURN_OUT_OF_RESOURCES;
961 }
962 //
963 // Build script data
964 //
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;
971
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);
974
975 SyncBootScript ();
976
977 return RETURN_SUCCESS;
978 }
979 /**
980 Adds a record for a PCI configuration space modify operation into a specified boot script table.
981
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.
987
988 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
989 @retval RETURN_SUCCESS Opcode is added.
990 **/
991 RETURN_STATUS
992 EFIAPI
993 S3BootScriptSavePciCfg2ReadWrite (
994 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
995 IN UINT16 Segment,
996 IN UINT64 Address,
997 IN VOID *Data,
998 IN VOID *DataMask
999 )
1000 {
1001 UINT8 Length;
1002 UINT8 *Script;
1003 UINT8 WidthInByte;
1004 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE ScriptPciReadWrite2;
1005
1006 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1007 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + (WidthInByte * 2));
1008
1009 Script = S3BootScriptGetEntryAddAddress (Length);
1010 if (Script == NULL) {
1011 return RETURN_OUT_OF_RESOURCES;
1012 }
1013 //
1014 // Build script data
1015 //
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;
1021
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);
1024 CopyMem (
1025 (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + WidthInByte),
1026 DataMask,
1027 WidthInByte
1028 );
1029
1030 SyncBootScript ();
1031
1032 return RETURN_SUCCESS;
1033 }
1034 /**
1035 Adds a record for an SMBus command execution into a specified boot script table.
1036
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
1039 transactions.
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.
1042
1043 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1044 @retval RETURN_SUCCESS Opcode is added.
1045 **/
1046 RETURN_STATUS
1047 EFIAPI
1048 S3BootScriptSaveSmbusExecute (
1049 IN UINTN SmBusAddress,
1050 IN EFI_SMBUS_OPERATION Operation,
1051 IN UINTN *Length,
1052 IN VOID *Buffer
1053 )
1054 {
1055 UINT8 DataSize;
1056 UINT8 *Script;
1057 EFI_BOOT_SCRIPT_SMBUS_EXECUTE ScriptSmbusExecute;
1058
1059 DataSize = (UINT8)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE) + (*Length));
1060
1061 Script = S3BootScriptGetEntryAddAddress (DataSize);
1062 if (Script == NULL) {
1063 return RETURN_OUT_OF_RESOURCES;
1064 }
1065 //
1066 // Build script data
1067 //
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;
1073
1074 CopyMem ((VOID*)Script, (VOID*)&ScriptSmbusExecute, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE));
1075 CopyMem (
1076 (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)),
1077 Buffer,
1078 (*Length)
1079 );
1080
1081 SyncBootScript ();
1082
1083 return RETURN_SUCCESS;
1084 }
1085 /**
1086 Adds a record for an execution stall on the processor into a specified boot script table.
1087
1088 @param Duration Duration in microseconds of the stall
1089
1090 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1091 @retval RETURN_SUCCESS Opcode is added.
1092 **/
1093 RETURN_STATUS
1094 EFIAPI
1095 S3BootScriptSaveStall (
1096 IN UINTN Duration
1097 )
1098 {
1099 UINT8 Length;
1100 UINT8 *Script;
1101 EFI_BOOT_SCRIPT_STALL ScriptStall;
1102
1103 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_STALL));
1104
1105 Script = S3BootScriptGetEntryAddAddress (Length);
1106 if (Script == NULL) {
1107 return RETURN_OUT_OF_RESOURCES;
1108 }
1109 //
1110 // Build script data
1111 //
1112 ScriptStall.OpCode = EFI_BOOT_SCRIPT_STALL_OPCODE;
1113 ScriptStall.Length = Length;
1114 ScriptStall.Duration = Duration;
1115
1116 CopyMem ((VOID*)Script, (VOID*)&ScriptStall, sizeof (EFI_BOOT_SCRIPT_STALL));
1117
1118 SyncBootScript ();
1119
1120 return RETURN_SUCCESS;
1121 }
1122 /**
1123 Adds a record for an execution stall on the processor into a specified boot script table.
1124
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.
1127
1128 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1129 @retval RETURN_SUCCESS Opcode is added.
1130 **/
1131 RETURN_STATUS
1132 EFIAPI
1133 S3BootScriptSaveDispatch2 (
1134 IN VOID *EntryPoint,
1135 IN VOID *Context
1136 )
1137 {
1138 UINT8 Length;
1139 UINT8 *Script;
1140 EFI_BOOT_SCRIPT_DISPATCH_2 ScriptDispatch2;
1141 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));
1142
1143 Script = S3BootScriptGetEntryAddAddress (Length);
1144 if (Script == NULL) {
1145 return RETURN_OUT_OF_RESOURCES;
1146 }
1147 //
1148 // Build script data
1149 //
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;
1154
1155 CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch2, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));
1156
1157 SyncBootScript ();
1158
1159 return RETURN_SUCCESS;
1160
1161 }
1162 /**
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.
1165
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.
1172
1173 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1174 @retval RETURN_SUCCESS Opcode is added.
1175
1176 **/
1177 RETURN_STATUS
1178 EFIAPI
1179 S3BootScriptSaveMemPoll (
1180 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
1181 IN UINT64 Address,
1182 IN VOID *BitMask,
1183 IN VOID *BitValue,
1184 IN UINTN Duration,
1185 IN UINTN LoopTimes
1186 )
1187 {
1188 UINT8 Length;
1189 UINT8 *Script;
1190 UINT8 WidthInByte;
1191 EFI_BOOT_SCRIPT_MEM_POLL ScriptMemPoll;
1192
1193 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1194
1195 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + (WidthInByte * 2));
1196
1197 Script = S3BootScriptGetEntryAddAddress (Length);
1198 if (Script == NULL) {
1199 return RETURN_OUT_OF_RESOURCES;
1200 }
1201 //
1202 // Build script data
1203 //
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;
1210
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));
1214
1215 SyncBootScript ();
1216
1217 return RETURN_SUCCESS;
1218 }
1219 /**
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.
1222
1223 @param InformationLength Length of the data in bytes
1224 @param Information Information to be logged in the boot scrpit
1225
1226 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1227 @retval RETURN_SUCCESS Opcode is added.
1228
1229 **/
1230 RETURN_STATUS
1231 EFIAPI
1232 S3BootScriptSaveInformation (
1233 IN UINT32 InformationLength,
1234 IN VOID *Information
1235 )
1236 {
1237 UINT8 Length;
1238 UINT8 *Script;
1239 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;
1240
1241 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);
1242
1243 Script = S3BootScriptGetEntryAddAddress (Length);
1244 if (Script == NULL) {
1245 return RETURN_OUT_OF_RESOURCES;
1246 }
1247 //
1248 // Build script data
1249 //
1250 ScriptInformation.OpCode = EFI_BOOT_SCRIPT_INFORMATION_OPCODE;
1251 ScriptInformation.Length = Length;
1252
1253
1254 ScriptInformation.InformationLength = InformationLength;
1255
1256 CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));
1257 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);
1258
1259 SyncBootScript ();
1260
1261 return RETURN_SUCCESS;
1262
1263 }
1264 /**
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.
1267
1268 @param String The string to save to boot script table
1269
1270 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1271 @retval RETURN_SUCCESS Opcode is added.
1272
1273 **/
1274 RETURN_STATUS
1275 EFIAPI
1276 S3BootScriptSaveInformationAsciiString (
1277 IN CONST CHAR8 *String
1278 )
1279 {
1280 return S3BootScriptSaveInformation (
1281 (UINT32) AsciiStrLen (String) + 1,
1282 (VOID*) String
1283 );
1284 }
1285 /**
1286 Adds a record for dispatching specified arbitrary code into a specified boot script table.
1287
1288 @param EntryPoint Entry point of the code to be dispatched.
1289
1290 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1291 @retval RETURN_SUCCESS Opcode is added.
1292 **/
1293 RETURN_STATUS
1294 EFIAPI
1295 S3BootScriptSaveDispatch (
1296 IN VOID *EntryPoint
1297 )
1298 {
1299 UINT8 Length;
1300 UINT8 *Script;
1301 EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch;
1302
1303 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH));
1304
1305 Script = S3BootScriptGetEntryAddAddress (Length);
1306 if (Script == NULL) {
1307 return RETURN_OUT_OF_RESOURCES;
1308 }
1309 //
1310 // Build script data
1311 //
1312 ScriptDispatch.OpCode = EFI_BOOT_SCRIPT_DISPATCH_OPCODE;
1313 ScriptDispatch.Length = Length;
1314 ScriptDispatch.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;
1315
1316 CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH));
1317
1318 SyncBootScript ();
1319
1320 return RETURN_SUCCESS;
1321
1322 }
1323 /**
1324 Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a
1325 defined duration.
1326
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.
1334
1335 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1336 @retval RETURN_SUCCESS Opcode is added.
1337
1338 **/
1339 RETURN_STATUS
1340 EFIAPI
1341 S3BootScriptSaveIoPoll (
1342 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
1343 IN UINT64 Address,
1344 IN VOID *Data,
1345 IN VOID *DataMask,
1346 IN UINT64 Delay
1347 )
1348 {
1349 UINT8 WidthInByte;
1350 UINT8 *Script;
1351 UINT8 Length;
1352 EFI_BOOT_SCRIPT_IO_POLL ScriptIoPoll;
1353
1354
1355 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1356 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));
1357
1358 Script = S3BootScriptGetEntryAddAddress (Length);
1359 if (Script == NULL) {
1360 return RETURN_OUT_OF_RESOURCES;
1361 }
1362 //
1363 // Build script data
1364 //
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;
1370
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);
1374
1375 SyncBootScript ();
1376
1377 return RETURN_SUCCESS;
1378 }
1379
1380 /**
1381 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1382 after a defined duration.
1383
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.
1391
1392 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1393 @retval RETURN_SUCCESS Opcode is added.
1394
1395 **/
1396 RETURN_STATUS
1397 EFIAPI
1398 S3BootScriptSavePciPoll (
1399 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
1400 IN UINT64 Address,
1401 IN VOID *Data,
1402 IN VOID *DataMask,
1403 IN UINT64 Delay
1404 )
1405 {
1406 UINT8 *Script;
1407 UINT8 WidthInByte;
1408 UINT8 Length;
1409 EFI_BOOT_SCRIPT_PCI_CONFIG_POLL ScriptPciPoll;
1410
1411 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1412 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));
1413
1414 Script = S3BootScriptGetEntryAddAddress (Length);
1415 if (Script == NULL) {
1416 return RETURN_OUT_OF_RESOURCES;
1417 }
1418 //
1419 // Build script data
1420 //
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;
1426
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);
1430
1431 SyncBootScript ();
1432
1433 return RETURN_SUCCESS;
1434 }
1435 /**
1436 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1437 after a defined duration.
1438
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.
1447
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.
1453 **/
1454 RETURN_STATUS
1455 EFIAPI
1456 S3BootScriptSavePci2Poll (
1457 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
1458 IN UINT16 Segment,
1459 IN UINT64 Address,
1460 IN VOID *Data,
1461 IN VOID *DataMask,
1462 IN UINT64 Delay
1463 )
1464 {
1465 UINT8 WidthInByte;
1466 UINT8 *Script;
1467 UINT8 Length;
1468 EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL ScriptPci2Poll;
1469
1470 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1471 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));
1472
1473 Script = S3BootScriptGetEntryAddAddress (Length);
1474 if (Script == NULL) {
1475 return RETURN_OUT_OF_RESOURCES;
1476 }
1477 //
1478 // Build script data
1479 //
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;
1486
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);
1490
1491 SyncBootScript ();
1492
1493 return RETURN_SUCCESS;
1494 }
1495 /**
1496 Do the calculation of start address from which a new s3 boot script entry will write into.
1497
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
1504 **/
1505 VOID
1506 S3BootScriptCalculateInsertAddress (
1507 IN UINT8 EntryLength,
1508 IN VOID *Position OPTIONAL,
1509 IN BOOLEAN BeforeOrAfter OPTIONAL,
1510 OUT UINT8 **Script
1511 )
1512 {
1513 UINTN TableLength;
1514 UINT8 *S3TableBase;
1515 UINTN PositionOffset;
1516 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;
1517 //
1518 // The entry inserting to table is already added to the end of the table
1519 //
1520 TableLength = mS3BootScriptTablePtr->TableLength - EntryLength;
1521 S3TableBase = mS3BootScriptTablePtr->TableBase ;
1522 //
1523 // calculate the Position offset
1524 //
1525 if (Position != NULL) {
1526 PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);
1527
1528 //
1529 // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.
1530 //
1531 if (!BeforeOrAfter) {
1532 CopyMem ((VOID*)&ScriptHeader, Position, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));
1533 PositionOffset += (ScriptHeader.Length);
1534 }
1535 //
1536 // Insert the node before the adjusted Position
1537 //
1538 CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);
1539 //
1540 // calculate the the start address for the new entry.
1541 //
1542 *Script = S3TableBase + PositionOffset;
1543
1544 } else {
1545 if (!BeforeOrAfter) {
1546 //
1547 // Insert the node to the end of the table
1548 //
1549 *Script = S3TableBase + TableLength;
1550 } else {
1551 //
1552 // Insert the node to the beginning of the table
1553 //
1554 PositionOffset = (UINTN) sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);
1555 CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);
1556 *Script = S3TableBase + PositionOffset;
1557 }
1558 }
1559 }
1560 /**
1561 Move the last boot script entry to the position
1562
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.
1570
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.
1574 **/
1575 RETURN_STATUS
1576 EFIAPI
1577 S3BootScriptMoveLastOpcode (
1578 IN BOOLEAN BeforeOrAfter,
1579 IN OUT VOID **Position OPTIONAL
1580 )
1581 {
1582 UINT8* Script;
1583 VOID *TempPosition;
1584 UINTN StartAddress;
1585 UINT32 TableLength;
1586 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;
1587 BOOLEAN ValidatePosition;
1588 UINT8* LastOpcode;
1589 UINT8 TempBootScriptEntry[BOOT_SCRIPT_NODE_MAX_LENGTH];
1590
1591 ValidatePosition = FALSE;
1592 TempPosition = (Position == NULL) ? NULL:(*Position);
1593 Script = mS3BootScriptTablePtr->TableBase;
1594 if (Script == 0) {
1595 return EFI_OUT_OF_RESOURCES;
1596 }
1597 StartAddress = (UINTN) Script;
1598 TableLength = mS3BootScriptTablePtr->TableLength;
1599 Script = Script + sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);
1600 LastOpcode = Script;
1601 //
1602 // Find the last boot Script Entry which is not the terminate node
1603 //
1604 while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {
1605 CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));
1606 if (TempPosition != NULL && TempPosition == Script) {
1607 //
1608 // If the position is specified, the position must be pointed to a boot script entry start address.
1609 //
1610 ValidatePosition = TRUE;
1611 }
1612 if (ScriptHeader.OpCode != S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE) {
1613 LastOpcode = Script;
1614 }
1615 Script = Script + ScriptHeader.Length;
1616 }
1617 //
1618 // If the position is specified, but not the start of a boot script entry, it is a invalid input
1619 //
1620 if (TempPosition != NULL && !ValidatePosition) {
1621 return RETURN_INVALID_PARAMETER;
1622 }
1623
1624 CopyMem ((VOID*)&ScriptHeader, LastOpcode, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));
1625
1626 CopyMem((VOID*)TempBootScriptEntry, LastOpcode, ScriptHeader.Length);
1627 //
1628 // Find the right position to write the node in
1629 //
1630 S3BootScriptCalculateInsertAddress (
1631 ScriptHeader.Length,
1632 TempPosition,
1633 BeforeOrAfter,
1634 &Script
1635 );
1636 //
1637 // Copy the node to Boot script table
1638 //
1639 CopyMem((VOID*)Script, (VOID*)TempBootScriptEntry, ScriptHeader.Length);
1640 //
1641 // return out the Position
1642 //
1643 if (Position != NULL) {
1644 *Position = Script;
1645 }
1646 return RETURN_SUCCESS;
1647 }
1648 /**
1649 Create a Label node in the boot script table.
1650
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
1660
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.
1664
1665 **/
1666 RETURN_STATUS
1667 EFIAPI
1668 S3BootScriptLabelInternal (
1669 IN BOOLEAN BeforeOrAfter,
1670 IN OUT VOID **Position OPTIONAL,
1671 IN UINT32 InformationLength,
1672 IN CONST CHAR8 *Information
1673 )
1674 {
1675 UINT8 Length;
1676 UINT8 *Script;
1677 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;
1678
1679 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);
1680
1681 Script = S3BootScriptGetEntryAddAddress (Length);
1682 if (Script == NULL) {
1683 return RETURN_OUT_OF_RESOURCES;
1684 }
1685 //
1686 // Build script data
1687 //
1688 ScriptInformation.OpCode = S3_BOOT_SCRIPT_LIB_LABEL_OPCODE;
1689 ScriptInformation.Length = Length;
1690
1691
1692 ScriptInformation.InformationLength = InformationLength;
1693
1694 CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));
1695 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);
1696
1697 return S3BootScriptMoveLastOpcode (BeforeOrAfter, Position);
1698
1699 }
1700 /**
1701 Find a label within the boot script table and, if not present, optionally create it.
1702
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.
1713
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
1718 Feature Flags.
1719 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
1720
1721 **/
1722 RETURN_STATUS
1723 EFIAPI
1724 S3BootScriptLabel (
1725 IN BOOLEAN BeforeOrAfter,
1726 IN BOOLEAN CreateIfNotFound,
1727 IN OUT VOID **Position OPTIONAL,
1728 IN CONST CHAR8 *Label
1729 )
1730 {
1731 UINT8* Script;
1732 UINTN StartAddress;
1733 UINT32 TableLength;
1734 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;
1735 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader;
1736 UINT32 LabelLength;
1737 //
1738 // Check NULL Label
1739 //
1740 if (Label == NULL) {
1741 return EFI_INVALID_PARAMETER;
1742 }
1743 //
1744 // Check empty Label
1745 //
1746 if (Label[0] == '\0') {
1747 return EFI_INVALID_PARAMETER;
1748 }
1749
1750 //
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
1753 // to be added.
1754 //
1755 Script = S3BootScriptGetEntryAddAddress (0);
1756 if (Script == NULL) {
1757 return RETURN_OUT_OF_RESOURCES;
1758 }
1759
1760 //
1761 // Check the header and search for existing label.
1762 //
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;
1767 }
1768 StartAddress = (UINTN) Script;
1769 TableLength = mS3BootScriptTablePtr->TableLength;
1770 Script = Script + TableHeader.Length;
1771 while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {
1772
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;
1777 return EFI_SUCCESS;
1778 }
1779 }
1780 Script = Script + ScriptHeader.Length;
1781 }
1782 if (CreateIfNotFound) {
1783 LabelLength = (UINT32)AsciiStrSize(Label);
1784 return S3BootScriptLabelInternal (BeforeOrAfter,Position, LabelLength, Label);
1785 } else {
1786 return EFI_NOT_FOUND;
1787 }
1788 }
1789
1790 /**
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
1795
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
1800 Feature Flags.
1801 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
1802
1803 **/
1804 RETURN_STATUS
1805 EFIAPI
1806 S3BootScriptCompare (
1807 IN UINT8 *Position1,
1808 IN UINT8 *Position2,
1809 OUT UINTN *RelativePosition
1810 )
1811 {
1812 UINT8* Script;
1813 UINT32 TableLength;
1814
1815 Script = mS3BootScriptTablePtr->TableBase;
1816 if (Script == NULL) {
1817 return EFI_OUT_OF_RESOURCES;
1818 }
1819 if (RelativePosition == NULL) {
1820 return EFI_INVALID_PARAMETER;
1821 }
1822 //
1823 // mS3BootScriptTablePtr->TableLength does not include the termination node, so add it up
1824 //
1825 TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);
1826 if (Position1 < Script || Position1 > Script+TableLength) {
1827 return EFI_INVALID_PARAMETER;
1828 }
1829 if (Position2 < Script || Position2 > Script+TableLength) {
1830 return EFI_INVALID_PARAMETER;
1831 }
1832 *RelativePosition = (Position1 < Position2)?-1:((Position1 == Position2)?0:1);
1833
1834 return EFI_SUCCESS;
1835 }
1836