Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local init
- do
- local component_invoke = component.invoke
- local function boot_invoke(address, method, ...)
- local result = table.pack(pcall(component_invoke, address, method, ...))
- if not result[1] then
- return nil, result[2]
- else
- return table.unpack(result, 2, result.n)
- end
- end
- -- Function to display a boot menu
- local function displayBootMenu()
- print("Available boot options:")
- local bootOptions = {}
- local i = 1
- for address in component.list("filesystem") do
- bootOptions[i] = address
- print(i .. ". " .. address)
- i = i + 1
- end
- -- Prompt the user for choice
- local choice
- repeat
- io.write("Enter the number corresponding to your choice: ")
- choice = tonumber(io.read())
- until choice and bootOptions[choice]
- return bootOptions[choice]
- end
- -- Backwards compatibility for EEPROM boot address
- local eeprom = component.list("eeprom")()
- computer.getBootAddress = function()
- return boot_invoke(eeprom, "getData")
- end
- computer.setBootAddress = function(address)
- return boot_invoke(eeprom, "setData", address)
- end
- -- Bind GPU to screen if available
- do
- local screen = component.list("screen")()
- local gpu = component.list("gpu")()
- if gpu and screen then
- boot_invoke(gpu, "bind", screen)
- end
- end
- local function tryLoadFrom(address)
- local handle, reason = boot_invoke(address, "open", "/init.lua")
- if not handle then
- return nil, reason
- end
- local buffer = ""
- repeat
- local data, reason = boot_invoke(address, "read", handle, math.maxinteger or math.huge)
- if not data and reason then
- return nil, reason
- end
- buffer = buffer .. (data or "")
- until not data
- boot_invoke(address, "close", handle)
- return load(buffer, "=init")
- end
- -- Attempt to load custom boot file
- local reason
- if computer.getBootAddress() then
- init, reason = tryLoadFrom(computer.getBootAddress())
- end
- if not init then
- local selectedAddress = displayBootMenu()
- if selectedAddress then
- init, reason = tryLoadFrom(selectedAddress)
- if init then
- computer.setBootAddress(selectedAddress)
- end
- end
- end
- -- Error handling
- if not init then
- error("Cannot Boot OS: " .. (reason or "Unknown error"))
- end
- computer.beep(1000, 0.10) -- Beep to indicate successful boot
- end
- return init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement