Showing posts with label bootloader. Show all posts
Showing posts with label bootloader. Show all posts

Tuesday, October 9, 2012

isolinux.cfg for a live CD

isolinux is a bootloader included in the syslinux package and is used to boot a CD or DVD. isolinux is one of several popular bootloaders used to create a bootable CD for open-source operating systems, including Linux and FreeDOS. Another bootloader is GRUB, which I rarely use except for booting Windows 7 with BIOS emulation.



To create a bootable CD with isolinux, just copy the file isolinux.bin to a folder that will hold CD contents for ISO mastering. On Debian or Ubuntu, the file is located at the /usr/lib/syslinux folder.



ISOFS contents

A file named isolinux.cfg needs to be created in order to provide a menu or boot entries. A sample isolinux.cfg is shown below. It makes use of vesamenu.c32 which helps display a nice graphical menu with a background picture. Vesamenu.c32 is also located in /usr/lib/syslinux and needs to be copied too.





# isolinux.cfg
# Configuration for ISOLINUX

# Wait for 60 seconds before booting up the default entry.
TIMEOUT 600
F1 help.txt #ff702640

UI vesamenu.c32
MENU RESOLUTION 640 480
MENU BACKGROUND back642.jpg
MENU COLOR border 30;44 #40dfdfdf #00000000 std
MENU COLOR title 1;36;43 #e0fdff49 #804206ef none
MENU COLOR sel 7;37;40 #e0000000 #40ffffff all
MENU COLOR hotsel 1;7;37;40 #e0af0000 #40ffffff all
MENU COLOR disabled 1;30;44 #ffe4de6f #00000000 std
MENU COLOR scrollbar 30;44 #40000000 #e0af0000 std
MENU TITLE Boot Menu
MENU CLEAR

MENU ROWS 14
MENU TABMSGROW 20
MENU CMDLINEROW 20
MENU TIMEOUTROW 22
MENU HELPMSGROW 24
MENU VSHIFT 1

# The default is to boot the first hard drive.
DEFAULT hd0
ONERROR hd1

MENU BEGIN livecd_menu

MENU TITLE Linux Live CD Menu

LABEL livecd_heading
MENU LABEL Boot Linux live CD
MENU DISABLE

LABEL livecd_640x480
MENU LABEL 640x480 screen
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND root=/dev/sr0 edd=off vmode=640x480

LABEL livecd_800x600
MENU LABEL 800x600 screen
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND root=/dev/sr0 edd=off vmode=800x600

LABEL livecd_1024x768
MENU LABEL 1024x768 screen
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND root=/dev/sr0 edd=off vmode=1024x768

MENU SEPARATOR

LABEL livecd_console_heading
MENU LABEL Boot Linux live CD into text console
MENU DISABLE

LABEL livecd_console
MENU LABEL text-mode console
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND root=/dev/sr0 nox

LABEL livecd_console_old
MENU LABEL text-mode console for an old computer
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND root=/dev/sr0 acpi=off irqpoll nox

MENU SEPARATOR

LABEL exit0
MENU LABEL Return to the main menu.
MENU EXIT

MENU END

MENU BEGIN cdram_menu

MENU TITLE Linux on RAMdisk Menu

LABEL cdram_heading
MENU LABEL Boot Linux on RAMdisk
MENU DISABLE

LABEL cdram_640x480
MENU LABEL 640x480 screen
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND boot=cdrom root=/dev/ram ramdisk_size=573440 edd=off vmode=640x480

LABEL cdram_800x600
MENU LABEL 800x600 screen
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND boot=cdrom root=/dev/ram ramdisk_size=573440 edd=off vmode=800x600

LABEL cdram_1024x768
MENU LABEL 1024x768 screen
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND boot=cdrom root=/dev/ram ramdisk_size=573440 edd=off vmode=1024x768

MENU SEPARATOR

LABEL cdram_console_heading
MENU LABEL Boot Linux RAMdisk into text console
MENU DISABLE

LABEL cdram_console
MENU LABEL text-mode console
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND boot=cdrom root=/dev/ram ramdisk_size=573440 nox

LABEL cdram_console_old
MENU LABEL text-mode console for an old computer
MENU INDENT 1
KERNEL 360.lnx
INITRD initram.lzm
APPEND boot=cdrom root=/dev/ram ramdisk_size=573440 acpi=off irqpoll nox

MENU SEPARATOR

LABEL exit1
MENU LABEL Return to the main menu.
MENU EXIT

MENU END

LABEL hd0
MENU LABEL First Hard Drive
LOCALBOOT 0x80

LABEL hd1
MENU LABEL Second Hard Drive
KERNEL chain.c32
APPEND hd1 swap

MENU BEGIN hd0_menu

MENU TITLE Partition in First Hard Drive

LABEL hd0_part1
MENU LABEL 1st partition
KERNEL chain.c32
APPEND hd0 1

LABEL hd0_part2
MENU LABEL 2nd partition
KERNEL chain.c32
APPEND hd0 2

LABEL hd0_part3
MENU LABEL 3rd partition
KERNEL chain.c32
APPEND hd0 3

LABEL hd0_part4
MENU LABEL 4th partition
KERNEL chain.c32
APPEND hd0 4

LABEL hd0_part5
MENU LABEL 5th partition
KERNEL chain.c32
APPEND hd0 5

LABEL hd0_part6
MENU LABEL 6th partition
KERNEL chain.c32
APPEND hd0 6

