Advertisement
DOGGYWOOF

DOGGY BOOTLOADER for opencompuer

Apr 7th, 2024 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. local function main()
  2. local ci = component.invoke
  3. local computer = computer or require("computer")
  4.  
  5. local bi = function(a, m, ...)
  6. local r = table.pack(pcall(ci, a, m, ...))
  7. if not r[1] then return nil, r[2] else return table.unpack(r, 2, r.n) end
  8. end
  9.  
  10. local e = component.list("eeprom")()
  11. computer.getBootAddress = function() return bi(e, "getData") end
  12. computer.setBootAddress = function(a) return bi(e, "setData", a) end
  13.  
  14. do
  15. local s, g = component.list("screen")(), component.list("gpu")()
  16. if g and s then bi(g, "bind", s) end
  17. end
  18.  
  19. local function tryLoad(a)
  20. local h, r = bi(a, "open", "/init.lua")
  21. if not h then return nil, "Failed to open /init.lua: " .. tostring(r) end
  22. local b = ""
  23. repeat
  24. local d, r = bi(a, "read", h, math.maxinteger or math.huge)
  25. if not d and r then
  26. bi(a, "close", h)
  27. return nil, "Failed to read /init.lua: " .. tostring(r)
  28. end
  29. b = b .. (d or "")
  30. until not d
  31. bi(a, "close", h)
  32. local i, l = load(b, "=init")
  33. if not i then return nil, "Failed to load /init.lua: " .. tostring(l) end
  34. return i
  35. end
  36.  
  37. local l = {}
  38. for a in component.list("filesystem") do
  39. local b = bi(a, "getLabel") or "Unnamed Drive"
  40. if b ~= "tmpfs" then
  41. table.insert(l, { address = a, label = b })
  42. end
  43. end
  44.  
  45. local function displayError(errorMessage)
  46. local g = component.list("gpu")()
  47. local w, h = bi(g, "getResolution")
  48. local boxWidth, boxHeight = 60, 4
  49. local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
  50. bi(g, "setBackground", 0x0000FF) -- Light blue background
  51. bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
  52. bi(g, "setForeground", 0xFF0000) -- Red text
  53. bi(g, "setBackground", 0x0000FF) -- Light blue background
  54. local textX = math.floor((boxWidth - #errorMessage) / 2) + boxX
  55. local textY = math.floor((boxHeight - 1) / 2) + boxY
  56. bi(g, "set", textX, textY, errorMessage)
  57.  
  58. computer.beep(800, 1) -- 1 second beep
  59. computer.beep(800, 0.5) -- Half-second beep
  60. computer.beep(800, 0.5) -- Half-second beep
  61. end
  62.  
  63. local function displayMenu(sI)
  64. local g = component.list("gpu")()
  65. local w, h = bi(g, "getResolution")
  66. local boxWidth, boxHeight = 60, #l + 4
  67. local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
  68. local filteredList = {}
  69. for _, o in ipairs(l) do
  70. if o.label ~= "tmpfs" then
  71. table.insert(filteredList, o)
  72. end
  73. end
  74. if #filteredList == 0 then
  75. displayError("No bootable medium found!")
  76. return
  77. end
  78. bi(g, "setBackground", 0x0000FF) -- Light blue background
  79. bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
  80. bi(g, "setForeground", 0xFFFFFF)
  81. bi(g, "setBackground", 0x0000FF) -- Light blue background
  82. bi(g, "set", boxX + 2, boxY + 1, "Select OS to boot:")
  83. for i, o in ipairs(filteredList) do
  84. local lN = boxY + i + 2
  85. local tC = i == sI and 0xFF0000 or 0xFFFFFF
  86. bi(g, "setForeground", tC)
  87. bi(g, "set", boxX + 2, lN, o.label .. " (" .. o.address .. ")")
  88. end
  89. computer.beep(800, 0.1) -- Shorter beep
  90. end
  91.  
  92. local sI = math.ceil(#l / 2)
  93. displayMenu(sI)
  94. while true do
  95. local e = { computer.pullSignal() }
  96. if e[1] == "key_down" then
  97. if e[4] == 200 and sI > 1 then
  98. sI = sI - 1
  99. displayMenu(sI)
  100. elseif e[4] == 208 and sI < #l then
  101. sI = sI + 1
  102. displayMenu(sI)
  103. elseif e[4] == 28 then
  104. local o = l[sI]
  105. local i, r = tryLoad(o.address)
  106. if i then
  107. computer.setBootAddress(o.address)
  108. computer.beep(800, 0.2)
  109. computer.beep(800, 0.6)
  110. computer.beep(800, 0.2)
  111. return i()
  112. else
  113. displayError("Unable to boot device!")
  114. computer.beep(800, 0.1)
  115. computer.beep(800, 0.1)
  116. computer.beep(800, 0.1)
  117. end
  118. end
  119. end
  120. end
  121. end
  122.  
  123. main()
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement