摘自 Embedded Linux System Design and Development
Linux 開機大致上可分成三個階段:
- Boot loader 階段
- Kernel 初始化階段
- User space 初始化階段
Boot loader 階段
- 硬體初始化及測試
- 載入 kernel image:Downloading Kernel Image and Initial Ram Disk: 從 flash 或網路載入到記憶體, 需要時作解壓縮。
- 設定 Linux kernel 需要的開機參數。
- 跳到 Kernel Entry Point,控制權轉移到 Linux kernel。
Kernel 初始化階段
does the platform-specific 初始化,brings up the kernel subsystems, turns on multitasking, mounts the root file system, and jumps to user space.
- CPU/Platform-specific 初始化:
- setting up the environment for the first C routine -- start_kernel(): turn on MMU, initialize cache, zero out BSS, set up stack
- The setup_arch() function: recognize the processor/board, analysis command-line parameters, identify the ram disk that can be mount as root file system, initial and reserve memory, call the paging initialization function
- initialization of exception -- the trap_init() function
- 中斷處理程序初始化 -- the init_IRQ() function (interrupt not enabled)
- timer tick 初始化 -- the time_init()
- console 初始化 -- console_init() -- and the start-up messages appear on the screen
- 計算 delay loop -- calibrate_delay() -- for udelay() to use
- 子系統初始化:scheduler, memory manager, and VFS initialization, and finally create the init process to do the rest of the startup
- 驅動程式初始化
- 掛載 rootfs: rootfs 位置可寫死在 kernel 裡或用開機參數 "root=" 指定。有 3 種 root file systems 常用在embedded systems: initrd, NFS, and Flash-based file systems.
- Doing initcall and freeing initial memory: The init section marked between __init_begin and __init_end contains text and data such as driver initialization functions that can be thrown away after they are used once during the system start-up. A use-and-throw function or variable is declared using the __init directive. Once all the driver and subsystem initialization is done, the start-up code frees all the memory. This is done just before moving to user space. Linux also provides a way of grouping functions that should be called at system start-up time. This can be done by declaring the function with __initcall directive. These functions are automatically called during kernel start-up, so you need not insert them into system start-up code.
- moving to user space: The kernel that is executing in the context of the init process jumps to the user space by overlaying itself (using execve) with the executable image of a special program also referred to as init. This executable normally resides in the root file system in the directory /sbin. Note that the user can specify the init program using a command line argument to the kernel. However if the kernel is unable to load either the user-specified init program or the default one, it enters the panic state.
參考:
- Embedded Linux System Design and Development