Skip to content

Commit 843f714

Browse files
committed
WIP: lAcpiAttach/Detach/GetData
Allows a user to post ACPI_OBJECTs onto namespace nodes. See actypes.h for all possible ACPI_OBJECTs. Currently being tested.
1 parent 0606a77 commit 843f714

File tree

5 files changed

+176
-2
lines changed

5 files changed

+176
-2
lines changed

stand/liblua/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SRCS+= lhash.c
3232
# ACPI
3333
.if ${MACHINE_CPUARCH} == "amd64" && ${DO32:U0} == 0
3434
.PATH: ${LIBLUASRC}/acpi
35-
SRCS+= lacpi.c lacpi_object.c lacpi_utils.c lacpi_walk.c
35+
SRCS+= lacpi.c lacpi_object.c lacpi_utils.c lacpi_walk.c lacp_data.c
3636
CFLAGS+= -I${SYSDIR}/contrib/dev/acpica/include \
3737
-I${EFISRC}/libacpi/acpi/include -I${EFISRC}/include \
3838
-I${EFISRC}/include/amd64 -I${LIBLUASRC}/acpi/include
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <lua.h>
4+
#include <lauxlib.h>
5+
6+
void lacpi_data_interp_ref(void);
7+
int luaopen_lacpi_data(lua_State *L);

stand/liblua/acpi/lacpi.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "lacpi.h"
55
#include "lacpi_object.h"
66
#include "lacpi_walk.h"
7+
#include "lacpi_data.h"
78

