Advertisement
Guest User

Untitled

a guest
Aug 6th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.49 KB | None | 0 0
  1. -- OCLinux kernel by WattanaGaming
  2. -- Set up variables
  3. _G.boot_invoke = nil
  4. _G._KERNELNAME = "OCLinux"
  5. _G._KERNELVER = "0.2.5 beta"
  6. _G._KERNELVERSION = _KERNELNAME.." ".._KERNELVER
  7.  
  8. -- Load up basic libraries
  9. component = component or require('component')
  10. computer = computer or require('computer')
  11. unicode = unicode or require('unicode')
  12.  
  13. -- Start of the graphics section
  14. -- Get the GPU and the display
  15. gpu = component.proxy(component.list("gpu")())
  16. screen = component.list("screen")()
  17. -- Bind the screen to the GPU and get the screen resolution
  18. gpu.bind(screen)
  19. screenRes = {}
  20. screenRes.w, screenRes.h = gpu.getResolution()
  21. -- Display related functions
  22. -- This variable will come in handy for managing cursor position
  23. cursorPos = {
  24.     x = 1,
  25.     y = 1
  26. }
  27. function print(...)
  28.     for i in string.gmatch(tostring(...), "([^\r\n]+)") do
  29.         if cursorPos.y > screenRes.h then
  30.             -- Why the hell did they use Cartesian Coordinate? WHY?!
  31.             gpu.copy(1, 2, screenRes.w, screenRes.h-1, 0, -1)
  32.             gpu.setForeground( 0x000000)
  33.             gpu.fill(1, screenRes.h, screenRes.w, 1, " ")
  34.             gpu.setForeground(0xFFFFFF)
  35.             cursorPos.y = cursorPos.y - 1
  36.         end
  37.         gpu.set(cursorPos.x, cursorPos.y, tostring(i))
  38.         cursorPos.x = 1
  39.         cursorPos.y = cursorPos.y + 1
  40.     end
  41. end
  42.  
  43. function write(...)
  44.     for i in string.gmatch(tostring(...), "([^\r\n]+)") do
  45.         gpu.set(cursorPos.x, cursorPos.y, tostring(i))
  46.         cursorPos.x = string.len(...) + 1
  47.     end
  48. end
  49. -- End of the graphics section
  50.  
  51. -- Start of the filesystem section
  52. fs = {}
  53. fs.lowLevel = component.proxy(computer.getBootAddress())
  54. fs.filesystems = {}
  55. fs.mtab = {name="", children={}, links={}}
  56. fs.fstab = {}
  57.  
  58. function table.maxn(tab)
  59.     local i = 0
  60.     for k, v in pairs(tab) do
  61.         i = math.max(i, k)
  62.     end
  63.     return i
  64. end
  65.  
  66. function fs.segments(path)
  67.     local parts = {}
  68.     for part in path:gmatch("[^\\/]+") do
  69.         local current, up = part:find("^%.?%.$")
  70.         if current then
  71.             if up == 2 then
  72.                 table.remove(parts)
  73.             end
  74.         else
  75.             table.insert(parts, part)
  76.         end
  77.     end
  78.     return parts
  79. end
  80.  
  81. function fs.hasMountPoint(path)
  82.     return fs.getNode(path) ~= nil
  83. end
  84.  
  85. function fs.canonical(path)
  86.     local result = table.concat(segments(path), "/")
  87.     if unicode.sub(path, 1, 1) == "/" then
  88.         return "/" .. result
  89.     else
  90.         return result
  91.     end
  92. end
  93.  
  94. function fs.concat(...)
  95.     local set = table.pack(...)
  96.     for index, value in ipairs(set) do
  97.         checkArg(index, value, "string")
  98.     end
  99.     return fs.canonical(table.concat(set, "/"))
  100. end
  101.  
  102. function fs.mount(path, fs)
  103.     --[[
  104.     if fs.hasMountPoint(path) then
  105.         error(path.." is already mounted")
  106.     end
  107.     ]]
  108.     fs.filesystem[#fs.filesystems+1] = {path,fs}
  109. end
  110.  
  111. function fs.localPath(node, path)
  112.     local localPath = path:gsub(node.path.."/", "")
  113.     return localPath
  114. end
  115.  
  116. function fs.getDrives(automount)
  117.     local tab = {}
  118.     -- TODO add support for drives when an standardized unmanaged filesystem is made for OC
  119.     local i = 0
  120.     for k, v in pairs(component.list("filesystem")) do
  121.         -- uncorrect naming due to not knowing if it's floppy or disk drive
  122.         table.insert(tab, "/dev/" .. "hd".. string.char(string.byte('a') + i) .. "/")
  123.         if automount then
  124.             if not fs.contains(component.proxy(v)) then
  125.                 fs.mount("/dev/" .. "hd".. string.char(string.byte('a') + i) .. "/", component.proxy(v))
  126.             end
  127.         end
  128.     end
  129. end
  130.  
  131. function fs.exists(path)
  132.     local node = fs.findNode(path)
  133.     if node == nil then
  134.         return false
  135.     end
  136.     local lp = fs.localPath(node, path)
  137.     return node.exists(lp)
  138. end
  139.  
  140. -- Also has compatibility for older versions
  141. function fs.isReadOnly(path)
  142.     local node = fs.findNode(path)
  143.     if node == nil then
  144.         return false
  145.     end
  146.     local lp = fs.localPath(node, path)
  147.     if node.isReadOnly then
  148.         return node.isReadOnly(lp)
  149.     else
  150.         return false
  151.     end
  152. end
  153.  
  154. function fs.getFile(path, mode)
  155.     local node fs.findNode(path)
  156.     local lp = fs.localPath(node, path)
  157.     local file = {}
  158.     file.handle = node.open(lp, mode)
  159.     file.seek = function(whence, offset)
  160.         return node.seek(file.handle, whence, offset)
  161.     end
  162.     file.write = function(value)
  163.         return node.write(file.handle, value)
  164.     end
  165.     file.read = function(amount)
  166.         return node.read(file.handle, amount)
  167.     end
  168.     file.size = function()
  169.         return node.size(lp)
  170.     end
  171.     file.close = function()
  172.         node.close(file.handle)
  173.     end
  174.     return file
  175. end
  176.  
  177. function fs.size(path)
  178.     local node = fs.findNode(path)
  179.     if node == nil then
  180.         return -1
  181.     end
  182.     local lp = fs.localPath(node, path)
  183.     return node.size(lp)
  184. end
  185.  
  186. function fs.findNode(path)
  187.     local lastPath = ""
  188.     local lastNode = nil
  189.     local seg = fs.segments(path)
  190.     for k, v in pairs(seg) do
  191.         if k < table.maxn(seg) then
  192.             lastPath = lastPath..v.."/"
  193.         else
  194.             lastPath = lastPath..v
  195.         end
  196.         local node = fs.getNode(lastPath)
  197.         if node ~= nil then
  198.             lastNode = node
  199.             node.path = lastPath
  200.         end
  201.     end
  202.     return lastNode
  203. end
  204.  
  205. function fs.getNode(mountPath)
  206.     for k, v in pairs(fs.filesystems) do
  207.         local p = v[1]
  208.         if p == mountPath then -- if is same path
  209.             p = mountPath
  210.             print(p)
  211.             return p
  212.         end
  213.     end
  214.     return nil
  215. end
  216.  
  217. function fs.contains(node)
  218.     for k, v in pairs(filesystems) do
  219.         local p = v[2]
  220.         if p == node then
  221.             return true
  222.         end
  223.     end
  224.     return false
  225. end
  226.  
  227. -- Start of the kernel specific section
  228. kernel = {}
  229.  
  230. -- Get the boot time until kernel in case there is a bootloader delay
  231. kernel.bootTime = computer.uptime()
  232. -- Get the boot address
  233. kernel.bootFs = computer.getBootAddress()
  234.  
  235. function kernel.execInit(init)
  236.     write("Looking for init \""..init.."\"....    ")
  237.     if fs.exists(kernel.bootFs, init) then
  238.         print("Init found!")
  239.         initHandle = fs.open(init, "r")
  240.         initC = fs.read(init, initHandle)
  241.         -- Zenith's error detection
  242.         local v, err = pcall(function()
  243.             load(initC, "=" .. init, nil, _G)()
  244.         end)
  245.         if not v then
  246.             kernel.panic("An error occured during execution of "..init, err)
  247.         end
  248.         return true
  249.     else
  250.         print("Not here")
  251.     end
  252. end
  253.  
  254. function kernel.panic(reason, traceback)
  255.     if not reason then
  256.         reason = "Not specified"
  257.     end
  258.     if not traceback then
  259.         traceback = "None"
  260.     end
  261.     --  Zenith's tweak of the kernel panic error
  262.     print("Kernel Panic!!")
  263.     print("  Reason: " .. reason)
  264.     print("  Traceback: "..traceback)
  265.     print("  Kernel version: ".. _KERNELVER)
  266.     print("  System uptime: ".. computer.uptime() - kernel.bootTime)
  267.     print("System halted.")
  268.     computer.beep(1000, 0.75)
  269.     while true do
  270.         computer.pullSignal()
  271.     end
  272. end
  273.  
  274. -- Mount the boot device
  275. fs.mount("/", kernel.bootFs)
  276.  
  277. if not kernel.execInit("/sbin/init.lua") and not kernel.execInit("/etc/init.lua") and not kernel.execInit("/bin/init.lua") then
  278.     kernel.panic("Init not found. You are on your own now, good luck!")
  279. end
  280.  
  281. -- Halt the system, everything should be ok if there is no BSoD
  282. kernel.panic("Init returned")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement