summaryrefslogtreecommitdiffstats
path: root/DynamicTablesPkg/Library/Common/AmlLib/Serialize/AmlSerialize.c
blob: 2a47c229d7480128ed0f782efa009c7a86f4a492 (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
/** @file
  AML Serialize.

  Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <AmlNodeDefines.h>

#include <AmlCoreInterface.h>
#include <Stream/AmlStream.h>
#include <Tree/AmlNode.h>
#include <Tree/AmlTree.h>
#include <Utils/AmlUtility.h>

/** Callback function to copy the AML bytecodes contained in a node
    to the Stream stored in the Context.
    The SDT header data contained in the root node is not serialized
    by this function.

  @param  [in]      Node      Pointer to the node to copy the AML bytecodes
                              from.
  @param  [in, out] Context   Contains a forward Stream to write to.
                              (AML_STREAM*)Context.
  @param  [in, out] Status    At entry, contains the status returned by the
                              last call to this exact function during the
                              enumeration.
                              As exit, contains the returned status of the
                              call to this function.
                              Optional, can be NULL.

  @retval TRUE if the enumeration can continue or has finished without
          interruption.
  @retval FALSE if the enumeration needs to stopped or has stopped.
**/
STATIC
BOOLEAN
EFIAPI
AmlSerializeNodeCallback (
  IN       AML_NODE_HEADER   * Node,
  IN  OUT  VOID              * Context,    OPTIONAL
  IN  OUT  EFI_STATUS        * Status      OPTIONAL
  )
{
  EFI_STATUS                Status1;

  CONST AML_DATA_NODE     * DataNode;
  CONST AML_OBJECT_NODE   * ObjectNode;
  AML_STREAM              * FStream;

  // Bytes needed to store OpCode[1] + SubOpcode[1] + MaxPkgLen[4] = 6 bytes.
  UINT8                     ObjectNodeInfoArray[6];
  UINT32                    Index;
  BOOLEAN                   ContinueEnum;

  CONST AML_OBJECT_NODE   * ParentNode;
  EAML_PARSE_INDEX          IndexPtr;

  if (!IS_AML_NODE_VALID (Node)   ||
      (Context == NULL)) {
    ASSERT (0);
    Status1 = EFI_INVALID_PARAMETER;
    ContinueEnum = FALSE;
    goto error_handler;
  }

  // Ignore the second fixed argument of method invocation nodes
  // as the information stored there (the argument count) is not in the
  // ACPI specification.
  ParentNode = (CONST AML_OBJECT_NODE*)AmlGetParent ((AML_NODE_HEADER*)Node);
  if (IS_AML_OBJECT_NODE (ParentNode)                             &&
      AmlNodeCompareOpCode (ParentNode, AML_METHOD_INVOC_OP, 0)   &&
      AmlIsNodeFixedArgument (Node, &IndexPtr)) {
    if (IndexPtr == EAmlParseIndexTerm1) {
      if (Status != NULL) {
        *Status = EFI_SUCCESS;
      }
      return TRUE;
    }
  }

  Status1 = EFI_SUCCESS;
  ContinueEnum = TRUE;
  FStream = (AML_STREAM*)Context;

  if (IS_AML_DATA_NODE (Node)) {
    // Copy the content of the Buffer for a DataNode.
    DataNode = (AML_DATA_NODE*)Node;
    Status1 = AmlStreamWrite (
                FStream,
                DataNode->Buffer,
                DataNode->Size
                );
    if (EFI_ERROR (Status1)) {
      ASSERT (0);
      ContinueEnum = FALSE;
      goto error_handler;
    }

  } else if (IS_AML_OBJECT_NODE (Node)  &&
             !AmlNodeHasAttribute (
                (CONST AML_OBJECT_NODE*)Node,
                AML_IS_PSEUDO_OPCODE)) {
    // Ignore pseudo-opcodes as they are not part of the
    // ACPI specification.

    ObjectNode = (AML_OBJECT_NODE*)Node;

    Index = 0;
    // Copy the opcode(s).
    ObjectNodeInfoArray[Index++] = ObjectNode->AmlByteEncoding->OpCode;
    if (ObjectNode->AmlByteEncoding->OpCode == AML_EXT_OP) {
      ObjectNodeInfoArray[Index++] = ObjectNode->AmlByteEncoding->SubOpCode;
    }

    // Copy the PkgLen.
    if (AmlNodeHasAttribute (ObjectNode, AML_HAS_PKG_LENGTH)) {
      Index += AmlSetPkgLength (
                 ObjectNode->PkgLen,
                 &ObjectNodeInfoArray[Index]
                 );
    }

    Status1 = AmlStreamWrite (
                FStream,
                ObjectNodeInfoArray,
                Index
                );
    if (EFI_ERROR (Status1)) {
      ASSERT (0);
      ContinueEnum = FALSE;
      goto error_handler;
    }
  } // IS_AML_OBJECT_NODE (Node)

error_handler:
  if (Status != NULL) {
    *Status = Status1;
  }
  return ContinueEnum;
}

/** Serialize a tree to create an ACPI DSDT/SSDT table.

  If:
   - the content of BufferSize is >= to the size needed to serialize the
     definition block;
   - Buffer is not NULL;
  first serialize the ACPI DSDT/SSDT header from the root node,
  then serialize the AML blob from the rest of the tree.

  The content of BufferSize is always updated to the size needed to
  serialize the definition block.

  @param  [in]      RootNode    Pointer to a root node.
  @param  [in]      Buffer      Buffer to write the DSDT/SSDT table to.
                                If Buffer is NULL, the size needed to
                                serialize the DSDT/SSDT table is returned
                                in BufferSize.
  @param  [in, out] BufferSize  Pointer holding the size of the Buffer.
                                Its content is always updated to the size
                                needed to serialize the DSDT/SSDT table.

  @retval EFI_SUCCESS             The function completed successfully.
  @retval EFI_INVALID_PARAMETER   Invalid parameter.
  @retval EFI_BUFFER_TOO_SMALL    No space left in the buffer.
**/
EFI_STATUS
EFIAPI
AmlSerializeTree (
  IN      AML_ROOT_NODE   * RootNode,
  IN      UINT8           * Buffer,     OPTIONAL
  IN  OUT UINT32          * BufferSize
  )
{
  EFI_STATUS    Status;
  AML_STREAM    FStream;
  UINT32        TableSize;

  if (!IS_AML_ROOT_NODE (RootNode) ||
      (BufferSize == NULL)) {
    ASSERT (0);
    return EFI_INVALID_PARAMETER;
  }

  // Compute the total size of the AML blob.
  Status = AmlComputeSize (
              (CONST AML_NODE_HEADER*)RootNode,
              &TableSize
              );
  if (EFI_ERROR (Status)) {
    ASSERT (0);
    return Status;
  }

  // Add the size of the ACPI header.
  TableSize += (UINT32)sizeof (EFI_ACPI_DESCRIPTION_HEADER);

  // Check the size against the SDT header.
  // The Length field in the SDT Header is updated if the tree has
  // been modified.
  if (TableSize != RootNode->SdtHeader->Length) {
    ASSERT (0);
    return EFI_INVALID_PARAMETER;
  }

  // Buffer is not big enough, or NULL.
  if ((TableSize < *BufferSize) || (Buffer == NULL)) {
    *BufferSize = TableSize;
    return EFI_SUCCESS;
  }

  // Initialize the stream to the TableSize that is needed.
  Status = AmlStreamInit (
             &FStream,
             Buffer,
             TableSize,
             EAmlStreamDirectionForward
             );
  if (EFI_ERROR (Status)) {
    ASSERT (0);
    return Status;
  }

  // Serialize the header.
  Status = AmlStreamWrite (
             &FStream,
             (UINT8*)RootNode->SdtHeader,
             sizeof (EFI_ACPI_DESCRIPTION_HEADER)
             );
  if (EFI_ERROR (Status)) {
    ASSERT (0);
    return Status;
  }

  Status = EFI_SUCCESS;
  AmlEnumTree (
    (AML_NODE_HEADER*)RootNode,
    AmlSerializeNodeCallback,
    (VOID*)&FStream,
    &Status
    );
  if (EFI_ERROR (Status)) {
    ASSERT (0);
    return Status;
  }

  // Update the checksum.
  return AcpiPlatformChecksum ((EFI_ACPI_DESCRIPTION_HEADER*)Buffer);
}

/** Serialize an AML definition block.

  This functions allocates memory with the "AllocateZeroPool ()"
  function. This memory is used to serialize the AML tree and is
  returned in the Table.

  @param [in]  RootNode         Root node of the tree.
  @param [out] Table            On return, hold the serialized
                                definition block.

  @retval EFI_SUCCESS            The function completed successfully.
  @retval EFI_INVALID_PARAMETER  Invalid parameter.
  @retval EFI_OUT_OF_RESOURCES   Could not allocate memory.
**/
EFI_STATUS
EFIAPI
AmlSerializeDefinitionBlock (
  IN  AML_ROOT_NODE                   * RootNode,
  OUT EFI_ACPI_DESCRIPTION_HEADER    ** Table
  )
{
  EFI_STATUS    Status;
  UINT8       * TableBuffer;
  UINT32        TableSize;

  if (!IS_AML_ROOT_NODE (RootNode) ||
      (Table == NULL)) {
    ASSERT (0);
    return EFI_INVALID_PARAMETER;
  }

  *Table = NULL;
  TableBuffer = NULL;
  TableSize = 0;

  // Get the size of the SSDT table.
  Status = AmlSerializeTree (
             RootNode,
             TableBuffer,
             &TableSize
             );
  if (EFI_ERROR (Status)) {
    ASSERT (0);
    return Status;
  }

  TableBuffer = (UINT8*)AllocateZeroPool (TableSize);
  if (TableBuffer == NULL) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: Failed to allocate memory for Table Buffer."
      ));
    ASSERT (0);
    return EFI_OUT_OF_RESOURCES;
  }

  // Serialize the tree to a SSDT table.
  Status = AmlSerializeTree (
             RootNode,
             TableBuffer,
             &TableSize
             );
  if (EFI_ERROR (Status)) {
    FreePool (TableBuffer);
    ASSERT (0);
  } else {
    // Save the allocated Table buffer in the table list
    *Table = (EFI_ACPI_DESCRIPTION_HEADER*)TableBuffer;
  }

  return Status;
}