!1 42

Tizen v2.3 Bootup

Embedded Software Lab. @ SKKU 内容 !2 42

• Bootloader(U-boot) – Bootloader 概述 – U-boot • Systemd – Systemd 概述 – Platform Bootup – Parsing Unit File

Embedded Software Lab. @ SKKU !3 42

Bootloader(U-boot)

Embedded Software Lab. @ SKKU !4 Tizen bootup 概述 43

• 引导加载程序 – 将机器 id和启动参数传递给内核

Power on ROM code Booting device

Bootloader

Kernel

Platform

Embedded Software Lab. @ SKKU !5 什么是引导加载程序 bootloader 43

• 引导加载程序是什么 ? – 引导加载程序是当系统初始化时的第⼀个执⾏程序 (在主程 序前,例如内核 ) – 引导加载程序是⼀个计算机程序 ,在⾃检完成后加载主程序 (即操作系统内核 )

• 为什么需要引导加载程序 ? – 给出了⼀个开发环境 – 节省了 flash读写的成本 – 给加载任何操作系统提供灵活性

Embedded Software Lab. @ SKKU !6 引导加载程序的类型 43

• 引导加载程序的不同类型是什么 ? – Boot-ROM ( or Pre-Boot Loader) • ⼩代码加载第⼀阶段引导加载程序 – First Stage Boot Loader • ⼩段代码初始化 NAND/MMC & DRAM控制器。 – Second Stage Boot Loader • 第⼆阶段的引导加载程序的主要功能是内核加载到 RAM或直接跳跃到 内核的开始。

First-Stage Second-Stage Pre-Bootloader OS Kernel Bootloader Bootloader

Embedded Software Lab. @ SKKU !7 U-boot 加载过程 43

Exynos4412 DRAM controller DRAM Cortex-A9

eSSD controller Internal ROM 1 iROM code Booting Device NAND controller (NAND, MMC, SD, USB) 1st BL SD/MMC controller 2 2nd BL Internal SRAM 3 OS UART/USB

Embedded Software Lab. @ SKKU !8 U-boot 加载过程 43

• BL0; iRom code (boot-rom or pre-boot loader) – 简单的平台⽆关的代码存储在内部 ROM – 初始化基本功能 (时钟 ,栈 ,堆 ,等等 ) – 加载第⼀阶段引导加载程序 (从启动设备内部存储器 ) • BL1; first boot loader – 简单的平台⽆关的代码存储在外部存储器 (引导设备 ) – 加载第⼆引导加载程序 – 初始化 NAND/MMC & DRAM控制器 • BL2; second boot loader – 平台依赖复杂的代码存储在外部存储器中 – 初始化时钟 ,UART – 从引导装载操作系统映像 DRAM的⼿段 – 跳转到操作系统

Embedded Software Lab. @ SKKU !9 U-boot ⺫录结构 43

/u-boot /u-boot

api examples Machine/arch independent API Example code for standalone applications

arch Architecture specific files fs Filesystem code (cramfs, , , etc.)

board include Board dependent files Header files

common Misc architecture independent functions lib Files generic to all architectures

disk net Code for disk drive partition handling Networking code

doc post Documentation Power on self test

drivers Commonly used device drivers rtc Real time clock drivers

tools Tools to build S-Record or U-boot images Embedded Software Lab. @ SKKU !10 U-boot 编码顺序 43

1. 禁⽤所有中断 2. 从 ROM将所有初始化数据复制到 RAM 3. 清空未初始化的数据区域 4. 分配和初始化堆栈空间 5. 初始化处理器的堆栈指针 6. 创建并初始化堆 7. 执⾏所有全局数据的初始化 8. 启⽤中断 9. 调⽤主循环

Embedded Software Lab. @ SKKU !11 U-boot 编码顺序 43

Reset CPU and Hardware cpu/armv7/start.S _start() Setup SP for Early Board Setup Environment (ASM->C)

Disable IRQ &FIQ. Switch to supervisor mode General Relocation arm/lib/crt0.S cpu/armv7/start.S _main() reset() cpu/armv7/start.S Early Board Setup relocate_code()

arm/lib/crt0.S Low Level Initialization board_init_f() Copy Binary to RAM

cpu_init_crit

Calculate Addresses (SP, Dest, GD) for Fix Relocation Invalidate and disable Relocation Instruction & data Cache Clear BSS

Disable MMU Setup SP for Common Board Setup

Setup GD and JUMP to Final Board Setup lowlevel_init arm/lib/crt0.S board/samsung/lowlevel_init.S board_init_r() lowlevel_init()

