This repository has been archived on 2024-04-19. You can view files and clone it, but cannot push or open issues or pull requests.
FREAX/boot/freax/kernel.boot
2022-07-18 19:33:40 +02:00

376 lines
9.2 KiB
Clojure

print("freax: loading")
local printLog = false
local isBooting = false
function setupSystem()
kerneldraw.printKernelMessage("Starting system setup")
kerneldraw.printKernelMessage("Please enter a new root password")
kerneldraw.printKernelAsking("Password:")
local passwd = secureInputReturn("")
if security.passbyte("root", passwd) then
kerneldraw.printKernelMessage("Changed password of the root user")
while true do
kerneldraw.printKernelMessage("Do you want to add another user?")
kerneldraw.printKernelAsking("[y/n]: ")
local add_user = "n"
add_user = read()
if add_user == "y" or add_user == "Y" then
local username = nil
local password = nil
kerneldraw.printKernelAsking("Username: ")
username = read()
if username == nil or username == "" then
kerneldraw.printKernelMessage("Invalid username")
break
end
kerneldraw.printKernelAsking("Password:")
password = secureInputReturn("")
if password == nil or password == "" then
kerneldraw.printKernelMessage("Invalid password")
end
if security.passbyte(username,password) then
log.writeSecurity("User ".. username .." added")
fs.makeDir("/home/" .. username)
else
kerneldraw.printKernelMessage("Failed to add user " .. username)
break
end
else
break
end
end
kerneldraw.printKernelMessage("System setup completed")
return true
else
kerneldraw.printKernelMessage("Failed to change the password. Shutting down in 3 seconds")
sleep(3)
reboot(true)
return(false)
end
end
local function initLogger(file)
if not fs.isDir("/var") and not fs.exists("/var/log") and not fs.isDir("/var/log") then
fs.makeDir("/var")
fs.makeDir("/var/log")
end
return "/var/log/" .. file
end
function halt()
writeKernelCall("halt()")
sleep(1)
writeShutdown("halting")
while true do
coroutine.create(function() while true do coroutine.yield() end end)
coroutine.yield()
end
end
function poweroff(varDelete)
if varDelete then
writeKernelCall("poweroff(true)")
else
writeKernelCall("poweroff(false)")
end
writeShutdown("Reaching system poweroff")
if varDelete then
writeShutdown("Removing temporary files")
if fs.exists("/var") and fs.isDir("/var") then
fs.delete("/var")
end
sleep(1)
end
writeShutdown("Reached system poweroff")
sleep(1)
os.shutdown()
halt()
end
function reboot(varDelete)
if varDelete then
writeKernelCall("reboot(true)")
else
writeKernelCall("reboot(false)")
end
writeShutdown("Reaching system reboot")
if varDelete then
sleep(0.5)
writeShutdown("Removing temporary files")
if fs.exists("/var") and fs.isDir("/var") then
fs.delete("/var")
end
end
writeShutdown("Reached system reboot")
sleep(1.5)
os.reboot()
halt()
end
function panic(text)
writeKernelCall("panic(" .. text .. ")")
local logger = fs.open("/etc/errorLog", "a")
logger.writeLine(tostring(os.day()).. "d:" ..textutils.formatTime(os.time(), true).. " | panic! " ..text)
logger.close()
printError("freax: panic! " .. text)
halt()
end
function import(file)
writeKernelCall("import(" .. file .. ")")
if fs.exists(file) and not fs.isDir(file) then
return os.loadAPI(file)
else
writeSyslog("Failed to load API " .. file)
return false
end
end
function require(file) -- A primitive require
writeKernelCall("require(" .. file .. ")")
if fs.exists(file) and not fs.isDir(file) then
return dofile(file)
else
writeSyslog("Failed to load module " .. file)
return nil
end
end
function APIHook(t)
writeKernelCall("APIHook(" .. t .. ")")
if t then
return true
else
-- Not loaded
if import("/system/api/" .. t) then
return true
end
end
return false
end
function writeKernelCall(calledFunc)
--kerneldraw.printKernelMessage("Function called: " .. calledFunc)
local logger = fs.open(initLogger("calls"), "a")
logger.writeLine(tostring(os.day()) .. "d:" .. textutils.formatTime(os.time(),true) .. " | Function called: " .. calledFunc)
logger.close()
end
function writeSyslog(text)
writeKernelCall("writeSyslog(" .. text .. ")")
kerneldraw.printKernelMessage(text)
local logger = fs.open(initLogger("syslog"), "a")
logger.writeLine(tostring(os.day()).. "d:" ..textutils.formatTime(os.time(), true).. " | " ..text)
logger.close()
end
function writeMessage(text)
writeKernelCall("writeMessage(" .. text .. ")")
kerneldraw.printKernelMessage(text)
local logger = fs.open(initLogger("messages"), "a")
logger.writeLine(tostring(os.day()).. "d:" ..textutils.formatTime(os.time(), true).. " | " ..text)
logger.close()
end
function writeSecurity(text)
writeKernelCall("writeSecurity(" .. text .. ")")
kerneldraw.printKernelMessage(text)
local logger = fs.open(initLogger("security"), "a")
logger.writeLine(tostring(os.day()).. "d:" ..textutils.formatTime(os.time(), true).. " | " ..text)
logger.close()
end
function writeShutdown(text)
writeKernelCall("writeShutdown(" .. text .. ")")
kerneldraw.printKernelMessage(text)
local logger = fs.open(initLogger("shutdown"), "a")
logger.writeLine(tostring(os.day()).. "d:" ..textutils.formatTime(os.time(), true).. " | " ..text)
logger.close()
end
function writeAuth(text)
writeKernelCall("writeAuth(" .. text .. ")")
kerneldraw.printKernelMessage(text)
local logger = fs.open(initLogger("auth"), "a")
logger.writeLine(tostring(os.day()).. "d:" ..textutils.formatTime(os.time(), true).. " | " ..text)
logger.close()
end
function writeBootLog(text)
writeKernelCall("writeBootLog(" .. text .. ")")
kerneldraw.printKernelMessage(text)
local logger = fs.open(initLogger("boot"), "a")
logger.writeLine(tostring(os.day()).. "d:" ..textutils.formatTime(os.time(), true).. " | " ..text)
logger.close()
end
function loadSysLibs()
writeKernelCall("loadSysLibs()")
writeBootLog("Loading system libraries")
local flist = fs.list("/etc/lib")
for _, file in ipairs(flist) do
sleep(0.25)
local returnValue = import("/etc/lib/" ..file)
if returnValue then
writeSyslog("Library " .. file .. " loaded")
else
writeSysLog("Library " .. file .. " failed to load")
end
end
end
function loadUsrLibs()
writeKernelCall("loadUsrLibs()")
writeBootLog("Loading user libraries")
local flist = fs.list("/usr/local/lib")
for _, file in ipairs(flist) do
sleep(0.25)
local returnValue = import("/usr/local/lib/" ..file)
if returnValue then
writeSyslog("Library " .. file .. " loaded")
else
writeSysLog("Library " .. file .. " failed to load")
end
end
end
function printFile(file)
writeKernelCall("printFile(" .. file .. ")")
if fs.exists(file) and not fs.isDir(file) then
local handle = fs.open(file, "r")
local contents = handle.readAll()
handle.close()
return contents
end
end
function getURLContents(url)
writeKernelCall("getURLContents(" .. url .. ")")
local handlex = http.get(url)
local contents = handlex.readAll()
handlex.close()
return contents
end
function getFile(file, url, overwrite)
writeKernelCall("getFile(" .. file .. "," .. url .. "," .. overwrite .. ")")
if not fs.exists(file) then
local handle = fs.open(file, "w")
handle.writeLine(getURLContents(url))
handle.close()
return true
elseif overwrite and not fs.isDir(file) then
local handle = fs.open(file, "w")
handle.writeLine(getURLContents(url))
handle.close()
return true
end
return false
end
function secureInput(invite, definition)
writeKernelCall("secureInput(" .. invite .. "," .. definition .. ")")
write(invite.. " ")
local input = read("*")
if input == definition then
return true
else
return false
end
end
function secureInputReturn(invite)
writeKernelCall("secureInput(" .. invite .. ")")
write(invite.. " ")
local input = read("*")
return input
end
function getName()
writeKernelCall("getName()")
return "FREAX Kernel"
end
function getVersion()
writeKernelCall("getVersion()")
return "d0.0.1.0"
end
function getRelease()
writeKernelCall("getRelease()")
return getName() .. " " .. getVersion()
end
function getMachine()
writeKernelCall("getMachine()")
if turtle then
if commands then
return "Command Turtle"
elseif term.isColor() then
return "Advanced Turtle"
else
return "Turtle"
end
elseif pocket then
if commands then
return "Command Pocket Computer"
elseif term.isColor() then
return "Advanced Pocket Computer"
else
return "Pocket Computer"
end
else
if commands then
return "Command Computer"
elseif term.isColor() then
return "Advanced Computer"
else
return "Computer"
end
end
end
function getProcessorArchitecture()
writeKernelCall("getProcessorArchitecture()")
return "lua"
end
function getHardwarePlatform()
writeKernelCall("getHardwarePlatform()")
return "ComputerCraft"
end
function getOS()
writeKernelCall("getOS()")
return "FREAX"
end
function getHostname()
writeKernelCall("getHostname()")
if os.getComputerLabel() == nil then
return getOS()
else
return os.getComputerLabel()
end
end
function setHostname(hostname)
writeKernelCall("setHostname()")
local returncode os.setComputerLabel(hostname)
kerneldraw.printAppInfo("net","Changed hostname to " .. hostname)
kernel.writeMessage("Changed hostname to " .. hostname)
return returncode
end
function numRound(num)
return num>=0 and math.floor(num+0.5) or math.ceil(num-0.5)
end
function string2table(string_convert)
local table = {}
string_convert:sub(".",function(c) table.insert(table,c) end)
return table
end
print("freax: loaded")