summaryrefslogtreecommitdiff
path: root/Platform/AMD/AgesaModulePkg/Library/DxeAmlGenerationLib/AmlDataObjects.c
blob: b5cf796da16a1175f3813c25f6279e8fa6bbb7ce (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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/** @file

  Copyright (C) 2020-2025 Advanced Micro Devices, Inc. All rights reserved.<BR>

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

**/

#include "LocalAmlLib.h"
#include <Filecode.h>

#define FILECODE  LIBRARY_DXEAMLGENERATIONLIB_AMLDATAOBJECTS_FILECODE

/*
  Creates an allocated buffer with sized data and no Op Code

  ByteData := 0x00 - 0xFF
  WordData := ByteData[0:7] ByteData[8:15] // 0x0000-0xFFFF
  DWordData := WordData[0:15] WordData[16:31] // 0x00000000-0xFFFFFFFF
  QWordData := DWordData[0:31] DWordData[32:63] // 0x0000000000000000- 0xFFFFFFFFFFFFFFFF

  Forces max integer size UINT64

  Caller is responsible for freeing returned buffer.

  @param[in]    Integer         - Integer value to encode
  @param[in]    IntegerSize     - Size of integer in bytes
  @param[out]   ReturnData      - Allocated DataBuffer with encoded integer
  @param[out]   ReturnDataSize  - Size of ReturnData

  @return       EFI_SUCCESS     - Successful completion
  @return       EFI_OUT_OF_RESOURCES - Failed to allocate ReturnDataBuffer
*/
EFI_STATUS
EFIAPI
InternalAmlSizedDataBuffer (
  IN      UINT64  Integer,
  IN      UINTN   IntegerSize,
  OUT     VOID    **ReturnData
  )
{
  UINT8  *Data;

  if ((IntegerSize != sizeof (UINT8)) &&
      (IntegerSize != sizeof (UINT16)) &&
      (IntegerSize != sizeof (UINT32)) &&
      (IntegerSize != sizeof (UINT64)))
  {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Incorrect integer size=%d requested.\n", __func__, IntegerSize));
    return EFI_INVALID_PARAMETER;
  }

  if ((IntegerSize < sizeof (UINT64)) && (Integer >= LShiftU64 (1, IntegerSize * 8))) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Integer is larger than requestd size.\n", __func__));
    return EFI_INVALID_PARAMETER;
  }

  // Max Data Size is 64 bit. Plus one Opcode byte
  Data = AllocateZeroPool (sizeof (UINT64));
  if (Data == NULL) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Integer Space Alloc Failed\n", __func__));
    return EFI_OUT_OF_RESOURCES;
  }

  // Already established we only have supported sizes above
  switch (IntegerSize) {
    case sizeof (UINT8):
      *(UINT8 *)Data = (UINT8)Integer;
      break;
    case sizeof (UINT16):
      *(UINT16 *)Data = (UINT16)Integer;
      break;
    case sizeof (UINT32):
      *(UINT32 *)Data = (UINT32)Integer;
      break;
    case sizeof (UINT64):
      *(UINT64 *)Data = (UINT64)Integer;
      break;
  }

  *ReturnData = (VOID *)Data;
  return EFI_SUCCESS;
}

