summaryrefslogtreecommitdiff
path: root/packages/initrdscripts/files/30-bootmenu.sh
blob: d54a6438079ca0eed61313490b80a1aea126dec2 (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
#
# (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

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"
    cnt=0
    echo -e $list | \
    while read l; do
        if [ $cnt == $num ]; then
	    echo -e -n "${E}1m"
	fi
        echo -e "$cnt: $l${E}0m"
	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
#	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)"

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

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

# Main loop
show_menu
while read -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

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" : '[^/]*\([^ ]*\).*'`

if [ "$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"
    CMDLINE="$CMDLINE root=$ROOT_DEVICE"
fi

echo ROOT_DEVICE=$ROOT_DEVICE
echo CMDLINE=$CMDLINE

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