added utility to write something to a boot partition - probably a kernel

This commit is contained in:
Izaya 2023-10-07 21:56:21 +10:00
parent 1d47f2fe58
commit 5dc27fd38b

52
boopu/exec/boopu.lua Normal file
View File

@ -0,0 +1,52 @@
local serial = require "serialization"
local component = require "component"
local computer = require "computer"
local diskpart = require "diskpart"
local defaults = {
path = "/boot/init.lua",
drive = computer.getBootAddress():match("^([^/])")
}
local tA = {...}
local config = defaults
local f = io.open("/boot/cfg/boop.cfg","rb")
if f then
config = setmetatable(serial.unserialize(f:read("*a")), {__index=defaults})
f:close()
end
config.path = tA[1] or config.path
config.drive = component.get(tA[2] or "", "drive") or config.drive
config.part = tA[3] or config.part
assert(component.type(config.drive) == "drive" or component.type(config.drive) == "tape_drive", "boot device is not a block device")
print("Detecting boot partition...")
local dpi, dpart
for k,v in ipairs(diskpart.getPartitions(config.drive)) do
if v[2] == "boot" and (config.part or k) == k then
dpi = k
end
end
assert(dpi, "no boot partition found on drive "..tostring(config.drive))
dpart = diskpart.proxyPartition(config.drive, dpi)
print(string.format('Boot partition found: %s/%i', config.drive:sub(1,8), dpi))
assert(dpart.getCapacity() > fs.size(config.path), "not enough space for init on boot partition")
local f,e = io.open(config.path, "rb")
assert(f, e)
print("Writing boot partition...")
local ssize = dpart.getSectorSize()
for i = 1, dpart.getCapacity()/ssize do
local data = f:read(ssize) or ""
data = #data == ssize and data or data .. ("\0"):rep(ssize - #data)
dpart.writeSector(i, data)
io.write(".")
end
f:close()
print("\nDone! Saving config...")
local f = io.open("/boot/cfg/boop.cfg", "wb")
f:write(serial.serialize(config))
f:close()