Skip to content

Commit e94a52a

Browse files
committed
liblua: Create lacpi
1 parent 5c16e1e commit e94a52a

File tree

13 files changed

+1214
-0
lines changed

13 files changed

+1214
-0
lines changed

stand/liblua/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ SRCS+= lfs.c
2929
.PATH: ${FLUALIB}/libhash
3030
SRCS+= lhash.c
3131

32+
# ACPI
33+
.if ${MACHINE_CPUARCH} == "amd64" && ${DO32:U0} == 0
34+
.PATH: ${LIBLUASRC}/acpi
35+
36+
SRCS+= lacpi.c lacpi_object.c lacpi_utils.c lacpi_walk.c lacpi_data.c
37+
38+
CFLAGS+= -I${SYSDIR}/contrib/dev/acpica/include \
39+
-I${EFISRC}/libacpi/acpi/include -I${EFISRC}/include \
40+
-I${EFISRC}/include/amd64 -I${LIBLUASRC}/acpi/include
41+
42+
DPADD+= ${LIBACPI}
43+
LDADD+= ${LIBACPI}
44+
.endif
45+
3246
WARNS?= 3
3347

3448
CFLAGS+= -DLUA_PATH=\"${LUAPATH}\" -DLUA_PATH_DEFAULT=\"${LUAPATH}/\?.lua\"

stand/liblua/acpi/include/lacpi.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <lua.h>
4+
#include <sys/linker_set.h>
5+
#include <contrib/dev/acpica/include/acpi.h>
6+
7+
typedef int (*lua_module_init_fn)(lua_State *L);
8+
extern void lacpi_object_interp_ref(void);
9+
extern void lacpi_data_interp_ref(void);
10+
11+
struct lua_acpi_module {
12+
const char *mod_name;
13+
lua_module_init_fn init;
14+
};
15+
16+
SET_DECLARE(lua_acpi_modules, struct lua_acpi_module);
17+
18+
#define LUA_ACPI_COMPILE_SET(name, initfn) \
19+
static struct lua_acpi_module lua_##name = \
20+
{ \
21+
.mod_name = #name, \
22+
.init = initfn \
23+
}; \
24+
DATA_SET(lua_acpi_modules, lua_##name)
25+
26+
struct lacpi_node {
27+
const char* pathname;
28+
ACPI_HANDLE handle;
29+
};
30+
31+
void lacpi_interp_ref(void);
32+
void lua_acpi_register_hook(void);
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);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include <lua.h>
4+
#include <lauxlib.h>
5+
6+
void lacpi_object_interp_ref(void);
7+
void lacpi_node_register_mt(lua_State *L);
8+
int luaopen_lacpi_object(lua_State *L);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#pragma once
2+
3+
#include <lauxlib.h>
4+
#include <lua.h>
5+
#include <lacpi_walk.h>
6+
#include <contrib/dev/acpica/include/acpi.h>
7+
8+
/***** UTILITY *****/
9+
10+
/* verifies lua passed over a handle on the stack */
11+
ACPI_HANDLE lacpi_check_handle(lua_State *L, int idx);
12+
13+
/* destructor dispatcher */
14+
typedef void (*acpi_destructor_t)(ACPI_OBJECT *);
15+
16+
/* safety check -- lua stores integers as 64bit */
17+
ACPI_STATUS lacpi_int_to_uint32(lua_State *L, int index, UINT32 *num);
18+
19+
/* build error code and push it onto stack */
20+
int lacpi_push_err(lua_State *L, const int push_nil, const char *errmsg,
21+
const ACPI_STATUS status);
22+
23+
/* extract error message relating to ACPI_STATUS */
24+
char *lacpi_extract_status(const ACPI_STATUS status);
25+
26+
/***** FACTORY *****/
27+
28+
/* create metatable gc */
29+
int lacpi_create_mt_gc(lua_State *L, const char *mt, lua_CFunction gc_func);
30+
31+
/*** ACPI_OBJECT ***/
32+
/* build ACPI_OBJECTs */
33+
ACPI_STATUS build_int(lua_State *L, ACPI_OBJECT *obj);
34+
ACPI_STATUS build_str(lua_State *L, ACPI_OBJECT *obj);
35+
ACPI_STATUS build_buff(lua_State *L, ACPI_OBJECT *obj);
36+
ACPI_STATUS build_pkg(lua_State *L, ACPI_OBJECT *obj);
37+
ACPI_STATUS build_acpi_obj(lua_State *L, ACPI_OBJECT *obj, UINT32 obj_type);
38+
ACPI_STATUS build_ref(lua_State *L, ACPI_OBJECT *obj);
39+
ACPI_STATUS build_proc(lua_State *L, ACPI_OBJECT *obj);
40+
ACPI_STATUS build_pow(lua_State *L, ACPI_OBJECT *obj);
41+
42+
/* push ACPI_OBJECTs onto lua stack */
43+
void push_int(lua_State *L, ACPI_OBJECT *obj);
44+
void push_str(lua_State *L, ACPI_OBJECT *obj);
45+
void push_buff(lua_State *L, ACPI_OBJECT *obj);
46+
void push_pkg(lua_State *L, ACPI_OBJECT *obj);
47+
void push_acpi_obj(lua_State *L, ACPI_OBJECT *obj);
48+
void push_ref(lua_State *L, ACPI_OBJECT *obj);
49+
void push_proc(lua_State *L, ACPI_OBJECT *obj);
50+
void push_pow(lua_State *L, ACPI_OBJECT *obj);
51+
52+
/* free ACPI_OBJECTs */
53+
void free_fake(ACPI_OBJECT *obj);
54+
void free_str(ACPI_OBJECT *obj);
55+
void free_buff(ACPI_OBJECT *obj);
56+
void free_pkg(ACPI_OBJECT *obj);
57+
void free_acpi_obj(ACPI_OBJECT *obj);
58+
void free_acpi_objs(ACPI_OBJECT *objs, UINT32 obj_count);
59+
60+
/*** ACPI Namespace Node ***/
61+
void push_path(struct context *curr_ctx, const char *path);
62+
void push_lvl(struct context *curr_ctx, UINT32 level);
63+
void push_hid(struct context *curr_ctx, const char *hid);
64+
void push_uid(struct context *curr_ctx, const char *uid);
65+
void push_node(struct context *curr_ctx, const char *path, UINT32 level,
66+
ACPI_HANDLE handle);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <lauxlib.h>
4+
5+
struct context {
6+
lua_State *L;
7+
int tbl;
8+
int idx;
9+
};
10+
11+
int luaopen_lacpi_walk(lua_State *L);
12+
void lacpi_walk_interp_ref(void);

stand/liblua/acpi/lacpi.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <init_acpi.h>
2+
#include <lutils.h>
3+
#include <lauxlib.h>
4+
#include "lacpi.h"
5+
#include "lacpi_object.h"
6+
#include "lacpi_walk.h"
7+
#include "lacpi_data.h"
8+
9+
/*
10+
* Reference set for all lacpi modules.
11+
*/
12+
void
13+
lacpi_interp_ref(void)
14+
{
15+
lacpi_object_interp_ref();
16+
lacpi_walk_interp_ref();
17+
lacpi_data_interp_ref();
18+
}
19+
20+
int
21+
luaopen_lacpi(lua_State *L)
22+
{
23+
lua_newtable(L);
24+
25+
luaopen_lacpi_object(L);
26+
lua_setfield(L, -2, "object");
27+
28+
luaopen_lacpi_walk(L);
29+
lua_setfield(L, -2, "walk");
30+
31+
luaopen_lacpi_data(L);
32+
lua_setfield(L, -2, "data");
33+
34+
return 1;
35+
}
36+
37+
/*
38+
* Unpacks all lacpi modules.
39+
*/
40+
static void
41+
lua_acpi_bindings(lua_State *L)
42+
{
43+
struct lua_acpi_module **mod;
44+
45+
SET_FOREACH(mod, lua_acpi_modules) {
46+
(*mod)->init(L);
47+
lua_setglobal(L, (*mod)->mod_name);
48+
}
49+
}
50+
51+
/*
52+
* Function hook for lacpi modules.
53+
*/
54+
void
55+
lua_acpi_register_hook(void)
56+
{
57+
if (acpi_is_initialized()) {
58+
lua_acpi_register = lua_acpi_bindings;
59+
}
60+
}
61+
62+
LUA_ACPI_COMPILE_SET(lacpi, luaopen_lacpi);

