From cac49960006bbf605d9fb65892d2239937177b63 Mon Sep 17 00:00:00 2001 From: cinder <> Date: Fri, 22 Mar 2024 16:13:21 -0700 Subject: [PATCH] initial commit with a simplified boot eeprom kitn is intended to be able to boot from the built-in lua bios, but doesn't need e.g. a primary screen/gpu bound --- .gitignore | 1 + boot.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 .gitignore create mode 100644 boot.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b1c8b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/dist diff --git a/boot.lua b/boot.lua new file mode 100644 index 0000000..6a061c9 --- /dev/null +++ b/boot.lua @@ -0,0 +1,47 @@ +local component, error, select = component, error, select +local cl, ci, ct = component.list, component.invoke, component.type + +local function bail(msg, ...) + if select('#', ...) > 0 then + msg = msg:format(...) + end + return error(msg, 0) +end +local function unique(addr1, addr2, what) + if addr1 and addr2 then + return bail('multiple %s: %s, %s', what, addr1, addr2) + end + return addr1 or addr2 or bail('no %s', what) +end + +local eeprom +for addr in cl('eeprom') do eeprom = unique(eeprom, addr, 'eeproms') end +eeprom = unique(eeprom, nil, 'eeprom') + +local data = ci(eeprom, 'getData') +local bootfs +if data == '' then + for fs in cl('filesystem') do + if ci(fs, 'exists', '/init.lua') then + bootfs = unique(bootfs, fs, 'bootable filesystems') + end + end + bootfs = unique(bootfs, nil, 'bootable filesystem') +else + local ty, err = ct(data) + if not ty then bail('bootfs %s absent', data) + if ty ~= 'filesystem' then bail('bootfs %s is a %s', data, ty) end + bootfs = data +end + +local fd, err = ci(bootfs, 'open', '/init.lua') +if not fd then bail('open %s/init.lua failed: %s', bootfs:sub(1, 8), err) end +local code = '' +repeat + local chunk, err = ci(bootfs, 'read', fd, 4096) + if err then bail('read %s/init.lua failed: %s', bootfs:sub(1, 8), err) end + code = code .. (chunk or '') +until not chunk +ci(bootfs, 'close', fd) +local fn = assert(load(code, '@/init.lua')) +return fn()