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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
/** @file
ACPI Sdt Protocol Driver
Copyright (c) 2010, Intel Corporation. All rights reserved. <BR>
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 "AcpiTable.h"
/**
Construct node list according to the AML handle.
@param[in] AmlHandle AML handle.
@param[in] AmlRootNodeList AML root node list.
@param[in] AmlParentNodeList AML parent node list.
@retval EFI_SUCCESS Success.
@retval EFI_INVALID_PARAMETER AML handle does not refer to a valid ACPI object.
**/
EFI_STATUS
AmlConstructNodeList (
IN EFI_AML_HANDLE *AmlHandle,
IN EFI_AML_NODE_LIST *AmlRootNodeList,
IN EFI_AML_NODE_LIST *AmlParentNodeList
);
/**
Create AML Node.
@param[in] NameSeg AML NameSeg.
@param[in] Parent AML parent node list.
@param[in] AmlByteEncoding AML Byte Encoding.
@return AML Node.
**/
EFI_AML_NODE_LIST *
AmlCreateNode (
IN UINT8 *NameSeg,
IN EFI_AML_NODE_LIST *Parent,
IN AML_BYTE_ENCODING *AmlByteEncoding
)
{
EFI_AML_NODE_LIST *AmlNodeList;
AmlNodeList = AllocatePool (sizeof(*AmlNodeList));
ASSERT (AmlNodeList != NULL);
AmlNodeList->Signature = EFI_AML_NODE_LIST_SIGNATURE;
CopyMem (AmlNodeList->Name, NameSeg, AML_NAME_SEG_SIZE);
AmlNodeList->Buffer = NULL;
AmlNodeList->Size = 0;
InitializeListHead (&AmlNodeList->Link);
InitializeListHead (&AmlNodeList->Children);
AmlNodeList->Parent = Parent;
AmlNodeList->AmlByteEncoding = AmlByteEncoding;
return AmlNodeList;
}
/**
Find the AML NameSeg in the children of AmlParentNodeList.
@param[in] NameSeg AML NameSeg.
@param[in] AmlParentNodeList AML parent node list.
@param[in] Create TRUE means to create node if not found.
@return AmlChildNode whoes name is same as NameSeg.
**/
EFI_AML_NODE_LIST *
AmlFindNodeInThis (
IN UINT8 *NameSeg,
IN EFI_AML_NODE_LIST *AmlParentNodeList,
IN BOOLEAN Create
)
{
EFI_AML_NODE_LIST *CurrentAmlNodeList;
LIST_ENTRY *CurrentLink;
LIST_ENTRY *StartLink;
EFI_AML_NODE_LIST *AmlNodeList;
StartLink = &AmlParentNodeList->Children;
CurrentLink = StartLink->ForwardLink;
while (CurrentLink != StartLink) {
CurrentAmlNodeList = EFI_AML_NODE_LIST_FROM_LINK (CurrentLink);
//
// AML name is same as the one stored
//
if (CompareMem (CurrentAmlNodeList->Name, NameSeg, AML_NAME_SEG_SIZE) == 0) {
//
// Good! Found it
//
return CurrentAmlNodeList;
}
CurrentLink = CurrentLink->ForwardLink;
}
//
// Not found
//
if (!Create) {
return NULL;
}
//
// Create new node with NULL buffer - it means namespace not be returned.
//
AmlNodeList = AmlCreateNode (NameSeg, AmlParentNodeList, NULL);
InsertTailList (&AmlParentNodeList->Children, &AmlNodeList->Link);
return AmlNodeList;
}
/**
Find the AML NameString in the children of AmlParentNodeList or AmlRootNodeList.
@param[in] NameString AML NameString.
@param[in] AmlRootNodeList AML root node list.
@param[in] AmlParentNodeList AML parent node list.
@param[in] Create TRUE means to create node if not found.
@return AmlChildNode whoes name is same as NameSeg.
**/
EFI_AML_NODE_LIST *
AmlFindNodeInTheTree (
IN UINT8 *NameString,
IN EFI_AML_NODE_LIST *AmlRootNodeList,
IN EFI_AML_NODE_LIST *AmlParentNodeList,
IN BOOLEAN Create
)
{
UINT8 *Buffer;
EFI_AML_NODE_LIST *AmlNodeList;
EFI_AML_NODE_LIST *AmlCurrentNodeList;
UINT8 Index;
UINT8 SegCount;
Buffer = NameString;
//
// Handle root or parent prefix
//
if (*Buffer == AML_ROOT_CHAR) {
AmlCurrentNodeList = AmlRootNodeList;
Buffer += 1;
} else if (*Buffer == AML_PARENT_PREFIX_CHAR) {
AmlCurrentNodeList = AmlParentNodeList;
do {
if (AmlCurrentNodeList->Parent != NULL) {
AmlCurrentNodeList = AmlCurrentNodeList->Parent;
} else {
//
// Only root has no parent
//
ASSERT (AmlCurrentNodeList == AmlRootNodeList);
}
Buffer += 1;
} while (*Buffer == AML_PARENT_PREFIX_CHAR);
} else {
AmlCurrentNodeList = AmlParentNodeList;
}
//
// Handle name segment
//
if (*Buffer == AML_DUAL_NAME_PREFIX) {
Buffer += 1;
SegCount = 2;
} else if (*Buffer == AML_MULTI_NAME_PREFIX) {
Buffer += 1;
SegCount = *Buffer;
Buffer += 1;
} else if (*Buffer == 0) {
//
// NULL name, only for Root
//
ASSERT (AmlCurrentNodeList == AmlRootNodeList);
return AmlCurrentNodeList;
} else {
SegCount = 1;
}
//
// Handle NamePath
//
Index = 0;
do {
AmlNodeList = AmlFindNodeInThis (Buffer, AmlCurrentNodeList, Create);
if (AmlNodeList == NULL) {
return NULL;
}
AmlCurrentNodeList = AmlNodeList;
Buffer += AML_NAME_SEG_SIZE;
Index ++;
} while (Index < SegCount);
return AmlNodeList;
}
/**
Insert the NameString to the AmlNodeList.
@param[in] NameString AML NameString.
@param[in] Buffer Buffer for the Node.
@param[in] Size Size for the Node.
@param[in] AmlRootNodeList AML root node list.
@param[in] AmlParentNodeList AML parent node list.
@return AmlChildNode whoes name is NameString.
**/
EFI_AML_NODE_LIST *
AmlInsertNodeToTree (
IN UINT8 *NameString,
IN VOID *Buffer,
IN UINTN Size,
IN EFI_AML_NODE_LIST *AmlRootNodeList,
IN EFI_AML_NODE_LIST *AmlParentNodeList
)
{
EFI_AML_NODE_LIST *AmlNodeList;
AmlNodeList = AmlFindNodeInTheTree (
NameString,
AmlRootNodeList,
AmlParentNodeList,
TRUE // Find and Create
);
ASSERT (AmlNodeList != NULL);
if (AmlNodeList == NULL) {
return NULL;
}
//
// Check buffer
//
if (AmlNodeList->Buffer == NULL) {
//
// NULL means new added one or SCOPE_OP
//
if (*(UINT8 *)Buffer != AML_SCOPE_OP) {
//
// We need check if new one is SCOPE_OP, because SCOPE_OP just means namespace, not a real device.
// We should not return SCOPE_OP.
//
AmlNodeList->Buffer = Buffer;
AmlNodeList->Size = Size;
AmlNodeList->AmlByteEncoding = AmlSearchByOpByte (Buffer);
}
return AmlNodeList;
}
//
// Already added
//
if (*(UINT8 *)Buffer == AML_SCOPE_OP) {
//
// The new one is SCOPE_OP, OK just return;
//
return AmlNodeList;
}
//
// Oops!!!, There must be something wrong.
//
DEBUG ((EFI_D_ERROR, "AML: Override Happen - %a!\n", NameString));
DEBUG ((EFI_D_ERROR, "AML: Existing Node - %x\n", AmlNodeList->Buffer));
DEBUG ((EFI_D_ERROR, "AML: New Buffer - %x\n", Buffer));
return NULL;
}
/**
Construct child node list according to the AML handle.
@param[in] AmlHandle AML handle.
@param[in] AmlRootNodeList AML root node list.
@param[in] AmlParentNodeList AML parent node list.
@retval EFI_SUCCESS Success.
@retval EFI_INVALID_PARAMETER AML handle does not refer to a valid ACPI object.
**/
EFI_STATUS
AmlConstructNodeListForChild (
IN EFI_AML_HANDLE *AmlHandle,
IN EFI_AML_NODE_LIST *AmlRootNodeList,
IN EFI_AML_NODE_LIST *AmlParentNodeList
)
{
AML_BYTE_ENCODING *AmlByteEncoding;
UINT8 *Buffer;
UINTN BufferSize;
UINT8 *CurrentBuffer;
EFI_AML_HANDLE *AmlChildHandle;
EFI_STATUS Status;
AmlByteEncoding = AmlHandle->AmlByteEncoding;
Buffer = AmlHandle->Buffer;
BufferSize = AmlHandle->Size;
//
// Check if we need recursively add node
//
if ((AmlByteEncoding->Attribute & AML_HAS_CHILD_OBJ) == 0) {
//
// No more node need to be added
//
return EFI_SUCCESS;
}
//
// Do we need add node within METHOD?
// Yes, just add Object is OK. But we need filter NameString for METHOD invoke.
//
//
// Now, we get the last node.
//
Status = AmlGetOffsetAfterLastOption (AmlHandle, &CurrentBuffer);
if (EFI_ERROR (Status)) {
return EFI_INVALID_PARAMETER;
}
//
// Go through all the reset buffer.
//
while ((UINTN)CurrentBuffer < (UINTN)Buffer + BufferSize) {
//
// Find the child node.
//
Status = SdtOpenEx (CurrentBuffer, (UINTN)Buffer + BufferSize - (UINTN)CurrentBuffer, (EFI_ACPI_HANDLE *)&AmlChildHandle);
if (EFI_ERROR (Status)) {
//
// No child found, break now.
//
break;
}
//
// Good, find the child. Construct node recursively
//
Status = AmlConstructNodeList (
AmlChildHandle,
AmlRootNodeList,
AmlParentNodeList
);
if (EFI_ERROR (Status)) {
break;
}
//
// Parse next one
//
CurrentBuffer += AmlChildHandle->Size;
Close ((EFI_ACPI_HANDLE)AmlChildHandle);
}
return EFI_SUCCESS;
}
/**
Construct node list according to the AML handle.
@param[in] AmlHandle AML handle.
@param[in] AmlRootNodeList AML root node list.
@param[in] AmlParentNodeList AML parent node list.
@retval EFI_SUCCESS Success.
@retval EFI_INVALID_PARAMETER AML handle does not refer to a valid ACPI object.
**/
EFI_STATUS
AmlConstructNodeList (
IN EFI_AML_HANDLE *AmlHandle,
IN EFI_AML_NODE_LIST *AmlRootNodeList,
IN EFI_AML_NODE_LIST *AmlParentNodeList
)
{
VOID *NameString;
EFI_AML_NODE_LIST *AmlNodeList;
//
// 1. Check if there is need to construct node for this OpCode.
//
if ((AmlHandle->AmlByteEncoding->Attribute & AML_IN_NAMESPACE) == 0) {
//
// No need to construct node, so we just skip this OpCode.
//
return EFI_SUCCESS;
}
//
// 2. Now, we need construct node for this OpCode.
//
NameString = AmlGetObjectName (AmlHandle);
if (NameString == NULL) {
return EFI_INVALID_PARAMETER;
}
//
// Now, we need to insert node to the node list.
// NOTE: The name here could be AML NameString. So the callee need parse it.
//
AmlNodeList = AmlInsertNodeToTree (NameString, AmlHandle->Buffer, AmlHandle->Size, AmlRootNodeList, AmlParentNodeList);
ASSERT (AmlNodeList != NULL);
//
// 3. Ok, we need to parse the object list to see if there are more node to be added.
//
return AmlConstructNodeListForChild (AmlHandle, AmlRootNodeList, AmlNodeList);
}
/**
Destruct node list
@param[in] AmlParentNodeList AML parent node list.
**/
VOID
AmlDestructNodeList (
IN EFI_AML_NODE_LIST *AmlParentNodeList
)
{
EFI_AML_NODE_LIST *CurrentAmlNodeList;
LIST_ENTRY *CurrentLink;
LIST_ENTRY *StartLink;
//
// Get the children link
//
StartLink = &AmlParentNodeList->Children;
CurrentLink = StartLink->ForwardLink;
//
// Go through all the children
//
while (CurrentLink != StartLink) {
//
// Destruct the child's list recursively
//
CurrentAmlNodeList = EFI_AML_NODE_LIST_FROM_LINK (CurrentLink);
CurrentLink = CurrentLink->ForwardLink;
//
// Remove this child from list and free the node
//
RemoveEntryList (&(CurrentAmlNodeList->Link));
AmlDestructNodeList (CurrentAmlNodeList);
}
//
// Done.
//
FreePool (AmlParentNodeList);
return ;
}
/**
Dump node list
@param[in] AmlParentNodeList AML parent node list.
@param[in] Level Output debug level.
**/
VOID
AmlDumpNodeInfo (
IN EFI_AML_NODE_LIST *AmlParentNodeList,
IN UINTN Level
)
{
EFI_AML_NODE_LIST *CurrentAmlNodeList;
volatile LIST_ENTRY *CurrentLink;
UINTN Index;
CurrentLink = AmlParentNodeList->Children.ForwardLink;
if (Level == 0) {
DEBUG ((EFI_D_ERROR, "\\"));
} else {
for (Index = 0; Index < Level; Index++) {
DEBUG ((EFI_D_ERROR, " "));
}
AmlPrintNameSeg (AmlParentNodeList->Name);
}
DEBUG ((EFI_D_ERROR, "\n"));
while (CurrentLink != &AmlParentNodeList->Children) {
CurrentAmlNodeList = EFI_AML_NODE_LIST_FROM_LINK (CurrentLink);
AmlDumpNodeInfo (CurrentAmlNodeList, Level + 1);
CurrentLink = CurrentLink->ForwardLink;
}
return ;
}
/**
Returns the handle of the ACPI object representing the specified ACPI AML path
@param[in] AmlHandle Points to the handle of the object representing the starting point for the path search.
@param[in] AmlPath Points to the ACPI AML path.
@param[out] Buffer On return, points to the ACPI object which represents AcpiPath, relative to
HandleIn.
@param[in] FromRoot TRUE means to find AML path from \ (Root) Node.
FALSE means to find AML path from this Node (The HandleIn).
@retval EFI_SUCCESS Success
@retval EFI_INVALID_PARAMETER HandleIn does not refer to a valid ACPI object.
**/
EFI_STATUS
AmlFindPath (
IN EFI_AML_HANDLE *AmlHandle,
IN UINT8 *AmlPath,
OUT VOID **Buffer,
IN BOOLEAN FromRoot
)
{
EFI_AML_NODE_LIST *AmlRootNodeList;
EFI_STATUS Status;
EFI_AML_NODE_LIST *AmlNodeList;
UINT8 RootNameSeg[AML_NAME_SEG_SIZE];
EFI_AML_NODE_LIST *CurrentAmlNodeList;
LIST_ENTRY *CurrentLink;
//
// 1. create tree
//
//
// Create root handle
//
RootNameSeg[0] = AML_ROOT_CHAR;
RootNameSeg[1] = 0;
AmlRootNodeList = AmlCreateNode (RootNameSeg, NULL, AmlHandle->AmlByteEncoding);
Status = AmlConstructNodeList (
AmlHandle,
AmlRootNodeList, // Root
AmlRootNodeList // Parent
);
if (EFI_ERROR (Status)) {
return EFI_INVALID_PARAMETER;
}
DEBUG_CODE_BEGIN ();
DEBUG ((EFI_D_ERROR, "AcpiSdt: NameSpace:\n"));
AmlDumpNodeInfo (AmlRootNodeList, 0);
DEBUG_CODE_END ();
//
// 2. Search the node in the tree
//
if (FromRoot) {
//
// Search from Root
//
CurrentAmlNodeList = AmlRootNodeList;
} else {
//
// Search from this node, NOT ROOT.
// Since we insert node to ROOT one by one, we just get the first node and search from it.
//
CurrentLink = AmlRootNodeList->Children.ForwardLink;
if (CurrentLink != &AmlRootNodeList->Children) {
//
// First node
//
CurrentAmlNodeList = EFI_AML_NODE_LIST_FROM_LINK (CurrentLink);
} else {
//
// No child
//
CurrentAmlNodeList = NULL;
}
}
//
// Search
//
if (CurrentAmlNodeList != NULL) {
DEBUG_CODE_BEGIN ();
DEBUG ((EFI_D_ERROR, "AcpiSdt: Search from: \\"));
AmlPrintNameSeg (CurrentAmlNodeList->Name);
DEBUG ((EFI_D_ERROR, "\n"));
DEBUG_CODE_END ();
AmlNodeList = AmlFindNodeInTheTree (
AmlPath,
AmlRootNodeList, // Root
CurrentAmlNodeList, // Parent
FALSE
);
} else {
AmlNodeList = NULL;
}
*Buffer = NULL;
Status = EFI_SUCCESS;
if (AmlNodeList != NULL && AmlNodeList->Buffer != NULL) {
*Buffer = AmlNodeList->Buffer;
}
//
// 3. free the tree
//
AmlDestructNodeList (AmlRootNodeList);
return Status;
}
|