#!/bin/sh mount -t proc proc /proc mount -t sysfs sysfs /sys mount -t devtmpfs none /dev exec < /dev/console > /dev/console 2>&1 for parm in $(cat /proc/cmdline); do case $parm in ramfs) RAMFS_MODE=1 ;; root=*) ROOT_DEVICE=`echo $parm | awk -F\= '{print $2}'` ;; console=*) CONSOLE_DEVICE=`echo $parm | awk -F\= '{print $2}' | awk -F\, '{print $1}'` ;; gpt=*) GPT_SUPPORT=`echo $parm | awk -F\= '{print $2}'` ;; esac done if [ "x$ROOT_DEVICE" = "x" ]; then ROOT_DEVICE=autoconfig fi VERITY_BLOCK=` sed 's/ /\n/g' /proc/cmdline | awk -F'=' '/^partition/{print $2}' | sed 's/:/\n/g'|awk -F'@' '/^verity_block/{print $2}' ` ROOTFSTYPE=` sed 's/ /\n/g' /proc/cmdline | awk -F'=' '/^rootfstype/{print $2}' ` echo [$0]: "getty is $CONSOLE_DEVICE" echo [$0]: RootDevice is \"$ROOT_DEVICE\" , GPT_SUPPORT=$GPT_SUPPORT echo [$0]: "verity block is /dev/$VERITY_BLOCK" echo [$0]: "rootfstype is $ROOTFSTYPE" #set the default device for autoconfig if [ "x$GPT_SUPPORT" = "x1" ]; then NAND_DEF_DEVICE=nand0p5 SDMMC_DEF_DEVICE=mmcblk0p5 else NAND_DEF_DEVICE=nandd SDMMC_DEF_DEVICE=mmcblk0p7 fi # $1: the name of block device wait_for_ready() { CNT=10 while true; do if [ -b $1 ]; then return 0 fi echo [$0]: Wait $1 ready ... CNT=`expr $CNT - 1` if [ $CNT -eq 0 ]; then echo [$0]: $1 is not available! return 1 fi sleep 1 done } # param1: root device; param2: verity block device do_mount_verity() { # get signature, Note: sign is the first blk. dd if=$2 of=/tmp/sign bs=1c count=256 > /dev/null 2>&1 # get raw_table length table_size=` hexdump -d -n 2 -s 256 $2 | awk '{print $2}'` # get raw_table dd if=$2 of=/tmp/raw_table bs=1c count=$table_size skip=4096 > /dev/null 2>&1 #verify signature openssl dgst -sha256 -verify verity_key -signature /tmp/sign /tmp/raw_table if [ $? -ne 0 ]; then echo [$0]: "raw_table signature verification failure!" return fi # get Root hash and Salt root_hash=` sed -n '/Root hash/p' /tmp/raw_table | awk '{print $3}' ` salt=` sed -n '/Salt/p' /tmp/raw_table | awk '{print $2}' ` # get hash tree offset if [ `expr ${table_size} % 4096 ` = "0" ]; then hash_offset=` expr 4096 + ${table_size} ` else hash_offset=` expr 4096 + ${table_size} / 4096 \* 4096 + 4096 ` fi # create /dev/mapper/rootfs veritysetup create rootfs --hash-offset=${hash_offset} $1 $2 $root_hash -s $salt # mount root if [ "x$ROOTFSTYPE" = "x" ]; then mount /dev/mapper/rootfs /mnt else mount -t $ROOTFSTYPE /dev/mapper/rootfs /mnt fi if [ $? -ne 0 ]; then echo [$0]: "Failed to mount /dev/mapper/rootfs!" return fi } # $1: The block device do_mount() { if [ "x$VERITY_BLOCK" != "x" ]; then if [ -f verity_key ]; then do_mount_verity $1 /dev/$VERITY_BLOCK return fi fi # e2fsck -y $1 # mount -o rw,noatime,nodiratime,norelatime,noauto_da_alloc,barrier=0,data=ordered -t ext4 $1 /mnt #mount -t squashfs $1 /mnt if [ "x$ROOTFSTYPE" = "x" ]; then mount $1 /mnt else mount -t $ROOTFSTYPE $1 /mnt fi if [ $? -ne 0 ]; then echo [$0]: Failed to mount $1! fi } load_nand() { echo [$0]: Try to load Nand ... # NAND_MODULE=/lib/modules/$(uname -r)/nand.ko # if [ ! -f $NAND_MODULE ]; then # echo [$0]: $NAND_MODULE does not exist! # return 1 # fi # insmod $NAND_MODULE # if [ $? -ne 0 ]; then # echo [$0]: $NAND_MODULE is invalid! # return 2 # fi wait_for_ready $1 if [ $? -eq 0 ]; then do_mount $1 fi } load_emmc() { echo [$0]: Try to load EMMC ... wait_for_ready $1 if [ $? -eq 0 ]; then do_mount $1 fi } load_nor() { echo [$0]: Try to load Nor ... wait_for_ready $1 if [ $? -eq 0 ]; then do_mount $1 fi } case $ROOT_DEVICE in /dev/nand*|/dev/system) load_nand $ROOT_DEVICE ;; /dev/mmc*) load_emmc $ROOT_DEVICE ;; /dev/mtd*) load_nor $ROOT_DEVICE ;; autoconfig*) sleep 1; echo [$0]: default device $SDMMC_DEF_DEVICE $NAND_DEF_DEVICE if cat /proc/partitions|grep "$SDMMC_DEF_DEVICE" >/dev/null;then magic_num=$(hexdump -s 1292 -n 2 -x /dev/$SDMMC_DEF_DEVICE|head -1|awk '{print $2 }') echo $magic_num|grep "f30a" >/dev/null if [ $? -eq 0 ]; then echo [$0]: "magic_num[f30a] match!!!" else load_emmc /dev/$SDMMC_DEF_DEVICE fi else load_nand /dev/$NAND_DEF_DEVICE fi ;; *) echo [$0]: "Use default type" ;; esac [ -x /mnt/pseudo_init ] && mount -o noatime,move /dev /mnt/dev && exec switch_root /mnt /pseudo_init [ -x /mnt/sbin/init ] && exec switch_root /mnt /sbin/init #/sbin/getty -L $CONSOLE_DEVICE 115200 vt100 -n -l /bin/sh /bin/sh