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