Erik Schmauss | 9585763 | 2018-03-14 16:13:07 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 2 | /******************************************************************************* |
| 3 | * |
| 4 | * Module Name: utstring - Common functions for strings and characters |
| 5 | * |
| 6 | ******************************************************************************/ |
| 7 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 8 | #include <acpi/acpi.h> |
| 9 | #include "accommon.h" |
| 10 | #include "acnamesp.h" |
| 11 | |
| 12 | #define _COMPONENT ACPI_UTILITIES |
| 13 | ACPI_MODULE_NAME("utstring") |
| 14 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 15 | /******************************************************************************* |
| 16 | * |
| 17 | * FUNCTION: acpi_ut_print_string |
| 18 | * |
| 19 | * PARAMETERS: string - Null terminated ASCII string |
Bob Moore | 0fb3adf | 2013-07-17 09:48:40 +0800 | [diff] [blame] | 20 | * max_length - Maximum output length. Used to constrain the |
| 21 | * length of strings during debug output only. |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 22 | * |
| 23 | * RETURN: None |
| 24 | * |
| 25 | * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape |
| 26 | * sequences. |
| 27 | * |
| 28 | ******************************************************************************/ |
Bob Moore | 0fb3adf | 2013-07-17 09:48:40 +0800 | [diff] [blame] | 29 | void acpi_ut_print_string(char *string, u16 max_length) |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 30 | { |
| 31 | u32 i; |
| 32 | |
| 33 | if (!string) { |
| 34 | acpi_os_printf("<\"NULL STRING PTR\">"); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | acpi_os_printf("\""); |
David Binderman | 5d42b0f | 2014-04-04 12:36:55 +0800 | [diff] [blame] | 39 | for (i = 0; (i < max_length) && string[i]; i++) { |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 40 | |
| 41 | /* Escape sequences */ |
| 42 | |
| 43 | switch (string[i]) { |
| 44 | case 0x07: |
Chao Guan | 1d1ea1b | 2013-06-08 00:58:14 +0000 | [diff] [blame] | 45 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 46 | acpi_os_printf("\\a"); /* BELL */ |
| 47 | break; |
| 48 | |
| 49 | case 0x08: |
Chao Guan | 1d1ea1b | 2013-06-08 00:58:14 +0000 | [diff] [blame] | 50 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 51 | acpi_os_printf("\\b"); /* BACKSPACE */ |
| 52 | break; |
| 53 | |
| 54 | case 0x0C: |
Chao Guan | 1d1ea1b | 2013-06-08 00:58:14 +0000 | [diff] [blame] | 55 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 56 | acpi_os_printf("\\f"); /* FORMFEED */ |
| 57 | break; |
| 58 | |
| 59 | case 0x0A: |
Chao Guan | 1d1ea1b | 2013-06-08 00:58:14 +0000 | [diff] [blame] | 60 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 61 | acpi_os_printf("\\n"); /* LINEFEED */ |
| 62 | break; |
| 63 | |
| 64 | case 0x0D: |
Chao Guan | 1d1ea1b | 2013-06-08 00:58:14 +0000 | [diff] [blame] | 65 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 66 | acpi_os_printf("\\r"); /* CARRIAGE RETURN */ |
| 67 | break; |
| 68 | |
| 69 | case 0x09: |
Chao Guan | 1d1ea1b | 2013-06-08 00:58:14 +0000 | [diff] [blame] | 70 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 71 | acpi_os_printf("\\t"); /* HORIZONTAL TAB */ |
| 72 | break; |
| 73 | |
| 74 | case 0x0B: |
Chao Guan | 1d1ea1b | 2013-06-08 00:58:14 +0000 | [diff] [blame] | 75 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 76 | acpi_os_printf("\\v"); /* VERTICAL TAB */ |
| 77 | break; |
| 78 | |
| 79 | case '\'': /* Single Quote */ |
| 80 | case '\"': /* Double Quote */ |
| 81 | case '\\': /* Backslash */ |
Chao Guan | 1d1ea1b | 2013-06-08 00:58:14 +0000 | [diff] [blame] | 82 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 83 | acpi_os_printf("\\%c", (int)string[i]); |
| 84 | break; |
| 85 | |
| 86 | default: |
| 87 | |
| 88 | /* Check for printable character or hex escape */ |
| 89 | |
Bob Moore | 4fa4616 | 2015-07-01 14:45:11 +0800 | [diff] [blame] | 90 | if (isprint((int)string[i])) { |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 91 | /* 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 Zheng | f5c1e1c | 2016-05-05 12:57:53 +0800 | [diff] [blame] | 97 | acpi_os_printf("\\x%2.2X", (s32)string[i]); |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 98 | } |
| 99 | break; |
| 100 | } |
| 101 | } |
Bob Moore | 1fad873 | 2015-12-29 13:54:36 +0800 | [diff] [blame] | 102 | |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 103 | acpi_os_printf("\""); |
| 104 | |
| 105 | if (i == max_length && string[i]) { |
| 106 | acpi_os_printf("..."); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /******************************************************************************* |
| 111 | * |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 112 | * 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 | |
| 132 | void 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 Moore | 2247235 | 2015-12-29 14:04:11 +0800 | [diff] [blame] | 140 | /* |
| 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 Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 148 | 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 Moore | 6a0df32 | 2016-05-05 13:00:36 +0800 | [diff] [blame] | 153 | if (acpi_ut_valid_name_char(name[i], i)) { |
Bob Moore | 42f8fb7 | 2013-01-11 13:08:51 +0100 | [diff] [blame] | 154 | 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 | |
| 196 | void 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 |