[OpenWrt] OpenWrt boot process analysis

http://www.programdevelop.com/4531245/

 

Summarize the the OpenWrt startup process: 1.CFE-> 2.linux-> 3./etc/preinit-> 4./sbin/init -> 5./etc/inittab -> 6./etc/init.d / rcS-> 7./etc/rc.d/S * -> 8.
OpenWrt is an open Linux platform, mainly for the wireless router with wifi.

Similar to Ubuntu, Red Hat, like Linux release, it will have its own start-up process. Now, I would to my Linksys WRT54G as a platform to introduce the OpenWrt startup process.

First of all, CFE (common firmware environment) - it is a bootloader, similar to the u-boot, redboot like a bit Broadcom company the Queen suspected of -

The first to start. Its task is to create a simple environment, so that the system up and running first. In addition to being able to jump to a specific address to start the operating system (such as Linux)

It also allows you to download things to the above, such as download a Linux, and then start it.

In addition, it is worth mentioning that, CFE will be 1,2 seconds after the start time to wait by the tftp upload the kernel and flash programmer

This operating system is corrupt the CFE can work "bricks and mortar" board a chance to revive. Please note that once the linux boot from linux all over the system,

At this time there is no CFE what's going on. The only connection the CFE passed to the kernel a command-line parameters after linux boot up, this can be viewed with the following command:

 

root @ OpenWrt :/ # cat / proc / cmdline

console = ttyS0, 115200 mtdparts = spi_flash: 1m (u-boot) ro, 3m (kernel), - (rootfs)

 

After the Linux system starts up. It uses similar to the following script command to parse cmdline:

for x in $ (cat / proc / cmdline); do

case $ x in

init = *)

init = $ {x # init =}

;;

root = *)

ROOT = $ {x # root =}

case $ ROOT in

LABEL = *)

ROOT = "/ dev / disk / by-label / $ {ROOT # LABEL =}"

;;

UUID = *)

ROOT = "/ dev / disk / by-uuid / $ {ROOT # UUID =}"

;;

esac

;;

rootflags = *)

ROOTFLAGS = "-o $ {x # rootflags =}"

;;

cryptopts = *)

cryptopts = "$ {x # cryptopts =}"

;;

nfsroot = *)

NFSROOT = "$ {x # nfsroot =}"

;;

nfsopts = *)

NFSOPTS = "-o $ {x # nfsopts =}"

;;

The boot = *)

BOOT = $ {x # boot =}

;;

resume = *)

RESUME = $ {x # resume =}

;;

noresume)

NORESUME = y

;;

quiet)

quiet = y

;;

ro)

readonly = y

;;

rw)

readonly = n

;;

debug)

debug = y

exec> / tmp / initramfs.debug 2> & 1

set-x

;;

break = *)

break = $ {x # break =}

;;

break)

break = premount

;;

esac

done

The OpenWrt cmdline mean is: root = / dev/mtdblock2 file system on the third flash partition (mtdblock0 1,2);

rootfstype = squashfs, JFFS2 file system type is the squashfs and the jffs2 (why two types, it is unclear,

But it is certain OpenWrt more complex file system squashfs compression and JFFS2 write);

init = / etc / preinit perform the initialization, noinitrd console = ttyS0, 115200

No initrd and console port settings.

 

init = / etc / preinit Linux performs initialization, as follows:

root @ OpenWrt :/ # cat / etc / preinit

#! / Bin / sh

# Copyright (C) 2006 OpenWrt.org

export PATH = / bin :/ sbin :/ usr / bin :/ usr / sbin

. / Etc / diag.sh

 

failsafe_ip () {

ifconfig $ ifname 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.1.2 55 up

}

 

failsafe () {

[-N "$ ifname"] && grep "$ ifname" / proc / net / dev> / dev / null && {

failsafe_ip

netmsg 192.168.1.255 "Entering Failsafe!"

telnetd-l / bin / login <> / dev / null 2> & 1

}

lock / tmp / .failsafe

ash - login

}

 

mount proc / proc-t proc

mount sysfs / sys-t sysfs

 

size = $ (awk '/ MemTotal :/ {l = 5242880; mt = ($ 2 * 1024); print ((s = mt / 2) <l)&&(mt> l)? mt-l: s}' / proc / meminfo)

mount tmpfs / tmp-t tmpfs-o size = $ size, nosuid, nodev, mode = 1777

 

