Android多核心嵌入式多媒體系統設計與實作

Bootloader and Linux Kernel 於嵌入式系統發展與重要性

賴槿峰 (Chin-Feng Lai) Assistant Professor, institute of CSIE, National Ilan University Sep 22th 2012 © 2012 MMN Lab. All Rights Reserved

2012 資訊軟體技術人才培訓 Outline

• Bootloader • Bootloader Introduction • Bootloader overview • Das U-Boot • OMAP boot sequence • Kernel • Kernel Introduction • Kernel architecture overview • Kernel boot step • Kernel configure

2

• Bootloader • Kernel • Lab

3 Bootloader Introduction • Role of android bootloader

File System

Linux Kernel

Hardware Bootloader

4 Bootloader Introduction

• What is Bootloader • The first software program that runs when a computer starts • Derived from the phrase “to pull oneself up by one's bootstraps

5 Bootloader Introduction

• Role of a Bootloader • Initialize hardware for programs • Read/Write non-volatile storage devices • Provide interfaces for loading/running programs • Memory dumping/modifying • Bootloader Challenges • DRAM Controller • Flash and RAM • Image Complexity • Execution Context

6 Bootloader Introduction

• Bootloader Challenges - DRAM Controller • Require detailed knowledge of DRAM architecture, controller, chips, and overall hardware design. • Bootloader needs to enable the memory subsystem at first to provide resource for execution environment. • Bootloader Challenges - Flash and RAM • Bootloader is stored in nonvolatile storage but is usually loaded into RAM for faster execution. • Bootloader must create its own operational context and move itself.

7 Bootloader Introduction

• Bootloader Challenges - Image Complexity • Complete control over how the image is constructed and linked. • Need to organize the startup code for specific processor’s boot sequence. • Linker for constructing a binary executable image.

8 Bootloader Overview

9 Bootloader Overview -

• Boot (able to be updated) • Identify, test, and initialize system devices • Other complex functions • Booting device • System clock • ACPI (Advanced Configuration and Power Interface) • Tools for overclocking • Hardware configuration • …etc

10 Bootloader Overview – GUN GRUB

• GRand Unified Bootloader • Feature • Provides the choice to boot one of multiple OS • Download OS images from a network • Support diskless systems\ • Provides a simple, bash-like, command line interface

11 Bootloader Overview – Das U-boot

• Open source universal bootloader • Support many processor architectures • ARM . MIPS . X86 • http://sourceforge.net/projects/u-boot • Easy to port to new architectures • Used for interacting with users and updating images and leading the kernel

12 Das U-boot

• Das U-Boot source tree . |-- api |-- doc |-- lib_mips |-- net |-- api_examples |-- drivers |-- lib_nios |-- nios2_config.mk |-- arm_config.mk |-- examples |-- lib_nios2 |-- nios_config.mk |-- avr32_config.mk |-- fs |-- lib_ppc |-- onenand_ipl |-- blackfin_config.mk |-- i386_config.mk |-- lib_sh |-- post |-- board |-- include |-- lib_sparc |-- ppc_config.mk |-- built |-- lib_arm |-- m68k_config.mk |-- README |-- CHANGELOG |-- lib_avr32 |-- MAINTAINERS |-- rules.mk |-- common |-- lib_blackfin |-- MAKEALL |-- sh_config.mk |-- config.mk |-- libfdt |-- Makefile |-- sparc_config.mk |-- COPYING |-- lib_generic |-- microblaze_config.mk `-- tools |-- cpu |-- lib_i386 |-- mips_config.mk |-- CREDITS |-- lib_m68k |-- mkconfig |-- disk |-- lib_microblaze |-- nand_spl

13 Das U-boot

• Das U-Boot source tree Directory Description api U-Boot machine/arch independent API for external applications board Board dependent files/directories common Misc architecture independent functions cpu CPU specific files Disk Code for disk drive partition handling Doc Basic documentation files drivers Device drivers for common peripherals include Header files (.h) lib_xxx Files generic to the XXX architecture net Networking support (bootp,tftp, rarp, nfs, and so on) nand_spl Support for NAND Flash boot with stage 0 boot loader

14 Das U-boot

• Das U-boot Porting Flow

Get the U-boot source code Configure u-boot by modifying the .h header toolchain for arm file

Modify Makefile Define a physical memory map u-boot-1.X-XXX.bin

Define a memory link map with the following sections load file (.bin ) , Execute

15 Das U-boot

• Das U-boot Porting Flow

1. Get the source from Get the U-boot source code • ftp://ftp.denx.de/pub/u-boot/ 2. Get the toolchain from • http://www.codesourcery.com/ toolchain for arm 3. Uncompress toolchain and modify $PATH • Host $ tar -xvf arm-none-linux-guneabi.src.tar • Host $ vim ~/.bashrc ( add export PATH=$PATH:ToolPATH) • Host $ source ~/.bashrc

16

Das U-boot – Porting flow

• The include directory: • Configs/.h • includes hardware configuration for memory map and peripherals. • .h for example arm926ejs.hh • contains the processor specific , register definitions.

17 Das U-boot – Porting flow

• The cpu directory: • /cpu.c • This file contains the cpu specific code. • /interrupt.c • contains the function related to interrupt and timer • /Start.S • contains startup code for ARM cpu core.

/cpu.c

18 Das U-boot – Porting flow

/interrupt.c

/Start.s

19 Das U-boot – Porting flow

• The lib_ directory: • board.c • memory map initialization , u-boot peripheral initialization. • div0.c • contains division-by zero exception handler

20 Das U-boot – Porting flow

• The board directory: • < boardname >/ < boardname >.c • contains functions like board initialization routine, Timer, Serial Port, NAND flash initialization etc

21 Das U-boot – Porting flow

• Modify the top level makefile Modify Makefile • to specify the new configuration.

_config : unconfig @./mkconfig $(@:_config=)

• Host $ make distclean u-boot-1.X-XXX.bin • Host $ make _config • Host $ make

22

Das U-boot Command

Command Function boot boot application image from memory bootp boot image via network using BootP/TFTP protocol bootd boot default, i.e., run 'bootcmd' go start application at address 'addr' help (or ?) print online help loadb load binary file over serial line (kermit mode) printenv print environment variable run run commands in an environment variable saveenv save environment variables to persistent storage setenv set environment variables tftpboot boot image via network using TFTP protocol and env variables ipaddr and serverip

23 OMAP Boot Sequence

• Comparison of Personal Computer and Embedded system

BIOS MBR Bootloader Kernel Rootfs User area

Personal Computer

Bootloader Kernel Rootfs User area

Embedded system

24 OMAP Boot Sequence

• Boot loader initial is divided into two stage • Stage1 • Initial hardware • Prepare the RAM space for Stage2 • Initial stack • Copy the bootloader Stage2 to RAM • Set the PC to the entry of stage2 • Stage2 • Enhanced initialization. • Copy Kernel image to RAM. • Give the execution permission to the kernel.

25 OMAP Boot Sequence

• Bootloader of OMAP3530 contain X-loader and U-boot

X-loader U-boot Kernel Rootfs User area

Bootloader

1

Run Bootloader progress on the SD card

2

Run Bootloader progress on the Nand Flash

26 OMAP Boot Sequence – x-loader

• a first level bootstrap program. • the ROM will copy the x-loader to internal RAM . • initialize the CPU • copy u-boot into the memory • give the control power to u-boot • a tiny first stage minimal bootloader.

27 OMAP Boot Sequence – x-loader(cont.)

• The internal ROM Code will attempt to boot.

Rom code loader

28 OMAP Boot Sequence – x-loader(cont.)

• SD Card Boot • ROM looks for an SD Card on the first MMC controller. • The ROM then looks for the first FAT32 partition within the partition table. • Scanned for a signed file called “MLO”. • It is transfered into the internal SRAM and control is passed to it. • Nand Boot • The ROM attempts to load the first sector of Nand. • If the sector is bad, corrupt, or blank, the ROM will try the next sector (up to 4) . • The ROM transfers the contents to SRAM and transfers control to it.

29 OMAP Boot Sequence – U-boot

• SD Card Boot • Looks for a first FAT32 partition on MMC. • Scans root dir for a file named "u-boot.bin".

stage 2 for SD card

30 OMAP Boot Sequence – U-boot

• NAND Boot • u-boot to be loaded at the 5th sector. • Transfers the image into main memory.

stage 2 for NAND Flash

31 OMAP Boot Sequence – Boot Device Priority

• Devkit8000 • Hold the BOOT_KEY push button, then apply power

Boot priority Device

0 NAND FLASH

1 SD Card

Boot key button

32 OMAP Boot Sequence – memory map

Omap3530 memory map

33 • Bootloader • Kernel • Lab

34 Kernel Introduction • Role of android kernel

File System

Linux Kernel

Hardware Bootloader

35 Kernel Introduction

• What is Kernel • The "core" of any computer system. • The "software" which allows users to share computer resources. • The kernel can be thought as the main software of the Operating System • which may also include graphics management.

36 Kernel Introduction

• Role of a Kernel • Provides a consistent interface to managed devices. • Protects the hardware from users of the computer. • Protects important software from users.

Protect Ring

37 Kernel Introduction

• Kernel Mode & User Mode • To avoid having applications that constantly crashed • Designed with 2 different operative modes: • Kernel Mode • User Mode

User Implementation Mode Abstraction Detail

Kernel Mode

38 Kernel Introduction

• User mode • Access user-mode memory • Illegal attempts will result in • faults/exceptions • Kernel mode • I/O instructions • Access both user- and kernel-mode • memory • An instruction to change to user mode

39 Kernel Introduction

• How to call kernel mode from user mode • System Call (general) • IRQ (or exception) comes • Trap

40 Kernel Introduction

• System call • Like special functions that manage OS routines which live in Kernel Mode. • A system call can be called when we: • access an I/O device or a file. • need to access privileged information • need to change execution context. • need to execute a particular command.

41 Kernel Introduction Such as sys_restart_syscall User mode sys_exit sys_fork_wrapper sys_read sys_write sys_open sys_close sys_chdir …

Kernel mode

42 Kernel Introduction

• System Calls define

43 Kernel Introduction

• System architecture (the users) system programs shells and commands compliers and interpreters system libraries System call interface to the kernel

Kernel Task kernel …

Kernel interface to the hardware terminal memory hardware device controllers controllers controlloers devices and tapes terminals physical memory

44 Kernel architecture

45 Kernel architecture - function

• Process management • Creating and destroying processes. • Communication among different processes. • Controls how processes share the CPU. • Kernel’s process management activity implements the abstraction of several processes on top of a single CPU or a few of them. • Memory management • Allow processes to safely access this memory as they require it. • Builds up a virtual addressing space for any and all processes. • Kernel interact with the memory-management subsystem through a set of function calls.

46 Kernel architecture - function(cont.)

• File system • Unix is heavily based on the file system concept. • For example disks may be formatted with the Linux-standard ext3 file system. • Device Control • All device control operations are performed by driver that is specific to the device being addressed. • Networking • Delivering data packets across program and network interfaces • Control the execution of programs according to their network activity. • Routing and address resolution issues.

47 Kernel architecture - function(cont.)

• Loadable Modules • Add / Remove functionality to the while the system is running is called a module. • Module is made up of object code that can be dynamically linked to the running kernel • Such as Driver .. • Linux is monolithic architecture. • All of the codes will be compiled and linked into a single static linking and executable file. • It’s possible to compile and incremental • linking into a single target module.

48 Kernel architecture

• Where do we get the kernel source • Download kernel source from http://www.kernel.org • Lastest stable version - 3.5.4

49 Kernel architecture

• Print system information

• Linux 2 . 6 . 28 - 19

VERSION PATCHLEVEL EXTRAVERSION

SUBLEVEL

50 Kernel architecture

• Linux kernel history

2006

Develop

stable

51 Kernel architecture • Kernel source tree . |-- arch |-- include |-- net |-- block |-- init |-- README |-- COPYING |-- ipc |-- REPORTING-BUGS |-- CREDITS |-- Kbuild |-- samples |-- crypto |-- kernel |-- scripts |-- Documentation |-- lib |-- security |-- drivers |-- MAINTAINERS |-- sound |-- firmware |-- Makefile |-- usr |-- fs |-- mm `-- virt

52 Kernel architecture • Kernel source tree • linux/arch - Architecture-specific source arch |-- mn10300 |-- alpha |-- parisc |-- arm |-- powerpc |-- avr32 |-- s390 |-- blackfin |-- sh |-- cris |-- sparc |-- frv |-- sparc64 |-- h8300 |-- um |-- ia64 |-- x86 |-- Kconfig `-- xtensa |-- m32r |-- m68k |-- m68knommu | -- mips

53

Kernel architecture

• Kernel source tree • linux/driver - Devices driver drivers |-- thermal |-- accessibility |-- cpuidle |-- ide |-- message |-- ps3 |-- uio |-- acpi |-- crypto |-- idle |-- mfd |-- rapidio |-- usb |-- amba |-- dca |-- ieee1394 |-- misc |-- regulator |-- uwb |-- ata |-- dio |-- infiniband |-- mmc |-- rtc |-- video |-- atm |-- dma |-- input |-- mtd |-- s390 |-- virtio |-- auxdisplay |-- dsp |-- isdn |-- net |-- sbus |-- w1 |-- base |-- edac |-- Kconfig |-- nubus |-- scsi |-- watchdog |-- block |-- eisa |-- leds |-- of |-- serial |-- xen |-- bluetooth |-- firewire |-- lguest |-- oprofile |-- sh `-- zorro |-- cbus |-- firmware |-- macintosh |-- parisc |-- sn |-- cdrom |-- gpio |-- Makefile |-- parport |-- spi |-- char |-- gpu |-- mca |-- pci |-- ssb |-- clocksource |-- hid |-- md |-- pcmcia |-- staging |-- connector |-- hwmon |-- media |-- pnp |-- tc |-- cpufreq |-- i2c |-- memstick |-- power |-- telephony

