From 513cbc26b177f52fa83d981a18cb7c3115351748 Mon Sep 17 00:00:00 2001
From: Andreas Loibl <andreas@andreas-loibl.de>
Date: Thu, 8 May 2014 18:16:26 +0200
Subject: grub EFI

---
 backend/modules/bootloader | 50 +++++++++++++++++++++++++++++-----------------
 backend/modules/hdmap      | 31 ----------------------------
 backend/modules/install    |  8 ++++++++
 backend/modules/partitions | 24 +++++++++++++++++++---
 4 files changed, 61 insertions(+), 52 deletions(-)

diff --git a/backend/modules/bootloader b/backend/modules/bootloader
index afb5091..99d378d 100644
--- a/backend/modules/bootloader
+++ b/backend/modules/bootloader
@@ -9,6 +9,13 @@
 #   /dev/sdb1 60003385344 Rootpartition (filesysem ext4)
 function list_bootloader_targets()
 {
+    if [ -d /sys/firmware/efi ]; then
+        root_disk="$(get_disk "$(hdmap_get device of mountpoint /)")"
+        for efi_part in $(list_efi_partitions | grep "$root_disk"; list_efi_partitions | grep -v "$root_disk")
+        do
+            echo "$efi_part $(blockdev --getsize64 $efi_part) EFI partition"
+        done
+    else
         for disk in $(list_all_disks)
         do
                 echo "$disk $(blockdev --getsize64 $disk) Master Boot Record"
@@ -22,6 +29,7 @@ function list_bootloader_targets()
                 echo "$root_dev $(blockdev --getsize64 $root_dev) Rootpartition (filesystem $root_fs)"
                 ;;
         esac
+    fi
 }
 
 function send_bootloader_targets()