Jump into main loop

Embedded Software Lab. @ SKKU !12 U-boot 编码顺序 43

• _start() /* Entry point function */ • reset() /* Reset CPU configurations */ – save_boot_params() /* Save boot parameters */ – Disable the FIQ & IRQ – Switch to supervisor mode – cpu_init_crit() • Invalidate I, D Cache • Disable I cache if CONFIG_SYS_ICACHE_OFF • Disable MMU • lowlevel_init() • _main() /* setup initial stack & global data. And jump to C routine for board initialization */

Embedded Software Lab. @ SKKU !13 U-boot 编码顺序 43

• board_init_f() – arch_cpu_init // basic arch cpu dependent setup – board_early_init_f // early board initialization, UART pin setting – timer_init // initialize timer – env_init // initialize mmc/sdcard environment – init_baudrate // initialize baudrate settings – serial_init // serial communication setup – console_init_f // stage 1 init of console, configure console from console.c – display_banner // say that we are here, print u-boot banner – print_cpuinfo // display cpu info – checkboard // display board info – init_func_i2c // initialize I2C – dram_init // configure available RAM backs – arm_pci_init

Embedded Software Lab. @ SKKU !14 U-boot 编码顺序 43

• board_init_r() – board_init // setup chipselects specific configuration – logbuff_init_ptrs // initialize default log level – mem_malloc_init // configure malloc area – nand_init mmc_init // initialize flash/mmc – env_relocate // relocate environment or set NULL – stdio_init // setup stdio ready for use – api_init // initialize API – console_init_r // fully init console as a device – arch_misc_init // miscellaneous arch dependent initialization – misc_init_r // miscellaneous platform dependent initialization – interrupt_init // set up exceptions – enable_interrupts // enable exceptions – board_late_init // board late initialization – eth_initialize // setup ethernet – main_loop // jump to main loop & waiting for commands from console

Embedded Software Lab. @ SKKU !15 U-boot 编码顺序 43

main_loop()

Wait for 3 seconds Key pressed within 3 seconds

Input command

Command_mode()

Autoboot_mode() boot() loads() tftpboot() flash()

Jump to kernel image address

Embedded Software Lab. @ SKKU !16 通过 U-boot加载 43

SDRAM Reset CPU Kernel

Root File-System

Booting device Bootloader uboot Bootloader uboot Kernel Image Optional compressed Root

Embedded Software Lab. @ SKKU !17 通过 U-boot加载 43

• 打包 uImage – When the kernel is complied, mkimage attaches image_header to kernel image • 解包 uImage – Magin number ! CRC check ! decompress ! get board information ! kernel booting

#define IH_MAGIC 0x27051956 /* Image Magic Number */ #define IH_NMLEN 32 /* Image Name Length */ typedef struct image_header { uint32_t ih_magic; /* Image Header Magic Number */ uint32_t ih_hcrc; /* Image Header CRC Checksum */ uint32_t ih_time; /* Image Creation Timestamp */ uint32_t ih_size; /* Image Data Size */ uint32_t ih_load; /* Data Load Address */ uint32_t ih_ep; /* Entry Point Address */ uint32_t ih_dcrc; /* Image Data CRC Checksum */ uint8_t ih_os; /* */ uint8_t ih_arch; /* CPU architecture */ uint8_t ih_type; /* Image Type */ uint8_t ih_comp; /* Compression Type */ uint8_t ih_name[IH_NMLEN]; /* Image Name */ } image_header_t;

Embedded Software Lab. @ SKKU !18 start_kernel 43

• Architecture dependent setup – setup_arch() [/arch/arm/kernel/setup.c] • Exception(or trap) initialization – trap_init() [/arch/arm/kernel/trap.c] • Interrupt initialization – init_IRQ() [/arch/arm/kernel/irq.c] – sotfirq_init() [/kernel/sotfirq.c] • Scheduler initialization – sched_init() [/kernel/sched.c] • Timer initialization – time_init() [/arch/XXX/kernel/time.c] • Console initialization – console_init() [/driver/char/tty_io.c] • Module initialization – init_module() [/kernel/module.c]

Embedded Software Lab. @ SKKU !19 start_kernel 43

