utfileio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*******************************************************************************
  2. *
  3. * Module Name: utfileio - simple file I/O routines
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "actables.h"
  45. #include "acapps.h"
  46. #ifdef ACPI_ASL_COMPILER
  47. #include "aslcompiler.h"
  48. #endif
  49. #define _COMPONENT ACPI_CA_DEBUGGER
  50. ACPI_MODULE_NAME("utfileio")
  51. #ifdef ACPI_APPLICATION
  52. /* Local prototypes */
  53. static acpi_status
  54. acpi_ut_check_text_mode_corruption(u8 *table,
  55. u32 table_length, u32 file_length);
  56. static acpi_status
  57. acpi_ut_read_table(FILE * fp,
  58. struct acpi_table_header **table, u32 *table_length);
  59. /*******************************************************************************
  60. *
  61. * FUNCTION: acpi_ut_check_text_mode_corruption
  62. *
  63. * PARAMETERS: table - Table buffer
  64. * table_length - Length of table from the table header
  65. * file_length - Length of the file that contains the table
  66. *
  67. * RETURN: Status
  68. *
  69. * DESCRIPTION: Check table for text mode file corruption where all linefeed
  70. * characters (LF) have been replaced by carriage return linefeed
  71. * pairs (CR/LF).
  72. *
  73. ******************************************************************************/
  74. static acpi_status
  75. acpi_ut_check_text_mode_corruption(u8 *table, u32 table_length, u32 file_length)
  76. {
  77. u32 i;
  78. u32 pairs = 0;
  79. if (table_length != file_length) {
  80. ACPI_WARNING((AE_INFO,
  81. "File length (0x%X) is not the same as the table length (0x%X)",
  82. file_length, table_length));
  83. }
  84. /* Scan entire table to determine if each LF has been prefixed with a CR */
  85. for (i = 1; i < file_length; i++) {
  86. if (table[i] == 0x0A) {
  87. if (table[i - 1] != 0x0D) {
  88. /* The LF does not have a preceding CR, table not corrupted */
  89. return (AE_OK);
  90. } else {
  91. /* Found a CR/LF pair */
  92. pairs++;
  93. }
  94. i++;
  95. }
  96. }
  97. if (!pairs) {
  98. return (AE_OK);
  99. }
  100. /*
  101. * Entire table scanned, each CR is part of a CR/LF pair --
  102. * meaning that the table was treated as a text file somewhere.
  103. *
  104. * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
  105. * original table are left untouched by the text conversion process --
  106. * meaning that we cannot simply replace CR/LF pairs with LFs.
  107. */
  108. acpi_os_printf("Table has been corrupted by text mode conversion\n");
  109. acpi_os_printf("All LFs (%u) were changed to CR/LF pairs\n", pairs);
  110. acpi_os_printf("Table cannot be repaired!\n");
  111. return (AE_BAD_VALUE);
  112. }
  113. /*******************************************************************************
  114. *
  115. * FUNCTION: acpi_ut_read_table
  116. *
  117. * PARAMETERS: fp - File that contains table
  118. * table - Return value, buffer with table
  119. * table_length - Return value, length of table
  120. *
  121. * RETURN: Status
  122. *
  123. * DESCRIPTION: Load the DSDT from the file pointer
  124. *
  125. ******************************************************************************/
  126. static acpi_status
  127. acpi_ut_read_table(FILE * fp,
  128. struct acpi_table_header **table, u32 *table_length)
  129. {
  130. struct acpi_table_header table_header;
  131. u32 actual;
  132. acpi_status status;
  133. u32 file_size;
  134. u8 standard_header = TRUE;
  135. s32 count;
  136. /* Get the file size */
  137. file_size = cm_get_file_size(fp);
  138. if (file_size == ACPI_UINT32_MAX) {
  139. return (AE_ERROR);
  140. }
  141. if (file_size < 4) {
  142. return (AE_BAD_HEADER);
  143. }
  144. /* Read the signature */
  145. fseek(fp, 0, SEEK_SET);
  146. count = fread(&table_header, 1, sizeof(struct acpi_table_header), fp);
  147. if (count != sizeof(struct acpi_table_header)) {
  148. acpi_os_printf("Could not read the table header\n");
  149. return (AE_BAD_HEADER);
  150. }
  151. /* The RSDP table does not have standard ACPI header */
  152. if (ACPI_VALIDATE_RSDP_SIG(table_header.signature)) {
  153. *table_length = file_size;
  154. standard_header = FALSE;
  155. } else {
  156. #if 0
  157. /* Validate the table header/length */
  158. status = acpi_tb_validate_table_header(&table_header);
  159. if (ACPI_FAILURE(status)) {
  160. acpi_os_printf("Table header is invalid!\n");
  161. return (status);
  162. }
  163. #endif
  164. /* File size must be at least as long as the Header-specified length */
  165. if (table_header.length > file_size) {
  166. acpi_os_printf
  167. ("TableHeader length [0x%X] greater than the input file size [0x%X]\n",
  168. table_header.length, file_size);
  169. #ifdef ACPI_ASL_COMPILER
  170. acpi_os_printf("File is corrupt or is ASCII text -- "
  171. "it must be a binary file\n");
  172. #endif
  173. return (AE_BAD_HEADER);
  174. }
  175. #ifdef ACPI_OBSOLETE_CODE
  176. /* We only support a limited number of table types */
  177. if (!ACPI_COMPARE_NAME
  178. ((char *)table_header.signature, ACPI_SIG_DSDT)
  179. && !ACPI_COMPARE_NAME((char *)table_header.signature,
  180. ACPI_SIG_PSDT)
  181. && !ACPI_COMPARE_NAME((char *)table_header.signature,
  182. ACPI_SIG_SSDT)) {
  183. acpi_os_printf
  184. ("Table signature [%4.4s] is invalid or not supported\n",
  185. (char *)table_header.signature);
  186. ACPI_DUMP_BUFFER(&table_header,
  187. sizeof(struct acpi_table_header));
  188. return (AE_ERROR);
  189. }
  190. #endif
  191. *table_length = table_header.length;
  192. }
  193. /* Allocate a buffer for the table */
  194. *table = acpi_os_allocate((size_t) file_size);
  195. if (!*table) {
  196. acpi_os_printf
  197. ("Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
  198. table_header.signature, *table_length);
  199. return (AE_NO_MEMORY);
  200. }
  201. /* Get the rest of the table */
  202. fseek(fp, 0, SEEK_SET);
  203. actual = fread(*table, 1, (size_t) file_size, fp);
  204. if (actual == file_size) {
  205. if (standard_header) {
  206. /* Now validate the checksum */
  207. status = acpi_tb_verify_checksum((void *)*table,
  208. ACPI_CAST_PTR(struct
  209. acpi_table_header,
  210. *table)->
  211. length);
  212. if (status == AE_BAD_CHECKSUM) {
  213. status =
  214. acpi_ut_check_text_mode_corruption((u8 *)
  215. *table,
  216. file_size,
  217. (*table)->
  218. length);
  219. return (status);
  220. }
  221. }
  222. return (AE_OK);
  223. }
  224. if (actual > 0) {
  225. acpi_os_printf("Warning - reading table, asked for %X got %X\n",
  226. file_size, actual);
  227. return (AE_OK);
  228. }
  229. acpi_os_printf("Error - could not read the table file\n");
  230. acpi_os_free(*table);
  231. *table = NULL;
  232. *table_length = 0;
  233. return (AE_ERROR);
  234. }
  235. /*******************************************************************************
  236. *
  237. * FUNCTION: acpi_ut_read_table_from_file
  238. *
  239. * PARAMETERS: filename - File where table is located
  240. * table - Where a pointer to the table is returned
  241. *
  242. * RETURN: Status
  243. *
  244. * DESCRIPTION: Get an ACPI table from a file
  245. *
  246. ******************************************************************************/
  247. acpi_status
  248. acpi_ut_read_table_from_file(char *filename, struct acpi_table_header ** table)
  249. {
  250. FILE *file;
  251. u32 file_size;
  252. u32 table_length;
  253. acpi_status status = AE_ERROR;
  254. /* Open the file, get current size */
  255. file = fopen(filename, "rb");
  256. if (!file) {
  257. perror("Could not open input file");
  258. return (status);
  259. }
  260. file_size = cm_get_file_size(file);
  261. if (file_size == ACPI_UINT32_MAX) {
  262. goto exit;
  263. }
  264. /* Get the entire file */
  265. fprintf(stderr,
  266. "Reading ACPI table from file %12s - Length %.8u (0x%06X)\n",
  267. filename, file_size, file_size);
  268. status = acpi_ut_read_table(file, table, &table_length);
  269. if (ACPI_FAILURE(status)) {
  270. acpi_os_printf("Could not get table from the file\n");
  271. }
  272. exit:
  273. fclose(file);
  274. return (status);
  275. }
  276. #endif