#!/bin/sh
# Copyright 1995, 1998 Patrick Volkerding, Moorhead, Minnesota USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

if [ ! "$UID" = "0" ]; then
    echo "You need to be root to run this script."
    exit 1
fi

# Make sure there's a proper temp directory:
TMP=/var/log/setup/tmp
# If the $TMP directory doesn't exist, create it:
if [ ! -d $TMP ]; then
  rm -rf $TMP # make sure it's not a symlink or something stupid
  mkdir -p $TMP
  chmod 700 $TMP # no need to leave it open
fi

ROOT_DEVICE="`mount | grep ' on / ' | cut -f 1 -d ' '`"

if mount | grep ' on / ' | grep umsdos 1> /dev/null 2> /dev/null ; then
 MOUNT="read-write"
else
 MOUNT="read-only"
fi

make_root_device() {
# Make a device:
makedev() {
  if [ ! -b $1 ]; then
    mknod $1 b $2 $3
    chown root.disk $1
    chmod 640 $1
  fi  
}

# Make ide device
# make ide major minor hd1 hd2 (2 base devs for major)
make_ide() {
  # Handle base devices:
  if [ "$2" = "0" ]; then
    makedev $TMP/lilo/dev/$3 $1 $2
    return 0
  elif [ "$2" = "64" ]; then
    makedev $TMP/lilo/dev/$4 $1 $2
    return 0
  fi
  # Must be a partition:
  if [ "`expr $2 / 64`" = "0" ]; then
    DEV=$3
    NUM=$2
  else
    DEV=$4
    NUM=`expr $2 - 64`
  fi
  makedev $TMP/lilo/dev/$DEV$NUM $1 $2
}

# Make SCSI device
make_scsi() {
  # find drive # 0 - 15
  DRV=`expr $1 / 16`
  NUM=`expr $1 % 16`
  if [ "$NUM" = "0" ]; then
    NUM=""
  fi
  if [ "$DRV" = "0" ]; then
    makedev $TMP/lilo/dev/sda$NUM 8 $1
  elif [ "$DRV" = "1" ]; then
    makedev $TMP/lilo/dev/sdb$NUM 8 $1
  elif [ "$DRV" = "2" ]; then
    makedev $TMP/lilo/dev/sdc$NUM 8 $1
  elif [ "$DRV" = "3" ]; then
    makedev $TMP/lilo/dev/sdd$NUM 8 $1
  elif [ "$DRV" = "4" ]; then
    makedev $TMP/lilo/dev/sde$NUM 8 $1
  elif [ "$DRV" = "5" ]; then
    makedev $TMP/lilo/dev/sdf$NUM 8 $1
  elif [ "$DRV" = "6" ]; then
    makedev $TMP/lilo/dev/sdg$NUM 8 $1
  elif [ "$DRV" = "7" ]; then
    makedev $TMP/lilo/dev/sdh$NUM 8 $1
  elif [ "$DRV" = "8" ]; then
    makedev $TMP/lilo/dev/sdi$NUM 8 $1
  elif [ "$DRV" = "9" ]; then
    makedev $TMP/lilo/dev/sdj$NUM 8 $1
  elif [ "$DRV" = "10" ]; then
    makedev $TMP/lilo/dev/sdk$NUM 8 $1
  elif [ "$DRV" = "11" ]; then
    makedev $TMP/lilo/dev/sdl$NUM 8 $1
  elif [ "$DRV" = "12" ]; then
    makedev $TMP/lilo/dev/sdm$NUM 8 $1
  elif [ "$DRV" = "13" ]; then
    makedev $TMP/lilo/dev/sdn$NUM 8 $1
  elif [ "$DRV" = "14" ]; then
    makedev $TMP/lilo/dev/sdo$NUM 8 $1
  elif [ "$DRV" = "15" ]; then
    makedev $TMP/lilo/dev/sdp$NUM 8 $1
  fi
}

if cat /proc/partitions | grep / 1> /dev/null 2> /dev/null ; then # new
  cat /proc/partitions | grep / | while read line ; do
    SMASHED_LINE=$line
    MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
    MINOR=`echo $SMASHED_LINE | cut -f 2 -d ' '`
    if [ "$MAJOR" = "3" ]; then
      make_ide $MAJOR $MINOR hda hdb
    elif [ "$MAJOR" = "8" ]; then
      make_scsi $MINOR
    elif [ "$MAJOR" = "22" ]; then
      make_ide $MAJOR $MINOR hdc hdd
    elif [ "$MAJOR" = "33" ]; then
      make_ide $MAJOR $MINOR hde hdf
    elif [ "$MAJOR" = "34" ]; then
      make_ide $MAJOR $MINOR hdg hdh
    elif [ "$MAJOR" = "56" ]; then
      make_ide $MAJOR $MINOR hdi hdj
    fi
  done
else # old format
  cat /proc/partitions | grep d | while read line ; do
    SMASHED_LINE=$line
    MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
    MINOR=`echo $SMASHED_LINE | cut -f 2 -d ' '`
    DEVNAME=`echo $SMASHED_LINE | cut -f 4 -d ' '`
    makedev $TMP/lilo/dev/$DEVNAME $MAJOR $MINOR
  done
fi
}

