DOGGYWOOF

Untitled

Jul 23rd, 2024
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. -- BIOS environment for Open OS Recovery
  2.  
  3. -- Function to print a message to the screen
  4. local function printMessage(message)
  5. local gpu = component.list("gpu")() -- Get the GPU component
  6. local screen = component.list("screen")() -- Get the screen component
  7. if gpu and screen then
  8. component.invoke(gpu, "bind", screen) -- Bind GPU to screen
  9. -- Split the message into lines and print each line
  10. local lines = {}
  11. for line in message:gmatch("[^\r\n]+") do
  12. table.insert(lines, line)
  13. end
  14. local x, y = 1, 1
  15. for _, line in ipairs(lines) do
  16. component.invoke(gpu, "set", x, y, line)
  17. y = y + 1
  18. end
  19. else
  20. print("No GPU or screen found.")
  21. end
  22. end
  23.  
  24. -- Function to clear the screen
  25. local function clearScreen()
  26. local gpu = component.list("gpu")()
  27. if gpu then
  28. component.invoke(gpu, "setBackground", 0x000000)
  29. component.invoke(gpu, "setForeground", 0xFFFFFF)
  30. component.invoke(gpu, "fill", 1, 1, 80, 25, " ")
  31. end
  32. end
  33.  
  34. -- Function to display the menu
  35. local function showMenu()
  36. clearScreen()
  37. printMessage(
  38. "Open OS Recovery Environment\n" ..
  39. "1. Boot Open OS disk - run boot code\n" ..
  40. "2. Backup User data - copy /home/ data of Open OS disk to the Recovery drive\n" ..
  41. "3. Restore - restore the /home/ dir of Recovery drive to Open OS disk\n" ..
  42. "Select an option (1/2/3): "
  43. )
  44. end
  45.  
  46. -- Function to read user input
  47. local function readInput()
  48. local input = ""
  49. local address = component.list("keyboard")()
  50. if address then
  51. while true do
  52. local event, _, _, code = computer.pullSignal()
  53. if event == "key_down" then
  54. if code == 28 then -- Enter key
  55. return input
  56. elseif code == 2 then -- '1' key
  57. return "1"
  58. elseif code == 3 then -- '2' key
  59. return "2"
  60. elseif code == 4 then -- '3' key
  61. return "3"
  62. end
  63. end
  64. end
  65. end
  66. return ""
  67. end
  68.  
  69. -- Function to boot Open OS disk
  70. local function bootOpenOS()
  71. clearScreen()
  72. printMessage("Booting Open OS disk...\n")
  73. local eeprom = component.list("eeprom")()
  74. if eeprom then
  75. local address = component.invoke(eeprom, "getData")
  76. if address then
  77. printMessage("Boot address found: " .. address .. "\n")
  78. -- Simulate booting from the address
  79. computer.beep(1000, 0.1)
  80. else
  81. printMessage("No boot address found.\n")
  82. end
  83. else
  84. printMessage("EEPROM component not found.\n")
  85. end
  86. printMessage("Press Enter to return to the menu.\n")
  87. readInput()
  88. end
  89.  
  90. -- Function to backup user data
  91. local function backupUserData()
  92. clearScreen()
  93. printMessage("Backing up user data...\n")
  94.  
  95. local openOSDiskAddress = component.list("filesystem")() -- Find Open OS disk
  96. local recoveryDriveAddress = component.list("filesystem")() -- Find Recovery drive
  97.  
  98. if openOSDiskAddress and recoveryDriveAddress then
  99. local openOSDisk = component.proxy(openOSDiskAddress)
  100. local recoveryDrive = component.proxy(recoveryDriveAddress)
  101.  
  102. local sourceDir = "/home/"
  103. local destDir = "/Recovery/"
  104.  
  105. local function copyDir(src, dest, srcFS, destFS)
  106. for file in srcFS.list(src) do
  107. local sourcePath = src .. file
  108. local destPath = dest .. file
  109. if srcFS.isDirectory(sourcePath) then
  110. -- Create the directory in the destination
  111. if not destFS.isDirectory(destPath) then
  112. destFS.makeDirectory(destPath)
  113. end
  114. copyDir(sourcePath .. "/", destPath .. "/", srcFS, destFS) -- Recursively copy subdirectories
  115. else
  116. -- Copy file
  117. local sourceFile = srcFS.open(sourcePath, "r")
  118. if sourceFile then
  119. local data = sourceFile.read(math.huge)
  120. sourceFile.close()
  121. local destFile = destFS.open(destPath, "w")
  122. if destFile then
  123. destFile.write(data)
  124. destFile.close()
  125. else
  126. printMessage("Failed to open destination file: " .. destPath .. "\n")
  127. end
  128. else
  129. printMessage("Failed to open source file: " .. sourcePath .. "\n")
  130. end
  131. end
  132. end
  133. end
  134.  
  135. copyDir(sourceDir, destDir, openOSDisk, recoveryDrive)
  136. printMessage("Backup completed successfully.\n")
  137. else
  138. printMessage("Required components for backup not found.\n")
  139. end
  140. printMessage("Press Enter to return to the menu.\n")
  141. readInput()
  142. end
  143.  
  144. -- Function to restore user data
  145. local function restoreUserData()
  146. clearScreen()
  147. printMessage("Restoring user data...\n")
  148.  
  149. local openOSDiskAddress = component.list("filesystem")() -- Find Open OS disk
  150. local recoveryDriveAddress = component.list("filesystem")() -- Find Recovery drive
  151.  
  152. if openOSDiskAddress and recoveryDriveAddress then
  153. local openOSDisk = component.proxy(openOSDiskAddress)
  154. local recoveryDrive = component.proxy(recoveryDriveAddress)
  155.  
  156. local sourceDir = "/Recovery/"
  157. local destDir = "/home/"
  158.  
  159. local function copyDir(src, dest, srcFS, destFS)
  160. for file in srcFS.list(src) do
  161. local sourcePath = src .. file
  162. local destPath = dest .. file
  163. if srcFS.isDirectory(sourcePath) then
  164. -- Create the directory in the destination
  165. if not destFS.isDirectory(destPath) then
  166. destFS.makeDirectory(destPath)
  167. end
  168. copyDir(sourcePath .. "/", destPath .. "/", srcFS, destFS) -- Recursively copy subdirectories
  169. else
  170. -- Copy file
  171. local sourceFile = srcFS.open(sourcePath, "r")
  172. if sourceFile then
  173. local data = sourceFile.read(math.huge)
  174. sourceFile.close()
  175. local destFile = destFS.open(destPath, "w")
  176. if destFile then
  177. destFile.write(data)
  178. destFile.close()
  179. else
  180. printMessage("Failed to open destination file: " .. destPath .. "\n")
  181. end
  182. else
  183. printMessage("Failed to open source file: " .. sourcePath .. "\n")
  184. end
  185. end
  186. end
  187. end
  188.  
  189. copyDir(sourceDir, destDir, recoveryDrive, openOSDisk)
  190. printMessage("Restore completed successfully.\n")
  191. else
  192. printMessage("Required components for restore not found.\n")
  193. end
  194. printMessage("Press Enter to return to the menu.\n")
  195. readInput()
  196. end
  197.  
  198. -- Main program loop
  199. while true do
  200. showMenu()
  201. local choice = readInput()
  202.  
  203. if choice == "1" then
  204. bootOpenOS()
  205. elseif choice == "2" then
  206. backupUserData()
  207. elseif choice == "3" then
  208. restoreUserData()
  209. else
  210. clearScreen()
  211. printMessage("Invalid option, please try again.\n")
  212. end
  213.  
  214. printMessage("Press Enter to return to the menu.\n")
  215. readInput()
  216. end
  217.  
Add Comment
Please, Sign In to add comment