PageRenderTime 37ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/freebsd/sys/contrib/dev/acpica/components/debugger/dbfileio.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 578 lines | 308 code | 105 blank | 165 comment | 41 complexity | bd5ce5eefc433040527b73c56c4b4997 MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*******************************************************************************
  2. *
  3. * Module Name: dbfileio - Debugger file I/O commands. These can't usually
  4. * be used when running the debugger in Ring 0 (Kernel mode)
  5. *
  6. ******************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2012, Intel Corp.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions, and the following disclaimer,
  16. * without modification.
  17. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  18. * substantially similar to the "NO WARRANTY" disclaimer below
  19. * ("Disclaimer") and any redistribution must be conditioned upon
  20. * including a substantially similar Disclaimer requirement for further
  21. * binary redistribution.
  22. * 3. Neither the names of the above-listed copyright holders nor the names
  23. * of any contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * Alternatively, this software may be distributed under the terms of the
  27. * GNU General Public License ("GPL") version 2 as published by the Free
  28. * Software Foundation.
  29. *
  30. * NO WARRANTY
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  40. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGES.
  42. */
  43. #include <contrib/dev/acpica/include/acpi.h>
  44. #include <contrib/dev/acpica/include/accommon.h>
  45. #include <contrib/dev/acpica/include/acdebug.h>
  46. #ifdef ACPI_APPLICATION
  47. #include <contrib/dev/acpica/include/actables.h>
  48. #endif
  49. #if (defined ACPI_DEBUGGER || defined ACPI_DISASSEMBLER)
  50. #define _COMPONENT ACPI_CA_DEBUGGER
  51. ACPI_MODULE_NAME ("dbfileio")
  52. /*
  53. * NOTE: this is here for lack of a better place. It is used in all
  54. * flavors of the debugger, need LCD file
  55. */
  56. #ifdef ACPI_APPLICATION
  57. #include <stdio.h>
  58. FILE *AcpiGbl_DebugFile = NULL;
  59. #endif
  60. #ifdef ACPI_DEBUGGER
  61. /* Local prototypes */
  62. #ifdef ACPI_APPLICATION
  63. static ACPI_STATUS
  64. AcpiDbCheckTextModeCorruption (
  65. UINT8 *Table,
  66. UINT32 TableLength,
  67. UINT32 FileLength);
  68. #endif
  69. /*******************************************************************************
  70. *
  71. * FUNCTION: AcpiDbCloseDebugFile
  72. *
  73. * PARAMETERS: None
  74. *
  75. * RETURN: None
  76. *
  77. * DESCRIPTION: If open, close the current debug output file
  78. *
  79. ******************************************************************************/
  80. void
  81. AcpiDbCloseDebugFile (
  82. void)
  83. {
  84. #ifdef ACPI_APPLICATION
  85. if (AcpiGbl_DebugFile)
  86. {
  87. fclose (AcpiGbl_DebugFile);
  88. AcpiGbl_DebugFile = NULL;
  89. AcpiGbl_DbOutputToFile = FALSE;
  90. AcpiOsPrintf ("Debug output file %s closed\n", AcpiGbl_DbDebugFilename);
  91. }
  92. #endif
  93. }
  94. /*******************************************************************************
  95. *
  96. * FUNCTION: AcpiDbOpenDebugFile
  97. *
  98. * PARAMETERS: Name - Filename to open
  99. *
  100. * RETURN: None
  101. *
  102. * DESCRIPTION: Open a file where debug output will be directed.
  103. *
  104. ******************************************************************************/
  105. void
  106. AcpiDbOpenDebugFile (
  107. char *Name)
  108. {
  109. #ifdef ACPI_APPLICATION
  110. AcpiDbCloseDebugFile ();
  111. AcpiGbl_DebugFile = fopen (Name, "w+");
  112. if (AcpiGbl_DebugFile)
  113. {
  114. AcpiOsPrintf ("Debug output file %s opened\n", Name);
  115. ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name);
  116. AcpiGbl_DbOutputToFile = TRUE;
  117. }
  118. else
  119. {
  120. AcpiOsPrintf ("Could not open debug file %s\n", Name);
  121. }
  122. #endif
  123. }
  124. #endif
  125. #ifdef ACPI_APPLICATION
  126. /*******************************************************************************
  127. *
  128. * FUNCTION: AcpiDbCheckTextModeCorruption
  129. *
  130. * PARAMETERS: Table - Table buffer
  131. * TableLength - Length of table from the table header
  132. * FileLength - Length of the file that contains the table
  133. *
  134. * RETURN: Status
  135. *
  136. * DESCRIPTION: Check table for text mode file corruption where all linefeed
  137. * characters (LF) have been replaced by carriage return linefeed
  138. * pairs (CR/LF).
  139. *
  140. ******************************************************************************/
  141. static ACPI_STATUS
  142. AcpiDbCheckTextModeCorruption (
  143. UINT8 *Table,
  144. UINT32 TableLength,
  145. UINT32 FileLength)
  146. {
  147. UINT32 i;
  148. UINT32 Pairs = 0;
  149. if (TableLength != FileLength)
  150. {
  151. ACPI_WARNING ((AE_INFO,
  152. "File length (0x%X) is not the same as the table length (0x%X)",
  153. FileLength, TableLength));
  154. }
  155. /* Scan entire table to determine if each LF has been prefixed with a CR */
  156. for (i = 1; i < FileLength; i++)
  157. {
  158. if (Table[i] == 0x0A)
  159. {
  160. if (Table[i - 1] != 0x0D)
  161. {
  162. /* The LF does not have a preceding CR, table not corrupted */
  163. return (AE_OK);
  164. }
  165. else
  166. {
  167. /* Found a CR/LF pair */
  168. Pairs++;
  169. }
  170. i++;
  171. }
  172. }
  173. if (!Pairs)
  174. {
  175. return (AE_OK);
  176. }
  177. /*
  178. * Entire table scanned, each CR is part of a CR/LF pair --
  179. * meaning that the table was treated as a text file somewhere.
  180. *
  181. * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
  182. * original table are left untouched by the text conversion process --
  183. * meaning that we cannot simply replace CR/LF pairs with LFs.
  184. */
  185. AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
  186. AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
  187. AcpiOsPrintf ("Table cannot be repaired!\n");
  188. return (AE_BAD_VALUE);
  189. }
  190. /*******************************************************************************
  191. *
  192. * FUNCTION: AcpiDbReadTable
  193. *
  194. * PARAMETERS: fp - File that contains table
  195. * Table - Return value, buffer with table
  196. * TableLength - Return value, length of table
  197. *
  198. * RETURN: Status
  199. *
  200. * DESCRIPTION: Load the DSDT from the file pointer
  201. *
  202. ******************************************************************************/
  203. static ACPI_STATUS
  204. AcpiDbReadTable (
  205. FILE *fp,
  206. ACPI_TABLE_HEADER **Table,
  207. UINT32 *TableLength)
  208. {
  209. ACPI_TABLE_HEADER TableHeader;
  210. UINT32 Actual;
  211. ACPI_STATUS Status;
  212. UINT32 FileSize;
  213. BOOLEAN StandardHeader = TRUE;
  214. /* Get the file size */
  215. fseek (fp, 0, SEEK_END);
  216. FileSize = (UINT32) ftell (fp);
  217. fseek (fp, 0, SEEK_SET);
  218. if (FileSize < 4)
  219. {
  220. return (AE_BAD_HEADER);
  221. }
  222. /* Read the signature */
  223. if (fread (&TableHeader, 1, 4, fp) != 4)
  224. {
  225. AcpiOsPrintf ("Could not read the table signature\n");
  226. return (AE_BAD_HEADER);
  227. }
  228. fseek (fp, 0, SEEK_SET);
  229. /* The RSDT, FACS and S3PT tables do not have standard ACPI headers */
  230. if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD ") ||
  231. ACPI_COMPARE_NAME (TableHeader.Signature, "FACS") ||
  232. ACPI_COMPARE_NAME (TableHeader.Signature, "S3PT"))
  233. {
  234. *TableLength = FileSize;
  235. StandardHeader = FALSE;
  236. }
  237. else
  238. {
  239. /* Read the table header */
  240. if (fread (&TableHeader, 1, sizeof (TableHeader), fp) !=
  241. sizeof (ACPI_TABLE_HEADER))
  242. {
  243. AcpiOsPrintf ("Could not read the table header\n");
  244. return (AE_BAD_HEADER);
  245. }
  246. #if 0
  247. /* Validate the table header/length */
  248. Status = AcpiTbValidateTableHeader (&TableHeader);
  249. if (ACPI_FAILURE (Status))
  250. {
  251. AcpiOsPrintf ("Table header is invalid!\n");
  252. return (Status);
  253. }
  254. #endif
  255. /* File size must be at least as long as the Header-specified length */
  256. if (TableHeader.Length > FileSize)
  257. {
  258. AcpiOsPrintf (
  259. "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
  260. TableHeader.Length, FileSize);
  261. return (AE_BAD_HEADER);
  262. }
  263. #ifdef ACPI_OBSOLETE_CODE
  264. /* We only support a limited number of table types */
  265. if (ACPI_STRNCMP ((char *) TableHeader.Signature, DSDT_SIG, 4) &&
  266. ACPI_STRNCMP ((char *) TableHeader.Signature, PSDT_SIG, 4) &&
  267. ACPI_STRNCMP ((char *) TableHeader.Signature, SSDT_SIG, 4))
  268. {
  269. AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
  270. (char *) TableHeader.Signature);
  271. ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
  272. return (AE_ERROR);
  273. }
  274. #endif
  275. *TableLength = TableHeader.Length;
  276. }
  277. /* Allocate a buffer for the table */
  278. *Table = AcpiOsAllocate ((size_t) FileSize);
  279. if (!*Table)
  280. {
  281. AcpiOsPrintf (
  282. "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
  283. TableHeader.Signature, *TableLength);
  284. return (AE_NO_MEMORY);
  285. }
  286. /* Get the rest of the table */
  287. fseek (fp, 0, SEEK_SET);
  288. Actual = fread (*Table, 1, (size_t) FileSize, fp);
  289. if (Actual == FileSize)
  290. {
  291. if (StandardHeader)
  292. {
  293. /* Now validate the checksum */
  294. Status = AcpiTbVerifyChecksum ((void *) *Table,
  295. ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
  296. if (Status == AE_BAD_CHECKSUM)
  297. {
  298. Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
  299. FileSize, (*Table)->Length);
  300. return (Status);
  301. }
  302. }
  303. return (AE_OK);
  304. }
  305. if (Actual > 0)
  306. {
  307. AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
  308. FileSize, Actual);
  309. return (AE_OK);
  310. }
  311. AcpiOsPrintf ("Error - could not read the table file\n");
  312. AcpiOsFree (*Table);
  313. *Table = NULL;
  314. *TableLength = 0;
  315. return (AE_ERROR);
  316. }
  317. /*******************************************************************************
  318. *
  319. * FUNCTION: AeLocalLoadTable
  320. *
  321. * PARAMETERS: Table - pointer to a buffer containing the entire
  322. * table to be loaded
  323. *
  324. * RETURN: Status
  325. *
  326. * DESCRIPTION: This function is called to load a table from the caller's
  327. * buffer. The buffer must contain an entire ACPI Table including
  328. * a valid header. The header fields will be verified, and if it
  329. * is determined that the table is invalid, the call will fail.
  330. *
  331. ******************************************************************************/
  332. static ACPI_STATUS
  333. AeLocalLoadTable (
  334. ACPI_TABLE_HEADER *Table)
  335. {
  336. ACPI_STATUS Status = AE_OK;
  337. /* ACPI_TABLE_DESC TableInfo; */
  338. ACPI_FUNCTION_TRACE (AeLocalLoadTable);
  339. #if 0
  340. if (!Table)
  341. {
  342. return_ACPI_STATUS (AE_BAD_PARAMETER);
  343. }
  344. TableInfo.Pointer = Table;
  345. Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
  346. if (ACPI_FAILURE (Status))
  347. {
  348. return_ACPI_STATUS (Status);
  349. }
  350. /* Install the new table into the local data structures */
  351. Status = AcpiTbInstallTable (&TableInfo);
  352. if (ACPI_FAILURE (Status))
  353. {
  354. if (Status == AE_ALREADY_EXISTS)
  355. {
  356. /* Table already exists, no error */
  357. Status = AE_OK;
  358. }
  359. /* Free table allocated by AcpiTbGetTable */
  360. AcpiTbDeleteSingleTable (&TableInfo);
  361. return_ACPI_STATUS (Status);
  362. }
  363. #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
  364. Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
  365. if (ACPI_FAILURE (Status))
  366. {
  367. /* Uninstall table and free the buffer */
  368. AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT);
  369. return_ACPI_STATUS (Status);
  370. }
  371. #endif
  372. #endif
  373. return_ACPI_STATUS (Status);
  374. }
  375. /*******************************************************************************
  376. *
  377. * FUNCTION: AcpiDbReadTableFromFile
  378. *
  379. * PARAMETERS: Filename - File where table is located
  380. * Table - Where a pointer to the table is returned
  381. *
  382. * RETURN: Status
  383. *
  384. * DESCRIPTION: Get an ACPI table from a file
  385. *
  386. ******************************************************************************/
  387. ACPI_STATUS
  388. AcpiDbReadTableFromFile (
  389. char *Filename,
  390. ACPI_TABLE_HEADER **Table)
  391. {
  392. FILE *fp;
  393. UINT32 TableLength;
  394. ACPI_STATUS Status;
  395. /* Open the file */
  396. fp = fopen (Filename, "rb");
  397. if (!fp)
  398. {
  399. AcpiOsPrintf ("Could not open input file %s\n", Filename);
  400. return (AE_ERROR);
  401. }
  402. /* Get the entire file */
  403. fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
  404. Status = AcpiDbReadTable (fp, Table, &TableLength);
  405. fclose(fp);
  406. if (ACPI_FAILURE (Status))
  407. {
  408. AcpiOsPrintf ("Could not get table from the file\n");
  409. return (Status);
  410. }
  411. return (AE_OK);
  412. }
  413. #endif
  414. /*******************************************************************************
  415. *
  416. * FUNCTION: AcpiDbGetTableFromFile
  417. *
  418. * PARAMETERS: Filename - File where table is located
  419. * ReturnTable - Where a pointer to the table is returned
  420. *
  421. * RETURN: Status
  422. *
  423. * DESCRIPTION: Load an ACPI table from a file
  424. *
  425. ******************************************************************************/
  426. ACPI_STATUS
  427. AcpiDbGetTableFromFile (
  428. char *Filename,
  429. ACPI_TABLE_HEADER **ReturnTable)
  430. {
  431. #ifdef ACPI_APPLICATION
  432. ACPI_STATUS Status;
  433. ACPI_TABLE_HEADER *Table;
  434. BOOLEAN IsAmlTable = TRUE;
  435. Status = AcpiDbReadTableFromFile (Filename, &Table);
  436. if (ACPI_FAILURE (Status))
  437. {
  438. return (Status);
  439. }
  440. #ifdef ACPI_DATA_TABLE_DISASSEMBLY
  441. IsAmlTable = AcpiUtIsAmlTable (Table);
  442. #endif
  443. if (IsAmlTable)
  444. {
  445. /* Attempt to recognize and install the table */
  446. Status = AeLocalLoadTable (Table);
  447. if (ACPI_FAILURE (Status))
  448. {
  449. if (Status == AE_ALREADY_EXISTS)
  450. {
  451. AcpiOsPrintf ("Table %4.4s is already installed\n",
  452. Table->Signature);
  453. }
  454. else
  455. {
  456. AcpiOsPrintf ("Could not install table, %s\n",
  457. AcpiFormatException (Status));
  458. }
  459. return (Status);
  460. }
  461. fprintf (stderr,
  462. "Acpi table [%4.4s] successfully installed and loaded\n",
  463. Table->Signature);
  464. }
  465. AcpiGbl_AcpiHardwarePresent = FALSE;
  466. if (ReturnTable)
  467. {
  468. *ReturnTable = Table;
  469. }
  470. #endif /* ACPI_APPLICATION */
  471. return (AE_OK);
  472. }
  473. #endif /* ACPI_DEBUGGER */