LABEL hd0_part7
MENU LABEL 7th partition
KERNEL chain.c32
APPEND hd0 7

LABEL hd0_part8
MENU LABEL 8th partition
KERNEL chain.c32
APPEND hd0 8

LABEL hd0_part9
MENU LABEL 9th partition
KERNEL chain.c32
APPEND hd0 9

LABEL hd0_part10
MENU LABEL 10th partition
KERNEL chain.c32
APPEND hd0 10

MENU SEPARATOR

LABEL exit3
MENU LABEL Return to the main menu
MENU EXIT

MENU END

LABEL fd0
MENU LABEL Floppy Drive
LOCALBOOT 0x00

LABEL next
MENU LABEL Next in boot sequence
LOCALBOOT -1

LABEL memtest
MENU LABEL Test your computer memory
LINUX memtest.bin
TEXT HELP
Run Memtest86 to test your computer memory
ENDTEXT

LABEL help
MENU LABEL Help
MENU HELP help.txt #ff702640


isolinux boot menu

The example above is one that I use for my live CD, so some settings are specific to my situation. Thus, KERNEL and APPEND settings may have to be adapted for other users. However, all the keywords shown above is standard and acceptable.


Saturday, October 6, 2012

My Live CD based on Debian Linux Sid

I created a Live CD from my Debian Linux installation. It is based on the unstable version (Sid). However, the live CD still lacks the capability to automatically configure X-Windows and the network, and uses IceWM for minimalist desktop. It can be downloaded from Google Drive. This Live CD has many uses, such as:



  • To troubleshoot and fix computer problems
  • To create backups and restore them
  • To partition a hard drive before installing Windows or Linux
  • To set up GRUB bootloader or enter boot commands
  • To install Debian, Ubuntu or Fedora
  • To access a Windows partition when it is unbootable
  • To survey and diagnose wireless and wired networks
  • To enjoy the Internet with the Web, IM, IRC and VoIP
  • To run a simple FTP or HTTP server
  • To participate in peer-to-peer networks
  • To do basic word processing and spreadsheet
  • To listen to music or watch movies
  • To rip CD's and DVD's


livecd-001

The snapshot above shows idesk icons on the desktop. Idesk is a neat application that displays icons on the desktop for you to quickly launch. Currently, I use IceWM instead of GNOME or KDE desktop to save space on the CD. So idesk is used to provide desktop icons for IceWM.





livecd-002

The snapshot above shows mlterm running. mlterm is my favorite terminal application. It allows me to use many languages.



livecd-003

Gparted is a cool application to have on a live CD. It lets you divide the hard drive into many partitions so you can run multiple operating systems.



livecd-004

IceWeasel is rebranded Firefox from Debian. Sorry, Chrome is not included.



livecd-005

LibreOffice is a fork of OpenOffice.org suite. I use it to create documents or read Microsoft Office documents.



Specifying the locale at boot time



To see your own language with the live CD, select an entry from the menu, click Tab and append LANG= option to the command line. For example, if you speak French, append LANG=fr_FR.UTF-8.

Tuesday, August 28, 2012

Manually Creating initrd / initramfs to Boot Linux

Initrd and initramfs files are used together with a Linux kernel and a bootloader (such as GRUB and syslinux) to start up Linux and boot it in many different ways. Many Linux distributions provide prepackaged kernels and convenient tools for creating initramfs, but I like to build my own kernel and initramfs because it allows me to customize the boot process. For instance, one can choose to boot from the local disk or network-mounted disk with the help of initramfs and some user-defined boot parameters. Here's a list of things that I'd like to achieve with my custom initramfs.




  1. Boot from a live CD
  2. Boot from a read-only filesystem image, compressed with SquashFS
  3. Copy the filesystem image to a RAM disk and run Linux entirely on memory
  4. Boot Linux from a USB flash drive or a Firewire disk
  5. Boot Linux from a local disk partition
  6. Boot Linux from a network drive
  7. Run a rescue shell without booting Linux
  8. Start a kdrive X-server and run gparted, partclone or partimage


In this post, I show how I normally create initrd / initramfs files. For this tutorial, the following Debian/Ubuntu packages are needed:




  • busybox

    provides small essential utilities for booting and rescue shell.
  • cpio

    is used to create the actual initramfs format.
  • dash

    provides a minimalist shell for running the init script and the rescue shell.
  • e2fsprogs, jfsutils, etc.

    provides fsck to check Linux filesystems and replay any stale journal
  • lzma or xz-utils

    compresses the initramfs
  • module-init-tools

    is used to load kernel modules necessary to activate hardware and mount the filesystem
  • pciutils

    is used to detect PCI hardware
  • unionfs-fuse

    makes it possible to use read-only Linux systems, such as live CD and filesystem images
  • unzip

    is used to apply customized settings and changes to the unionfs boot mode
  • v86d

    is used to set the screen resolution for the framebuffer screen


First, create a text file with a list of files to put in initramfs. The following is an example of such file:



bin/busybox
bin/dash
bin/mount
etc/filesystems
etc/fuse.conf
etc/group
etc/modprobe.d
lib/ld-linux.so.2
lib/libacl.so.1
lib/libattr.so.1
lib/libblkid.so.1
lib/libbz2.so.1.0
lib/libcom_err.so.2
lib/libc.so.6
lib/libdl.so.2
lib/libe2p.so.2
lib/libext2fs.so.2
lib/libfuse.so.2
lib/libkmod.so.2
lib/libmount.so.1
lib/libm.so.6
lib/libpci.so.3
lib/libpthread.so.0
lib/libresolv.so.2
lib/librt.so.1
lib/libselinux.so.1
lib/libsepol.so.1
lib/libuuid.so.1
lib/libx86.so.1
lib/libz.so.1
lib/modules/3.5.3/kernel/drivers/ata
lib/modules/3.5.3/kernel/drivers/block/loop.ko
lib/modules/3.5.3/kernel/drivers/cdrom/cdrom.ko
lib/modules/3.5.3/kernel/drivers/connector/cn.ko
lib/modules/3.5.3/kernel/drivers/firewire/firewire-core.ko
lib/modules/3.5.3/kernel/drivers/firewire/firewire-ohci.ko
lib/modules/3.5.3/kernel/drivers/firewire/firewire-sbp2.ko
lib/modules/3.5.3/kernel/drivers/i2c/algos/i2c-algo-bit.ko
lib/modules/3.5.3/kernel/drivers/scsi/sd_mod.ko
lib/modules/3.5.3/kernel/drivers/scsi/sr_mod.ko
lib/modules/3.5.3/kernel/drivers/usb/host/ehci-hcd.ko
lib/modules/3.5.3/kernel/drivers/usb/host/ohci-hcd.ko
lib/modules/3.5.3/kernel/drivers/usb/host/uhci-hcd.ko
lib/modules/3.5.3/kernel/drivers/usb/storage/usb-storage.ko
lib/modules/3.5.3/kernel/drivers/video
lib/modules/3.5.3/kernel/fs/ext3/ext3.ko
lib/modules/3.5.3/kernel/fs/ext4/ext4.ko
lib/modules/3.5.3/kernel/fs/fat/fat.ko
lib/modules/3.5.3/kernel/fs/fat/vfat.ko
lib/modules/3.5.3/kernel/fs/fuse/fuse.ko
lib/modules/3.5.3/kernel/fs/isofs/isofs.ko
lib/modules/3.5.3/kernel/fs/jbd2/jbd2.ko
lib/modules/3.5.3/kernel/fs/jbd/jbd.ko
lib/modules/3.5.3/kernel/fs/jfs/jfs.ko
lib/modules/3.5.3/kernel/fs/nls/nls_cp437.ko
lib/modules/3.5.3/kernel/fs/nls/nls_iso8859-1.ko
lib/modules/3.5.3/kernel/fs/nls/nls_utf8.ko
lib/modules/3.5.3/kernel/fs/reiserfs/reiserfs.ko
lib/modules/3.5.3/kernel/fs/squashfs/squashfs.ko
lib/modules/3.5.3/kernel/fs/xfs/xfs.ko
lib/modules/3.5.3/kernel/lib/crc16.ko
lib/modules/3.5.3/kernel/lib/crc-t10dif.ko
lib/modules/3.5.3/modules.dep
lib/modules/3.5.3/modules.dep.bin
lib/modules/3.5.3/modules.pcimap
sbin/blkid
sbin/e2fsck
sbin/jfs_fsck
sbin/modprobe
sbin/v86d
usr/bin/pcimodules
usr/bin/unionfs-fuse
usr/bin/unzip


Then, create an empty directory, for example, /tmp/initrd and copy the files listed in the above-mentioned text file (called rd354.txt) to the new directory.



mkdir /tmp/initrd
cd /
tar cvhf - -T rd354.txt | (cd /tmp/initrd; tar xf -)


Then, create the necessary directory structure under /tmp/initrd:



cd /tmp/initrd
mkdir -p dev media mnt proc root sys tmp


Since I chose to use busybox, I need to create symbolic links to busybox in /bin. Busybox provides incomplete functionality for modprobe, mount, sh and unzip, so I removed their symbolic links.



cd /tmp/initrd/bin
for f in $(./busybox --list); do [ -e $f ] || ln -s busybox $f ; done
rm modprobe unzip


Then, create essential device nodes in /tmp/initrd/dev:



cd /tmp/initrd/dev
MAKEDEV std fb0 fd0 sda sdb scd
mknod console c 5 1
mknod fuse c 10 229
chmod 666 fuse


Now, create an init script. The contents of this init script is crucial for customization of the boot process. The following is the script I use. This script is capable of booting a live CD and running Linux within memory in addition to booting Linux from hard drives and USB flash.



#!/bin/dash

# Define a function to parse kernel command line options.
get_opt() {
echo $@ | cut -d "=" -f 2
}

# Define a function to load drivers.
loadmod() {
for i in $@ ; do
for j in $(grep $i /tmp/pcimodules.txt); do
modprobe $j
done
done
}

# Define a function to guess the partition type.
gpart() {
for i in $(blkid | grep $1); do
case $i in
*\=*)
eval $i
;;
*)
true
;;
esac
done
}

# Define a function for mounting the root partition.
mountr() {
if [ $uuid ]; then
if [ $# = 2 ]; then
mount -r -U $uuid $2
elif [ $# = 1 ]; then
mount -r -U $uuid $1
else mount -r -U $uuid /mnt
fi
elif [ $label ]; then
if [ $# = 2 ]; then
mount -r -L $label $2
elif [ $# = 1 ]; then
mount -r -L $label $1
else mount -r -L $label /mnt
fi
else
gpart $1
case $TYPE in
ext*)
e2fsck -p $1
[ $# = 2 ] && mount $1 $2 || mount $1 /mnt
;;
jfs)
jfs_fsck $1
if [ $# = 2 ]; then
mount -t jfs -o ro,iocharset=utf8 $1 $2
else mount -t jfs -o ro,iocharset=utf8 $1 /mnt
fi
;;
vfat)
if [ $# = 2 ]; then
mount -t vfat -o ro,gid=100,dmask=2,fmask=113 $1 $2
else mount -t vfat -o ro,gid=100,dmask=2,fmask=113 $1 /mnt
fi
;;
*)
[ $# = 2 ] && mount -r $1 $2 || mount -r $1 /mnt
;;
esac
fi
}

# Create a union filesystem
union() {
mount -t tmpfs none /opt/tmp
modinfo unionfs > /dev/null 2>&1 &&
mount -t unionfs -o dirs=/opt/tmp=rw:/opt=ro none /mnt ||
( mkdir /opt/tmp/.change
modprobe fuse
unionfs-fuse -o allow_other,use_ino,suid,dev,nonempty,kernel_cache \
-o cow,chroot=/opt,max_files=32768 /tmp/.change=RW:/=RO /mnt )
}

# Mount proc and sysfs.
mount -t proc none /proc
mount -t sysfs none /sys

# Find the available PCI hardware
mount -t tmpfs none /tmp
pcimodules > /tmp/pcimodules.txt

# Populate /dev (Needs kernel >= 2.6.32)
mount -t devtmpfs none /dev
mkdir -m 755 /dev/pts
mount -t devpts -o gid=5,mode=620 none /dev/pts

# Set default values
boot=ata
root=/dev/sda6

# Find the root=, label=, uuid= and boot= values on kernel command line.
for i in $(cat /proc/cmdline); do
case $i in
root\=*)
root=$(get_opt $i)
case $root in
/dev/cdr* | /dev/dvd* | /dev/sr* | /dev/scd*)
boot=cdrom
;;
0x200)
root=/dev/fd0
;;
esac
;;
label\=* | uuid\=* | boot\=* | vmode\=* )
eval $i
;;
single)
RUNLEVEL=single
;;
nox)
RUNLEVEL=2
;;
esac
done

# Activate framebuffer display devices.
if [ $vmode ]; then
if [ $boot = cdrom ]; then
modprobe uvesafb scroll=ywrap mode_option=$vmode-16
else for i in $(grep fb /tmp/pcimodules.txt); do
case $i in
atyfb)
modprobe $i mode=$vmode-16
;;
nvidiafb | rivafb)
modprobe nvidiafb mode_option=$vmode bpp=16 hwcur=1
;;
radeonfb | savagefb)
modprobe $i mode_option=$vmode-16
;;
sisfb)
modprobe $i mode=$vmodex16 mem=12288 font=SUN12x22
;;
viafb | vt8623fb)
modprobe viafb viafb_mode=$vmode viafb_bpp=16
;;
*)
modprobe $i
;;
esac
done
if grep -q i915 /tmp/pcimodules.txt; then true
else [ -c /dev/fb0 ] || modprobe uvesafb scroll=ywrap mode_option=$vmode-16
fi
fi
fi

