PatchworkOS
Loading...
Searching...
No Matches
name.c
Go to the documentation of this file.
2
9#include <kernel/log/log.h>
10
11#include <errno.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/list.h>
16
18{
19 if (aml_byte_data_read(ctx, out) == ERR)
20 {
21 AML_DEBUG_ERROR(ctx, "Failed to read ByteData");
22 return ERR;
23 }
24 return 0;
25}
26
28{
29 const uint8_t* start = ctx->current;
30
31 aml_token_t leadnamechar;
32 aml_token_read(ctx, &leadnamechar);
33
34 if (!AML_IS_LEAD_NAME_CHAR(&leadnamechar))
35 {
36 AML_DEBUG_ERROR(ctx, "Invalid LeadNameChar 0x%04x in NameSeg", leadnamechar.num);
37 errno = EILSEQ;
38 return ERR;
39 }
40
41 for (uint8_t i = 0; i < 3; i++)
42 {
43 aml_token_t namechar;
44 aml_token_read(ctx, &namechar);
45
46 if (!AML_IS_NAME_CHAR(&namechar))
47 {
48 AML_DEBUG_ERROR(ctx, "Invalid char 0x%04x in NameSeg", namechar.num);
49 errno = EILSEQ;
50 return ERR;
51 }
52 }
53
54 *out = (aml_name_seg_t*)start;
55 return 0;
56}
57
59{
61 {
62 AML_DEBUG_ERROR(ctx, "Failed to read DualNamePrefix");
63 return ERR;
64 }
65
66 const uint8_t* start = ctx->current;
67
68 // We just read the two NameSegs to verify they are valid.
69 aml_name_seg_t* temp;
70 if (aml_name_seg_read(ctx, &temp) == ERR || aml_name_seg_read(ctx, &temp) == ERR)
71 {
72 AML_DEBUG_ERROR(ctx, "Failed to read NameSeg");
73 return ERR;
74 }
75
76 *out = (aml_name_seg_t*)start;
77 return 0;
78}
79
81{
83 {
84 AML_DEBUG_ERROR(ctx, "Failed to read MultiNamePrefix");
85 return ERR;
86 }
87
88 uint8_t segCount;
89 if (aml_seg_count_read(ctx, &segCount) == ERR)
90 {
91 AML_DEBUG_ERROR(ctx, "Failed to read SegCount");
92 return ERR;
93 }
94
95 const uint8_t* start = ctx->current;
96
97 // We just read the NameSegs to verify they are valid.
98 aml_name_seg_t* temp;
99 for (size_t i = 0; i < segCount; i++)
100 {
101 if (aml_name_seg_read(ctx, &temp) == ERR)
102 {
103 AML_DEBUG_ERROR(ctx, "Failed to read NameSeg");
104 return ERR;
105 }
106 }
107
108 *outSegments = (aml_name_seg_t*)start;
109 *outSegCount = segCount;
110 return 0;
111}
112
114{
116 {
117 AML_DEBUG_ERROR(ctx, "Failed to read NullName");
118 return ERR;
119 }
120
121 return 0;
122}
123
125{
126 aml_token_t firstToken;
127 aml_token_peek(ctx, &firstToken);
128
129 if (AML_IS_LEAD_NAME_CHAR(&firstToken))
130 {
131 out->segmentCount = 1;
132 return aml_name_seg_read(ctx, &out->segments);
133 }
134 else if (firstToken.num == AML_DUAL_NAME_PREFIX)
135 {
136 out->segmentCount = 2;
137 return aml_dual_name_path_read(ctx, &out->segments);
138 }
139 else if (firstToken.num == AML_MULTI_NAME_PREFIX)
140 {
141 return aml_multi_name_path_read(ctx, &out->segments, &out->segmentCount);
142 }
143 else if (firstToken.num == AML_NULL_NAME)
144 {
145 out->segmentCount = 0;
146 out->segments = NULL;
147 return aml_null_name_read(ctx);
148 }
149 else
150 {
151 AML_DEBUG_ERROR(ctx, "Invalid name that starts with 0x%x", firstToken.num);
152 errno = EILSEQ;
153 return ERR;
154 }
155
156 return 0;
157}
158
160{
161 out->depth = 0;
162 while (true)
163 {
164 aml_token_t chr;
165 aml_token_peek(ctx, &chr);
166
167 if (chr.num != AML_PARENT_PREFIX_CHAR)
168 {
169 return 0;
170 }
171
172 ctx->current += chr.length;
173 out->depth++;
174 }
175}
176
178{
180 {
181 AML_DEBUG_ERROR(ctx, "Failed to read RootChar");
182 return ERR;
183 }
184
185 out->present = true;
186 return 0;
187}
188
190{
191 aml_token_t token;
192 aml_token_peek(ctx, &token);
193
194 aml_name_string_t nameString = {0};
195 // Starts with either rootchar or prefixpath.
196 switch (token.num)
197 {
198 case AML_ROOT_CHAR:
199 if (aml_root_char_read(ctx, &nameString.rootChar) == ERR)
200 {
201 AML_DEBUG_ERROR(ctx, "Failed to read root char");
202 return ERR;
203 }
204 break;
206 if (aml_prefix_path_read(ctx, &nameString.prefixPath) == ERR)
207 {
208 AML_DEBUG_ERROR(ctx, "Failed to read prefix path");
209 return ERR;
210 }
211 break;
212 default:
213 // Is a empty prefixpath.
214 break;
215 }
216
217 if (aml_name_path_read(ctx, &nameString.namePath) == ERR)
218 {
219 AML_DEBUG_ERROR(ctx, "Failed to read name path");
220 return ERR;
221 }
222
223 *out = nameString;
224 return 0;
225}
226
228{
229 aml_name_string_t nameStringLocal;
230 if (aml_name_string_read(ctx, &nameStringLocal) == ERR)
231 {
232 AML_DEBUG_ERROR(ctx, "Failed to read NameString");
233 return NULL;
234 }
235
236 aml_object_t* out = aml_namespace_find_by_name_string(&ctx->state->overlay, ctx->scope, &nameStringLocal);
237 if (out == NULL)
238 {
239 errno = ENOENT;
240 return NULL;
241 }
242
243 if (out->type == AML_UNINITIALIZED)
244 {
245 errno = ENOENT;
246 DEREF(out);
247 return NULL;
248 }
249
250 return out; // Transfer ownership
251}
252
254{
255 aml_token_t token;
256 aml_token_peek(ctx, &token);
257
258 aml_object_t* out = NULL;
259 switch (token.props->type)
260 {
263 break;
265 out = aml_arg_obj_read(ctx);
266 break;
268 out = aml_local_obj_read(ctx);
269 break;
270 default:
271 AML_DEBUG_ERROR(ctx, "Invalid token type '%s'", aml_token_type_to_string(token.props->type));
272 errno = EILSEQ;
273 return NULL;
274 }
275
276 if (out == NULL)
277 {
278 AML_DEBUG_ERROR(ctx, "Failed to read '%s'", token.props->name);
279 return NULL;
280 }
281
282 return out; // Transfer ownership
283}
284
286{
287 aml_token_t token;
288 aml_token_peek(ctx, &token);
289
290 aml_object_t* out = NULL;
291 switch (token.props->type)
292 {
297 break;
299 out = aml_debug_obj_read(ctx);
300 break;
303 break;
304 default:
305 AML_DEBUG_ERROR(ctx, "Invalid token type '%s'", aml_token_type_to_string(token.props->type));
306 errno = EILSEQ;
307 return NULL;
308 }
309
310 if (out == NULL)
311 {
312 AML_DEBUG_ERROR(ctx, "Failed to read '%s'", token.props->name);
313 return NULL;
314 }
315
316 return out; // Transfer ownership
317}
318
320{
321 aml_token_t token;
322 aml_token_peek(ctx, &token);
323
324 if (token.num == AML_NULL_NAME)
325 {
326 *out = NULL;
327 if (aml_null_name_read(ctx) == ERR)
328 {
329 AML_DEBUG_ERROR(ctx, "Failed to read null name");
330 return ERR;
331 }
332 }
333 else
334 {
335 *out = aml_super_name_read_and_resolve(ctx); // Transfer ownership
336 if (*out == NULL)
337 {
338 AML_DEBUG_ERROR(ctx, "Failed to read or resolve SuperName");
339 return ERR;
340 }
341 }
342
343 return 0;
344}
#define AML_DEBUG_ERROR(ctx, format,...)
Macro to simplify calling aml_debug_error() with the current function name.
Definition debug.h:30
aml_object_t * aml_arg_obj_read(aml_term_list_ctx_t *ctx)
Reads a ArgObj structure from the AML byte stream.
Definition arg.c:8
uint64_t aml_byte_data_read(aml_term_list_ctx_t *ctx, uint8_t *out)
Read a ByteData structure from the AML stream.
Definition data.c:16
aml_object_t * aml_debug_obj_read(aml_term_list_ctx_t *ctx)
Reads a DebugObj structure from the AML byte stream.
Definition debug.c:6
aml_object_t * aml_reference_type_opcode_read(aml_term_list_ctx_t *ctx)
aml_object_t * aml_local_obj_read(aml_term_list_ctx_t *ctx)
Reads a LocalObj structure from the AML byte stream.
Definition local.c:8
uint64_t aml_seg_count_read(aml_term_list_ctx_t *ctx, uint8_t *out)
Reads the next data as a SegCount structure from the AML bytecode stream.
Definition name.c:17
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_null_name_read(aml_term_list_ctx_t *ctx)
Definition name.c:113
uint64_t aml_multi_name_path_read(aml_term_list_ctx_t *ctx, aml_name_seg_t **outSegments, uint64_t *outSegCount)
Reads the next data as a MultiNamePath structure from the AML bytecode stream.
Definition name.c:80
aml_object_t * aml_simple_name_read_and_resolve(aml_term_list_ctx_t *ctx)
Reads a SimpleName structure from the AML byte stream and resolves it to a object.
Definition name.c:253
uint64_t aml_name_seg_read(aml_term_list_ctx_t *ctx, aml_name_seg_t **out)
Reads the next data as a NameSeg from the AML bytecode stream.
Definition name.c:27
uint64_t aml_name_path_read(aml_term_list_ctx_t *ctx, aml_name_path_t *out)
Reads the next data as a NamePath structure from the AML bytecode stream.
Definition name.c:124
#define AML_IS_LEAD_NAME_CHAR(token)
Check if a token is a LeadNameChar structure.
Definition name.h:28
#define AML_IS_NAME_CHAR(token)
Check if a token is a NameChar structure.
Definition name.h:45
aml_object_t * aml_super_name_read_and_resolve(aml_term_list_ctx_t *ctx)
Reads a SuperName structure from the AML byte stream and resolves it to a object.
Definition name.c:285
uint64_t aml_root_char_read(aml_term_list_ctx_t *ctx, aml_root_char_t *out)
Reads the next data as a RootChar from the AML bytecode stream.
Definition name.c:177
uint64_t aml_target_read_and_resolve(aml_term_list_ctx_t *ctx, aml_object_t **out)
Reads a Target structure from the AML byte stream and resolves it to a object.
Definition name.c:319
uint64_t aml_dual_name_path_read(aml_term_list_ctx_t *ctx, aml_name_seg_t **out)
Reads the next data as a DualNamePath structure from the AML bytecode stream.
Definition name.c:58
uint64_t aml_prefix_path_read(aml_term_list_ctx_t *ctx, aml_prefix_path_t *out)
Reads the next data as a PrefixPath structure from the AML bytecode stream.
Definition name.c:159
aml_object_t * aml_namespace_find_by_name_string(aml_namespace_overlay_t *overlay, aml_object_t *start, const aml_name_string_t *nameString)
Find an object in the namespace heirarchy by a name string.
Definition namespace.c:174
@ AML_UNINITIALIZED
Definition object.h:58
const char * aml_token_type_to_string(aml_token_type_t type)
Convert a token type to a string.
Definition token.c:139
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_read(aml_term_list_ctx_t *ctx, aml_token_t *out)
Attempt to read a token from the AML stream.
Definition token.h:340
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_ROOT_CHAR
Definition token.h:92
@ AML_NULL_NAME
Definition token.h:37
@ AML_PARENT_PREFIX_CHAR
Definition token.h:93
@ AML_MULTI_NAME_PREFIX
Definition token.h:54
@ AML_DUAL_NAME_PREFIX
Definition token.h:53
@ AML_TOKEN_TYPE_DEBUG
Is a Debug Object (section 20.2.6.3).
Definition token.h:234
@ AML_TOKEN_TYPE_EXPRESSION
Is an Expression Opcode (section 20.2.5.4).
Definition token.h:230
@ AML_TOKEN_TYPE_NAME
Is a Name Object (section 20.2.2).
Definition token.h:226
@ AML_TOKEN_TYPE_ARG
Is an Arg Object (section 20.2.6.1).
Definition token.h:231
@ AML_TOKEN_TYPE_LOCAL
Is a Local Object (section 20.2.6.2).
Definition token.h:232
#define DEREF(ptr)
Decrement reference count.
Definition ref.h:80
#define ENOENT
No such file or directory.
Definition errno.h:42
#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
Represents the NamePath, DualNamePath, MultiNamePath and NullPath structures.
Definition name.h:77
aml_name_seg_t * segments
Array of segments in the name path.
Definition name.h:79
uint64_t segmentCount
Number of segments in the name path.
Definition name.h:78
A NameSeg strcture.
A NameString structure.
Definition name.h:87
aml_name_path_t namePath
Definition name.h:90
aml_prefix_path_t prefixPath
Definition name.h:89
aml_root_char_t rootChar
Definition name.h:88
ACPI object.
Definition object.h:425
A PrefixPath structure.
Definition name.h:52
uint16_t depth
Definition name.h:53
A RootChar structure.
Definition name.h:62
bool present
If the first character is a root character ('\'), if yes, the name string is absolute.
Definition name.h:63
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
const char * name
Definition token.h:243
aml_token_type_t type
Definition token.h:245
Token.
Definition token.h:253
const aml_token_props_t * props
Definition token.h:256
uint8_t length
Definition token.h:255
aml_token_num_t num
Definition token.h:254