[ACPICA]
[reactos.git] / reactos / drivers / bus / acpi / acpica / executer / exmisc.c
1 /******************************************************************************
2 *
3 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
4 *
5 *****************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2015, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
90 *
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
99 *
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
114 *
115 *****************************************************************************/
116
117 #include "acpi.h"
118 #include "accommon.h"
119 #include "acinterp.h"
120 #include "amlcode.h"
121 #include "amlresrc.h"
122
123
124 #define _COMPONENT ACPI_EXECUTER
125 ACPI_MODULE_NAME ("exmisc")
126
127
128 /*******************************************************************************
129 *
130 * FUNCTION: AcpiExGetObjectReference
131 *
132 * PARAMETERS: ObjDesc - Create a reference to this object
133 * ReturnDesc - Where to store the reference
134 * WalkState - Current state
135 *
136 * RETURN: Status
137 *
138 * DESCRIPTION: Obtain and return a "reference" to the target object
139 * Common code for the RefOfOp and the CondRefOfOp.
140 *
141 ******************************************************************************/
142
143 ACPI_STATUS
144 AcpiExGetObjectReference (
145 ACPI_OPERAND_OBJECT *ObjDesc,
146 ACPI_OPERAND_OBJECT **ReturnDesc,
147 ACPI_WALK_STATE *WalkState)
148 {
149 ACPI_OPERAND_OBJECT *ReferenceObj;
150 ACPI_OPERAND_OBJECT *ReferencedObj;
151
152
153 ACPI_FUNCTION_TRACE_PTR (ExGetObjectReference, ObjDesc);
154
155
156 *ReturnDesc = NULL;
157
158 switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc))
159 {
160 case ACPI_DESC_TYPE_OPERAND:
161
162 if (ObjDesc->Common.Type != ACPI_TYPE_LOCAL_REFERENCE)
163 {
164 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
165 }
166
167 /*
168 * Must be a reference to a Local or Arg
169 */
170 switch (ObjDesc->Reference.Class)
171 {
172 case ACPI_REFCLASS_LOCAL:
173 case ACPI_REFCLASS_ARG:
174 case ACPI_REFCLASS_DEBUG:
175
176 /* The referenced object is the pseudo-node for the local/arg */
177
178 ReferencedObj = ObjDesc->Reference.Object;
179 break;
180
181 default:
182
183 ACPI_ERROR ((AE_INFO, "Unknown Reference Class 0x%2.2X",
184 ObjDesc->Reference.Class));
185 return_ACPI_STATUS (AE_AML_INTERNAL);
186 }
187 break;
188
189 case ACPI_DESC_TYPE_NAMED:
190 /*
191 * A named reference that has already been resolved to a Node
192 */
193 ReferencedObj = ObjDesc;
194 break;
195
196 default:
197
198 ACPI_ERROR ((AE_INFO, "Invalid descriptor type 0x%X",
199 ACPI_GET_DESCRIPTOR_TYPE (ObjDesc)));
200 return_ACPI_STATUS (AE_TYPE);
201 }
202
203
204 /* Create a new reference object */
205
206 ReferenceObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
207 if (!ReferenceObj)
208 {
209 return_ACPI_STATUS (AE_NO_MEMORY);
210 }
211
212 ReferenceObj->Reference.Class = ACPI_REFCLASS_REFOF;
213 ReferenceObj->Reference.Object = ReferencedObj;
214 *ReturnDesc = ReferenceObj;
215
216 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
217 "Object %p Type [%s], returning Reference %p\n",
218 ObjDesc, AcpiUtGetObjectTypeName (ObjDesc), *ReturnDesc));
219
220 return_ACPI_STATUS (AE_OK);
221 }
222
223
224 /*******************************************************************************
225 *
226 * FUNCTION: AcpiExConcatTemplate
227 *
228 * PARAMETERS: Operand0 - First source object
229 * Operand1 - Second source object
230 * ActualReturnDesc - Where to place the return object
231 * WalkState - Current walk state
232 *
233 * RETURN: Status
234 *
235 * DESCRIPTION: Concatenate two resource templates
236 *
237 ******************************************************************************/
238
239 ACPI_STATUS
240 AcpiExConcatTemplate (
241 ACPI_OPERAND_OBJECT *Operand0,
242 ACPI_OPERAND_OBJECT *Operand1,
243 ACPI_OPERAND_OBJECT **ActualReturnDesc,
244 ACPI_WALK_STATE *WalkState)
245 {
246 ACPI_STATUS Status;
247 ACPI_OPERAND_OBJECT *ReturnDesc;
248 UINT8 *NewBuf;
249 UINT8 *EndTag;
250 ACPI_SIZE Length0;
251 ACPI_SIZE Length1;
252 ACPI_SIZE NewLength;
253
254
255 ACPI_FUNCTION_TRACE (ExConcatTemplate);
256
257
258 /*
259 * Find the EndTag descriptor in each resource template.
260 * Note1: returned pointers point TO the EndTag, not past it.
261 * Note2: zero-length buffers are allowed; treated like one EndTag
262 */
263
264 /* Get the length of the first resource template */
265
266 Status = AcpiUtGetResourceEndTag (Operand0, &EndTag);
267 if (ACPI_FAILURE (Status))
268 {
269 return_ACPI_STATUS (Status);
270 }
271
272 Length0 = ACPI_PTR_DIFF (EndTag, Operand0->Buffer.Pointer);
273
274 /* Get the length of the second resource template */
275
276 Status = AcpiUtGetResourceEndTag (Operand1, &EndTag);
277 if (ACPI_FAILURE (Status))
278 {
279 return_ACPI_STATUS (Status);
280 }
281
282 Length1 = ACPI_PTR_DIFF (EndTag, Operand1->Buffer.Pointer);
283
284 /* Combine both lengths, minimum size will be 2 for EndTag */
285
286 NewLength = Length0 + Length1 + sizeof (AML_RESOURCE_END_TAG);
287
288 /* Create a new buffer object for the result (with one EndTag) */
289
290 ReturnDesc = AcpiUtCreateBufferObject (NewLength);
291 if (!ReturnDesc)
292 {
293 return_ACPI_STATUS (AE_NO_MEMORY);
294 }
295
296 /*
297 * Copy the templates to the new buffer, 0 first, then 1 follows. One
298 * EndTag descriptor is copied from Operand1.
299 */
300 NewBuf = ReturnDesc->Buffer.Pointer;
301 memcpy (NewBuf, Operand0->Buffer.Pointer, Length0);
302 memcpy (NewBuf + Length0, Operand1->Buffer.Pointer, Length1);
303
304 /* Insert EndTag and set the checksum to zero, means "ignore checksum" */
305
306 NewBuf[NewLength - 1] = 0;
307 NewBuf[NewLength - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
308
309 /* Return the completed resource template */
310
311 *ActualReturnDesc = ReturnDesc;
312 return_ACPI_STATUS (AE_OK);
313 }
314
315
316 /*******************************************************************************
317 *
318 * FUNCTION: AcpiExDoConcatenate
319 *
320 * PARAMETERS: Operand0 - First source object
321 * Operand1 - Second source object
322 * ActualReturnDesc - Where to place the return object
323 * WalkState - Current walk state
324 *
325 * RETURN: Status
326 *
327 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
328 *
329 ******************************************************************************/
330
331 ACPI_STATUS
332 AcpiExDoConcatenate (
333 ACPI_OPERAND_OBJECT *Operand0,
334 ACPI_OPERAND_OBJECT *Operand1,
335 ACPI_OPERAND_OBJECT **ActualReturnDesc,
336 ACPI_WALK_STATE *WalkState)
337 {
338 ACPI_OPERAND_OBJECT *LocalOperand1 = Operand1;
339 ACPI_OPERAND_OBJECT *ReturnDesc;
340 char *NewBuf;
341 ACPI_STATUS Status;
342
343
344 ACPI_FUNCTION_TRACE (ExDoConcatenate);
345
346
347 /*
348 * Convert the second operand if necessary. The first operand
349 * determines the type of the second operand, (See the Data Types
350 * section of the ACPI specification.) Both object types are
351 * guaranteed to be either Integer/String/Buffer by the operand
352 * resolution mechanism.
353 */
354 switch (Operand0->Common.Type)
355 {
356 case ACPI_TYPE_INTEGER:
357
358 Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
359 break;
360
361 case ACPI_TYPE_STRING:
362
363 Status = AcpiExConvertToString (Operand1, &LocalOperand1,
364 ACPI_IMPLICIT_CONVERT_HEX);
365 break;
366
367 case ACPI_TYPE_BUFFER:
368
369 Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
370 break;
371
372 default:
373
374 ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
375 Operand0->Common.Type));
376 Status = AE_AML_INTERNAL;
377 }
378
379 if (ACPI_FAILURE (Status))
380 {
381 goto Cleanup;
382 }
383
384 /*
385 * Both operands are now known to be the same object type
386 * (Both are Integer, String, or Buffer), and we can now perform the
387 * concatenation.
388 */
389
390 /*
391 * There are three cases to handle:
392 *
393 * 1) Two Integers concatenated to produce a new Buffer
394 * 2) Two Strings concatenated to produce a new String
395 * 3) Two Buffers concatenated to produce a new Buffer
396 */
397 switch (Operand0->Common.Type)
398 {
399 case ACPI_TYPE_INTEGER:
400
401 /* Result of two Integers is a Buffer */
402 /* Need enough buffer space for two integers */
403
404 ReturnDesc = AcpiUtCreateBufferObject ((ACPI_SIZE)
405 ACPI_MUL_2 (AcpiGbl_IntegerByteWidth));
406 if (!ReturnDesc)
407 {
408 Status = AE_NO_MEMORY;
409 goto Cleanup;
410 }
411
412 NewBuf = (char *) ReturnDesc->Buffer.Pointer;
413
414 /* Copy the first integer, LSB first */
415
416 memcpy (NewBuf, &Operand0->Integer.Value,
417 AcpiGbl_IntegerByteWidth);
418
419 /* Copy the second integer (LSB first) after the first */
420
421 memcpy (NewBuf + AcpiGbl_IntegerByteWidth,
422 &LocalOperand1->Integer.Value,
423 AcpiGbl_IntegerByteWidth);
424 break;
425
426 case ACPI_TYPE_STRING:
427
428 /* Result of two Strings is a String */
429
430 ReturnDesc = AcpiUtCreateStringObject (
431 ((ACPI_SIZE) Operand0->String.Length +
432 LocalOperand1->String.Length));
433 if (!ReturnDesc)
434 {
435 Status = AE_NO_MEMORY;
436 goto Cleanup;
437 }
438
439 NewBuf = ReturnDesc->String.Pointer;
440
441 /* Concatenate the strings */
442
443 strcpy (NewBuf, Operand0->String.Pointer);
444 strcpy (NewBuf + Operand0->String.Length,
445 LocalOperand1->String.Pointer);
446 break;
447
448 case ACPI_TYPE_BUFFER:
449
450 /* Result of two Buffers is a Buffer */
451
452 ReturnDesc = AcpiUtCreateBufferObject (
453 ((ACPI_SIZE) Operand0->Buffer.Length +
454 LocalOperand1->Buffer.Length));
455 if (!ReturnDesc)
456 {
457 Status = AE_NO_MEMORY;
458 goto Cleanup;
459 }
460
461 NewBuf = (char *) ReturnDesc->Buffer.Pointer;
462
463 /* Concatenate the buffers */
464
465 memcpy (NewBuf, Operand0->Buffer.Pointer,
466 Operand0->Buffer.Length);
467 memcpy (NewBuf + Operand0->Buffer.Length,
468 LocalOperand1->Buffer.Pointer,
469 LocalOperand1->Buffer.Length);
470 break;
471
472 default:
473
474 /* Invalid object type, should not happen here */
475
476 ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
477 Operand0->Common.Type));
478 Status =AE_AML_INTERNAL;
479 goto Cleanup;
480 }
481
482 *ActualReturnDesc = ReturnDesc;
483
484 Cleanup:
485 if (LocalOperand1 != Operand1)
486 {
487 AcpiUtRemoveReference (LocalOperand1);
488 }
489 return_ACPI_STATUS (Status);
490 }
491
492
493 /*******************************************************************************
494 *
495 * FUNCTION: AcpiExDoMathOp
496 *
497 * PARAMETERS: Opcode - AML opcode
498 * Integer0 - Integer operand #0
499 * Integer1 - Integer operand #1
500 *
501 * RETURN: Integer result of the operation
502 *
503 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
504 * math functions here is to prevent a lot of pointer dereferencing
505 * to obtain the operands.
506 *
507 ******************************************************************************/
508
509 UINT64
510 AcpiExDoMathOp (
511 UINT16 Opcode,
512 UINT64 Integer0,
513 UINT64 Integer1)
514 {
515
516 ACPI_FUNCTION_ENTRY ();
517
518
519 switch (Opcode)
520 {
521 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
522
523 return (Integer0 + Integer1);
524
525 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
526
527 return (Integer0 & Integer1);
528
529 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
530
531 return (~(Integer0 & Integer1));
532
533 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
534
535 return (Integer0 | Integer1);
536
537 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
538
539 return (~(Integer0 | Integer1));
540
541 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
542
543 return (Integer0 ^ Integer1);
544
545 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
546
547 return (Integer0 * Integer1);
548
549 case AML_SHIFT_LEFT_OP: /* ShiftLeft (Operand, ShiftCount, Result)*/
550
551 /*
552 * We need to check if the shiftcount is larger than the integer bit
553 * width since the behavior of this is not well-defined in the C language.
554 */
555 if (Integer1 >= AcpiGbl_IntegerBitWidth)
556 {
557 return (0);
558 }
559 return (Integer0 << Integer1);
560
561 case AML_SHIFT_RIGHT_OP: /* ShiftRight (Operand, ShiftCount, Result) */
562
563 /*
564 * We need to check if the shiftcount is larger than the integer bit
565 * width since the behavior of this is not well-defined in the C language.
566 */
567 if (Integer1 >= AcpiGbl_IntegerBitWidth)
568 {
569 return (0);
570 }
571 return (Integer0 >> Integer1);
572
573 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
574
575 return (Integer0 - Integer1);
576
577 default:
578
579 return (0);
580 }
581 }
582
583
584 /*******************************************************************************
585 *
586 * FUNCTION: AcpiExDoLogicalNumericOp
587 *
588 * PARAMETERS: Opcode - AML opcode
589 * Integer0 - Integer operand #0
590 * Integer1 - Integer operand #1
591 * LogicalResult - TRUE/FALSE result of the operation
592 *
593 * RETURN: Status
594 *
595 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
596 * operators (LAnd and LOr), both operands must be integers.
597 *
598 * Note: cleanest machine code seems to be produced by the code
599 * below, rather than using statements of the form:
600 * Result = (Integer0 && Integer1);
601 *
602 ******************************************************************************/
603
604 ACPI_STATUS
605 AcpiExDoLogicalNumericOp (
606 UINT16 Opcode,
607 UINT64 Integer0,
608 UINT64 Integer1,
609 BOOLEAN *LogicalResult)
610 {
611 ACPI_STATUS Status = AE_OK;
612 BOOLEAN LocalResult = FALSE;
613
614
615 ACPI_FUNCTION_TRACE (ExDoLogicalNumericOp);
616
617
618 switch (Opcode)
619 {
620 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
621
622 if (Integer0 && Integer1)
623 {
624 LocalResult = TRUE;
625 }
626 break;
627
628 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
629
630 if (Integer0 || Integer1)
631 {
632 LocalResult = TRUE;
633 }
634 break;
635
636 default:
637
638 Status = AE_AML_INTERNAL;
639 break;
640 }
641
642 /* Return the logical result and status */
643
644 *LogicalResult = LocalResult;
645 return_ACPI_STATUS (Status);
646 }
647
648
649 /*******************************************************************************
650 *
651 * FUNCTION: AcpiExDoLogicalOp
652 *
653 * PARAMETERS: Opcode - AML opcode
654 * Operand0 - operand #0
655 * Operand1 - operand #1
656 * LogicalResult - TRUE/FALSE result of the operation
657 *
658 * RETURN: Status
659 *
660 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
661 * functions here is to prevent a lot of pointer dereferencing
662 * to obtain the operands and to simplify the generation of the
663 * logical value. For the Numeric operators (LAnd and LOr), both
664 * operands must be integers. For the other logical operators,
665 * operands can be any combination of Integer/String/Buffer. The
666 * first operand determines the type to which the second operand
667 * will be converted.
668 *
669 * Note: cleanest machine code seems to be produced by the code
670 * below, rather than using statements of the form:
671 * Result = (Operand0 == Operand1);
672 *
673 ******************************************************************************/
674
675 ACPI_STATUS
676 AcpiExDoLogicalOp (
677 UINT16 Opcode,
678 ACPI_OPERAND_OBJECT *Operand0,
679 ACPI_OPERAND_OBJECT *Operand1,
680 BOOLEAN *LogicalResult)
681 {
682 ACPI_OPERAND_OBJECT *LocalOperand1 = Operand1;
683 UINT64 Integer0;
684 UINT64 Integer1;
685 UINT32 Length0;
686 UINT32 Length1;
687 ACPI_STATUS Status = AE_OK;
688 BOOLEAN LocalResult = FALSE;
689 int Compare;
690
691
692 ACPI_FUNCTION_TRACE (ExDoLogicalOp);
693
694
695 /*
696 * Convert the second operand if necessary. The first operand
697 * determines the type of the second operand, (See the Data Types
698 * section of the ACPI 3.0+ specification.) Both object types are
699 * guaranteed to be either Integer/String/Buffer by the operand
700 * resolution mechanism.
701 */
702 switch (Operand0->Common.Type)
703 {
704 case ACPI_TYPE_INTEGER:
705
706 Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
707 break;
708
709 case ACPI_TYPE_STRING:
710
711 Status = AcpiExConvertToString (Operand1, &LocalOperand1,
712 ACPI_IMPLICIT_CONVERT_HEX);
713 break;
714
715 case ACPI_TYPE_BUFFER:
716
717 Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
718 break;
719
720 default:
721
722 Status = AE_AML_INTERNAL;
723 break;
724 }
725
726 if (ACPI_FAILURE (Status))
727 {
728 goto Cleanup;
729 }
730
731 /*
732 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
733 */
734 if (Operand0->Common.Type == ACPI_TYPE_INTEGER)
735 {
736 /*
737 * 1) Both operands are of type integer
738 * Note: LocalOperand1 may have changed above
739 */
740 Integer0 = Operand0->Integer.Value;
741 Integer1 = LocalOperand1->Integer.Value;
742
743 switch (Opcode)
744 {
745 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
746
747 if (Integer0 == Integer1)
748 {
749 LocalResult = TRUE;
750 }
751 break;
752
753 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
754
755 if (Integer0 > Integer1)
756 {
757 LocalResult = TRUE;
758 }
759 break;
760
761 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
762
763 if (Integer0 < Integer1)
764 {
765 LocalResult = TRUE;
766 }
767 break;
768
769 default:
770
771 Status = AE_AML_INTERNAL;
772 break;
773 }
774 }
775 else
776 {
777 /*
778 * 2) Both operands are Strings or both are Buffers
779 * Note: Code below takes advantage of common Buffer/String
780 * object fields. LocalOperand1 may have changed above. Use
781 * memcmp to handle nulls in buffers.
782 */
783 Length0 = Operand0->Buffer.Length;
784 Length1 = LocalOperand1->Buffer.Length;
785
786 /* Lexicographic compare: compare the data bytes */
787
788 Compare = memcmp (Operand0->Buffer.Pointer,
789 LocalOperand1->Buffer.Pointer,
790 (Length0 > Length1) ? Length1 : Length0);
791
792 switch (Opcode)
793 {
794 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
795
796 /* Length and all bytes must be equal */
797
798 if ((Length0 == Length1) &&
799 (Compare == 0))
800 {
801 /* Length and all bytes match ==> TRUE */
802
803 LocalResult = TRUE;
804 }
805 break;
806
807 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
808
809 if (Compare > 0)
810 {
811 LocalResult = TRUE;
812 goto Cleanup; /* TRUE */
813 }
814 if (Compare < 0)
815 {
816 goto Cleanup; /* FALSE */
817 }
818
819 /* Bytes match (to shortest length), compare lengths */
820
821 if (Length0 > Length1)
822 {
823 LocalResult = TRUE;
824 }
825 break;
826
827 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
828
829 if (Compare > 0)
830 {
831 goto Cleanup; /* FALSE */
832 }
833 if (Compare < 0)
834 {
835 LocalResult = TRUE;
836 goto Cleanup; /* TRUE */
837 }
838
839 /* Bytes match (to shortest length), compare lengths */
840
841 if (Length0 < Length1)
842 {
843 LocalResult = TRUE;
844 }
845 break;
846
847 default:
848
849 Status = AE_AML_INTERNAL;
850 break;
851 }
852 }
853
854 Cleanup:
855
856 /* New object was created if implicit conversion performed - delete */
857
858 if (LocalOperand1 != Operand1)
859 {
860 AcpiUtRemoveReference (LocalOperand1);
861 }
862
863 /* Return the logical result and status */
864
865 *LogicalResult = LocalResult;
866 return_ACPI_STATUS (Status);
867 }