case $boot in
cdrom)
# Boot Linux from a live CD.
loadmod ata_ ahci pdc_adma ^.hci-hcd
modprobe usb-storage &&
modprobe sr_mod &&
sleep 7
modprobe isofs
mount -t iso9660 /dev/sr0 /media
[ -d /media/isolinux -o -d /media/boot/isolinux ] ||
mount -t iso9660 /dev/sr1 /media
if [ -f /media/*.[Ss][Qq]* ]; then
SQF=$(ls -t /media/*.[Ss][Qq]* | head -n 1)
if [ $root = /dev/ram ]; then
echo "Please wait until the RAM disk is ready."
dd if=$SQF of=/dev/ram1 bs=2048 &&
mount -t squashfs /dev/ram1 /opt
else modprobe loop
mount -t squashfs -o loop $SQF /opt
fi
else
mount --move /media /opt
fi
union
;;
loop)
# Boot Linux from an image file.
loadmod ata_ ahci pdc_adma ^.hci-hcd
modprobe usb-storage &&
modprobe sd_mod &&
sleep 7
mountr $root /media
modprobe loop
if [ -f /media/*.[Ss][Qq]* ]; then
SQF=$(ls -t /media/*.[Ss][Qq]* | head -n 1)
mount -t squashfs -o loop $SQF /opt
elif [ -f /media/*.[Ii][Ss][Oo] ]; then
ISO=$(ls -t /media/*.[Ii][Ss][Oo] | head -n 1)
modprobe isofs
mount -t iso9660 -o loop $ISO /opt
fi
union
;;
ram)
# Boot Linux from ramdisk.
loadmod ata_ ahci pdc_adma ^.hci-hcd
modprobe usb-storage &&
modprobe sd_mod &&
sleep 7
mountr $root /media
echo "Please wait until the RAM disk is ready."
if [ -f /media/*.[Ss][Qq]* ]; then
SQF=$(ls -t /media/*.[Ss][Qq]* | head -n 1)
dd if=$SQF of=/dev/ram1 &&
mount -t squashfs /dev/ram1 /opt
elif [ -f /media/*.[Ii][Ss][Oo] ]; then
ISO=$(ls -t /media/*.[Ii][Ss][Oo] | head -n 1)
dd if=$ISO of=/dev/ram1 bs=2048 &&
modprobe isofs
mount -t iso9660 /dev/ram1 /opt
fi
union
;;
usb*)
# Boot Linux from a USB drive.
loadmod ^.hci-hcd
modprobe usb-storage &&
modprobe sd_mod &&
sleep 7
mountr $root
;;
ata*)
loadmod ata_ ahci pdc_adma &&
modprobe sd_mod &&
mountr $root
;;
esac

# Make sure that init exists and is executable.
if [ -x /mnt/sbin/init ]; then
mount --move /dev /mnt/dev
mount --move /proc /mnt/proc
mount --move /sys /mnt/sys
umount /tmp

# Start init from the root filesystem.
cd /mnt
[ -f /media/updates.zip ] && unzip -o /media/updates.zip
case $boot in
cdrom)
[ $root = /dev/ram ] && umount /media
[ $RUNLEVEL ] || RUNLEVEL=3
;;
loop | ram)
umount /media
[ $RUNLEVEL ] || RUNLEVEL=3
;;
*)
[ $RUNLEVEL ] || RUNLEVEL=5
;;
esac
[ -d initrd ] && pivot_root . initrd
exec chroot . /sbin/init $RUNLEVEL
fi

# Start a shell as a last resort.
echo "Error booting from the root filesystem. Starting a shell."
exec /bin/dash


Copy the init script to the initrd folder (Assuming its filename is init.sh).



cp init.sh /tmp/initrd/init
cp init.sh /tmp/initrd/etc
chmod 775 /tmp/initrd/init


Now, use cpio and lzma to create an initramfs file. This assumes that the kernel was compiled with CONFIG_RD_LZMA option enabled.



cd /tmp/initrd
find . | cpio -H newc -o | lzma -c > ../initram.lzm


A file named initram.lzm will be created. If you want to create an old-style initrd file instead, use the mkcramfs command. This assumes your kernel has built-in cramfs support (CONFIG_CRAMFS):



mkcramfs /tmp/initrd /boot/initrd.bin


Copy the kernel and the initramfs file to your boot directory (whereever it is). I use SYSLINUX which I installed at /dev/sda1 partition. Finally, edit the boot configuration file. The following is the contents of a sample syslinux.cfg:



LABEL debian
KERNEL 314.lnx
INITRD /initrd/initram.lzm
APPEND root=/dev/sda7

LABEL sid
KERNEL 314.lnx
INITRD /initrd/initram.lzm
APPEND boot=usb label=SID edd=off vmode=800x600

LABEL bfile
KERNEL 314.lnx
INITRD /initrd/initram.lzm
APPEND boot=loop root=/dev/sda1

LABEL ram
KERNEL 314.lnx
INITRD /initrd/initram.lzm
APPEND boot=ram ramdisk_size=524288 root=/dev/sda1

Friday, October 2, 2009

To Compile GRUB with GFXBOOT and SLIC patch

I use GRUB 0.97 to boot multiple operating systems on my computer. While I was searching Yahoo! China, I found a patch called loadslic.patch. I wanted to use MinGW to compile GRUB with GFXBOOT and SLIC patch. But I received an error saying “GRUB requires a working absolute objcopy; upgrade your binutils.” So I switched to Linux and compiled GRUB. Below I show how I did it.




  1. I prepared my compiler environment by installing GCC 3.4. Then I downloaded the following files:





  2. Unpack the GRUB 0.97 source.


    tar xzvf grub-0.97.tar.gz
    cd grub-0.97


  3. Apply various patches.


    patch -p1 -l < ../040_all_grub-0.96-nxstack.patch
    patch -p1 -l < ../05-grub-0.97-initrdaddr.diff
    patch -p1 -l < ../i2o.patch
    patch -p1 -l < ../intelmac.patch
    patch -p1 -l < ../more-raid.patch
    patch -p1 -l < ../special-devices.patch
    patch -p1 -l < ../grub-0.97-pure64-1.patch


  4. Apply the GFXBOOT patch.


    cd ..
    tar xjvf gfxboot-4.1.19.tar.bz2
    cd ../grub-0.97
    patch -p0 -l < ../gfxboot-4.1.19/patches/grub-0.97.diff



  5. Apply the following patch which adds a feature to load an OEM SLIC table for Windows activation. I found this patch through Yahoo! China. Save it as loadslic.patch.


    --- grub-0.97/stage2/builtins.c 2005-02-16 05:58:23.000000000 +0800
    +++ grub-0.97.new/stage2/builtins.c 2009-08-11 22:37:26.000000000 +0800
    @@ -48,6 +48,7 @@
    #ifdef USE_MD5_PASSWORDS
    # include <md5.h>
    #endif
    +#include <defs.h>

    /* The type of kernel loaded. */
    kernel_t kernel_type;
    @@ -397,6 +398,180 @@
    "Print the contents of the file FILE."
    };

    +
    +/*
    + * Add by yangrz, 2009-08-11
    + */
    +#define SLIC_LENGTH 0x176
    +
    +typedef struct acpi_rsdp {
    + char signature[8]; /* RSDP signature "RSD PTR" */
    + u_char checksum; /* checksum of the first 20 bytes */
    + char oem_id[6]; /* OEM ID, "LXBIOS" */
    + u_char revision; /* 0 for APCI 1.0, 2 for ACPI 2.0 */
    + u_int rsdt_address; /* physical address of RSDT */
    + u_int length; /* total length of RSDP (including extended part) */
    + unsigned long long xsdt_address; /* physical address of XSDT */
    + u_char ext_checksum; /* chechsum of whole table */
    + u_char reserved[3];
    +} __attribute__((packed)) acpi_rsdp_t;
    +
    +typedef struct acpi_table_header /* ACPI common table header */
    +{
    + char signature [4]; /* ACPI signature (4 ASCII characters) */\
    + u_int length; /* Length of table, in bytes, including header */\
    + u_char revision; /* ACPI Specification minor version # */\
    + u_char checksum; /* To make sum of entire table == 0 */\
    + char oem_id [6]; /* OEM identification */\
    + char oem_table_id [8]; /* OEM table identification */\
    + u_int oem_revision; /* OEM revision number */\
    + char asl_compiler_id [4]; /* ASL compiler vendor ID */\
    + u_int asl_compiler_revision; /* ASL compiler revision number */
    +} __attribute__ ((packed)) acpi_header_t;
    +
    +/* RSDT */
    +typedef struct acpi_rsdt {
    + struct acpi_table_header header;
    + /* MCONFIG, HPET, FADT, SRAT, SLIT, MADT(APIC), SSDT, SSDTX, and SSDT for CPU pstate*/
    + u_int entry[1];
    +} __attribute__ ((packed)) acpi_rsdt_t;
    +
    +static u_char
    +acpi_checksum(u_char *table, u_int length)
    +{
    + u_char ret=0;
    + while (length--)
    + {
    + ret += *table;
    + table++;
    + }
    + return -ret;
    +}
    +
    +static acpi_rsdp_t *
    +acpi_find_rsdp(void)
    +{
    + acpi_rsdp_t *rsdp;
    +
    + //TODO: also search EBDA
    + for ( rsdp = (acpi_rsdp_t *)(RAW_ADDR(0xe0000));
    + rsdp < (acpi_rsdp_t *)(RAW_ADDR(0xfffff - 0x16));
    + rsdp = (acpi_rsdp_t *)((unsigned char *)rsdp + 16) )
    + {
    + if ( memcmp(rsdp->signature, "RSD PTR ", 8) == 0
    + && acpi_checksum((u_char *)rsdp, rsdp->length) == 0 )
    + {
    + return rsdp;
    + }
    + }
    +
    + return NULL;
    +}
    +
    +static acpi_header_t *
    +acpi_find_slic(acpi_rsdp_t *rsdp)
    +{
    + int i;
    + acpi_rsdt_t *rsdt;
    + acpi_header_t *slic;
    +
    + rsdt = (acpi_rsdt_t *)(rsdp->rsdt_address);
    + for ( i = 0;
    + i < (rsdt->header.length - sizeof(acpi_header_t))/4;
    + i++)
    + {
    + slic = (acpi_header_t *)(RAW_ADDR(rsdt->entry[i]));
    + if ( slic != NULL && memcmp(slic->signature, "SLIC", 4) == 0 )
    + {
    + return slic;
    + }
    + }
    +
    + return NULL;
    +}
    +
    +/*
    + * return value : success 0
    + * fail 1
    + */
    +static int
    +acpi_copy_memory(char *target, char *source, int len)
    +{
    + int i;
    +
    + for ( i = 0; i < len; i++ )
    + {
    + target[i] = source[i];
    + }
    + /* check data */
    + if ( memcmp(target, source, len) != 0 )
    + {
    + printf("modify memory fail: maybe address 0x%x write protect\n",
    + target);
    + return 1;
    + }
    + return 0;
    +}
    +
    +static int
    +acpi_add_slic(char *arg, int flags)
    +{
    + char *file = arg;
    + acpi_rsdp_t *rsdp;
    + acpi_header_t *slic;
    + char buf[SLIC_LENGTH];
    +
    + rsdp = acpi_find_rsdp();
    + if ( rsdp == NULL )
    + {
    + grub_printf("can not find rsdp\n");
    + return 1;
    + }
    + slic = acpi_find_slic(rsdp);
    + if ( slic == NULL )
    + {
    + grub_printf("can not find slic\n");
    + return 1;
    + }
    +
    + if ( slic->length != SLIC_LENGTH )
    + {
    + return 1;
    + }
    +
    + if ( !grub_open(file) )
    + {
    + printf("grub open %s fail\n", file);
    + return 1;
    + }
    + if ( grub_read (buf, SLIC_LENGTH) != SLIC_LENGTH )
    + {
    + printf("grub read fail\n");
    + grub_close();
    + return 1;
    + }
    + grub_close();
    +
    + if ( acpi_copy_memory((char *)slic, buf, SLIC_LENGTH) )
    + {
    + errnum = ERR_WONT_FIT;
    + return 1;
    + }
    +
    + errnum = ERR_NONE;
    + return 0;
    +}
    +
    +static struct builtin builtin_load_slic =
    +{
    + "loadslic",
    + acpi_add_slic,
    + BUILTIN_CMDLINE | BUILTIN_HELP_LIST,
    + "loadslic FILE",
    + "load acpi slic form file"
    +};
    +/* end yangrz add */
    +

    /* chainloader */
    static int
    @@ -4835,6 +5010,8 @@
    &builtin_install,
    &builtin_ioprobe,
    &builtin_kernel,
    + /* Add by yangrz, 2009-08-08 */
    + &builtin_load_slic,
    &builtin_lock,
    &builtin_makeactive,
    &builtin_map,

    Apply this patch as follows:


    patch -p1 -l < ../loadslic.patch


  6. Compile GRUB.


    ./configure --prefix=/usr
    make
    make install

    The following files are installed:


    /usr/bin/mbchk
    /usr/info/multiboot.info
    /usr/lib/grub/i386-pc
    /usr/man/man1/mbchk.1
    /usr/man/man8/grub.8
    /usr/man/man8/grub-install.8
    /usr/man/man8/grub-md5-crypt.8
    /usr/man/man8/grub-terminfo.8
    /usr/sbin/grub
    /usr/sbin/grub-install
    /usr/sbin/grub-md5-crypt
    /usr/sbin/grub-set-default
    /usr/sbin/grub-terminfo

    To get my GRUB binaries, read this post.





