blob: bd57a77bbcb2ef87698f874e9286a47efa3dca6f [file] [log] [blame]
Erik Schmauss95857632018-03-14 16:13:07 -07001// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
Bob Moore42f8fb72013-01-11 13:08:51 +01002/*******************************************************************************
3 *
4 * Module Name: utstring - Common functions for strings and characters
5 *
6 ******************************************************************************/
7
Bob Moore42f8fb72013-01-11 13:08:51 +01008#include <acpi/acpi.h>
9#include "accommon.h"
10#include "acnamesp.h"
11
12#define _COMPONENT ACPI_UTILITIES
13ACPI_MODULE_NAME("utstring")
14
Bob Moore42f8fb72013-01-11 13:08:51 +010015/*******************************************************************************
16 *
17 * FUNCTION: acpi_ut_print_string
18 *
19 * PARAMETERS: string - Null terminated ASCII string
Bob Moore0fb3adf2013-07-17 09:48:40 +080020 * max_length - Maximum output length. Used to constrain the
21 * length of strings during debug output only.
Bob Moore42f8fb72013-01-11 13:08:51 +010022 *
23 * RETURN: None
24 *
25 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
26 * sequences.
27 *
28 ******************************************************************************/
Bob Moore0fb3adf2013-07-17 09:48:40 +080029void acpi_ut_print_string(char *string, u16 max_length)
Bob Moore42f8fb72013-01-11 13:08:51 +010030{
31 u32 i;
32
33 if (!string) {
34 acpi_os_printf("<\"NULL STRING PTR\">");
35 return;
36 }
37
38 acpi_os_printf("\"");
David Binderman5d42b0f2014-04-04 12:36:55 +080039 for (i = 0; (i < max_length) && string[i]; i++) {
Bob Moore42f8fb72013-01-11 13:08:51 +010040
41 /* Escape sequences */
42
43 switch (string[i]) {
44 case 0x07:
Chao Guan1d1ea1b2013-06-08 00:58:14 +000045
Bob Moore42f8fb72013-01-11 13:08:51 +010046 acpi_os_printf("\\a"); /* BELL */
47 break;
48
49 case 0x08:
Chao Guan1d1ea1b2013-06-08 00:58:14 +000050
Bob Moore42f8fb72013-01-11 13:08:51 +010051 acpi_os_printf("\\b"); /* BACKSPACE */
52 break;
53
54 case 0x0C:
Chao Guan1d1ea1b2013-06-08 00:58:14 +000055
Bob Moore42f8fb72013-01-11 13:08:51 +010056 acpi_os_printf("\\f"); /* FORMFEED */
57 break;
58
59 case 0x0A:
Chao Guan1d1ea1b2013-06-08 00:58:14 +000060
Bob Moore42f8fb72013-01-11 13:08:51 +010061 acpi_os_printf("\\n"); /* LINEFEED */
62 break;
63
64 case 0x0D:
Chao Guan1d1ea1b2013-06-08 00:58:14 +000065
Bob Moore42f8fb72013-01-11 13:08:51 +010066 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
67 break;
68
69 case 0x09:
Chao Guan1d1ea1b2013-06-08 00:58:14 +000070
Bob Moore42f8fb72013-01-11 13:08:51 +010071 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
72 break;
73
74 case 0x0B:
Chao Guan1d1ea1b2013-06-08 00:58:14 +000075
Bob Moore42f8fb72013-01-11 13:08:51 +010076 acpi_os_printf("\\v"); /* VERTICAL TAB */
77 break;
78
79 case '\'': /* Single Quote */
80 case '\"': /* Double Quote */
81 case '\\': /* Backslash */
Chao Guan1d1ea1b2013-06-08 00:58:14 +000082
Bob Moore42f8fb72013-01-11 13:08:51 +010083 acpi_os_printf("\\%c", (int)string[i]);
84 break;
85
86 default:
87
88 /* Check for printable character or hex escape */
89
Bob Moore4fa46162015-07-01 14:45:11 +080090 if (isprint((int)string[i])) {
Bob Moore42f8fb72013-01-11 13:08:51 +010091 /* This is a normal character */
92
93 acpi_os_printf("%c", (int)string[i]);
94 } else {
95 /* All others will be Hex escapes */
96
Lv Zhengf5c1e1c2016-05-05 12:57:53 +080097 acpi_os_printf("\\x%2.2X", (s32)string[i]);
Bob Moore42f8fb72013-01-11 13:08:51 +010098 }
99 break;
100 }
101 }
Bob Moore1fad8732015-12-29 13:54:36 +0800102
Bob Moore42f8fb72013-01-11 13:08:51 +0100103 acpi_os_printf("\"");
104
105 if (i == max_length && string[i]) {
106 acpi_os_printf("...");
107 }
108}
109
110/*******************************************************************************
111 *
Bob Moore42f8fb72013-01-11 13:08:51 +0100112 * FUNCTION: acpi_ut_repair_name
113 *
114 * PARAMETERS: name - The ACPI name to be repaired
115 *
116 * RETURN: Repaired version of the name
117 *
118 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
119 * return the new name. NOTE: the Name parameter must reside in
120 * read/write memory, cannot be a const.
121 *
122 * An ACPI Name must consist of valid ACPI characters. We will repair the name
123 * if necessary because we don't want to abort because of this, but we want
124 * all namespace names to be printable. A warning message is appropriate.
125 *
126 * This issue came up because there are in fact machines that exhibit
127 * this problem, and we want to be able to enable ACPI support for them,
128 * even though there are a few bad names.
129 *
130 ******************************************************************************/
131
132void acpi_ut_repair_name(char *name)
133{
134 u32 i;
135 u8 found_bad_char = FALSE;
136 u32 original_name;
137
138 ACPI_FUNCTION_NAME(ut_repair_name);
139
Bob Moore22472352015-12-29 14:04:11 +0800140 /*
141 * Special case for the root node. This can happen if we get an
142 * error during the execution of module-level code.
143 */
144 if (ACPI_COMPARE_NAME(name, "\\___")) {
145 return;
146 }
147
Bob Moore42f8fb72013-01-11 13:08:51 +0100148 ACPI_MOVE_NAME(&original_name, name);
149
150 /* Check each character in the name */
151
152 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore6a0df322016-05-05 13:00:36 +0800153 if (acpi_ut_valid_name_char(name[i], i)) {
Bob Moore42f8fb72013-01-11 13:08:51 +0100154 continue;
155 }
156
157 /*
158 * Replace a bad character with something printable, yet technically
159 * still invalid. This prevents any collisions with existing "good"
160 * names in the namespace.
161 */
162 name[i] = '*';
163 found_bad_char = TRUE;
164 }
165
166 if (found_bad_char) {
167
168 /* Report warning only if in strict mode or debug mode */
169
170 if (!acpi_gbl_enable_interpreter_slack) {
171 ACPI_WARNING((AE_INFO,
172 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
173 original_name, name));
174 } else {
175 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
176 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
177 original_name, name));
178 }
179 }
180}
181
182#if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
183/*******************************************************************************
184 *
185 * FUNCTION: ut_convert_backslashes
186 *
187 * PARAMETERS: pathname - File pathname string to be converted
188 *
189 * RETURN: Modifies the input Pathname
190 *
191 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
192 * the entire input file pathname string.
193 *
194 ******************************************************************************/
195
196void ut_convert_backslashes(char *pathname)
197{
198
199 if (!pathname) {
200 return;
201 }
202
203 while (*pathname) {
204 if (*pathname == '\\') {
205 *pathname = '/';
206 }
207
208 pathname++;
209 }
210}
211#endif