utfileio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. status = fl_check_for_ascii(fp, NULL, FALSE);
  171. if (ACPI_SUCCESS(status)) {
  172. acpi_os_printf
  173. ("File appears to be ASCII only, must be binary\n");
  174. }
  175. #endif
  176. return (AE_BAD_HEADER);
  177. }
  178. #ifdef ACPI_OBSOLETE_CODE
  179. /* We only support a limited number of table types */
  180. if (!ACPI_COMPARE_NAME
  181. ((char *)table_header.signature, ACPI_SIG_DSDT)
  182. && !ACPI_COMPARE_NAME((char *)table_header.signature,
  183. ACPI_SIG_PSDT)
  184. && !ACPI_COMPARE_NAME((char *)table_header.signature,
  185. ACPI_SIG_SSDT)) {
  186. acpi_os_printf
  187. ("Table signature [%4.4s] is invalid or not supported\n",
  188. (char *)table_header.signature);
  189. ACPI_DUMP_BUFFER(&table_header,
  190. sizeof(struct acpi_table_header));
  191. return (AE_ERROR);
  192. }
  193. #endif
  194. *table_length = table_header.length;
  195. }
  196. /* Allocate a buffer for the table */
  197. *table = acpi_os_allocate((size_t) file_size);
  198. if (!*table) {
  199. acpi_os_printf
  200. ("Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
  201. table_header.signature, *table_length);
  202. return (AE_NO_MEMORY);
  203. }
  204. /* Get the rest of the table */
  205. fseek(fp, 0, SEEK_SET);
  206. actual = fread(*table, 1, (size_t) file_size, fp);
  207. if (actual == file_size) {
  208. if (standard_header) {
  209. /* Now validate the checksum */
  210. status = acpi_tb_verify_checksum((void *)*table,
  211. ACPI_CAST_PTR(struct
  212. acpi_table_header,
  213. *table)->
  214. length);
  215. if (status == AE_BAD_CHECKSUM) {
  216. status =
  217. acpi_ut_check_text_mode_corruption((u8 *)
  218. *table,
  219. file_size,
  220. (*table)->
  221. length);
  222. return (status);
  223. }
  224. }
  225. return (AE_OK);
  226. }
  227. if (actual > 0) {
  228. acpi_os_printf("Warning - reading table, asked for %X got %X\n",
  229. file_size, actual);
  230. return (AE_OK);
  231. }
  232. acpi_os_printf("Error - could not read the table file\n");
  233. acpi_os_free(*table);
  234. *table = NULL;
  235. *table_length = 0;
  236. return (AE_ERROR);
  237. }
  238. /*******************************************************************************
  239. *
  240. * FUNCTION: acpi_ut_read_table_from_file
  241. *
  242. * PARAMETERS: filename - File where table is located
  243. * table - Where a pointer to the table is returned
  244. *
  245. * RETURN: Status
  246. *
  247. * DESCRIPTION: Get an ACPI table from a file
  248. *
  249. ******************************************************************************/
  250. acpi_status
  251. acpi_ut_read_table_from_file(char *filename, struct acpi_table_header ** table)
  252. {
  253. FILE *file;
  254. u32 file_size;
  255. u32 table_length;
  256. acpi_status status = AE_ERROR;
  257. /* Open the file, get current size */
  258. file = fopen(filename, "rb");
  259. if (!file) {
  260. perror("Could not open input file");
  261. return (status);
  262. }
  263. file_size = cm_get_file_size(file);
  264. if (file_size == ACPI_UINT32_MAX) {
  265. goto exit;
  266. }
  267. /* Get the entire file */
  268. fprintf(stderr,
  269. "Loading Acpi table from file %10s - Length %.8u (%06X)\n",
  270. filename, file_size, file_size);
  271. status = acpi_ut_read_table(file, table, &table_length);
  272. if (ACPI_FAILURE(status)) {
  273. acpi_os_printf("Could not get table from the file\n");
  274. }
  275. exit:
  276. fclose(file);
  277. return (status);
  278. }
  279. #endif