54 Kernel architecture

• Other kernel source tree Directory Description Documentation Kernel source documentation init Kernel boot and initialization

ipc Interprocess communication code kernel Core subsystems lib Helper routines net Networking subsystem security Linux Security Module sound Sound subsystem usr Early user-space code (called initramfs)

include header files, H/W independent header files in include/linux

fs opn() read() write(), ext2, ext3, ext4, nfs, ufs, msdos, vfat, proc, devfs, , reiserfs, ,etc

55 Kernel architecture – image format • Kernel images generated by the kernel build process are either uncompressed "Image" files or compressed files. • Vmlinux • Composite kernel image. Kernel proper, in ELF format, including symbols, comments, debug info and architecture-generic components.concept. • zImage • Final composite kernel image loaded by bootloader • bzImage • It is a big zImage • uImage • it is the u-boot bootable image format generated by mkimage tool

56

Kernel architecture – image format

• mkimage is the tool used to create installable application images, including the initial system bootstrap file image • -A ==> set architecture to 'arch' • -O ==> set operating system to 'os' • -T ==> set image type to 'type' • -C ==> set compression type 'comp' • -a ==> set load address to 'addr' (hex) • -e ==> set entry point to 'ep' (hex) • -n ==> set image name to 'name' • -d ==> use image data from 'datafile' • -x ==> set XIP (execute in place)

Example: host $ mkimage -A arm -O linux -T kernel -C gzip -a 0xc0008000 –e 0xc0008000 -n “Linux-2.4.20” -d vmlinux.bin /tftpboot/uImage

57 Kernel boot step

• Boot function flow

Decompress_kernel() Do_basic_setup()

Call_kernel() Prepare_namespace()

Start_kernel() Execve(“/sbin/init”,”ar gv_init”,”envp_init”) Setup_arch()

Rest_init()

init()

58 Kernel boot step

• Dmesg information Setup_arch

Toolchain

Boot information

Kernel parameter

59 Kernel boot step

