Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- BIOS environment for Open OS Recovery
- -- Function to print a message to the screen
- local function printMessage(message)
- local gpu = component.list("gpu")() -- Get the GPU component
- local screen = component.list("screen")() -- Get the screen component
- if gpu and screen then
- component.invoke(gpu, "bind", screen) -- Bind GPU to screen
- -- Split the message into lines and print each line
- local lines = {}
- for line in message:gmatch("[^\r\n]+") do
- table.insert(lines, line)
- end
- local x, y = 1, 1
- for _, line in ipairs(lines) do
- component.invoke(gpu, "set", x, y, line)
- y = y + 1
- end
- else
- print("No GPU or screen found.")
- end
- end
- -- Function to clear the screen
- local function clearScreen()
- local gpu = component.list("gpu")()
- if gpu then
- component.invoke(gpu, "setBackground", 0x000000)
- component.invoke(gpu, "setForeground", 0xFFFFFF)
- component.invoke(gpu, "fill", 1, 1, 80, 25, " ")
- end
- end
- -- Function to display the menu
- local function showMenu()
- clearScreen()
- printMessage(
- "Open OS Recovery Environment\n" ..
- "1. Boot Open OS disk - run boot code\n" ..
- "2. Backup User data - copy /home/ data of Open OS disk to the Recovery drive\n" ..
- "3. Restore - restore the /home/ dir of Recovery drive to Open OS disk\n" ..
- "Select an option (1/2/3): "
- )
- end
- -- Function to read user input
- local function readInput()
- local input = ""
- local address = component.list("keyboard")()
- if address then
- while true do
- local event, _, _, code = computer.pullSignal()
- if event == "key_down" then
- if code == 28 then -- Enter key
- return input
- elseif code == 2 then -- '1' key
- return "1"
- elseif code == 3 then -- '2' key
- return "2"
- elseif code == 4 then -- '3' key
- return "3"
- end
- end
- end
- end
- return ""
- end
- -- Function to boot Open OS disk
- local function bootOpenOS()
- clearScreen()
- printMessage("Booting Open OS disk...\n")
- local eeprom = component.list("eeprom")()
- if eeprom then
- local address = component.invoke(eeprom, "getData")
- if address then
- printMessage("Boot address found: " .. address .. "\n")
- -- Simulate booting from the address
- computer.beep(1000, 0.1)
- else
- printMessage("No boot address found.\n")
- end
- else
- printMessage("EEPROM component not found.\n")
- end
- printMessage("Press Enter to return to the menu.\n")
- readInput()
- end
- -- Function to backup user data
- local function backupUserData()
- clearScreen()
- printMessage("Backing up user data...\n")
- local openOSDiskAddress = component.list("filesystem")() -- Find Open OS disk
- local recoveryDriveAddress = component.list("filesystem")() -- Find Recovery drive
- if openOSDiskAddress and recoveryDriveAddress then
- local openOSDisk = component.proxy(openOSDiskAddress)
- local recoveryDrive = component.proxy(recoveryDriveAddress)
- local sourceDir = "/home/"
- local destDir = "/Recovery/"
- local function copyDir(src, dest, srcFS, destFS)
- for file in srcFS.list(src) do
- local sourcePath = src .. file
- local destPath = dest .. file
- if srcFS.isDirectory(sourcePath) then
- -- Create the directory in the destination
- if not destFS.isDirectory(destPath) then
- destFS.makeDirectory(destPath)
- end
- copyDir(sourcePath .. "/", destPath .. "/", srcFS, destFS) -- Recursively copy subdirectories
- else
- -- Copy file
- local sourceFile = srcFS.open(sourcePath, "r")
- if sourceFile then
- local data = sourceFile.read(math.huge)
- sourceFile.close()
- local destFile = destFS.open(destPath, "w")
- if destFile then
- destFile.write(data)
- destFile.close()
- else
- printMessage("Failed to open destination file: " .. destPath .. "\n")
- end
- else
- printMessage("Failed to open source file: " .. sourcePath .. "\n")
- end
- end
- end
- end
- copyDir(sourceDir, destDir, openOSDisk, recoveryDrive)
- printMessage("Backup completed successfully.\n")
- else
- printMessage("Required components for backup not found.\n")
- end
- printMessage("Press Enter to return to the menu.\n")
- readInput()
- end
- -- Function to restore user data
- local function restoreUserData()
- clearScreen()
- printMessage("Restoring user data...\n")
- local openOSDiskAddress = component.list("filesystem")() -- Find Open OS disk
- local recoveryDriveAddress = component.list("filesystem")() -- Find Recovery drive
- if openOSDiskAddress and recoveryDriveAddress then
- local openOSDisk = component.proxy(openOSDiskAddress)
- local recoveryDrive = component.proxy(recoveryDriveAddress)
- local sourceDir = "/Recovery/"
- local destDir = "/home/"
- local function copyDir(src, dest, srcFS, destFS)
- for file in srcFS.list(src) do
- local sourcePath = src .. file
- local destPath = dest .. file
- if srcFS.isDirectory(sourcePath) then
- -- Create the directory in the destination
- if not destFS.isDirectory(destPath) then
- destFS.makeDirectory(destPath)
- end
- copyDir(sourcePath .. "/", destPath .. "/", srcFS, destFS) -- Recursively copy subdirectories
- else
- -- Copy file
- local sourceFile = srcFS.open(sourcePath, "r")
- if sourceFile then
- local data = sourceFile.read(math.huge)
- sourceFile.close()
- local destFile = destFS.open(destPath, "w")
- if destFile then
- destFile.write(data)
- destFile.close()
- else
- printMessage("Failed to open destination file: " .. destPath .. "\n")
- end
- else
- printMessage("Failed to open source file: " .. sourcePath .. "\n")
- end
- end
- end
- end
- copyDir(sourceDir, destDir, recoveryDrive, openOSDisk)
- printMessage("Restore completed successfully.\n")
- else
- printMessage("Required components for restore not found.\n")
- end
- printMessage("Press Enter to return to the menu.\n")
- readInput()
- end
- -- Main program loop
- while true do
- showMenu()
- local choice = readInput()
- if choice == "1" then
- bootOpenOS()
- elseif choice == "2" then
- backupUserData()
- elseif choice == "3" then
- restoreUserData()
- else
- clearScreen()
- printMessage("Invalid option, please try again.\n")
- end
- printMessage("Press Enter to return to the menu.\n")
- readInput()
- end
Add Comment
Please, Sign In to add comment