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