Advertisement
DOGGYWOOF

BIOS

Mar 18th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. local init
  2. do
  3. local component_invoke = component.invoke
  4. local function boot_invoke(address, method, ...)
  5. local result = table.pack(pcall(component_invoke, address, method, ...))
  6. if not result[1] then
  7. return nil, result[2]
  8. else
  9. return table.unpack(result, 2, result.n)
  10. end
  11. end
  12.  
  13. -- Function to display a boot menu
  14. local function displayBootMenu()
  15. print("Available boot options:")
  16. local bootOptions = {}
  17. local i = 1
  18. for address in component.list("filesystem") do
  19. bootOptions[i] = address
  20. print(i .. ". " .. address)
  21. i = i + 1
  22. end
  23.  
  24. -- Prompt the user for choice
  25. local choice
  26. repeat
  27. io.write("Enter the number corresponding to your choice: ")
  28. choice = tonumber(io.read())
  29. until choice and bootOptions[choice]
  30.  
  31. return bootOptions[choice]
  32. end
  33.  
  34. -- Backwards compatibility for EEPROM boot address
  35. local eeprom = component.list("eeprom")()
  36. computer.getBootAddress = function()
  37. return boot_invoke(eeprom, "getData")
  38. end
  39. computer.setBootAddress = function(address)
  40. return boot_invoke(eeprom, "setData", address)
  41. end
  42.  
  43. -- Bind GPU to screen if available
  44. do
  45. local screen = component.list("screen")()
  46. local gpu = component.list("gpu")()
  47. if gpu and screen then
  48. boot_invoke(gpu, "bind", screen)
  49. end
  50. end
  51.  
  52. local function tryLoadFrom(address)
  53. local handle, reason = boot_invoke(address, "open", "/init.lua")
  54. if not handle then
  55. return nil, reason
  56. end
  57. local buffer = ""
  58. repeat
  59. local data, reason = boot_invoke(address, "read", handle, math.maxinteger or math.huge)
  60. if not data and reason then
  61. return nil, reason
  62. end
  63. buffer = buffer .. (data or "")
  64. until not data
  65. boot_invoke(address, "close", handle)
  66. return load(buffer, "=init")
  67. end
  68.  
  69. -- Attempt to load custom boot file
  70. local reason
  71. if computer.getBootAddress() then
  72. init, reason = tryLoadFrom(computer.getBootAddress())
  73. end
  74. if not init then
  75. local selectedAddress = displayBootMenu()
  76. if selectedAddress then
  77. init, reason = tryLoadFrom(selectedAddress)
  78. if init then
  79. computer.setBootAddress(selectedAddress)
  80. end
  81. end
  82. end
  83.  
  84. -- Error handling
  85. if not init then
  86. error("Cannot Boot OS: " .. (reason or "Unknown error"))
  87. end
  88.  
  89. computer.beep(1000, 0.10) -- Beep to indicate successful boot
  90. end
  91.  
  92. return init()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement