Advertisement
DOGGYWOOF

DOGGY BOOTLOADER for opencomputers with BIOS UPDATE FEATURE

Apr 12th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 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 function flashEEPROM(address)
  38. local h, r = bi(address, "open", "/DOGGY.ROM")
  39. if not h then return false, "Failed to open DOGGY.ROM: " .. tostring(r) end
  40. local totalSize = bi(address, "size", "/DOGGY.ROM")
  41. local pageSize = 64 -- Example EEPROM page size
  42. local chunkSize = math.min(totalSize, pageSize * 2) -- Choose a chunk size that is a multiple of page size, e.g., 128 bytes
  43. local data, chunk = "", ""
  44. local progressWidth = 20 -- Width of the progress bar
  45. repeat
  46. chunk = bi(address, "read", h, chunkSize)
  47. if chunk then
  48. bi(e, "set", data .. chunk) -- Write chunk to EEPROM
  49. data = data .. chunk
  50. local progress = #data / totalSize
  51. local progressBar = string.rep("=", math.floor(progress * progressWidth)) .. string.rep(" ", progressWidth - math.floor(progress * progressWidth))
  52. bi(component.list("gpu")(), "set", 1, 1, "Flashing: [" .. progressBar .. "] " .. math.floor(progress * 100) .. "%")
  53. end
  54. computer.pullSignal(6) -- Wait for 6 seconds
  55. until not chunk
  56. bi(address, "close", h)
  57. return true
  58. end
  59.  
  60. local function bootloader()
  61. local l = {}
  62. for a in component.list("filesystem") do
  63. local b = bi(a, "getLabel") or "Unnamed Drive"
  64. if b ~= "tmpfs" then
  65. table.insert(l, { address = a, label = b })
  66. end
  67. end
  68.  
  69. local function displayError(errorMessage)
  70. local g = component.list("gpu")()
  71. local w, h = bi(g, "getResolution")
  72. local boxWidth, boxHeight = 60, 4
  73. local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
  74. bi(g, "setBackground", 0x0000FF) -- Light blue background
  75. bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
  76. bi(g, "setForeground", 0xFF0000) -- Red text
  77. bi(g, "setBackground", 0x0000FF) -- Light blue background
  78. local textX = math.floor((boxWidth - #errorMessage) / 2) + boxX
  79. local textY = math.floor((boxHeight - 1) / 2) + boxY
  80. bi(g, "set", textX, textY, errorMessage)
  81.  
  82. computer.beep(800, 1) -- 1 second beep
  83. computer.beep(800, 0.5) -- Half-second beep
  84. computer.beep(800, 0.5) -- Half-second beep
  85. end
  86.  
  87. local function displayMenu(sI)
  88. local g = component.list("gpu")()
  89. local w, h = bi(g, "getResolution")
  90. local boxWidth, boxHeight = 60, #l + 4
  91. local boxX, boxY = math.floor((w - boxWidth) / 2), math.floor((h - boxHeight) / 2)
  92. local filteredList = {}
  93. for _, o in ipairs(l) do
  94. if o.label ~= "tmpfs" then
  95. table.insert(filteredList, o)
  96. end
  97. end
  98. if #filteredList == 0 then
  99. displayError("No bootable medium found!")
  100. return
  101. end
  102. bi(g, "setBackground", 0x0000FF) -- Light blue background
  103. bi(g, "fill", boxX, boxY, boxWidth, boxHeight, " ")
  104. bi(g, "setForeground", 0xFFFFFF)
  105. bi(g, "setBackground", 0x0000FF) -- Light blue background
  106. bi(g, "set", boxX + 2, boxY + 1, "Select OS to boot:")
  107. for i, o in ipairs(filteredList) do
  108. local lN = boxY + i + 2
  109. local tC = i == sI and 0xFF0000 or 0xFFFFFF
  110. bi(g, "setForeground", tC)
  111. bi(g, "set", boxX + 2, lN, o.label .. " (" .. o.address .. ")")
  112. end
  113. computer.beep(800, 0.1) -- Shorter beep
  114. end
  115.  
  116. local sI = math.ceil(#l / 2)
  117. displayMenu(sI)
  118. while true do
  119. local e = { computer.pullSignal() }
  120. if e[1] == "key_down" then
  121. if e[4] == 200 and sI > 1 then
  122. sI = sI - 1
  123. displayMenu(sI)
  124. elseif e[4] == 208 and sI < #l then
  125. sI = sI + 1
  126. displayMenu(sI)
  127. elseif e[4] == 28 then
  128. local o = l[sI]
  129. local i, r = tryLoad(o.address)
  130. if i then
  131. computer.setBootAddress(o.address)
  132. computer.beep(800, 0.2)
  133. computer.beep(800, 0.6)
  134. computer.beep(800, 0.2)
  135. return i()
  136. else
  137. displayError("Unable to boot device!")
  138. computer.beep(800, 0.1)
  139. computer.beep(800, 0.1)
  140. computer.beep(800, 0.1)
  141. end
  142. end
  143. end
  144. end
  145. end
  146.  
  147. -- Check for DOGGY.ROM and flash it to EEPROM if found
  148. local hasDoggyRom = false
  149. for _, drive in ipairs(l) do
  150. hasDoggyRom, err = bi(drive.address, "exists", "/DOGGY.ROM")
  151. if hasDoggyRom then
  152. local success, flashError = flashEEPROM(drive.address)
  153. if success then
  154. computer.beep(800, 0.2)
  155. computer.beep(800, 0.2)
  156. computer.beep(800, 0.2)
  157. bi(component.list("gpu")(), "set", 1, 1, "Flash complete!")
  158. computer.beep(800, 1) -- Beep once to indicate flash completion
  159. else
  160. bi(component.list("gpu")(), "set", 1, 1, "Flash failed: " .. flashError)
  161. computer.beep(800, 0.1)
  162. computer.beep(800, 0.1)
  163. computer.beep(800, 0.1)
  164. computer.beep(800, 1) -- Beep to indicate failure
  165. computer.shutdown(true) -- Halt the system
  166. end
  167. break
  168. end
  169. end
  170.  
  171. if not hasDoggyRom then
  172. bootloader() -- Call the bootloader function if DOGGY.ROM is not found
  173. end
  174. end
  175.  
  176. main()
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement