
Allwinner T113 Linux 5.4 Boot Program Flow
This document provides a detailed walkthrough of the Linux 5.4 boot process on the Allwinner T113 platform, covering the sequence from U-Boot parameter passing to the execution of the user-space init process. It explains how the kernel initializes, mounts the root filesystem, and transitions to user mode, highlighting key components like the kernel_init function and the /etc/inittab configuration file. This serves as a practical reference for developers looking to understand or customize the system's auto-start behavior on T113-based embedded systems.
Boot Steps
- Pass parameters by specifying U-Boot environment variables.
- The kernel starts init, obtains the first process, and switches to user mode.
- init starts system services and applications according to the configuration file.
When checking the U-Boot environment variables, it can be seen that when the boot method is specified by setargs_mmc, there is a parameter init=${init}, where ${init} refers to the environment variable init=/init.
init=/init setargs_mmc=setenv bootargs earlycon=${earlycon} clk_ignore_unused initcall_debug=${initcall_debug} console=${console} loglevel=${loglevel} root=${mmc_root} init=${init} partitions=${partitions} cma=${cma} snum=${snum} mac_addr=${eth0_mac} wifi_mac=${wifi_mac} bt_mac=${bt_mac} specialstr=${specialstr} gpt=1
This indicates that the boot parameter configuration is already set during the U-Boot stage (on some platforms, a /linuxrc file is used).
After kernel initialization is complete, the init process runs with process ID 1 and serves as the parent process of all other processes. Other software also starts running concurrently. Switch to the init program running in user mode. The init program is configured through the /etc/inittab file.
The kernel source code in kernel/linux-5.4/init/main.c contains the kernel_init function. The process running this function is called the init process, which is process 1.
In the T113, kernel_init calls kernel_init_freeable, and kernel_init_freeable opens the console.
/* Open the /dev/console on the rootfs, this should never fail */ if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0) pr_err("Warning: unable to open an initial console.\n"); (void) ksys_dup(0); (void) ksys_dup(0);
Here, the /dev/console file is opened, and the file descriptor is duplicated twice, resulting in three file descriptors: 0, 1, and 2, which correspond to the standard input, standard output, and standard error, respectively. Process 1 opens these three file descriptors, so all processes spawned from process 1 inherit these file descriptors by default.
However, for the init process to transition from kernel mode to user mode, it must execute a user-space application. To run this application, it must first be located. To locate the application, the root filesystem must be mounted, since all applications reside within the filesystem. In the source code, it can be found that the init process mounts the root filesystem through the prepare_namespace function.
if (ksys_access((const char __user *) ramdisk_execute_command, 0) != 0) { ramdisk_execute_command = NULL; prepare_namespace(); }
Next, U-Boot environment variables are used to pass parameters specifying the root filesystem partition and other related information. In U-Boot's cmdline parameters, init=/init indicates that the init program located in the root directory of the root filesystem is the 'init application,' which the kernel identifies and executes.
Identify the init program. if (ramdisk_execute_command) { ret = run_init_process(ramdisk_execute_command); if (!ret) return 0; pr_err("Failed to execute %s (error %d)\n", ramdisk_execute_command, ret); } Execute the init program. if (execute_command) { ret = run_init_process(execute_command); if (!ret) return 0; panic("Requested init %s failed (error %d).", execute_command, ret); }
The init process transitions from a kernel-mode process to a user-mode process. The process ID remains unchanged (still process 1) during the transition.
The above describes the process of the kernel starting the init program, but what exactly is the init program? This binary file is located in the root filesystem and is a symbolic link pointing to the /bin/busybox command within the root filesystem.
root@ok113i:/# ls -l lrwxrwxrwx 1 root root 11 Sep 11 2024 init -> bin/busybox
Earlier, it was mentioned that the init program is configured through the /etc/inittab file. Upon examining the /etc/inittab configuration file, the entries are defined in four fields separated by colons.
Format id:runlevels:action:process
Example Explanation:
::sysinit:/etc/init.d/rcS is the command for the boot startup script, meaning that the command /etc/init.d/rcS runs at system initialization in all runlevels.
id: It is the identifier for each entry, uniquely identifying it. It cannot be duplicated and specifies the terminal on which it runs, such as a tty.
runlevels: The system runlevels indicating at which levels the process action should be executed. Multiple runlevels can be specified without any separators. If left empty, the action runs at all runlevels.
Linux defines six runlevels with typical behaviors as follows:
Runlevel | Description |
---|---|
0 | Shutdown |
1 | Single-user mode; no password required; network drivers and some services are disabled |
2 | Multi-user mode without NFS service enabled |
3 | Command-line mode |
4 | Reserved and unused |
5 | Graphical user interface mode |
6 | System reboot |
action: Represents the condition under which the process for the corresponding entry is executed.
Here are a few commonly used examples:
sysinit | Executed during system initialization |
null::sysinit | Indicates that the command does not need to run at system startup but runs as needed during system operation. Such commands are usually used to set environment variables or create symbolic links, which may not be needed during system startup but might be required during runtime. |
respawn | Restarts the process whenever it terminates (used for daemon processes) |
shutdown | Commands that run during system shutdown |
process | Specifies which program, script, or command to execute |