summaryrefslogtreecommitdiff
path: root/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Bert/BertParser.c
blob: 3990aecff4348a04b01ebbacb696123d989a86ea (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
/** @file
  BERT table parser

  Copyright (c) 2026, Arm Limited. All rights reserved.
  SPDX-License-Identifier: BSD-2-Clause-Patent

  @par Reference(s):
    - ACPI 6.6 Specification - January 2025
**/

#include <IndustryStandard/Acpi.h>
#include <Library/BaseLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include "AcpiParser.h"
#include "AcpiTableParser.h"
#include "AcpiViewConfig.h"

// Local variables
STATIC ACPI_DESCRIPTION_HEADER_INFO  mAcpiHdrInfo;
STATIC CONST UINT32                  *mBootErrorRegionLength;

/**
  Check that an entire physical address range is mapped as reserved memory.

  @param [in] BaseAddress  Physical base address of the range.
  @param [in] Length       Length of the range in bytes.

  @retval EFI_SUCCESS           The entire range is mapped as reserved memory.
  @retval EFI_INVALID_PARAMETER The range is invalid.
  @retval EFI_NOT_FOUND         Some or all of the range is not in the memory
                                map.
  @retval EFI_NO_MAPPING        Some or all of the range is not mapped as
                                reserved memory.
  @retval EFI_OUT_OF_RESOURCES  The memory map could not be allocated.
  @retval Others                An error returned by GetMemoryMap().
**/
STATIC
EFI_STATUS
CheckRangeReserved (
  IN EFI_PHYSICAL_ADDRESS  BaseAddress,
  IN UINT64                Length
  )
{
  EFI_PHYSICAL_ADDRESS   CurrentAddress;
  EFI_PHYSICAL_ADDRESS   DescriptorEnd;
  EFI_PHYSICAL_ADDRESS   RangeEnd;
  EFI_MEMORY_DESCRIPTOR  *Descriptor;
  EFI_MEMORY_DESCRIPTOR  *MemoryMap;
  EFI_STATUS             Status;
  UINT32                 DescriptorVersion;
  UINT64                 DescriptorLength;
  UINTN                  DescriptorSize;
  UINTN                  MapKey;
  UINTN                  MemoryMapSize;
  UINTN                  Offset;
  BOOLEAN                DescriptorFound;

  if ((Length == 0) || (BaseAddress > (MAX_UINT64 - Length))) {
    return EFI_INVALID_PARAMETER;
  }

  MemoryMap     = NULL;
  MemoryMapSize = 0;
  Status        = gBS->GetMemoryMap (
                         &MemoryMapSize,
                         MemoryMap,
                         &MapKey,
                         &DescriptorSize,
                         &DescriptorVersion
                         );
  if (Status != EFI_BUFFER_TOO_SMALL) {
    return Status;
  }

  do {
    MemoryMapSize += SIZE_1KB;
    MemoryMap      = AllocatePool (MemoryMapSize);
    if (MemoryMap == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }

    Status = gBS->GetMemoryMap (
                    &MemoryMapSize,
                    MemoryMap,
                    &MapKey,
                    &DescriptorSize,
                    &DescriptorVersion
                    );
    if (EFI_ERROR (Status)) {
      FreePool (MemoryMap);
      MemoryMap = NULL;
    }
  } while (Status == EFI_BUFFER_TOO_SMALL);

  if (EFI_ERROR (Status)) {
    return Status;
  }

  RangeEnd       = BaseAddress + Length;
  CurrentAddress = BaseAddress;

  while (CurrentAddress < RangeEnd) {
    DescriptorFound = FALSE;

    for (Offset = 0; Offset < MemoryMapSize; Offset += DescriptorSize) {
      Descriptor       = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + Offset);
      DescriptorLength = LShiftU64 (Descriptor->NumberOfPages, EFI_PAGE_SHIFT);
      DescriptorEnd    = Descriptor->PhysicalStart + DescriptorLength;
      if ((CurrentAddress < Descriptor->PhysicalStart) ||
          (CurrentAddress >= DescriptorEnd))
      {
        continue;
      }

      if (Descriptor->Type != EfiReservedMemoryType) {
        Status = EFI_NO_MAPPING;
        goto FreeMemoryMap;
      }

      CurrentAddress  = MIN (DescriptorEnd, RangeEnd);
      DescriptorFound = TRUE;
      break;
    }

    if (!DescriptorFound) {
      Status = EFI_NOT_FOUND;
      goto FreeMemoryMap;
    }
  }

  Status = EFI_SUCCESS;

FreeMemoryMap:
  FreePool (MemoryMap);
  return Status;
}

/**
  Validate the Boot Error Region length.

  @param [in] Ptr       Pointer to the start of the field data.
  @param [in] Length    Length of the field.
  @param [in] Context   Pointer to context specific information.
**/
STATIC
VOID
EFIAPI
ValidateBootErrorRegionLength (
  IN UINT8   *Ptr,
  IN UINT32  Length,
  IN VOID    *Context
  )
{
  CONST UINT32  RegionLength = *(UINT32 *)Ptr;

  if (RegionLength < sizeof (EFI_ACPI_6_6_GENERIC_ERROR_STATUS_STRUCTURE)) {
    IncrementErrorCount ();
    Print (
      L"\nERROR: Boot Error Region Length must be at least 0x%x.",
      (UINT32)sizeof (EFI_ACPI_6_6_GENERIC_ERROR_STATUS_STRUCTURE)
      );
  }
}

/**
  Validate the Boot Error Region address.

  @param [in] Ptr       Pointer to the start of the field data.
  @param [in] Length    Length of the field.
  @param [in] Context   Pointer to context specific information.
**/
STATIC
VOID
EFIAPI
ValidateBootErrorRegion (
  IN UINT8   *Ptr,
  IN UINT32  Length,
  IN VOID    *Context
  )
{
  CONST UINT64  RegionAddress = *(UINT64 *)Ptr;
  EFI_STATUS    Status;

  if (RegionAddress == 0) {
    IncrementErrorCount ();
    Print (L"\nERROR: Boot Error Region address must not be zero.");
    return;
  }

  if ((mBootErrorRegionLength != NULL) &&
      (*mBootErrorRegionLength > (MAX_UINT64 - RegionAddress)))
  {
    IncrementErrorCount ();
    Print (L"\nERROR: Boot Error Region address range overflows.");
    return;
  }

  if ((mBootErrorRegionLength == NULL) ||
      (*mBootErrorRegionLength <
       sizeof (EFI_ACPI_6_6_GENERIC_ERROR_STATUS_STRUCTURE)))
  {
    return;
  }

  Status = CheckRangeReserved (RegionAddress, *mBootErrorRegionLength);
  if ((Status == EFI_NOT_FOUND) || (Status == EFI_NO_MAPPING)) {
    IncrementErrorCount ();
    Print (
      L"\nERROR: Boot Error Region must be entirely mapped as "
      L"EfiReservedMemoryType."
      );
  } else if (EFI_ERROR (Status)) {
    IncrementErrorCount ();
    Print (
      L"\nERROR: Failed to check the Boot Error Region memory type. "
      L"Status = %r.",
      Status
      );
  }
}

/**
  An ACPI_PARSER array describing the ACPI BERT table.
**/
STATIC CONST ACPI_PARSER  mBertParser[] = {
  PARSE_ACPI_HEADER (&mAcpiHdrInfo),
  { L"Boot Error Region Length",    4,  36, L"0x%x",  NULL, (VOID **)&mBootErrorRegionLength, ValidateBootErrorRegionLength, NULL },
  { L"Boot Error Region",           8,  40, L"0x%lx", NULL, NULL,                             ValidateBootErrorRegion,       NULL }
};

/**
  This function parses the ACPI BERT table.
  When trace is enabled this function parses the BERT table and
  traces the ACPI table fields.

  This function also performs validation of the ACPI table fields.

  @param [in] Trace              If TRUE, trace the ACPI fields.
  @param [in] Ptr                Pointer to the start of the buffer.
  @param [in] AcpiTableLength    Length of the ACPI table.
  @param [in] AcpiTableRevision  Revision of the ACPI table.
**/
VOID
EFIAPI
ParseAcpiBert (
  IN BOOLEAN  Trace,
  IN UINT8    *Ptr,
  IN UINT32   AcpiTableLength,
  IN UINT8    AcpiTableRevision
  )
{
  if (!Trace) {
    return;
  }

  ParseAcpi (
    Trace,
    0,
    "BERT",
    Ptr,
    AcpiTableLength,
    PARSER_PARAMS (mBertParser)
    );
}