]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
MdeModulePkg: Add PiDxeS3BootScriptLib
[mirror_edk2.git] / MdeModulePkg / Library / PiDxeS3BootScriptLib / BootScriptSave.c
1 /** @file
2 Save the S3 data to S3 boot script.
3
4 Copyright (c) 2006 - 2010, 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 // mS3BootScriptTablePtr->TableLength does not include EFI_BOOT_SCRIPT_TERMINATE, because we need add entry at runtime.
117 // Save all info here, just in case that no one will add boot script entry in SMM.
118 //
119 Status = SaveLockBox (
120 &mBootScriptDataGuid,
121 (VOID *)mS3BootScriptTablePtr->TableBase,
122 mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE)
123 );
124 ASSERT_EFI_ERROR (Status);
125
126 Status = SetLockBoxAttributes (&mBootScriptDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
127 ASSERT_EFI_ERROR (Status);
128
129 //
130 // Just need save TableBase.
131 // Do not update other field because they will NOT be used in S3.
132 //
133 Status = SaveLockBox (
134 &mBootScriptHeaderDataGuid,
135 (VOID *)&mS3BootScriptTablePtr->TableBase,
136 sizeof(mS3BootScriptTablePtr->TableBase)
137 );
138 ASSERT_EFI_ERROR (Status);
139
140 Status = SetLockBoxAttributes (&mBootScriptHeaderDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
141 ASSERT_EFI_ERROR (Status);
142 }
143
144 /**
145 This is the Event call back function to notify the Library the system is entering
146 run time phase.
147
148 @param Event Pointer to this event
149 @param Context Event hanlder private data
150 **/
151 VOID
152 EFIAPI
153 S3BootScriptEventCallBack (
154 IN EFI_EVENT Event,
155 IN VOID *Context
156 )
157 {
158 EFI_STATUS Status;
159 VOID *Interface;
160
161 //
162 // Try to locate it because EfiCreateProtocolNotifyEvent will trigger it once when registration.
163 // Just return if it is not found.
164 //
165 Status = gBS->LocateProtocol (
166 &gEfiDxeSmmReadyToLockProtocolGuid,
167 NULL,
168 &Interface
169 );
170 if (EFI_ERROR (Status)) {
171 return ;
172 }
173
174 //
175 // Here we should tell the library that we are enter into runtime phase. and
176 // the memory page number occupied by the table should not grow anymore.
177 //
178 if (!mS3BootScriptTablePtr->AtRuntime) {
179 //
180 // In boot time, we need not write the terminate node when adding a node to boot scipt table
181 // or else, that will impact the performance. However, in runtime, we should append terminate
182 // node on every add to boot script table
183 //
184 S3BootScriptInternalCloseTable ();
185 mS3BootScriptTablePtr->AtRuntime = TRUE;
186
187 //
188 // Save BootScript data to lockbox
189 //
190 SaveBootScriptDataToLockBox ();
191 }
192 }
193 /**
194 This is the Event call back function is triggered in SMM to notify the Library the system is entering
195 run time phase and set InSmm flag.
196
197 @param Protocol Points to the protocol's unique identifier
198 @param Interface Points to the interface instance
199 @param Handle The handle on which the interface was installed
200
201 @retval EFI_SUCCESS SmmEventCallback runs successfully
202 **/
203 EFI_STATUS
204 EFIAPI
205 S3BootScriptSmmEventCallBack (
206 IN CONST EFI_GUID *Protocol,
207 IN VOID *Interface,
208 IN EFI_HANDLE Handle
209 )
210 {
211 //
212 // Check if it is already done
213 //
214 if (mS3BootScriptTablePtr == &mS3BootScriptTable) {
215 return EFI_SUCCESS;
216 }
217
218 //
219 // Last chance to call-out, just make sure AtRuntime is set
220 //
221 S3BootScriptEventCallBack (NULL, NULL);
222
223 //
224 // Save a local copy
225 //
226 CopyMem (&mS3BootScriptTable, mS3BootScriptTablePtr, sizeof(*mS3BootScriptTablePtr));
227 //
228 // We should not use ACPINvs copy, because it is not safe.
229 //
230 mS3BootScriptTablePtr = &mS3BootScriptTable;
231
232 //
233 // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.
234 // InSmm will only be checked if AtRuntime is TRUE.
235 //
236 mS3BootScriptTablePtr->InSmm = TRUE;
237
238 //
239 // Record LockBoxLength
240 //
241 mLockBoxLength = mS3BootScriptTable.TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE);
242
243 return EFI_SUCCESS;
244 }
245
246 /**
247 Library Constructor.
248 this function just identify it is a smm driver or non-smm driver linked against
249 with the library
250
251 @param ImageHandle The firmware allocated handle for the EFI image.
252 @param SystemTable A pointer to the EFI System Table.
253
254 @retval RETURN_SUCCESS Allocate the global memory space to store S3 boot script table private data
255 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
256 **/
257 RETURN_STATUS
258 EFIAPI
259 S3BootScriptLibInitialize (
260 IN EFI_HANDLE ImageHandle,
261 IN EFI_SYSTEM_TABLE *SystemTable
262 )
263 {
264 EFI_STATUS Status;
265 SCRIPT_TABLE_PRIVATE_DATA *S3TablePtr;
266 VOID *Registration;
267 EFI_SMM_BASE2_PROTOCOL *SmmBase2;
268 BOOLEAN InSmm;
269 EFI_SMM_SYSTEM_TABLE2 *Smst;
270 EFI_PHYSICAL_ADDRESS Buffer;
271
272 S3TablePtr = (SCRIPT_TABLE_PRIVATE_DATA*)(UINTN)PcdGet64(PcdS3BootScriptTablePrivateDataPtr);
273 //
274 // The Boot script private data is not be initialized. create it
275 //
276 if (S3TablePtr == 0) {
277 Buffer = SIZE_4GB - 1;
278 Status = gBS->AllocatePages (
279 AllocateMaxAddress,
280 EfiACPIMemoryNVS,
281 EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA)),
282 &Buffer
283 );
284 if (EFI_ERROR (Status)) {
285 return RETURN_OUT_OF_RESOURCES;
286 }
287 S3TablePtr = (VOID *) (UINTN) Buffer;
288
289 PcdSet64 (PcdS3BootScriptTablePrivateDataPtr, (UINT64) (UINTN)S3TablePtr);
290 ZeroMem (S3TablePtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));
291 //
292 // create event to notify the library system enter the runtime phase
293 //
294 mEnterRuntimeEvent = EfiCreateProtocolNotifyEvent (
295 &gEfiDxeSmmReadyToLockProtocolGuid,
296 TPL_CALLBACK,
297 S3BootScriptEventCallBack,
298 NULL,
299 &Registration
300 );
301 ASSERT (mEnterRuntimeEvent != NULL);
302 }
303 mS3BootScriptTablePtr = S3TablePtr;
304
305 //
306 // Get InSmm, we need to register SmmReadyToLock if this library is linked to SMM driver.
307 //
308 Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID**) &SmmBase2);
309 if (EFI_ERROR (Status)) {
310 return RETURN_SUCCESS;
311 }
312 Status = SmmBase2->InSmm (SmmBase2, &InSmm);
313 if (EFI_ERROR (Status)) {
314 return RETURN_SUCCESS;
315 }
316 if (!InSmm) {
317 return RETURN_SUCCESS;
318 }
319 //
320 // Good, we are in SMM
321 //
322 Status = SmmBase2->GetSmstLocation (SmmBase2, &Smst);
323 if (EFI_ERROR (Status)) {
324 return RETURN_SUCCESS;
325 }
326
327 //
328 // Then register event after lock
329 //
330 Registration = NULL;
331 Status = Smst->SmmRegisterProtocolNotify (
332 &gEfiSmmReadyToLockProtocolGuid,
333 S3BootScriptSmmEventCallBack,
334 &Registration
335 );
336 ASSERT_EFI_ERROR (Status);
337
338 return RETURN_SUCCESS;
339 }
340 /**
341 To get the start address from which a new boot time s3 boot script entry will write into.
342 If the table is not exist, the functio will first allocate a buffer for the table
343 If the table buffer is not enough for the new entry, in non-smm mode, the funtion will
344 invoke reallocate to enlarge buffer.
345
346 @param EntryLength the new entry length.
347
348 @retval the address from which the a new s3 boot script entry will write into
349 **/
350 UINT8*
351 S3BootScriptGetBootTimeEntryAddAddress (
352 UINT8 EntryLength
353 )
354 {
355 EFI_PHYSICAL_ADDRESS S3TableBase;
356 EFI_PHYSICAL_ADDRESS NewS3TableBase;
357 UINT8 *NewEntryPtr;
358 UINT32 TableLength;
359 UINT16 PageNumber;
360 EFI_STATUS Status;
361 EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;
362
363 S3TableBase = (EFI_PHYSICAL_ADDRESS)(UINTN)(mS3BootScriptTablePtr->TableBase);
364 if (S3TableBase == 0) {
365 // The table is not exist. This is the first to add entry.
366 // Allocate ACPI script table space under 4G memory. We need it to save
367 // some settings done by CSM, which runs after normal script table closed
368 //
369 S3TableBase = 0xffffffff;
370 Status = gBS->AllocatePages (
371 AllocateMaxAddress,
372 EfiACPIMemoryNVS,
373 2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),
374 (EFI_PHYSICAL_ADDRESS*)&S3TableBase
375 );
376
377 if (EFI_ERROR(Status)) {
378 ASSERT_EFI_ERROR (Status);
379 return 0;
380 }
381 //
382 // Fill Table Header
383 //
384 ScriptTableInfo = (EFI_BOOT_SCRIPT_TABLE_HEADER*)(UINTN)S3TableBase;
385 ScriptTableInfo->OpCode = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;
386 ScriptTableInfo->Length = (UINT8) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
387 ScriptTableInfo->TableLength = 0; // will be calculate at CloseTable
388 mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
389 mS3BootScriptTablePtr->TableBase = (UINT8*)(UINTN)S3TableBase;
390 mS3BootScriptTablePtr->TableMemoryPageNumber = (UINT16)(2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));
391 }
392
393 // Here we do not count the reserved memory for runtime script table.
394 PageNumber = (UINT16)(mS3BootScriptTablePtr->TableMemoryPageNumber - PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));
395 TableLength = mS3BootScriptTablePtr->TableLength;
396 if ((UINT32)(PageNumber * EFI_PAGE_SIZE) < (TableLength + EntryLength)) {
397 //
398 // The buffer is too small to hold the table, Reallocate the buffer
399 //
400 NewS3TableBase = 0xffffffff;
401 Status = gBS->AllocatePages (
402 AllocateMaxAddress,
403 EfiACPIMemoryNVS,
404 2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),
405 (EFI_PHYSICAL_ADDRESS*)&NewS3TableBase
406 );
407
408 if (EFI_ERROR(Status)) {
409 ASSERT_EFI_ERROR (Status);
410 return 0;
411 }
412
413 CopyMem ((VOID*)(UINTN)NewS3TableBase, (VOID*)(UINTN)S3TableBase, TableLength);
414 gBS->FreePages (S3TableBase, mS3BootScriptTablePtr->TableMemoryPageNumber);
415
416 mS3BootScriptTablePtr->TableBase = (UINT8*)(UINTN)NewS3TableBase;
417 mS3BootScriptTablePtr->TableMemoryPageNumber = (UINT16) (2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));
418 }
419 //
420 // calculate the the start address for the new entry.
421 //
422 NewEntryPtr = mS3BootScriptTablePtr->TableBase + TableLength;
423
424 //
425 // update the table lenghth
426 //
427 mS3BootScriptTablePtr->TableLength = TableLength + EntryLength;
428
429 //
430 // In the boot time, we will not append the termination entry to the boot script
431 // table until the callers think there is no boot time data that should be added and
432 // it is caller's responsibility to explicit call the CloseTable.
433 //
434 //
435
436 return NewEntryPtr;
437 }
438 /**
439 To get the start address from which a new runtime s3 boot script entry will write into.
440 In this case, it should be ensured that there is enough buffer to hold the entry.
441
442 @param EntryLength the new entry length.
443
444 @retval the address from which the a new s3 runtime script entry will write into
445 **/
446 UINT8*
447 S3BootScriptGetRuntimeEntryAddAddress (
448 UINT8 EntryLength
449 )
450 {
451 UINT8 *NewEntryPtr;
452
453 NewEntryPtr = NULL;
454 //
455 // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node.
456 //
457 if (mS3BootScriptTablePtr->TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE) <= EFI_PAGES_TO_SIZE((UINT32)(mS3BootScriptTablePtr->TableMemoryPageNumber))) {
458 NewEntryPtr = mS3BootScriptTablePtr->TableBase + mS3BootScriptTablePtr->TableLength;
459 mS3BootScriptTablePtr->TableLength = mS3BootScriptTablePtr->TableLength + EntryLength;
460 //
461 // Append a terminate node on every insert
462 //
463 S3BootScriptInternalCloseTable ();
464 }
465 return (UINT8*)NewEntryPtr;
466 }
467 /**
468 To get the start address from which a new s3 boot script entry will write into.
469
470 @param EntryLength the new entry length.
471
472 @retval the address from which the a new s3 runtime script entry will write into
473 **/
474 UINT8*
475 S3BootScriptGetEntryAddAddress (
476 UINT8 EntryLength
477 )
478 {
479 UINT8* NewEntryPtr;
480 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader;
481 EFI_STATUS Status;
482
483 if (mS3BootScriptTablePtr->AtRuntime) {
484 //
485 // We need check InSmm when AtRuntime, because after SmmReadyToLock, only SMM driver is allowed to write boot script.
486 //
487 if (!mS3BootScriptTablePtr->InSmm) {
488 //
489 // Add DEBUG ERROR, so that we can find it at boot time.
490 // Do not use ASSERT, because we may have test invoke this interface.
491 //
492 DEBUG ((EFI_D_ERROR, "FATAL ERROR: Set boot script after ReadyToLock!!!\n"));
493 return NULL;
494 }
495
496 //
497 // NOTE: OS will restore ACPINvs data. After S3, the table length in mS3BootScriptTable (SMM) is different with
498 // table length in BootScriptTable header (ACPINvs).
499 // So here we need sync them. We choose ACPINvs table length, because we want to override the boot script saved
500 // in SMM every time.
501 //
502 ASSERT (mS3BootScriptTablePtr == &mS3BootScriptTable);
503 CopyMem ((VOID*)&TableHeader, (VOID*)mS3BootScriptTablePtr->TableBase, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));
504 if (mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE) != TableHeader.TableLength) {
505 //
506 // Restore it to use original value
507 //
508 RestoreLockBox (&mBootScriptDataGuid, NULL, NULL);
509 //
510 // Copy it again to get original value
511 // NOTE: We should NOT use TableHeader.TableLength, because it is already updated to be whole length.
512 //
513 mS3BootScriptTablePtr->TableLength = (UINT32)(mLockBoxLength - sizeof(EFI_BOOT_SCRIPT_TERMINATE));
514 }
515
516 NewEntryPtr = S3BootScriptGetRuntimeEntryAddAddress (EntryLength);
517 //
518 // Now the length field is updated, need sync to lockbox.
519 // So in S3 resume, the data can be restored correctly.
520 //
521 CopyMem ((VOID*)&TableHeader, (VOID*)mS3BootScriptTablePtr->TableBase, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));
522 Status = UpdateLockBox (
523 &mBootScriptDataGuid,
524 OFFSET_OF(EFI_BOOT_SCRIPT_TABLE_HEADER, TableLength),
525 &TableHeader.TableLength,
526 sizeof(TableHeader.TableLength)
527 );
528 ASSERT_EFI_ERROR (Status);
529 } else {
530 NewEntryPtr = S3BootScriptGetBootTimeEntryAddAddress (EntryLength);
531 }
532 return NewEntryPtr;
533
534 }
535
536 /**
537 Sync BootScript LockBox data.
538 **/
539 VOID
540 SyncBootScript (
541 VOID
542 )
543 {
544 EFI_STATUS Status;
545
546 if (!mS3BootScriptTablePtr->AtRuntime || !mS3BootScriptTablePtr->InSmm) {
547 return ;
548 }
549 //
550 // Update Terminate
551 // So in S3 resume, the data can be restored correctly.
552 //
553 Status = UpdateLockBox (
554 &mBootScriptDataGuid,
555 mLockBoxLength - sizeof(EFI_BOOT_SCRIPT_TERMINATE),
556 (VOID *)((UINTN)mS3BootScriptTablePtr->TableBase + mLockBoxLength - sizeof(EFI_BOOT_SCRIPT_TERMINATE)),
557 sizeof(EFI_BOOT_SCRIPT_TERMINATE)
558 );
559 ASSERT_EFI_ERROR (Status);
560 }
561
562 /**
563 This is an function to close the S3 boot script table. The function could only be called in
564 BOOT time phase. To comply with the Framework spec definition on
565 EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable(), this function will fulfill following things:
566 1. Closes the specified boot script table
567 2. It allocates a new memory pool to duplicate all the boot scripts in the specified table.
568 Once this function is called, the table maintained by the library will be destroyed
569 after it is copied into the allocated pool.
570 3. Any attempts to add a script record after calling this function will cause a new table
571 to be created by the library.
572 4. The base address of the allocated pool will be returned in Address. Note that after
573 using the boot script table, the CALLER is responsible for freeing the pool that is allocated
574 by this function.
575
576 In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is
577 for Framework Spec compatibility.
578
579 If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out
580 how to get the script to run on an S3 resume because the boot script maintained by the lib will be
581 destroyed.
582
583 @return the base address of the new copy of the boot script tble.
584 @note this function could only called in boot time phase
585
586 **/
587 UINT8*
588 EFIAPI
589 S3BootScriptCloseTable (
590 VOID
591 )
592 {
593 UINT8 *S3TableBase;
594 UINT32 TableLength;
595 UINT8 *Buffer;
596 EFI_STATUS Status;
597 EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;
598
599 S3TableBase = mS3BootScriptTablePtr->TableBase;
600 if (S3TableBase == 0) {
601 return 0;
602 }
603 //
604 // Append the termination record the S3 boot script table
605 //
606 S3BootScriptInternalCloseTable();
607 TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);
608 //
609 // Allocate the buffer and copy the boot script to the buffer.
610 //
611 Status = gBS->AllocatePool (
612 EfiBootServicesData,
613 (UINTN)TableLength,
614 (VOID **) &Buffer
615 );
616 if (EFI_ERROR (Status)) {
617 return 0;
618 }
619 CopyMem (Buffer, S3TableBase, TableLength);
620
621 //
622 // Destroy the table maintained by the library so that the next write operation
623 // will write the record to the first entry of the table.
624 //
625 // Fill the table header.
626 ScriptTableInfo = (EFI_BOOT_SCRIPT_TABLE_HEADER*)S3TableBase;
627 ScriptTableInfo->OpCode = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;
628 ScriptTableInfo->Length = (UINT8) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
629 ScriptTableInfo->TableLength = 0; // will be calculate at close the table
630
631 mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
632 return Buffer;
633 }
634 /**
635 Save I/O write to boot script
636
637 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
638 @param Address The base address of the I/O operations.
639 @param Count The number of I/O operations to perform.
640 @param Buffer The source buffer from which to write data.
641
642 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
643 @retval RETURN_SUCCESS Opcode is added.
644 **/
645 RETURN_STATUS
646 EFIAPI
647 S3BootScriptSaveIoWrite (
648 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
649 IN UINT64 Address,
650 IN UINTN Count,
651 IN VOID *Buffer
652 )
653
654 {
655 UINT8 Length;
656 UINT8 *Script;
657 UINT8 WidthInByte;
658 EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite;
659
660 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
661 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE) + (WidthInByte * Count));
662
663 Script = S3BootScriptGetEntryAddAddress (Length);
664 if (Script == NULL) {
665 return RETURN_OUT_OF_RESOURCES;
666 }
667 //
668 // save script data
669 //
670 ScriptIoWrite.OpCode = EFI_BOOT_SCRIPT_IO_WRITE_OPCODE;
671 ScriptIoWrite.Length = Length;
672 ScriptIoWrite.Width = Width;
673 ScriptIoWrite.Address = Address;
674 ScriptIoWrite.Count = (UINT32) Count;
675 CopyMem ((VOID*)Script, (VOID*)&ScriptIoWrite, sizeof(EFI_BOOT_SCRIPT_IO_WRITE));
676 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_WRITE)), Buffer, WidthInByte * Count);
677
678 SyncBootScript ();
679
680 return RETURN_SUCCESS;
681 }
682
683 /**
684 Adds a record for an I/O modify operation into a S3 boot script table
685
686 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
687 @param Address The base address of the I/O operations.
688 @param Data A pointer to the data to be OR-ed.
689 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register
690
691 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
692 @retval RETURN_SUCCESS Opcode is added.
693 **/
694 RETURN_STATUS
695 EFIAPI
696 S3BootScriptSaveIoReadWrite (
697 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
698 IN UINT64 Address,
699 IN VOID *Data,
700 IN VOID *DataMask
701 )
702 {
703 UINT8 Length;
704 UINT8 *Script;
705 UINT8 WidthInByte;
706 EFI_BOOT_SCRIPT_IO_READ_WRITE ScriptIoReadWrite;
707
708 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
709 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + (WidthInByte * 2));
710
711 Script = S3BootScriptGetEntryAddAddress (Length);
712 if (Script == NULL) {
713 return RETURN_OUT_OF_RESOURCES;
714 }
715 //
716 // Build script data
717 //
718 ScriptIoReadWrite.OpCode = EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE;
719 ScriptIoReadWrite.Length = Length;
720 ScriptIoReadWrite.Width = Width;
721 ScriptIoReadWrite.Address = Address;
722
723 CopyMem ((VOID*)Script, (VOID*)&ScriptIoReadWrite, sizeof(EFI_BOOT_SCRIPT_IO_READ_WRITE));
724 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE)), Data, WidthInByte);
725 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + WidthInByte), DataMask, WidthInByte);
726
727 SyncBootScript ();
728
729 return RETURN_SUCCESS;
730 }
731 /**
732 Adds a record for a memory write operation into a specified boot script table.
733
734 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
735 @param Address The base address of the memory operations
736 @param Count The number of memory operations to perform.
737 @param Buffer The source buffer from which to write the data.
738
739 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
740 @retval RETURN_SUCCESS Opcode is added.
741 **/
742 RETURN_STATUS
743 EFIAPI
744 S3BootScriptSaveMemWrite (
745 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
746 IN UINT64 Address,
747 IN UINTN Count,
748 IN VOID *Buffer
749 )
750 {
751 UINT8 Length;
752 UINT8 *Script;
753 UINT8 WidthInByte;
754 EFI_BOOT_SCRIPT_MEM_WRITE ScriptMemWrite;
755
756 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
757 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE) + (WidthInByte * Count));
758
759 Script = S3BootScriptGetEntryAddAddress (Length);
760 if (Script == NULL) {
761 return RETURN_OUT_OF_RESOURCES;
762 }
763 //
764 // Build script data
765 //
766 ScriptMemWrite.OpCode = EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE;
767 ScriptMemWrite.Length = Length;
768 ScriptMemWrite.Width = Width;
769 ScriptMemWrite.Address = Address;
770 ScriptMemWrite.Count = (UINT32) Count;
771
772 CopyMem ((VOID*)Script, (VOID*)&ScriptMemWrite, sizeof(EFI_BOOT_SCRIPT_MEM_WRITE));
773 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_WRITE)), Buffer, WidthInByte * Count);
774
775 SyncBootScript ();
776
777 return RETURN_SUCCESS;
778 }
779 /**
780 Adds a record for a memory modify operation into a specified boot script table.
781
782 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
783 @param Address The base address of the memory operations. Address needs alignment if required
784 @param Data A pointer to the data to be OR-ed.
785 @param DataMask A pointer to the data mask to be AND-ed with the data read from the register.
786
787 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
788 @retval RETURN_SUCCESS Opcode is added.
789 **/
790 RETURN_STATUS
791 EFIAPI
792 S3BootScriptSaveMemReadWrite (
793 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
794 IN UINT64 Address,
795 IN VOID *Data,
796 IN VOID *DataMask
797 )
798 {
799 UINT8 Length;
800 UINT8 *Script;
801 UINT8 WidthInByte;
802 EFI_BOOT_SCRIPT_MEM_READ_WRITE ScriptMemReadWrite;
803
804 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
805 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + (WidthInByte * 2));
806
807 Script = S3BootScriptGetEntryAddAddress (Length);
808 if (Script == NULL) {
809 return RETURN_OUT_OF_RESOURCES;
810 }
811 //
812 // Build script data
813 //
814 ScriptMemReadWrite.OpCode = EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE;
815 ScriptMemReadWrite.Length = Length;
816 ScriptMemReadWrite.Width = Width;
817 ScriptMemReadWrite.Address = Address;
818
819 CopyMem ((VOID*)Script, (VOID*)&ScriptMemReadWrite , sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE));
820 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE)), Data, WidthInByte);
821 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + WidthInByte), DataMask, WidthInByte);
822
823 SyncBootScript ();
824
825 return RETURN_SUCCESS;
826 }
827 /**
828 Adds a record for a PCI configuration space write operation into a specified boot script table.
829
830 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
831 @param Address The address within the PCI configuration space.
832 @param Count The number of PCI operations to perform.
833 @param Buffer The source buffer from which to write the data.
834
835 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
836 @retval RETURN_SUCCESS Opcode is added.
837 **/
838 RETURN_STATUS
839 EFIAPI
840 S3BootScriptSavePciCfgWrite (
841 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
842 IN UINT64 Address,
843 IN UINTN Count,
844 IN VOID *Buffer
845 )
846 {
847 UINT8 Length;
848 UINT8 *Script;
849 UINT8 WidthInByte;
850 EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE ScriptPciWrite;
851
852 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
853 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE) + (WidthInByte * Count));
854
855 Script = S3BootScriptGetEntryAddAddress (Length);
856 if (Script == NULL) {
857 return RETURN_OUT_OF_RESOURCES;
858 }
859 //
860 // Build script data
861 //
862 ScriptPciWrite.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE;
863 ScriptPciWrite.Length = Length;
864 ScriptPciWrite.Width = Width;
865 ScriptPciWrite.Address = Address;
866 ScriptPciWrite.Count = (UINT32) Count;
867
868 CopyMem ((VOID*)Script, (VOID*)&ScriptPciWrite, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE));
869 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE)), Buffer, WidthInByte * Count);
870
871 SyncBootScript ();
872
873 return RETURN_SUCCESS;
874 }
875 /**
876 Adds a record for a PCI configuration space modify operation into a specified boot script table.
877
878 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
879 @param Address The address within the PCI configuration space.
880 @param Data A pointer to the data to be OR-ed.The size depends on Width.
881 @param DataMask A pointer to the data mask to be AND-ed.
882
883 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
884 @retval RETURN__SUCCESS Opcode is added.
885 **/
886 RETURN_STATUS
887 EFIAPI
888 S3BootScriptSavePciCfgReadWrite (
889 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
890 IN UINT64 Address,
891 IN VOID *Data,
892 IN VOID *DataMask
893 )
894 {
895 UINT8 Length;
896 UINT8 *Script;
897 UINT8 WidthInByte;
898 EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE ScriptPciReadWrite;
899
900 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
901 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + (WidthInByte * 2));
902
903 Script = S3BootScriptGetEntryAddAddress (Length);
904 if (Script == NULL) {
905 return RETURN_OUT_OF_RESOURCES;
906 }
907 //
908 // Build script data
909 //
910 ScriptPciReadWrite.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE;
911 ScriptPciReadWrite.Length = Length;
912 ScriptPciReadWrite.Width = Width;
913 ScriptPciReadWrite.Address = Address;
914
915 CopyMem ((VOID*)Script, (VOID*)&ScriptPciReadWrite, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE));
916 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE)), Data, WidthInByte);
917 CopyMem (
918 (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + WidthInByte),
919 DataMask,
920 WidthInByte
921 );
922
923 SyncBootScript ();
924
925 return RETURN_SUCCESS;
926 }
927 /**
928 Adds a record for a PCI configuration space modify operation into a specified boot script table.
929
930 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
931 @param Segment The PCI segment number for Address.
932 @param Address The address within the PCI configuration space.
933 @param Count The number of PCI operations to perform.
934 @param Buffer The source buffer from which to write the data.
935
936 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
937 @retval RETURN_SUCCESS Opcode is added.
938 **/
939 RETURN_STATUS
940 EFIAPI
941 S3BootScriptSavePciCfg2Write (
942 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
943 IN UINT16 Segment,
944 IN UINT64 Address,
945 IN UINTN Count,
946 IN VOID *Buffer
947 )
948 {
949 UINT8 Length;
950 UINT8 *Script;
951 UINT8 WidthInByte;
952 EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE ScriptPciWrite2;
953
954 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
955 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE) + (WidthInByte * Count));
956
957 Script = S3BootScriptGetEntryAddAddress (Length);
958 if (Script == NULL) {
959 return RETURN_OUT_OF_RESOURCES;
960 }
961 //
962 // Build script data
963 //
964 ScriptPciWrite2.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE;
965 ScriptPciWrite2.Length = Length;
966 ScriptPciWrite2.Width = Width;
967 ScriptPciWrite2.Address = Address;
968 ScriptPciWrite2.Segment = Segment;
969 ScriptPciWrite2.Count = (UINT32)Count;
970
971 CopyMem ((VOID*)Script, (VOID*)&ScriptPciWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE));
972 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE)), Buffer, WidthInByte * Count);
973
974 SyncBootScript ();
975
976 return RETURN_SUCCESS;
977 }
978 /**
979 Adds a record for a PCI configuration space modify operation into a specified boot script table.
980
981 @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
982 @param Segment The PCI segment number for Address.
983 @param Address The address within the PCI configuration space.
984 @param Data A pointer to the data to be OR-ed. The size depends on Width.
985 @param DataMask A pointer to the data mask to be AND-ed.
986
987 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
988 @retval RETURN_SUCCESS Opcode is added.
989 **/
990 RETURN_STATUS
991 EFIAPI
992 S3BootScriptSavePciCfg2ReadWrite (
993 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
994 IN UINT16 Segment,
995 IN UINT64 Address,
996 IN VOID *Data,
997 IN VOID *DataMask
998 )
999 {
1000 UINT8 Length;
1001 UINT8 *Script;
1002 UINT8 WidthInByte;
1003 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE ScriptPciReadWrite2;
1004
1005 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1006 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + (WidthInByte * 2));
1007
1008 Script = S3BootScriptGetEntryAddAddress (Length);
1009 if (Script == NULL) {
1010 return RETURN_OUT_OF_RESOURCES;
1011 }
1012 //
1013 // Build script data
1014 //
1015 ScriptPciReadWrite2.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE;
1016 ScriptPciReadWrite2.Length = Length;
1017 ScriptPciReadWrite2.Width = Width;
1018 ScriptPciReadWrite2.Segment = Segment;
1019 ScriptPciReadWrite2.Address = Address;
1020
1021 CopyMem ((VOID*)Script, (VOID*)&ScriptPciReadWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE));
1022 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE)), Data, WidthInByte);
1023 CopyMem (
1024 (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + WidthInByte),
1025 DataMask,
1026 WidthInByte
1027 );
1028
1029 SyncBootScript ();
1030
1031 return RETURN_SUCCESS;
1032 }
1033 /**
1034 Adds a record for an SMBus command execution into a specified boot script table.
1035
1036 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length, and PEC.
1037 @param Operation Indicates which particular SMBus protocol it will use to execute the SMBus
1038 transactions.
1039 @param Length A pointer to signify the number of bytes that this operation will do.
1040 @param Buffer Contains the value of data to execute to the SMBUS slave device.
1041
1042 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1043 @retval RETURN_SUCCESS Opcode is added.
1044 **/
1045 RETURN_STATUS
1046 EFIAPI
1047 S3BootScriptSaveSmbusExecute (
1048 IN UINTN SmBusAddress,
1049 IN EFI_SMBUS_OPERATION Operation,
1050 IN UINTN *Length,
1051 IN VOID *Buffer
1052 )
1053 {
1054 UINT8 DataSize;
1055 UINT8 *Script;
1056 EFI_BOOT_SCRIPT_SMBUS_EXECUTE ScriptSmbusExecute;
1057
1058 DataSize = (UINT8)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE) + (*Length));
1059
1060 Script = S3BootScriptGetEntryAddAddress (DataSize);
1061 if (Script == NULL) {
1062 return RETURN_OUT_OF_RESOURCES;
1063 }
1064 //
1065 // Build script data
1066 //
1067 ScriptSmbusExecute.OpCode = EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE;
1068 ScriptSmbusExecute.Length = DataSize;
1069 ScriptSmbusExecute.SmBusAddress = (UINT64) SmBusAddress;
1070 ScriptSmbusExecute.Operation = Operation;
1071 ScriptSmbusExecute.DataSize = (UINT32) *Length;
1072
1073 CopyMem ((VOID*)Script, (VOID*)&ScriptSmbusExecute, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE));
1074 CopyMem (
1075 (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)),
1076 Buffer,
1077 (*Length)
1078 );
1079
1080 SyncBootScript ();
1081
1082 return RETURN_SUCCESS;
1083 }
1084 /**
1085 Adds a record for an execution stall on the processor into a specified boot script table.
1086
1087 @param Duration Duration in microseconds of the stall
1088
1089 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1090 @retval RETURN_SUCCESS Opcode is added.
1091 **/
1092 RETURN_STATUS
1093 EFIAPI
1094 S3BootScriptSaveStall (
1095 IN UINTN Duration
1096 )
1097 {
1098 UINT8 Length;
1099 UINT8 *Script;
1100 EFI_BOOT_SCRIPT_STALL ScriptStall;
1101
1102 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_STALL));
1103
1104 Script = S3BootScriptGetEntryAddAddress (Length);
1105 if (Script == NULL) {
1106 return RETURN_OUT_OF_RESOURCES;
1107 }
1108 //
1109 // Build script data
1110 //
1111 ScriptStall.OpCode = EFI_BOOT_SCRIPT_STALL_OPCODE;
1112 ScriptStall.Length = Length;
1113 ScriptStall.Duration = Duration;
1114
1115 CopyMem ((VOID*)Script, (VOID*)&ScriptStall, sizeof (EFI_BOOT_SCRIPT_STALL));
1116
1117 SyncBootScript ();
1118
1119 return RETURN_SUCCESS;
1120 }
1121 /**
1122 Adds a record for an execution stall on the processor into a specified boot script table.
1123
1124 @param EntryPoint Entry point of the code to be dispatched.
1125 @param Context Argument to be passed into the EntryPoint of the code to be dispatched.
1126
1127 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1128 @retval RETURN_SUCCESS Opcode is added.
1129 **/
1130 RETURN_STATUS
1131 EFIAPI
1132 S3BootScriptSaveDispatch2 (
1133 IN VOID *EntryPoint,
1134 IN VOID *Context
1135 )
1136 {
1137 UINT8 Length;
1138 UINT8 *Script;
1139 EFI_BOOT_SCRIPT_DISPATCH_2 ScriptDispatch2;
1140 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));
1141
1142 Script = S3BootScriptGetEntryAddAddress (Length);
1143 if (Script == NULL) {
1144 return RETURN_OUT_OF_RESOURCES;
1145 }
1146 //
1147 // Build script data
1148 //
1149 ScriptDispatch2.OpCode = EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE;
1150 ScriptDispatch2.Length = Length;
1151 ScriptDispatch2.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;
1152 ScriptDispatch2.Context = (EFI_PHYSICAL_ADDRESS)(UINTN)Context;
1153
1154 CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch2, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));
1155
1156 SyncBootScript ();
1157
1158 return RETURN_SUCCESS;
1159
1160 }
1161 /**
1162 Adds a record for memory reads of the memory location and continues when the exit criteria is
1163 satisfied or after a defined duration.
1164
1165 @param Width The width of the memory operations.
1166 @param Address The base address of the memory operations.
1167 @param BitMask A pointer to the bit mask to be AND-ed with the data read from the register.
1168 @param BitValue A pointer to the data value after to be Masked.
1169 @param Duration Duration in microseconds of the stall.
1170 @param LoopTimes The times of the register polling.
1171
1172 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1173 @retval RETURN_SUCCESS Opcode is added.
1174
1175 **/
1176 RETURN_STATUS
1177 EFIAPI
1178 S3BootScriptSaveMemPoll (
1179 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
1180 IN UINT64 Address,
1181 IN VOID *BitMask,
1182 IN VOID *BitValue,
1183 IN UINTN Duration,
1184 IN UINTN LoopTimes
1185 )
1186 {
1187 UINT8 Length;
1188 UINT8 *Script;
1189 UINT8 WidthInByte;
1190 EFI_BOOT_SCRIPT_MEM_POLL ScriptMemPoll;
1191
1192 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1193
1194 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + (WidthInByte * 2));
1195
1196 Script = S3BootScriptGetEntryAddAddress (Length);
1197 if (Script == NULL) {
1198 return RETURN_OUT_OF_RESOURCES;
1199 }
1200 //
1201 // Build script data
1202 //
1203 ScriptMemPoll.OpCode = EFI_BOOT_SCRIPT_MEM_POLL_OPCODE;
1204 ScriptMemPoll.Length = Length;
1205 ScriptMemPoll.Width = Width;
1206 ScriptMemPoll.Address = Address;
1207 ScriptMemPoll.Duration = Duration;
1208 ScriptMemPoll.LoopTimes = LoopTimes;
1209
1210 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL)), BitValue, WidthInByte);
1211 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + WidthInByte), BitMask, WidthInByte);
1212 CopyMem ((VOID*)Script, (VOID*)&ScriptMemPoll, sizeof (EFI_BOOT_SCRIPT_MEM_POLL));
1213
1214 SyncBootScript ();
1215
1216 return RETURN_SUCCESS;
1217 }
1218 /**
1219 Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only
1220 used for debugging script issues.
1221
1222 @param InformationLength Length of the data in bytes
1223 @param Information Information to be logged in the boot scrpit
1224
1225 @retval RETURN_UNSUPPORTED If entering runtime, this method will not support.
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 RETURN_STATUS Status;
1238 UINT8 Length;
1239 UINT8 *Script;
1240 EFI_PHYSICAL_ADDRESS Buffer;
1241 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;
1242
1243 if (mS3BootScriptTablePtr->AtRuntime) {
1244 return RETURN_UNSUPPORTED;
1245 }
1246 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION));
1247
1248 Buffer = 0xFFFFFFFF;
1249 Status = gBS->AllocatePages (
1250 AllocateMaxAddress,
1251 EfiACPIMemoryNVS,
1252 EFI_SIZE_TO_PAGES(InformationLength),
1253 &Buffer
1254 );
1255 if (EFI_ERROR (Status)) {
1256 return RETURN_OUT_OF_RESOURCES;
1257 }
1258
1259 Script = S3BootScriptGetEntryAddAddress (Length);
1260 if (Script == NULL) {
1261 return RETURN_OUT_OF_RESOURCES;
1262 }
1263 //
1264 // Build script data
1265 //
1266 ScriptInformation.OpCode = EFI_BOOT_SCRIPT_INFORMATION_OPCODE;
1267 ScriptInformation.Length = Length;
1268
1269
1270 ScriptInformation.InformationLength = InformationLength;
1271
1272 CopyMem ((VOID *)(UINTN)Buffer, Information,(UINTN) InformationLength);
1273 ScriptInformation.Information = (EFI_PHYSICAL_ADDRESS) (UINTN) Buffer;
1274
1275 CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));
1276 return RETURN_SUCCESS;
1277
1278 }
1279 /**
1280 Store a string in the boot script table. This opcode is a no-op on dispatch and is only
1281 used for debugging script issues.
1282
1283 @param String The string to save to boot script table
1284
1285 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1286 @retval RETURN_SUCCESS Opcode is added.
1287
1288 **/
1289 RETURN_STATUS
1290 EFIAPI
1291 S3BootScriptSaveInformationAsciiString (
1292 IN CONST CHAR8 *String
1293 )
1294 {
1295 return S3BootScriptSaveInformation (
1296 (UINT32) AsciiStrLen (String) + 1,
1297 (VOID*) String
1298 );
1299 }
1300 /**
1301 Adds a record for dispatching specified arbitrary code into a specified boot script table.
1302
1303 @param EntryPoint Entry point of the code to be dispatched.
1304
1305 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1306 @retval RETURN_SUCCESS Opcode is added.
1307 **/
1308 RETURN_STATUS
1309 EFIAPI
1310 S3BootScriptSaveDispatch (
1311 IN VOID *EntryPoint
1312 )
1313 {
1314 UINT8 Length;
1315 UINT8 *Script;
1316 EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch;
1317
1318 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH));
1319
1320 Script = S3BootScriptGetEntryAddAddress (Length);
1321 if (Script == NULL) {
1322 return RETURN_OUT_OF_RESOURCES;
1323 }
1324 //
1325 // Build script data
1326 //
1327 ScriptDispatch.OpCode = EFI_BOOT_SCRIPT_DISPATCH_OPCODE;
1328 ScriptDispatch.Length = Length;
1329 ScriptDispatch.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;
1330
1331 CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH));
1332
1333 SyncBootScript ();
1334
1335 return RETURN_SUCCESS;
1336
1337 }
1338 /**
1339 Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a
1340 defined duration.
1341
1342 @param Width The width of the I/O operations.
1343 @param Address The base address of the I/O operations.
1344 @param Data The comparison value used for the polling exit criteria.
1345 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1346 in Data are ignored when polling the memory address.
1347 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1348 granularity so the delay may be longer.
1349
1350 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1351 @retval RETURN_SUCCESS Opcode is added.
1352
1353 **/
1354 RETURN_STATUS
1355 EFIAPI
1356 S3BootScriptSaveIoPoll (
1357 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
1358 IN UINT64 Address,
1359 IN VOID *Data,
1360 IN VOID *DataMask,
1361 IN UINT64 Delay
1362 )
1363 {
1364 UINT8 WidthInByte;
1365 UINT8 *Script;
1366 UINT8 Length;
1367 EFI_BOOT_SCRIPT_IO_POLL ScriptIoPoll;
1368
1369
1370 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1371 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));
1372
1373 Script = S3BootScriptGetEntryAddAddress (Length);
1374 if (Script == NULL) {
1375 return RETURN_OUT_OF_RESOURCES;
1376 }
1377 //
1378 // Build script data
1379 //
1380 ScriptIoPoll.OpCode = EFI_BOOT_SCRIPT_IO_POLL_OPCODE;
1381 ScriptIoPoll.Length = (UINT8) (sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));
1382 ScriptIoPoll.Width = Width;
1383 ScriptIoPoll.Address = Address;
1384 ScriptIoPoll.Delay = Delay;
1385
1386 CopyMem ((VOID*)Script, (VOID*)&ScriptIoPoll, sizeof (EFI_BOOT_SCRIPT_IO_POLL));
1387 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL)), Data, WidthInByte);
1388 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL) + WidthInByte), DataMask, WidthInByte);
1389
1390 SyncBootScript ();
1391
1392 return RETURN_SUCCESS;
1393 }
1394
1395 /**
1396 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1397 after a defined duration.
1398
1399 @param Width The width of the I/O operations.
1400 @param Address The address within the PCI configuration space.
1401 @param Data The comparison value used for the polling exit criteria.
1402 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1403 in Data are ignored when polling the memory address
1404 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1405 granularity so the delay may be longer.
1406
1407 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1408 @retval RETURN_SUCCESS Opcode is added.
1409
1410 **/
1411 RETURN_STATUS
1412 EFIAPI
1413 S3BootScriptSavePciPoll (
1414 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
1415 IN UINT64 Address,
1416 IN VOID *Data,
1417 IN VOID *DataMask,
1418 IN UINT64 Delay
1419 )
1420 {
1421 UINT8 *Script;
1422 UINT8 WidthInByte;
1423 UINT8 Length;
1424 EFI_BOOT_SCRIPT_PCI_CONFIG_POLL ScriptPciPoll;
1425
1426 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1427 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));
1428
1429 Script = S3BootScriptGetEntryAddAddress (Length);
1430 if (Script == NULL) {
1431 return RETURN_OUT_OF_RESOURCES;
1432 }
1433 //
1434 // Build script data
1435 //
1436 ScriptPciPoll.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE;
1437 ScriptPciPoll.Length = (UINT8) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));
1438 ScriptPciPoll.Width = Width;
1439 ScriptPciPoll.Address = Address;
1440 ScriptPciPoll.Delay = Delay;
1441
1442 CopyMem ((VOID*)Script, (VOID*)&ScriptPciPoll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL));
1443 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL)), Data, WidthInByte);
1444 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + WidthInByte), DataMask, WidthInByte);
1445
1446 SyncBootScript ();
1447
1448 return RETURN_SUCCESS;
1449 }
1450 /**
1451 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
1452 after a defined duration.
1453
1454 @param Width The width of the I/O operations.
1455 @param Segment The PCI segment number for Address.
1456 @param Address The address within the PCI configuration space.
1457 @param Data The comparison value used for the polling exit criteria.
1458 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
1459 in Data are ignored when polling the memory address
1460 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
1461 granularity so the delay may be longer.
1462
1463 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1464 @retval RETURN_SUCCESS Opcode is added.
1465 @note A known Limitations in the implementation: When interpreting the opcode EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE
1466 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE and EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE, the 'Segment' parameter is assumed as
1467 Zero, or else, assert.
1468 **/
1469 RETURN_STATUS
1470 EFIAPI
1471 S3BootScriptSavePci2Poll (
1472 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
1473 IN UINT16 Segment,
1474 IN UINT64 Address,
1475 IN VOID *Data,
1476 IN VOID *DataMask,
1477 IN UINT64 Delay
1478 )
1479 {
1480 UINT8 WidthInByte;
1481 UINT8 *Script;
1482 UINT8 Length;
1483 EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL ScriptPci2Poll;
1484
1485 WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1486 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));
1487
1488 Script = S3BootScriptGetEntryAddAddress (Length);
1489 if (Script == NULL) {
1490 return RETURN_OUT_OF_RESOURCES;
1491 }
1492 //
1493 // Build script data
1494 //
1495 ScriptPci2Poll.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE;
1496 ScriptPci2Poll.Length = (UINT8) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));
1497 ScriptPci2Poll.Width = Width;
1498 ScriptPci2Poll.Segment = Segment;
1499 ScriptPci2Poll.Address = Address;
1500 ScriptPci2Poll.Delay = Delay;
1501
1502 CopyMem ((VOID*)Script, (VOID*)&ScriptPci2Poll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL));
1503 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL)), Data, WidthInByte);
1504 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + WidthInByte), DataMask, WidthInByte);
1505
1506 SyncBootScript ();
1507
1508 return RETURN_SUCCESS;
1509 }
1510 /**
1511 Do the calculation of start address from which a new s3 boot script entry will write into.
1512
1513 @param EntryLength The new entry length.
1514 @param Position specifies the position in the boot script table where the opcode will be
1515 inserted, either before or after, depending on BeforeOrAfter.
1516 @param BeforeOrAfter The flag to indicate to insert the nod before or after the position.
1517 This parameter is effective when InsertFlag is TRUE
1518 @param Script return out the position from which the a new s3 boot script entry will write into
1519 **/
1520 VOID
1521 S3BootScriptCalculateInsertAddress (
1522 IN UINT8 EntryLength,
1523 IN VOID *Position OPTIONAL,
1524 IN BOOLEAN BeforeOrAfter OPTIONAL,
1525 OUT UINT8 **Script
1526 )
1527 {
1528 UINTN TableLength;
1529 UINT8 *S3TableBase;
1530 UINTN PositionOffset;
1531 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;
1532 //
1533 // The entry inserting to table is already added to the end of the table
1534 //
1535 TableLength = mS3BootScriptTablePtr->TableLength - EntryLength;
1536 S3TableBase = mS3BootScriptTablePtr->TableBase ;
1537 //
1538 // calculate the Position offset
1539 //
1540 if (Position != NULL) {
1541 PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);
1542
1543 //
1544 // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.
1545 //
1546 if (!BeforeOrAfter) {
1547 CopyMem ((VOID*)&ScriptHeader, Position, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));
1548 PositionOffset += (ScriptHeader.Length);
1549 }
1550 //
1551 // Insert the node before the adjusted Position
1552 //
1553 CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);
1554 //
1555 // calculate the the start address for the new entry.
1556 //
1557 *Script = S3TableBase + PositionOffset;
1558
1559 } else {
1560 if (!BeforeOrAfter) {
1561 //
1562 // Insert the node to the end of the table
1563 //
1564 *Script = S3TableBase + TableLength;
1565 } else {
1566 //
1567 // Insert the node to the beginning of the table
1568 //
1569 PositionOffset = (UINTN) sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);
1570 CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);
1571 *Script = S3TableBase + PositionOffset;
1572 }
1573 }
1574 }
1575 /**
1576 Move the last boot script entry to the position
1577
1578 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
1579 in the boot script table specified by Position. If Position is NULL or points to
1580 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
1581 of the table (if FALSE).
1582 @param Position On entry, specifies the position in the boot script table where the opcode will be
1583 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
1584 the position of the inserted opcode in the boot script table.
1585
1586 @retval RETURN_OUT_OF_RESOURCES The table is not available.
1587 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
1588 @retval RETURN_SUCCESS Opcode is inserted.
1589 **/
1590 RETURN_STATUS
1591 EFIAPI
1592 S3BootScriptMoveLastOpcode (
1593 IN BOOLEAN BeforeOrAfter,
1594 IN OUT VOID **Position OPTIONAL
1595 )
1596 {
1597 UINT8* Script;
1598 VOID *TempPosition;
1599 UINTN StartAddress;
1600 UINT32 TableLength;
1601 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;
1602 BOOLEAN ValidatePosition;
1603 UINT8* LastOpcode;
1604 UINT8 TempBootScriptEntry[BOOT_SCRIPT_NODE_MAX_LENGTH];
1605
1606 ValidatePosition = FALSE;
1607 TempPosition = (Position == NULL) ? NULL:(*Position);
1608 Script = mS3BootScriptTablePtr->TableBase;
1609 if (Script == 0) {
1610 return EFI_OUT_OF_RESOURCES;
1611 }
1612 StartAddress = (UINTN) Script;
1613 TableLength = mS3BootScriptTablePtr->TableLength;
1614 Script = Script + sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);
1615 LastOpcode = Script;
1616 //
1617 // Find the last boot Script Entry which is not the terminate node
1618 //
1619 while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {
1620 CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));
1621 if (TempPosition != NULL && TempPosition == Script) {
1622 //
1623 // If the position is specified, the position must be pointed to a boot script entry start address.
1624 //
1625 ValidatePosition = TRUE;
1626 }
1627 if (ScriptHeader.OpCode != S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE) {
1628 LastOpcode = Script;
1629 }
1630 Script = Script + ScriptHeader.Length;
1631 }
1632 //
1633 // If the position is specified, but not the start of a boot script entry, it is a invalid input
1634 //
1635 if (TempPosition != NULL && !ValidatePosition) {
1636 return RETURN_INVALID_PARAMETER;
1637 }
1638
1639 CopyMem ((VOID*)&ScriptHeader, LastOpcode, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));
1640
1641 CopyMem((VOID*)TempBootScriptEntry, LastOpcode, ScriptHeader.Length);
1642 //
1643 // Find the right position to write the node in
1644 //
1645 S3BootScriptCalculateInsertAddress (
1646 ScriptHeader.Length,
1647 TempPosition,
1648 BeforeOrAfter,
1649 &Script
1650 );
1651 //
1652 // Copy the node to Boot script table
1653 //
1654 CopyMem((VOID*)Script, (VOID*)TempBootScriptEntry, ScriptHeader.Length);
1655 //
1656 // return out the Position
1657 //
1658 if (Position != NULL) {
1659 *Position = Script;
1660 }
1661 return RETURN_SUCCESS;
1662 }
1663 /**
1664 Create a Label node in the boot script table.
1665
1666 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
1667 in the boot script table specified by Position. If Position is NULL or points to
1668 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
1669 of the table (if FALSE).
1670 @param Position On entry, specifies the position in the boot script table where the opcode will be
1671 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
1672 the position of the inserted opcode in the boot script table.
1673 @param InformationLength Length of the label in bytes
1674 @param Information Label to be logged in the boot scrpit
1675
1676 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
1677 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
1678 @retval RETURN_SUCCESS Opcode is added.
1679
1680 **/
1681 RETURN_STATUS
1682 EFIAPI
1683 S3BootScriptLabelInternal (
1684 IN BOOLEAN BeforeOrAfter,
1685 IN OUT VOID **Position OPTIONAL,
1686 IN UINT32 InformationLength,
1687 IN CONST CHAR8 *Information
1688 )
1689 {
1690 UINT8 Length;
1691 UINT8 *Script;
1692 VOID *Buffer;
1693 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;
1694
1695 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);
1696
1697 Script = S3BootScriptGetEntryAddAddress (Length);
1698 if (Script == NULL) {
1699 return RETURN_OUT_OF_RESOURCES;
1700 }
1701 Buffer = Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION);
1702 //
1703 // Build script data
1704 //
1705 ScriptInformation.OpCode = S3_BOOT_SCRIPT_LIB_LABEL_OPCODE;
1706 ScriptInformation.Length = Length;
1707
1708
1709 ScriptInformation.InformationLength = InformationLength;
1710
1711 AsciiStrnCpy (Buffer, Information,(UINTN) InformationLength);
1712 ScriptInformation.Information = (EFI_PHYSICAL_ADDRESS) (UINTN) Buffer;
1713
1714 CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));
1715
1716 return S3BootScriptMoveLastOpcode (BeforeOrAfter, Position);
1717
1718 }
1719 /**
1720 Find a label within the boot script table and, if not present, optionally create it.
1721
1722 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE)
1723 or after (FALSE) the position in the boot script table
1724 specified by Position.
1725 @param CreateIfNotFound Specifies whether the label will be created if the label
1726 does not exists (TRUE) or not (FALSE).
1727 @param Position On entry, specifies the position in the boot script table
1728 where the opcode will be inserted, either before or after,
1729 depending on BeforeOrAfter. On exit, specifies the position
1730 of the inserted opcode in the boot script table.
1731 @param Label Points to the label which will be inserted in the boot script table.
1732
1733 @retval EFI_SUCCESS The operation succeeded. A record was added into the
1734 specified script table.
1735 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
1736 If the opcode is unknow or not supported because of the PCD
1737 Feature Flags.
1738 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
1739
1740 **/
1741 RETURN_STATUS
1742 EFIAPI
1743 S3BootScriptLabel (
1744 IN BOOLEAN BeforeOrAfter,
1745 IN BOOLEAN CreateIfNotFound,
1746 IN OUT VOID **Position OPTIONAL,
1747 IN CONST CHAR8 *Label
1748 )
1749 {
1750 UINT8* Script;
1751 UINTN StartAddress;
1752 UINT32 TableLength;
1753 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;
1754 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader;
1755 UINT32 LabelLength;
1756 //
1757 // Assume Label is not NULL
1758 //
1759 if (Label == NULL) {
1760 return EFI_INVALID_PARAMETER;
1761 }
1762
1763 //
1764 // Check that the script is initialized without adding an entry to the script.
1765 // The code must search for the label first befor it knows if a new entry needs
1766 // to be added.
1767 //
1768 Script = S3BootScriptGetEntryAddAddress (0);
1769 if (Script == NULL) {
1770 return RETURN_OUT_OF_RESOURCES;
1771 }
1772
1773 //
1774 // Check the header and search for existing label.
1775 //
1776 Script = mS3BootScriptTablePtr->TableBase;
1777 CopyMem ((VOID*)&TableHeader, Script, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));
1778 if (TableHeader.OpCode != S3_BOOT_SCRIPT_LIB_TABLE_OPCODE) {
1779 return EFI_INVALID_PARAMETER;
1780 }
1781 StartAddress = (UINTN) Script;
1782 TableLength = mS3BootScriptTablePtr->TableLength;
1783 Script = Script + TableHeader.Length;
1784 while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {
1785
1786 CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));
1787 if (ScriptHeader.OpCode == S3_BOOT_SCRIPT_LIB_LABEL_OPCODE) {
1788 if (AsciiStrCmp ((CHAR8 *)(UINTN)(Script+sizeof(EFI_BOOT_SCRIPT_INFORMATION)), Label) == 0) {
1789 (*Position) = Script;
1790 return EFI_SUCCESS;
1791 }
1792 }
1793 Script = Script + ScriptHeader.Length;
1794 }
1795 if (CreateIfNotFound) {
1796 LabelLength = (UINT32)AsciiStrSize(Label);
1797 return S3BootScriptLabelInternal (BeforeOrAfter,Position, LabelLength, Label);
1798 } else {
1799 return EFI_NOT_FOUND;
1800 }
1801 }
1802
1803 /**
1804 Compare two positions in the boot script table and return their relative position.
1805 @param Position1 The positions in the boot script table to compare
1806 @param Position2 The positions in the boot script table to compare
1807 @param RelativePosition On return, points to the result of the comparison
1808
1809 @retval EFI_SUCCESS The operation succeeded. A record was added into the
1810 specified script table.
1811 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
1812 If the opcode is unknow or not supported because of the PCD
1813 Feature Flags.
1814 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
1815
1816 **/
1817 RETURN_STATUS
1818 EFIAPI
1819 S3BootScriptCompare (
1820 IN UINT8 *Position1,
1821 IN UINT8 *Position2,
1822 OUT UINTN *RelativePosition
1823 )
1824 {
1825 UINT8* Script;
1826 UINT32 TableLength;
1827
1828 Script = mS3BootScriptTablePtr->TableBase;
1829 if (Script == NULL) {
1830 return EFI_OUT_OF_RESOURCES;
1831 }
1832 if (RelativePosition == NULL) {
1833 return EFI_INVALID_PARAMETER;
1834 }
1835 TableLength = ((EFI_BOOT_SCRIPT_TABLE_HEADER*)Script)->TableLength;
1836 //
1837 // If in boot time, TableLength does not include the termination node. so add it up
1838 //
1839 if (!mS3BootScriptTablePtr->AtRuntime) {
1840 TableLength += sizeof(EFI_BOOT_SCRIPT_TERMINATE);
1841 }
1842 if (Position1 < Script || Position1 > Script+TableLength) {
1843 return EFI_INVALID_PARAMETER;
1844 }
1845 if (Position2 < Script || Position2 > Script+TableLength) {
1846 return EFI_INVALID_PARAMETER;
1847 }
1848 *RelativePosition = (Position1 < Position2)?-1:((Position1 == Position2)?0:1);
1849
1850 return EFI_SUCCESS;
1851 }
1852