choose_kernel() {
while [ 0 ]; do # input loop
cat << EOF > $TMP/tmpmsg

Some possible paths to kernels are these: 

/vmlinuz
/boot/vmlinuz 
/usr/src/linux/arch/i386/boot/zImage
/usr/src/linux/arch/i386/boot/bzImage

Put the path to the kernel you want to use in the box below.

EOF

 dialog --title "CHOOSE KERNEL" --inputbox "`cat $TMP/tmpmsg`" \
 16 72 2> $TMP/return
 if [ ! $? = 0 ]; then
  exit
 fi

 KERNEL="`cat $TMP/return`"
    
 if [ ! -r "$KERNEL" ]; then  
    dialog --title "NOT FOUND!" --msgbox "$KERNEL" 5 60
    continue
 fi
break
done
}

while [ 0 ]; do # menu loop
 dialog --title "MAKE BOOTDISK FROM KERNEL" --menu "This menu allows you \
 to make a bootdisk from a compiled kernel.  There are two types of bootdisks \
 that you can make: a LILO bootdisk (which is more flexible) or a simple \
 bootdisk (which is just a kernel image written directly to disk). \
 Note that the simple bootdisk may no longer work for larger recent kernels, \
 and the LILO bootdisk is the recommended choice. \
 Which option would you like?" 16 74 4 \
"format" "format floppy disk in /dev/fd0 (1.68, 1.44, or 1.2 MB)" \
"lilo" "make lilo bootdisk (recommended)" \
"simple" "make simple vmlinuz > /dev/fd0 bootdisk (obsolete)" \
"exit" "exit this program" 2> $TMP/return
 if [ ! $? = 0 ]; then
  break;
 fi
 REPLY=`cat $TMP/return`
 rm -f $TMP/return
 if [ "$REPLY" = "format" ]; then
 dialog --title "SPECIFY FLOPPY SIZE" --menu \
 "What size should format should be used for /dev/fd0?" 10 60 3 \
  "1.68" "1.68 megabytes (format 1.44 floppy to 1.68)" \
  "1.44" "1.44 megabytes" \
  "1.2" "1.2 megabytes" 2> $TMP/return
 if [ ! $? = 0 ]; then
  continue
 fi
  if [ "`cat $TMP/return`" = "1.68" ]; then
   dialog --title "Formatting /dev/fd0u1680" --infobox \
   "Formatting /dev/fd0, 1.68 megabytes." 3 50 
   fdformat -n /dev/fd0u1680 1> /dev/null 2> /dev/null
  elif [ "`cat $TMP/return`" = "1.44" ]; then
   dialog --title "Formatting /dev/fd0u1440" --infobox \
   "Formatting /dev/fd0, 1.44 megabytes." 3 50 
   fdformat -n /dev/fd0u1440 1> /dev/null 2> /dev/null
  elif [ "`cat $TMP/return`" = "1.2" ]; then
   dialog --title "Formatting /dev/fd0h1200" --infobox \
   "Formatting /dev/fd0, 1.2 megabytes." 3 50
   fdformat -n /dev/fd0h1200 1> /dev/null 2> /dev/null
  fi
  rm -f $TMP/return
 elif [ "$REPLY" = "simple" ]; then # make simple bootdisk

choose_kernel

   kernel_size=`du -Lk $KERNEL | cut -f1`

   if [ "$kernel_size" -gt "1023" ]; then
cat << EOF > $TMP/tmpmsg

The kernel $KERNEL is $kernel_size K (which is
more than 1023 Kb in size), so it probably won't 
boot standalone on the floppy.  Use the 'lilo' 
method instead.
 
EOF
   dialog --title "KERNEL TOO BIG!" --msgbox "`cat $TMP/tmpmsg`" 10 60
continue
fi

  dialog --title "BOOT DISK CREATION" --yesno \
