summaryrefslogtreecommitdiff
path: root/Core/EM/ACPI/AmlString.c
blob: b3640f3da49cbd618ef6564be143668d3bb6bfaf (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
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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/BIN/Modules/ACPI/Template/Core/AmlString.c 3     12/13/12 12:00p Oleksiyy $
//
// $Revision: 3 $
//
// $Date: 12/13/12 12:00p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Modules/ACPI/Template/Core/AmlString.c $
// 
// 3     12/13/12 12:00p Oleksiyy
// [TAG]  		EIP109290
// [Category]  	Improvement
// [Description]  	Issues found by CppCheck in ACPI eModule
// [Files]  		AcpiCore.c, mptable.c, AmlString.c, BootScriptSave.c and
// BootScriptExecuter.c
// 
// 2     5/14/11 2:13p Yakovlevs
// [TAG]  		EIP 56526
// [Category]  	New Feature
// [Description]  	ACPI Manipulation Protocol. PI 1.2 Spec Vol 5 Section
// 9. Initial checkin.
// [Files]  		AcpiCore.c; AcpiCore.h; AcpiSdtPrivate.h; Aml.c; AmlChild.c;
// AmlNamespace.c; AmlOption.c; AmlString.c AcpiSdt.c; 
// Protocol\AcpiSdt.h. 
// 
//**********************************************************************


//**********************************************************************
//<AMI_FHDR_START>
//
// Name: AmlString.c
//
// Description:	
//  ACPI AML Name space parsing/manipulation functions for ACPI SDT.
//
//<AMI_FHDR_END>
//**********************************************************************

#include "AcpiCore.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;

  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 = Malloc (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) {
    MemSet (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);
    MemSet (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
  )
{
  TRACE ((-1, "%c", Buffer[0]));
  if ((Buffer[1] == '_') && (Buffer[2] == '_') && (Buffer[3] == '_')) {
    return ;
  }
  TRACE ((-1, "%c", Buffer[1]));
  if ((Buffer[2] == '_') && (Buffer[3] == '_')) {
    return ;
  }
  TRACE ((-1, "%c", Buffer[2]));
  if (Buffer[3] == '_') {
    return ;
  }
  TRACE ((-1, "%c", Buffer[3]));
  return ;
}

/**
  Print AML NameString.

  @param[in] Buffer AML NameString.
**/
VOID
AmlPrintNameString (
  IN UINT8              *Buffer
  )
{
  UINT8                 SegCount;
  UINT8                 Index;
 
  if (*Buffer == AML_ROOT_CHAR) {
    //
    // RootChar
    //
    Buffer ++;
    TRACE ((-1, "\\"));
  } else if (*Buffer == AML_PARENT_PREFIX_CHAR) {
    //
    // ParentPrefixChar
    //
    do {
      Buffer ++;
      TRACE ((-1, "^"));
    } 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++) {
    TRACE ((-1, "."));
    AmlPrintNameSeg (Buffer);
    Buffer += AML_NAME_SEG_SIZE;
  }

  return ;
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************