Using GRUB to load SLIC


Note that the SLIC patch for GRUB only works on computers with BIOS that actually contain a SLIC table. To load SLIC with the patched GRUB, use the loadslic command at the GRUB prompt or in the menu.lst configuration file. For example, a menu.lst may contain a section like the following:


# GRUB Boot Menu
hiddenmenu

# By default, boot Windows Vista
default 0
timeout 0

# Boot Windows Vista Home Premium
title Windows Vista Home Premium
unhide (hd0,0)
hide (hd0,1)
loadslic (hd0,5)/boot/grub/slic.bin
chainloader (hd0,0)+1


For old computers with non-SLP BIOS, read this post.

Tuesday, September 29, 2009

GRUB: Booting and Activating Windows Vista/7

This post describes a way to activate Windows Vista/7 with Grub and SLIC loader. In order to successfully activate Windows Vista/7, you essentially need the following 3 things.



  • OEM SLIC code

    Every SLIC code is 374 bytes. It can be extracted from the BIOS by using the SLIC Dump ToolKit on a PC running legitimate Windows.
  • OEM Certificate (*.xrm-ms) that matches with the corresponding SLIC code
  • Valid 25-digit Windows key


Once these 3 things are obtained, you need a program to insert the SLIC code into memory to emulate OEM BIOS. I found an executable file called wow.exe whose job is to insert a SLIC code into BIOS. Also, GRUB.EXE is needed to find Windows partition and to boot Windows.



