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
|
/** @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"
/**
Check if it is AML Root name
@param[in] Buffer AML path.
@retval TRUE AML path is root.
@retval FALSE AML path is not root.
**/
BOOLEAN
AmlIsRootPath (
IN UINT8 *Buffer
)
{
if ((Buffer[0] == AML_ROOT_CHAR) && (Buffer[1] == 0)) {
return TRUE;
} else {
return FALSE;
}
}
/**
Check if it is AML LeadName.
@param[in] Ch Char.
@retval TRUE Char is AML LeadName.
@retval FALSE Char is not AML LeadName.
**/
BOOLEAN
AmlIsLeadName (
IN CHAR8 Ch
)
{
if ((Ch == '_') || (Ch >= 'A' && Ch <= 'Z')) {
return TRUE;
} else {
return FALSE;
}
}
/**
Check if it is AML Name.
@param[in] Ch Char.
@retval TRUE Char is AML Name.
@retval FALSE Char is not AML Name.
**/
BOOLEAN
AmlIsName (
IN CHAR8 Ch
)
{
if (AmlIsLeadName (Ch) || (Ch >= '0' && Ch <= '9')) {
return TRUE;
} else {
return FALSE;
}
}
/**
Return is buffer is AML NameSeg.
@param[in] Buffer AML NameSement.
@retval TRUE It is AML NameSegment.
@retval FALSE It is not AML NameSegment.
**/
BOOLEAN
AmlIsNameSeg (
IN UINT8 *Buffer
)
{
UINTN Index;
if (!AmlIsLeadName (Buffer[0])) {
return FALSE;
}
for (Index = 1; Index < AML_NAME_SEG_SIZE; Index++) {
if (!AmlIsName (Buffer[Index])) {
return FALSE;
}
}
return TRUE;
}
/**
Get AML NameString size.
@param[in] Buffer AML NameString.
@param[out] BufferSize AML NameString size
@retval EFI_SUCCESS Success.
@retval EFI_INVALID_PARAMETER Buffer does not refer to a valid AML NameString.
**/
EFI_STATUS
AmlGetNameStringSize (
IN UINT8 *Buffer,
OUT UINTN *BufferSize
)
{
UINTN SegCount;
UINTN Length;
UINTN Index;
UINT8 *Name;
Name = Buffer;
Length = 0;
//
// Parse root or parent prefix
//
if (*Buffer == AML_ROOT_CHAR) {
Buffer ++;
Length ++;
} else if (*Buffer == AML_PARENT_PREFIX_CHAR) {
do {
Buffer ++;
Length ++;
} while (*Buffer == AML_PARENT_PREFIX_CHAR);
}
//
// Parse name segment
//
if (*Buffer == AML_DUAL_NAME_PREFIX) {
Buffer ++;
Length ++;
SegCount = 2;
} else if (*Buffer == AML_MULTI_NAME_PREFIX) {
Buffer ++;
Length ++;
SegCount = *Buffer;
Buffer ++;
Length ++;
} else if (*Buffer == 0) {
//
// NULL Name, only for Root
//
SegCount = 0;
Buffer --;
if ((Length == 1) && (*Buffer == AML_ROOT_CHAR)) {
*BufferSize = 2;
return EFI_SUCCESS;
} else {
return EFI_INVALID_PARAMETER;
}
} else {
//
// NameSeg
//
SegCount = 1;
}
Index = 0;
do {
if (!AmlIsNameSeg (Buffer)) {
return EFI_INVALID_PARAMETER;
}
Buffer += AML_NAME_SEG_SIZE;
Length += AML_NAME_SEG_SIZE;
Index ++;
} while (Index < SegCount);
*BufferSize = Length;
return EFI_SUCCESS;
}
/**
Check if it is ASL LeadName.
@param[in] Ch Char.
@retval TRUE Char is ASL LeadName.
@retval FALSE Char is not ASL LeadName.
**/
BOOLEAN
AmlIsAslLeadName (
IN CHAR8 Ch
)
{
if (AmlIsLeadName (Ch) || (Ch >= 'a' && Ch <= 'z')) {
return TRUE;
} else {
return FALSE;
}
}
/**
Check if it is ASL Name.
@param[in] Ch Char.
@retval TRUE Char is ASL Name.
@retval FALSE Char is not ASL Name.
**/
BOOLEAN
AmlIsAslName (
IN CHAR8 Ch
)
{
if (AmlIsAslLeadName (Ch) || (Ch >= '0' && Ch <= '9')) {
return TRUE;
} else {
return FALSE;
}
}
/**
Get ASL NameString size.
@param[in] Buffer ASL NameString.
@return ASL NameString size.
**/
UINTN
AmlGetAslNameSegLength (
IN UINT8 *Buffer
)
{
UINTN Length;
UINTN Index;
if (*Buffer == 0) {
return 0;
}
Length = 0;
//
// 1st
//
if (AmlIsAslLeadName (*Buffer)) {
Length ++;
Buffer ++;
}
if ((*Buffer == 0) || (*Buffer == '.')) {
return Length;
}
//
// 2, 3, 4 name char
//
for (Index = 0; Index < 3; Index++) {
if (AmlIsAslName (*Buffer)) {
Length ++;
Buffer ++;
}
if ((*Buffer == 0) || (*Buffer == '.')) {
return Length;
}
}
//
// Invalid ASL name
//
return 0;
}
/**
Get ASL NameString size.
@param[in] Buffer ASL NameString.
@param[out] Root On return, points to Root char number.
@param[out] Parent On return, points to Parent char number.
@param[out] SegCount On return, points to Segment count.
@return ASL NameString size.
**/
UINTN
AmlGetAslNameStringSize (
IN UINT8 *Buffer,
OUT UINTN *Root,
OUT UINTN *Parent,
OUT UINTN *SegCount
)
{
UINTN NameLength;
UINTN TotalLength;
*Root = 0;
*Parent = 0;
*SegCount = 0;
TotalLength = 0;
NameLength = 0;
if (*Buffer == AML_ROOT_CHAR) {
*Root = 1;
Buffer ++;
} else if (*Buffer == AML_PARENT_PREFIX_CHAR) {
do {
Buffer ++;
(*Parent) ++;
} while (*Buffer == AML_PARENT_PREFIX_CHAR);
}
//
// Now parse name
//
while (*Buffer != 0) {
NameLength = AmlGetAslNameSegLength (Buffer);
if ((NameLength == 0) || (NameLength > AML_NAME_SEG_SIZE)) {
return 0;
}
(*SegCount) ++;
Buffer += NameLength;
if (*Buffer == 0) {
break;
}
Buffer ++;
}
//
// Check SegCoount
//
if (*SegCount > 0xFF) {
return 0;
}
//
// Calculate total length
//
TotalLength = *Root + *Parent + (*SegCount) * AML_NAME_SEG_SIZE;
if (*SegCount > 2) {
TotalLength += 2;
} else if (*SegCount == 2) {
TotalLength += 1;
}
//
// Add NULL char
//
TotalLength ++;
return TotalLength;
}
/**
Copy mem, and cast all the char in dest to be upper case.
@param[in] DstBuffer Destination buffer.
@param[in] SrcBuffer Source buffer.
@param[in] Length Buffer length.
**/
VOID
AmlUpperCaseCopyMem (
IN UINT8 *DstBuffer,
IN UINT8 *SrcBuffer,
IN UINTN Length
)
{
UINTN Index;
for (Index = 0; Index < Length; Index++) {
if (SrcBuffer[Index] >= 'a' && SrcBuffer[Index] <= 'z') {
DstBuffer[Index] = (UINT8)(SrcBuffer[Index] - 'a' + 'A');
} else {
DstBuffer[Index] = SrcBuffer[Index];
}
}
}
/**
Return AML name according to ASL name.
The caller need free the AmlName returned.
@param[in] AslPath ASL name.
@return AmlName
**/
UINT8 *
AmlNameFromAslName (
IN UINT8 *AslPath
)
{
UINTN Root;
UINTN Parent;
UINTN SegCount;
UINTN TotalLength;
UINTN NameLength;
UINT8 *Buffer;
UINT8 *AmlPath;
UINT8 *AmlBuffer;
TotalLength = AmlGetAslNameStringSize (AslPath, &Root, &Parent, &SegCount);
if (TotalLength == 0) {
return NULL;
}
AmlPath = AllocatePool (TotalLength);
ASSERT (AmlPath != NULL);
AmlBuffer = AmlPath;
Buffer = AslPath;
//
// Handle Root and Parent
//
if (Root == 1) {
*AmlBuffer = AML_ROOT_CHAR;
AmlBuffer ++;
Buffer ++;
} else if (Parent > 0) {
SetMem (AmlBuffer, Parent, AML_PARENT_PREFIX_CHAR);
AmlBuffer += Parent;
Buffer += Parent;
}
//
// Handle SegCount
//
if (SegCount > 2) {
*AmlBuffer = AML_MULTI_NAME_PREFIX;
AmlBuffer ++;
*AmlBuffer = (UINT8)SegCount;
AmlBuffer ++;
} else if (SegCount == 2) {
*AmlBuffer = AML_DUAL_NAME_PREFIX;
AmlBuffer ++;
}
//
// Now to name
//
while (*Buffer != 0) {
NameLength = AmlGetAslNameSegLength (Buffer);
ASSERT ((NameLength != 0) && (NameLength <= AML_NAME_SEG_SIZE));
AmlUpperCaseCopyMem (AmlBuffer, Buffer, NameLength);
SetMem (AmlBuffer + NameLength, AML_NAME_SEG_SIZE - NameLength, AML_NAME_CHAR__);
Buffer += NameLength;
AmlBuffer += AML_NAME_SEG_SIZE;
if (*Buffer == 0) {
break;
}
Buffer ++;
}
//
// Add NULL
//
AmlPath[TotalLength - 1] = 0;
return AmlPath;
}
/**
Print AML NameSeg.
@param[in] Buffer AML NameSeg.
**/
VOID
AmlPrintNameSeg (
IN UINT8 *Buffer
)
{
DEBUG ((EFI_D_ERROR, "%c", Buffer[0]));
if ((Buffer[1] == '_') && (Buffer[2] == '_') && (Buffer[3] == '_')) {
return ;
}
DEBUG ((EFI_D_ERROR, "%c", Buffer[1]));
if ((Buffer[2] == '_') && (Buffer[3] == '_')) {
return ;
}
DEBUG ((EFI_D_ERROR, "%c", Buffer[2]));
if (Buffer[3] == '_') {
return ;
}
DEBUG ((EFI_D_ERROR, "%c", Buffer[3]));
return ;
}
/**
Print AML NameString.
@param[in] Buffer AML NameString.
**/
VOID
AmlPrintNameString (
IN UINT8 *Buffer
)
{
UINT8 SegCount;
UINT8 Index;
UINT8 *Name;
Name = Buffer;
if (*Buffer == AML_ROOT_CHAR) {
//
// RootChar
//
Buffer ++;
DEBUG ((EFI_D_ERROR, "\\"));
} else if (*Buffer == AML_PARENT_PREFIX_CHAR) {
//
// ParentPrefixChar
//
do {
Buffer ++;
DEBUG ((EFI_D_ERROR, "^"));
} while (*Buffer == AML_PARENT_PREFIX_CHAR);
}
if (*Buffer == AML_DUAL_NAME_PREFIX) {
//
// DualName
//
Buffer ++;
SegCount = 2;
} else if (*Buffer == AML_MULTI_NAME_PREFIX) {
//
// MultiName
//
Buffer ++;
SegCount = *Buffer;
Buffer ++;
} else if (*Buffer == 0) {
//
// NULL Name
//
return ;
} else {
//
// NameSeg
//
SegCount = 1;
}
AmlPrintNameSeg (Buffer);
Buffer += AML_NAME_SEG_SIZE;
for (Index = 0; Index < SegCount - 1; Index++) {
DEBUG ((EFI_D_ERROR, "."));
AmlPrintNameSeg (Buffer);
Buffer += AML_NAME_SEG_SIZE;
}
return ;
}
|