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