Because wow.exe and grub.exe are DOS executables, DOS should be booted first and run wow.exe and grub.exe in turn. grub.exe will chain-load Windows bootmgr, and then Windows will start. FreeDOS is preferred to MS-DOS because it works better with GRUB. Download a floppy image containing FreeDOS kernel and FreeCOM command processor below.




Inserting SLIC data into wow.exe with XVI32


wow.exe has a SLIC table embedded in itself beginning at $A58. Start XVI32 and open wow.exe. Start another XVI32 and open a SLIC table which should be 374 bytes.

With the XVI32 window showing a SLIC.table, open the Edit menu, click the beginning of file, select Block mark from the Edit menu, go to the end of file and select Block mark again. Open the Edit menu, open the Clipboard submenu and select Copy.

With the XVI32 window showing wow.exe, go to $A58, open the Edit window, select Block <n> chars..., enter 374 and click OK. Select Block delete from the Edit menu. Then, open the Edit menu, open the Clipboard submenu, and choose Paste. Go to $A38 and select Overwrite string... and enter the new 14-character OEM identifier which should match the string starting at $A62. Save the file wow.exe.


Open the FreeDOS floppy image with WinImage. Insert wow.exe into the floppy image. Create a batch file called AUTOEXEC.BAT with a text editor and put the following contents in it.


@ECHO OFF
WOW
GRUB

Save the file AUTOEXEC.BAT and insert in into FreeDOS floppy image.


Assuming that Windows 7 is installed, open a Command Prompt as administrator. Create an arbitrary file in C:\Boot, for example, with the filename win7. This file will be used to find the Windows partition (useful in mutiple-Windows environment).


copy nul C:\Boot\win7

Then, create a new text file MENU.LST with the following contents:


timeout 0
default 0
fallback 0
hiddenmenu

title Windows 7
find --set-root /boot/win7
chainloader /bootmgr

Save the file as MENU.LST and insert it into the floppy image with WinImage. Save the floppy image and quit WinImage.


With GRUB installed into MBR, type the following GRUB commands at boot time to boot Windows with BIOS-inserted SLIC code. Assume memdisk and freedos.flp is in the (hd0,3) partition.


kernel (hd0,3)/boot/grub/memdisk
initrd (hd0,3)/boot/grub/freedos.flp


Activating Windows


Once Windows has started, find the OEM certificate (*.xrm-ms) and 25-character Windows key that match the SLIC table just loaded into BIOS. Open a command prompt as administrator and type the following commands to activate Windows:


slmgr -ilc oemcert.xrm-ms
slmgr -ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX

Hopefully, Windows will be activated.

Saturday, September 26, 2009

Setting Up Windows Recovery Environment with Bootmgr

After installing Windows 7, I found Winre.wim in C:\Recovery folder. I wanted to create a separate recovery partition and set it up so that it will boot to Windows Recovery Environment. So I created a new primary partition formatted in NTFS. I rebooted with Windows 7 DVD, chose “Repair Your Computer.” and opened a Command Prompt. I typed the following commands:


C:
bootrec /fixboot
copy D:\bootmgr C:\
robocopy D:\Boot C:\Boot /E
robocopy D:\Recovery C:\Recovery /E

D: is the Windows 7 partition. I used bcdedit in the following way to enable Windows Recovery Partition:


