From 818bc1c3a9ce4b6de59c178f6b50a716ec2568b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 16 Dec 2014 14:27:18 +0100 Subject: [PATCH] Added OpenLoader EEPROM flasher --- .../loot/OpenLoader/OpenLoader.man | 2 + .../opencomputers/loot/OpenLoader/autorun.lua | 6 + .../loot/OpenLoader/bin/opl-flash.lua | 156 ++++++++++++++++++ .../opencomputers/loot/OpenLoader/init.lua | 2 +- 4 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/assets/opencomputers/loot/OpenLoader/autorun.lua create mode 100644 src/main/resources/assets/opencomputers/loot/OpenLoader/bin/opl-flash.lua diff --git a/src/main/resources/assets/opencomputers/loot/OpenLoader/OpenLoader.man b/src/main/resources/assets/opencomputers/loot/OpenLoader/OpenLoader.man index 8dd6c4902..5f1293f4c 100644 --- a/src/main/resources/assets/opencomputers/loot/OpenLoader/OpenLoader.man +++ b/src/main/resources/assets/opencomputers/loot/OpenLoader/OpenLoader.man @@ -10,6 +10,8 @@ DESCRIPTION for /boot/kernel, the label is file name for /init.lua label is contained in /.osprop file, which is simple lua script returning table with properties. + + The program can be flashed to EEPROM using opl-flash program EXAMPLE .osprop file: return {name = "OpenOS"} diff --git a/src/main/resources/assets/opencomputers/loot/OpenLoader/autorun.lua b/src/main/resources/assets/opencomputers/loot/OpenLoader/autorun.lua new file mode 100644 index 000000000..30037aa95 --- /dev/null +++ b/src/main/resources/assets/opencomputers/loot/OpenLoader/autorun.lua @@ -0,0 +1,6 @@ +local filesystem = require("filesystem") +local shell = require("shell") + +local args = {...} +shell.setPath(shell.getPath() .. ":/mnt/"..args[1].address:sub(1,3).."/bin/") + diff --git a/src/main/resources/assets/opencomputers/loot/OpenLoader/bin/opl-flash.lua b/src/main/resources/assets/opencomputers/loot/OpenLoader/bin/opl-flash.lua new file mode 100644 index 000000000..44c72fc25 --- /dev/null +++ b/src/main/resources/assets/opencomputers/loot/OpenLoader/bin/opl-flash.lua @@ -0,0 +1,156 @@ +local eeprom = [[ +_G._OSVERSION = "OpenLoader 0.1EE" +local component = component or require('component') +local computer = computer or require('computer') +local unicode = unicode or require('unicode') + + +local gpu = component.list("gpu")() +local w, h + +local screen = component.list('screen')() +for address in component.list('screen') do + if #component.invoke(address, 'getKeyboards') > 0 then + screen = address + end +end + +local cls = function()end +if gpu and screen then + component.invoke(gpu, "bind", screen) + w, h = component.invoke(gpu, "getResolution") + component.invoke(gpu, "setResolution", w, h) + component.invoke(gpu, "setBackground", 0x000000) + component.invoke(gpu, "setForeground", 0xFFFFFF) + component.invoke(gpu, "fill", 1, 1, w, h, " ") + cls = function()component.invoke(gpu,"fill", 1, 1, w, h, " ")end +end +local y = 1 +local function status(msg) + if gpu and screen then + component.invoke(gpu, "set", 1, y, msg) + if y == h then + component.invoke(gpu, "copy", 1, 2, w, h - 1, 0, -1) + component.invoke(gpu, "fill", 1, h, w, 1, " ") + else + y = y + 1 + end + end +end + +local function loadfile(fs, file) + --status("> " .. file) + local handle, reason = component.invoke(fs,"open",file) + if not handle then + error(reason) + end + local buffer = "" + repeat + local data, reason = component.invoke(fs,"read",handle,math.huge) + if not data and reason then + error(reason) + end + buffer = buffer .. (data or "") + until not data + component.invoke(fs,"close",handle) + return load(buffer, "=" .. file) +end + +local function dofile(fs, file) + local program, reason = loadfile(fs, file) + if program then + local result = table.pack(pcall(program)) + if result[1] then + return table.unpack(result, 2, result.n) + else + error(result[2]) + end + else + error(reason) + end +end + +local function boot(kernel) + status("BOOTING") + _G.computer.getBootAddress = function()return kernel.address end + cls() + dofile(kernel.address, kernel.fpx .. kernel.file) +end + +status(_OSVERSION) +status("Select what to boot:") + +local osList = {} + +for fs in component.list("filesystem") do + if component.invoke(fs, "isDirectory", "boot/kernel/")then + for _,file in ipairs(component.invoke(fs, "list", "boot/kernel/")) do + osList[#osList+1] = {fpx = "boot/kernel/", file = file, address = fs} + status(tostring(#osList).."."..file.." from "..(fs:sub(1,3))) + end + end + if component.invoke(fs, "exists", "init.lua") then + local osName = "init.lua" + if component.invoke(fs, "exists", ".osprop") then + pcall(function() + local prop = dofile(fs, ".osprop") + osName = (prop and prop.name) or "init.lua" + end) + end + osList[#osList+1] = {fpx = "", file = "init.lua", address = fs} + status(tostring(#osList).."."..osName.." from "..(fs:sub(1,3))) + end +end +status("Select os: ") +if #osList == 1 then + boot(osList[1]) +end +if #osList == 0 then + error("No OS found") + while true do computer.pullSignal() end +end +while true do + local sig = {computer.pullSignal()} + if sig[1] == "key_down" then + if sig[4] >= 2 and sig[4] <= 11 then + if osList[sig[4]-1] then + boot(osList[sig[4]-1]) + else + status("Not found!") + end + end + end +end +error("System crashed") +while true do computer.pullSignal() end +]] + +local component = require "component" +local computer = require "computer" +local shell = require "shell" +local term = require "term" + +local args, options = shell.parse(...) + +if options.h or options.help then + print("opl-flash [-hq]") + print(" -q --quiet Do not output anything, and don't ask if sure") + print(" -r --reboot Reboot after installing") + print(" -h --help Display this help") + return +end + +local say = not (options.q or options.quiet) and print or function()end +say ("Do you really want to flash openloader to EEPROM("..tostring(#eeprom).." bytes)[Y/n]") + +if io.read():lower() == "y" or options.h or options.help then + say("Flashing... Do not reboot now!") + component.eeprom.set(eeprom) + component.eeprom.setLabel("OpenLoader") + if options.r or options.reboot then + computer.shutdown(true) + else + say("Done. you can reboot now") + end +end + diff --git a/src/main/resources/assets/opencomputers/loot/OpenLoader/init.lua b/src/main/resources/assets/opencomputers/loot/OpenLoader/init.lua index 4922ed85b..2f70d5a3b 100644 --- a/src/main/resources/assets/opencomputers/loot/OpenLoader/init.lua +++ b/src/main/resources/assets/opencomputers/loot/OpenLoader/init.lua @@ -76,7 +76,7 @@ local function boot(kernel) dofile(kernel.address, kernel.fpx .. kernel.file) end -status("OpenLoader") +status(_OSVERSION) status("Select what to boot:") local osList = {}