OpenWrt kernel boot analysis

  u-boot reads the Linux kernel from the Flash partition to the memory, and then jumps to the memory (a certain address) to execute the Linux kernel. The Linux kernel will perform a series of verifications, register related drivers according to the device tree file (see the figure below openwrt/target/linux/realtek/dts-5.15/XXX.dts), create partitions, and then mount the root file system, start the first user space process.

insert image description here

1. The first process started (/etc/preinit)

  The first user space process started by the Linux kernel (the root file is busybox) is /sbin/init by default. But openwrt modified it so that the first userspace process started by default is /etc/preinit.
  In the /init/main.c file of the linux source code file, the static int __ref kernel_init(void *unused) function will call /sbin/init after execution,
insert image description here
  and in the openwrt source code /package/base-files/file/etc/ Preinit is actually a shell script that reads:

#!/bin/sh
# Copyright (C) 2006-2016 OpenWrt.org
# Copyright (C) 2010 Vertical Communications

[ -z "$PREINIT" ] && exec /sbin/init

export PATH="%PATH%"

. /lib/functions.sh
. /lib/functions/preinit.sh
. /lib/functions/system.sh

boot_hook_init preinit_essential
boot_hook_init preinit_main
boot_hook_init failsafe
boot_hook_init initramfs
boot_hook_init preinit_mount_root

for pi_source_file in /lib/preinit/*; do
	. $pi_source_file
done

boot_run_hook preinit_essential

pi_mount_skip_next=false
pi_jffs2_mount_success=false
pi_failsafe_net_message=false

boot_run_hook preinit_main

  After compiling, the internal content of the openwrt system is shown in the figure below:
insert image description here
  the first statement it executes is as follows: [ -z "PREINIT"] means that "PREINIT" is empty, that is, it is true when PREINIT is NULL. Since the "PREINIT" variable is not defined when the preinit script is executed, the condition is true, and the following statement (exec /sbin/init) is executed.
  After /etc/preinit is executed, the /etc/inittab file will be executed.

Two, /etc/inittab file

  The Linux operating system /etc/inittab file is generally interpreted by /sbin/init under busybox; while the openwrt system /etc/inittab file (located at /target/linux/ramips/base-files/etc/inittab of the openwrt source code) is Interpreted by /sbin/procd.
insert image description here

Guess you like

Origin blog.csdn.net/xxxx123041/article/details/132722419