@@ -74,26 +82,32 @@ function install_grub()
 {
     rm -f $TARGET/boot/vmlinuz $TARGET/boot/System.map $TARGET/boot/initrd.img
 
+    apple=0
+    # TODO Apple detection, e.g. using dmidecode
+    false && apple=1
+
     # install grub
     mkdir -p "$TARGET/boot/grub"
-    grub-install --recheck --no-floppy --root-directory=$TARGET "$cfg_bootloader_target" &>/dev/null || \
-    grub-install --force --recheck --no-floppy --root-directory=$TARGET "$cfg_bootloader_target" &>/dev/null
-
-#    # create device.map and save it to target
-#    export device_map=$TARGET/tmp/device.map
-#    get_device_map > $device_map
-#    cat $device_map > $TARGET/boot/grub/device.map
-#    rm -f $device_map
-
-    # preseed grub-pc with install-target
-    for path in /dev/disk/by-id/*
-    do
-    	[ -e "$path" ] || continue
-    	if [ "$(readlink -f "$path")" = "$(readlink -f "$cfg_bootloader_target")" ]; then
-            echo "grub-pc grub-pc/install_devices multiselect $path" | chroot_it debconf-set-selections &>/dev/null
-	    break
-	fi
-    done
+    if [ -d /sys/firmware/efi ]; then
+        if is_removeable "$cfg_bootloader_target" && [ "$apple" = 1 ]; then
+            chroot_it grub-install --removeable &>/dev/null
+        else
+            chroot_it grub-install &>/dev/null
+        fi
+    else
+        grub-install --recheck --no-floppy --root-directory=$TARGET "$cfg_bootloader_target" &>/dev/null || \
+        grub-install --force --recheck --no-floppy --root-directory=$TARGET "$cfg_bootloader_target" &>/dev/null
+
+        # preseed grub-pc with install-target
+        for path in /dev/disk/by-id/*
+        do
+            [ -e "$path" ] || continue
+            if [ "$(readlink -f "$path")" = "$(readlink -f "$cfg_bootloader_target")" ]; then
+                echo "grub-pc grub-pc/install_devices multiselect $path" | chroot_it debconf-set-selections &>/dev/null
+            break
+        fi
+        done
+    fi
     
     write_kernel_img_conf
 
diff --git a/backend/modules/hdmap b/backend/modules/hdmap
index 09e2b91..ee447b4 100644
--- a/backend/modules/hdmap
+++ b/backend/modules/hdmap
@@ -327,40 +327,9 @@ function fill_hdmap()
         (
                 echo "$cfg_hdmap"
                 root_disk="$(get_disk "$(hdmap_get device of mountpoint /)")"
-                efi_part="$(hdmap_get device of mountpoint /boot/efi)"
-                for part in $(list_efi_partitions | grep "$root_disk"; list_efi_partitions | grep -v "$root_disk")
-                do
-                        # prefer EFI partition on root disk
-                        if [ -z "$efi_part" ]; then
-                                # preselect the first of the EFI partitions found
-                                echo "$part:/boot/efi::auto"
-                                efi_part="$part"
-                        else
-                                # list other possible EFI partitions in hdmap but don't select them
-                                echo "$part:::"
-                        fi
-                done
                 for part in $(list_partitions -usage filesystem)
                 do
                         disk="$(get_disk "$part")"
-                        if [ -z "$efi_part" ] && [ "$disk" = "$root_disk" ] && \
-                           [ "$(get_filesystem_type $part)" = "DOS" ]; then
-                                case "$(blkid -p -s PTTYPE -o value "$disk")" in
-                                gpt)
-                                        efi_part="$part"
-                                        ;;
-                                dos)
-                                        # only primary MSDOS partitions can be EFI partitions
-                                        partnr="${part/$disk}"
-                                        partnr="${partnr//[^0-9]}"
-                                        [ -n "$partnr" -a "$partnr" -ge 1 -a "$partnr" -le 4 ] && efi_part="$part"
-                                        ;;
-                                esac
-                                if [ "$efi_part" ]; then
-                                        echo "$efi_part:/boot/efi::auto"
-                                        continue
-                                fi
-                        fi
                         if is_removeable "$part" && [ "$disk" != "$root_disk" ]; then
                                 echo "$part:::"
                         else
diff --git a/backend/modules/install b/backend/modules/install
index fc400da..4d63fec 100644
--- a/backend/modules/install
+++ b/backend/modules/install
@@ -24,6 +24,14 @@ function prepare_target()
 	send install_step prepare_target
 	mkdir -p /live/hdinstall
 	export TARGET=/live/hdinstall
+
+    # add EFI bootloader target to hdmap
+    if [ -d /sys/firmware/efi ]; then
+        efi_part="$(get_disk "$cfg_bootloader_target")"
+        if [ "$efi_part" ]; then
+            hdmap_set "$efi_part:/boot/efi::auto"
+        fi
+    fi
 }
 
 # Synopsis: chroot_it <...>
diff --git a/backend/modules/partitions b/backend/modules/partitions
index 9af0b96..eac5e6f 100644
--- a/backend/modules/partitions
+++ b/backend/modules/partitions
@@ -7,7 +7,7 @@
 #   /dev/sda
 function strip_live_media()
 {
-	grep -ve '^$' $(awk '{if($2=="/live/image" && $3=="iso9660"){gsub(/[0-9]*$/,"",$1); print "-e ^"$1; exit 0;}}' < /proc/mounts)
+	grep -ve '^$' $(awk '{if(($2=="/live/image" || $2=="/lib/live/mount/medium") && $3=="iso9660"){gsub(/[0-9]*$/,"",$1); print "-e ^"$1; exit 0;}}' < /proc/mounts)
 }
 
 # Synopsis: list_all_disks
@@ -175,14 +175,32 @@ function list_linux_partitions()
 # Output example:
 #   /dev/sda1
 #   /dev/sdb1
-function list_linux_partitions()
+function list_efi_partitions()
 {
-    for part in $(list_all_partitions)
+    root_disk="$(get_disk "$(hdmap_get device of mountpoint /)")"
+    for part in $(list_all_partitions | strip_live_media)
     do
         case "$(blkid -p -s PART_ENTRY_TYPE -o value "$part")" in
             0xef|c12a7328-f81f-11d2-ba4b-00a0c93ec93b)
                 [ "$(get_filesystem_type $part)" = "DOS" ] && echo "$part"
                 ;;
+            *)
+                disk="$(get_disk "$part")"
+                if [ "$disk" = "$root_disk" ] && \
+                   [ "$(get_filesystem_type "$part")" = "DOS" ]; then
+                    case "$(blkid -p -s PTTYPE -o value "$disk")" in
+                    gpt)
+                        echo "$part"
+                        ;;
+                    dos)
+                        # only primary MSDOS partitions can be EFI partitions
+                        partnr="${part/$disk}"
+                        partnr="${partnr//[^0-9]}"
+                        [ -n "$partnr" -a "$partnr" -ge 1 -a "$partnr" -le 4 ] && echo "$part"
+                        ;;
+                    esac
+                fi
+                ;;
         esac
     done
 }
-- 
cgit v1.0