it's shit

This commit is contained in:
sam 2020-05-08 23:51:05 -04:00
commit 137861d5d9
34 changed files with 641 additions and 0 deletions

181
build.lua Normal file
View File

@ -0,0 +1,181 @@
--[[----------------------------------------------------------------------------
LZSS - encoder / decoder
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
--]]----------------------------------------------------------------------------
--------------------------------------------------------------------------------
local M = {}
local string, table = string, table
--------------------------------------------------------------------------------
local POS_BITS = 12
local LEN_BITS = 16 - POS_BITS
local POS_SIZE = 1 << POS_BITS
local LEN_SIZE = 1 << LEN_BITS
local LEN_MIN = 3
--------------------------------------------------------------------------------
function M.compress(input)
local offset, output = 1, {}
local window = ''
local function search()
for i = LEN_SIZE + LEN_MIN - 1, LEN_MIN, -1 do
local str = string.sub(input, offset, offset + i - 1)
local pos = string.find(window, str, 1, true)
if pos then
return pos, str
end
end
end
while offset <= #input do
local flags, buffer = 0, {}
for i = 0, 7 do
if offset <= #input then
local pos, str = search()
if pos and #str >= LEN_MIN then
local tmp = ((pos - 1) << LEN_BITS) | (#str - LEN_MIN)
buffer[#buffer + 1] = string.pack('>I2', tmp)
else
flags = flags | (1 << i)
str = string.sub(input, offset, offset)
buffer[#buffer + 1] = str
end
window = string.sub(window .. str, -POS_SIZE)
offset = offset + #str
else
break
end
end
if #buffer > 0 then
output[#output + 1] = string.char(flags)
output[#output + 1] = table.concat(buffer)
end
end
return table.concat(output)
end
--------------------------------------------------------------------------------
function M.decompress(input)
local offset, output = 1, {}
local window = ''
while offset <= #input do
local flags = string.byte(input, offset)
offset = offset + 1
for i = 1, 8 do
local str = nil
if (flags & 1) ~= 0 then
if offset <= #input then
str = string.sub(input, offset, offset)
offset = offset + 1
end
else
if offset + 1 <= #input then
local tmp = string.unpack('>I2', input, offset)
offset = offset + 2
local pos = (tmp >> LEN_BITS) + 1
local len = (tmp & (LEN_SIZE - 1)) + LEN_MIN
str = string.sub(window, pos, pos + len - 1)
end
end
flags = flags >> 1
if str then
output[#output + 1] = str
window = string.sub(window .. str, -POS_SIZE)
end
end
end
return table.concat(output)
end
local function struct(tbl)
local pat = tbl.endian or "="
local args = {}
for i=1, #tbl do
local a, b = pairs(tbl[i])
local k, v = a(b)
args[i] = k
pat = pat .. v
end
return setmetatable({}, {__call=function(_, arg)
--checkArg(1, arg, "string", "table")
if (type(arg) == "string") then
local sval = {string.unpack(pat, arg)}
local rtn = {}
for i=1, #args do
rtn[args[i]] = sval[i]
end
return rtn, sval[#sval]
elseif (type(arg) == "table") then
local sval = {}
for i=1, #args do
sval[i] = arg[args[i]]
end
return string.pack(pat, unpack(sval))
end
end, __len=function()
return string.packsize(pat)
end})
end
local velx_spec = struct {
endian = "<",
{magic="c5"},
{compression="B"},
{lver="B"},
{fver="B"},
{os="B"},
{psize="I3"},
{lsize="I3"},
{ssize="I3"},
{rsize="I4"}
}
-- This builds Tsuki into a VELX executable.
print("Compiling kernel...")
local h = io.popen("luacomp ksrc/init.lua")
print("Compressing kernel...")
local data = M.compress(h:read("*a"))
print("Writing out tkrnl.velx...")
local h = io.open("tkrnl.velx", "wb")
local header = velx_spec({
magic = "\27VelX",
compression = 1,
lver = 0x53,
fver = 1,
os = 0xFF,
psize = #data,
lsize=0,
ssize=0,
rsize=0
})
h:write(header)
h:write(data)
h:close()

22
docs/kargs.md Normal file
View File

@ -0,0 +1,22 @@
# Kernel Arguments
/boot/kernel/tsuki-0.1.velx [arguments ...]
## --security
This argument changes the security enviroment of the Tsuki kernel.
### --security=none
This disables most of Tsuki's security modules, leaving the system in a vulnerable state.
### --security=noverify
This disables executable verification. This is default.
### --security=full
This enables all security features.
### --security=permissive
This makes all security errors into logged messages.
## --root=partspec
This mounts the partition as the root (`/`). An example partspec would be `osdi(f81aa211-3552-4196-91f6-57cc51c3ebfb,2)`

0
ksrc/acl.lua Normal file
View File

View File

79
ksrc/arcs/tsar/init.lua Normal file
View File

@ -0,0 +1,79 @@
local magic = 0x5f7d
local magic_rev = 0x7d5f
local header = "I2I2I2I2I2I6I6"
local function read_header(dat)
local e = "<"
local m = string.unpack("<I2", dat)
if m ~= magic and m ~= magic_rev then return nil, "bad magic" end
if m ~= magic then
e = ">"
end
local ent = {}
ent.magic, ent.namesize, ent.mode, ent.uid, ent.gid, ent.filesize, ent.mtime = string.unpack(e..header_fmt, dat)
return ent
end
local arc = {}
function arc:handle(path)
for i=1, #self.tbl do
if (self.tbl[i].name == path and self.tbl[i].mode & 32768 > 0) then
self.seek(self.tbl[i].pos-self.seek(0))
return self.read(self.tbl[i].filesize), self.tbl[i]
end
end
return nil, "file not found"
end
function arc:exists(path)
for i=1, #self.tbl do
if (self.tbl[i].name == path) then
return true
end
end
return false
end
function arc:list(path)
if path:sub(#path) ~= "/" then path = path .. "/" end
local ent = {}
for i=1, #self.tbl do
if (self.tbl[i].name:sub(1, #path) == path and not self.tbl[i].name:find("/", #path+1, false)) then
ent[#ent+1] = self.tbl[i].name
end
end
return ent
end
function arc:meta(path)
for i=1, #self.tbl do
if (self.tbl[i].name == path) then
return self.tbl[i]
end
end
return nil, "file not found"
end
function arc:close()
self.close()
end
return {
read = function(read, seek, close)
local tbl = {}
local lname = ""
while lname ~= "TRAILER!!!" do
local dat = read(22)
local et = read_header(dat)
et.name = read(et.namesize)
et.pos = seek(et.namesize & 1)
seek(et.filesize + (et.filesize & 1))
lname = et.name
if lname ~= "TRAILER!!!" then
tbl[#tbl+1] = et
end
end
return setmetatable({tbl = tbl, read = read, seek = seek, close = close}, {__index=arc})
end
}

View File

@ -0,0 +1 @@
return _LOAD == "Zorya"

View File

@ -0,0 +1,5 @@
return {
name = "Zorya",
version = {_ZVER//1, tonumber(tostring(_ZVER):match("%.(%d+)")), _ZPAT, string = _ZVER..".".._ZPAT},
oefiver = oefi.getAPIVersion()
}

View File

@ -0,0 +1 @@
zorya = nil

View File

@ -0,0 +1 @@
return _BIOS == "Zorya NEO"

View File

@ -0,0 +1,5 @@
return {
name = "Zorya NEO",
version = {_ZVER//1, tonumber(tostring(_ZVER):match("%.(%d+)")), _ZPAT, git = _ZGIT, string = _ZVSTR},
loader = _ZLOADER
}

View File

20
ksrc/biosfixes.lua Normal file
View File

@ -0,0 +1,20 @@
local _biossupport = {}
@[[function biosfix(bios)]]
_biossupport["@[{bios}]"] = {
quirks = function()
--#include @[{"ksrc/bios/"..bios.."/quirks.lua"}]
end,
info = function()
--#include @[{"ksrc/bios/"..bios.."/info.lua"}]
end,
detect = function()
--#include @[{"ksrc/bios/"..bios.."/detect.lua"}]
end,
name = "@[{bios}]"
}
@[[end]]
@[[biosfix("zoryalegacy")]]
@[[biosfix("zoryaneo")]]
@[[biosfix = nil]]

0
ksrc/buffer.lua Normal file
View File

15
ksrc/exec.lua Normal file
View File

@ -0,0 +1,15 @@
local velx = (function()
--#include "ksrc/execs/velx_spec/init.lua"
end)()
local zlua = (function()
--#include "ksrc/execs/zlua/init.lua"
end)
-- Executable loading process:
-- - Link
-- - Load
-- - Execute
if ()

View File

@ -0,0 +1 @@
--#include "ksrc/execs/velx_spec/velxspec.lua"

View File

View File

@ -0,0 +1,81 @@
local velx_spec = struct {
endian = "<",
{magic="c5"},
{fver="B"},
{compression="B"},
{lver="B"},
{os="B"},
{psize="I3"},
{lsize="I3"},
{ssize="I3"},
{rsize="I4"}
}
local velx = {}
local exec = {}
function velx.identify(path)
local h = io.open(path, "rb")
local header = velx_spec(h:read(#velx_spec))
h:close()
return header.magic == "\27VelX"
end
function velx.load(path)
end
function velx.verify(path)
local h = io.open(path, "rb")
local header = velx_spec(h:read(#velx_spec))
local code = h:read(header.psize)
h:seek("cur", lsize)
local sig = h:read(header.ssize)
h:close()
return security.verify(code, sig)
end
function exec:link()
self.file:seek("set", #velx_spec+self.header.psize)
local linkinfo = self.file:read(self.header.lsize)
local linker = {}
local pos = 1
while true do
local size = string.unpack("<H", linkinfo:sub(pos))
pos = pos + 2
local name = linkinfo:sub(pos, pos+size-1)
pos = pos + size
size = string.unpack("<H", linkinfo:sub(pos))
pos = pos + 2
local library = linkinfo:sub(pos, pos+size-1)
pos = pos + size
if (name == "" or library == "") then
break
end
linker[#linker+1] = {name, library}
end
local linkscript = ""
for i=1, #linker do
linkscript = linkscript .. "local " .. linker[i][1] .. "=require(\"" .. linker[i][2] .. "\")\n"
end
self.code = linkscript .. self.code
return true
end
function exec:load()
self.file:seek("set", #velx_spec)
local code = self.file:read(self.header.psize)
if (self.header.compression == 0) then
self.code = code
elseif (self.header.compression == 1) then
self.code = lzss.decompress(self.code)
else
return nil, "invalid compression method"
end
return true
end
function exec:run(args, evar)
system.load(self.code, args, system.getglobalenv({_ARCHIVE=self.arc}), system.getevars(evar))
end

3
ksrc/execs/zlua/init.lua Normal file
View File

@ -0,0 +1,3 @@
local zlua = {}
return {}

View File

@ -0,0 +1 @@
local blkstr = {}

6
ksrc/fs/foxfs/dir.lua Normal file
View File

@ -0,0 +1,6 @@
local dirent = struct {
endian="<",
{inode="I4"},
{name_size="H"},
{etype="B"}
}

11
ksrc/fs/foxfs/init.lua Normal file
View File

@ -0,0 +1,11 @@
local fox = {}
do
--#include "ksrc/fs/foxfs/inode.lua"
--#include "ksrc/fs/foxfs/dir.lua"
--#include "ksrc/fs/foxfs/superblock.lua"
--#include "ksrc/fs/foxfs/blkstream.lua"
function fox.proxy(blkdev)
-- Read the superblock
end
end

33
ksrc/fs/foxfs/inode.lua Normal file
View File

@ -0,0 +1,33 @@
local struct_inode = struct {
endian = "<",
{mode="H"},
{uid="H"},
{fsize="I6"},
{atime="I6"},
{ctime="I6"},
{mtime="I6"},
{dtime="I6"},
{gid="H"},
{sec_count="I4"},
{flags="I4"},
{osval="I4"},
{dbp0="I4"},
{dbp1="I4"},
{dbp2="I4"},
{dbp3="I4"},
{dbp4="I4"},
{dbp5="I4"},
{dbp6="I4"},
{dbp7="I4"},
{dbp8="I4"},
{dbp9="I4"},
{dbp10="I4"},
{dbp11="I4"},
{sibp="I4"},
{dibp="I4"},
{tibp="I4"},
{gen="H"},
{acl="I4"},
{fragaddr="I4"},
{osval2="c10"}
}

View File

View File

@ -0,0 +1,13 @@
local superblock = struct {
endian = "<",
{sig="H"},
{ver_maj="H"},
{ver_min="H"},
{state="H"},
{total_inodes="I4"},
{total_blocks="I4"},
{reserved_blocks="I4"},
{total_unalloc_blocks="I4"},
{total_unalloc_inodes="I4"},
{total_unalloc_inodes="I4"},
}

11
ksrc/init.lua Normal file
View File

@ -0,0 +1,11 @@
--#include "ksrc/struct.lua"
--#include "ksrc/string.lua"
--#include "ksrc/acl.lua"
--#include "ksrc/security.lua"
--#include "ksrc/exec.lua"
--#include "ksrc/fs/foxfs/init.lua"
--#include "ksrc/tty.lua"
--#include "ksrc/biosfixes.lua"
-- Mount the rootfs
vfs.mount()

View File

14
ksrc/security.lua Normal file
View File

@ -0,0 +1,14 @@
local security = {}
function security.checkacl(permission)
local perms = acl.get("group", thread.info().egid, {})
end
function security.getmode()
end
function security.init()
klog("security", 1, "Security init started.")
end

5
ksrc/stdlib/init.lua Normal file
View File

@ -0,0 +1,5 @@
function _P.load(...)
if (security.checkacl("unsigned_code")) then
end
end

9
ksrc/stdlib/thread.lua Normal file
View File

@ -0,0 +1,9 @@
_P.threads = {}
function _P.threads.add(name, func)
end
function _P.threads.load(path, args)
end

23
ksrc/string.lua Normal file
View File

@ -0,0 +1,23 @@
function string.trim(self)
return self:gsub("^%s+", ""):gsub("%s+$", "")
end
function string.explode(self, pat)
local t, ll
t={}
ll=0
if(#p == 1) then
return {p}
end
while true do
l = string.find(self, pat, ll, true) -- find the next d in the string
if l ~= nil then -- if "not not" found then..
table.insert(t, string.sub(self,ll,l-1)) -- Save it in our array.
ll = l + 1 -- save just after where we found it for searching next time.
else
table.insert(t, string.sub(self,ll)) -- Save what's left in our array.
break -- Break at end, as it should be, according to the lua manual.
end
end
return t
end

29
ksrc/struct.lua Normal file
View File

@ -0,0 +1,29 @@
function struct(tbl)
local pat = tbl.endian or "="
local args = {}
for i=1, do
local a, b = pairs(tbl[i])
local k, v = a(b)
args[i] = k
pat = pat .. v
end
return setmetatable({}, {__call=function(_, arg)
checkArg(1, arg, "string", "table")
if (type(arg) == "string") then
local sval = {string.unpack(pat, arg)}
local rtn = {}
for i=1, #args do
rtn[args[i]] = sval[i]
end
return rtn, sval[#sval]
elseif (type(arg) == "table") then
local sval = {}
for i=1, #args do
sval[i] = arg[args[i]]
end
return string.pack(pat, unpack(sval))
end
end, __len=function()
return string.packsize(pat)
end})
end

56
ksrc/threads.lua Normal file
View File

@ -0,0 +1,56 @@
local thd = {}
local next_pause = 0
local max_proctime = 5 -- 5s of process time max.
local last_proctime = 0
local lastsig = {}
local threads = {}
local function run_threads()
local rthd = {}
local mfd = {}
for i=1, #thd do
if (threads[i].dead) then
mfd[#mfd+1] = i
elseif (threads[i].deadline >= computer.uptime()) then
rthd[#rthd+1] = threads[i]
end
end
table.sort(rthd, function(a, b)
if (a.priority == b.priority) then
return a.deadline < b.deadline
end
return a.priority < b.priority
end)
local starttime = computer.uptime()
local mindl = math.huge
for i=1, #rthd do
local ok, dl = coroutine.resume(rthd[i].coro, table.unpack(rthd[i].args or lastsig))
if (not ok or coroutine.status(rthd[i].coro) == "dead") then
signal.push(thd.get(rthd[i].parent), {"subproc", "dead", rthd[i].pid, (rthd[i].exitcode or (dl and 1) or 0)})
for j=1, #thd do
if (threads[j] == rthd[i]) then
mfd[#mfd+1] = j
break
end
end
else
mindl = ((mindl > dl) and dl) or mindl
buffers.update(rthd[i])
if (rthd[i].sigpushed) then
mindl = 0
rthd[i].sigpushed = false
end
end
if (computer.uptime() >= starttime+max_proctime) then
goto cleanup
end
end
::cleanup::
for i=#mfd, 1, -1 do
table.remove(threads, mfd[i])
end
lastsig = table.pack(computer.pullSignal(mindl))
end
function thd.add(tinfo)
end

15
ksrc/tty.lua Normal file
View File

@ -0,0 +1,15 @@
local tty = {}
do
local _tty = {}
function _tty:getgpu()
return self.gpu.address
end
function _tty:getkb()
return self.kb.address
end
function _tty:getscreen()
return self.screen.address
end
end

BIN
tkrnl.velx Normal file

Binary file not shown.