|
| 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 | +} |
0 commit comments