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