Difference between revisions of "Windows PE"

From ThinkWiki
Jump to: navigation, search
m (How to build a bootable WinPE USB-FDD image)
(rewrite using kexec-loader)
Line 93: Line 93:
 
Any BIOS that supports booting from some USB media at all supports booting from USB-FDD. As it was mentioned ealier, the problem with WinPE is that the bootloader <tt>bootmgr</tt> does not seem to like such devices. A natural thing to do would be to create a virtual HDD, e.g. with <tt>memdisk</tt>, and boot from there. But here we face a deficiency of <tt>memdisk</tt>: for some BIOSes (including Thinkpads!) it will boot if the disk-image physically resides on a real HDD, but will hang if the image is on a USB media.
 
Any BIOS that supports booting from some USB media at all supports booting from USB-FDD. As it was mentioned ealier, the problem with WinPE is that the bootloader <tt>bootmgr</tt> does not seem to like such devices. A natural thing to do would be to create a virtual HDD, e.g. with <tt>memdisk</tt>, and boot from there. But here we face a deficiency of <tt>memdisk</tt>: for some BIOSes (including Thinkpads!) it will boot if the disk-image physically resides on a real HDD, but will hang if the image is on a USB media.
  
The only workaround I've found for making a bootable WinPE USB-FDD looks awkward, but it works! Ironically, to boot Windows we will have to use Linux, namely the '''kexec''' feature of the Linux kernel.
+
The only workaround I've found for making a bootable WinPE USB-FDD looks awkward, but it works! Ironically, to boot Windows we will have to use Linux, namely the '''kexec''' feature of the Linux kernel. The bootloader [http://www.solemnwarning.net/kexec-loader kexec-loader] does exactly that: it is a stripped down Linux kernel which can detect block devices, mount them and execute <tt>kexec</tt>.
  
* get <tt>kexec</tt> binary compiled for i386 (for example, you can extract it from the Debian package [http://packages.debian.org/testing/i386/kexec-tools/download kexec-tools])
+
* get [http://www.solemnwarning.net/kexec-loader/downloads/kexec-loader-2.2.1-floppy.img.gz kexec-loader-floppy], [http://www.solemnwarning.net/kexec-loader/downloads/modules-2.6.30.5-1.tar.gz modules] and the [http://www.solemnwarning.net/kexec-loader/downloads/kexec-loader-2.2.1.tar.gz sources].
* get <tt>grub.exe</tt> from [http://sourceforge.net/projects/grub4dos/files/ grub4dos]
+
* insert your flash media into USB (I will assume it is detected as <tt>/dev/sdb</tt>) and format it
* download some Linux "liveCD" capable of booting from USB-FDD (I personally like [http://www.sysresccd.org/Download SystemRescueCd]) and make a bootable USB-FDD
+
mke2fs -t ext2 -L kexecloader /dev/sdb
* mount your Linux USB-FDD to <tt>/mnt/flash</tt> and create the directory <tt>/mnt/flash/winpe</tt>
+
* mount it to <tt>/mnt/flash</tt> and install '''extlinux'''
* copy the files
+
mount /dev/sdb /mnt/flash
  cp grub.exe /mnt/flash/winpe/
+
mkdir /mnt/flash/extlinux
  cp kexec /mnt/flash/winpe/
+
extlinux -i /mnt/flash/extlinux
  cp /tmp/winpe3_x86p1.img /mnt/flash/winpe/winpe.img
+
* mount <tt>kexec-loader-floppy.img</tt> to <tt>/mnt/floppy</tt> and copy its contents into /mnt/flash
the last file you've created when building the bootable USB-HDD image. Note that this is the image of the partition, not of the whole HD.
+
mount -o loop kexec-loader-floppy.img /mnt/floppy
* create the script <tt>/mnt/flash/winpe/winpe.sh</tt>
+
cp -r /mnt/floppy/* /mnt/flash
  #!/bin/sh
+
* configure extlinux
  ./kexec -l grub.exe --initrd=winpe.img --command-line="--config-file=map (rd) (fd0); map --hook; chainloader (fd0)+1; rootnoverify (fd0)"
+
  mv /mnt/flash/syslinux/syslinux.cfg /mnt/flash/extlinux/extlinux.conf
  ./kexec -e
+
rm -fr /mnt/flash/syslinux
* unmount <tt>/mnt/flash</tt>, the image is ready
+
* download [http://sourceforge.net/projects/grub4dos/files/ grub4dos] and copy <tt>grub.exe</tt>
* now, you may first need to change some of your BIOS settings. For me, <tt>grub.exe</tt> refuses to boot if the harddrive controller is in AHCI mode, so I have to put it into "Compatibility mode". Boot Linux from your USB-FDD as usual, but make sure you don't use any KMS video capabilities of modern kernels -- <tt>grub.exe</tt> doesn't seem to like them as well. Once you are in the root shell, you can start WinPE
+
  cp grub.exe /mnt/flash/
  # cp /mnt/flash/winpe/* /tmp/
+
* copy WinPE image
  # cd /tmp
+
  cp /tmp/winpe3_x86p1.img /mnt/flash/winpe.img
  # chmod 755 kexec winpe.sh
+
Note that this is the image of the partition, not of the whole HD.
  # ./winpe.sh
+
* figure out UUID of your USB-flash
 +
blkid /dev/sdb
 +
 
 +
/dev/sdb: LABEL="kexecloader" UUID="'''c09fadee-4262-4b48-bf33-cf059b53467d'''" TYPE="ext2"
 +
* configure kexec-loader by replacing <tt>/mnt/flash/kexec-loader.conf</tt> with
 +
 
 +
timeout 1
 +
   
 +
  title WinPE
 +
root UUID='''c09fadee-4262-4b48-bf33-cf059b53467d'''
 +
kernel /grub.exe
 +
cmdline --config-file="map (rd) (fd0); map --hook; chainloader (fd0)+1; rootnoverify (fd0)"
 +
  initrd /winpe.img
 +
* unpack kexec-loader modules into <tt>/tmp/modules</tt> and sources into <tt>/tmp/kexec-loader</tt>
 +
* copy initrd.img into <tt>/tmp/modules</tt>, add necessary drivers and copy it back
 +
  cp /mnt/flash/initrd.img /tmp/modules
 +
  cd /tmp/modules
 +
  /tmp/kexec-loader/addmod.sh initrd.img memstick.tlz mmc-sd.tlz fs/ext2.tlz
 +
  cp initrd.img /mnt/flash/initrd.img
 +
Make sure that you DO NOT include any SATA-drivers, or <tt>grub.exe</tt> will refuse to boot (or you will have to tweak BIOS and switch harddrive controller from AHCI mode into "Compatibility mode").
 +
* now you can unmount <tt>/mnt/flash</tt>, the image is ready. When booting -- be patient. Reading speed of USB media is slow.

Revision as of 09:32, 7 April 2010

Some utilities/drivers provided by IBM/Lenovo come only in the form of Windows executables (for example, Intel AMT firmware updates). And for people who don't use Windows OS on their computers it becomes impossible to use/apply them. Luckily, Microsoft provides Automated Installation Kit (aka AIK) for free to everyone with very few resctictions on usage (basically, they only prohibit using it as a substitute of a "real" OS, and allow to use it for any diagnostic and reapair tasks). The latest version is The Windows® Automated Installation Kit (AIK) for Windows® 7. Users of Windows OS can install this AIK and create bootable CD-ROMs and bootable USB-flash drives with Windows PE (or WinPE for short), which is essentially a stripped-down version of Windows. In this article we will explain how to create bootable CD-ROMs and USB-flash drives with WinPE using only free software.

How to build a bootable WinPE *.iso image

  • From the AIK installation file KB3AIK_EN.iso, provided in the form of a UDF disk image, use 7z (from p7zip) to extract the files wAIKX86.msi and WinPE.cab
  • use 7z or cabextract to unpack these files into /tmp/wAIKX86.msi/ and /tmp/WinPE.cab/ respectively
  • create a bootable WinPE *.iso image /tmp/winpe3_x86.iso
cd /tmp
mkdir -p winpe3_x86/boot
mkdir -p winpe3_x86/sources
cp wAIKX86.msi/F_WINPE_X86_etfsboot.com winpe3_x86/etfsboot.com
cp wAIKX86.msi/F1_BOOTMGR winpe3_x86/bootmgr
cp wAIKX86.msi/F_WINPE_X86_bcd winpe3_x86/boot/bcd
cp wAIKX86.msi/F_WINPE_X86_boot.sdi winpe3_x86/boot/boot.sdi
cp WinPE.cab/F1_WINPE.WIM winpe3_x86/sources/boot.wim
genisoimage -sysid "" -A "" -V "Microsoft Windows PE (x86)" -d -N -b etfsboot.com -no-emul-boot \
 -c boot.cat -hide etfsboot.com -hide boot.cat -o winpe3_x86.iso winpe3_x86

the file you will get will be about 120M in size. Then you can burn this *.iso and boot it on any x86-machine which supports booting from CD-ROMs (which is pretty much any PC today)

How to build a bootable WinPE USB-HDD image

a more convenient option would be to create a bootable USB-flash drive. Unfortunately, Windows loader does not seem to support booting from USB-FDDs, while for some BIOSes this is the only type of bootable USB-flash devices.

Building WinPE USB-HDD image in a virtual machine

Since we already have a bootable WinPE *.iso image, we can use it to build a bootable WinPE USB-HDD image in a virtual machine like qemu.

  • prepare a blank
dd if=/dev/zero of=winpe3_x86.img count=250000
  • boot winpe3_x86.iso (which you've created before) in a virtual machine
qemu -cdrom winpe3_x86.iso -boot d -m 640 -hda winpe3_x86.img
  • now, in the shell provided by WinPE in the virtual machine
diskpart.exe
diskpart> list disk
diskpart> select disk 0
diskpart> clean
diskpart> create partition primary
diskpart> list partition
diskpart> select partition 1
diskpart> format fs=fat32 quick
diskpart> active
diskpart> assign
diskpart> list volume
diskpart> exit
xcopy /s d:\* c:\
wpeutil shutdown
  • when the virtual machine shuts down, the bootable WinPE image is ready. If you have write permissions for some flash media device (e.g. /dev/sdb), you can copy it with
# dd if=winpe3_x86.img of=/dev/sdb

and use it for test/diagnostic tasks such as firmware upgrades. If you want, you can create an additional partition for you firmware/diagnostic tools, just don't mess with the partition created by Windows -- you may reder it unbootable.

Building WinPE USB-HDD image with syslinux

if for some ethical or religious reasons you do not want to run any Windows code even in a virtual machine, or you don't have a virtual machine at all -- you can build a bootable WinPE USB-HDD image using syslinux and mtools.

  • prepare a blank
dd if=/dev/zero of=winpe3_x86.img count=250000
  • create a bootable partition entry
/sbin/parted winpe3_x86.img
(parted) mklabel msdos
(parted) unit s
(parted) print free
(parted) mkpart primary fat32
(parted) set 1 boot on
(parted) print

in my case the output looks like this. Note the numbers corresponding to "Start" and "Size" of your partition, we will use them in the next command

Model:  (file)
Disk /tmp/winpe3_x86.img: 250000s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start  End      Size     Type     File system  Flags
 1      32s    249999s  249968s  primary               boot, lba
  • create and format the partition image
dd if=/dev/zero of=winpe3_x86p1.img count=249968
/sbin/mkfs.vfat -F32 winpe3_x86p1.img
  • copy the files (you may have to put "mtools_skip_check=1" into ~/.mtoolsrc if your partition is not aligned like mine)
mmd -i winpe3_x86p1.img boot
mmd -i winpe3_x86p1.img sources
mcopy -i winpe3_x86p1.img /tmp/wAIKX86.msi/F1_BOOTMGR ::/bootmgr
mcopy -i winpe3_x86p1.img /tmp/wAIKX86.msi/F_WINPE_X86_bcd ::/boot/bcd
mcopy -i winpe3_x86p1.img /tmp/wAIKX86.msi/F_WINPE_X86_boot.sdi ::/boot/boot.sdi
mcopy -i winpe3_x86p1.img /tmp/WinPE.cab/F1_WINPE.WIM ::/sources/boot.wim
mcopy -i winpe3_x86p1.img /usr/lib/syslinux/chain.c32 ::/chain.c32
  • create the config file /tmp/syslinux.cfg and copy it
DEFAULT WinPE
LABEL WinPE
        COM32 /chain.c32
        APPEND boot ntldr=/bootmgr
mcopy -i winpe3_x86p1.img /tmp/syslinux.cfg ::/syslinux.cfg
  • install syslinux and copy the partition image into the disk image
syslinux winpe3_x86p1.img
dd if=winpe3_x86p1.img of=winpe3_x86.img seek=32
  • now the image is ready. If you have write permissions for some flash media device (e.g. /dev/sdb), you can copy it with
# dd if=winpe3_x86.img of=/dev/sdb

and use it for test/diagnostic tasks such as firmware upgrades.

How to build a bootable WinPE USB-FDD image

Any BIOS that supports booting from some USB media at all supports booting from USB-FDD. As it was mentioned ealier, the problem with WinPE is that the bootloader bootmgr does not seem to like such devices. A natural thing to do would be to create a virtual HDD, e.g. with memdisk, and boot from there. But here we face a deficiency of memdisk: for some BIOSes (including Thinkpads!) it will boot if the disk-image physically resides on a real HDD, but will hang if the image is on a USB media.

The only workaround I've found for making a bootable WinPE USB-FDD looks awkward, but it works! Ironically, to boot Windows we will have to use Linux, namely the kexec feature of the Linux kernel. The bootloader kexec-loader does exactly that: it is a stripped down Linux kernel which can detect block devices, mount them and execute kexec.

mke2fs -t ext2 -L kexecloader /dev/sdb
  • mount it to /mnt/flash and install extlinux
mount /dev/sdb /mnt/flash
mkdir /mnt/flash/extlinux
extlinux -i /mnt/flash/extlinux
  • mount kexec-loader-floppy.img to /mnt/floppy and copy its contents into /mnt/flash
mount -o loop kexec-loader-floppy.img /mnt/floppy
cp -r /mnt/floppy/* /mnt/flash
  • configure extlinux
mv /mnt/flash/syslinux/syslinux.cfg /mnt/flash/extlinux/extlinux.conf
rm -fr /mnt/flash/syslinux
cp grub.exe /mnt/flash/
  • copy WinPE image
cp /tmp/winpe3_x86p1.img /mnt/flash/winpe.img

Note that this is the image of the partition, not of the whole HD.

  • figure out UUID of your USB-flash
blkid /dev/sdb
/dev/sdb: LABEL="kexecloader" UUID="c09fadee-4262-4b48-bf33-cf059b53467d" TYPE="ext2" 
  • configure kexec-loader by replacing /mnt/flash/kexec-loader.conf with
timeout 1

title WinPE
root UUID=c09fadee-4262-4b48-bf33-cf059b53467d
kernel /grub.exe
cmdline --config-file="map (rd) (fd0); map --hook; chainloader (fd0)+1; rootnoverify (fd0)"
initrd /winpe.img
  • unpack kexec-loader modules into /tmp/modules and sources into /tmp/kexec-loader
  • copy initrd.img into /tmp/modules, add necessary drivers and copy it back
cp /mnt/flash/initrd.img /tmp/modules
cd /tmp/modules
/tmp/kexec-loader/addmod.sh initrd.img memstick.tlz mmc-sd.tlz fs/ext2.tlz
cp initrd.img /mnt/flash/initrd.img

Make sure that you DO NOT include any SATA-drivers, or grub.exe will refuse to boot (or you will have to tweak BIOS and switch harddrive controller from AHCI mode into "Compatibility mode").

  • now you can unmount /mnt/flash, the image is ready. When booting -- be patient. Reading speed of USB media is slow.