Sie sind auf Seite 1von 35

LINUX BOOTLOADER

Presented By
Abhiram Annangi
USC
TOPICS AT A GLANCE

´ Brief introduction to Linux kernel


´ Linux executable format (ELF)

´ Linux boot process

´ Linux bootloader (GRUB)

´ Linux kernel compile procedure


LINUX OS (SYSTEM PROGRAMMER’S VIEW)
™ Any Operating System is implemented as two modules
at a macro level.
• Module 1- User interface functionality (Shell)
• Module 2- Request Processing functionality (kernel)
™ Shell is responsible for interaction with the user, which
collects user requests and forward it to the kernel to
process.
™ Shell scripting/programming is customizing the user
interface of an OS and nothing to do with the kernel.
™ Kernel itself is a module, which has different sub
modules
LINUX KERNEL (OVERVIEW)
Process management Access IPC Memory management

LOADER

I/O File System and NETWORK SERVICES


Device Drivers
KERNEL

ARCHITECTURE
SPECIFIC (AS)

HARDWARE PERIPHERALS
CONTD..
´ Architecture Specific (AS) , Kernel and Loader are the
key units which together are called the Engine of an OS
´ Architecture Specific
• AS layer contains the CPU initialization , Board
initialization code.
• In Embedded Systems terminology, it is referred to as
the Board Support Package (BSP)
• AS of Linux is very much specific to the hardware
architecture.
• AS has all the startup drivers which initialize the MMU
and on board devices.
KERNEL
´ Within Kernel, there is a layer called In-Kernel which is the
core of the kernel.
´ In-Kernel is responsible for the boot process.
´ When system is initialized and ready to use, Loader starts
up. Loader is started by the Linux Kernel.
´ The rest of the boxes in the figure are just used to provide
Services, called Service Layers.
´ For a general purpose OS, all the services needed, where as
for a RT-OS, depending on the requirement the services are
provided.
´ Hence because of this modular nature of the Linux kernel,
the size of Linux kernel varies a lot making it highly scalable.
KERNEL CONTD..
´ Loader contains routines to receive requests from the shell and
process them by forwarding to the respective service layers.
´ Kernel processes include :-
ƒ Process Management
ƒ Access
ƒ IPC
ƒ Memory Management
ƒ N/W services
ƒ I/O file system
ƒ Device drivers (High level drivers, as Initial drivers initialized by AS)
ƒ File system
KERNEL CONTD..
´ At start up time, memory is divided into two parts, user
space and kernel space.
´ Shell and applications are resided on the user space.
´ Shell and applications cannot access the K space where
as kernel space can access the whole of memory,
including the user space.
´ Windows being a “user-friendly” OS puts the shell as
one of the kernel service.
´ But the disadvantage is that both the shell and kernel
cannot be upgraded/customized independently.
´ It also saves kernel from many application level bugs,
making the kernel more safe.
KERNEL INTEGRATION
™ Kernel is an aggregation of many source codes (Loader, Services etc..)
which have to be compiled and built to convert to an executable image
in order to use it. This is called Kernel Integration.
™ There are two types of Kernel Integration
• Monolithic integration
• Micro integration
™ In Monolithic integration, the entire kernel including the services are
compiled and built into the memory.
™ In Micro integration, only the kernel, AS and the loader are compiled and
built into the memory where as the services are installed only when
required.
™ Hence Monolithic kernels occupy more memory but are much faster as
they avoid the compiling and loading time where as micro kernels
occupy less memory but have high response time.
™ What does Linux kernel support??
MODULAR INTEGRATION
´ Linux defines Modular integration. It gathers the
components which are needed frequently and makes
them Static and keeps rest as modules.
´ In Linux the static ones are the Loader, BSP and some
services like Process and memory management,
´ Rest of the dynamic modules/processes can also be
made Static depending on the environment. This
flexibility gives Linux to be used as embedded and
RTOS.
EXECUTABLES IN LINUX
´ Any executable file when formed consists of two parts: Functionality
and Runtime.
´ When an executable gets loaded into memory (process address
space), it requires key resource allocation to support the execution of
functionality. Runtime code is responsible to acquire such resources.
´ Runtime code involves making system calls to acquire resources, as
system calls are OS specific, this makes Runtime and hence the
executable to be OS specific.
´ Runtime code in Linux is called GNU runtime, which consists of three
functions: Init, Start and Fini.
´ Any application starts with an Init which does the required resource
allocation. Functionality of an executable is triggered with Start. Fini
is called after the execution of application for freeing up resources.
CONTD..
´ Format is a standard to organize data with in a file.
´ Binary file format specifies how binary code is to be organized and
saved on disk. Assembler and linker should make use of this file
format to create an executable.
´ Binary file format is platform specific. Linux follows ELF 32 file
format.
´ The combination of Binary file format and Runtime called Application
Binary Interface (ABI) makes an executable platform dependent.
´ Hence Linux ABI is a combination of GNU runtime plus ELF 32.
´ There are 3 types of ELF files:
• shared objects (loadables) - .so
• Executables (runnables)
• Relocatables (linkables) - .o
• Assembler arranges the binaries
produced into some logical groups
(sections) according to ELF.
• An ELF 32 relocatable has 6
sections, shown here. Each section
having some virtual memory
associated.
• A specific .o file has all these 6
sections in it. Now as an
executable is formed by linking of
two or more .os and some
libraries, so an ELF 32 executable
has 24 sections.
• linker creates the PLT tables to
show the exact location of each
section.
• Any file (relocatable or
executable) has an ELF header and
image.
• Tools like objdump (Linux),
elfdump (Solaris), readelf (Unix)
can dump all these sections.

ELF FILE FORMAT


LINUX BOOT PROCESS (OVERVIEW)
´ System boot up – Power
BIOS/Boot monitor up/reset

BIOS/Boot
´ Stage 1 boot loader – monitor

MBR
MBR
´ Stage 2 boot loader –
LILO/GRUB LILO/GRUB

´ Kernel - Linux
´ Init – user space Linux

´ Gettys trigger
User-space
operation
SYSTEM BOOT UP
´ There is a slight difference in the start up between PC
and embedded platform.
´ Embedded platform
• Embedded platforms have a bootstrap environment
used when system is powered on or reset.
• Embedded platforms also have something called Boot
Monitor. It is a small interactive computer program
which supports an interaction for the user to select a list
of Oss, do system test and also cover abilities of first
and second level boot loaders.
• This boot monitor loads the Linux (strip down) kernel to
flash and execute it.
SYSTEM BOOT UP - PC
™ BIOS begins at address 0xFFFF0 and performs POST (Power
on Self Test) which includes the following
ƒ Initialization of CPU ( checking CPU type, speed etc.) and
probing the CPU to real mode.
ƒ Probe the memory. Allocate real mode address space i.e
architecture specific segments. No paging and virtual
memory.
ƒ Initialize basic I/O ( standard I/O,O/P, IDE controllers, hard
disk, graphic cards)
™ Jump to sector 0 of configured boot device.
™ Commonly, Linux is booted from a hard disk, where the
Master Boot Record (MBR) contains the primary boot loader.
MBR

446
bytes

64
bytes

2
bytes
MBR
´ The primary boot loader that resides in the MBR is a 512-
byte image containing both program code and a small
partition table
´ The first 446 bytes are the primary boot loader, which
contains both executable code and error message text.
´ The next sixty-four bytes are the partition table, which
contains a record for each of four partitions (sixteen bytes
each).
´ The job of the primary boot loader is to find and load the
secondary boot loader (GRUB). It does this by looking
through the partition table for an active partition.
´ When it finds an active partition, it scans the remaining
partitions in the table to ensure that they're all inactive.
GRUB ( GRAND UNIFIED BOOTLOADER)
´ When MBR calls GRUB, it reads the target of the bzimage from
/boot/grub, pulls it from there and loads into memory.
´ Bzimage is a compressed kernel image. Bzimage is a
combination of vmlinuz and bootstrap code.

BZIMAGE
BOOTSTRAP

VMLINUZ
CONTD..
™ When kernel image (bzimage) is loaded, first the bootstrap
code is loaded.
™ Bootstrap performs the following functions
• Puts the CPU to protected mode
• Sets up page tables
• Uncompresses Vmlinuz
• Calls start_kernel function
• With the call to start_kernel, a long list of initialization
functions are called to set up interrupts, perform further
memory configuration.
™ Vmlinuz starts executing. This is the process 0, also called
Swapper.
WHAT IS VMLINUZ
´ Vmlinuz is the name of the Linux kernel
executable.
´ It is a compressed kernel image which is in the
bootable form which means it has the capability to
load OS into the memory.
´ Vmlinux on the other hand is the non-compressed
version of Vmlinuz, a non bootable form. It is an
intermediate step to produce Vmlinuz.
´ Vmlinuz is located in /boot directory which is the
directory required that needs files needed to begin
booting the system.
SWAPPER
´ Swapper does the following
• Initializes CPU and probes BOGOMIPS
• Initializes the onboard controllers, timers, and the
start up drivers (called BSP drivers in embedded
systems lexicon)
• Initialize interrupt descriptor tables
• Initialize the file system
• Swapper finally starts a kernel thread called “Init”,
seen in /bin/init.
INIT
´ Init runs as a daemon with PID as 1.
´ Init then runs the Inittab file present in /etc/init.d
´ Inittab file describes which process should be started
at boot up and during the normal operation.
´ Finally, the idle task is started and the scheduler can
now take control (after the call to cpu_idle). With
interrupts enabled, the pre-emptive scheduler
periodically takes control to provide multitasking.
GETTYS
´ Init then runs the “mingetty” application ( the
one which shows the login prompt).
´ Now mingetty validates by running a task called
“passwd” present in /etc/passwd. If it fails, it
then respawns the login prompt. If it validates,
it execs the shell.
´ All the steps of Linux boot process can be seen
using the command “dmesg|more”, right
immediately after boot up.
STEPS TO COMPILE/RECOMPILE KERNEL

