Advertisement
Luca_S

advancedLoader Installer

Aug 20th, 2018
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.47 KB | None | 0 0
  1. -- The following copyright notice applies to this file and the bootcode itself.
  2. --[[
  3. Copyright 2018 Luca_S
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  6.  
  7. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8.  
  9. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  10. ]]--
  11.  
  12. local bootCode = [[local drives = {}
  13. local filesystems = {}
  14. local invoke = component.invoke
  15.  
  16. local function formatMemory(mem)
  17.     local units = {"B", "KiB", "MiB", "GiB"}
  18.     local unit = 1
  19.     while mem > 1024 and units[unit] do
  20.         unit = unit + 1
  21.         mem = mem/1024
  22.     end
  23.     return mem.." "..units[unit]
  24. end
  25.  
  26. local function getBootCode(addr)
  27.     local sector1 = invoke(addr, "readSector", 1)
  28.     for i = 1,#sector1 do
  29.         if sector1:sub(i,i)=="\0" then
  30.             sector1 = sector1:sub(1,i-1)
  31.             break
  32.         end
  33.     end
  34.     return sector1
  35. end
  36.  
  37. local eeprom = component.list("eeprom")()
  38.  
  39. computer.getBootAddress = function()
  40.     return invoke(eeprom, "getData")
  41. end
  42.  
  43. computer.setBootAddress = function(addr)
  44.     return invoke(eeprom, "setData", addr)
  45. end
  46.  
  47. local function isBootable(addr)
  48.     if component.type(addr) == "drive" then
  49.         local f, err = load(getBootCode(addr))
  50.         if not f then
  51.             return false
  52.         end
  53.         return true
  54.     elseif component.type(addr) == "filesystem" then
  55.         return invoke(addr, "exists", "init.lua") and not invoke(addr, "isDirectory", "init.lua")
  56.     else
  57.         return false
  58.     end
  59. end
  60.  
  61. for i, _ in pairs(component.list("drive")) do
  62.     if isBootable(i) then
  63.         drives[#drives+1] = i
  64.     end
  65. end
  66.  
  67. for i, _ in pairs(component.list("filesystem")) do
  68.     if isBootable(i) then
  69.         filesystems[#filesystems+1] = i
  70.     end
  71. end
  72.  
  73. local screen = component.list("screen")()
  74. local gpu = component.list("gpu")()
  75. invoke(gpu, "bind", screen)
  76. invoke(gpu, "setResolution", invoke(gpu, "maxResolution"))
  77. local w, h = invoke(gpu, "getResolution")
  78. local function clear()
  79.     invoke(gpu, "setForeground", 0xFFFFFF)
  80.     invoke(gpu, "setBackground", 0x000000)
  81.     invoke(gpu, "fill", 1, 1, w, h, " ")   
  82. end
  83. clear()
  84.  
  85. local function center(text, y)
  86.     local x = w/2-#text/2
  87.     invoke(gpu, "set", x, y, text)
  88. end
  89.  
  90. local bootable = {}
  91. for _, v in pairs(drives) do
  92.     bootable[#bootable+1] = v
  93. end
  94. for _, v in pairs(filesystems) do
  95.     bootable[#bootable+1] = v
  96. end
  97.  
  98. local bootAddress = computer.getBootAddress()
  99.  
  100. for i, v in pairs(bootable) do
  101.     if v == bootAddress then
  102.         selected = i
  103.         break
  104.     end
  105. end
  106.  
  107. local function drawMenu()
  108.     center("Advanced Bootloader", 2)
  109.     center("Select boot medium", 3)
  110.     center("Computer Memory: "..formatMemory(computer.totalMemory()), 4)
  111.  
  112.     for i, v in pairs(bootable) do
  113.         if i == selected then
  114.             invoke(gpu, "setForeground", 0x000000)
  115.             invoke(gpu, "setBackground", 0xFFFFFF)
  116.         end
  117.  
  118.         invoke(gpu, "fill", 1, i+5, w, 1, " ") -- Clear line
  119.         center(v .. "(" .. (invoke(v, "getLabel") or "No Label") .. ")", i+5)
  120.  
  121.         if i == selected then
  122.             invoke(gpu, "setForeground", 0xFFFFFF)
  123.             invoke(gpu, "setBackground", 0x000000)
  124.         end
  125.     end
  126. end
  127.  
  128. local function boot(addr)
  129.     clear()
  130.     center("Booting " .. addr .. "(" .. (invoke(addr, "getLabel") or "No Label") .. ")", math.floor(h/2))
  131.     center("Please wait...", math.floor(h/2)+1)
  132.     computer.setBootAddress(addr)
  133.     if component.type(addr) == "filesystem" then
  134.         local handle, err = invoke(addr, "open", "/init.lua")
  135.         if not handle then
  136.             error(err)
  137.         end
  138.         local bootCode = ""
  139.         repeat
  140.             local chunk = invoke(addr, "read", handle, math.huge)
  141.             bootCode = bootCode..(chunk or "")
  142.         until not chunk
  143.         load(bootCode)()
  144.     elseif component.type(addr) == "drive" then
  145.         load(getBootCode(addr))()
  146.     end
  147.     clear()
  148. end
  149.  
  150. if #bootable == 1 then
  151.     boot(bootable[1])
  152. elseif #bootable == 0 then
  153.     error("No bootable device!")
  154. end
  155.  
  156. -- Down arrow: 208
  157. -- Up arrow: 200
  158. -- Enter: 28
  159.  
  160. drawMenu()
  161.  
  162. while true do
  163.     local e, _, _, code = computer.pullSignal()
  164.     if e == "key_down" then
  165.         if code == 208 then
  166.             selected = selected + 1
  167.             if selected > #bootable then
  168.                 selected = 1
  169.             end
  170.         elseif code == 200 then
  171.             selected = selected - 1
  172.             if selected < 1 then
  173.                 selected = #bootable
  174.             end
  175.         elseif code == 28 then
  176.             boot(bootable[selected])
  177.         end
  178.         drawMenu()
  179.     elseif e == "touch" then
  180.         -- Code is the Y coordinate of the touch
  181.         if bootable[code-5] then
  182.             if selected == code - 5 then
  183.                 boot(bootable[selected])
  184.             else
  185.                 selected = code - 5
  186.             end
  187.         end
  188.         drawMenu()
  189.     end
  190. end]]
  191. io.write("Do you want to install advancedLoader to the currently inserted EEPROM? [y/N]")
  192. if io.read():lower() ~= "y" then
  193.     print("Aborted!")
  194.     return
  195. end
  196. local c = require("component")
  197. print("Flashing EEPROM, do NOT power down your computer!")
  198. c.eeprom.set(bootCode)
  199. c.eeprom.setLabel("advancedLoader")
  200. print("Completed! :)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement