PatchworkOS
Loading...
Searching...
No Matches
copy.c
Go to the documentation of this file.
2
7#include <kernel/log/log.h>
8
9#include <errno.h>
10
12{
13 if (src == NULL || dest == NULL)
14 {
15 errno = EINVAL;
16 return ERR;
17 }
18
19 if (!(src->type & AML_DATA_REF_OBJECTS))
20 {
21 LOG_ERR("cannot copy object of type '%s'\n", aml_type_to_string(src->type));
22 errno = EINVAL;
23 return ERR;
24 }
25
26 switch (src->type)
27 {
28 case AML_INTEGER:
29 if (aml_integer_set(dest, src->integer.value) == ERR)
30 {
31 return ERR;
32 }
33 break;
34 case AML_STRING:
35 if (aml_string_set(dest, src->string.content) == ERR)
36 {
37 return ERR;
38 }
39 break;
40 case AML_BUFFER:
41 if (aml_buffer_set(dest, src->buffer.content, src->buffer.length, src->buffer.length) == ERR)
42 {
43 return ERR;
44 }
45 break;
46 case AML_PACKAGE:
47 if (aml_package_set(dest, src->package.length) == ERR)
48 {
49 return ERR;
50 }
51
52 for (uint64_t i = 0; i < src->package.length; i++)
53 {
55 {
56 aml_object_clear(dest);
57 return ERR;
58 }
59 }
60 break;
63 {
64 return ERR;
65 }
66 break;
67 default:
68 LOG_ERR("cannot copy object of type '%s'\n", aml_type_to_string(src->type));
69 errno = EINVAL;
70 return ERR;
71 }
72
73 // To make debugging easier we copy the name of the object to if the dest is not already named.
74 // The copied name would be overwritten if the dest is named later.
75 if (!(dest->flags & AML_OBJECT_NAMED) && (src->flags & AML_OBJECT_NAMED))
76 {
77 dest->name = src->name;
78 }
79
80 // Inherits the `AML_OBJECT_EXCEPTION_ON_USE` flag.
81 if (src->flags & AML_OBJECT_EXCEPTION_ON_USE)
82 {
83 dest->flags |= AML_OBJECT_EXCEPTION_ON_USE;
84 }
85 else
86 {
87 dest->flags &= ~AML_OBJECT_EXCEPTION_ON_USE;
88 }
89
90 return 0;
91}
92
94{
95 if (src == NULL || dest == NULL)
96 {
97 errno = EINVAL;
98 return ERR;
99 }
100
101 if (src->type == AML_UNINITIALIZED)
102 {
103 errno = EINVAL;
104 return ERR;
105 }
106
107 // TODO: Windows seems to allow this so we do the same but I need to do more reading on the whole Windows acpi
108 // weirdness thing.
109 if (src == dest)
110 {
111 return 0;
112 }
113
114 if (dest->type == AML_ARG)
115 {
116 if (dest->arg.value == NULL) // Is uninitialized
117 {
118 aml_object_t* newValue = aml_object_new();
119 if (newValue == NULL)
120 {
121 return ERR;
122 }
123
124 dest->arg.value = newValue; // Transfer ownership
125 return aml_copy_data_and_type(src, dest->arg.value);
126 }
127
128 if (dest->arg.value->type == AML_OBJECT_REFERENCE)
129 {
130 return aml_copy_object(state, src, dest->arg.value->objectReference.target);
131 }
132 else
133 {
134 return aml_copy_data_and_type(src, dest->arg.value);
135 }
136 }
137
138 if (dest->type == AML_LOCAL)
139 {
140 return aml_copy_data_and_type(src, dest->local.value);
141 }
142
143 if (dest->type == AML_FIELD_UNIT)
144 {
145 return aml_field_unit_store(state, &dest->fieldUnit, src);
146 }
147 if (dest->type == AML_BUFFER_FIELD)
148 {
149 return aml_buffer_field_store(&dest->bufferField, src);
150 }
151
152 if (dest->flags & AML_OBJECT_NAMED)
153 {
154 return aml_convert_result(state, src, dest);
155 }
156
157 if (dest->type == AML_UNINITIALIZED)
158 {
159 return aml_copy_data_and_type(src, dest);
160 }
161
162 LOG_ERR("illegal copy operation from type '%s' to type '%s'\n", aml_type_to_string(src->type),
163 aml_type_to_string(dest->type));
164 errno = ENOSYS;
165 return ERR;
166}
uint64_t aml_buffer_field_store(aml_buffer_field_obj_t *bufferField, aml_object_t *in)
Write a value to a BufferField.
uint64_t aml_convert_result(aml_state_t *state, aml_object_t *result, aml_object_t *target)
Performs a "Implicit Result Object Conversion" acording to the rules in section 19....
Definition convert.c:489
uint64_t aml_copy_data_and_type(aml_object_t *src, aml_object_t *dest)
Copies the data and type from the source object to the destination object, completly overwriting it.
Definition copy.c:11
uint64_t aml_copy_object(aml_state_t *state, aml_object_t *src, aml_object_t *dest)
Copies the data from the source object to the destination object.
Definition copy.c:93
uint64_t aml_field_unit_store(aml_state_t *state, aml_field_unit_obj_t *fieldUnit, aml_object_t *in)
Write a value to a FieldUnit. FieldUnits include Fields, IndexFields and BankFields.
Definition field_unit.c:631
uint64_t aml_object_reference_set(aml_object_t *object, aml_object_t *target)
Set a object as an ObjectReference to the given target object.
Definition object.c:831
uint64_t aml_package_set(aml_object_t *object, uint64_t length)
Set a object as a package with the given number of elements.
Definition object.c:869
uint64_t aml_integer_set(aml_object_t *object, aml_integer_t value)
Set a object as an integer with the given value and bit width.
Definition object.c:708
aml_object_t * aml_object_new(void)
Allocate a new ACPI object.
Definition object.c:54
uint64_t aml_string_set(aml_object_t *object, const char *str)
Set a object as a string with the given value.
Definition object.c:988
void aml_object_clear(aml_object_t *object)
Clear the data of a object, setting its type to AML_UNINITIALIZED.
Definition object.c:87
uint64_t aml_buffer_set(aml_object_t *object, const uint8_t *buffer, uint64_t bytesToCopy, uint64_t length)
Set a object as a buffer with the given content.
Definition object.c:520
@ AML_BUFFER_FIELD
Definition object.h:60
@ AML_PACKAGE
Definition object.h:79
@ AML_OBJECT_REFERENCE
Definition object.h:77
@ AML_STRING
Definition object.h:83
@ AML_ARG
Not in the spec, used internally to represent method arguments.
Definition object.h:88
@ AML_FIELD_UNIT
Definition object.h:64
@ AML_DATA_REF_OBJECTS
Definition object.h:103
@ AML_LOCAL
Not in the spec, used internally to represent method local variables.
Definition object.h:89
@ AML_INTEGER
Definition object.h:65
@ AML_UNINITIALIZED
Definition object.h:58
@ AML_BUFFER
Definition object.h:59
@ AML_OBJECT_EXCEPTION_ON_USE
Definition object.h:133
@ AML_OBJECT_NAMED
Appears in the namespace tree. Will be set in aml_object_add().
Definition object.h:126
const char * aml_type_to_string(aml_type_t type)
Convert an aml data type to a string.
Definition to_string.c:5
#define LOG_ERR(format,...)
Definition log.h:89
#define EINVAL
Invalid argument.
Definition errno.h:142
#define ENOSYS
Function not implemented.
Definition errno.h:222
#define errno
Error number variable.
Definition errno.h:27
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
aml_object_t * value
The object that was passed as the argument.
Definition object.h:407
uint64_t length
Definition object.h:214
uint8_t * content
Definition object.h:213
aml_integer_t value
Definition object.h:266
aml_object_t * value
The value of the local variable.
Definition object.h:417
aml_object_t * target
Definition object.h:315
ACPI object.
Definition object.h:425
aml_buffer_obj_t buffer
Definition object.h:431
aml_buffer_field_obj_t bufferField
Definition object.h:432
aml_local_obj_t local
Definition object.h:449
aml_object_reference_obj_t objectReference
Definition object.h:439
aml_integer_obj_t integer
Definition object.h:435
aml_field_unit_obj_t fieldUnit
Definition object.h:434
aml_package_obj_t package
Definition object.h:441
aml_string_obj_t string
Definition object.h:444
aml_arg_obj_t arg
Definition object.h:448
uint64_t length
Definition object.h:340
aml_object_t ** elements
Definition object.h:339
AML State.
Definition state.h:25
char * content
Definition object.h:370