™ Configuring the kernel


™ Build dependencies

™ Build the kernel

™ Build the modules

™ Store it in the boot folder

™ Grub configuration
CONFIGURING THE KERNEL (2.6.X VERSIONS)
™ On the 2.6.x kernels there are three main frontend programs: config,
menuconfig, and xconfig.
™ config is the least user-friendly option as it merely presents a series
of questions that must be answered sequentially.
™ menuconfig is an ncurses based frontend. The system must have the
ncurses-devel libraries installed in order to use this utility.
™ ‘Y’ for static and include it during the kernel compile, ‘N’ for not
including in the kernel and ‘M’ for making it as module.
™ In the kernel source tree, use either of these, preferrably [2] and [3]
1) make config
2) make menuconfig
3) make xconfig
™ Make mproper should be done on the source tree before
performing kernel recompile.
MENUCONFIG
XCONFIG
BUILD DEPENDENCIES
´ The next step is to create the necessary include
files and generate dependency information. This
step is only required for the 2.4.x kernel tree.
´ make dep (only for 2.4.x kernel versions)
´ Lots of messages will scroll by. Depending on the
speed of your machine and on what options you
chose, this may take several minutes to complete.
´ Once the dependency information is created we
can clean up some miscellaneous object files. This
step is required for all versions of the kernel.
´ make clean (for all versions)
BUILD THE KERNEL
´ (finally) ready to start the actual kernel build. At
the prompt type: make bzimage
´ Some computers do not work with ‘make
bzimage’ either due to some hardware
incompatibilities or due to older versions of
bootloaders or due to smaller kernel images.
Use ‘make zimage’ or ‘make zdisk’
´ If everything went correctly then the new kernel
should exist in ./arch/$ARCH/boot.
BUILD THE MODULES
´ There is one more step needed for the build process i.e to
create all the loadable modules if we have them configured.
´ ‘make modules’
´ Once the modules are built they can be installed. If we were
building as a non-privileged user you will now need to switch
to root to complete this next step:
´ $ su
´ password:
´ $ make modules_install
´ The freshly baked modules will be copied into
/lib/modules/KERNEL_VERSION.
CHANGING LOCATION
´ Once your kernel is created, we can prepare it for
use. From the ./linux directory, copy the kernel and
System.map file to /boot. In the following
examples change KERNEL_VERSION to the version
of the new kernel.
´ $ cp arch/i386/boot/bzImage /boot/bzImage-
KERNEL_VERSION
´ $ cp System.map /boot/System.map-
KERNEL_VERSION
´ The next step is to configure our bootloader.
CONFIGURING THE BOOTLOADER
´ This forms the final step in the Linux compile procedure.
The bootloader is the first program that runs when a
computer is booted.
´ Once we have copied the bzImage and System.map to
/boot, edit the grub configuration file located in
/boot/grub/menu.lst. On some distributions
/etc/grub.conf is a symbolic link to this file.
´ # Note that we do not have to rerun grub after making
changes to this file
´ title Test Kernel (2.6.0)
root (hd0,1) kernel /boot/bzImage-2.6.0 ro
root=LABEL=/ initrd /boot/initrd-2.6.0.img
REFERENCES
Links:
1. http://www.ibm.com/developerworks/linux/library/l-
linuxboot/
2. http://www.linfo.org/vmlinuz.html
3. http://www.yolinux.com/TUTORIALS/LinuxTutorialInitProces
s.html
4. http://www.digitalhermit.com/linux/Kernel-Build-
HOWTO.html#BUILDING
Books:
1. Understanding the Linux Kernel, by Daniel. P. Bovet and
Marco Cesati, O’Reilly third edition.
2. Running Linux by Matt Wesh, O’Reilly fourth edition.
THANK YOU

Das könnte Ihnen auch gefallen