Linux mobile phone DIY. kernel preliminary study. Brief Introduction to system background startup

Source: Internet
Author: User

Linux mobile phone DIY. kernel preliminary study. Brief Introduction to system background startup

Papayas on September 25

I. Sequencing

How to start a Linux system is very important for future application development.
Forum, with a brief introduction to Moto e680, Xia xine600 and Philips 968

Ii. Important Notes

To facilitate a better understanding of this article, we provide the following link.
Full range of articles address, mobile phone application development column: http://blog.csdn.net/liwei_cmg

Iii. Linux Startup Process Overview

Reading the Linux source code is the most direct way to learn how to start a Linux system.
The sub-source code mainly uses the C language and involves a small number of assembly languages. Also executed during startup
A large number of shell scripts. The following is the general startup process.

The user first powers on, the motherboard BIOS starts power-on self-check, according to the BIOS set to start the device (such as hard
Boot, and then start the boot program LILO or GRUB installed on the device to boot Linux,
The Linux boot program first performs kernel boot, and then executes the INIT program. The INIT program calls
RC. sysinit and RC. After RC. sysinit and RC complete system initialization and running service tasks,
Return init. After mingetty is started by init, you can open the terminal to log on to the system.
To log on to and enter the shell window, so far the entire process from boot to login has been completed.

Power on-> BIOS-> IDE/CDROM-> LILO/grub-> kernel boot-> init
(RC. sysinit RC)-> mingetty-> Shell

Iv. Introduction to the kernel Startup Process of Linux Mobile Embedded System

The following describes the system startup process with the e680 kernel code.

Embedded systems or PCs, first use bootloader boot programs like LILO or GRUB to guide
In Linux, after the boot program successfully completes the boot task, Linux takes over CPU control from them.
Then, the CPU starts to execute the Linux core image code and starts the Linux Startup Process.
After bootloader completes the system boot and transfers the Linux kernel to the memory, it calls bootlinux (),
This function will jump to the start position of the kernel. If the kernel is not compressed, you can start it. For example
If the kernel is compressed, decompress it. There is an decompressed program in the compressed kernel header. Compressed
The source code of the first file in the kernel entry is arch/ARM/boot/compressed/head. S. It will call
Decompress_kernel (), which is in the arch/ARM/boot/compressed/Misc. c file,
Decompress_kernel () also calls proc_decomp_setup (), arch_decomp_setup () to set,
After "Uncompressing Linux..." is printed, gunzip () of LIB/inflate. C is called ().
Place the kernel at a specified location.
The first running files to start are:
ARCH/ARM/boot/compressed/head. s
ARCH/ARM/boot/compressed/head-xscale.S
ARCH/ARM/boot/compressed/Misc. c
These files are mainly used to decompress the kernel and start the kernel image. Once the kernel is started, these files
The occupied memory space will be released. In addition, once the system restarts through reset, when bootloader compresses
Into the memory, the first thing to execute is the code.

Head. s code:

/*
* Check to see if we will overwrite ourselves.
* R4 = final kernel address
* R5 = start of this image
* R2 = end of malloc space (and therefore this image)
* We basically want:
* R4> = R2-> OK
* R4 + image length <= R5-> OK
*/
CMP R4, r2
BHS wont_overwrite
Add r0, R4, #4096*1024 @ 4 MB largest kernel size
CMP r0, R5
BLS wont_overwrite

MoV R5, R2 @ decompress after malloc Space
MoV r0, R5
MoV R3, r7
BL decompress_kernel

Add r0, R0, #127
Bic r0, R0, #127 @ align the kernel Length
/*
* R0 = decompressed kernel Length
* R1-r3 = Unused
* R4 = kernel execution address
* R5 = decompressed kernel start
* R6 = processor ID
* R7 = architecture ID
* R8-r14 = Unused
*/
Add R1, R5, R0 @ end of decompressed Kernel

Misc. C code

Decompress_kernel (ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
Int arch_id)
{
Output_data = (UCH *) output_start;/* points to kernel start */
Free_mem_ptr = free_mem_ptr_p;
Free_mem_ptr_end = free_mem_ptr_end_p;
_ Machine_arch_type = arch_id;

Arch_decomp_setup ();

Makecrc ();
Puts ("Uncompressing Linux ...");
Gunzip ();
Puts ("done, booting the kernel./N ");
Return output_ptr;
}

After the kernel is decompressed, run the actual kernel source code through call_kernel. The system will call the file:
ARCH/ARM/kernel/head_armv.s or arch/ARM/kernel/head_armo.s. For ARM kernel,
There are two sets of. s files: _ armv. s and _ Armo. S. Choose whether _ armv. s or _ Armo. s depends on the processor. Arm's
Version 1 and Version 2 support only 26-bit address spaces. Version 3 supports 32-bit address space,
It is also backward compatible with 26-bit address spaces. Version 4 is no longer backward compatible with 26-bit address spaces. Here
In e680, Version5 is used, so only the head_armv.s file is involved.
Head_armv.s is the entry point of the kernel. After the kernel is extracted to a predetermined position, it will run. Head_armv.s
Call some files for CPU and memory settings. Finally, enable MMU and clear the pipeline to make all the memory
And can be accessed correctly. Return _ mmap_switched to clear the BSS and save the CPU type value (processor ID)
And machine type, and then jump to start_kernel.

Related code:


_ Mmap_switched:
# Ifdef config_xip_rom
LDR R2, etext @ data section copy
LDR R3, sdata
LDR R4, edata
1:
LDR R5, [R2], #4
STR R5, [R3], #4
CMP R3, r4
BLT 1b
# Endif

ADR R3, _ switch_data + 4
Ldmia R3, {R4, R5, R6, R7, R8, SP} @ r2 = compat
@ Sp = Stack pointer

MoV FP, #0 @ clear BSS (and zero FP)
1: CMP R4, R5
Strcc FP, [R4], #4
BCC 1b

STR R9, [R6] @ save processor ID
STR R1, [R7] @ save Machine Type
# Ifdef config_alignment_trap
ORR r0, R0, #2.
# Endif
Bic R2, R0, #2 @ clear 'A' bit
Stmia R8, {r0, R2} @ save control register values
B symbol_name (start_kernel)

Start_kernel () is a function defined in init/Main. C. start_kernel () calls a series
Column initialization function to complete the kernel settings. It does a lot of work to build a basic Linux
Core Environment. After start_kernel () is executed, the basic Linux core environment has been established.
The following are some initialization functions in start_kernel:

Trap_init ();
Init_irq ();
Sched_init ();
Softirq_init ();
Time_init ();
Lele_init ();
# Ifdef config_modules
Init_modules ();
# Endif
Mem_init ();
Kmem_cache_sizes_init ();
Pgtable_cache_init ();

...

The start_kernel () function finally calls rest_init ();

Static void rest_init (void)
{
Kernel_thread (init, null, clone_fs | clone_files | clone_signal );
Unlock_kernel ();
Cpu_idle ();
}

Rest_init uses kernel_thread to create the first core thread of the system and start Init ().
The Core Thread Init () mainly performs peripheral initialization, including calling do_basic_setup () to complete
The peripherals and their drivers are loaded and initialized to complete file system initialization and root file system installation.

After the do_basic_setup () function is executed, INIT () is used to open the/dev/console device and redirect
Standard input and output files stdin, stdout, and stderr to the console.

If (open ("/dev/console", o_rdwr, 0) <0)
Printk ("Warning: Unable to open an initial console./N ");

Init () searches for the INIT program in the file system or the program specified by the init = command line parameter,
Execve using the execve () function.

If (execute_command)
Execve (execute_command, argv_init, envp_init );
Execve ("/sbin/init", argv_init, envp_init );
Execve ("/etc/init", argv_init, envp_init );
Execve ("/bin/init", argv_init, envp_init );
Execve ("/bin/sh", argv_init, envp_init );
Panic ("No init found. Try passing init = option to kernel .");

The init () function ends, and the kernel boot part ends.

V. Execution Process of init in Linux mobile phone Embedded System

Run the shell command PS-Ef to view system processes.

Moto e680 Series
------------------------------------------------------
Uid pid ppid C stime tty time cmd
Root 1 0 0 :14? 00:00:00 init

Phillips 968
------------------------------------------------------

PID uid vmsize STAT command
1 root 512 s init

Xia xine600
------------------------------------------------------

PID uid vmsize STAT command
1 root 532 s init

The INIT process IDs are all 1. From this point, we can see that the INIT process is the starting point of all processes in the system,
After Linux completes kernel boot, it starts to execute the INIT program. Different from PC init, embedded devices
The initialization can be completed by independent init processes, such as/sbin/init. New.
Without the source code, it is hard to know what he will do, but the INIT program still needs to read the configuration file.
/Etc/inittab, which is a common text file consisting of several lines of commands.

The inittab file of xiaxin e600 is as follows:

# Format for each entry: <ID >:< runlevels >:< action >:< process>
#
# Id = tty to run on, or empty for/dev/console
# Runlevels = ignored
# Action = one of sysinit, respawn, askfirst, wait, and once
# Process = program to run

: Sysinit:/etc/rc. d/rc. sysinit
: Respawn:/root/Logger
: Respawn:/root/Watchdog
: Once:/root/run_dv2.sh
 
# Namkyu.kim@cecwireless.com.cn
# Do we have to wait here?
#: Wait:/etc/rc. d/RC 3

# What to do when CTRL-ALT-DEL is pressed.
: Ctrlaltdel:/sbin/poweroff

# What to do before system shutdown
Null: shutdown:/bin/shutdown. Sh

# What to do before power is cut (post init)
Null: postinit:/bin/postinit. Sh

#: Respawn:/sbin/Getty-l ttys1 38400 VT100
#: Respawn:/sbin/Getty-l ttys2 115200 VT100
#: Once:/sbin/Getty-l ttyusb0 115200 VT100
# Ttyusb0: respawn:-/bin/sh
Ttys2: respawn:-/bin/sh

Let's take a look at the inittab file of Moto:

#/Etc/inittab: Init (8) configuration.
# $ ID: inittab, V 1.8 10:37:50 miquels exp $

# The default runlevel.
# Ezx -- ID: 3: initdefault:
ID: 2: initdefault:

# Boot-time system configuration/initialization script.
# This is run first packet t when booting in Emergency (-B) mode.
Si: sysinit:/etc/init. d/RCS

# What to do in single-user mode.
~~ : S: Wait:/sbin/sulogin

#/Etc/init. d executes the S and K scripts upon change
# Of runlevel.
#
# Runlevel 0 is halt.
# Runlevel 1 is single-user.
# Runlevels 2-5 are multi-user.
# Runlevel 6 is reboot.

L0: 0: Wait:/etc/init. d/RC 0
L1: 1: Wait:/etc/init. d/RC 1
L2: 2: Wait:/etc/init. d/RC 2
L3: 3: Wait:/etc/init. d/RC 3
L4: 4: Wait:/etc/init. d/RC 4
L5: 5: Wait:/etc/init. d/RC 5
L6: 6: Wait:/etc/init. d/RC 6
# Normally not reached, but fallthrough in case of emergency.
Z6: 6: respawn:/sbin/sulogin

# What to do when CTRL-ALT-DEL is pressed.
CAS: 12345: ctrlaltdel:/sbin/shutdown-T1-a-r now

# Action on special keypress (Alt-uparrow ).
KB: kbrequest:/bin/echo "keyboard request -- edit/etc/inittab to let this work ."

# What to do when the power fails/returns.
PF: powerwait:/etc/init. d/powerfail start
Pn: powerfailnow:/etc/init. d/powerfail now
Po: powerokwait:/etc/init. d/powerfail stop

# This line provides a nice out-of-box experience. For regular use, you
# Shocould replace it with the proper Getty lines below.

# Ezx -- con: 2345: respawn:/sbin/Getty Console
Con: 2345: Once:/sbin/Getty-L/bin/fakelogin3-N Console

The row starting with # Is a comment row. Each row has the following format except the comment row:
ID: runlevel: Action: Process

The most straightforward way is to use the # Man inittab to view the standard manual.

A. ID

ID is the entry identifier, which is a 1-4 character string.
Login program item, requires the ID to be the same as the TTY number, otherwise the Getty program will not work normally.

B. runlevels

Runlevel is the running level identifier of init. It is generally 0-6, S, or S. 0, 1, 6 run
Level is retained by the system. 0 is used as the halt action, 1 is used as the restart to single-user mode, and 6 is the restart. Meaning of S and S
It indicates the single-user mode, and does not require the inittab file, so it does not appear in inittab. In fact,
In single-user mode, init runs/sbin/sulogin directly on the console (/dev/console.
In the RedHat System of the PC, 2 indicates the multi-user mode not supported by NFS, and 3 indicates the full multi-user mode.
(Also the most commonly used level), 4 is reserved for the User-Defined, 5 indicates the xdm graphical login mode. 7-9 levels
Yes. Traditional UNIX systems do not have these levels defined. Runlevel can be multiple parallel values,
To match multiple running levels. For most actions, only when runlevel matches the current running level successfully
.
For example:
# Default runlevel. The runlevels used by RHS are:
#0-halt (do not set initdefault to this)
#1-Single User Mode
#2-multiuser, without NFS (the same as 3, if you do not havenetworking)
#3-full multiuser Mode
#4-unused
#5-X11
#6-Reboot (do not set initdefault to this)

C. Action
Action is the running method of the process described later. Optional values of action include respawn, wait,
Once, Boot, bootwait, off, OnDemand, initdefault, and sysinit.
Actions such as sysinit, boot, and bootwait will run unconditionally when the system is started, and
Runlevel.
Respawn indicates that the process is automatically restarted when it is disabled.
Initdefault is a special action value used to identify the default startup level.
After being active, it reads the initdefault item in the inittab, obtains the runlevel, and acts as the current
Running level. If there is no inittab file or there is no initdefault item, init will be on the console
Request to enter runlevel. The subsequent process is ignored.
Once indicates that the process runs only once at the corresponding running level.
Wait runs only once, and init waits until the end of its running.
Kbrequest indicates that init starts the subsequent process when it receives a special keyboard event.

D. Process
Process is a specific execution program. The program can be followed by parameters.

V. Linux Embedded system initialization process

Some system initialization is also performed when the INIT process executes the process defined by inittab. Above
The inittab example contains the following similar content:

Xia xine600:
: Sysinit:/etc/rc. d/rc. sysinit

Moto e680:
# Boot-time system configuration/initialization script.
# This is run first packet t when booting in Emergency (-B) mode.
Si: sysinit:/etc/init. d/RCS

L0: 0: Wait:/etc/init. d/RC 0
L1: 1: Wait:/etc/init. d/RC 1
L2: 2: Wait:/etc/init. d/RC 2
L3: 3: Wait:/etc/init. d/RC 3
L4: 4: Wait:/etc/init. d/RC 4
L5: 5: Wait:/etc/init. d/RC 5
L6: 6: Wait:/etc/init. d/RC 6

RC. sysinit, RC, and RC are shell scripts that perform a lot of system initialization.
Main tasks include activating swap partitions, checking disks, loading hardware modules, and other requirements.
Priority is given to tasks. These scripts have a lot of content, and xiaxin e600 is relatively simple.
Description.

First, call the/etc/rc. d/rc. sysinit script.
Rs. sysinit first sets the path environment variable and then calls/etc/rc. d/init. d/functions
Set some environment variables and register some functions. These functions are mainly used to manage processes.
Then, Return Rs. sysinit for some file systems and network settings. Call
/Etc/rc. d/rc. modules to install the driver.
Finally, determine whether init is complete. Otherwise, the "factory settings script" will be executed ".
[-F "/mnt/user/etc/init_done"] |/usr/bin/origin. Sh

Here: sysinit:/etc/rc. d/rc. sysinit is over.

Then run the following necessary programs. There is nothing to say about the binary files used for both logs. No
Here/root is a connection, and the actual location is/mnt/user/etc.
: Respawn:/root/Logger
: Respawn:/root/Watchdog

The last script to be started is also located in/mnt/user/etc.
The graphic platform of the mobile phone is running.
: Once:/root/run_dv2.sh

Vi. Summary


This is a preliminary concept of the system startup process. But we can also understand that Xia Xin
Some functions of the e600 are idle. Compared with the moto e680, xiaxin e600 and Philips 968 are easy to start. However
Xiaxin e600 and Philips 968 prefer encapsulation. The basic sh commands are fully encapsulated in busybox and graphical programs.
All of them are encapsulated in quicklauncher. The init initialization process is not transparent, and many services have been started.
Encapsulation. In addition, the restrictions on software installation can be described as abnormal, and the software management method is also very rigid. Really no
I know whether or not I should call him a Linux phone or a Windows Phone.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.