[U-Boot] Magically determining the correct root

Alexander Holler holler at ahsoftware.de
Thu Aug 14 22:28:41 CEST 2014


Hello,

Over the years I've seen many error message from beginners because root= 
wasn't set or was set wrong. Often they call the cryptic message from 
the Linux kernel memory error or something similiar (because they just 
look at the stuff after the "init not found").

One of the reason they run into that is because they use a different 
partition layout than which is enforced by the board-config.

So here are my 2¢ to give people some more freedom in regard to the 
partition layout:

---------------------
	...
        "find_root=" \
                "setenv stdout nulldev;" \
                "for i in 0 1 2; do " \
                        "setexpr rootpart $part + $i;" \
                        "if ls $hw $dev:$rootpart dev; then " \
                                "setenv stderr serial;" \
                                "setenv stdout serial;" \
                                "exit;" \
                        "fi;" \
                "done;" \
                "setenv stdout serial;" \
                "setenv rootpart\0" \
        "linuxmmc=mmcblk0p\0" \
        "set_root=" \
                "run find_root;" \
                "if test -z \"$rootpart\"; then " \
                        "echo \"NO ROOT FOUND! (no partition with 
directory /dev)\";" \
                        "setexpr rootpart $part + 1;" \
                "fi;" \
                "if test $hw = mmc; then " \
                        "setenv root /dev/${linuxmmc}${rootpart};" \
                 "else " \
                        "setenv root /dev/sda${rootpart};" \
                "fi;" \
                "echo \"root is $root\"\0" \
	...
---------------------

What it does is the following:

It uses the variable $hw, $dev, $part and $linuxmmc as input and
$root as output.

- $hw might be mmc, usb or sata (or whatever "ls" does understand
- $dev is the device number (usually 0)
- $part is the partition number where the kernel is loaded from (most of 
the time 1 for the first partition)
- $linuxmmc is the linux-name of the mmc-device (e.g. mmcblk0p).

It then searches the partition from which the kernel was loaded from and 
the following two partitions for a "/dev" and assumes that will be the 
correct root partition.

So, assuming /dev does only exist on the root partition that little 
magic supports parition layouts like the following:

- boot (with kernel), root, something else
- root (with kernel), something else
- swap, boot (with kernel), root, something else
- boot (with kernel), swap, root, something else
...

Maybe someone does find usefull.

Regards,

Alexander Holler



More information about the U-Boot mailing list