setup-gummiboot-conf: fix the script
[profile/ivi/setup-efi-ivi.git] / setup-efi-ivi
1 #!/bin/sh -efu
2
3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
5 # License: GPLv2
6
7 #
8 # This is a simple script which installs gummiboot to the ESP and creates the
9 # gummiboot configuration files. This script requires a number of environment
10 # variables to be defined. Namely:
11 #
12 # 1. INSTALLERFW_PART_COUNT - count of paritions
13 # 2. INSTALLERFW_PARTX_MOUNTPOINT - mount point of partition number X
14 #    (0 <= x < $INSTALLERFW_PART_COUNT)
15 # 3. INSTALLERFW_PARTX_PARTUUID - GPT GUID (AKA PARTUUID) of partition number X
16 # 4. INSTALLERFW_MOUNT_PREFIX - where the target partitions are mounted (the
17 #    "root" directory of the file-system we install gummiboot to)
18
19 PROG="setup-efi-ivi"
20
21 # This is a helper function which prints an error message and exits
22 fatal()
23 {
24         printf "%s\n" "$PROG: error: $1" 1>&2
25         exit 1
26 }
27
28 # Make sure the installer framework variables are defined
29 [ "${INSTALLERFW_PART_COUNT:+x}" == "x" ] ||
30        fatal "installer framework environment variables not found"
31
32 # Find the required root and boot parition parameters
33 pnum=0
34 root_partuuid=
35 boot_mountpoint=
36
37 while [ "$pnum" -lt "$INSTALLERFW_PART_COUNT" ]; do
38         mountpoint="INSTALLERFW_PART${pnum}_MOUNTPOINT"
39         mountpoint="$(eval printf "%s" '$'$mountpoint)"
40
41         # Find out all the required data for the root and boot partition
42         if [ "$mountpoint" == "/" ]; then
43                 root_partuuid="INSTALLERFW_PART${pnum}_PARTUUID"
44                 root_partuuid="$(eval printf "%s" '$'$root_partuuid)"
45         # The boot parition has to be at "/boot"
46         elif [ "$mountpoint" == "/boot" ]; then
47                 boot_mountpoint="$mountpoint"
48         fi
49
50         pnum="$((pnum+1))"
51 done
52
53 # Get the ESP location
54 esp="$INSTALLERFW_MOUNT_PREFIX/$boot_mountpoint"
55
56 # Make sure gummiboot is installed in the system
57 if ! ([ -f /usr/lib/gummiboot/gummibootia32.efi ] || \
58       [ -f /usr/lib/gummiboot/gummibootx64.efi ]); then
59         fatal "\"/usr/lib/gummiboot/gummiboot*.efi\" files not found!"
60 fi
61
62 # Install gummiboot
63 mkdir -p "$esp/EFI/boot"
64 [ -f "/usr/lib/gummiboot/gummibootia32.efi" ] &&
65         cp "/usr/lib/gummiboot/gummibootia32.efi" "$esp/EFI/boot/bootia32.efi"
66 [ -f "/usr/lib/gummiboot/gummibootx64.efi" ] &&
67         cp "/usr/lib/gummiboot/gummibootx64.efi" "$esp/boot/EFI/boot/bootx64.efi"
68
69 # Generate the list of installed kernels
70 kernels="$(ls -1 "$esp" | grep "^vmlinuz-" | sort -r | head -n1)"
71 # Get the newest kernel
72 newest_kernel="$(printf "%s" "$kernels" | head -n1)"
73
74 # Create the gummiboot configuration file
75 mkdir -p "$esp/loader"
76
77 cat > "$esp/loader/loader.conf" <<-EOF
78 timeout 0
79 default $newest_kernel.conf
80 EOF
81
82 # Create a gummiboot entry for each kernel
83 mkdir -p "$esp/loader/entries"
84 printf "%s\n" "$kernels" | while IFS= read -r kernel; do
85
86         kernel_version="$(printf "%s" $kernel | sed -e 's/vmlinuz-\(.*\)/\1/')"
87
88         cat > "$esp/loader/entries/$kernel.conf" <<-EOF
89         title Tizen IVI 3.0
90         version $kernel_version
91         efi /$kernel
92         options $INSTALLERFW_KERNEL_OPTS root=PARTUUID=$root_partuuid
93         EOF
94 done