acfileio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /******************************************************************************
  2. *
  3. * Module Name: acfileio - Get ACPI tables from file
  4. *
  5. *****************************************************************************/
  6. /******************************************************************************
  7. *
  8. * 1. Copyright Notice
  9. *
  10. * Some or all of this work - Copyright (c) 1999 - 2016, Intel Corp.
  11. * All rights reserved.
  12. *
  13. * 2. License
  14. *
  15. * 2.1. This is your license from Intel Corp. under its intellectual property
  16. * rights. You may have additional license terms from the party that provided
  17. * you this software, covering your right to use that party's intellectual
  18. * property rights.
  19. *
  20. * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
  21. * copy of the source code appearing in this file ("Covered Code") an
  22. * irrevocable, perpetual, worldwide license under Intel's copyrights in the
  23. * base code distributed originally by Intel ("Original Intel Code") to copy,
  24. * make derivatives, distribute, use and display any portion of the Covered
  25. * Code in any form, with the right to sublicense such rights; and
  26. *
  27. * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
  28. * license (with the right to sublicense), under only those claims of Intel
  29. * patents that are infringed by the Original Intel Code, to make, use, sell,
  30. * offer to sell, and import the Covered Code and derivative works thereof
  31. * solely to the minimum extent necessary to exercise the above copyright
  32. * license, and in no event shall the patent license extend to any additions
  33. * to or modifications of the Original Intel Code. No other license or right
  34. * is granted directly or by implication, estoppel or otherwise;
  35. *
  36. * The above copyright and patent license is granted only if the following
  37. * conditions are met:
  38. *
  39. * 3. Conditions
  40. *
  41. * 3.1. Redistribution of Source with Rights to Further Distribute Source.
  42. * Redistribution of source code of any substantial portion of the Covered
  43. * Code or modification with rights to further distribute source must include
  44. * the above Copyright Notice, the above License, this list of Conditions,
  45. * and the following Disclaimer and Export Compliance provision. In addition,
  46. * Licensee must cause all Covered Code to which Licensee contributes to
  47. * contain a file documenting the changes Licensee made to create that Covered
  48. * Code and the date of any change. Licensee must include in that file the
  49. * documentation of any changes made by any predecessor Licensee. Licensee
  50. * must include a prominent statement that the modification is derived,
  51. * directly or indirectly, from Original Intel Code.
  52. *
  53. * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
  54. * Redistribution of source code of any substantial portion of the Covered
  55. * Code or modification without rights to further distribute source must
  56. * include the following Disclaimer and Export Compliance provision in the
  57. * documentation and/or other materials provided with distribution. In
  58. * addition, Licensee may not authorize further sublicense of source of any
  59. * portion of the Covered Code, and must include terms to the effect that the
  60. * license from Licensee to its licensee is limited to the intellectual
  61. * property embodied in the software Licensee provides to its licensee, and
  62. * not to intellectual property embodied in modifications its licensee may
  63. * make.
  64. *
  65. * 3.3. Redistribution of Executable. Redistribution in executable form of any
  66. * substantial portion of the Covered Code or modification must reproduce the
  67. * above Copyright Notice, and the following Disclaimer and Export Compliance
  68. * provision in the documentation and/or other materials provided with the
  69. * distribution.
  70. *
  71. * 3.4. Intel retains all right, title, and interest in and to the Original
  72. * Intel Code.
  73. *
  74. * 3.5. Neither the name Intel nor any other trademark owned or controlled by
  75. * Intel shall be used in advertising or otherwise to promote the sale, use or
  76. * other dealings in products derived from or relating to the Covered Code
  77. * without prior written authorization from Intel.
  78. *
  79. * 4. Disclaimer and Export Compliance
  80. *
  81. * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
  82. * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
  83. * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
  84. * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
  85. * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
  86. * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
  87. * PARTICULAR PURPOSE.
  88. *
  89. * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
  90. * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
  91. * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
  92. * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
  93. * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
  94. * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
  95. * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
  96. * LIMITED REMEDY.
  97. *
  98. * 4.3. Licensee shall not export, either directly or indirectly, any of this
  99. * software or system incorporating such software without first obtaining any
  100. * required license or other approval from the U. S. Department of Commerce or
  101. * any other agency or department of the United States Government. In the
  102. * event Licensee exports any such software from the United States or
  103. * re-exports any such software from a foreign destination, Licensee shall
  104. * ensure that the distribution and export/re-export of the software is in
  105. * compliance with all laws, regulations, orders, or other restrictions of the
  106. * U.S. Export Administration Regulations. Licensee agrees that neither it nor
  107. * any of its subsidiaries will export/re-export any technical data, process,
  108. * software, or service, directly or indirectly, to any country for which the
  109. * United States government or any agency thereof requires an export license,
  110. * other governmental approval, or letter of assurance, without first obtaining
  111. * such license, approval or letter.
  112. *
  113. *****************************************************************************/
  114. #include "acpi.h"
  115. #include "accommon.h"
  116. #include "acapps.h"
  117. #include "actables.h"
  118. #include "acutils.h"
  119. #include <errno.h>
  120. #define _COMPONENT ACPI_UTILITIES
  121. ACPI_MODULE_NAME ("acfileio")
  122. /* Local prototypes */
  123. static ACPI_STATUS
  124. AcGetOneTableFromFile (
  125. char *Filename,
  126. FILE *File,
  127. UINT8 GetOnlyAmlTables,
  128. ACPI_TABLE_HEADER **Table);
  129. static ACPI_STATUS
  130. AcCheckTextModeCorruption (
  131. ACPI_TABLE_HEADER *Table);
  132. /*******************************************************************************
  133. *
  134. * FUNCTION: AcGetAllTablesFromFile
  135. *
  136. * PARAMETERS: Filename - Table filename
  137. * GetOnlyAmlTables - TRUE if the tables must be AML tables
  138. * ReturnListHead - Where table list is returned
  139. *
  140. * RETURN: Status
  141. *
  142. * DESCRIPTION: Get all ACPI tables from within a single file.
  143. *
  144. ******************************************************************************/
  145. ACPI_STATUS
  146. AcGetAllTablesFromFile (
  147. char *Filename,
  148. UINT8 GetOnlyAmlTables,
  149. ACPI_NEW_TABLE_DESC **ReturnListHead)
  150. {
  151. ACPI_NEW_TABLE_DESC *ListHead = NULL;
  152. ACPI_NEW_TABLE_DESC *ListTail = NULL;
  153. ACPI_NEW_TABLE_DESC *TableDesc;
  154. FILE *File;
  155. ACPI_TABLE_HEADER *Table = NULL;
  156. UINT32 FileSize;
  157. ACPI_STATUS Status = AE_OK;
  158. File = fopen (Filename, "rb");
  159. if (!File)
  160. {
  161. perror ("Could not open input file");
  162. if (errno == ENOENT)
  163. {
  164. return (AE_NOT_EXIST);
  165. }
  166. return (AE_ERROR);
  167. }
  168. /* Get the file size */
  169. FileSize = CmGetFileSize (File);
  170. if (FileSize == ACPI_UINT32_MAX)
  171. {
  172. Status = AE_ERROR;
  173. goto ErrorExit;
  174. }
  175. fprintf (stderr,
  176. "Input file %s, Length 0x%X (%u) bytes\n",
  177. Filename, FileSize, FileSize);
  178. /* We must have at least one ACPI table header */
  179. if (FileSize < sizeof (ACPI_TABLE_HEADER))
  180. {
  181. Status = AE_BAD_HEADER;
  182. goto ErrorExit;
  183. }
  184. /* Check for an non-binary file */
  185. if (!AcIsFileBinary (File))
  186. {
  187. fprintf (stderr,
  188. " %s: File does not appear to contain a valid AML table\n",
  189. Filename);
  190. return (AE_TYPE);
  191. }
  192. /* Read all tables within the file */
  193. while (ACPI_SUCCESS (Status))
  194. {
  195. /* Get one entire ACPI table */
  196. Status = AcGetOneTableFromFile (
  197. Filename, File, GetOnlyAmlTables, &Table);
  198. if (Status == AE_CTRL_TERMINATE)
  199. {
  200. Status = AE_OK;
  201. break;
  202. }
  203. else if (Status == AE_TYPE)
  204. {
  205. return (AE_OK);
  206. }
  207. else if (ACPI_FAILURE (Status))
  208. {
  209. goto ErrorExit;
  210. }
  211. /* Print table header for iASL/disassembler only */
  212. #ifdef ACPI_ASL_COMPILER
  213. AcpiTbPrintTableHeader (0, Table);
  214. #endif
  215. /* Allocate and link a table descriptor */
  216. TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
  217. TableDesc->Table = Table;
  218. TableDesc->Next = NULL;
  219. /* Link at the end of the local table list */
  220. if (!ListHead)
  221. {
  222. ListHead = TableDesc;
  223. ListTail = TableDesc;
  224. }
  225. else
  226. {
  227. ListTail->Next = TableDesc;
  228. ListTail = TableDesc;
  229. }
  230. }
  231. /* Add the local table list to the end of the global list */
  232. if (*ReturnListHead)
  233. {
  234. ListTail = *ReturnListHead;
  235. while (ListTail->Next)
  236. {
  237. ListTail = ListTail->Next;
  238. }
  239. ListTail->Next = ListHead;
  240. }
  241. else
  242. {
  243. *ReturnListHead = ListHead;
  244. }
  245. ErrorExit:
  246. fclose(File);
  247. return (Status);
  248. }
  249. /*******************************************************************************
  250. *
  251. * FUNCTION: AcGetOneTableFromFile
  252. *
  253. * PARAMETERS: Filename - File where table is located
  254. * File - Open FILE pointer to Filename
  255. * GetOnlyAmlTables - TRUE if the tables must be AML tables.
  256. * ReturnTable - Where a pointer to the table is returned
  257. *
  258. * RETURN: Status
  259. *
  260. * DESCRIPTION: Read the next ACPI table from a file. Implements support
  261. * for multiple tables within a single file. File must already
  262. * be open.
  263. *
  264. * Note: Loading an RSDP is not supported.
  265. *
  266. ******************************************************************************/
  267. static ACPI_STATUS
  268. AcGetOneTableFromFile (
  269. char *Filename,
  270. FILE *File,
  271. UINT8 GetOnlyAmlTables,
  272. ACPI_TABLE_HEADER **ReturnTable)
  273. {
  274. ACPI_STATUS Status = AE_OK;
  275. ACPI_TABLE_HEADER TableHeader;
  276. ACPI_TABLE_HEADER *Table;
  277. INT32 Count;
  278. long TableOffset;
  279. *ReturnTable = NULL;
  280. /* Get the table header to examine signature and length */
  281. TableOffset = ftell (File);
  282. Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
  283. if (Count != sizeof (ACPI_TABLE_HEADER))
  284. {
  285. return (AE_CTRL_TERMINATE);
  286. }
  287. /* Validate the table signature/header (limited ASCII chars) */
  288. Status = AcValidateTableHeader (File, TableOffset);
  289. if (ACPI_FAILURE (Status))
  290. {
  291. return (Status);
  292. }
  293. if (GetOnlyAmlTables)
  294. {
  295. /* Table must be an AML table (DSDT/SSDT) or FADT */
  296. if (!ACPI_COMPARE_NAME (TableHeader.Signature, ACPI_SIG_FADT) &&
  297. !AcpiUtIsAmlTable (&TableHeader))
  298. {
  299. fprintf (stderr,
  300. " %s: Table [%4.4s] is not an AML table - ignoring\n",
  301. Filename, TableHeader.Signature);
  302. return (AE_TYPE);
  303. }
  304. }
  305. /* Allocate a buffer for the entire table */
  306. Table = AcpiOsAllocate ((size_t) TableHeader.Length);
  307. if (!Table)
  308. {
  309. return (AE_NO_MEMORY);
  310. }
  311. /* Read the entire ACPI table, including header */
  312. fseek (File, TableOffset, SEEK_SET);
  313. Count = fread (Table, 1, TableHeader.Length, File);
  314. if (Count != (INT32) TableHeader.Length)
  315. {
  316. Status = AE_ERROR;
  317. goto ErrorExit;
  318. }
  319. /* Validate the checksum (just issue a warning) */
  320. Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
  321. if (ACPI_FAILURE (Status))
  322. {
  323. Status = AcCheckTextModeCorruption (Table);
  324. if (ACPI_FAILURE (Status))
  325. {
  326. goto ErrorExit;
  327. }
  328. }
  329. *ReturnTable = Table;
  330. return (AE_OK);
  331. ErrorExit:
  332. AcpiOsFree (Table);
  333. return (Status);
  334. }
  335. /*******************************************************************************
  336. *
  337. * FUNCTION: AcIsFileBinary
  338. *
  339. * PARAMETERS: File - Open input file
  340. *
  341. * RETURN: TRUE if file appears to be binary
  342. *
  343. * DESCRIPTION: Scan a file for any non-ASCII bytes.
  344. *
  345. * Note: Maintains current file position.
  346. *
  347. ******************************************************************************/
  348. BOOLEAN
  349. AcIsFileBinary (
  350. FILE *File)
  351. {
  352. UINT8 Byte;
  353. BOOLEAN IsBinary = FALSE;
  354. long FileOffset;
  355. /* Scan entire file for any non-ASCII bytes */
  356. FileOffset = ftell (File);
  357. while (fread (&Byte, 1, 1, File) == 1)
  358. {
  359. if (!isprint (Byte) && !isspace (Byte))
  360. {
  361. IsBinary = TRUE;
  362. goto Exit;
  363. }
  364. }
  365. Exit:
  366. fseek (File, FileOffset, SEEK_SET);
  367. return (IsBinary);
  368. }
  369. /*******************************************************************************
  370. *
  371. * FUNCTION: AcValidateTableHeader
  372. *
  373. * PARAMETERS: File - Open input file
  374. *
  375. * RETURN: Status
  376. *
  377. * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
  378. * tables, via the
  379. * following checks on what would be the table header:
  380. * 1) File must be at least as long as an ACPI_TABLE_HEADER
  381. * 2) There must be enough room in the file to hold entire table
  382. * 3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
  383. *
  384. * Note: There can be multiple definition blocks per file, so we cannot
  385. * expect/compare the file size to be equal to the table length. 12/2015.
  386. *
  387. * Note: Maintains current file position.
  388. *
  389. ******************************************************************************/
  390. ACPI_STATUS
  391. AcValidateTableHeader (
  392. FILE *File,
  393. long TableOffset)
  394. {
  395. ACPI_TABLE_HEADER TableHeader;
  396. size_t Actual;
  397. long OriginalOffset;
  398. UINT32 FileSize;
  399. UINT32 i;
  400. ACPI_FUNCTION_TRACE ("AcValidateTableHeader");
  401. /* Read a potential table header */
  402. OriginalOffset = ftell (File);
  403. fseek (File, TableOffset, SEEK_SET);
  404. Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
  405. fseek (File, OriginalOffset, SEEK_SET);
  406. if (Actual < sizeof (ACPI_TABLE_HEADER))
  407. {
  408. return (AE_ERROR);
  409. }
  410. /* Validate the signature (limited ASCII chars) */
  411. if (!AcpiUtValidNameseg (TableHeader.Signature))
  412. {
  413. fprintf (stderr, "Invalid table signature: 0x%8.8X\n",
  414. *ACPI_CAST_PTR (UINT32, TableHeader.Signature));
  415. return (AE_BAD_SIGNATURE);
  416. }
  417. /* Validate table length against bytes remaining in the file */
  418. FileSize = CmGetFileSize (File);
  419. if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
  420. {
  421. fprintf (stderr, "Table [%4.4s] is too long for file - "
  422. "needs: 0x%.2X, remaining in file: 0x%.2X\n",
  423. TableHeader.Signature, TableHeader.Length,
  424. (UINT32) (FileSize - TableOffset));
  425. return (AE_BAD_HEADER);
  426. }
  427. /*
  428. * These fields must be ASCII: OemId, OemTableId, AslCompilerId.
  429. * We allow a NULL terminator in OemId and OemTableId.
  430. */
  431. for (i = 0; i < ACPI_NAME_SIZE; i++)
  432. {
  433. if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
  434. {
  435. goto BadCharacters;
  436. }
  437. }
  438. for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
  439. {
  440. if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
  441. {
  442. goto BadCharacters;
  443. }
  444. }
  445. for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
  446. {
  447. if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
  448. {
  449. goto BadCharacters;
  450. }
  451. }
  452. return (AE_OK);
  453. BadCharacters:
  454. ACPI_WARNING ((AE_INFO,
  455. "Table header for [%4.4s] has invalid ASCII character(s)",
  456. TableHeader.Signature));
  457. return (AE_OK);
  458. }
  459. /*******************************************************************************
  460. *
  461. * FUNCTION: AcCheckTextModeCorruption
  462. *
  463. * PARAMETERS: Table - Table buffer starting with table header
  464. *
  465. * RETURN: Status
  466. *
  467. * DESCRIPTION: Check table for text mode file corruption where all linefeed
  468. * characters (LF) have been replaced by carriage return linefeed
  469. * pairs (CR/LF).
  470. *
  471. ******************************************************************************/
  472. static ACPI_STATUS
  473. AcCheckTextModeCorruption (
  474. ACPI_TABLE_HEADER *Table)
  475. {
  476. UINT32 i;
  477. UINT32 Pairs = 0;
  478. UINT8 *Buffer = ACPI_CAST_PTR (UINT8, Table);
  479. /* Scan entire table to determine if each LF has been prefixed with a CR */
  480. for (i = 1; i < Table->Length; i++)
  481. {
  482. if (Buffer[i] == 0x0A)
  483. {
  484. if (Buffer[i - 1] != 0x0D)
  485. {
  486. /* The LF does not have a preceding CR, table not corrupted */
  487. return (AE_OK);
  488. }
  489. else
  490. {
  491. /* Found a CR/LF pair */
  492. Pairs++;
  493. }
  494. i++;
  495. }
  496. }
  497. if (!Pairs)
  498. {
  499. return (AE_OK);
  500. }
  501. /*
  502. * Entire table scanned, each CR is part of a CR/LF pair --
  503. * meaning that the table was treated as a text file somewhere.
  504. *
  505. * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
  506. * original table are left untouched by the text conversion process --
  507. * meaning that we cannot simply replace CR/LF pairs with LFs.
  508. */
  509. AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
  510. AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
  511. AcpiOsPrintf ("Table cannot be repaired!\n");
  512. return (AE_BAD_VALUE);
  513. }