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