From c70f91446e978a8ea113b00de33d8d862d5d0d58 Mon Sep 17 00:00:00 2001 From: Marcel Ritter Date: Mon, 15 Dec 2008 15:41:18 +0100 Subject: COM32: lua - basic pci functions implemented --- com32/lua/src/Makefile | 4 +- com32/lua/src/linit.c | 1 + com32/lua/src/lualib.h | 3 + com32/lua/src/pci.c | 183 +++++++++++++++++++++++++++++++++++++++++++++++++ com32/lua/test/pci.lua | 34 +++++++++ 5 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 com32/lua/src/pci.c create mode 100644 com32/lua/test/pci.lua diff --git a/com32/lua/src/Makefile b/com32/lua/src/Makefile index 07ae9e6b..366bfc20 100644 --- a/com32/lua/src/Makefile +++ b/com32/lua/src/Makefile @@ -28,7 +28,7 @@ NASM = nasm NASMOPT = -O9999 RANLIB = ranlib CFLAGS = $(M32) -mregparm=3 -DREGPARM=3 -W -Wall -march=i386 -Os \ - -fomit-frame-pointer -D__COM32__ \ + -fomit-frame-pointer -D__COM32__ -std=gnu99 \ -nostdinc -iwithprefix include \ -I../../libutil/include -I../../include \ -Wp,-MT,$@,-MD,$(dir $@).$(notdir $@).d @@ -53,6 +53,7 @@ LIBLUA_OBJS += lauxlib.o lbaselib.o ldblib.o ltablib.o \ lstrlib.o loadlib.o linit.o LIBLUA_OBJS += liolib.o LIBLUA_OBJS += dmi.o ../../modules/dmi.o +LIBLUA_OBJS += pci.o CFLAGS += -DLUA_ANSI @@ -64,6 +65,7 @@ liblua.a: $(LIBLUA_OBJS) + .PRECIOUS: %.o %.o: %.S $(CC) $(SFLAGS) -c -o $@ $< diff --git a/com32/lua/src/linit.c b/com32/lua/src/linit.c index e457ee8d..bd908e40 100644 --- a/com32/lua/src/linit.c +++ b/com32/lua/src/linit.c @@ -24,6 +24,7 @@ static const luaL_Reg lualibs[] = { #if !defined LUA_NUMBER_INTEGRAL {LUA_MATHLIBNAME, luaopen_math}, #endif + {LUA_PCILIBNAME, luaopen_pci}, {LUA_DBLIBNAME, luaopen_debug}, {LUA_DMILIBNAME, luaopen_dmi}, {LUA_SYSLINUXLIBNAME, luaopen_syslinux}, diff --git a/com32/lua/src/lualib.h b/com32/lua/src/lualib.h index 8e220ef1..3f53b105 100644 --- a/com32/lua/src/lualib.h +++ b/com32/lua/src/lualib.h @@ -33,6 +33,9 @@ LUALIB_API int (luaopen_string) (lua_State *L); #define LUA_MATHLIBNAME "math" LUALIB_API int (luaopen_math) (lua_State *L); +#define LUA_PCILIBNAME "pci" +LUALIB_API int (luaopen_pci) (lua_State *L); + #define LUA_DBLIBNAME "debug" LUALIB_API int (luaopen_debug) (lua_State *L); diff --git a/com32/lua/src/pci.c b/com32/lua/src/pci.c new file mode 100644 index 00000000..b9c1605e --- /dev/null +++ b/com32/lua/src/pci.c @@ -0,0 +1,183 @@ +#include +#include +#include + +#define lpcilib_c /* Define the library */ + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include + + +static int pci_getinfo(lua_State *L) +{ + struct pci_domain *pci_domain; + struct pci_device *pci_device; + int pci_dev = 1; + + pci_domain = pci_scan(); + + lua_newtable(L); /* list of busses */ + + for_each_pci_func(pci_device, pci_domain) { + lua_pushnumber(L, pci_dev++); + + lua_newtable(L); /* device infos */ + + lua_pushstring(L, "bus"); + lua_pushnumber(L, __pci_bus); + lua_settable(L,-3); + + lua_pushstring(L, "slot"); + lua_pushnumber(L, __pci_slot); + lua_settable(L,-3); + + lua_pushstring(L, "func"); + lua_pushnumber(L, __pci_func); + lua_settable(L,-3); + + lua_pushstring(L, "vendor"); + lua_pushnumber(L, pci_device->vendor); + lua_settable(L,-3); + + lua_pushstring(L, "product"); + lua_pushnumber(L, pci_device->product); + lua_settable(L,-3); + + lua_pushstring(L, "sub_vendor"); + lua_pushnumber(L, pci_device->sub_vendor); + lua_settable(L,-3); + + lua_pushstring(L, "sub_product"); + lua_pushnumber(L, pci_device->sub_product); + lua_settable(L,-3); + + lua_settable(L,-3); /* end device infos */ + } + + return 1; +} + +/* searching the next char that is not a space */ +static char *skipspace(char *p) +{ + while (*p && *p <= ' ') + p++; + + return p; +} + +/* removing any \n found in a string */ +static void remove_eol(char *string) +{ + int j = strlen(string); + int i = 0; + for(i = 0; i < j; i++) if(string[i] == '\n') string[i] = 0; +} + + +/* Try to match any pci device to the appropriate kernel module */ +/* it uses the modules.pcimap from the boot device*/ +static int pci_getidlist(lua_State *L) +{ + const char *pciidfile; + char line[1024]; + char vendor[255]; + char vendor_id[5]; + char product[255]; + char productvendor[9]; + char productvendorsub[17]; + FILE *f; + + if (lua_gettop(L) == 1) { + pciidfile = luaL_checkstring(L, 1); + } else { + pciidfile = "pci.ids"; + } + + lua_newtable(L); /* list of vendors */ + + /* Opening the pci.ids from the boot device*/ + f=fopen(pciidfile,"r"); + if (!f) + return -1; + + /* for each line we found in the pci.ids*/ + while ( fgets(line, sizeof line, f) ) { + /* Skipping uncessary lines */ + if ((line[0] == '#') || (line[0] == ' ') || (line[0] == 'C') || + (line[0] == 10)) + continue; + + /* If the line doesn't start with a tab, it means that's a vendor id */ + if (line[0] != '\t') { + + /* the 4th first chars are the vendor_id */ + strlcpy(vendor_id,line,4); + + /* the vendor name is the next field*/ + vendor_id[4]=0; + strlcpy(vendor,skipspace(strstr(line," ")),255); + remove_eol(vendor); + + /* ffff is an invalid vendor id */ + if (strstr(vendor_id,"ffff")) break; + + lua_pushstring(L, vendor_id); + lua_pushstring(L, vendor); + lua_settable(L,-3); + + /* if we have a tab + a char, it means this is a product id */ + } else if ((line[0] == '\t') && (line[1] != '\t')) { + + /* the product name the second field */ + strlcpy(product,skipspace(strstr(line," ")),255); + remove_eol(product); + + /* the 4th first chars are the vendor_id */ + strlcpy(productvendor,vendor_id,4); + /* the product id is first field */ + strlcpy(productvendor+4,&line[1],4); + productvendor[8]=0; + + lua_pushstring(L, productvendor); + lua_pushstring(L, product); + lua_settable(L,-3); + + /* if we have two tabs, it means this is a sub product */ + } else if ((line[0] == '\t') && (line[1] == '\t')) { + + /* the product name is last field */ + strlcpy(product,skipspace(strstr(line," ")),255); + strlcpy(product,skipspace(strstr(product," ")),255); + remove_eol(product); + + strlcpy(productvendorsub, productvendor,8); + strlcpy(productvendorsub+8, &line[2],4); + strlcpy(productvendorsub+12, &line[7],4); + productvendorsub[16]=0; + + lua_pushstring(L, productvendorsub); + lua_pushstring(L, product); + lua_settable(L,-3); + + } + } + fclose(f); + return(1); +} + +static const luaL_reg pcilib[] = { + {"getinfo", pci_getinfo}, + {"getidlist", pci_getidlist}, + {NULL, NULL} +}; + +/* This defines a function that opens up your library. */ + +LUALIB_API int luaopen_pci (lua_State *L) { + luaL_openlib(L, LUA_PCILIBNAME, pcilib, 0); + return 1; +} + diff --git a/com32/lua/test/pci.lua b/com32/lua/test/pci.lua new file mode 100644 index 00000000..8d7f7d42 --- /dev/null +++ b/com32/lua/test/pci.lua @@ -0,0 +1,34 @@ +-- get nice output +printf = function(s,...) + return io.write(s:format(...)) + end + +-- get device info +pciinfo = pci.getinfo() + +-- get plain text device description +pciids = pci.getidlist("/pci.ids") + +-- list all pci busses +for dind,device in pairs(pciinfo) do + + -- search for device description + search = string.format("%04x%04x", device['vendor'], device['product']) + + printf(" %04x:%04x:%04x:%04x = ", device['vendor'], device['product'], + device['sub_vendor'], device['sub_product']) + + if ( pciids[search] ) then + printf("%s\n", pciids[search]) + else + printf("Unknown\n") + end +end + +-- print(pciids["8086"]) +-- print(pciids["10543009"]) +-- print(pciids["00700003"]) +-- print(pciids["0070e817"]) +-- print(pciids["1002437a1002437a"]) + + -- cgit v1.2.1