]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
Correct function comments of S3BootScriptCloseTable().
[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
ce68d3bc 4 Copyright (c) 2006 - 2012, 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
65 @return the base address of the boot script tble. \r
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
654 @return the base address of the new copy of the boot script tble. \r
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
1383 \r
1384 @param Width The width of the memory operations.\r
1385 @param Address The base address of the memory operations.\r
1386 @param BitMask A pointer to the bit mask to be AND-ed with the data read from the register.\r
1387 @param BitValue A pointer to the data value after to be Masked.\r
1388 @param Duration Duration in microseconds of the stall.\r
1389 @param LoopTimes The times of the register polling.\r
1390\r
1391 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1392 @retval RETURN_SUCCESS Opcode is added.\r
1393\r
1394**/\r
1395RETURN_STATUS\r
1396EFIAPI\r
1397S3BootScriptSaveMemPoll (\r
1398 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1399 IN UINT64 Address,\r
1400 IN VOID *BitMask,\r
1401 IN VOID *BitValue,\r
1402 IN UINTN Duration,\r
1403 IN UINTN LoopTimes\r
1404 )\r
1405{\r
1406 UINT8 Length;\r
1407 UINT8 *Script;\r
1408 UINT8 WidthInByte; \r
1409 EFI_BOOT_SCRIPT_MEM_POLL ScriptMemPoll; \r
1410\r
1411 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
1412 \r
1413 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + (WidthInByte * 2));\r
1414 \r
1415 Script = S3BootScriptGetEntryAddAddress (Length);\r
1416 if (Script == NULL) {\r
1417 return RETURN_OUT_OF_RESOURCES;\r
1418 }\r
1419 //\r
1420 // Build script data\r
1421 //\r
1422 ScriptMemPoll.OpCode = EFI_BOOT_SCRIPT_MEM_POLL_OPCODE;\r
1423 ScriptMemPoll.Length = Length;\r
1424 ScriptMemPoll.Width = Width; \r
1425 ScriptMemPoll.Address = Address;\r
1426 ScriptMemPoll.Duration = Duration;\r
1427 ScriptMemPoll.LoopTimes = LoopTimes;\r
1428\r
1429 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL)), BitValue, WidthInByte);\r
1430 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + WidthInByte), BitMask, WidthInByte);\r
1431 CopyMem ((VOID*)Script, (VOID*)&ScriptMemPoll, sizeof (EFI_BOOT_SCRIPT_MEM_POLL)); \r
1432\r
02f49fc2 1433 SyncBootScript (Script);\r
64d14edf 1434\r
1435 return RETURN_SUCCESS;\r
1436}\r
1437/**\r
1438 Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only\r
1439 used for debugging script issues.\r
1440 \r
1441 @param InformationLength Length of the data in bytes\r
1442 @param Information Information to be logged in the boot scrpit\r
1443 \r
64d14edf 1444 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1445 @retval RETURN_SUCCESS Opcode is added.\r
1446\r
1447**/\r
1448RETURN_STATUS\r
1449EFIAPI\r
1450S3BootScriptSaveInformation (\r
1451 IN UINT32 InformationLength, \r
1452 IN VOID *Information\r
1453 )\r
1454{\r
64d14edf 1455 UINT8 Length;\r
1456 UINT8 *Script;\r
64d14edf 1457 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;\r
1458\r
93b21ade
SZ
1459 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);\r
1460\r
64d14edf 1461 Script = S3BootScriptGetEntryAddAddress (Length);\r
1462 if (Script == NULL) {\r
1463 return RETURN_OUT_OF_RESOURCES;\r
1464 }\r
1465 //\r
1466 // Build script data\r
1467 //\r
1468 ScriptInformation.OpCode = EFI_BOOT_SCRIPT_INFORMATION_OPCODE;\r
1469 ScriptInformation.Length = Length;\r
1470\r
1471\r
1472 ScriptInformation.InformationLength = InformationLength; \r
1473\r
93b21ade
SZ
1474 CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
1475 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);\r
1476\r
02f49fc2 1477 SyncBootScript (Script);\r
93b21ade 1478\r
64d14edf 1479 return RETURN_SUCCESS;\r
1480\r
1481}\r
1482/**\r
1483 Store a string in the boot script table. This opcode is a no-op on dispatch and is only\r
1484 used for debugging script issues.\r
1485 \r
1486 @param String The string to save to boot script table\r
1487 \r
1488 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1489 @retval RETURN_SUCCESS Opcode is added.\r
1490\r
1491**/\r
1492RETURN_STATUS\r
1493EFIAPI\r
1494S3BootScriptSaveInformationAsciiString (\r
1495 IN CONST CHAR8 *String\r
1496 )\r
1497{\r
1498 return S3BootScriptSaveInformation ( \r
1499 (UINT32) AsciiStrLen (String) + 1, \r
1500 (VOID*) String\r
1501 );\r
1502}\r
1503/**\r
1504 Adds a record for dispatching specified arbitrary code into a specified boot script table.\r
1505\r
1506 @param EntryPoint Entry point of the code to be dispatched.\r
1507 \r
1508 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1509 @retval RETURN_SUCCESS Opcode is added.\r
1510**/\r
1511RETURN_STATUS\r
1512EFIAPI\r
1513S3BootScriptSaveDispatch (\r
1514 IN VOID *EntryPoint\r
1515 )\r
1516{\r
1517 UINT8 Length;\r
1518 UINT8 *Script;\r
1519 EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch;\r
1520 \r
1521 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH));\r
1522 \r
1523 Script = S3BootScriptGetEntryAddAddress (Length);\r
1524 if (Script == NULL) {\r
1525 return RETURN_OUT_OF_RESOURCES;\r
1526 } \r
1527 //\r
1528 // Build script data\r
1529 //\r
1530 ScriptDispatch.OpCode = EFI_BOOT_SCRIPT_DISPATCH_OPCODE;\r
1531 ScriptDispatch.Length = Length;\r
1532 ScriptDispatch.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;\r
1533 \r
1534 CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH)); \r
1535 \r
02f49fc2 1536 SyncBootScript (Script);\r
64d14edf 1537\r
1538 return RETURN_SUCCESS;\r
1539\r
1540}\r
1541/**\r
1542 Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a\r
1543 defined duration.\r
1544 \r
1545 @param Width The width of the I/O operations. \r
1546 @param Address The base address of the I/O operations.\r
1547 @param Data The comparison value used for the polling exit criteria.\r
1548 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
1549 in Data are ignored when polling the memory address.\r
1550 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer\r
1551 granularity so the delay may be longer.\r
1552\r
1553 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1554 @retval RETURN_SUCCESS Opcode is added.\r
1555\r
1556**/\r
1557RETURN_STATUS\r
1558EFIAPI\r
1559S3BootScriptSaveIoPoll (\r
1560 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1561 IN UINT64 Address,\r
1562 IN VOID *Data,\r
1563 IN VOID *DataMask, \r
1564 IN UINT64 Delay \r
1565 )\r
1566{\r
1567 UINT8 WidthInByte; \r
1568 UINT8 *Script;\r
1569 UINT8 Length;\r
1570 EFI_BOOT_SCRIPT_IO_POLL ScriptIoPoll;\r
1571 \r
1572\r
1573 WidthInByte = (UINT8) (0x01 << (Width & 0x03)); \r
1574 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));\r
1575 \r
1576 Script = S3BootScriptGetEntryAddAddress (Length);\r
1577 if (Script == NULL) {\r
1578 return RETURN_OUT_OF_RESOURCES;\r
1579 } \r
1580 //\r
1581 // Build script data\r
1582 //\r
1583 ScriptIoPoll.OpCode = EFI_BOOT_SCRIPT_IO_POLL_OPCODE;\r
1584 ScriptIoPoll.Length = (UINT8) (sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));\r
1585 ScriptIoPoll.Width = Width; \r
1586 ScriptIoPoll.Address = Address;\r
1587 ScriptIoPoll.Delay = Delay;\r
1588\r
1589 CopyMem ((VOID*)Script, (VOID*)&ScriptIoPoll, sizeof (EFI_BOOT_SCRIPT_IO_POLL)); \r
1590 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL)), Data, WidthInByte);\r
1591 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL) + WidthInByte), DataMask, WidthInByte);\r
1592 \r
02f49fc2 1593 SyncBootScript (Script);\r
64d14edf 1594\r
1595 return RETURN_SUCCESS;\r
1596}\r
1597\r
1598/**\r
1599 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or\r
1600 after a defined duration.\r
1601\r
1602 @param Width The width of the I/O operations. \r
1603 @param Address The address within the PCI configuration space.\r
1604 @param Data The comparison value used for the polling exit criteria.\r
1605 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
1606 in Data are ignored when polling the memory address\r
1607 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer\r
1608 granularity so the delay may be longer.\r
1609\r
1610 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1611 @retval RETURN_SUCCESS Opcode is added.\r
2a956f74 1612 @note A known Limitations in the implementation which is 64bits operations are not supported.\r
64d14edf 1613\r
1614**/\r
1615RETURN_STATUS\r
1616EFIAPI\r
1617S3BootScriptSavePciPoll (\r
1618 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1619 IN UINT64 Address,\r
1620 IN VOID *Data,\r
1621 IN VOID *DataMask,\r
1622 IN UINT64 Delay\r
1623)\r
1624{\r
1625 UINT8 *Script;\r
1626 UINT8 WidthInByte; \r
1627 UINT8 Length;\r
1628 EFI_BOOT_SCRIPT_PCI_CONFIG_POLL ScriptPciPoll;\r
1629\r
2a956f74
SZ
1630 if (Width == S3BootScriptWidthUint64 ||\r
1631 Width == S3BootScriptWidthFifoUint64 ||\r
1632 Width == S3BootScriptWidthFillUint64) {\r
1633 return EFI_INVALID_PARAMETER;\r
1634 }\r
1635\r
64d14edf 1636 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
1637 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));\r
1638 \r
1639 Script = S3BootScriptGetEntryAddAddress (Length);\r
1640 if (Script == NULL) {\r
1641 return RETURN_OUT_OF_RESOURCES;\r
1642 }\r
1643 //\r
1644 // Build script data\r
1645 //\r
1646 ScriptPciPoll.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE;\r
1647 ScriptPciPoll.Length = (UINT8) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));\r
1648 ScriptPciPoll.Width = Width; \r
1649 ScriptPciPoll.Address = Address;\r
1650 ScriptPciPoll.Delay = Delay;\r
1651\r
1652 CopyMem ((VOID*)Script, (VOID*)&ScriptPciPoll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL));\r
1653 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL)), Data, WidthInByte);\r
1654 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + WidthInByte), DataMask, WidthInByte);\r
1655 \r
02f49fc2 1656 SyncBootScript (Script);\r
64d14edf 1657\r
1658 return RETURN_SUCCESS;\r
1659}\r
1660/**\r
1661 Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or\r
1662 after a defined duration.\r
1663\r
1664 @param Width The width of the I/O operations. \r
1665 @param Segment The PCI segment number for Address.\r
1666 @param Address The address within the PCI configuration space.\r
1667 @param Data The comparison value used for the polling exit criteria.\r
1668 @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
1669 in Data are ignored when polling the memory address\r
1670 @param Delay The number of 100ns units to poll. Note that timer available may be of poorer\r
1671 granularity so the delay may be longer.\r
1672\r
1673 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1674 @retval RETURN_SUCCESS Opcode is added.\r
2a956f74
SZ
1675 @note A known Limitations in the implementation which is non-zero Segment and 64bits operations are not supported.\r
1676\r
64d14edf 1677**/\r
1678RETURN_STATUS\r
1679EFIAPI\r
1680S3BootScriptSavePci2Poll (\r
1681 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
1682 IN UINT16 Segment,\r
1683 IN UINT64 Address,\r
1684 IN VOID *Data,\r
1685 IN VOID *DataMask,\r
1686 IN UINT64 Delay\r
1687)\r
1688{\r
1689 UINT8 WidthInByte; \r
1690 UINT8 *Script;\r
1691 UINT8 Length;\r
1692 EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL ScriptPci2Poll;\r
2a956f74
SZ
1693\r
1694 if (Segment != 0 ||\r
1695 Width == S3BootScriptWidthUint64 ||\r
1696 Width == S3BootScriptWidthFifoUint64 ||\r
1697 Width == S3BootScriptWidthFillUint64) {\r
1698 return EFI_INVALID_PARAMETER;\r
1699 }\r
1700\r
64d14edf 1701 WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
1702 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));\r
1703 \r
1704 Script = S3BootScriptGetEntryAddAddress (Length);\r
1705 if (Script == NULL) {\r
1706 return RETURN_OUT_OF_RESOURCES;\r
1707 } \r
1708 //\r
1709 // Build script data\r
1710 //\r
1711 ScriptPci2Poll.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE;\r
1712 ScriptPci2Poll.Length = (UINT8) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));\r
1713 ScriptPci2Poll.Width = Width; \r
1714 ScriptPci2Poll.Segment = Segment;\r
1715 ScriptPci2Poll.Address = Address;\r
1716 ScriptPci2Poll.Delay = Delay;\r
1717\r
1718 CopyMem ((VOID*)Script, (VOID*)&ScriptPci2Poll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL));\r
1719 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL)), Data, WidthInByte);\r
1720 CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + WidthInByte), DataMask, WidthInByte);\r
1721 \r
02f49fc2 1722 SyncBootScript (Script);\r
64d14edf 1723\r
1724 return RETURN_SUCCESS;\r
1725}\r
1726/**\r
1727 Do the calculation of start address from which a new s3 boot script entry will write into.\r
1728 \r
1729 @param EntryLength The new entry length.\r
1730 @param Position specifies the position in the boot script table where the opcode will be\r
1731 inserted, either before or after, depending on BeforeOrAfter. \r
1732 @param BeforeOrAfter The flag to indicate to insert the nod before or after the position.\r
1733 This parameter is effective when InsertFlag is TRUE\r
1734 @param Script return out the position from which the a new s3 boot script entry will write into\r
1735**/\r
1736VOID\r
1737S3BootScriptCalculateInsertAddress (\r
1738 IN UINT8 EntryLength,\r
1739 IN VOID *Position OPTIONAL,\r
1740 IN BOOLEAN BeforeOrAfter OPTIONAL,\r
1741 OUT UINT8 **Script \r
1742 )\r
1743{\r
1744 UINTN TableLength;\r
1745 UINT8 *S3TableBase;\r
1746 UINTN PositionOffset; \r
1747 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;\r
1748 //\r
1749 // The entry inserting to table is already added to the end of the table\r
1750 //\r
1751 TableLength = mS3BootScriptTablePtr->TableLength - EntryLength;\r
1752 S3TableBase = mS3BootScriptTablePtr->TableBase ;\r
1753 // \r
1754 // calculate the Position offset\r
1755 //\r
1756 if (Position != NULL) {\r
1757 PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);\r
1758 \r
1759 //\r
1760 // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.\r
1761 //\r
1762 if (!BeforeOrAfter) {\r
1763 CopyMem ((VOID*)&ScriptHeader, Position, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
1764 PositionOffset += (ScriptHeader.Length);\r
1765 }\r
1766 // \r
1767 // Insert the node before the adjusted Position\r
1768 //\r
1769 CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset); \r
1770 //\r
1771 // calculate the the start address for the new entry. \r
1772 //\r
1773 *Script = S3TableBase + PositionOffset;\r
1774 \r
1775 } else {\r
1776 if (!BeforeOrAfter) {\r
1777 //\r
1778 // Insert the node to the end of the table\r
1779 //\r
1780 *Script = S3TableBase + TableLength; \r
1781 } else {\r
1782 // \r
1783 // Insert the node to the beginning of the table\r
1784 //\r
1785 PositionOffset = (UINTN) sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);\r
1786 CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset); \r
1787 *Script = S3TableBase + PositionOffset; \r
1788 }\r
1789 } \r
1790}\r
1791/**\r
1792 Move the last boot script entry to the position \r
1793\r
1794 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position\r
1795 in the boot script table specified by Position. If Position is NULL or points to\r
1796 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end\r
1797 of the table (if FALSE).\r
1798 @param Position On entry, specifies the position in the boot script table where the opcode will be\r
1799 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies\r
1800 the position of the inserted opcode in the boot script table.\r
1801\r
1802 @retval RETURN_OUT_OF_RESOURCES The table is not available.\r
1803 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.\r
1804 @retval RETURN_SUCCESS Opcode is inserted.\r
1805**/\r
1806RETURN_STATUS\r
1807EFIAPI\r
1808S3BootScriptMoveLastOpcode (\r
1809 IN BOOLEAN BeforeOrAfter,\r
1810 IN OUT VOID **Position OPTIONAL\r
1811)\r
1812{\r
1813 UINT8* Script;\r
1814 VOID *TempPosition; \r
1815 UINTN StartAddress;\r
1816 UINT32 TableLength;\r
1817 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;\r
1818 BOOLEAN ValidatePosition;\r
1819 UINT8* LastOpcode;\r
1820 UINT8 TempBootScriptEntry[BOOT_SCRIPT_NODE_MAX_LENGTH];\r
1821 \r
1822 ValidatePosition = FALSE;\r
1823 TempPosition = (Position == NULL) ? NULL:(*Position);\r
02f49fc2
SZ
1824\r
1825 //\r
1826 // Check that the script is initialized and synced without adding an entry to the script.\r
1827 //\r
1828 Script = S3BootScriptGetEntryAddAddress (0);\r
1829 if (Script == NULL) {\r
1830 return RETURN_OUT_OF_RESOURCES;\r
64d14edf 1831 }\r
02f49fc2
SZ
1832 Script = mS3BootScriptTablePtr->TableBase;\r
1833\r
64d14edf 1834 StartAddress = (UINTN) Script;\r
1835 TableLength = mS3BootScriptTablePtr->TableLength;\r
1836 Script = Script + sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);\r
1837 LastOpcode = Script;\r
1838 //\r
1839 // Find the last boot Script Entry which is not the terminate node\r
1840 //\r
1841 while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) { \r
1842 CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
1843 if (TempPosition != NULL && TempPosition == Script) {\r
1844 //\r
1845 // If the position is specified, the position must be pointed to a boot script entry start address. \r
1846 //\r
1847 ValidatePosition = TRUE;\r
1848 }\r
1849 if (ScriptHeader.OpCode != S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE) {\r
1850 LastOpcode = Script;\r
1851 } \r
1852 Script = Script + ScriptHeader.Length;\r
1853 }\r
1854 //\r
1855 // If the position is specified, but not the start of a boot script entry, it is a invalid input\r
1856 //\r
1857 if (TempPosition != NULL && !ValidatePosition) {\r
1858 return RETURN_INVALID_PARAMETER;\r
1859 }\r
1860 \r
1861 CopyMem ((VOID*)&ScriptHeader, LastOpcode, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
1862 \r
1863 CopyMem((VOID*)TempBootScriptEntry, LastOpcode, ScriptHeader.Length); \r
1864 //\r
1865 // Find the right position to write the node in\r
1866 //\r
1867 S3BootScriptCalculateInsertAddress (\r
1868 ScriptHeader.Length,\r
1869 TempPosition,\r
1870 BeforeOrAfter,\r
1871 &Script \r
1872 );\r
1873 //\r
1874 // Copy the node to Boot script table\r
1875 //\r
02f49fc2
SZ
1876 CopyMem((VOID*)Script, (VOID*)TempBootScriptEntry, ScriptHeader.Length);\r
1877\r
1878 SyncBootScript (Script);\r
1879\r
64d14edf 1880 //\r
1881 // return out the Position\r
1882 //\r
1883 if (Position != NULL) {\r
1884 *Position = Script;\r
1885 }\r
1886 return RETURN_SUCCESS;\r
1887}\r
1888/**\r
1889 Create a Label node in the boot script table. \r
1890 \r
1891 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position\r
1892 in the boot script table specified by Position. If Position is NULL or points to\r
1893 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end\r
1894 of the table (if FALSE).\r
1895 @param Position On entry, specifies the position in the boot script table where the opcode will be\r
1896 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies\r
1897 the position of the inserted opcode in the boot script table. \r
1898 @param InformationLength Length of the label in bytes\r
1899 @param Information Label to be logged in the boot scrpit\r
1900 \r
1901 @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.\r
1902 @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.\r
1903 @retval RETURN_SUCCESS Opcode is added.\r
1904\r
1905**/\r
1906RETURN_STATUS\r
1907EFIAPI\r
1908S3BootScriptLabelInternal (\r
1909 IN BOOLEAN BeforeOrAfter,\r
1910 IN OUT VOID **Position OPTIONAL, \r
1911 IN UINT32 InformationLength, \r
1912 IN CONST CHAR8 *Information\r
1913 )\r
1914{\r
1915 UINT8 Length;\r
1916 UINT8 *Script;\r
64d14edf 1917 EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;\r
1918 \r
1919 Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);\r
1920 \r
1921 Script = S3BootScriptGetEntryAddAddress (Length);\r
1922 if (Script == NULL) {\r
1923 return RETURN_OUT_OF_RESOURCES;\r
1924 }\r
64d14edf 1925 //\r
1926 // Build script data\r
1927 //\r
1928 ScriptInformation.OpCode = S3_BOOT_SCRIPT_LIB_LABEL_OPCODE;\r
1929 ScriptInformation.Length = Length;\r
1930\r
1931\r
1932 ScriptInformation.InformationLength = InformationLength; \r
1933\r
93b21ade
SZ
1934 CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
1935 CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);\r
1936\r
02f49fc2
SZ
1937 SyncBootScript (Script);\r
1938\r
64d14edf 1939 return S3BootScriptMoveLastOpcode (BeforeOrAfter, Position);\r
1940\r
1941}\r
1942/**\r
1943 Find a label within the boot script table and, if not present, optionally create it.\r
1944\r
1945 @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE)\r
1946 or after (FALSE) the position in the boot script table \r
1947 specified by Position.\r
1948 @param CreateIfNotFound Specifies whether the label will be created if the label \r
1949 does not exists (TRUE) or not (FALSE).\r
1950 @param Position On entry, specifies the position in the boot script table\r
1951 where the opcode will be inserted, either before or after,\r
1952 depending on BeforeOrAfter. On exit, specifies the position\r
1953 of the inserted opcode in the boot script table.\r
1954 @param Label Points to the label which will be inserted in the boot script table.\r
1955\r
1956 @retval EFI_SUCCESS The operation succeeded. A record was added into the\r
1957 specified script table.\r
1958 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.\r
1959 If the opcode is unknow or not supported because of the PCD \r
1960 Feature Flags.\r
1961 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.\r
1962\r
1963**/\r
1964RETURN_STATUS\r
1965EFIAPI \r
1966S3BootScriptLabel (\r
1967 IN BOOLEAN BeforeOrAfter,\r
1968 IN BOOLEAN CreateIfNotFound,\r
1969 IN OUT VOID **Position OPTIONAL,\r
1970 IN CONST CHAR8 *Label\r
1971 )\r
1972{\r
1973 UINT8* Script;\r
1974 UINTN StartAddress;\r
1975 UINT32 TableLength;\r
1976 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;\r
1977 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader;\r
1978 UINT32 LabelLength;\r
1979 //\r
96072947 1980 // Check NULL Label\r
64d14edf 1981 //\r
96072947
JY
1982 if (Label == NULL) {\r
1983 return EFI_INVALID_PARAMETER;\r
1984 }\r
1985 //\r
1986 // Check empty Label\r
1987 //\r
1988 if (Label[0] == '\0') {\r
64d14edf 1989 return EFI_INVALID_PARAMETER;\r
1990 }\r
1991 \r
1992 //\r
02f49fc2
SZ
1993 // Check that the script is initialized and synced without adding an entry to the script.\r
1994 // The code must search for the label first before it knows if a new entry needs\r
64d14edf 1995 // to be added.\r
1996 //\r
1997 Script = S3BootScriptGetEntryAddAddress (0);\r
1998 if (Script == NULL) {\r
1999 return RETURN_OUT_OF_RESOURCES;\r
2000 }\r
2001 \r
2002 //\r
2003 // Check the header and search for existing label.\r
2004 // \r
2005 Script = mS3BootScriptTablePtr->TableBase;\r
2006 CopyMem ((VOID*)&TableHeader, Script, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
2007 if (TableHeader.OpCode != S3_BOOT_SCRIPT_LIB_TABLE_OPCODE) {\r
2008 return EFI_INVALID_PARAMETER;\r
2009 }\r
2010 StartAddress = (UINTN) Script;\r
2011 TableLength = mS3BootScriptTablePtr->TableLength;\r
2012 Script = Script + TableHeader.Length;\r
2013 while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {\r
2014 \r
2015 CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
2016 if (ScriptHeader.OpCode == S3_BOOT_SCRIPT_LIB_LABEL_OPCODE) {\r
2017 if (AsciiStrCmp ((CHAR8 *)(UINTN)(Script+sizeof(EFI_BOOT_SCRIPT_INFORMATION)), Label) == 0) {\r
2018 (*Position) = Script; \r
2019 return EFI_SUCCESS;\r
2020 }\r
2021 } \r
2022 Script = Script + ScriptHeader.Length;\r
2023 }\r
2024 if (CreateIfNotFound) {\r
2025 LabelLength = (UINT32)AsciiStrSize(Label);\r
2026 return S3BootScriptLabelInternal (BeforeOrAfter,Position, LabelLength, Label); \r
2027 } else {\r
2028 return EFI_NOT_FOUND;\r
2029 } \r
2030}\r
2031\r
2032/**\r
2033 Compare two positions in the boot script table and return their relative position.\r
2034 @param Position1 The positions in the boot script table to compare\r
2035 @param Position2 The positions in the boot script table to compare\r
2036 @param RelativePosition On return, points to the result of the comparison\r
2037\r
2038 @retval EFI_SUCCESS The operation succeeded. A record was added into the\r
2039 specified script table.\r
2040 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.\r
2041 If the opcode is unknow or not supported because of the PCD \r
2042 Feature Flags.\r
2043 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.\r
2044\r
2045**/\r
2046RETURN_STATUS\r
2047EFIAPI \r
2048S3BootScriptCompare (\r
2049 IN UINT8 *Position1,\r
2050 IN UINT8 *Position2,\r
2051 OUT UINTN *RelativePosition\r
2052 )\r
2053{\r
2054 UINT8* Script;\r
2055 UINT32 TableLength; \r
2056\r
64d14edf 2057 if (RelativePosition == NULL) {\r
2058 return EFI_INVALID_PARAMETER;\r
2059 }\r
02f49fc2
SZ
2060\r
2061 //\r
2062 // Check that the script is initialized and synced without adding an entry to the script.\r
2063 //\r
2064 Script = S3BootScriptGetEntryAddAddress (0);\r
2065 if (Script == NULL) {\r
2066 return RETURN_OUT_OF_RESOURCES;\r
2067 }\r
2068 Script = mS3BootScriptTablePtr->TableBase;\r
2069\r
64d14edf 2070 //\r
879dbe18 2071 // mS3BootScriptTablePtr->TableLength does not include the termination node, so add it up\r
64d14edf 2072 //\r
879dbe18 2073 TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
64d14edf 2074 if (Position1 < Script || Position1 > Script+TableLength) {\r
2075 return EFI_INVALID_PARAMETER;\r
2076 }\r
2077 if (Position2 < Script || Position2 > Script+TableLength) {\r
2078 return EFI_INVALID_PARAMETER;\r
2079 }\r
2080 *RelativePosition = (Position1 < Position2)?-1:((Position1 == Position2)?0:1);\r
2081 \r
2082 return EFI_SUCCESS;\r
2083}\r
2084\r