OSDN Git Service

Search for efi partition
[android-x86/bootable-newinstaller.git] / install / scripts / 1-install
index de4d861..094a032 100644 (file)
@@ -1,6 +1,6 @@
 #
 # By Chih-Wei Huang <cwhuang@linux.org.tw>
-# Last updated 2010/10/14
+# Last updated 2012/07/07
 #
 # License: GNU Public License
 # We explicitely grant the right to use the scripts
@@ -10,6 +10,8 @@
 tempfile=/tmp/temp-$$
 menufile=/tmp/menu-$$
 
+CPIO=cpio
+
 rebooting()
 {
        dialog --title " Rebooting... " --nocancel --pause "" 8 41 1
@@ -27,14 +29,79 @@ choose()
        choice=`cat $tempfile`
 }
 
+partition_drive()
+{
+       echo -n > $menufile
+       for i in /sys/block/[shv]d[a-z] /sys/block/mmcblk?; do
+               if [ ! -d $i ]; then  # pathname expansion failed
+                       continue
+               fi
+               echo -n `basename $i` >> $menufile
+               if [ -f $i/removable -a `cat $i/removable` -eq 0 ]; then
+                       echo -n ' "Harddisk ' >> $menufile
+               else
+                       echo -n ' "Removable' >> $menufile
+               fi
+               if [ -f $i/size ]; then
+                       echo -n " (" `cat $i/size` "blocks)" >> $menufile
+               fi
+               echo '"' >> $menufile
+       done
+       count=`wc -l $menufile | awk '{ print $1 }'`
+       if [ $count -eq 0 ]; then
+               dialog --title " Error " --msgbox \
+                       "\nOK. There is no hard drive to edit partitions." 8 49
+               return 255
+       fi
+       if [ $count -eq 1 ]; then
+               choice=`awk '{ print $1 }' $menufile`
+               retval=0
+       else
+               choose "Choose Drive" "Please select a drive to edit partitions:"
+       fi
+       if [ $retval -eq 0 ]; then
+               dialog --title " Confirm " --defaultno --yesno "\n Do you want to use GPT?" 7 29
+               if [ $? -eq 0 ]; then
+                       cgdisk /dev/$choice
+               else
+                       cfdisk /dev/$choice
+               fi
+               if [ $? -eq 0 ]; then
+                       retval=1
+               else
+                       retval=255
+               fi
+       fi
+       return $retval
+}
+
 select_dev()
 {
-       fdisk -l | grep ^/dev | cut -b6-12,55- | awk '{
-               if (!match($2, "Extended")) {
-                       printf("\"%-28s", $0)
-                       system("echo -n `cat /sys/block/*/"$1"/../device/model`")
-                       printf("\" \"\"\n")
-               }
+       blkid | grep -v -E "^/dev/block/|^/dev/loop" | cut -b6- | sort | awk '{
+               t="unknown"
+               for (i = NF; i > 1; --i)
+                       if (match($i, "^TYPE")) {
+                               t=$i
+                               break
+                       }
+               gsub(/TYPE=|"/, "", t)
+               printf("%s\t%s\n", $1, t)
+       }' > $tempfile
+
+       lsblk=`ls /sys/block | grep -v -E "loop|ram|sr|boot|rpmb"`
+       for d in $lsblk; do
+               p=0
+               for i in /sys/block/$d/$d* /sys/block/$d; do
+                       [ -e $i/partition ] && p=1
+                       [ $p -eq 1 -a "$i" = "/sys/block/$d" ] && break
+                       echo $i | grep -q -E "boot|rpmb" && continue
+                       [ -d $i ] && ( grep "`basename $i:`" $tempfile || echo "`basename $i` unknown" )
+               done
+       done | awk '{
+               sub(/:/, "", $1)
+               printf("\"%-13s%-17s", $1, $2)
+               system("cd /sys/block; for f in "$1"/device/model "$1"/device/name */"$1"/../device/model */"$1"/../device/name; do [ -e $f ] && echo -n `cat $f` && break; done")
+               printf("\" \"\"\n")
        } END {
                printf("\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
        }' > $menufile
@@ -63,13 +130,13 @@ format_fs()
                        cmd="mkntfs -fL"
                        ;;
                fat32)
-                       cmd="mkdosfs -vn"
+                       cmd="mkdosfs -n"
                        ;;
                *)
                        ;;
        esac
        if [ -n "$cmd" ]; then
