summaryrefslogtreecommitdiff
path: root/packages/initrdscripts/files/30-bootmenu.sh
blob: 283daadd44a64cbcad27bd0bae8556dcbec501a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# (c) 2007 Paul Sokolovsky
#

# If root is explicitly specified, skip interactive selection
if [ -z "$ROOT_DEVICE" ]; then
##############################

E="\033["
MOUNTLOC="tmp"
LOOP_IMG_MASK='*.img'

if ! (echo " " | read -n1 foo) >/dev/null 2>&1; then
    echo "'read' command lacks -n switch support, aborting" 
    exit 1
fi

if [ -z "$rootdelay" ]; then
    echo "rootdelay parameter was not passed on kernel command line - assuming 2s delay"
    echo "If you would like to avoid this delay, pass explicit rootdelay=0"
    sleep 2
    dev_setup
fi

mkdir -p $MOUNTLOC

list=""

add_menu_item()
{
    if [ -n "$list" ]; then
	list="$list\n"
    fi
	
    list="$list$1"
}

show_menu() {
    echo -e -n "${E}3;0H" >$CONSOLE
    cnt=0
    echo -e $list | \
    while read l; do
        if [ $cnt == $num ]; then
	    echo -e -n "${E}1m" >$CONSOLE
	fi
        echo -e "$cnt: $l${E}0m" >$CONSOLE
	cnt=$((cnt + 1))
    done
}

get_menu_selection()
{
    cnt=0
    sel=`echo -e $list | \
    while read l; do
    if [ $cnt == $num ]; then
	    echo $l
	    break
	fi
	cnt=$((cnt + 1))
    done`
}

get_partition_type()
{
#    fstype=`mount -f --guess-fstype /dev/$dev $MOUNTLOC`
    fstype=`fstype </dev/$dev`
    fstype=`expr "$fstype" : 'FSTYPE=\([A-Za-z0-9]*\).*'`
}

scan_for_loopimgs()
{
# Scan a device for loopback images, add to the list if found
	mount /dev/$dev $MOUNTLOC
	p=$PWD
	cd $MOUNTLOC
	for img in `ls -1 $LOOP_IMG_MASK 2>/dev/null`; do
	    add_menu_item "$dev/$img (loop img on vfat)"
	done
	cd $p
	umount $MOUNTLOC
}

# Scan all available device/partitions
while read maj min nblk dev; do
    if [ -z "$maj" -o "$maj" == "major" ]; then
	continue;
    fi

    get_partition_type
    if [ "$fstype" != "ext2" -a "$fstype" != "ext3" -a "$fstype" != "vfat" -a "$fstype" != "jffs2" ]; then
	# Comment following line to show all available block devices regardless of FS (for debug purposes)
	continue
	true
    fi
    
    if [ "$fstype" == "vfat" ]; then
	scan_for_loopimgs
	continue
    fi
    
    add_menu_item "$dev ($fstype)"
done < /proc/partitions

add_menu_item "NFS (nfsroot=192.168.2.200:/srv/nfs/oe/image)"
add_menu_item "Shell"

total=`echo -e $list | wc -l`
num=0

# Draw UI
stty -F $CONSOLE -echo
echo -e -n "${E}2J" >$CONSOLE
echo -e -n "${E}0;0H" >$CONSOLE
echo "Select boot image:" >$CONSOLE

# Main loop
show_menu
while read -s -n1 i; do
    case "$i" in
	"A")
	    num=$((num - 1))
	    if [ $num -lt 0 ]; then
		num=$(($total - 1))
	    fi
	;;
	["B"-"Z"])
	    num=$((num + 1))
	    if [ $num -ge $total ]; then
		num=0
	    fi
	;;
	"q")
	    exec sh
	;;
	"")
	    break
	;;
    esac
    show_menu
#    echo "*$esc$i"
done < $CONSOLE

stty echo

# Process results of user selection, prepare input arguments
# for boot modules

get_menu_selection
echo Selected: $sel

dev=`expr "$sel" : '\([^ /]*\)'`
path=`expr "$sel" : '[^/]*\([^ ]*\).*'`
fstype=`expr "$sel" : '[^ ]* *\(.*\)'`

if [ "$dev" == "Shell" ]; then
    exec /bin/sh
elif [ "$dev" == "NFS" ]; then
    ROOT_DEVICE="/dev/nfs"
    CMDLINE="$CMDLINE root=/dev/nfs nfsroot=192.168.2.200:/srv/nfs/oe/image"
elif [ -n "$path" ]; then
    ROOT_DEVICE="/dev/loop"
    CMDLINE="$CMDLINE root=/dev/loop looproot=/dev/$dev:$path"
else
    ROOT_DEVICE="/dev/$dev"
    # jffs2 is not recognized by mount automagically
    if [ "$fstype" == "(jffs2)" ]; then
	ROOT_FSTYPE="jffs2"
    fi
    CMDLINE="$CMDLINE root=$ROOT_DEVICE"
fi

echo ROOT_DEVICE=$ROOT_DEVICE >$CONSOLE
echo CMDLINE=$CMDLINE >$CONSOLE

##############################
fi