summarylogtreecommitdiffstats
path: root/30_android-prober
blob: 59a653c45d365680c2031c4aa1c08548feaa7471 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
set -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009  Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

. "$pkgdatadir/grub-mkconfig_lib"
. /etc/default/grub

export TEXTDOMAIN=grub
export TEXTDOMAINDIR="${datarootdir}/locale"

CLASS="--class android --class os"
OS="Android"

sub_indent() { echo "$submenu_indentation$*"; }

android_entry() {
	title="$1"
	src="$2"
	kernel="$3"
	initrd="$4"
	disk="$5"
	device_id="$6"
	type="$7"

	[ "$type" != "default" ] &&
		case "$kernels" in
		gearlock*) title="$title, with ${kernel##*/} - $system" ;;
		ab) title="$title - Slot $(case "${kernel}" in *_a) echo A ;; *) echo B ;; esac)" ;;
		*) title="$title, with ${kernel##*/}, and ${initrd##*/}" ;;
		esac

	sub_indent "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option '$OS-${src##*/}-${kernel##*/}-${initrd##*/}-$type-$device_id' {"

	save_default_entry | grub_add_tab

	# Use ELILO's generic "efifb" when it's known to be available.
	# FIXME: We need an interface to select vesafb in case efifb can't be used.
	if [ ! "$GRUB_GFXPAYLOAD_LINUX" ]; then
		sub_indent "	load_video"
		grep -qx "CONFIG_FB_EFI=y" "${config}" 2>/dev/null &&
			grep -qx "CONFIG_VT_HW_CONSOLE_BINDING=y" "${config}" 2>/dev/null &&
			sub_indent "	set gfxpayload=keep"

	else
		[ "$GRUB_GFXPAYLOAD_LINUX" = text ] ||
			sub_indent "	load_video"
		sub_indent "	set gfxpayload=$GRUB_GFXPAYLOAD_LINUX"
	fi

	sub_indent "	insmod gzio"
	echo "$disk" | sed "s/^/$submenu_indentation	/gm"
	sub_indent "	linux $kernel $GRUB_CMDLINE_ANDROID root=/dev/ram0 SRC=$src"
	sub_indent "	initrd $initrd"

	sub_indent "}
"
}

gen_entry() {
	detect=$1
	shift

	i=1
	while read -rd $'\0' arg; do
		case $i in
		1) device=$arg ;;
		2) src=$arg ;;
		3) os=$arg ;;
		4) kernels=$arg ;;
		5) initrds=$arg ;;
		6) ucodes=$arg ;;
		7) system=$arg ;;
		8) data=$arg ;;
		*) break ;;
		esac
		i=$((i + 1))
	done < <(sed 's/\xFF0/\xFF\x00/g; s/\xFF\(.\)/\1/g' <<<"$detect")

	ucode_list=""
	ucode=""
	disk="$(prepare_grub_to_access_device "$device")"
	boot_device_id="$(grub_get_device_id "$device")"

	unset IFS

	echo "Found $os on $device" >&2

	type=default
	MAIN_ENTRY=""
	EXTENDED_ENTRIES=""

	# Extra indentation to add to menu entries in a submenu. We're not in a submenu
	# yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
	submenu_indentation=""

	entry() {
		android_entry \
			"$os" \
			"$src" \
			"${src}/${2}" \
			"${ucode_list# } ${src}/${3}" \
			"$disk" \
			"$boot_device_id" \
			"$1"
	}

	parse_to_entry() {
		if [ "$type" = "default" ]; then
			MAIN_ENTRY="$(entry default "$1" "$2")"
			type=extended
			submenu_indentation=$grub_tab
		fi
		EXTENDED_ENTRIES="$EXTENDED_ENTRIES
$(entry extended "$1" "$2")"
	}

	IFS=","
	case "$kernels" in
	gearlock*)
		for ucode in $ucodes; do
			ucode_list="$ucode_list $src/boot/$ucode"
		done

		if [ "$GRUB_DISABLE_LINUX_UUID" != "true" ]; then
			ROOT_DEVICE=$device
			ROOT_UUID=$(${grub_probe} --device "$device" --target=fs_uuid 2>/dev/null)

			[ "$ROOT_UUID" ] &&
				ROOT_DEVICE="UUID=$ROOT_UUID"
		fi

		old_cmdline=$GRUB_CMDLINE_ANDROID
		GRUB_CMDLINE_ANDROID="$GRUB_CMDLINE_ANDROID ROOT=$ROOT_DEVICE SYSTEM=$system DATA=$data GEARLOCK=$kernels"

		for kver in $initrds; do
			parse_to_entry "boot/kernel-$kver" "boot/initrd-$kver.img"
		done

		GRUB_CMDLINE_ANDROID=$old_cmdline
		;;
	ab)
		for ucode in $ucodes; do
			ucode_list="$ucode_list $src/$ucode"
		done

		if [ "$GRUB_DISABLE_LINUX_UUID" != "true" ] &&
			[ "$GRUB_ENABLE_ANDROID_UUID" = "true" ]; then
			ROOT_DEVICE=$device
			ROOT_UUID=$(${grub_probe} --device "$device" --target=fs_uuid 2>/dev/null)

			[ "$ROOT_UUID" ] &&
				ROOT_DEVICE="UUID=$ROOT_UUID"
		fi

		old_cmdline=$GRUB_CMDLINE_ANDROID
		GRUB_CMDLINE_ANDROID="$GRUB_CMDLINE_ANDROID ROOT=$ROOT_DEVICE"

		for mode in a b; do
			parse_to_entry "kernel_$mode" "initrd_$mode.img"
		done

		GRUB_CMDLINE_ANDROID=$old_cmdline
		;;
	*)
		for ucode in $ucodes; do
			ucode_list="$ucode_list $src/$ucode"
		done

		for kernel in $kernels; do
			for initrd in $initrds; do
				parse_to_entry "$kernel" "$initrd"
			done
		done
		;;
	esac

	unset IFS

	cat <<EOF

$MAIN_ENTRY

submenu '$(echo "Advanced options for $os" | grub_quote)' \$menuentry_id_option '$OS-${src##*/}-$type-$boot_device_id' { 
$EXTENDED_ENTRIES
}

EOF
}

while read -r detect; do
	gen_entry "$detect"
done < <(grub-android-prober)