1 /** @file
2   ACPI Table Protocol Implementation
3 
4   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5   Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
6   SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 //
11 // Includes
12 //
13 #include "AcpiTable.h"
14 //
15 // The maximum number of tables that pre-allocated.
16 //
17 UINTN         mEfiAcpiMaxNumTables = EFI_ACPI_MAX_NUM_TABLES;
18 
19 //
20 // Allocation strategy to use for AllocatePages ().
21 // Runtime value depends on PcdExposedAcpiTableVersions.
22 //
23 STATIC EFI_ALLOCATE_TYPE      mAcpiTableAllocType;
24 
25 /**
26   This function adds an ACPI table to the table list.  It will detect FACS and
27   allocate the correct type of memory and properly align the table.
28 
29   @param  AcpiTableInstance         Instance of the protocol.
30   @param  Table                     Table to add.
31   @param  Checksum                  Does the table require checksumming.
32   @param  Version                   The version of the list to add the table to.
33   @param  Handle                    Pointer for returning the handle.
34 
35   @return EFI_SUCCESS               The function completed successfully.
36   @return EFI_OUT_OF_RESOURCES      Could not allocate a required resource.
37   @return EFI_ABORTED               The table is a duplicate of a table that is required
38                                     to be unique.
39 
40 **/
41 EFI_STATUS
42 AddTableToList (
43   IN EFI_ACPI_TABLE_INSTANCE              *AcpiTableInstance,
44   IN VOID                                 *Table,
45   IN BOOLEAN                              Checksum,
46   IN EFI_ACPI_TABLE_VERSION               Version,
47   OUT UINTN                               *Handle
48   );
49 
50 /**
51   This function finds and removes the table specified by the handle.
52 
53   @param  AcpiTableInstance  Instance of the protocol.
54   @param  Version            Bitmask of which versions to remove.
55   @param  Handle             Table to remove.
56 
57   @return EFI_SUCCESS    The function completed successfully.
58   @return EFI_ABORTED    An error occurred.
59   @return EFI_NOT_FOUND  Handle not found in table list.
60 
61 **/
62 EFI_STATUS
63 RemoveTableFromList (
64   IN EFI_ACPI_TABLE_INSTANCE              *AcpiTableInstance,
65   IN EFI_ACPI_TABLE_VERSION               Version,
66   IN UINTN                                Handle
67   );
68 
69 /**
70   This function calculates and updates an UINT8 checksum.
71 
72   @param  Buffer          Pointer to buffer to checksum
73   @param  Size            Number of bytes to checksum
74   @param  ChecksumOffset  Offset to place the checksum result in
75 
76   @return EFI_SUCCESS             The function completed successfully.
77 **/
78 EFI_STATUS
79 AcpiPlatformChecksum (
80   IN VOID       *Buffer,
81   IN UINTN      Size,
82   IN UINTN      ChecksumOffset
83   );
84 
85 /**
86   Checksum all versions of the common tables, RSDP, RSDT, XSDT.
87 
88   @param  AcpiTableInstance  Protocol instance private data.
89 
90   @return EFI_SUCCESS        The function completed successfully.
91 
92 **/
93 EFI_STATUS
94 ChecksumCommonTables (
95   IN OUT EFI_ACPI_TABLE_INSTANCE          *AcpiTableInstance
96   );
97 
98 //
99 // Protocol function implementations.
100 //
101 
102 /**
103   This function publishes the specified versions of the ACPI tables by
104   installing EFI configuration table entries for them.  Any combination of
105   table versions can be published.
106 
107   @param  AcpiTableInstance  Instance of the protocol.
108   @param  Version            Version(s) to publish.
109 
110   @return EFI_SUCCESS  The function completed successfully.
111   @return EFI_ABORTED  The function could not complete successfully.
112 
113 **/
114 EFI_STATUS
115 EFIAPI
PublishTables(IN EFI_ACPI_TABLE_INSTANCE * AcpiTableInstance,IN EFI_ACPI_TABLE_VERSION Version)116 PublishTables (
117   IN EFI_ACPI_TABLE_INSTANCE              *AcpiTableInstance,
118   IN EFI_ACPI_TABLE_VERSION               Version
119   )
120 {
121   EFI_STATUS                Status;
122   UINT32                    *CurrentRsdtEntry;
123   VOID                      *CurrentXsdtEntry;
124   UINT64                    Buffer64;
125 
126   //
127   // Reorder tables as some operating systems don't seem to find the
128   // FADT correctly if it is not in the first few entries
129   //
130 
131   //
132   // Add FADT as the first entry
133   //
134   if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
135     CurrentRsdtEntry  = (UINT32 *) ((UINT8 *) AcpiTableInstance->Rsdt1 + sizeof (EFI_ACPI_DESCRIPTION_HEADER));
136     *CurrentRsdtEntry = (UINT32) (UINTN) AcpiTableInstance->Fadt1;
137 
138     CurrentRsdtEntry  = (UINT32 *) ((UINT8 *) AcpiTableInstance->Rsdt3 + sizeof (EFI_ACPI_DESCRIPTION_HEADER));
139     *CurrentRsdtEntry = (UINT32) (UINTN) AcpiTableInstance->Fadt3;
140   }
141   if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
142     CurrentXsdtEntry  = (VOID *) ((UINT8 *) AcpiTableInstance->Xsdt + sizeof (EFI_ACPI_DESCRIPTION_HEADER));
143     //
144     // Add entry to XSDT, XSDT expects 64 bit pointers, but
145     // the table pointers in XSDT are not aligned on 8 byte boundary.
146     //
147     Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Fadt3;
148     CopyMem (
149       CurrentXsdtEntry,
150       &Buffer64,
151       sizeof (UINT64)
152       );
153   }
154 
155   //
156   // Do checksum again because Dsdt/Xsdt is updated.
157   //
158   ChecksumCommonTables (AcpiTableInstance);
159 
160   //
161   // Add the RSD_PTR to the system table and store that we have installed the
162   // tables.
163   //
164   if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
165     Status = gBS->InstallConfigurationTable (&gEfiAcpi10TableGuid, AcpiTableInstance->Rsdp1);
166     if (EFI_ERROR (Status)) {
167       return EFI_ABORTED;
168     }
169   }
170 
171   if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
172     Status = gBS->InstallConfigurationTable (&gEfiAcpiTableGuid, AcpiTableInstance->Rsdp3);
173     if (EFI_ERROR (Status)) {
174       return EFI_ABORTED;
175     }
176   }
177 
178   return EFI_SUCCESS;
179 }
180 
181 
182 /**
183   Installs an ACPI table into the RSDT/XSDT.
184   Note that the ACPI table should be checksumed before installing it.
185   Otherwise it will assert.
186 
187   @param  This                 Protocol instance pointer.
188   @param  AcpiTableBuffer      A pointer to a buffer containing the ACPI table to be installed.
189   @param  AcpiTableBufferSize  Specifies the size, in bytes, of the AcpiTableBuffer buffer.
190   @param  TableKey             Reurns a key to refer to the ACPI table.
191 
192   @return EFI_SUCCESS            The table was successfully inserted.
193   @return EFI_INVALID_PARAMETER  Either AcpiTableBuffer is NULL, TableKey is NULL, or AcpiTableBufferSize
194                                  and the size field embedded in the ACPI table pointed to by AcpiTableBuffer
195                                  are not in sync.
196   @return EFI_OUT_OF_RESOURCES   Insufficient resources exist to complete the request.
197   @retval EFI_ACCESS_DENIED      The table signature matches a table already
198                                  present in the system and platform policy
199                                  does not allow duplicate tables of this type.
200 
201 **/
202 EFI_STATUS
203 EFIAPI
InstallAcpiTable(IN EFI_ACPI_TABLE_PROTOCOL * This,IN VOID * AcpiTableBuffer,IN UINTN AcpiTableBufferSize,OUT UINTN * TableKey)204 InstallAcpiTable (
205   IN   EFI_ACPI_TABLE_PROTOCOL                    *This,
206   IN   VOID                                       *AcpiTableBuffer,
207   IN   UINTN                                      AcpiTableBufferSize,
208   OUT  UINTN                                      *TableKey
209   )
210 {
211   EFI_ACPI_TABLE_INSTANCE   *AcpiTableInstance;
212   EFI_STATUS                Status;
213   VOID                      *AcpiTableBufferConst;
214   EFI_ACPI_TABLE_VERSION    Version;
215 
216   //
217   // Check for invalid input parameters
218   //
219   if ((AcpiTableBuffer == NULL) || (TableKey == NULL)
220      || (((EFI_ACPI_DESCRIPTION_HEADER *) AcpiTableBuffer)->Length != AcpiTableBufferSize)) {
221     return EFI_INVALID_PARAMETER;
222   }
223 
224   Version = PcdGet32 (PcdAcpiExposedTableVersions);
225 
226   //
227   // Get the instance of the ACPI table protocol
228   //
229   AcpiTableInstance = EFI_ACPI_TABLE_INSTANCE_FROM_THIS (This);
230 
231   //
232   // Install the ACPI table
233   //
234   AcpiTableBufferConst = AllocateCopyPool (AcpiTableBufferSize,AcpiTableBuffer);
235   *TableKey = 0;
236   Status = AddTableToList (
237              AcpiTableInstance,
238              AcpiTableBufferConst,
239              TRUE,
240              Version,
241              TableKey
242              );
243   if (!EFI_ERROR (Status)) {
244     Status = PublishTables (
245                AcpiTableInstance,
246                Version
247                );
248   }
249   FreePool (AcpiTableBufferConst);
250 
251   //
252   // Add a new table successfully, notify registed callback
253   //
254   if (FeaturePcdGet (PcdInstallAcpiSdtProtocol)) {
255     if (!EFI_ERROR (Status)) {
256       SdtNotifyAcpiList (
257         AcpiTableInstance,
258         Version,
259         *TableKey
260         );
261     }
262   }
263 
264   return Status;
265 }
266 
267 
268 /**
269   Removes an ACPI table from the RSDT/XSDT.
270 
271   @param  This      Protocol instance pointer.
272   @param  TableKey  Specifies the table to uninstall.  The key was returned from InstallAcpiTable().
273 
274   @return EFI_SUCCESS    The table was successfully uninstalled.
275   @return EFI_NOT_FOUND  TableKey does not refer to a valid key for a table entry.
276 
277 **/
278 EFI_STATUS
279 EFIAPI
UninstallAcpiTable(IN EFI_ACPI_TABLE_PROTOCOL * This,IN UINTN TableKey)280 UninstallAcpiTable (
281   IN  EFI_ACPI_TABLE_PROTOCOL                    *This,
282   IN  UINTN                                      TableKey
283   )
284 {
285   EFI_ACPI_TABLE_INSTANCE   *AcpiTableInstance;
286   EFI_STATUS                Status;
287   EFI_ACPI_TABLE_VERSION    Version;
288 
289   //
290   // Get the instance of the ACPI table protocol
291   //
292   AcpiTableInstance = EFI_ACPI_TABLE_INSTANCE_FROM_THIS (This);
293 
294   Version = PcdGet32 (PcdAcpiExposedTableVersions);
295 
296   //
297   // Uninstall the ACPI table
298   //
299   Status = RemoveTableFromList (
300              AcpiTableInstance,
301              Version,
302              TableKey
303              );
304   if (!EFI_ERROR (Status)) {
305     Status = PublishTables (
306                AcpiTableInstance,
307                Version
308                );
309   }
310 
311   if (EFI_ERROR (Status)) {
312     return EFI_NOT_FOUND;
313   } else {
314     return EFI_SUCCESS;
315   }
316 }
317 
318 /**
319   If the number of APCI tables exceeds the preallocated max table number, enlarge the table buffer.
320 
321   @param  AcpiTableInstance       ACPI table protocol instance data structure.
322 
323   @return EFI_SUCCESS             reallocate the table beffer successfully.
324   @return EFI_OUT_OF_RESOURCES    Unable to allocate required resources.
325 
326 **/
327 EFI_STATUS
ReallocateAcpiTableBuffer(IN EFI_ACPI_TABLE_INSTANCE * AcpiTableInstance)328 ReallocateAcpiTableBuffer (
329   IN EFI_ACPI_TABLE_INSTANCE                   *AcpiTableInstance
330   )
331 {
332   UINTN                    NewMaxTableNumber;
333   UINTN                    TotalSize;
334   UINT8                    *Pointer;
335   EFI_PHYSICAL_ADDRESS     PageAddress;
336   EFI_ACPI_TABLE_INSTANCE  TempPrivateData;
337   EFI_STATUS               Status;
338   UINT64                   CurrentData;
339 
340   CopyMem (&TempPrivateData, AcpiTableInstance, sizeof (EFI_ACPI_TABLE_INSTANCE));
341   //
342   // Enlarge the max table number from mEfiAcpiMaxNumTables to mEfiAcpiMaxNumTables + EFI_ACPI_MAX_NUM_TABLES
343   //
344   NewMaxTableNumber = mEfiAcpiMaxNumTables + EFI_ACPI_MAX_NUM_TABLES;
345   //
346   // Create RSDT, XSDT structures and allocate buffers.
347   //
348   TotalSize = sizeof (EFI_ACPI_DESCRIPTION_HEADER) +         // for ACPI 2.0/3.0 XSDT
349               NewMaxTableNumber * sizeof (UINT64);
350 
351   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
352     TotalSize += sizeof (EFI_ACPI_DESCRIPTION_HEADER) +      // for ACPI 1.0 RSDT
353                  NewMaxTableNumber * sizeof (UINT32) +
354                  sizeof (EFI_ACPI_DESCRIPTION_HEADER) +      // for ACPI 2.0/3.0 RSDT
355                  NewMaxTableNumber * sizeof (UINT32);
356   }
357 
358   if (mAcpiTableAllocType != AllocateAnyPages) {
359     //
360     // Allocate memory in the lower 32 bit of address range for
361     // compatibility with ACPI 1.0 OS.
362     //
363     // This is done because ACPI 1.0 pointers are 32 bit values.
364     // ACPI 2.0 OS and all 64 bit OS must use the 64 bit ACPI table addresses.
365     // There is no architectural reason these should be below 4GB, it is purely
366     // for convenience of implementation that we force memory below 4GB.
367     //
368     PageAddress = 0xFFFFFFFF;
369     Status = gBS->AllocatePages (
370                     mAcpiTableAllocType,
371                     EfiACPIReclaimMemory,
372                     EFI_SIZE_TO_PAGES (TotalSize),
373                     &PageAddress
374                     );
375   } else {
376     Status = gBS->AllocatePool (
377                     EfiACPIReclaimMemory,
378                     TotalSize,
379                     (VOID **)&Pointer
380                     );
381   }
382 
383   if (EFI_ERROR (Status)) {
384     return EFI_OUT_OF_RESOURCES;
385   }
386 
387   if (mAcpiTableAllocType != AllocateAnyPages) {
388     Pointer = (UINT8 *)(UINTN)PageAddress;
389   }
390 
391   ZeroMem (Pointer, TotalSize);
392 
393   AcpiTableInstance->Rsdt1 = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
394   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
395     Pointer += (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + NewMaxTableNumber * sizeof (UINT32));
396     AcpiTableInstance->Rsdt3 = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
397     Pointer += (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + NewMaxTableNumber * sizeof (UINT32));
398   }
399   AcpiTableInstance->Xsdt = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
400 
401   //
402   // Update RSDP to point to the new Rsdt and Xsdt address.
403   //
404   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
405     AcpiTableInstance->Rsdp1->RsdtAddress = (UINT32) (UINTN) AcpiTableInstance->Rsdt1;
406     AcpiTableInstance->Rsdp3->RsdtAddress = (UINT32) (UINTN) AcpiTableInstance->Rsdt3;
407   }
408   CurrentData = (UINT64) (UINTN) AcpiTableInstance->Xsdt;
409   CopyMem (&AcpiTableInstance->Rsdp3->XsdtAddress, &CurrentData, sizeof (UINT64));
410 
411   //
412   // copy the original Rsdt1, Rsdt3 and Xsdt structure to new buffer
413   //
414   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
415     CopyMem (AcpiTableInstance->Rsdt1, TempPrivateData.Rsdt1, (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + mEfiAcpiMaxNumTables * sizeof (UINT32)));
416     CopyMem (AcpiTableInstance->Rsdt3, TempPrivateData.Rsdt3, (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + mEfiAcpiMaxNumTables * sizeof (UINT32)));
417   }
418   CopyMem (AcpiTableInstance->Xsdt, TempPrivateData.Xsdt, (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + mEfiAcpiMaxNumTables * sizeof (UINT64)));
419 
420   if (mAcpiTableAllocType != AllocateAnyPages) {
421     //
422     // Calculate orignal ACPI table buffer size
423     //
424     TotalSize = sizeof (EFI_ACPI_DESCRIPTION_HEADER) +         // for ACPI 2.0/3.0 XSDT
425                 mEfiAcpiMaxNumTables * sizeof (UINT64);
426 
427     if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
428       TotalSize += sizeof (EFI_ACPI_DESCRIPTION_HEADER) +         // for ACPI 1.0 RSDT
429                    mEfiAcpiMaxNumTables * sizeof (UINT32) +
430                    sizeof (EFI_ACPI_DESCRIPTION_HEADER) +         // for ACPI 2.0/3.0 RSDT
431                    mEfiAcpiMaxNumTables * sizeof (UINT32);
432     }
433 
434     gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)TempPrivateData.Rsdt1,
435            EFI_SIZE_TO_PAGES (TotalSize));
436   } else {
437     gBS->FreePool (TempPrivateData.Rsdt1);
438   }
439 
440   //
441   // Update the Max ACPI table number
442   //
443   mEfiAcpiMaxNumTables = NewMaxTableNumber;
444   return EFI_SUCCESS;
445 }
446 
447 /**
448   Free the memory associated with the provided EFI_ACPI_TABLE_LIST instance.
449 
450   @param  TableEntry                EFI_ACPI_TABLE_LIST instance pointer
451 
452 **/
453 STATIC
454 VOID
FreeTableMemory(EFI_ACPI_TABLE_LIST * TableEntry)455 FreeTableMemory (
456   EFI_ACPI_TABLE_LIST   *TableEntry
457   )
458 {
459   if (TableEntry->PoolAllocation) {
460     gBS->FreePool (TableEntry->Table);
461   } else {
462     gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)TableEntry->Table,
463            EFI_SIZE_TO_PAGES (TableEntry->TableSize));
464   }
465 }
466 
467 /**
468   This function adds an ACPI table to the table list.  It will detect FACS and
469   allocate the correct type of memory and properly align the table.
470 
471   @param  AcpiTableInstance         Instance of the protocol.
472   @param  Table                     Table to add.
473   @param  Checksum                  Does the table require checksumming.
474   @param  Version                   The version of the list to add the table to.
475   @param  Handle                    Pointer for returning the handle.
476 
477   @return EFI_SUCCESS               The function completed successfully.
478   @return EFI_OUT_OF_RESOURCES      Could not allocate a required resource.
479   @retval EFI_ACCESS_DENIED         The table signature matches a table already
480                                     present in the system and platform policy
481                                     does not allow duplicate tables of this type.
482 
483 **/
484 EFI_STATUS
AddTableToList(IN EFI_ACPI_TABLE_INSTANCE * AcpiTableInstance,IN VOID * Table,IN BOOLEAN Checksum,IN EFI_ACPI_TABLE_VERSION Version,OUT UINTN * Handle)485 AddTableToList (
486   IN EFI_ACPI_TABLE_INSTANCE              *AcpiTableInstance,
487   IN VOID                                 *Table,
488   IN BOOLEAN                              Checksum,
489   IN EFI_ACPI_TABLE_VERSION               Version,
490   OUT UINTN                               *Handle
491   )
492 {
493   EFI_STATUS            Status;
494   EFI_ACPI_TABLE_LIST   *CurrentTableList;
495   UINT32                CurrentTableSignature;
496   UINT32                CurrentTableSize;
497   UINT32                *CurrentRsdtEntry;
498   VOID                  *CurrentXsdtEntry;
499   EFI_PHYSICAL_ADDRESS  AllocPhysAddress;
500   UINT64                Buffer64;
501   BOOLEAN               AddToRsdt;
502 
503   //
504   // Check for invalid input parameters
505   //
506   ASSERT (AcpiTableInstance);
507   ASSERT (Table);
508   ASSERT (Handle);
509 
510   //
511   // Init locals
512   //
513   AddToRsdt = TRUE;
514 
515   //
516   // Create a new list entry
517   //
518   CurrentTableList = AllocatePool (sizeof (EFI_ACPI_TABLE_LIST));
519   ASSERT (CurrentTableList);
520 
521   //
522   // Determine table type and size
523   //
524   CurrentTableSignature = ((EFI_ACPI_COMMON_HEADER *) Table)->Signature;
525   CurrentTableSize      = ((EFI_ACPI_COMMON_HEADER *) Table)->Length;
526 
527   //
528   // Allocate a buffer for the table.  All tables are allocated in the lower 32 bits of address space
529   // for backwards compatibility with ACPI 1.0 OS.
530   //
531   // This is done because ACPI 1.0 pointers are 32 bit values.
532   // ACPI 2.0 OS and all 64 bit OS must use the 64 bit ACPI table addresses.
533   // There is no architectural reason these should be below 4GB, it is purely
534   // for convenience of implementation that we force memory below 4GB.
535   //
536   AllocPhysAddress                  = 0xFFFFFFFF;
537   CurrentTableList->TableSize       = CurrentTableSize;
538   CurrentTableList->PoolAllocation  = FALSE;
539 
540   //
541   // Allocation memory type depends on the type of the table
542   //
543   if ((CurrentTableSignature == EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) ||
544       (CurrentTableSignature == EFI_ACPI_4_0_UEFI_ACPI_DATA_TABLE_SIGNATURE)) {
545     //
546     // Allocate memory for the FACS.  This structure must be aligned
547     // on a 64 byte boundary and must be ACPI NVS memory.
548     // Using AllocatePages should ensure that it is always aligned.
549     // Do not change signature for new ACPI version because they are same.
550     //
551     // UEFI table also need to be in ACPI NVS memory, because some data field
552     // could be updated by OS present agent. For example, BufferPtrAddress in
553     // SMM communication ACPI table.
554     //
555     ASSERT ((EFI_PAGE_SIZE % 64) == 0);
556     Status = gBS->AllocatePages (
557                     AllocateMaxAddress,
558                     EfiACPIMemoryNVS,
559                     EFI_SIZE_TO_PAGES (CurrentTableList->TableSize),
560                     &AllocPhysAddress
561                     );
562   } else if (mAcpiTableAllocType == AllocateAnyPages) {
563     //
564     // If there is no allocation limit, there is also no need to use page
565     // based allocations for ACPI tables, which may be wasteful on platforms
566     // such as AArch64 that allocate multiples of 64 KB
567     //
568     Status = gBS->AllocatePool (
569                     EfiACPIReclaimMemory,
570                     CurrentTableList->TableSize,
571                     (VOID **)&CurrentTableList->Table
572                     );
573     CurrentTableList->PoolAllocation = TRUE;
574   } else {
575     //
576     // All other tables are ACPI reclaim memory, no alignment requirements.
577     //
578     Status = gBS->AllocatePages (
579                     mAcpiTableAllocType,
580                     EfiACPIReclaimMemory,
581                     EFI_SIZE_TO_PAGES (CurrentTableList->TableSize),
582                     &AllocPhysAddress
583                     );
584     CurrentTableList->Table = (EFI_ACPI_COMMON_HEADER *)(UINTN)AllocPhysAddress;
585   }
586   //
587   // Check return value from memory alloc.
588   //
589   if (EFI_ERROR (Status)) {
590     gBS->FreePool (CurrentTableList);
591     return EFI_OUT_OF_RESOURCES;
592   }
593 
594   if (!CurrentTableList->PoolAllocation) {
595     CurrentTableList->Table = (EFI_ACPI_COMMON_HEADER *)(UINTN)AllocPhysAddress;
596   }
597 
598   //
599   // Initialize the table contents
600   //
601   CurrentTableList->Signature = EFI_ACPI_TABLE_LIST_SIGNATURE;
602   CopyMem (CurrentTableList->Table, Table, CurrentTableSize);
603   CurrentTableList->Handle  = AcpiTableInstance->CurrentHandle++;
604   *Handle                   = CurrentTableList->Handle;
605   CurrentTableList->Version = Version;
606 
607   //
608   // Update internal pointers if this is a required table.  If it is a required
609   // table and a table of that type already exists, return an error.
610   //
611   // Calculate the checksum if the table is not FACS.
612   //
613   switch (CurrentTableSignature) {
614 
615   case EFI_ACPI_1_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE:
616     //
617     // We don't add the FADT in the standard way because some
618     // OS expect the FADT to be early in the table list.
619     // So we always add it as the first element in the list.
620     //
621     AddToRsdt = FALSE;
622 
623     //
624     // Check that the table has not been previously added.
625     //
626     if (((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0 && AcpiTableInstance->Fadt1 != NULL) ||
627         ((Version & ACPI_TABLE_VERSION_GTE_2_0)  != 0 && AcpiTableInstance->Fadt3 != NULL)
628         ) {
629       FreeTableMemory (CurrentTableList);
630       gBS->FreePool (CurrentTableList);
631       return EFI_ACCESS_DENIED;
632     }
633     //
634     // Add the table to the appropriate table version
635     //
636     if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
637       //
638       // Save a pointer to the table
639       //
640       AcpiTableInstance->Fadt1 = (EFI_ACPI_1_0_FIXED_ACPI_DESCRIPTION_TABLE *) CurrentTableList->Table;
641 
642       //
643       // Update pointers in FADT.  If tables don't exist this will put NULL pointers there.
644       //
645       AcpiTableInstance->Fadt1->FirmwareCtrl  = (UINT32) (UINTN) AcpiTableInstance->Facs1;
646       AcpiTableInstance->Fadt1->Dsdt          = (UINT32) (UINTN) AcpiTableInstance->Dsdt1;
647 
648       //
649       // RSDP OEM information is updated to match the FADT OEM information
650       //
651       CopyMem (
652         &AcpiTableInstance->Rsdp1->OemId,
653         &AcpiTableInstance->Fadt1->Header.OemId,
654         6
655         );
656 
657       //
658       // RSDT OEM information is updated to match the FADT OEM information.
659       //
660       CopyMem (
661         &AcpiTableInstance->Rsdt1->OemId,
662         &AcpiTableInstance->Fadt1->Header.OemId,
663         6
664         );
665 
666       CopyMem (
667         &AcpiTableInstance->Rsdt1->OemTableId,
668         &AcpiTableInstance->Fadt1->Header.OemTableId,
669         sizeof (UINT64)
670         );
671       AcpiTableInstance->Rsdt1->OemRevision = AcpiTableInstance->Fadt1->Header.OemRevision;
672     }
673 
674     if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
675       //
676       // Save a pointer to the table
677       //
678       AcpiTableInstance->Fadt3 = (EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE *) CurrentTableList->Table;
679 
680       //
681       // Update pointers in FADT.  If tables don't exist this will put NULL pointers there.
682       // Note: If the FIRMWARE_CTRL is non-zero, then X_FIRMWARE_CTRL must be zero, and
683       // vice-versa.
684       //
685       if ((UINT64)(UINTN)AcpiTableInstance->Facs3 < BASE_4GB) {
686         AcpiTableInstance->Fadt3->FirmwareCtrl  = (UINT32) (UINTN) AcpiTableInstance->Facs3;
687         ZeroMem (&AcpiTableInstance->Fadt3->XFirmwareCtrl, sizeof (UINT64));
688       } else {
689         Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Facs3;
690         CopyMem (
691           &AcpiTableInstance->Fadt3->XFirmwareCtrl,
692           &Buffer64,
693           sizeof (UINT64)
694           );
695         AcpiTableInstance->Fadt3->FirmwareCtrl = 0;
696       }
697       if ((UINT64)(UINTN)AcpiTableInstance->Dsdt3 < BASE_4GB) {
698         AcpiTableInstance->Fadt3->Dsdt = (UINT32) (UINTN) AcpiTableInstance->Dsdt3;
699         //
700         // Comment block "the caller installs the tables in "DSDT, FADT" order"
701         // The below comments are also in "the caller installs the tables in "FADT, DSDT" order" comment block.
702         //
703         // The ACPI specification, up to and including revision 5.1 Errata A,
704         // allows the DSDT and X_DSDT fields to be both set in the FADT.
705         // (Obviously, this only makes sense if the DSDT address is representable in 4 bytes.)
706         // Starting with 5.1 Errata B, specifically for Mantis 1393 <https://mantis.uefi.org/mantis/view.php?id=1393>,
707         // the spec requires at most one of DSDT and X_DSDT fields to be set to a nonzero value,
708         // but strangely an exception is 6.0 that has no this requirement.
709         //
710         // Here we do not make the DSDT and X_DSDT fields mutual exclusion conditionally
711         // by checking FADT revision, but always set both DSDT and X_DSDT fields in the FADT
712         // to have better compatibility as some OS may have assumption to only consume X_DSDT
713         // field even the DSDT address is < 4G.
714         //
715         Buffer64 = AcpiTableInstance->Fadt3->Dsdt;
716       } else {
717         AcpiTableInstance->Fadt3->Dsdt = 0;
718         Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Dsdt3;
719       }
720       CopyMem (&AcpiTableInstance->Fadt3->XDsdt, &Buffer64, sizeof (UINT64));
721 
722       //
723       // RSDP OEM information is updated to match the FADT OEM information
724       //
725       CopyMem (
726         &AcpiTableInstance->Rsdp3->OemId,
727         &AcpiTableInstance->Fadt3->Header.OemId,
728         6
729         );
730 
731       if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
732         //
733         // RSDT OEM information is updated to match FADT OEM information.
734         //
735         CopyMem (
736           &AcpiTableInstance->Rsdt3->OemId,
737           &AcpiTableInstance->Fadt3->Header.OemId,
738           6
739           );
740         CopyMem (
741           &AcpiTableInstance->Rsdt3->OemTableId,
742           &AcpiTableInstance->Fadt3->Header.OemTableId,
743           sizeof (UINT64)
744           );
745         AcpiTableInstance->Rsdt3->OemRevision = AcpiTableInstance->Fadt3->Header.OemRevision;
746       }
747 
748       //
749       // XSDT OEM information is updated to match FADT OEM information.
750       //
751       CopyMem (
752         &AcpiTableInstance->Xsdt->OemId,
753         &AcpiTableInstance->Fadt3->Header.OemId,
754         6
755         );
756       CopyMem (
757         &AcpiTableInstance->Xsdt->OemTableId,
758         &AcpiTableInstance->Fadt3->Header.OemTableId,
759         sizeof (UINT64)
760         );
761       AcpiTableInstance->Xsdt->OemRevision = AcpiTableInstance->Fadt3->Header.OemRevision;
762     }
763     //
764     // Checksum the table
765     //
766     if (Checksum) {
767       AcpiPlatformChecksum (
768         CurrentTableList->Table,
769         CurrentTableList->Table->Length,
770         OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
771         Checksum)
772         );
773     }
774     break;
775 
776   case EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE:
777     //
778     // Check that the table has not been previously added.
779     //
780     if (((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0 && AcpiTableInstance->Facs1 != NULL) ||
781         ((Version & ACPI_TABLE_VERSION_GTE_2_0)  != 0 && AcpiTableInstance->Facs3 != NULL)
782         ) {
783       FreeTableMemory (CurrentTableList);
784       gBS->FreePool (CurrentTableList);
785       return EFI_ACCESS_DENIED;
786     }
787     //
788     // FACS is referenced by FADT and is not part of RSDT
789     //
790     AddToRsdt = FALSE;
791 
792     //
793     // Add the table to the appropriate table version
794     //
795     if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
796       //
797       // Save a pointer to the table
798       //
799       AcpiTableInstance->Facs1 = (EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) CurrentTableList->Table;
800 
801       //
802       // If FADT already exists, update table pointers.
803       //
804       if (AcpiTableInstance->Fadt1 != NULL) {
805         AcpiTableInstance->Fadt1->FirmwareCtrl = (UINT32) (UINTN) AcpiTableInstance->Facs1;
806 
807         //
808         // Checksum FADT table
809         //
810         AcpiPlatformChecksum (
811           AcpiTableInstance->Fadt1,
812           AcpiTableInstance->Fadt1->Header.Length,
813           OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
814           Checksum)
815           );
816       }
817     }
818 
819     if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
820       //
821       // Save a pointer to the table
822       //
823       AcpiTableInstance->Facs3 = (EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) CurrentTableList->Table;
824 
825       //
826       // If FADT already exists, update table pointers.
827       //
828       if (AcpiTableInstance->Fadt3 != NULL) {
829         //
830         // Note: If the FIRMWARE_CTRL is non-zero, then X_FIRMWARE_CTRL must be zero, and
831         // vice-versa.
832         //
833         if ((UINT64)(UINTN)AcpiTableInstance->Facs3 < BASE_4GB) {
834           AcpiTableInstance->Fadt3->FirmwareCtrl  = (UINT32) (UINTN) AcpiTableInstance->Facs3;
835           ZeroMem (&AcpiTableInstance->Fadt3->XFirmwareCtrl, sizeof (UINT64));
836         } else {
837           Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Facs3;
838           CopyMem (
839             &AcpiTableInstance->Fadt3->XFirmwareCtrl,
840             &Buffer64,
841             sizeof (UINT64)
842             );
843           AcpiTableInstance->Fadt3->FirmwareCtrl = 0;
844         }
845 
846         //
847         // Checksum FADT table
848         //
849         AcpiPlatformChecksum (
850           AcpiTableInstance->Fadt3,
851           AcpiTableInstance->Fadt3->Header.Length,
852           OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
853           Checksum)
854           );
855       }
856     }
857 
858     break;
859 
860   case EFI_ACPI_1_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE:
861     //
862     // Check that the table has not been previously added.
863     //
864     if (((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0 && AcpiTableInstance->Dsdt1 != NULL) ||
865         ((Version & ACPI_TABLE_VERSION_GTE_2_0)  != 0 && AcpiTableInstance->Dsdt3 != NULL)
866         ) {
867       FreeTableMemory (CurrentTableList);
868       gBS->FreePool (CurrentTableList);
869       return EFI_ACCESS_DENIED;
870     }
871     //
872     // DSDT is referenced by FADT and is not part of RSDT
873     //
874     AddToRsdt = FALSE;
875 
876     //
877     // Add the table to the appropriate table version
878     //
879     if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
880       //
881       // Save a pointer to the table
882       //
883       AcpiTableInstance->Dsdt1 = (EFI_ACPI_DESCRIPTION_HEADER *) CurrentTableList->Table;
884 
885       //
886       // If FADT already exists, update table pointers.
887       //
888       if (AcpiTableInstance->Fadt1 != NULL) {
889         AcpiTableInstance->Fadt1->Dsdt = (UINT32) (UINTN) AcpiTableInstance->Dsdt1;
890 
891         //
892         // Checksum FADT table
893         //
894         AcpiPlatformChecksum (
895           AcpiTableInstance->Fadt1,
896           AcpiTableInstance->Fadt1->Header.Length,
897           OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
898           Checksum)
899           );
900       }
901     }
902 
903     if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
904       //
905       // Save a pointer to the table
906       //
907       AcpiTableInstance->Dsdt3 = (EFI_ACPI_DESCRIPTION_HEADER *) CurrentTableList->Table;
908 
909       //
910       // If FADT already exists, update table pointers.
911       //
912       if (AcpiTableInstance->Fadt3 != NULL) {
913         if ((UINT64)(UINTN)AcpiTableInstance->Dsdt3 < BASE_4GB) {
914           AcpiTableInstance->Fadt3->Dsdt = (UINT32) (UINTN) AcpiTableInstance->Dsdt3;
915           //
916           // Comment block "the caller installs the tables in "FADT, DSDT" order"
917           // The below comments are also in "the caller installs the tables in "DSDT, FADT" order" comment block.
918           //
919           // The ACPI specification, up to and including revision 5.1 Errata A,
920           // allows the DSDT and X_DSDT fields to be both set in the FADT.
921           // (Obviously, this only makes sense if the DSDT address is representable in 4 bytes.)
922           // Starting with 5.1 Errata B, specifically for Mantis 1393 <https://mantis.uefi.org/mantis/view.php?id=1393>,
923           // the spec requires at most one of DSDT and X_DSDT fields to be set to a nonzero value,
924           // but strangely an exception is 6.0 that has no this requirement.
925           //
926           // Here we do not make the DSDT and X_DSDT fields mutual exclusion conditionally
927           // by checking FADT revision, but always set both DSDT and X_DSDT fields in the FADT
928           // to have better compatibility as some OS may have assumption to only consume X_DSDT
929           // field even the DSDT address is < 4G.
930           //
931           Buffer64 = AcpiTableInstance->Fadt3->Dsdt;
932         } else {
933           AcpiTableInstance->Fadt3->Dsdt = 0;
934           Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Dsdt3;
935         }
936         CopyMem (&AcpiTableInstance->Fadt3->XDsdt, &Buffer64, sizeof (UINT64));
937 
938         //
939         // Checksum FADT table
940         //
941         AcpiPlatformChecksum (
942           AcpiTableInstance->Fadt3,
943           AcpiTableInstance->Fadt3->Header.Length,
944           OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
945           Checksum)
946           );
947       }
948     }
949     //
950     // Checksum the table
951     //
952     if (Checksum) {
953       AcpiPlatformChecksum (
954         CurrentTableList->Table,
955         CurrentTableList->Table->Length,
956         OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
957         Checksum)
958         );
959     }
960     break;
961 
962   default:
963     //
964     // Checksum the table
965     //
966     if (Checksum) {
967       AcpiPlatformChecksum (
968         CurrentTableList->Table,
969         CurrentTableList->Table->Length,
970         OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
971         Checksum)
972         );
973     }
974     break;
975   }
976   //
977   // Add the table to the current list of tables
978   //
979   InsertTailList (&AcpiTableInstance->TableList, &CurrentTableList->Link);
980 
981   //
982   // Add the table to RSDT and/or XSDT table entry lists.
983   //
984   //
985   // Add to ACPI 1.0b table tree
986   //
987   if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
988     if (AddToRsdt) {
989       //
990       // If the table number exceed the gEfiAcpiMaxNumTables, enlarge the table buffer
991       //
992       if (AcpiTableInstance->NumberOfTableEntries1 >= mEfiAcpiMaxNumTables) {
993         Status = ReallocateAcpiTableBuffer (AcpiTableInstance);
994         ASSERT_EFI_ERROR (Status);
995       }
996       CurrentRsdtEntry = (UINT32 *)
997         (
998           (UINT8 *) AcpiTableInstance->Rsdt1 +
999           sizeof (EFI_ACPI_DESCRIPTION_HEADER) +
1000           AcpiTableInstance->NumberOfTableEntries1 *
1001           sizeof (UINT32)
1002         );
1003 
1004       //
1005       // Add entry to the RSDT unless its the FACS or DSDT
1006       //
1007       *CurrentRsdtEntry = (UINT32) (UINTN) CurrentTableList->Table;
1008 
1009       //
1010       // Update RSDT length
1011       //
1012       AcpiTableInstance->Rsdt1->Length = AcpiTableInstance->Rsdt1->Length + sizeof (UINT32);
1013 
1014       AcpiTableInstance->NumberOfTableEntries1++;
1015     }
1016   }
1017   //
1018   // Add to ACPI 2.0/3.0  table tree
1019   //
1020   if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
1021     if (AddToRsdt) {
1022       //
1023       // If the table number exceed the gEfiAcpiMaxNumTables, enlarge the table buffer
1024       //
1025       if (AcpiTableInstance->NumberOfTableEntries3 >= mEfiAcpiMaxNumTables) {
1026         Status = ReallocateAcpiTableBuffer (AcpiTableInstance);
1027         ASSERT_EFI_ERROR (Status);
1028       }
1029 
1030       if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1031         //
1032         // At this time, it is assumed that RSDT and XSDT maintain parallel lists of tables.
1033         // If it becomes necessary to maintain separate table lists, changes will be required.
1034         //
1035         CurrentRsdtEntry = (UINT32 *)
1036          (
1037            (UINT8 *) AcpiTableInstance->Rsdt3 +
1038            sizeof (EFI_ACPI_DESCRIPTION_HEADER) +
1039            AcpiTableInstance->NumberOfTableEntries3 *
1040            sizeof (UINT32)
1041          );
1042 
1043         //
1044         // Add entry to the RSDT
1045         //
1046         *CurrentRsdtEntry = (UINT32) (UINTN) CurrentTableList->Table;
1047 
1048         //
1049         // Update RSDT length
1050         //
1051         AcpiTableInstance->Rsdt3->Length = AcpiTableInstance->Rsdt3->Length + sizeof (UINT32);
1052       }
1053 
1054       //
1055       // This pointer must not be directly dereferenced as the XSDT entries may not
1056       // be 64 bit aligned resulting in a possible fault.  Use CopyMem to update.
1057       //
1058       CurrentXsdtEntry = (VOID *)
1059         (
1060           (UINT8 *) AcpiTableInstance->Xsdt +
1061           sizeof (EFI_ACPI_DESCRIPTION_HEADER) +
1062           AcpiTableInstance->NumberOfTableEntries3 *
1063           sizeof (UINT64)
1064         );
1065 
1066       //
1067       // Add entry to XSDT, XSDT expects 64 bit pointers, but
1068       // the table pointers in XSDT are not aligned on 8 byte boundary.
1069       //
1070       Buffer64 = (UINT64) (UINTN) CurrentTableList->Table;
1071       CopyMem (
1072         CurrentXsdtEntry,
1073         &Buffer64,
1074         sizeof (UINT64)
1075         );
1076 
1077       //
1078       // Update length
1079       //
1080       AcpiTableInstance->Xsdt->Length = AcpiTableInstance->Xsdt->Length + sizeof (UINT64);
1081 
1082       AcpiTableInstance->NumberOfTableEntries3++;
1083     }
1084   }
1085 
1086   ChecksumCommonTables (AcpiTableInstance);
1087   return EFI_SUCCESS;
1088 }
1089 
1090 
1091 /**
1092   This function finds the table specified by the handle and returns a pointer to it.
1093   If the handle is not found, EFI_NOT_FOUND is returned and the contents of Table are
1094   undefined.
1095 
1096   @param  Handle      Table to find.
1097   @param  TableList   Table list to search
1098   @param  Table       Pointer to table found.
1099 
1100   @return EFI_SUCCESS    The function completed successfully.
1101   @return EFI_NOT_FOUND  No table found matching the handle specified.
1102 
1103 **/
1104 EFI_STATUS
FindTableByHandle(IN UINTN Handle,IN LIST_ENTRY * TableList,OUT EFI_ACPI_TABLE_LIST ** Table)1105 FindTableByHandle (
1106   IN UINTN                                Handle,
1107   IN LIST_ENTRY                       *TableList,
1108   OUT EFI_ACPI_TABLE_LIST                 **Table
1109   )
1110 {
1111   LIST_ENTRY      *CurrentLink;
1112   EFI_ACPI_TABLE_LIST *CurrentTable;
1113 
1114   //
1115   // Check for invalid input parameters
1116   //
1117   ASSERT (Table);
1118 
1119   //
1120   // Find the table
1121   //
1122   CurrentLink = TableList->ForwardLink;
1123 
1124   while (CurrentLink != TableList) {
1125     CurrentTable = EFI_ACPI_TABLE_LIST_FROM_LINK (CurrentLink);
1126     if (CurrentTable->Handle == Handle) {
1127       //
1128       // Found handle, so return this table.
1129       //
1130       *Table = CurrentTable;
1131       return EFI_SUCCESS;
1132     }
1133 
1134     CurrentLink = CurrentLink->ForwardLink;
1135   }
1136   //
1137   // Table not found
1138   //
1139   return EFI_NOT_FOUND;
1140 }
1141 
1142 
1143 /**
1144   This function removes a basic table from the RSDT and/or XSDT.
1145   For Acpi 1.0 tables, pass in the Rsdt.
1146   For Acpi 2.0 tables, pass in both Rsdt and Xsdt.
1147 
1148   @param  Table                 Pointer to table found.
1149   @param  NumberOfTableEntries  Current number of table entries in the RSDT/XSDT
1150   @param  Rsdt                  Pointer to the RSDT to remove from
1151   @param  Xsdt                  Pointer to the Xsdt to remove from
1152 
1153   @return EFI_SUCCESS            The function completed successfully.
1154   @return EFI_INVALID_PARAMETER  The table was not found in both Rsdt and Xsdt.
1155 
1156 **/
1157 EFI_STATUS
RemoveTableFromRsdt(IN OUT EFI_ACPI_TABLE_LIST * Table,IN OUT UINTN * NumberOfTableEntries,IN OUT EFI_ACPI_DESCRIPTION_HEADER * Rsdt OPTIONAL,IN OUT EFI_ACPI_DESCRIPTION_HEADER * Xsdt OPTIONAL)1158 RemoveTableFromRsdt (
1159   IN OUT EFI_ACPI_TABLE_LIST              * Table,
1160   IN OUT UINTN                            *NumberOfTableEntries,
1161   IN OUT EFI_ACPI_DESCRIPTION_HEADER      * Rsdt OPTIONAL,
1162   IN OUT EFI_ACPI_DESCRIPTION_HEADER      * Xsdt OPTIONAL
1163   )
1164 {
1165   UINT32  *CurrentRsdtEntry;
1166   VOID    *CurrentXsdtEntry;
1167   UINT64  CurrentTablePointer64;
1168   UINTN   Index;
1169 
1170   //
1171   // Check for invalid input parameters
1172   //
1173   ASSERT (Table);
1174   ASSERT (NumberOfTableEntries);
1175   ASSERT (Rsdt || Xsdt);
1176 
1177   //
1178   // Find the table entry in the RSDT and XSDT
1179   //
1180   for (Index = 0; Index < *NumberOfTableEntries; Index++) {
1181     //
1182     // At this time, it is assumed that RSDT and XSDT maintain parallel lists of tables.
1183     // If it becomes necessary to maintain separate table lists, changes will be required.
1184     //
1185     if (Rsdt != NULL) {
1186       CurrentRsdtEntry = (UINT32 *) ((UINT8 *) Rsdt + sizeof (EFI_ACPI_DESCRIPTION_HEADER) + Index * sizeof (UINT32));
1187     } else {
1188       CurrentRsdtEntry = NULL;
1189     }
1190     if (Xsdt != NULL) {
1191       //
1192       // This pointer must not be directly dereferenced as the XSDT entries may not
1193       // be 64 bit aligned resulting in a possible fault.  Use CopyMem to update.
1194       //
1195       CurrentXsdtEntry = (VOID *) ((UINT8 *) Xsdt + sizeof (EFI_ACPI_DESCRIPTION_HEADER) + Index * sizeof (UINT64));
1196 
1197       //
1198       // Read the entry value out of the XSDT
1199       //
1200       CopyMem (&CurrentTablePointer64, CurrentXsdtEntry, sizeof (UINT64));
1201     } else {
1202       //
1203       // Initialize to NULL
1204       //
1205       CurrentXsdtEntry      = 0;
1206       CurrentTablePointer64 = 0;
1207     }
1208     //
1209     // Check if we have found the corresponding entry in both RSDT and XSDT
1210     //
1211     if (((Rsdt == NULL) || *CurrentRsdtEntry == (UINT32) (UINTN) Table->Table) &&
1212         ((Xsdt == NULL) || CurrentTablePointer64 == (UINT64) (UINTN) Table->Table)
1213         ) {
1214       //
1215       // Found entry, so copy all following entries and shrink table
1216       // We actually copy all + 1 to copy the initialized value of memory over
1217       // the last entry.
1218       //
1219       if (Rsdt != NULL) {
1220         CopyMem (CurrentRsdtEntry, CurrentRsdtEntry + 1, (*NumberOfTableEntries - Index) * sizeof (UINT32));
1221         Rsdt->Length = Rsdt->Length - sizeof (UINT32);
1222       }
1223       if (Xsdt != NULL) {
1224         CopyMem (CurrentXsdtEntry, ((UINT64 *) CurrentXsdtEntry) + 1, (*NumberOfTableEntries - Index) * sizeof (UINT64));
1225         Xsdt->Length = Xsdt->Length - sizeof (UINT64);
1226       }
1227       break;
1228     } else if (Index + 1 == *NumberOfTableEntries) {
1229       //
1230       // At the last entry, and table not found
1231       //
1232       return EFI_INVALID_PARAMETER;
1233     }
1234   }
1235   //
1236   // Checksum the tables
1237   //
1238   if (Rsdt != NULL) {
1239     AcpiPlatformChecksum (
1240       Rsdt,
1241       Rsdt->Length,
1242       OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1243       Checksum)
1244       );
1245   }
1246 
1247   if (Xsdt != NULL) {
1248     AcpiPlatformChecksum (
1249       Xsdt,
1250       Xsdt->Length,
1251       OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1252       Checksum)
1253       );
1254   }
1255   //
1256   // Decrement the number of tables
1257   //
1258   (*NumberOfTableEntries)--;
1259 
1260   return EFI_SUCCESS;
1261 }
1262 
1263 
1264 /**
1265   This function removes a table and frees any associated memory.
1266 
1267   @param  AcpiTableInstance  Instance of the protocol.
1268   @param  Version            Version(s) to delete.
1269   @param  Table              Pointer to table found.
1270 
1271   @return EFI_SUCCESS  The function completed successfully.
1272 
1273 **/
1274 EFI_STATUS
DeleteTable(IN EFI_ACPI_TABLE_INSTANCE * AcpiTableInstance,IN EFI_ACPI_TABLE_VERSION Version,IN OUT EFI_ACPI_TABLE_LIST * Table)1275 DeleteTable (
1276   IN EFI_ACPI_TABLE_INSTANCE              *AcpiTableInstance,
1277   IN EFI_ACPI_TABLE_VERSION               Version,
1278   IN OUT EFI_ACPI_TABLE_LIST              *Table
1279   )
1280 {
1281   UINT32  CurrentTableSignature;
1282   BOOLEAN RemoveFromRsdt;
1283 
1284   //
1285   // Check for invalid input parameters
1286   //
1287   ASSERT (AcpiTableInstance);
1288   ASSERT (Table);
1289 
1290   //
1291   // Init locals
1292   //
1293   RemoveFromRsdt        = TRUE;
1294   //
1295   // Check for Table->Table
1296   //
1297   ASSERT (Table->Table != NULL);
1298   CurrentTableSignature = ((EFI_ACPI_COMMON_HEADER *) Table->Table)->Signature;
1299 
1300   //
1301   // Basic tasks to accomplish delete are:
1302   //   Determine removal requirements (in RSDT/XSDT or not)
1303   //   Remove entry from RSDT/XSDT
1304   //   Remove any table references to the table
1305   //   If no one is using the table
1306   //      Free the table (removing pointers from private data and tables)
1307   //      Remove from list
1308   //      Free list structure
1309   //
1310   //
1311   // Determine if this table is in the RSDT or XSDT
1312   //
1313   if ((CurrentTableSignature == EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) ||
1314       (CurrentTableSignature == EFI_ACPI_2_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) ||
1315       (CurrentTableSignature == EFI_ACPI_3_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE)
1316       ) {
1317     RemoveFromRsdt = FALSE;
1318   }
1319   //
1320   // We don't remove the FADT in the standard way because some
1321   // OS expect the FADT to be early in the table list.
1322   // So we always put it as the first element in the list.
1323   //
1324   if (CurrentTableSignature == EFI_ACPI_1_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE) {
1325     RemoveFromRsdt = FALSE;
1326   }
1327 
1328   //
1329   // Remove the table from RSDT and XSDT
1330   //
1331   if (Table->Table != NULL) {
1332     //
1333     // This is a basic table, remove it from any lists and the Rsdt and/or Xsdt
1334     //
1335     if (Version & EFI_ACPI_TABLE_VERSION_NONE & Table->Version) {
1336       //
1337       // Remove this version from the table
1338       //
1339       Table->Version = Table->Version &~EFI_ACPI_TABLE_VERSION_NONE;
1340     }
1341 
1342     if (Version & EFI_ACPI_TABLE_VERSION_1_0B & Table->Version) {
1343       //
1344       // Remove this version from the table
1345       //
1346       Table->Version = Table->Version &~EFI_ACPI_TABLE_VERSION_1_0B;
1347 
1348       //
1349       // Remove from Rsdt.  We don't care about the return value because it is
1350       // acceptable for the table to not exist in Rsdt.
1351       // We didn't add some tables so we don't remove them.
1352       //
1353       if (RemoveFromRsdt) {
1354         RemoveTableFromRsdt (
1355           Table,
1356           &AcpiTableInstance->NumberOfTableEntries1,
1357           AcpiTableInstance->Rsdt1,
1358           NULL
1359           );
1360       }
1361     }
1362 
1363     if (Version & ACPI_TABLE_VERSION_GTE_2_0 & Table->Version) {
1364       //
1365       // Remove this version from the table
1366       //
1367       Table->Version = Table->Version &~(Version & ACPI_TABLE_VERSION_GTE_2_0);
1368 
1369       //
1370       // Remove from Rsdt and Xsdt.  We don't care about the return value
1371       // because it is acceptable for the table to not exist in Rsdt/Xsdt.
1372       // We didn't add some tables so we don't remove them.
1373       //
1374       if (RemoveFromRsdt) {
1375         RemoveTableFromRsdt (
1376           Table,
1377           &AcpiTableInstance->NumberOfTableEntries3,
1378           AcpiTableInstance->Rsdt3,
1379           AcpiTableInstance->Xsdt
1380           );
1381       }
1382     }
1383     //
1384     // Free the table, clean up any dependent tables and our private data pointers.
1385     //
1386     switch (Table->Table->Signature) {
1387 
1388     case EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE:
1389       if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1390         AcpiTableInstance->Fadt1 = NULL;
1391       }
1392 
1393       if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
1394         AcpiTableInstance->Fadt3 = NULL;
1395       }
1396       break;
1397 
1398     case EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE:
1399       if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1400         AcpiTableInstance->Facs1 = NULL;
1401 
1402         //
1403         // Update FADT table pointers
1404         //
1405         if (AcpiTableInstance->Fadt1 != NULL) {
1406           AcpiTableInstance->Fadt1->FirmwareCtrl = 0;
1407 
1408           //
1409           // Checksum table
1410           //
1411           AcpiPlatformChecksum (
1412             AcpiTableInstance->Fadt1,
1413             AcpiTableInstance->Fadt1->Header.Length,
1414             OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1415             Checksum)
1416             );
1417         }
1418       }
1419 
1420       if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
1421         AcpiTableInstance->Facs3 = NULL;
1422 
1423         //
1424         // Update FADT table pointers
1425         //
1426         if (AcpiTableInstance->Fadt3 != NULL) {
1427           AcpiTableInstance->Fadt3->FirmwareCtrl = 0;
1428           ZeroMem (&AcpiTableInstance->Fadt3->XFirmwareCtrl, sizeof (UINT64));
1429 
1430           //
1431           // Checksum table
1432           //
1433           AcpiPlatformChecksum (
1434             AcpiTableInstance->Fadt3,
1435             AcpiTableInstance->Fadt3->Header.Length,
1436             OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1437             Checksum)
1438             );
1439         }
1440       }
1441       break;
1442 
1443     case EFI_ACPI_3_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE:
1444       if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1445         AcpiTableInstance->Dsdt1 = NULL;
1446 
1447         //
1448         // Update FADT table pointers
1449         //
1450         if (AcpiTableInstance->Fadt1 != NULL) {
1451           AcpiTableInstance->Fadt1->Dsdt = 0;
1452 
1453           //
1454           // Checksum table
1455           //
1456           AcpiPlatformChecksum (
1457             AcpiTableInstance->Fadt1,
1458             AcpiTableInstance->Fadt1->Header.Length,
1459             OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1460             Checksum)
1461             );
1462         }
1463       }
1464 
1465 
1466       if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
1467         AcpiTableInstance->Dsdt3 = NULL;
1468 
1469         //
1470         // Update FADT table pointers
1471         //
1472         if (AcpiTableInstance->Fadt3 != NULL) {
1473           AcpiTableInstance->Fadt3->Dsdt = 0;
1474           ZeroMem (&AcpiTableInstance->Fadt3->XDsdt, sizeof (UINT64));
1475 
1476           //
1477           // Checksum table
1478           //
1479           AcpiPlatformChecksum (
1480             AcpiTableInstance->Fadt3,
1481             AcpiTableInstance->Fadt3->Header.Length,
1482             OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1483             Checksum)
1484             );
1485         }
1486       }
1487       break;
1488 
1489     default:
1490       //
1491       // Do nothing
1492       //
1493       break;
1494     }
1495   }
1496   //
1497   // If no version is using this table anymore, remove and free list entry.
1498   //
1499   if (Table->Version == 0) {
1500     //
1501     // Free the Table
1502     //
1503     FreeTableMemory (Table);
1504     RemoveEntryList (&(Table->Link));
1505     gBS->FreePool (Table);
1506   }
1507   //
1508   // Done
1509   //
1510   return EFI_SUCCESS;
1511 }
1512 
1513 
1514 /**
1515   This function finds and removes the table specified by the handle.
1516 
1517   @param  AcpiTableInstance  Instance of the protocol.
1518   @param  Version            Bitmask of which versions to remove.
1519   @param  Handle             Table to remove.
1520 
1521   @return EFI_SUCCESS    The function completed successfully.
1522   @return EFI_ABORTED    An error occurred.
1523   @return EFI_NOT_FOUND  Handle not found in table list.
1524 
1525 **/
1526 EFI_STATUS
RemoveTableFromList(IN EFI_ACPI_TABLE_INSTANCE * AcpiTableInstance,IN EFI_ACPI_TABLE_VERSION Version,IN UINTN Handle)1527 RemoveTableFromList (
1528   IN EFI_ACPI_TABLE_INSTANCE              *AcpiTableInstance,
1529   IN EFI_ACPI_TABLE_VERSION               Version,
1530   IN UINTN                                Handle
1531   )
1532 {
1533   EFI_ACPI_TABLE_LIST *Table;
1534   EFI_STATUS          Status;
1535 
1536   Table = (EFI_ACPI_TABLE_LIST*) NULL;
1537 
1538   //
1539   // Check for invalid input parameters
1540   //
1541   ASSERT (AcpiTableInstance);
1542 
1543   //
1544   // Find the table
1545   //
1546   Status = FindTableByHandle (
1547             Handle,
1548             &AcpiTableInstance->TableList,
1549             &Table
1550             );
1551   if (EFI_ERROR (Status)) {
1552     return EFI_NOT_FOUND;
1553   }
1554   //
1555   // Remove the table
1556   //
1557   Status = DeleteTable (AcpiTableInstance, Version, Table);
1558   if (EFI_ERROR (Status)) {
1559     return EFI_ABORTED;
1560   }
1561   //
1562   // Completed successfully
1563   //
1564   return EFI_SUCCESS;
1565 }
1566 
1567 
1568 /**
1569   This function calculates and updates an UINT8 checksum.
1570 
1571   @param  Buffer          Pointer to buffer to checksum
1572   @param  Size            Number of bytes to checksum
1573   @param  ChecksumOffset  Offset to place the checksum result in
1574 
1575   @return EFI_SUCCESS             The function completed successfully.
1576 
1577 **/
1578 EFI_STATUS
AcpiPlatformChecksum(IN VOID * Buffer,IN UINTN Size,IN UINTN ChecksumOffset)1579 AcpiPlatformChecksum (
1580   IN VOID       *Buffer,
1581   IN UINTN      Size,
1582   IN UINTN      ChecksumOffset
1583   )
1584 {
1585   UINT8 Sum;
1586   UINT8 *Ptr;
1587 
1588   Sum = 0;
1589   //
1590   // Initialize pointer
1591   //
1592   Ptr = Buffer;
1593 
1594   //
1595   // set checksum to 0 first
1596   //
1597   Ptr[ChecksumOffset] = 0;
1598 
1599   //
1600   // add all content of buffer
1601   //
1602   while ((Size--) != 0) {
1603     Sum = (UINT8) (Sum + (*Ptr++));
1604   }
1605   //
1606   // set checksum
1607   //
1608   Ptr                 = Buffer;
1609   Ptr[ChecksumOffset] = (UINT8) (0xff - Sum + 1);
1610 
1611   return EFI_SUCCESS;
1612 }
1613 
1614 
1615 /**
1616   Checksum all versions of the common tables, RSDP, RSDT, XSDT.
1617 
1618   @param  AcpiTableInstance  Protocol instance private data.
1619 
1620   @return EFI_SUCCESS        The function completed successfully.
1621 
1622 **/
1623 EFI_STATUS
ChecksumCommonTables(IN OUT EFI_ACPI_TABLE_INSTANCE * AcpiTableInstance)1624 ChecksumCommonTables (
1625   IN OUT EFI_ACPI_TABLE_INSTANCE                   *AcpiTableInstance
1626   )
1627 {
1628   //
1629   // RSDP ACPI 1.0 checksum for 1.0 table.  This is only the first 20 bytes of the structure
1630   //
1631   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1632     AcpiPlatformChecksum (
1633       AcpiTableInstance->Rsdp1,
1634       sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER),
1635       OFFSET_OF (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER,
1636       Checksum)
1637       );
1638   }
1639 
1640   //
1641   // RSDP ACPI 1.0 checksum for 2.0/3.0 table.  This is only the first 20 bytes of the structure
1642   //
1643   AcpiPlatformChecksum (
1644     AcpiTableInstance->Rsdp3,
1645     sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER),
1646     OFFSET_OF (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER,
1647     Checksum)
1648     );
1649 
1650   //
1651   // RSDP ACPI 2.0/3.0 checksum, this is the entire table
1652   //
1653   AcpiPlatformChecksum (
1654     AcpiTableInstance->Rsdp3,
1655     sizeof (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER),
1656     OFFSET_OF (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER,
1657     ExtendedChecksum)
1658     );
1659 
1660   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1661     //
1662     // RSDT checksums
1663     //
1664     AcpiPlatformChecksum (
1665       AcpiTableInstance->Rsdt1,
1666       AcpiTableInstance->Rsdt1->Length,
1667       OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1668       Checksum)
1669       );
1670 
1671     AcpiPlatformChecksum (
1672       AcpiTableInstance->Rsdt3,
1673       AcpiTableInstance->Rsdt3->Length,
1674       OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1675       Checksum)
1676       );
1677   }
1678 
1679   //
1680   // XSDT checksum
1681   //
1682   AcpiPlatformChecksum (
1683     AcpiTableInstance->Xsdt,
1684     AcpiTableInstance->Xsdt->Length,
1685     OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1686     Checksum)
1687     );
1688 
1689   return EFI_SUCCESS;
1690 }
1691 
1692 
1693 /**
1694   Constructor for the ACPI table protocol.  Initializes instance
1695   data.
1696 
1697   @param  AcpiTableInstance   Instance to construct
1698 
1699   @return EFI_SUCCESS             Instance initialized.
1700   @return EFI_OUT_OF_RESOURCES    Unable to allocate required resources.
1701 
1702 **/
1703 EFI_STATUS
AcpiTableAcpiTableConstructor(EFI_ACPI_TABLE_INSTANCE * AcpiTableInstance)1704 AcpiTableAcpiTableConstructor (
1705   EFI_ACPI_TABLE_INSTANCE                   *AcpiTableInstance
1706   )
1707 {
1708   EFI_STATUS            Status;
1709   UINT64                CurrentData;
1710   UINTN                 TotalSize;
1711   UINTN                 RsdpTableSize;
1712   UINT8                 *Pointer;
1713   EFI_PHYSICAL_ADDRESS  PageAddress;
1714 
1715   //
1716   // Check for invalid input parameters
1717   //
1718   ASSERT (AcpiTableInstance);
1719 
1720   //
1721   // If ACPI v1.0b is among the ACPI versions we aim to support, we have to
1722   // ensure that all memory allocations are below 4 GB.
1723   //
1724   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1725     mAcpiTableAllocType = AllocateMaxAddress;
1726   } else {
1727     mAcpiTableAllocType = AllocateAnyPages;
1728   }
1729 
1730   InitializeListHead (&AcpiTableInstance->TableList);
1731   AcpiTableInstance->CurrentHandle              = 1;
1732 
1733   AcpiTableInstance->AcpiTableProtocol.InstallAcpiTable   = InstallAcpiTable;
1734   AcpiTableInstance->AcpiTableProtocol.UninstallAcpiTable = UninstallAcpiTable;
1735 
1736   if (FeaturePcdGet (PcdInstallAcpiSdtProtocol)) {
1737     SdtAcpiTableAcpiSdtConstructor (AcpiTableInstance);
1738   }
1739 
1740   //
1741   // Create RSDP table
1742   //
1743   RsdpTableSize = sizeof (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER);
1744   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1745     RsdpTableSize += sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER);
1746   }
1747 
1748   if (mAcpiTableAllocType != AllocateAnyPages) {
1749     PageAddress = 0xFFFFFFFF;
1750     Status = gBS->AllocatePages (
1751                     mAcpiTableAllocType,
1752                     EfiACPIReclaimMemory,
1753                     EFI_SIZE_TO_PAGES (RsdpTableSize),
1754                     &PageAddress
1755                     );
1756   } else {
1757     Status = gBS->AllocatePool (
1758                     EfiACPIReclaimMemory,
1759                     RsdpTableSize,
1760                     (VOID **)&Pointer
1761                     );
1762   }
1763 
1764   if (EFI_ERROR (Status)) {
1765     return EFI_OUT_OF_RESOURCES;
1766   }
1767 
1768   if (mAcpiTableAllocType != AllocateAnyPages) {
1769     Pointer = (UINT8 *)(UINTN)PageAddress;
1770   }
1771   ZeroMem (Pointer, RsdpTableSize);
1772 
1773   AcpiTableInstance->Rsdp1 = (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER *) Pointer;
1774   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1775     Pointer += sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER);
1776   }
1777   AcpiTableInstance->Rsdp3 = (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER *) Pointer;
1778 
1779   //
1780   // Create RSDT, XSDT structures
1781   //
1782   TotalSize = sizeof (EFI_ACPI_DESCRIPTION_HEADER) +         // for ACPI 2.0/3.0 XSDT
1783               mEfiAcpiMaxNumTables * sizeof (UINT64);
1784 
1785   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1786     TotalSize += sizeof (EFI_ACPI_DESCRIPTION_HEADER) +      // for ACPI 1.0 RSDT
1787                  mEfiAcpiMaxNumTables * sizeof (UINT32) +
1788                  sizeof (EFI_ACPI_DESCRIPTION_HEADER) +      // for ACPI 2.0/3.0 RSDT
1789                  mEfiAcpiMaxNumTables * sizeof (UINT32);
1790   }
1791 
1792   if (mAcpiTableAllocType != AllocateAnyPages) {
1793     //
1794     // Allocate memory in the lower 32 bit of address range for
1795     // compatibility with ACPI 1.0 OS.
1796     //
1797     // This is done because ACPI 1.0 pointers are 32 bit values.
1798     // ACPI 2.0 OS and all 64 bit OS must use the 64 bit ACPI table addresses.
1799     // There is no architectural reason these should be below 4GB, it is purely
1800     // for convenience of implementation that we force memory below 4GB.
1801     //
1802     PageAddress = 0xFFFFFFFF;
1803     Status = gBS->AllocatePages (
1804                     mAcpiTableAllocType,
1805                     EfiACPIReclaimMemory,
1806                     EFI_SIZE_TO_PAGES (TotalSize),
1807                     &PageAddress
1808                     );
1809   } else {
1810     Status = gBS->AllocatePool (
1811                     EfiACPIReclaimMemory,
1812                     TotalSize,
1813                     (VOID **)&Pointer
1814                     );
1815   }
1816 
1817   if (EFI_ERROR (Status)) {
1818     if (mAcpiTableAllocType != AllocateAnyPages) {
1819       gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)AcpiTableInstance->Rsdp1,
1820              EFI_SIZE_TO_PAGES (RsdpTableSize));
1821     } else {
1822       gBS->FreePool (AcpiTableInstance->Rsdp1);
1823     }
1824     return EFI_OUT_OF_RESOURCES;
1825   }
1826 
1827   if (mAcpiTableAllocType != AllocateAnyPages) {
1828     Pointer = (UINT8 *)(UINTN)PageAddress;
1829   }
1830   ZeroMem (Pointer, TotalSize);
1831 
1832   AcpiTableInstance->Rsdt1 = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
1833   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1834     Pointer += (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + EFI_ACPI_MAX_NUM_TABLES * sizeof (UINT32));
1835     AcpiTableInstance->Rsdt3 = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
1836     Pointer += (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + EFI_ACPI_MAX_NUM_TABLES * sizeof (UINT32));
1837   }
1838   AcpiTableInstance->Xsdt = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
1839 
1840   //
1841   // Initialize RSDP
1842   //
1843   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1844     CurrentData = EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE;
1845     CopyMem (&AcpiTableInstance->Rsdp1->Signature, &CurrentData, sizeof (UINT64));
1846     CopyMem (AcpiTableInstance->Rsdp1->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Rsdp1->OemId));
1847     AcpiTableInstance->Rsdp1->Reserved    = EFI_ACPI_RESERVED_BYTE;
1848     AcpiTableInstance->Rsdp1->RsdtAddress = (UINT32) (UINTN) AcpiTableInstance->Rsdt1;
1849   }
1850 
1851   CurrentData = EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE;
1852   CopyMem (&AcpiTableInstance->Rsdp3->Signature, &CurrentData, sizeof (UINT64));
1853   CopyMem (AcpiTableInstance->Rsdp3->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Rsdp3->OemId));
1854   AcpiTableInstance->Rsdp3->Revision    = EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION;
1855   AcpiTableInstance->Rsdp3->Length      = sizeof (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER);
1856   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1857     AcpiTableInstance->Rsdp3->RsdtAddress = (UINT32) (UINTN) AcpiTableInstance->Rsdt3;
1858   }
1859   CurrentData = (UINT64) (UINTN) AcpiTableInstance->Xsdt;
1860   CopyMem (&AcpiTableInstance->Rsdp3->XsdtAddress, &CurrentData, sizeof (UINT64));
1861   SetMem (AcpiTableInstance->Rsdp3->Reserved, 3, EFI_ACPI_RESERVED_BYTE);
1862 
1863   if ((PcdGet32 (PcdAcpiExposedTableVersions) & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1864     //
1865     // Initialize Rsdt
1866     //
1867     // Note that we "reserve" one entry for the FADT so it can always be
1868     // at the beginning of the list of tables.  Some OS don't seem
1869     // to find it correctly if it is too far down the list.
1870     //
1871     AcpiTableInstance->Rsdt1->Signature = EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_TABLE_SIGNATURE;
1872     AcpiTableInstance->Rsdt1->Length    = sizeof (EFI_ACPI_DESCRIPTION_HEADER);
1873     AcpiTableInstance->Rsdt1->Revision  = EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_TABLE_REVISION;
1874     CopyMem (AcpiTableInstance->Rsdt1->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Rsdt1->OemId));
1875     CurrentData = PcdGet64 (PcdAcpiDefaultOemTableId);
1876     CopyMem (&AcpiTableInstance->Rsdt1->OemTableId, &CurrentData, sizeof (UINT64));
1877     AcpiTableInstance->Rsdt1->OemRevision     = PcdGet32 (PcdAcpiDefaultOemRevision);
1878     AcpiTableInstance->Rsdt1->CreatorId       = PcdGet32 (PcdAcpiDefaultCreatorId);
1879     AcpiTableInstance->Rsdt1->CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
1880     //
1881     // We always reserve first one for FADT
1882     //
1883     AcpiTableInstance->NumberOfTableEntries1  = 1;
1884     AcpiTableInstance->Rsdt1->Length          = AcpiTableInstance->Rsdt1->Length + sizeof(UINT32);
1885 
1886     AcpiTableInstance->Rsdt3->Signature       = EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_TABLE_SIGNATURE;
1887     AcpiTableInstance->Rsdt3->Length          = sizeof (EFI_ACPI_DESCRIPTION_HEADER);
1888     AcpiTableInstance->Rsdt3->Revision        = EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_TABLE_REVISION;
1889     CopyMem (AcpiTableInstance->Rsdt3->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Rsdt3->OemId));
1890     CurrentData = PcdGet64 (PcdAcpiDefaultOemTableId);
1891     CopyMem (&AcpiTableInstance->Rsdt3->OemTableId, &CurrentData, sizeof (UINT64));
1892     AcpiTableInstance->Rsdt3->OemRevision     = PcdGet32 (PcdAcpiDefaultOemRevision);
1893     AcpiTableInstance->Rsdt3->CreatorId       = PcdGet32 (PcdAcpiDefaultCreatorId);
1894     AcpiTableInstance->Rsdt3->CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
1895     //
1896     // We always reserve first one for FADT
1897     //
1898     AcpiTableInstance->Rsdt3->Length          = AcpiTableInstance->Rsdt3->Length + sizeof(UINT32);
1899   }
1900   AcpiTableInstance->NumberOfTableEntries3  = 1;
1901 
1902   //
1903   // Initialize Xsdt
1904   //
1905   AcpiTableInstance->Xsdt->Signature  = EFI_ACPI_3_0_EXTENDED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE;
1906   AcpiTableInstance->Xsdt->Length     = sizeof (EFI_ACPI_DESCRIPTION_HEADER);
1907   AcpiTableInstance->Xsdt->Revision   = EFI_ACPI_3_0_EXTENDED_SYSTEM_DESCRIPTION_TABLE_REVISION;
1908   CopyMem (AcpiTableInstance->Xsdt->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Xsdt->OemId));
1909   CurrentData = PcdGet64 (PcdAcpiDefaultOemTableId);
1910   CopyMem (&AcpiTableInstance->Xsdt->OemTableId, &CurrentData, sizeof (UINT64));
1911   AcpiTableInstance->Xsdt->OemRevision      = PcdGet32 (PcdAcpiDefaultOemRevision);
1912   AcpiTableInstance->Xsdt->CreatorId        = PcdGet32 (PcdAcpiDefaultCreatorId);
1913   AcpiTableInstance->Xsdt->CreatorRevision  = PcdGet32 (PcdAcpiDefaultCreatorRevision);
1914   //
1915   // We always reserve first one for FADT
1916   //
1917   AcpiTableInstance->Xsdt->Length           = AcpiTableInstance->Xsdt->Length + sizeof(UINT64);
1918 
1919   ChecksumCommonTables (AcpiTableInstance);
1920 
1921   //
1922   // Completed successfully
1923   //
1924   return EFI_SUCCESS;
1925 }
1926 
1927