Advertisement
Guest User

bios_bootloader.lua

a guest
Sep 21st, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.56 KB | None | 0 0
  1. local cinv = component.invoke
  2. function binv(address, method, ...)
  3.   local result = table.pack(pcall(cinv, address, method, ...))
  4.   if not result[1] then
  5.     return nil, result[2]
  6.   else
  7.     return table.unpack(result, 2, result.n)
  8.   end
  9. end
  10. local eeprom = component.list("eeprom")()
  11. computer.getBootAddress = function()
  12.   return binv(eeprom, "getData")
  13. end
  14. computer.setBootAddress = function(address)
  15.   return binv(eeprom, "setData", address)
  16. end
  17. do
  18.   local screen = component.list("screen")()
  19.   local gpu = component.list("gpu")()
  20.   if gpu and screen then
  21.     binv(gpu, "bind", screen)
  22.     binv(gpu, "setResolution", 53, 22)
  23.     binv(gpu, "setForeground", 0x00FF00)
  24.   end
  25. end
  26. local function tryLoadFrom(address)
  27.   local handle, reason = binv(address, "open", "/init.lua")
  28.   if not handle then
  29.     return nil, reason
  30.   end
  31.   local buffer = ""
  32.   repeat
  33.     local data, reason = binv(address, "read", handle, math.huge)
  34.     if not data and reason then
  35.       return nil, reason
  36.     end
  37.     buffer = buffer .. (data or "")
  38.   until not data
  39.   binv(address, "close", handle)
  40.   return load(buffer, "=init")
  41. end
  42. function startsWith(string,start)
  43.    return string.sub(string,1,string.len(start))==start
  44. end
  45. local init, reason
  46. do
  47.   local keyboard = component.list("keyboard")()
  48.   local screen = component.list("screen")()
  49.   local gpu = component.list("gpu")()
  50.   if not keyboard then
  51.     if gpu and screen then
  52.       computer.beep(500, 0.4)
  53.       binv(gpu, "fill", 1, 1, 53, 22, " ")
  54.       binv(gpu, "set", 1, 1, "NO KEYBOARD CONNECTED")
  55.     end
  56.     while true do
  57.       computer.pullSignal()
  58.     end
  59.   else
  60.     local userinput = true
  61.     local keycount = 0
  62.     local useraddr = ""
  63.     if gpu and screen then
  64.       binv(gpu, "fill", 1, 1, 53, 22, " ")
  65.       binv(gpu, "set", 1, 1, "MY BOOTLOADER")
  66.       binv(gpu, "set", 1, 2, "ENTER FS ADDRESS OR LABEL")
  67.       while userinput do
  68.         binv(gpu, "set", 1, 4, ">")
  69.         local name, _, key = computer.pullSignal()
  70.         if name == "key_up" then
  71.           if (key >= 0x30 and key <= 0x39) or (key >= 0x61 and key <= 0x66) or (key >= 0x41 and key <= 0x46) then
  72.             binv(gpu, "set", 3 + keycount, 4, unicode.upper(unicode.char(key)))
  73.             useraddr = useraddr .. unicode.lower(unicode.char(key))
  74.             keycount = keycount + 1
  75.           end
  76.         end
  77.         if keycount == 3 then
  78.           binv(gpu, "set", 1, 6, "TRYING TO BOOT FROM FS " .. unicode.upper(useraddr))
  79.           local addr, _
  80.           for addr, _ in component.list("filesystem") do
  81.             if binv(addr, "getLabel") ~= nil and startsWith(unicode.lower(binv(addr, "getLabel")), useraddr) then
  82.               init, reason = tryLoadFrom(addr)
  83.               if init then
  84.                 break
  85.               end
  86.             else
  87.               if startsWith(addr, useraddr) then
  88.                 init, reason = tryLoadFrom(addr)
  89.                 if init then
  90.                   break
  91.                 end
  92.               end
  93.             end
  94.           end
  95.           if init then
  96.             userinput = false
  97.             computer.setBootAddress(addr)
  98.             binv(gpu, "set", 1, 7, "SUCCESS, STARTING INIT.LUA...")
  99.           else
  100.             binv(gpu, "set", 1, 7, "FAILURE, UNABLE TO BOOT")
  101.             keycount = 0
  102.             useraddr = ""
  103.             local sTime = computer.uptime()
  104.             while computer.uptime() < sTime + 3 do
  105.               computer.pullSignal(0.5)
  106.             end
  107.             binv(gpu, "fill", 1, 4, 53, 4, " ")
  108.             binv(gpu, "fill", 1, 6, 53, 6, " ")
  109.             binv(gpu, "fill", 1, 7, 53, 7, " ")
  110.           end
  111.         end
  112.       end
  113.     end
  114.   end
  115. end
  116. computer.beep(1000, 0.2)
  117. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement