From 77e542785e50d6284e3244faf22cfa48b76e5086 Mon Sep 17 00:00:00 2001 From: Jane Roxanne Date: Wed, 11 Mar 2020 17:19:08 -0500 Subject: [PATCH] vBIOS --- installer_dat/package_list.lua | 5 +- ksrc/require.lua | 2 +- lib/net_minitel/microtel/microtel-3.lua | 2 +- mods/loader_fuchas/init.lua | 1 + mods/loader_openos/init.lua | 1 + mods/menu_classic/init.lua | 40 +++++------ mods/util_searchpaths/init.lua | 35 ++++++++++ mods/vdev_biosdev/init.lua | 8 +-- mods/vdev_vbios/crc32.lua | 0 mods/vdev_vbios/init.lua | 83 +++++++++++++++++++++++ mods/vdev_vbios/luabios.lua | 64 +++++++++++++++++ mods/vdev_vbios/machine.lua | 73 ++++++++++++++++++++ mods/vfs/init.lua | 2 + release.lua | 4 +- src/zy-neo/builtins/init_managed/init.lua | 9 ++- src/zy-neo/utils.lua | 1 + src/zy-neo/zinit.lua | 6 +- 17 files changed, 304 insertions(+), 32 deletions(-) create mode 100644 mods/util_searchpaths/init.lua create mode 100644 mods/vdev_vbios/crc32.lua create mode 100644 mods/vdev_vbios/init.lua create mode 100644 mods/vdev_vbios/luabios.lua create mode 100644 mods/vdev_vbios/machine.lua create mode 100644 mods/vfs/init.lua diff --git a/installer_dat/package_list.lua b/installer_dat/package_list.lua index a6286a8..04cb5d4 100644 --- a/installer_dat/package_list.lua +++ b/installer_dat/package_list.lua @@ -7,6 +7,9 @@ {name="oefiv1", cat="util", path="mods/util_oefiv1.zy2m"}, {name="openos", cat="loader", path="mods/loader_openos.zy2m"}, {name="fuchas", cat="loader", path="mods/loader_fuchas.zy2m"}, + {name="vbios", cat="vdev", path="mods/vdev_vbios.zy2m"}, + {name="biosdev", cat="vdev", path="mods/vdev_biosdev.zy2m"}, + {name="searchpaths", cat="util", path="mods/util_searchpaths.zy2m"}, --{name="tsuki", cat="loader", path="mods/loader_tsuki.zy2m"}, --{name="romfs", cat="util", path="mods/util_romfs.zy2m"}, {name="cpio", cat="util", path="lib/util_cpio.zy2l"}, @@ -14,7 +17,7 @@ --{name="loadfile", cat="loader", path="mods/loader_loadfile.zy2m"}, {name="minitel", cat="net", path="lib/net_minitel.zy2l"}, --{name="vdev", cat="util", path="mods/util_vdev.zy2m"}, - {name="menu", cat="menu", path="mods/menu_classic.zy2m"}, + {name="classic", cat="menu", path="mods/menu_classic.zy2m"}, --{name="vdevrt", cat="rtmod", path="mods/rtmod_vdevrt.zy2m"}, --{name="tsukinet", cat="net", path="mods/net_tsukinet"}, --{name="biosemu", cat="loader", path="mods/loader_biosemu.zy2m", warning="warn_mod_biosemu"}, diff --git a/ksrc/require.lua b/ksrc/require.lua index d28462d..e18ea07 100644 --- a/ksrc/require.lua +++ b/ksrc/require.lua @@ -10,4 +10,4 @@ function krequire(pkg) if (r) then krlib[pkg] = r() return krlib[pkg] end end end -local krequire = krequire \ No newline at end of file +local krequire = krequire diff --git a/lib/net_minitel/microtel/microtel-3.lua b/lib/net_minitel/microtel/microtel-3.lua index 6198b79..ef783e0 100644 --- a/lib/net_minitel/microtel/microtel-3.lua +++ b/lib/net_minitel/microtel/microtel-3.lua @@ -49,7 +49,7 @@ local function checkCache(packetID) end --local realComputerPullSignal = computer.pullSignal -thd.add(function() +thd.add("minitel_handler", function() while true do local eventTab = {computer.pullSignal(0)} for k,v in pairs(net.hook) do diff --git a/mods/loader_fuchas/init.lua b/mods/loader_fuchas/init.lua index 92a676d..e22c9c6 100644 --- a/mods/loader_fuchas/init.lua +++ b/mods/loader_fuchas/init.lua @@ -21,6 +21,7 @@ end return function(addr, args) --oefi.getExtensions().ZyNeo_ExecOEFIApp(addr, ".efi/fuchas.efi2", ...) --We don't do that here. + local fuch = {} fuch.env = oefi.getExtensions().ZyNeo_GetOEFIEnv(addr) fuch.env.computer.supportsOEFI = function() return true diff --git a/mods/loader_openos/init.lua b/mods/loader_openos/init.lua index 2b06879..49fbb91 100644 --- a/mods/loader_openos/init.lua +++ b/mods/loader_openos/init.lua @@ -12,6 +12,7 @@ return function(addr) end function computer.setBootAddress()end --log(env, env.computer, env.computer.getBootAddress, env.computer.getBootAddress()) + local old_dl = utils.debug_log load(utils.readfile(fs.address, fs.open("init.lua")), "=init.lua", "t")() krequire = kr end \ No newline at end of file diff --git a/mods/menu_classic/init.lua b/mods/menu_classic/init.lua index 1530383..b98affa 100644 --- a/mods/menu_classic/init.lua +++ b/mods/menu_classic/init.lua @@ -29,31 +29,33 @@ function menu.add(text, func) end function menu.draw() + local w, h = gpu.getViewport() - local cls = function()gpu.fill(1,1,w,h," ")end - gpu.setBackground(bg) - gpu.setForeground(fg) - cls() - --Draw some things - local namestr = _BIOS .. " " .. string.format("%.1f.%d %s", _ZVER, _ZPAT, _ZGIT) - gpu.set((w/2)-(#namestr/2), 1, namestr) - gpu.set(1, 2, border_chars[1]) - gpu.set(2, 2, border_chars[2]:rep(w-2)) - gpu.set(w, 2, border_chars[3]) - for i=1, h-6 do - gpu.set(1, i+2, border_chars[4]) - gpu.set(w, i+2, border_chars[4]) - end - gpu.set(1, h-3, border_chars[5]) - gpu.set(2, h-3, border_chars[2]:rep(w-2)) - gpu.set(w, h-3, border_chars[6]) - gpu.set(1, h-1, "Use ↑ and ↓ keys to select which entry is highlighted.") - gpu.set(1, h, "Use ENTER to boot the selected entry.") local stime = computer.uptime() local autosel = true local ypos = 1 local sel = 1 local function redraw() + local w, h = gpu.getViewport() + local cls = function()gpu.fill(1,1,w,h," ")end + gpu.setBackground(bg) + gpu.setForeground(fg) + cls() + --Draw some things + local namestr = _BIOS .. " " .. string.format("%.1f.%d %s", _ZVER, _ZPAT, _ZGIT) + gpu.set((w/2)-(#namestr/2), 1, namestr) + gpu.set(1, 2, border_chars[1]) + gpu.set(2, 2, border_chars[2]:rep(w-2)) + gpu.set(w, 2, border_chars[3]) + for i=1, h-6 do + gpu.set(1, i+2, border_chars[4]) + gpu.set(w, i+2, border_chars[4]) + end + gpu.set(1, h-3, border_chars[5]) + gpu.set(2, h-3, border_chars[2]:rep(w-2)) + gpu.set(w, h-3, border_chars[6]) + gpu.set(1, h-1, "Use ↑ and ↓ keys to select which entry is highlighted.") + gpu.set(1, h, "Use ENTER to boot the selected entry.") gpu.setBackground(bg) gpu.setForeground(fg) gpu.fill(1, h-2, w, 1, " ") diff --git a/mods/util_searchpaths/init.lua b/mods/util_searchpaths/init.lua new file mode 100644 index 0000000..767ba2d --- /dev/null +++ b/mods/util_searchpaths/init.lua @@ -0,0 +1,35 @@ +local zy = krequire("zorya") +local sys = krequire("sys") +local utils = krequire("utils") + +local computer, component = computer, component + +local sp = {} + +function sp.add_mod_path(drive, path) + local px = component.proxy(drive) + zy.add_mod_search(function(mod) + if (px.exists(path.."/"..mod..".zy2m")) then + local h = px.open(path.."/"..mod..".zy2m", "r") + return utils.load_lua(utils.readfile(drive, h)) + elseif (px.exists(path.."/"..mod.."/init.zy2m")) then + local h = px.open(path.."/"..mod.."/init.zy2m", "r") + return utils.load_lua(utils.readfile(drive, h)) + end + end) +end + +function sp.add_lib_path(drive, path) + local px = component.proxy(drive) + sys.add_search(function(mod) + if (px.exists(path.."/"..mod..".zy2l")) then + local h = px.open(path.."/"..mod..".zy2l", "r") + return utils.load_lua(utils.readfile(drive, h)) + elseif (px.exists(path.."/"..mod.."/init.zy2l")) then + local h = px.open(path.."/"..mod.."/init.zy2l", "r") + return utils.load_lua(utils.readfile(drive, h)) + end + end) +end + +return sp \ No newline at end of file diff --git a/mods/vdev_biosdev/init.lua b/mods/vdev_biosdev/init.lua index 94a43f5..85ad416 100644 --- a/mods/vdev_biosdev/init.lua +++ b/mods/vdev_biosdev/init.lua @@ -1,6 +1,6 @@ local zy = krequire("zorya") local vdev = krequire("util_vcomponent") -vdev.register("ZORYA_BIOS", "zybios", { +vdev.register("ZORYA_BIOS", "zybios", { get_threads_info = function() local threads = {} @@ -8,13 +8,13 @@ vdev.register("ZORYA_BIOS", "zybios", { local info = zy.lkthdi(i) threads[i] = { name = info[1], - deadline = info[6] + deadline = info[4] } end return threads end, get_version = function() - return string.format("%f.%d", _ZVER, _ZPAT) + return string.format("%1.1f.%d", _ZVER, _ZPAT) end, get_git_revision = function() return _ZGIT @@ -25,6 +25,6 @@ vdev.register("ZORYA_BIOS", "zybios", { get_version = "get_version():string -- Returns the Zorya NEO version.", get_git_revision = "get_git_revision():string -- Returns the git revision of the build." } -}) +) return true \ No newline at end of file diff --git a/mods/vdev_vbios/crc32.lua b/mods/vdev_vbios/crc32.lua new file mode 100644 index 0000000..e69de29 diff --git a/mods/vdev_vbios/init.lua b/mods/vdev_vbios/init.lua new file mode 100644 index 0000000..d3c7a0f --- /dev/null +++ b/mods/vdev_vbios/init.lua @@ -0,0 +1,83 @@ +local vdev = krequire("util_vcomponent") +local utils = krequire("utils") +local thd = krequire("thd") +local comp = component +local eeprom = {} +local lua_code = [[ +--#include "luabios.lua" +]] +local function generate_vbios(f_, path) + local fs = comp.proxy(f_) + if not fs.exists(path) then + fs.makeDirectory(path) + end + if not fs.exists(path.."/code.lua") then + local h = fs.open(path.."/code.lua", "wb") + fs.write(h, lua_code) + fs.close(h) + end + if not fs.exists(path.."/data.bin") then + local h = fs.open(path.."/data.bin", "wb") + fs.write(h, "") + fs.close(h) + end + if not fs.exists(path.."/label.txt") then + local h = fs.open(path.."/label.txt", "wb") + fs.write(h, "Lua BIOS") + fs.close(h) + end + local tbl = { + get = function() + local h = fs.open(path.."/code.lua", "rb") + return utils.readfile(f_, h) + end, + getData = function() + local h = fs.open(path.."/data.bin", "rb") + return utils.readfile(f_, h) + end, + getLabel = function() + local h = fs.open(path.."/label.txt", "rb") + return utils.readfile(f_, h) + end, + set = function(data) + local h = fs.open(path.."/code.lua", "wb") + fs.write(h, data) + fs.close(h) + end, + setData = function(data) + local h = fs.open(path.."/data.bin", "wb") + fs.write(h, data) + fs.close(h) + end, + setLabel = function(label) + local h = fs.open(path.."/label.txt", "wb") + fs.write(h, label:sub(1, 16)) + fs.close(h) + end, + getDataSize = function() + + return fs.spaceTotal() + end, + getSize = function() + + return fs.spaceTotal() + end, + getChecksum = function() + + return 0/0 + end, + makeReadonly = function() + + return + end + } + vdev.register("vdev-ZY_VBIOS", "eeprom", tbl) + local nice = function() + +--#include "machine.lua" + + end + return nice, tbl +end + +return generate_vbios \ No newline at end of file diff --git a/mods/vdev_vbios/luabios.lua b/mods/vdev_vbios/luabios.lua new file mode 100644 index 0000000..e2f1f8f --- /dev/null +++ b/mods/vdev_vbios/luabios.lua @@ -0,0 +1,64 @@ +local init +do + local component_invoke = component.invoke + local 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 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) +end +init() diff --git a/mods/vdev_vbios/machine.lua b/mods/vdev_vbios/machine.lua new file mode 100644 index 0000000..2865c4e --- /dev/null +++ b/mods/vdev_vbios/machine.lua @@ -0,0 +1,73 @@ + -- This our custom machine.lua + local utils = krequire("utils") + local computer = computer + xpcall(function() + utils.debug_log("Copying env...") + local env = utils.deepcopy(_G) + utils.debug_log("Coppied env.") + env._G = env + env._ENV = env + env.krequire = nil + env._BIOS = nil + env._ZVSTR = nil + env._ZVER = nil + env._ZPAT = nil + env._ZGIT = nil + vdev.install(env) + local _oldlist = env.component.list + local thdid = string.format("%.4x", math.random(0, 2^16-1)) + function env.component.list(...) + local ol = _oldlist(...) + local tcall = function() + local a, t = ol() + if (a ~= "vdev-ZY_VBIOS" and t == "eeprom") then + a, t = ol() + end + return a, t + end + for k, v in pairs(ol) do + if (k ~= "vdev-ZY_VBIOS" and v == "eeprom") then + ol[k] = nil + end + end + return setmetatable({}, {__index=ol, __call=tcall}) + end + function env.load(code, name, mode, e, ...) + local e = e or env + return load(code, name, mode, e, ...) + end + local ded = false + function env.computer.returnToBios() + local thds = thd.get_threads() + local vbname = "vbios$"..tbl.getLabel().."#"..thdid + for i=1, #thds do + if (thds[i][1] == vbname) then + thds[i][6] = true + end + end + ded = true + utils.debug_log("Returning to BIOS...") + end + utils.debug_log("Loading vBIOS...") + thd.add("vbios$"..tbl.getLabel().."#"..thdid, function() + xpcall(function() + utils.debug_log("Starting BIOS.") + assert(load(tbl.get(), "=vbios", "t", env))() + end, function(err) + utils.debug_log("ERROR", "vBIOS error!") + utils.debug_log(err, debug.traceback()) + end) + utils.debug_log("Sending signal.") + computer.pushSignal("vbios_dead") + end) + while true do + if computer.pullSignal() == "vbios_dead" then + utils.debug_log("Got signal.") + break + end + end + utils.debug_log("Dead.") + end, function(err) + utils.debug_log("ERROR", "vBIOS error!") + utils.debug_log(err, debug.traceback()) + end) \ No newline at end of file diff --git a/mods/vfs/init.lua b/mods/vfs/init.lua new file mode 100644 index 0000000..58f93f3 --- /dev/null +++ b/mods/vfs/init.lua @@ -0,0 +1,2 @@ +local component, computer = component, computer +local vfs = {} diff --git a/release.lua b/release.lua index 32a927c..82732d2 100644 --- a/release.lua +++ b/release.lua @@ -22,14 +22,14 @@ status("\n\nBuilding modules.") if (os.execute("stat mods 1>/dev/null 2>&1")) then for l in io.popen("ls mods"):lines() do status("MOD\t"..l) - os.execute("zsh -c 'cd mods/"..l.."; luacomp init.lua -mluamin | lua ../../utils/zlua.lua > ../../pkg/mods/"..l..".zy2m'") + os.execute("zsh -c 'cd mods/"..l.."; luacomp init.lua | lua ../../utils/zlua.lua > ../../pkg/mods/"..l..".zy2m'") end end status("Module build complete.\n\nBuilding libraries.") if (os.execute("stat lib 1>/dev/null 2>&1")) then for l in io.popen("ls lib"):lines() do status("LIB\t"..l) - os.execute("zsh -c 'cd lib/"..l.."; luacomp init.lua -mluamin | lua ../../utils/zlua.lua > ../../pkg/lib/"..l..".zy2l'") + os.execute("zsh -c 'cd lib/"..l.."; luacomp init.lua | lua ../../utils/zlua.lua > ../../pkg/lib/"..l..".zy2l'") end end status("Library build complete.\n\nBuilding installer...") diff --git a/src/zy-neo/builtins/init_managed/init.lua b/src/zy-neo/builtins/init_managed/init.lua index 323712f..db98be6 100644 --- a/src/zy-neo/builtins/init_managed/init.lua +++ b/src/zy-neo/builtins/init_managed/init.lua @@ -24,7 +24,14 @@ assert(bootfs.exists(".zy2/image.romfs"), "No boot image!") local romfs_file = assert(bootfs.open(".zy2/image.romfs", "rb")) local romfs_dev = romfs.read(function(a) - return bootfs.read(romfs_file, a) + local c = "" + local d + while a > 0 do + d = bootfs.read(romfs_file, a) + a = a - #d + c = c .. d + end + return c end, function(a) return bootfs.seek(romfs_file, "cur", a) end, function() diff --git a/src/zy-neo/utils.lua b/src/zy-neo/utils.lua index 91f3eff..b83e7cb 100644 --- a/src/zy-neo/utils.lua +++ b/src/zy-neo/utils.lua @@ -57,4 +57,5 @@ function utils.deepcopy(src, dest) end return dest end + builtins.utils = function() return utils end \ No newline at end of file diff --git a/src/zy-neo/zinit.lua b/src/zy-neo/zinit.lua index a9d6a22..d8ba0d4 100644 --- a/src/zy-neo/zinit.lua +++ b/src/zy-neo/zinit.lua @@ -72,11 +72,11 @@ sys.add_lib("zorya", (function() end function zy.lkthdn() - return #thd.getthreads() + return #thd.get_threads() end function zy.lkthdi(i) - return thd.getthreads()[i][1] + return thd.get_threads()[i] end return zy end)()) @@ -93,7 +93,7 @@ th_a(function() utils.debug_log(mod, ".zy2m") return load_lua(bfs.getfile(".zy2/mods/"..mod..".zy2m"), "=.zy2/mods/"..mod..".zy2m")() elseif (bfs.exists(".zy2/mods/"..mod.."/init.zy2m")) then - return load_lua(bfs.getfile(".zy2/mods/"..mod.."/init.zy2m"), "=.zy2/mods/"..mod.."/init.zy2m")() + return assert(load_lua(bfs.getfile(".zy2/mods/"..mod.."/init.zy2m"), "=.zy2/mods/"..mod.."/init.zy2m"))() end end) sys.add_search(function(mod)