xref: /openbmc/linux/drivers/acpi/acpica/utascii.c (revision bd5a594b)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: utascii - Utility ascii functions
5  *
6  * Copyright (C) 2000 - 2023, Intel Corp.
7  *
8  *****************************************************************************/
9 
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 
13 /*******************************************************************************
14  *
15  * FUNCTION:    acpi_ut_valid_nameseg
16  *
17  * PARAMETERS:  name            - The name or table signature to be examined.
18  *                                Four characters, does not have to be a
19  *                                NULL terminated string.
20  *
21  * RETURN:      TRUE if signature is has 4 valid ACPI characters
22  *
23  * DESCRIPTION: Validate an ACPI table signature.
24  *
25  ******************************************************************************/
26 
27 u8 acpi_ut_valid_nameseg(char *name)
28 {
29 	u32 i;
30 
31 	/* Validate each character in the signature */
32 
33 	for (i = 0; i < ACPI_NAMESEG_SIZE; i++) {
34 		if (!acpi_ut_valid_name_char(name[i], i)) {
35 			return (FALSE);
36 		}
37 	}
38 
39 	return (TRUE);
40 }
41 
42 /*******************************************************************************
43  *
44  * FUNCTION:    acpi_ut_valid_name_char
45  *
46  * PARAMETERS:  char            - The character to be examined
47  *              position        - Byte position (0-3)
48  *
49  * RETURN:      TRUE if the character is valid, FALSE otherwise
50  *
51  * DESCRIPTION: Check for a valid ACPI character. Must be one of:
52  *              1) Upper case alpha
53  *              2) numeric
54  *              3) underscore
55  *
56  *              We allow a '!' as the last character because of the ASF! table
57  *
58  ******************************************************************************/
59 
60 u8 acpi_ut_valid_name_char(char character, u32 position)
61 {
62 
63 	if (!((character >= 'A' && character <= 'Z') ||
64 	      (character >= '0' && character <= '9') || (character == '_'))) {
65 
66 		/* Allow a '!' in the last position */
67 
68 		if (character == '!' && position == 3) {
69 			return (TRUE);
70 		}
71 
72 		return (FALSE);
73 	}
74 
75 	return (TRUE);
76 }
77 
78 /*******************************************************************************
79  *
80  * FUNCTION:    acpi_ut_check_and_repair_ascii
81  *
82  * PARAMETERS:  name                - Ascii string
83  *              count               - Number of characters to check
84  *
85  * RETURN:      None
86  *
87  * DESCRIPTION: Ensure that the requested number of characters are printable
88  *              Ascii characters. Sets non-printable and null chars to <space>.
89  *
90  ******************************************************************************/
91 
92 void acpi_ut_check_and_repair_ascii(u8 *name, char *repaired_name, u32 count)
93 {
94 	u32 i;
95 
96 	for (i = 0; i < count; i++) {
97 		repaired_name[i] = (char)name[i];
98 
99 		if (!name[i]) {
100 			return;
101 		}
102 		if (!isprint(name[i])) {
103 			repaired_name[i] = ' ';
104 		}
105 	}
106 }
107