From 5dc27fd38b630c5619c804bff8403121c6dcd227 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Sat, 7 Oct 2023 21:56:21 +1000 Subject: [PATCH] added utility to write something to a boot partition - probably a kernel --- boopu/exec/boopu.lua | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 boopu/exec/boopu.lua diff --git a/boopu/exec/boopu.lua b/boopu/exec/boopu.lua new file mode 100644 index 0000000..2461031 --- /dev/null +++ b/boopu/exec/boopu.lua @@ -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()