1 /****************************************************************************** 2 * 3 * Module Name: nsconvert - Object conversions for objects returned by 4 * predefined methods 5 * 6 *****************************************************************************/ 7 8 /* 9 * Copyright (C) 2000 - 2015, Intel Corp. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions, and the following disclaimer, 17 * without modification. 18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 19 * substantially similar to the "NO WARRANTY" disclaimer below 20 * ("Disclaimer") and any redistribution must be conditioned upon 21 * including a substantially similar Disclaimer requirement for further 22 * binary redistribution. 23 * 3. Neither the names of the above-listed copyright holders nor the names 24 * of any contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * Alternatively, this software may be distributed under the terms of the 28 * GNU General Public License ("GPL") version 2 as published by the Free 29 * Software Foundation. 30 * 31 * NO WARRANTY 32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 42 * POSSIBILITY OF SUCH DAMAGES. 43 */ 44 45 #include "acpi.h" 46 #include "accommon.h" 47 #include "acnamesp.h" 48 #include "acinterp.h" 49 #include "acpredef.h" 50 #include "amlresrc.h" 51 52 #define _COMPONENT ACPI_NAMESPACE 53 ACPI_MODULE_NAME ("nsconvert") 54 55 56 /******************************************************************************* 57 * 58 * FUNCTION: AcpiNsConvertToInteger 59 * 60 * PARAMETERS: OriginalObject - Object to be converted 61 * ReturnObject - Where the new converted object is returned 62 * 63 * RETURN: Status. AE_OK if conversion was successful. 64 * 65 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer. 66 * 67 ******************************************************************************/ 68 69 ACPI_STATUS 70 AcpiNsConvertToInteger ( 71 ACPI_OPERAND_OBJECT *OriginalObject, 72 ACPI_OPERAND_OBJECT **ReturnObject) 73 { 74 ACPI_OPERAND_OBJECT *NewObject; 75 ACPI_STATUS Status; 76 UINT64 Value = 0; 77 UINT32 i; 78 79 80 switch (OriginalObject->Common.Type) 81 { 82 case ACPI_TYPE_STRING: 83 84 /* String-to-Integer conversion */ 85 86 Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer, 87 ACPI_ANY_BASE, &Value); 88 if (ACPI_FAILURE (Status)) 89 { 90 return (Status); 91 } 92 break; 93 94 case ACPI_TYPE_BUFFER: 95 96 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */ 97 98 if (OriginalObject->Buffer.Length > 8) 99 { 100 return (AE_AML_OPERAND_TYPE); 101 } 102 103 /* Extract each buffer byte to create the integer */ 104 105 for (i = 0; i < OriginalObject->Buffer.Length; i++) 106 { 107 Value |= ((UINT64) 108 OriginalObject->Buffer.Pointer[i] << (i * 8)); 109 } 110 break; 111 112 default: 113 114 return (AE_AML_OPERAND_TYPE); 115 } 116 117 NewObject = AcpiUtCreateIntegerObject (Value); 118 if (!NewObject) 119 { 120 return (AE_NO_MEMORY); 121 } 122 123 *ReturnObject = NewObject; 124 return (AE_OK); 125 } 126 127 128 /******************************************************************************* 129 * 130 * FUNCTION: AcpiNsConvertToString 131 * 132 * PARAMETERS: OriginalObject - Object to be converted 133 * ReturnObject - Where the new converted object is returned 134 * 135 * RETURN: Status. AE_OK if conversion was successful. 136 * 137 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String. 138 * 139 ******************************************************************************/ 140 141 ACPI_STATUS 142 AcpiNsConvertToString ( 143 ACPI_OPERAND_OBJECT *OriginalObject, 144 ACPI_OPERAND_OBJECT **ReturnObject) 145 { 146 ACPI_OPERAND_OBJECT *NewObject; 147 ACPI_SIZE Length; 148 ACPI_STATUS Status; 149 150 151 switch (OriginalObject->Common.Type) 152 { 153 case ACPI_TYPE_INTEGER: 154 /* 155 * Integer-to-String conversion. Commonly, convert 156 * an integer of value 0 to a NULL string. The last element of 157 * _BIF and _BIX packages occasionally need this fix. 158 */ 159 if (OriginalObject->Integer.Value == 0) 160 { 161 /* Allocate a new NULL string object */ 162 163 NewObject = AcpiUtCreateStringObject (0); 164 if (!NewObject) 165 { 166 return (AE_NO_MEMORY); 167 } 168 } 169 else 170 { 171 Status = AcpiExConvertToString (OriginalObject, 172 &NewObject, ACPI_IMPLICIT_CONVERT_HEX); 173 if (ACPI_FAILURE (Status)) 174 { 175 return (Status); 176 } 177 } 178 break; 179 180 case ACPI_TYPE_BUFFER: 181 /* 182 * Buffer-to-String conversion. Use a ToString 183 * conversion, no transform performed on the buffer data. The best 184 * example of this is the _BIF method, where the string data from 185 * the battery is often (incorrectly) returned as buffer object(s). 186 */ 187 Length = 0; 188 while ((Length < OriginalObject->Buffer.Length) && 189 (OriginalObject->Buffer.Pointer[Length])) 190 { 191 Length++; 192 } 193 194 /* Allocate a new string object */ 195 196 NewObject = AcpiUtCreateStringObject (Length); 197 if (!NewObject) 198 { 199 return (AE_NO_MEMORY); 200 } 201 202 /* 203 * Copy the raw buffer data with no transform. String is already NULL 204 * terminated at Length+1. 205 */ 206 memcpy (NewObject->String.Pointer, 207 OriginalObject->Buffer.Pointer, Length); 208 break; 209 210 default: 211 212 return (AE_AML_OPERAND_TYPE); 213 } 214 215 *ReturnObject = NewObject; 216 return (AE_OK); 217 } 218 219 220 /******************************************************************************* 221 * 222 * FUNCTION: AcpiNsConvertToBuffer 223 * 224 * PARAMETERS: OriginalObject - Object to be converted 225 * ReturnObject - Where the new converted object is returned 226 * 227 * RETURN: Status. AE_OK if conversion was successful. 228 * 229 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer. 230 * 231 ******************************************************************************/ 232 233 ACPI_STATUS 234 AcpiNsConvertToBuffer ( 235 ACPI_OPERAND_OBJECT *OriginalObject, 236 ACPI_OPERAND_OBJECT **ReturnObject) 237 { 238 ACPI_OPERAND_OBJECT *NewObject; 239 ACPI_STATUS Status; 240 ACPI_OPERAND_OBJECT **Elements; 241 UINT32 *DwordBuffer; 242 UINT32 Count; 243 UINT32 i; 244 245 246 switch (OriginalObject->Common.Type) 247 { 248 case ACPI_TYPE_INTEGER: 249 /* 250 * Integer-to-Buffer conversion. 251 * Convert the Integer to a packed-byte buffer. _MAT and other 252 * objects need this sometimes, if a read has been performed on a 253 * Field object that is less than or equal to the global integer 254 * size (32 or 64 bits). 255 */ 256 Status = AcpiExConvertToBuffer (OriginalObject, &NewObject); 257 if (ACPI_FAILURE (Status)) 258 { 259 return (Status); 260 } 261 break; 262 263 case ACPI_TYPE_STRING: 264 265 /* String-to-Buffer conversion. Simple data copy */ 266 267 NewObject = AcpiUtCreateBufferObject 268 (OriginalObject->String.Length); 269 if (!NewObject) 270 { 271 return (AE_NO_MEMORY); 272 } 273 274 memcpy (NewObject->Buffer.Pointer, 275 OriginalObject->String.Pointer, OriginalObject->String.Length); 276 break; 277 278 case ACPI_TYPE_PACKAGE: 279 /* 280 * This case is often seen for predefined names that must return a 281 * Buffer object with multiple DWORD integers within. For example, 282 * _FDE and _GTM. The Package can be converted to a Buffer. 283 */ 284 285 /* All elements of the Package must be integers */ 286 287 Elements = OriginalObject->Package.Elements; 288 Count = OriginalObject->Package.Count; 289 290 for (i = 0; i < Count; i++) 291 { 292 if ((!*Elements) || 293 ((*Elements)->Common.Type != ACPI_TYPE_INTEGER)) 294 { 295 return (AE_AML_OPERAND_TYPE); 296 } 297 Elements++; 298 } 299 300 /* Create the new buffer object to replace the Package */ 301 302 NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count)); 303 if (!NewObject) 304 { 305 return (AE_NO_MEMORY); 306 } 307 308 /* Copy the package elements (integers) to the buffer as DWORDs */ 309 310 Elements = OriginalObject->Package.Elements; 311 DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer); 312 313 for (i = 0; i < Count; i++) 314 { 315 *DwordBuffer = (UINT32) (*Elements)->Integer.Value; 316 DwordBuffer++; 317 Elements++; 318 } 319 break; 320 321 default: 322 323 return (AE_AML_OPERAND_TYPE); 324 } 325 326 *ReturnObject = NewObject; 327 return (AE_OK); 328 } 329 330 331 /******************************************************************************* 332 * 333 * FUNCTION: AcpiNsConvertToUnicode 334 * 335 * PARAMETERS: OriginalObject - ASCII String Object to be converted 336 * ReturnObject - Where the new converted object is returned 337 * 338 * RETURN: Status. AE_OK if conversion was successful. 339 * 340 * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer. 341 * 342 ******************************************************************************/ 343 344 ACPI_STATUS 345 AcpiNsConvertToUnicode ( 346 ACPI_OPERAND_OBJECT *OriginalObject, 347 ACPI_OPERAND_OBJECT **ReturnObject) 348 { 349 ACPI_OPERAND_OBJECT *NewObject; 350 char *AsciiString; 351 UINT16 *UnicodeBuffer; 352 UINT32 UnicodeLength; 353 UINT32 i; 354 355 356 if (!OriginalObject) 357 { 358 return (AE_OK); 359 } 360 361 /* If a Buffer was returned, it must be at least two bytes long */ 362 363 if (OriginalObject->Common.Type == ACPI_TYPE_BUFFER) 364 { 365 if (OriginalObject->Buffer.Length < 2) 366 { 367 return (AE_AML_OPERAND_VALUE); 368 } 369 370 *ReturnObject = NULL; 371 return (AE_OK); 372 } 373 374 /* 375 * The original object is an ASCII string. Convert this string to 376 * a unicode buffer. 377 */ 378 AsciiString = OriginalObject->String.Pointer; 379 UnicodeLength = (OriginalObject->String.Length * 2) + 2; 380 381 /* Create a new buffer object for the Unicode data */ 382 383 NewObject = AcpiUtCreateBufferObject (UnicodeLength); 384 if (!NewObject) 385 { 386 return (AE_NO_MEMORY); 387 } 388 389 UnicodeBuffer = ACPI_CAST_PTR (UINT16, NewObject->Buffer.Pointer); 390 391 /* Convert ASCII to Unicode */ 392 393 for (i = 0; i < OriginalObject->String.Length; i++) 394 { 395 UnicodeBuffer[i] = (UINT16) AsciiString[i]; 396 } 397 398 *ReturnObject = NewObject; 399 return (AE_OK); 400 } 401 402 403 /******************************************************************************* 404 * 405 * FUNCTION: AcpiNsConvertToResource 406 * 407 * PARAMETERS: OriginalObject - Object to be converted 408 * ReturnObject - Where the new converted object is returned 409 * 410 * RETURN: Status. AE_OK if conversion was successful 411 * 412 * DESCRIPTION: Attempt to convert a Integer object to a ResourceTemplate 413 * Buffer. 414 * 415 ******************************************************************************/ 416 417 ACPI_STATUS 418 AcpiNsConvertToResource ( 419 ACPI_OPERAND_OBJECT *OriginalObject, 420 ACPI_OPERAND_OBJECT **ReturnObject) 421 { 422 ACPI_OPERAND_OBJECT *NewObject; 423 UINT8 *Buffer; 424 425 426 /* 427 * We can fix the following cases for an expected resource template: 428 * 1. No return value (interpreter slack mode is disabled) 429 * 2. A "Return (Zero)" statement 430 * 3. A "Return empty buffer" statement 431 * 432 * We will return a buffer containing a single EndTag 433 * resource descriptor. 434 */ 435 if (OriginalObject) 436 { 437 switch (OriginalObject->Common.Type) 438 { 439 case ACPI_TYPE_INTEGER: 440 441 /* We can only repair an Integer==0 */ 442 443 if (OriginalObject->Integer.Value) 444 { 445 return (AE_AML_OPERAND_TYPE); 446 } 447 break; 448 449 case ACPI_TYPE_BUFFER: 450 451 if (OriginalObject->Buffer.Length) 452 { 453 /* Additional checks can be added in the future */ 454 455 *ReturnObject = NULL; 456 return (AE_OK); 457 } 458 break; 459 460 case ACPI_TYPE_STRING: 461 default: 462 463 return (AE_AML_OPERAND_TYPE); 464 } 465 } 466 467 /* Create the new buffer object for the resource descriptor */ 468 469 NewObject = AcpiUtCreateBufferObject (2); 470 if (!NewObject) 471 { 472 return (AE_NO_MEMORY); 473 } 474 475 Buffer = ACPI_CAST_PTR (UINT8, NewObject->Buffer.Pointer); 476 477 /* Initialize the Buffer with a single EndTag descriptor */ 478 479 Buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE); 480 Buffer[1] = 0x00; 481 482 *ReturnObject = NewObject; 483 return (AE_OK); 484 } 485