PatchworkOS
Loading...
Searching...
No Matches
namespace_modifier.c
Go to the documentation of this file.
2
11#include <kernel/log/log.h>
12
13#include <sys/list.h>
14
15#include <errno.h>
16#include <stdint.h>
17
19{
21 {
22 AML_DEBUG_ERROR(ctx, "Failed to read AliasOp");
23 return ERR;
24 }
25
27 if (source == NULL)
28 {
29 AML_DEBUG_ERROR(ctx, "Failed to read or resolve source NameString");
30 return ERR;
31 }
32 DEREF_DEFER(source);
33
34 aml_name_string_t nameString;
35 if (aml_name_string_read(ctx, &nameString) == ERR)
36 {
37 AML_DEBUG_ERROR(ctx, "Failed to read or resolve target NameString");
38 return ERR;
39 }
40
41 aml_object_t* newObject = aml_object_new();
42 if (newObject == NULL)
43 {
44 return ERR;
45 }
46 DEREF_DEFER(newObject);
47
48 if (aml_alias_set(newObject, source) == ERR ||
49 aml_namespace_add_by_name_string(&ctx->state->overlay, ctx->scope, &nameString, newObject) == ERR)
50 {
51 AML_DEBUG_ERROR(ctx, "Failed to add alias object '%s'", aml_name_string_to_string(&nameString));
52 return ERR;
53 }
54
55 return 0;
56}
57
59{
60 if (aml_token_expect(ctx, AML_NAME_OP) == ERR)
61 {
62 AML_DEBUG_ERROR(ctx, "Failed to read NameOp");
63 return ERR;
64 }
65
66 aml_name_string_t nameString;
67 if (aml_name_string_read(ctx, &nameString) == ERR)
68 {
69 AML_DEBUG_ERROR(ctx, "Failed to read NameString");
70 return ERR;
71 }
72
73 aml_object_t* newObject = aml_object_new();
74 if (newObject == NULL)
75 {
76 return ERR;
77 }
78 DEREF_DEFER(newObject);
79
80 if (aml_data_ref_object_read(ctx, newObject) == ERR ||
81 aml_namespace_add_by_name_string(&ctx->state->overlay, ctx->scope, &nameString, newObject) == ERR)
82 {
83 AML_DEBUG_ERROR(ctx, "Failed to add object '%s'", aml_name_string_to_string(&nameString));
84 return ERR;
85 }
86
87 return 0;
88}
89
91{
93 {
94 AML_DEBUG_ERROR(ctx, "Failed to read ScopeOp");
95 return ERR;
96 }
97
98 const uint8_t* start = ctx->current;
99
100 aml_pkg_length_t pkgLength;
101 if (aml_pkg_length_read(ctx, &pkgLength) == ERR)
102 {
103 AML_DEBUG_ERROR(ctx, "Failed to read PkgLength");
104 return ERR;
105 }
106
108 if (scope == NULL)
109 {
110 AML_DEBUG_ERROR(ctx, "Failed to read or resolve NameString");
111 return ERR;
112 }
113 DEREF_DEFER(scope);
114
115 const uint8_t* end = start + pkgLength;
116
117 aml_type_t type = scope->type;
118 if (type != AML_PREDEFINED_SCOPE && type != AML_DEVICE && type != AML_PROCESSOR && type != AML_THERMAL_ZONE &&
119 type != AML_POWER_RESOURCE)
120 {
121 AML_DEBUG_ERROR(ctx, "Invalid object type '%s'", aml_type_to_string(type));
122 errno = EILSEQ;
123 return ERR;
124 }
125
126 if (aml_term_list_read(ctx->state, scope, ctx->current, end, ctx) == ERR)
127 {
128 AML_DEBUG_ERROR(ctx, "Failed to read TermList");
129 return ERR;
130 }
131 ctx->current = end;
132
133 return 0;
134}
135
137{
138 aml_token_t token;
139 aml_token_peek(ctx, &token);
140
141 switch (token.num)
142 {
143 case AML_ALIAS_OP:
144 if (aml_def_alias_read(ctx) == ERR)
145 {
146 AML_DEBUG_ERROR(ctx, "Failed to read DefAlias");
147 return ERR;
148 }
149 return 0;
150 case AML_NAME_OP:
151 if (aml_def_name_read(ctx) == ERR)
152 {
153 AML_DEBUG_ERROR(ctx, "Failed to read DefName");
154 return ERR;
155 }
156 return 0;
157 case AML_SCOPE_OP:
158 if (aml_def_scope_read(ctx) == ERR)
159 {
160 AML_DEBUG_ERROR(ctx, "Failed to read DefScope");
161 return ERR;
162 }
163 return 0;
164 default:
165 AML_DEBUG_ERROR(ctx, "Invalid NamespaceModifierObj '0x%x'", token.num);
166 errno = EILSEQ;
167 return ERR;
168 }
169}
#define AML_DEBUG_ERROR(ctx, format,...)
Macro to simplify calling aml_debug_error() with the current function name.
Definition debug.h:30
uint64_t aml_data_ref_object_read(aml_term_list_ctx_t *ctx, aml_object_t *out)
Read a DataRefObject structure from the AML stream.
Definition data.c:529
uint64_t aml_name_string_read(aml_term_list_ctx_t *ctx, aml_name_string_t *out)
Reads the next data as a NameString structure from the AML bytecode stream.
Definition name.c:189
aml_object_t * aml_name_string_read_and_resolve(aml_term_list_ctx_t *ctx)
Reads the next data as a NameString structure from the AML bytecode stream and resolves it to a objec...
Definition name.c:227
uint64_t aml_def_scope_read(aml_term_list_ctx_t *ctx)
Reads a DefScope structure from the AML byte stream.
uint64_t aml_def_name_read(aml_term_list_ctx_t *ctx)
Reads a DefName structure from the AML byte stream.
uint64_t aml_namespace_modifier_obj_read(aml_term_list_ctx_t *ctx)
Reads a NameSpaceModifierObj structure from the AML byte stream.
uint64_t aml_def_alias_read(aml_term_list_ctx_t *ctx)
Reads a DefAlias structure from the AML byte stream.
uint32_t aml_pkg_length_t
PkgLength structure.
uint64_t aml_pkg_length_read(aml_term_list_ctx_t *ctx, aml_pkg_length_t *out)
Reads a PkgLength structure from the AML byte stream.
uint64_t aml_term_list_read(aml_state_t *state, aml_object_t *scope, const uint8_t *start, const uint8_t *end, aml_term_list_ctx_t *parentCtx)
Reads a TermList structure from the AML byte stream.
Definition term.c:216
uint64_t aml_namespace_add_by_name_string(aml_namespace_overlay_t *overlay, aml_object_t *start, const aml_name_string_t *nameString, aml_object_t *object)
Add an object to the namespace heirarchy using a name string.
Definition namespace.c:381
aml_type_t
ACPI data types.
Definition object.h:57
aml_object_t * aml_object_new(void)
Allocate a new ACPI object.
Definition object.c:54
uint64_t aml_alias_set(aml_object_t *object, aml_object_t *target)
Set a object as an alias to the given target object.
Definition object.c:1096
@ AML_PROCESSOR
Definition object.h:81
@ AML_THERMAL_ZONE
Definition object.h:84
@ AML_POWER_RESOURCE
Definition object.h:80
@ AML_DEVICE
Definition object.h:62
@ AML_PREDEFINED_SCOPE
Not in the spec, used internally to represent _SB, _GPE, etc.
Definition object.h:87
const char * aml_name_string_to_string(const aml_name_string_t *nameString)
Convert an aml NameString to a string.
Definition to_string.c:254
const char * aml_type_to_string(aml_type_t type)
Convert an aml data type to a string.
Definition to_string.c:5
static uint64_t aml_token_expect(aml_term_list_ctx_t *ctx, aml_token_num_t expected)
Reads a token from the AML stream and verifies it matches the expected token.
Definition token.h:353
static void aml_token_peek(aml_term_list_ctx_t *ctx, aml_token_t *out)
Attempt to read a token from the AML stream, without advancing the instruction pointer.
Definition token.h:301
@ AML_NAME_OP
Definition token.h:41
@ AML_ALIAS_OP
Definition token.h:40
@ AML_SCOPE_OP
Definition token.h:47
#define DEREF_DEFER(ptr)
RAII-style cleanup for scoped references.
Definition ref.h:54
#define errno
Error number variable.
Definition errno.h:27
#define EILSEQ
Illegal byte sequence.
Definition errno.h:447
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
static void start()
Definition main.c:542
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
A NameString structure.
Definition name.h:87
ACPI object.
Definition object.h:425
aml_namespace_overlay_t overlay
Holds any named objects created during parsing.
Definition state.h:30
Context for reading a TermList.
Definition term.h:37
aml_object_t * scope
Definition term.h:39
aml_state_t * state
Definition term.h:38
const uint8_t * current
Definition term.h:42
Token.
Definition token.h:253
aml_token_num_t num
Definition token.h:254