Skip to content

Commit ffadf65

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

File tree

5 files changed

+154
-2
lines changed

5 files changed

+154
-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
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/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: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 (see actypes.h)
49+
* 3: Value || *Pointer || *Elements
50+
* 4. If applicable: Length
51+
*/
52+
static int
53+
lAcpiAttachData(lua_State *L)
54+
{
55+
ACPI_STATUS status;
56+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
57+
UINT32 type = lua_int_to_uint32(L, 2, "Object type must be 32 bit");
58+
ACPI_OBJECT *obj;
59+
60+
if (handle == NULL) {
61+
return luaL_error(L, "lAcpiAttachData: Handle is NULL");
62+
}
63+
64+
obj = malloc(sizeof(ACPI_OBJECT));
65+
if (obj == NULL) {
66+
return luaL_error(L,
67+
"lAcpiAttachData: Failed to malloc ACPI_OBJECT");
68+
}
69+
70+
build_acpi_obj(L, obj, type);
71+
72+
if (ACPI_FAILURE(status = AcpiAttachData(handle, acpi_object_handler, (void *)obj))) {
73+
free_acpi_obj(obj);
74+
return luaL_error(L,
75+
"lAcpiAttachData: AcpiAttachData failed with status: 0x%x",
76+
status);
77+
}
78+
79+
lua_pushinteger(L, (lua_Integer)status);
80+
return 1;
81+
}
82+
83+
static int
84+
lAcpiDetachData(lua_State *L)
85+
{
86+
ACPI_STATUS status;
87+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
88+
89+
if (handle == NULL) {
90+
return luaL_error(L, "lAcpiDetachData: NULL Argument(s)");
91+
}
92+
93+
if (ACPI_FAILURE(status = AcpiDetachData(handle, acpi_object_handler))) {
94+
return luaL_error(L,
95+
"lAcpiDetachData: AcpiDetachData failed with status: 0x%x",
96+
status);
97+
}
98+
99+
lua_pushinteger(L, (lua_Integer)status);
100+
return 1;
101+
}
102+
103+
static int
104+
lAcpiGetData(lua_State *L)
105+
{
106+
ACPI_STATUS status;
107+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
108+
void *data = NULL;
109+
110+
if (handle == NULL) {
111+
return luaL_error(L, "lAcpiGetData: NULL Argument(s)");
112+
}
113+
114+
if (ACPI_FAILURE(status = AcpiGetData(handle, acpi_object_handler, &data))) {
115+
return luaL_error(L,
116+
"lAcpiGetData: AcpiGetData failed with status: 0x%x",
117+
status);
118+
}
119+
120+
push_acpi_obj(L, (ACPI_OBJECT *)data);
121+
return 1;
122+
}
123+
124+
static const
125+
luaL_Reg lacpi_data_funcs[] = {
126+
{ "attach", lAcpiAttachData },
127+
{ "detach", lAcpiDetachData },
128+
{ "get", lAcpiGetData },
129+
{ NULL, NULL }
130+
};
131+
132+
int
133+
luaopen_lacpi_data(lua_State *L)
134+
{
135+
luaL_newlib(L, lacpi_data_funcs);
136+
return 1;
137+
}
138+
139+
void
140+
lacpi_data_interp_ref(void)
141+
{
142+
}

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)