Advertisement
DOGGYWOOF

Doggy OS bootloader

Apr 6th, 2024 (edited)
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. local term = term
  2. local fs = fs
  3. local colors = colors
  4.  
  5. -- Function to check if a directory exists
  6. local function directoryExists(path)
  7. return fs.isDir(path)
  8. end
  9.  
  10. -- Function to check if a file exists
  11. local function fileExists(path)
  12. return fs.exists(path) and not fs.isDir(path)
  13. end
  14.  
  15. -- Function to list all bootable drives
  16. local function listBootableDrives()
  17. local drives = {}
  18. for _, side in ipairs(rs.getSides()) do
  19. local path = side.."/disk/bootloader/VA11-ILLA.lua"
  20. if directoryExists(path) then
  21. table.insert(drives, side)
  22. end
  23. end
  24. return drives
  25. end
  26.  
  27. -- Function to list installed operating systems on a drive
  28. local function listInstalledOS(drive)
  29. local osList = {}
  30. if directoryExists(drive.."/disk/") then
  31. local files = fs.list(drive.."/disk/")
  32. for _, file in ipairs(files) do
  33. if file:match("%.Boot%d$") then
  34. table.insert(osList, file)
  35. end
  36. end
  37. end
  38. return osList
  39. end
  40.  
  41. -- Function to display boot menu
  42. local function displayBootMenu()
  43. term.clear()
  44. term.setCursorPos(1, 1)
  45. print("UEFI Boot Menu")
  46. print("Select an OS using arrow keys and press Enter to boot.")
  47. print("Use arrow keys to navigate, press 'q' to exit.")
  48. print()
  49.  
  50. local drives = listBootableDrives()
  51. for i, drive in ipairs(drives) do
  52. term.setTextColor(colors.green)
  53. term.write(drive.."/disk")
  54. term.setTextColor(colors.white)
  55. local osList = listInstalledOS(drive)
  56. for _, os in ipairs(osList) do
  57. term.write(" "..os)
  58. if i == selectedDriveIndex and os == selectedOS then
  59. term.write(" <")
  60. end
  61. print()
  62. end
  63. end
  64. end
  65.  
  66. -- Function to handle key input
  67. local function handleInput()
  68. while true do
  69. local event, key = os.pullEvent("key")
  70. if key == keys.q then
  71. return false
  72. elseif key == keys.enter then
  73. if selectedDriveIndex and selectedOS then
  74. local selectedDrive = listBootableDrives()[selectedDriveIndex]
  75. fs.copy(selectedDrive.."/disk"..selectedOS, selectedDrive)
  76. return true
  77. end
  78. elseif key == keys.up or key == keys.down then
  79. local drives = listBootableDrives()
  80. if #drives > 0 then
  81. local numOS = #listInstalledOS(drives[selectedDriveIndex])
  82. if key == keys.up then
  83. selectedOSIndex = (selectedOSIndex - 1) % numOS + 1
  84. elseif key == keys.down then
  85. selectedOSIndex = (selectedOSIndex + 1) % numOS + 1
  86. end
  87. end
  88. end
  89. end
  90. end
  91.  
  92. -- Main function
  93. local function main()
  94. while true do
  95. displayBootMenu()
  96. if not handleInput() then
  97. break
  98. end
  99. end
  100. term.clear()
  101. term.setCursorPos(1, 1)
  102. print("Exiting UEFI Boot Menu")
  103. end
  104.  
  105. -- Start the program
  106. main()
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement