#!/bin/sh # Zipit /sbin/init script to check for mmc or nfsroot fs to mount # before booting internal flash ramdisk. # written by Chris Studholme PATH=/sbin:/bin:/usr/sbin:/usr/bin DEBUG_LOG=/tmp/init-debug.log echo "init started" >$DEBUG_LOG # boot to internal flash loaded ramdisk boot_local() { rm -f /sbin/init # this program ln -s /bin/busybox /sbin/init echo "/sbin/init (local boot)" >>$DEBUG_LOG exec /sbin/init /dev/console 2>&1 } # boot mmc card mounted with mount_mmc() boot_mmc() { cd /mmc echo "pivot_root . $MMC_OLD_ROOT (pwd "`pwd`")" >>$DEBUG_LOG echo "chroot . $MMC_INIT (mmc boot)" >>$DEBUG_LOG pivot_root . "$MMC_OLD_ROOT" exec chroot . "$MMC_INIT" dev/console 2>&1 } # returns only if mmc card is not immediately booted, # return true(0) if mmc card successfully mounted and is ready to boot, # return false(1) if failed to mount mmc card mount_mmc() { # check for mmc device file and mountpoint if ! test -b /dev/mmca1 -a -d /mmc; then return 1 fi # mount /proc as it's needed by mount (auto fs type detection I believe) mount -t proc proc /proc # load mmc module echo "insmod mmc" >>$DEBUG_LOG if ! insmod mmc >>$DEBUG_LOG 2>&1; then echo "insmod failed" >>$DEBUG_LOG return 1 fi # mount filesystem echo "mount -oro /dev/mmca1 /mmc" >>$DEBUG_LOG if ! mount -oro /dev/mmca1 /mmc >>$DEBUG_LOG 2>&1; then echo "mount failed" >>$DEBUG_LOG echo "-- dmesg|tail" >>$DEBUG_LOG dmesg |tail >>$DEBUG_LOG echo "--" >>$DEBUG_LOG return 1 fi # check for necessary files if ! test -c /mmc/dev/console -a \ -r /mmc/etc/zipit.conf; then echo "files /mmc/dev/console and /mmc/etc/zipit.conf missing" >>$DEBUG_LOG return 1 fi # read config file . /mmc/etc/zipit.conf # make sure mountpoint for ramdisk root exists if test -z "$MMC_OLD_ROOT" -o ! -d /mmc/"$MMC_OLD_ROOT"; then echo "dir /mmc/$MMC_OLD_ROOT missing" >>$DEBUG_LOG return 1 fi # make sure init program exists if test -z "$MMC_INIT"; then MMC_INIT=sbin/init fi if ! test -x /mmc/"$MMC_INIT"; then echo "executable /mmc/$MMC_INIT missing" >>$DEBUG_LOG return 1 fi if test "$MMC_BOOT" = "yes"; then boot_mmc elif test "$MMC_BOOT" = "net"; then echo "mmc boot deferred" >>$DEBUG_LOG return 0 else echo "mmc boot disabled" >>$DEBUG_LOG return 1 fi } # mount and boot remote root (nfs) filesystem # does not return if successful boot_net() { if ! test -b /dev/mtdblock5 -a -d /mtdpref; then return 1 fi # load mtd and cramfs modules /etc/init.d/S10mtd_rwdev /etc/init.d/S11cramfs if ! mount -o ro /dev/mtdblock5 /mtdpref; then echo "mount mtdpref failed" >>$DEBUG_LOG return 1 fi if ! test -r /mtdpref/etc/nfsroot.conf; then echo "nfsroot.conf not found" >>$DEBUG_LOG umount /mtdpref return 1 fi # read config file . /mtdpref/etc/nfsroot.conf umount /mtdpref # vars are: # NET_BOOT= yes to boot net # NET_INIT= path to init program in new root # NET_NEW_ROOT= absolute path to new root # NET_OLD_ROOT= relative path in new root for old root # check if net boot is enabled if test "$NET_BOOT" != "yes"; then echo "net boot disabled" >>$DEBUG_LOG return 1 fi # make sure mountpoint for ramdisk root exists if test -z "$NET_OLD_ROOT" -o ! -d "$NET_NEW_ROOT/$NET_OLD_ROOT"; then echo "dir $NET_NEW_ROOT/$NET_OLD_ROOT missing" >>$DEBUG_LOG return 1 fi # make sure init program exists if test -z "$NET_INIT"; then NET_INIT=sbin/init fi if ! test -x "$NET_NEW_ROOT/$NET_INIT"; then echo "executable $NET_NEW_ROOT/$NET_INIT missing" >>$DEBUG_LOG return 1 fi cd "$NET_NEW_ROOT" echo "pivot_root . $NET_OLD_ROOT (pwd "`pwd`")" >>$DEBUG_LOG echo "chroot . $NET_INIT (net boot)" >>$DEBUG_LOG pivot_root . "$NET_OLD_ROOT" exec chroot . "$NET_INIT" dev/console 2>&1 } # failsafe: if lid switch is depressed (lid closed), boot internal flash if /bin/lid; then boot_local fi # first check for mmc card, then check for net root if mount_mmc; then boot_net boot_mmc else boot_net fi # all attempts for remote root failed, just boot internal flash boot_local