Advertisement
DOGGYWOOF

DOGGY BOOTLOADER V2

Apr 28th, 2024 (edited)
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. local function main()
  2. local ci = component.invoke
  3. local computer = computer or require("computer")
  4.  
  5. local function invoke(component, method, ...)
  6. local result = table.pack(pcall(component[method], ...))
  7. if not result[1] then return nil, result[2] else return table.unpack(result, 2, result.n) end
  8. end
  9.  
  10. local function tryLoad(address)
  11. local handle, err = invoke(component.proxy(address), "open", "/init.lua")
  12. if not handle then return nil, "Failed to open /init.lua: " .. tostring(err) end
  13. local buffer = ""
  14. repeat
  15. local data, err = invoke(component.proxy(address), "read", handle, math.maxinteger or math.huge)
  16. if not data and err then
  17. invoke(component.proxy(address), "close", handle)
  18. return nil, "Failed to read /init.lua: " .. tostring(err)
  19. end
  20. buffer = buffer .. (data or "")
  21. until not data
  22. invoke(component.proxy(address), "close", handle)
  23. local fn, err = load(buffer, "=init")
  24. if not fn then return nil, "Failed to load /init.lua: " .. tostring(err) end
  25. return fn
  26. end
  27.  
  28. local function displayError(errorMessage)
  29. local gpu = component.list("gpu")()
  30. local w, h = invoke(component.proxy(gpu), "getResolution")
  31. local boxWidth, boxHeight = 80, 6
  32. local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
  33. invoke(component.proxy(gpu), "setBackground", 0x001f3f)
  34. invoke(component.proxy(gpu), "fill", boxX, boxY, boxWidth, boxHeight, " ")
  35. invoke(component.proxy(gpu), "setForeground", 0xFF4500)
  36. invoke(component.proxy(gpu), "setBackground", 0x001f3f)
  37. local textX = math.floor((boxWidth - #errorMessage) / 2) + boxX
  38. local textY = math.floor((boxHeight - 1) / 2) + boxY
  39. invoke(component.proxy(gpu), "set", textX, textY, errorMessage)
  40. computer.beep(800, 1)
  41. while true do
  42. local event = {computer.pullSignal()}
  43. if event[1] == "key_down" then
  44. computer.shutdown(true)
  45. break
  46. end
  47. end
  48. end
  49.  
  50. local function displayMenu(selectedIndex)
  51. local gpu = component.list("gpu")()
  52. local w, h = invoke(component.proxy(gpu), "getResolution")
  53. local boxWidth, boxHeight = 60, 4 + #l
  54. local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
  55. local drives = {}
  56. for address, label in component.list("filesystem") do
  57. if label ~= "tmpfs" then
  58. table.insert(drives, {address = address, label = label})
  59. end
  60. end
  61. if #drives == 0 then
  62. displayError("ERROR: No bootable medium found! Insert a bootable device.")
  63. return
  64. end
  65. invoke(component.proxy(gpu), "setBackground", 0x001f3f)
  66. invoke(component.proxy(gpu), "fill", boxX, boxY, boxWidth, boxHeight, " ")
  67. invoke(component.proxy(gpu), "setForeground", 0xFFFFFF)
  68. invoke(component.proxy(gpu), "setBackground", 0x001f3f)
  69. invoke(component.proxy(gpu), "set", boxX + 2, boxY + 1, "Select OS to boot:")
  70. for i, drive in ipairs(drives) do
  71. local lineY = boxY + i + 2
  72. local textColor = i == selectedIndex and 0xFFD700 or 0xFFFFFF
  73. invoke(component.proxy(gpu), "setForeground", textColor)
  74. invoke(component.proxy(gpu), "set", boxX + 2, lineY, drive.label .. " (" .. drive.address .. ")")
  75. end
  76. computer.beep(800, 0.1)
  77. end
  78.  
  79. local selectedIndex = math.ceil(#l / 2)
  80. displayMenu(selectedIndex)
  81.  
  82. while true do
  83. local event = {computer.pullSignal()}
  84. if event[1] == "key_down" then
  85. if event[4] == 200 and selectedIndex > 1 then
  86. selectedIndex = selectedIndex - 1
  87. displayMenu(selectedIndex)
  88. elseif event[4] == 208 and selectedIndex < #l then
  89. selectedIndex = selectedIndex + 1
  90. displayMenu(selectedIndex)
  91. elseif event[4] == 28 then
  92. local selectedDrive = component.list("filesystem")()
  93. local initFn, err = tryLoad(selectedDrive)
  94. if initFn then
  95. computer.setBootAddress(selectedDrive)
  96. computer.beep(800, 0.2)
  97. computer.beep(800, 0.6)
  98. computer.beep(800, 0.2)
  99. return initFn()
  100. else
  101. displayError("ERROR: Unable to boot from " .. (l[selectedIndex] and l[selectedIndex].label or "selected device") .. ". Check the device and try again.")
  102. computer.beep(800, 0.1)
  103. computer.beep(800, 0.1)
  104. computer.beep(800, 0.1)
  105. end
  106. end
  107. end
  108. end
  109. end
  110.  
  111. main()
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement