Ch2 Bootloader

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Lecture 2 - Bootloader

ARM architecture overview Bootloader overview Case study UBoot


PART1: Startup PART2: Monitoring mode PART3: OS Boot

Available bootloaders for Linux

ARM Architecture Overview


ARM Operating Modes
User mode: a normal program execution state FIQ Fast Interrupt: for fast interrupt handling IRQ Normal Interrupt: for general purpose interrupt handling Supervisor mode (SVC): a protected mode for operating system Abort mode: when a data or instruction pre-fetch is aborted Undefined mode: when an undefined instruction is executed System mode: a privileged user mode for the operating system

ARM Processor States


Arm state: all instructions are 32bits long, word-aligned. Thumb state: all instructions are 16 bits wide, half-word aligned. Jazelle state: all instructions are 8 bits wide for Java Bytecode (for v5TEJ only)
2

ARM Architecture Overview


ARM-State Registers

ARM Architecture Overview


Thumb-State Registers

ARM Architecture Overview


Program Status Register (PSR)
CPSR: Current Program Status Register SPSR: Saved Program Status Register

ARM Architecture Overview


ARM Exception Types
Reset
Hardware reset: when the processor reset pin is asserted Software reset: by branching to the reset vector (0x0000)

Undefined instruction
the processor cannot recognize the currently execution instruction

Software Interrupt (SWI)


By s/w instruction, to allow a program running in User mode to request privileged operations that are in Supervisor mode

Prefetch Abort
Fetch an instruction from an illegal address

Data Abort
A data transfer instr. try to load or store data at an illegal address

IRQ: The processor IRQ pin is asserted and the I bit in CPSR is clear FIQ: The processor FIQ pin is asserted and the F bit in CPSR is clear
6

ARM Architecture Overview


ARM Exception Vectors

(1)

Memory
7

ARM Architecture Overview


ARM Instruction Set
Data processing instruction (x24)
Arithmetic instruction (x8) Bit-wise Logic instruction (x4) Movement instruction (x2) Comparison instruction (x4) Shift and rotator instruction (x6)

Load and store instructions (x4)


Single-register load/store instructions (x2) Multiple-register load/store instructions (x2)

Branch instruction (x2) Status Register Transfer instruction (x2) Exception Generating instructions (x2)
8

ARM Architecture Overview


ARM Instruction Summary

ARM Architecture Overview


ARM Instruction Summary

10

ARM Architecture Overview


ARM Instruction Condition Code
In ARM, all instructions can be conditionally executed according to the state of the CPSR condition codes

11

Bootloader Overview
Bootstrapping
When system is powered on, the first piece of code (called bootstrapping) initializes basic hardware and then bring up OS kernel. It can be included in a bootloader, or in OS directly.

Reference documents that you need


The data sheet and users manual of the target platform Processors Architectural Reference Manual Processors developers guide The user guides for Processors Assembly, Compiler and Linker. Boot requirement for the target OS

12

Bootloader Overview
Bootloader Selection
The Processor architectures and platforms that it supports The OS that it supports Debugging abilities, Stability and Portability Functionalities: simple bootup, or with monitoring capability and lots of drivers support. The medium they are installed in usually ROM, EEPROM or Flash The storage device from which the kernel image is downloaded host PC, local Flash, disk, CF, etc. The device and protocol by which the kernel image is transmitted Serial port, USB, ethernet, IDE, etc. The 'host-side' software used to transmit the kernel image if it is coming from an external device/machine. 'sleep' reset support Not to do a normal reboot if this was a sleep wakeup.
13

Case Study - UBoot


Das UBoot features
UBoot stands for Universal Bootloader Open source bootloader, under GPL license Source code available at http://sourceforge.net/projects/u-boot Based on PPCBoot and ARMBoot projects Support many CPU architectures and platform types ARM, PPC, MIPS, x86, and their variance Support many OS Linux, NetBSD, VxWorks, QNX, RTEMS, ARTOS, LynxOS Support booting image from TFTP, IDE/SCSI HDD, Flash, Support lots of device drivers and monitor commands Supports user configuration using Environment Variables which can be made persistent by saving to Flash memory. Support tool to compress (by gzip, bzip2) and pack images
14

Part- I: Startup

Make U-Boot
Check the target boards supported by U-Boot
vi /u-boot-1.1.2/makefile :
smdk2410_config : unconfig @./mkconfig $(@:_config=) arm arm920t smdk2410 NULL s3c24x0

Config your target board smdk2410 as an example


[root@test u-boot-1.1.2]# make smdk2410_config Configuring for smdk2410 board .

Make u-boot
[root@test u-boot-1.1.2]# make ELF format : Motorola S-Record -Map u-boot.map o u-boot arm-linux-objcopy --gap-fill=0xff O srec u-boot u-boot.srec arm-linux-objcopy --gap-fill=0xff O binary u-boot u-boot.bin
plain binary
15

Part- I:

UBoot Startup
_start: 1. setup exception vector table, and each handler 1. 2. 3. 4. set SVC mode turn off watchdog disable all IRQs set System Clock (data sheet 7-22)

reset:

cpu_init_crit:

memsetup:

1. flush I and D cache 2. flush TLB 3. disable MMU stuff and caches In /board/smdk2410/memsetup.S 1. enable and set memory 2. return to /cpu/arm920t/start.S 1. return to reset:
16

cpu_init_crit:

reset:

Part- I:

UBoot Startup
reset: 1. Setup C environment and run on RAM - copy U-Boot code/ initialized data to RAM
from: _start: 0x0, Flash base to: _TEXT_BASE, 0x33F80000, Entry on RAM size: _armboot_start ~ bss_start (0x33F80000) (0x33F9-8558)

relocate:

- setup stack pointer _start_armboot:


sp = _TEXT_BASE glb data heap irq_stack fiq_stack abrt_stack - clear BSS (_bss_start ~ _bss_end )

start_armboot()

main_loop()

In common/main.c

In lib_arm/board.c 1. Flash initialization 2. Init environment 3. Set IP addr 4. Set MAC addr 5. Devices initialize 6. Console initialize 7. Enable interrupts 8. Enable Ethernet

In C code

17

Part- I:

Linker Descriptor

U-boot-1.1.2 /board/smdk2410/u-boot.lds

[root@test u-boot-1.1.2]# make


: :

arm-linux-ld Bstatic Tuboot-1.1.2/board/smdk2410/u-boot.lds Ttext 0x33F80000

U-boot-1.1.2/board/smdk2410/config.mk
TEXT_BASE = 0x33F8000 UBoot entry point address
18

Part- I:

UBoot trace- startup (1/7)

U-boot-1.1.2 /cpu/arm920t/start.S

19

Part- I:

UBoot trace- startup (2/7)

U-boot-1.1.2 /cpu/arm920t/start.S

20

Part- I:

UBoot trace- startup (3/7)

U-boot-1.1.2 /cpu/arm920t/start.S

21

Part- I:

UBoot trace- startup (4/7)

U-boot-1.1.2 /cpu/arm920t/start.S

22

Part- I:

UBoot trace- startup (5/7)

U-boot-1.1.2 /cpu/arm920t/start.S

/lib_arm/board.c
23

Part- I:

UBoot trace- startup (6/7)

U-boot-1.1.2 /cpu/arm920t/start.S

/board/smdk2410/memsetup.S
24

Part- I:

UBoot trace- startup (7/7)

U-boot-1.1.2 /board/smdk2410/memsetup.S

r0 as src (SMRDARA addr on Flash) r1 as dest (BWSCON reg addr) r2 as copy length (13 words)

.. ..

13 words

......
25

Part- II: Monitoring mode

UBoot Commands
help / ?

- print online help

list all UBoot commands

boot bootelf bootm bootp tftpboot go

- boot default, i.e., run bootcmd - boot from an ELF image in memory - boot application image from memory - boot image via network using BootP/TFTP - boot image via network using TFTP protocol - start application at address addr - memory copy - memory display - memory modify - simple RAM test

Boot

cp md mm mtest

Memory

erase finfo protect imls

- erase FLASH memory - print FLASH memory information - enable or disable FLASH write protection - list all images found in flash - set environment variables - save environment variables (on flash) - print environment variables

Flash

setenv saveenv printenv

Environment variables
26

Part- II:

UBoot Environment Variables


ethaddr, ipaddr, gatewayip, netmask: target board information serverip: tftp server IP address bootfile: filepath/name on the tftp server TFTP download info fileaddr: addr on the target to put the file bootargs: kernel commands bootdelay: auto-boot delay time auto-boot Kernel info bootcmd: auto-boot startup command EX.1
... boot from network # setenv bootfile /tftpboot/multi-2.4.18.img # setenv fileaddr 33000000 # setenv bootargs root=/dev/ram0 console=ttyS0,115200 init=linuxrc # setenv boot tftp\; bootm 33000000 # setenv bootcmd run boot # saveenv # erase 20000 1effff # cp.b 33000000 20000 $(filesize) # setenv boot cp.b 20000 33000000 1059a4 \; bootm 33000000 # saveenv
27

EX.2 # tftp

download image and put on flash

boot from flash

Part- II:

UBoot trace- Commands


main_loop()
s = getenv ("bootdelay"); bootdelay = s; s = getenv ("bootcmd");
s = NULL; bootdelay >= 0; !abortboot( bootdelay )

U-boot-1.1.2 /common/main.c

Y
run_command(s) len = readline() run_command() cmdtp = find_cmd() (cmdtp->cmd) ()

If booting is NOT aborted by the user during the initial bootdelay seconds, it will run the commands pre-set in bootcmd automatically. Otherwise it will run the commands input by user in the run-time. In common/command.c Look up commands in the command table call function to do the command
28

Part- III: OS Boot

Boot Requirements for Linux (1/6)


To boot Linux, bootloader has to load two things into memory
the Linux kernel
also possible to configure the kernel to run on FLASH/ ROM

a root filesystem (RFS)


which usually takes the form of an initial RAMdisk (or 'initrd') which is a gzip-compress normal ext2 filesystem image it can also be a ramfs, cramfs or jffs2 image.

Most boot-loaders are capable of two operation modes


Bootloading
autonomously load its Kernel and RFS from an internal device - i.e. the normal booting procedure.

Downloading
downloads the Kernel and RFS from an external device, often under the control of that device. This is the mode that is used to first install the Kernel and RFS, and for subsequent updates.

29

Part- III:

Boot Requirements for Linux (2/6)


What does the loader do before giving control to Linux kernel? 1. Initialize base hardware
CPU speed, memory timings, detect location and size of RAM (passed to Kernel by tagged list) detect machine type (passed to Kernel by r1) any device that needs for reading Kernel and RFS images Init UART to be Kernel console (passed to Kernel by tagged list.)

2. Initialize devices

3. Copy Kernel and RFS images to RAM


arrange a block of contiguous physical memory for the Kernel, and another contiguous one for RFS (no need to be adjacent) Copy (or download remotely) and decompress (if needed) the Kernel and RFS image to their contiguous areas Note. Linux Kernel kernel uses 16K of RAM below it to store Page Tables. The recommended placement is 32KB into RAM.
30

Part- III:

Boot Requirements for Linux (3/6)


4. Setup the Kernel Tagged List (see next slide)
5. Calling the Linux Kernel with following settings
CPU register settings
r0 = 0, r1 = machine architecture number (the MACH_TYPE_XXX in kernel) Must match one of define in linux/arch/arm/tools/mach-types. r2 = physical address of tagged list in system RAM

CPU mode
All forms of interrupts must be disabled (IRQs and FIQs) CPU must be in SVC mode

Cache, MMU
MMU must be off D-cache must be off and no any stale data (I-Cache can be on/off)

Device
DMA to/from devices should be quiesced.
31

Part- III:

Boot Requirements for Linux (4/6)


How does boot-loader pass data to the Kernel?
CPU registers (r0, r1, r2) Kernel Tagged List (located in memory)
A tag includes a header and a body
Tag header has two fields: tag ID and tag size (header+body) Tag body: the structure depends on tag ID.

A tagged list starts with ATAG_CORE and ends with ATAG_NONE


ATAG_CORE may or may not be empty. An empty ATAG_CORE has size = 2, ATAG_NONE has size = 0

The loader must pass at a minimum the size and location of the system memory (specified in ATAG_MEM).
A minimum Tagged list: ATAG_CORE, ATAG_MEM, ATAG_NONE.

There are also some other TAGs for use


ATAG_RAMDISK: how the ramdisk will be used in kernel ATAG_INITRD: virtual addr of the compressed ramdisk image ATAG_INITRD2: physical addr of the compressed ramdisk image ATAG_CMDLINE
32

Part- III:

Boot Requirements for Linux (5/6)


TAG format header TAG ID:
TAG size: (header+body)

ATAG_CORE ATAG_MEM ATAG_RAMDISK ATAG_INITRD ATAG_INITRD2 ATAG_CMDLINE

start

body depends on TAG type


Increasing address

Tagged list

ATAG_CORE ATAG_MEM ATAG_NONE A minimum tagged list

:
ATAG_NONE
end

An example of a tagged list

Note:

For some embedded systems, the tagged list is hard-coded in the Kernel. In such case, the bootloader does not need to setup it.
33

Part- III:

Boot Requirements for Linux (6/6)


ARM Linux Kernel Boot Requirements
1. Setup and initialise RAM
Existing boot loaders: MANDATORY New boot loaders: MANDATORY

2. Initialise one serial port


Existing boot loaders: OPTIONAL, RECOMMENDED New boot loaders: OPTIONAL, RECOMMENDED

3. Detect the machine type


Existing boot loaders: OPTIONAL New boot loaders: MANDATORY

4. Setup the kernel tagged list


Existing boot loaders: OPTIONAL, HIGHLY RECOMMENDED New boot loaders: MANDATORY

5. Calling the kernel image (with special settings)


Existing boot loaders: MANDATORY New boot loaders: MANDATORY

Reference http://www.arm.linux.org.uk/developer/booting.php
34

Part- III:

UBoot Tool - mkimage


Build mkimage and intall
[root@test u-boot-1.1.2]# make tools [root@test u-boot-1.1.2]# cp tools/mkimage /usr/bin

Usage:
mkimage A arch O os T type C compress a loadaddr e entrypoint \ n name d data_file[:data_file] outputimage

Example 1: Kernel
[root@test tftpboot]# gzip -9 < Image > Image.gz [root@test tftpboot]# mkimage n Kernel 2.4.18 A arm O linux \
> T kernel C gzip a 30008000 e 30008000 \ > -d Image.gz vmlinux-2.4.18.img

Example 2: Kernel+RFS
[root@test tftpboot]# mkimage n Kernel+initrd 2.4.18 A arm O linux \
> T multi C gzip a 30008000 e 30008000 \ > -d Image.gz:initrd.gz multi-2.4.18.img
35

Part- III:

UBoot trace- Linux boot (1/3)

U-boot-1.1.2 /include/image.h

Image header
36

Part- III:

UBoot trace- Linux boot (2/3)


do_bootm()
Read image header Check image magic number Verify image header checksum Verify image data checksum Show image information Check hdr->ih_comp Uncompress image Check hdr->ih_type Check hdr->ih_os run corresponding OS function

U-boot-1.1.2 /common/cmd_bootm.c

must be the same to IH_MAGIC

Uncompress image using gzip or bzip2 based on ih_comp For IH_TYPE_STANDALONE, run from its hdr->ih_ep; Otherwise, handle it later For IH_OS_LINUX, run do_bootm_linux(), which is in /lib_arm/ armlinux.c
37

Part- III:

UBoot trace- Linux boot (3/3)


do_bootm_linux()
*commandline = getenv ("bootargs"); theKernel = (void (*)(int, int, uint))ntohl(hdr->ih_ep); Check whether initrd image exists Setup Tagged list setup_start_tag() setup_memory_tags() setup_initrd_tag( ) setup_commandline_tag(commandline) setup_end_tag() cleanup_before_linux (); theKernel (0, arch_number, boot_params);

