1 /****************************************************************************** 2 * 3 * Module Name: axutils - Utility functions for acpixtract tool. 4 * 5 *****************************************************************************/ 6 7 /****************************************************************************** 8 * 9 * 1. Copyright Notice 10 * 11 * Some or all of this work - Copyright (c) 1999 - 2017, 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 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 88 * PARTICULAR PURPOSE. 89 * 90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 97 * LIMITED REMEDY. 98 * 99 * 4.3. Licensee shall not export, either directly or indirectly, any of this 100 * software or system incorporating such software without first obtaining any 101 * required license or other approval from the U. S. Department of Commerce or 102 * any other agency or department of the United States Government. In the 103 * event Licensee exports any such software from the United States or 104 * re-exports any such software from a foreign destination, Licensee shall 105 * ensure that the distribution and export/re-export of the software is in 106 * compliance with all laws, regulations, orders, or other restrictions of the 107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor 108 * any of its subsidiaries will export/re-export any technical data, process, 109 * software, or service, directly or indirectly, to any country for which the 110 * United States government or any agency thereof requires an export license, 111 * other governmental approval, or letter of assurance, without first obtaining 112 * such license, approval or letter. 113 * 114 ***************************************************************************** 115 * 116 * Alternatively, you may choose to be licensed under the terms of the 117 * following license: 118 * 119 * Redistribution and use in source and binary forms, with or without 120 * modification, are permitted provided that the following conditions 121 * are met: 122 * 1. Redistributions of source code must retain the above copyright 123 * notice, this list of conditions, and the following disclaimer, 124 * without modification. 125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 126 * substantially similar to the "NO WARRANTY" disclaimer below 127 * ("Disclaimer") and any redistribution must be conditioned upon 128 * including a substantially similar Disclaimer requirement for further 129 * binary redistribution. 130 * 3. Neither the names of the above-listed copyright holders nor the names 131 * of any contributors may be used to endorse or promote products derived 132 * from this software without specific prior written permission. 133 * 134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 145 * 146 * Alternatively, you may choose to be licensed under the terms of the 147 * GNU General Public License ("GPL") version 2 as published by the Free 148 * Software Foundation. 149 * 150 *****************************************************************************/ 151 152 #include "acpixtract.h" 153 154 155 /******************************************************************************* 156 * 157 * FUNCTION: AxCheckAscii 158 * 159 * PARAMETERS: Name - Ascii string, at least as long as Count 160 * Count - Number of characters to check 161 * 162 * RETURN: None 163 * 164 * DESCRIPTION: Ensure that the requested number of characters are printable 165 * Ascii characters. Sets non-printable and null chars to <space>. 166 * 167 ******************************************************************************/ 168 169 void 170 AxCheckAscii ( 171 char *Name, 172 int Count) 173 { 174 int i; 175 176 177 for (i = 0; i < Count; i++) 178 { 179 if (!Name[i] || !isprint ((int) Name[i])) 180 { 181 Name[i] = ' '; 182 } 183 } 184 } 185 186 187 /****************************************************************************** 188 * 189 * FUNCTION: AxIsEmptyLine 190 * 191 * PARAMETERS: Buffer - Line from input file 192 * 193 * RETURN: TRUE if line is empty (zero or more blanks only) 194 * 195 * DESCRIPTION: Determine if an input line is empty. 196 * 197 ******************************************************************************/ 198 199 int 200 AxIsEmptyLine ( 201 char *Buffer) 202 { 203 204 /* Skip all spaces */ 205 206 while (*Buffer == ' ') 207 { 208 Buffer++; 209 } 210 211 /* Line is empty when a Unix or DOS-style line terminator is found. */ 212 213 if ((*Buffer == '\r') || (*Buffer == '\n')) 214 { 215 return (1); 216 } 217 218 return (0); 219 } 220 221 222 /******************************************************************************* 223 * 224 * FUNCTION: AxNormalizeSignature 225 * 226 * PARAMETERS: Name - Ascii string containing an ACPI signature 227 * 228 * RETURN: None 229 * 230 * DESCRIPTION: Change "RSD PTR" to "RSDP" 231 * 232 ******************************************************************************/ 233 234 void 235 AxNormalizeSignature ( 236 char *Signature) 237 { 238 239 if (!strncmp (Signature, "RSD ", 4)) 240 { 241 Signature[3] = 'P'; 242 } 243 } 244 245 246 /****************************************************************************** 247 * 248 * FUNCTION: AxConvertLine 249 * 250 * PARAMETERS: InputLine - One line from the input acpidump file 251 * OutputData - Where the converted data is returned 252 * 253 * RETURN: The number of bytes actually converted 254 * 255 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes) 256 * 257 ******************************************************************************/ 258 259 size_t 260 AxConvertLine ( 261 char *InputLine, 262 unsigned char *OutputData) 263 { 264 char *End; 265 int BytesConverted; 266 int Converted[16]; 267 int i; 268 269 270 /* Terminate the input line at the end of the actual data (for sscanf) */ 271 272 End = strstr (InputLine + 2, " "); 273 if (!End) 274 { 275 return (0); /* Don't understand the format */ 276 } 277 *End = 0; 278 279 /* 280 * Convert one line of table data, of the form: 281 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline> 282 * 283 * Example: 284 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........ 285 */ 286 BytesConverted = sscanf (InputLine, 287 "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", 288 &Converted[0], &Converted[1], &Converted[2], &Converted[3], 289 &Converted[4], &Converted[5], &Converted[6], &Converted[7], 290 &Converted[8], &Converted[9], &Converted[10], &Converted[11], 291 &Converted[12], &Converted[13], &Converted[14], &Converted[15]); 292 293 /* Pack converted data into a byte array */ 294 295 for (i = 0; i < BytesConverted; i++) 296 { 297 OutputData[i] = (unsigned char) Converted[i]; 298 } 299 300 return ((size_t) BytesConverted); 301 } 302 303 304 /****************************************************************************** 305 * 306 * FUNCTION: AxGetTableHeader 307 * 308 * PARAMETERS: InputFile - Handle for the input acpidump file 309 * OutputData - Where the table header is returned 310 * 311 * RETURN: The actual number of bytes converted 312 * 313 * DESCRIPTION: Extract and convert an ACPI table header 314 * 315 ******************************************************************************/ 316 317 size_t 318 AxGetTableHeader ( 319 FILE *InputFile, 320 unsigned char *OutputData) 321 { 322 size_t BytesConverted; 323 size_t TotalConverted = 0; 324 int i; 325 326 327 /* Get the full 36 byte ACPI table header, requires 3 input text lines */ 328 329 for (i = 0; i < 3; i++) 330 { 331 if (!fgets (Gbl_HeaderBuffer, AX_LINE_BUFFER_SIZE, InputFile)) 332 { 333 return (TotalConverted); 334 } 335 336 BytesConverted = AxConvertLine (Gbl_HeaderBuffer, OutputData); 337 TotalConverted += BytesConverted; 338 OutputData += 16; 339 340 if (BytesConverted != 16) 341 { 342 return (TotalConverted); 343 } 344 } 345 346 return (TotalConverted); 347 } 348 349 350 /****************************************************************************** 351 * 352 * FUNCTION: AxCountTableInstances 353 * 354 * PARAMETERS: InputPathname - Filename for acpidump file 355 * Signature - Requested signature to count 356 * 357 * RETURN: The number of instances of the signature 358 * 359 * DESCRIPTION: Count the instances of tables with the given signature within 360 * the input acpidump file. 361 * 362 ******************************************************************************/ 363 364 unsigned int 365 AxCountTableInstances ( 366 char *InputPathname, 367 char *Signature) 368 { 369 FILE *InputFile; 370 unsigned int Instances = 0; 371 372 373 InputFile = fopen (InputPathname, "r"); 374 if (!InputFile) 375 { 376 printf ("Could not open input file %s\n", InputPathname); 377 return (0); 378 } 379 380 /* Count the number of instances of this signature */ 381 382 while (fgets (Gbl_InstanceBuffer, AX_LINE_BUFFER_SIZE, InputFile)) 383 { 384 /* Ignore empty lines and lines that start with a space */ 385 386 if (AxIsEmptyLine (Gbl_InstanceBuffer) || 387 (Gbl_InstanceBuffer[0] == ' ')) 388 { 389 continue; 390 } 391 392 AxNormalizeSignature (Gbl_InstanceBuffer); 393 if (ACPI_COMPARE_NAME (Gbl_InstanceBuffer, Signature)) 394 { 395 Instances++; 396 } 397 } 398 399 fclose (InputFile); 400 return (Instances); 401 } 402 403 404 /****************************************************************************** 405 * 406 * FUNCTION: AxGetNextInstance 407 * 408 * PARAMETERS: InputPathname - Filename for acpidump file 409 * Signature - Requested ACPI signature 410 * 411 * RETURN: The next instance number for this signature. Zero if this 412 * is the first instance of this signature. 413 * 414 * DESCRIPTION: Get the next instance number of the specified table. If this 415 * is the first instance of the table, create a new instance 416 * block. Note: only SSDT and PSDT tables can have multiple 417 * instances. 418 * 419 ******************************************************************************/ 420 421 unsigned int 422 AxGetNextInstance ( 423 char *InputPathname, 424 char *Signature) 425 { 426 AX_TABLE_INFO *Info; 427 428 429 Info = Gbl_TableListHead; 430 while (Info) 431 { 432 if (*(UINT32 *) Signature == Info->Signature) 433 { 434 break; 435 } 436 437 Info = Info->Next; 438 } 439 440 if (!Info) 441 { 442 /* Signature not found, create new table info block */ 443 444 Info = malloc (sizeof (AX_TABLE_INFO)); 445 if (!Info) 446 { 447 printf ("Could not allocate memory (0x%X bytes)\n", 448 (unsigned int) sizeof (AX_TABLE_INFO)); 449 exit (0); 450 } 451 452 Info->Signature = *(UINT32 *) Signature; 453 Info->Instances = AxCountTableInstances (InputPathname, Signature); 454 Info->NextInstance = 1; 455 Info->Next = Gbl_TableListHead; 456 Gbl_TableListHead = Info; 457 } 458 459 if (Info->Instances > 1) 460 { 461 return (Info->NextInstance++); 462 } 463 464 return (0); 465 } 466 467 468 /****************************************************************************** 469 * 470 * FUNCTION: AxIsDataBlockHeader 471 * 472 * PARAMETERS: None 473 * 474 * RETURN: Status. 1 if the table header is valid, 0 otherwise. 475 * 476 * DESCRIPTION: Check if the ACPI table identifier in the input acpidump text 477 * file is valid (of the form: <sig> @ <addr>). 478 * 479 ******************************************************************************/ 480 481 int 482 AxIsDataBlockHeader ( 483 void) 484 { 485 486 /* Ignore lines that are too short to be header lines */ 487 488 if (strlen (Gbl_LineBuffer) < AX_MIN_BLOCK_HEADER_LENGTH) 489 { 490 return (0); 491 } 492 493 /* Ignore empty lines and lines that start with a space */ 494 495 if (AxIsEmptyLine (Gbl_LineBuffer) || 496 (Gbl_LineBuffer[0] == ' ')) 497 { 498 return (0); 499 } 500 501 /* 502 * Ignore lines that are not headers of the form <sig> @ <addr>. 503 * Basically, just look for the '@' symbol, surrounded by spaces. 504 * 505 * Examples of headers that must be supported: 506 * 507 * DSDT @ 0x737e4000 508 * XSDT @ 0x737f2fff 509 * RSD PTR @ 0xf6cd0 510 * SSDT @ (nil) 511 */ 512 if (!strstr (Gbl_LineBuffer, " @ ")) 513 { 514 return (0); 515 } 516 517 AxNormalizeSignature (Gbl_LineBuffer); 518 return (1); 519 } 520 521 522 /****************************************************************************** 523 * 524 * FUNCTION: AxProcessOneTextLine 525 * 526 * PARAMETERS: OutputFile - Where to write the binary data 527 * ThisSignature - Signature of current ACPI table 528 * ThisTableBytesWritten - Total count of data written 529 * 530 * RETURN: Length of the converted line 531 * 532 * DESCRIPTION: Convert one line of input hex ascii text to binary, and write 533 * the binary data to the table output file. 534 * 535 ******************************************************************************/ 536 537 long 538 AxProcessOneTextLine ( 539 FILE *OutputFile, 540 char *ThisSignature, 541 unsigned int ThisTableBytesWritten) 542 { 543 size_t BytesWritten; 544 size_t BytesConverted; 545 546 547 /* Check for the end of this table data block */ 548 549 if (AxIsEmptyLine (Gbl_LineBuffer) || 550 (Gbl_LineBuffer[0] != ' ')) 551 { 552 printf (AX_TABLE_INFO_FORMAT, 553 ThisSignature, ThisTableBytesWritten, Gbl_OutputFilename); 554 return (0); 555 } 556 557 /* Convert one line of ascii hex data to binary */ 558 559 BytesConverted = AxConvertLine (Gbl_LineBuffer, Gbl_BinaryData); 560 561 /* Write the binary data */ 562 563 BytesWritten = fwrite (Gbl_BinaryData, 1, BytesConverted, OutputFile); 564 if (BytesWritten != BytesConverted) 565 { 566 printf ("Error while writing file %s\n", Gbl_OutputFilename); 567 return (-1); 568 } 569 570 return (BytesWritten); 571 } 572