summaryrefslogtreecommitdiffstats
path: root/DynamicTablesPkg/Library/Common/AmlLib/Tree/AmlClone.c
blob: 484c4e8f67758a7e5d724fc2980b5d662bce2a12 (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
/** @file
  AML Clone.

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

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

#include <AmlNodeDefines.h>

#include <AmlCoreInterface.h>
#include <Tree/AmlNode.h>
#include <Tree/AmlTree.h>

/** Clone a node.

  This function does not clone the children nodes.
  The cloned node returned is not attached to any tree.

  @param  [in]  Node        Pointer to a node.
  @param  [out] ClonedNode  Pointer holding the cloned node.

  @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
AmlCloneNode (
  IN  AML_NODE_HEADER  *Node,
  OUT AML_NODE_HEADER  **ClonedNode
  )
{
  EFI_STATUS  Status;

  AML_OBJECT_NODE  *ObjectNode;
  AML_DATA_NODE    *DataNode;
  AML_ROOT_NODE    *RootNode;

  if (!IS_AML_NODE_VALID (Node) ||
      (ClonedNode == NULL))
  {
    ASSERT (0);
    return EFI_INVALID_PARAMETER;
  }

  if (IS_AML_DATA_NODE (Node)) {
    DataNode = (AML_DATA_NODE *)Node;
    Status   = AmlCreateDataNode (
                 DataNode->DataType,
                 DataNode->Buffer,
                 DataNode->Size,
                 (AML_DATA_NODE **)ClonedNode
                 );
    if (EFI_ERROR (Status)) {
      ASSERT (0);
    }
  } else if (IS_AML_OBJECT_NODE (Node)) {
    ObjectNode = (AML_OBJECT_NODE *)Node;

    Status = AmlCreateObjectNode (
               ObjectNode->AmlByteEncoding,
               ObjectNode->PkgLen,
               (AML_OBJECT_NODE **)ClonedNode
               );
    if (EFI_ERROR (Status)) {
      ASSERT (0);
    }
  } else if (IS_AML_ROOT_NODE (Node)) {
    RootNode = (AML_ROOT_NODE *)Node;

    Status = AmlCreateRootNode (
               RootNode->SdtHeader,
               (AML_ROOT_NODE **)ClonedNode
               );
    if (EFI_ERROR (Status)) {
      ASSERT (0);
    }
  } else {
    ASSERT (0);
    return EFI_INVALID_PARAMETER;
  }

  return Status;
}

/** Clone a node and its children (clone a tree branch).

  The cloned branch returned is not attached to any tree.

  @param  [in]  Node        Pointer to a node.
                            Node is the head of the branch to clone.
  @param  [out] ClonedNode  Pointer holding the head of the created cloned
                            branch.

  @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
AmlCloneTree (
  IN  AML_NODE_HEADER  *Node,
  OUT AML_NODE_HEADER  **ClonedNode
  )
{
  EFI_STATUS  Status;

  AML_NODE_HEADER  *HeadNode;
  AML_NODE_HEADER  *ClonedChildNode;
  AML_NODE_HEADER  *FixedArgNode;

  EAML_PARSE_INDEX  Index;
  EAML_PARSE_INDEX  MaxIndex;

  LIST_ENTRY  *StartLink;
  LIST_ENTRY  *CurrentLink;

  if (!IS_AML_NODE_VALID (Node) ||
      (ClonedNode == NULL))
  {
    ASSERT (0);
    return EFI_INVALID_PARAMETER;
  }

  Status = AmlCloneNode (Node, &HeadNode);
  if (EFI_ERROR (Status)) {
    ASSERT (0);
    return Status;
  }

  // Clone the fixed arguments and bind them to their parent.
  MaxIndex = (EAML_PARSE_INDEX)AmlGetFixedArgumentCount (
                                 (AML_OBJECT_NODE *)Node
                                 );
  for (Index = EAmlParseIndexTerm0; Index < MaxIndex; Index++) {
    FixedArgNode = AmlGetFixedArgument ((AML_OBJECT_NODE *)Node, Index);
    if (FixedArgNode == NULL) {
      Status = EFI_INVALID_PARAMETER;
      ASSERT (0);
      goto error_handler;
    }

    // Clone child.
    Status = AmlCloneTree (
               FixedArgNode,
               &ClonedChildNode
               );
    if (EFI_ERROR (Status)) {
      ASSERT (0);
      goto error_handler;
    }

    // Bind child.
    Status = AmlSetFixedArgument (
               (AML_OBJECT_NODE *)HeadNode,
               Index,
               ClonedChildNode
               );
    if (EFI_ERROR (Status)) {
      AmlDeleteTree (ClonedChildNode);
      ASSERT (0);
      goto error_handler;
    }
  } // for

  // Clone the variable arguments and bind them to their parent.
  StartLink = AmlNodeGetVariableArgList (Node);
  if (StartLink != NULL) {
    CurrentLink = StartLink->ForwardLink;
    while (CurrentLink != StartLink) {
      // Clone child.
      Status = AmlCloneTree ((AML_NODE_HEADER *)CurrentLink, &ClonedChildNode);
      if (EFI_ERROR (Status)) {
        ASSERT (0);
        goto error_handler;
      }

      // Bind child.
      Status = AmlVarListAddTailInternal (
                 HeadNode,
                 ClonedChildNode
                 );
      if (EFI_ERROR (Status)) {
        AmlDeleteTree (ClonedChildNode);
        ASSERT (0);
        goto error_handler;
      }

      CurrentLink = CurrentLink->ForwardLink;
    } // while
  }

  *ClonedNode = HeadNode;
  return Status;

error_handler:
  *ClonedNode = NULL;

  if (HeadNode != NULL) {
    AmlDeleteTree (HeadNode);
  }

  return Status;
}