Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function main()
- 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
- 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
- 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
- -- Get a list of bootable drives and their names
- local osList = {}
- for address in component.list("filesystem") do
- local label = boot_invoke(address, "getLabel") or "Unnamed Drive"
- table.insert(osList, {address = address, label = label})
- end
- -- Function to display boot menu
- local function displayBootMenu(selectedIndex)
- local gpu = component.list("gpu")()
- local w, h = boot_invoke(gpu, "getResolution")
- boot_invoke(gpu, "fill", 1, 1, w, h, " ") -- Clear screen
- boot_invoke(gpu, "setForeground", 0xFFFFFF) -- Set text color to white
- boot_invoke(gpu, "set", 1, 1, "Select OS to boot:")
- for i, os in ipairs(osList) do
- local line = i + 1
- local textColor = i == selectedIndex and 0xFF0000 or 0xFFFFFF
- boot_invoke(gpu, "setForeground", textColor)
- boot_invoke(gpu, "set", 1, line, os.label .. " (" .. os.address .. ")")
- end
- computer.beep(1000, 0.10) -- Beep when boot menu appears
- end
- -- Main boot menu logic
- local selectedIndex = math.ceil(#osList / 2) -- Select middle OS initially
- displayBootMenu(selectedIndex)
- while true do
- local event = {computer.pullSignal()}
- if event[1] == "key_down" then
- if event[4] == 200 and selectedIndex > 1 then -- Arrow up
- selectedIndex = selectedIndex - 1
- displayBootMenu(selectedIndex)
- elseif event[4] == 208 and selectedIndex < #osList then -- Arrow down
- selectedIndex = selectedIndex + 1
- displayBootMenu(selectedIndex)
- elseif event[4] == 28 then -- Enter key
- local selectedOS = osList[selectedIndex]
- local init, reason = tryLoadFrom(selectedOS.address)
- if init then
- computer.setBootAddress(selectedOS.address)
- computer.beep(1500, 0.10) -- Beep when booting into the operating system
- return init() -- Return the result of running loaded code
- else
- print("Failed to boot from " .. selectedOS.label .. ": " .. tostring(reason))
- end
- end
- end
- end
- end
- main() -- Call the main function to execute the boot process
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement