1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#include <acpi/acpi.h>
46#include "accommon.h"
47#include "acnamesp.h"
48#include "acinterp.h"
49#include "acpredef.h"
50#include "amlresrc.h"
51
52#define _COMPONENT ACPI_NAMESPACE
53ACPI_MODULE_NAME("nsconvert")
54
55
56
57
58
59
60
61
62
63
64
65
66
67acpi_status
68acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
69 union acpi_operand_object **return_object)
70{
71 union acpi_operand_object *new_object;
72 acpi_status status;
73 u64 value = 0;
74 u32 i;
75
76 switch (original_object->common.type) {
77 case ACPI_TYPE_STRING:
78
79
80
81 status = acpi_ut_strtoul64(original_object->string.pointer,
82 ACPI_ANY_BASE, &value);
83 if (ACPI_FAILURE(status)) {
84 return (status);
85 }
86 break;
87
88 case ACPI_TYPE_BUFFER:
89
90
91
92 if (original_object->buffer.length > 8) {
93 return (AE_AML_OPERAND_TYPE);
94 }
95
96
97
98 for (i = 0; i < original_object->buffer.length; i++) {
99 value |=
100 ((u64)original_object->buffer.
101 pointer[i] << (i * 8));
102 }
103 break;
104
105 default:
106
107 return (AE_AML_OPERAND_TYPE);
108 }
109
110 new_object = acpi_ut_create_integer_object(value);
111 if (!new_object) {
112 return (AE_NO_MEMORY);
113 }
114
115 *return_object = new_object;
116 return (AE_OK);
117}
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132acpi_status
133acpi_ns_convert_to_string(union acpi_operand_object *original_object,
134 union acpi_operand_object **return_object)
135{
136 union acpi_operand_object *new_object;
137 acpi_size length;
138 acpi_status status;
139
140 switch (original_object->common.type) {
141 case ACPI_TYPE_INTEGER:
142
143
144
145
146
147 if (original_object->integer.value == 0) {
148
149
150
151 new_object = acpi_ut_create_string_object(0);
152 if (!new_object) {
153 return (AE_NO_MEMORY);
154 }
155 } else {
156 status =
157 acpi_ex_convert_to_string(original_object,
158 &new_object,
159 ACPI_IMPLICIT_CONVERT_HEX);
160 if (ACPI_FAILURE(status)) {
161 return (status);
162 }
163 }
164 break;
165
166 case ACPI_TYPE_BUFFER:
167
168
169
170
171
172
173 length = 0;
174 while ((length < original_object->buffer.length) &&
175 (original_object->buffer.pointer[length])) {
176 length++;
177 }
178
179
180
181 new_object = acpi_ut_create_string_object(length);
182 if (!new_object) {
183 return (AE_NO_MEMORY);
184 }
185
186
187
188
189
190 ACPI_MEMCPY(new_object->string.pointer,
191 original_object->buffer.pointer, length);
192 break;
193
194 default:
195
196 return (AE_AML_OPERAND_TYPE);
197 }
198
199 *return_object = new_object;
200 return (AE_OK);
201}
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216acpi_status
217acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
218 union acpi_operand_object **return_object)
219{
220 union acpi_operand_object *new_object;
221 acpi_status status;
222 union acpi_operand_object **elements;
223 u32 *dword_buffer;
224 u32 count;
225 u32 i;
226
227 switch (original_object->common.type) {
228 case ACPI_TYPE_INTEGER:
229
230
231
232
233
234
235
236 status =
237 acpi_ex_convert_to_buffer(original_object, &new_object);
238 if (ACPI_FAILURE(status)) {
239 return (status);
240 }
241 break;
242
243 case ACPI_TYPE_STRING:
244
245
246
247 new_object =
248 acpi_ut_create_buffer_object(original_object->string.
249 length);
250 if (!new_object) {
251 return (AE_NO_MEMORY);
252 }
253
254 ACPI_MEMCPY(new_object->buffer.pointer,
255 original_object->string.pointer,
256 original_object->string.length);
257 break;
258
259 case ACPI_TYPE_PACKAGE:
260
261
262
263
264
265
266
267
268 elements = original_object->package.elements;
269 count = original_object->package.count;
270
271 for (i = 0; i < count; i++) {
272 if ((!*elements) ||
273 ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
274 return (AE_AML_OPERAND_TYPE);
275 }
276 elements++;
277 }
278
279
280
281 new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
282 if (!new_object) {
283 return (AE_NO_MEMORY);
284 }
285
286
287
288 elements = original_object->package.elements;
289 dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
290
291 for (i = 0; i < count; i++) {
292 *dword_buffer = (u32)(*elements)->integer.value;
293 dword_buffer++;
294 elements++;
295 }
296 break;
297
298 default:
299
300 return (AE_AML_OPERAND_TYPE);
301 }
302
303 *return_object = new_object;
304 return (AE_OK);
305}
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320acpi_status
321acpi_ns_convert_to_unicode(union acpi_operand_object *original_object,
322 union acpi_operand_object **return_object)
323{
324 union acpi_operand_object *new_object;
325 char *ascii_string;
326 u16 *unicode_buffer;
327 u32 unicode_length;
328 u32 i;
329
330 if (!original_object) {
331 return (AE_OK);
332 }
333
334
335
336 if (original_object->common.type == ACPI_TYPE_BUFFER) {
337 if (original_object->buffer.length < 2) {
338 return (AE_AML_OPERAND_VALUE);
339 }
340
341 *return_object = NULL;
342 return (AE_OK);
343 }
344
345
346
347
348
349 ascii_string = original_object->string.pointer;
350 unicode_length = (original_object->string.length * 2) + 2;
351
352
353
354 new_object = acpi_ut_create_buffer_object(unicode_length);
355 if (!new_object) {
356 return (AE_NO_MEMORY);
357 }
358
359 unicode_buffer = ACPI_CAST_PTR(u16, new_object->buffer.pointer);
360
361
362
363 for (i = 0; i < original_object->string.length; i++) {
364 unicode_buffer[i] = (u16)ascii_string[i];
365 }
366
367 *return_object = new_object;
368 return (AE_OK);
369}
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385acpi_status
386acpi_ns_convert_to_resource(union acpi_operand_object *original_object,
387 union acpi_operand_object **return_object)
388{
389 union acpi_operand_object *new_object;
390 u8 *buffer;
391
392
393
394
395
396
397
398
399
400
401 if (original_object) {
402 switch (original_object->common.type) {
403 case ACPI_TYPE_INTEGER:
404
405
406
407 if (original_object->integer.value) {
408 return (AE_AML_OPERAND_TYPE);
409 }
410 break;
411
412 case ACPI_TYPE_BUFFER:
413
414 if (original_object->buffer.length) {
415
416
417
418 *return_object = NULL;
419 return (AE_OK);
420 }
421 break;
422
423 case ACPI_TYPE_STRING:
424 default:
425
426 return (AE_AML_OPERAND_TYPE);
427 }
428 }
429
430
431
432 new_object = acpi_ut_create_buffer_object(2);
433 if (!new_object) {
434 return (AE_NO_MEMORY);
435 }
436
437 buffer = ACPI_CAST_PTR(u8, new_object->buffer.pointer);
438
439
440
441 buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);
442 buffer[1] = 0x00;
443
444 *return_object = new_object;
445 return (AE_OK);
446}
447