]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/BootScriptSaveDxe/ScriptSave.c
Vlv2TbltDevicePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / Vlv2TbltDevicePkg / BootScriptSaveDxe / ScriptSave.c
1 /** @file
2 Implementation for S3 Boot Script Saver driver.
3
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8
9
10 **/
11
12 #include "InternalBootScriptSave.h"
13
14 EFI_HANDLE mHandle = NULL;
15 EFI_BOOT_SCRIPT_SAVE_PROTOCOL mS3ScriptSave = {
16 BootScriptWrite,
17 BootScriptCloseTable
18 };
19
20 /**
21 Internal function to add IO write opcode to the table.
22
23 @param Marker The variable argument list to get the opcode
24 and associated attributes.
25
26 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
27 @retval EFI_SUCCESS Opcode is added.
28
29 **/
30 EFI_STATUS
31 BootScriptIoWrite (
32 IN VA_LIST Marker
33 )
34 {
35 S3_BOOT_SCRIPT_LIB_WIDTH Width;
36 UINT64 Address;
37 UINTN Count;
38 UINT8 *Buffer;
39
40 Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
41 Address = VA_ARG (Marker, UINT64);
42 Count = VA_ARG (Marker, UINTN);
43 Buffer = VA_ARG (Marker, UINT8 *);
44
45 return S3BootScriptSaveIoWrite (Width, Address, Count, Buffer);
46 }
47
48 /**
49 Internal function to add IO read/write opcode to the table.
50
51 @param Marker The variable argument list to get the opcode
52 and associated attributes.
53
54 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
55 @retval EFI_SUCCESS Opcode is added.
56
57 **/
58 EFI_STATUS
59 BootScriptIoReadWrite (
60 IN VA_LIST Marker
61 )
62 {
63 S3_BOOT_SCRIPT_LIB_WIDTH Width;
64 UINT64 Address;
65 UINT8 *Data;
66 UINT8 *DataMask;
67
68 Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
69 Address = VA_ARG (Marker, UINT64);
70 Data = VA_ARG (Marker, UINT8 *);
71 DataMask = VA_ARG (Marker, UINT8 *);
72
73 return S3BootScriptSaveIoReadWrite (Width, Address, Data, DataMask);
74 }
75
76 /**
77 Internal function to add memory write opcode to the table.
78
79 @param Marker The variable argument list to get the opcode
80 and associated attributes.
81
82 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
83 @retval EFI_SUCCESS Opcode is added.
84
85 **/
86 EFI_STATUS
87 BootScriptMemWrite (
88 IN VA_LIST Marker
89 )
90 {
91 S3_BOOT_SCRIPT_LIB_WIDTH Width;
92 UINT64 Address;
93 UINTN Count;
94 UINT8 *Buffer;
95
96 Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
97 Address = VA_ARG (Marker, UINT64);
98 Count = VA_ARG (Marker, UINTN);
99 Buffer = VA_ARG (Marker, UINT8 *);
100
101 return S3BootScriptSaveMemWrite (Width, Address, Count, Buffer);
102 }
103
104 /**
105 Internal function to add memory read/write opcode to the table.
106
107 @param Marker The variable argument list to get the opcode
108 and associated attributes.
109
110 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
111 @retval EFI_SUCCESS Opcode is added.
112
113 **/
114 EFI_STATUS
115 BootScriptMemReadWrite (
116 IN VA_LIST Marker
117 )
118 {
119 S3_BOOT_SCRIPT_LIB_WIDTH Width;
120 UINT64 Address;
121 UINT8 *Data;
122 UINT8 *DataMask;
123
124 Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
125 Address = VA_ARG (Marker, UINT64);
126 Data = VA_ARG (Marker, UINT8 *);
127 DataMask = VA_ARG (Marker, UINT8 *);
128
129 return S3BootScriptSaveMemReadWrite (Width, Address, Data, DataMask);
130 }
131
132 /**
133 Internal function to add PciCfg write opcode to the table.
134
135 @param Marker The variable argument list to get the opcode
136 and associated attributes.
137
138 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
139 @retval EFI_SUCCESS Opcode is added.
140
141 **/
142 EFI_STATUS
143 BootScriptPciCfgWrite (
144 IN VA_LIST Marker
145 )
146 {
147 S3_BOOT_SCRIPT_LIB_WIDTH Width;
148 UINT64 Address;
149 UINTN Count;
150 UINT8 *Buffer;
151
152 Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
153 Address = VA_ARG (Marker, UINT64);
154 Count = VA_ARG (Marker, UINTN);
155 Buffer = VA_ARG (Marker, UINT8 *);
156
157 return S3BootScriptSavePciCfgWrite (Width, Address, Count, Buffer);
158 }
159
160 /**
161 Internal function to PciCfg read/write opcode to the table.
162
163 @param Marker The variable argument list to get the opcode
164 and associated attributes.
165
166 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
167 @retval EFI_SUCCESS Opcode is added.
168
169 **/
170 EFI_STATUS
171 BootScriptPciCfgReadWrite (
172 IN VA_LIST Marker
173 )
174 {
175 S3_BOOT_SCRIPT_LIB_WIDTH Width;
176 UINT64 Address;
177 UINT8 *Data;
178 UINT8 *DataMask;
179
180 Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
181 Address = VA_ARG (Marker, UINT64);
182 Data = VA_ARG (Marker, UINT8 *);
183 DataMask = VA_ARG (Marker, UINT8 *);
184
185 return S3BootScriptSavePciCfgReadWrite (Width, Address, Data, DataMask);
186 }
187
188 /**
189 Internal function to add PciCfg2 write opcode to the table.
190
191 @param Marker The variable argument list to get the opcode
192 and associated attributes.
193
194 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
195 @retval EFI_SUCCESS Opcode is added.
196
197 **/
198 EFI_STATUS
199 BootScriptPciCfg2Write (
200 IN VA_LIST Marker
201 )
202 {
203 S3_BOOT_SCRIPT_LIB_WIDTH Width;
204 UINT64 Address;
205 UINTN Count;
206 UINT8 *Buffer;
207 UINT16 Segment;
208
209 Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
210 Address = VA_ARG (Marker, UINT64);
211 Count = VA_ARG (Marker, UINTN);
212 Buffer = VA_ARG (Marker, UINT8 *);
213 Segment = VA_ARG (Marker, UINT16);
214
215 return S3BootScriptSavePciCfg2Write (Width, Segment, Address, Count, Buffer);
216 }
217
218 /**
219 Internal function to PciCfg2 read/write opcode to the table.
220
221 @param Marker The variable argument list to get the opcode
222 and associated attributes.
223
224 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
225 @retval EFI_SUCCESS Opcode is added.
226
227 **/
228 EFI_STATUS
229 BootScriptPciCfg2ReadWrite (
230 IN VA_LIST Marker
231 )
232 {
233 S3_BOOT_SCRIPT_LIB_WIDTH Width;
234 UINT16 Segment;
235 UINT64 Address;
236 UINT8 *Data;
237 UINT8 *DataMask;
238
239 Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
240 Address = VA_ARG (Marker, UINT64);
241 Segment = VA_ARG (Marker, UINT16);
242 Data = VA_ARG (Marker, UINT8 *);
243 DataMask = VA_ARG (Marker, UINT8 *);
244
245 return S3BootScriptSavePciCfg2ReadWrite (Width, Segment, Address, Data, DataMask);
246 }
247
248 /**
249 Internal function to add smbus execute opcode to the table.
250
251 @param Marker The variable argument list to get the opcode
252 and associated attributes.
253
254 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
255 @retval EFI_SUCCESS Opcode is added.
256
257 **/
258 EFI_STATUS
259 BootScriptSmbusExecute (
260 IN VA_LIST Marker
261 )
262 {
263 EFI_SMBUS_DEVICE_ADDRESS SlaveAddress;
264 EFI_SMBUS_DEVICE_COMMAND Command;
265 EFI_SMBUS_OPERATION Operation;
266 BOOLEAN PecCheck;
267 VOID *Buffer;
268 UINTN *DataSize;
269 UINTN SmBusAddress;
270
271 SlaveAddress.SmbusDeviceAddress = VA_ARG (Marker, UINTN);
272 Command = VA_ARG (Marker, EFI_SMBUS_DEVICE_COMMAND);
273 Operation = VA_ARG (Marker, EFI_SMBUS_OPERATION);
274 PecCheck = VA_ARG (Marker, BOOLEAN);
275 SmBusAddress = SMBUS_LIB_ADDRESS (SlaveAddress.SmbusDeviceAddress,Command,0,PecCheck);
276 DataSize = VA_ARG (Marker, UINTN *);
277 Buffer = VA_ARG (Marker, VOID *);
278
279 return S3BootScriptSaveSmbusExecute (SmBusAddress, Operation, DataSize, Buffer);
280 }
281
282 /**
283 Internal function to add stall opcode to the table.
284
285 @param Marker The variable argument list to get the opcode
286 and associated attributes.
287
288 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
289 @retval EFI_SUCCESS Opcode is added.
290
291 **/
292 EFI_STATUS
293 BootScriptStall (
294 IN VA_LIST Marker
295 )
296 {
297 UINT32 Duration;
298
299 Duration = VA_ARG (Marker, UINT32);
300
301 return S3BootScriptSaveStall (Duration);
302 }
303
304 /**
305 Internal function to add Save jmp address according to DISPATCH_OPCODE.
306 We ignore "Context" parameter.
307
308 @param Marker The variable argument list to get the opcode
309 and associated attributes.
310
311 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
312 @retval EFI_SUCCESS Opcode is added.
313
314 **/
315 EFI_STATUS
316 BootScriptDispatch (
317 IN VA_LIST Marker
318 )
319 {
320 VOID *EntryPoint;
321
322 EntryPoint = (VOID*)(UINTN)VA_ARG (Marker, EFI_PHYSICAL_ADDRESS);
323 return S3BootScriptSaveDispatch (EntryPoint);
324 }
325
326 /**
327 Internal function to add memory pool operation to the table.
328
329 @param Marker The variable argument list to get the opcode
330 and associated attributes.
331
332 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
333 @retval EFI_SUCCESS Opcode is added.
334
335 **/
336 EFI_STATUS
337 BootScriptMemPoll (
338 IN VA_LIST Marker
339 )
340 {
341 S3_BOOT_SCRIPT_LIB_WIDTH Width;
342 UINT64 Address;
343 UINT8 *BitMask;
344 UINT8 *BitValue;
345 UINTN Duration;
346 UINT64 LoopTimes;
347
348 Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
349 Address = VA_ARG (Marker, UINT64);
350 BitMask = VA_ARG (Marker, UINT8 *);
351 BitValue = VA_ARG (Marker, UINT8 *);
352 Duration = (UINTN)VA_ARG (Marker, UINT64);
353 LoopTimes = VA_ARG (Marker, UINT64);
354
355 return S3BootScriptSaveMemPoll (Width, Address, BitMask, BitValue, Duration, LoopTimes);
356 }
357
358 /**
359 Internal function to add Save jmp address according to DISPATCH_OPCODE2.
360 The "Context" parameter is not ignored.
361
362 @param Marker The variable argument list to get the opcode
363 and associated attributes.
364
365 @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
366 @retval EFI_SUCCESS Opcode is added.
367
368 **/
369 EFI_STATUS
370 BootScriptDispatch2 (
371 IN VA_LIST Marker
372 )
373 {
374 VOID *EntryPoint;
375 VOID *Context;
376
377 EntryPoint = (VOID*)(UINTN)VA_ARG (Marker, EFI_PHYSICAL_ADDRESS);
378 Context = (VOID*)(UINTN)VA_ARG (Marker, EFI_PHYSICAL_ADDRESS);
379
380 return S3BootScriptSaveDispatch2 (EntryPoint, Context);
381 }
382
383 /**
384 Internal function to add the opcode link node to the link list.
385
386 @param Marker The variable argument list to get the opcode
387 and associated attributes.
388
389 @retval EFI_OUT_OF_RESOURCES Not enought resource to complete the operations.
390 @retval EFI_SUCCESS The opcode entry is added to the link list
391 successfully.
392 **/
393 EFI_STATUS
394 BootScriptInformation (
395 IN VA_LIST Marker
396 )
397 {
398 UINT32 InformationLength;
399 EFI_PHYSICAL_ADDRESS Information;
400
401 InformationLength = VA_ARG (Marker, UINT32);
402 Information = VA_ARG (Marker, EFI_PHYSICAL_ADDRESS);
403 return S3BootScriptSaveInformation (InformationLength, (VOID*)(UINTN)Information);
404 }
405
406 /**
407 Adds a record into a specified Framework boot script table.
408
409 This function is used to store a boot script record into a given boot
410 script table. If the table specified by TableName is nonexistent in the
411 system, a new table will automatically be created and then the script record
412 will be added into the new table. A boot script table can add new script records
413 until EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is called. Currently, the only
414 meaningful table name is EFI_ACPI_S3_RESUME_SCRIPT_TABLE. This function is
415 responsible for allocating necessary memory for the script.
416
417 This function has a variable parameter list. The exact parameter list depends on
418 the OpCode that is passed into the function. If an unsupported OpCode or illegal
419 parameter list is passed in, this function returns EFI_INVALID_PARAMETER.
420 If there are not enough resources available for storing more scripts, this function returns
421 EFI_OUT_OF_RESOURCES.
422
423 @param This A pointer to the EFI_BOOT_SCRIPT_SAVE_PROTOCOL instance.
424 @param TableName Name of the script table. Currently, the only meaningful value is
425 EFI_ACPI_S3_RESUME_SCRIPT_TABLE.
426 @param OpCode The operation code (opcode) number.
427 @param ... Argument list that is specific to each opcode.
428
429 @retval EFI_SUCCESS The operation succeeded. A record was added into the
430 specified script table.
431 @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
432 If the opcode is unknow or not supported because of the PCD
433 Feature Flags.
434 @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
435
436 **/
437 EFI_STATUS
438 EFIAPI
439 BootScriptWrite (
440 IN EFI_BOOT_SCRIPT_SAVE_PROTOCOL *This,
441 IN UINT16 TableName,
442 IN UINT16 OpCode,
443 ...
444 )
445 {
446 EFI_STATUS Status;
447 VA_LIST Marker;
448
449 if (TableName != FRAMEWORK_EFI_ACPI_S3_RESUME_SCRIPT_TABLE) {
450 //
451 // Only S3 boot script is supported for now.
452 //
453 return EFI_OUT_OF_RESOURCES;
454 }
455
456 //
457 // Build script according to opcode.
458 //
459 switch (OpCode) {
460
461 case EFI_BOOT_SCRIPT_IO_WRITE_OPCODE:
462 VA_START (Marker, OpCode);
463 Status = BootScriptIoWrite (Marker);
464 VA_END (Marker);
465 break;
466
467 case EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE:
468 VA_START (Marker, OpCode);
469 Status = BootScriptIoReadWrite (Marker);
470 VA_END (Marker);
471 break;
472
473 case EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE:
474 VA_START (Marker, OpCode);
475 Status = BootScriptMemWrite (Marker);
476 VA_END (Marker);
477 break;
478
479 case EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE:
480 VA_START (Marker, OpCode);
481 Status = BootScriptMemReadWrite (Marker);
482 VA_END (Marker);
483 break;
484
485 case EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE:
486 VA_START (Marker, OpCode);
487 Status = BootScriptPciCfgWrite (Marker);
488 VA_END (Marker);
489 break;
490
491 case EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE:
492 VA_START (Marker, OpCode);
493 Status = BootScriptPciCfgReadWrite (Marker);
494 VA_END (Marker);
495 break;
496
497 case EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE:
498 VA_START (Marker, OpCode);
499 Status = BootScriptSmbusExecute (Marker);
500 VA_END (Marker);
501 break;
502
503 case EFI_BOOT_SCRIPT_STALL_OPCODE:
504 VA_START (Marker, OpCode);
505 Status = BootScriptStall (Marker);
506 VA_END (Marker);
507
508 break;
509
510 case EFI_BOOT_SCRIPT_DISPATCH_OPCODE:
511 VA_START (Marker, OpCode);
512 Status = BootScriptDispatch (Marker);
513 VA_END (Marker);
514 break;
515
516 case EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE:
517 VA_START (Marker, OpCode);
518 Status = BootScriptDispatch2 (Marker);
519 VA_END (Marker);
520 break;
521
522 case EFI_BOOT_SCRIPT_INFORMATION_OPCODE:
523 VA_START (Marker, OpCode);
524 Status = BootScriptInformation (Marker);
525 VA_END (Marker);
526 break;
527
528 case EFI_BOOT_SCRIPT_MEM_POLL_OPCODE:
529 VA_START (Marker, OpCode);
530 Status = BootScriptMemPoll (Marker);
531 VA_END (Marker);
532 break;
533
534 case EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE:
535 VA_START (Marker, OpCode);
536 Status = BootScriptPciCfg2Write (Marker);
537 VA_END (Marker);
538 break;
539
540 case EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE:
541 VA_START (Marker, OpCode);
542 Status = BootScriptPciCfg2ReadWrite (Marker);
543 VA_END (Marker);
544 break;
545
546 default:
547 Status = EFI_INVALID_PARAMETER;
548 break;
549 }
550
551 return Status;
552 }
553
554 /**
555 Closes the specified script table.
556
557 This function closes the specified boot script table and returns the base address
558 of the table. It allocates a new pool to duplicate all the boot scripts in the specified
559 table. Once this function is called, the specified table will be destroyed after it is
560 copied into the allocated pool. As a result, any attempts to add a script record into a
561 closed table will cause a new table to be created. The base address of the allocated pool
562 will be returned in Address. After using the boot script table, the caller is responsible
563 for freeing the pool that is allocated by this function. If the boot script table,
564 such as EFI_ACPI_S3_RESUME_SCRIPT_TABLE, is required to be stored in a nonperturbed
565 memory region, the caller should copy the table into the nonperturbed memory region by itself.
566
567 @param This A pointer to the EFI_BOOT_SCRIPT_SAVE_PROTOCOL instance.
568 @param TableName Name of the script table. Currently, the only meaningful value is
569 EFI_ACPI_S3_RESUME_SCRIPT_TABLE.
570 @param Address A pointer to the physical address where the table begins.
571
572 @retval EFI_SUCCESS The table was successfully returned.
573 @retval EFI_NOT_FOUND The specified table was not created previously.
574 @retval EFI_OUT_OF_RESOURCE Memory is insufficient to hold the reorganized boot script table.
575 @retval EFI_UNSUPPORTED The table type is not EFI_ACPI_S3_RESUME_SCRIPT_TABLE.
576
577 **/
578 EFI_STATUS
579 EFIAPI
580 BootScriptCloseTable (
581 IN EFI_BOOT_SCRIPT_SAVE_PROTOCOL *This,
582 IN UINT16 TableName,
583 OUT EFI_PHYSICAL_ADDRESS *Address
584 )
585 {
586 if (TableName != FRAMEWORK_EFI_ACPI_S3_RESUME_SCRIPT_TABLE) {
587 //
588 // Only S3 boot script is supported for now.
589 //
590 return EFI_NOT_FOUND;
591 }
592 *Address = (EFI_PHYSICAL_ADDRESS)(UINTN)S3BootScriptCloseTable ();
593
594 if (*Address == 0) {
595 return EFI_NOT_FOUND;
596 }
597 return EFI_SUCCESS;
598 }
599
600 /**
601 This routine is entry point of ScriptSave driver.
602
603 @param ImageHandle Handle for this drivers loaded image protocol.
604 @param SystemTable EFI system table.
605
606 @retval EFI_OUT_OF_RESOURCES No enough resource.
607 @retval EFI_SUCCESS Succesfully installed the ScriptSave driver.
608 @retval other Errors occured.
609
610 **/
611 EFI_STATUS
612 EFIAPI
613 InitializeScriptSave (
614 IN EFI_HANDLE ImageHandle,
615 IN EFI_SYSTEM_TABLE *SystemTable
616 )
617 {
618 return gBS->InstallProtocolInterface (
619 &mHandle,
620 &gEfiBootScriptSaveProtocolGuid,
621 EFI_NATIVE_INTERFACE,
622 &mS3ScriptSave
623 );
624
625 }
626