From f2436f8aa565aec619b5d0d9f08677e74ffb741e Mon Sep 17 00:00:00 2001 From: sam <30084950+lunaboards-dev@users.noreply.github.com> Date: Sat, 29 May 2021 17:18:55 -0400 Subject: [PATCH 1/4] Drives and serial. --- src/c/blkdev.c | 155 ++++++++++++++++++++++++++++++++++++ src/c/init.c | 1 + src/c/modules.c | 2 + src/c/run.c | 2 + src/c/serial.c | 159 +++++++++++++++++++++++++++++++++++++ src/lua/core/component.lua | 2 +- src/lua/core/drive.lua | 59 ++++++++++++++ src/lua/core/init.lua | 9 ++- src/lua/core/serial.lua | 32 ++++++++ 9 files changed, 419 insertions(+), 2 deletions(-) create mode 100644 src/c/blkdev.c create mode 100644 src/c/serial.c create mode 100644 src/lua/core/drive.lua create mode 100644 src/lua/core/serial.lua diff --git a/src/c/blkdev.c b/src/c/blkdev.c new file mode 100644 index 0000000..1a50054 --- /dev/null +++ b/src/c/blkdev.c @@ -0,0 +1,155 @@ +#include "lupi.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct blkdev { + int fd; + int secsize; + int platters; + size_t size; + char * buffer; +}; + +int blk_open(lua_State * L) { + char * path = luaL_checkstring(L, 1); + char rpath[255]; + sprintf(rpath, "/dev/%s", path); + int fd = open(rpath, O_RDWR | O_DSYNC); + if (fd == -1) { + luaL_error(L, "Failed to open %s: %s", rpath, strerror(errno)); + } + struct blkdev * dev = lua_newuserdata(L, sizeof(struct blkdev)); + dev->fd = fd; + struct hd_driveid driveid; + if (ioctl(fd, HDIO_GET_IDENTITY, &driveid) < 0) { + //luaL_error("Failed to get identity for %s", rpath); + dev->platters = 3; + // Use another ioctl call to get sector size. + if (!ioctl(fd, BLKSSZGET, &dev->secsize)) { + // Fuck it, i guess it's 512 bytes. + dev->secsize = 512; + // Maybe a warning here? + } + dev->size = lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); + } else { + dev->size = driveid.lba_capacity_2*driveid.sector_bytes; + dev->platters = driveid.cur_heads/2; + dev->secsize = driveid.sector_bytes; + if (dev->size == 0) { + dev->size = lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); + } + if (dev->platters == 0) { + dev->platters = 3; + } + if (dev->secsize == 0) { + if (!ioctl(fd, BLKSSZGET, &dev->secsize)) { + // Fuck it, i guess it's 512 bytes. + dev->secsize = 512; + // Maybe a warning here too? + } + } + } + dev->buffer = malloc(dev->secsize); + return 1; +} + +int blk_readbyte(lua_State * L) { + struct blkdev * dev = lua_touserdata(L, 1); + off_t offset = luaL_checkinteger(L, 2); + lseek(dev->fd, offset-1, SEEK_SET); + uint8_t c = 0; + size_t ioc = 0; + if ((ioc = read(dev->fd, &c, 1)) != 1) { + luaL_error(L, "io error! %llu != %llu", ioc, 1); + } + lua_pushinteger(L, c); + return 1; +} + +int blk_writebyte(lua_State * L) { + struct blkdev * dev = lua_touserdata(L, 1); + off_t offset = luaL_checkinteger(L, 2); + long byte = luaL_checkinteger(L, 3); + if (byte > 255 || byte < 0) + luaL_error(L, "%lld is out of range (should be between 0 and 255)", byte); + uint8_t i = byte & 0xFF; + lseek(dev->fd, offset-1, SEEK_SET); + size_t ioc = 0; + if ((ioc = write(dev->fd, &i, 1)) != 1) { + luaL_error(L, "io error! %llu != %llu", ioc, 1); + } + return 0; +} + +int blk_readsector(lua_State * L) { + struct blkdev * dev = lua_touserdata(L, 1); + off_t offset = luaL_checkinteger(L, 2); + lseek(dev->fd, (offset-1)*dev->secsize, SEEK_SET); + size_t ioc = 0; + if ((ioc = read(dev->fd, dev->buffer, dev->secsize)) != dev->secsize) { + luaL_error(L, "io error! %llu != %llu", ioc, dev->secsize); + } + lua_pushlstring(L, dev->buffer, dev->secsize); + return 1; +} + +int blk_writesector(lua_State * L) { + struct blkdev * dev = lua_touserdata(L, 1); + off_t offset = luaL_checkinteger(L, 2); + size_t len = 0; + char * sec = luaL_checklstring(L, 3, &len); + lseek(dev->fd, (offset-1)*dev->secsize, SEEK_SET); + if (len > dev->secsize) { + len = dev->secsize; + } + size_t ioc = 0; + if ((ioc = write(dev->fd, sec, len)) != len) { + luaL_error(L, "io error! %llu != %llu", ioc, dev->secsize); + } + return 0; +} + +int blk_getsecsize(lua_State * L) { + struct blkdev * dev = lua_touserdata(L, 1); + lua_pushinteger(L, dev->secsize); + return 1; +} + +int blk_getsize(lua_State * L) { + struct blkdev * dev = lua_touserdata(L, 1); + lua_pushinteger(L, dev->size); + return 1; +} + +int blk_getplatcount(lua_State * L) { + struct blkdev * dev = lua_touserdata(L, 1); + lua_pushinteger(L, dev->platters); + return 1; +} + +void blk_start(lua_State * L) { + struct luaL_Reg blklib[] = { + {"open", blk_open}, + {"readbyte", blk_readbyte}, + {"writebyte", blk_writebyte}, + {"readsector", blk_readsector}, + {"writesector", blk_writesector}, + {"secsize", blk_getsecsize}, + {"size", blk_getsize}, + {"platcount", blk_getplatcount}, + {NULL, NULL} + }; + luaL_openlib(L, "blk", blklib, 0); +} \ No newline at end of file diff --git a/src/c/init.c b/src/c/init.c index facacd0..6d8bff8 100644 --- a/src/c/init.c +++ b/src/c/init.c @@ -12,6 +12,7 @@ void lupi_init() { mount(NULL, "/sys", "sysfs", 0, NULL); mount(NULL, "/proc", "procfs", 0, NULL); mount(NULL, "/tmp", "tmpfs", 0, NULL); + mount(NULL, "/dev", "devtmpfs", 0, NULL); } struct statvfs fsstat; diff --git a/src/c/modules.c b/src/c/modules.c index 7ca75ea..841fa1f 100644 --- a/src/c/modules.c +++ b/src/c/modules.c @@ -14,12 +14,14 @@ void setup_modules(lua_State *L) { pushstuple(L, "component", lua_component); pushstuple(L, "computer", lua_computer); pushstuple(L, "debug", lua_debug); + pushstuple(L, "drive", lua_drive); pushstuple(L, "eeprom", lua_eeprom); pushstuple(L, "filesystem", lua_filesystem); pushstuple(L, "gpio", lua_gpio); pushstuple(L, "gpudetect", lua_gpudetect); pushstuple(L, "internet", lua_internet); pushstuple(L, "sandbox", lua_sandbox); + pushstuple(L, "serial", lua_serial); pushstuple(L, "textgpu", lua_textgpu); pushstuple(L, "fbgpu", lua_fbgpu); pushstuple(L, "winapigpu", lua_winapigpu); diff --git a/src/c/run.c b/src/c/run.c index 4b91ee2..711099b 100644 --- a/src/c/run.c +++ b/src/c/run.c @@ -31,6 +31,8 @@ void run_init(int argc, char **argv) { #endif fb_start (L); termutils_start (L); + serial_start (L); + blk_start (L); event_prepare(); int status = luaL_loadbuffer(L, lua_init, strlen(lua_init), "=INIT"); diff --git a/src/c/serial.c b/src/c/serial.c new file mode 100644 index 0000000..4ab5a1f --- /dev/null +++ b/src/c/serial.c @@ -0,0 +1,159 @@ +#include "lupi.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int s_listports(lua_State * L) { + DIR * d = opendir("/sys/class/tty"); + struct dirent * ent = NULL; + struct stat statbuf; + char path[255]; + char linkpath[255]; + lua_newtable(L); + size_t index = 1; + while ((ent = readdir(d)) != NULL) { + sprintf(path, "/sys/class/tty/%s/device", ent->d_name); + if (stat(path, &statbuf) == 0) { + readlink(path, linkpath, 255); + if (strstr(linkpath, "serial8250") == NULL) { + // We have a serial port + lua_pushinteger(L, index); + lua_pushstring(L, ent->d_name); + lua_settable(L, -3); + } + } + } + return 1; +} + +struct baudrate { + uint32_t baud; + tcflag_t flags; +}; + +// taken from here: https://man7.org/linux/man-pages/man3/termios.3.html +struct baudrate baudrates[] = { + {0, B0}, + {50, B50}, + {75, B75}, + {110, B110}, + {134, B134}, + {150, B150}, + {200, B200}, + {300, B300}, + {600, B600}, + {1200, B1200}, + {1800, B1800}, + {2400, B2400}, + {4800, B4800}, + {9600, B9600}, + {19200, B19200}, + {38400, B38400}, + {57600, B57600}, + {115200, B115200}, + {230400, B230400} +}; + +struct baudrate * getbaud(uint32_t baud) { + for (size_t i=0; i baud) { + return &baudrates[i-1]; + } + } + return getbaud(230400); +} + +struct sport { + uint32_t baudrate; + int fd; + struct termios options; +}; + +int s_openport(lua_State * L) { + char * path = luaL_checkstring(L, 1); + struct sport * port = lua_newuserdata(L, sizeof(struct sport)); + port->baudrate = 9600; + char rpath[255]; + sprintf(rpath, "/dev/%s", path); + port->fd = open(rpath, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK); + if (port->fd == -1) + luaL_error(L, "unable to open port %s: ", path, strerror(errno)); + tcgetattr(port->fd, &port->options); + cfsetispeed(&port->options, B9600); + cfsetospeed(&port->options, B9600); + tcsetattr(port->fd, TCSANOW, &port->options); + return 1; +} + +int s_setbaudrate(lua_State * L) { + struct sport * port = lua_touserdata(L, 1); + long raw_br = luaL_checkinteger(L, 2); + struct baudrate * br = getbaud(raw_br & 0xFFFFFFFF); + //port->options.c_cflag &= ~(CBAUD); + //port->options.c_cflag |= br->flags; + cfsetispeed(&port->options, br->flags); + cfsetospeed(&port->options, br->flags); + port->baudrate = br->baud; + lua_pushinteger(L, br->baud); + port->options.c_cflag &= ~PARENB; + port->options.c_cflag &= ~CSTOPB; + port->options.c_cflag &= ~CSIZE; + port->options.c_cflag |= CS8; + tcsetattr(port->fd, TCSANOW, &port->options); + return 1; +} + +int s_getbaudrate(lua_State * L) { + struct sport * port = lua_touserdata(L, 1); + lua_pushinteger(L, port->baudrate); + return 1; +} + +int s_write(lua_State * L) { + struct sport * port = lua_touserdata(L, 1); + size_t len = 0; + char * data = luaL_checklstring(L, 2, &len); + size_t ioc = 0; + if ((ioc = write(port->fd, data, len)) != len) { + luaL_error(L, "io error! %llu != %llu", ioc, len); + } + return 0; +} + +int s_read(lua_State * L) { + struct sport * port = lua_touserdata(L, 1); + char buffer[2048]; // Max packet size + long size = luaL_checkinteger(L, 2); + size_t ioc = read(port->fd, buffer, size); + if (ioc != size && ioc != 0) { + luaL_error(L, "io error! %llu != %llu or 0", ioc, size); + } else if (ioc != 0) { + lua_pushlstring(L, buffer, size); + return 1; + } + return 0; +} + +void serial_start(lua_State * L) { + struct luaL_Reg seriallib[] = { + {"open", s_openport}, + {"list", s_listports}, + {"write", s_write}, + {"read", s_read}, + {"setbaud", s_setbaudrate}, + {"getbaud", s_getbaudrate}, + {NULL, NULL} + }; + luaL_openlib(L, "serial", seriallib, 0); +} \ No newline at end of file diff --git a/src/lua/core/component.lua b/src/lua/core/component.lua index 655f7be..b9eef60 100644 --- a/src/lua/core/component.lua +++ b/src/lua/core/component.lua @@ -109,7 +109,7 @@ function api.invoke(address, method, ...) local msg = tostring((native.uptime() - start) / 1000) .. " [+" .. native.uptime() - last .. "] " .. caller.short_src .. ":".. caller.currentline .. " > c.invoke(" .. address .. "): " .. components[address].type .. "." .. method .. "(" .. table.concat(t, ", ") .. ")" - native.log(msg) + --native.log(msg) end return components[address].rawproxy[method](...) end diff --git a/src/lua/core/drive.lua b/src/lua/core/drive.lua new file mode 100644 index 0000000..f2e6668 --- /dev/null +++ b/src/lua/core/drive.lua @@ -0,0 +1,59 @@ +local drive = {} + +function drive.register() + local list = native.fs_list("/sys/block") + for _, ent in ipairs(list) do + -- Match hard drives. floppy drives, an rewindable tape drives + if (ent:match("sd%a%d*") or ent:match("hd%a%d*") or ent:match("fd%d+") or ent:match("n?st%d+[lma]?") or ent:match("n?ht%d+[lma]?")) then + --if true then + lprint("Attempting to open: "..ent) + local ok, disk = pcall(blk.open, ent) + if ok then + local component = {} + + function component.readByte(offset) + return blk.readbyte(disk, offset) + end + + function component.writeByte(offset, value) + return blk.writebyte(disk, offset, value) + end + + function component.getLabel() + return ent + end + + function component.setLabel(str) + -- stub + return ent + end + + function component.readSector(sec) + return blk.readsector(disk, sec) + end + + function component.writeSector(sec, val) + return blk.writesector(disk, sec, val) + end + + function component.getPlatterCount() + return blk.platcount(disk) + end + + function component.getSectorSize() + return blk.secsize(disk) + end + + function component.getCapacity() + return blk.size(disk) + end + + modules.component.api.register("Block:"..ent, "drive", component) + else + lprint("Can't open blkdev: "..ent..": "..disk) + end + end + end +end + +return drive \ No newline at end of file diff --git a/src/lua/core/init.lua b/src/lua/core/init.lua index 31f0f0a..faba5ba 100644 --- a/src/lua/core/init.lua +++ b/src/lua/core/init.lua @@ -44,6 +44,10 @@ lprint = function (...) end lprint("LuPI L1 INIT") +local ports = serial.list() +for i=1, #ports do + lprint(ports[i]) +end modules = {} deadhooks = {} @@ -90,6 +94,8 @@ function main() loadModule("gpudetect") loadModule("filesystem") loadModule("internet") + loadModule("serial") + loadModule("drive") --Userspace loadModule("sandbox") @@ -99,7 +105,8 @@ function main() modules.component.prepare() modules.computer.prepare() _G.pushEvent = modules.computer.api.pushSignal - + modules.serial.register() + modules.drive.register() modules.eeprom.register() modules.gpio.register() modules.internet.start() diff --git a/src/lua/core/serial.lua b/src/lua/core/serial.lua new file mode 100644 index 0000000..d26552c --- /dev/null +++ b/src/lua/core/serial.lua @@ -0,0 +1,32 @@ +local com = {} + +function com.register() + local ports = serial.list() + for i=1, #ports do + lprint("Serial port: "..ports[i]) + local component = {} + local ok, dev = pcall(serial.open, ports[i]) + if not dev then lprint("Can't open port: "..ports[i]..": "..dev) goto continue end + --lprint(dev) + function component.setBaudRate(baud) + return serial.setbaud(dev, baud) + end + + function component.getBaudRate() + return serial.getbaud(dev) + end + + function component.write(s) + return serial.write(dev, s) + end + + function component.read(amt) + return serial.read(dev, amt) + end + + modules.component.api.register("Serial:"..ports[i], "serial", component) + ::continue:: + end +end + +return com \ No newline at end of file -- 2.30.2 From ecbee6c83a7f86f4113839d97f65fcf439a0c9a8 Mon Sep 17 00:00:00 2001 From: sam <30084950+lunaboards-dev@users.noreply.github.com> Date: Sat, 29 May 2021 19:19:29 -0400 Subject: [PATCH 2/4] Static UUIDs. --- include/luares.h | 3 ++ include/lupi.h | 4 +++ src/c/event/event.c | 4 +-- src/c/lnative.c | 15 +++++++++ src/c/modules.c | 1 + src/lua/core/drive.lua | 2 +- src/lua/core/eeprom.lua | 2 +- src/lua/core/fbgpu.lua | 6 ++-- src/lua/core/filesystem.lua | 2 +- src/lua/core/gpio.lua | 2 +- src/lua/core/init.lua | 8 +++-- src/lua/core/internet.lua | 2 +- src/lua/core/serial.lua | 2 +- src/lua/core/textgpu.lua | 6 ++-- src/lua/core/uuidmgr.lua | 63 +++++++++++++++++++++++++++++++++++++ src/lua/core/winapigpu.lua | 6 ++-- 16 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 src/lua/core/uuidmgr.lua diff --git a/include/luares.h b/include/luares.h index 5f3d109..018bcec 100644 --- a/include/luares.h +++ b/include/luares.h @@ -4,6 +4,7 @@ extern char lua_boot[]; extern char lua_component[]; extern char lua_computer[]; extern char lua_debug[]; +extern char lua_drive[]; extern char lua_eeprom[]; extern char lua_fbgpu[]; extern char lua_filesystem[]; @@ -12,9 +13,11 @@ extern char lua_gpudetect[]; extern char lua_init[]; extern char lua_internet[]; extern char lua_sandbox[]; +extern char lua_serial[]; extern char lua_textgpu[]; extern char lua_util_buffer[]; extern char lua_util_color[]; extern char lua_util_random[]; +extern char lua_uuidmgr[]; extern char lua_winapigpu[]; #endif diff --git a/include/lupi.h b/include/lupi.h index 40377d4..638522a 100644 --- a/include/lupi.h +++ b/include/lupi.h @@ -22,9 +22,13 @@ typedef unsigned short ushort; lua_State* getL(); +char * get_kbid(); // ugly hack + void run_init(int argc, char **argv); void lupi_init(); void luanative_start(lua_State *L); +void serial_start(lua_State *L); +void blk_start(lua_State *L); void fb_start(lua_State *L); void setup_modules(lua_State *L); void termutils_start(lua_State *L); diff --git a/src/c/event/event.c b/src/c/event/event.c index aaab3f0..a7ba3ee 100644 --- a/src/c/event/event.c +++ b/src/c/event/event.c @@ -35,7 +35,7 @@ static void handleStdin(evutil_socket_t fd, short what, void *eventc) { lua_getglobal(L, "pushEvent"); lua_pushstring(L, "key_down"); - lua_pushstring(L, "TODO:SetThisUuid");/* Also in textgpu.lua */ + lua_pushstring(L, get_kbid());/* Also in textgpu.lua */ lua_pushnumber(L, buf); lua_pushnumber(L, -1); lua_pushstring(L, "root"); @@ -43,7 +43,7 @@ static void handleStdin(evutil_socket_t fd, short what, void *eventc) { lua_getglobal(L, "pushEvent"); lua_pushstring(L, "key_up"); - lua_pushstring(L, "TODO:SetThisUuid"); + lua_pushstring(L, get_kbid()); lua_pushnumber(L, buf); lua_pushnumber(L, -1); lua_pushstring(L, "root"); diff --git a/src/c/lnative.c b/src/c/lnative.c index 875815b..0e2f699 100644 --- a/src/c/lnative.c +++ b/src/c/lnative.c @@ -476,6 +476,20 @@ static int l_debug (lua_State *L) { } #endif +char * kbid = "TODO:SetThisUuid"; + +static int l_setkbid(lua_State *L) { + size_t len = 0; + char * id = luaL_checklstring(L, 1, &len); + kbid = malloc(len+1); + memcpy(kbid, id, len); + return 0; +} + +char * get_kbid() { + return kbid; +} + void luanative_start(lua_State *L) { struct timeval tp; @@ -511,6 +525,7 @@ void luanative_start(lua_State *L) { {"pull", l_pull}, {"platform", l_platform}, {"isinit", l_isinit}, + {"setkbid", l_setkbid}, #ifdef DEBUG {"debug", l_debug}, #endif diff --git a/src/c/modules.c b/src/c/modules.c index 841fa1f..22035c5 100644 --- a/src/c/modules.c +++ b/src/c/modules.c @@ -27,6 +27,7 @@ void setup_modules(lua_State *L) { pushstuple(L, "winapigpu", lua_winapigpu); pushstuple(L, "color", lua_util_color); pushstuple(L, "random", lua_util_random); + pushstuple(L, "uuidmgr", lua_uuidmgr); pushstuple(L, "buffer", lua_util_buffer); pushstuple(L, "eepromDefault", res_eepromDefault); diff --git a/src/lua/core/drive.lua b/src/lua/core/drive.lua index f2e6668..f3ec1bb 100644 --- a/src/lua/core/drive.lua +++ b/src/lua/core/drive.lua @@ -48,7 +48,7 @@ function drive.register() return blk.size(disk) end - modules.component.api.register("Block:"..ent, "drive", component) + modules.component.api.register(modules.uuidmgr.lookup("drive", ent), "drive", component) else lprint("Can't open blkdev: "..ent..": "..disk) end diff --git a/src/lua/core/eeprom.lua b/src/lua/core/eeprom.lua index ec01b81..53c5df1 100644 --- a/src/lua/core/eeprom.lua +++ b/src/lua/core/eeprom.lua @@ -62,7 +62,7 @@ function eeprom.register() function component.makeReadonly() return false, "Method stub" end - modules.component.api.register(nil, "eeprom", component) + modules.component.api.register(modules.uuidmgr.lookup("eeprom", "lmao"), "eeprom", component) end return eeprom diff --git a/src/lua/core/fbgpu.lua b/src/lua/core/fbgpu.lua index 0cd3268..bedeb61 100644 --- a/src/lua/core/fbgpu.lua +++ b/src/lua/core/fbgpu.lua @@ -165,9 +165,9 @@ function fbgpu.start() termutils.init() write("\x1b[?25l") --Disable cursor - modules.component.api.register(nil, "gpu", gpu) - screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c - modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) + modules.component.api.register(modules.uuidmgr.lookup("gpu", "fbgpu"), "gpu", gpu) + screenAddr = modules.component.api.register(modules.uuidmgr.lookup("screen", "lmao"), "screen", {getKeyboards = function() return {modules.uuidmgr.lookup("keyboard", "lmao")} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c + modules.component.api.register(modules.uuidmgr.lookup("keyboard", "lmao"), "keyboard", {}) deadhooks[#deadhooks + 1] = function() write("\x1b[?25h") --Enable cursor on quit diff --git a/src/lua/core/filesystem.lua b/src/lua/core/filesystem.lua index 2214488..70d6924 100644 --- a/src/lua/core/filesystem.lua +++ b/src/lua/core/filesystem.lua @@ -156,7 +156,7 @@ function filesystem.register(basePath, uuid) checkArg(1, value, "number") return value --TODO: Implement, use real labels end - return modules.component.api.register(uuid, "filesystem", fs) + return modules.component.api.register(modules.uuidmgr.lookup("filesystem", basePath), "filesystem", fs) end return filesystem diff --git a/src/lua/core/gpio.lua b/src/lua/core/gpio.lua index 31f72b1..5373c85 100644 --- a/src/lua/core/gpio.lua +++ b/src/lua/core/gpio.lua @@ -68,7 +68,7 @@ gpio.register = function () end return _read("/sys/class/gpio/gpio" .. pin .. "/value") end - return modules.component.api.register(uuid, "gpio", component) + return modules.component.api.register(modules.uuidmgr.lookup("gpio", "lmao"), "gpio", component) end return gpio diff --git a/src/lua/core/init.lua b/src/lua/core/init.lua index faba5ba..8e06cbb 100644 --- a/src/lua/core/init.lua +++ b/src/lua/core/init.lua @@ -78,10 +78,14 @@ function main() loadModule("debug") end loadModule("random") + loadModule("uuidmgr") loadModule("color") loadModule("buffer") - modules.address = modules.random.uuid() --TODO: PREALPHA: Make constant + --modules.address = modules.random.uuid() --TODO: PREALPHA: Make constant + modules.address = modules.uuidmgr.lookup("modules", "lmao") --Made constant + native.setkbid(modules.uuidmgr.lookup("keyboard", "lmao")) + --Core loadModule("component") @@ -127,7 +131,7 @@ function main() if native.debug then modules.debug.hook() end - + modules.uuidmgr.store() modules.boot.boot() end diff --git a/src/lua/core/internet.lua b/src/lua/core/internet.lua index 64fe311..3cbc1d8 100644 --- a/src/lua/core/internet.lua +++ b/src/lua/core/internet.lua @@ -157,7 +157,7 @@ function internet.start() } end - modules.component.api.register(nil, "internet", component) + modules.component.api.register(modules.uuidmgr.lookup("internet", "lmao"), "internet", component) end return internet diff --git a/src/lua/core/serial.lua b/src/lua/core/serial.lua index d26552c..7688495 100644 --- a/src/lua/core/serial.lua +++ b/src/lua/core/serial.lua @@ -24,7 +24,7 @@ function com.register() return serial.read(dev, amt) end - modules.component.api.register("Serial:"..ports[i], "serial", component) + modules.component.api.register(modules.uuidmgr.lookup("serial", ports[i]), "serial", component) ::continue:: end end diff --git a/src/lua/core/textgpu.lua b/src/lua/core/textgpu.lua index cac1045..814a2ea 100644 --- a/src/lua/core/textgpu.lua +++ b/src/lua/core/textgpu.lua @@ -255,9 +255,9 @@ function textgpu.start() gpu.setForeground(0xFFFFFF) gpu.setBackground(0x000000) - local gpuaddr = modules.component.api.register(nil, "gpu", gpu) - screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c - modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) + local gpuaddr = modules.component.api.register(modules.uuidmgr.lookup("gpu", "text"), "gpu", gpu) + screenAddr = modules.component.api.register(modules.uuidmgr.lookup("screen", "lmao"), "screen", {getKeyboards = function() return {modules.uuidmgr.lookup("keyboard", "lmao")} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c + modules.component.api.register(modules.uuidmgr.lookup("keyboard", "lmao"), "keyboard", {}) deadhooks[#deadhooks + 1] = function() write("\x1b[?25h\x1b[" .. ((h-1)|0) .. ";1H") --Enable cursor on quit diff --git a/src/lua/core/uuidmgr.lua b/src/lua/core/uuidmgr.lua new file mode 100644 index 0000000..76611c8 --- /dev/null +++ b/src/lua/core/uuidmgr.lua @@ -0,0 +1,63 @@ +local uuidmgr = {} + +local uuidstorage = {} + +local storeheader = "c32BH" + +do + local store = io.open("uuidstore.dat", "r") + if store then + pcall(function() + local count = string.unpack("H", store:read(2)) + for i=1, count do + local uuid, clen, ilen = storeheader:unpack(store:read(storeheader:packsize())) + local cname, id = store:read(clen), store:read(ilen) + uuidstorage[cname] = uuidstorage[cname] or {} + uuidstorage[cname][id] = uuid + end + store:close() + end) + end +end + +function uuidmgr.lookup(component, identifier) + uuidstorage[component] = uuidstorage[component] or {} + local uuid = uuidstorage[component][identifier] + if not uuid then + uuid = modules.random.uuid() + if native.debug then + lprint(string.format("DEBUG: Registed component of type %s with identifier %s as uuid %s", component, identifier, uuid)) + end + uuidstorage[component][identifier] = uuid + uuidmgr.store() + else + if native.debug then + lprint(string.format("DEBUG: Looked up component of type %s with identifier %s as uuid %s", component, identifier, uuid)) + end + end + return uuid +end + +function uuidmgr.store() + local store = io.open("uuidstore.dat", "w") + local saved_ids = {} + for component, ids in pairs(uuidstorage) do + for id, uuid in pairs(ids) do + table.insert(saved_ids, {uuid=uuid, component=component, id=id}) + end + end + if native.debug then + lprint(string.format("DEBUG: Saving %d static UUIDs", #saved_ids)) + end + store:write(string.pack("H", #saved_ids)) + for i=1, #saved_ids do + local uuid, component, id = saved_ids[i].uuid, saved_ids[i].component, saved_ids[i].id + store:write(storeheader:pack(uuid, #component, #id),component,id) + if native.debug then + lprint(string.format("DEBUG: Saving %s:%s as %s", component, id, uuid)) + end + end + store:close() +end + +return uuidmgr \ No newline at end of file diff --git a/src/lua/core/winapigpu.lua b/src/lua/core/winapigpu.lua index c709783..3f1cbb9 100644 --- a/src/lua/core/winapigpu.lua +++ b/src/lua/core/winapigpu.lua @@ -158,9 +158,9 @@ function wingpu.start() lprint("Couldn't open window: " .. tostring(reason)) end - modules.component.api.register(nil, "gpu", gpu) - screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c - modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) + modules.component.api.register(modules.uuidmgr.lookup("gpu", "wingpu"), "gpu", gpu) + screenAddr = modules.component.api.register(modules.uuidmgr.lookup("screen", "lmao"), "screen", {getKeyboards = function() return {modules.uuidmgr.lookup("keyboard", "lmao")} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c + modules.component.api.register(modules.uuidmgr.lookup("keyboard", "lmao"), "keyboard", {}) return s end -- 2.30.2 From e6f4d404853f017adc716d261f1463276444a3e8 Mon Sep 17 00:00:00 2001 From: sam <30084950+lunaboards-dev@users.noreply.github.com> Date: Sat, 29 May 2021 20:18:59 -0400 Subject: [PATCH 3/4] bugfixes --- src/lua/core/drive.lua | 21 ++++++++++++++++++--- src/lua/core/filesystem.lua | 4 ++-- src/lua/core/init.lua | 8 ++++---- src/lua/core/uuidmgr.lua | 17 +++++++++++++---- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/lua/core/drive.lua b/src/lua/core/drive.lua index f3ec1bb..c1e828d 100644 --- a/src/lua/core/drive.lua +++ b/src/lua/core/drive.lua @@ -9,6 +9,19 @@ function drive.register() lprint("Attempting to open: "..ent) local ok, disk = pcall(blk.open, ent) if ok then + local nh = io.open("/sys/block/"..ent.."/device/model", "r") + local name = nh:read("*a"):gsub("%s+$", "") + nh:close() + nh = io.open("/sys/block/"..ent.."/device/vendor", "r") + local vendor = nh:read("*a"):gsub("%s+$", "") + nh:close() + nh = io.open("/sys/block/"..ent.."/device/wwid", "r") + local wwid + if nh then + wwid = nh:read("*a"):gsub("%s+$", "") + nh:close() + end + local component = {} function component.readByte(offset) @@ -20,12 +33,12 @@ function drive.register() end function component.getLabel() - return ent + return name end function component.setLabel(str) -- stub - return ent + return name end function component.readSector(sec) @@ -48,7 +61,9 @@ function drive.register() return blk.size(disk) end - modules.component.api.register(modules.uuidmgr.lookup("drive", ent), "drive", component) + local gid = wwid or (vendor.." "..name.." "..ent) + + modules.component.api.register(modules.uuidmgr.lookup("drive", gid), "drive", component) else lprint("Can't open blkdev: "..ent..": "..disk) end diff --git a/src/lua/core/filesystem.lua b/src/lua/core/filesystem.lua index 70d6924..039a846 100644 --- a/src/lua/core/filesystem.lua +++ b/src/lua/core/filesystem.lua @@ -50,7 +50,7 @@ local function concat(pathA, pathB, ...) return canonical(_concat(2, pathA, pathB, ...)) end -function filesystem.register(basePath, uuid) +function filesystem.register(basePath, identifier) checkArg(1, basePath, "string") if not native.fs_exists(basePath) then @@ -156,7 +156,7 @@ function filesystem.register(basePath, uuid) checkArg(1, value, "number") return value --TODO: Implement, use real labels end - return modules.component.api.register(modules.uuidmgr.lookup("filesystem", basePath), "filesystem", fs) + return modules.component.api.register(modules.uuidmgr.lookup("filesystem", basePath or identifier), "filesystem", fs) end return filesystem diff --git a/src/lua/core/init.lua b/src/lua/core/init.lua index 8e06cbb..a7921b1 100644 --- a/src/lua/core/init.lua +++ b/src/lua/core/init.lua @@ -114,15 +114,15 @@ function main() modules.eeprom.register() modules.gpio.register() modules.internet.start() - modules.filesystem.register("root") + modules.filesystem.register("root", "root") if native.debug and native.platform():match("unix") then - modules.filesystem.register("/", "11111111-1111-1111-1111-111111111111") + modules.filesystem.register("/", "realroot") end if native.platform():match("unix") then - modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid()) + modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid(), "tmpfs") else native.fs_mkdir("tmp") - modules.computer.tmp = modules.filesystem.register("tmp/lupi-" .. modules.random.uuid()) + modules.computer.tmp = modules.filesystem.register("tmp/lupi-" .. modules.random.uuid(), "tmpfs") --TODO: cleaning hook or something end diff --git a/src/lua/core/uuidmgr.lua b/src/lua/core/uuidmgr.lua index 76611c8..81e72f0 100644 --- a/src/lua/core/uuidmgr.lua +++ b/src/lua/core/uuidmgr.lua @@ -1,19 +1,28 @@ -local uuidmgr = {} +local uuidmgr = { + fileversion = 0 +} local uuidstorage = {} -local storeheader = "c32BH" +local storeheader = "c36HH" -- oops do local store = io.open("uuidstore.dat", "r") if store then + if native.debug then lprint("DEBUG: Loading uuidstore.dat...") end pcall(function() - local count = string.unpack("H", store:read(2)) + local version, count = string.unpack("IH", store:read(6)) + if native.debug then lprint(string.format("DEBUG: File version %u, count %u", version, count)) end + if version > uuidmgr.fileversion then + lprint("ERROR: Invalid version!") + error("invalid version") + end for i=1, count do local uuid, clen, ilen = storeheader:unpack(store:read(storeheader:packsize())) local cname, id = store:read(clen), store:read(ilen) uuidstorage[cname] = uuidstorage[cname] or {} uuidstorage[cname][id] = uuid + if native.debug then lprint(string.format("DEBUG: Found %s:%s's UUID of %s", cname, id, uuid)) end end store:close() end) @@ -49,7 +58,7 @@ function uuidmgr.store() if native.debug then lprint(string.format("DEBUG: Saving %d static UUIDs", #saved_ids)) end - store:write(string.pack("H", #saved_ids)) + store:write(string.pack("IH", uuidmgr.fileversion, #saved_ids)) for i=1, #saved_ids do local uuid, component, id = saved_ids[i].uuid, saved_ids[i].component, saved_ids[i].id store:write(storeheader:pack(uuid, #component, #id),component,id) -- 2.30.2 From af39ca5749ab510bb1be8e8226fa596319ffb7ff Mon Sep 17 00:00:00 2001 From: sam <30084950+lunaboards-dev@users.noreply.github.com> Date: Sat, 29 May 2021 20:22:29 -0400 Subject: [PATCH 4/4] sam moment --- src/lua/core/filesystem.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lua/core/filesystem.lua b/src/lua/core/filesystem.lua index 039a846..a0c34ed 100644 --- a/src/lua/core/filesystem.lua +++ b/src/lua/core/filesystem.lua @@ -156,7 +156,7 @@ function filesystem.register(basePath, identifier) checkArg(1, value, "number") return value --TODO: Implement, use real labels end - return modules.component.api.register(modules.uuidmgr.lookup("filesystem", basePath or identifier), "filesystem", fs) + return modules.component.api.register(modules.uuidmgr.lookup("filesystem", identifier or basePath), "filesystem", fs) end return filesystem -- 2.30.2