Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --#--
- dev = false
- defaultPath = nil
- WaitTime = 15
- ID = nil
- profanity = false
- --==--==--==--==--==--==--==--==--==--
- function cp(str) --FBI, its not what you think it is
- local x, y = term.getSize()
- local cx, cy = term.getCursorPos()
- local l = #str
- term.setCursorPos((x/2)-(l/2), cy)
- print(str)
- end
- function halt()
- os.pullEvent("HALT")
- end
- function fuck(err, act)
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setTextColor(colors.black)
- if profanity then cp("Fuck, it broke") else cp("It broke") end
- print("\r\n")
- if err == nil then cp("Thats all I know") end
- if err ~= nil then print(err) end
- if act == "reboot" then os.reboot() else halt() end
- end
- local counter = 0
- function LoadCFG()
- if fs.exists(".bcfg") then
- local f = fs.open(".bcfg", 'r')
- local d = f.readAll()
- f.close()
- ion = textutils.unserialize(d)
- for k, v in pairs(ion) do
- if dev then print(tostring(k) .. " = " .. tostring(v)) end
- if k == 'SecureBootFile' then
- if SecureBoot == nil then enableSecureBoot(v) end
- else
- ls = loadstring(k .. " = " .. tostring(v))
- setfenv(ls, getfenv())
- ls()
- end
- end
- if dev then sleep(.5) end
- end
- end
- --sha256 adapted from: http://www.computercraft.info/forums2/index.php?/topic/8169-sha-256-in-pure-lua/
- --usage a5(msg)
- local a=2^32;local b=a-1;local function c(d)local mt={}local e=setmetatable({},mt)function mt:__index(f)local g=d(f)e[f]=g;return g end;return e end;local function h(e,i)local function j(k,l)local m,o=0,1;while k~=0 and l~=0 do local p,q=k%i,l%i;m=m+e[p][q]*o;k=(k-p)/i;l=(l-q)/i;o=o*i end;m=m+(k+l)*o;return m end;return j end;local function r(e)local s=h(e,2^1)local t=c(function(k)return c(function(l)return s(k,l)end)end)return h(t,2^e.n or 1)end;local u=r({[0]={[0]=0,[1]=1},[1]={[0]=1,[1]=0},n=4})local function v(k,l,w,...)local x=nil;if l then k=k%a;l=l%a;x=u(k,l)if w then x=v(x,w,...)end;return x elseif k then return k%a else return 0 end end;local function y(k,l,w,...)local x;if l then k=k%a;l=l%a;x=(k+l-u(k,l))/2;if w then x=bit32_band(x,w,...)end;return x elseif k then return k%a else return b end end;local function z(A)return(-1-A)%a end;local function B(k,C)if C<0 then return lshift(k,-C)end;return math.floor(k%2^32/2^C)end;local function D(A,C)if C>31 or C<-31 then return 0 end;return B(A%a,C)end;local function lshift(k,C)if C<0 then return D(k,-C)end;return k*2^C%2^32 end;local function E(A,C)A=A%a;C=C%32;local F=y(A,2^C-1)return D(A,C)+lshift(F,32-C)end;local f={0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2}local function G(H)return string.gsub(H,".",function(w)return string.format("%02x",string.byte(w))end)end;local function I(J,n)local H=""for K=1,n do local L=J%256;H=string.char(L)..H;J=(J-L)/256 end;return H end;local function M(H,K)local n=0;for K=K,K+3 do n=n*256+string.byte(H,K)end;return n end;local function N(O,P)local Q=64-(P+9)%64;P=I(8*P,8)O=O.."\128"..string.rep("\0",Q)..P;assert(#O%64==0)return O end;local function R(S)S[1]=0x6a09e667;S[2]=0xbb67ae85;S[3]=0x3c6ef372;S[4]=0xa54ff53a;S[5]=0x510e527f;S[6]=0x9b05688c;S[7]=0x1f83d9ab;S[8]=0x5be0cd19;return S end;local function T(O,K,S)local U={}for V=1,16 do U[V]=M(O,K+(V-1)*4)end;for V=17,64 do local g=U[V-15]local W=v(E(g,7),E(g,18),D(g,3))g=U[V-2]U[V]=U[V-16]+W+U[V-7]+v(E(g,17),E(g,19),D(g,10))end;local k,l,w,X,Y,d,Z,_=S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[8]for K=1,64 do local W=v(E(k,2),E(k,13),E(k,22))local a0=v(y(k,l),y(k,w),y(l,w))local a1=W+a0;local a2=v(E(Y,6),E(Y,11),E(Y,25))local a3=v(y(Y,d),y(z(Y),Z))local a4=_+a2+a3+f[K]+U[K]_,Z,d,Y,X,w,l,k=Z,d,Y,X+a4,w,l,k,a4+a1 end;S[1]=y(S[1]+k)S[2]=y(S[2]+l)S[3]=y(S[3]+w)S[4]=y(S[4]+X)S[5]=y(S[5]+Y)S[6]=y(S[6]+d)S[7]=y(S[7]+Z)S[8]=y(S[8]+_)end;local function a5(O)O=N(O,#O)local S=R({})for K=1,#O,64 do T(O,K,S)end;return G(I(S[1],4)..I(S[2],4)..I(S[3],4)..I(S[4],4)..I(S[5],4)..I(S[6],4)..I(S[7],4)..I(S[8],4))end
- local function generateKey(strin)
- local key = '"' .. tostring(a5(strin)) .. '"'
- return key
- end
- function enableSecureBoot(file)
- local buffer = fs.open(file, 'r')
- local dump = buffer.readAll()
- --print(dump)
- buffer.close()
- print("Generating Key")
- SecureBootKey = generateKey(dump)
- print("Generated Key")
- --Rewriting startup
- print("Rewriting startup")
- buffer = fs.open('startup', 'r')
- dump = buffer.readAll()
- buffer.close()
- fs.delete("startup")
- buffer = fs.open('startup', 'w')
- buffer.write("--#-- \n")
- buffer.write('local SecureBoot = \'' .. file .. "' \n")
- buffer.write('local SecureBootKey = ' .. SecureBootKey .. " \n")
- buffer.write(dump)
- buffer.close()
- print("Rewrote Startup")
- return 200
- end
- function checkSecureBoot(boot)
- local function protect()
- if SecureBootKey == nil then return true end
- local buffer = fs.open(boot, 'r')
- local dump = buffer.readAll()
- buffer.close()
- local key = generateKey(dump)
- if key == '"' .. SecureBootKey .. '"' and boot == SecureBoot then return true end
- if key ~= '"' .. SecureBootKey .. '"' then term.setTextColour(colors.red) print("Secure Boot: Key Not Recognized, This is not the key for " .. SecureBoot) term.setTextColour(colors.black) return false end
- if boot ~= SecureBoot then term.setTextColour(colors.red) print("Secure Boot: File not recognized") term.setTextColour(colors.black) return false end
- end
- o, e = pcall(protect)
- if not o then fuck(e, 'reboot') end
- return e
- end
- --sha256 algorithm adapted from http://www.computercraft.info/forums2/index.php?/topic/8169-sha-256-in-pure-lua/
- local a=2^32;local b=a-1;local function c(d)local mt={}local e=setmetatable({},mt)function mt:__index(f)local g=d(f)e[f]=g;return g end;return e end;local function h(e,i)local function j(k,l)local m,o=0,1;while k~=0 and l~=0 do local p,q=k%i,l%i;m=m+e[p][q]*o;k=(k-p)/i;l=(l-q)/i;o=o*i end;m=m+(k+l)*o;return m end;return j end;local function r(e)local s=h(e,2^1)local t=c(function(k)return c(function(l)return s(k,l)end)end)return h(t,2^e.n or 1)end;local u=r({[0]={[0]=0,[1]=1},[1]={[0]=1,[1]=0},n=4})local function v(k,l,w,...)local x=nil;if l then k=k%a;l=l%a;x=u(k,l)if w then x=v(x,w,...)end;return x elseif k then return k%a else return 0 end end;local function y(k,l,w,...)local x;if l then k=k%a;l=l%a;x=(k+l-u(k,l))/2;if w then x=bit32_band(x,w,...)end;return x elseif k then return k%a else return b end end;local function z(A)return(-1-A)%a end;local function B(k,C)if C<0 then return lshift(k,-C)end;return math.floor(k%2^32/2^C)end;local function D(A,C)if C>31 or C<-31 then return 0 end;return B(A%a,C)end;local function lshift(k,C)if C<0 then return D(k,-C)end;return k*2^C%2^32 end;local function E(A,C)A=A%a;C=C%32;local F=y(A,2^C-1)return D(A,C)+lshift(F,32-C)end;local f={0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2}local function G(H)return string.gsub(H,".",function(w)return string.format("%02x",string.byte(w))end)end;local function I(J,n)local H=""for K=1,n do local L=J%256;H=string.char(L)..H;J=(J-L)/256 end;return H end;local function M(H,K)local n=0;for K=K,K+3 do n=n*256+string.byte(H,K)end;return n end;local function N(O,P)local Q=64-(P+9)%64;P=I(8*P,8)O=O.."\128"..string.rep("\0",Q)..P;assert(#O%64==0)return O end;local function R(S)S[1]=0x6a09e667;S[2]=0xbb67ae85;S[3]=0x3c6ef372;S[4]=0xa54ff53a;S[5]=0x510e527f;S[6]=0x9b05688c;S[7]=0x1f83d9ab;S[8]=0x5be0cd19;return S end;local function T(O,K,S)local U={}for V=1,16 do U[V]=M(O,K+(V-1)*4)end;for V=17,64 do local g=U[V-15]local W=v(E(g,7),E(g,18),D(g,3))g=U[V-2]U[V]=U[V-16]+W+U[V-7]+v(E(g,17),E(g,19),D(g,10))end;local k,l,w,X,Y,d,Z,_=S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[8]for K=1,64 do local W=v(E(k,2),E(k,13),E(k,22))local a0=v(y(k,l),y(k,w),y(l,w))local a1=W+a0;local a2=v(E(Y,6),E(Y,11),E(Y,25))local a3=v(y(Y,d),y(z(Y),Z))local a4=_+a2+a3+f[K]+U[K]_,Z,d,Y,X,w,l,k=Z,d,Y,X+a4,w,l,k,a4+a1 end;S[1]=y(S[1]+k)S[2]=y(S[2]+l)S[3]=y(S[3]+w)S[4]=y(S[4]+X)S[5]=y(S[5]+Y)S[6]=y(S[6]+d)S[7]=y(S[7]+Z)S[8]=y(S[8]+_)end;local function a5(O)O=N(O,#O)local S=R({})for K=1,#O,64 do T(O,K,S)end;return G(I(S[1],4)..I(S[2],4)..I(S[3],4)..I(S[4],4)..I(S[5],4)..I(S[6],4)..I(S[7],4)..I(S[8],4))end
- function ViewInsert()
- if fs.exists("/Views") ~= true then fs.makeDir("/Views") end
- if fs.exists("/Views/main.view") ~= true then
- local f = fs.open("/Views/main.view", 'w')
- local payload = [[{
- Children={
- {
- X=1,
- Width="100%",
- Y=2,
- Name="HelloLabel",
- Type="Label",
- Text="Bootloader Setup",
- Align="Center",
- },
- {
- X=2,
- Y=4,
- Name="DebugMode",
- Type="Button",
- Text="Debug Mode: Disabled",
- },
- {
- X="1",
- Y="100%",
- Align = "Right",
- Name="Save",
- Type="Button",
- Text="Save",
- },
- {
- X=2,
- Y=6,
- Width=22,
- Type="TextBox",
- Name="DPath",
- Placeholder="Default Path",
- Active=false
- },
- {
- X=2,
- Y=8,
- Width=22,
- Type="TextBox",
- Name="WaitT",
- Placeholder="Wait Time",
- Active=false,
- },
- {
- X="88%",
- Y="100%",
- Align = "Left",
- Name="Reboot",
- Type="Button",
- Text="Reboot",
- },
- },
- BackgroundColour='white'
- } ]]
- f.write(payload)
- f.close()
- end
- end
- function startup()
- if fs.exists('startup') then
- local f = fs.open('startup', 'r')
- local ck = f.readLine()
- debug(ck)
- if ck ~= "--#--" then
- --to preserve existing startup, uncomment
- --fs.move('startup', 'startup.boot')
- end
- end
- local f = fs.open(shell.getRunningProgram(), 'r')
- local ck = f.readLine()
- f.close()
- local f = fs.open(shell.getRunningProgram(), 'r')
- if ck == "--#--" then
- debug('sgrp = --#--')
- local w = fs.open('startup', 'w')
- w.write(f.readAll())
- w.close()
- --debug('New Startup Created')
- end
- f.close()
- --sleep(5)
- end
- function debug(txt)
- if dev == 'true' or dev then print(txt) end
- end
- function cfill(c)
- if term.isColor() ~= true then c = colors.black end
- term.setCursorPos(1, 1)
- term.setBackgroundColor(c)
- term.setTextColor(c)
- for i = 1, 24 do
- print("=========================================================")
- end
- term.setCursorPos(1, 1)
- end
- function kt()
- r = true
- debug('Keypress Listener Started')
- x, y = term.getSize()
- x1, y1 = term.getCursorPos()
- term.setCursorPos(x/2-11, y)
- print("DELETE for more options")
- if term.isColor() then
- term.setCursorPos(x/2-10, y-2)
- print("ESCAPE to enter setup")
- end
- while true do
- os.startTimer(.1)
- term.setCursorPos(1, y1+1)
- print("Time Remaining: ")
- term.setCursorPos(17, y1+1)
- print(" ")
- term.setCursorPos(17, y1+1)
- print(WaitTime - counter)
- local v, k = os.pullEvent()
- r = 'Continue'
- if v == 'key' then
- if k == keys.enter then r = 'Continue' debug('Enter Pressed') break end
- if k == keys.delete then r = "Menu" debug('Delete Pressed') break end
- if k == 1 then r = "Setup" debug("Escape Pressed") break end
- if k == keys.tab then os.loadAPI('/rom/programs/lua') os.reboot() end
- end
- sleep(.1)
- counter = counter + 1
- if counter >= WaitTime then break end
- end
- return r
- end
- function find()
- local results = {}
- if fs then
- local suffix = {'boot', 'start', 'startup', 'load', 'bin'}
- for k, v in pairs(suffix) do
- debug('searching *.' .. v)
- local res = {}
- res = fs.find('*.' .. v)
- for g, h in pairs(res) do
- table.insert(results, h)
- end
- end
- debug('')
- else
- error("NO FS API")
- end
- return results
- end
- function select(l)
- debug(l[1] .. " Selected")
- return l[1]
- end
- function exec(file)
- local f = fs.open(file, 'r')
- local r = f.readAll()
- f.close()
- local c = loadstring(r)
- setfenv(c, getfenv())
- if term then
- term.clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.black)
- cfill(colors.black)
- term.setTextColor(colors.white)
- else
- error("NO TERM API")
- end
- c()
- end
- function boot()
- local r = "Continue"
- if ID ~= nil then function os.getComputerID() return ID end end
- print('Searching...')
- local f = find()
- if #f < 1 then
- term.setTextColor(colors.red)
- print('No Bootable Medium Found')
- term.setTextColor(colors.white)
- sleep(2)
- r = false
- else
- local p = ''
- if #f > 1 or #f < 1 then p = 's' end
- print('found ' .. #f .. " file" .. p)
- for v, i in pairs(f) do
- print(' ' .. i)
- end
- local chosen = select(f)
- if defaultPath ~= nil and #defaultPath >= 1 then chosen = defaultPath end
- print('Selecting: ' .. chosen)
- debug('')
- local k = kt()
- if k == "Continue" and checkSecureBoot(chosen) then
- exec(chosen)
- elseif k == "Menu" then
- r = "Menu"
- elseif k == "Setup" then
- r = "Setup"
- else
- term.setTextColor(colors.red)
- print("WARNING: IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING VERY NASTY, PLEASE REBOOT AND MAKE SURE THAT", string.upper(chosen), "HAS NOT BEEN TAMPERED WITH.")
- os.pullEvent("HALT")
- end
- end
- return r
- end
- function setup()
- local bedrockPath='/' if OneOS then OneOS.LoadAPI('/System/API/Bedrock.lua', false)elseif fs.exists(bedrockPath..'/Bedrock')then os.loadAPI(bedrockPath..'/Bedrock')else if http then print('Downloading Bedrock...')local h=http.get('http://pastebin.com/raw.php?i=0MgKNqpN')if h then local f=fs.open(bedrockPath..'/Bedrock','w')f.write(h.readAll())f.close()h.close()os.loadAPI(bedrockPath..'/Bedrock')else error('Failed to download Bedrock. Is your internet working?') end else error('This program needs to download Bedrock to work. Please enable HTTP.') end end if Bedrock then Bedrock.BasePath = bedrockPath Bedrock.ProgramPath = shell.getRunningProgram() end
- ViewInsert()
- local program = Bedrock:Initialise()
- program:Run(function()
- DevMode = dev
- --DebugMode Button
- if DevMode == false then
- program:GetObject('DebugMode').Text = "Debug Mode: Disabled"
- else
- program:GetObject('DebugMode').Text = "Debug Mode: Enabled"
- end
- program:GetObject('DebugMode').OnClick = function(self, event, side, x, y)
- if DevMode == false then DevMode = true else DevMode = false end
- dev = DevMode
- if DevMode == false then
- program:GetObject('DebugMode').Text = "Debug Mode: Disabled"
- else
- program:GetObject('DebugMode').Text = "Debug Mode: Enabled"
- end
- end
- --End
- --Reboot Button
- program:GetObject('Reboot').OnClick = function(self, event, side, x, y) os.reboot() end
- --End
- --Default Path
- function getpath()
- local path = program:GetObject('DPath').Text
- local safe = false
- if path ~= nil and fs.exists(path) then
- safe = true
- elseif path ~= nil and fs.exists(path) ~= true then
- program:DisplayAlertWindow('Error', 'Invalid Path', {'OK'}, function(value) end)
- safe = false
- path = ""
- elseif path == nil then
- safe = true
- path = ""
- end
- msg = {}
- msg.safe = safe
- msg.path = tostring("'" .. path .. "'")
- return msg
- end
- --End
- --Wait Time
- function getwait()
- local path = program:GetObject('WaitT').Text
- local safe = false
- if path ~= nil and tonumber(path) then
- safe = true
- elseif path == "" then
- safe = true
- path = "15"
- else
- program:DisplayAlertWindow('Error', 'Invalid Wait Time', {'OK'}, function(value) end)
- safe = false
- path = ""
- end
- msg = {}
- msg.safe = safe
- msg.value = path
- return msg
- end
- --End
- --Save Button
- program:GetObject('Save').OnClick = function(self, event, side, x, y)
- local ne = true
- local p = getpath()
- local WTV = getwait()
- local path = ""
- local WV = ""
- local out = {}
- if p['safe'] then path = p['path'] else ne = false end
- if WTV['safe'] then WV = WTV['value'] else ne = false end
- --vars
- out.dev = DevMode
- out.defaultPath = path
- out.WaitTime = WV
- --end
- if ne then local f = fs.open(".bcfg", 'w') f.write(textutils.serialize(out)) f.close() end
- if ne then program:DisplayAlertWindow('Success', 'Settings Saved', {'OK'}, function(value) end) end
- end
- end)
- end
- function menu()
- term.clear()
- cfill(colors.blue)
- if term.isColor then term.setBackgroundColor(colors.blue) end
- term.setTextColor(colors.white)
- local x, y = term.getSize()
- term.setCursorPos((x/2)-8, 1)
- print('Select a boot file')
- local x, y = term.getSize()
- term.setCursorPos(1,4)
- dev = false
- local f = find()
- dev = true
- for i, v in pairs(f) do
- print(i .. '. ' .. v)
- end
- print('0. Other')
- print('')
- print('')
- write('#> ')
- local n = read()
- if tonumber(n) == 0 then
- write('Path> ')
- local o = read()
- if fs.exists(o) then
- exec(o)
- else
- print('file does not exist')
- sleep(2)
- menu()
- end
- elseif tonumber(n) == nil or tonumber(n)>#f or tonumber(n)<1 then
- print('Invalid boot file')
- menu()
- else
- local s = f[tonumber(n)]
- print('Selecting ' .. s)
- sleep(1)
- exec(s)
- end
- end
- argv = {...}
- function main()
- if #argv > 0 then
- --Someones trying to configure via commandline
- local cfg = {}
- if fs.exists('.bcfg') then
- local cfgb = fs.open('.bcfg', 'r')
- cfg = cfgb.readAll()
- cfgb.close()
- cfg = textutils.unserialise(cfg)
- end
- --f, s = string.match(v, "(%w+)=(%w+)")
- local f = argv[1]
- local t = argv[2]
- cfg[f] = t
- print("Inserted ", t, " into ", f)
- local cfgb = fs.open('.bcfg', 'w')
- cfgb.write(textutils.serialize(cfg))
- cfgb.close()
- elseif shell.getRunningProgram() ~= nil then
- if shell.getRunningProgram() ~= 'startup' or fs.exists('startup') == false then
- startup()
- os.reboot()
- else
- LoadCFG()
- local b = boot()
- if b == "Continue" then
- --booted successfully
- elseif b == "Menu" then
- --launch menu(
- menu()
- elseif b == "Setup" then
- --start setup
- setup()
- end
- end
- else
- --launch a shell
- os.loadAPI('/rom/programs/lua')
- end
- end
- local stat, err = pcall(main)
- if not stat then fuck(err) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement