This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Y.Petremann's profile picture

Simple bootloader

Started by Y.Petremann, 05 December 2012 - 11:11 PM
Y.Petremann #1
Posted 06 December 2012 - 12:11 AM
This is a very simple bootloader that can verify if the file exist and run it.
CODE :
Spoiler/startup

-- Script at /startup or /disk/startup
-- Actual boot sequence
bootSequence = {
  {"HTTP","/boot/http"   }, -- it will download scripts over http and boot it, usefull for easy installation
  {"LAN" ,"/boot/lan"    }, -- the computer is controled by a lua interpreter over rednet, usefull for controlling a turtle with a computer
  -- {"DISK","/disk/startup"}, -- it will boot a disk, if installed in a disk it will make a infinite loop
  {"HDD" ,"/hdd/startup" }, -- the same thing as rom but you can modify programs, hdd programs are used
  {"ROM" ,"/rom/startup" }, -- standard boot
}
-- Program
term.setCursorPos(1,1)
term.clear()
for k,d in ipairs(bootSequence) do
  name, path = d[1], d[2]
  if fs.exists(path) and not fs.isDir(path) then
    print("boot "..name)
    sleep(1)
    term.setCursorPos(1,1)
    term.clear()
    shell.run( path )
    break
  else
    print(name.." skipped")
  end
end
/boot/lan

-- Script at /boot/lan
-- Find a RedNet Modem
for n,sSide in ipairs(rs.getSides()) do
    if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "modem" then
        sModemSide = sSide
        break
    end
end
if not sModemSide then
    print("No wireless modem attached")
    return
else
    print("Started connection on "..sModemSide)
    print("Started connection on "..os.getComputerID())
end
-- Main lua interpreter loop
local bRunning = true
local tEnv = {}
setmetatable( tEnv, { __index = getfenv() } )
rednet.open(sModemSide)
while bRunning do
    local event, senderId, message, distance = os.pullEvent()
    if event == "rednet_message" then
        print("== Receive command ==")
    print(message)
    local nForcePrint = 0
        local func, e = loadstring( message, "lua" )
        local func2, e2 = loadstring( "return "..message, "lua" )
        if not func then
            if func2 then
                print("> is a variable")
                func = func2
                e = nil
                nForcePrint = 1
            end
        else
            print("> is a function")
            if func2 then
                func = func2
            end
        end
        if func then
            setfenv( func, tEnv )
            local tResults = { pcall( function() return func() end ) }
            if tResults[1] then
                local n = 1
                while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do
                    rednet.send(senderId, tostring( tResults[n + 1] ))
                    print( tostring( tResults[n + 1] ) )
                    n = n + 1
                end
            else
                rednet.send(senderId, tResults[2])
                print( tResults[2] )
            end
        else
            rednet.send(senderId, e)
            printError( e )
        end
    end
end
TODO :
Spoiler
  • Add script failed test ( so if the boot script that is booted failed it boot the next script)
  • Add a separated boot sequence file
  • Reboot the computer for each failed boot and boot the next entry, if correctly booted ( normaly can't fail with /rom/startup ) it reset the sequence for the next boot
  • Pressing a key to skip the current entry in the boot sequence
  • F2 show a menu to choose directly the entry to boot
  • Verifying if the actual script is /disk/startup or if /disk/startup is the same script as /startup ( to remove infinite loop effect )
TheVarmari #2
Posted 06 December 2012 - 01:55 AM
Why is there a break on line 21?
Y.Petremann #3
Posted 06 December 2012 - 07:37 AM
Because if a script has booted correctly, it don't boot with other script.
TheVarmari #4
Posted 07 December 2012 - 01:20 AM
Because if a script has booted correctly, it don't boot with other script.

I thought the point of this was to run x, then when x finishes run y, then when y finishes run z etc.
But, nice program, I just really can't find a use for this.