summaryrefslogtreecommitdiff
path: root/src/VBox/Devices/EFI/Firmware/VBoxPkg/VBoxSysTables/TableConversion.c
blob: 1fb7d6f980556feb550cdb1b77c067ab9c4d7f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* $Id: TableConversion.c $ */
/** @file
 * TableConversion.c
 */

/*
 * Copyright (C) 2009-2010 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */

/*++

  This code is baed on:

  Copyright (c) 2006 - 2007, Intel Corporation
  All rights reserved. This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  --*/

#include <IndustryStandard/Pci.h>
#include <IndustryStandard/Acpi.h>
#include <IndustryStandard/SmBios.h>
#include "LegacyBiosMpTable.h"

#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseLib.h>

#include <Guid/Acpi.h>
#include <Guid/SmBios.h>
#include <Guid/Mps.h>
#include <Guid/HobList.h>
#include <Guid/GlobalVariable.h>

#define SYS_TABLE_PAD(ptr) (((~ptr) +1) & 0x07 )
#define EFI_SYSTEM_TABLE_MAX_ADDRESS 0xFFFFFFFF

EFI_STATUS
ConvertAcpiTable (
    IN     UINTN                       TableLen,
    IN OUT VOID                        **Table
                  )
/*++

  Routine Description:
  Convert RSDP of ACPI Table if its location is lower than Address:0x100000
  Assumption here:
  As in legacy Bios, ACPI table is required to place in E/F Seg,
  So here we just check if the range is E/F seg,
  and if Not, assume the Memory type is EfiACPIReclaimMemory/EfiACPIMemoryNVS

  Arguments:
  TableLen  - Acpi RSDP length
  Table     - pointer to the table

  Returns:
  EFI_SUCCESS - Convert Table successfully
  Other       - Failed

  --*/
{
    VOID                  *AcpiTableOri;
    VOID                  *AcpiTableNew;
    EFI_STATUS            Status;
    EFI_PHYSICAL_ADDRESS  BufferPtr;


    AcpiTableOri    =  (VOID *)(UINTN)((*Table));

    BufferPtr = EFI_SYSTEM_TABLE_MAX_ADDRESS;
    Status = gBS->AllocatePages (
                                 AllocateMaxAddress,
                                 EfiACPIMemoryNVS,
                                 EFI_SIZE_TO_PAGES(TableLen),
                                 &BufferPtr
                                 );
    ASSERT_EFI_ERROR (Status);
    AcpiTableNew = (VOID *)(UINTN)BufferPtr;
    CopyMem (AcpiTableNew, AcpiTableOri, TableLen);

    //
    // Change configuration table Pointer
    //
    *Table = AcpiTableNew;

    return EFI_SUCCESS;
}

EFI_STATUS
ConvertSmbiosTable (
    IN OUT VOID        **Table
                    )
/*++

  Routine Description:

  Convert Smbios Table if the Location of the SMBios Table is lower than Address 0x100000
  Assumption here:
  As in legacy Bios, Smbios table is required to place in E/F Seg,
  So here we just check if the range is F seg,
  and if Not, assume the Memory type is EfiACPIMemoryNVS/EfiRuntimeServicesData
  Arguments:
  Table     - pointer to the table

  Returns:
  EFI_SUCCESS - Convert Table successfully
  Other       - Failed

  --*/
{
    SMBIOS_TABLE_ENTRY_POINT *SmbiosTableNew;
    SMBIOS_TABLE_ENTRY_POINT *SmbiosTableOri;
    EFI_STATUS               Status;
    UINT32                   SmbiosEntryLen;
    UINT32                   BufferLen;
    EFI_PHYSICAL_ADDRESS     BufferPtr;

    SmbiosTableNew  = NULL;
    SmbiosTableOri  = NULL;

    //
    // Get Smibos configuration Table
    //
    SmbiosTableOri =  (SMBIOS_TABLE_ENTRY_POINT *)(UINTN)((*Table));


    ASSERT(CalculateSum8((UINT8*)SmbiosTableOri, sizeof(SMBIOS_TABLE_ENTRY_POINT)) == 0);
    //
    // Relocate the Smibos memory
    //
    BufferPtr = EFI_SYSTEM_TABLE_MAX_ADDRESS;
    if (SmbiosTableOri->SmbiosBcdRevision != 0x21) {
        SmbiosEntryLen  = SmbiosTableOri->EntryPointLength;
    } else {
        //
        // According to Smbios Spec 2.4, we should set entry point length as 0x1F if version is 2.1
        //
        SmbiosEntryLen = 0x1F;
    }
    BufferLen = SmbiosEntryLen + SYS_TABLE_PAD(SmbiosEntryLen) + SmbiosTableOri->TableLength;
    Status = gBS->AllocatePages (
        AllocateMaxAddress,
        EfiACPIMemoryNVS,
        EFI_SIZE_TO_PAGES(BufferLen),
        &BufferPtr
                                 );
    ASSERT_EFI_ERROR (Status);
    SmbiosTableNew = (SMBIOS_TABLE_ENTRY_POINT *)(UINTN)BufferPtr;
    CopyMem (
        SmbiosTableNew,
        SmbiosTableOri,
        SmbiosEntryLen
             );
    //
    // Get Smbios Structure table address, and make sure the start address is 32-bit align
    //
    BufferPtr += SmbiosEntryLen + SYS_TABLE_PAD(SmbiosEntryLen);
    CopyMem (
        (VOID *)(UINTN)BufferPtr,
        (VOID *)(UINTN)(SmbiosTableOri->TableAddress),
        SmbiosTableOri->TableLength
             );
    SmbiosTableNew->TableAddress = (UINT32)BufferPtr;
    SmbiosTableNew->IntermediateChecksum = 0;
    SmbiosTableNew->IntermediateChecksum =
            CalculateCheckSum8 ((UINT8*)SmbiosTableNew + 0x10, SmbiosEntryLen -0x10);
    //
    // Change the SMBIOS pointer
    //
    *Table = SmbiosTableNew;

    return EFI_SUCCESS;
}

EFI_STATUS
ConvertMpsTable (
    IN OUT VOID          **Table
                 )
/*++

  Routine Description:

  Convert MP Table if the Location of the SMBios Table is lower than Address 0x100000
  Assumption here:
  As in legacy Bios, MP table is required to place in E/F Seg,
  So here we just check if the range is E/F seg,
  and if Not, assume the Memory type is EfiACPIMemoryNVS/EfiRuntimeServicesData
  Arguments:
  Table     - pointer to the table

  Returns:
  EFI_SUCCESS - Convert Table successfully
  Other       - Failed

  --*/
{
    UINT32                                       Data32;
    UINT32                                       FPLength;
    EFI_LEGACY_MP_TABLE_FLOATING_POINTER         *MpsFloatingPointerOri;
    EFI_LEGACY_MP_TABLE_FLOATING_POINTER         *MpsFloatingPointerNew;
    EFI_LEGACY_MP_TABLE_HEADER                   *MpsTableOri;
    EFI_LEGACY_MP_TABLE_HEADER                   *MpsTableNew;
    VOID                                         *OemTableOri;
    VOID                                         *OemTableNew;
    EFI_STATUS                                   Status;
    EFI_PHYSICAL_ADDRESS                         BufferPtr;

    //
    // Get MP configuration Table
    //
    MpsFloatingPointerOri = (EFI_LEGACY_MP_TABLE_FLOATING_POINTER *)(UINTN)(*Table);
    //
    // Get Floating pointer structure length
    //
    FPLength = MpsFloatingPointerOri->Length * 16;
    ASSERT(CalculateSum8((UINT8*)MpsFloatingPointerOri, FPLength) == 0);
    Data32   = FPLength + SYS_TABLE_PAD (FPLength);
    MpsTableOri = (EFI_LEGACY_MP_TABLE_HEADER *)(UINTN)(MpsFloatingPointerOri->PhysicalAddress);
    ASSERT(MpsTableOri != NULL);
    ASSERT(CalculateSum8((UINT8*)MpsTableOri, MpsTableOri->BaseTableLength) == 0);

    Data32 += MpsTableOri->BaseTableLength;
    Data32 += MpsTableOri->ExtendedTableLength;
    if (MpsTableOri->OemTablePointer != 0x00) {
        Data32 += SYS_TABLE_PAD (Data32);
        Data32 += MpsTableOri->OemTableSize;
    }

    //
    // Relocate memory
    //
    BufferPtr = EFI_SYSTEM_TABLE_MAX_ADDRESS;
    Status = gBS->AllocatePages (
        AllocateMaxAddress,
        EfiACPIMemoryNVS,
        EFI_SIZE_TO_PAGES(Data32),
        &BufferPtr
                                 );
    ASSERT_EFI_ERROR (Status);
    MpsFloatingPointerNew = (EFI_LEGACY_MP_TABLE_FLOATING_POINTER *)(UINTN)BufferPtr;
    CopyMem (MpsFloatingPointerNew, MpsFloatingPointerOri, FPLength);
    //
    // If Mp Table exists
    //
    if (MpsTableOri != NULL) {
        //
        // Get Mps table length, including Ext table
        //
        BufferPtr = BufferPtr + FPLength + SYS_TABLE_PAD (FPLength);
        MpsTableNew = (EFI_LEGACY_MP_TABLE_HEADER *)(UINTN)BufferPtr;
        CopyMem (MpsTableNew, MpsTableOri, MpsTableOri->BaseTableLength + MpsTableOri->ExtendedTableLength);

        if ((MpsTableOri->OemTableSize != 0x0000) && (MpsTableOri->OemTablePointer != 0x0000)){
            BufferPtr += MpsTableOri->BaseTableLength + MpsTableOri->ExtendedTableLength;
            BufferPtr += SYS_TABLE_PAD (BufferPtr);
            OemTableNew = (VOID *)(UINTN)BufferPtr;
            OemTableOri = (VOID *)(UINTN)MpsTableOri->OemTablePointer;
            CopyMem (OemTableNew, OemTableOri, MpsTableOri->OemTableSize);
            MpsTableNew->OemTablePointer = (UINT32)(UINTN)OemTableNew;
        }
        MpsTableNew->Checksum = 0;
        MpsTableNew->Checksum = CalculateCheckSum8 ((UINT8*)MpsTableNew, MpsTableOri->BaseTableLength);
        MpsFloatingPointerNew->PhysicalAddress = (UINT32)(UINTN)MpsTableNew;
        MpsFloatingPointerNew->Checksum = 0;
        MpsFloatingPointerNew->Checksum = CalculateCheckSum8 ((UINT8*)MpsFloatingPointerNew, FPLength);
    }
    //
    // Change the pointer
    //
    *Table = MpsFloatingPointerNew;

    return EFI_SUCCESS;
}