89
/*
910
* Reference set for all lacpi modules.
@@ -13,6 +14,7 @@ lacpi_interp_ref(void)
1314
{
1415
lacpi_object_interp_ref();
1516
lacpi_walk_interp_ref();
17+
lacpi_data_interp_ref();
1618
}
1719

1820
int
@@ -25,6 +27,9 @@ luaopen_lacpi(lua_State *L)
2527

2628
luaopen_lacpi_walk(L);
2729
lua_setfield(L, -2, "walk");
30+
31+
luaopen_lacpi_data(L);
32+
lua_setfield(L, -2, "data");
2833

2934
return 1;
3035
}

stand/liblua/acpi/lacpi_data.c

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#include <efi.h>
2+
#include <efilib.h>
3+
#include <lua.h>
4+
#include <lauxlib.h>
5+
#include <lualib.h>
6+
#include <contrib/dev/acpica/include/acpi.h>
7+
#include "lacpi.h"
8+
#include "lacpi_data.h"
9+
#include "lacpi_utils.h"
10+
11+
/*
12+
* Dynamic dispatcher for destructor based on ACPI_OBJECT_TYPE.
13+
* Do nothing in the case of integer-based ACPI_OBJECT_TYPES.
14+
*/
15+
acpi_destructor_t
16+
get_object_destructor(UINT32 type)
17+
{
18+
switch (type) {
19+
case ACPI_TYPE_INTEGER:
20+
case ACPI_TYPE_LOCAL_REFERENCE:
21+
case ACPI_TYPE_PROCESSOR:
22+
case ACPI_TYPE_POWER:
23+
return free_fake;
24+
case ACPI_TYPE_STRING:
25+
return free_str;
26+
case ACPI_TYPE_BUFFER:
27+
return free_buff;
28+
case ACPI_TYPE_PACKAGE:
29+
return free_pkg;
30+
default:
31+
return NULL;
32+
}
33+
}
34+
35+
/*
36+
* Attaching data requires providing a handler method.
37+
*/
38+
void
39+
acpi_object_handler(ACPI_HANDLE handle, void *data)
40+
{
41+
if (data != NULL) {
42+
ACPI_OBJECT *obj = (ACPI_OBJECT *)data;
43+
44+
acpi_destructor_t dtor = get_object_destructor(obj->Type);
45+
if (dtor) {
46+
dtor(obj);
47+
} else {
48+
free(obj);
49+
obj == NULL;
50+
}
51+
}
52+
}
53+
54+
/*
55+
* Create an ACPI_OBJECT to attach to a namespace node.
56+
* Uses ACPI_OBJECT as an interface for building the data
57+
* object so we can re-use it rather than re-invent the wheel.
58+
*
59+
* Lua stack expectations:
60+
* 1 = ACPI_HANDLE - the handle of the node to attach data onto
61+
* 2 = ACPI_OBJECT_TYPE - the type of the object
62+
*
63+
* The rest of the stack should match 1:1 to the ACPI_OBJECTs.
64+
* Example: if attaching ACPI_TYPE_PACKAGE, Count should be next
65+
* on the stack, and then *Elements.
66+
*/
67+
static int
68+
lAcpiAttachData(lua_State *L)
69+
{
70+
ACPI_STATUS status;
71+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
72+
UINT32 type = lua_int_to_uint32(L, 2, "Object type must be 32 bit");
73+
ACPI_OBJECT *obj;
74+
75+
if (handle == NULL) {
76+
return luaL_error(L, "lAcpiAttachData: Handle is NULL");
77+
}
78+
79+
obj = malloc(sizeof(ACPI_OBJECT));
80+
if (obj == NULL) {
81+
return luaL_error(L,
82+
"lAcpiAttachData: Failed to malloc ACPI_OBJECT");
83+
}
84+
85+
build_acpi_obj(L, obj, type);
86+
87+
if (ACPI_FAILURE(status = AcpiAttachData(handle, acpi_object_handler, (void *)obj))) {
88+
free_acpi_obj(obj);
89+
return luaL_error(L,
90+
"lAcpiAttachData: AcpiAttachData failed with status: 0x%x",
91+
status);
92+
}
93+
94+
lua_pushinteger(L, (lua_Integer)status);
95+
return 1;
96+
}
97+
98+
/*
99+
* Removes data from an ACPI node.
100+
*/
101+
static int
102+
lAcpiDetachData(lua_State *L)
103+
{
104+
ACPI_STATUS status;
105+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
106+
107+
if (handle == NULL) {
108+
return luaL_error(L, "lAcpiDetachData: NULL Argument(s)");
109+
}
110+
111+
if (ACPI_FAILURE(status = AcpiDetachData(handle, acpi_object_handler))) {
112+
return luaL_error(L,
113+
"lAcpiDetachData: AcpiDetachData failed with status: 0x%x",
114+
status);
115+
}
116+
117+
lua_pushinteger(L, (lua_Integer)status);
118+
return 1;
119+
}
120+
121+
/*
122+
* Retrieves data from an ACPI node.
123+
*/
124+
static int
125+
lAcpiGetData(lua_State *L)
126+
{
127+
ACPI_STATUS status;
128+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
129+
void *data = NULL;
130+
131+
if (handle == NULL) {
132+
return luaL_error(L, "lAcpiGetData: NULL Argument(s)");
133+
}
134+
135+
if (ACPI_FAILURE(status = AcpiGetData(handle, acpi_object_handler, &data))) {
136+
return luaL_error(L,
137+
"lAcpiGetData: AcpiGetData failed with status: 0x%x",
138+
status);
139+
}
140+
141+
push_acpi_obj(L, (ACPI_OBJECT *)data);
142+
return 1;
143+
}
144+
145+
static const
146+
luaL_Reg lacpi_data_funcs[] = {
147+
{ "attach", lAcpiAttachData },
148+
{ "detach", lAcpiDetachData },
149+
{ "get", lAcpiGetData },
150+
{ NULL, NULL }
151+
};
152+
153+
int
154+
luaopen_lacpi_data(lua_State *L)
155+
{
156+
luaL_newlib(L, lacpi_data_funcs);
157+
return 1;
158+
}
159+
160+
void
161+
lacpi_data_interp_ref(void)
162+
{
163+
}

stand/liblua/acpi/lacpi_utils.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ free_acpi_obj(ACPI_OBJECT *obj)
365365
case ACPI_TYPE_PACKAGE:
366366
free_pkg(obj);
367367
break;
368-
369368
default:
370369
assert(obj == NULL);
371370
break;

0 commit comments

Comments
 (0)