bcdedit /create /d "Windows Recovery Environment" /application osloader
bcdedit /set {13800a57...} device ramdisk=[C:]\Recovery\Winre.wim,{ramdiskoptions}
bcdedit /set {13800a57...} osdevice ramdisk=[C:]\Recovery\Winre.wim,{ramdiskoptions}
bcdedit /set {13800a57...} path \Windows\System32\winload.exe
bcdedit /set {13800a57...} systemroot \Windows
bcdedit /set {13800a57...} winpe yes
bcdedit /displayorder {13800a57...} /addlast

Here, the UUID's have been abbreviated. However, Windows Recovery Partition seemed to be loading for a few seconds, then crashed with blue screen.

Creating a Bootable USB Thumb Drive

Most modern computers allow booting from a USB thumb drive. However, creating a bootable USB drive can be a tricky task because different BIOS may assign different hexadecimal designators for a USB drive, depending on computers and situations. For example, my laptop assigns 0x80 to the internal hard drive and 0x81 to a USB drive when the computer boots from the internal hard drive as usual. However, if I choose to boot from my USB thumb drive at the BIOS boot menu after pressing Esc, the BIOS assigns 0x80 to the USB drive and 0x81 to the hard drive.


Booting from external storage, such as a USB drive or SDHC card, allows one to start an operating system externally, freeing oneself from the limitations of an internel hard drive. In this post, I show heterogenous methods to create a bootable USB drive.



Partitioning a USB drive


Most USB thumb drives have only one partition. If you are happy with just one partition, that's fine. But if you want to slice your USB drive into multiple partitions, I recommend you to use GParted. To download and use GParted, get a bootable ISO from the following links and burn it with CD burning software, such as CDBurnerXP.



I partition my USB drive in order to install Linux on it. Usually, I make 2 partitions (one FAT16 partition and one EXT2 partition). Sometimes, I create another partition that I format as Linux swap. Here's my typical partition layout for USB thumb drives.



/dev/sdb1 FAT16 or FAT32
/dev/sdb2 EXT2, EXT3 or JFS
/dev/sdb3 Linux Swap


Installing MBR


Setting up MBR (short for master boot record) correctly is crucial in creating a bootable USB drive. In this post, I use the Debian/Ubuntu mbr package to set up MBR on my USB thumb drive.


Use the install-mbr command from the mbr package to install a functional master boot record.


install-mbr /dev/sdc -v --drive 0x80 --enable +12


Making FAT partitions bootable



FreeDOS kernel and FreeCOM


Originally, my 256MB USB thumb drive has only one primary partition formatted in FAT16. Since it's not feasible to install Windows on USB thumb drives, I installed a FreeDOS kernel on my USB thumb drive. I downloaded FreeDOS kernel and FreeCom command shell. I booted FreeDOS using a FreeDOS floppy. Then, I installed kernel.sys and command.com just to have a bootable DOS.


sys D /update /oem:fd
copy command.com D:

Restart your computer with the USB thumb drive still plugged in. Press the key for displaying the BIOS boot menu. This key may be Esc or F12. Select your USB drive from the menu. Hopefully, you'll reach the FreeDOS command prompt.



Syslinux


Syslinux can be installed on either FAT or EXT2 partition. Syslinux provides a versatile multi-boot functionality, comparable to GRUB. In Linux, I used the following command to make the FAT partition bootable with syslinux.


syslinux /dev/sdb1

This command copies ldlinux.sys to the FAT partition.

Monday, September 21, 2009

To Create Windows Vista/7 Boot Floppy

Windows Vista and Windows 7 are overly complicated pieces of software, and thus bring a bunch of boot problems with them. In preparation for such problems, it is wise to create a boot floppy for Windows Vista or Windows 7. Floppy drives are hard to find in new computers, and are being phased out in favor of USB thumb drives. If your computer doesn't have a built-in floppy drive, you can get a USB floppy drive and use it to create a boot floppy. If you don't have a floppy drive, download a floppy image from the links below and use it with a bootloader, such as GRUB.


To create a Windows Vista/7 boot floppy, follow these steps:


  1. Insert a blank floppy diskette into your floppy drive. If your floppy drive is a USB type, connect it to your computer.
  2. Click Computer in the Start menu. This opens Computer in Windows Explorer.
  3. Right-click on Floppy Disk Drive (A:) and choose Format... from the menu.
  4. Click Start in the Format window.
  5. Copy C:\bootmgr to A:\. The file bootmgr is also located on Windows Vista/7 installer CD. You may have to unhide bootmgr before copying it if it is hidden.
  6. Create a new folder A:\BOOT.
  7. Open a Command Prompt and run the following commands to copy C:\BOOT\BCD.

    bcdedit /export A:\BOOT\BCD

  8. Reboot your computer with the boot floppy. You'll be able to get back to Windows Vista/7.


For those who can't create their own boot floppy for lack of a floppy drive, here's my boot floppy image:



You can use it with GRUB like this:


kernel (hd0,4)/boot/grub/memdisk bigraw
initrd (hd0,4)/boot/grub/vstaboot.bin
boot

memdisk is a part of Loadlin package. Note that this image is specific to my Vista installation. You must edit it with bcdedit before using it:


bcdedit /enum /store A:\Boot\BCD
bcdedit /store A:\Boot\BCD /set {bootmgr} device partition=C:
bcdedit /store A:\Boot\BCD /set {default} device partition=C:
bcdedit /store A:\Boot\BCD /set {default} osdevice partition=C:

About This Blog

KBlog logo This blog is about current events and issues concerning general population. Thanks for visiting the blog and posting your comments.

© Contents by KBlog

© Blogger template by Emporium Digital 2008

Followers

Total Pageviews

icon
Powered By Blogger