Skip to main content

How does it work: FreeBSD boot loader.

Posted in

Boot loader? What is this? And what is this for?

Boot loader (or simply loader in FreeBSD) is the program which loads the kernel. Or, if you would like a more precise definition, loader is a kernel bootstrapping final stage(`man 8 loader`). It goes right after boot2 stage. Just a little reminder, boot2 stage is the stage when /boot/loader is loaded into memory and the execution is passed to the loader's entry point.

So, let's find out what's going on there and how this loader works. We will do this by investigating the source code, and we will start from sys/boot/common/boot.c

There are 2 things that a loader can do:

  • Auto boot on timeout
  • Boot the specified kernel

The default timeout is 10 seconds (source code says so :) ).

Scenario 1: Autoboot happens.

Loader tries to get the name of the kernel to boot. This is achieved by

kernelname = getenv("kernelname");

This call may fail and kernelname might be NULL. In this case another attempt (to find the file to boot) is to be done.

spec = getenv("bootfile")

I hope none of these steps fail for you, because if they do it will mean that you don't have valid kernels.

Assuming that loader has got the kernel to boot it loads it by calling

command_boot(1, argv)

Now we will leave Scenario 1 because it continues right at that point where the 2nd Scenario starts (i.e. command_boot)

Scenario 2: Boot the kernel.

Now it is time for loader to find out whether the kernel has been specified explicitly. If this is so, then it has to find the kernel module and load it.

It might happen that it has been already loaded (I'm talking about kernel module). If this is the case, then all is left to do is just load the kernel itself by

loadakernel(0, argc - 1, argv + 1)

Once it is over, it is time to autoload modules and and 'start' the kernel.

Post new comment

The content of this field is kept private and will not be shown publicly.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.