acfileio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. #define _COMPONENT ACPI_UTILITIES
  120. ACPI_MODULE_NAME ("acfileio")
  121. /* Local prototypes */
  122. static ACPI_STATUS
  123. AcGetOneTableFromFile (
  124. char *Filename,
  125. FILE *File,
  126. UINT8 GetOnlyAmlTables,
  127. ACPI_TABLE_HEADER **Table);
  128. static ACPI_STATUS
  129. AcCheckTextModeCorruption (
  130. ACPI_TABLE_HEADER *Table);
  131. /*******************************************************************************
  132. *
  133. * FUNCTION: AcGetAllTablesFromFile
  134. *
  135. * PARAMETERS: Filename - Table filename
  136. * GetOnlyAmlTables - TRUE if the tables must be AML tables
  137. * ReturnListHead - Where table list is returned
  138. *
  139. * RETURN: Status
  140. *
  141. * DESCRIPTION: Get all ACPI tables from within a single file.
  142. *
  143. ******************************************************************************/
  144. ACPI_STATUS
  145. AcGetAllTablesFromFile (
  146. char *Filename,
  147. UINT8 GetOnlyAmlTables,
  148. ACPI_NEW_TABLE_DESC **ReturnListHead)
  149. {
  150. #if 0
  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. #endif
  249. sysfatal("Panic can't do this");
  250. return AE_OK;
  251. }
  252. #if 0
  253. /*******************************************************************************
  254. *
  255. * FUNCTION: AcGetOneTableFromFile
  256. *
  257. * PARAMETERS: Filename - File where table is located
  258. * File - Open FILE pointer to Filename
  259. * GetOnlyAmlTables - TRUE if the tables must be AML tables.
  260. * ReturnTable - Where a pointer to the table is returned
  261. *
  262. * RETURN: Status
  263. *
  264. * DESCRIPTION: Read the next ACPI table from a file. Implements support
  265. * for multiple tables within a single file. File must already
  266. * be open.
  267. *
  268. * Note: Loading an RSDP is not supported.
  269. *
  270. ******************************************************************************/
  271. static ACPI_STATUS
  272. AcGetOneTableFromFile (
  273. char *Filename,
  274. FILE *File,
  275. UINT8 GetOnlyAmlTables,
  276. ACPI_TABLE_HEADER **ReturnTable)
  277. {
  278. ACPI_STATUS Status = AE_OK;
  279. ACPI_TABLE_HEADER TableHeader;
  280. ACPI_TABLE_HEADER *Table;
  281. INT32 Count;
  282. long TableOffset;
  283. *ReturnTable = NULL;
  284. /* Get the table header to examine signature and length */
  285. TableOffset = ftell (File);
  286. Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
  287. if (Count != sizeof (ACPI_TABLE_HEADER))
  288. {
  289. return (AE_CTRL_TERMINATE);
  290. }
  291. /* Validate the table signature/header (limited ASCII chars) */
  292. Status = AcValidateTableHeader (File, TableOffset);
  293. if (ACPI_FAILURE (Status))
  294. {
  295. return (Status);
  296. }
  297. if (GetOnlyAmlTables)
  298. {
  299. /* Table must be an AML table (DSDT/SSDT) or FADT */
  300. if (!ACPI_COMPARE_NAME (TableHeader.Signature, ACPI_SIG_FADT) &&
  301. !AcpiUtIsAmlTable (&TableHeader))
  302. {
  303. fprintf (stderr,
  304. " %s: Table [%4.4s] is not an AML table - ignoring\n",
  305. Filename, TableHeader.Signature);
  306. return (AE_TYPE);
  307. }
  308. }
  309. /* Allocate a buffer for the entire table */
  310. Table = AcpiOsAllocate ((size_t) TableHeader.Length);
  311. if (!Table)
  312. {
  313. return (AE_NO_MEMORY);
  314. }
  315. /* Read the entire ACPI table, including header */
  316. fseek (File, TableOffset, SEEK_SET);
  317. Count = fread (Table, 1, TableHeader.Length, File);
  318. if (Count != (INT32) TableHeader.Length)
  319. {
  320. Status = AE_ERROR;
  321. goto ErrorExit;
  322. }
  323. /* Validate the checksum (just issue a warning) */
  324. Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
  325. if (ACPI_FAILURE (Status))
  326. {
  327. Status = AcCheckTextModeCorruption (Table);
  328. if (ACPI_FAILURE (Status))
  329. {
  330. goto ErrorExit;
  331. }
  332. }
  333. *ReturnTable = Table;
  334. return (AE_OK);
  335. ErrorExit:
  336. AcpiOsFree (Table);
  337. return (Status);
  338. }
  339. /*******************************************************************************
  340. *
  341. * FUNCTION: AcIsFileBinary
  342. *
  343. * PARAMETERS: File - Open input file
  344. *
  345. * RETURN: TRUE if file appears to be binary
  346. *
  347. * DESCRIPTION: Scan a file for any non-ASCII bytes.
  348. *
  349. * Note: Maintains current file position.
  350. *
  351. ******************************************************************************/
  352. BOOLEAN
  353. AcIsFileBinary (
  354. FILE *File)
  355. {
  356. UINT8 Byte;
  357. BOOLEAN IsBinary = FALSE;
  358. long FileOffset;
  359. /* Scan entire file for any non-ASCII bytes */
  360. FileOffset = ftell (File);
  361. while (fread (&Byte, 1, 1, File) == 1)
  362. {
  363. if (!isprint (Byte) && !isspace (Byte))
  364. {
  365. IsBinary = TRUE;
  366. goto Exit;
  367. }
  368. }
  369. Exit:
  370. fseek (File, FileOffset, SEEK_SET);
  371. return (IsBinary);
  372. }
  373. /*******************************************************************************
  374. *
  375. * FUNCTION: AcValidateTableHeader
  376. *
  377. * PARAMETERS: File - Open input file
  378. *
  379. * RETURN: Status
  380. *
  381. * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
  382. * tables, via the
  383. * following checks on what would be the table header:
  384. * 1) File must be at least as long as an ACPI_TABLE_HEADER
  385. * 2) There must be enough room in the file to hold entire table
  386. * 3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
  387. *
  388. * Note: There can be multiple definition blocks per file, so we cannot
  389. * expect/compare the file size to be equal to the table length. 12/2015.
  390. *
  391. * Note: Maintains current file position.
  392. *
  393. ******************************************************************************/
  394. ACPI_STATUS
  395. AcValidateTableHeader (
  396. FILE *File,
  397. long TableOffset)
  398. {
  399. ACPI_TABLE_HEADER TableHeader;
  400. size_t Actual;
  401. long OriginalOffset;
  402. UINT32 FileSize;
  403. UINT32 i;
  404. ACPI_FUNCTION_TRACE ("AcValidateTableHeader");
  405. /* Read a potential table header */
  406. OriginalOffset = ftell (File);
  407. fseek (File, TableOffset, SEEK_SET);
  408. Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
  409. fseek (File, OriginalOffset, SEEK_SET);
  410. if (Actual < sizeof (ACPI_TABLE_HEADER))
  411. {
  412. return (AE_ERROR);
  413. }
  414. /* Validate the signature (limited ASCII chars) */
  415. if (!AcpiUtValidNameseg (TableHeader.Signature))
  416. {
  417. fprintf (stderr, "Invalid table signature: 0x%8.8X\n",
  418. *ACPI_CAST_PTR (UINT32, TableHeader.Signature));
  419. return (AE_BAD_SIGNATURE);
  420. }
  421. /* Validate table length against bytes remaining in the file */
  422. FileSize = CmGetFileSize (File);
  423. if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
  424. {
  425. fprintf (stderr, "Table [%4.4s] is too long for file - "
  426. "needs: 0x%.2X, remaining in file: 0x%.2X\n",
  427. TableHeader.Signature, TableHeader.Length,
  428. (UINT32) (FileSize - TableOffset));
  429. return (AE_BAD_HEADER);
  430. }
  431. /*
  432. * These fields must be ASCII: OemId, OemTableId, AslCompilerId.
  433. * We allow a NULL terminator in OemId and OemTableId.
  434. */
  435. for (i = 0; i < ACPI_NAME_SIZE; i++)
  436. {
  437. if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
  438. {
  439. goto BadCharacters;
  440. }
  441. }
  442. for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
  443. {
  444. if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
  445. {
  446. goto BadCharacters;
  447. }
  448. }
  449. for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
  450. {
  451. if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
  452. {
  453. goto BadCharacters;
  454. }
  455. }
  456. return (AE_OK);
  457. BadCharacters:
  458. ACPI_WARNING ((AE_INFO,
  459. "Table header for [%4.4s] has invalid ASCII character(s)",
  460. TableHeader.Signature));
  461. return (AE_OK);
  462. }
  463. /*******************************************************************************
  464. *
  465. * FUNCTION: AcCheckTextModeCorruption
  466. *
  467. * PARAMETERS: Table - Table buffer starting with table header
  468. *
  469. * RETURN: Status
  470. *
  471. * DESCRIPTION: Check table for text mode file corruption where all linefeed
  472. * characters (LF) have been replaced by carriage return linefeed
  473. * pairs (CR/LF).
  474. *
  475. ******************************************************************************/
  476. static ACPI_STATUS
  477. AcCheckTextModeCorruption (
  478. ACPI_TABLE_HEADER *Table)
  479. {
  480. UINT32 i;
  481. UINT32 Pairs = 0;
  482. UINT8 *Buffer = ACPI_CAST_PTR (UINT8, Table);
  483. /* Scan entire table to determine if each LF has been prefixed with a CR */
  484. for (i = 1; i < Table->Length; i++)
  485. {
  486. if (Buffer[i] == 0x0A)
  487. {
  488. if (Buffer[i - 1] != 0x0D)
  489. {
  490. /* The LF does not have a preceding CR, table not corrupted */
  491. return (AE_OK);
  492. }
  493. else
  494. {
  495. /* Found a CR/LF pair */
  496. Pairs++;
  497. }
  498. i++;
  499. }
  500. }
  501. if (!Pairs)
  502. {
  503. return (AE_OK);
  504. }
  505. /*
  506. * Entire table scanned, each CR is part of a CR/LF pair --
  507. * meaning that the table was treated as a text file somewhere.
  508. *
  509. * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
  510. * original table are left untouched by the text conversion process --
  511. * meaning that we cannot simply replace CR/LF pairs with LFs.
  512. */
  513. AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
  514. AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
  515. AcpiOsPrintf ("Table cannot be repaired!\n");
  516. return (AE_BAD_VALUE);
  517. }
  518. #endif