-               dialog --title " Confirm " --no-label Skip --yesno \
+               dialog --title " Confirm " --defaultno --yesno \
                        "\n You chose to format $1 to $choice.\n All data in that partition will LOSE.\n\n Are you sure to format the partition $1?" 10 51
                [ $? -ne 0 ] && return 1
                $cmd Android-x86 /dev/$1 | awk '{
@@ -90,13 +157,13 @@ create_entry()
 create_menulst()
 {
        menulst=/hd/grub/menu.lst
-       [ -n "$VESA" ] && vga=" vga=788"
-       echo -e "default=0\ntimeout=6\nroot (hd0,$1)\nsplashimage=/grub/android-x86.xpm.gz\n" > $menulst
-       cmdline=`cat /proc/cmdline | sed "s|\(initrd.*img\s*\)||; s|quiet\s*||; s|\(vga=\w\+\?\s*\)||; s|\(.DPI=\w\+\?\s*\)||; s|\(INSTALL=\w\+\?\s*\)||; s|\(SRC=\S\+\?\s*\)||; s|\(DEBUG=\w\+\?\s*\)||; s|\(BOOT_IMAGE.*\)||"`
+       [ -n "$VESA" ] && vga=" vga=788 modeset=0"
+       echo -e "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/android-x86.xpm.gz\n}root (hd0,$1)\n" > $menulst
 
-       create_entry "Android-x86 $VER (HDPI)" quiet $cmdline HDPI=1
-       create_entry "Android-x86 $VER (MDPI)" quiet $cmdline MDPI=1
-       create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=1
+       create_entry "Android-x86 $VER" quiet $cmdline
+       create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=2
+       create_entry "Android-x86 $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2
+       create_entry "Android-x86 $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2
 }
 
 create_winitem()
@@ -109,13 +176,81 @@ create_winitem()
                dialog --title " Confirm " --yesno \
                        "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
                [ $? -ne 0 ] && return 1
-               p=$((`echo $win | cut -b4-`-1))
-               echo -e "title Windows\n\trootnoverify (hd$d,$p)\n\tchainloader +1\n" >> $menulst
+               wp=$((`echo $win | cut -b4-`-1))
+               echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst
        fi
 }
 
+create_img()
+{
+       bname=`basename $2`
+       if [ -e $2 ]; then
+               dialog --title " Confirm " --defaultno --yesno \
+                       "\n $bname exists. Overwrite it?" 7 38
+               [ $? -ne 0 ] && return 255
+               rm -f $2
+       fi
+       dialog --title " Question " --nook --nocancel --inputbox \
+               "\nPlease input the size of the $bname in MB:" 8 63 $1 2> $tempfile
+       size=`cat $tempfile`
+       [ 0$size -le 0 ] && size=1024
+       ( dd bs=1M count=$size if=/dev/zero | pv -ns ${size}m | dd of=$2 ) 2>&1 \
+               | progress_bar "Creating $bname" "Expect to write $size MB..."
+}
+
+create_data_img()
+{
+       dialog --title " Confirm " --yesno \
+               "\nThe installer is going to create a disk image to save the user data. At least 512MB free disk space is recommended.\n\nAre you sure to create the image?" 11 62
+
+       if [ $? -eq 0 ]; then
+               if create_img 512 data.img; then
+                       losetup /dev/loop6 data.img
+                       mke2fs -jL /data /dev/loop6 > /dev/tty6
+               fi
+               [ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33
+       else
+               dialog --title " Warning " --msgbox \
+                       "\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49
+       fi
+}
+
+try_upgrade()
+{
+       [ -d $1 ] && return
+       PREV_VERS="4.4-r4 4.4-r3 4.4-r2 4.4-r1 4.4-RC2 4.4-RC1 4.4-test 4.3-test 4.2-test 4.0-r1 4.0-RC2 4.0-RC1"
+       for v in $PREV_VERS; do
+               prev=hd/android-$v
+               if [ -d $prev ]; then
+                       dialog --title " Question " --yesno \
+                               "\nAn older Android-x86 version $v is detected.\nWould you like to upgrade it?" 8 55
+                       if [ $? -eq 0 ]; then
+                               mv $prev $1
+                               rm -rf $1/data/dalvik-cache/* $1/data/system/wpa_supplicant
+                               sed -i 's/\(ctrl_interface=\)\(.*\)/\1wlan0/' $1/data/misc/wifi/wpa_supplicant.conf
+                               break
+                       fi
+               fi
+       done
+}
+
+get_part_info()
+{
+       d=0
+       while [ 1 ]; do
+               h=`echo $d | awk '{ printf("%c", $1+97) }'`
+               for part in /sys/block/[shv]d$h/$1 /sys/block/mmcblk$d/$1; do
+                       [ -d $part ] && break 2
+               done
+               d=$(($d+1))
+       done
+       p=`cat $part/partition`
+       disk=$(basename `dirname $part`)
+}
+
 install_to()
 {
+       cd /
        mountpoint -q /hd && umount /hd
        while [ 1 ]; do
                format_fs $1
@@ -126,65 +261,114 @@ install_to()
        done
 
        fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`
-       if [ "$fs" = "vfat" ]; then
-               dialog --title " Warning " --yesno \
-                       "\nYou are going to install Android-x86 to a fat32 partition. Though this is possible, android cannot save user data to that partition. So data will be save to a RAMDISK(tmpfs), and lose after power off.\n\nAre you sure to continue?" 12 63
-               [ $? -ne 0 ] && return 1
-       elif [ "$fs" = "fuseblk" ]; then
-               dialog --title " Warning " --yesno \
-                       "\nYou are going to install Android-x86 to an NTFS partition. Though this is possible, some user data cannot be saved properly to that partition and will lose after power off.\n\nAre you sure to continue?" 11 63
-               [ $? -ne 0 ] && return 1
-       fi
+       cmdline=`sed "s|\(initrd.*img\s*\)||; s|quiet\s*||; s|\(vga=\w\+\?\s*\)||; s|\(DPI=\w\+\?\s*\)||; s|\(INSTALL=\w\+\?\s*\)||; s|\(SRC=\S\+\?\s*\)||; s|\(DEBUG=\w\+\?\s*\)||; s|\(BOOT_IMAGE=\S\+\?\s*\)||" /proc/cmdline`
 
        asrc=android-$VER
-       dialog --title " Confirm " --no-label Skip --yesno \
+       dialog --title " Confirm " --no-label Skip --defaultno --yesno \
                "\n Do you want to install boot loader GRUB?" 7 47
        if [ $? -eq 0 ]; then
                cp -af /grub /hd
-               d=0
-               while [ 1 ]; do
-                       h=`echo $d | awk '{ printf("%c", $1+97) }'`
-                       [ -d /sys/block/[sh]d$h/$1 ] && break
-                       d=$(($d+1))
-               done
-               p=$((`echo $1 | cut -b4-`-1))
+               get_part_info $1
+               p=$(($p-1))
                create_menulst $p
                create_winitem $1 $d
                rm -f /hd/boot/grub/stage1
-               echo "setup (hd$d) (hd$d,$p)" | grub > /dev/tty5
+               echo "(hd$d) /dev/$disk" > /hd/grub/device.map
+               echo "setup (hd$d) (hd$d,$p)" | grub --device-map /hd/grub/device.map > /dev/tty5
                [ $? -ne 0 ] && return 255
        fi
 
-       cd /mnt/$SRC
-       files="kernel initrd.img ramdisk.img system.*"
+       dialog --title " Confirm " --no-label Skip --defaultno --yesno \
+               "\n Do you want to install EFI GRUB2?" 7 39
+       if [ $? -eq 0 ]; then
+               get_part_info $1
+               for i in /sys/block/$disk/$disk*; do
+                       [ 0`cat $i/partition` -eq 1 ] && b=$i
+                       [ $(blkid /dev/`basename $i` | grep -c vfat) -ne 0 ] && b=$i && break
+               done
+               boot=`basename $b`
+               mountpoint -q /hd && umount /hd
+               dialog --title " Confirm " --defaultno --yesno \
+                       "\n Do you want to format the boot partition\n /dev/$boot?" 8 45
+               [ $? -eq 0 ] && mkdosfs -n EFI /dev/$boot
+               try_mount rw /dev/$boot /hd
+               cp -af /grub2/efi /hd
+               mkdir -p /hd/boot/grub
+               grubcfg=/hd/boot/grub/grub.cfg
+               echo -e "set timeout=5\n\n" > $grubcfg
+               echo -e "menuentry \"Android-x86 $VER\" {\n\tsearch --set=root --file /$asrc/kernel\n\tlinux /$asrc/kernel quiet $cmdline \n\tinitrd /$asrc/initrd.img\n}" >> $grubcfg
+               echo -e "menuentry \"Android-x86 $VER (DEBUG mode)\" {\n\tsearch --set=root --file /$asrc/kernel\n\tlinux /$asrc/kernel $cmdline DEBUG=2\n\tinitrd /$asrc/initrd.img\n}" >> $grubcfg
+               if [ -e /hd/EFI/Microsoft/Boot/bootmgfw.efi ]; then
+                       echo -e "menuentry \"Windows (UEFI)\" {\n\tsearch --set=root --file /EFI/Microsoft/Boot/bootmgfw.efi\n\tchainloader /EFI/Microsoft/Boot/bootmgfw.efi\n}" >> $grubcfg
+               fi
+               mountpoint -q /hd && umount /hd
+               try_mount rw /dev/$1 /hd
+       fi
+
+       dialog --title " Question " --yesno \
+               "\nDo you want to install /system directory as read-write?\n\nMaking /system be read-write is easier for debugging, but it needs more disk space and longer installation time." 10 61
+       instal_rw=$?
+
+       files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/ramdisk.img"
+       if [ $instal_rw -eq 0 ]; then
+               if [ "$fs" = "vfat" -o "$fs" = "fuseblk" ]; then
+                       [ -e /sfs/system.img ] && sysimg="/sfs/system.img" || sysimg="mnt/$SRC/system.*"
+               else
+                       sysimg="android/system"
+               fi
+       else
+               sysimg="mnt/$SRC/system.*"
+       fi
+       files="$files $sysimg"
        size=0
-       for s in `ls -ks $files | awk '{print $1}'`; do
+       for s in `du -sk $files | awk '{print $1}'`; do
                size=$(($size+$s))
        done
-       mkdir /hd/$asrc
-       ( ls $files | cpio -H newc -o | pv -ns ${size}k | ( cd /hd/$asrc; cpio -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
+       try_upgrade hd/$asrc
+       mkdir -p hd/$asrc
+       cd hd/$asrc
+       rm -rf system*
+       ( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
                | progress_bar "Installing Android-x86" "Expect to write $size KB..."
+       result=$((`cat /tmp/result`*255))
 
-       chmod 644 /hd/$asrc/*
-       chown 0.0 /hd/$asrc/*
-       [ "$fs" != "vfat" ] && mkdir /hd/$asrc/data
+       if [ $result -eq 0 ]; then
+               for d in android mnt sfs ./$SRC; do
+                       [ -d $d ] && mv $d/* . && rmdir $d
+               done
+               chown 0.0 *
+               for f in *; do
+                       [ -d $f ] || chmod 644 $f
+               done
+
+               case "$fs" in
+                       vfat|fuseblk)
+                               create_data_img
+                               ;;
+                       *)
+                               mkdir -p data
+                               ;;
+               esac
+       fi
+
+       dialog --infobox "\n Syncing to disk..." 5 27
        sync
 
-       return $((`cat /tmp/result`*255))
+       return $result
 }
 
 install_hd()
 {
-       mdev -s
        select_dev || rebooting
        retval=1
        case "$choice" in
-               [sh]d*)
+               [shvm][dm]*)
                        install_to $choice
                        retval=$?
                        ;;
                Create*)
-                       cfdisk
+                       partition_drive
+                       retval=$?
                        ;;
                Detect*)
                        dialog --title " Detecting... " --nocancel --pause "" 8 41 1
@@ -198,7 +382,7 @@ do_install()
        until install_hd; do
                if [ $retval -eq 255 ]; then
                        dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
-                               --yesno '\n      Installation failed!' 7 35
+                               --yesno '\nInstallation failed! Please check if you have enough free disk space to install Android-x86.' 8 51
                        [ $? -eq 1 ] && rebooting
                fi
        done
@@ -211,14 +395,20 @@ do_install()
                Run*)
                        cd /android
                        umount system
-                       if mountpoint -q /sfs; then
-                               umount /sfs
+                       mountpoint -q /sfs && umount /sfs
+                       if [ -e /hd/$asrc/system.sfs ]; then
                                mount -o loop /hd/$asrc/system.sfs /sfs
                                mount -o loop /sfs/system.img system
-                       else
+                       elif [ -e /hd/$asrc/system.img ]; then
                                mount -o loop /hd/$asrc/system.img system
+                       else
+                               mount --bind /hd/$asrc/system system
+                       fi
+                       if [ -d /hd/$asrc/data ]; then
+                               mount --bind /hd/$asrc/data data
+                       elif [ -e /hd/$asrc/data.img ]; then
+                               mount -o loop /hd/$asrc/data.img data
                        fi
-                       [ -d /hd/$asrc/data ] && mount --bind /hd/$asrc/data data
                        ;;
                *)
                        rebooting