awefs/files/kernel
2025-02-02 05:30:04 -05:00

380 lines
7.9 KiB
Plaintext
Executable File

_G.kapi = {}
kapi.version = "5-devel_1-22-25-AWEFS"
local component = component
local computer = computer
local unicode = unicode
_G.component = nil
_G.computer = nil
_G.unicode = nil
if not screen_x then
_G.screen_x = 1
end
if not screen_y then
_G.screen_y = 1
end
function printk(msg)
if gpu then
local a, b = pcall(gpu.set, 1, screen_y, msg)
if not a then
end
if screen_y == h then
gpu.copy(1, 2, w, h - 1, 0, -1)
gpu.fill(1, h, w, 1, " ")
else
screen_y = screen_y + 1
end
end
end
kapi.printk = printk
printk("Kernel " .. kapi.version)
function kapi.printsysinfo()
local freemem = computer.freeMemory()
local totalmem = computer.totalMemory()
print("System information\n\r")
print(" /\\_/\\ Memory " .. totalmem .. "B\n\r")
print(" ( ^ ^ ) Free " .. freemem .. "b\n\r")
print(" \\ w / Used " .. totalmem - freemem .. "b\n\r")
print(" Uptime " .. computer.uptime() .. "S\n\r")
print(" Arch. " .. computer.getArchitecture() .. "\n\r")
print(" Rootdisk " .. _G.BootAddress .."\n\n\r")
print(" Kernel " .. kapi.version .."\n\r")
end
function kapi.panic(message, trace)
if _G._KFLAGS and _G._KFLAGS._PANIC_IS_ERROR then
error(message)
else
if _G.gpu ~= nil then
if gpu_driver.set_bg ~= nil then
gpu_driver.set_bg(0xff5555)
gpu_driver.set_fg(0xffffff)
screen_y = 1
screen_x = 1
print("Kernel Panic\r\n")
print("Kernel version " .. kapi.version .. "\r\n")
kapi.printsysinfo()
if trace ~= nil then
print(trace .. "\r\n")
else
print(debug.traceback() .. "\r\n")
end
print(message .. "\r\n")
print("Kernel Panic\r\n")
end
end
computer.beep(200, 0.25)
computer.beep(300, 0.25)
computer.beep(400, 0.25)
computer.beep(500, 0.25)
waitsleep(2)
computer.beep(400, 0.1)
computer.beep(500, 0.1)
computer.beep(500, 0.1)
computer.beep(200, 0.1)
hcf()
end
end
kapi.loadfile = loadfile
function kapi.dofile(path)
local program, reason = kapi.loadfile(path)
if program then
local result = table.pack(pcall(program))
if result[1] then
program = nil
reason = nil
return table.unpack(result, 2, result.n)
else
kapi.panic(result[2], debug.traceback())
end
else
kapi.panic(reason, debug.traceback())
end
end
function kapi.dprintk(m)
if _G._KFLAGS._KERNEL_DEBUG then
if _G.gpu then
set_gpu_fg(0x6666ff)
set_gpu_bg(0x000000)
printk(m)
set_gpu_fg(0xffffff)
end
end
end
function kapi.rom_invoke(root_key, method, ...)
if _G.BootAddress ~= "N/A" then
return component.invoke(_G.BootAddress, method, ...)
else
panic("Boot address is N/A", debug.traceback())
end
end
local w, h
local screen
local gpu
_G.gpu_driver = {}
_G.screen_driver = {}
function gpu_driver.init()
screen = component.list("screen", true)()
gpu = screen and component.list("gpu", true)()
if gpu and screen then
gpu = component.proxy(gpu)
_G.gpu = gpu
if not gpu.getScreen() then
gpu.bind(screen)
end
_G.boot_screen = gpu.getScreen()
w, h = gpu.maxResolution()
_G.screen_width = w
_G.screen_height = h
gpu.setResolution(w, h)
gpu.setBackground(0x000000)
gpu.setForeground(0xFFFFFF)
if screen_y == nil then
gpu.fill(1, 1, w, h, " ")
end
end
end
gpu_driver.init()
function gpu_driver.clearscr()
if gpu then
gpu.setResolution(w, h)
gpu.setBackground(0x000000)
gpu.setForeground(0xFFFFFF)
gpu.fill(1, 1, w, h, " ")
end
end
function gpu_driver.set_bg(color)
if gpu then
gpu.setBackground(color)
end
end
function gpu_driver.set_fg(color)
if gpu then
gpu.setForeground(color)
end
end
function gpu_driver.scroll()
if gpu then
gpu.copy(1, 2, w, h - 1, 0, -1)
gpu.fill(1, h, w, 1, " ")
end
screen_y = screen_height - 1
end
function gpu_driver.newln()
screen_y = screen_y + 1
if screen_y == screen_height then
gpu_driver.scroll()
else
screen_x = screen_x + 1
end
end
function gpu_driver.carriageret()
screen_x = 1
end
function print(msg)
if gpu then
if msg ~= "string" then
msg = tostring(msg)
end
for i = 1, #msg do
local c = msg:sub(i, i)
if c == "\n" then
gpu_driver.newln()
elseif c == "\09" then
gpu_driver.carriageret()
screen_x = screen_x + 8
elseif c == "\r" then
gpu_driver.carriageret()
else
if screen_x == screen_width then
screen_x = 1
screen_y = screen_y + 1
end
gpu.set(screen_x, screen_y, c)
end
if screen_y == screen_height then
gpu_driver.scroll()
else
screen_x = screen_x + 1
end
end
end
return
end
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
if _KFLAGS._MODULES_ALLOWED then
local list = kapi.loadfile(_KFLAGS._MODULE_DIR .. "enabled.list")()
if list == nil then
printk("no " .. _KFLAGS._MODULE_DIR .. "enabled.list file")
else
for i in ipairs(list) do
kapi.loadfile(_KFLAGS._MODULE_DIR .. list[i])()
end
end
end
printk("component lib")
function component.isAvailable(componentType)
if component[componentType] then
return true
end
return false
end
function component.get(address, componentType)
local comp = component.list(componentType)
for a,_ in comp do
local s,e = string.find(a,address)
if s == 1 then
return a
end
end
return nil, "no such component"
end
function component.getPrimary(componentType)
local prim = component[componentType]
if not prim then
error(string.format("no primary '%s' available", componentType))
end
return prim
end
function component.setPrimary(componentType, address)
if componentType == "filesystem" then
return
end
address = component.get(address:sub(1,5), componentType) -- don't sub
component[componentType] = component.proxy(address)
end
local components = component.list()
printk("Setting primary components")
for address, componentType in components do
component.setPrimary(componentType,address)
end
local loaded = {
["_G"] = _G,
["bit32"] = bit32,
["coroutine"] = coroutine,
["math"] = math,
["os"] = os,
["package"] = package,
["string"] = string,
["table"] = table,
["computer"] = computer,
["component"] = component,
["unicode"] = unicode
}
function unload_module(module)
if loaded[module] ~= nil then
loaded[module] = nil
end
return
end
function require(module)
if loaded[module] then
return loaded[module]
end
local library, status, arg
library = raw_loadfile("/lib/" .. module ..".lua")
library, status = pcall(library, arg or module)
assert(library, string.format("module '%s' load failed:\n%s", module, status))
loaded[module] = status
return status
end
function requirepath(module)
if loaded[module] then
return loaded[module]
end
local library, status, arg
library = raw_loadfile(module)
library, status = pcall(library, arg or module)
assert(library, string.format("module '%s' load failed:\n%s", module, status))
loaded[module] = status
return status
end
function Split(str)
local words = {}
for word in str:gmatch("%S+") do
table.insert(words, word)
end
return words
end
function InArray(arr,elem)
for i in ipairs(arr) do
if tostring(arr[i]) == tostring(elem) then
return true
end
end
return false
end
-- Security.
printk("Welcome to the AWEKernel")
printk(" /\\_/\\")
printk("( ^ ^ )")
printk("\\ w /")
local f = kapi.loadfile(_KFLAGS._INIT_LOCATION)
local s, r = pcall(f)
if not s then
kapi.panic(r)
end
kapi.panic("init killed")