OC-PsychOS/sebios2.lua

173 lines
4.0 KiB
Lua
Raw Normal View History

local ci = component.invoke
function bi(A, method, ...)
local result = table.pack(pcall(ci, A, 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 E = component.list("eeprom")()
computer.getBootAddress = function()
return bi(E, "getData")
end
computer.setBootAddress = function(A)
return bi(E, "setData", A)
end
local tryLoadFrom = 0
do
local S = component.list("S")()
local G = component.list("G")()
if G and S then
bi(G, "bind", S)
end
end
do
local gA,sA = component.list("gpu")(),component.list("screen")()
if gA and sA then
local gP,cy = component.proxy(gA),1
gP.bind(sA)
local sx, sy = gP.getResolution()
local function wl(s)
gP.set(1,cy,s)
cy=cy+1
end
function tryLoadFrom(A)
if component.type(A) == "filesystem" then
local H, R = bi(A, "open", "/init.lua")
if not H then
return nil, R
end
local buffer = ""
repeat
local data, R = bi(A, "read", H, math.huge)
if not data and R then
return nil, R
end
buffer = buffer .. (data or "")
until not data
bi(A, "close", H)
return load(buffer, "=init")
elseif component.type(A) == "tape_drive" then
bi(A,"seek",-math.huge)
local boottype = bi(A,"read",1)
if boottype == "!" then
local rl = tonumber(bi(A,"read",8))
local buffer = bi(A,"read",rl)
return load(buffer, "=init")
end
elseif component.type(A) == "modem" then
bi(A,"open",9671)
bi(A,"broadcast",9671,computer.A())
s=""
local bt = computer.uptime()
while bt+10 > computer.uptime() do
computer.beep()
local ev={computer.pullSignal(0.5)}
if ev[1] == "modem_message" and ev[4] == 9671 then
bt=computer.uptime()
if ev[6] == "." then break end
s=s..ev[6]
end
end
if s ~= "" then
return load(s,"=init")
end
elseif component.type(A) == "internet" then
local I = component.proxy(A)
local S = ""
wl("Paste an address to boot from - or wait for the default.")
local T = 5
local U = "https://lain.shadowkat.net/~izaya/ocdoc/PsychOS-build/fsdev/PsychOS-fsdev-latest/severything.lua"
while computer.uptime() < T do
t,_,c,C = computer.pullSignal(0.5)
if t == "clipboard" then
U = c
end
end
local R=I.request(U)
if not R then return false end
repeat
computer.pullSignal(0.5)
until R.finishConnect()
local code, msg = R.response()
if code ~= 200 then
return false, code, msg
end
repeat
computer.pullSignal(0.5)
ns = R.read(2048)
S=S..(ns or "")
computer.beep()
until not ns
return load(S,"=init")
end
end
local bd = {}
for k,v in ipairs({"filesystem","tape_drive","modem","internet"}) do
for A in component.list(v) do
bd[#bd+1] = A
end
end
local cba = computer.getBootAddress()
local S = (" "):rep(3)
local function rdraw()
gP.fill(1,1,sx,sy," ")
cy=1
wl("SKS Enhanced BIOS v2")
wl("Memory: "..tostring(computer.totalMemory()/1024).."K")
wl(" ")
for k,v in ipairs(bd) do
if v == cba then S = " ".."*".." " else S = (" "):rep(3) end
local e,l = pcall(component.invoke,v,"getLabel")
if not e then l = "" end
wl(S..tostring(k).." "..v.." "..component.type(v).." "..(l or ""))
end
end
rdraw()
local T = 5
local bhc = false
while computer.uptime() < T do
t,_,c,C = computer.pullSignal(0.5)
if t == "key_down" and c > 48 and c < 58 then
cba = bd[c-48]
bhc = true
rdraw()
elseif t == "key_down" and c == 32 then
T = T + 10
elseif t == "key_down" and c == 13 and C == 28 then
T = 0
end
end
if bhc then
computer.setBootAddress(cba)
end
end
end
local init, R
if computer.getBootAddress() then
init, R = tryLoadFrom(computer.getBootAddress())
end
if not init then
computer.setBootAddress()
for k,A in ipairs(bd) do
init, R = tryLoadFrom(A)
if init then
computer.setBootAddress(A)
break
end
end
end
if not init then
error("no bootable medium found" .. (R and (": " .. tostring(R)) or ""), 0)
end
computer.beep(1000, 0.2)
init()