Enable i915.fastboot=1 for the splash boot entry
[platform/adaptation/setup-scripts.git] / setup-ivi-bootloader-conf
1 #!/bin/sh -euf
2
3 # Copyright 2013-2014 Intel Corporation
4 # Author: Artem Bityutskiy
5 # License: GPLv2
6
7 PROG="setup-ivi-bootloader-conf"
8 VER="1.0"
9
10 srcdir="$(readlink -ev -- ${0%/*})"
11 PATH="/usr/share/setup-ivi:$srcdir:$PATH"
12
13 if [ -f "$srcdir/setup-ivi-sh-functions" ]; then
14         . "$srcdir/setup-ivi-sh-functions"
15         . "$srcdir/installerfw-sh-functions"
16 else
17         .  /usr/share/setup-ivi/setup-ivi-sh-functions
18         .  /usr/share/setup-ivi/installerfw-sh-functions
19 fi
20
21 # This is a small trick which I use to make sure my scripts are portable -
22 # check if 'dash' is present, and if yes - use it.
23 if can_switch_to_dash; then
24         exec dash -euf -- "$srcdir/$PROG" "$@"
25         exit $?
26 fi
27
28 # Preparation stuff common for all subcommands
29 prepare()
30 {
31         # Get the installer framework environment
32         installerfw_restore_env
33
34         rootdir="$(installerfw_mnt_prefix "/")"
35         bootdir="$(installerfw_mnt_prefix "/boot")"
36
37         if installerfw_is_efi_boot_system; then
38                 boot="gummiboot"
39         else
40                 boot="extlinux"
41         fi
42 }
43
44 #
45 # -----------------------------------------------------------------------------
46 # The "add" subcommand
47 # -----------------------------------------------------------------------------
48 #
49
50 show_add_usage()
51 {
52         cat <<-EOF
53 Usage: $PROG add [options] <kernel>
54
55 Add bootloader entries for kernel <kernel>.
56
57 Options:
58   -f, --force  if bootloader entries for <kernel> already exist in bootloader's
59                config file(s) - re-write them, if <kernel> does not exist - do
60                not fail
61   -h, --help   show this text and exit
62 EOF
63 }
64
65 show_add_usage_fail()
66 {
67         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
68         show_add_usage >&2
69         exit 1
70 }
71
72 add_subcommand()
73 {
74         if [ "$#" -eq 0  ]; then
75                 show_add_usage
76                 exit 0
77         fi
78
79         local tmp
80         tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
81                 show_add_usage_fail "cannot parse command-line options"
82         eval set -- "$tmp"
83
84         local force=
85         while true; do
86                 case "$1" in
87                 -f|--force)
88                         force="-f"
89                         ;;
90                 -h|--help)
91                         show_add_usage
92                         exit 0
93                         ;;
94                 --) shift; break
95                         ;;
96                 *) show_add_usage_fail "unrecognized option \"$1\""
97                         ;;
98                 esac
99                 shift
100         done
101
102         if [ "$#" -lt 1 ]; then
103                 show_add_usage_fail "please, specify the kernel"
104         fi
105         if [ "$#" -gt 1 ]; then
106                 show_add_usage_fail "too many arguments: \"$1\""
107         fi
108
109         prepare
110
111         local kernel="$1"
112         local kernel_path="$bootdir/$kernel"
113
114         if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
115                 fatal "cannot find kernel \"$kernel_path\"" \
116                       "(use -f to ignore this error)"
117         fi
118
119         # Get root partition PARTUUID
120         installerfw_get_part_info "/" "PARTUUID" "root_partuuid"
121         [ -n "$root_partuuid" ] || \
122                 fatal "cannot find PARTUUID of the root partition"
123
124         # Get kernel options
125         local options="${INSTALLERFW_KERNEL_OPTS:-} root=PARTUUID=$root_partuuid"
126
127         # Get OS name
128         local os_name=
129         get_os_name "os_name"
130
131         # Add the default bootloader entry
132         setup-$boot-conf $verbose --bootdir "$bootdir" add $force \
133                 "$kernel" "$os_name" "$kernel" "$options"
134
135         # Add the debug bootloader entry. If there is the "quiet" option,
136         # create a non-quiet configuration.
137         local verbose_opts="$(printf "%s" "$options" | LC_ALL=C \
138                                 sed -e "s/[[:blank:]]\+quiet[[:blank:]]\+/ /
139                                         s/^quiet[[:blank:]]\+//
140                                         s/[[:blank:]]\+quiet$//
141                                         s/^quiet$//")"
142
143         local debug_opts="$verbose_opts ignore_loglevel log_buf_len=2M"
144         local debug_opts="$debug_opts initcall_debug"
145
146
147         setup-$boot-conf $verbose --bootdir "$bootdir" add \
148                 $force "$kernel-debug" "Debug $os_name" \
149                 "$kernel" "$verbose_opts"
150
151         # Add the clone bootloader entry, but only if the cloning tool is
152         # installed
153         if [ -f "$rootdir/usr/sbin/setup-ivi-clone" ]; then
154                 clone_opts="$options systemd.unit=ivi-clone.service"
155                 clone_opts="$clone_opts ivi-clone-target=autodetect"
156                 setup-$boot-conf $verbose --bootdir "$bootdir" add \
157                         $force "$kernel-clone" "Clone $os_name" \
158                         "$kernel" "$clone_opts"
159         fi
160
161         # Use default gummiboot-splash file
162         local splash_path="$rootdir/usr/share/gummiboot/splash.bmp"
163
164         # Add a splash entry for fastboot testing and disable fbcon
165         if [ "$boot" = "gummiboot" ] && [ -f "$splash_path" ]; then
166                 splash_opts="$options i915.fastboot=1 fbcon=map:9"
167                 setup-$boot-conf $verbose --bootdir "$bootdir" add \
168                         $force --splash "$splash_path" "$kernel-splash" \
169                         "Splash $os_name" "$kernel" "$splash_opts"
170         fi
171 }
172
173 #
174 # -----------------------------------------------------------------------------
175 # The "remove" subcommand
176 # -----------------------------------------------------------------------------
177 #
178
179 show_remove_usage()
180 {
181         cat <<-EOF
182 Usage: $PROG remove [options] <kernel>
183
184 Delete bootloader entries for kernel <kernel> (only those which were previously
185 created with "$PROG add").
186
187 Options:
188   -f, --force  do not fail if <kernel> does not have corresponding bootloader
189                entries
190   -h, --help   show this text and exit
191 EOF
192 }
193
194 show_remove_usage_fail()
195 {
196         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
197         show_remove_usage >&2
198         exit 1
199 }
200
201 remove_subcommand()
202 {
203         if [ "$#" -eq 0  ]; then
204                 show_remove_usage
205                 exit 0
206         fi
207
208         local tmp
209         tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
210                 show_remove_usage_fail "cannot parse command-line options"
211         eval set -- "$tmp"
212
213         local force=
214         while true; do
215                 case "$1" in
216                 -f|--force)
217                         force="-f"
218                         ;;
219                 -h|--help)
220                         show_remove_usage
221                         exit 0
222                         ;;
223                 --) shift; break
224                         ;;
225                 *) show_remove_usage_fail "unrecognized option \"$1\""
226                         ;;
227                 esac
228                 shift
229         done
230
231         if [ "$#" -lt 1 ]; then
232                 show_add_usage_fail "please, specify the kernel"
233         fi
234         if [ "$#" -gt 1 ]; then
235                 show_remove_usage_fail "too many arguments: \"$1\""
236         fi
237
238         local kernel="$1"
239
240         prepare
241
242         # Get the current default entry
243         local default="$(setup-$boot-conf $verbose --bootdir "$bootdir" \
244                                               default $force)"
245         [ $? -eq 0 ] || \
246                 fatal "cannot get the default kernel, setup-$boot-conf failed"
247
248         local default_kernel="$(printf "%s" "$default" | LC_ALL=C \
249                          sed -n -e 's/^kernel: \(.\+\)$/\1/p')"
250
251         if [ -n "$default_kernel" ]; then
252                 verbose "current default boot kernel is " \
253                         "\"$default_kernel\""
254         else
255                 verbose "did not find the default kernel," \
256                       "setup-$boot-conf returned: $default"
257         fi
258
259         # Remove the kernel
260         setup-$boot-conf $verbose --bootdir "$bootdir" \
261                              remove $force "$kernel" || \
262                 fatal "setup-$boot-conf failed to remove" \
263                       "entry \"$kernel\""
264         setup-$boot-conf $verbose --bootdir "$bootdir" \
265                              remove $force "$kernel-debug" || \
266                 fatal "setup-$boot-conf failed to remove" \
267                       "entry \"$kernel-verbose\""
268         # The "clone" entry does not necessary exist, so use --force
269         setup-$boot-conf $verbose --bootdir "$bootdir" \
270                              remove --force "$kernel-clone" || \
271                 fatal "setup-$boot-conf failed to remove" \
272                       "entry \"$kernel-clone\""
273         # Ditto for "splash"
274         setup-$boot-conf $verbose --bootdir "$bootdir" \
275                              remove --force "$kernel-splash" || \
276                 fatal "setup-$boot-conf failed to remove" \
277                       "entry \"$kernel-splash\""
278
279         # If this is not the default kernel, we are done
280         [ "$kernel" = "$default_kernel" ] || return 0
281
282         # We've just removed the default kernel, find the kernel with the
283         # latest version and make it to be the default
284
285         verbose "removed the default kernel, find the newest available"
286
287         local newest_kernel="$(get_newest_kernel "$bootdir" "$kernel")"
288
289         if [ -z "$newest_kernel" ]; then
290                 verbose "no more kernels, set the kernel to none"
291                 setup-$boot-conf $verbose --bootdir "$bootdir" \
292                                      default --force "<none>"
293         else
294                 verbose "new default kernel is \"$newest_kernel\""
295                 setup-$boot-conf $verbose --bootdir "$bootdir" \
296                                      default "$newest_kernel"
297         fi
298
299         if [ "$?" -ne 0 ]; then
300                 fatal "cannot set default kernel, \"setup-$boot-conf\" failed"
301         fi
302 }
303
304 #
305 # -----------------------------------------------------------------------------
306 # The "default" subcommand
307 # -----------------------------------------------------------------------------
308 #
309
310 show_default_usage()
311 {
312         cat <<-EOF
313 Usage: $PROG default [options] <kernel>
314
315 Set the default boot kernel to <kernel>. If <kernel> is omited, print the
316 current default boot kernel name.
317
318 Options:
319   -f, --force  do not fail if <kernel> doesn't exist
320   -h, --help   show this text and exit
321 EOF
322 }
323
324 show_default_usage_fail()
325 {
326         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
327         show_default_usage >&2
328         exit 1
329 }
330
331 default_subcommand()
332 {
333         local tmp
334         tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
335                 show_default_usage_fail "cannot parse command-line options"
336         eval set -- "$tmp"
337
338         local force=
339         while true; do
340                 case "$1" in
341                 -f|--force)
342                         force="-f"
343                         ;;
344                 -h|--help)
345                         show_default_usage
346                         exit 0
347                         ;;
348                 --) shift; break
349                         ;;
350                 *) show_default_usage_fail "unrecognized option \"$1\""
351                         ;;
352                 esac
353                 shift
354         done
355
356         if [ "$#" -gt 1 ]; then
357                 show_default_usage_fail "too many arguments: \"$1\""
358         fi
359
360         prepare
361
362         local kernel="${1:-}"
363         local kernel_path="$bootdir/$kernel"
364
365         if [ -n "$kernel" ] && ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
366                 fatal "cannot find kernel \"$kernel_path\"" \
367                       "(use -f to ignore this error)"
368         fi
369
370         setup-$boot-conf $verbose --bootdir "$bootdir" default $force "$kernel"
371 }
372
373 #
374 # -----------------------------------------------------------------------------
375 #
376
377 show_usage()
378 {
379         cat <<-EOF
380 Usage: $PROG [options] <subcommand> [options] <arguments>
381
382 This program adds or removes a kernel to/from the bootloader configuration
383 file(s). This is a Tizen IVI-specific program and it currently supports only 2
384 bootloader types - extlinux and gummiboot. Each new kernel gets 2 boot menu
385 entries - the default and verbose, and both of these are removed by the
386 "remove" subcommand.
387
388 The supported subcommands are:
389    add     - add bootloader entries for a kernel
390    remove  - remove bootloader entries for a kernel
391    default - get or set the default boot kernel
392
393 Run "$PROG <subcommand>" to see subcommand-specific help.
394
395 Options:
396   --version      show the program version and exit
397   -v, --verbose  be verbose
398   -h, --help     show this text and exit
399 EOF
400 }
401
402 show_usage_fail()
403 {
404         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
405         show_usage >&2
406         exit 1
407 }
408
409 verbose=
410 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
411         case "$1" in
412         --version)
413                 printf "%s\n" "$VER"
414                 exit 0
415                 ;;
416         -v|--verbose)
417                 verbose="-v"
418                 ;;
419         -h|--help)
420                 show_usage
421                 exit 0
422                 ;;
423         --) shift; break
424                 ;;
425         *) show_usage_fail "unrecognized option \"$1\""
426                 ;;
427         esac
428         shift
429 done
430
431 if [ "$#" -eq 0 ]; then
432         show_usage
433         exit 0
434 fi
435
436 # Parse subcommands
437
438 subcommand="$1"; shift
439
440 case "$subcommand" in
441 add)
442         add_subcommand "$@"
443         break
444         ;;
445 remove)
446         remove_subcommand "$@"
447         break
448         ;;
449 default)
450         default_subcommand "$@"
451         break
452         ;;
453 *) show_usage_fail "unrecognized subcommand \"$subcommand\""
454         ;;
455 esac