stand/liblua/acpi/lacpi_data.c

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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;
73+
ACPI_OBJECT *obj;
74+
75+
if (handle == NULL) {
76+
return luaL_error(L, "lAcpiAttachData: Handle is NULL");
77+
}
78+
79+
if (ACPI_FAILURE(status = lacpi_int_to_uint32(L, 2, &type))) {
80+
lua_pushnil(L);
81+
lua_pushstring(L, "ACPI_OBJECT_TYPE must be a UINT32");
82+
return 2;
83+
}
84+
85+
obj = malloc(sizeof(ACPI_OBJECT));
86+
if (obj == NULL) {
87+
return luaL_error(L,
88+
"lAcpiAttachData: Failed to malloc ACPI_OBJECT");
89+
}
90+
91+
build_acpi_obj(L, obj, type);
92+
93+
if (ACPI_FAILURE(status = AcpiAttachData(handle, acpi_object_handler, (void *)obj))) {
94+
free_acpi_obj(obj);
95+
return luaL_error(L,
96+
"lAcpiAttachData: AcpiAttachData failed with status: 0x%x",
97+
status);
98+
}
99+
100+
lua_pushinteger(L, (lua_Integer)status);
101+
return 1;
102+
}
103+
104+
/*
105+
* Removes data from an ACPI node.
106+
*/
107+
static int
108+
lAcpiDetachData(lua_State *L)
109+
{
110+
ACPI_STATUS status;
111+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
112+
113+
if (handle == NULL) {
114+
return luaL_error(L, "lAcpiDetachData: NULL Argument(s)");
115+
}
116+
117+
if (ACPI_FAILURE(status = AcpiDetachData(handle, acpi_object_handler))) {
118+
return luaL_error(L,
119+
"lAcpiDetachData: AcpiDetachData failed with status: 0x%x",
120+
status);
121+
}
122+
123+
lua_pushinteger(L, (lua_Integer)status);
124+
return 1;
125+
}
126+
127+
/*
128+
* Retrieves data from an ACPI node.
129+
*/
130+
static int
131+
lAcpiGetData(lua_State *L)
132+
{
133+
ACPI_STATUS status;
134+
ACPI_HANDLE handle = (ACPI_HANDLE) lua_touserdata(L, 1);
135+
void *data = NULL;
136+
137+
if (handle == NULL) {
138+
return luaL_error(L, "lAcpiGetData: NULL Argument(s)");
139+
}
140+
141+
if (ACPI_FAILURE(status = AcpiGetData(handle, acpi_object_handler, &data))) {
142+
return luaL_error(L,
143+
"lAcpiGetData: AcpiGetData failed with status: 0x%x",
144+
status);
145+
}
146+
147+
push_acpi_obj(L, (ACPI_OBJECT *)data);
148+
return 1;
149+
}
150+
151+
static const
152+
luaL_Reg lacpi_data_funcs[] = {
153+
{ "attach", lAcpiAttachData },
154+
{ "detach", lAcpiDetachData },
155+
{ "get", lAcpiGetData },
156+
{ NULL, NULL }
157+
};
158+
159+
int
160+
luaopen_lacpi_data(lua_State *L)
161+
{
162+
luaL_newlib(L, lacpi_data_funcs);
163+
return 1;
164+
}
165+
166+
void
167+
lacpi_data_interp_ref(void)
168+
{
169+
}

0 commit comments

Comments
 (0)