/*
  Calculates the optimized integer value used by AmlOPDataInteger and others

  Forces max integer size UINT64

  @param[in]    Integer         - Integer value to encode
  @param[out]   ReturnData      - Allocated DataBuffer with encoded integer
  @param[out]   ReturnDataSize  - Size of ReturnData

  @return       EFI_SUCCESS     - Successful completion
  @return       EFI_OUT_OF_RESOURCES - Failed to allocate ReturnDataBuffer
*/
EFI_STATUS
EFIAPI
InternalAmlDataIntegerBuffer (
  IN      UINT64  Integer,
  OUT     VOID    **ReturnData,
  OUT     UINTN   *ReturnDataSize
  )
{
  UINT8  *IntegerData;
  UINTN  IntegerDataSize;
  UINT8  *Data = NULL;
  UINTN  DataSize;

  // Max Data Size is 64 bit. Plus one Opcode byte
  IntegerData = AllocateZeroPool (sizeof (UINT64) + 1);
  if (IntegerData == NULL) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Integer Space Alloc Failed\n", __func__));
    return EFI_OUT_OF_RESOURCES;
  }

  if (Integer == 0) {
    // ZeroOp
    IntegerDataSize = 1;
    IntegerData[0]  = AML_ZERO_OP;
  } else if (Integer == 1) {
    // OneOp
    IntegerDataSize = 1;
    IntegerData[0]  = AML_ONE_OP;
  } else if (Integer == (UINT64) ~0x0) {
    // OnesOp
    IntegerDataSize = 1;
    IntegerData[0]  = AML_ONES_OP;
  } else {
    if (Integer >= 0x100000000) {
      // QWordConst
      IntegerDataSize = sizeof (UINT64) + 1;
      IntegerData[0]  = AML_QWORD_PREFIX;
    } else if (Integer >= 0x10000) {
      // DWordConst
      IntegerDataSize = sizeof (UINT32) + 1;
      IntegerData[0]  = AML_DWORD_PREFIX;
    } else if (Integer >= 0x100) {
      // WordConst
      IntegerDataSize = sizeof (UINT16) + 1;
      IntegerData[0]  = AML_WORD_PREFIX;
    } else {
      // ByteConst
      IntegerDataSize = sizeof (UINT8) + 1;
      IntegerData[0]  = AML_BYTE_PREFIX;
    }

    DataSize = IntegerDataSize - 1;
    InternalAmlSizedDataBuffer (Integer, DataSize, (VOID **)&Data);
    if (Data == NULL) {
      DEBUG ((DEBUG_ERROR, "%a: ERROR: Integer Data Space Alloc Failed\n", __func__));
      FreePool (IntegerData);
      return EFI_OUT_OF_RESOURCES;
    }

    CopyMem (&IntegerData[1], Data, DataSize);
    FreePool (Data);
  }

  // Reallocate the pool so size is exact
  *ReturnData     = (VOID *)IntegerData;
  *ReturnDataSize = IntegerDataSize;

  return EFI_SUCCESS;
}

/**
  Creates an optimized integer object

  Forces max integer size UINT64

  ComputationalData := ByteConst | WordConst | DWordConst | QWordConst | String |
                       ConstObj | RevisionOp | DefBuffer
  DataObject        := ComputationalData | DefPackage | DefVarPackage
  DataRefObject     := DataObject | ObjectReference | DDBHandle
  ByteConst         := BytePrefix ByteData
  BytePrefix        := 0x0A
  WordConst         := WordPrefix WordData
  WordPrefix        := 0x0B
  DWordConst        := DWordPrefix DWordData
  DWordPrefix       := 0x0C
  QWordConst        := QWordPrefix QWordData
  QWordPrefix       := 0x0E
  ConstObj          := ZeroOp | OneOp | OnesOp
  ByteData          := 0x00 - 0xFF
  WordData          := ByteData[0:7] ByteData[8:15]
                       // 0x0000-0xFFFF
  DWordData         := WordData[0:15] WordData[16:31]
                       // 0x00000000-0xFFFFFFFF
  QWordData         := DWordData[0:31] DWordData[32:63]
                       // 0x0000000000000000-0xFFFFFFFFFFFFFFFF
  ZeroOp            := 0x00
  OneOp             := 0x01
  OnesOp            := 0xFF

  @param[in]      Integer   - Number to be optimized and encoded
  @param[in,out]  ListHead  - Head of Linked List of all AML Objects

  @return   EFI_SUCCESS     - Success
  @return   all others      - Fail
**/
EFI_STATUS
EFIAPI
AmlOPDataInteger (
  IN      UINT64      Integer,
  IN OUT  LIST_ENTRY  *ListHead
  )
{
  EFI_STATUS           Status;
  AML_OBJECT_INSTANCE  *Object;

  if (ListHead == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Status = EFI_DEVICE_ERROR;
  Object = NULL;

  Status = InternalAppendNewAmlObjectNoData (&Object, ListHead);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Start %a object\n", __func__, "DATA_INTEGER"));
    goto Done;
  }

  Status = InternalAmlDataIntegerBuffer (
             Integer,
             (VOID **)&(Object->Data),
             &(Object->DataSize)
             );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: ACPI Integer 0x%X object\n", __func__, Integer));
    goto Done;
  }

  Object->Completed = TRUE;

  Status = EFI_SUCCESS;

