195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
295b482a8SLen Brown /******************************************************************************
395b482a8SLen Brown *
495b482a8SLen Brown * Module Name: exnames - interpreter/scanner name load/execute
595b482a8SLen Brown *
6*612c2932SBob Moore * Copyright (C) 2000 - 2023, Intel Corp.
795b482a8SLen Brown *
895857638SErik Schmauss *****************************************************************************/
995b482a8SLen Brown
1095b482a8SLen Brown #include <acpi/acpi.h>
11e2f7a777SLen Brown #include "accommon.h"
12e2f7a777SLen Brown #include "acinterp.h"
13e2f7a777SLen Brown #include "amlcode.h"
1495b482a8SLen Brown
1595b482a8SLen Brown #define _COMPONENT ACPI_EXECUTER
1695b482a8SLen Brown ACPI_MODULE_NAME("exnames")
1795b482a8SLen Brown
1895b482a8SLen Brown /* Local prototypes */
1995b482a8SLen Brown static char *acpi_ex_allocate_name_string(u32 prefix_count, u32 num_name_segs);
2095b482a8SLen Brown
211f86e8c1SLv Zheng static acpi_status acpi_ex_name_segment(u8 **in_aml_address, char *name_string);
2295b482a8SLen Brown
2395b482a8SLen Brown /*******************************************************************************
2495b482a8SLen Brown *
2595b482a8SLen Brown * FUNCTION: acpi_ex_allocate_name_string
2695b482a8SLen Brown *
2795b482a8SLen Brown * PARAMETERS: prefix_count - Count of parent levels. Special cases:
2895b482a8SLen Brown * (-1)==root, 0==none
2995b482a8SLen Brown * num_name_segs - count of 4-character name segments
3095b482a8SLen Brown *
3195b482a8SLen Brown * RETURN: A pointer to the allocated string segment. This segment must
3295b482a8SLen Brown * be deleted by the caller.
3395b482a8SLen Brown *
3495b482a8SLen Brown * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
3595b482a8SLen Brown * string is long enough, and set up prefix if any.
3695b482a8SLen Brown *
3795b482a8SLen Brown ******************************************************************************/
3895b482a8SLen Brown
acpi_ex_allocate_name_string(u32 prefix_count,u32 num_name_segs)3995b482a8SLen Brown static char *acpi_ex_allocate_name_string(u32 prefix_count, u32 num_name_segs)
4095b482a8SLen Brown {
4195b482a8SLen Brown char *temp_ptr;
4295b482a8SLen Brown char *name_string;
4395b482a8SLen Brown u32 size_needed;
4495b482a8SLen Brown
4595b482a8SLen Brown ACPI_FUNCTION_TRACE(ex_allocate_name_string);
4695b482a8SLen Brown
4795b482a8SLen Brown /*
4895b482a8SLen Brown * Allow room for all \ and ^ prefixes, all segments and a multi_name_prefix.
4995b482a8SLen Brown * Also, one byte for the null terminator.
5095b482a8SLen Brown * This may actually be somewhat longer than needed.
5195b482a8SLen Brown */
5295b482a8SLen Brown if (prefix_count == ACPI_UINT32_MAX) {
5395b482a8SLen Brown
5495b482a8SLen Brown /* Special case for root */
5595b482a8SLen Brown
5632786755SBob Moore size_needed = 1 + (ACPI_NAMESEG_SIZE * num_name_segs) + 2 + 1;
5795b482a8SLen Brown } else {
5895b482a8SLen Brown size_needed =
5932786755SBob Moore prefix_count + (ACPI_NAMESEG_SIZE * num_name_segs) + 2 + 1;
6095b482a8SLen Brown }
6195b482a8SLen Brown
6295b482a8SLen Brown /*
6395b482a8SLen Brown * Allocate a buffer for the name.
6495b482a8SLen Brown * This buffer must be deleted by the caller!
6595b482a8SLen Brown */
6695b482a8SLen Brown name_string = ACPI_ALLOCATE(size_needed);
6795b482a8SLen Brown if (!name_string) {
6895b482a8SLen Brown ACPI_ERROR((AE_INFO,
69f6a22b0bSBob Moore "Could not allocate size %u", size_needed));
7095b482a8SLen Brown return_PTR(NULL);
7195b482a8SLen Brown }
7295b482a8SLen Brown
7395b482a8SLen Brown temp_ptr = name_string;
7495b482a8SLen Brown
7595b482a8SLen Brown /* Set up Root or Parent prefixes if needed */
7695b482a8SLen Brown
7795b482a8SLen Brown if (prefix_count == ACPI_UINT32_MAX) {
7895b482a8SLen Brown *temp_ptr++ = AML_ROOT_PREFIX;
7995b482a8SLen Brown } else {
8095b482a8SLen Brown while (prefix_count--) {
8195b482a8SLen Brown *temp_ptr++ = AML_PARENT_PREFIX;
8295b482a8SLen Brown }
8395b482a8SLen Brown }
8495b482a8SLen Brown
8595b482a8SLen Brown /* Set up Dual or Multi prefixes if needed */
8695b482a8SLen Brown
8795b482a8SLen Brown if (num_name_segs > 2) {
8895b482a8SLen Brown
8995b482a8SLen Brown /* Set up multi prefixes */
9095b482a8SLen Brown
919ff5a21aSBob Moore *temp_ptr++ = AML_MULTI_NAME_PREFIX;
9295b482a8SLen Brown *temp_ptr++ = (char)num_name_segs;
9395b482a8SLen Brown } else if (2 == num_name_segs) {
9495b482a8SLen Brown
9595b482a8SLen Brown /* Set up dual prefixes */
9695b482a8SLen Brown
9795b482a8SLen Brown *temp_ptr++ = AML_DUAL_NAME_PREFIX;
9895b482a8SLen Brown }
9995b482a8SLen Brown
10095b482a8SLen Brown /*
10195b482a8SLen Brown * Terminate string following prefixes. acpi_ex_name_segment() will
10295b482a8SLen Brown * append the segment(s)
10395b482a8SLen Brown */
10495b482a8SLen Brown *temp_ptr = 0;
10595b482a8SLen Brown
10695b482a8SLen Brown return_PTR(name_string);
10795b482a8SLen Brown }
10895b482a8SLen Brown
10995b482a8SLen Brown /*******************************************************************************
11095b482a8SLen Brown *
11195b482a8SLen Brown * FUNCTION: acpi_ex_name_segment
11295b482a8SLen Brown *
11395b482a8SLen Brown * PARAMETERS: in_aml_address - Pointer to the name in the AML code
11495b482a8SLen Brown * name_string - Where to return the name. The name is appended
11595b482a8SLen Brown * to any existing string to form a namepath
11695b482a8SLen Brown *
11795b482a8SLen Brown * RETURN: Status
11895b482a8SLen Brown *
11995b482a8SLen Brown * DESCRIPTION: Extract an ACPI name (4 bytes) from the AML byte stream
12095b482a8SLen Brown *
12195b482a8SLen Brown ******************************************************************************/
12295b482a8SLen Brown
acpi_ex_name_segment(u8 ** in_aml_address,char * name_string)12395b482a8SLen Brown static acpi_status acpi_ex_name_segment(u8 ** in_aml_address, char *name_string)
12495b482a8SLen Brown {
12595b482a8SLen Brown char *aml_address = (void *)*in_aml_address;
12695b482a8SLen Brown acpi_status status = AE_OK;
12795b482a8SLen Brown u32 index;
12895b482a8SLen Brown char char_buf[5];
12995b482a8SLen Brown
13095b482a8SLen Brown ACPI_FUNCTION_TRACE(ex_name_segment);
13195b482a8SLen Brown
13295b482a8SLen Brown /*
1331fad8738SBob Moore * If first character is a digit, then we know that we aren't looking
1341fad8738SBob Moore * at a valid name segment
13595b482a8SLen Brown */
13695b482a8SLen Brown char_buf[0] = *aml_address;
13795b482a8SLen Brown
13895b482a8SLen Brown if ('0' <= char_buf[0] && char_buf[0] <= '9') {
13995b482a8SLen Brown ACPI_ERROR((AE_INFO, "Invalid leading digit: %c", char_buf[0]));
14095b482a8SLen Brown return_ACPI_STATUS(AE_CTRL_PENDING);
14195b482a8SLen Brown }
14295b482a8SLen Brown
1431f86e8c1SLv Zheng for (index = 0;
14432786755SBob Moore (index < ACPI_NAMESEG_SIZE)
1456a0df32cSBob Moore && (acpi_ut_valid_name_char(*aml_address, 0)); index++) {
14695b482a8SLen Brown char_buf[index] = *aml_address++;
14795b482a8SLen Brown }
14895b482a8SLen Brown
14995b482a8SLen Brown /* Valid name segment */
15095b482a8SLen Brown
15195b482a8SLen Brown if (index == 4) {
15295b482a8SLen Brown
15395b482a8SLen Brown /* Found 4 valid characters */
15495b482a8SLen Brown
15595b482a8SLen Brown char_buf[4] = '\0';
15695b482a8SLen Brown
15795b482a8SLen Brown if (name_string) {
15895b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
1591ef63231SBob Moore "Appending NameSeg %s\n", char_buf));
1601ef63231SBob Moore strcat(name_string, char_buf);
16195b482a8SLen Brown } else {
16295b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
16395b482a8SLen Brown "No Name string - %s\n", char_buf));
16495b482a8SLen Brown }
16595b482a8SLen Brown } else if (index == 0) {
16695b482a8SLen Brown /*
16795b482a8SLen Brown * First character was not a valid name character,
16895b482a8SLen Brown * so we are looking at something other than a name.
16995b482a8SLen Brown */
17095b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_INFO,
17195b482a8SLen Brown "Leading character is not alpha: %02Xh (not a name)\n",
17295b482a8SLen Brown char_buf[0]));
17395b482a8SLen Brown status = AE_CTRL_PENDING;
17495b482a8SLen Brown } else {
17595b482a8SLen Brown /*
17695b482a8SLen Brown * Segment started with one or more valid characters, but fewer than
17795b482a8SLen Brown * the required 4
17895b482a8SLen Brown */
17995b482a8SLen Brown status = AE_AML_BAD_NAME;
18095b482a8SLen Brown ACPI_ERROR((AE_INFO,
181f6a22b0bSBob Moore "Bad character 0x%02x in name, at %p",
18295b482a8SLen Brown *aml_address, aml_address));
18395b482a8SLen Brown }
18495b482a8SLen Brown
18595b482a8SLen Brown *in_aml_address = ACPI_CAST_PTR(u8, aml_address);
18695b482a8SLen Brown return_ACPI_STATUS(status);
18795b482a8SLen Brown }
18895b482a8SLen Brown
18995b482a8SLen Brown /*******************************************************************************
19095b482a8SLen Brown *
19195b482a8SLen Brown * FUNCTION: acpi_ex_get_name_string
19295b482a8SLen Brown *
19395b482a8SLen Brown * PARAMETERS: data_type - Object type to be associated with this
19495b482a8SLen Brown * name
19595b482a8SLen Brown * in_aml_address - Pointer to the namestring in the AML code
19695b482a8SLen Brown * out_name_string - Where the namestring is returned
19795b482a8SLen Brown * out_name_length - Length of the returned string
19895b482a8SLen Brown *
19995b482a8SLen Brown * RETURN: Status, namestring and length
20095b482a8SLen Brown *
20195b482a8SLen Brown * DESCRIPTION: Extract a full namepath from the AML byte stream,
20295b482a8SLen Brown * including any prefixes.
20395b482a8SLen Brown *
20495b482a8SLen Brown ******************************************************************************/
20595b482a8SLen Brown
20695b482a8SLen Brown acpi_status
acpi_ex_get_name_string(acpi_object_type data_type,u8 * in_aml_address,char ** out_name_string,u32 * out_name_length)20795b482a8SLen Brown acpi_ex_get_name_string(acpi_object_type data_type,
20895b482a8SLen Brown u8 * in_aml_address,
20995b482a8SLen Brown char **out_name_string, u32 * out_name_length)
21095b482a8SLen Brown {
21195b482a8SLen Brown acpi_status status = AE_OK;
21295b482a8SLen Brown u8 *aml_address = in_aml_address;
21395b482a8SLen Brown char *name_string = NULL;
21495b482a8SLen Brown u32 num_segments;
21595b482a8SLen Brown u32 prefix_count = 0;
21695b482a8SLen Brown u8 has_prefix = FALSE;
21795b482a8SLen Brown
21895b482a8SLen Brown ACPI_FUNCTION_TRACE_PTR(ex_get_name_string, aml_address);
21995b482a8SLen Brown
22095b482a8SLen Brown if (ACPI_TYPE_LOCAL_REGION_FIELD == data_type ||
22195b482a8SLen Brown ACPI_TYPE_LOCAL_BANK_FIELD == data_type ||
22295b482a8SLen Brown ACPI_TYPE_LOCAL_INDEX_FIELD == data_type) {
22395b482a8SLen Brown
22495b482a8SLen Brown /* Disallow prefixes for types associated with field_unit names */
22595b482a8SLen Brown
22695b482a8SLen Brown name_string = acpi_ex_allocate_name_string(0, 1);
22795b482a8SLen Brown if (!name_string) {
22895b482a8SLen Brown status = AE_NO_MEMORY;
22995b482a8SLen Brown } else {
23095b482a8SLen Brown status =
23195b482a8SLen Brown acpi_ex_name_segment(&aml_address, name_string);
23295b482a8SLen Brown }
23395b482a8SLen Brown } else {
23495b482a8SLen Brown /*
23595b482a8SLen Brown * data_type is not a field name.
23695b482a8SLen Brown * Examine first character of name for root or parent prefix operators
23795b482a8SLen Brown */
23895b482a8SLen Brown switch (*aml_address) {
23995b482a8SLen Brown case AML_ROOT_PREFIX:
24095b482a8SLen Brown
24195b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
24295b482a8SLen Brown "RootPrefix(\\) at %p\n",
24395b482a8SLen Brown aml_address));
24495b482a8SLen Brown
24595b482a8SLen Brown /*
24695b482a8SLen Brown * Remember that we have a root_prefix --
24795b482a8SLen Brown * see comment in acpi_ex_allocate_name_string()
24895b482a8SLen Brown */
24995b482a8SLen Brown aml_address++;
25095b482a8SLen Brown prefix_count = ACPI_UINT32_MAX;
25195b482a8SLen Brown has_prefix = TRUE;
25295b482a8SLen Brown break;
25395b482a8SLen Brown
25495b482a8SLen Brown case AML_PARENT_PREFIX:
25595b482a8SLen Brown
25695b482a8SLen Brown /* Increment past possibly multiple parent prefixes */
25795b482a8SLen Brown
25895b482a8SLen Brown do {
25995b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
26095b482a8SLen Brown "ParentPrefix (^) at %p\n",
26195b482a8SLen Brown aml_address));
26295b482a8SLen Brown
26395b482a8SLen Brown aml_address++;
26495b482a8SLen Brown prefix_count++;
26595b482a8SLen Brown
26695b482a8SLen Brown } while (*aml_address == AML_PARENT_PREFIX);
26795b482a8SLen Brown
26895b482a8SLen Brown has_prefix = TRUE;
26995b482a8SLen Brown break;
27095b482a8SLen Brown
27195b482a8SLen Brown default:
27295b482a8SLen Brown
27395b482a8SLen Brown /* Not a prefix character */
27495b482a8SLen Brown
27595b482a8SLen Brown break;
27695b482a8SLen Brown }
27795b482a8SLen Brown
27895b482a8SLen Brown /* Examine first character of name for name segment prefix operator */
27995b482a8SLen Brown
28095b482a8SLen Brown switch (*aml_address) {
28195b482a8SLen Brown case AML_DUAL_NAME_PREFIX:
28295b482a8SLen Brown
28395b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
28495b482a8SLen Brown "DualNamePrefix at %p\n",
28595b482a8SLen Brown aml_address));
28695b482a8SLen Brown
28795b482a8SLen Brown aml_address++;
28895b482a8SLen Brown name_string =
28995b482a8SLen Brown acpi_ex_allocate_name_string(prefix_count, 2);
29095b482a8SLen Brown if (!name_string) {
29195b482a8SLen Brown status = AE_NO_MEMORY;
29295b482a8SLen Brown break;
29395b482a8SLen Brown }
29495b482a8SLen Brown
29595b482a8SLen Brown /* Indicate that we processed a prefix */
29695b482a8SLen Brown
29795b482a8SLen Brown has_prefix = TRUE;
29895b482a8SLen Brown
29995b482a8SLen Brown status =
30095b482a8SLen Brown acpi_ex_name_segment(&aml_address, name_string);
30195b482a8SLen Brown if (ACPI_SUCCESS(status)) {
30295b482a8SLen Brown status =
30395b482a8SLen Brown acpi_ex_name_segment(&aml_address,
30495b482a8SLen Brown name_string);
30595b482a8SLen Brown }
30695b482a8SLen Brown break;
30795b482a8SLen Brown
3089ff5a21aSBob Moore case AML_MULTI_NAME_PREFIX:
30995b482a8SLen Brown
31095b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
31195b482a8SLen Brown "MultiNamePrefix at %p\n",
31295b482a8SLen Brown aml_address));
31395b482a8SLen Brown
31495b482a8SLen Brown /* Fetch count of segments remaining in name path */
31595b482a8SLen Brown
31695b482a8SLen Brown aml_address++;
31795b482a8SLen Brown num_segments = *aml_address;
31895b482a8SLen Brown
31995b482a8SLen Brown name_string =
32095b482a8SLen Brown acpi_ex_allocate_name_string(prefix_count,
32195b482a8SLen Brown num_segments);
32295b482a8SLen Brown if (!name_string) {
32395b482a8SLen Brown status = AE_NO_MEMORY;
32495b482a8SLen Brown break;
32595b482a8SLen Brown }
32695b482a8SLen Brown
32795b482a8SLen Brown /* Indicate that we processed a prefix */
32895b482a8SLen Brown
32995b482a8SLen Brown aml_address++;
33095b482a8SLen Brown has_prefix = TRUE;
33195b482a8SLen Brown
33295b482a8SLen Brown while (num_segments &&
33395b482a8SLen Brown (status =
33495b482a8SLen Brown acpi_ex_name_segment(&aml_address,
33595b482a8SLen Brown name_string)) == AE_OK) {
33695b482a8SLen Brown num_segments--;
33795b482a8SLen Brown }
33895b482a8SLen Brown
33995b482a8SLen Brown break;
34095b482a8SLen Brown
34195b482a8SLen Brown case 0:
34295b482a8SLen Brown
34395b482a8SLen Brown /* null_name valid as of 8-12-98 ASL/AML Grammar Update */
34495b482a8SLen Brown
34595b482a8SLen Brown if (prefix_count == ACPI_UINT32_MAX) {
34695b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
34795b482a8SLen Brown "NameSeg is \"\\\" followed by NULL\n"));
34895b482a8SLen Brown }
34995b482a8SLen Brown
35095b482a8SLen Brown /* Consume the NULL byte */
35195b482a8SLen Brown
35295b482a8SLen Brown aml_address++;
35395b482a8SLen Brown name_string =
35495b482a8SLen Brown acpi_ex_allocate_name_string(prefix_count, 0);
35595b482a8SLen Brown if (!name_string) {
35695b482a8SLen Brown status = AE_NO_MEMORY;
35795b482a8SLen Brown break;
35895b482a8SLen Brown }
35995b482a8SLen Brown
36095b482a8SLen Brown break;
36195b482a8SLen Brown
36295b482a8SLen Brown default:
36395b482a8SLen Brown
36495b482a8SLen Brown /* Name segment string */
36595b482a8SLen Brown
36695b482a8SLen Brown name_string =
36795b482a8SLen Brown acpi_ex_allocate_name_string(prefix_count, 1);
36895b482a8SLen Brown if (!name_string) {
36995b482a8SLen Brown status = AE_NO_MEMORY;
37095b482a8SLen Brown break;
37195b482a8SLen Brown }
37295b482a8SLen Brown
37395b482a8SLen Brown status =
37495b482a8SLen Brown acpi_ex_name_segment(&aml_address, name_string);
37595b482a8SLen Brown break;
37695b482a8SLen Brown }
37795b482a8SLen Brown }
37895b482a8SLen Brown
37995b482a8SLen Brown if (AE_CTRL_PENDING == status && has_prefix) {
38095b482a8SLen Brown
38195b482a8SLen Brown /* Ran out of segments after processing a prefix */
38295b482a8SLen Brown
38395b482a8SLen Brown ACPI_ERROR((AE_INFO, "Malformed Name at %p", name_string));
38495b482a8SLen Brown status = AE_AML_BAD_NAME;
38595b482a8SLen Brown }
38695b482a8SLen Brown
38795b482a8SLen Brown if (ACPI_FAILURE(status)) {
38895b482a8SLen Brown if (name_string) {
38995b482a8SLen Brown ACPI_FREE(name_string);
39095b482a8SLen Brown }
39195b482a8SLen Brown return_ACPI_STATUS(status);
39295b482a8SLen Brown }
39395b482a8SLen Brown
39495b482a8SLen Brown *out_name_string = name_string;
39595b482a8SLen Brown *out_name_length = (u32) (aml_address - in_aml_address);
39695b482a8SLen Brown
39795b482a8SLen Brown return_ACPI_STATUS(status);
39895b482a8SLen Brown }
399