Embedded Linux Design and Programming
Download
Report
Transcript Embedded Linux Design and Programming
Introduction to
Embedded Systems
Dr. Jerry Shiao, Silicon Valley University
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
1
Section 10 Bootloader
What is a Bootloader?
Responsible for loading an Operating System Kernel.
Responsible for loading Kernel infrastructure ( i.e. File System ).
Start Kernel execution (init Process, System Daemons).
Linux Server Bootloader
System Firmware ( BIOS )
RAM-based File System to mount the root File System.
Configuration of hardware devices.
Program System Memory Controllers.
Initialize Processor Caches.
Enables Hardware Devices.
Network Boot Infrastructure.
Interrupt Routing details.
Load Kernel / Load File System / Start Kernel
Embedded Linux Bootloader
Performs System Firmware tasks through Bootloader.
Load Kernel / Load File System / Start Kernel
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
2
Section 10 Bootloader
Linux In Desktop PC – Power Up
Spring 2014
BIOS in host (X86 system)
Program core system components (RAM Controllers, CPU State /
Registers), APIC Tables (Interrupt Routing), Flush Processor Cache.
POST (Power-On-Self-Test).
BIOS access the hard drive Master Boot Record.
In MBR is GRUB, Linux bootloader in Linux distributions.
Load Kernel Image, Load File System, Starts Kernel.
GRUB provides choice to boot one of multiple OS (i.e. Linux or Windows).
/boot/grub/grub.conf contains GRUB configuration file with menu
selection (i.e. which OS) and disk partition containing Linux Kernel and
the root file system.
GRUB loads Linux Kernel from /boot directory ( contains Linux Kernel(s),
Kernel System Map(s), initrd (initial Ramdisk with drivers), Kernel config
file(s) from Kernel build ).
Initrd is temporary root file system with executables (i.e. insmod) and
drivers to mount the root file system on disk. After mounting, initrd is
unmounted and memory freed.
NOTE: In embedded Linux systems, initrd is the final root file system.
Linux Kernel starts init process. Init process is the root/parent process of all
other process executing on Linux.
Init process runs script, /etc/rc.d/rc.sysinit.
Init process runs script, /etc/inittab, to execute scripts to start processes
based on the runlevel.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
3
Section 10 Bootloader
Linux In Desktop PC – Power Up
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
4
Section 10 Bootloader
Embedded Linux Boot Process – Bootloader
Spring 2014
First stage bootloader located in small ROM (2K or 4K Bytes).
ROM bootloader loads second stage bootloader (i.e. Redboot) from fixed
address from flash to RAM. ROM bootloader must have flash driver.
NAND Flash –large, less cost, cannot execute-in-place.
NOR Flash – smaller, execute-in-place (i.e. Second Stage Bootloader
execute from NOR).
Second stage bootloader will initializes CPU, MMU, on-chip devices,
configures memory map.
POST ( Power-On-Self-Test ).
Load the Linux Kernel and RAMDisk from flash to RAM:
Automatically decompress the Linux Kernel and RAMDisk.
Manually interrupt bootloader (<cntrl> <c>) and use tftp to load Linux
Kernel from server.
After Linux Kernel starts running, bootloader is no longer in RAM.
Bootloader configuration (i.e. IP address, host system (server) IP address)
saved in high address space in flash. Only for development and testing.
Bootloader configured with the starting address space in flash where Linux is
located and where RAMDisk is located.
Bootloader executes a jump to the Kernel code to configure the
microprocessor registers and start_kernel() function.
Bootloader architecture specific. Bootloader has memory map for the flash
and for loading Kernel and RAMDisk and will be different for another
microcontroller.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
5
Section 10 Bootloader
Pro Linux Embedded
Systems, Gene Sally
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
6
Section 10 Bootloader
Embedded Linux: Execution Context
1st Stage ROM Bootloader
2nd
Assembly Code.
Loads 2nd Stage Bootloader into RAM.
Stage RAM Bootloader
Lack of Execution Context: No Resources
No RAM stack structure for C calling conventions.
Creates the Execution Context
Spring 2014
Higher-level language, “C”, cannot run.
Initialize RAM timings in the memory controller circuitry.
C Code can be used: Allow Bootloader to be written in C Code,
instead of Assembly Language.
Flush the processor caches.
Program up the CPU registers with default values.
Determine hardware installed within system and supply to
Linux Kernel in architectual-dependent software tables.
Built Independent from Linux Kernel.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
7
Section 10 Bootloader
Embedded Bootloaders
First task is porting Bootloader to controller board.
Diagnostic routines, system configuration options.
LILO (LInux LOader)
Commercial Linux Distributions.
Desktop PC (Intel x86) Architecture.
lilo.conf: Used by lilo Configuration Utility when lilo image
created.
MBR in first hard drive /dev/hda.
boot = /dev/hda
timeout=50
default=linux
image=/boot/myLinux-2.6.11.1
label=linux
initrd=/boot/myInitrd-2.6.11.1.img
read-only
append="root=LABEL=/“
other=/dev/hda1
optional
label=that_other_os
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
Primary boot image.
Initial RAMDisk.
Linux command line passed to
Linux Kernel.
Dual boot, second boot image.
8
Section 10 Bootloader
GRUB (Grand Unified Bootloader)
Commercial Linux Distribution.
Enhanced features not found in Lilo.
Understand File Systems and Kernel Image Formats.
Read and Modify its configuration at boot time.
Network boot.
Kernel command line interface at boot time.
default=0
timeout=3
splashimage=(hd0,1)/grub/splash.xpm.gz
title Fedora Core 2 (2.6.9)
root (hd0,1)
kernel /bzImage-2.6.9 ro root=LABEL=/ rhgb proto=imps quiet
initrd /initrd-2.6.9.img
title Fedora Core (2.6.5-1.358)
root (hd0,1)
kernel /vmlinuz-2.6.5-1.358 ro root=LABEL=/ rhgb quiet
title That Other OS
rootnoverify (hd0,0)
chainloader +1
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
Root Filesystem where all files are
located. First partition of first drive.
Kernel image with the Kernel
command line.
Initial RAMDisk.
9
Section 10 Bootloader
Coreboot (LinuxBIOS)
Used in x86 system without PC BIOS.
Performs the function of PC BIOS (initial system bringup,
interrupt-routing assignment, device initialization).
Coreboot typically loads a Linux kernel, but it can load any
other stand-alone ELF executable.
Written for speed and reliability for boot process.
Boots Linux in seconds.
Yamon
MIPs ROM Monitor.
Spring 2014
Bootstrap program in ROM that initializes the hardware and boots the
board Operating System during power on.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
10
Section 10 Bootloader
RedBoot
Red Hat Embedded Debug and Bootstrap firmware.
Open source application that uses the eCos real-time
operating system Hardware Abstraction Layer to provide
bootstrap firmware for embedded Linux systems.
Spring 2014
eCos (embedded configurable operating system).
One process with multiple threads.
Customizable to precise application requirements of run-time
performance and hardware needs.
Memory size in the tens to hundreds of kilobytes, or with realtime requirements.
Used on hardware with too little RAM to support embedded
Linux (currently needs a minimum of about 2 MB of RAM, not
including application and services).
Provides an interactive command line interface to allow
management of the Flash images, image download, RedBoot
configuration, etc., accessible via serial or Ethernet.
Boot scripts can be stored in Flash allowing loading of
images from Flash or a TFTP server.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
11
Section 10 Bootloader
U-Boot
Spring 2014
Most actively developed open source embedded bootloader.
Supports ARM, x86, Motorola 68K, MIPS, PowerPC, etc.
TFTP Kernel over network connection, USB, and Flash.
Supports filesystems: Cramfs (Linux), ext2 (Linux), FAT
(Microsoft), JFF2 (Linux).
U-boot and Linux Kernel retrieved to local storage (RAM).
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
12
Section 10 Bootloader
Network File Server ( NFS ).
Spring 2014
Kernel can mount Root FileSystem from remote Network File
System (NFS) Server.
/etc/exports
Describes the directories that each host or set of hosts can
access.
/data/root/virtex4 192.168.1.201(rw, no_root_squash)
/data/root/virtex4: Directory of the Root FileSystem (same
structure as Linux “/” directory.
192.168.1.201: Remote system.
rw: Remote system has read and write privilege.
no_root_squash: Allows remote system to access directory with
root privilege.
/usr/sbin/exportfs –a
Maintain the current table of exported file systems for NFS.
List of all file systems named in /etc/exports.
/sbin/service nfs restart
Restart the NFS daemons (portmap, lockd, mountd, rquotad,
statd).
File containing exported file system are read by the mountd
daemon.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
13
Section 10 Bootloader
Network File Server ( NFS )
Access Control Files control access to the NFS server.
/etc/hosts.deny
Access will be denied when a (daemon,client) pair matches an entry.
/etc/hosts/allow
Entry Format: daemon_list : client_list [ : shell_command ]
Access will be granted when a (daemon,client) pair matches an entry.
Access Control Files Entries:
portmap: ALL
Converts TCP/IP port numbers to RPC program numbers.
lockd: ALL
A daemon that is not necessary with modern kernels.
mountd: ALL
Checks for access permission for the exported filesystem.
rquotad: ALL
An RPC server providing user quota information for remote users.
statd: ALL
Implements the Network Status Monitor (NSM) RPC protocol.
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
14
Section 10 Bootloader
U-Boot
Environment Variables
Similar to how environment variables are used in UNIX shells (i.e.
bash), but used in U-Boot environment.
Saved in flash and copied to RAM when U-Boot starts.
GTA01Bv4 # printenv
bootargs=
bootdelay=3
baudrate=115200
usbtty=cdc_acm
bootargs_base=rootfstype=jffs2 root=/dev/mtdblock4 console=ttySAC0,115200 console=tty0
loglevel=8
stdin=serial
stdout=serial
stderr=serial
dontask=y
bootcmd=setenv bootargs ${bootargs_base} ${mtdparts}; bootm 0x30100000
menu_1=Set console to USB: setenv stdin usbtty; setenv stdout usbtty; se
menu_2=Set console to serial: setenv stdin serial; setenv stdout serial; \
setenv stderr serial
menu_3=Power off: neo1973 power-off
...
Environment size: 766/16380 bytes
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
15
=> setenv ipaddr 192.168.100.6 => setenv serverip 192.168.1.1 => setenv netmask 255.255.0.0 => setenv hostname canyonlands => setenv rootpath /opt/eldk-4.2
Section 10 Bootloader
U-boot
Environment Variables for NFS.
bootargs: Environment Variable automatically passed to Linux
Kernel .
=> setenv ipaddr 192.168.100.6
=> setenv serverip 192.168.1.1
=> setenv netmask 255.255.0.0
=> setenv hostname canyonlands
=> setenv rootpath /opt/eldk-4.2/ppc_4xx
=> saveenv
=> setenv nfsargs 'root=/dev/nfs rw nfsroot=${serverip}:${rootpath}‘
=> setenv bootargs ${nfsargs}
ip=${ipaddr}:${serverip}:${serverip}:${netmask}:${hostname}::off
Assigning variable “bootargs” without environment variables:
=> setenv bootargs root=/dev/nfs rw nfsroot=192.168.1.1:/opt/eldk-4.2/ppc_4xx
ip=192.168.100.6:192.168.1.1:192.168.1.1:255.255.0.0:canyonlands::off
Spring 2014
Display Boot Arguments used by the Kernel
SILICON VALLEY UNIVERSITY
$ cat /proc/cmdline
CONFIDENTIAL
16
=> setenv ipaddr 192.168.100.6 => setenv serverip 192.168.1.1 => setenv netmask 255.255.0.0 => setenv hostname canyonlands => setenv rootpath /opt/eldk-4.2
Section 10 Bootloader
Redboot
Command Set Structured around the Bootstrap Environment.
Designed to be Simple to Use and Remember.
Class of Commands:
Program Loading and Execution.
Flash image and Configuration Management.
Miscellaneous Commands.
RedBoot> COMMAND [-S]... [-s val]... Operand
Spring 2014
-s: A boolean switch. The behavior of the command will differ depending on
the presence of the switch.
-s val: A qualified value. The letter “s” introduces the value, qualifying its
meaning.
Multiple commands may be entered on a single line, separated by the semicolon “;” character.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
17
=> setenv ipaddr 192.168.100.6 => setenv serverip 192.168.1.1 => setenv netmask 255.255.0.0 => setenv hostname canyonlands => setenv rootpath /opt/eldk-4.2
Section 10 Bootloader
Redboot
RedBoot> help
Execute code at a location
Manage aliases kept in FLASH memory
go [-w <timeout>] [-c] [-n] [entry]
alias name [value]
Help about help?
Set/Query the system console baud rate
help [<topic>]
baudrate [-b <rate>]
Set/change IP addresses
Manage machine caches
ip_address [-l
<local_ip_address>[/<mask_length>]] [-h
cache [ON | OFF]
<server_address>]
Display/switch console channel
Load a file
channel [-1|<channel number>]
load [-r] [-v] [-d] [-c <channel>] [-h <host>] [-m
Display disk partitions
{TFTP | HTTP | {x|y}MODEM | disk}]
disks
[-b <base_address>] <file_name>
Display (hex dump) a range of memory
Network connectivity test
dump -b <location> [-l <length>] [-s]
ping [-v] [-n <count>] [-t <timeout>] [-i <IP_addr]
Execute an image - with MMU off
-h <host>
exec [-w timeout] [-b <load addr> [-l <length>]]
Reset the system
[-r <ramdisk addr> [-s <ramdisk length>]]
reset
[-c "kernel command line"] [<entry_point>]
Display RedBoot version information
Manage flash images
version
fis {cmds}
Display (hex dump) a range of memory
Manage configuration kept in FLASH memory
x -b <location> [-l <length>] [-s]
fconfig [-i] [-l] [-n] [-f] [-d] | [-d] nickname
SILICON VALLEY UNIVERSITY
[value]
Spring 2014
CONFIDENTIAL
18
=> setenv ipaddr 192.168.100.6 => setenv serverip 192.168.1.1 => setenv netmask 255.255.0.0 => setenv hostname canyonlands => setenv rootpath /opt/eldk-4.2
Section 10 Bootloader
Redboot
RedBoot> fconfig -l
Run script at boot: true
Boot script:
.. fis load ramdisk
.. fis load zImage
.. exec -r 0x800000 -s 0x300000
Boot script timeout (1000ms resolution): 1
Use BOOTP for network configuration: false
Gateway IP address: 0.0.0.0
Local IP address: 192.168.1.200
Local IP address mask: 0.0.0.0
Default server IP address: 0.0.0.0
DNS server IP address: 0.0.0.0
Set eth0 network hardware address [MAC]: true
eth0 network hardware address [MAC]: 0x00:0x00:0x00:0x00:0x4C:0x33
GDB connection port: 9000
Force console for special debug messages: false
Network debug at boot time: false
RedBoot>
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
19
=> setenv ipaddr 192.168.100.6 => setenv serverip 192.168.1.1 => setenv netmask 255.255.0.0 => setenv hostname canyonlands => setenv rootpath /opt/eldk-4.2
Section 10 Bootloader
Redboot
... Resetting.
+Ethernet eth0: MAC address 00:00:00:00:4c:33
IP: 192.168.1.200/255.255.255.0, Gateway: 0.0.0.0
Default server: 0.0.0.0, DNS server IP: 0.0.0.0
RedBoot(tm) bootstrap and debug environment [ROMRAM]
Non-certified release, version v2_0 - built 23:18:18, Jan 15 2006
Platform: Cirrus Logic EDB9302 Board (ARM920T) Rev A
Copyright (C) 2000, 2001, 2002, Red Hat, Inc.
RAM: 0x00000000-0x02000000, 0x00041e48-0x01fdd000 available
FLASH: 0x60000000 - 0x60400000, 32 blocks of 0x00020000 bytes each.
== Executing boot script in 1.000 seconds - enter ^C to abort
RedBoot> fis load ramdisk
RedBoot> fis load zImage
RedBoot> exec -r 0x800000 -s 0x300000
Using base address 0x00080000 and length 0x0017b380
Uncompressing Linux.............................................................
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
20
=> setenv ipaddr 192.168.100.6 => setenv serverip 192.168.1.1 => setenv netmask 255.255.0.0 => setenv hostname canyonlands => setenv rootpath /opt/eldk-4.2
Section 10 Bootloader
Redboot
NFS Configuration.
Spring 2014
fis load zImage
exec -c
"ip=192.168.1.200:192.168.1.230:192.168.1.230:255.255.255.0::eth0:off
ethaddr=00:00:00:00:4c:33 root=/dev/nfs rw
nfsroot=192.168.1.230:/tftpboot/initrd noinitrd rootdelay=2"
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
21
=> setenv ipaddr 192.168.100.6 => setenv serverip 192.168.1.1 => setenv netmask 255.255.0.0 => setenv hostname canyonlands => setenv rootpath /opt/eldk-4.2
Section 10 Bootloader
Redboot
NFS Configuration.
...
eth0: generated random MAC address d2:21:b6:29:45:70.
IP-Config: Complete:
device=eth0, addr=192.168.1.200, mask=255.255.255.0, gw=192.168.1.230,
host=192.168.1.200, domain=, nis-domain=(none),
bootserver=192.168.1.230, rootserver=192.168.1.96, rootpath=
Waiting 2sec before mounting root device...
Looking up port of RPC 100003/2 on 192.168.1.96
Looking up port of RPC 100005/1 on 192.168.1.96
VFS: Mounted root (nfs filesystem).
Freeing init memory: 112K
init started: BusyBox v1.00 (2004.12.18-13:12+0000) multi-call binary
Please press Enter to activate this console.
BusyBox v1.00 (2004.12.18-13:12+0000) Built-in shell (ash)
Enter 'help' for a list of built-in commands.
~#
Spring 2014
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
22