Files
HyperionOS/prod/Hyperion-firmware-ccpc/boot/ccpc/boot.lua

2 lines
6.3 KiB
Lua

local a=({...})[1]or"/$"local term=term;local os=os;local function b(c)local d,e=term.getCursorPos()local f,g=term.getSize()for h=1,#c do local i=c:sub(h,h)if i=="\n"then e=e+1;d=1 elseif i=="\t"then local j=4;local k=j-(d-1)%j;term.write(string.rep(" ",k))d=d+k elseif i=="\b"then if d>1 then d=d-1;term.setCursorPos(d,e)term.write(" ")term.setCursorPos(d,e)end else if d<=f and e<=g then term.setCursorPos(d,e)term.write(i)d=d+1 end end;if d>f then d=1;e=e+1 end;if e-1>=g then term.scroll(1)e=g;term.setCursorPos(d,e)end end;term.setCursorPos(d,e)end;local function l(m)term.setBackgroundColor(0x1)term.setTextColor(0x4)term.clear()term.setCursorPos(1,1)term.write("A critical error occurred while loading the system:")term.setCursorPos(1,3)b(m)while true do end end;term.setCursorBlink(false)local n,m=xpcall(function()local o={BOOT_DRIVE_PATH=a}local p={coroutine=true,debug=true,_VERSION=true,assert=true,collectgarbage=true,error=true,gcinfo=true,getmetatable=true,ipairs=true,__inext=true,load=true,math=true,next=true,pairs=true,pcall=true,rawequal=true,rawget=true,rawlen=true,rawset=true,select=true,setmetatable=true,string=true,table=true,tonumber=true,tostring=true,type=true,xpcall=true,_G=true}local debug=debug;for h,q in pairs(_G)do if not p[h]or p[h]==nil then o[h]=q;_G[h]=nil end end;local r={[o.keys.enter]="\n",[o.keys.tab]="\t",[o.keys.backspace]="\b",[o.keys.up]="\17",[o.keys.down]="\18",[o.keys.left]="\19",[o.keys.right]="\20"}function sleep(s)local t=o.os.clock()+s;while t>o.os.clock()do end end;o.term.setPaletteColor(0x1,0xFFFFFF)o.term.setPaletteColor(0x2,0xFF0000)o.term.setPaletteColor(0x4,0x00FF00)o.term.setPaletteColor(0x8,0x0000FF)o.term.setPaletteColor(0x10,0x00FFFF)o.term.setPaletteColor(0x20,0xFF00FF)o.term.setPaletteColor(0x40,0xFFFF00)o.term.setPaletteColor(0x80,0xFF6D00)o.term.setPaletteColor(0x100,0x6DFF55)o.term.setPaletteColor(0x200,0x24FFFF)o.term.setPaletteColor(0x400,0x924900)o.term.setPaletteColor(0x800,0x6D6D55)o.term.setPaletteColor(0x1000,0xDBDBAA)o.term.setPaletteColor(0x2000,0x6D00FF)o.term.setPaletteColor(0x4000,0xB6FF00)o.term.setPaletteColor(0x8000,0x000000)local function u(v)local w=o.fs.open(v,"r")if not w then l("Could not open file: "..v)end;local x=w.readAll()w.close()return x end;local y=load(u(a.."/boot/kernel.lua"),"@Kernel")local z=load(u(a.."/boot/cct/initdisks"),"@Init_disks")(o)local A=load(u(a.."/boot/initfs"),"@InitFs")()if not y then l("Could not load kernel.")end;if not z then l("Could not load initdisks.")end;if not A then l("Could not load initfs.")end;if not o.fs.exists("/nvram.dat")then local w=o.fs.open("/nvram.dat","w")w.write("Hello, World!")w.close()end;local B;if o.fs.exists("/startup.lua")then B="/startup.lua"elseif o.fs.exists("/eeprom")then B="/eeprom"end;local C={}local function D(E,...)table.insert(C,{E,...})end;local F={[0x000000]=0x0001,[0xFFFFFF]=0x0002,[0xFF0000]=0x0004,[0x00FF00]=0x0008,[0x0000FF]=0x0010,[0x00FFFF]=0x0020,[0xFF00FF]=0x0040,[0xFFFF00]=0x0080,[0xFF6D00]=0x0100,[0x6DFF55]=0x0200,[0x24FFFF]=0x0400,[0x924900]=0x0800,[0x6D6D55]=0x1000,[0xDBDBAA]=0x2000,[0x6D00FF]=0x4000,[0xB6FF00]=0x8000}local G,H=0x6D6D55,0x000000;local I,J,K,L={},{},{},0;local function M(N,O)local P=nil;local Q=math.huge;for R,S in pairs(N)do local T=math.abs(R-O)if T<Q then Q=T;P=R end end;return P end;local function U(V)L=L+1;if L%1024==0 then J={}I={}end;if L%8192==0 then K={}end;if K[V]~=nil then return K[V]end;if J[V]~=nil then I[V]=I[V]+1;if I[V]>=16 then K[V]=J[V]J[V]=nil;I[V]=nil;return K[V]end;return J[V]end;local W=M(F,V)if not W then return nil end;local X=F[W]J[V]=X;I[V]=1;return X end;local Y={getEpochMs=function()return o.os.epoch("utc")end,getUptime=function()return o.os.clock()*1000 end,date=function()return o.os.date("!%Y-%m-%dT%H:%M:%SZ",o.os.epoch("utc")/1000)end,getMachineEvent=function()if#C>0 then return table.unpack(table.remove(C,1))else return nil end end,getEEPROM=function()return u(B)end,setEEPROM=function(S,c)local g=o.fs.open(B,"w")g.write(c)g.close()end,initfs=A,disks=z,screenCtl={print=function(S,c)b(c.."\n")end,printInline=function(S,c)b(c)end,clear=function()o.term.clear()o.term.setCursorPos(1,1)end,setCursorPos=function(S,d,e)o.term.setCursorPos(d,e)end,getCursorPos=function()return o.term.getCursorPos()end,getSize=function()return o.term.getSize()end,setBackgroundColor=function(S,Z)o.term.setBackgroundColor(U(Z))end,setTextColor=function(S,Z)o.term.setTextColor(U(Z))end,getBackgroundColor=function()return H end,getTextColor=function()return G end,enable=function()end,disable=function()end},architecture="cct",getNvram=function()return u("/nvram.dat")end,setNvram=function(S,c)local g=o.fs.open("/nvram.dat","w")g.write(c)g.close()end,firmware=o,reboot=false}o.term.setBackgroundColor(0x8000)o.term.setTextColor(0x1000)o.term.clear()o.term.setCursorPos(1,1)local _=coroutine.create(function()local n,m=xpcall(y,debug.traceback,Y)if not n and not Y.reboot then l(m)end;if m then o.os.reboot()else o.os.shutdown()end end)function coroutine.resumeWithTimeout(a0,a1,...)local a2=Y.getEpochMs()debug.sethook(a0,function()if Y.getEpochMs()>a2+a1 then return coroutine.yield("timeout")end end,"",1000)local a3={coroutine.resume(a0,...)}if a3[1]and a3[2]=="timeout"then return"timeout"elseif a3[1]==false then return"error",a3[2]else debug.sethook(a0)return"success",table.unpack(a3,2)end end;b("Loaded in "..tostring(o.os.clock()).." seconds.\n")while true do local a4,m=coroutine.resumeWithTimeout(_,50)o.os.queueEvent("NoSleep")local a5=false;while not a5 do local E={coroutine.yield()}if E[1]=="key"then D("keyPressed",1,E[2])if r[E[2]]then D("keyTyped",1,r[E[2]])end elseif E[1]=="char"then D("keyTyped",1,E[2])elseif E[1]=="key_up"then D("keyReleased",1,E[2])elseif E[1]=="disk"then D("componentAdded","disk")elseif E[1]=="disk_eject"then D("componentRemoved","disk")elseif E[1]=="modem_message"then D("modem_message",table.unpack(E,2))elseif E[1]=="rednet_message"then D("rednet_message",table.unpack(E,2))elseif E[1]=="http_success"then D("http_success",table.unpack(E,2))elseif E[1]=="http_failure"then D("http_failure",table.unpack(E,2))elseif E[1]=="NoSleep"then a5=true end end;if a4=="error"or coroutine.status(_)=="dead"then if Y.reboot then o.os.reboot()end;l("Kernel error: "..tostring(m))coroutine.yield("key")end;z:refresh()end end,debug.traceback)if not n then l("Fatal error during boot: "..m)end;while true do coroutine.yield("key")end