Done:
  if (EFI_ERROR (Status)) {
    InternalFreeAmlObject (&Object, ListHead);
  }

  return Status;
}

/**
  Creates an Sized Data integer object for use in Buffer objects.  Does not
  include opcode.

  ByteData          := 0x00 - 0xFF
  WordData          := ByteData[0:7] ByteData[8:15]
                       // 0x0000-0xFFFF
  DWordData         := WordData[0:15] WordData[16:31]
                       // 0x00000000-0xFFFFFFFF
  QWordData         := DWordData[0:31] DWordData[32:63]
                       // 0x0000000000000000-0xFFFFFFFFFFFFFFFF

  @param[in]      Integer   - Number to be optimized and encoded
  @param[in,out]  ListHead  - Head of Linked List of all AML Objects

  @return   EFI_SUCCESS     - Success
  @return   all others      - Fail
**/
EFI_STATUS
EFIAPI
InternalAmlOPSizedData (
  IN      UINT64      Integer,
  IN      UINTN       IntegerSize,
  IN OUT  LIST_ENTRY  *ListHead
  )
{
  EFI_STATUS           Status;
  AML_OBJECT_INSTANCE  *Object;

  if (ListHead == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Status = EFI_DEVICE_ERROR;
  Object = NULL;

  Status = InternalAppendNewAmlObjectNoData (&Object, ListHead);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Start %a object\n", __func__, "SIZED_DATA_INTEGER"));
    goto Done;
  }

  Object->DataSize = IntegerSize;
  Status           = InternalAmlSizedDataBuffer (
                       Integer,
                       Object->DataSize,
                       (VOID **)&(Object->Data)
                       );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: ACPI Integer 0x%X object\n", __func__, Integer));
    goto Done;
  }

  Object->Completed = TRUE;

  Status = EFI_SUCCESS;

Done:
  if (EFI_ERROR (Status)) {
    InternalFreeAmlObject (&Object, ListHead);
  }

  return Status;
}

/**
  Creates a ByteData integer object for use in Buffer objects.  Does not
  include opcode.

  ByteData          := 0x00 - 0xFF

  @param[in]      Integer   - Number to be placed in object
  @param[in,out]  ListHead  - Head of Linked List of all AML Objects

  @return   EFI_SUCCESS     - Success
  @return   all others      - Fail
**/
EFI_STATUS
EFIAPI
AmlOPByteData (
  IN      UINT8       Integer,
  IN OUT  LIST_ENTRY  *ListHead
  )
{
  return InternalAmlOPSizedData (Integer, sizeof (UINT8), ListHead);
}

/**
  Creates a WordData integer object for use in Buffer objects.  Does not
  include opcode.

  WordData          := 0x0000 - 0xFFFF

  @param[in]      Integer   - Number to be placed in object
  @param[in,out]  ListHead  - Head of Linked List of all AML Objects

  @return   EFI_SUCCESS     - Success
  @return   all others      - Fail
**/
EFI_STATUS
EFIAPI
AmlOPWordData (
  IN      UINT16      Integer,
  IN OUT  LIST_ENTRY  *ListHead
  )
{
  return InternalAmlOPSizedData (Integer, sizeof (UINT16), ListHead);
}

/**
  Creates a DWordData integer object for use in Buffer objects.  Does not
  include opcode.

  DWordData          := 0x00000000 - 0xFFFFFFFF

  @param[in]      Integer   - Number to be placed in object
  @param[in,out]  ListHead  - Head of Linked List of all AML Objects

  @return   EFI_SUCCESS     - Success
  @return   all others      - Fail
**/
EFI_STATUS
EFIAPI
AmlOPDWordData (
  IN      UINT32      Integer,
  IN OUT  LIST_ENTRY  *ListHead
  )
{
  return InternalAmlOPSizedData (Integer, sizeof (UINT32), ListHead);
}

/**
  Creates a QWordData integer object for use in Buffer objects.  Does not
  include opcode.

  QWordData          := 0x00000000_00000000 - 0xFFFFFFFF_FFFFFFFF

  @param[in]      Integer   - Number to be placed in object
  @param[in,out]  ListHead  - Head of Linked List of all AML Objects

  @return   EFI_SUCCESS     - Success
  @return   all others      - Fail
**/
EFI_STATUS
EFIAPI
AmlOPQWordData (
  IN      UINT64      Integer,
  IN OUT  LIST_ENTRY  *ListHead
  )
{
  return InternalAmlOPSizedData (Integer, sizeof (UINT64), ListHead);
}

/**
  Creates a data string object

  ComputationalData   := String

  String              := StringPrefix AsciiCharList NullChar
  StringPrefix        := 0x0D
  AsciiCharList       := Nothing | <AsciiChar AsciiCharList>
  AsciiChar           := 0x01 - 0x7F
  NullChar            := 0x00

  @param[in]      String    - String to be encoded
  @param[in,out]  ListHead  - Head of Linked List of all AML Objects

  @return   EFI_SUCCESS     - Success
  @return   all others      - Fail
**/
EFI_STATUS
EFIAPI
AmlOPDataString (
  IN      CHAR8       *String,
  IN OUT  LIST_ENTRY  *ListHead
  )
{
  EFI_STATUS           Status;
  AML_OBJECT_INSTANCE  *Object;
  UINT8                *Data;
  UINTN                DataSize;
  UINTN                Index;

  if ((String == NULL) || (ListHead == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  Status = EFI_DEVICE_ERROR;
  Object = NULL;

  // Validate all characters
  DataSize = AsciiStrLen (String);
  for (Index = 0; Index < DataSize; Index++) {
    if (String[Index] < 0x01) {
      Status = EFI_INVALID_PARAMETER;
      DEBUG ((
        DEBUG_ERROR,
        "%a: ERROR: Invalid character String[%d] : %a\n",
        __func__,
        Index,
        String
        ));
      return Status;
    }
  }

  Status = InternalAppendNewAmlObjectNoData (&Object, ListHead);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Start %a object\n", __func__, String));
    goto Done;
  }

  // AML_STRING_PREFIX + String + NULL Terminator
  DataSize += 2;
  Data      = AllocatePool (DataSize);
  if (Data == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    DEBUG ((
      DEBUG_ERROR,
      "%a: ERROR: String Space Allocation %a\n",
      __func__,
      String
      ));
    goto Done;
  }

  Data[0] = AML_STRING_PREFIX;
  CopyMem (&Data[1], String, DataSize - 1);

  // DataString Complete, Put into Object
  Object->Data      = Data;
  Object->DataSize  = DataSize;
  Object->Completed = TRUE;

  Status = EFI_SUCCESS;

Done:
  if (EFI_ERROR (Status)) {
    InternalFreeAmlObject (&Object, ListHead);
  }

  return Status;
}

/**
  Creates a data buffer AML object from an array

  This will take the passed in buffer and generate an AML Object from that
  buffer

  @param[in]      Buffer      - Buffer to be placed in AML Object
  @param[in]      BufferSize  - Size of Buffer to be copied into Object
  @param[in,out]  ListHead    - Head of Linked List of all AML Objects

  @return   EFI_SUCCESS       - Success
  @return   all others        - Fail
**/
EFI_STATUS
EFIAPI
AmlOPDataBufferFromArray (
  IN      VOID        *Buffer,
  IN      UINTN       BufferSize,
  IN OUT  LIST_ENTRY  *ListHead
  )
{
  EFI_STATUS           Status;
  AML_OBJECT_INSTANCE  *Object;

  if ((Buffer == NULL) || (BufferSize == 0) || (ListHead == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  Object = NULL;

  Status = InternalAppendNewAmlObjectNoData (&Object, ListHead);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Start Data Buffer object\n", __func__));
    goto Done;
  }

  Object->Data     = AllocatePool (BufferSize);
  Object->DataSize = BufferSize;
  if (Object->Data == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Data Buffer allocate failed\n", __func__));
    goto Done;
  }

  CopyMem (Object->Data, Buffer, BufferSize);
  Object->Completed = TRUE;

  Status = EFI_SUCCESS;

Done:
  if (EFI_ERROR (Status)) {
    InternalFreeAmlObject (&Object, ListHead);
  }

  return Status;
}

/**
  19.6.36 EISAID (EISA ID String To Integer Conversion Macro)

  Syntax:
    EISAID (EisaIdString) => DWordConst

  Arguments:
    The EisaIdString must be a String object of the form "UUUNNNN", where "U"
    is an uppercase letter and "N" is a hexadecimal digit. No asterisks or other
    characters are allowed in the string.

  Description:
    Converts EisaIdString, a 7-character text string argument, into its
    corresponding 4-byte numeric EISA ID encoding. It can be used when declaring
    IDs for devices that have EISA IDs.

    Encoded EISA ID Definition - 32-bits
     bits[15:0] - three character compressed ASCII EISA ID. *
     bits[31:16] - binary number
      * Compressed ASCII is 5 bits per character 0b00001 = 'A' 0b11010 = 'Z'


  Example:
    EISAID ("PNP0C09") // This is a valid invocation of the macro.

  @param[in]      String    - EISA ID string.
  @param[in,out]  ListHead  - Head of Linked List of all AML Objects
**/
EFI_STATUS
EFIAPI
AmlOPEisaId (
  IN      CHAR8       *String,
  IN OUT  LIST_ENTRY  *ListHead
  )
{
  EFI_STATUS  Status;
  UINT32      EncodedEisaId;
  UINT8       i;

  EncodedEisaId = 0;

  if ((String == NULL) || (ListHead == NULL)) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Invalid parameter, inputs cannot == NULL.\n", __func__));
    return EFI_INVALID_PARAMETER;
  }

  if (AsciiStrLen (String) != 0x7) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Invalid length for 'String' parameter.\n", __func__));
    return EFI_INVALID_PARAMETER;
  }

  //
  // Verify String is formatted as "UUUNNNN".
  //
  for (i = 0; i <= 0x6; i++) {
    //
    // If first 3 characters are not uppercase alpha or last 4 characters are not hexadecimal
    //
    if (((i <= 0x2) && (!IS_ASCII_UPPER_ALPHA (String[i]))) ||
        ((i >= 0x3) && (!IS_ASCII_HEX_DIGIT (String[i]))))
    {
      DEBUG ((DEBUG_ERROR, "%a: ERROR: Invalid EISA ID string format!\n", __func__));
      DEBUG ((DEBUG_ERROR, "  Input String must be formatted as 'UUUNNNN'.\n"));
      return EFI_INVALID_PARAMETER;
    }
  }

  //
  // Convert string to 4-byte EISA ID encoding.
  //   Ex: 'PNP0A03' encodes to '0x30AD041'
  //
  EncodedEisaId = ((((String[0] - AML_NAME_CHAR_A + 1) & 0x1f) << 10)
                   + (((String[1] - AML_NAME_CHAR_A + 1) & 0x1f) <<  5)
                   + (((String[2] - AML_NAME_CHAR_A + 1) & 0x1f) <<  0)
                   + (UINT32)(AsciiStrHexToUint64 (&String[3]) << 16));

  //
  // Swap bytes of upper and lower WORD to format EISA ID with proper endian-ness.
  //
  EncodedEisaId = Swap4Bytes (EncodedEisaId);

  //
  // Insert DWordPrefix into list.
  //   Note: EncodedEisaId will always be 32-bits, resulting in DWordConst.
  //
  Status = AmlOPDataInteger (EncodedEisaId, ListHead);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: ERROR: Unable to create ACPI DWordConst from Encoded EISA ID.\n", __func__));
    return Status;
  }

  return Status;
}