EFI_STATUS
ConvertSystemTable (
    IN     EFI_GUID        *TableGuid,
    IN OUT VOID            **Table
                    )
/*++

  Routine Description:
  Convert ACPI Table /Smbios Table /MP Table if its location is lower than Address:0x100000
  Assumption here:
  As in legacy Bios, ACPI/Smbios/MP table is required to place in E/F Seg,
  So here we just check if the range is E/F seg,
  and if Not, assume the Memory type is EfiACPIReclaimMemory/EfiACPIMemoryNVS

  Arguments:
  TableGuid - Guid of the table
  Table     - pointer to the table

  Returns:
  EFI_SUCCESS - Convert Table successfully
  Other       - Failed

  --*/
{
    EFI_STATUS      Status = EFI_SUCCESS;
    VOID            *AcpiHeader;
    UINTN           AcpiTableLen;

    //
    // If match acpi guid (1.0, 2.0, or later), Convert ACPI table according to version.
    //

    if (CompareGuid(TableGuid, &gEfiAcpiTableGuid) || CompareGuid(TableGuid, &gEfiAcpi20TableGuid)){
        AcpiHeader = (VOID*)(UINTN)(*Table);

        if (((EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)AcpiHeader)->Reserved == 0x00){
            //
            // If Acpi 1.0 Table, then RSDP structure doesn't contain Length field, use structure size
            //
            AcpiTableLen = sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER);
        } else if (((EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)AcpiHeader)->Reserved >= 0x02){
            //
            // If Acpi 2.0 or later, use RSDP Length fied.
            //
            AcpiTableLen = ((EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)AcpiHeader)->Length;
        } else {
            //
            // Invalid Acpi Version, return
            //
            return EFI_UNSUPPORTED;
        }
        Status = ConvertAcpiTable (AcpiTableLen, Table);
        return Status;
    }

    //
    // If matches smbios guid, convert Smbios table.
    //
    if (CompareGuid(TableGuid, &gEfiSmbiosTableGuid)){
        Status = ConvertSmbiosTable (Table);
        return Status;
    }

    //
    // If the table is MP table?
    //
    if (CompareGuid(TableGuid, &gEfiMpsTableGuid)){
        Status = ConvertMpsTable (Table);
        return Status;
    }

    return EFI_UNSUPPORTED;
}