Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local searchPaths = {
- '/boot/boot.lua',
- '/boot.lua',
- '/init.lua',
- }
- local sf = string.format
- -- Boot device
- local bdev = {
- comp = computer,
- ls = component.list,
- disp = false,
- rom = component.list('eeprom', true)(),
- sl = { version = '1.0' }
- }
- setmetatable(bdev, {__index = component})
- function bdev:panic(msg, data)
- self.comp.beep(2000, 0.2)
- return error(msg .. (data and ': ' .. data or ''), 0)
- end
- function bdev:pullsig(timeout)
- return self.comp.pullSignal(timeout)
- end
- function bdev:print(msg)
- if self.disp then
- self.gpu.set(1, self.gpu.cur_y, msg)
- if self.gpu.cur_y == self.gpu.scr_h then
- self.gpu.copy(1, 2, self.gpu.scr_w, self.gpu.scr_h - 1, 0, -1)
- self.gpu.fill(1, self.gpu.scr_h, self.gpu.scr_w, 1, ' ')
- else self.gpu.cur_y = self.gpu.cur_y + 1 end
- end
- end
- function bdev:clear()
- if self.disp then
- self.gpu.cur_y = 1
- self.gpu.fill(1, 1, self.gpu.scr_w, self.gpu.scr_h, ' ')
- end
- end
- function bdev:sleep(timeout)
- local deadline = self.comp.uptime() + (timeout or 0)
- repeat self.pullsig(deadline - self.comp.uptime())
- until self.comp.uptime() >= deadline
- end
- function computer.getBootAddress()
- return bdev.invoke(bdev.rom, 'getData')
- end
- function computer.setBootAddress(address)
- return bdev.invoke(bdev.rom, 'setData', address)
- end
- do
- local gpu = bdev.ls('gpu', true)()
- local screen = bdev.ls('screen', true)()
- if gpu and screen then
- bdev.disp = true
- bdev.gpu = bdev.proxy(gpu)
- bdev.screen = bdev.proxy(screen)
- bdev.gpu.bind(screen)
- bdev.screen.turnOn()
- bdev.gpu.setBackground(0)
- bdev.gpu.setForeground(0xFFFFFF)
- bdev.gpu.scr_w, bdev.gpu.scr_h = bdev.gpu.getResolution()
- bdev:clear()
- end
- end
- bdev:print(sf('RogueBIOS v%s --- by RogueNet', bdev.sl.version))
- bdev:print''
- bdev:print'Searching for boot files ...'
- local bFSs = {}
- for address in bdev.ls('filesystem', true) do
- for _,path in ipairs(searchPaths) do
- if bdev.invoke(address, 'exists', path) then
- table.insert(bFSs, {address, path})
- break
- end
- end
- if #bFSs == 9 then break end
- end
- local nbFSs = #bFSs
- local sFS = 0
- if nbFSs == 1 or not bdev.disp then sFS = 1
- elseif nbFSs > 1 then
- bdev:print''
- bdev:print'Select a filesystem to boot from:'
- for i,fst in ipairs(bFSs) do
- local fsLabel = bdev.invoke(fst[1], 'getLabel')
- fsLabel = fsLabel and fsLabel..' ' or ''
- bdev:print(sf('%i) %s:%s', i, fsLabel .. fst[1], fst[2]))
- end
- local v = 0
- repeat
- e = table.pack(bdev:pullsig())
- if e[1] == 'component_removed' or e[1] == 'component_added' then
- bdev.comp.shutdown(true) end
- if e[1] == 'key_down' and tonumber(e[3]) then v = tonumber(e[3]) - 48
- end
- until e[1] == 'key_down' and v > 0 and v <= nbFSs
- sFS = v
- bdev:print''
- else bdev:panic'No bootable medium' end
- local bootaddr = bFSs[sFS][1]
- local bootfile = bFSs[sFS][2]
- bdev.bootfile = bootfile
- bdev.bootaddress = bootaddr
- bdev:print(sf('Loading %s:%s', bootaddr, bootfile))
- local bfh, reason = bdev.invoke(bootaddr, 'open', bootfile)
- if not bfh then bdev:panic('Couldnt open boot file', reason) end
- local buffer = ''
- repeat
- local data, reason = bdev.invoke(bootaddr, 'read', bfh, math.huge)
- if not data and reason then bdev:panic('Couldnt read boot file', reason)
- end
- buffer = buffer .. (data or '')
- until not data
- bdev.invoke(bootaddr, 'close', bfh)
- if not buffer then bdev:panic'Empty buffer' end
- bdev.comp.beep(2000, 0.2)
- bdev.comp.setBootAddress(bootaddr)
- local env = { bootdevice = bdev }
- setmetatable(env, {__index = _G})
- local boot = load(buffer, '@'..bootfile, 'bt', env)
- if not boot then bdev:panic('Booting failed.', 'Errors in boot file') end
- boot()
Advertisement
Add Comment
Please, Sign In to add comment