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