Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / compiler / dtutils.c
blobe7376f43d09d09c137c514e479cc33c85de00194
1 /******************************************************************************
3 * Module Name: dtutils.c - Utility routines for the data table compiler
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #define __DTUTILS_C__
46 #include "aslcompiler.h"
47 #include "dtcompiler.h"
48 #include "actables.h"
50 #define _COMPONENT DT_COMPILER
51 ACPI_MODULE_NAME ("dtutils")
53 /* Local prototypes */
55 static void
56 DtSum (
57 DT_SUBTABLE *Subtable,
58 void *Context,
59 void *ReturnValue);
62 /******************************************************************************
64 * FUNCTION: DtError
66 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
67 * MessageId - Index into global message buffer
68 * Op - Parse node where error happened
69 * ExtraMessage - additional error message
71 * RETURN: None
73 * DESCRIPTION: Common error interface for data table compiler
75 *****************************************************************************/
77 void
78 DtError (
79 UINT8 Level,
80 UINT8 MessageId,
81 DT_FIELD *FieldObject,
82 char *ExtraMessage)
85 /* Check if user wants to ignore this exception */
87 if (AslIsExceptionDisabled (Level, MessageId))
89 return;
92 if (FieldObject)
94 AslCommonError (Level, MessageId,
95 FieldObject->Line,
96 FieldObject->Line,
97 FieldObject->ByteOffset,
98 FieldObject->Column,
99 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
101 else
103 AslCommonError (Level, MessageId, 0,
104 0, 0, 0, 0, ExtraMessage);
109 /******************************************************************************
111 * FUNCTION: DtNameError
113 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
114 * MessageId - Index into global message buffer
115 * Op - Parse node where error happened
116 * ExtraMessage - additional error message
118 * RETURN: None
120 * DESCRIPTION: Error interface for named objects
122 *****************************************************************************/
124 void
125 DtNameError (
126 UINT8 Level,
127 UINT8 MessageId,
128 DT_FIELD *FieldObject,
129 char *ExtraMessage)
132 switch (Level)
134 case ASL_WARNING2:
135 case ASL_WARNING3:
137 if (Gbl_WarningLevel < Level)
139 return;
141 break;
143 default:
145 break;
148 if (FieldObject)
150 AslCommonError (Level, MessageId,
151 FieldObject->Line,
152 FieldObject->Line,
153 FieldObject->ByteOffset,
154 FieldObject->NameColumn,
155 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
157 else
159 AslCommonError (Level, MessageId, 0,
160 0, 0, 0, 0, ExtraMessage);
165 /*******************************************************************************
167 * FUNCTION: DtFatal
169 * PARAMETERS: None
171 * RETURN: None
173 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
174 * compile or I/O errors
176 ******************************************************************************/
178 void
179 DtFatal (
180 UINT8 MessageId,
181 DT_FIELD *FieldObject,
182 char *ExtraMessage)
185 DtError (ASL_ERROR, MessageId, FieldObject, ExtraMessage);
188 * TBD: remove this entire function, DtFatal
190 * We cannot abort the compiler on error, because we may be compiling a
191 * list of files. We must move on to the next file.
193 #ifdef __OBSOLETE
194 CmCleanupAndExit ();
195 exit (1);
196 #endif
200 /******************************************************************************
202 * FUNCTION: DtStrtoul64
204 * PARAMETERS: String - Null terminated string
205 * ReturnInteger - Where the converted integer is returned
207 * RETURN: Status
209 * DESCRIPTION: Simple conversion of a string hex integer constant to unsigned
210 * value. Assumes no leading "0x" for the constant.
212 * Portability note: The reason this function exists is because a 64-bit
213 * sscanf is not available in all environments.
215 *****************************************************************************/
217 ACPI_STATUS
218 DtStrtoul64 (
219 char *String,
220 UINT64 *ReturnInteger)
222 char *ThisChar = String;
223 UINT32 ThisDigit;
224 UINT64 ReturnValue = 0;
225 int DigitCount = 0;
228 /* Skip over any white space in the buffer */
230 while ((*ThisChar == ' ') || (*ThisChar == '\t'))
232 ThisChar++;
235 /* Skip leading zeros */
237 while ((*ThisChar) == '0')
239 ThisChar++;
242 /* Convert character-by-character */
244 while (*ThisChar)
246 if (ACPI_IS_DIGIT (*ThisChar))
248 /* Convert ASCII 0-9 to Decimal value */
250 ThisDigit = ((UINT8) *ThisChar) - '0';
252 else /* Letter */
254 ThisDigit = (UINT32) ACPI_TOUPPER (*ThisChar);
255 if (!ACPI_IS_XDIGIT ((char) ThisDigit))
257 /* Not A-F */
259 return (AE_BAD_CHARACTER);
262 /* Convert ASCII Hex char (A-F) to value */
264 ThisDigit = (ThisDigit - 'A') + 10;
267 /* Insert the 4-bit hex digit */
269 ReturnValue <<= 4;
270 ReturnValue += ThisDigit;
272 ThisChar++;
273 DigitCount++;
274 if (DigitCount > 16)
276 /* Value is too large (> 64 bits/8 bytes/16 hex digits) */
278 return (AE_LIMIT);
282 *ReturnInteger = ReturnValue;
283 return (AE_OK);
287 /******************************************************************************
289 * FUNCTION: DtGetFileSize
291 * PARAMETERS: Handle - Open file handler
293 * RETURN: Current file size
295 * DESCRIPTION: Get the current size of a file. Seek to the EOF and get the
296 * offset. Seek back to the original location.
298 *****************************************************************************/
300 UINT32
301 DtGetFileSize (
302 FILE *Handle)
304 int CurrentOffset;
305 int LastOffset;
308 CurrentOffset = ftell (Handle);
309 fseek (Handle, 0, SEEK_END);
310 LastOffset = ftell (Handle);
311 fseek (Handle, CurrentOffset, SEEK_SET);
313 return ((UINT32) LastOffset);
317 /******************************************************************************
319 * FUNCTION: DtGetFieldValue
321 * PARAMETERS: Field - Current field list pointer
323 * RETURN: Field value
325 * DESCRIPTION: Get field value
327 *****************************************************************************/
329 char *
330 DtGetFieldValue (
331 DT_FIELD *Field)
333 if (!Field)
335 return (NULL);
338 return (Field->Value);
342 /******************************************************************************
344 * FUNCTION: DtGetFieldType
346 * PARAMETERS: Info - Data table info
348 * RETURN: Field type
350 * DESCRIPTION: Get field type
352 *****************************************************************************/
354 UINT8
355 DtGetFieldType (
356 ACPI_DMTABLE_INFO *Info)
358 UINT8 Type;
361 /* DT_FLAG means that this is the start of a block of flag bits */
362 /* TBD - we can make these a separate opcode later */
364 if (Info->Flags & DT_FLAG)
366 return (DT_FIELD_TYPE_FLAGS_INTEGER);
369 /* Type is based upon the opcode for this field in the info table */
371 switch (Info->Opcode)
373 case ACPI_DMT_FLAG0:
374 case ACPI_DMT_FLAG1:
375 case ACPI_DMT_FLAG2:
376 case ACPI_DMT_FLAG3:
377 case ACPI_DMT_FLAG4:
378 case ACPI_DMT_FLAG5:
379 case ACPI_DMT_FLAG6:
380 case ACPI_DMT_FLAG7:
381 case ACPI_DMT_FLAGS0:
382 case ACPI_DMT_FLAGS1:
383 case ACPI_DMT_FLAGS2:
384 case ACPI_DMT_FLAGS4:
386 Type = DT_FIELD_TYPE_FLAG;
387 break;
389 case ACPI_DMT_NAME4:
390 case ACPI_DMT_SIG:
391 case ACPI_DMT_NAME6:
392 case ACPI_DMT_NAME8:
393 case ACPI_DMT_STRING:
395 Type = DT_FIELD_TYPE_STRING;
396 break;
398 case ACPI_DMT_BUFFER:
399 case ACPI_DMT_BUF7:
400 case ACPI_DMT_BUF10:
401 case ACPI_DMT_BUF16:
402 case ACPI_DMT_BUF128:
403 case ACPI_DMT_PCI_PATH:
405 Type = DT_FIELD_TYPE_BUFFER;
406 break;
408 case ACPI_DMT_GAS:
409 case ACPI_DMT_HESTNTFY:
411 Type = DT_FIELD_TYPE_INLINE_SUBTABLE;
412 break;
414 case ACPI_DMT_UNICODE:
416 Type = DT_FIELD_TYPE_UNICODE;
417 break;
419 case ACPI_DMT_UUID:
421 Type = DT_FIELD_TYPE_UUID;
422 break;
424 case ACPI_DMT_DEVICE_PATH:
426 Type = DT_FIELD_TYPE_DEVICE_PATH;
427 break;
429 case ACPI_DMT_LABEL:
431 Type = DT_FIELD_TYPE_LABEL;
432 break;
434 default:
436 Type = DT_FIELD_TYPE_INTEGER;
437 break;
440 return (Type);
444 /******************************************************************************
446 * FUNCTION: DtGetBufferLength
448 * PARAMETERS: Buffer - List of integers,
449 * for example "10 3A 4F 2E"
451 * RETURN: Count of integer
453 * DESCRIPTION: Get length of bytes needed to store the integers
455 *****************************************************************************/
457 UINT32
458 DtGetBufferLength (
459 char *Buffer)
461 UINT32 ByteLength = 0;
464 while (*Buffer)
466 if (*Buffer == ' ')
468 ByteLength++;
470 while (*Buffer == ' ')
472 Buffer++;
476 Buffer++;
479 return (++ByteLength);
483 /******************************************************************************
485 * FUNCTION: DtGetFieldLength
487 * PARAMETERS: Field - Current field
488 * Info - Data table info
490 * RETURN: Field length
492 * DESCRIPTION: Get length of bytes needed to compile the field
494 * Note: This function must remain in sync with AcpiDmDumpTable.
496 *****************************************************************************/
498 UINT32
499 DtGetFieldLength (
500 DT_FIELD *Field,
501 ACPI_DMTABLE_INFO *Info)
503 UINT32 ByteLength = 0;
504 char *Value;
507 /* Length is based upon the opcode for this field in the info table */
509 switch (Info->Opcode)
511 case ACPI_DMT_FLAG0:
512 case ACPI_DMT_FLAG1:
513 case ACPI_DMT_FLAG2:
514 case ACPI_DMT_FLAG3:
515 case ACPI_DMT_FLAG4:
516 case ACPI_DMT_FLAG5:
517 case ACPI_DMT_FLAG6:
518 case ACPI_DMT_FLAG7:
519 case ACPI_DMT_FLAGS0:
520 case ACPI_DMT_FLAGS1:
521 case ACPI_DMT_FLAGS2:
522 case ACPI_DMT_FLAGS4:
523 case ACPI_DMT_LABEL:
524 case ACPI_DMT_EXTRA_TEXT:
526 ByteLength = 0;
527 break;
529 case ACPI_DMT_UINT8:
530 case ACPI_DMT_CHKSUM:
531 case ACPI_DMT_SPACEID:
532 case ACPI_DMT_ACCWIDTH:
533 case ACPI_DMT_IVRS:
534 case ACPI_DMT_MADT:
535 case ACPI_DMT_PMTT:
536 case ACPI_DMT_SRAT:
537 case ACPI_DMT_ASF:
538 case ACPI_DMT_HESTNTYP:
539 case ACPI_DMT_FADTPM:
540 case ACPI_DMT_EINJACT:
541 case ACPI_DMT_EINJINST:
542 case ACPI_DMT_ERSTACT:
543 case ACPI_DMT_ERSTINST:
545 ByteLength = 1;
546 break;
548 case ACPI_DMT_UINT16:
549 case ACPI_DMT_DMAR:
550 case ACPI_DMT_HEST:
551 case ACPI_DMT_PCI_PATH:
553 ByteLength = 2;
554 break;
556 case ACPI_DMT_UINT24:
558 ByteLength = 3;
559 break;
561 case ACPI_DMT_UINT32:
562 case ACPI_DMT_NAME4:
563 case ACPI_DMT_SLIC:
564 case ACPI_DMT_SIG:
566 ByteLength = 4;
567 break;
569 case ACPI_DMT_UINT40:
571 ByteLength = 5;
572 break;
574 case ACPI_DMT_UINT48:
575 case ACPI_DMT_NAME6:
577 ByteLength = 6;
578 break;
580 case ACPI_DMT_UINT56:
581 case ACPI_DMT_BUF7:
583 ByteLength = 7;
584 break;
586 case ACPI_DMT_UINT64:
587 case ACPI_DMT_NAME8:
589 ByteLength = 8;
590 break;
592 case ACPI_DMT_STRING:
594 Value = DtGetFieldValue (Field);
595 if (Value)
597 ByteLength = ACPI_STRLEN (Value) + 1;
599 else
600 { /* At this point, this is a fatal error */
602 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
603 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
604 return (0);
606 break;
608 case ACPI_DMT_GAS:
610 ByteLength = sizeof (ACPI_GENERIC_ADDRESS);
611 break;
613 case ACPI_DMT_HESTNTFY:
615 ByteLength = sizeof (ACPI_HEST_NOTIFY);
616 break;
618 case ACPI_DMT_BUFFER:
620 Value = DtGetFieldValue (Field);
621 if (Value)
623 ByteLength = DtGetBufferLength (Value);
625 else
626 { /* At this point, this is a fatal error */
628 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
629 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
630 return (0);
632 break;
634 case ACPI_DMT_BUF10:
636 ByteLength = 10;
637 break;
639 case ACPI_DMT_BUF16:
640 case ACPI_DMT_UUID:
642 ByteLength = 16;
643 break;
645 case ACPI_DMT_BUF128:
647 ByteLength = 128;
648 break;
650 case ACPI_DMT_UNICODE:
652 Value = DtGetFieldValue (Field);
654 /* TBD: error if Value is NULL? (as below?) */
656 ByteLength = (ACPI_STRLEN (Value) + 1) * sizeof(UINT16);
657 break;
659 default:
661 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid table opcode");
662 return (0);
665 return (ByteLength);
669 /******************************************************************************
671 * FUNCTION: DtSum
673 * PARAMETERS: DT_WALK_CALLBACK:
674 * Subtable - Subtable
675 * Context - Unused
676 * ReturnValue - Store the checksum of subtable
678 * RETURN: Status
680 * DESCRIPTION: Get the checksum of subtable
682 *****************************************************************************/
684 static void
685 DtSum (
686 DT_SUBTABLE *Subtable,
687 void *Context,
688 void *ReturnValue)
690 UINT8 Checksum;
691 UINT8 *Sum = ReturnValue;
694 Checksum = AcpiTbChecksum (Subtable->Buffer, Subtable->Length);
695 *Sum = (UINT8) (*Sum + Checksum);
699 /******************************************************************************
701 * FUNCTION: DtSetTableChecksum
703 * PARAMETERS: ChecksumPointer - Where to return the checksum
705 * RETURN: None
707 * DESCRIPTION: Set checksum of the whole data table into the checksum field
709 *****************************************************************************/
711 void
712 DtSetTableChecksum (
713 UINT8 *ChecksumPointer)
715 UINT8 Checksum = 0;
716 UINT8 OldSum;
719 DtWalkTableTree (Gbl_RootTable, DtSum, NULL, &Checksum);
721 OldSum = *ChecksumPointer;
722 Checksum = (UINT8) (Checksum - OldSum);
724 /* Compute the final checksum */
726 Checksum = (UINT8) (0 - Checksum);
727 *ChecksumPointer = Checksum;
731 /******************************************************************************
733 * FUNCTION: DtSetTableLength
735 * PARAMETERS: None
737 * RETURN: None
739 * DESCRIPTION: Walk the subtables and set all the length fields
741 *****************************************************************************/
743 void
744 DtSetTableLength (
745 void)
747 DT_SUBTABLE *ParentTable;
748 DT_SUBTABLE *ChildTable;
751 ParentTable = Gbl_RootTable;
752 ChildTable = NULL;
754 if (!ParentTable)
756 return;
759 DtSetSubtableLength (ParentTable);
761 while (1)
763 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
764 if (ChildTable)
766 if (ChildTable->LengthField)
768 DtSetSubtableLength (ChildTable);
771 if (ChildTable->Child)
773 ParentTable = ChildTable;
774 ChildTable = NULL;
776 else
778 ParentTable->TotalLength += ChildTable->TotalLength;
779 if (ParentTable->LengthField)
781 DtSetSubtableLength (ParentTable);
785 else
787 ChildTable = ParentTable;
789 if (ChildTable == Gbl_RootTable)
791 break;
794 ParentTable = DtGetParentSubtable (ParentTable);
796 ParentTable->TotalLength += ChildTable->TotalLength;
797 if (ParentTable->LengthField)
799 DtSetSubtableLength (ParentTable);
806 /******************************************************************************
808 * FUNCTION: DtWalkTableTree
810 * PARAMETERS: StartTable - Subtable in the tree where walking begins
811 * UserFunction - Called during the walk
812 * Context - Passed to user function
813 * ReturnValue - The return value of UserFunction
815 * RETURN: None
817 * DESCRIPTION: Performs a depth-first walk of the subtable tree
819 *****************************************************************************/
821 void
822 DtWalkTableTree (
823 DT_SUBTABLE *StartTable,
824 DT_WALK_CALLBACK UserFunction,
825 void *Context,
826 void *ReturnValue)
828 DT_SUBTABLE *ParentTable;
829 DT_SUBTABLE *ChildTable;
832 ParentTable = StartTable;
833 ChildTable = NULL;
835 if (!ParentTable)
837 return;
840 UserFunction (ParentTable, Context, ReturnValue);
842 while (1)
844 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
845 if (ChildTable)
847 UserFunction (ChildTable, Context, ReturnValue);
849 if (ChildTable->Child)
851 ParentTable = ChildTable;
852 ChildTable = NULL;
855 else
857 ChildTable = ParentTable;
858 if (ChildTable == Gbl_RootTable)
860 break;
863 ParentTable = DtGetParentSubtable (ParentTable);
865 if (ChildTable->Peer == StartTable)
867 break;
874 /******************************************************************************
876 * FUNCTION: DtFreeFieldList
878 * PARAMETERS: None
880 * RETURN: None
882 * DESCRIPTION: Free the field list
884 *****************************************************************************/
886 void
887 DtFreeFieldList (
888 void)
890 DT_FIELD *Field = Gbl_FieldList;
891 DT_FIELD *NextField;
894 /* Walk and free entire field list */
896 while (Field)
898 NextField = Field->Next; /* Save link */
900 if (!(Field->Flags & DT_FIELD_NOT_ALLOCATED))
902 ACPI_FREE (Field->Name);
903 ACPI_FREE (Field->Value);
906 ACPI_FREE (Field);
907 Field = NextField;