local serial = require "serialization" local fs = fs or require "filesystem" local component = require "component" local computer = require "computer" local diskpart = require "diskpart" 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 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...") 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