U-boot-1.1.2 /lib_arm/armlinux.c

see P.xx for whats bootargs If initrd exists, load it as well disable interrupts turn off I/D-cache flush I/D-cache ARM uses r0~r3 for function parameters. So, this will make r0 = 0 r1 = arch_number r2 = boot_params before startup Kernel
38

Available Bootloaders for Linux (1/3)


Bootloaders for Multiple Architectures
U-Boot (http://sourceforge.net/projects/u-boot/ ) Universal Boot loader supports PowerPC, ARM, MIPS, RedBoot (http://www.redhat.com/embedded/technologies/redboot/) By RedHat. capable of flash and network booting of Linux kernel. Supports ARM, MIPS, PowerPC, Hitachi SHx, and x86. Smart Firmware (http://www.codegen.com/SmartFirmware/) By CodeGen. It is designed to be very easy and fast to port. Supports M68K, PowerPC, x86, MIPS, Alpha, Sparc, ARM, MicroMonitor (http://www.linuxdevices.com/links/LK3381469889.html) A open source embedded system boot platform. The majority of the code is CPU and platform independent and has been used with a variety of different embedded operating systems. x86, Mips, PowerPC, SH, ARM, and M68K.
39

Available Bootloaders for Linux (2/3)


Bootloaders for X86 Architectures
Lilo (http://www.acm.uiuc.edu/workshops/linux_install/lilo.html) GRUB (http://www.gnu.org/software/grub/) Etherboot (http://etherboot.sourceforge.net/)
The bootROM on NIC can download code over Ethernet to run on x86.

LinuxBIOS (http://www.acl.lanl.gov/linuxbios/index.html)
replacing the normal BIOS with fast boot from a cold start.

ROLO (ftp://www.elinos.com/pub/elinos/rolo/)
By SYSGO. Capable of booting directly from ROM without BIOS.

SYSLINUX (http://syslinux.zytor.com/)
It is a lightweight bootloaders for floppy media.

NILO (http://nilo.sourceforge.net/)
it is the Network Interface Loader. NILO will boot Linux, FreeBSD, Windows 95/98/NT4 and support the Intel PXE standard.
40

Available Bootloaders for Linux (3/3)


Bootloaders for ARM(TM) Architectures
Blob (http://www.lart.tudelft.nl/lartware/blob/)
Boot Loader OBject for SA1100's

IPAQ (http://www.handhelds.org/Compaq/iPAQH3600/OSloader.html)
A bootloader, which can boot Linux from Windows CE.

Bootloaders for PPC(TM) Architectures


PPCBoot (http://ppcboot.sourceforge.net/)
the PPCBoot project has been superceded by U-Boot.

Yaboot (http://penguinppc.org/projects/yaboot/)
It works on "New" class PowerMacs (iMac and later) only.

Bootloaders for MIPS(TM) Architectures


PMON (http://pmon.groupbsd.org)

Bootloaders for Sun(TM) Architectures


Sun3 Bootloader (ftp://sammy.net/pub/m68k/sun3/boot/)

Bootloaders for Super-H(TM) Architectures


sh-ipl+g (ftp://ftp.m17n.org/pub/super-h)
sh-ipl+g stands for SH Initial Program Loader with gdb-stub.
41

References
ARM7TDMI Technical Reference Manual http://www.eecs.umich.edu/~tnm/power/ARM7TDMIvE.pdf Samsung S3C2410X 32-Bit RISC Microprocessor users manual http://www.samsung.com/Products/Semiconductor/SystemLSI/ MobileSolutions/MobileASSP/MobileComputing/S3C2410X/um_ s3c2410s_rev12_030428.pdf U-Boot source code http://sourceforge.net/projects/u-boot Related documents: PPCBoot: http://ppcboot.sourceforge.net ARMBoot: http://armboot.sourceforge.net ARM Linux Kernel Boot Requirement http://www.arm.linux.org.uk/developer/booting.php
42

You might also like