PsychOSPackages/boopu/exec/boopu.lua

60 lines
1.8 KiB
Lua
Raw Normal View History

local serial = require "serialization"
2023-10-11 13:33:45 +11:00
local fs = fs or require "filesystem"
local component = require "component"
local computer = require "computer"
local diskpart = require "diskpart"
2023-10-11 13:33:45 +11:00
local cfgpath = (_OSVERSION or ""):sub(1,7) == "PsychOS" and "/boot/cfg/boop.cfg" or "/etc/boop.cfg"
local defaults = {
path = "/boot/init.lua",
drive = computer.getBootAddress():match("^([^/])")
}
local tA = {...}
local config = defaults
2023-10-11 13:33:45 +11:00
local f = io.open(cfgpath,"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...")
2023-10-11 13:33:45 +11:00
local f = io.open(cfgpath, "wb")
if f then
f:write(serial.serialize(config))
f:close()
else
print("Failed to save config. Lost contents:")
print(serial.serialize(config))
end