if grep devfs / proc / filesystems> / dev / null; then

mount devfs / dev-t devfs

M0 = / dev/pty/m0

M1 = / dev/pty/m1

HOTPLUG = / sbin / hotplug-call

 

elif [--x / sbin/hotplug2]; then

mount-t tmpfs tmpfs / dev-o size = 512K

mknod / dev / console c 5 1

/ Sbin/hotplug2 - coldplug - set-rules-file / etc/hotplug2-init.rules

/ Sbin/hotplug2 - no-coldplug - persistent - set-rules-file / etc/hotplug2-init.rules &

M0 = / dev / ptmx

M1 = / dev / ptmx

Hotplug =

 

elif [-x / sbin / udevd]; then

mount-n-t tmpfs-o mode = 0755 udev / dev

/ Sbin / udevd - daemon

/ Sbin / udevtrigger

/ Sbin / udevsettle

M0 = / dev / pty / ptmx

M1 = / dev / pty / ptmx

Hotplug =

fi

 

mkdir-p / dev / pts / dev / shm

mount devpts / dev / pts-t devpts

 

# The shell really doesn't like having stdin / out closed

# That's why we use / dev/pty/m0 and m1 as replacement

# For / dev / console if there's no serial console available

dd if = / dev / console of = / dev / null bs = 1 count = 0> / dev / null 2> / dev / null && {

M0 = / dev / console

M1 = / dev / console

}

 

exec <$ M0> $ M1 2> & 0

 

echo "- preinit -"

echo "Press CTRL-C for failsafe"

trap 'FAILSAFE = true' INT

trap 'FAILSAFE = true' USR1

[-E / etc / preinit.arch] &&. / Etc / preinit.arch

set_state preinit

echo "$ HOTPLUG"> / proc / sys / kernel / hotplug

eval $ {FAILSAFE: + failsafe}

lock-w / tmp / .failsafe

 

if [-z "$ INITRAMFS"]; then

mount_root

[-F / sysupgrade.tgz] && {

echo "- config restore -

cd /

the mv sysupgrade.tgz / tmp

tar xzf / tmp / sysupgrade.tgz

rm-f / tmp / sysupgrade.tgz

sync

}

 

echo "- init -

 

exec / sbin / init

fi

 

Visible, it is mounted Some systems require a file system, for example the tmpfs, proc and sysfs (really mount depends on kernel 2.6).

And device node and Recovery (failsafe) preparation. (Have not read)

Finally, the exec / sbin / init to start the file system on the OpenWrt busybox init program.

It will automatically analyze the / etc / inittab file, its contents explained the cmd help see the busybox website.

 

The contents of the / etc / inittab:

:: Sysinit :/ etc / init.d / rcS S boot

:: Shutdown :/ etc / init.d / rcS K stop

tts / 0 :: askfirst :/ bin / ash - login

ttyS0 :: askfirst :/ bin / ash - login

tty1 :: askfirst :/ bin / ash - login

 

Run / etc / init.d / rcS:

 

#! / Bin / sh

# Copyright (C) 2006 OpenWrt.org

 

run_scripts () {

for i in / etc / rc.d / $ 1 *; do

[-X $ i] && $ i $ 2 2> & 1

done | $ LOGGER

}

 

LOGGER = "cat"

[-X / usr / bin / logger] && LOGGER = "logger-s-p 6-t sysinit"

 

if ["$ 1" = "S"]; then

run_scripts "$ 1" "$ 2" &

else

run_scripts "$ 1" "$ 2"

fi

/ Etc / rc.d / S * these scripts will execute:

root @ OpenWrt :/ # ls / etc / rc.d / S *

/ Etc/rc.d/S10boot / etc/rc.d/S50cron / etc/rc.d/S60led

/ Etc/rc.d/S20fstab / etc/rc.d/S50dropbear / etc/rc.d/S95done

/ Etc/rc.d/S39usb / etc/rc.d/S50httpd / etc/rc.d/S97watchdog

/ Etc/rc.d/S40network / etc/rc.d/S50telnet / etc/rc.d/S99sysctl

/ Etc/rc.d/S45firewall / etc/rc.d/S60dnsmasq

According to figures from small to large order.

posted @ 2014-11-25 14:15  fastwave2004  阅读(668)  评论(0)    收藏  举报