87 lines
2.2 KiB
Lua
87 lines
2.2 KiB
Lua
local partition = {}
|
|
|
|
local eformat = "c20c4>I4>I4"
|
|
function partition.parse(s)
|
|
local rt = {}
|
|
for i = 1, s:len(), 32 do
|
|
local n, t, start, length = string.unpack(eformat, s, i)
|
|
n = n:gsub("\0", "")
|
|
if n ~= "" then
|
|
rt[#rt+1] = {n,t,start,length}
|
|
end
|
|
end
|
|
return rt
|
|
end
|
|
function partition.generate(pt)
|
|
local ps = ""
|
|
for k,v in ipairs(pt) do
|
|
ps = ps .. string.pack(eformat, table.unpack(v))
|
|
end
|
|
return ps
|
|
end
|
|
|
|
local function getProxy(addr)
|
|
if type(addr) == "string" then
|
|
return component.proxy(component.get(addr))
|
|
end
|
|
return addr
|
|
end
|
|
|
|
function partition.getPartitions(drive)
|
|
drive = getProxy(drive)
|
|
local rv = partition.parse(drive.readSector(drive.getCapacity() / drive.getSectorSize()))
|
|
return rv[1][2] == "mtpt" and rv or {}
|
|
end
|
|
function partition.setPartitions(drive, pt, name)
|
|
drive = getProxy(drive)
|
|
name = name or ""
|
|
if pt[1][2] ~= "mtpt" then table.insert(pt, 1, {name, "mtpt", 0, 0}) end
|
|
local ns = partition.generate(pt)
|
|
return drive.writeSector(drive.getCapacity() / drive.getSectorSize(), ns .. ("\0"):rep(drive.getSectorSize() - #ns))
|
|
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]}
|
|
|
|
function proxy.getCapacity()
|
|
return drive.getSectorSize() * part[4]
|
|
end
|
|
function proxy.getLabel()
|
|
return part[1]
|
|
end
|
|
function proxy.setLabel()
|
|
return false
|
|
end
|
|
|
|
local function offsetSector(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
|