From 7d5a223f8540f4032073b37c1a6ba53aff4bb0e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 6 Jan 2016 21:32:53 +0100 Subject: [PATCH] Implement eeprom component --- include/luares.h | 2 + src/c/modules.c | 2 + src/lua/core/boot.lua | 32 +++++++++++----- src/lua/core/eeprom.lua | 68 ++++++++++++++++++++++++++++++++++ src/lua/core/eepromDefault.lua | 61 ++++++++++++++++++++++++++++++ src/lua/core/init.lua | 3 ++ 6 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 src/lua/core/eeprom.lua create mode 100644 src/lua/core/eepromDefault.lua diff --git a/include/luares.h b/include/luares.h index 11e6ff0..a19e3b5 100644 --- a/include/luares.h +++ b/include/luares.h @@ -3,6 +3,8 @@ extern char lua_boot[]; extern char lua_component[]; extern char lua_computer[]; +extern char lua_eepromDefault[]; +extern char lua_eeprom[]; extern char lua_init[]; extern char lua_sandbox[]; extern char lua_textgpu[]; diff --git a/src/c/modules.c b/src/c/modules.c index 76de31e..b7fa8db 100644 --- a/src/c/modules.c +++ b/src/c/modules.c @@ -11,6 +11,8 @@ void setup_modules(lua_State *L) { pushstuple(L, "boot", lua_boot); pushstuple(L, "component", lua_component); pushstuple(L, "computer", lua_computer); + pushstuple(L, "eeprom", lua_eeprom); + pushstuple(L, "eepromDefault", lua_eepromDefault); pushstuple(L, "sandbox", lua_sandbox); pushstuple(L, "textgpu", lua_textgpu); pushstuple(L, "color", lua_util_color); diff --git a/src/lua/core/boot.lua b/src/lua/core/boot.lua index e61fc55..249a81e 100644 --- a/src/lua/core/boot.lua +++ b/src/lua/core/boot.lua @@ -1,15 +1,29 @@ local boot = {} function boot.boot() - local gpu = modules.component.api.proxy(modules.component.api.list("gpu", true)()) - local w, h = gpu.getResolution() - print("r= " .. tostring(w) .. " " .. tostring(h)) - gpu.fill(0, 0, w, h, " ") - gpu.set(10, 5, "HHHHHHHHHHHHH") - print("LuPI L2 INIT") - print("FIXME: boot stub") - native.sleep(1000000) - error("Unable to boot") + local gpu = modules.component.api.proxy(modules.component.api.list("gpu", true)()) + local w, h = gpu.getResolution() + print("r= " .. tostring(w) .. " " .. tostring(h)) + gpu.fill(0, 0, w, h, " ") + gpu.set(10, 5, "HHHHHHHHHHHHH") + gpu.set(11, 11, "VVVVVVVVVVVVVVVVV", true) + print("LuPI L2 INIT") + print("FIXME: boot stub") + native.sleep(1000000) + + local code = modules.component.api.invoke(modules.component.api.list("eeprom", true)(), "get") + if not code then + print("No bootcode") + error("No bootcode") + end + ---PASS SANDBOX!!!!!! + local f, reason = load(code, "=BIOS") + if not f then + print(reason) + else + f() + print("System quit, Panic") + end end return boot diff --git a/src/lua/core/eeprom.lua b/src/lua/core/eeprom.lua new file mode 100644 index 0000000..ec01b81 --- /dev/null +++ b/src/lua/core/eeprom.lua @@ -0,0 +1,68 @@ +local eeprom = {} +local default = moduleCode["eepromDefault"] +local size = 4092 +local dataSize = 256 + +function eeprom.register() + local component = {} + function component.get() + local h = io.open("usereeprom.lua", "r") + if h then + local data = h:read("*a") + h:close() + return data + else + return default + end + end + function component.set(data) + checkArg(1, data, "string") + data = data:sub(1, size) + local h = io.open("usereeprom.lua", "w") + if not h then + error("Critical: Cannot open EERPOM file") + end + h:write(data) + h:close() + end + function component.getLabel() + return "LUA BIOS" + end + function component.setLabel() + return nil, "Cannot set label" + end + function component.getSize() + return size + end + function component.getData() + local h = io.open("usereepromdata.lua", "r") + if h then + local data = h:read("*a") + h:close() + return data + else + return default + end + end + function component.setData(data) + checkArg(1, data, "string") + data = data:sub(1, dataSize) + local h = io.open("usereepromdata.lua", "w") + if not h then + error("Critical: Cannot open EERPOM file") + end + h:write(data) + h:close() + end + + --FIXME: Implement + function component.getChecksum() + error("Method stub") + end + function component.makeReadonly() + return false, "Method stub" + end + modules.component.api.register(nil, "eeprom", component) +end + +return eeprom diff --git a/src/lua/core/eepromDefault.lua b/src/lua/core/eepromDefault.lua new file mode 100644 index 0000000..f47f963 --- /dev/null +++ b/src/lua/core/eepromDefault.lua @@ -0,0 +1,61 @@ +local component_invoke = component.invoke +function boot_invoke(address, method, ...) + local result = table.pack(pcall(component_invoke, address, method, ...)) + if not result[1] then + return nil, result[2] + else + return table.unpack(result, 2, result.n) + end +end + +-- backwards compatibility, may remove later +local eeprom = component.list("eeprom")() +computer.getBootAddress = function() + return boot_invoke(eeprom, "getData") +end +computer.setBootAddress = function(address) + return boot_invoke(eeprom, "setData", address) +end + +do + local screen = component.list("screen")() + local gpu = component.list("gpu")() + if gpu and screen then + boot_invoke(gpu, "bind", screen) + end +end +local function tryLoadFrom(address) + local handle, reason = boot_invoke(address, "open", "/init.lua") + if not handle then + return nil, reason + end + local buffer = "" + repeat + local data, reason = boot_invoke(address, "read", handle, math.huge) + if not data and reason then + return nil, reason + end + buffer = buffer .. (data or "") + until not data + boot_invoke(address, "close", handle) + return load(buffer, "=init") +end +local init, reason +if computer.getBootAddress() then + init, reason = tryLoadFrom(computer.getBootAddress()) +end +if not init then + computer.setBootAddress() + for address in component.list("filesystem") do + init, reason = tryLoadFrom(address) + if init then + computer.setBootAddress(address) + break + end + end +end +if not init then + error("no bootable medium found" .. (reason and (": " .. tostring(reason)) or ""), 0) +end +computer.beep(1000, 0.2) +init() diff --git a/src/lua/core/init.lua b/src/lua/core/init.lua index 3154e8c..d308e2d 100644 --- a/src/lua/core/init.lua +++ b/src/lua/core/init.lua @@ -41,6 +41,7 @@ loadModule("color") loadModule("component") loadModule("computer") +loadModule("eeprom") loadModule("textgpu") loadModule("sandbox") @@ -50,5 +51,7 @@ loadModule("boot") modules.component.prepare() modules.computer.prepare() +modules.eeprom.register() modules.textgpu.start() + modules.boot.boot()