• Dmesg information Setup_arch

Target board information

60 Kernel boot step

• Dmesg information Peripheral initial

Network Protocol Initial

Serial Port Initial

61 Kernel boot step

• Dmesg information Peripheral initial

NAND partition

USB Initial

62 Kernel boot step

• Dmesg information

Peripheral initial

USB Initial

LED Initial

Soc Initial

63 Kernel boot step

• Dmesg information

Network & FS initial

eth0 Initial & setting

mount FileSystem

64 Kernel configure

• Configuration targets: • config ‐ Update current config utilising a line‐oriented program • menuconfig ‐ Update current config utilising a menu based program • xconfig ‐ Update current config utilising a QT based front‐end • gconfig ‐ Update current config utilising a GTK based front‐end • oldconfig ‐ Update current config utilising a provided .config as base • silentoldconfig ‐ Same as oldconfig, but quietly • randconfig ‐ New config with random answer to all options • defconfig ‐ New config with default answer to all options • allmodconfig ‐ New config selecting modules when possible • allyesconfig ‐ New config where all options are accepted with yes • allnoconfig ‐ New config where all options are answered with no

65 Kernel configure

• host $ make menuconfig

Kconfig

66 • Bootloader • Kernel • LAB

67 Lab overview

• Lab • build uboot . Kernel image • Learn how to use U-Boot and Kernel • Understanding U-boot , Kernel src • Build U-Boot , uImage image • Run bootloader , kernel image

68 U-boot Lab

• Step1 : Get the source code & tool  Host$ cd ~/embed/src  Host$ tar –xvf u-boot-1.3.3.tar.bz2

• Step2 : Build U-boot source code  Host$ cd ~/embed/src/u-boot-1.3.3  Host$ make distclean  Host$ make omap3devkit8000_config  Host$ make  Step3 : Copy uboot image to SD card  Host$ cp ~/embed/src/u-boot-1.3.3/u-boot.bin /media/SD_NAME

69

U-boot Lab

• Step4 : Connect Devkit8000 • Host$ kermit -c

70 U-boot Lab

• Step5 : Setting Target Board • OMAP3 Devkit8000 # mmcinit • OMAP3 Devkit8000 # fatload mmc 0:1 80000000 u-boot.bin • OMAP3 Devkit8000 # bootm 80000000

71 kernel Lab

• Step1 : Decompress Kernel source • Host$ cd ~/embed/src • Host$ tar -xvf ./linux-2.6.28-omap.tar.bz2

Kernel source layout

72 kernel Lab

• Step2 : make .defconfig ( two method )

• Method 1 • Host$ cd ~/embed/src/linux-2.6.28-omap • Host$ cp arch/arm/configs/omap3_devkit8000_defconfig ./config • Method 2 • Host$ cd ~/embed/src/linux-2.6.28-omap choose one of the • Host$ make omap3_devkit8000_defconfig options

73 kernel Lab

• Step3 : Config Kernel source • Host$ cd ~/embed/src/linux-2.6.28-omap • Host$ make menuconfig

• Step4 : Make menuconfig and save it

74 kernel Lab

• Step5 : make uImage • Host$ cd ~/embed/src/linux-2.6.28-omap • Host$ make uImage

Note

75 kernel Lab

• Step6 : Copy uImage to SD card

• Host$ cp ~/embed/src/linux-2.6.28-omap/arch/arm/boot/uImage /media/SD_NAME

76 kernel Lab

• Step7 : Connect Devkit8000 • Host$ kermit -c

77 kernel Lab

• Step8 : Update the image for NAND Flash • OMAP3 Devkit8000 # mmcinit • OMAP3 Devkit8000 # fatload mmc 0:1 80000000 binary_file • OMAP3 Devkit8000 # nand unlock • OMAP3 Devkit8000 # nand ecc sw • OMAP3 Devkit8000 # nand erase NAND_MAPAddr Size • OMAP3 Devkit8000 # nand write.i 80000000 NAND_MAPAddr $(filesize)

1. 2. 6. 3~5. SD

Binary file Note : File system using 81000000

78