Building Embedded Linux System Toolchains Bootloader Kernel Root File System
정의 Toolchain 소스를컴파일하여바이너리실행파일를생성하기위해필요한컴파일러및라이브러리, 바이너리유틸리티모음
구성요소 Toolchain GCC : 컴파일러 Binutils : 어셈블러및로더, 바이너리파일편집유틸리티 Glibc : 크로스컴파일을위한라이브러리및일반라이브러리 Linux 커널 : 리눅스커널소스, Kernel headers
Toolchain
Toolchain
Toolchain
Toolchain
/opt/toolchains 디렉토리를만든다 # mkdir /opt/toolchains Toolchain 압축파일의압축을푼다. Toolchain 설치 # tar jxvf arm-2014.05-29-arm-none-linux-gnueabi-i686-pclinux-gnu.tar.bz2 -C /opt/toolchains/
Toolchain 설치 컴파일에사용되는명령어들을어느디렉토리에서나실행할수있기위해서는.bashrc 파일의 PATH환경변수에 /opt/toolchains/arm- 2014.05/bin을추가해주어야한다. 크로스컴파일러의명령어디렉토리 (/opt/toolchains/arm-2014.05/bin)
Toolchain 설치.bashrc 파일수정 # vi.bashrc source 명령을통해변경된.bashrc 을쉘에적용 # source.bashrc
Toolchain Test X86 계열에서사용하는리눅스 gcc 와다른점은생성되는코드가 ARM 용으로생성된다는것이다. 간단한프로그램을작성하여테스트한다. 테스트프로그램작성
컴파일 Toolchain Test # gcc o hello hello.c # arm-none-linux-gnueabi-gcc o hello-arm hello.c 컴파일된파일정보확인 (file 명령사용 )
Minicom 이란? TARGET은출력을위한별도의터미널을가지고있지않음 일반적으로 serial port를통한터미널프로그램이용 Linux에서는일반적으로 minicom 사용 Windows 에서는 HyperTerminal 과유사한프로그램을사용 Minicom 이란? 호스트와타겟을연결해주는가상터미널이다. 하이퍼터미널과유사한프로그램이고, 타켓의화면을호스트에서볼수있게해준다. Minicom 프로그램을사용하기위해먼저설정을해준다. minicom 프로그램은시리얼에연결되어있기때문에타겟의시리얼설정에맞는호스트설정이필요하다. Minicom 용도 부트로더의명령프롬프트 임베디드리눅스의쉘프롬프트를위한콘솔로사용
Minicom 설정 minicom 설치 # apt-get install minicom Serial port 확인 # dmesg
Minicom 설정 minicom 환경설정모드로진입 # minicom s
COM Port 선택 Minicom 설정 Serial port setup 항목을선택한다. A 를선택하여 Serial Device 를직렬케이블이연결된직렬포트로설정한다.
설정값저장 Minicom 설정 Save setup as dfl 를선택한후 Enter 를눌러설정된값을저장한다. 저장후 Exit 를눌러설정밖으로나간다. ( 그후타켓보드를켜면부팅되면서그화면이 minicom 으로보여진다.)
TFTP(Trivial File Transfer Protocol) 란? TFTP 란? FTP 보다간단하지만기능이조금덜한네트워크어플리케이션이다. 이것은사용자인증이불필요하고, 디렉토리를보여주지않아도되는곳에사용된다. 부트로더에서 kernel 이미지를 Host 에서 Target 으로이더넷포트를통하여다운로드하기위해사용한다.
프로그램을설치 # apt-get install tftp tftpd xinetd TFTP-Server 설치하기 이미설치되어있을경우의화면
/etc/xinet.d/tftp 파일작성또는수정 # vi /etc/xinetd.d/tftp service tftp { protocol = udp socket_type = dgram wait = yes user = root server = /usr/sbin/in.tftpd server_args = -s /tftpboot disable = no per_source = 11 cps = 100 2 flags = IPv4 } TFTP-Server 환경설정
tftpboot 디렉토리생성 TFTP-Server 환경설정 tftp가연결된디렉토리이다. /tftpboot라는디렉토리가없으면 mkdir로만들어준다. # mkdir /tftpboot tftp 가실행되도록다음명령을실행한다. # /etc/init.d/xinetd restart 또는 # service xinetd restart # netstat -a grep tftp
TFTP 시험 TFTP
NFS(Network File System) NFS 컴퓨터사용자가원격지컴퓨터에있는파일을마치자신의컴퓨터에있는것처럼검색하고, 저장하거나수정하도록해주는클라이언트 / 서버형태의메커니즘 NFS 서버설치 # apt-get install nfs-kernel-server NFS 서버설정 공유디렉토리생성 # mkdir /nfsroot /etc/exports 파일수정 # vi /etc/exports /nfsroot *(rw,sync,no_root_squash,no_subtree_check)
NFS NFS 시작 NFS 데몬재시작 # /etc/init.d/nfs-kernel-server restart 또는 # service nfs-kernel-server restart # netstat -a grep nfs NFS 테스트
Bootloader
Target Board Initialization Memory setting CPU clock setting GPIO setting Serial port setting MAC address and Ethernet port setting Bootloader Overview Kernel Booting Image download from host by tftp Copy image from Flash to ram or from ram to flash. Jump to kernel Command line interface
Bootloader 와 PC BIOS 의비교
Bootloader 의종류
Bootloader 의종류 x86 X86에서는아래의 2가지타입이주로사용된다. : LILO and GRUB LILO: http://brun.dyndns.org/pub/linux/lilo/ GRUB: http://www.gnu.org/software/grub/ 그외의bootloader: Rolo, EtherBoot, LinuxBIOS Arm architecture U-Boot ARM bootloader의표준이되어가고있다. Armboot로합쳐진 ppcboot는 u-boot로진화 http://armboot.sourceforge.net/ BLOB LART hardware project에서소개된 bootloader 다양한 ARM-based system에포팅 http://www.lart.tudelft.nl/lartware/blob
1 Reset 후내부ROM 코드실행 2 Operating Mode 에따라부트디바이스선택 3 BL1과일부초기화코드를내부 RAM으로복사 4 BL1에서 U-Boot 복사 5 U-Boot 실행시작 Booting Sequence
Kernel
Kernel Overview Kernel 의역사 UNIX: 1969 Thompson & Ritchie AT&T Bell Labs. BSD: 1978 Berkeley Software Distribution. Commercial Vendors: Sun, HP, IBM, SGI, DEC. GNU: 1984 Richard Stallman, FSF. POSIX: 1986 IEEE Portable Operating System unix. Minix: 1987 Andy Tannenbaum. SVR4: 1989 AT&T and Sun. Linux: 1991 Linus Torvalds Intel 386 (i386). Open Source: GPL.
Kernel Overview Linux 의특징 UNIX-like operating system. 특징 : Preemptive multitasking. Virtual memory (protected memory, paging). Shared libraries. Demand loading, dynamic kernel modules. Shared copy-on-write executables. TCP/IP networking. SMP support. Open source.
Kernel Overview kernel 이란? AKA: executive, system monitor. hardware에접근하는것을제어하고중재한다. 핵심적인개념을구현하고지원한다 : Processes, files, devices etc. Schedules / allocates system resources: Memory, CPU, disk, descriptors, etc. Security와 protection 수행. service (system calls) 를위한사용자요청에대한응답.
Kernel Core Kernel Overview
Linux Kernel Kernel Overview 운영체제에서가장기초적이고핵심적인기능을담당. 프로세스의관리, CPU 스케쥴링, 입출력제어 메모리, 파일, 주변장치와같은시스템관리 Monolithic Kernel이지만 Micro Kernel의특징인모듈을이용 Monolithic kernel: 하나의 Kernel 에필요한모든기능이통합. 같은메모리공간에필요한기능이존재, 함수호출방식으로 Kernel 에서제공하는기능에접근. 구현이간편하고효율적, 포팅과확장이어렵다. Micro kernel 필요한기능들은작은서버모듈로나뉘어설계, 서버를관리할수있는최소의크기, 하드웨어환경에따라기능의확장과기능재구성이용이. 단점은서비스를사용하는과정에서여러번의메시지전송과 Context Switching 이발생. 높은자원사용.
Process Management Linux Kernel 하나의프로그램은하나이상의프로세스를가질수있다. Kernel은해당프로세스가시스템자원을분배하고, 다른자원을침해할수없도록관리 프로세스의생성과소멸에대한전반적인내용을관리한다. Memory Management 시스템에서구동되는프로세스는프로세스만의독립적인메모리를갖는다. Linux는물리메모리에직접적으로데이터를기록하는것을허용하지않는다 반환된메모리의관리등과같은기능을수행
File System Management Linux Kernel Linux 는범용운영체제로다양한파일시스템을지원 Linux 에서모든장치는파일로표현된다. CDFS, FAT, JFFS, YAFFS, FAT16/32, EXT3/4, NTFS등을지원 Device Management Bootloader에서초기화된이후모든디바이스는 Kernel 의관리를받는다. 응용프로그램에서디바이스를이용해야하는경우 Kernel 을통해야만제어가가능하다. 사용하고자하는장치드라이버에따라서문자, 블록방식으로나뉘게된다. Network Management Kernel 은기기와외부시스템이정상적인네트워크를통해서통신할수있도록한다. IPv4, IPv6, Ethernet, ATM, CDMA등이지원된다.
Linux Kernel
Kernel source www.kernel.org Kernel의대부분은 C로작성되어있다. Kernel Directories Kernel Source Tree
Kernel Source Tree arch/ CPU dependent (arch/i386, arch/alpha, arch/arm ) arch/arm/boot/ Boot Strap arch/arm/kernel/ Hardware dependent kernel management routines Trap, Interrupt processing routines Context Switching routines Device configuration, initialization arch/arm/mm/ Hardware dependent memory management routines init/ Hardware independent kernel initialization (start_kernel) Task 0 (init_task or task[0]) creation Demon process creation - Task 1, 2, 3 and so on
Kernel Source Tree kernel/ Central section of the kernel Hardware independent kernel management routines fork, exit Scheduler Signal handling Time management mm/ Hardware independent memory management routines Virtual Memory Management Paging / Swapping
Kernel Source Tree fs/ Virtual file system Management open, read file system related system call routines Subdirectories for special file systems (ext2, fat,... ) ipc/ IPC between processes Semaphores Shared memory Message queues
drivers/ Device driver routines drivers/block/ : Block device driver drivers/char/ : Character device driver drivers/net : Network device driver drivers/pci/ : PCI bus control drivers/sound/ : sound card device driver drivers/cdrom/ : CD-ROM device driver drivers/scsi/ : SCSI device driver net/ Network protocols : TCP/IP, ARP and so on Socket interface Kernel Source Tree
Kernel Source Tree include/ Header files for the kernel Hardware independent : include/linux/ Hardware dependent : include/asm-***/ modules/ Kernel module routines insmod(modprobe), rmmod : dynamic load and removal lib/ Kernel library routines Doc/ Kernel document
Kernel Image Writing
Root File System for Embedded Linux
Minimum Components init: The program that starts everything off, usually by running a series of scripts. shell: Needed to give you a command prompt but, more importantly, to run the shell scripts called by init and other programs. daemons: Various server programs, started by init. libraries: Usually, the programs mentioned so far are linked with shared libraries which must be present in the root file system. Configuration files: The configuration for init and other daemons is stored in a series of ASCII text files, usually in the /etc directory.
Minimum Components Device nodes: The special files that give access to various device drivers. /proc and /sys: Two pseudo file systems that represent kernel data structures as a hierarchy of directories and files. Many programs and library functions read these files. kernel modules: If you have configured some parts of your kernel to be modules, they will be here, usually in /lib/modules/[kernel version].
Directory Layout /bin: programs essential for all users /dev: device nodes and other special files /etc: system configuration /lib: essential shared libraries, for example, those that make up the C library /proc: the proc files ystem /sbin: programs essential to the system administrator /sys: the sysfs file system /tmp: a place to put temporary or volatile files /usr: as a minimum, this should contain the directories /usr/bin, /usr/lib and /usr/sbin, which contain additional programs, libraries, and system administrator utilities /var: a hierarchy of files and directories that may be modified at runtime, for example, log messages, some of which must be retained after boot
Programs for the root file system The init program You have seen in the previous chapter that init is the first program to be run and so has PID 1. It runs as the root user and so has maximum access to system resources. Usually, it runs shell scripts which start daemons: a daemon is a program that runs in the background with no connection to a terminal, in other places it would be called a server program. Shell We need a shell to run scripts and to give us a command-line prompt so that we can interact with the system. An interactive shell is probably not necessary in a production device, but it is useful for development, debugging, and maintenance.
Utilities Programs for the root file system The shell is just a way of launching other programs and a shell script is little more than a list of programs to run, with some flow control and a means of passing information between programs. To make a shell useful, you need the utility programs that the Unix command-line is based on. Even for a basic root filesystem, there are approximately 50 utilities, which presents two problems. Firstly, tracking down the source code for each and cross compiling it would be quite a big job. Secondly, the resulting collection of programs would take up several tens of megabytes, which was a real problem in the early days of embedded Linux when a few megabytes was all you had. To solve this problem, BusyBox was born. BusyBox
Files for the root file system Libraries for the root file system Device nodes $ sudo mknod -m 666 dev/null c 1 3 $ sudo mknod -m 600 dev/console c 5 1 $ ls -l dev total 0 crw------- 1 root root 5, 1 Oct 28 11:37 console crw-rw-rw- 1 root root 1, 3 Oct 28 11:37 null
Build Systems The idea of a build system is to automate all the steps. A build system should be able to build, from upstream source code, some or all of the following: The toolchain The bootloader The kernel The root file system
Build Systems Buildroot: An easy-to-use system using GNU make and Kconfig (http://buildroot.org) EmbToolkit: A simple system for generating root filesystems; the only one at the time of writing that supports LLVM/Clang out of the box (https://www.embtoolkit.org) OpenEmbedded: A powerful system which is also a core component of the Yocto Project and others (http://openembedded.org) OpenWrt: A build tool oriented towards building firmware for wireless routers (https://openwrt.org) PTXdist: An open source build system sponsored by Pengutronix (http://www.pengutronix.de/software/ptxdist/index_en.html) Tizen: A comprehensive system, with emphasis on mobile, media, and invehicle devices (https://www.tizen.org) The Yocto Project: This extends the OpenEmbedded core with configuration, layers, tools, and documentation: probably the most popular system (http://www.yoctoproject.org)
SD Booting
NFS Booting