"\n\
Now put a formatted floppy in your boot drive. \n\
This will be made into your Linux boot disk. Use this to\n\
boot Linux until LILO has been configured to boot from\n\
the hard drive.\n\n\
Any data on the target disk will be destroyed.\n\n\
YES creates the disk, NO aborts.\n" 14 62
  if [ $? = 0 ]; then
   dialog --title "CREATING DISK" --infobox "Creating boot disk from $KERNEL..." 5 72
   dd if=$KERNEL of=/dev/fd0 2> /dev/null
   rdev /dev/fd0 $ROOT_DEVICE
   rdev -v /dev/fd0 -1
   if [ "$MOUNT" = "read-only" ]; then
    rdev -R /dev/fd0 1
   else
    rdev -R /dev/fd0 0
   fi
  fi
 elif [ "$REPLY" = "lilo" ]; then # make lilo bootdisk

if [ ! -x "`type -path lilo`" ]; then
cat << EOF > $TMP/tmpmsg

You don't have 'lilo' installed on the system.
I guess you didn't install the 'lilo.tgz' package.
 
EOF
   dialog --title "LILO NOT FOUND" --msgbox "`cat $TMP/tmpmsg`" 8 60
continue
fi

choose_kernel

  dialog --title "CREATING LILO BOOTDISK IN /dev/fd0" --yesno "Now put a \
  formatted floppy in your boot drive.  This will be made into a LILO \
  bootdisk that you can use to start your Linux system.  Any data on the \
  target disk will be destroyed.  YES creates the disk, NO aborts." 8 62
  if [ $? = 0 ]; then # make the disk
 dialog --title "SPECIFY FLOPPY SIZE" --menu \
 "What size is your /dev/fd0 (a:) drive?" 10 64 3 \
   "1.68" "1.68 megabytes (1.44 floppy formatted to 1.68)" \
   "1.44" "1.44 megabytes" \
   "1.2" "1.2 megabytes" 2> $TMP/return
   if [ "`cat $TMP/return`" = "1.68" ]; then
    DEV=/dev/fd0u1680
    mknod_fd="-m 0640 $TMP/lilo$DEV b 2 44"
   elif [ "`cat $TMP/return`" = "1.44" ]; then
    DEV=/dev/fd0u1440
    mknod_fd="-m 0640 $TMP/lilo$DEV b 2 28"
   else
    DEV=/dev/fd0h1200
    mknod_fd="-m 0640 $TMP/lilo$DEV b 2 8"
   fi

   dialog --infobox "Creating LILO bootdisk from $KERNEL for $ROOT_DEVICE..." 4 60
   mke2fs -q -m 0 -i 4096 $DEV 1> /dev/null 2> /dev/null || exit 1
   if [ ! -d $TMP/lilo ]; then
    mkdir -p $TMP/lilo
   fi
   mount -t ext2 $DEV $TMP/lilo 1> /dev/null || exit 1
   rmdir $TMP/lilo/lost+found
   cp $KERNEL $TMP/lilo/vmlinuz || exit 1
   mkdir $TMP/lilo/dev
   make_root_device
   mknod -m 0640 $TMP/lilo/dev/fd0 b 2 0
   mknod -m 0640 $TMP/lilo/dev/fd1 b 2 1
   mknod $mknod_fd
   mknod -m 0666 $TMP/lilo/dev/null c 1 3
   mkdir $TMP/lilo/etc
   cat << EOF > $TMP/lilo/etc/lilo.conf
boot = $DEV
message=/boot/message
backup=/dev/null
prompt
image = /vmlinuz
        label = mount
        ramdisk = 0
        root = $ROOT_DEVICE
        vga = normal
        $MOUNT
EOF
   mkdir $TMP/lilo/boot
   cp -a /boot/chain.b $TMP/lilo/boot
   if [ -f /boot/boot-text.b ]; then
   cp -a /boot/boot-text.b $TMP/lilo/boot/boot.b
   else
   cp -a /boot/boot.b $TMP/lilo/boot
   fi   
 
   cat << EOF > $TMP/lilo/boot/message

Welcome to the Slackware Linux custom LILO bootdisk!  

By default, this disk boots a root Linux partition on $ROOT_DEVICE when
you hit ENTER.  If you'd like to boot some other partition, use a command
like this on the LILO prompt below:

    mount root=/dev/sda1 ro

Where "/dev/sda1" is the partition you want to boot, and "ro" specifies that
the partition should be initially mounted as read-only.  If you which to mount
the partition read-write, use "rw" instead.  You may also add any other kernel
parameters you might need depending on your hardware, and which drivers are
included in your kernel.

EOF
   lilo -r $TMP/lilo > /dev/null
   umount $TMP/lilo
   rm -rf $TMP/lilo
  fi
 elif [ "$REPLY" = "exit" ]; then
  break;
 fi
done
