119 lines
3.1 KiB
Lua
119 lines
3.1 KiB
Lua
local partition = {types={"mtpt","osdi","openupt"}}
|
|
|
|
local function getProxy(addr)
|
|
if type(addr) == "string" then
|
|
return component.proxy(component.get(addr))
|
|
end
|
|
return addr
|
|
end
|
|
local function aop(td,n,mn,...)
|
|
local rts = td.seek(-math.huge)
|
|
td.seek(n)
|
|
local rv = {td[mn](...)}
|
|
td.seek(-math.huge)
|
|
td.seek(rts)
|
|
return table.unpack(rv)
|
|
end
|
|
|
|
local function rf()
|
|
return false
|
|
end
|
|
local function getlib(f)
|
|
local drop = package.loaded["part/"..f] == nil
|
|
local w,r = pcall(require,"part/"..f)
|
|
if drop then package.loaded["part/"..f] = nil end
|
|
return w and r or {getPartitions=rf, setPartitions=rf}
|
|
end
|
|
|
|
function partition.getPartitions(drive)
|
|
drive = getProxy(drive)
|
|
for _, t in ipairs(partition.types) do
|
|
local pt = getlib(t).getPartitions(drive)
|
|
if pt then
|
|
return pt, t
|
|
end
|
|
end
|
|
return {}, partition.types[1]
|
|
end
|
|
function partition.setPartitions(drive, pt, name)
|
|
drive = getProxy(drive)
|
|
local _, ptt = partition.getPartitions(drive)
|
|
return getlib(ptt or partition.types[1]).setPartitions(drive, pt, name)
|
|
end
|
|
|
|
function partition.proxyPartition(drive, index)
|
|
drive = getProxy(drive)
|
|
local part = partition.getPartitions(drive)[index]
|
|
local sectorOffset, byteOffset, finish = part[3] - 1, (part[3]-1) * drive.getSectorSize()
|
|
local proxy = {label=part[1],ptype=part[2],type="partition",address=string.format("%s/%i",drive.address,index)}
|
|
|
|
function proxy.getLabel()
|
|
return part[1]
|
|
end
|
|
function proxy.setLabel()
|
|
return false
|
|
end
|
|
if drive.type == "tape_drive" then
|
|
local function checkBounds(n)
|
|
assert(n >= 0 and n <= len*bs, "invalid offset, not in a usable sector")
|
|
if n < 0 or n > len * bs then
|
|
error("invalid offset, not in a usable sector")
|
|
end
|
|
return (start * bs) + n
|
|
end
|
|
function proxy.readSector(n)
|
|
return aop(td, checkBounds((n-1) * bs), "read", bs)
|
|
end
|
|
function proxy.writeSector(n,d)
|
|
return aop(td, checkBounds((n-1) * bs), "write", d .. ("\0"):rep(512-#d))
|
|
end
|
|
function proxy.readByte(n)
|
|
return aop(td, checkBounds(n-1), "read")
|
|
end
|
|
function proxy.writeByte(n,d)
|
|
return aop(td, checkBounds(n-1), "write",d)
|
|
end
|
|
|
|
function proxy.getSectorSize()
|
|
return bs
|
|
end
|
|
function proxy.getCapacity()
|
|
return len * 512
|
|
end
|
|
return proxy
|
|
end
|
|
|
|
function proxy.getCapacity()
|
|
return drive.getSectorSize() * part[4]
|
|
end
|
|
|
|
local function offsetSector(sector)
|
|
assert(sector > 0 and sector <= part[4], "invalid offset, not in a usable sector")
|
|
if sector < 1 or sector > part[4] then error("invalid offset, not in a usable sector") end
|
|
return sectorOffset + sector
|
|
end
|
|
function proxy.readSector(sector)
|
|
return drive.readSector(offsetSector(sector))
|
|
end
|
|
function proxy.writeSector(sector, data)
|
|
return drive.writeSector(offsetSector(sector), data)
|
|
end
|
|
|
|
local function offsetByte(byte)
|
|
if byte < 1 or byte > part[4]*drive.getSectorSize() then return 0 end
|
|
return byteOffset + byte
|
|
end
|
|
function proxy.readByte(byte)
|
|
return drive.readByte(offsetByte(byte))
|
|
end
|
|
function proxy.writeByte(byte, data)
|
|
return drive.writeByte(offsetByte(byte), data)
|
|
end
|
|
for k,v in pairs(drive) do
|
|
proxy[k] = proxy[k] or v
|
|
end
|
|
return proxy
|
|
end
|
|
|
|
return partition
|