Skip to content

Commit 529771e

Browse files
committed
liblua: add ACPI Data module
Description: Continuation of freebsd#1819. [WIP] Needs to be tested.
1 parent 4376693 commit 529771e

File tree

6 files changed

+162
-3
lines changed

6 files changed

+162
-3
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
35+
SRCS+= lacpi.c lacpi_object.c lacpi_utils.c lacpi_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/include/lacpi_object.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <lua.h>
44
#include <lauxlib.h>
55

6-
int luaopen_lacpi(lua_State *L);
76
void lacpi_object_interp_ref(void);
87
void lacpi_node_register_mt(lua_State *L);
98
int luaopen_lacpi_object(lua_State *L);

stand/liblua/acpi/lacpi.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <contrib/dev/acpica/include/acpi.h>
66
#include "lacpi.h"
77
#include "lacpi_object.h"
8+
#include "lacpi_data.h"
89

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

1820
/*
@@ -45,6 +47,8 @@ luaopen_lacpi(lua_State *L)
4547
{
4648
lua_newtable(L);
4749

50+
luaopen_lacpi_data(L);
51+
lua_setfield(L, -2, "data");
4852
luaopen_lacpi_object(L);
4953
lua_setfield(L, -2, "object");
5054

stand/liblua/acpi/lacpi_data.c

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
acpi_destructor_t
12+
get_object_destructor(UINT32 type)
13+
{
14+
switch (type) {
15+
case ACPI_TYPE_INTEGER:
16+
case ACPI_TYPE_LOCAL_REFERENCE:
17+
case ACPI_TYPE_PROCESSOR:
18+
case ACPI_TYPE_POWER:
19+
return free_fake;
20+
case ACPI_TYPE_STRING:
21+
return free_str;
22+
case ACPI_TYPE_BUFFER:
23+
return free_buff;
24+
case ACPI_TYPE_PACKAGE:
25+
return free_pkg;
26+
default:
27+
return NULL;
28+
}
29+
}
30+
31+
void
32+
acpi_object_handler(ACPI_HANDLE handle, void *data)
33+
{
34+
ACPI_OBJECT *obj = (ACPI_OBJECT *)data;
35+
36+
acpi_destructor_t dtor = get_object_destructor(obj->Type);
37+
if (dtor) {
38+
dtor(obj);
39+
} else {
40+
free(obj);
41+
obj == NULL;
42+
}
43+
}
44+
45+
/*
46+
* Lua stack expectations:
47+
* 1 = ACPI_HANDLE - the handle of the node to attach data onto
48+
* 2 = ACPI_OBJECT_TYPE - the type of the object
49+
*
50+
* The rest of the stack should match 1:1 to the ACPI_OBJECTs.
51+
* Example: if attaching ACPI_TYPE_PACKAGE, Count should be next
52+
* on the stack, and then *Elements.
53+
*/
54+
static int
55+
lAcpiAttachData(lua_State *L)
56+
{
57+
ACPI_STATUS status;
58+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
59+
UINT32 type = lua_int_to_uint32(L, 2, "Object type must be 32 bit");
60+
ACPI_OBJECT *obj;
61+
62+
if (handle == NULL) {
63+
return luaL_error(L, "lAcpiAttachData: Handle is NULL");
64+
}
65+
66+
obj = malloc(sizeof(ACPI_OBJECT));
67+
if (obj == NULL) {
68+
return luaL_error(L,
69+
"lAcpiAttachData: Failed to malloc ACPI_OBJECT");
70+
}
71+
72+
build_acpi_obj(L, obj, type);
73+
74+
if (ACPI_FAILURE(status = AcpiAttachData(handle, acpi_object_handler, (void *)obj))) {
75+
free_acpi_obj(obj);
76+
return luaL_error(L,
77+
"lAcpiAttachData: AcpiAttachData failed with status: 0x%x",
78+
status);
79+
}
80+
81+
lua_pushinteger(L, (lua_Integer)status);
82+
return 1;
83+
}
84+
85+
/*
86+
* Removes data from an ACPI node.
87+
*/
88+
static int
89+
lAcpiDetachData(lua_State *L)
90+
{
91+
ACPI_STATUS status;
92+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
93+
94+
if (handle == NULL) {
95+
return luaL_error(L, "lAcpiDetachData: NULL Argument(s)");
96+
}
97+
98+
if (ACPI_FAILURE(status = AcpiDetachData(handle, acpi_object_handler))) {
99+
return luaL_error(L,
100+
"lAcpiDetachData: AcpiDetachData failed with status: 0x%x",
101+
status);
102+
}
103+
104+
lua_pushinteger(L, (lua_Integer)status);
105+
return 1;
106+
}
107+
108+
/*
109+
* Retrieves data from an ACPI node.
110+
*/
111+
static int
112+
lAcpiGetData(lua_State *L)
113+
{
114+
ACPI_STATUS status;
115+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
116+
void *data = NULL;
117+
118+
if (handle == NULL) {
119+
return luaL_error(L, "lAcpiGetData: NULL Argument(s)");
120+
}
121+
122+
if (ACPI_FAILURE(status = AcpiGetData(handle, acpi_object_handler, &data))) {
123+
return luaL_error(L,
124+
"lAcpiGetData: AcpiGetData failed with status: 0x%x",
125+
status);
126+
}
127+
128+
push_acpi_obj(L, (ACPI_OBJECT *)data);
129+
return 1;
130+
}
131+
132+
static const
133+
luaL_Reg lacpi_data_funcs[] = {
134+
{ "attach", lAcpiAttachData },
135+
{ "detach", lAcpiDetachData },
136+
{ "get", lAcpiGetData },
137+
{ NULL, NULL }
138+
};
139+
140+
int
141+
luaopen_lacpi_data(lua_State *L)
142+
{
143+
luaL_newlib(L, lacpi_data_funcs);
144+
return 1;
145+
}
146+
147+
void
148+
lacpi_data_interp_ref(void)
149+
{
150+
}

stand/liblua/acpi/lacpi_utils.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ free_acpi_obj(ACPI_OBJECT *obj)
349349
case ACPI_TYPE_PACKAGE:
350350
free_pkg(obj);
351351
break;
352-
353352
default:
354353
assert(obj == NULL);
355354
break;

0 commit comments

Comments
 (0)