• 内存和系统初始化 ,缓存和缓冲初始化 ,⽂件系统初始化 – kmem_cache_init() [/mm/slab.c] – mem_init() [/arch/a,r/mm/init.c] – kmem_cache_size() [/mm/slab.c] – fork_init() [/kernel/.c] – proc_caches_init() [/kernel/fork.c] – vfs_caches_init() [/fs/dcache.c] – buffer_init() [/fs/buffer.c] – page_cache_init() [/mm/filemap.c] – signals_init() [/kernel/signal.c] – proc_root_init() [/fs/proc/root.c] – ipc_init() [/ipc/util.c] • init process creation – rest_init() [/init/main.c]

Embedded Software Lab. @ SKKU !20

43

systemd

Embedded Software Lab. @ SKKU !21 什么是 systemd? 43

• Sysvinit – userspace launch (PID 1) – ⼀次只有⼀个进程运⾏ – 直接启动系统 ,然后 sleep直到下⼀次重新启动 • Systemd – Default init in Fedora 15 and later Leonard Poettering (2009) – 改善软件框架来表达依赖性 – 允许在系统启动时同时或并⾏处理更多的过程 – 减少计算开销。

Embedded Software Lab. @ SKKU !22 什么是 systemd? 43

• Systemd in Tizen – System and service manager for tizen systemd – Ver. 43 (download http://www.freedesktop.org/software/systemd/) – Parallelizes service execution – Socket and D-Bus activation for starting services and daemon – on-demand starting of daemons – Managing the service processes as a group using cgroup – Supporting automount points – Snapshotting and restoring of services

Embedded Software Lab. @ SKKU !23 systemd 体系结构 43

Embedded Software Lab. @ SKKU !24 systemd 体系结构 43

• systemd Utility – 监测和控制 systemd本⾝和它的服务 – “systemctl”开发⼈员可以查询或发送控制命令对 systemd服务管理者 – 'systemd-journalctl' ⽤于查看⽇志⽂件 • systemd target – Tizen启动过程分开在不同的离散步骤,每个步骤使⽤ “target”单元同步点来分组单位 – 每个⺫标的启动过程是⾼度并⾏ ,具体⺫标单位在这样的顺序下的到达具有不确定性 • systemd daemon – Tizen平台系统和服务管理者 – 作为初始化系统 – 'systemd' daemonruns as user session with '--user' option – 'systemd-journald' is a system service that collects and stores log data from the Kernel, from user processes via syslog or STDOUT/STDERR

Embedded Software Lab. @ SKKU !25 systemd 体系结构 43

• systemd core – 管理所有单元如 service, socket, mount 等等 , 储存所有的⽇志数据 . – controlled by using systemd utility like 'systemctl' • systemd library & Linux Kernel – systemd requires to enable 'cgroup' and 'autofs' option in Kernel configuration – It also depends on dbus and some libraries such as libnotify, libudev

Embedded Software Lab. @ SKKU !26 平台启动 – sysinit.target 43

• 早期启动脚本 – Most of file system are mounted and systemd related daemons are launched

Kernel

sysinit.target -.mount local-fs.target boot.mount dev-hugepages.mount csa.mount dev-mqueue.mount basic.target fsck-root.service proc-sys-fs-binfmt_misc.automount media.mount sys-fs-fuse-connections.mount opt-usr.mount sys-kernel-config.mount bootmode.target opt.mount sys-kernel-debug.mount remount-rootfs.service sys-kernel-security.mount resize2fs-root.service systemd-ask-password-console.path systemd-remount-api-vfs.service multi-user.target systemd-binfmt.service var-run.mount systemd-journald.service systemd-modules-load.service graphical.target systemd-random-seed-load.service systemd-readahead-collect.service systemd-readahead-replay.service systemd-sysctl.service socket.target systemd-tmpfiles-setup.service dbus.socket systemd-initctl.socket systemd-journald.socket systemd-shutdownd.socket telnetd.socket udev-control.socket udev-kernel.socket swap.target

Embedded Software Lab. @ SKKU !27 平台启动 – basic.target 43

• 基础启动 – All necessary initialization for general purpose daemons are completed

Kernel

sysinit.target

basic.target opt-var-kdb-db-libsqlfs.service bootmode.target opt-var-kdb-db-smack-labels.service smack-default-labeling.service multi-user.target systemd-tmpfiles-clean.timer tizen-debug-level.service graphical.target udev-trigger.service udev.service vconf-setup.service

Embedded Software Lab. @ SKKU !28 平台启动 – bootmote.target 43

• 选择启动模式 – If kernel boot parameter has ‘charger_detect_boot’, the platform boots up as charging mode

Kernel

sysinit.target

basic.target

bootmode.target

multi-user.target bootmode-charging.service bootmode-graphical.service graphical.target

Embedded Software Lab. @ SKKU !29 平台启动 – multi-user.target 43

• 设置⼀个⽆图形⽀持的多⽤户系统 – Launching platform infrastructure daemons (such as dbus, power manager, gps manager, telephony daemon, WRT security daemon, media server, systemd-related daemons)

Kernel

alarm-server.service sysinit.target avsystem.service bluetooth-address.service bluetooth-frwk-service.service basic.target bluetooth.service dbus.service dlog-main.service bootmode.target dlog-radio.service getty.target multi-user.target gps-manager.service media-server.service nfc-manager.service graphical.target power-manager.service pulseaudio.service secure-storage.service sensor-framework.service system-server.service systemd-ask-password-wall.path systemd-logind.service systemd-user-sessions.service telephony.service

Embedded Software Lab. @ SKKU !30 平台启动 – graphical.target 43

• 设置⼀个图形环境 – System user session for app privilege daemons such as Enlightenment (window manager) – Special target for middleware & mobile service

Kernel

ac.service sysinit.target [email protected] [email protected] [email protected] basic.target

xorg.target tizen-mobile-session.target bootmode.target xorg.service wrt-preinstall-widgets.service

multi-user.target

core-efl.target graphical.target boot-animation.service cbhm.service e17.service tizen-middleware.target indicator.service calendar.service osp-app-service.service contacts-service.service quickpanel.service download-provider.service scim.service email.service starter.path msg-service.service xmodmap.service pushd.service xrdb.service sound-server.service

Embedded Software Lab. @ SKKU !31

43

Systemd 分析

Embedded Software Lab. @ SKKU !32 平台启动 43

• systemd – 控制从 initramfs移动到 Tizen平台的 systemd – Run hardware init and fs mount, etc services – 启动流程运⾏⼏个单元 (.target) • 不同的⺫标可以并⾏运⾏ – 许多 linux发⾏版采⽤ systemd作为默认 • Red Hat Enterprise Linux, CentOS, Fedora, Arch Linux, openSUSE – 不要使⽤守护进程控制脚本 t(/sbin/init, /etc/init.d/*, /etc/rc*.d) – /usr/lib/systemd • unit files: service, target, socket, device, mount, automount, path, snapshot, swap, timer • systemd utilities: systemctl, notify, analyze, cgis, cgtop, logictl, journalctl, nspawn

Embedded Software Lab. @ SKKU !33 平台启动 43

• https://wiki.tizen.org/wiki/Porting_Guide

entry point

.target vs .service • .target; service grouping • .service; process run

Embedded Software Lab. @ SKKU !34 解析单元⽂件 – ⽰例 (1/8) 43

• /usr/lib/systemd/system

target to parse

• cat sysinit.target

Embedded Software Lab. @ SKKU !35 解析单元⽂件 – ⽰例 (2/8) 43

• Website – http://www.freedesktop.org/wiki/software/systemd/ • systemd download – http://www.freedesktop.org/software/systemd/

manpage

Embedded Software Lab. @ SKKU !36 解析单元⽂件 – ⽰例 (3/8) 43

Can search all function and unit, configuration of systemd

enter target index

Embedded Software Lab. @ SKKU !37 解析单元⽂件 – ⽰例 (4/8) 43

target unit file

Embedded Software Lab. @ SKKU !38 解析单元⽂件 – ⽰例 (5/8) 43

Description on unit file option

Embedded Software Lab. @ SKKU !39 解析单元⽂件 – ⽰例 (6/8) 43

Embedded Software Lab. @ SKKU !40 解析单元⽂件 – ⽰例 (7/8) 43

Embedded Software Lab. @ SKKU !41 解析单元⽂件 – ⽰例 (8/8) 43

• User awareness unit name = “System Initialization” • If emergency is running, wait • Run with local-fs.target, swap.target, sysinit.target.wants • Run after local-fs.target, swap.target, emergency.service, emergency.target • User can control service start and stop

Embedded Software Lab. @ SKKU !42 不同的会话 43

• [Unit], [Service], [Install], [Socket], [Swap], [Mount], … – example; telephony.service – [Unit]; general options • unit name = Telephony daemon • Run after modem.service – [Service]; process options • Run telephony-daemon as main process – [Install]; actual work of unit excute • Create telephony.service symlink at multi-user.target.wants

Embedded Software Lab. @ SKKU !43 References 43

• systemd official Site http://www.freedesktop.org/wiki/Software/ systemd/ • systemd FAQ (written by developer) – http://0pointer.de/blog/projects/the-biggest-myths

Embedded Software Lab. @ SKKU