Block devices.

This commit is contained in:
sam 2020-05-21 00:17:17 -04:00
parent d4b07eebc1
commit d09c52f6c2
8 changed files with 1446 additions and 0 deletions

View File

@ -20,6 +20,8 @@
--{name="vdev", cat="util", path="mods/util_vdev.zy2m"},
{name="classic", cat="menu", path="mods/menu_classic.zy2m"},
{name="luaconsole", cat="util", path="mods/util_luaconsole.zy2m"},
{name="blkdev", cat="util", path="mods/util_blkdev.zy2m"},
{name="fat", cat="fs", path="mods/fs_fat.zy2m"},
--{name="vdevrt", cat="rtmod", path="mods/rtmod_vdevrt.zy2m"},
--{name="tsukinet", cat="net", path="mods/net_tsukinet"},
--{name="biosemu", cat="loader", path="mods/loader_biosemu.zy2m", warning="warn_mod_biosemu"},

1
mods/fs_fat/init.lua Normal file
View File

@ -0,0 +1 @@
--#include "msdosfs.lua"

1172
mods/fs_fat/msdosfs.lua Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
local zy = require("zorya")
local formats = {
["msdos\0\0\0"] = "fs_fat",
["FoX FSys"] = "fs_foxfs",
["linux\0\0\0"] = "fs_ext2"
}

64
mods/util_blkdev/hdd.lua Normal file
View File

