]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptExecute.c
Add the memory check to reserve memory for TERMINATE opcode in S3BootScriptGetBootTim...
[mirror_edk2.git] / MdeModulePkg / Library / PiDxeS3BootScriptLib / BootScriptExecute.c
CommitLineData
64d14edf 1/** @file\r
2 Interpret and execute the S3 data in S3 boot script. \r
3\r
f6ec0c77 4 Copyright (c) 2006 - 2011, 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 Checks the parameter of SmbusExecute().\r
20\r
21 This function checks the input parameters of SmbusExecute(). If the input parameters are valid\r
22 for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain\r
23 error code based on the input SMBus bus protocol.\r
24\r
25 @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length, \r
26 and PEC.\r
27 @param Operation Signifies which particular SMBus hardware protocol instance that\r
28 it will use to execute the SMBus transactions. This SMBus\r
29 hardware protocol is defined by the SMBus Specification and is\r
30 not related to EFI.\r
31 @param Length Signifies the number of bytes that this operation will do. The\r
32 maximum number of bytes can be revision specific and operation\r
33 specific. This field will contain the actual number of bytes that\r
34 are executed for this operation. Not all operations require this\r
35 argument.\r
36 @param Buffer Contains the value of data to execute to the SMBus slave device.\r
37 Not all operations require this argument. The length of this\r
38 buffer is identified by Length.\r
39\r
40 @retval EFI_SUCCESS All the parameters are valid for the corresponding SMBus bus\r
41 protocol. \r
42 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.\r
43 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead\r
44 and EfiSmbusQuickWrite. Length is outside the range of valid\r
45 values.\r
46 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.\r
47 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.\r
48\r
49**/\r
50EFI_STATUS\r
51CheckParameters (\r
52 IN UINTN SmBusAddress,\r
53 IN EFI_SMBUS_OPERATION Operation,\r
54 IN OUT UINTN *Length,\r
55 IN OUT VOID *Buffer\r
56 )\r
57{\r
58 EFI_STATUS Status;\r
59 UINTN RequiredLen;\r
60 EFI_SMBUS_DEVICE_COMMAND Command;\r
61 BOOLEAN PecCheck;\r
62 \r
63 Command = SMBUS_LIB_COMMAND (SmBusAddress);\r
64 PecCheck = SMBUS_LIB_PEC (SmBusAddress);\r
65 //\r
66 // Set default value to be 2:\r
67 // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall. \r
68 //\r
69 RequiredLen = 2;\r
70 Status = EFI_SUCCESS;\r
71 switch (Operation) {\r
72 case EfiSmbusQuickRead:\r
73 case EfiSmbusQuickWrite:\r
74 if (PecCheck || Command != 0) {\r
75 return EFI_UNSUPPORTED;\r
76 }\r
77 break;\r
78 case EfiSmbusReceiveByte:\r
79 case EfiSmbusSendByte:\r
80 if (Command != 0) {\r
81 return EFI_UNSUPPORTED;\r
82 }\r
83 //\r
84 // Cascade to check length parameter.\r
85 //\r
86 case EfiSmbusReadByte:\r
87 case EfiSmbusWriteByte:\r
88 RequiredLen = 1;\r
89 //\r
90 // Cascade to check length parameter.\r
91 //\r
92 case EfiSmbusReadWord:\r
93 case EfiSmbusWriteWord:\r
94 case EfiSmbusProcessCall:\r
95 if (Buffer == NULL || Length == NULL) {\r
96 return EFI_INVALID_PARAMETER;\r
97 } else if (*Length < RequiredLen) {\r
98 Status = EFI_BUFFER_TOO_SMALL;\r
99 }\r
100 *Length = RequiredLen;\r
101 break;\r
102 case EfiSmbusReadBlock:\r
103 case EfiSmbusWriteBlock:\r
104 if ((Buffer == NULL) || \r
105 (Length == NULL) || \r
106 (*Length < MIN_SMBUS_BLOCK_LEN) ||\r
107 (*Length > MAX_SMBUS_BLOCK_LEN)) {\r
108 return EFI_INVALID_PARAMETER;\r
109 } \r
110 break;\r
111 case EfiSmbusBWBRProcessCall:\r
112 return EFI_UNSUPPORTED;\r
113 default:\r
114 return EFI_INVALID_PARAMETER;\r
115 }\r
116 return Status;\r
117}\r
118\r
119/**\r
120 Executes an SMBus operation to an SMBus controller. Returns when either the command has been\r
121 executed or an error is encountered in doing the operation.\r
122\r
123 The SmbusExecute() function provides a standard way to execute an operation as defined in the System\r
124 Management Bus (SMBus) Specification. The resulting transaction will be either that the SMBus\r
125 slave devices accept this transaction or that this function returns with error.\r
126\r
127 @param SmbusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length, \r
128 and PEC.\r
129 @param Operation Signifies which particular SMBus hardware protocol instance that\r
130 it will use to execute the SMBus transactions. This SMBus\r
131 hardware protocol is defined by the SMBus Specification and is\r
132 not related to EFI.\r
133 @param Length Signifies the number of bytes that this operation will do. The\r
134 maximum number of bytes can be revision specific and operation\r
135 specific. This field will contain the actual number of bytes that\r
136 are executed for this operation. Not all operations require this\r
137 argument.\r
138 @param Buffer Contains the value of data to execute to the SMBus slave device.\r
139 Not all operations require this argument. The length of this\r
140 buffer is identified by Length.\r
141\r
142 @retval EFI_SUCCESS The last data that was returned from the access matched the poll\r
143 exit criteria.\r
144 @retval EFI_CRC_ERROR Checksum is not correct (PEC is incorrect).\r
145 @retval EFI_TIMEOUT Timeout expired before the operation was completed. Timeout is\r
146 determined by the SMBus host controller device.\r
147 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
148 @retval EFI_DEVICE_ERROR The request was not completed because a failure that was\r
149 reflected in the Host Status Register bit. Device errors are a\r
150 result of a transaction collision, illegal command field,\r
151 unclaimed cycle (host initiated), or bus errors (collisions).\r
152 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.\r
153 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead\r
154 and EfiSmbusQuickWrite. Length is outside the range of valid\r
155 values.\r
156 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.\r
157 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.\r
158\r
159**/\r
160EFI_STATUS\r
161SmbusExecute (\r
162 IN UINTN SmbusAddress,\r
163 IN EFI_SMBUS_OPERATION Operation,\r
164 IN OUT UINTN *Length,\r
165 IN OUT VOID *Buffer\r
166 )\r
167{\r
168 EFI_STATUS Status;\r
169 UINTN WorkBufferLen;\r
170 UINT8 WorkBuffer[MAX_SMBUS_BLOCK_LEN];\r
171\r
172 Status = CheckParameters (SmbusAddress, Operation, Length, Buffer);\r
173 if (EFI_ERROR (Status)) {\r
174 return Status;\r
175 }\r
176\r
177 switch (Operation) {\r
178 case EfiSmbusQuickRead:\r
179 DEBUG ((EFI_D_INFO, "EfiSmbusQuickRead - 0x%08x\n", SmbusAddress));\r
180 SmBusQuickRead (SmbusAddress, &Status);\r
181 break;\r
182 case EfiSmbusQuickWrite:\r
183 DEBUG ((EFI_D_INFO, "EfiSmbusQuickWrite - 0x%08x\n", SmbusAddress));\r
184 SmBusQuickWrite (SmbusAddress, &Status);\r
185 break;\r
186 case EfiSmbusReceiveByte:\r
187 DEBUG ((EFI_D_INFO, "EfiSmbusReceiveByte - 0x%08x\n", SmbusAddress));\r
188 *(UINT8 *) Buffer = SmBusReceiveByte (SmbusAddress, &Status);\r
189 break;\r
190 case EfiSmbusSendByte:\r
191 DEBUG ((EFI_D_INFO, "EfiSmbusReceiveByte - 0x%08x (0x%02x)\n", SmbusAddress, (UINTN)*(UINT8 *) Buffer));\r
192 SmBusSendByte (SmbusAddress, *(UINT8 *) Buffer, &Status);\r
193 break;\r
194 case EfiSmbusReadByte:\r
195 DEBUG ((EFI_D_INFO, "EfiSmbusReadByte - 0x%08x\n", SmbusAddress));\r
196 *(UINT8 *) Buffer = SmBusReadDataByte (SmbusAddress, &Status);\r
197 break;\r
198 case EfiSmbusWriteByte:\r
199 DEBUG ((EFI_D_INFO, "EfiSmbusWriteByte - 0x%08x (0x%02x)\n", SmbusAddress, (UINTN)*(UINT8 *) Buffer));\r
200 SmBusWriteDataByte (SmbusAddress, *(UINT8 *) Buffer, &Status);\r
201 break;\r
202 case EfiSmbusReadWord:\r
203 DEBUG ((EFI_D_INFO, "EfiSmbusReadWord - 0x%08x\n", SmbusAddress));\r
204 *(UINT16 *) Buffer = SmBusReadDataWord (SmbusAddress, &Status);\r
205 break;\r
206 case EfiSmbusWriteWord:\r
207 DEBUG ((EFI_D_INFO, "EfiSmbusWriteByte - 0x%08x (0x%04x)\n", SmbusAddress, (UINTN)*(UINT16 *) Buffer));\r
208 SmBusWriteDataWord (SmbusAddress, *(UINT16 *) Buffer, &Status);\r
209 break;\r
210 case EfiSmbusProcessCall:\r
211 DEBUG ((EFI_D_INFO, "EfiSmbusProcessCall - 0x%08x (0x%04x)\n", SmbusAddress, (UINTN)*(UINT16 *) Buffer));\r
212 *(UINT16 *) Buffer = SmBusProcessCall (SmbusAddress, *(UINT16 *) Buffer, &Status);\r
213 break;\r
214 case EfiSmbusReadBlock:\r
215 DEBUG ((EFI_D_INFO, "EfiSmbusReadBlock - 0x%08x\n", SmbusAddress));\r
216 WorkBufferLen = SmBusReadBlock (SmbusAddress, WorkBuffer, &Status);\r
217 if (!EFI_ERROR (Status)) {\r
218 //\r
219 // Read block transaction is complete successfully, and then\r
220 // check whether the output buffer is large enough. \r
221 //\r
222 if (*Length >= WorkBufferLen) {\r
223 CopyMem (Buffer, WorkBuffer, WorkBufferLen);\r
224 } else {\r
225 Status = EFI_BUFFER_TOO_SMALL;\r
226 }\r
227 *Length = WorkBufferLen;\r
228 }\r
229 break;\r
230 case EfiSmbusWriteBlock:\r
231 DEBUG ((EFI_D_INFO, "EfiSmbusWriteBlock - 0x%08x (0x%04x)\n", SmbusAddress, (UINTN)*(UINT16 *) Buffer));\r
232 SmBusWriteBlock ((SmbusAddress + SMBUS_LIB_ADDRESS (0, 0, (*Length), FALSE)) , Buffer, &Status);\r
233 break;\r
234 case EfiSmbusBWBRProcessCall:\r
235 //\r
236 // BUGBUG: Should this case be handled?\r
237 //\r
238 break;\r
239 }\r
240\r
241 return Status; \r
242}\r
243\r
244/**\r
245 Translates boot script width and address stride to MDE library interface.\r
246\r
247\r
248 @param Width Width of the operation.\r
249 @param Address Address of the operation.\r
250 @param AddressStride Instride for stepping input buffer.\r
251 @param BufferStride Outstride for stepping output buffer. \r
252\r
253 @retval EFI_SUCCESS Successful translation.\r
254 @retval EFI_INVALID_PARAMETER Width or Address is invalid.\r
255**/\r
256EFI_STATUS\r
257BuildLoopData (\r
258 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
259 IN UINT64 Address,\r
260 OUT UINTN *AddressStride,\r
261 OUT UINTN *BufferStride\r
262 )\r
263{\r
264 UINTN AlignMask;\r
265\r
266 if (Width >= S3BootScriptWidthMaximum) {\r
267 return EFI_INVALID_PARAMETER;\r
268 }\r
269\r
270 *AddressStride = (UINT32)(1 << (Width & 0x03));\r
271 *BufferStride = *AddressStride;\r
272\r
273 AlignMask = *AddressStride - 1;\r
274 if ((Address & AlignMask) != 0) {\r
275 return EFI_INVALID_PARAMETER;\r
276 }\r
277\r
278 if (Width >= S3BootScriptWidthFifoUint8 && Width <= S3BootScriptWidthFifoUint64) {\r
279 *AddressStride = 0;\r
280 }\r
281\r
282 if (Width >= S3BootScriptWidthFillUint8 && Width <= S3BootScriptWidthFillUint64) {\r
283 *BufferStride = 0;\r
284 }\r
285\r
286 return EFI_SUCCESS;\r
287}\r
288\r
289/**\r
290 Translates boot script to MDE library interface.\r
291 \r
292 @param[in] Width Width of the operation.\r
293 @param[in] Address Address of the operation.\r
294 @param[in] Count Count of the number of accesses to perform.\r
295 @param[out] Buffer Pointer to the buffer to read from I/O space. \r
296\r
297 @retval EFI_SUCCESS The data was written to the EFI System.\r
298 @retval EFI_INVALID_PARAMETER Width is invalid for this EFI System.\r
299 Buffer is NULL.\r
300 The Buffer is not aligned for the given Width.\r
301 Address is outside the legal range of I/O ports. \r
302 \r
303**/\r
304EFI_STATUS\r
305ScriptIoRead (\r
306 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
307 IN UINT64 Address,\r
308 IN UINTN Count,\r
309 OUT VOID *Buffer\r
310 )\r
311{\r
312 EFI_STATUS Status;\r
313 UINTN AddressStride;\r
314 UINTN BufferStride;\r
315 PTR Out;\r
316\r
317 Out.Buf = (UINT8 *) Buffer;\r
318\r
319 if (Address > MAX_IO_ADDRESS) {\r
320 return EFI_INVALID_PARAMETER;\r
321 }\r
322\r
323 Status = BuildLoopData (Width, Address, &AddressStride, &BufferStride);\r
324 if (EFI_ERROR (Status)) {\r
325 return Status;\r
326 }\r
327 //\r
328 // Loop for each iteration and move the data\r
329 //\r
330 for (; Count > 0; Count--, Address += AddressStride, Out.Buf += BufferStride) {\r
331 switch (Width) {\r
332\r
333 case S3BootScriptWidthUint8:\r
334 case S3BootScriptWidthFifoUint8:\r
335 case S3BootScriptWidthFillUint8:\r
336 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint8Read - 0x%08x\n", Address));\r
337 *Out.Uint8 = IoRead8 ((UINTN) Address);\r
338 break;\r
339\r
340 case S3BootScriptWidthUint16:\r
341 case S3BootScriptWidthFifoUint16:\r
342 case S3BootScriptWidthFillUint16:\r
343 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint16Read - 0x%08x\n", Address));\r
344 *Out.Uint16 = IoRead16 ((UINTN) Address);\r
345 break;\r
346\r
347 case S3BootScriptWidthUint32:\r
348 case S3BootScriptWidthFifoUint32:\r
349 case S3BootScriptWidthFillUint32:\r
350 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint32Read - 0x%08x\n", Address));\r
351 *Out.Uint32 = IoRead32 ((UINTN) Address);\r
352 break;\r
353\r
354 default:\r
355 return EFI_INVALID_PARAMETER;\r
356 }\r
357 }\r
358\r
359 return EFI_SUCCESS;\r
360}\r
361\r
362/**\r
363 Perform a write operation\r
364 \r
365 @param[in] Width Width of the operation.\r
366 @param[in] Address Address of the operation.\r
367 @param[in] Count Count of the number of accesses to perform.\r
368 @param[in] Buffer Pointer to the buffer to write to I/O space. \r
369\r
370 @retval EFI_SUCCESS The data was written to the EFI System.\r
371 @retval EFI_INVALID_PARAMETER Width is invalid for this EFI System.\r
372 Buffer is NULL.\r
373 The Buffer is not aligned for the given Width.\r
374 Address is outside the legal range of I/O ports. \r
375 \r
376**/\r
377EFI_STATUS\r
378ScriptIoWrite (\r
379 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
380 IN UINT64 Address,\r
381 IN UINTN Count,\r
382 IN VOID *Buffer\r
383 )\r
384{\r
385 EFI_STATUS Status;\r
386 UINTN AddressStride;\r
387 UINTN BufferStride;\r
388 UINT64 OriginalAddress;\r
389 PTR In;\r
390 PTR OriginalIn;\r
391\r
392 In.Buf = (UINT8 *) Buffer;\r
393\r
394 if (Address > MAX_IO_ADDRESS) {\r
395 return EFI_INVALID_PARAMETER;\r
396 }\r
397\r
398 Status = BuildLoopData (Width, Address, &AddressStride, &BufferStride);\r
399 if (EFI_ERROR (Status)) {\r
400 return Status;\r
401 }\r
402 //\r
403 // Loop for each iteration and move the data\r
404 //\r
405 OriginalAddress = Address;\r
406 OriginalIn.Buf = In.Buf;\r
407 for (; Count > 0; Count--, Address += AddressStride, In.Buf += BufferStride) {\r
408 switch (Width) {\r
409 case S3BootScriptWidthUint8:\r
410 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint8 - 0x%08x (0x%02x)\n", (UINTN)Address, (UINTN)*In.Uint8));\r
411 IoWrite8 ((UINTN) Address, *In.Uint8);\r
412 break; \r
413 case S3BootScriptWidthFifoUint8:\r
414 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFifoUint8 - 0x%08x (0x%02x)\n", (UINTN)OriginalAddress, (UINTN)*In.Uint8));\r
415 IoWrite8 ((UINTN) OriginalAddress, *In.Uint8);\r
416 break; \r
417 case S3BootScriptWidthFillUint8:\r
418 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFillUint8 - 0x%08x (0x%02x)\n", (UINTN)Address, (UINTN)*In.Uint8));\r
419 IoWrite8 ((UINTN) Address, *OriginalIn.Uint8);\r
420 break;\r
421 case S3BootScriptWidthUint16:\r
422 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint16 - 0x%08x (0x%04x)\n", (UINTN)Address, (UINTN)*In.Uint16));\r
423 IoWrite16 ((UINTN) Address, *In.Uint16);\r
424 break; \r
425 case S3BootScriptWidthFifoUint16:\r
426 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFifoUint16 - 0x%08x (0x%04x)\n", (UINTN)OriginalAddress, (UINTN)*In.Uint16));\r
427 IoWrite16 ((UINTN) OriginalAddress, *In.Uint16);\r
428 break; \r
429 case S3BootScriptWidthFillUint16:\r
430 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFillUint16 - 0x%08x (0x%04x)\n", (UINTN)Address, (UINTN)*In.Uint16));\r
431 IoWrite16 ((UINTN) Address, *OriginalIn.Uint16);\r
432 break;\r
433 case S3BootScriptWidthUint32:\r
434 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint32 - 0x%08x (0x%08x)\n", (UINTN)Address, (UINTN)*In.Uint32));\r
435 IoWrite32 ((UINTN) Address, *In.Uint32);\r
436 break; \r
437 case S3BootScriptWidthFifoUint32:\r
438 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFifoUint32 - 0x%08x (0x%08x)\n", (UINTN)OriginalAddress, (UINTN)*In.Uint32));\r
439 IoWrite32 ((UINTN) OriginalAddress, *In.Uint32);\r
440 break;\r
441 case S3BootScriptWidthFillUint32:\r
442 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFillUint32 - 0x%08x (0x%08x)\n", (UINTN)Address, (UINTN)*In.Uint32));\r
443 IoWrite32 ((UINTN) Address, *OriginalIn.Uint32);\r
444 break;\r
445 case S3BootScriptWidthUint64:\r
446 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint64 - 0x%08x (0x%016lx)\n", (UINTN)Address, *In.Uint64));\r
447 IoWrite64 ((UINTN) Address, *In.Uint64);\r
448 break; \r
449 case S3BootScriptWidthFifoUint64:\r
450 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFifoUint64 - 0x%08x (0x%016lx)\n", (UINTN)Address, *In.Uint64));\r
451 IoWrite64 ((UINTN) OriginalAddress, *In.Uint64);\r
452 break; \r
453 case S3BootScriptWidthFillUint64:\r
454 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFillUint64 - 0x%08x (0x%016lx)\n", (UINTN)Address, *In.Uint64));\r
455 IoWrite64 ((UINTN) Address, *OriginalIn.Uint64);\r
456 break;\r
457 default:\r
458 return EFI_INVALID_PARAMETER;\r
459 }\r
460 }\r
461 \r
462\r
463 return EFI_SUCCESS;\r
464}\r
465/**\r
466 Interprete the IO write entry in S3 boot script and perform the write operation\r
467 \r
468 @param Script Pointer to the node which is to be interpreted.\r
469\r
470 @retval EFI_SUCCESS The data was written to the EFI System.\r
471 @retval EFI_INVALID_PARAMETER Width is invalid for this EFI System.\r
472 Buffer is NULL.\r
473 The Buffer is not aligned for the given Width.\r
474 Address is outside the legal range of I/O ports. \r
475 \r
476**/\r
477EFI_STATUS\r
478BootScriptExecuteIoWrite (\r
479 IN UINT8 *Script \r
480 )\r
481{\r
482 S3_BOOT_SCRIPT_LIB_WIDTH Width;\r
483 UINT64 Address;\r
484 UINTN Count;\r
485 VOID *Buffer;\r
486 EFI_BOOT_SCRIPT_IO_WRITE IoWrite;\r
487 \r
488 CopyMem ((VOID*)&IoWrite, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_IO_WRITE));\r
489 Width = (S3_BOOT_SCRIPT_LIB_WIDTH) IoWrite.Width;\r
490 Address = IoWrite.Address;\r
491 Count = IoWrite.Count;\r
492 Buffer = Script + sizeof (EFI_BOOT_SCRIPT_IO_WRITE);\r
493 \r
494 return ScriptIoWrite(Width, Address, Count, Buffer);\r
495}\r
496/**\r
497 Perform memory read operation\r
498 \r
499 @param Width Width of the operation.\r
500 @param Address Address of the operation.\r
501 @param Count Count of the number of accesses to perform.\r
502 @param Buffer Pointer to the buffer read from memory. \r
503\r
504 @retval EFI_SUCCESS The data was written to the EFI System.\r
505 @retval EFI_INVALID_PARAMETER Width is invalid for this EFI System.\r
506 Buffer is NULL.\r
507 The Buffer is not aligned for the given Width.\r
508 @retval EFI_UNSUPPORTED The address range specified by Address, Width, and Count \r
509 is not valid for this EFI System. \r
510 \r
511**/\r
512EFI_STATUS\r
513ScriptMemoryRead (\r
514 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
515 IN UINT64 Address,\r
516 IN UINTN Count,\r
517 IN OUT VOID *Buffer\r
518 )\r
519{\r
520 EFI_STATUS Status;\r
521 UINTN AddressStride;\r
522 UINTN BufferStride;\r
523 PTR Out;\r
524\r
525 Out.Buf = Buffer;\r
526\r
527 Status = BuildLoopData (Width, Address, &AddressStride, &BufferStride);\r
528 if (EFI_ERROR (Status)) {\r
529 return Status;\r
530 }\r
531 //\r
532 // Loop for each iteration and move the data\r
533 //\r
534 for (; Count > 0; Count--, Address += AddressStride, Out.Buf += BufferStride) {\r
535 switch (Width) {\r
536 case S3BootScriptWidthUint8:\r
537 case S3BootScriptWidthFifoUint8:\r
538 case S3BootScriptWidthFillUint8:\r
539 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint8 - 0x%08x\n", (UINTN)Address));\r
540 *Out.Uint8 = MmioRead8 ((UINTN) Address);\r
541 break;\r
542\r
543 case S3BootScriptWidthUint16:\r
544 case S3BootScriptWidthFifoUint16:\r
545 case S3BootScriptWidthFillUint16:\r
546 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint16 - 0x%08x\n", (UINTN)Address));\r
547 *Out.Uint16 = MmioRead16 ((UINTN) Address);\r
548 break;\r
549\r
550 case S3BootScriptWidthUint32:\r
551 case S3BootScriptWidthFifoUint32:\r
552 case S3BootScriptWidthFillUint32:\r
553 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint32 - 0x%08x\n", (UINTN)Address));\r
554 *Out.Uint32 = MmioRead32 ((UINTN) Address);\r
555 break;\r
556\r
557 case S3BootScriptWidthUint64:\r
558 case S3BootScriptWidthFifoUint64:\r
559 case S3BootScriptWidthFillUint64:\r
560 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint64 - 0x%08x\n", (UINTN)Address));\r
561 *Out.Uint64 = MmioRead64 ((UINTN) Address);\r
562 break;\r
563\r
564 default:\r
565 return EFI_UNSUPPORTED;\r
566 }\r
567 }\r
568\r
569 return EFI_SUCCESS;\r
570}\r
571/**\r
572 Translates boot script to MDE library interface.\r
573 \r
574 @param Width Width of the operation.\r
575 @param Address Address of the operation.\r
576 @param Count Count of the number of accesses to perform.\r
577 @param Buffer Pointer to the buffer write to memory. \r
578\r
579 @retval EFI_SUCCESS The data was written to the EFI System.\r
580 @retval EFI_INVALID_PARAMETER Width is invalid for this EFI System.\r
581 Buffer is NULL.\r
582 The Buffer is not aligned for the given Width.\r
583 @retval EFI_UNSUPPORTED The address range specified by Address, Width, and Count \r
584 is not valid for this EFI System. \r
585 \r
586**/\r
587EFI_STATUS\r
588ScriptMemoryWrite (\r
589 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
590 IN UINT64 Address,\r
591 IN UINTN Count,\r
592 IN OUT VOID *Buffer\r
593 )\r
594{\r
595 EFI_STATUS Status;\r
596 UINTN AddressStride;\r
597 UINT64 OriginalAddress; \r
598 UINTN BufferStride;\r
599 PTR In;\r
600 PTR OriginalIn;\r
601\r
602 In.Buf = Buffer;\r
603\r
604 Status = BuildLoopData (Width, Address, &AddressStride, &BufferStride);\r
605 if (EFI_ERROR (Status)) {\r
606 return Status;\r
607 }\r
608 //\r
609 // Loop for each iteration and move the data\r
610 //\r
611 OriginalAddress = Address;\r
612 OriginalIn.Buf = In.Buf; \r
613 for (; Count > 0; Count--, Address += AddressStride, In.Buf += BufferStride) {\r
614 switch (Width) {\r
615 case S3BootScriptWidthUint8:\r
616 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint8 - 0x%08x (0x%02x)\n", (UINTN)Address, (UINTN)*In.Uint8));\r
617 MmioWrite8 ((UINTN) Address, *In.Uint8);\r
618 break; \r
619 case S3BootScriptWidthFifoUint8:\r
620 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFifoUint8 - 0x%08x (0x%02x)\n", (UINTN)OriginalAddress, (UINTN)*In.Uint8));\r
621 MmioWrite8 ((UINTN) OriginalAddress, *In.Uint8);\r
622 break; \r
623 case S3BootScriptWidthFillUint8:\r
624 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFillUint8 - 0x%08x (0x%02x)\n", (UINTN)Address, (UINTN)*In.Uint8));\r
625 MmioWrite8 ((UINTN) Address, *OriginalIn.Uint8);\r
626 break;\r
627 case S3BootScriptWidthUint16:\r
628 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint16 - 0x%08x (0x%04x)\n", (UINTN)Address, (UINTN)*In.Uint16));\r
629 MmioWrite16 ((UINTN) Address, *In.Uint16);\r
630 break; \r
631 case S3BootScriptWidthFifoUint16:\r
632 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFifoUint16 - 0x%08x (0x%04x)\n", (UINTN)OriginalAddress, (UINTN)*In.Uint16));\r
633 MmioWrite16 ((UINTN) OriginalAddress, *In.Uint16);\r
634 break; \r
635 case S3BootScriptWidthFillUint16:\r
636 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFillUint16 - 0x%08x (0x%04x)\n", (UINTN)Address, (UINTN)*In.Uint16));\r
637 MmioWrite16 ((UINTN) Address, *OriginalIn.Uint16);\r
638 break;\r
639 case S3BootScriptWidthUint32:\r
640 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint32 - 0x%08x (0x%08x)\n", (UINTN)Address, (UINTN)*In.Uint32));\r
641 MmioWrite32 ((UINTN) Address, *In.Uint32);\r
642 break; \r
643 case S3BootScriptWidthFifoUint32:\r
644 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFifoUint32 - 0x%08x (0x%08x)\n", (UINTN)OriginalAddress, (UINTN)*In.Uint32));\r
645 MmioWrite32 ((UINTN) OriginalAddress, *In.Uint32);\r
646 break; \r
647 case S3BootScriptWidthFillUint32:\r
648 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFillUint32 - 0x%08x (0x%08x)\n", (UINTN)Address, (UINTN)*In.Uint32));\r
649 MmioWrite32 ((UINTN) Address, *OriginalIn.Uint32);\r
650 break;\r
651 case S3BootScriptWidthUint64:\r
652 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint64 - 0x%08x (0x%016lx)\n", (UINTN)Address, *In.Uint64));\r
653 MmioWrite64 ((UINTN) Address, *In.Uint64);\r
654 break; \r
655 case S3BootScriptWidthFifoUint64:\r
656 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFifoUint64 - 0x%08x (0x%016lx)\n", (UINTN)OriginalAddress, *In.Uint64));\r
657 MmioWrite64 ((UINTN) OriginalAddress, *In.Uint64);\r
658 break; \r
659 case S3BootScriptWidthFillUint64:\r
660 DEBUG ((EFI_D_INFO, "S3BootScriptWidthFillUint64 - 0x%08x (0x%016lx)\n", (UINTN)Address, *In.Uint64));\r
661 MmioWrite64 ((UINTN) Address, *OriginalIn.Uint64);\r
662 break;\r
663 default:\r
664 return EFI_UNSUPPORTED;\r
665 }\r
666 }\r
667 return EFI_SUCCESS;\r
668}\r
669/**\r
670 Interprete the boot script node with EFI_BOOT_SCRIPT_MEM_WRITE OP code.\r
671\r
672 @param[in] Script Pointer to the node which is to be interpreted.\r
673 \r
674 @retval EFI_SUCCESS The data was written to the EFI System.\r
675 @retval EFI_INVALID_PARAMETER Width is invalid for this EFI System.\r
676 Buffer is NULL.\r
677 The Buffer is not aligned for the given Width.\r
678 @retval EFI_UNSUPPORTED The address range specified by Address, Width, and Count \r
679 is not valid for this EFI System. \r
680 \r
681**/\r
682EFI_STATUS\r
683BootScriptExecuteMemoryWrite (\r
684 IN UINT8 *Script\r
685 )\r
686{\r
687 VOID *Buffer;\r
688 S3_BOOT_SCRIPT_LIB_WIDTH Width;\r
689 UINT64 Address;\r
690 UINTN Count;\r
691 EFI_BOOT_SCRIPT_MEM_WRITE MemWrite;\r
692 \r
693 CopyMem((VOID*)&MemWrite, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_MEM_WRITE));\r
694 Width = (S3_BOOT_SCRIPT_LIB_WIDTH)MemWrite.Width;\r
695 Address = MemWrite.Address;\r
696 Count = MemWrite.Count;\r
697 Buffer = Script + sizeof(EFI_BOOT_SCRIPT_MEM_WRITE);\r
698\r
699 DEBUG ((EFI_D_INFO, "BootScriptExecuteMemoryWrite - 0x%08x, 0x%08x, 0x%08x\n", (UINTN)Address, (UINTN)Count, (UINTN)Width));\r
700 return ScriptMemoryWrite (Width,Address, Count, Buffer);\r
701 \r
702} \r
703/**\r
704 Translates boot script to MDE library interface for PCI configuration read operation\r
705\r
706 @param Width Width of the operation.\r
707 @param Address Address of the operation.\r
708 @param Buffer Pointer to the buffer reaf from PCI config space\r
709 \r
710 @retval EFI_SUCCESS The read succeed.\r
711 @retval EFI_INVALID_PARAMETER if Width is not defined \r
712 \r
713**/\r
714EFI_STATUS\r
715ScriptPciCfgRead (\r
716 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
717 IN UINT64 Address,\r
718 OUT VOID *Buffer\r
719 )\r
720{\r
721 switch (Width) {\r
722 case S3BootScriptWidthUint8:\r
723 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint8 - 0x%016lx\n", Address));\r
724 * (UINT8 *) Buffer = PciRead8 (PCI_ADDRESS_ENCODE(Address));\r
725 break;\r
726\r
727 case S3BootScriptWidthUint16:\r
728 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint16 - 0x%016lx\n", Address));\r
729 * (UINT16 *) Buffer = PciRead16 (PCI_ADDRESS_ENCODE(Address));\r
730 break;\r
731\r
732 case S3BootScriptWidthUint32:\r
733 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint32 - 0x%016lx\n", Address));\r
734 * (UINT32 *) Buffer = PciRead32 (PCI_ADDRESS_ENCODE(Address));\r
735 break;\r
736\r
737 default:\r
738 return EFI_INVALID_PARAMETER;\r
739 }\r
740 return EFI_SUCCESS;\r
741}\r
742\r
743/**\r
744 Translates boot script to MDE library interface for PCI configuration write operation\r
745\r
746 @param Width Width of the operation.\r
747 @param Address Address of the operation.\r
748 @param Buffer Pointer to the buffer reaf from PCI config space\r
749 \r
750 @retval EFI_SUCCESS The write succeed.\r
751 @retval EFI_INVALID_PARAMETER if Width is not defined \r
752 \r
753**/\r
754EFI_STATUS\r
755ScriptPciCfgWrite (\r
756 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
757 IN UINT64 Address,\r
758 OUT VOID *Buffer\r
759 )\r
760{\r
761 switch (Width) {\r
762 case S3BootScriptWidthUint8:\r
763 case S3BootScriptWidthFifoUint8:\r
764 case S3BootScriptWidthFillUint8: \r
765 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint8 - 0x%016lx (0x%02x)\n", Address, (UINTN)*(UINT8 *) Buffer));\r
766 PciWrite8 (PCI_ADDRESS_ENCODE(Address), *(UINT8 *) Buffer);\r
767 break;\r
768 case S3BootScriptWidthUint16:\r
769 case S3BootScriptWidthFifoUint16:\r
770 case S3BootScriptWidthFillUint16: \r
771 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint16 - 0x%016lx (0x%04x)\n", Address, (UINTN)*(UINT16 *) Buffer));\r
772 PciWrite16 (PCI_ADDRESS_ENCODE(Address), *(UINT16 *) Buffer);\r
773 break;\r
774 case S3BootScriptWidthUint32:\r
775 case S3BootScriptWidthFifoUint32:\r
776 case S3BootScriptWidthFillUint32: \r
777 DEBUG ((EFI_D_INFO, "S3BootScriptWidthUint32 - 0x%016lx (0x%08x)\n", Address, (UINTN)*(UINT32 *) Buffer));\r
778 PciWrite32 (PCI_ADDRESS_ENCODE(Address), *(UINT32 *) Buffer);\r
779 break;\r
780 default:\r
781 return EFI_INVALID_PARAMETER;\r
782 }\r
783 return EFI_SUCCESS;\r
784}\r
785/**\r
786 Perform pci configure 2 read operation.\r
787 \r
788 @param Width Width of the operation.\r
789 @param Segment Pci segment number\r
790 @param Address Address of the operation.\r
791 @param Buffer Pointer to the buffer to write to I/O space. \r
792\r
793 @retval EFI_SUCCESS The data was written to the EFI System.\r
794 @retval EFI_INVALID_PARAMETER Width is invalid for this EFI System.\r
795 Buffer is NULL.\r
796 The Buffer is not aligned for the given Width.\r
797 Address is outside the legal range of I/O ports.\r
798 @note A known Limitations in the implementation which is the 'Segment' parameter is assumed as \r
799 Zero, or else, assert.\r
800**/\r
801EFI_STATUS\r
802ScriptPciCfg2Read (\r
803 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
804 IN UINT16 Segment, \r
805 IN UINT64 Address,\r
806 OUT VOID *Buffer\r
807 )\r
808{\r
809 ASSERT (Segment==0);\r
810 \r
811 return ScriptPciCfgRead (Width, Address, Buffer);\r
812}\r
813/**\r
814 Perform pci configure write operation.\r
815 \r
816 @param Width Width of the operation.\r
817 @param Segment Pci segment number\r
818 @param Address Address of the operation.\r
819 @param Buffer Pointer to the buffer to write to I/O space. \r
820\r
821 @retval EFI_SUCCESS The data was written to the EFI System.\r
822 @retval EFI_INVALID_PARAMETER Width is invalid for this EFI System.\r
823 Buffer is NULL.\r
824 The Buffer is not aligned for the given Width.\r
825 Address is outside the legal range of I/O ports.\r
826 @note A known Limitations in the implementation which is the 'Segment' parameter is assumed as \r
827 Zero, or else, assert.\r
828 \r
829**/\r
830EFI_STATUS\r
831EFIAPI\r
832ScriptPciCfg2Write (\r
833 IN S3_BOOT_SCRIPT_LIB_WIDTH Width,\r
834 IN UINT16 Segment, \r
835 IN UINT64 Address,\r
836 OUT VOID *Buffer\r
837 )\r
838{\r
839 ASSERT (Segment==0);\r
840 return ScriptPciCfgWrite (Width, Address, Buffer);\r
841}\r
842/**\r
843 Perform Pci configuration Write operation.\r
844 \r
845 @param Script The pointer of typed node in boot script table \r
846 \r
847 @retval EFI_SUCCESS The operation was executed successfully\r
848**/\r
849EFI_STATUS\r
850BootScriptExecutePciCfgWrite (\r
851 IN UINT8 *Script\r
852 )\r
853{\r
854 EFI_STATUS Status;\r
855 UINT8 *Buffer;\r
856 UINTN DataWidth;\r
857 UINTN Index;\r
858 UINT64 PciAddress;\r
859 UINT8 Reg;\r
860 EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE PciConfigWrite;\r
861\r
862 CopyMem ((VOID*)&PciConfigWrite, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE));\r
863 Status = EFI_SUCCESS;\r
864\r
865 PciAddress = PciConfigWrite.Address;\r
866 DataWidth = (UINT32)(0x01 << (PciConfigWrite.Width));\r
867 Buffer = Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE);\r
868\r
869 DEBUG ((EFI_D_INFO, "BootScriptExecutePciCfgWrite - 0x%08x, 0x%08x, 0x%08x\n", (UINTN)PciAddress, (UINTN)PciConfigWrite.Count, (UINTN)DataWidth));\r
870\r
871 for (Index = 0; Index < PciConfigWrite.Count; Index++) {\r
872 Status = ScriptPciCfgWrite (\r
873 (S3_BOOT_SCRIPT_LIB_WIDTH) PciConfigWrite.Width,\r
874 PciAddress,\r
875 Buffer\r
876 );\r
877\r
878 if ( S3BootScriptWidthFillUint8 != PciConfigWrite.Width ||\r
879 S3BootScriptWidthFillUint16 != PciConfigWrite.Width || \r
880 S3BootScriptWidthFillUint32 != PciConfigWrite.Width ||\r
881 S3BootScriptWidthFillUint64 != PciConfigWrite.Width){\r
882 Reg = (UINT8) ((UINT8) PciAddress + DataWidth);\r
883 PciAddress = (PciAddress & 0xFFFFFFFFFFFFFF00ULL) + Reg;\r
884 }\r
885\r
886 if (S3BootScriptWidthFifoUint8 != PciConfigWrite.Width ||\r
887 S3BootScriptWidthFifoUint16 != PciConfigWrite.Width || \r
888 S3BootScriptWidthFifoUint32 != PciConfigWrite.Width ||\r
889 S3BootScriptWidthFifoUint64 != PciConfigWrite.Width) {\r
890 Buffer += DataWidth;\r
891 }\r
892 }\r
893\r
894 return Status;\r
895}\r
896/**\r
897 Excute the script to perform IO modification operation.\r
898\r
899 @param Script The pointer of typed node in boot script table \r
900 @param AndMask Mask value for 'and' operation\r
901 @param OrMask Mask value for 'or' operation\r
902\r
903 @retval EFI_SUCCESS The operation was executed successfully\r
904**/\r
905EFI_STATUS\r
906BootScriptExecuteIoReadWrite (\r
907 IN UINT8 *Script,\r
908 IN UINT64 AndMask,\r
909 IN UINT64 OrMask\r
910 )\r
911\r
912{\r
913 EFI_STATUS Status;\r
914 UINT64 Data;\r
915 EFI_BOOT_SCRIPT_IO_READ_WRITE IoReadWrite;\r
916 \r
917 Data = 0;\r
918 \r
919 CopyMem((VOID*)&IoReadWrite, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_IO_READ_WRITE));\r
920\r
921 DEBUG ((EFI_D_INFO, "BootScriptExecuteIoReadWrite - 0x%08x, 0x%016lx, 0x%016lx\n", (UINTN)IoReadWrite.Address, (UINT64)AndMask, (UINT64)OrMask));\r
922\r
923 Status = ScriptIoRead (\r
924 (S3_BOOT_SCRIPT_LIB_WIDTH) IoReadWrite.Width,\r
925 IoReadWrite.Address,\r
926 1,\r
927 &Data\r
928 );\r
929 if (!EFI_ERROR (Status)) {\r
930 Data = (Data & AndMask) | OrMask;\r
931 Status = ScriptIoWrite (\r
932 (S3_BOOT_SCRIPT_LIB_WIDTH) IoReadWrite.Width,\r
933 IoReadWrite.Address,\r
934 1,\r
935 &Data\r
936 );\r
937 }\r
938 return Status;\r
939}\r
940/**\r
941 Excute the script to perform memory modification operation.\r
942\r
943 @param Script The pointer of typed node in boot script table \r
944 @param AndMask Mask value for 'and' operation\r
945 @param OrMask Mask value for 'or' operation\r
946\r
947 @retval EFI_SUCCESS The operation was executed successfully\r
948**/\r
949EFI_STATUS\r
950BootScriptExecuteMemoryReadWrite (\r
951 IN UINT8 *Script,\r
952 IN UINT64 AndMask,\r
953 IN UINT64 OrMask\r
954 )\r
955\r
956{\r
957 EFI_STATUS Status;\r
958 UINT64 Data;\r
959 EFI_BOOT_SCRIPT_MEM_READ_WRITE MemReadWrite;\r
960 \r
961 Data = 0;\r
962 \r
963 CopyMem((VOID*)&MemReadWrite, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_MEM_READ_WRITE));\r
964\r
965 DEBUG ((EFI_D_INFO, "BootScriptExecuteMemoryReadWrite - 0x%08x, 0x%016lx, 0x%016lx\n", (UINTN)MemReadWrite.Address, (UINT64)AndMask, (UINT64)OrMask));\r
966 \r
967 Status = ScriptMemoryRead (\r
968 (S3_BOOT_SCRIPT_LIB_WIDTH) MemReadWrite.Width,\r
969 MemReadWrite.Address,\r
970 1,\r
971 &Data\r
972 );\r
973 if (!EFI_ERROR (Status)) {\r
974 Data = (Data & AndMask) | OrMask;\r
975 Status = ScriptMemoryWrite (\r
976 (S3_BOOT_SCRIPT_LIB_WIDTH) MemReadWrite.Width,\r
977 MemReadWrite.Address,\r
978 1,\r
979 &Data\r
980 );\r
981 }\r
982 return Status;\r
983}\r
984/**\r
985 Excute the script to perform PCI IO modification operation.\r
986\r
987 @param Script The pointer of typed node in boot script table \r
988 @param AndMask Mask value for 'and' operation\r
989 @param OrMask Mask value for 'or' operation\r
990\r
991 @retval EFI_SUCCESS The operation was executed successfully\r
992**/\r
993EFI_STATUS\r
994BootScriptExecutePciCfgReadWrite (\r
995 IN UINT8 *Script,\r
996 IN UINT64 AndMask,\r
997 IN UINT64 OrMask\r
998 )\r
999\r
1000{\r
1001 EFI_STATUS Status;\r
1002 UINT64 Data;\r
1003 EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE PciCfgReadWrite;\r
1004 \r
1005 CopyMem((VOID*)&PciCfgReadWrite, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE));\r
1006\r
1007 DEBUG ((EFI_D_INFO, "BootScriptExecutePciCfgReadWrite - 0x%08x, 0x%016lx, 0x%016lx\n", (UINTN)PciCfgReadWrite.Address, (UINT64)AndMask, (UINT64)OrMask));\r
1008 \r
1009 Status = ScriptPciCfgRead (\r
1010 (S3_BOOT_SCRIPT_LIB_WIDTH) PciCfgReadWrite.Width,\r
1011 PciCfgReadWrite.Address,\r
1012 &Data\r
1013 );\r
1014 if (EFI_ERROR (Status)) {\r
1015 return Status;\r
1016 }\r
1017\r
1018 Data = (Data & AndMask) | OrMask;\r
1019\r
1020 Status = ScriptPciCfgWrite (\r
1021 (S3_BOOT_SCRIPT_LIB_WIDTH) PciCfgReadWrite.Width,\r
1022 PciCfgReadWrite.Address,\r
1023 &Data\r
1024 );\r
1025\r
1026 return Status;\r
1027}\r
1028/**\r
1029 To Execute SMBUS command. \r
1030\r
1031 @param Script The pointer of typed node in boot script table \r
1032 \r
1033 @retval EFI_SUCCESS The operation was executed successfully\r
1034 @retval EFI_UNSUPPORTED Cannot locate smbus ppi or occur error of script execution\r
1035 @retval Others Result of script execution \r
1036**/\r
1037EFI_STATUS\r
1038BootScriptExecuteSmbusExecute (\r
1039 IN UINT8 *Script\r
1040 )\r
1041{\r
1042 UINTN SmBusAddress;\r
1043 UINTN DataSize;\r
1044 EFI_BOOT_SCRIPT_SMBUS_EXECUTE SmbusExecuteEntry;\r
1045 \r
1046 CopyMem ((VOID*)&SmbusExecuteEntry, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_SMBUS_EXECUTE ));\r
1047\r
1048 DEBUG ((EFI_D_INFO, "BootScriptExecuteSmbusExecute - 0x%08x, 0x%08x\n", (UINTN)SmbusExecuteEntry.SmBusAddress, (UINTN)SmbusExecuteEntry.Operation));\r
1049\r
1050 SmBusAddress = (UINTN)SmbusExecuteEntry.SmBusAddress;\r
1051 DataSize = (UINTN) SmbusExecuteEntry.DataSize;\r
1052 return SmbusExecute (\r
1053 SmBusAddress,\r
1054 (EFI_SMBUS_OPERATION) SmbusExecuteEntry.Operation,\r
1055 &DataSize,\r
1056 Script + sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)\r
1057 );\r
1058}\r
1059/**\r
1060 Execute stall operation in boot script table.\r
1061\r
1062 @param Script The pointer of typed node in boot script table \r
1063 \r
1064 @retval EFI_SUCCESS The operation was executed successfully\r
1065**/\r
1066EFI_STATUS\r
1067BootScriptExecuteStall (\r
1068 IN UINT8 *Script\r
1069 )\r
1070{\r
1071 EFI_BOOT_SCRIPT_STALL Stall;\r
1072 \r
1073 CopyMem ((VOID*)&Stall, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_STALL));\r
1074\r
1075 DEBUG ((EFI_D_INFO, "BootScriptExecuteStall - 0x%08x\n", (UINTN)Stall.Duration));\r
1076\r
1077 MicroSecondDelay ((UINTN) Stall.Duration);\r
1078 return EFI_SUCCESS;\r
1079}\r
1080/**\r
1081 To execute assigned function.\r
1082 \r
1083 @param Script The pointer of typed node in boot script table \r
1084 @retval EFI_SUCCESS The operation was executed successfully\r
1085**/\r
1086EFI_STATUS\r
1087BootScriptExecuteDispatch (\r
1088 IN UINT8 *Script\r
1089 )\r
1090{\r
1091 EFI_STATUS Status;\r
1092 DISPATCH_ENTRYPOINT_FUNC EntryFunc;\r
1093 EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch;\r
1094 \r
1095 CopyMem ((VOID*)&ScriptDispatch, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_DISPATCH));\r
1096 EntryFunc = (DISPATCH_ENTRYPOINT_FUNC) (UINTN) (ScriptDispatch.EntryPoint);\r
1097\r
1098 DEBUG ((EFI_D_INFO, "BootScriptExecuteDispatch - 0x%08x\n", (UINTN)ScriptDispatch.EntryPoint));\r
1099\r
1100 Status = EntryFunc (NULL, NULL);\r
1101\r
1102 return Status;\r
1103}\r
1104/**\r
1105 Execute dispach2 opertion code which is to invoke a spcified function with one parameter. \r
1106\r
1107 @param Script The pointer of typed node in boot script table \r
1108 @retval EFI_SUCCESS The operation was executed successfully\r
1109**/\r
1110EFI_STATUS\r
1111BootScriptExecuteDispatch2 (\r
1112 IN UINT8 *Script\r
1113 )\r
1114{\r
1115 EFI_STATUS Status;\r
1116 DISPATCH_ENTRYPOINT_FUNC EntryFunc;\r
1117 EFI_BOOT_SCRIPT_DISPATCH_2 ScriptDispatch2;\r
1118 \r
1119 CopyMem ((VOID*)&ScriptDispatch2, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_DISPATCH_2));\r
1120\r
1121 DEBUG ((EFI_D_INFO, "BootScriptExecuteDispatch2 - 0x%08x(0x%08x)\n", (UINTN)ScriptDispatch2.EntryPoint, (UINTN)ScriptDispatch2.Context));\r
1122 \r
1123 EntryFunc = (DISPATCH_ENTRYPOINT_FUNC) (UINTN) (ScriptDispatch2.EntryPoint);\r
1124\r
1125 Status = EntryFunc (NULL, (VOID *) (UINTN) ScriptDispatch2.Context);\r
1126\r
1127 return Status;\r
1128}\r
1129/**\r
1130 Excute the script to poll memory.\r
1131\r
1132 @param Script The pointer of typed node in boot script table \r
1133 @param AndMask Mask value for 'and' operation\r
1134 @param OrMask Mask value for 'or' operation\r
1135 \r
1136 @retval EFI_DEVICE_ERROR Data polled from memory does not equal to \r
1137 the epecting data within the Loop Times.\r
1138 @retval EFI_SUCCESS The operation was executed successfully\r
1139**/\r
1140EFI_STATUS\r
1141BootScriptExecuteMemPoll (\r
1142 IN UINT8 *Script,\r
1143 IN UINT64 AndMask,\r
1144 IN UINT64 OrMask \r
1145 )\r
1146{\r
1147 \r
1148 UINT64 Data;\r
1149 UINT64 LoopTimes;\r
1150 EFI_STATUS Status;\r
1151 EFI_BOOT_SCRIPT_MEM_POLL MemPoll;\r
1152 \r
1153 CopyMem ((VOID*)&MemPoll, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_MEM_POLL));\r
1154\r
1155 DEBUG ((EFI_D_INFO, "BootScriptExecuteMemPoll - 0x%08x\n", (UINTN)MemPoll.Address));\r
1156\r
1157 Data = 0;\r
1158 Status = ScriptMemoryRead (\r
1159 (S3_BOOT_SCRIPT_LIB_WIDTH) MemPoll.Width,\r
1160 MemPoll.Address,\r
1161 1,\r
1162 &Data\r
1163 );\r
1164 if ((!EFI_ERROR (Status)) && (Data & AndMask) == OrMask) {\r
1165 return EFI_SUCCESS;\r
1166 }\r
1167\r
1168 for (LoopTimes = 0; LoopTimes < MemPoll.LoopTimes; LoopTimes++) {\r
1169 NanoSecondDelay ((UINTN)MemPoll.Duration);\r
1170\r
1171 Data = 0;\r
1172 Status = ScriptMemoryRead (\r
1173 (S3_BOOT_SCRIPT_LIB_WIDTH) MemPoll.Width,\r
1174 MemPoll.Address,\r
1175 1,\r
1176 &Data\r
1177 );\r
1178 if ((!EFI_ERROR (Status)) && (Data & AndMask) == OrMask) {\r
1179 return EFI_SUCCESS;\r
1180 }\r
1181 }\r
1182\r
1183 if (LoopTimes < MemPoll.LoopTimes) {\r
1184 return EFI_SUCCESS;\r
1185 } else {\r
1186 return EFI_DEVICE_ERROR;\r
1187 }\r
1188}\r
1189/**\r
1190 Execute the boot script to interpret the Store arbitrary information. \r
1191 This opcode is a no-op on dispatch and is only used for debugging script issues.\r
1192\r
1193 @param Script The pointer of node in boot script table \r
1194 \r
1195**/\r
1196VOID\r
1197BootScriptExecuteInformation (\r
1198 IN UINT8 *Script\r
1199 )\r
1200\r
1201{\r
f6ec0c77
JY
1202 UINT32 Index;\r
1203 EFI_BOOT_SCRIPT_INFORMATION Information;\r
1204\r
1205 CopyMem ((VOID*)&Information, (VOID*)Script, sizeof(Information));\r
1206\r
1207 DEBUG ((EFI_D_INFO, "BootScriptExecuteInformation - 0x%08x\n", (UINTN)Information.Information));\r
1208\r
1209 DEBUG ((EFI_D_INFO, "BootScriptInformation: "));\r
1210 for (Index = 0; Index < Information.InformationLength; Index++) {\r
1211 DEBUG ((EFI_D_INFO, "%02x ", *(UINT8 *)(UINTN)(Information.Information + Index)));\r
1212 }\r
1213 DEBUG ((EFI_D_INFO, "\n"));\r
64d14edf 1214}\r
1215/**\r
1216 calculate the mask value for 'and' and 'or' operation\r
1217 @param ScriptHeader The pointer of header of node in boot script table \r
1218 @param AndMask The Mask value for 'and' operation\r
1219 @param OrMask The Mask value for 'or' operation\r
1220 @param Script Pointer to the entry.\r
1221\r
1222**/\r
1223VOID\r
1224CheckAndOrMask (\r
1225 IN EFI_BOOT_SCRIPT_COMMON_HEADER *ScriptHeader,\r
1226 OUT UINT64 *AndMask,\r
1227 OUT UINT64 *OrMask,\r
1228 IN UINT8 *Script\r
1229 )\r
1230{\r
1231 UINT8 *DataPtr;\r
1232 UINTN Size;\r
1233\r
1234 switch (ScriptHeader->OpCode) {\r
1235 case EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE:\r
1236 Size = sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE);\r
1237 break;\r
1238\r
1239 case EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE:\r
1240 Size = sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE);\r
1241 break;\r
1242\r
1243 case EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE:\r
1244 Size = sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE);\r
1245 break;\r
1246 case EFI_BOOT_SCRIPT_MEM_POLL_OPCODE:\r
1247 Size = sizeof (EFI_BOOT_SCRIPT_MEM_POLL);\r
1248 break;\r
1249 \r
1250 case EFI_BOOT_SCRIPT_IO_POLL_OPCODE:\r
1251 Size = sizeof (EFI_BOOT_SCRIPT_IO_POLL);\r
1252 break; \r
1253 \r
1254 case EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE:\r
1255 Size = sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE);\r
1256 break;\r
1257 \r
1258 case EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE:\r
1259 Size = sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL);\r
1260 break;\r
1261 \r
1262 case EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE:\r
1263 Size = sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL);\r
1264 break;\r
1265 \r
1266 default:\r
1267 return;\r
1268 }\r
1269 \r
1270 DataPtr = Script + Size;\r
1271\r
1272 switch (ScriptHeader->Width) {\r
1273 case S3BootScriptWidthUint8:\r
1274 *AndMask = (UINT64) *(DataPtr + 1);\r
1275 *OrMask = (UINT64) (*DataPtr);\r
1276 break;\r
1277\r
1278 case S3BootScriptWidthUint16:\r
1279 *AndMask = (UINT64) (*(UINT16 *) (DataPtr + 2));\r
1280 *OrMask = (UINT64) (*(UINT16 *) DataPtr);\r
1281 break;\r
1282\r
1283 case S3BootScriptWidthUint32:\r
1284 *AndMask = (UINT64) (*(UINT32 *) (DataPtr + 4));\r
1285 *OrMask = (UINT64) (*(UINT32 *) DataPtr);\r
1286 break;\r
1287\r
1288 case S3BootScriptWidthUint64:\r
1289 *AndMask = (UINT64) (*(UINT64 *) (DataPtr + 8));\r
1290 *OrMask = (UINT64) (*(UINT64 *) DataPtr);\r
1291 break;\r
1292\r
1293 default:\r
1294 break;\r
1295 }\r
1296\r
1297 return;\r
1298}\r
1299/**\r
1300 Excute the script to poll Io port for some time\r
1301\r
1302 @param Script The pointer of typed node in boot script table \r
1303 @param AndMask Mask value for 'and' operation\r
1304 @param OrMask Mask value for 'or' operation\r
1305 \r
1306 @retval EFI_DEVICE_ERROR Data polled from memory does not equal to \r
1307 the epecting data within the Loop Times.\r
1308 @retval EFI_SUCCESS The operation was executed successfully\r
1309**/\r
1310EFI_STATUS\r
1311BootScriptExecuteIoPoll (\r
1312 IN UINT8 *Script,\r
1313 IN UINT64 AndMask,\r
1314 IN UINT64 OrMask\r
1315 )\r
1316{\r
1317 EFI_STATUS Status;\r
1318 UINT64 Data;\r
1319 UINT64 LoopTimes;\r
1320 EFI_BOOT_SCRIPT_IO_POLL IoPoll;\r
1321 \r
1322 CopyMem ((VOID*)&IoPoll, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_IO_POLL));\r
1323\r
1324 DEBUG ((EFI_D_INFO, "BootScriptExecuteIoPoll - 0x%08x\n", (UINTN)IoPoll.Address));\r
1325\r
1326 Data = 0;\r
1327 Status = ScriptIoRead (\r
1328 (S3_BOOT_SCRIPT_LIB_WIDTH) IoPoll.Width,\r
1329 IoPoll.Address,\r
1330 1,\r
1331 &Data\r
1332 );\r
1333 if ((!EFI_ERROR (Status)) && (Data & AndMask) == OrMask) {\r
1334 return EFI_SUCCESS;\r
1335 }\r
1336 for (LoopTimes = 0; LoopTimes < IoPoll.Delay; LoopTimes++) {\r
1337 NanoSecondDelay (100);\r
1338 Data = 0;\r
1339 Status = ScriptIoRead (\r
1340 (S3_BOOT_SCRIPT_LIB_WIDTH) IoPoll.Width,\r
1341 IoPoll.Address,\r
1342 1,\r
1343 &Data\r
1344 );\r
1345 if ((!EFI_ERROR (Status)) &&(Data & AndMask) == OrMask) {\r
1346 return EFI_SUCCESS;\r
1347 } \r
1348 }\r
1349\r
1350 if (LoopTimes < IoPoll.Delay) {\r
1351 return EFI_SUCCESS;\r
1352 } else {\r
1353 return EFI_DEVICE_ERROR;\r
1354 }\r
1355}\r
1356/**\r
1357 Perform Pci configuration Write operation.\r
1358\r
1359 @param Script The pointer of S3 boot script\r
1360\r
1361 @retval EFI_SUCCESS The operation was executed successfully\r
1362\r
1363**/\r
1364EFI_STATUS\r
1365BootScriptExecutePciCfg2Write (\r
1366 IN UINT8 *Script\r
1367 )\r
1368{\r
1369 UINT8 Reg;\r
1370 UINT8 *Buffer;\r
1371 UINTN DataWidth;\r
1372 UINTN Index;\r
1373 UINT16 Segment;\r
1374 UINT64 PciAddress;\r
1375 EFI_STATUS Status;\r
1376 EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE PciCfg2Write;\r
1377 \r
1378 CopyMem ((VOID*)&PciCfg2Write, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE));\r
1379 Status = EFI_SUCCESS;\r
1380 Segment = PciCfg2Write.Segment;\r
1381 PciAddress = PciCfg2Write.Address;\r
1382 DataWidth = (UINT32)(0x01 << (PciCfg2Write.Width));\r
1383 Buffer = Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE);\r
1384\r
1385 DEBUG ((EFI_D_INFO, "BootScriptExecutePciCfg2Write - 0x%08x\n", (UINTN)PciAddress));\r
1386\r
1387 for (Index = 0; Index < PciCfg2Write.Count; Index++) {\r
1388 Status = ScriptPciCfg2Write (\r
1389 (S3_BOOT_SCRIPT_LIB_WIDTH) PciCfg2Write.Width,\r
1390 Segment,\r
1391 PciAddress,\r
1392 Buffer\r
1393 );\r
1394 if (S3BootScriptWidthFillUint8 != PciCfg2Write.Width ||\r
1395 S3BootScriptWidthFillUint16 != PciCfg2Write.Width || \r
1396 S3BootScriptWidthFillUint32 != PciCfg2Write.Width ||\r
1397 S3BootScriptWidthFillUint64 != PciCfg2Write.Width){\r
1398 Reg = (UINT8) ((UINT8) PciAddress + DataWidth);\r
1399 PciAddress = (PciAddress & 0xFFFFFFFFFFFFFF00ULL) + Reg;\r
1400 }\r
1401\r
1402 if (S3BootScriptWidthFifoUint8 != PciCfg2Write.Width ||\r
1403 S3BootScriptWidthFifoUint16 != PciCfg2Write.Width || \r
1404 S3BootScriptWidthFifoUint32 != PciCfg2Write.Width ||\r
1405 S3BootScriptWidthFifoUint64 != PciCfg2Write.Width) {\r
1406 Buffer += DataWidth;\r
1407 }\r
1408 }\r
1409 return Status;\r
1410}\r
1411\r
1412\r
1413/**\r
1414 Perform pci configuration read & Write operation.\r
1415 \r
1416 @param Script The pointer of S3 boot script\r
1417 @param AndMask Mask value for 'and' operation\r
1418 @param OrMask Mask value for 'or' operation\r
1419\r
1420 @retval EFI_SUCCESS The operation was executed successfully\r
1421\r
1422**/\r
1423EFI_STATUS\r
1424BootScriptExecutePciCfg2ReadWrite (\r
1425 IN UINT8 *Script,\r
1426 IN UINT64 AndMask,\r
1427 IN UINT64 OrMask\r
1428 )\r
1429{\r
1430 UINT64 Data;\r
1431 EFI_STATUS Status;\r
1432 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE PciCfg2ReadWrite;\r
1433 CopyMem ((VOID*)&PciCfg2ReadWrite, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE));\r
1434\r
1435 DEBUG ((EFI_D_INFO, "BootScriptExecutePciCfg2ReadWrite - 0x%08x\n", (UINTN)PciCfg2ReadWrite.Address));\r
1436 \r
1437 Status = ScriptPciCfg2Read (\r
1438 (S3_BOOT_SCRIPT_LIB_WIDTH) PciCfg2ReadWrite.Width,\r
1439 PciCfg2ReadWrite.Segment,\r
1440 PciCfg2ReadWrite.Address,\r
1441 &Data\r
1442 );\r
1443 if (EFI_ERROR (Status)) {\r
1444 return Status;\r
1445 }\r
1446\r
1447 Data = (Data & AndMask) | OrMask;\r
1448 Status = ScriptPciCfg2Write (\r
1449 (S3_BOOT_SCRIPT_LIB_WIDTH) PciCfg2ReadWrite.Width,\r
1450 PciCfg2ReadWrite.Segment,\r
1451 PciCfg2ReadWrite.Address,\r
1452 &Data\r
1453 );\r
1454 return Status;\r
1455}\r
1456/**\r
1457 To perform poll pci configure operation.\r
1458 \r
1459 @param Script The pointer of S3 boot script\r
1460 @param AndMask Mask value for 'and' operation\r
1461 @param OrMask Mask value for 'or' operation\r
1462\r
1463 @retval EFI_SUCCESS The operation was executed successfully\r
1464 @retval EFI_DEVICE_ERROR Data polled from Pci configuration space does not equal to \r
1465 epecting data within the Loop Times.\r
1466**/\r
1467EFI_STATUS\r
1468BootScriptPciCfgPoll (\r
1469 IN UINT8 *Script,\r
1470 IN UINT64 AndMask,\r
1471 IN UINT64 OrMask \r
1472 )\r
1473{\r
1474 UINT64 Data;\r
1475 UINT64 LoopTimes;\r
1476 EFI_STATUS Status;\r
1477 EFI_BOOT_SCRIPT_PCI_CONFIG_POLL PciCfgPoll;\r
1478 CopyMem ((VOID*)&PciCfgPoll, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_PCI_CONFIG_POLL));\r
1479\r
1480 DEBUG ((EFI_D_INFO, "BootScriptPciCfgPoll - 0x%08x\n", (UINTN)PciCfgPoll.Address));\r
1481 \r
1482 Data = 0;\r
1483 Status = ScriptPciCfgRead (\r
1484 (S3_BOOT_SCRIPT_LIB_WIDTH) PciCfgPoll.Width,\r
1485 PciCfgPoll.Address,\r
1486 &Data\r
1487 );\r
1488 if ((!EFI_ERROR (Status)) &&(Data & AndMask) == OrMask) {\r
1489 return EFI_SUCCESS;\r
1490 }\r
1491\r
1492 for (LoopTimes = 0; LoopTimes < PciCfgPoll.Delay; LoopTimes++) {\r
1493 NanoSecondDelay (100);\r
1494 Data = 0;\r
1495 Status = ScriptPciCfgRead (\r
1496 (S3_BOOT_SCRIPT_LIB_WIDTH) PciCfgPoll.Width,\r
1497 PciCfgPoll.Address,\r
1498 &Data\r
1499 );\r
1500 if ((!EFI_ERROR (Status)) &&\r
1501 (Data & AndMask) == OrMask) {\r
1502 return EFI_SUCCESS;\r
1503 }\r
1504 }\r
1505\r
1506 if (LoopTimes < PciCfgPoll.Delay) {\r
1507 return EFI_SUCCESS;\r
1508 } else {\r
1509 return EFI_DEVICE_ERROR;\r
1510 }\r
1511}\r
1512\r
1513/**\r
1514 To perform poll pci configure operation.\r
1515 \r
1516 @param Script The pointer of S3 Boot Script\r
1517 @param AndMask Mask value for 'and' operation\r
1518 @param OrMask Mask value for 'or' operation\r
1519\r
1520 @retval EFI_SUCCESS The operation was executed successfully\r
1521 @retval EFI_DEVICE_ERROR Data polled from Pci configuration space does not equal to \r
1522 epecting data within the Loop Times.\r
1523\r
1524**/\r
1525EFI_STATUS\r
1526BootScriptPciCfg2Poll (\r
1527 IN UINT8 *Script,\r
1528 IN UINT64 AndMask,\r
1529 IN UINT64 OrMask \r
1530 )\r
1531{\r
1532 EFI_STATUS Status;\r
1533 UINT64 Data;\r
1534 UINT64 LoopTimes;\r
1535 EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL PciCfg2Poll;\r
1536\r
1537 Data = 0;\r
1538 CopyMem ((VOID*)&PciCfg2Poll, (VOID*)Script, sizeof(EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL));\r
1539\r
1540 DEBUG ((EFI_D_INFO, "BootScriptPciCfg2Poll - 0x%08x\n", (UINTN)PciCfg2Poll.Address));\r
1541 \r
1542 Status = ScriptPciCfg2Read (\r
1543 (S3_BOOT_SCRIPT_LIB_WIDTH) PciCfg2Poll.Width,\r
1544 PciCfg2Poll.Segment,\r
1545 PciCfg2Poll.Address,\r
1546 &Data\r
1547 );\r
1548 if ((!EFI_ERROR (Status)) && (Data & AndMask) == OrMask) {\r
1549 return EFI_SUCCESS;\r
1550 }\r
1551\r
1552 for (LoopTimes = 0; LoopTimes < PciCfg2Poll.Delay; LoopTimes++) {\r
1553 NanoSecondDelay (100);\r
1554\r
1555 Data = 0;\r
1556 Status = ScriptPciCfg2Read (\r
1557 (S3_BOOT_SCRIPT_LIB_WIDTH) PciCfg2Poll.Width,\r
1558 PciCfg2Poll.Segment, \r
1559 PciCfg2Poll.Address,\r
1560 &Data\r
1561 );\r
1562 if ((!EFI_ERROR (Status)) && (Data & AndMask) == OrMask) {\r
1563 return EFI_SUCCESS;\r
1564 }\r
1565 }\r
1566\r
1567 if (LoopTimes < PciCfg2Poll.Delay) {\r
1568 return EFI_SUCCESS;\r
1569 } else {\r
1570 return EFI_DEVICE_ERROR;\r
1571 }\r
1572 \r
1573}\r
1574\r
1575/**\r
1576 Executes the S3 boot script table.\r
1577 \r
1578 @retval RETURN_SUCCESS The boot script table was executed successfully.\r
1579 @retval RETURN_UNSUPPORTED Invalid script table or opcode. \r
1580 \r
1581 @note A known Limitations in the implementation: When interpreting the opcode EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE\r
1582 EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE and EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE, the 'Segment' parameter is assumed as \r
1583 Zero, or else, assert.\r
1584**/\r
1585RETURN_STATUS\r
1586EFIAPI\r
1587S3BootScriptExecute (\r
1588 VOID\r
1589 )\r
1590{\r
1591 EFI_STATUS Status;\r
1592 UINT8* Script;\r
1593 UINTN StartAddress;\r
1594 UINT32 TableLength;\r
1595 UINT64 AndMask;\r
1596 UINT64 OrMask;\r
1597 EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;\r
1598 EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader;\r
1599 Script = mS3BootScriptTablePtr->TableBase;\r
1600 if (Script != 0) { \r
1601 CopyMem ((VOID*)&TableHeader, Script, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
1602 } else {\r
1603 return EFI_INVALID_PARAMETER;\r
1604 }\r
1605\r
1606 DEBUG ((EFI_D_INFO, "S3BootScriptExecute:\n"));\r
1607 if (TableHeader.OpCode != S3_BOOT_SCRIPT_LIB_TABLE_OPCODE) {\r
1608 return EFI_UNSUPPORTED;\r
1609 }\r
1610\r
1611 DEBUG ((EFI_D_INFO, "TableHeader - 0x%08x\n", Script));\r
1612 \r
1613 StartAddress = (UINTN) Script;\r
1614 TableLength = TableHeader.TableLength;\r
1615 Script = Script + TableHeader.Length;\r
1616 Status = EFI_SUCCESS;\r
1617 AndMask = 0;\r
1618 OrMask = 0;\r
1619\r
1620 DEBUG ((EFI_D_INFO, "TableHeader.TableLength - 0x%08x\n", (UINTN)TableLength));\r
1621\r
1622 while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {\r
1623 DEBUG ((EFI_D_INFO, "ExecuteBootScript - %08x\n", (UINTN)Script));\r
1624 \r
1625 CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));\r
1626 switch (ScriptHeader.OpCode) {\r
1627\r
1628 case EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE:\r
1629 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE\n"));\r
1630 Status = BootScriptExecuteMemoryWrite (Script);\r
1631 break;\r
1632\r
1633 case EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE:\r
1634 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE\n"));\r
1635 CheckAndOrMask (&ScriptHeader, &AndMask, &OrMask, Script);\r
1636 Status = BootScriptExecuteMemoryReadWrite (\r
1637 Script,\r
1638 AndMask,\r
1639 OrMask\r
1640 );\r
1641 break;\r
1642\r
1643 case EFI_BOOT_SCRIPT_IO_WRITE_OPCODE:\r
1644 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_IO_WRITE_OPCODE\n"));\r
1645 Status = BootScriptExecuteIoWrite (Script);\r
1646 break;\r
1647\r
1648 case EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE:\r
1649 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE\n"));\r
1650 Status = BootScriptExecutePciCfgWrite (Script);\r
1651 break;\r
1652\r
1653 case EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE:\r
1654 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE\n"));\r
1655 CheckAndOrMask (&ScriptHeader, &AndMask, &OrMask, Script);\r
1656 Status = BootScriptExecutePciCfgReadWrite (\r
1657 Script,\r
1658 AndMask,\r
1659 OrMask\r
1660 );\r
1661 break;\r
1662 case EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE:\r
1663 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE\n"));\r
1664 Status = BootScriptExecutePciCfg2Write (Script);\r
1665 break;\r
1666\r
1667 case EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE:\r
1668 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE\n"));\r
1669 CheckAndOrMask (&ScriptHeader, &AndMask, &OrMask, Script);\r
1670 Status = BootScriptExecutePciCfg2ReadWrite (\r
1671 Script,\r
1672 AndMask,\r
1673 OrMask\r
1674 );\r
1675 break;\r
1676 case EFI_BOOT_SCRIPT_DISPATCH_OPCODE:\r
1677 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_DISPATCH_OPCODE\n"));\r
1678 Status = BootScriptExecuteDispatch (Script);\r
1679 break;\r
1680\r
1681 case EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE:\r
1682 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE\n"));\r
1683 Status = BootScriptExecuteDispatch2 (Script);\r
1684 break;\r
1685\r
1686 case EFI_BOOT_SCRIPT_INFORMATION_OPCODE:\r
1687 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_INFORMATION_OPCODE\n"));\r
1688 BootScriptExecuteInformation (Script);\r
1689 break; \r
1690\r
1691 case S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE:\r
1692 DEBUG ((EFI_D_INFO, "S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE\n"));\r
1693 return EFI_SUCCESS;\r
1694\r
1695 case EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE:\r
1696 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE\n"));\r
1697 CheckAndOrMask (&ScriptHeader, &AndMask, &OrMask, Script);\r
1698 Status = BootScriptExecuteIoReadWrite (\r
1699 Script,\r
1700 AndMask,\r
1701 OrMask\r
1702 );\r
1703 break;\r
1704\r
1705 case EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE:\r
1706 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE\n"));\r
1707 Status = BootScriptExecuteSmbusExecute (Script);\r
1708 break;\r
1709\r
1710 case EFI_BOOT_SCRIPT_STALL_OPCODE:\r
1711 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_STALL_OPCODE\n"));\r
1712 Status = BootScriptExecuteStall (Script);\r
1713 break;\r
1714\r
1715 case EFI_BOOT_SCRIPT_MEM_POLL_OPCODE:\r
1716 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_MEM_POLL_OPCODE\n"));\r
1717 CheckAndOrMask (&ScriptHeader, &AndMask, &OrMask, Script);\r
1718 Status = BootScriptExecuteMemPoll (Script, AndMask, OrMask);\r
1719 \r
1720 break;\r
1721 \r
1722 case EFI_BOOT_SCRIPT_IO_POLL_OPCODE:\r
1723 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_IO_POLL_OPCODE\n"));\r
1724 CheckAndOrMask (&ScriptHeader, &AndMask, &OrMask, Script);\r
1725 Status = BootScriptExecuteIoPoll (Script, AndMask, OrMask);\r
1726 break;\r
1727 \r
1728 case EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE:\r
1729 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE\n"));\r
1730 CheckAndOrMask (&ScriptHeader, &AndMask, &OrMask, Script);\r
1731 Status = BootScriptPciCfgPoll (Script, AndMask, OrMask);\r
1732 break;\r
1733 \r
1734 case EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE:\r
1735 DEBUG ((EFI_D_INFO, "EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE\n"));\r
1736 CheckAndOrMask (&ScriptHeader, &AndMask, &OrMask, Script);\r
1737 Status = BootScriptPciCfg2Poll (Script, AndMask, OrMask);\r
1738 break;\r
1739\r
1740 case S3_BOOT_SCRIPT_LIB_LABEL_OPCODE:\r
1741 //\r
1742 // For label\r
1743 //\r
1744 DEBUG ((EFI_D_INFO, "S3_BOOT_SCRIPT_LIB_LABEL_OPCODE\n"));\r
1745 break;\r
1746 default:\r
1747 DEBUG ((EFI_D_INFO, "S3BootScriptDone - %r\n", EFI_UNSUPPORTED));\r
1748 return EFI_UNSUPPORTED;\r
1749 }\r
1750\r
1751 if (EFI_ERROR (Status)) {\r
1752 DEBUG ((EFI_D_INFO, "S3BootScriptDone - %r\n", Status));\r
1753 return Status;\r
1754 }\r
1755\r
1756 Script = Script + ScriptHeader.Length;\r
1757 }\r
1758\r
1759 DEBUG ((EFI_D_INFO, "S3BootScriptDone - %r\n", Status));\r
1760\r
1761 return Status;\r
1762}\r
1763\r