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