@ -0,0 +1,64 @@
do
local cproxy = component.proxy
local hdd = {}
function hdd.open(addr)
return {pos=1, dev=cproxy(addr)}
end
function hdd.size(blk)
return blk.dev.getCapacity()
end
function hdd.seek(blk, amt)
blk.pos = blk.pos + amt
if (blk.pos < 1) then
blk.pos = 1
elseif (blk.pos < hdd.size(blk)) then
blk.pos = hdd.size(blk)
end
return blk.pos
end
function hdd.setpos(blk, pos)
blk.pos = pos
if (blk.pos < 1) then
blk.pos = 1
elseif (blk.pos < hdd.size(blk)) then
blk.pos = hdd.size(blk)
end
return blk.pos
end
local function hd_read(dev, pos, amt)
local start_sec = ((pos-1) // 512)+1
local start_byte = ((pos-1) % 512)+1
local end_sec = ((pos+amt-1) // 512)+1
local buf = ""
for i=0, end_sec-start_sec do
buf = buf .. dev.readSector(start_sec+i)
end
return buf:sub(start_byte, start_byte+amt-1)
end
function hdd.read(blk, amt)
blk.pos = hdd.seek(blk, amt)
return hd_read(blk.dev, blk.pos, amt)
end
function hdd.write(blk, data)
local pos = blk.pos
local amt = #data
local start_sec = ((pos-1) // 512)+1
local start_byte = ((pos-1) % 512)+1
local end_sec = ((pos+amt-1) // 512)+1
local end_byte = ((pos+amt-1) % 512)+1
local s_sec = blk.dev.readSector(start_sec)
local e_sec = blk.dev.readSector(end_sec)
local dat = s_sec:sub(1, start_byte-1)..data..e_sec:sub(end_byte)
for i=0, end_sec-start_sec do
blk.dev.writeSector(start_sec+i, dat:sub((i*512)+1, (i+1)*512))
end
end
blkdev.register("hdd", hdd)
end

57
mods/util_blkdev/init.lua Normal file
View File

@ -0,0 +1,57 @@
local blkdev = {}
do
local protos = {}
local blk = {}
function blkdev.proxy(name, ...)
return setmetatable({udat=protos[name].open(...),type=name}, {__index=function(t, i)
if (blk[i]) then
return blk[i]
elseif (protos[t.name].hasmethod(t.udat, i)) then
return function(b, ...)
return protos[b.type](b.udat, i, ...)
end
end
end})
end
function blkdev.register(name, proto)
protos[name] = proto
end
function blk:read(amt)
return protos[self.type].read(self.udat, amt)
end
function blk:write(data)
return protos[self.type].write(self.udat, data)
end
function blk:blktype()
return self.type
end
function blk:seek(whence, amt)
whence = whence or 0
if (type(whence) == "number") then
amt = whence
whence = "cur"
end
if (whence == "cur") then
return protos[self.type].seek(self.udat, amt)
elseif (whence == "set") then
return protos[self.type].setpos(self.udat, amt)
elseif (whence == "end") then
return protos[self.type].setpos(self.udat, protos[self.type].size(self.udat)+amt)
end
end
end
--#include "hdd.lua"
--#include "prom.lua"
--#include "osdi.lua"
---#include "mtpart.lua"
---#include "mbr.lua"
return blkdev

79
mods/util_blkdev/osdi.lua Normal file
View File

@ -0,0 +1,79 @@
do
local osdi = {}
local sig = string.pack("<I4I4c8I3c13", 1, 0, "OSDIpart", 0, "")
function osdi.open(blkdev, partnumber)
blkdev:seek(1-blkdev:seek(0))
local verinfo = blkdev:read(32)
if (verinfo ~= sig) then
return nil, "bad block"
end
blkdev:seek((partnumber-1)*32)
local start, size, ptype, flags, name = string.unpack("<I4I4c8I3c13", blkdev:read(32))
return {start=start, size=size, ptype=ptype, flags=flags, name=name, dev=blkdev, pos=1, part=partnumber}
end
function osdi.size(blk)
return blk.size*512
end
function osdi.seek(blk, amt)
blk.pos = blk.pos + amt
if (blk.pos < 1) then
blk.pos = 1
elseif (blk.pos < osdi.size(blk)) then
blk.pos = osdi.size(blk)
end
return blk.pos
end
function osdi.setpos(blk, pos)
blk.pos = pos
if (blk.pos < 1) then
blk.pos = 1
elseif (blk.pos < osdi.size(blk)) then
blk.pos = osdi.size(blk)
end
return blk.pos
end
function osdi.read(blk, amt)
blk.dev:seek(((blk.start*512)+(blk.pos-1))-blk.dev:seek(0))
osdi.seek(blk, amt)
return blk.dev:read(amt)
end
function osdi.write(blk, data)
blk.dev:seek(((blk.start*512)+(blk.pos-1))-blk.dev:seek(0))
osdi.seek(blk, #data)
blk.dev:write(data)
end
local custom = {}
function custom:type()
return self.type
end
function custom:name()
return self.name
end
function custom:flags()
return self.flags
end
function custom:partnumber()
return self.part
end
function osdi.custom(blk, fun, ...)
return custom[fun](blk, ...)
end
function osdi.hasmethod(blk, fun)
return custom[fun] ~= nil
end
blkdev.register("osdi", osdi)
end

64
mods/util_blkdev/prom.lua Normal file
View File

@ -0,0 +1,64 @@
do
local cproxy = component.proxy
local prom = {}
function prom.open(addr)
return {pos=1, dev=cproxy(addr)}
end
function prom.size(blk)
return blk.dev.numBlocks()*blk.dev.blockSize()
end
function prom.seek(blk, amt)
blk.pos = blk.pos + amt
if (blk.pos < 1) then
blk.pos = 1
elseif (blk.pos < prom.size(blk)) then
blk.pos = prom.size(blk)
end
return blk.pos
end
function prom.setpos(blk, pos)
blk.pos = pos
if (blk.pos < 1) then
blk.pos = 1
elseif (blk.pos < prom.size(blk)) then
blk.pos = prom.size(blk)
end
return blk.pos
end
local function hd_read(dev, pos, amt)
local start_sec = ((pos-1) // 512)+1
local start_byte = ((pos-1) % 512)+1
local end_sec = ((pos+amt-1) // 512)+1
local buf = ""
for i=0, end_sec-start_sec do
buf = buf .. dev.blockRead(start_sec+i)
end
return buf:sub(start_byte, start_byte+amt-1)
end
function prom.read(blk, amt)
blk.pos = prom.seek(blk, amt)
return hd_read(blk.dev, blk.pos, amt)
end
function prom.write(blk, data)
local pos = blk.pos
local amt = #data
local start_sec = ((pos-1) // 512)+1
local start_byte = ((pos-1) % 512)+1
local end_sec = ((pos+amt-1) // 512)+1
local end_byte = ((pos+amt-1) % 512)+1
local s_sec = blk.dev.blockRead(start_sec)
local e_sec = blk.dev.blockRead(end_sec)
local dat = s_sec:sub(1, start_byte-1)..data..e_sec:sub(end_byte)
for i=0, end_sec-start_sec do
blk.dev.blockWrite(start_sec+i, dat:sub((i*512)+1, (i+1)*512))
end
end
blkdev.register("prom", prom)
end